source: MondoRescue/branches/2.2.8/mindi/analyze-my-lvm@ 2072

Last change on this file since 2072 was 2072, checked in by Bruno Cornec, 15 years ago
  • Adds multipath support (as exists on RHEL 5.2)
    • analyze-my-lvm/mindi uses the /dev/mapper/mpath... entry and not /dev/dm-... entry which is not automatically created at restore time
    • Lots of mapth dmsetup commands added to deplist
    • Adds required modules to mindi
    • Fix a bug for this in mindi around real devices with /dev/mapper/mpath... names
    • init has a new StartMpath function to start multipath correctly hopefully
    • mondorestore has a new clean_multiconf function which removes /var/lib/multipath/bindings and edits /etc/multipath.conf if needed
  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 2072 2008-12-05 14:32:59Z bruno $
4#
5
6Die() {
7 echo "$1" >> /dev/stderr
8 exit -1
9}
10
11
12
13GetValueFromField() {
14 local res
15 sed s/' '/~/ "$1" | tr -s ' ' ' ' | sed s/'~ '/'~'/ | grep -i "$2~" | cut -d'~' -f2,3,4,5 | tr '~' ' ' | gawk '{ if ($2=="MB") {printf "%dm",$1;} else if ($2=="KB") {printf "%dk",$1;} else if ($2=="GB") {printf "%fg",$1;} else {print $0;};}'
16}
17
18
19GetLastBit() {
20 local i res
21 i=20
22 res=""
23 while [ ! "$res" ] ; do
24 i=$(($i-1))
25 res=`echo "$1" | cut -d'/' -f$i`
26 done
27 echo "$res"
28}
29
30
31ProcessLogicalVolume() {
32 local LV_full_string fname logical_volume volume_group device
33 LV_full_string=$1
34 [ ! -e "$1" ] && Die "Cannot find LV file $1"
35 volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
36 logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
37 if [ $lvmversion = 2 ]; then
38 device=$LV_full_string
39 params=`GenerateLvcreateParameters $device`
40 else
41 fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
42 if [ ! -e "$fname" ] ; then
43 echo "Warning - cannot find $volume_group's $logical_volume LV file"
44 else
45 device=`GetValueFromField $fname "name:"`
46 params=`GenerateLvcreateParameters $device`
47 fi
48 fi
49 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
50}
51
52
53GenerateLvcreateParameters() {
54 local device stripes stripesize device fname allocation output readahead
55 fname=$MINDI_TMP/PLF.$$.txt
56 device=$1
57 output=""
58 $LVMCMD lvdisplay $device > $fname
59 stripes=`GetValueFromField $fname "Stripes"`
60 stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
61 [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
62 [ "$stripesize" = "k" ] && stripesize=""
63 allocation=`GetValueFromField $fname "LV Size"`
64 [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
65 if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
66 val=`echo "$allocation" | sed s/g//`
67 allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
68 fi
69 readahead=`GetValueFromField $fname "Read ahead sectors"`
70 rm -f $fname
71 [ "$stripes" ] && output="$output -i $stripes"
72 [ "$stripesize" ] && output="$output -I $stripesize"
73 [ "$allocation" ] && output="$output -L $allocation"
74 [ "$readahead" ] && output="$output -r $readahead"
75 echo "$output"
76}
77
78
79
80GenerateVgcreateParameters() {
81 local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
82 current_VG=$1
83 VG_info_file=$MINDI_TMP/$$.vg-info.txt
84 $LVMCMD vgdisplay $current_VG > $VG_info_file
85 max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
86 [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
87 max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
88 [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
89 physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
90 output=""
91 [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
92 [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
93 [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
94 echo "$output"
95 rm -f $VG_info_file
96}
97
98
99
100
101
102ProcessVolumeGroup() {
103 local current_VG physical_volumes i list_of_devices VG_params
104 current_VG=$1
105 if [ $lvmversion = 2 ]; then
106 VG_params=`GenerateVgcreateParameters $current_VG`
107 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
108 else
109 info_file=/proc/lvm/VGs/$current_VG/group
110 physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
111 VG_params=`GenerateVgcreateParameters $current_VG`
112 list_of_devices=""
113 for i in $physical_volumes ; do
114 fname=/proc/lvm/VGs/$current_VG/PVs/$i
115 device=`GetValueFromField $fname "name:"`
116 list_of_devices="$list_of_devices $device"
117 done
118 fi
119 l=""
120 if [ -f /etc/multipath.conf ]; then
121 # If multipath check which type of devidec are given, mpath prefered
122 for d in $list_of_devices; do
123 l="$l `GiveMapperOfdm $d`"
124 done
125 list_of_devices=$l
126 fi
127
128 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
129 echo "# $LVMCMD vgchange -a y $current_VG"
130}
131
132
133
134ListAllPhysicalVolumes() {
135 if [ $lvmversion = 2 ]; then
136 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
137 else
138 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
139 fi
140 if [ -f /etc/multipath.conf ]; then
141 # If multipath check which type of devidec are given, mpath prefered
142 for d in `cat $MINDI_TMP/pv.tmp`; do
143 GiveMapperOfdm $d
144 done
145 else
146 cat $MINDI_TMP/pv.tmp
147 fi
148 rm -f $MINDI_TMP/pv.tmp
149}
150
151
152ListAllVolumeGroups() {
153 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
154}
155
156GiveMapperOfdm () {
157
158major=`stat -c "%t" $1`
159minor=`stat -c "%T" $1`
160
161for i in `ls /dev/mapper/*`; do
162 mj=`stat -c "%t" $i`
163 mn=`stat -c "%T" $i`
164 if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
165 echo "$i"
166 return
167 fi
168done
169echo $1
170}
171
172
173ListLvmDrivesAndPartitions() {
174 $LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}' > $MINDI_TMP/vg.tmp
175 if [ -f /etc/multipath.conf ]; then
176 # If multipath check which type of devidec are given, mpath prefered
177 for d in `cat $MINDI_TMP/vg.tmp`; do
178 GiveMapperOfdm $d
179 done
180 else
181 cat $MINDI_TMP/vg.tmp
182 fi
183 rm -f $MINDI_TMP/vg.tmp
184}
185
186
187
188PrettifyList() {
189 local i
190 echo -en "$1"
191 for i in $2 ; do
192 echo -en "$i "
193 done
194 echo ""
195}
196
197
198ListAllLogicalVolumes() {
199 if [ $lvmversion = 2 ]; then
200 $LVMCMD lvscan 2> /dev/null | grep "'" | cut -d"'" -f2
201 else
202 lvscan 2> /dev/null | grep '"' | cut -d'"' -f2
203 fi
204}
205
206
207
208WriteShutdownScript() {
209 local i
210 echo ""
211 echo "Finally, to shut down and delete the volumes, do this:-"
212 for i in `ListAllLogicalVolumes` ; do
213 echo "($LVMCMD lvremove -f $i)"
214 done
215 for i in `ListAllVolumeGroups` ; do
216 echo "($LVMCMD vgchange -a n $i)"
217 done
218 for i in `ListAllVolumeGroups` ; do
219 echo "($LVMCMD vgremove $i)"
220 done
221 if [ $lvmversion = 2 ]; then
222 echo "(rmmod dm-mod & rmmod dm_mod & )"
223 else
224 echo "(rmmod lvm-mod)"
225 fi
226}
227
228
229
230# -------------------------------- main -----------------------------------
231which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
232if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
233 exit 1
234fi
235
236lvmversion=`lvmdiskscan --help 2>&1 |
237 grep -E "Logical Volume Manager|LVM version:" |
238 cut -d: -f2 | cut -d. -f1 |
239 awk '{print $NF}' |
240 sed -e 's/ //g'`
241
242if which lvm 2>/dev/null; then
243 version=`lvm version | grep "LVM version" | awk '{print $3}'`
244 i="`echo "$version" | cut -d'.' -f1`"
245 echo "i=$i"
246 if [ "$i" -ge "2" ] ; then
247 lvmversion=2
248 fi
249fi
250
251if [ $lvmversion = 2 ]; then
252 echo "LVM version >= 2.0 found."
253 LVMCMD="lvm"
254else
255 LVMCMD=""
256fi
257
258all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
259echo "Just before you extrapolate mountlist to include RAID partitions,"
260echo "extrapolate it to include the following LVM drives and partitions:-"
261PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
262echo "To get started, type:-"
263if [ $lvmversion = 2 ]; then
264 echo "(insmod dm-mod)"
265 echo "(insmod dm_mod)"
266else
267 echo "(insmod lvm-mod)"
268fi
269echo "# $LVMCMD vgchange -an"
270for i in `ListAllPhysicalVolumes` ; do
271 echo "# echo y | $LVMCMD pvcreate -ff $i"
272done
273echo "# $LVMCMD vgscan"
274echo ""
275echo "Create and activate the VG's (volume groups)."
276all_volume_groups=`ListAllVolumeGroups`
277for current_VG in $all_volume_groups ; do
278 if [ $lvmversion -ne 2 ]; then
279 echo "# rm -Rf /dev/$current_VG"
280 fi
281 ProcessVolumeGroup $current_VG
282done
283echo ""
284echo "Finally, create the LV's (logical volumes)."
285all_logical_volumes=`ListAllLogicalVolumes`
286for current_LV in $all_logical_volumes ; do
287 ProcessLogicalVolume $current_LV
288done
289echo ""
290echo "# $LVMCMD vgscan"
291echo "Now you may format the LV's:-"
292for i in `ListAllLogicalVolumes` ; do
293 echo "(mkfs -t foo $i or something like that)"
294done
295WriteShutdownScript
296exit 0
297
298
299
Note: See TracBrowser for help on using the repository browser.