[SHELL脚本练习]自动分区格式化挂载磁盘

1,297次阅读

共计 1828 个字符,预计需要花费 5 分钟才能阅读完成。

脚本用途

主要用于阿里云批量开通Linux服务器后的数据盘挂载处理

脚本功能

1、自动分区格式化并挂载磁盘

2、多磁盘可自动重复识别操作

3、对已挂载硬盘过滤处理,不会影响

4、仅学习shell测试编写23333333

脚本测试截图

已准备三块未挂载磁盘用于测试

[SHELL脚本练习]自动分区格式化挂载磁盘

单块磁盘依次挂载执行

挂载磁盘TEST-1前执行

[SHELL脚本练习]自动分区格式化挂载磁盘

挂载TEST-1磁盘,并执行

[SHELL脚本练习]自动分区格式化挂载磁盘

[SHELL脚本练习]自动分区格式化挂载磁盘

挂载TEST-2前执行

[SHELL脚本练习]自动分区格式化挂载磁盘

挂载TEST-2后执行

[SHELL脚本练习]自动分区格式化挂载磁盘

挂载TEST-3前执行

[SHELL脚本练习]自动分区格式化挂载磁盘

挂载TEST-3后执行

[SHELL脚本练习]自动分区格式化挂载磁盘

依次挂载结果

[SHELL脚本练习]自动分区格式化挂载磁盘

多磁盘同时挂载

[SHELL脚本练习]自动分区格式化挂载磁盘

脚本代码

#!/bin/bash
#2017-12-26
#Auto scan and mount useless disk 
#Version: 1.0

function SCAN {
	echo -e "\033[44;37mScaning...\033[0m"
	sleep 1
	> /tmp/mount.log
	ALL_DISK=`fdisk -l | grep -Ev "mapper|root|swap|docker" |grep ^"Disk /"|cut -d ' ' -f2 |cut -d: -f1`
	for i in ${ALL_DISK}
	do
		df -Th | grep ${i} &> /dev/null
		if [ $? -eq 0 ];then
			echo -e "Found Disk: ${i}  - \033[31mUsed\033[0m" | tee -a /tmp/mount.log
		else
			echo -e "Found Disk: ${i}  - \033[32mUseless\033[0m" | tee -a /tmp/mount.log
		fi
	done
	Used_Disk=`cat /tmp/mount.log | grep Used | cut -d ' ' -f3`
	Useless_Disk=`cat /tmp/mount.log | grep Useless | cut -d ' ' -f3`
}
function PART {
	for i in ${Useless_Disk}
	do
		echo -e "\033[36mFormating ${i}....\033]0m"
	sleep 1
FDISK=`which fdisk`
${FDISK} ${i} &> /dev/null <<EOF
n
p
1


wq
EOF
	echo -e "\033[32mDone\033[0m"
	done
}
function MKFS {
	for i in ${Useless_Disk}
	do
		echo -e "\033[36mMkfs ${i}....\033]0m"
		mkfs.ext4 ${i}1 &> /dev/null
		echo -e "\033[32mDone\033[0m"
		sleep 1
	done
}
function MOUNT {
	for i in ${Useless_Disk}
	do
		if [ ! -d /data ];then
			mkdir /data
			UUID_NUM=`blkid | grep "${i}1" | cut -d ' ' -f2`
			echo "${UUID_NUM} /data	ext4	defaults 0 0" >> /etc/fstab
			mount -a
			[ $? -eq 0 ] && echo "${i} Mount Finished." 
		else
			read -p "/data in uesd,Input new mount point:" NEW_POINT
			if [ -d ${NEW_POINT} ];then
				read -p "${NEW_POINT} in uesd,Input new mount point again:" NEW_POINT
				mkdir ${NEW_POINT}
				UUID_NUM=`blkid | grep "${i}1" | cut -d ' ' -f2`
				echo "${UUID_NUM} ${NEW_POINT}	ext4	defaults 0 0" >> /etc/fstab
				mount -a
				[ $? -eq 0 ] && echo "${i} Mount Finished." 
			else
				mkdir ${NEW_POINT}
				UUID_NUM=`blkid | grep "${i}1" | cut -d ' ' -f2`
				echo "${UUID_NUM} ${NEW_POINT}	ext4	defaults 0 0" >> /etc/fstab
				mount -a
				[ $? -eq 0 ] && echo "${i} Mount Finished." 
			fi
		fi
	done
}
function MAIN {
	SCAN
	if [ -z "${Useless_Disk}" ];then
		echo -e "\033[31mNot Fount Useless Disk.Exited...\033[0m" && exit 2
	fi
	PART
	MKFS
	MOUNT
	rm -fr /tmp/mount.log
}
MAIN
正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2017-12-29发表,共计1828字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索