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

Last change on this file since 2701 was 2701, checked in by Bruno Cornec, 13 years ago

r4177@localhost: bruno | 2011-01-26 19:59:03 +0100

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