source: branches/3.2/mindi/analyze-my-lvm @ 3607

Last change on this file since 3607 was 3559, checked in by bruno, 3 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
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 3559 2016-04-11 10:45:32Z bruno $
4#
5
6Die() {
7    echo "$1" >> /dev/stderr
8    exit -1
9}
10
11
12
13GetValueFromField() {
14    local res
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/'
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
54    # Exclude LVs member of that env var
55    if [ "$MINDI_EXCLUDE_DEVS" ] ; then
56        list_of_devices="`mindi --nolog --readalllink $LV_full_string 2> /dev/null`"
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`"
62        for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
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
69    # Do not process LV whose VG are excluded
70    if [ -f $MINDI_TMP/excludedvgs ]; then
71        if [ "`grep -x $volume_group $MINDI_TMP/excludedvgs`" != "" ]; then
72            echo "Not including LV $logical_volume as VG $volume_group was excluded"
73            return
74        fi
75    fi
76
77    echo "# echo y | $LVMCMD lvcreate$params -n $logical_volume $volume_group"
78}
79
80
81GenerateLvcreateParameters() {
82    local device stripes stripesize device fname allocation output readahead
83    fname=$MINDI_TMP/PLF.$$.txt
84    fname2=$MINDI_TMP/PLF2.$$.txt
85    device=$1
86    output=""
87    $LVMCMD lvdisplay $device | cat > $fname
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=""
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    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
104    readahead=`GetValueFromField $fname "Read ahead sectors"`
105    rm -f $fname $fname2
106    [ "$stripes" ]    && output="$output -i $stripes"
107    [ "$stripesize" ] && output="$output -I $stripesize"
108    [ "$readahead" ]  && output="$output -r $readahead"
109    echo "$output"
110}
111
112
113
114GenerateVgcreateParameters() {
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
117    VG_info_file=$MINDI_TMP/$$.vg-info.txt
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
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
131}
132
133
134
135ProcessVolumeGroup() {
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`
140        current_PVs=`$LVMCMD pvscan | grep " $current_VG " | awk '{print $2}' | tr '\n' ' '`
141        list_of_devices=$current_PVs
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
148            fname=/proc/lvm/VGs/$current_VG/PVs/$i
149            device=`GetValueFromField $fname "name:"`
150            list_of_devices="$list_of_devices $device"
151        done
152        current_PVs=$list_of_devices
153    fi
154    l=""
155    if [ -f /etc/multipath.conf ]; then
156        # If multipath check which type of devide are given, mpath prefered
157        for d in $list_of_devices; do
158            l="$l `mindi --nolog --readalllink $d 2> /dev/null`"
159            l="$l `GiveMapperOfdm $d`"
160        done
161        list_of_devices="`echo $l | sort -u`"
162    fi
163
164    if [ "$MINDI_EXCLUDE_DEVS" ] ; then
165        for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
166            if  [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
167                echo $current_VG >> $MINDI_TMP/excludedvgs
168                return
169            fi
170        done
171    fi
172    echo "# $LVMCMD vgcreate $current_VG$VG_params $current_PVs"
173    echo "# $LVMCMD vgchange -a y $current_VG"
174}
175
176
177
178ListAllPhysicalVolumes() {
179    if [ $lvmversion = 2 ]; then
180        $LVMCMD pvscan 2> /dev/null | grep 'PV' | grep -v unknown | awk '{print $2}' >  $MINDI_TMP/pv.tmp
181    else
182        pvscan 2> /dev/null | grep '"' | cut -d'"' -f2  >  $MINDI_TMP/pv.tmp
183    fi
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
189        l=""
190        l="$l `mindi --nolog --readalllink $d 2> /dev/null`"
191        l="$l `GiveMapperOfdm $d`"
192        list_of_devices="`echo $l | sort -u`"
193        if [ "$MINDI_EXCLUDE_DEVS" ] ; then
194            for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
195                if  [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
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
207    if [ -f /etc/multipath.conf ]; then
208        # If multipath check which type of devide are given, mpath prefered
209        if [ -f  $MINDI_TMP/pv.tmp2 ]; then
210            l=""
211            for d in `cat $MINDI_TMP/pv.tmp2`; do
212                skip=0
213                l="$l `mindi --nolog --readalllink $d 2> /dev/null`"
214                l="$l `GiveMapperOfdm $d`"
215                list_of_devices="`echo $l | sort -u`"
216                if [ "$MINDI_EXCLUDE_DEVS" ] ; then
217                    for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
218                        if  [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
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
230    else
231        if [ -f  $MINDI_TMP/pv.tmp2 ]; then
232            cat $MINDI_TMP/pv.tmp2
233        fi
234    fi
235    rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
236}
237
238
239ListAllVolumeGroups() {
240    $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
241}
242
243GiveMapperOfdm () {
244
245major=`stat -L -c "%t" $1 2> /dev/null`
246minor=`stat -L -c "%T" $1 2> /dev/null`
247
248for i in `ls /dev/mapper/*`; do
249    mj=`stat -L -c "%t" $i`
250    mn=`stat -L -c "%T" $i`
251    if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
252        echo "$i"
253        return
254    fi
255done
256echo $1
257}
258
259GiveVGLVOfdm () {
260
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
278ListLvmDrivesAndPartitions() {
279    # We get partitions in this loop not devices
280    for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do 
281        # If multipath check which type of devices are given, mpath prefered
282        if [ -f /etc/multipath.conf ]; then
283            i=`GiveMapperOfdm $d`
284            rep=$i
285        else
286            rep=$d
287        fi
288        skip=0
289        if [ "$MINDI_EXCLUDE_DEVS" ] ; then
290            for ed in `echo $MINDI_EXCLUDE_DEVS | sed 's/|/ /g'`; do
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
301    done
302}
303
304
305
306PrettifyList() {
307    local i
308    echo -en "$1"
309    for i in $2 ; do
310        echo -en "$i "
311    done
312    echo ""
313}
314
315ListAllLogicalVolumesSortedBydm() {
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
322        cut -d'-' -f2- $MINDI_TMP/sorteddm | sort -t'|' -n | cut -d'|' -f2
323    fi
324}
325
326ListAllLogicalVolumes() {
327    if [ $lvmversion = 2 ]; then
328        $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
329    else
330        lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
331    fi
332}
333
334
335
336WriteShutdownScript() {
337    local i
338    echo ""
339    echo "Finally, to shut down and delete the volumes, do this:-"
340    for i in `ListAllLogicalVolumes` ; do
341        echo "($LVMCMD lvremove -f $i)"
342    done
343    for i in `ListAllVolumeGroups` ; do
344        echo "($LVMCMD vgchange -a n $i)"
345    done
346    for i in `ListAllVolumeGroups` ; do
347        echo "($LVMCMD vgremove $i)"
348    done
349    if [ $lvmversion = 2 ]; then
350        echo "(rmmod dm-mod & rmmod dm_mod & )"
351    else
352        echo "(rmmod lvm-mod)"
353    fi
354}
355
356
357
358# -------------------------------- main -----------------------------------
359
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
365if [ "$1" = "--givemapperofdm" ] ; then
366    shift
367    if [ _"$1" != _"" ] ; then
368        GiveMapperOfdm $1
369    fi
370    exit 0
371fi
372
373if [ "$1" = "--givevglvofdm" ] ; then
374    shift
375    if [ _"$1" != _"" ] ; then
376        GiveVGLVOfdm $1
377    fi
378    exit 0
379fi
380
381
382which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
383if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
384    exit 1
385fi
386
387if [ _"$MINDI_TMP" = _"" ]; then
388    # Launched stdalone, so create a temp dir
389    STDALONE="true"
390    TMPDIR=${TMPDIR:=/tmp}
391    if [ ! -d $TMPDIR ]; then
392        mkdir -p $TMPDIR
393    fi
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
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
415lvmversion=`lvmdiskscan --help 2>&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
421if which lvm 2>/dev/null; then
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
428fi
429
430if [ $lvmversion = 2 ]; then
431    echo "LVM version >= 2.0 found."
432    LVMCMD="lvm"
433else
434    LVMCMD=""
435fi
436
437rm -f $MINDI_TMP/excludedvgs
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
444    echo "(insmod dm-mod)"
445    echo "(insmod dm_mod)"
446else
447    echo "(insmod lvm-mod)"
448fi
449echo "# $LVMCMD vgchange -an"
450for i in `ListAllPhysicalVolumes` ; do
451    echo "# echo y | $LVMCMD pvcreate -ff $i"
452done
453echo "# $LVMCMD vgscan"
454echo ""
455echo "Create and activate the VG's (volume groups)."
456all_volume_groups=`ListAllVolumeGroups`
457for current_VG in $all_volume_groups ; do
458    if [ $lvmversion -ne 2 ]; then
459        echo "# rm -Rf /dev/$current_VG"
460    fi
461    ProcessVolumeGroup $current_VG
462done
463echo ""
464echo "Finally, create the LV's (logical volumes)."
465all_logical_volumes=`ListAllLogicalVolumesSortedBydm`
466for current_LV in $all_logical_volumes ; do
467    ProcessLogicalVolume $current_LV
468done
469echo ""
470echo "# $LVMCMD vgscan"
471echo "Now you may format the LV's:-"
472for i in `ListAllLogicalVolumes` ; do
473    echo "(mkfs -t foo $i or something like that)"
474done
475rm -f $MINDI_TMP/excludedvgs
476WriteShutdownScript
477if [ _"$STDALONE" = _"true" ]; then
478    rm -rf $MINDI_TMP
479fi
480exit 0
481
482
483
Note: See TracBrowser for help on using the repository browser.