Ticket #413: analyze-my-lvm.with_mod

File analyze-my-lvm.with_mod, 10.7 KB (added by Michael Shapiro, 10 years ago)

analyze-my-lvm with line added to fix problem

Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 2571 2010-02-03 03:20:35Z bruno $
4#
5
6Die() {
7    echo "$1" >> /dev/stderr
8    exit -1
9}
10
11
12
13GetValueFromField() {
14    local res
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;};}'
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 --readalllink $LV_full_string`"
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 $MINDI_EXCLUDE_DEVS ; 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 $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 "# $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    device=$1
85    output=""
86    $LVMCMD lvdisplay $device > $fname
87    stripes=`GetValueFromField $fname "Stripes"`
88    stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
89    [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
90    [ "$stripesize" = "k" ] && stripesize=""
91    allocation=`GetValueFromField $fname "LV Size"`
92    [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
93    if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
94        val=`echo "$allocation" | sed s/g//`
95        allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
96    fi
97    readahead=`GetValueFromField $fname "Read ahead sectors"`
98    rm -f $fname
99    [ "$stripes" ]    && output="$output -i $stripes"
100    [ "$stripesize" ] && output="$output -I $stripesize"
101    [ "$allocation" ] && output="$output -L $allocation"
102    [ "$readahead" ]  && output="$output -r $readahead"
103    echo "$output"
104}
105
106
107
108GenerateVgcreateParameters() {
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
111    VG_info_file=$MINDI_TMP/$$.vg-info.txt
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
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
125}
126
127
128
129
130
131ProcessVolumeGroup() {
132    local current_VG physical_volumes i list_of_devices VG_params
133    current_VG=$1
134    if [ $lvmversion = 2 ]; then
135        VG_params=`GenerateVgcreateParameters $current_VG`
136        current_PVs=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
137        list_of_devices=$current_PVs
138    else
139        info_file=/proc/lvm/VGs/$current_VG/group
140        physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
141        VG_params=`GenerateVgcreateParameters $current_VG`
142        list_of_devices=""
143        for i in $physical_volumes ; do
144            fname=/proc/lvm/VGs/$current_VG/PVs/$i
145            device=`GetValueFromField $fname "name:"`
146            list_of_devices="$list_of_devices $device"
147        done
148        current_PVs=$list_of_devices
149    fi
150    l=""
151    if [ -f /etc/multipath.conf ]; then
152        # If multipath check which type of devide are given, mpath prefered
153        for d in $list_of_devices; do
154            l="$l `mindi --readalllink $d`"
155            l="$l `GiveMapperOfdm $d`"
156        done
157        list_of_devices="`echo $l | sort -u`"
158    fi
159
160    if [ "$MINDI_EXCLUDE_DEVS" ] ; then
161        for ed in $MINDI_EXCLUDE_DEVS ; do
162            if  [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
163                echo $current_VG >> $MINDI_TMP/excludedvgs
164                return
165            fi
166        done
167    fi
168    echo "# $LVMCMD vgcreate $current_VG$VG_params $current_PVs"
169    echo "# $LVMCMD vgchange -a y $current_VG"
170}
171
172
173
174ListAllPhysicalVolumes() {
175    if [ $lvmversion = 2 ]; then
176        $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' >  $MINDI_TMP/pv.tmp
177    else
178        pvscan 2> /dev/null | grep '"' | cut -d'"' -f2  >  $MINDI_TMP/pv.tmp
179    fi
180
181    rm -f $MINDI_TMP/pv.tmp2
182    l=""
183    for d in `cat $MINDI_TMP/pv.tmp`; do
184        # Skip devices excluded, coming from mondoarchive
185        skip=0
186        l=""
187        l="$l `mindi --readalllink $d`"
188        l="$l `GiveMapperOfdm $d`"
189        list_of_devices="`echo $l | sort -u`"
190        if [ "$MINDI_EXCLUDE_DEVS" ] ; then
191            for ed in $MINDI_EXCLUDE_DEVS ; do
192                if  [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
193                    skip=1
194                    continue
195                fi
196            done
197        fi
198        if [ $skip -eq 1 ]; then
199            continue
200        fi
201        echo $d >> $MINDI_TMP/pv.tmp2
202    done
203
204    if [ -f /etc/multipath.conf ]; then
205        # If multipath check which type of devide are given, mpath prefered
206        if [ -f  $MINDI_TMP/pv.tmp2 ]; then
207            l=""
208            for d in `cat $MINDI_TMP/pv.tmp2`; do
209                skip=0
210                l="$l `mindi --readalllink $d`"
211                l="$l `GiveMapperOfdm $d`"
212                list_of_devices="`echo $l | sort -u`"
213                if [ "$MINDI_EXCLUDE_DEVS" ] ; then
214                    for ed in $MINDI_EXCLUDE_DEVS ; do
215                        if  [ "`echo " $list_of_devices " | grep " $ed"`" != "" ]; then
216                            skip=1
217                            continue
218                        fi
219                    done
220                fi
221                if [ $skip -eq 1 ]; then
222                    continue
223                fi
224                GiveMapperOfdm $d
225            done
226        fi
227    else
228        if [ -f  $MINDI_TMP/pv.tmp2 ]; then
229            cat $MINDI_TMP/pv.tmp2
230        fi
231    fi
232    rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
233}
234
235
236ListAllVolumeGroups() {
237    $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
238}
239
240GiveMapperOfdm () {
241
242major=`stat -c "%t" $1`
243minor=`stat -c "%T" $1`
244
245for i in `ls /dev/mapper/*`; do
246    mj=`stat -c "%t" $i`
247    mn=`stat -c "%T" $i`
248    if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
249        echo "$i"
250        return
251    fi
252done
253echo $1
254}
255
256
257ListLvmDrivesAndPartitions() {
258    # We get partitions in this loop not devices
259    for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do 
260        # If multipath check which type of devices are given, mpath prefered
261        if [ -f /etc/multipath.conf ]; then
262            i=`GiveMapperOfdm $d`
263            rep=$i
264        else
265            rep=$d
266        fi
267        skip=0
268        if [ "$MINDI_EXCLUDE_DEVS" ] ; then
269            for ed in $MINDI_EXCLUDE_DEVS ; do
270                if  [ "`echo " $rep " | grep " $ed"`" != "" ]; then
271                    skip=1
272                    continue
273                fi
274            done
275        fi
276        if [ $skip -eq 1 ]; then
277            continue
278        fi
279        echo $rep
280    done
281}
282
283
284
285PrettifyList() {
286    local i
287    echo -en "$1"
288    for i in $2 ; do
289        echo -en "$i "
290    done
291    echo ""
292}
293
294
295ListAllLogicalVolumes() {
296    if [ $lvmversion = 2 ]; then
297        $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
298    else
299        lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
300    fi
301}
302
303
304
305WriteShutdownScript() {
306    local i
307    echo ""
308    echo "Finally, to shut down and delete the volumes, do this:-"
309    for i in `ListAllLogicalVolumes` ; do
310        echo "($LVMCMD lvremove -f $i)"
311    done
312    for i in `ListAllVolumeGroups` ; do
313        echo "($LVMCMD vgchange -a n $i)"
314    done
315    for i in `ListAllVolumeGroups` ; do
316        echo "($LVMCMD vgremove $i)"
317    done
318    if [ $lvmversion = 2 ]; then
319        echo "(rmmod dm-mod & rmmod dm_mod & )"
320    else
321        echo "(rmmod lvm-mod)"
322    fi
323}
324
325
326
327# -------------------------------- main -----------------------------------
328which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
329if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
330    exit 1
331fi
332
333if [ _"$MINDI_TMP" = _"" ]; then
334    # Launched stdalone, so create a temp dir
335    MINDI_TMP=`mktemp -d $TMPDIR/mindi.XXXXXXXXXX`
336    if [ $? -ne 0 ]; then
337        df $TMPDIR
338        Die "Unable to create a temporary directory ! Check space on $TMPDIR"
339    fi
340    if [ _"$MINDI_TMP" = _"" ]; then
341        Die "MINDI_TMP is empty, aborting"
342    fi
343    if [ _"$MINDI_TMP" = _"/" ]; then
344        Die "MINDI_TMP is /, aborting"
345    fi
346fi
347
348# Older lvmdiskscan use --help, newer --version
349lvmopt="--help"
350lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
351if [ $? -eq 0 ]; then
352    lvmopt="--version"
353fi
354
355
356lvmversion=`lvmdiskscan --help 2>&1 |
357  grep -E "Logical Volume Manager|LVM version:" |
358  cut -d: -f2 | cut -d. -f1 |
359  awk '{print $NF}' |
360  sed -e 's/ //g'`
361
362if which lvm 2>/dev/null; then
363    version=`lvm version | grep "LVM version" | awk '{print $3}'`
364    i="`echo "$version" | cut -d'.' -f1`"
365    echo "i=$i"
366    if [ "$i" -ge "2" ] ; then
367        lvmversion=2
368    fi
369fi
370
371if [ $lvmversion = 2 ]; then
372    echo "LVM version >= 2.0 found."
373    LVMCMD="lvm"
374else
375    LVMCMD=""
376fi
377
378rm -f $MINDI_TMP/excludedvgs
379all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
380echo "Just before you extrapolate mountlist to include RAID partitions,"
381echo "extrapolate it to include the following LVM drives and partitions:-"
382PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
383echo "To get started, type:-"
384if [ $lvmversion = 2 ]; then
385    echo "(insmod dm-mod)"
386    echo "(insmod dm_mod)"
387else
388    echo "(insmod lvm-mod)"
389fi
390echo "# $LVMCMD vgchange -an"
391for i in `ListAllPhysicalVolumes` ; do
392    echo "# echo y | $LVMCMD pvcreate -ff $i"
393done
394echo "# $LVMCMD vgscan"
395echo ""
396echo "Create and activate the VG's (volume groups)."
397all_volume_groups=`ListAllVolumeGroups`
398for current_VG in $all_volume_groups ; do
399    if [ $lvmversion -ne 2 ]; then
400        echo "# rm -Rf /dev/$current_VG"
401    fi
402    ProcessVolumeGroup $current_VG
403done
404echo ""
405echo "Finally, create the LV's (logical volumes)."
406all_logical_volumes=`ListAllLogicalVolumes`
407for current_LV in $all_logical_volumes ; do
408    ProcessLogicalVolume $current_LV
409done
410echo ""
411echo "# $LVMCMD vgscan"
412echo "Now you may format the LV's:-"
413for i in `ListAllLogicalVolumes` ; do
414    echo "(mkfs -t foo $i or something like that)"
415done
416rm -f $MINDI_TMP/excludedvgs
417WriteShutdownScript
418exit 0
419
420
421