1 | #!/bin/bash
|
---|
2 | #
|
---|
3 | # $Id: analyze-my-lvm 2428 2009-09-28 00:29:12Z bruno $
|
---|
4 | #
|
---|
5 |
|
---|
6 | Die() {
|
---|
7 | echo "$1" >> /dev/stderr
|
---|
8 | exit -1
|
---|
9 | }
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 | GetValueFromField() {
|
---|
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 |
|
---|
19 | GetLastBit() {
|
---|
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 |
|
---|
31 | ProcessLogicalVolume() {
|
---|
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 |
|
---|
57 | GenerateLvcreateParameters() {
|
---|
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 |
|
---|
84 | GenerateVgcreateParameters() {
|
---|
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 | # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
|
---|
89 | $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
|
---|
90 | max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
|
---|
91 | [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
|
---|
92 | max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
|
---|
93 | [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
|
---|
94 | physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
|
---|
95 | output=""
|
---|
96 | [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
|
---|
97 | [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
|
---|
98 | [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
|
---|
99 | echo "$output"
|
---|
100 | rm -f $VG_info_file
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | ProcessVolumeGroup() {
|
---|
108 | local current_VG physical_volumes i list_of_devices VG_params
|
---|
109 | current_VG=$1
|
---|
110 | if [ $lvmversion = 2 ]; then
|
---|
111 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
112 | list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
|
---|
113 | else
|
---|
114 | info_file=/proc/lvm/VGs/$current_VG/group
|
---|
115 | physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
|
---|
116 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
117 | list_of_devices=""
|
---|
118 | for i in $physical_volumes ; do
|
---|
119 | fname=/proc/lvm/VGs/$current_VG/PVs/$i
|
---|
120 | device=`GetValueFromField $fname "name:"`
|
---|
121 | list_of_devices="$list_of_devices $device"
|
---|
122 | done
|
---|
123 | fi
|
---|
124 | l=""
|
---|
125 | if [ -f /etc/multipath.conf ]; then
|
---|
126 | # If multipath check which type of devidec are given, mpath prefered
|
---|
127 | for d in $list_of_devices; do
|
---|
128 | l="$l `GiveMapperOfdm $d`"
|
---|
129 | done
|
---|
130 | list_of_devices=$l
|
---|
131 | fi
|
---|
132 |
|
---|
133 | echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
|
---|
134 | echo "# $LVMCMD vgchange -a y $current_VG"
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | ListAllPhysicalVolumes() {
|
---|
140 | if [ $lvmversion = 2 ]; then
|
---|
141 | $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
|
---|
142 | else
|
---|
143 | pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
|
---|
144 | fi
|
---|
145 | if [ -f /etc/multipath.conf ]; then
|
---|
146 | # If multipath check which type of devidec are given, mpath prefered
|
---|
147 | for d in `cat $MINDI_TMP/pv.tmp`; do
|
---|
148 | GiveMapperOfdm $d
|
---|
149 | done
|
---|
150 | else
|
---|
151 | cat $MINDI_TMP/pv.tmp
|
---|
152 | fi
|
---|
153 | rm -f $MINDI_TMP/pv.tmp
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | ListAllVolumeGroups() {
|
---|
158 | $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
|
---|
159 | }
|
---|
160 |
|
---|
161 | GiveMapperOfdm () {
|
---|
162 |
|
---|
163 | major=`stat -c "%t" $1`
|
---|
164 | minor=`stat -c "%T" $1`
|
---|
165 |
|
---|
166 | for i in `ls /dev/mapper/*`; do
|
---|
167 | mj=`stat -c "%t" $i`
|
---|
168 | mn=`stat -c "%T" $i`
|
---|
169 | if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
|
---|
170 | echo "$i"
|
---|
171 | return
|
---|
172 | fi
|
---|
173 | done
|
---|
174 | echo $1
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | ListLvmDrivesAndPartitions() {
|
---|
179 | for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
|
---|
180 | # Skip devices excluded, coming from mondoarchive
|
---|
181 | if [ "$EXCLUDE_DEVS" ] && [ "`echo " $EXCLUDE_DEVS " | grep " $d "`" ]; then
|
---|
182 | continue
|
---|
183 | fi
|
---|
184 | # If multipath check which type of devides are given, mpath prefered
|
---|
185 | if [ -f /etc/multipath.conf ]; then
|
---|
186 | GiveMapperOfdm $d
|
---|
187 | else
|
---|
188 | echo $d
|
---|
189 | fi
|
---|
190 | done
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 |
|
---|
195 | PrettifyList() {
|
---|
196 | local i
|
---|
197 | echo -en "$1"
|
---|
198 | for i in $2 ; do
|
---|
199 | echo -en "$i "
|
---|
200 | done
|
---|
201 | echo ""
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | ListAllLogicalVolumes() {
|
---|
206 | if [ $lvmversion = 2 ]; then
|
---|
207 | $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
|
---|
208 | else
|
---|
209 | lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
|
---|
210 | fi
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 |
|
---|
215 | WriteShutdownScript() {
|
---|
216 | local i
|
---|
217 | echo ""
|
---|
218 | echo "Finally, to shut down and delete the volumes, do this:-"
|
---|
219 | for i in `ListAllLogicalVolumes` ; do
|
---|
220 | echo "($LVMCMD lvremove -f $i)"
|
---|
221 | done
|
---|
222 | for i in `ListAllVolumeGroups` ; do
|
---|
223 | echo "($LVMCMD vgchange -a n $i)"
|
---|
224 | done
|
---|
225 | for i in `ListAllVolumeGroups` ; do
|
---|
226 | echo "($LVMCMD vgremove $i)"
|
---|
227 | done
|
---|
228 | if [ $lvmversion = 2 ]; then
|
---|
229 | echo "(rmmod dm-mod & rmmod dm_mod & )"
|
---|
230 | else
|
---|
231 | echo "(rmmod lvm-mod)"
|
---|
232 | fi
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 |
|
---|
237 | # -------------------------------- main -----------------------------------
|
---|
238 | which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
|
---|
239 | if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
|
---|
240 | exit 1
|
---|
241 | fi
|
---|
242 |
|
---|
243 | # Older lvmdiskscan use --help, newer --version
|
---|
244 | lvmopt="--help"
|
---|
245 | lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
|
---|
246 | if [ $? -eq 0 ]; then
|
---|
247 | lvmopt="--version"
|
---|
248 | fi
|
---|
249 |
|
---|
250 |
|
---|
251 | lvmversion=`lvmdiskscan --help 2>&1 |
|
---|
252 | grep -E "Logical Volume Manager|LVM version:" |
|
---|
253 | cut -d: -f2 | cut -d. -f1 |
|
---|
254 | awk '{print $NF}' |
|
---|
255 | sed -e 's/ //g'`
|
---|
256 |
|
---|
257 | if which lvm 2>/dev/null; then
|
---|
258 | version=`lvm version | grep "LVM version" | awk '{print $3}'`
|
---|
259 | i="`echo "$version" | cut -d'.' -f1`"
|
---|
260 | echo "i=$i"
|
---|
261 | if [ "$i" -ge "2" ] ; then
|
---|
262 | lvmversion=2
|
---|
263 | fi
|
---|
264 | fi
|
---|
265 |
|
---|
266 | if [ $lvmversion = 2 ]; then
|
---|
267 | echo "LVM version >= 2.0 found."
|
---|
268 | LVMCMD="lvm"
|
---|
269 | else
|
---|
270 | LVMCMD=""
|
---|
271 | fi
|
---|
272 |
|
---|
273 | all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
|
---|
274 | echo "Just before you extrapolate mountlist to include RAID partitions,"
|
---|
275 | echo "extrapolate it to include the following LVM drives and partitions:-"
|
---|
276 | PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
|
---|
277 | echo "To get started, type:-"
|
---|
278 | if [ $lvmversion = 2 ]; then
|
---|
279 | echo "(insmod dm-mod)"
|
---|
280 | echo "(insmod dm_mod)"
|
---|
281 | else
|
---|
282 | echo "(insmod lvm-mod)"
|
---|
283 | fi
|
---|
284 | echo "# $LVMCMD vgchange -an"
|
---|
285 | for i in `ListAllPhysicalVolumes` ; do
|
---|
286 | echo "# echo y | $LVMCMD pvcreate -ff $i"
|
---|
287 | done
|
---|
288 | echo "# $LVMCMD vgscan"
|
---|
289 | echo ""
|
---|
290 | echo "Create and activate the VG's (volume groups)."
|
---|
291 | all_volume_groups=`ListAllVolumeGroups`
|
---|
292 | for current_VG in $all_volume_groups ; do
|
---|
293 | if [ $lvmversion -ne 2 ]; then
|
---|
294 | echo "# rm -Rf /dev/$current_VG"
|
---|
295 | fi
|
---|
296 | ProcessVolumeGroup $current_VG
|
---|
297 | done
|
---|
298 | echo ""
|
---|
299 | echo "Finally, create the LV's (logical volumes)."
|
---|
300 | all_logical_volumes=`ListAllLogicalVolumes`
|
---|
301 | for current_LV in $all_logical_volumes ; do
|
---|
302 | ProcessLogicalVolume $current_LV
|
---|
303 | done
|
---|
304 | echo ""
|
---|
305 | echo "# $LVMCMD vgscan"
|
---|
306 | echo "Now you may format the LV's:-"
|
---|
307 | for i in `ListAllLogicalVolumes` ; do
|
---|
308 | echo "(mkfs -t foo $i or something like that)"
|
---|
309 | done
|
---|
310 | WriteShutdownScript
|
---|
311 | exit 0
|
---|
312 |
|
---|
313 |
|
---|
314 |
|
---|