Centos/Ubuntu/Debian系统数据盘一键分区|一键扩容脚本

作者: ropon 分类: Shell 发布时间: 2018-09-01 13:57
#!/bin/bash
#一键分区
#一键扩容
#用法:bash homeV2.sh

#v1.1.20180914
#新增避免重复同时运行
#新增日志记录

rsname=homeV2 #shell名称
LOCKfile=/root/.$(basename $0).lock
LOGfile=/root/.$(basename $0).log

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
}

CheckWDCP()
{
	service httpd $1
	service nginxd $1
	service mysqld $1
	service pureftpd $1
	[ -e /etc/init.d/wdcp ] && service wdcp $1
	[ -e /etc/init.d/wdapache ] && service wdapache $1
	[ -e /etc/init.d/redis_6379 ] && service redis_6379 $1
	[ -e /etc/init.d/memcached ] && service memcached $1
}

YunPanMBR()
{
	Echo "msg" "有挂载云盘$1且没有分区,是否需要一键分区。" "Log"
	fstab_file="/etc/fstab"
	read -p "[y/n]: " autoyunpan
	while [[ ! $autoyunpan =~ ^[y,n]$ ]] 
	do
		Echo "warning" "输入错误,只能输入y或n" "Log"
		read -p "[y/n]: " autoyunpan
	done
	if [ "$autoyunpan" == 'y' ];then
		yunpandev=$1
		fdisk $yunpandev << EOF
n
p
1
2048

w
EOF
		sleep 5
		mkfs.ext4 ${yunpandev}1
		Echo "msg" "请输入挂载点,比如:/yunpan" "Log"
		read -p ": " mountdir
		[ ! -d "$mountdir" ] && mkdir -p $mountdir
		echo "${yunpandev}1		$mountdir			ext4    defaults	0 0" >>$fstab_file
		mount -a
		df -vh
		Echo "success" "$yunpandev一键分区成功,挂载点是$mountdir" "Log"
	fi
}


Auto()
{
	tempdev=`ls /dev/vd[b-z]`
	Echo "msg" "请输入设备名,比如以下设备名,扩容数据盘[home分区]/dev/vdb请直接回车" "Log"
	Echo "msg" "$tempdev" "Log"
	read -p ":" devpart
	[ -z "$devpart" ] && devpart="/dev/vdb"
	while [[ ! -e "$devpart" ]] 
	do
		Echo "warning" "输入设备名错误,请重新输入" "Log"
		read -p ":" devpart
	done
	data_part=`df -vh |grep ${devpart}1|awk '{print $6}'`
	Swap=`free -m | awk '/Swap:/{print $2}'`
	Swapdir=$(cat /etc/fstab |grep swap|awk '{print $1}')
	[ "$Swap" -ne '0' ] && swapoff $Swapdir	
	[ -d /www/wdlinux/wdcp ] && Echo "msg" "预装WDCP面板系统,正在停止WDCP相关服务。" && CheckWDCP stop
	which fuser >/dev/null 2>&1
	[ $? -ne 0 ] && yum -y install psmisc fuser
	fuser -m $data_part -k
	umount $data_part
	[ $? -ne 0 ] && Echo "warning" "取消$data_part挂载失败,程序自动退出,请手动操作扩展" "Log" && [ -d /www/wdlinux/wdcp ] && CheckWDCP start && Exit
	Startflag=`parted $devpart print|sed -n '/primary/p'|awk '{print $2}'`
	parted $devpart print|grep primary > /parted.txt
	[ ! -e /parted.txt ] && Echo "warning" "备份分区表文件不成功,程序自动退出,请手动操作扩展" "Log"
	parted $devpart  rm 1
	yes|parted $devpart  mkpart primary $Startflag 100%
	[ $? -ne 0 ] && Echo "warning" "分区扩容失败,请手动操作扩展" "Log" && Exit
	Echo "msg" "如果硬盘较大,时间可能会很长,请耐心等待!" "Log"
	resize2fs -f ${devpart}1
	mount -a
	[ "$Swap" -ne '0' ] && swapon $Swapdir	
	[ -d /www/wdlinux/wdcp ] && Echo "msg" "预装WDCP面板系统,正在启动WDCP相关服务" "Log" && CheckWDCP start
	df -vh
	Echo "success" "$devpart扩容成功" "Log"
	Exit
}

Main()
{
	if [ -e "/dev/vdc" ] ;then 
		for devname in `ls /dev/vd[c-z]`
		do
			if [ -e "$devname" ] && [ ! -e "${devname}1" ] ;then
				YunPanMBR $devname
			fi
		done
	fi
	Echo "msg" "是否需要扩容磁盘" "Log"
	read -p "[y/n]: " autodisk
	while [[ ! $autodisk =~ ^[y,n]$ ]] 
	do
		Echo "warning" "输入错误,只能输入y或n" "Log"
		read -p "[y/n]: " autodisk
	done
	if [ "$autodisk" == 'y' ];then
		Auto
	fi
}
if [ -f "$LOCKfile" ];then
	Echo "warning" "核实脚本正在运行中,请勿重复运行,若是之前强行中断引起的,请手工清理$LOCKfile" "Log" && exit
else
	Echo "msg" "首次运行脚本,将自动创建锁文件然后继续,避免在执行中重复运行脚本。" "Log"
	Shell_lock
fi
Main
Exit

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注