[1] | 1 | #!/bin/sh
|
---|
[275] | 2 | #
|
---|
| 3 | # $Id: analyze-my-lvm 346 2006-01-25 14:43:55Z bcornec $
|
---|
| 4 | #
|
---|
[1] | 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() {
|
---|
[298] | 38 | echo "$1" >> /dev/stderr
|
---|
| 39 | exit 1
|
---|
[1] | 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | GetValueFromField() {
|
---|
[298] | 45 | local res
|
---|
[346] | 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;};}'
|
---|
[1] | 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | GetLastBit() {
|
---|
[298] | 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"
|
---|
[1] | 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | ProcessLogicalVolume() {
|
---|
[298] | 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`
|
---|
[1] | 71 | else
|
---|
[298] | 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
|
---|
[1] | 79 | fi
|
---|
[298] | 80 | echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
|
---|
[1] | 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | GenerateLvcreateParameters() {
|
---|
[298] | 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"
|
---|
[1] | 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | GenerateVgcreateParameters() {
|
---|
[298] | 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
|
---|
[1] | 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | ProcessVolumeGroup() {
|
---|
[298] | 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"
|
---|
[1] | 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | ListAllPhysicalVolumes() {
|
---|
[298] | 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
|
---|
[1] | 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | ListAllVolumeGroups() {
|
---|
[298] | 166 | $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
|
---|
[1] | 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | ListLvmDrivesAndPartitions() {
|
---|
[298] | 171 | $LVMCMD vgdisplay -v |grep "PV Name" | awk '{print $3}'
|
---|
[1] | 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | PrettifyList() {
|
---|
[298] | 177 | local i
|
---|
| 178 | echo -en "$1"
|
---|
| 179 | for i in $2 ; do
|
---|
| 180 | echo -en "$i "
|
---|
| 181 | done
|
---|
| 182 | echo ""
|
---|
[1] | 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | ListAllLogicalVolumes() {
|
---|
[298] | 187 | if [ $lvmversion = 2 ]; then
|
---|
| 188 | $LVMCMD lvscan | grep "'" | cut -d"'" -f2
|
---|
| 189 | else
|
---|
| 190 | lvscan | grep '"' | cut -d'"' -f2
|
---|
| 191 | fi
|
---|
[1] | 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | WriteShutdownScript() {
|
---|
[298] | 197 | local i
|
---|
| 198 | echo ""
|
---|
| 199 | echo "Finally, to shut down and delete the volumes, do this:-"
|
---|
[1] | 200 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 201 | echo "($LVMCMD lvremove -f $i)"
|
---|
[1] | 202 | done
|
---|
| 203 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 204 | echo "($LVMCMD vgchange -a n $i)"
|
---|
[1] | 205 | done
|
---|
| 206 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 207 | echo "($LVMCMD vgremove $i)"
|
---|
[1] | 208 | done
|
---|
[298] | 209 | if [ $lvmversion = 2 ]; then
|
---|
| 210 | echo "(rmmod dm-mod & rmmod dm_mod & )"
|
---|
| 211 | else
|
---|
| 212 | echo "(rmmod lvm-mod)"
|
---|
| 213 | fi
|
---|
[1] | 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | # -------------------------------- main -----------------------------------
|
---|
[97] | 219 | which lvmdiskscan 2>/dev/null 2>&1 || Die "Cannot find lvmdiskscan. Are you sure you're using LVM?"
|
---|
[275] | 220 | if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then
|
---|
[298] | 221 | exit 0
|
---|
[1] | 222 | fi
|
---|
| 223 |
|
---|
[299] | 224 | lvmversion=`lvmdiskscan --help 2>&1 |
|
---|
[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 |
|
---|
[97] | 230 | if which lvm 2>/dev/null; then
|
---|
[298] | 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
|
---|
[1] | 237 | fi
|
---|
| 238 |
|
---|
| 239 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 240 | echo "LVM version >= 2.0 found."
|
---|
| 241 | LVMCMD="lvm"
|
---|
| 242 | else
|
---|
| 243 | LVMCMD=""
|
---|
[1] | 244 | fi
|
---|
| 245 |
|
---|
| 246 | all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
|
---|
| 247 | echo "Just before you extrapolate mountlist to include RAID partitions,"
|
---|
| 248 | echo "extrapolate it to include the following LVM drives and partitions:-"
|
---|
| 249 | PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
|
---|
| 250 | echo "To get started, type:-"
|
---|
| 251 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 252 | echo "(insmod dm-mod)"
|
---|
| 253 | echo "(insmod dm_mod)"
|
---|
[1] | 254 | else
|
---|
[298] | 255 | echo "(insmod lvm-mod)"
|
---|
[1] | 256 | fi
|
---|
[298] | 257 | echo "# $LVMCMD vgchange -an"
|
---|
[1] | 258 | for i in `ListAllPhysicalVolumes` ; do
|
---|
[298] | 259 | echo "# echo y | $LVMCMD pvcreate -ff $i"
|
---|
[1] | 260 | done
|
---|
[298] | 261 | echo "# $LVMCMD vgscan; echo"
|
---|
[1] | 262 | echo ""
|
---|
| 263 | echo "Create and activate the VG's (volume groups)."
|
---|
| 264 | all_volume_groups=`ListAllVolumeGroups`
|
---|
| 265 | for current_VG in $all_volume_groups ; do
|
---|
[298] | 266 | if [ $lvmversion -ne 2 ]; then
|
---|
| 267 | echo "# rm -Rf /dev/$current_VG"
|
---|
| 268 | fi
|
---|
| 269 | ProcessVolumeGroup $current_VG
|
---|
[1] | 270 | done
|
---|
| 271 | echo ""
|
---|
| 272 | echo "Finally, create the LV's (logical volumes)."
|
---|
| 273 | all_logical_volumes=`ListAllLogicalVolumes`
|
---|
| 274 | for current_LV in $all_logical_volumes ; do
|
---|
[298] | 275 | ProcessLogicalVolume $current_LV
|
---|
[1] | 276 | done
|
---|
| 277 | echo ""
|
---|
[298] | 278 | echo "# $LVMCMD vgscan"
|
---|
[1] | 279 | echo "Now you may format the LV's:-"
|
---|
| 280 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 281 | echo "(mkfs -t foo $i or something like that)"
|
---|
[1] | 282 | done
|
---|
| 283 | WriteShutdownScript
|
---|
| 284 | exit 0
|
---|
| 285 |
|
---|
| 286 |
|
---|
| 287 |
|
---|