source: trunk/scripts/pv-fixup/lvmanip @ 2304

Last change on this file since 2304 was 2285, checked in by quentin, 16 years ago

Update scripts for extracting LVM data

File size: 3.2 KB
Line 
1#!/bin/bash
2exit 1
3
4DISK=/dev/mapper/36090a028407d6e2b2589a45cdb971489
5
6# The name of the LVM archive to get data from
7ARCHIVE=/etc/lvm/archive/xenvg_01514.vg
8
9lvmextractsection () {
10  perl -ne 'print if ( /^(\s*)'"$1"' [\{[]/ ... /^$1[\}\]]/ )'
11}
12lvmextractint () {
13  perl -lne 'print $1 if (/'"$1"' = ([0-9]+)/)'
14}
15
16lvinfo () {
17  perl -ne 'print if ( /^(\s*)'"$1"' \{/ ... /^$1\}/ )' $ARCHIVE
18}
19segment2 () {
20  perl -ne 'print if ( /^(\s*)segment2 \{/ ... /^$1\}/ )'
21}
22
23# Extract the first block of an lv to a file named part-$lv
24getfirstblock () {
25  lvname="$1"
26  lvinfo=$(lvinfo "$lvname")
27  firststripe=$(echo "$lvinfo" | lvmextractsection 'stripes =' | tail -n +2 | head -n 1)
28  offset=$(echo "$firststripe" | cut -f 2 -d ,);
29  if [ -z "$offset" ]; then echo "WARNING: LV $lvname not found";
30  else
31    pvname=$(echo "$firststripe" | perl -pe 's/.*"([^"]+)".*/\1/')
32    pvdevice=$(cat $ARCHIVE | lvmextractsection "$pvname" | perl -lne 'print $1 if (/device = "(.+)"/)')
33    pe_start=$(cat $ARCHIVE | lvmextractsection "$pvname" | lvmextractint "pe_start")
34    echo "Extracting $lvname from PE offset $offset on $pvname ($pvdevice)"
35    dd if=$pvdevice of=part-$lvname bs=1M skip=$(( $offset * 8192 + $pe_start )) bs=512 count=1;
36  fi
37}
38
39if /bin/false; then
40# Usage:
41mkdir new
42mkdir old
43(cd old; for i in $(listlvs); do getfirstblock "$i"; done)
44(cd new; for i in $(listlvs); do dd if=/dev/xenvg/$i of=part-$i bs=512 count=1; done)
45
46diff -u <(cd old && sfdisk -l * 2>/dev/null) <(cd new && sfdisk -l * 2>/dev/null)
47(cd old && md5sum *) | (cd new && md5sum -c) | grep -v OK
48# end usage
49fi
50
51# Get a list of LVs in an LVM archive
52listlvs () {
53  cat $ARCHIVE | lvmextractsection "logical_volumes" | tail -n +2 | perl -lne 'if (/^(\s+)\S+\s+{/) { $indent ||= $1; }; print "$1" if /^$indent(\S+)\s+{/;'
54}
55
56
57# Generate a dd command to copy the LV data to /dev/xenvg/$lv
58# Only works for single-segment LVs.
59ddlv () {
60  lvname="$1"
61  if ! lvinfo $lvname | grep -q 'segment_count = 1'; then
62    echo "# WARNING: LV $lvname has more than one segment, skipping" >&2
63    return 1
64  fi
65  offset=$(lvinfo $lvname | perl -lne 'print $1 if (/"pv0", ([0-9]+)/)')
66  length=$(lvinfo $lvname | perl -lne 'print $1 if (/extent_count = ([0-9]+)/)')
67
68  if [ -z "$offset" ]; then
69    echo "# WARNING: LV $lvname not found";
70  else
71    echo "# Extracting $lvname from offset $offset PEs with length $length PEs"
72    echo dd if=$DISK of=/dev/xenvg/$lvname \
73      skip=$(( $offset * 64 + 3 )) bs=64K count=$(( $length * 64 ))
74  fi
75}
76
77# Generate a dd command to copy the LV data for the second segment.
78ddlv2 () {
79  lvname="$1"
80  if ! lvinfo $lvname | grep -q 'segment_count = 2'; then
81    echo "# WARNING: LV $lvname has other than two segments, skipping" >&2
82    return 1
83  fi
84  offset=$(lvinfo $lvname | segment2 | perl -lne 'print $1 if (/"pv0", ([0-9]+)/)')
85  length=$(lvinfo $lvname | segment2 | perl -lne 'print $1 if (/extent_count = ([0-9]+)/)')
86  seek=$(lvinfo $lvname | segment2 | perl -lne 'print $1 if (/start_extent = ([0-9]+)/)')
87
88  if [ -z "$offset" ]; then
89    echo "# WARNING: LV $lvname not found";
90  else
91    echo "# Extracting $lvname segment 2 from offset $offset PEs with length $length PEs"
92    echo dd if=$DISK of=/dev/xenvg/$lvname bs=64K \
93      skip=$(($offset*64 + 3)) count=$(($length*64)) seek=$(($seek*64))
94  fi
95}
Note: See TracBrowser for help on using the repository browser.