source: branches/stable/mindi/analyze-my-lvm @ 850

Last change on this file since 850 was 850, checked in by bruno, 13 years ago

Fix Bug #4 (bbaumer_at_users.berlios.de)

  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1#!/bin/bash
2#
3# $Id: analyze-my-lvm 850 2006-09-26 23:44:22Z bruno $
4#
5
6#------------------------- ANALYZE-MY-LVM ----------------------- Hugo Rabson
7# 07/14
8# - no longer blank first 4k of /dev/mdX
9#
10# 06/14/2004
11# - fixed "n >= 2.00" bug (shell doesn't handle floating points properly)
12# - handle dm_mod as well as dm-mod
13#
14# 02/18/2004
15# - nice patch to fix ListAllVolumeGroups() --- J. Richard
16# - patch to support LVM2 by Takeru Komoriya
17#
18# 10/15/2003
19# - fixed '-L'-handling to allow for floating-point gigabyte values
20#
21# 01/15/2003
22# - patch (LVM) by Brian Borgeson
23#
24# 12/10/2002
25# - patch by Benjamin Mampaey
26#
27# 09/05/2002
28# - additional patch by Ralph Gruwe
29#
30# 08/30/2002
31# - modified by Ralph Gruwe
32#
33# 10/01/2001
34# - last modified by Hugo :)
35#------------------------------------------------------------------------------
36
37Die() {
38    echo "$1" >> /dev/stderr
39    exit 1
40}
41
42
43
44GetValueFromField() {
45    local res
46    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 "%dg",$1;} else {print $0;};}'
47}
48
49
50GetLastBit() {
51    local i res
52    i=20
53    res=""
54    while [ ! "$res" ] ; do
55        i=$(($i-1))
56        res=`echo "$1" | cut -d'/' -f$i`
57    done
58    echo "$res"
59}
60
61
62ProcessLogicalVolume() {
63    local LV_full_string fname logical_volume volume_group device
64    LV_full_string=$1
65    [ ! -e "$1" ] && Die "Cannot find LV file $1"
66    volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
67    logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
68    if [ $lvmversion = 2 ]; then
69        device=$LV_full_string
70        params=`GenerateLvcreateParameters $device`
71    else
72        fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
73        if [ ! -e "$fname" ] ; then
74            echo "Warning - cannot find $volume_group's $logical_volume LV file"
75        else
76            device=`GetValueFromField $fname "name:"`
77            params=`GenerateLvcreateParameters $device`
78        fi
79    fi
80    echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
81}
82
83
84GenerateLvcreateParameters() {
85    local device stripes stripesize device fname allocation output readahead
86    fname=/tmp/PLF.$$.txt
87    device=$1
88    output=""
89    $LVMCMD lvdisplay $device > $fname
90    stripes=`GetValueFromField $fname "Stripes"`
91    stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
92    [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
93    [ "$stripesize" = "k" ] && stripesize=""
94    allocation=`GetValueFromField $fname "LV Size"`
95    [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
96    if echo "$allocation" | grep -x ".*g" > /dev/null 2> /dev/null ; then
97        val=`echo "$allocation" | sed s/g//`
98        allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
99    fi
100    readahead=`GetValueFromField $fname "Read ahead sectors"`
101    rm -f $fname
102    [ "$stripes" ]    && output="$output -i $stripes"
103    [ "$stripesize" ] && output="$output -I $stripesize"
104    [ "$allocation" ] && output="$output -L $allocation"
105    [ "$readahead" ]  && output="$output -r $readahead"
106    echo "$output"
107}
108
109
110
111GenerateVgcreateParameters() {
112    local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
113    current_VG=$1
114    VG_info_file=/tmp/$$.vg-info.txt
115    $LVMCMD vgdisplay $current_VG > $VG_info_file
116    max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
117    [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
118    max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
119    [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
120    physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
121    output=""
122    [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
123    [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
124    [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
125    echo "$output"
126    rm -f $VG_info_file
127}
128
129
130
131
132
133ProcessVolumeGroup() {
134    local current_VG physical_volumes i list_of_devices VG_params
135    current_VG=$1
136    if [ $lvmversion = 2 ]; then
137        VG_params=`GenerateVgcreateParameters $current_VG`
138        list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
139    else
140        info_file=/proc/lvm/VGs/$current_VG/group
141        physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
142        VG_params=`GenerateVgcreateParameters $current_VG`
143        list_of_devices=""
144        for i in $physical_volumes ; do
145            fname=/proc/lvm/VGs/$current_VG/PVs/$i
146            device=`GetValueFromField $fname "name:"`
147            list_of_devices="$list_of_devices $device"
148        done
149    fi
150    echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
151    echo "# $LVMCMD vgchange -a y $current_VG"
152}
153
154
155
156ListAllPhysicalVolumes() {
157    if [ $lvmversion = 2 ]; then
158        $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}'
159    else
160        pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
161    fi
162}
163
164
165ListAllVolumeGroups() {
166    $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
167}
168
169
170ListLvmDrivesAndPartitions() {
171    $LVMCMD vgdisplay -v 2> /dev/null |grep "PV Name" | sed 's/(#)//' | awk '{print $3}'
172}
173
174
175
176PrettifyList() {
177    local i
178    echo -en "$1"
179    for i in $2 ; do
180        echo -en "$i "
181    done
182    echo ""
183}
184
185
186ListAllLogicalVolumes() {
187    if [ $lvmversion = 2 ]; then
188        $LVMCMD lvscan | grep "'" | cut -d"'" -f2
189    else
190        lvscan | grep '"' | cut -d'"' -f2
191    fi
192}
193
194
195
196WriteShutdownScript() {
197    local i
198    echo ""
199    echo "Finally, to shut down and delete the volumes, do this:-"
200    for i in `ListAllLogicalVolumes` ; do
201        echo "($LVMCMD lvremove -f $i)"
202    done
203    for i in `ListAllVolumeGroups` ; do
204        echo "($LVMCMD vgchange -a n $i)"
205    done
206    for i in `ListAllVolumeGroups` ; do
207        echo "($LVMCMD vgremove $i)"
208    done
209    if [ $lvmversion = 2 ]; then
210        echo "(rmmod dm-mod & rmmod dm_mod & )"
211    else
212        echo "(rmmod lvm-mod)"
213    fi
214}
215
216
217
218# -------------------------------- main -----------------------------------
219which lvmdiskscan 2>/dev/null 2>&1 || Die "Cannot find lvmdiskscan. Are you sure you're using LVM?"
220if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then
221    exit 0
222fi
223
224lvmversion=`lvmdiskscan --help 2>&1 |
225  grep -E "Logical Volume Manager|LVM version:" |
226  cut -d: -f2 | cut -d. -f1 |
227  awk '{print $NF}' |
228  sed -e 's/ //g'`
229
230if which lvm 2>/dev/null; then
231    version=`lvm version | grep "LVM version" | awk '{print $3}'`
232    i="`echo "$version" | cut -d'.' -f1`"
233    echo "i=$i"
234    if [ "$i" -ge "2" ] ; then
235        lvmversion=2
236    fi
237fi
238
239if [ $lvmversion = 2 ]; then
240    echo "LVM version >= 2.0 found."
241    LVMCMD="lvm"
242else
243    LVMCMD=""
244fi
245
246all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
247echo "Just before you extrapolate mountlist to include RAID partitions,"
248echo "extrapolate it to include the following LVM drives and partitions:-"
249PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
250echo "To get started, type:-"
251if [ $lvmversion = 2 ]; then
252    echo "(insmod dm-mod)"
253    echo "(insmod dm_mod)"
254else
255    echo "(insmod lvm-mod)"
256fi
257echo "# $LVMCMD vgchange -an"
258for i in `ListAllPhysicalVolumes` ; do
259    echo "# echo y | $LVMCMD pvcreate -ff $i"
260done
261echo "# $LVMCMD vgscan"
262echo ""
263echo "Create and activate the VG's (volume groups)."
264all_volume_groups=`ListAllVolumeGroups`
265for current_VG in $all_volume_groups ; do
266    if [ $lvmversion -ne 2 ]; then
267        echo "# rm -Rf /dev/$current_VG"
268    fi
269    ProcessVolumeGroup $current_VG
270done
271echo ""
272echo "Finally, create the LV's (logical volumes)."
273all_logical_volumes=`ListAllLogicalVolumes`
274for current_LV in $all_logical_volumes ; do
275    ProcessLogicalVolume $current_LV
276done
277echo ""
278echo "# $LVMCMD vgscan"
279echo "Now you may format the LV's:-"
280for i in `ListAllLogicalVolumes` ; do
281    echo "(mkfs -t foo $i or something like that)"
282done
283WriteShutdownScript
284exit 0
285
286
287
Note: See TracBrowser for help on using the repository browser.