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

Last change on this file since 2555 was 2555, checked in by Bruno Cornec, 14 years ago
  • Fix a bug where LV were not created anymore if no exclusion !!
  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 2555 2010-01-26 19:03:21Z 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 # Do not process LV whose VG are excluded
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"
57 fi
58 else
59 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
60 fi
61 rm -f $MINDI_TMP/excludedvgs
62}
63
64
65GenerateLvcreateParameters() {
66 local device stripes stripesize device fname allocation output readahead
67 fname=$MINDI_TMP/PLF.$$.txt
68 device=$1
69 output=""
70 $LVMCMD lvdisplay $device > $fname
71 stripes=`GetValueFromField $fname "Stripes"`
72 stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
73 [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
74 [ "$stripesize" = "k" ] && stripesize=""
75 allocation=`GetValueFromField $fname "LV Size"`
76 [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
77 if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
78 val=`echo "$allocation" | sed s/g//`
79 allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
80 fi
81 readahead=`GetValueFromField $fname "Read ahead sectors"`
82 rm -f $fname
83 [ "$stripes" ] && output="$output -i $stripes"
84 [ "$stripesize" ] && output="$output -I $stripesize"
85 [ "$allocation" ] && output="$output -L $allocation"
86 [ "$readahead" ] && output="$output -r $readahead"
87 echo "$output"
88}
89
90
91
92GenerateVgcreateParameters() {
93 local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
94 current_VG=$1
95 VG_info_file=$MINDI_TMP/$$.vg-info.txt
96 # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
97 $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
98 max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
99 [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
100 max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
101 [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
102 physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
103 output=""
104 [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
105 [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
106 [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
107 echo "$output"
108 rm -f $VG_info_file
109}
110
111
112
113
114
115ProcessVolumeGroup() {
116 local current_VG physical_volumes i list_of_devices VG_params
117 current_VG=$1
118 if [ $lvmversion = 2 ]; then
119 VG_params=`GenerateVgcreateParameters $current_VG`
120 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
121 else
122 info_file=/proc/lvm/VGs/$current_VG/group
123 physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
124 VG_params=`GenerateVgcreateParameters $current_VG`
125 list_of_devices=""
126 for i in $physical_volumes ; do
127 fname=/proc/lvm/VGs/$current_VG/PVs/$i
128 device=`GetValueFromField $fname "name:"`
129 list_of_devices="$list_of_devices $device"
130 done
131 fi
132 l=""
133 if [ -f /etc/multipath.conf ]; then
134 # If multipath check which type of devidec are given, mpath prefered
135 for d in $list_of_devices; do
136 l="$l `GiveMapperOfdm $d`"
137 done
138 list_of_devices=$l
139 fi
140
141 rm -f $MINDI_TMP/excludedvgs
142 if [ "$EXCLUDE_DEVS" ] ; then
143 for ed in $EXCLUDE_DEVS ; do
144 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
145 echo $current_VG >> $MINDI_TMP/excludedvgs
146 return
147 fi
148 done
149 fi
150 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
151 echo "# $LVMCMD vgchange -a y $current_VG"
152}
153
154
155
156ListAllPhysicalVolumes() {
157 if [ $lvmversion = 2 ]; then
158 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
159 else
160 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
161 fi
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
167 if [ "$EXCLUDE_DEVS" ] ; then
168 for ed in $EXCLUDE_DEVS ; do
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
181 if [ -f /etc/multipath.conf ]; then
182 # If multipath check which type of devidec are given, mpath prefered
183 if [ -f $MINDI_TMP/pv.tmp2 ]; then
184 for d in `cat $MINDI_TMP/pv.tmp2`; do
185 skip=0
186 if [ "$EXCLUDE_DEVS" ] ; then
187 for ed in $EXCLUDE_DEVS ; do
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
200 else
201 if [ -f $MINDI_TMP/pv.tmp2 ]; then
202 cat $MINDI_TMP/pv.tmp2
203 fi
204 fi
205 rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
206}
207
208
209ListAllVolumeGroups() {
210 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
211}
212
213GiveMapperOfdm () {
214
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
230ListLvmDrivesAndPartitions() {
231 # We get partitions in this loop not devices
232 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
233 # If multipath check which type of devices are given, mpath prefered
234 if [ -f /etc/multipath.conf ]; then
235 i=`GiveMapperOfdm $d`
236 rep=$i
237 else
238 rep=$d
239 fi
240 skip=0
241 if [ "$EXCLUDE_DEVS" ] ; then
242 for ed in $EXCLUDE_DEVS ; do
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
253 done
254}
255
256
257
258PrettifyList() {
259 local i
260 echo -en "$1"
261 for i in $2 ; do
262 echo -en "$i "
263 done
264 echo ""
265}
266
267
268ListAllLogicalVolumes() {
269 if [ $lvmversion = 2 ]; then
270 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
271 else
272 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
273 fi
274}
275
276
277
278WriteShutdownScript() {
279 local i
280 echo ""
281 echo "Finally, to shut down and delete the volumes, do this:-"
282 for i in `ListAllLogicalVolumes` ; do
283 echo "($LVMCMD lvremove -f $i)"
284 done
285 for i in `ListAllVolumeGroups` ; do
286 echo "($LVMCMD vgchange -a n $i)"
287 done
288 for i in `ListAllVolumeGroups` ; do
289 echo "($LVMCMD vgremove $i)"
290 done
291 if [ $lvmversion = 2 ]; then
292 echo "(rmmod dm-mod & rmmod dm_mod & )"
293 else
294 echo "(rmmod lvm-mod)"
295 fi
296}
297
298
299
300# -------------------------------- main -----------------------------------
301which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
302if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
303 exit 1
304fi
305
306# Older lvmdiskscan use --help, newer --version
307lvmopt="--help"
308lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
309if [ $? -eq 0 ]; then
310 lvmopt="--version"
311fi
312
313
314lvmversion=`lvmdiskscan --help 2>&1 |
315 grep -E "Logical Volume Manager|LVM version:" |
316 cut -d: -f2 | cut -d. -f1 |
317 awk '{print $NF}' |
318 sed -e 's/ //g'`
319
320if which lvm 2>/dev/null; then
321 version=`lvm version | grep "LVM version" | awk '{print $3}'`
322 i="`echo "$version" | cut -d'.' -f1`"
323 echo "i=$i"
324 if [ "$i" -ge "2" ] ; then
325 lvmversion=2
326 fi
327fi
328
329if [ $lvmversion = 2 ]; then
330 echo "LVM version >= 2.0 found."
331 LVMCMD="lvm"
332else
333 LVMCMD=""
334fi
335
336all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
337echo "Just before you extrapolate mountlist to include RAID partitions,"
338echo "extrapolate it to include the following LVM drives and partitions:-"
339PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
340echo "To get started, type:-"
341if [ $lvmversion = 2 ]; then
342 echo "(insmod dm-mod)"
343 echo "(insmod dm_mod)"
344else
345 echo "(insmod lvm-mod)"
346fi
347echo "# $LVMCMD vgchange -an"
348for i in `ListAllPhysicalVolumes` ; do
349 echo "# echo y | $LVMCMD pvcreate -ff $i"
350done
351echo "# $LVMCMD vgscan"
352echo ""
353echo "Create and activate the VG's (volume groups)."
354all_volume_groups=`ListAllVolumeGroups`
355for current_VG in $all_volume_groups ; do
356 if [ $lvmversion -ne 2 ]; then
357 echo "# rm -Rf /dev/$current_VG"
358 fi
359 ProcessVolumeGroup $current_VG
360done
361echo ""
362echo "Finally, create the LV's (logical volumes)."
363all_logical_volumes=`ListAllLogicalVolumes`
364for current_LV in $all_logical_volumes ; do
365 ProcessLogicalVolume $current_LV
366done
367echo ""
368echo "# $LVMCMD vgscan"
369echo "Now you may format the LV's:-"
370for i in `ListAllLogicalVolumes` ; do
371 echo "(mkfs -t foo $i or something like that)"
372done
373WriteShutdownScript
374exit 0
375
376
377
Note: See TracBrowser for help on using the repository browser.