pv-fixup: more metadata, align PEs, and tweaks
[invirt/scripts/pv-fixup.git] / pv-fixup
1 #!/bin/sh
2 exit 1  #Not actually a script.
3
4 VG=xenvg
5 DISK=/dev/mapper/36090a028407d6e2b2589a45cdb971489
6 HALFSIZETiB=5.23
7 TMPDM=tmpfoo
8 TMPDEV=/dev/mapper/$TMPDM
9 TMPDM2=tmpbar
10 TMPDEV2=/dev/mapper/$TMPDM2
11 METADATASIZE=8128k
12
13 #  - pvresize the old PV to half the space
14 pvresize --setphysicalvolumesize=${HALFSIZETiB}t $DISK
15
16 #  - dmsetup create a device in the latter half
17 dmstart=$(python -c "print int(($HALFSIZETiB + 0.01) * 1024 * 1024 * 1024 * 2)")
18 dmlen=$(python -c "print int(($HALFSIZETiB - 0.03) * 1024 * 1024 * 1024 * 2)")
19 #Foreach host:
20 dmsetup create $TMPDM --table "0 $dmlen linear $DISK $dmstart"
21 #done
22
23 #  - pvcreate a temporary PV on the new device
24 pvcreate $TMPDEV
25
26 #  - vgextend with the temporary PV
27 vgextend $VG $TMPDEV
28
29 #  - pvmove all the LVs to the temporary PV
30
31 yamlkeys () {
32   python -c 'if 1:
33     import yaml, sys
34     mm = yaml.load(sys.stdin.read())
35     for m in sorted(mm.iterkeys()): print m'
36 }
37
38 lvsleft () {
39   lvs -o name,devices xenvg \
40     | perl -lane "print \$F[0] if (\$F[1] =~ m|$DISK|)"
41 }
42
43 vmstolvs () {
44   perl -pe 's/^/d_/;s/$/_hda/'
45 }
46
47 totalsize () {
48   # handy for estimating time; output in GiB; pipe in a list of LVs
49   perl -pe 's.^.xenvg/.' \
50    | xargs lvs -o size --units=g \
51    | perl -lne '$total += $_; END { print $total; }'
52 }
53
54 movelv () {
55   echo pvmoving $VG/$1...
56   pvmove -i 10 -n $VG/$1 $2
57 }
58
59 ssh root@xvm remctl remote web listvms \
60  | yamlkeys \
61  > runningvms
62 LVM_SYSTEM_DIR=/root/lvm lvsleft \
63  | grep '^d_.*_hda$' \
64  | grep -v -xf <(vmstolvs <runningvms) \
65  > offvmlvs
66 for lv in $(offvmlvs); do
67   LVM_SYSTEM_DIR=/root/lvm movelv $lv $DISK
68 done
69 #FOREACH host:
70 for lv in $(invirt-listvms | yamlkeys | vmstolvs \
71              | grep -xf <(LVM_SYSTEM_DIR=/root/lvm lvsleft)); do
72   LVM_SYSTEM_DIR=/root/lvm movelv $lv $DISK
73 done
74 #done
75
76 ssh root@xvm remctl remote web listvms \
77  | perl -lne 'print if (s/^  cdrom: //)' \
78  > usedcdroms
79 LVM_SYSTEM_DIR=/root/lvm lvsleft \
80  | grep ^image_ \
81  | grep -v -xf <(perl -pe "s|/dev/$VG/||" usedcdroms) \
82  > offcdlvs
83 for lv in $(offcdlvs); do
84   LVM_SYSTEM_DIR=/root/lvm movelv $lv $DISK
85 done
86
87 # deal with $(cat usedcdroms)
88 # deal with s_* LVs
89 # deal with any remaining $(lvsleft)
90
91 [ -z $(lvsleft) ]
92
93
94 #  - pvremove the old PV
95 LVM_SYSTEM_DIR=lvm vgreduce $VG $DISK
96 dmsetup create $TMPDM2 --table "0 $((2 * 1024 * 20)) linear $DISK 0"
97 pvremove $TMPDEV2
98
99 #  - pvcreate a new PV in the old space,
100 #     with --setphysicalvolumesize <half size>
101 #     and with --metadatasize <huge>    (the point of this exercise)
102 shortsize=$(python -c "print $HALFSIZETiB - 0.01")t
103 pvcreate $TMPDEV2 --setphysicalvolumesize $shortsize --metadatasize $METADATASIZE
104 dmsetup remove $TMPDM2
105 pvscan
106 LVM_SYSTEM_DIR=lvm vgextend $VG $DISK
107
108 #  - pvmove all the LVs back
109
110 #FOREACH host:
111 for lv in $(lvs -o lv_name --noheadings $VG); do
112   movelv $VG/$lv $TMPDEV
113 done
114 #done
115
116 # hopefully empty:
117 lvs $VG -o lv_name,devices | grep $TMPDEV
118 # if not, do some more movelv
119
120 #  - pvremove the temporary PV
121 vgreduce $VG $TMPDEV
122 pvremove $TMPDEV
123 dmsetup remove $TMPDM
124
125 #  - pvresize the new PV to use the whole space
126 pvresize $DISK