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, 14 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
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 2566 2010-02-01 16:07:15Z 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
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
64 # Do not process LV whose VG are excluded
65 if [ -f $MINDI_TMP/excludedvgs ]; then
66 if [ "`grep $volume_group $MINDI_TMP/excludedvgs`" != "" ]; then
67 echo "Not including LV $logical_volume as VG $volume_group was excluded"
68 return
69 fi
70 fi
71
72 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
73}
74
75
76GenerateLvcreateParameters() {
77 local device stripes stripesize device fname allocation output readahead
78 fname=$MINDI_TMP/PLF.$$.txt
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
88 if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
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"
99}
100
101
102
103GenerateVgcreateParameters() {
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
106 VG_info_file=$MINDI_TMP/$$.vg-info.txt
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
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
120}
121
122
123
124
125
126ProcessVolumeGroup() {
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`
131 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
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
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
152 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
153 for ed in $MINDI_EXCLUDE_DEVS ; do
154 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
155 echo $current_VG >> $MINDI_TMP/excludedvgs
156 return
157 fi
158 done
159 fi
160 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
161 echo "# $LVMCMD vgchange -a y $current_VG"
162}
163
164
165
166ListAllPhysicalVolumes() {
167 if [ $lvmversion = 2 ]; then
168 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
169 else
170 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
171 fi
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
177 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
178 for ed in $MINDI_EXCLUDE_DEVS ; do
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
191 if [ -f /etc/multipath.conf ]; then
192 # If multipath check which type of devidec are given, mpath prefered
193 if [ -f $MINDI_TMP/pv.tmp2 ]; then
194 for d in `cat $MINDI_TMP/pv.tmp2`; do
195 skip=0
196 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
197 for ed in $MINDI_EXCLUDE_DEVS ; do
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
210 else
211 if [ -f $MINDI_TMP/pv.tmp2 ]; then
212 cat $MINDI_TMP/pv.tmp2
213 fi
214 fi
215 rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
216}
217
218
219ListAllVolumeGroups() {
220 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
221}
222
223GiveMapperOfdm () {
224
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
240ListLvmDrivesAndPartitions() {
241 # We get partitions in this loop not devices
242 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
243 # If multipath check which type of devices are given, mpath prefered
244 if [ -f /etc/multipath.conf ]; then
245 i=`GiveMapperOfdm $d`
246 rep=$i
247 else
248 rep=$d
249 fi
250 skip=0
251 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
252 for ed in $MINDI_EXCLUDE_DEVS ; do
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
263 done
264}
265
266
267
268PrettifyList() {
269 local i
270 echo -en "$1"
271 for i in $2 ; do
272 echo -en "$i "
273 done
274 echo ""
275}
276
277
278ListAllLogicalVolumes() {
279 if [ $lvmversion = 2 ]; then
280 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
281 else
282 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
283 fi
284}
285
286
287
288WriteShutdownScript() {
289 local i
290 echo ""
291 echo "Finally, to shut down and delete the volumes, do this:-"
292 for i in `ListAllLogicalVolumes` ; do
293 echo "($LVMCMD lvremove -f $i)"
294 done
295 for i in `ListAllVolumeGroups` ; do
296 echo "($LVMCMD vgchange -a n $i)"
297 done
298 for i in `ListAllVolumeGroups` ; do
299 echo "($LVMCMD vgremove $i)"
300 done
301 if [ $lvmversion = 2 ]; then
302 echo "(rmmod dm-mod & rmmod dm_mod & )"
303 else
304 echo "(rmmod lvm-mod)"
305 fi
306}
307
308
309
310# -------------------------------- main -----------------------------------
311which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
312if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
313 exit 1
314fi
315
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
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
339lvmversion=`lvmdiskscan --help 2>&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
345if which lvm 2>/dev/null; then
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
352fi
353
354if [ $lvmversion = 2 ]; then
355 echo "LVM version >= 2.0 found."
356 LVMCMD="lvm"
357else
358 LVMCMD=""
359fi
360
361rm -f $MINDI_TMP/excludedvgs
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
368 echo "(insmod dm-mod)"
369 echo "(insmod dm_mod)"
370else
371 echo "(insmod lvm-mod)"
372fi
373echo "# $LVMCMD vgchange -an"
374for i in `ListAllPhysicalVolumes` ; do
375 echo "# echo y | $LVMCMD pvcreate -ff $i"
376done
377echo "# $LVMCMD vgscan"
378echo ""
379echo "Create and activate the VG's (volume groups)."
380all_volume_groups=`ListAllVolumeGroups`
381for current_VG in $all_volume_groups ; do
382 if [ $lvmversion -ne 2 ]; then
383 echo "# rm -Rf /dev/$current_VG"
384 fi
385 ProcessVolumeGroup $current_VG
386done
387echo ""
388echo "Finally, create the LV's (logical volumes)."
389all_logical_volumes=`ListAllLogicalVolumes`
390for current_LV in $all_logical_volumes ; do
391 ProcessLogicalVolume $current_LV
392done
393echo ""
394echo "# $LVMCMD vgscan"
395echo "Now you may format the LV's:-"
396for i in `ListAllLogicalVolumes` ; do
397 echo "(mkfs -t foo $i or something like that)"
398done
399rm -f $MINDI_TMP/excludedvgs
400WriteShutdownScript
401exit 0
402
403
404
Note: See TracBrowser for help on using the repository browser.