[673] | 1 | #!/bin/bash
|
---|
[275] | 2 | #
|
---|
| 3 | # $Id: analyze-my-lvm 2430 2009-09-28 10:08:31Z 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
|
---|
| 34 | [ ! -e "$1" ] && Die "Cannot find LV file $1"
|
---|
| 35 | volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
|
---|
| 36 | logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
|
---|
| 37 | if [ $lvmversion = 2 ]; then
|
---|
| 38 | device=$LV_full_string
|
---|
| 39 | params=`GenerateLvcreateParameters $device`
|
---|
[1] | 40 | else
|
---|
[298] | 41 | fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
|
---|
| 42 | if [ ! -e "$fname" ] ; then
|
---|
| 43 | echo "Warning - cannot find $volume_group's $logical_volume LV file"
|
---|
| 44 | else
|
---|
| 45 | device=`GetValueFromField $fname "name:"`
|
---|
| 46 | params=`GenerateLvcreateParameters $device`
|
---|
| 47 | fi
|
---|
[1] | 48 | fi
|
---|
[298] | 49 | echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
|
---|
[1] | 50 | }
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | GenerateLvcreateParameters() {
|
---|
[298] | 54 | local device stripes stripesize device fname allocation output readahead
|
---|
[1487] | 55 | fname=$MINDI_TMP/PLF.$$.txt
|
---|
[298] | 56 | device=$1
|
---|
| 57 | output=""
|
---|
| 58 | $LVMCMD lvdisplay $device > $fname
|
---|
| 59 | stripes=`GetValueFromField $fname "Stripes"`
|
---|
| 60 | stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
|
---|
| 61 | [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
|
---|
| 62 | [ "$stripesize" = "k" ] && stripesize=""
|
---|
| 63 | allocation=`GetValueFromField $fname "LV Size"`
|
---|
| 64 | [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
|
---|
[911] | 65 | if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
|
---|
[298] | 66 | val=`echo "$allocation" | sed s/g//`
|
---|
| 67 | allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
|
---|
| 68 | fi
|
---|
| 69 | readahead=`GetValueFromField $fname "Read ahead sectors"`
|
---|
| 70 | rm -f $fname
|
---|
| 71 | [ "$stripes" ] && output="$output -i $stripes"
|
---|
| 72 | [ "$stripesize" ] && output="$output -I $stripesize"
|
---|
| 73 | [ "$allocation" ] && output="$output -L $allocation"
|
---|
| 74 | [ "$readahead" ] && output="$output -r $readahead"
|
---|
| 75 | echo "$output"
|
---|
[1] | 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | GenerateVgcreateParameters() {
|
---|
[298] | 81 | local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
|
---|
| 82 | current_VG=$1
|
---|
[1487] | 83 | VG_info_file=$MINDI_TMP/$$.vg-info.txt
|
---|
[2430] | 84 | # We use cat here as a way to avoid SElinux to prevent us from writing in $VG_info_file
|
---|
| 85 | $LVMCMD vgdisplay $current_VG | cat > $VG_info_file
|
---|
[298] | 86 | max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
|
---|
| 87 | [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
|
---|
| 88 | max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
|
---|
| 89 | [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
|
---|
| 90 | physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
|
---|
| 91 | output=""
|
---|
| 92 | [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
|
---|
| 93 | [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
|
---|
| 94 | [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
|
---|
| 95 | echo "$output"
|
---|
| 96 | rm -f $VG_info_file
|
---|
[1] | 97 | }
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | ProcessVolumeGroup() {
|
---|
[298] | 104 | local current_VG physical_volumes i list_of_devices VG_params
|
---|
| 105 | current_VG=$1
|
---|
| 106 | if [ $lvmversion = 2 ]; then
|
---|
| 107 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
[850] | 108 | list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
|
---|
[298] | 109 | else
|
---|
| 110 | info_file=/proc/lvm/VGs/$current_VG/group
|
---|
| 111 | physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
|
---|
| 112 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
| 113 | list_of_devices=""
|
---|
| 114 | for i in $physical_volumes ; do
|
---|
| 115 | fname=/proc/lvm/VGs/$current_VG/PVs/$i
|
---|
| 116 | device=`GetValueFromField $fname "name:"`
|
---|
| 117 | list_of_devices="$list_of_devices $device"
|
---|
| 118 | done
|
---|
| 119 | fi
|
---|
[2072] | 120 | l=""
|
---|
| 121 | if [ -f /etc/multipath.conf ]; then
|
---|
| 122 | # If multipath check which type of devidec are given, mpath prefered
|
---|
| 123 | for d in $list_of_devices; do
|
---|
| 124 | l="$l `GiveMapperOfdm $d`"
|
---|
| 125 | done
|
---|
| 126 | list_of_devices=$l
|
---|
| 127 | fi
|
---|
| 128 |
|
---|
[298] | 129 | echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
|
---|
| 130 | echo "# $LVMCMD vgchange -a y $current_VG"
|
---|
[1] | 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | ListAllPhysicalVolumes() {
|
---|
[298] | 136 | if [ $lvmversion = 2 ]; then
|
---|
[2072] | 137 | $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}' > $MINDI_TMP/pv.tmp
|
---|
[298] | 138 | else
|
---|
[2072] | 139 | pvscan 2> /dev/null | grep '"' | cut -d'"' -f2 > $MINDI_TMP/pv.tmp
|
---|
[298] | 140 | fi
|
---|
[2072] | 141 | if [ -f /etc/multipath.conf ]; then
|
---|
| 142 | # If multipath check which type of devidec are given, mpath prefered
|
---|
| 143 | for d in `cat $MINDI_TMP/pv.tmp`; do
|
---|
| 144 | GiveMapperOfdm $d
|
---|
| 145 | done
|
---|
| 146 | else
|
---|
| 147 | cat $MINDI_TMP/pv.tmp
|
---|
| 148 | fi
|
---|
| 149 | rm -f $MINDI_TMP/pv.tmp
|
---|
[1] | 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | ListAllVolumeGroups() {
|
---|
[298] | 154 | $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
|
---|
[1] | 155 | }
|
---|
| 156 |
|
---|
[2072] | 157 | GiveMapperOfdm () {
|
---|
[1] | 158 |
|
---|
[2072] | 159 | major=`stat -c "%t" $1`
|
---|
| 160 | minor=`stat -c "%T" $1`
|
---|
| 161 |
|
---|
| 162 | for i in `ls /dev/mapper/*`; do
|
---|
| 163 | mj=`stat -c "%t" $i`
|
---|
| 164 | mn=`stat -c "%T" $i`
|
---|
| 165 | if [ "$mj" = "$major" ] && [ "$mn" = "$minor" ]; then
|
---|
| 166 | echo "$i"
|
---|
| 167 | return
|
---|
| 168 | fi
|
---|
| 169 | done
|
---|
| 170 | echo $1
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
[1] | 174 | ListLvmDrivesAndPartitions() {
|
---|
[2072] | 175 | $LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}' > $MINDI_TMP/vg.tmp
|
---|
| 176 | if [ -f /etc/multipath.conf ]; then
|
---|
| 177 | # If multipath check which type of devidec are given, mpath prefered
|
---|
| 178 | for d in `cat $MINDI_TMP/vg.tmp`; do
|
---|
| 179 | GiveMapperOfdm $d
|
---|
| 180 | done
|
---|
| 181 | else
|
---|
| 182 | cat $MINDI_TMP/vg.tmp
|
---|
| 183 | fi
|
---|
| 184 | rm -f $MINDI_TMP/vg.tmp
|
---|
[1] | 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | PrettifyList() {
|
---|
[298] | 190 | local i
|
---|
| 191 | echo -en "$1"
|
---|
| 192 | for i in $2 ; do
|
---|
| 193 | echo -en "$i "
|
---|
| 194 | done
|
---|
| 195 | echo ""
|
---|
[1] | 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | ListAllLogicalVolumes() {
|
---|
[298] | 200 | if [ $lvmversion = 2 ]; then
|
---|
[1236] | 201 | $LVMCMD lvscan 2> /dev/null | grep "'" | cut -d"'" -f2
|
---|
[298] | 202 | else
|
---|
[1236] | 203 | lvscan 2> /dev/null | grep '"' | cut -d'"' -f2
|
---|
[298] | 204 | fi
|
---|
[1] | 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | WriteShutdownScript() {
|
---|
[298] | 210 | local i
|
---|
| 211 | echo ""
|
---|
| 212 | echo "Finally, to shut down and delete the volumes, do this:-"
|
---|
[1] | 213 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 214 | echo "($LVMCMD lvremove -f $i)"
|
---|
[1] | 215 | done
|
---|
| 216 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 217 | echo "($LVMCMD vgchange -a n $i)"
|
---|
[1] | 218 | done
|
---|
| 219 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 220 | echo "($LVMCMD vgremove $i)"
|
---|
[1] | 221 | done
|
---|
[298] | 222 | if [ $lvmversion = 2 ]; then
|
---|
| 223 | echo "(rmmod dm-mod & rmmod dm_mod & )"
|
---|
| 224 | else
|
---|
| 225 | echo "(rmmod lvm-mod)"
|
---|
| 226 | fi
|
---|
[1] | 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | # -------------------------------- main -----------------------------------
|
---|
[963] | 232 | which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
|
---|
[2044] | 233 | if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ]; then
|
---|
| 234 | exit 1
|
---|
[1] | 235 | fi
|
---|
| 236 |
|
---|
[299] | 237 | lvmversion=`lvmdiskscan --help 2>&1 |
|
---|
[1] | 238 | grep -E "Logical Volume Manager|LVM version:" |
|
---|
| 239 | cut -d: -f2 | cut -d. -f1 |
|
---|
| 240 | awk '{print $NF}' |
|
---|
| 241 | sed -e 's/ //g'`
|
---|
| 242 |
|
---|
[97] | 243 | if which lvm 2>/dev/null; then
|
---|
[298] | 244 | version=`lvm version | grep "LVM version" | awk '{print $3}'`
|
---|
| 245 | i="`echo "$version" | cut -d'.' -f1`"
|
---|
| 246 | echo "i=$i"
|
---|
| 247 | if [ "$i" -ge "2" ] ; then
|
---|
| 248 | lvmversion=2
|
---|
| 249 | fi
|
---|
[1] | 250 | fi
|
---|
| 251 |
|
---|
| 252 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 253 | echo "LVM version >= 2.0 found."
|
---|
| 254 | LVMCMD="lvm"
|
---|
| 255 | else
|
---|
| 256 | LVMCMD=""
|
---|
[1] | 257 | fi
|
---|
| 258 |
|
---|
| 259 | all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
|
---|
| 260 | echo "Just before you extrapolate mountlist to include RAID partitions,"
|
---|
| 261 | echo "extrapolate it to include the following LVM drives and partitions:-"
|
---|
| 262 | PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
|
---|
| 263 | echo "To get started, type:-"
|
---|
| 264 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 265 | echo "(insmod dm-mod)"
|
---|
| 266 | echo "(insmod dm_mod)"
|
---|
[1] | 267 | else
|
---|
[298] | 268 | echo "(insmod lvm-mod)"
|
---|
[1] | 269 | fi
|
---|
[298] | 270 | echo "# $LVMCMD vgchange -an"
|
---|
[1] | 271 | for i in `ListAllPhysicalVolumes` ; do
|
---|
[298] | 272 | echo "# echo y | $LVMCMD pvcreate -ff $i"
|
---|
[1] | 273 | done
|
---|
[706] | 274 | echo "# $LVMCMD vgscan"
|
---|
[1] | 275 | echo ""
|
---|
| 276 | echo "Create and activate the VG's (volume groups)."
|
---|
| 277 | all_volume_groups=`ListAllVolumeGroups`
|
---|
| 278 | for current_VG in $all_volume_groups ; do
|
---|
[298] | 279 | if [ $lvmversion -ne 2 ]; then
|
---|
| 280 | echo "# rm -Rf /dev/$current_VG"
|
---|
| 281 | fi
|
---|
| 282 | ProcessVolumeGroup $current_VG
|
---|
[1] | 283 | done
|
---|
| 284 | echo ""
|
---|
| 285 | echo "Finally, create the LV's (logical volumes)."
|
---|
| 286 | all_logical_volumes=`ListAllLogicalVolumes`
|
---|
| 287 | for current_LV in $all_logical_volumes ; do
|
---|
[298] | 288 | ProcessLogicalVolume $current_LV
|
---|
[1] | 289 | done
|
---|
| 290 | echo ""
|
---|
[298] | 291 | echo "# $LVMCMD vgscan"
|
---|
[1] | 292 | echo "Now you may format the LV's:-"
|
---|
| 293 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 294 | echo "(mkfs -t foo $i or something like that)"
|
---|
[1] | 295 | done
|
---|
| 296 | WriteShutdownScript
|
---|
| 297 | exit 0
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 |
|
---|