source: trunk/mindi/analyze-my-lvm @ 276

Last change on this file since 276 was 276, checked in by bcornec, 14 years ago

merge -r274:275 $SVN_M/branches/2.06

  • Property svn:keywords set to Id
File size: 8.9 KB
Line 
1#!/bin/sh
2#
3# $Id: analyze-my-lvm 276 2006-01-03 16:01:12Z bcornec $
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 "%sm",$1;} else if ($2=="KB") {printf "%sk",$1;} else if ($2=="GB") {printf "%sg",$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    echo "# lvm lvcreate$params -n $logical_volume $volume_group"
72    else
73    fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
74    if [ ! -e "$fname" ] ; then
75        echo "Warning - cannot find $volume_group's $logical_volume LV file"
76    else
77        device=`GetValueFromField $fname "name:"`
78        params=`GenerateLvcreateParameters $device`
79        echo "# lvcreate$params -n $logical_volume $volume_group"
80    fi
81    fi
82}
83
84
85GenerateLvcreateParameters() {
86    local device stripes stripesize device fname allocation output readahead
87    fname=/tmp/PLF.$$.txt
88    device=$1
89    output=""
90    if [ $lvmversion = 2 ]; then
91    lvm lvdisplay $device > $fname
92    else
93    lvdisplay $device > $fname
94    fi
95    stripes=`GetValueFromField $fname "Stripes"`
96    stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
97    [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
98    [ "$stripesize" = "k" ] && stripesize=""
99    allocation=`GetValueFromField $fname "LV Size"`
100    [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
101    if echo "$allocation" | grep -x ".*g" > /dev/null 2> /dev/null ; then
102    val=`echo "$allocation" | sed s/g//`
103    allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
104    fi
105    readahead=`GetValueFromField $fname "Read ahead sectors"`
106    rm -f $fname
107    [ "$stripes" ]    && output="$output -i $stripes"
108    [ "$stripesize" ] && output="$output -I $stripesize"
109    [ "$allocation" ] && output="$output -L $allocation"
110    [ "$readahead" ]  && output="$output -r $readahead"
111    echo "$output"
112}
113
114
115
116GenerateVgcreateParameters() {
117    local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
118    current_VG=$1
119    VG_info_file=/tmp/$$.vg-info.txt
120    if [ $lvmversion = 2 ]; then
121    lvm vgdisplay $current_VG > $VG_info_file
122    else
123    vgdisplay $current_VG > $VG_info_file
124    fi
125    max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
126    [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
127    max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
128    [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
129    physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
130    output=""
131    [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
132    [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
133    [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
134    echo "$output"
135    rm -f $VG_info_file
136}
137
138
139
140
141
142ProcessVolumeGroup() {
143    local current_VG physical_volumes i list_of_devices VG_params
144    current_VG=$1
145    if [ $lvmversion = 2 ]; then
146    VG_params=`GenerateVgcreateParameters $current_VG`
147    list_of_devices=`lvm pvs | grep "$current_VG" | awk '{print $1}'`
148    echo "# lvm vgcreate $current_VG$VG_params $list_of_devices"
149    echo "# lvm vgchange -a y $current_VG"
150    else
151    info_file=/proc/lvm/VGs/$current_VG/group
152    physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
153    VG_params=`GenerateVgcreateParameters $current_VG`
154    list_of_devices=""
155    for i in $physical_volumes ; do
156        fname=/proc/lvm/VGs/$current_VG/PVs/$i
157        device=`GetValueFromField $fname "name:"`
158        list_of_devices="$list_of_devices $device"
159    done
160    echo "# vgcreate $current_VG$VG_params$list_of_devices"
161    echo "# vgchange -a y $current_VG"
162    fi
163}
164
165
166
167ListAllPhysicalVolumes() {
168    if [ $lvmversion = 2 ]; then
169    lvm pvscan 2> /dev/null | grep 'PV' | awk '{print $2}'
170    else
171    pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
172    fi
173}
174
175
176ListAllVolumeGroups() {
177    if [ $lvmversion = 2 ]; then
178        lvm vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
179    else
180        vgdisplay 2> /dev/null | awk '/^VG Name/ {print $3;}'
181    fi
182}
183
184
185ListLvmDrivesAndPartitions() {
186    if [ $lvmversion = 2 ]; then
187    lvm vgdisplay -v |grep "PV Name" | awk '{print $3}'
188    else
189    vgdisplay -v |grep "PV Name" | awk '{print $4}'
190    fi
191}
192
193
194
195PrettifyList() {
196    local i
197    echo -en "$1"
198    for i in $2 ; do
199    echo -en "$i "
200    done
201    echo ""
202}
203
204
205ListAllLogicalVolumes() {
206    if [ $lvmversion = 2 ]; then
207    lvm lvscan | grep "'" | cut -d"'" -f2
208    else
209    lvscan | grep '"' | cut -d'"' -f2
210    fi
211}
212
213
214
215WriteShutdownScript() {
216    local i
217    echo ""
218    echo "Finally, to shut down and delete the volumes, do this:-"
219    if [ $lvmversion = 2 ]; then
220    for i in `ListAllLogicalVolumes` ; do
221        echo "(lvm lvremove -f $i)"
222    done
223    for i in `ListAllVolumeGroups` ; do
224        echo "(lvm vgchange -a n $i)"
225    done
226    for i in `ListAllVolumeGroups` ; do
227        echo "(lvm vgremove $i)"
228    done
229    echo "(rmmod dm-mod & rmmod dm_mod & )"
230    else
231    for i in `ListAllLogicalVolumes` ; do
232        echo "(lvremove -f $i)"
233    done
234    for i in `ListAllVolumeGroups` ; do
235        echo "(vgchange -a n $i)"
236    done
237    for i in `ListAllVolumeGroups` ; do
238        echo "(vgremove $i)"
239    done
240    echo "(rmmod lvm-mod)"
241    fi
242}
243
244
245
246# -------------------------------- main -----------------------------------
247which lvmdiskscan 2>/dev/null 2>&1 || Die "Cannot find lvmdiskscan. Are you sure you're using LVM?"
248if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then
249    exit 0
250fi
251
252# Sq-Mod ... V1 has different output. --version is not legal but version
253# gets displayed so I guess it's ok... This should work for both.
254
255#lvmversion=`lvmdiskscan --version | grep "LVM version:" | cut -d: -f2 | cut -d. -f1 | sed -e 's/ //g'`
256
257lvmversion=`lvmdiskscan --help |
258  grep -E "Logical Volume Manager|LVM version:" |
259  cut -d: -f2 | cut -d. -f1 |
260  awk '{print $NF}' |
261  sed -e 's/ //g'`
262
263if which lvm 2>/dev/null; then
264    version=`lvm version | grep "LVM version" | awk '{print $3}'`
265    i="`echo "$version" | cut -d'.' -f1`"
266    echo "i=$i"
267    if [ "$i" -ge "2" ] ; then
268#    if [ $version >= "2.00" ]; then
269    lvmversion=2
270    fi
271fi
272
273if [ $lvmversion = 2 ]; then
274    echo "LVM version >= 2.0 found."
275fi
276
277all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
278echo "Just before you extrapolate mountlist to include RAID partitions,"
279echo "extrapolate it to include the following LVM drives and partitions:-"
280PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
281echo "To get started, type:-"
282if [ $lvmversion = 2 ]; then
283    echo "(insmod dm-mod)"
284    echo "(insmod dm_mod)"
285    echo "# lvm vgchange -an"
286else
287    echo "(insmod lvm-mod)"
288    echo "# vgchange -an"
289fi
290for i in `ListAllPhysicalVolumes` ; do
291#    echo "# dd if=/dev/zero of=$i bs=512 count=1"
292    if [ $lvmversion = 2 ]; then
293    echo "# echo y | lvm pvcreate -ff $i"
294    else
295    echo "# echo y | pvcreate -ff $i"
296    fi
297done
298if [ $lvmversion = 2 ]; then
299    echo "# lvm vgscan; echo"
300else
301    echo "# vgscan; echo"
302fi
303echo ""
304echo "Create and activate the VG's (volume groups)."
305all_volume_groups=`ListAllVolumeGroups`
306for current_VG in $all_volume_groups ; do
307    if [ $lvmversion -ne 2 ]; then
308        echo "# rm -Rf /dev/$current_VG"
309    fi
310    ProcessVolumeGroup $current_VG
311done
312echo ""
313echo "Finally, create the LV's (logical volumes)."
314all_logical_volumes=`ListAllLogicalVolumes`
315for current_LV in $all_logical_volumes ; do
316    ProcessLogicalVolume $current_LV
317done
318echo ""
319if [ $lvmversion = 2 ]; then
320    echo "# lvm vgscan"
321else
322    echo "# vgscan"
323fi
324echo "Now you may format the LV's:-"
325for i in `ListAllLogicalVolumes` ; do
326    echo "(mkfs -t foo $i or something like that)"
327done
328WriteShutdownScript
329exit 0
330
331
332
Note: See TracBrowser for help on using the repository browser.