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, 16 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
RevLine 
[673]1#!/bin/bash
[275]2#
3# $Id: analyze-my-lvm 2565 2010-02-01 13:37:55Z 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
[2534]53 # Do not process LV whose VG are excluded
[2537]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"
[2565]57 else
58 echo "Not including LV $logical_volume as VG $volume_group was excluded"
[2537]59 fi
[2555]60 else
61 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
[2534]62 fi
[1]63}
64
65
66GenerateLvcreateParameters() {
[298]67 local device stripes stripesize device fname allocation output readahead
[1487]68 fname=$MINDI_TMP/PLF.$$.txt
[298]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
[911]78 if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
[298]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"
[1]89}
90
91
92
93GenerateVgcreateParameters() {
[298]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
[1487]96 VG_info_file=$MINDI_TMP/$$.vg-info.txt
[2342]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
[298]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
[1]110}
111
112
113
114
115
116ProcessVolumeGroup() {
[298]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`
[850]121 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
[298]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
[2072]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
[2564]142 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
143 for ed in $MINDI_EXCLUDE_DEVS ; do
[2532]144 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
[2534]145 echo $current_VG >> $MINDI_TMP/excludedvgs
[2532]146 return
147 fi
148 done
149 fi
[298]150 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
151 echo "# $LVMCMD vgchange -a y $current_VG"
[1]152}
153
154
155
156ListAllPhysicalVolumes() {
[298]157 if [ $lvmversion = 2 ]; then
[2072]158 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
[298]159 else
[2072]160 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
[298]161 fi
[2532]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
[2564]167 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
168 for ed in $MINDI_EXCLUDE_DEVS ; do
[2532]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
[2072]181 if [ -f /etc/multipath.conf ]; then
182 # If multipath check which type of devidec are given, mpath prefered
[2554]183 if [ -f $MINDI_TMP/pv.tmp2 ]; then
184 for d in `cat $MINDI_TMP/pv.tmp2`; do
185 skip=0
[2564]186 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
187 for ed in $MINDI_EXCLUDE_DEVS ; do
[2554]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
[2072]200 else
[2554]201 if [ -f $MINDI_TMP/pv.tmp2 ]; then
202 cat $MINDI_TMP/pv.tmp2
203 fi
[2072]204 fi
[2532]205 rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
[1]206}
207
208
209ListAllVolumeGroups() {
[298]210 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
[1]211}
212
[2072]213GiveMapperOfdm () {
[1]214
[2072]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
[1]230ListLvmDrivesAndPartitions() {
[2531]231 # We get partitions in this loop not devices
[2424]232 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
[2526]233 # If multipath check which type of devices are given, mpath prefered
[2424]234 if [ -f /etc/multipath.conf ]; then
[2526]235 i=`GiveMapperOfdm $d`
[2536]236 rep=$i
[2424]237 else
[2536]238 rep=$d
[2424]239 fi
[2536]240 skip=0
[2564]241 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
242 for ed in $MINDI_EXCLUDE_DEVS ; do
[2536]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
[2424]253 done
[1]254}
255
256
257
258PrettifyList() {
[298]259 local i
260 echo -en "$1"
261 for i in $2 ; do
262 echo -en "$i "
263 done
264 echo ""
[1]265}
266
267
268ListAllLogicalVolumes() {
[298]269 if [ $lvmversion = 2 ]; then
[2244]270 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
[298]271 else
[2244]272 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
[298]273 fi
[1]274}
275
276
277
278WriteShutdownScript() {
[298]279 local i
280 echo ""
281 echo "Finally, to shut down and delete the volumes, do this:-"
[1]282 for i in `ListAllLogicalVolumes` ; do
[298]283 echo "($LVMCMD lvremove -f $i)"
[1]284 done
285 for i in `ListAllVolumeGroups` ; do
[298]286 echo "($LVMCMD vgchange -a n $i)"
[1]287 done
288 for i in `ListAllVolumeGroups` ; do
[298]289 echo "($LVMCMD vgremove $i)"
[1]290 done
[298]291 if [ $lvmversion = 2 ]; then
292 echo "(rmmod dm-mod & rmmod dm_mod & )"
293 else
294 echo "(rmmod lvm-mod)"
295 fi
[1]296}
297
298
299
300# -------------------------------- main -----------------------------------
[963]301which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
[2044]302if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
303 exit 1
[1]304fi
305
[2565]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
[2170]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
[299]329lvmversion=`lvmdiskscan --help 2>&1 |
[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
[97]335if which lvm 2>/dev/null; then
[298]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
[1]342fi
343
344if [ $lvmversion = 2 ]; then
[298]345 echo "LVM version >= 2.0 found."
346 LVMCMD="lvm"
347else
348 LVMCMD=""
[1]349fi
350
[2565]351rm -f $MINDI_TMP/excludedvgs
[1]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
[298]358 echo "(insmod dm-mod)"
359 echo "(insmod dm_mod)"
[1]360else
[298]361 echo "(insmod lvm-mod)"
[1]362fi
[298]363echo "# $LVMCMD vgchange -an"
[1]364for i in `ListAllPhysicalVolumes` ; do
[298]365 echo "# echo y | $LVMCMD pvcreate -ff $i"
[1]366done
[706]367echo "# $LVMCMD vgscan"
[1]368echo ""
369echo "Create and activate the VG's (volume groups)."
370all_volume_groups=`ListAllVolumeGroups`
371for current_VG in $all_volume_groups ; do
[298]372 if [ $lvmversion -ne 2 ]; then
373 echo "# rm -Rf /dev/$current_VG"
374 fi
375 ProcessVolumeGroup $current_VG
[1]376done
377echo ""
378echo "Finally, create the LV's (logical volumes)."
379all_logical_volumes=`ListAllLogicalVolumes`
380for current_LV in $all_logical_volumes ; do
[298]381 ProcessLogicalVolume $current_LV
[1]382done
383echo ""
[298]384echo "# $LVMCMD vgscan"
[1]385echo "Now you may format the LV's:-"
386for i in `ListAllLogicalVolumes` ; do
[298]387 echo "(mkfs -t foo $i or something like that)"
[1]388done
[2565]389rm -f $MINDI_TMP/excludedvgs
[1]390WriteShutdownScript
391exit 0
392
393
394
Note: See TracBrowser for help on using the repository browser.