Index: /trunk/scripts/pv-fixup/lvmanip
===================================================================
--- /trunk/scripts/pv-fixup/lvmanip	(revision 2284)
+++ /trunk/scripts/pv-fixup/lvmanip	(revision 2285)
@@ -5,15 +5,11 @@
 
 # The name of the LVM archive to get data from
-ARCHIVE=/etc/lvm/archive/xenvg_00685.vg
+ARCHIVE=/etc/lvm/archive/xenvg_01514.vg
 
-# Extract the first block of an lv to a file named part-$lv
-getfirstblock () {
-  lvname="$1"
-  offset=$(grep -A16 $'\t'$lvname $ARCHIVE  | grep pv0 | cut -f 2 -d ,);
-  if [ -z "$offset" ]; then echo "WARNING: LV not found";
-  else
-    echo "Extracting $lvname from offset $offset"
-    dd if=$DISK of=part-$lvname bs=1M skip=$(( $offset * 8192 + 384 )) bs=512 count=1;
-  fi
+lvmextractsection () {
+  perl -ne 'print if ( /^(\s*)'"$1"' [\{[]/ ... /^$1[\}\]]/ )'
+}
+lvmextractint () {
+  perl -lne 'print $1 if (/'"$1"' = ([0-9]+)/)'
 }
 
@@ -23,4 +19,37 @@
 segment2 () {
   perl -ne 'print if ( /^(\s*)segment2 \{/ ... /^$1\}/ )'
+}
+
+# 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+{/;'
 }
 
