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

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