--- /dev/null
+#!/bin/sh
+exit 1 #Not actually a script.
+
+VG=xenvg
+DISK=/dev/sdb
+HALFSIZETiB=2.04
+TMPDM=tmpfoo
+TMPDEV=/dev/mapper/$TMPDM
+TMPDM2=tmpbar
+TMPDEV2=/dev/mapper/$TMPDM2
+METADATASIZE=3.5m
+
+# - pvresize the old PV to half the space
+pvresize --setphysicalvolumesize=${HALFSIZETiB}t $DISK
+
+# - dmsetup create a device in the latter half
+dmstart=$(python -c "print int(($HALFSIZETiB + 0.01) * 1024 * 1024 * 1024 * 2)")
+dmlen=$(python -c "print int(($HALFSIZETiB - 0.02) * 1024 * 1024 * 1024 * 2)")
+#foreach host:
+dmsetup create $TMPDM --table "0 $dmlen linear $DISK $dmstart"
+#done
+
+# - pvcreate a temporary PV on the new device
+pvcreate $TMPDEV
+
+# - vgextend with the temporary PV
+vgextend $VG $TMPDEV
+
+# - pvmove all the LVs to the temporary PV
+# (something like
+# for lv in $(LVs for running VMs on this host); do
+# #some lvchange -a commands
+# pvmove -n $lv <oldpv> <newpv>
+# #possibly more lvchange -a to restore old state
+# done)
+
+movelv () {
+ echo pvmoving $1...
+ lvchange -an $1
+ pvmove -i 10 -n $1 $2
+}
+
+#touch /etc/invirt/nocreate
+#FOREACH host:
+#machines=$(invirt-listvms | perl -lne 'print if s/^([^ ]*):.*/$1/')
+#lvs=$(echo $machines | perl -lpe "s|^|$VG/d_|; s|$|_hda|")
+for lv in $(lvs -o lv_name --noheadings $VG); do
+ movelv $VG/$lv $DISK
+done
+#done
+
+# hopefully empty:
+lvs $VG -o lv_name,devices | grep $DISK
+# if not, do some more movelv
+
+# This is possible if some LVs were used on more than one host during
+# migration.
+
+
+# - pvremove the old PV
+vgreduce $VG $DISK
+pvremove $DISK
+
+# - pvcreate a new PV in the old space,
+# with --setphysicalvolumesize <half size>
+# and with --metadatasize <huge> (the point of this exercise)
+shortsize=$(python -c "print $HALFSIZETiB - 0.01")t
+dmsetup create $TMPDM2 --table "0 $((2 * 1024 * 4)) linear $DISK 0"
+pvcreate $TMPDEV2 --setphysicalvolumesize $shortsize --metadatasize $METADATASIZE
+dmsetup remove $TMPDM2
+pvscan
+vgextend $VG $DISK
+
+# - pvmove all the LVs back
+
+#FOREACH host:
+for lv in $(lvs -o lv_name --noheadings $VG); do
+ movelv $VG/$lv $TMPDEV
+done
+#done
+
+# hopefully empty:
+lvs $VG -o lv_name,devices | grep $TMPDEV
+# if not, do some more movelv
+
+# - pvremove the temporary PV
+vgreduce $VG $TMPDEV
+pvremove $TMPDEV
+dmsetup remove $TMPDM
+
+# - pvresize the new PV to use the whole space
+pvresize $DISK