1 | #!/bin/sh |
---|
2 | # |
---|
3 | # $Id: analyze-my-lvm 298 2006-01-11 11:44:18Z 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 | |
---|
37 | Die() { |
---|
38 | echo "$1" >> /dev/stderr |
---|
39 | exit 1 |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | GetValueFromField() { |
---|
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 | |
---|
50 | GetLastBit() { |
---|
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 | |
---|
62 | ProcessLogicalVolume() { |
---|
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 | |
---|
84 | GenerateLvcreateParameters() { |
---|
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 | |
---|
111 | GenerateVgcreateParameters() { |
---|
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 | |
---|
133 | ProcessVolumeGroup() { |
---|
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 | |
---|
156 | ListAllPhysicalVolumes() { |
---|
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 | |
---|
165 | ListAllVolumeGroups() { |
---|
166 | $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}' |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | ListLvmDrivesAndPartitions() { |
---|
171 | $LVMCMD vgdisplay -v |grep "PV Name" | awk '{print $3}' |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | PrettifyList() { |
---|
177 | local i |
---|
178 | echo -en "$1" |
---|
179 | for i in $2 ; do |
---|
180 | echo -en "$i " |
---|
181 | done |
---|
182 | echo "" |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | ListAllLogicalVolumes() { |
---|
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 | |
---|
196 | WriteShutdownScript() { |
---|
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 ----------------------------------- |
---|
219 | which lvmdiskscan 2>/dev/null 2>&1 || Die "Cannot find lvmdiskscan. Are you sure you're using LVM?" |
---|
220 | if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then |
---|
221 | exit 0 |
---|
222 | fi |
---|
223 | |
---|
224 | # Sq-Mod ... V1 has different output. --version is not legal but version |
---|
225 | # gets displayed so I guess it's ok... This should work for both. |
---|
226 | |
---|
227 | #lvmversion=`lvmdiskscan --version | grep "LVM version:" | cut -d: -f2 | cut -d. -f1 | sed -e 's/ //g'` |
---|
228 | |
---|
229 | lvmversion=`lvmdiskscan --help | |
---|
230 | grep -E "Logical Volume Manager|LVM version:" | |
---|
231 | cut -d: -f2 | cut -d. -f1 | |
---|
232 | awk '{print $NF}' | |
---|
233 | sed -e 's/ //g'` |
---|
234 | |
---|
235 | if which lvm 2>/dev/null; then |
---|
236 | version=`lvm version | grep "LVM version" | awk '{print $3}'` |
---|
237 | i="`echo "$version" | cut -d'.' -f1`" |
---|
238 | echo "i=$i" |
---|
239 | if [ "$i" -ge "2" ] ; then |
---|
240 | lvmversion=2 |
---|
241 | fi |
---|
242 | fi |
---|
243 | |
---|
244 | if [ $lvmversion = 2 ]; then |
---|
245 | echo "LVM version >= 2.0 found." |
---|
246 | LVMCMD="lvm" |
---|
247 | else |
---|
248 | LVMCMD="" |
---|
249 | fi |
---|
250 | |
---|
251 | all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions` |
---|
252 | echo "Just before you extrapolate mountlist to include RAID partitions," |
---|
253 | echo "extrapolate it to include the following LVM drives and partitions:-" |
---|
254 | PrettifyList ">>>>> " "$all_lvm_drives_and_partitions" |
---|
255 | echo "To get started, type:-" |
---|
256 | if [ $lvmversion = 2 ]; then |
---|
257 | echo "(insmod dm-mod)" |
---|
258 | echo "(insmod dm_mod)" |
---|
259 | else |
---|
260 | echo "(insmod lvm-mod)" |
---|
261 | fi |
---|
262 | echo "# $LVMCMD vgchange -an" |
---|
263 | for i in `ListAllPhysicalVolumes` ; do |
---|
264 | echo "# echo y | $LVMCMD pvcreate -ff $i" |
---|
265 | done |
---|
266 | echo "# $LVMCMD vgscan; echo" |
---|
267 | echo "" |
---|
268 | echo "Create and activate the VG's (volume groups)." |
---|
269 | all_volume_groups=`ListAllVolumeGroups` |
---|
270 | for current_VG in $all_volume_groups ; do |
---|
271 | if [ $lvmversion -ne 2 ]; then |
---|
272 | echo "# rm -Rf /dev/$current_VG" |
---|
273 | fi |
---|
274 | ProcessVolumeGroup $current_VG |
---|
275 | done |
---|
276 | echo "" |
---|
277 | echo "Finally, create the LV's (logical volumes)." |
---|
278 | all_logical_volumes=`ListAllLogicalVolumes` |
---|
279 | for current_LV in $all_logical_volumes ; do |
---|
280 | ProcessLogicalVolume $current_LV |
---|
281 | done |
---|
282 | echo "" |
---|
283 | echo "# $LVMCMD vgscan" |
---|
284 | echo "Now you may format the LV's:-" |
---|
285 | for i in `ListAllLogicalVolumes` ; do |
---|
286 | echo "(mkfs -t foo $i or something like that)" |
---|
287 | done |
---|
288 | WriteShutdownScript |
---|
289 | exit 0 |
---|
290 | |
---|
291 | |
---|
292 | |
---|