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

Last change on this file since 2531 was 2531, checked in by Bruno Cornec, 16 years ago

r3749@localhost: bruno | 2010-01-08 12:28:35 +0100

  • Still fixing the same issues on device exclusion and LVM
  • Property svn:keywords set to Id
File size: 8.7 KB
RevLine 
[673]1#!/bin/bash
[275]2#
3# $Id: analyze-my-lvm 2531 2010-01-08 12:58:43Z 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
[298]53 echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
[1]54}
55
56
57GenerateLvcreateParameters() {
[298]58 local device stripes stripesize device fname allocation output readahead
[1487]59 fname=$MINDI_TMP/PLF.$$.txt
[298]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
[911]69 if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
[298]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"
[1]80}
81
82
83
84GenerateVgcreateParameters() {
[298]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
[1487]87 VG_info_file=$MINDI_TMP/$$.vg-info.txt
[2342]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
[298]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
[1]101}
102
103
104
105
106
107ProcessVolumeGroup() {
[298]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`
[850]112 list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
[298]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
[2072]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
[298]133 echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
134 echo "# $LVMCMD vgchange -a y $current_VG"
[1]135}
136
137
138
139ListAllPhysicalVolumes() {
[298]140 if [ $lvmversion = 2 ]; then
[2072]141 $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
[298]142 else
[2072]143 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
[298]144 fi
[2072]145 if [ -f /etc/multipath.conf ]; then
146 # If multipath check which type of devidec are given, mpath prefered
147 for d in `cat $MINDI_TMP/pv.tmp`; do
148 GiveMapperOfdm $d
149 done
150 else
151 cat $MINDI_TMP/pv.tmp
152 fi
153 rm -f $MINDI_TMP/pv.tmp
[1]154}
155
156
157ListAllVolumeGroups() {
[298]158 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
[1]159}
160
[2072]161GiveMapperOfdm () {
[1]162
[2072]163major=`stat -c "%t" $1`
164minor=`stat -c "%T" $1`
165
166for i in `ls /dev/mapper/*`; do
167 mj=`stat -c "%t" $i`
168 mn=`stat -c "%T" $i`
169 if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
170 echo "$i"
171 return
172 fi
173done
174echo $1
175}
176
177
[1]178ListLvmDrivesAndPartitions() {
[2531]179 # We get partitions in this loop not devices
[2424]180 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
181 # Skip devices excluded, coming from mondoarchive
[2531]182 skip=0
183 if [ "$EXCLUDE_DEVS" ] ; then
184 for ed in $EXCLUDE_DEVS ; do
185 if [ "`echo " $d " | grep " $ed"`" != "" ]; then
186 echo "Excluding $d from i-want-my-lvm (due to excluded device $ed)"
187 skip=1
188 continue
189 fi
190 done
191 fi
192 if [ $skip -eq 1 ]; then
[2526]193 continue
[2424]194 fi
[2526]195 # If multipath check which type of devices are given, mpath prefered
[2424]196 if [ -f /etc/multipath.conf ]; then
[2526]197 i=`GiveMapperOfdm $d`
[2531]198 skip=0
199 if [ "$EXCLUDE_DEVS" ] ; then
200 for ed in $EXCLUDE_DEVS ; do
201 if [ "`echo " $i " | grep " $ed"`" != "" ]; then
202 echo "Excluding $i from i-want-my-lvm (due to excluded device $ed)"
203 skip=1
204 continue
205 fi
206 done
207 fi
208 if [ $skip -eq 1 ]; then
[2526]209 continue
210 fi
211 echo $i
[2424]212 else
213 echo $d
214 fi
215 done
[1]216}
217
218
219
220PrettifyList() {
[298]221 local i
222 echo -en "$1"
223 for i in $2 ; do
224 echo -en "$i "
225 done
226 echo ""
[1]227}
228
229
230ListAllLogicalVolumes() {
[298]231 if [ $lvmversion = 2 ]; then
[2244]232 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
[298]233 else
[2244]234 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
[298]235 fi
[1]236}
237
238
239
240WriteShutdownScript() {
[298]241 local i
242 echo ""
243 echo "Finally, to shut down and delete the volumes, do this:-"
[1]244 for i in `ListAllLogicalVolumes` ; do
[298]245 echo "($LVMCMD lvremove -f $i)"
[1]246 done
247 for i in `ListAllVolumeGroups` ; do
[298]248 echo "($LVMCMD vgchange -a n $i)"
[1]249 done
250 for i in `ListAllVolumeGroups` ; do
[298]251 echo "($LVMCMD vgremove $i)"
[1]252 done
[298]253 if [ $lvmversion = 2 ]; then
254 echo "(rmmod dm-mod & rmmod dm_mod & )"
255 else
256 echo "(rmmod lvm-mod)"
257 fi
[1]258}
259
260
261
262# -------------------------------- main -----------------------------------
[963]263which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
[2044]264if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
265 exit 1
[1]266fi
267
[2170]268# Older lvmdiskscan use --help, newer --version
269lvmopt="--help"
270lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
271if [ $? -eq 0 ]; then
272 lvmopt="--version"
273fi
274
275
[299]276lvmversion=`lvmdiskscan --help 2>&1 |
[1]277 grep -E "Logical Volume Manager|LVM version:" |
278 cut -d: -f2 | cut -d. -f1 |
279 awk '{print $NF}' |
280 sed -e 's/ //g'`
281
[97]282if which lvm 2>/dev/null; then
[298]283 version=`lvm version | grep "LVM version" | awk '{print $3}'`
284 i="`echo "$version" | cut -d'.' -f1`"
285 echo "i=$i"
286 if [ "$i" -ge "2" ] ; then
287 lvmversion=2
288 fi
[1]289fi
290
291if [ $lvmversion = 2 ]; then
[298]292 echo "LVM version >= 2.0 found."
293 LVMCMD="lvm"
294else
295 LVMCMD=""
[1]296fi
297
298all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
299echo "Just before you extrapolate mountlist to include RAID partitions,"
300echo "extrapolate it to include the following LVM drives and partitions:-"
301PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
302echo "To get started, type:-"
303if [ $lvmversion = 2 ]; then
[298]304 echo "(insmod dm-mod)"
305 echo "(insmod dm_mod)"
[1]306else
[298]307 echo "(insmod lvm-mod)"
[1]308fi
[298]309echo "# $LVMCMD vgchange -an"
[1]310for i in `ListAllPhysicalVolumes` ; do
[298]311 echo "# echo y | $LVMCMD pvcreate -ff $i"
[1]312done
[706]313echo "# $LVMCMD vgscan"
[1]314echo ""
315echo "Create and activate the VG's (volume groups)."
316all_volume_groups=`ListAllVolumeGroups`
317for current_VG in $all_volume_groups ; do
[298]318 if [ $lvmversion -ne 2 ]; then
319 echo "# rm -Rf /dev/$current_VG"
320 fi
321 ProcessVolumeGroup $current_VG
[1]322done
323echo ""
324echo "Finally, create the LV's (logical volumes)."
325all_logical_volumes=`ListAllLogicalVolumes`
326for current_LV in $all_logical_volumes ; do
[298]327 ProcessLogicalVolume $current_LV
[1]328done
329echo ""
[298]330echo "# $LVMCMD vgscan"
[1]331echo "Now you may format the LV's:-"
332for i in `ListAllLogicalVolumes` ; do
[298]333 echo "(mkfs -t foo $i or something like that)"
[1]334done
335WriteShutdownScript
336exit 0
337
338
339
Note: See TracBrowser for help on using the repository browser.