chiark / gitweb /
Backup: Honour CHIARK_BACKUP_SHAREDIR in snap/lvm script.
[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 daft_sleep () {
54         sleep 5
55 }
56
57 lvmdropcore () {
58         snmnt="$vardir/snap-mount"
59         daft_sleep
60         umount -v "$snmnt" || true
61         daft_sleep
62         test ! -d "$snmnt" || rmdir -- "$snmnt" || rm -f "$snmnt"
63         daft_sleep
64
65         set +e
66         old_lv_dev="$(readlink $vardir/snap-device)"
67         rc=$?
68         set -e
69
70         if [ $rc = 0 ]; then
71                 set +e
72                 daft_sleep
73                 lvremove -f $lvm_lvtools_opts      $old_lv_dev
74                 daft_sleep
75                 set -e
76                 rm $vardir/snap-device
77         fi
78 }
79
80 lvmextentscore1 () {
81         # vgroup must be set
82         vgdisplay_out="$(really vgdisplay -c "$vgroup")"
83         extents="$(printf "%s" "$vgdisplay_out" | awk -F: '{print $16}')"
84         extsize="$(printf "%s" "$vgdisplay_out" | awk -F: '{print $13}')"
85 }
86
87 lvmextentscore2 () {
88         if [ $extents2 -lt $extents ]; then extents=$extents2; fi
89         lvm_lvsize_opts="-l $extents"
90 }
91
92 lvmcreatecore1 () {
93         # vgroup must be set
94         lvpath="/dev/$vgroup/$lvm_lv"
95         ln -s -- "$lvpath" "$vardir"/snap-device
96         sync
97 }