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

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

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

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

执行方法:以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

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

原创文章,作者:SODQD,如若转载,请注明出处:http://www.wangzhanshi.com/n/7351.html

(0)
SODQD的头像SODQD
上一篇 2025年1月1日 16:29:23
下一篇 2025年1月1日 16:29:25

相关推荐

发表回复

登录后才能评论