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

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

r3761@localhost: bruno | 2010-01-08 15:59:41 +0100

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