DDR爱好者之家 Design By 杰米

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

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

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

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

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

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

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

脚本内容如下:

#!/bin/bash
# Author: yeho <lj2007331 AT gmail.com>
# 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/oneinstack
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
#######################################################################
#    OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+   #
#               Auto fdisk               #
#    For more information please visit http://oneinstack.com    #
#######################################################################
"
echo=echo
for cmd in echo /bin/echo; do
    $cmd >/dev/null 2>&1 || continue
    if ! $cmd -e "" | grep -qE '^-e'; then
        echo=$cmd
        break
    fi
done
CSI=$($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=/data
FSTAB_FILE=/etc/fstab
count=0
TMP1=/tmp/.tmp1
TMP2=/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
  exit
else
  echo
  echo "${CMSG}Step 1.No lock file, begin to create lock file and continue${CEND}"
  echo
  touch $LOCKfile
fi
# check disk partition
check_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 os
check_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 ext4
modify_env() {
  modprobe ext4
  yum -y install e4fsprogs
}
# fdisk ,formating and create the file system
fdisk_fun() {
fdisk -S 56 $1 << EOF
n
p
1
wq
EOF
sleep 5
mkfs.ext4 ${1}1
}
# make directory
make_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 device
main() {
  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 script
echo "${CMSG}Step 2.Begin to check free disk${CEND}"
#service mysqld stop
#mv /data /root
check_os
check_disk
main
df -h
#mv /root/data/* /data
#service mysqld start
rm -rf $LOCKfile $TMP1 $TMP2

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。