source: MondoRescue/branches/2.2.9/mindi/analyze-my-lvm@ 2565

Last change on this file since 2565 was 2565, checked in by Bruno Cornec, 14 years ago
  • Fix LV exclusion when VGs are excluded (the excludedvgs file has to be removed at start and end of analyze-my-lvm not in functions, as they are called multiple times)
  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 2565 2010-02-01 13:37:55Z 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 if [ ! -e "$1" ]; then
35 echo "WARNING - cannot find LV file $1" | tee -a /dev/stderr
36 return
37 fi
38 volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
39 logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
40 if [ $lvmversion = 2 ]; then
41 device=$LV_full_string
42 params=`GenerateLvcreateParameters $device`
43 else
44 fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
45 if [ ! -e "$fname" ] ; then
46 echo "WARNING - cannot find $volume_group's $logical_volume LV file" | tee -a /dev/stderr
47 return
48 else
49 device=`GetValueFromField $fname "name:"`
50 params=`GenerateLvcreateParameters $device`
51 fi
52 fi
53 # Do not process LV whose VG are excluded
54 if [ -f $MINDI_TMP/excludedvgs ]; then
55 if [ "`grep $volume_group $MINDI_TMP/excludedvgs`" = "" ]; then
56 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
57 else
58 echo "Not including LV $logical_volume as VG $volume_group was excluded"
59 fi
60 else
61 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
62 fi
63}
64
65
66GenerateLvcreateParameters() {
67 local device stripes stripesize device fname allocation output readahead
68 fname=$MINDI_TMP/PLF.$$.txt
69 device=$1
70 output=""
71 $LVMCMD lvdisplay $device > $fname
72 stripes=`GetValueFromField $fname "Stripes"`
73 stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
74 [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
75 [ "$stripesize" = "k" ] && stripesize=""
76 allocation=`GetValueFromField $fname "LV Size"`
77 [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
78 if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
79 val=`echo "$allocation" | sed s/g//`
80 allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
81 fi
82 readahead=`GetValueFromField $fname "Read ahead sectors"`
83 rm -f $fname
84 [ "$stripes" ] && output="$output -i $stripes"
85 [ "$stripesize" ] && output="$output -I $stripesize"
86 [ "$allocation" ] && output="$output -L $allocation"
87 [ "$readahead" ] && output="$output -r $readahead"
88 echo "$output"
89}
90
91
92
93GenerateVgcreateParameters() {
94 local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
95 current_VG=$1
96 VG_info_file=$MINDI_TMP/$$.vg-info.txt
97 # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
98 $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
99 max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
100 [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
101 max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
102 [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
103 physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
104 output=""
105 [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
106 [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
107 [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
108 echo "$output"
109 rm -f $VG_info_file
110}
111
112
113
114
115
116ProcessVolumeGroup() {
117 local current_VG physical_volumes i list_of_devices VG_params
118 current_VG=$1
119 if [ $lvmversion = 2 ]; then
120 VG_params=`GenerateVgcreateParameters $current_VG`
121 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
122 else
123 info_file=/proc/lvm/VGs/$current_VG/group
124 physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
125 VG_params=`GenerateVgcreateParameters $current_VG`
126 list_of_devices=""
127 for i in $physical_volumes ; do
128 fname=/proc/lvm/VGs/$current_VG/PVs/$i
129 device=`GetValueFromField $fname "name:"`
130 list_of_devices="$list_of_devices $device"
131 done
132 fi
133 l=""
134 if [ -f /etc/multipath.conf ]; then
135 # If multipath check which type of devidec are given, mpath prefered
136 for d in $list_of_devices; do
137 l="$l `GiveMapperOfdm $d`"
138 done
139 list_of_devices=$l
140 fi
141
142 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
143 for ed in $MINDI_EXCLUDE_DEVS ; do
144 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
145 echo $current_VG >> $MINDI_TMP/excludedvgs
146 return
147 fi
148 done
149 fi
150 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
151 echo "# $LVMCMD vgchange -a y $current_VG"
152}
153
154
155
156ListAllPhysicalVolumes() {
157 if [ $lvmversion = 2 ]; then
158 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
159 else
160 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
161 fi
162
163 rm -f $MINDI_TMP/pv.tmp2
164 for d in `cat $MINDI_TMP/pv.tmp`; do
165 # Skip devices excluded, coming from mondoarchive
166 skip=0
167 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
168 for ed in $MINDI_EXCLUDE_DEVS ; do
169 if [ "`echo " $d " | grep " $ed"`" != "" ]; then
170 skip=1
171 continue
172 fi
173 done
174 fi
175 if [ $skip -eq 1 ]; then
176 continue
177 fi
178 echo $d >> $MINDI_TMP/pv.tmp2
179 done
180
181 if [ -f /etc/multipath.conf ]; then
182 # If multipath check which type of devidec are given, mpath prefered
183 if [ -f $MINDI_TMP/pv.tmp2 ]; then
184 for d in `cat $MINDI_TMP/pv.tmp2`; do
185 skip=0
186 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
187 for ed in $MINDI_EXCLUDE_DEVS ; do
188 if [ "`echo " $d " | grep " $ed"`" != "" ]; then
189 skip=1
190 continue
191 fi
192 done
193 fi
194 if [ $skip -eq 1 ]; then
195 continue
196 fi
197 GiveMapperOfdm $d
198 done
199 fi
200 else
201 if [ -f $MINDI_TMP/pv.tmp2 ]; then
202 cat $MINDI_TMP/pv.tmp2
203 fi
204 fi
205 rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
206}
207
208
209ListAllVolumeGroups() {
210 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
211}
212
213GiveMapperOfdm () {
214
215major=`stat -c "%t" $1`
216minor=`stat -c "%T" $1`
217
218for i in `ls /dev/mapper/*`; do
219 mj=`stat -c "%t" $i`
220 mn=`stat -c "%T" $i`
221 if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
222 echo "$i"
223 return
224 fi
225done
226echo $1
227}
228
229
230ListLvmDrivesAndPartitions() {
231 # We get partitions in this loop not devices
232 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
233 # If multipath check which type of devices are given, mpath prefered
234 if [ -f /etc/multipath.conf ]; then
235 i=`GiveMapperOfdm $d`
236 rep=$i
237 else
238 rep=$d
239 fi
240 skip=0
241 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
242 for ed in $MINDI_EXCLUDE_DEVS ; do
243 if [ "`echo " $rep " | grep " $ed"`" != "" ]; then
244 skip=1
245 continue
246 fi
247 done
248 fi
249 if [ $skip -eq 1 ]; then
250 continue
251 fi
252 echo $rep
253 done
254}
255
256
257
258PrettifyList() {
259 local i
260 echo -en "$1"
261 for i in $2 ; do
262 echo -en "$i "
263 done
264 echo ""
265}
266
267
268ListAllLogicalVolumes() {
269 if [ $lvmversion = 2 ]; then
270 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
271 else
272 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
273 fi
274}
275
276
277
278WriteShutdownScript() {
279 local i
280 echo ""
281 echo "Finally, to shut down and delete the volumes, do this:-"
282 for i in `ListAllLogicalVolumes` ; do
283 echo "($LVMCMD lvremove -f $i)"
284 done
285 for i in `ListAllVolumeGroups` ; do
286 echo "($LVMCMD vgchange -a n $i)"
287 done
288 for i in `ListAllVolumeGroups` ; do
289 echo "($LVMCMD vgremove $i)"
290 done
291 if [ $lvmversion = 2 ]; then
292 echo "(rmmod dm-mod & rmmod dm_mod & )"
293 else
294 echo "(rmmod lvm-mod)"
295 fi
296}
297
298
299
300# -------------------------------- main -----------------------------------
301which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
302if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
303 exit 1
304fi
305
306if [ _"$MINDI_TMP" = _"" ]; then
307 # Launched stdalone, so create a temp dir
308 MINDI_TMP=`mktemp -d $TMPDIR/mindi.XXXXXXXXXX`
309 if [ $? -ne 0 ]; then
310 df $TMPDIR
311 Die "Unable to create a temporary directory ! Check space on $TMPDIR"
312 fi
313 if [ _"$MINDI_TMP" = _"" ]; then
314 Die "MINDI_TMP is empty, aborting"
315 fi
316 if [ _"$MINDI_TMP" = _"/" ]; then
317 Die "MINDI_TMP is /, aborting"
318 fi
319fi
320
321# Older lvmdiskscan use --help, newer --version
322lvmopt="--help"
323lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
324if [ $? -eq 0 ]; then
325 lvmopt="--version"
326fi
327
328
329lvmversion=`lvmdiskscan --help 2>&1 |
330 grep -E "Logical Volume Manager|LVM version:" |
331 cut -d: -f2 | cut -d. -f1 |
332 awk '{print $NF}' |
333 sed -e 's/ //g'`
334
335if which lvm 2>/dev/null; then
336 version=`lvm version | grep "LVM version" | awk '{print $3}'`
337 i="`echo "$version" | cut -d'.' -f1`"
338 echo "i=$i"
339 if [ "$i" -ge "2" ] ; then
340 lvmversion=2
341 fi
342fi
343
344if [ $lvmversion = 2 ]; then
345 echo "LVM version >= 2.0 found."
346 LVMCMD="lvm"
347else
348 LVMCMD=""
349fi
350
351rm -f $MINDI_TMP/excludedvgs
352all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
353echo "Just before you extrapolate mountlist to include RAID partitions,"
354echo "extrapolate it to include the following LVM drives and partitions:-"
355PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
356echo "To get started, type:-"
357if [ $lvmversion = 2 ]; then
358 echo "(insmod dm-mod)"
359 echo "(insmod dm_mod)"
360else
361 echo "(insmod lvm-mod)"
362fi
363echo "# $LVMCMD vgchange -an"
364for i in `ListAllPhysicalVolumes` ; do
365 echo "# echo y | $LVMCMD pvcreate -ff $i"
366done
367echo "# $LVMCMD vgscan"
368echo ""
369echo "Create and activate the VG's (volume groups)."
370all_volume_groups=`ListAllVolumeGroups`
371for current_VG in $all_volume_groups ; do
372 if [ $lvmversion -ne 2 ]; then
373 echo "# rm -Rf /dev/$current_VG"
374 fi
375 ProcessVolumeGroup $current_VG
376done
377echo ""
378echo "Finally, create the LV's (logical volumes)."
379all_logical_volumes=`ListAllLogicalVolumes`
380for current_LV in $all_logical_volumes ; do
381 ProcessLogicalVolume $current_LV
382done
383echo ""
384echo "# $LVMCMD vgscan"
385echo "Now you may format the LV's:-"
386for i in `ListAllLogicalVolumes` ; do
387 echo "(mkfs -t foo $i or something like that)"
388done
389rm -f $MINDI_TMP/excludedvgs
390WriteShutdownScript
391exit 0
392
393
394
Note: See TracBrowser for help on using the repository browser.