source: trunk/scripts/pv-fixup/pv-fixup @ 2374

Last change on this file since 2374 was 2321, checked in by price, 15 years ago

pv-fixup, lvmanip: last tweaks

This is from the very beginning of April, at the close of the LVM saga.
Somehow I never pushed.

File size: 3.9 KB
Line 
1#!/bin/sh
2exit #Not actually a script.
3
4VG=xenvg
5DISK=/dev/mapper/36090a028407d6e2b2589a45cdb971489
6HALFSIZETiB=5.23
7TMPDM=tmpfoo
8TMPDEV=/dev/mapper/$TMPDM
9TMPDM2=tmpbar
10TMPDEV2=/dev/mapper/$TMPDM2
11METADATASIZE=8128k
12
13#  - pvresize the old PV to half the space
14pvresize --setphysicalvolumesize=${HALFSIZETiB}t $DISK
15
16#  - dmsetup create a device in the latter half
17dmstart=$(python -c "print int(($HALFSIZETiB + 0.01) * 1024 * 1024 * 1024 * 2)")
18dmlen=$(python -c "print int(($HALFSIZETiB - 0.03) * 1024 * 1024 * 1024 * 2)")
19#Foreach host:
20dmsetup create $TMPDM --table "0 $dmlen linear $DISK $dmstart"
21#done
22
23#  - pvcreate a temporary PV on the new device
24pvcreate $TMPDEV
25
26#  - vgextend with the temporary PV
27vgextend $VG $TMPDEV
28
29#  - pvmove all the LVs to the temporary PV
30
31yamlkeys () {
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
38vmstolvs () {
39  perl -pe 's/^/d_/;s/$/_hda/'
40}
41
42totalsize () {
43  # handy for estimating time; output in GiB; pipe in a list of LVs
44  perl -pe 's.^.xenvg/.' \
45   | xargs lvs -o size --units=g \
46   | perl -lne '$total += $_; END { print $total; }'
47}
48
49lvsleft () {
50  lvs -o name,devices $VG \
51    | perl -lane "print \$F[0] if (\$F[1] =~ m|$DISK|)"
52}
53
54movelv () {
55  echo pvmoving $VG/$1...
56  pvmove -i 10 -n $VG/$1 $2
57}
58
59ssh root@xvm remctl remote web listvms \
60 | yamlkeys \
61 > runningvms
62LVM_SYSTEM_DIR=/root/lvm lvsleft \
63 | grep '^d_.*_hda$' \
64 | grep -v -xf <(vmstolvs <runningvms) \
65 > offvmlvs
66for lv in $(offvmlvs); do
67  LVM_SYSTEM_DIR=/root/lvm movelv $lv $DISK
68done
69#FOREACH host:
70for 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
73done
74#done
75
76ssh root@xvm remctl remote web listvms \
77 | perl -lne 'print if (s/^  cdrom: //)' \
78 > usedcdroms
79LVM_SYSTEM_DIR=/root/lvm lvsleft \
80 | grep ^image_ \
81 | grep -v -xf <(perl -pe "s|/dev/$VG/||" usedcdroms) \
82 > offcdlvs
83for lv in $(offcdlvs); do
84  LVM_SYSTEM_DIR=/root/lvm movelv $lv $DISK
85done
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
95LVM_SYSTEM_DIR=lvm vgreduce $VG $DISK
96dmsetup create $TMPDM2 --table "0 $((2 * 1024 * 20)) linear $DISK 0"
97pvremove $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)
102shortsize=$(python -c "print $HALFSIZETiB - 0.01")t
103pvcreate $TMPDEV2 --setphysicalvolumesize $shortsize --metadatasize $METADATASIZE
104dmsetup remove $TMPDM2
105pvscan
106LVM_SYSTEM_DIR=lvm vgextend $VG $DISK
107
108#  - pvmove all the LVs back
109
110# Doing this part with CLVM again.
111
112lvsleft () {
113  lvs -o name,devices $VG \
114    | perl -lane "print \$F[0] if (\$F[1] =~ m|$TMPDEV|)"
115}
116
117movelv () {
118  echo pvmoving $VG/$1...
119  lvchange -an $VG/$1
120  pvmove -i 10 -n $VG/$1 $2
121  lvchange -ay $VG/$1
122}
123
124movestuff () {
125  date
126  while read lv; do
127    echo MOVING: $lv >>/var/log/lvm2.log
128    movelv $lv $TMPDEV
129    date
130  done
131}
132
133moveall () {
134  date
135  for lv in $(lvsleft); do
136    echo MOVING: $lv >>/var/log/lvm2.log
137    movelv $lv $TMPDEV
138    date
139  done
140}
141
142#FOREACH host:
143#set logging in /etc/lvm/lvm.conf
144while sleep 1; do
145  echo MARK: $(date) >>/var/log/lvm2.log
146done &
147lvsleft | grep -xf <(invirt-listvms | yamlkeys | vmstolvs) \
148 | movestuff >>/root/lvm/movelv.log 2>&1
149moveall >>/root/lvm/movelv.log 2>&1
150while true; do
151  kinit -k -45
152  sh -x status.sh 2>&1 | zwrite -c xvm-auto -i pvmove -O auto
153  sleep 1800
154done &
155#done
156
157#suffix=.return-1; mv /var/log/lvm2.log lvm/lvm2.$suffix.log; mv lvm/movelv{,.$suffix}.log
158
159# hopefully empty:
160lvs $VG -o lv_name,devices | grep $TMPDEV
161# if not, do some more movelv
162
163#  - pvremove the temporary PV
164vgreduce $VG $TMPDEV
165pvremove $TMPDEV
166#FOREACH host:
167dmsetup remove $TMPDM
168#done
169
170#  - pvresize the new PV to use the whole space
171pvresize $DISK
Note: See TracBrowser for help on using the repository browser.