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

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

r3751@localhost: bruno | 2010-01-08 14:03:56 +0100
LVM excludion based on devices was flawed as the function targeted was not really useful. That script shold be fully rewritten to have a data structure contaiing what is needed. Anyway the current patch should allow to have some improvements

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