source: MondoRescue/branches/3.2/mindi/analyze-my-lvm@ 3432

Last change on this file since 3432 was 3432, checked in by Bruno Cornec, 9 years ago
  • Change analyze-my-lvm in order to generate percentage of Extents of a VG to use to create the LV instead of a fixed size. Should improve bugs when creating LVM on a disk of different size greatly.
  • Property svn:keywords set to Id
File size: 11.9 KB
RevLine 
[673]1#!/bin/bash
[275]2#
3# $Id: analyze-my-lvm 3432 2015-08-28 00:34:47Z 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
[3275]15 grep -i "$2" "$1" | perl -p -e 's/\s*(.*)\s\s\s+(.+)$/$2/;s/\sT[i]*B/t/;s/\sG[i]*B/g/;s/\sM[i]*B/m/;s/\sK[i]*B/k/'
[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
[2566]53
54 # Exclude LVs member of that env var
55 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
[2995]56 list_of_devices="`mindi --nolog --readalllink $LV_full_string 2> /dev/null`"
[2567]57 l=""
58 for d in $list_of_devices; do
59 l="$l `GiveMapperOfdm $d`"
60 done
61 list_of_devices="`echo $l | sort -u`"
[2852]62 for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
[2566]63 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
64 echo "Not including device $LV_full_string as it was excluded"
65 return
66 fi
67 done
68 fi
[2534]69 # Do not process LV whose VG are excluded
[2537]70 if [ -f $MINDI_TMP/excludedvgs ]; then
[2684]71 if [ "`grep -x $volume_group $MINDI_TMP/excludedvgs`" != "" ]; then
[2565]72 echo "Not including LV $logical_volume as VG $volume_group was excluded"
[2566]73 return
[2537]74 fi
[2534]75 fi
[2566]76
[3312]77 echo "# echo y | $LVMCMD lvcreate$params -n $logical_volume $volume_group"
[1]78}
79
80
81GenerateLvcreateParameters() {
[298]82 local device stripes stripesize device fname allocation output readahead
[1487]83 fname=$MINDI_TMP/PLF.$$.txt
[3432]84 fname2=$MINDI_TMP/PLF2.$$.txt
[298]85 device=$1
86 output=""
[3016]87 $LVMCMD lvdisplay $device | cat > $fname
[298]88 stripes=`GetValueFromField $fname "Stripes"`
89 stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
90 [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
91 [ "$stripesize" = "k" ] && stripesize=""
[3432]92 currentLE=`GetValueFromField $fname "Current LE"`
93 vgname=`GetValueFromField $fname "VG Name"`
94 $LVMCMD vgdisplay $vgname | cat > $fname2
95 totalLE=`GetValueFromField $fname2 "Total PE"`
96 allocation=`perl -e '$per='$currentLE'*100/'$totalLE' ; print int($per+0.5)."%VG";'`
[298]97 readahead=`GetValueFromField $fname "Read ahead sectors"`
[3432]98 rm -f $fname $fname2
[298]99 [ "$stripes" ] && output="$output -i $stripes"
100 [ "$stripesize" ] && output="$output -I $stripesize"
[3432]101 [ "$allocation" ] && output="$output -l $allocation"
[298]102 [ "$readahead" ] && output="$output -r $readahead"
103 echo "$output"
[1]104}
105
106
107
108GenerateVgcreateParameters() {
[298]109 local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
110 current_VG=$1
[1487]111 VG_info_file=$MINDI_TMP/$$.vg-info.txt
[2342]112 # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
113 $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
[298]114 max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
115 [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
116 max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
117 [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
118 physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
119 output=""
120 [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
121 [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
122 [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
123 echo "$output"
124 rm -f $VG_info_file
[1]125}
126
127
128
129ProcessVolumeGroup() {
[298]130 local current_VG physical_volumes i list_of_devices VG_params
131 current_VG=$1
132 if [ $lvmversion = 2 ]; then
133 VG_params=`GenerateVgcreateParameters $current_VG`
[2702]134 current_PVs=`$LVMCMD pvscan | grep " $current_VG " | awk '{print $2}' | tr '\n' ' '`
[2571]135 list_of_devices=$current_PVs
[298]136 else
137 info_file=/proc/lvm/VGs/$current_VG/group
138 physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
139 VG_params=`GenerateVgcreateParameters $current_VG`
140 list_of_devices=""
141 for i in $physical_volumes ; do
[2700]142 fname=/proc/lvm/VGs/$current_VG/PVs/$i
143 device=`GetValueFromField $fname "name:"`
144 list_of_devices="$list_of_devices $device"
[298]145 done
[2571]146 current_PVs=$list_of_devices
[298]147 fi
[2072]148 l=""
149 if [ -f /etc/multipath.conf ]; then
[2567]150 # If multipath check which type of devide are given, mpath prefered
[2072]151 for d in $list_of_devices; do
[2995]152 l="$l `mindi --nolog --readalllink $d 2> /dev/null`"
[2072]153 l="$l `GiveMapperOfdm $d`"
154 done
[2567]155 list_of_devices="`echo $l | sort -u`"
[2072]156 fi
157
[2564]158 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
[2852]159 for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
[2532]160 if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
[2534]161 echo $current_VG >> $MINDI_TMP/excludedvgs
[2532]162 return
163 fi
164 done
165 fi
[2571]166 echo "# $LVMCMD vgcreate $current_VG$VG_params $current_PVs"
[298]167 echo "# $LVMCMD vgchange -a y $current_VG"
[1]168}
169
170
171
172ListAllPhysicalVolumes() {
[298]173 if [ $lvmversion = 2 ]; then
[2620]174 $LVMCMD pvscan 2> /dev/null | grep 'PV' | grep -v unknown | awk '{print $2}' > $MINDI_TMP/pv.tmp
[298]175 else
[2072]176 pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
[298]177 fi
[2532]178
179 rm -f $MINDI_TMP/pv.tmp2
180 for d in `cat $MINDI_TMP/pv.tmp`; do
181 # Skip devices excluded, coming from mondoarchive
182 skip=0
[2616]183 l=""
[2995]184 l="$l `mindi --nolog --readalllink $d 2> /dev/null`"
[2567]185 l="$l `GiveMapperOfdm $d`"
186 list_of_devices="`echo $l | sort -u`"
[2564]187 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
[2852]188 for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
[2567]189 if [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
[2532]190 skip=1
191 continue
192 fi
193 done
194 fi
195 if [ $skip -eq 1 ]; then
196 continue
197 fi
198 echo $d >> $MINDI_TMP/pv.tmp2
199 done
200
[2072]201 if [ -f /etc/multipath.conf ]; then
[2567]202 # If multipath check which type of devide are given, mpath prefered
[2554]203 if [ -f $MINDI_TMP/pv.tmp2 ]; then
[2567]204 l=""
[2554]205 for d in `cat $MINDI_TMP/pv.tmp2`; do
206 skip=0
[2995]207 l="$l `mindi --nolog --readalllink $d 2> /dev/null`"
[2567]208 l="$l `GiveMapperOfdm $d`"
209 list_of_devices="`echo $l | sort -u`"
[2564]210 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
[2852]211 for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
[2567]212 if [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
[2554]213 skip=1
214 continue
215 fi
216 done
217 fi
218 if [ $skip -eq 1 ]; then
219 continue
220 fi
221 GiveMapperOfdm $d
222 done
223 fi
[2072]224 else
[2554]225 if [ -f $MINDI_TMP/pv.tmp2 ]; then
226 cat $MINDI_TMP/pv.tmp2
227 fi
[2072]228 fi
[2532]229 rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
[1]230}
231
232
233ListAllVolumeGroups() {
[298]234 $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
[1]235}
236
[2072]237GiveMapperOfdm () {
[1]238
[3001]239major=`stat -L -c "%t" $1 2> /dev/null`
240minor=`stat -L -c "%T" $1 2> /dev/null`
[2072]241
242for i in `ls /dev/mapper/*`; do
[3001]243 mj=`stat -L -c "%t" $i`
244 mn=`stat -L -c "%T" $i`
[2072]245 if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
246 echo "$i"
247 return
248 fi
249done
250echo $1
251}
252
[3066]253GiveVGLVOfdm () {
[2072]254
[3066]255major=`stat -L -c "%t" $1 2> /dev/null`
256minor=`stat -L -c "%T" $1 2> /dev/null`
257
258for v in `vgs --noheadings | awk '{print $1}'`; do
259 for i in `ls /dev/$v/*`; do
260 mj=`stat -L -c "%t" $i`
261 mn=`stat -L -c "%T" $i`
262 if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
263 echo "$i"
264 return
265 fi
266 done
267done
268echo $1
269}
270
271
[1]272ListLvmDrivesAndPartitions() {
[2531]273 # We get partitions in this loop not devices
[2424]274 for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
[2526]275 # If multipath check which type of devices are given, mpath prefered
[2424]276 if [ -f /etc/multipath.conf ]; then
[2526]277 i=`GiveMapperOfdm $d`
[2536]278 rep=$i
[2424]279 else
[2536]280 rep=$d
[2424]281 fi
[2536]282 skip=0
[2564]283 if [ "$MINDI_EXCLUDE_DEVS" ] ; then
[2852]284 for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
[2536]285 if [ "`echo " $rep " | grep " $ed"`" != "" ]; then
286 skip=1
287 continue
288 fi
289 done
290 fi
291 if [ $skip -eq 1 ]; then
292 continue
293 fi
294 echo $rep
[2424]295 done
[1]296}
297
298
299
300PrettifyList() {
[298]301 local i
302 echo -en "$1"
303 for i in $2 ; do
304 echo -en "$i "
305 done
306 echo ""
[1]307}
308
[2925]309ListAllLogicalVolumesSortedBydm() {
[3029]310 rm -f $MINDI_TMP/sorteddm
311 for d in `ListAllLogicalVolumes` ; do
312 dm=`mindi --nolog --readalllink $d 2> /dev/null | tail -1`
313 echo "$dm|$d" >> $MINDI_TMP/sorteddm
314 done
315 if [ -f $MINDI_TMP/sorteddm ]; then
[3037]316 cut -d'-' -f2- $MINDI_TMP/sorteddm | sort -t'|' -n | cut -d'|' -f2
[3029]317 fi
[2925]318}
[1]319
320ListAllLogicalVolumes() {
[298]321 if [ $lvmversion = 2 ]; then
[2244]322 $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
[298]323 else
[2244]324 lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
[298]325 fi
[1]326}
327
328
329
330WriteShutdownScript() {
[298]331 local i
332 echo ""
333 echo "Finally, to shut down and delete the volumes, do this:-"
[1]334 for i in `ListAllLogicalVolumes` ; do
[298]335 echo "($LVMCMD lvremove -f $i)"
[1]336 done
337 for i in `ListAllVolumeGroups` ; do
[298]338 echo "($LVMCMD vgchange -a n $i)"
[1]339 done
340 for i in `ListAllVolumeGroups` ; do
[298]341 echo "($LVMCMD vgremove $i)"
[1]342 done
[298]343 if [ $lvmversion = 2 ]; then
344 echo "(rmmod dm-mod & rmmod dm_mod & )"
345 else
346 echo "(rmmod lvm-mod)"
347 fi
[1]348}
349
350
351
352# -------------------------------- main -----------------------------------
[2579]353
[3063]354# These exports are needed to avoid bad formating of display commands (, instead of . for separator e.g.)
355# Fix #654
356export LANG=C
357export LANGUAGE=C
358
[2579]359if [ "$1" = "--givemapperofdm" ] ; then
360 shift
361 if [ _"$1" != _"" ] ; then
362 GiveMapperOfdm $1
363 fi
364 exit 0
365fi
366
[3066]367if [ "$1" = "--givevglvofdm" ] ; then
368 shift
369 if [ _"$1" != _"" ] ; then
370 GiveVGLVOfdm $1
371 fi
372 exit 0
373fi
[2579]374
[3066]375
[963]376which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
[2044]377if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
378 exit 1
[1]379fi
380
[2565]381if [ _"$MINDI_TMP" = _"" ]; then
382 # Launched stdalone, so create a temp dir
[2719]383 STDALONE="true"
[2565]384 MINDI_TMP=`mktemp -d $TMPDIR/mindi.XXXXXXXXXX`
385 if [ $? -ne 0 ]; then
386 df $TMPDIR
387 Die "Unable to create a temporary directory ! Check space on $TMPDIR"
388 fi
389 if [ _"$MINDI_TMP" = _"" ]; then
390 Die "MINDI_TMP is empty, aborting"
391 fi
392 if [ _"$MINDI_TMP" = _"/" ]; then
393 Die "MINDI_TMP is /, aborting"
394 fi
395fi
396
[2170]397# Older lvmdiskscan use --help, newer --version
398lvmopt="--help"
399lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
400if [ $? -eq 0 ]; then
401 lvmopt="--version"
402fi
403
404
[299]405lvmversion=`lvmdiskscan --help 2>&1 |
[1]406 grep -E "Logical Volume Manager|LVM version:" |
407 cut -d: -f2 | cut -d. -f1 |
408 awk '{print $NF}' |
409 sed -e 's/ //g'`
410
[97]411if which lvm 2>/dev/null; then
[298]412 version=`lvm version | grep "LVM version" | awk '{print $3}'`
413 i="`echo "$version" | cut -d'.' -f1`"
414 echo "i=$i"
415 if [ "$i" -ge "2" ] ; then
416 lvmversion=2
417 fi
[1]418fi
419
420if [ $lvmversion = 2 ]; then
[298]421 echo "LVM version >= 2.0 found."
422 LVMCMD="lvm"
423else
424 LVMCMD=""
[1]425fi
426
[2565]427rm -f $MINDI_TMP/excludedvgs
[1]428all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
429echo "Just before you extrapolate mountlist to include RAID partitions,"
430echo "extrapolate it to include the following LVM drives and partitions:-"
431PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
432echo "To get started, type:-"
433if [ $lvmversion = 2 ]; then
[298]434 echo "(insmod dm-mod)"
435 echo "(insmod dm_mod)"
[1]436else
[298]437 echo "(insmod lvm-mod)"
[1]438fi
[298]439echo "# $LVMCMD vgchange -an"
[1]440for i in `ListAllPhysicalVolumes` ; do
[298]441 echo "# echo y | $LVMCMD pvcreate -ff $i"
[1]442done
[706]443echo "# $LVMCMD vgscan"
[1]444echo ""
445echo "Create and activate the VG's (volume groups)."
446all_volume_groups=`ListAllVolumeGroups`
447for current_VG in $all_volume_groups ; do
[298]448 if [ $lvmversion -ne 2 ]; then
449 echo "# rm -Rf /dev/$current_VG"
450 fi
451 ProcessVolumeGroup $current_VG
[1]452done
453echo ""
454echo "Finally, create the LV's (logical volumes)."
[2925]455all_logical_volumes=`ListAllLogicalVolumesSortedBydm`
[1]456for current_LV in $all_logical_volumes ; do
[298]457 ProcessLogicalVolume $current_LV
[1]458done
459echo ""
[298]460echo "# $LVMCMD vgscan"
[1]461echo "Now you may format the LV's:-"
462for i in `ListAllLogicalVolumes` ; do
[298]463 echo "(mkfs -t foo $i or something like that)"
[1]464done
[2565]465rm -f $MINDI_TMP/excludedvgs
[1]466WriteShutdownScript
[2719]467if [ _"$STDALONE" = _"true" ]; then
468 rm -rf $MINDI_TMP
469fi
[1]470exit 0
471
472
473
Note: See TracBrowser for help on using the repository browser.