source: MondoRescue/trunk/mindi/analyze-my-lvm@ 110

Last change on this file since 110 was 98, checked in by bcornec, 18 years ago

merge -r 96:97 from 2.04 branch

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