+# Extract the first block of an lv to a file named part-$lv
+getfirstblock () {
+ lvname="$1"
+ lvinfo=$(lvinfo "$lvname")
+ firststripe=$(echo "$lvinfo" | lvmextractsection 'stripes =' | tail -n +2 | head -n 1)
+ offset=$(echo "$firststripe" | cut -f 2 -d ,);
+ if [ -z "$offset" ]; then echo "WARNING: LV $lvname not found";
+ else
+ pvname=$(echo "$firststripe" | perl -pe 's/.*"([^"]+)".*/\1/')
+ pvdevice=$(cat $ARCHIVE | lvmextractsection "$pvname" | perl -lne 'print $1 if (/device = "(.+)"/)')
+ pe_start=$(cat $ARCHIVE | lvmextractsection "$pvname" | lvmextractint "pe_start")
+ echo "Extracting $lvname from PE offset $offset on $pvname ($pvdevice)"
+ dd if=$pvdevice of=part-$lvname bs=1M skip=$(( $offset * 8192 + $pe_start )) bs=512 count=1;
+ fi
+}
+
+if /bin/false; then
+# Usage:
+mkdir new
+mkdir old
+(cd old; for i in $(listlvs); do getfirstblock "$i"; done)
+(cd new; for i in $(listlvs); do dd if=/dev/xenvg/$i of=part-$i bs=512 count=1; done)
+
+diff -u <(cd old && sfdisk -l * 2>/dev/null) <(cd new && sfdisk -l * 2>/dev/null)
+(cd old && md5sum *) | (cd new && md5sum -c) | grep -v OK
+# end usage
+fi
+
+# Get a list of LVs in an LVM archive
+listlvs () {
+ cat $ARCHIVE | lvmextractsection "logical_volumes" | tail -n +2 | perl -lne 'if (/^(\s+)\S+\s+{/) { $indent ||= $1; }; print "$1" if /^$indent(\S+)\s+{/;'
+}
+