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

Last change on this file since 2567 was 2567, checked in by Bruno Cornec, 14 years ago
  • Improve device exclusion for LVM by adding support for symlinks and mapper systematically at all levels (PV, VG, LV). Should really provvide the best support possible for LVM exclusion.
  • Property svn:keywords set to Id
File size: 10.6 KB
RevLine 
[673]1#!/bin/bash
[275]2#
3# $Id: analyze-my-lvm 2567 2010-02-01 17:10:24Z 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
[2566]71 if [ "`grep $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`
[850]136 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
[298]137 else
138 info_file=/proc/lvm/VGs/$current_VG/group
139 physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
140 VG_params=`GenerateVgcreateParameters $current_VG`
141 list_of_devices=""
142 for i in $physical_volumes ; do
143 fname=/proc/lvm/VGs/$current_VG/PVs/$i
144 device=`GetValueFromField $fname "name:"`
145 list_of_devices="$list_of_devices $device"
146 done
147 fi
[2072]148 l=""
149 if [ -f /etc/multipath.conf ]; then
[2567]150 # If multipath check which type of devide are given, mpath prefered
[2072]151 for d in $list_of_devices; do
[2567]152 l="$l `mindi --readalllink $d`"
[2072]153 l="$l `GiveMapperOfdm $d`"
154 done
[2567]155 list_of_devices="`echo $l | sort -u`"
[2072]156 fi
157
[2564]158 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
159 for ed in $MINDI_EXCLUDE_DEVS ; do
[2532]160 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
[2534]161 echo $current_VG >> $MINDI_TMP/excludedvgs
[2532]162 return
163 fi
164 done
165 fi
[298]166 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
167 echo "# $LVMCMD vgchange -a y $current_VG"
[1]168}
169
170
171
172ListAllPhysicalVolumes() {
[298]173 if [ $lvmversion = 2 ]; then
[2072]174 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
[298]175 else
[2072]176 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
[298]177 fi
[2532]178
179 rm -f $MINDI_TMP/pv.tmp2
[2567]180 l=""
[2532]181 for d in `cat $MINDI_TMP/pv.tmp`; do
182 # Skip devices excluded, coming from mondoarchive
183 skip=0
[2567]184 l="$l `mindi --readalllink $d`"
185 l="$l `GiveMapperOfdm $d`"
186 list_of_devices="`echo $l | sort -u`"
[2564]187 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
188 for ed in $MINDI_EXCLUDE_DEVS ; do
[2567]189 if [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
[2532]190 skip=1
191 continue
192 fi
193 done
194 fi
195 if [ $skip -eq 1 ]; then
196 continue
197 fi
198 echo $d >> $MINDI_TMP/pv.tmp2
199 done
200
[2072]201 if [ -f /etc/multipath.conf ]; then
[2567]202 # If multipath check which type of devide are given, mpath prefered
[2554]203 if [ -f $MINDI_TMP/pv.tmp2 ]; then
[2567]204 l=""
[2554]205 for d in `cat $MINDI_TMP/pv.tmp2`; do
206 skip=0
[2567]207 l="$l `mindi --readalllink $d`"
208 l="$l `GiveMapperOfdm $d`"
209 list_of_devices="`echo $l | sort -u`"
[2564]210 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
211 for ed in $MINDI_EXCLUDE_DEVS ; do
[2567]212 if [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
[2554]213 skip=1
214 continue
215 fi
216 done
217 fi
218 if [ $skip -eq 1 ]; then
219 continue
220 fi
221 GiveMapperOfdm $d
222 done
223 fi
[2072]224 else
[2554]225 if [ -f $MINDI_TMP/pv.tmp2 ]; then
226 cat $MINDI_TMP/pv.tmp2
227 fi
[2072]228 fi
[2532]229 rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
[1]230}
231
232
233ListAllVolumeGroups() {
[298]234 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
[1]235}
236
[2072]237GiveMapperOfdm () {
[1]238
[2072]239major=`stat -c "%t" $1`
240minor=`stat -c "%T" $1`
241
242for i in `ls /dev/mapper/*`; do
243 mj=`stat -c "%t" $i`
244 mn=`stat -c "%T" $i`
245 if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
246 echo "$i"
247 return
248 fi
249done
250echo $1
251}
252
253
[1]254ListLvmDrivesAndPartitions() {
[2531]255 # We get partitions in this loop not devices
[2424]256 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
[2526]257 # If multipath check which type of devices are given, mpath prefered
[2424]258 if [ -f /etc/multipath.conf ]; then
[2526]259 i=`GiveMapperOfdm $d`
[2536]260 rep=$i
[2424]261 else
[2536]262 rep=$d
[2424]263 fi
[2536]264 skip=0
[2564]265 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
266 for ed in $MINDI_EXCLUDE_DEVS ; do
[2536]267 if [ "`echo " $rep " | grep " $ed"`" != "" ]; then
268 skip=1
269 continue
270 fi
271 done
272 fi
273 if [ $skip -eq 1 ]; then
274 continue
275 fi
276 echo $rep
[2424]277 done
[1]278}
279
280
281
282PrettifyList() {
[298]283 local i
284 echo -en "$1"
285 for i in $2 ; do
286 echo -en "$i "
287 done
288 echo ""
[1]289}
290
291
292ListAllLogicalVolumes() {
[298]293 if [ $lvmversion = 2 ]; then
[2244]294 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
[298]295 else
[2244]296 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
[298]297 fi
[1]298}
299
300
301
302WriteShutdownScript() {
[298]303 local i
304 echo ""
305 echo "Finally, to shut down and delete the volumes, do this:-"
[1]306 for i in `ListAllLogicalVolumes` ; do
[298]307 echo "($LVMCMD lvremove -f $i)"
[1]308 done
309 for i in `ListAllVolumeGroups` ; do
[298]310 echo "($LVMCMD vgchange -a n $i)"
[1]311 done
312 for i in `ListAllVolumeGroups` ; do
[298]313 echo "($LVMCMD vgremove $i)"
[1]314 done
[298]315 if [ $lvmversion = 2 ]; then
316 echo "(rmmod dm-mod & rmmod dm_mod & )"
317 else
318 echo "(rmmod lvm-mod)"
319 fi
[1]320}
321
322
323
324# -------------------------------- main -----------------------------------
[963]325which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
[2044]326if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
327 exit 1
[1]328fi
329
[2565]330if [ _"$MINDI_TMP" = _"" ]; then
331 # Launched stdalone, so create a temp dir
332 MINDI_TMP=`mktemp -d $TMPDIR/mindi.XXXXXXXXXX`
333 if [ $? -ne 0 ]; then
334 df $TMPDIR
335 Die "Unable to create a temporary directory ! Check space on $TMPDIR"
336 fi
337 if [ _"$MINDI_TMP" = _"" ]; then
338 Die "MINDI_TMP is empty, aborting"
339 fi
340 if [ _"$MINDI_TMP" = _"/" ]; then
341 Die "MINDI_TMP is /, aborting"
342 fi
343fi
344
[2170]345# Older lvmdiskscan use --help, newer --version
346lvmopt="--help"
347lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
348if [ $? -eq 0 ]; then
349 lvmopt="--version"
350fi
351
352
[299]353lvmversion=`lvmdiskscan --help 2>&1 |
[1]354 grep -E "Logical Volume Manager|LVM version:" |
355 cut -d: -f2 | cut -d. -f1 |
356 awk '{print $NF}' |
357 sed -e 's/ //g'`
358
[97]359if which lvm 2>/dev/null; then
[298]360 version=`lvm version | grep "LVM version" | awk '{print $3}'`
361 i="`echo "$version" | cut -d'.' -f1`"
362 echo "i=$i"
363 if [ "$i" -ge "2" ] ; then
364 lvmversion=2
365 fi
[1]366fi
367
368if [ $lvmversion = 2 ]; then
[298]369 echo "LVM version >= 2.0 found."
370 LVMCMD="lvm"
371else
372 LVMCMD=""
[1]373fi
374
[2565]375rm -f $MINDI_TMP/excludedvgs
[1]376all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
377echo "Just before you extrapolate mountlist to include RAID partitions,"
378echo "extrapolate it to include the following LVM drives and partitions:-"
379PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
380echo "To get started, type:-"
381if [ $lvmversion = 2 ]; then
[298]382 echo "(insmod dm-mod)"
383 echo "(insmod dm_mod)"
[1]384else
[298]385 echo "(insmod lvm-mod)"
[1]386fi
[298]387echo "# $LVMCMD vgchange -an"
[1]388for i in `ListAllPhysicalVolumes` ; do
[298]389 echo "# echo y | $LVMCMD pvcreate -ff $i"
[1]390done
[706]391echo "# $LVMCMD vgscan"
[1]392echo ""
393echo "Create and activate the VG's (volume groups)."
394all_volume_groups=`ListAllVolumeGroups`
395for current_VG in $all_volume_groups ; do
[298]396 if [ $lvmversion -ne 2 ]; then
397 echo "# rm -Rf /dev/$current_VG"
398 fi
399 ProcessVolumeGroup $current_VG
[1]400done
401echo ""
402echo "Finally, create the LV's (logical volumes)."
403all_logical_volumes=`ListAllLogicalVolumes`
404for current_LV in $all_logical_volumes ; do
[298]405 ProcessLogicalVolume $current_LV
[1]406done
407echo ""
[298]408echo "# $LVMCMD vgscan"
[1]409echo "Now you may format the LV's:-"
410for i in `ListAllLogicalVolumes` ; do
[298]411 echo "(mkfs -t foo $i or something like that)"
[1]412done
[2565]413rm -f $MINDI_TMP/excludedvgs
[1]414WriteShutdownScript
415exit 0
416
417
418
Note: See TracBrowser for help on using the repository browser.