source: MondoRescue/branches/2.2.9/mindi/analyze-my-lvm @ 2244

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