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

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

r3761@localhost: bruno | 2010-01-08 15:59:41 +0100

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