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

Last change on this file since 2534 was 2534, checked in by Bruno Cornec, 14 years ago

r3755@localhost: bruno | 2010-01-08 14:31:20 +0100

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