人为调试参考 http://blog.csdn.net/andy_yf/article/details/7831482
centos6.5 分区调整...脚本...
#!/bin/sh
#获取分区名及其大小
#待扩容的分区
a="/dev/mapper/vg_slaver01-lv_root"
#待缩小的分区
b="/dev/mapper/vg_slaver01-lv_home"
size=$(df -h | egrep ${b} | awk '{print $2}'| sed s/[GTM]//g)
unit=$(df -h | egrep ${b} | awk '{print $2}'| sed s/[0-9]//g)
#根据单位换算size
case ${unit} in
G)
size=${size};;
T)
size=`echo "${size}*1000" | bc`;;
M)
size=`echo "${size}/1000" | bc`;;
esac
partition=$(df -h | egrep ${b} | awk '{print $1}')
root1=$( df -h | egrep ${a} | awk '{print $1}')
#获取为重新分配前free的值
vgdisplay
size1=($(vgdisplay | egrep Free | awk '{print $7}'))
#换算 size1
unit1=($(vgdisplay | egrep Free | awk '{print $8}'))
for(( c=$[${#size1[@]}-1] ; c>=0 ; c-- ))
do
case ${unit1} in
GiB)
size1[c]=${size1[c]};;
TiB)
size1[c]=`echo "${size1[c]}*1024" | bc`;;
MiB)
size1[c]=`echo "${size1[c]}/1024" | bc`;;
esac
done
#判断/home是否存在
if [ "${partition}" = "" ];then
echo " ${b} Partition does not exist"
else
#输入需要分配的大小
df -h
echo -n "enter ${root1} enlarge num:"
read num
#打印 关键数据
echo ${size}
echo ${partition}
echo ${root1}
echo ${num}
#卸载/home分区
fuser -m ${partition} ; umount ${partition}
if [ $? != 0 ];then
echo "Uninstall fail"
else
#从新设定/home的大小
e2fsck -f ${partition}
if [ $? != 0 ];then
echo "fail:e2fsck -f ${partition}"
else
size=`expr ${size} - ${num}`
resize2fs -p ${partition} ${size}G
if [ $? != 0 ];then
echo "fail:resize2fs -p ${partition} ${size}"
else
mount ${partition}
#将/home逻辑分区减小到指定大小
lvreduce -L ${size}G ${partition} <
y
EOF
if [ $? != 0 ];then
echo "lvreduce ${partition} fail"
else
#设定root大小
difference=0
lvextend -L +${num}G ${root1}
if [ $? != 0 ];then
echo "lvextend fail"
else
resize2fs -p ${root1} || echo "resize2fs ${root1} fail"
vgdisplay
size2=($(vgdisplay | egrep Free | awk '{print $7}'))
#换算size2的值
#换算 size2
unit2=($(vgdisplay | egrep Free | awk '{print $8}'))
for(( c=$[${#size1[@]}-1] ; c>=0 ; c-- ))
do
case ${unit1} in
GiB)
size2[c]=${size2[c]};;
TiB)
size2[c]=`echo "${size2[c]}*1024" | bc`;;
MiB)
size2[c]=`echo "${size2[c]}/1024" | bc`;;
esac
done
#获取分配后free的值
for(( c=$[${#size1[@]}-1] ; c>=0 ; c-- ))
do
difference=`echo "${size2[c]}-${size1[c]}+ ${difference}" | bc`
done
echo ${difference}
lvextend -L +${difference}G ${partition}
resize2fs -p ${partition}
fi
fi
fi
fi
fi
fi