exit_script(){
exit 1
}
input_error_exit(){
if [ -z $1 ];then
echo -e "\033[31m "Incorrect input!!!" \033[0m"
exit_script
fi
}
#从现有物理卷扩展逻辑卷
extend_lv_from_pv(){
PVS_NAME=`pvs | grep -v "PFree" | awk '{if($6>0){print $1;}}'`
VG_NAME=`vgdisplay | awk '/VG Name/ {print $3}'`
LVS_NAME=`lvdisplay | awk '/LV Name/ {print $3}'`
if [ ${#PVS_NAME} == 0 ];then
echo -e "\033[31m "Don\'t available physics volume!" \033[0m"
exit_script
fi
echo -e "\033[34m Please select physics volume to operate: \033[0m"
select OPERATE_DISK in $PVS_NAME
do
break
done
input_error_exit $OPERATE_DISK
echo -e "\033[34m Please select logical volume to extend: \033[0m"
echo -e "\n"
select LV_NAME in $LVS_NAME
do
break
done
input_error_exit $LV_NAME
echo -e "\n"
#判断逻辑卷是根分区还是swap分区
unset TEMP
TEMP=`cat /etc/fstab | grep $LV_NAME | awk '{print $2}'`
if [ $TEMP = "/" ];then
lvextend /dev/$VG_NAME/$LV_NAME $OPERATE_DISK
unset TEST
TEST=`cat /etc/fstab | grep $LV_NAME | awk '{print $3}'`
if [ "$TEST" = "ext4" ] || [ "$TEST" = "ext3" ];then
resize2fs /dev/$VG_NAME/$LV_NAME
elif [ "$TEST" = "xfs" ];then
xfs_growfs /dev/$VG_NAME/$LV_NAME
fi
elif [ $TEMP = "swap" ] || [ $TEMP = "none" ];then
view_vg_size
read -p "Please input size to extend(G,g,M,m):" SIZE
swapoff /dev/$VG_NAME/$LV_NAME
lvextend -L +${SIZE} /dev/$VG_NAME/$LV_NAME
mkswap /dev/$VG_NAME/$LV_NAME
swapon /dev/$VG_NAME/$LV_NAME
fi
}
#从未分区的硬盘扩展逻辑卷
extend_lv_from_disk(){
#搜索所有/dev/sd开头的硬盘
DISKS=`fdisk -l | awk '/Disk/ {print $2}' | cut -d: -f1 | grep '\/dev\/sd*'`
I=0
#筛选未分区的硬盘,并将未分区的硬盘存入数组中
for J in $DISKS;do
TEST1=`fdisk -l $J | grep ^$J.`
FLAG1=$?
if [ $FLAG1 != 0 ];then
DISK[$I]=$J
I=`expr $I + 1`
fi
done
#如果没有可用硬盘,则退出脚本
if [ ${#DISK[*]} == 0 ];then
echo -e "\033[31m "Don\'t available disk!" \033[0m"
exit_script
fi
echo -e "\n"
echo -e "\033[34m "The following are unused hard drives." \033[0m"
#从未分区数组中选择要操作的硬盘
select OPERATION_DISK in ${DISK[*]}
do
break
done
input_error_exit $OPERATION_DISK
#创建分区
fdisk $OPERATION_DISK<<EOF
n
p
1
t
8e
w
EOF
PV_NAME=`fdisk -l $OPERATION_DISK | grep ^$OPERATION_DISK | awk '{print $1}'`
VG_NAME=`vgdisplay | awk '/VG Name/ {print $3}'`
LVS_NAME=`lvdisplay | awk '/LV Name/ {print $3}'`
#创建物理卷
pvcreate $PV_NAME
#将新建的物理卷加入到已有卷组中
vgextend $VG_NAME $PV_NAME
echo -e "\n"
echo -e "\033[34m Please select logical volume to extend: \033[0m"
select LV_NAME in $LVS_NAME
do
break
done
input_error_exit $LV_NAME
#判断逻辑卷是根分区还是swap分区
unset TEMP
TEMP=`cat /etc/fstab | grep $LV_NAME | awk '{print $2}'`
if [ $TEMP = "/" ];then
lvextend /dev/$VG_NAME/$LV_NAME $PV_NAME
unset TEST
TEST=`cat /etc/fstab | grep $LV_NAME | awk '{print $3}'`
if [ "$TEST" = "ext4" ] || [ "$TEST" = "ext3" ];then
resize2fs /dev/$VG_NAME/$LV_NAME
elif [ "$TEST" = "xfs" ];then
xfs_growfs /dev/$VG_NAME/$LV_NAME
fi
elif [ $TEMP = "swap" ] || [ $TEMP = "none" ];then
view_vg_size
read -p "Please input size to extend(G,g,M,m):" SIZE
echo -e "\n"
swapoff /dev/$VG_NAME/$LV_NAME
lvextend -L +${SIZE}G /dev/$VG_NAME/$LV_NAME
mkswap /dev/$VG_NAME/$LV_NAME
swapon /dev/$VG_NAME/$LV_NAME
fi
}
#查看可用的卷组大小
view_vg_size(){
VG_SIZE=`vgs |grep -v VG |awk '{print $7}'`
echo -e "\n"
echo -e "\033[36m "The size of the available logical group is:" \033[0m"
echo -e "\033[33m $VG_SIZE \033[0m"
echo -e "\n"
}
OPTION1='Extend Logical volume using a unpartitioned hard disk.'
OPTION2='Extend Logical volume using a free physics volume.'
echo -e "\n"
select OPTION in "$OPTION1" "$OPTION2"
do
echo -e "\n"
break
done
echo -e "\n"
input_error_exit $OPTION
case "$OPTION" in
"$OPTION1")
extend_lv_from_disk
;;
"$OPTION2")
extend_lv_from_pv
;;
*)
echo -e "\033[31m "Incorrect input!!!" \033[0m"
exit_script
;;
esac
CentOS新增加磁盘自动创建LVM卷组
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...