[673] | 1 | #!/bin/bash
|
---|
[275] | 2 | #
|
---|
| 3 | # $Id: analyze-my-lvm 2564 2010-02-01 13:12:16Z bruno $
|
---|
| 4 | #
|
---|
[1] | 5 |
|
---|
| 6 | Die() {
|
---|
[298] | 7 | echo "$1" >> /dev/stderr
|
---|
[963] | 8 | exit -1
|
---|
[1] | 9 | }
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | GetValueFromField() {
|
---|
[298] | 14 | local res
|
---|
[1942] | 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;};}'
|
---|
[1] | 16 | }
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | GetLastBit() {
|
---|
[298] | 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"
|
---|
[1] | 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | ProcessLogicalVolume() {
|
---|
[298] | 32 | local LV_full_string fname logical_volume volume_group device
|
---|
| 33 | LV_full_string=$1
|
---|
[2244] | 34 | if [ ! -e "$1" ]; then
|
---|
| 35 | echo "WARNING - cannot find LV file $1" | tee -a /dev/stderr
|
---|
| 36 | return
|
---|
| 37 | fi
|
---|
[298] | 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`
|
---|
[1] | 43 | else
|
---|
[298] | 44 | fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
|
---|
| 45 | if [ ! -e "$fname" ] ; then
|
---|
[2244] | 46 | echo "WARNING - cannot find $volume_group's $logical_volume LV file" | tee -a /dev/stderr
|
---|
| 47 | return
|
---|
[298] | 48 | else
|
---|
| 49 | device=`GetValueFromField $fname "name:"`
|
---|
| 50 | params=`GenerateLvcreateParameters $device`
|
---|
| 51 | fi
|
---|
[1] | 52 | fi
|
---|
[2534] | 53 | # Do not process LV whose VG are excluded
|
---|
[2537] | 54 | if [ -f $MINDI_TMP/excludedvgs ]; then
|
---|
| 55 | if [ "`grep $volume_group $MINDI_TMP/excludedvgs`" = "" ]; then
|
---|
| 56 | echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
|
---|
| 57 | fi
|
---|
[2555] | 58 | else
|
---|
| 59 | echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
|
---|
[2534] | 60 | fi
|
---|
| 61 | rm -f $MINDI_TMP/excludedvgs
|
---|
[1] | 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | GenerateLvcreateParameters() {
|
---|
[298] | 66 | local device stripes stripesize device fname allocation output readahead
|
---|
[1487] | 67 | fname=$MINDI_TMP/PLF.$$.txt
|
---|
[298] | 68 | device=$1
|
---|
| 69 | output=""
|
---|
| 70 | $LVMCMD lvdisplay $device > $fname
|
---|
| 71 | stripes=`GetValueFromField $fname "Stripes"`
|
---|
| 72 | stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
|
---|
| 73 | [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
|
---|
| 74 | [ "$stripesize" = "k" ] && stripesize=""
|
---|
| 75 | allocation=`GetValueFromField $fname "LV Size"`
|
---|
| 76 | [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
|
---|
[911] | 77 | if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
|
---|
[298] | 78 | val=`echo "$allocation" | sed s/g//`
|
---|
| 79 | allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
|
---|
| 80 | fi
|
---|
| 81 | readahead=`GetValueFromField $fname "Read ahead sectors"`
|
---|
| 82 | rm -f $fname
|
---|
| 83 | [ "$stripes" ] && output="$output -i $stripes"
|
---|
| 84 | [ "$stripesize" ] && output="$output -I $stripesize"
|
---|
| 85 | [ "$allocation" ] && output="$output -L $allocation"
|
---|
| 86 | [ "$readahead" ] && output="$output -r $readahead"
|
---|
| 87 | echo "$output"
|
---|
[1] | 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 | GenerateVgcreateParameters() {
|
---|
[298] | 93 | local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
|
---|
| 94 | current_VG=$1
|
---|
[1487] | 95 | VG_info_file=$MINDI_TMP/$$.vg-info.txt
|
---|
[2342] | 96 | # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
|
---|
| 97 | $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
|
---|
[298] | 98 | max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
|
---|
| 99 | [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
|
---|
| 100 | max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
|
---|
| 101 | [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
|
---|
| 102 | physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
|
---|
| 103 | output=""
|
---|
| 104 | [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
|
---|
| 105 | [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
|
---|
| 106 | [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
|
---|
| 107 | echo "$output"
|
---|
| 108 | rm -f $VG_info_file
|
---|
[1] | 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | ProcessVolumeGroup() {
|
---|
[298] | 116 | local current_VG physical_volumes i list_of_devices VG_params
|
---|
| 117 | current_VG=$1
|
---|
| 118 | if [ $lvmversion = 2 ]; then
|
---|
| 119 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
[850] | 120 | list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
|
---|
[298] | 121 | else
|
---|
| 122 | info_file=/proc/lvm/VGs/$current_VG/group
|
---|
| 123 | physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
|
---|
| 124 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
| 125 | list_of_devices=""
|
---|
| 126 | for i in $physical_volumes ; do
|
---|
| 127 | fname=/proc/lvm/VGs/$current_VG/PVs/$i
|
---|
| 128 | device=`GetValueFromField $fname "name:"`
|
---|
| 129 | list_of_devices="$list_of_devices $device"
|
---|
| 130 | done
|
---|
| 131 | fi
|
---|
[2072] | 132 | l=""
|
---|
| 133 | if [ -f /etc/multipath.conf ]; then
|
---|
| 134 | # If multipath check which type of devidec are given, mpath prefered
|
---|
| 135 | for d in $list_of_devices; do
|
---|
| 136 | l="$l `GiveMapperOfdm $d`"
|
---|
| 137 | done
|
---|
| 138 | list_of_devices=$l
|
---|
| 139 | fi
|
---|
| 140 |
|
---|
[2534] | 141 | rm -f $MINDI_TMP/excludedvgs
|
---|
[2564] | 142 | if [ "$MINDI_EXCLUDE_DEVS" ] ; then
|
---|
| 143 | for ed in $MINDI_EXCLUDE_DEVS ; do
|
---|
[2532] | 144 | if [ "`echo " $list_of_devices" | grep " $ed"`" != "" ]; then
|
---|
[2534] | 145 | echo $current_VG >> $MINDI_TMP/excludedvgs
|
---|
[2532] | 146 | return
|
---|
| 147 | fi
|
---|
| 148 | done
|
---|
| 149 | fi
|
---|
[298] | 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
|
---|
[2072] | 158 | $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
|
---|
[298] | 159 | else
|
---|
[2072] | 160 | pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
|
---|
[298] | 161 | fi
|
---|
[2532] | 162 |
|
---|
| 163 | rm -f $MINDI_TMP/pv.tmp2
|
---|
| 164 | for d in `cat $MINDI_TMP/pv.tmp`; do
|
---|
| 165 | # Skip devices excluded, coming from mondoarchive
|
---|
| 166 | skip=0
|
---|
[2564] | 167 | if [ "$MINDI_EXCLUDE_DEVS" ] ; then
|
---|
| 168 | for ed in $MINDI_EXCLUDE_DEVS ; do
|
---|
[2532] | 169 | if [ "`echo " $d " | grep " $ed"`" != "" ]; then
|
---|
| 170 | skip=1
|
---|
| 171 | continue
|
---|
| 172 | fi
|
---|
| 173 | done
|
---|
| 174 | fi
|
---|
| 175 | if [ $skip -eq 1 ]; then
|
---|
| 176 | continue
|
---|
| 177 | fi
|
---|
| 178 | echo $d >> $MINDI_TMP/pv.tmp2
|
---|
| 179 | done
|
---|
| 180 |
|
---|
[2072] | 181 | if [ -f /etc/multipath.conf ]; then
|
---|
| 182 | # If multipath check which type of devidec are given, mpath prefered
|
---|
[2554] | 183 | if [ -f $MINDI_TMP/pv.tmp2 ]; then
|
---|
| 184 | for d in `cat $MINDI_TMP/pv.tmp2`; do
|
---|
| 185 | skip=0
|
---|
[2564] | 186 | if [ "$MINDI_EXCLUDE_DEVS" ] ; then
|
---|
| 187 | for ed in $MINDI_EXCLUDE_DEVS ; do
|
---|
[2554] | 188 | if [ "`echo " $d " | grep " $ed"`" != "" ]; then
|
---|
| 189 | skip=1
|
---|
| 190 | continue
|
---|
| 191 | fi
|
---|
| 192 | done
|
---|
| 193 | fi
|
---|
| 194 | if [ $skip -eq 1 ]; then
|
---|
| 195 | continue
|
---|
| 196 | fi
|
---|
| 197 | GiveMapperOfdm $d
|
---|
| 198 | done
|
---|
| 199 | fi
|
---|
[2072] | 200 | else
|
---|
[2554] | 201 | if [ -f $MINDI_TMP/pv.tmp2 ]; then
|
---|
| 202 | cat $MINDI_TMP/pv.tmp2
|
---|
| 203 | fi
|
---|
[2072] | 204 | fi
|
---|
[2532] | 205 | rm -f $MINDI_TMP/pv.tmp $MINDI_TMP/pv.tmp2
|
---|
[1] | 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | ListAllVolumeGroups() {
|
---|
[298] | 210 | $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
|
---|
[1] | 211 | }
|
---|
| 212 |
|
---|
[2072] | 213 | GiveMapperOfdm () {
|
---|
[1] | 214 |
|
---|
[2072] | 215 | major=`stat -c "%t" $1`
|
---|
| 216 | minor=`stat -c "%T" $1`
|
---|
| 217 |
|
---|
| 218 | for i in `ls /dev/mapper/*`; do
|
---|
| 219 | mj=`stat -c "%t" $i`
|
---|
| 220 | mn=`stat -c "%T" $i`
|
---|
| 221 | if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
|
---|
| 222 | echo "$i"
|
---|
| 223 | return
|
---|
| 224 | fi
|
---|
| 225 | done
|
---|
| 226 | echo $1
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
[1] | 230 | ListLvmDrivesAndPartitions() {
|
---|
[2531] | 231 | # We get partitions in this loop not devices
|
---|
[2424] | 232 | for d in `$LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'`; do
|
---|
[2526] | 233 | # If multipath check which type of devices are given, mpath prefered
|
---|
[2424] | 234 | if [ -f /etc/multipath.conf ]; then
|
---|
[2526] | 235 | i=`GiveMapperOfdm $d`
|
---|
[2536] | 236 | rep=$i
|
---|
[2424] | 237 | else
|
---|
[2536] | 238 | rep=$d
|
---|
[2424] | 239 | fi
|
---|
[2536] | 240 | skip=0
|
---|
[2564] | 241 | if [ "$MINDI_EXCLUDE_DEVS" ] ; then
|
---|
| 242 | for ed in $MINDI_EXCLUDE_DEVS ; do
|
---|
[2536] | 243 | if [ "`echo " $rep " | grep " $ed"`" != "" ]; then
|
---|
| 244 | skip=1
|
---|
| 245 | continue
|
---|
| 246 | fi
|
---|
| 247 | done
|
---|
| 248 | fi
|
---|
| 249 | if [ $skip -eq 1 ]; then
|
---|
| 250 | continue
|
---|
| 251 | fi
|
---|
| 252 | echo $rep
|
---|
[2424] | 253 | done
|
---|
[1] | 254 | }
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | PrettifyList() {
|
---|
[298] | 259 | local i
|
---|
| 260 | echo -en "$1"
|
---|
| 261 | for i in $2 ; do
|
---|
| 262 | echo -en "$i "
|
---|
| 263 | done
|
---|
| 264 | echo ""
|
---|
[1] | 265 | }
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 | ListAllLogicalVolumes() {
|
---|
[298] | 269 | if [ $lvmversion = 2 ]; then
|
---|
[2244] | 270 | $LVMCMD lvscan 2> /dev/null | grep "'" | grep -iw "ACTIVE" | cut -d"'" -f2
|
---|
[298] | 271 | else
|
---|
[2244] | 272 | lvscan 2> /dev/null | grep '"' | grep -iw "ACTIVE" | cut -d'"' -f2
|
---|
[298] | 273 | fi
|
---|
[1] | 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 | WriteShutdownScript() {
|
---|
[298] | 279 | local i
|
---|
| 280 | echo ""
|
---|
| 281 | echo "Finally, to shut down and delete the volumes, do this:-"
|
---|
[1] | 282 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 283 | echo "($LVMCMD lvremove -f $i)"
|
---|
[1] | 284 | done
|
---|
| 285 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 286 | echo "($LVMCMD vgchange -a n $i)"
|
---|
[1] | 287 | done
|
---|
| 288 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 289 | echo "($LVMCMD vgremove $i)"
|
---|
[1] | 290 | done
|
---|
[298] | 291 | if [ $lvmversion = 2 ]; then
|
---|
| 292 | echo "(rmmod dm-mod & rmmod dm_mod & )"
|
---|
| 293 | else
|
---|
| 294 | echo "(rmmod lvm-mod)"
|
---|
| 295 | fi
|
---|
[1] | 296 | }
|
---|
| 297 |
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 | # -------------------------------- main -----------------------------------
|
---|
[963] | 301 | which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
|
---|
[2044] | 302 | if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
|
---|
| 303 | exit 1
|
---|
[1] | 304 | fi
|
---|
| 305 |
|
---|
[2170] | 306 | # Older lvmdiskscan use --help, newer --version
|
---|
| 307 | lvmopt="--help"
|
---|
| 308 | lvmdiskscan $lvmopt 2>&1 | grep -q -- "--version"
|
---|
| 309 | if [ $? -eq 0 ]; then
|
---|
| 310 | lvmopt="--version"
|
---|
| 311 | fi
|
---|
| 312 |
|
---|
| 313 |
|
---|
[299] | 314 | lvmversion=`lvmdiskscan --help 2>&1 |
|
---|
[1] | 315 | grep -E "Logical Volume Manager|LVM version:" |
|
---|
| 316 | cut -d: -f2 | cut -d. -f1 |
|
---|
| 317 | awk '{print $NF}' |
|
---|
| 318 | sed -e 's/ //g'`
|
---|
| 319 |
|
---|
[97] | 320 | if which lvm 2>/dev/null; then
|
---|
[298] | 321 | version=`lvm version | grep "LVM version" | awk '{print $3}'`
|
---|
| 322 | i="`echo "$version" | cut -d'.' -f1`"
|
---|
| 323 | echo "i=$i"
|
---|
| 324 | if [ "$i" -ge "2" ] ; then
|
---|
| 325 | lvmversion=2
|
---|
| 326 | fi
|
---|
[1] | 327 | fi
|
---|
| 328 |
|
---|
| 329 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 330 | echo "LVM version >= 2.0 found."
|
---|
| 331 | LVMCMD="lvm"
|
---|
| 332 | else
|
---|
| 333 | LVMCMD=""
|
---|
[1] | 334 | fi
|
---|
| 335 |
|
---|
| 336 | all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
|
---|
| 337 | echo "Just before you extrapolate mountlist to include RAID partitions,"
|
---|
| 338 | echo "extrapolate it to include the following LVM drives and partitions:-"
|
---|
| 339 | PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
|
---|
| 340 | echo "To get started, type:-"
|
---|
| 341 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 342 | echo "(insmod dm-mod)"
|
---|
| 343 | echo "(insmod dm_mod)"
|
---|
[1] | 344 | else
|
---|
[298] | 345 | echo "(insmod lvm-mod)"
|
---|
[1] | 346 | fi
|
---|
[298] | 347 | echo "# $LVMCMD vgchange -an"
|
---|
[1] | 348 | for i in `ListAllPhysicalVolumes` ; do
|
---|
[298] | 349 | echo "# echo y | $LVMCMD pvcreate -ff $i"
|
---|
[1] | 350 | done
|
---|
[706] | 351 | echo "# $LVMCMD vgscan"
|
---|
[1] | 352 | echo ""
|
---|
| 353 | echo "Create and activate the VG's (volume groups)."
|
---|
| 354 | all_volume_groups=`ListAllVolumeGroups`
|
---|
| 355 | for current_VG in $all_volume_groups ; do
|
---|
[298] | 356 | if [ $lvmversion -ne 2 ]; then
|
---|
| 357 | echo "# rm -Rf /dev/$current_VG"
|
---|
| 358 | fi
|
---|
| 359 | ProcessVolumeGroup $current_VG
|
---|
[1] | 360 | done
|
---|
| 361 | echo ""
|
---|
| 362 | echo "Finally, create the LV's (logical volumes)."
|
---|
| 363 | all_logical_volumes=`ListAllLogicalVolumes`
|
---|
| 364 | for current_LV in $all_logical_volumes ; do
|
---|
[298] | 365 | ProcessLogicalVolume $current_LV
|
---|
[1] | 366 | done
|
---|
| 367 | echo ""
|
---|
[298] | 368 | echo "# $LVMCMD vgscan"
|
---|
[1] | 369 | echo "Now you may format the LV's:-"
|
---|
| 370 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 371 | echo "(mkfs -t foo $i or something like that)"
|
---|
[1] | 372 | done
|
---|
| 373 | WriteShutdownScript
|
---|
| 374 | exit 0
|
---|
| 375 |
|
---|
| 376 |
|
---|
| 377 |
|
---|