chiark / gitweb /
do not placate -Wpointer-sign
[chiark-utils.git] / backup / snap-common
1 # sourced by snap/lvm and snap/remountrocp
2
3 #---------- common arg parsing
4
5 nargs=$#
6 opmode="$1"
7 vardir="$2"
8 device="$3"
9 mountpoint="$4"
10
11 lvm_lv=chiark-backup
12 lvm_lvtools_opts='-A n'
13 lvm_lvcreate_opts=
14 lvm_lvcreate_args=
15
16 test ! -f /etc/chiark-backup/settings.sh || . /etc/chiark-backup/settings.sh
17
18 case "$nargs.$opmode" in
19 4.snap|2.drop)
20         ;;
21 *)
22         cat >&2 <<END
23 usage: .../$snapkind snap VARDIR DEV MOUNT
24        .../$snapkind drop VARDIR
25 END
26         exit 1
27         ;;
28 esac
29
30 #---------- common functions
31
32 lvmunmapperdevice () {
33         # turns device=/dev/mapper/... into /dev/<group>/<volume>
34         case "$device" in
35         /dev/mapper/*)
36                 device="`printf '%s' "$device" | perl -pe '
37                         s,^/dev/mapper/,,;
38                         die if m,/,;
39                         s,\-\-,!,g;
40                         s,\-,/,g;
41                         s,\!,-,g;
42                         s,^,/dev/,;
43                 '`"
44                 ;;
45         esac
46 }
47
48 lvmdevice2vgroup () {
49         vgroup="${device#/dev/}"
50         vgroup="${vgroup%/*}"
51 }
52
53 lvmdropcore () {
54         snmnt="$vardir/snap-mount"
55         umount -v "$snmnt" || true
56         test ! -d "$snmnt" || rmdir -- "$snmnt" || rm -f "$snmnt"
57
58         set +e
59         old_lv_dev="$(readlink $vardir/snap-device)"
60         rc=$?
61         set -e
62
63         if [ $rc = 0 ]; then
64                 set +e
65                 lvchange    $lvm_lvtools_opts -a n $old_lv_dev
66                 lvremove -f $lvm_lvtools_opts      $old_lv_dev
67                 set -e
68                 rm $vardir/snap-device
69         fi
70 }
71
72 lvmextentscore1 () {
73         # vgroup must be set
74         vgdisplay_out="$(really vgdisplay -c "$vgroup")"
75         extents="$(printf "%s" "$vgdisplay_out" | awk -F: '{print $16}')"
76         extsize="$(printf "%s" "$vgdisplay_out" | awk -F: '{print $13}')"
77 }
78
79 lvmextentscore2 () {
80         if [ $extents2 -lt $extents ]; then extents=$extents2; fi
81         lvm_lvsize_opts="-l $extents"
82 }
83
84 lvmcreatecore1 () {
85         # vgroup must be set
86         lvpath="/dev/$vgroup/$lvm_lv"
87         ln -s -- "$lvpath" "$vardir"/snap-device
88         sync
89 }