自动分区、格式化、挂载脚本

2019年11月5日

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

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

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

  1. wget auto_fdisk.sh
  2. chmod +x auto_fdisk.sh
  3. ./auto_fdisk.sh

vim auto_fdisk.sh

  1.  
  2.     #!/bin/bash
  3.     # Author:  yeho <lj2007331 AT gmail.com>
  4.     # BLOG:  https://linuxeye.com
  5.     #
  6.     # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  7.     #
  8.     # Project home page:
  9.     #       http://oneinstack.com
  10.     #       https://github.com/lj2007331/oneinstack
  11.     export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  12.     clear
  13.     printf "
  14.     #######################################################################
  15.     #       OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+      #
  16.     #                             Auto fdisk                              #
  17.     #       For more information please visit http://oneinstack.com       #
  18.     #######################################################################
  19.     "
  20.     echo=echo
  21.     for cmd in echo /bin/echo; do
  22.             $cmd >/dev/null 2>&1 || continue
  23.             if ! $cmd -e "" | grep -qE '^-e'; then
  24.                     echo=$cmd
  25.                     break
  26.             fi
  27.     done
  28.     CSI=$($echo -e "\033[")
  29.     CEND="${CSI}0m"
  30.     CDGREEN="${CSI}32m"
  31.     CRED="${CSI}1;31m"
  32.     CGREEN="${CSI}1;32m"
  33.     CYELLOW="${CSI}1;33m"
  34.     CBLUE="${CSI}1;34m"
  35.     CMAGENTA="${CSI}1;35m"
  36.     CCYAN="${CSI}1;36m"
  37.     CSUCCESS="$CDGREEN"
  38.     CFAILURE="$CRED"
  39.     CQUESTION="$CMAGENTA"
  40.     CWARNING="$CYELLOW"
  41.     CMSG="$CCYAN"
  42.     # Check if user is root
  43.     [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  44.     MOUNT_DIR=/data
  45.     FSTAB_FILE=/etc/fstab
  46.     count=0
  47.     TMP1=/tmp/.tmp1
  48.     TMP2=/tmp/.tmp2
  49.     > $TMP1
  50.     > $TMP2
  51.     # check lock file, one time only let the script run one time
  52.     LOCKfile=/tmp/.$(basename $0)
  53.     if [ -f "$LOCKfile" ];then
  54.         echo
  55.         echo "${CWARNING}The script is already exist, please next time to run this script${CEND}"
  56.         echo
  57.         exit
  58.     else
  59.         echo
  60.         echo "${CMSG}Step 1.No lock file, begin to create lock file and continue${CEND}"
  61.         echo
  62.         touch $LOCKfile
  63.     fi
  64.     # check disk partition
  65.     check_disk() {
  66.         > $LOCKfile
  67.         for i in `fdisk -l | grep "Disk" | grep "/dev" | awk '{print $2}' | awk -F: '{print $1}' | grep "vd"`
  68.         do
  69.             DEVICE_COUNT=$(fdisk -l $i | grep "$i" | awk '{print $2}' | awk -F: '{print $1}' | wc -l)
  70.             NEW_MOUNT=$(df -h)
  71.             if [ $DEVICE_COUNT -lt 2 ];then
  72.                 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
  73.                     echo "${CWARNING}The $i disk is mounted${CEND}"
  74.                 else
  75.                     echo $i >> $LOCKfile
  76.                     echo "You have a free disk, Now will fdisk it and mount it"
  77.                 fi
  78.             fi
  79.         done
  80.         DISK_LIST=$(cat $LOCKfile)
  81.         if [ "X$DISK_LIST" == "X" ];then
  82.             echo
  83.             echo "${CWARNING}No free disk need to be fdisk. Exit script${CEND}"
  84.             echo
  85.             rm -rf $LOCKfile
  86.             exit 0
  87.         else
  88.             echo "${CMSG}This system have free disk :${CEND}"
  89.             for i in `echo $DISK_LIST`
  90.             do
  91.                 echo "$i"
  92.                 count=$((count+1))
  93.             done
  94.             [ $count -gt 1 ] && { echo "${CWARNING}This system has at least two free disk, You must manually mount it${CEND}"; exit 0; }
  95.         fi
  96.     }
  97.     # check os
  98.     check_os() {
  99.         os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
  100.         os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
  101.         if [ "$os_release" ] && [ "$os_release_2" ];then
  102.             if echo "$os_release" | grep "release 5" >/dev/null 2>&1;then
  103.                 os_release=aliyun5
  104.                 modify_env
  105.             fi
  106.         fi
  107.     }
  108.     # install ext4
  109.     modify_env() {
  110.         modprobe ext4
  111.         yum -y install e4fsprogs
  112.     }
  113.     # fdisk ,formating and create the file system
  114.     fdisk_fun() {
  115.     fdisk -S 56 $1 << EOF
  116.     n
  117.     p
  118.     1
  119.     wq
  120.     EOF
  121.     sleep 5
  122.     mkfs.ext4 ${1}1
  123.     }
  124.     # make directory
  125.     make_dir() {
  126.         echo "${CMSG}Step 4.Begin to make directory${CEND}"
  127.         [ -d "$MOUNT_DIR" ] && mv ${MOUNT_DIR}{,_bk}
  128.         mkdir -p $MOUNT_DIR
  129.         echo "$MOUNT_DIR" >> $TMP1
  130.     }
  131.     # config /etc/fstab and mount device
  132.     main() {
  133.         for i in `echo $DISK_LIST`
  134.         do
  135.             echo
  136.             echo "${CMSG}Step 3.Begin to fdisk free disk${CEND}"
  137.             [ -n "`df -h | grep ${i}1`" ] && { echo "${CFAILURE}The ${i}1 already mount${CEND}"; echo; exit 0; }
  138.             fdisk_fun $i > /dev/null 2>&1
  139.             echo
  140.             echo "${i}1" >> $TMP2
  141.         done
  142.         make_dir
  143.         > $LOCKfile
  144.         paste $TMP2 $TMP1 > $LOCKfile
  145.         echo
  146.         echo "${CMSG}Step 5.Begin to write configuration to /etc/fstab and mount device${CEND}"
  147.         while read a b
  148.         do
  149.             [ -z "`grep ^${a} $FSTAB_FILE`" -a -z "`grep ${b} $FSTAB_FILE`" ] && echo "${a} $b      ext4    defaults      0 0" >> $FSTAB_FILE
  150.         done < $LOCKfile
  151.         mount -a
  152.         echo
  153.     }
  154.     # start script
  155.     echo "${CMSG}Step 2.Begin to check free disk${CEND}"
  156.     #service mysqld stop
  157.     #mv /data /root
  158.     check_os
  159.     check_disk
  160.     main
  161.     df -h
  162.     #mv /root/data/* /data
  163.     #service mysqld start
  164.     rm -rf $LOCKfile $TMP1 $TMP2

没有评论

发表回复

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