added -A for ACLs in SELinux
[sysadmin-cookbook] / recepies / ganeti / restore-to-zfs-vol.sh
1 #!/bin/sh -e 
2
3 pool=`zfs list -H -o name | head -1` # use first pool as target for zvol
4 ganeti_export=/var/lib/ganeti/export
5
6 size=60G
7 vol_opt="-b 4k -s"
8 #vol_opt="-b 4k -s -o compression=lz4"
9
10 if [ -z "$1" ] ; then
11         zfs list -r $pool/block
12 #       echo
13 #       test ! -z "/mnt/*" && df -h /mnt/* | grep -v aufs
14         echo
15         du -hcs $ganeti_export/* | cut -d/ -f1,6 | sed 's,/,,'
16         exit 1
17 else
18         instance=$1
19 fi
20
21 umount /mnt/$instance || true
22 zfs destroy $pool/block/$instance || true
23
24 zfs create -V $size $vol_opt $pool/block/$instance
25
26 test -d /mnt/$instance || mkdir /mnt/$instance
27
28 wait_for() {
29         echo -n "waiting for $1 " 
30         while [ ! -e $1 ] ; do
31                 echo -n .
32                 sleep 1
33         done
34         echo " found"
35 }
36
37 wait_for /dev/zvol/$pool/block/$instance
38
39 label=`echo $instance | cut -d. -f1`
40 echo "label $label = $instance"
41
42 time mkfs.ext4 -m 0 -L $label /dev/zvol/$pool/block/$instance 
43
44 wait_for /dev/disk/by-label/$label
45
46 mount /dev/disk/by-label/$label /mnt/$instance/
47 cd /mnt/$instance/
48 dump=`ls $ganeti_export/$instance/*.disk0*.snap`
49 ls -alh $dump
50 zpool iostat 2 | tee /dev/shm/$instance &
51 pid_iostat=$!
52 ( while true; do zfs list $pool/block/$instance ; sleep 10 ; done ) &
53 pid_list=$! 
54 time restore rf $dump
55 kill $pid_iostat
56 kill $pid_list
57
58 wc -l /dev/shm/$instance
59 df -h /mnt/$instance