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

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