Centos6/7、Ubuntu、Debain 系统盘扩容方法

作者: ropon 分类: Shell 发布时间: 2018-07-18 21:16

CentOS6

需要使用gdisk工具,若无可使用yum -y install gdisk命令安装

开始扩容

gdisk /dev/vda

p #查看分区

d #删除分区

n #新建分区

p #默认选择主要分区

1 #分区编号

2048 #开始扇区(默认直接回车)

xxxxx #结束扇区(默认直接回车)

w #保存分区表并退出

 

Centos7

与Centos6相同,只不过可以直接使用fdisk工具

 

#!/bin/bash
rsnamedir="/root/auto_expansion.sh"
rclocal_cmd="/root/autodisksystem.sh"
rclocal_dir="/etc/rc.d/rc.local"
fstab_dir="/etc/fstab"
Swapdir="/www/swap"
rsname=auto_expansion #shell名称
LOCKfile=/root/.$(basename $0).lock
LOGfile=/root/.$(basename $0).log
devname="/dev/vda"

Echo()
{
	case $1 in
	    success)  flag="\033[1;32m"
	    ;;
	    failure)  flag="\033[1;31m"
	    ;;
	    warning)  flag="\033[1;33m"
	    ;;
	    msg)  flag="\033[1;34m"
	    ;;
	    *)  flag="\033[1;34m"
	    ;;
	esac
	if [[ $LANG =~ [Uu][Tt][Ff] ]]
	then
		echo -e "${flag}${2}\033[0m"
	else
		echo -e "${flag}${2}\033[0m" | iconv -f utf-8 -t gbk
	fi
	#写日志
	[ "${3}A" == "LogA" ] && Shell_log $2
}

Shell_log(){
    LOG_INFO=$1
    Echo "msg" "$(date "+%Y-%m-%d") $(date "+%H:%M:%S"):$rsname:$LOG_INFO" >> $LOGfile
}

Shell_lock(){
    touch $LOCKfile
}
Shell_unlock(){
    rm -f $LOCKfile
}
Exit(){
	Shell_unlock
	exit
}

Check_OS()
{
	if [ -e /etc/redhat-release ]; then
		OS=CentOS
		[ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && OS_Ver=7
		[ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" ] && OS_Ver=6
	elif [ -n "$(grep -i 'Debian' /etc/issue 2> /dev/null)" ]; then
		OS=Debian
	elif [ -n "$(grep -i 'Ubuntu' /etc/issue 2> /dev/null)" ]; then
		OS=Ubuntu
	else
		Echo "failure" "不支持此系统" "Log"
		Exit
	fi

}

CentOS6()
{
	which gdisk >/dev/null 2>&1
	[ $? -ne 0 ] && yum -y install gdisk
	gdisk $1 << EOF
p
d
n	
1
2048


w
y
y
EOF
	sed -i "/^nohup \/root\/.*/a$rclocal_cmd" $rclocal_dir
}


CentOS7()
{
	fdisk $1 << EOF
p
d
n
p	
1
2048

w
EOF
	sed -i "/^nohup \/root\/.*/a$rclocal_cmd" $rclocal_dir
}

Debian()
{
	fdisk $1 << EOF
p
d
n
p	
1
2048

w
EOF
	rclocal_dir="/etc/rc.local"
	sed -i "/^nohup \/root\/.*/a$rclocal_cmd" $rclocal_dir
}

Ubuntu()
{
	fdisk $1 << EOF
p
d
n
p	
1
2048

w
EOF
	rclocal_dir="/etc/rc.local"
	sed -i "/^\/root\/.*/a$rclocal_cmd" $rclocal_dir
}

SwapOff()
{
	Swapdir=$(cat /etc/fstab |grep swap|awk '{print $1}')
	swapoff $Swapdir
	cp ${fstab_dir}{,_bak}
	temp="#${Swapdir}    swap    swap    defaults    0 0"
	sed -i "s@${Swapdir}.*@$temp@" $fstab_dir
}

Main()
{
	Check_OS
	Swap=`free -m | awk '/Swap:/{print $2}'`
	[ "$Swap" -ne '0' ] && Echo "msg" "根分区存在交换分区,正在关闭。" "Log" && SwapOff
	Echo "msg" "当前系统是$OS$OS_Ver,开始扩容。" "Log"
	$OS$OS_Ver $devname
	Echo "msg" "扩容完成,将创建重启后处理脚本$rclocal_cmd。" "Log"
	cat > $rclocal_cmd <<EOF
#!/bin/bash
resize2fs -f /dev/vda1
sed -i '/^\/root\/autodisksystem.sh/d' $rclocal_dir
if [ "$Swap" -ne "0" ]; then
	sed -i "s@#${Swapdir}.*@${Swapdir}    swap    swap    defaults    0 0@" $fstab_dir
	swapon $Swapdir
fi
[ -e /root/autodisksystem.sh ] && rm -f /root/autodisksystem.sh	
[ -e /root/auto_expansion.sh ] && rm -f /root/auto_expansion.sh	
EOF
	chmod +x $rclocal_cmd
	Echo "success" "所有准备工作完成,3秒后自动重启并完成扩容大小"  "Log"
	Shell_unlock
	sleep 3
	reboot
}
if [ -f "$LOCKfile" ];then
	Echo "warning" "核实脚本正在运行中,请勿重复运行,若是之前强行中断引起的,请手工清理$LOCKfile"  "Log" && exit
else
	Echo "msg" "首次运行脚本,将自动创建锁文件然后继续,避免在执行中重复运行脚本。"  "Log"
	Shell_lock
fi
Main

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!