Update scripts for extracting LVM data
authorQuentin Smith <quentin@mit.edu>
Wed, 1 Apr 2009 04:32:50 +0000 (00:32 -0400)
committerQuentin Smith <quentin@mit.edu>
Wed, 1 Apr 2009 04:32:50 +0000 (00:32 -0400)
svn path=/trunk/scripts/pv-fixup/; revision=2285

lvmanip

diff --git a/lvmanip b/lvmanip
index 0bd06af..4df4d80 100644 (file)
--- a/lvmanip
+++ b/lvmanip
@@ -4,17 +4,13 @@ exit 1
 DISK=/dev/mapper/36090a028407d6e2b2589a45cdb971489
 
 # 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]+)/)'
 }
 
 lvinfo () {
@@ -24,6 +20,39 @@ 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+{/;'
+}
+
 
 # Generate a dd command to copy the LV data to /dev/xenvg/$lv
 # Only works for single-segment LVs.