[673] | 1 | #!/bin/bash
|
---|
[275] | 2 | #
|
---|
| 3 | # $Id: analyze-my-lvm 1942 2008-05-23 14:40:56Z 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
|
---|
[298] | 84 | $LVMCMD vgdisplay $current_VG > $VG_info_file
|
---|
| 85 | max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
|
---|
| 86 | [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
|
---|
| 87 | max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
|
---|
| 88 | [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
|
---|
| 89 | physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
|
---|
| 90 | output=""
|
---|
| 91 | [ "$max_logical_volumes" ] && output="$output -l $max_logical_volumes"
|
---|
| 92 | [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
|
---|
| 93 | [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
|
---|
| 94 | echo "$output"
|
---|
| 95 | rm -f $VG_info_file
|
---|
[1] | 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | ProcessVolumeGroup() {
|
---|
[298] | 103 | local current_VG physical_volumes i list_of_devices VG_params
|
---|
| 104 | current_VG=$1
|
---|
| 105 | if [ $lvmversion = 2 ]; then
|
---|
| 106 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
[850] | 107 | list_of_devices=`$LVMCMD pvs | grep " $current_VG " | awk '{print $1}'`
|
---|
[298] | 108 | else
|
---|
| 109 | info_file=/proc/lvm/VGs/$current_VG/group
|
---|
| 110 | physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
|
---|
| 111 | VG_params=`GenerateVgcreateParameters $current_VG`
|
---|
| 112 | list_of_devices=""
|
---|
| 113 | for i in $physical_volumes ; do
|
---|
| 114 | fname=/proc/lvm/VGs/$current_VG/PVs/$i
|
---|
| 115 | device=`GetValueFromField $fname "name:"`
|
---|
| 116 | list_of_devices="$list_of_devices $device"
|
---|
| 117 | done
|
---|
| 118 | fi
|
---|
| 119 | echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
|
---|
| 120 | echo "# $LVMCMD vgchange -a y $current_VG"
|
---|
[1] | 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | ListAllPhysicalVolumes() {
|
---|
[298] | 126 | if [ $lvmversion = 2 ]; then
|
---|
| 127 | $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}'
|
---|
| 128 | else
|
---|
| 129 | pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
|
---|
| 130 | fi
|
---|
[1] | 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | ListAllVolumeGroups() {
|
---|
[298] | 135 | $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
|
---|
[1] | 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | ListLvmDrivesAndPartitions() {
|
---|
[911] | 140 | $LVMCMD vgdisplay -v 2> /dev/null | grep "PV Name" | sed 's/(#)//' | awk '{print $3}'
|
---|
[1] | 141 | }
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | PrettifyList() {
|
---|
[298] | 146 | local i
|
---|
| 147 | echo -en "$1"
|
---|
| 148 | for i in $2 ; do
|
---|
| 149 | echo -en "$i "
|
---|
| 150 | done
|
---|
| 151 | echo ""
|
---|
[1] | 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
| 155 | ListAllLogicalVolumes() {
|
---|
[298] | 156 | if [ $lvmversion = 2 ]; then
|
---|
[1236] | 157 | $LVMCMD lvscan 2> /dev/null | grep "'" | cut -d"'" -f2
|
---|
[298] | 158 | else
|
---|
[1236] | 159 | lvscan 2> /dev/null | grep '"' | cut -d'"' -f2
|
---|
[298] | 160 | fi
|
---|
[1] | 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | WriteShutdownScript() {
|
---|
[298] | 166 | local i
|
---|
| 167 | echo ""
|
---|
| 168 | echo "Finally, to shut down and delete the volumes, do this:-"
|
---|
[1] | 169 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 170 | echo "($LVMCMD lvremove -f $i)"
|
---|
[1] | 171 | done
|
---|
| 172 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 173 | echo "($LVMCMD vgchange -a n $i)"
|
---|
[1] | 174 | done
|
---|
| 175 | for i in `ListAllVolumeGroups` ; do
|
---|
[298] | 176 | echo "($LVMCMD vgremove $i)"
|
---|
[1] | 177 | done
|
---|
[298] | 178 | if [ $lvmversion = 2 ]; then
|
---|
| 179 | echo "(rmmod dm-mod & rmmod dm_mod & )"
|
---|
| 180 | else
|
---|
| 181 | echo "(rmmod lvm-mod)"
|
---|
| 182 | fi
|
---|
[1] | 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | # -------------------------------- main -----------------------------------
|
---|
[963] | 188 | which lvmdiskscan 2>/dev/null 2>&1 || Die "lvmdiskscan not found. Won't handle LVM."
|
---|
[275] | 189 | if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then
|
---|
[298] | 190 | exit 0
|
---|
[1] | 191 | fi
|
---|
| 192 |
|
---|
[299] | 193 | lvmversion=`lvmdiskscan --help 2>&1 |
|
---|
[1] | 194 | grep -E "Logical Volume Manager|LVM version:" |
|
---|
| 195 | cut -d: -f2 | cut -d. -f1 |
|
---|
| 196 | awk '{print $NF}' |
|
---|
| 197 | sed -e 's/ //g'`
|
---|
| 198 |
|
---|
[97] | 199 | if which lvm 2>/dev/null; then
|
---|
[298] | 200 | version=`lvm version | grep "LVM version" | awk '{print $3}'`
|
---|
| 201 | i="`echo "$version" | cut -d'.' -f1`"
|
---|
| 202 | echo "i=$i"
|
---|
| 203 | if [ "$i" -ge "2" ] ; then
|
---|
| 204 | lvmversion=2
|
---|
| 205 | fi
|
---|
[1] | 206 | fi
|
---|
| 207 |
|
---|
| 208 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 209 | echo "LVM version >= 2.0 found."
|
---|
| 210 | LVMCMD="lvm"
|
---|
| 211 | else
|
---|
| 212 | LVMCMD=""
|
---|
[1] | 213 | fi
|
---|
| 214 |
|
---|
| 215 | all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
|
---|
| 216 | echo "Just before you extrapolate mountlist to include RAID partitions,"
|
---|
| 217 | echo "extrapolate it to include the following LVM drives and partitions:-"
|
---|
| 218 | PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
|
---|
| 219 | echo "To get started, type:-"
|
---|
| 220 | if [ $lvmversion = 2 ]; then
|
---|
[298] | 221 | echo "(insmod dm-mod)"
|
---|
| 222 | echo "(insmod dm_mod)"
|
---|
[1] | 223 | else
|
---|
[298] | 224 | echo "(insmod lvm-mod)"
|
---|
[1] | 225 | fi
|
---|
[298] | 226 | echo "# $LVMCMD vgchange -an"
|
---|
[1] | 227 | for i in `ListAllPhysicalVolumes` ; do
|
---|
[298] | 228 | echo "# echo y | $LVMCMD pvcreate -ff $i"
|
---|
[1] | 229 | done
|
---|
[706] | 230 | echo "# $LVMCMD vgscan"
|
---|
[1] | 231 | echo ""
|
---|
| 232 | echo "Create and activate the VG's (volume groups)."
|
---|
| 233 | all_volume_groups=`ListAllVolumeGroups`
|
---|
| 234 | for current_VG in $all_volume_groups ; do
|
---|
[298] | 235 | if [ $lvmversion -ne 2 ]; then
|
---|
| 236 | echo "# rm -Rf /dev/$current_VG"
|
---|
| 237 | fi
|
---|
| 238 | ProcessVolumeGroup $current_VG
|
---|
[1] | 239 | done
|
---|
| 240 | echo ""
|
---|
| 241 | echo "Finally, create the LV's (logical volumes)."
|
---|
| 242 | all_logical_volumes=`ListAllLogicalVolumes`
|
---|
| 243 | for current_LV in $all_logical_volumes ; do
|
---|
[298] | 244 | ProcessLogicalVolume $current_LV
|
---|
[1] | 245 | done
|
---|
| 246 | echo ""
|
---|
[298] | 247 | echo "# $LVMCMD vgscan"
|
---|
[1] | 248 | echo "Now you may format the LV's:-"
|
---|
| 249 | for i in `ListAllLogicalVolumes` ; do
|
---|
[298] | 250 | echo "(mkfs -t foo $i or something like that)"
|
---|
[1] | 251 | done
|
---|
| 252 | WriteShutdownScript
|
---|
| 253 | exit 0
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 |
|
---|