博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自动分区、格式化、挂载脚本
阅读量:4485 次
发布时间:2019-06-08

本文共 4987 字,大约阅读时间需要 16 分钟。

功能:自动检测是否有尚未分区的数据盘,格式化新的数据盘并自动挂载

解决了什么问题:一键式检测是否有尚未分区的数据盘,并能对其格式化和自动挂载,省去了复杂的命令和步骤

执行方法:以root身份执行命令

wget http://mirrors.linuxeye.com/scripts/auto_fdisk.shchmod +x auto_fdisk.sh./auto_fdisk.sh

结果:出现如下即自动分区、格式化、挂载成功:

脚本内容如下:

#!/bin/bash# Author:  yeho 
# BLOG: https://blog.linuxeye.com## Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+## Project home page:# http://oneinstack.com# https://github.com/lj2007331/oneinstackexport PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binclearprintf "######################################################################## OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ ## Auto fdisk ## For more information please visit http://oneinstack.com ########################################################################"echo=echofor cmd in echo /bin/echo; do $cmd >/dev/null 2>&1 || continue if ! $cmd -e "" | grep -qE '^-e'; then echo=$cmd break fidoneCSI=$($echo -e "\033[")CEND="${CSI}0m"CDGREEN="${CSI}32m"CRED="${CSI}1;31m"CGREEN="${CSI}1;32m"CYELLOW="${CSI}1;33m"CBLUE="${CSI}1;34m"CMAGENTA="${CSI}1;35m"CCYAN="${CSI}1;36m"CSUCCESS="$CDGREEN"CFAILURE="$CRED"CQUESTION="$CMAGENTA"CWARNING="$CYELLOW"CMSG="$CCYAN"# Check if user is root[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; } MOUNT_DIR=/dataFSTAB_FILE=/etc/fstabcount=0TMP1=/tmp/.tmp1TMP2=/tmp/.tmp2> $TMP1> $TMP2# check lock file, one time only let the script run one time LOCKfile=/tmp/.$(basename $0)if [ -f "$LOCKfile" ];then echo echo "${CWARNING}The script is already exist, please next time to run this script${CEND}" echo exitelse echo echo "${CMSG}Step 1.No lock file, begin to create lock file and continue${CEND}" echo touch $LOCKfilefi# check disk partitioncheck_disk() { > $LOCKfile for i in `fdisk -l | grep "Disk" | grep "/dev" | awk '{print $2}' | awk -F: '{print $1}' | grep "vd"` do DEVICE_COUNT=$(fdisk -l $i | grep "$i" | awk '{print $2}' | awk -F: '{print $1}' | wc -l) NEW_MOUNT=$(df -h) if [ $DEVICE_COUNT -lt 2 ];then if [ -n "$(echo $NEW_MOUNT | grep -w "$i")" -o "$(grep -v '^#' $FSTAB_FILE | grep -v ^$ | awk '{print $1,$2,$3}' | grep -w "$i" | awk '{print $2}')" == '/' -o "$(grep -v '^#' $FSTAB_FILE | grep -v ^$ | awk '{print $1,$2,$3}' | grep -w "$i" | awk '{print $3}')" == 'swap' ];then echo "${CWARNING}The $i disk is mounted${CEND}" else echo $i >> $LOCKfile echo "You have a free disk, Now will fdisk it and mount it" fi fi done DISK_LIST=$(cat $LOCKfile) if [ "X$DISK_LIST" == "X" ];then echo echo "${CWARNING}No free disk need to be fdisk. Exit script${CEND}" echo rm -rf $LOCKfile exit 0 else echo "${CMSG}This system have free disk :${CEND}" for i in `echo $DISK_LIST` do echo "$i" count=$((count+1)) done [ $count -gt 1 ] && { echo "${CWARNING}This system has at least two free disk, You must manually mount it${CEND}"; exit 0; } fi}# check oscheck_os() { os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null) os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null) if [ "$os_release" ] && [ "$os_release_2" ];then if echo "$os_release" | grep "release 5" >/dev/null 2>&1;then os_release=aliyun5 modify_env fi fi}# install ext4modify_env() { modprobe ext4 yum -y install e4fsprogs}# fdisk ,formating and create the file systemfdisk_fun() {fdisk -S 56 $1 << EOFnp1wqEOFsleep 5mkfs.ext4 ${1}1}# make directorymake_dir() { echo "${CMSG}Step 4.Begin to make directory${CEND}" [ -d "$MOUNT_DIR" ] && mv ${MOUNT_DIR}{,_bk} mkdir -p $MOUNT_DIR echo "$MOUNT_DIR" >> $TMP1}# config /etc/fstab and mount devicemain() { for i in `echo $DISK_LIST` do echo echo "${CMSG}Step 3.Begin to fdisk free disk${CEND}" [ -n "`df -h | grep ${i}1`" ] && { echo "${CFAILURE}The ${i}1 already mount${CEND}"; echo; exit 0; } fdisk_fun $i > /dev/null 2>&1 echo echo "${i}1" >> $TMP2 done make_dir > $LOCKfile paste $TMP2 $TMP1 > $LOCKfile echo echo "${CMSG}Step 5.Begin to write configuration to /etc/fstab and mount device${CEND}" while read a b do [ -z "`grep ^${a} $FSTAB_FILE`" -a -z "`grep ${b} $FSTAB_FILE`" ] && echo "${a} $b ext4 defaults 0 0" >> $FSTAB_FILE done < $LOCKfile mount -a echo}# start scriptecho "${CMSG}Step 2.Begin to check free disk${CEND}"#service mysqld stop#mv /data /rootcheck_oscheck_diskmaindf -h#mv /root/data/* /data#service mysqld startrm -rf $LOCKfile $TMP1 $TMP2

 原文:https://blog.linuxeye.com/433.html

转载于:https://www.cnblogs.com/lj2007331/p/4955571.html

你可能感兴趣的文章
Ubuntu install 错误 E:Unable to locate package
查看>>
数据库索引的优缺点分析
查看>>
H.264 SODB RBSP EBSP的区别
查看>>
iOS裁剪,缩放图片白边问题解决办法
查看>>
CMD Markdown basic & Math Cheatsheet
查看>>
中国剩余定理
查看>>
高性能 CSS3 动画
查看>>
列出zip文件内全部内容 当前目录下的所有文件压缩成zip格式的文件(file.zip)
查看>>
centos 7 五笔安装
查看>>
[转]js获得session
查看>>
模糊查询和聚合函数
查看>>
弱网测试总结
查看>>
js动弹特效
查看>>
淘宝登录脚本-selenium
查看>>
maven坐标及依赖范围的学习(1)
查看>>
微网站|手机端html弹窗、弹层、提示框、加载条
查看>>
Android WebView ScrollBar设置
查看>>
JQuery中查找父元素,子元素,追加元素,插入元素和删除元素 及其他常用方法...
查看>>
Python单例实现
查看>>
Pytest+Allure定制报告
查看>>