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

Last change on this file since 2897 was 2321, checked in by price, 16 years ago

pv-fixup, lvmanip: last tweaks

This is from the very beginning of April, at the close of the LVM saga.
Somehow I never pushed.

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