source: branches/2.2.9/mindi/analyze-my-lvm @ 2536

Last change on this file since 2536 was 2536, checked in by bruno, 9 years ago

r3759@localhost: bruno | 2010-01-08 15:18:49 +0100

  • Finally the function ListLvmDrivesAndPartitions? is used, as it creates info parsed by mindi !! So reverting back to filter excluded devices in it
  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 2536 2010-01-08 15:49:02Z 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    # Do not process LV whose VG are excluded
54    if ["`grep $volume_group $MINDI_TMP/excludedvgs`" = "" ]; then
55        echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
56    fi
57    rm -f $MINDI_TMP/excludedvgs
58}
59
60
61GenerateLvcreateParameters() {
62    local device stripes stripesize device fname allocation output readahead
63    fname=$MINDI_TMP/PLF.$$.txt
64    device=$1
65    output=""
66    $LVMCMD lvdisplay $device > $fname
67    stripes=`GetValueFromField $fname "Stripes"`
68    stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
69    [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
70    [ "$stripesize" = "k" ] && stripesize=""
71    allocation=`GetValueFromField $fname "LV Size"`
72    [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
73    if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
74        val=`echo "$allocation" | sed s/g//`
75        allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
76    fi
77    readahead=`GetValueFromField $fname "Read ahead sectors"`
78    rm -f $fname
79    [ "$stripes" ]    && output="$output -i $stripes"
80    [ "$stripesize" ] && output="$output -I $stripesize"
81    [ "$allocation" ] && output="$output -L $allocation"
82    [ "$readahead" ]  && output="$output -r $readahead"
83    echo "$output"
84}
85
86
87
88GenerateVgcreateParameters() {
89    local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
90    current_VG=$1
91    VG_info_file=$MINDI_TMP/$$.vg-info.txt
92    # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
93    $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
94    max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
95    [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
96    max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
97    [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
98    physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
99    output=""
100    [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
101    [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
102    [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
103    echo "$output"
104    rm -f $VG_info_file
105}
106
107
108
109
110
111ProcessVolumeGroup() {
112    local current_VG physical_volumes i list_of_devices VG_params
113    current_VG=$1
114    if [ $lvmversion = 2 ]; then
115        VG_params=`GenerateVgcreateParameters $current_VG`
116        list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
117    else
118        info_file=/proc/lvm/VGs/$current_VG/group
119        physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
120        VG_params=`GenerateVgcreateParameters $current_VG`
121        list_of_devices=""
122        for i in $physical_volumes ; do
123            fname=/proc/lvm/VGs/$current_VG/PVs/$i
124            device=`GetValueFromField $fname "name:"`
125            list_of_devices="$list_of_devices $device"
126        done
127    fi
128    l=""
129    if [ -f /etc/multipath.conf ]; then
130        # If multipath check which type of devidec are given, mpath prefered
131        for d in $list_of_devices; do
132            l="$l `GiveMapperOfdm $d`"
133        done
134        list_of_devices=$l
135    fi
136
137    rm -f $MINDI_TMP/excludedvgs
138    if [ "$EXCLUDE_DEVS" ] ; then
139        for ed in $EXCLUDE_DEVS ; do
140            if  [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
141                echo $current_VG >> $MINDI_TMP/excludedvgs
142                return
143            fi
144        done
145    fi
146    echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
147    echo "# $LVMCMD vgchange -a y $current_VG"
148}
149
150
151
152ListAllPhysicalVolumes() {
153    if [ $lvmversion = 2 ]; then
154        $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' >  $MINDI_TMP/pv.tmp
155    else
156        pvscan 2> /dev/null | grep '"' | cut -d'"' -f2  >  $MINDI_TMP/pv.tmp
157    fi
158
159    rm -f $MINDI_TMP/pv.tmp2
160    for d in `cat $MINDI_TMP/pv.tmp`; do
161        # Skip devices excluded, coming from mondoarchive
162        skip=0
163        if [ "$EXCLUDE_DEVS" ] ; then
164            for ed in $EXCLUDE_DEVS ; do
165                if  [ "`echo " $d " | grep " $ed"`" != "" ]; then
166                    skip=1
167                    continue
168                fi
169            done
170        fi
171        if [ $skip -eq 1 ]; then
172            continue
173        fi
174        echo $d >> $MINDI_TMP/pv.tmp2
175    done
176
177    if [ -f /etc/multipath.conf ]; then
178        # If multipath check which type of devidec are given, mpath prefered
179        for d in `cat $MINDI_TMP/pv.tmp2`; do
180            skip=0
181            if [ "$EXCLUDE_DEVS" ] ; then
182                for ed in $EXCLUDE_DEVS ; do
183                    if  [ "`echo " $d " | grep " $ed"`" != "" ]; then
184                        skip=1
185                        continue
186                    fi
187                done
188            fi
189            if [ $skip -eq 1 ]; then
190                continue
191            fi
192            GiveMapperOfdm $d
193        done
194    else
195        cat $MINDI_TMP/pv.tmp2
196    fi
197    rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
198}
199
200
201ListAllVolumeGroups() {
202    $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
203}
204
205GiveMapperOfdm () {
206
207major=`stat -c "%t" $1`
208minor=`stat -c "%T" $1`
209
210for i in `ls /dev/mapper/*`; do
211    mj=`stat -c "%t" $i`
212    mn=`stat -c "%T" $i`
213    if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
214        echo "$i"
215        return
216    fi
217done
218echo $1
219}
220
221
222ListLvmDrivesAndPartitions() {
223    # We get partitions in this loop not devices
224    for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do 
225        # If multipath check which type of devices are given, mpath prefered
226        if [ -f /etc/multipath.conf ]; then
227            i=`GiveMapperOfdm $d`
228            rep=$i
229        else
230            rep=$d
231        fi
232        skip=0
233        if [ "$EXCLUDE_DEVS" ] ; then
234            for ed in $EXCLUDE_DEVS ; do
235                if  [ "`echo " $rep " | grep " $ed"`" != "" ]; then
236                    skip=1
237                    continue
238                fi
239            done
240        fi
241        if [ $skip -eq 1 ]; then
242            continue
243        fi
244        echo $rep
245    done
246}
247
248
249
250PrettifyList() {
251    local i
252    echo -en "$1"
253    for i in $2 ; do
254        echo -en "$i "
255    done
256    echo ""
257}
258
259
260ListAllLogicalVolumes() {
261    if [ $lvmversion = 2 ]; then
262        $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
263    else
264        lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
265    fi
266}
267
268
269
270WriteShutdownScript() {
271    local i
272    echo ""
273    echo "Finally, to shut down and delete the volumes, do this:-"
274    for i in `ListAllLogicalVolumes` ; do
275        echo "($LVMCMD lvremove -f $i)"
276    done
277    for i in `ListAllVolumeGroups` ; do
278        echo "($LVMCMD vgchange -a n $i)"
279    done
280    for i in `ListAllVolumeGroups` ; do
281        echo "($LVMCMD vgremove $i)"
282    done
283    if [ $lvmversion = 2 ]; then
284        echo "(rmmod dm-mod & rmmod dm_mod & )"
285    else
286        echo "(rmmod lvm-mod)"
287    fi
288}
289
290
291
292# -------------------------------- main -----------------------------------
293which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
294if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
295    exit 1
296fi
297
298# Older lvmdiskscan use --help, newer --version
299lvmopt="--help"
300lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
301if [ $? -eq 0 ]; then
302    lvmopt="--version"
303fi
304
305
306lvmversion=`lvmdiskscan --help 2>&1 |
307  grep -E "Logical Volume Manager|LVM version:" |
308  cut -d: -f2 | cut -d. -f1 |
309  awk '{print $NF}' |
310  sed -e 's/ //g'`
311
312if which lvm 2>/dev/null; then
313    version=`lvm version | grep "LVM version" | awk '{print $3}'`
314    i="`echo "$version" | cut -d'.' -f1`"
315    echo "i=$i"
316    if [ "$i" -ge "2" ] ; then
317        lvmversion=2
318    fi
319fi
320
321if [ $lvmversion = 2 ]; then
322    echo "LVM version >= 2.0 found."
323    LVMCMD="lvm"
324else
325    LVMCMD=""
326fi
327
328all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
329echo "Just before you extrapolate mountlist to include RAID partitions,"
330echo "extrapolate it to include the following LVM drives and partitions:-"
331PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
332echo "To get started, type:-"
333if [ $lvmversion = 2 ]; then
334    echo "(insmod dm-mod)"
335    echo "(insmod dm_mod)"
336else
337    echo "(insmod lvm-mod)"
338fi
339echo "# $LVMCMD vgchange -an"
340for i in `ListAllPhysicalVolumes` ; do
341    echo "# echo y | $LVMCMD pvcreate -ff $i"
342done
343echo "# $LVMCMD vgscan"
344echo ""
345echo "Create and activate the VG's (volume groups)."
346all_volume_groups=`ListAllVolumeGroups`
347for current_VG in $all_volume_groups ; do
348    if [ $lvmversion -ne 2 ]; then
349        echo "# rm -Rf /dev/$current_VG"
350    fi
351    ProcessVolumeGroup $current_VG
352done
353echo ""
354echo "Finally, create the LV's (logical volumes)."
355all_logical_volumes=`ListAllLogicalVolumes`
356for current_LV in $all_logical_volumes ; do
357    ProcessLogicalVolume $current_LV
358done
359echo ""
360echo "# $LVMCMD vgscan"
361echo "Now you may format the LV's:-"
362for i in `ListAllLogicalVolumes` ; do
363    echo "(mkfs -t foo $i or something like that)"
364done
365WriteShutdownScript
366exit 0
367
368
369
Note: See TracBrowser for help on using the repository browser.