Linux系统挂载数据盘演示与一键挂载数据盘脚本

作者:简简单单 2015-01-16
适用系统:Linux(Redhat , CentOS,Debian,Ubuntu)

 

*  Linux的服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作。

 

下面的操作将会把数据盘划分为一个分区来使用。

 

1、查看数据盘

 

在没有分区和格式化数据盘之前,使用 “df –h”命令,是无法看到数据盘的,可以使用“fdisk -l”命令查看。如下图:

 

1(7)

 

友情提示:若您执行fdisk -l命令,发现没有 /dev/xvdb 标明您的服务无数据盘,那么您无需进行挂载,此时该教程对您不适用

 

2、 对数据盘进行分区

 

执行“fdisk -S 56 /dev/xvdb”命令,对数据盘进行分区;

 

根据提示,依次输入“n”,“p”“1”,两次回车,“wq”,分区就开始了,很快就会完成。

 

 2

 

3、 查看新的分区

 

使用“fdisk -l”命令可以看到,新的分区xvdb1已经建立完成了。

 

3 

 

4、格式化新分区

 

使用“mkfs.ext3 /dev/xvdb1”命令对新分区进行格式化,格式化的时间根据硬盘大小有所不同。

 

(也可自主决定选用 ext4 格式)

 

 4

 

5、添加分区信息

 

使用“echo ‘/dev/xvdb1  /mnt ext3    defaults    0  0′ >> /etc/fstab”(不含引号)命令写入新分区信息。
然后使用“cat /etc/fstab”命令查看,出现以下信息就表示写入成功。

 

*  如果需要把数据盘单独挂载到某个文件夹,比如单独用来存放网页,可以修改以上命令中的/mnt部分

 

5

 

6、挂载新分区

 

使用“mount -a”命令挂载新分区,然后用“df -h”命令查看,出现以下信息就说明挂载成功,可以开始使用新的分区了。

 

6

 

到这里你就完成了数据盘的挂载

下面分享一个脚本,一个挂载数据盘的脚本,只需运行脚本即可挂载数据盘,且数据盘挂载路径为/a

count=0
tmp1=/tmp/.tmp1
tmp2=/tmp/.tmp2
>$tmp1
>$tmp2
fstab_file=/etc/fstab

#check lock file ,one time only let the script run one time
LOCKfile=/tmp/.$(basename $0)
if [ -f "$LOCKfile" ]
then
  echo -e "33[1;40;31mThe script is already exist,please next time to run this script.33[0m"
  exit
else
  echo -e "33[40;32mStep 1.No lock file,begin to create lock file and continue.33[40;37m"
  touch $LOCKfile
fi

#check user
if [ $(id -u) != "0" ]
then
  echo -e "33[1;40;31mError: You must be root to run this script, please use root to install this script.33[0m"
  rm -rf $LOCKfile
  exit 1
fi

#check disk partition
check_disk()
{
  >$LOCKfile
  device_list=$(fdisk -l|grep "Disk"|grep "/dev"|awk '{print $2}'|awk -F: '{print $1}'|grep "xv")
  for i in `echo $device_list`
  do
    device_count=$(fdisk -l $i|grep "$i"|awk '{print $2}'|awk -F: '{print $1}'|wc -l)
    echo
    if [ $device_count -lt 2 ]
    then
      now_mount=$(df -h)
      if echo $now_mount|grep -w "$i" >/dev/null 2>&1
      then
        echo -e "33[40;32mThe $i disk is mounted.33[40;37m"
      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 -e "33[1;40;31mNo free disk need to be fdisk.Exit script.33[0m"
    rm -rf $LOCKfile
    exit 0
  else
    echo -e "33[40;32mThis system have free disk :33[40;37m"
    for i in `echo $disk_list`
    do
      echo "$i"
      count=$((count+1))
    done
  fi
}

#fdisk ,formating and create the file system
fdisk_fun()
{
fdisk -S 56 $1 << EOF
n
p
1


wq
EOF

sleep 5
mkfs.ext3 ${1}1
}

#make directory
make_dir()
{
  echo -e "33[40;32mStep 4.Begin to make directory33[40;37m"
  for j in `seq $count`
  do
    if [ -d "/a" ]
    then
      echo -e "33[1;40;31m/a is exists.This script will exit,you must to choose a directory for mount.33[0m"
      rm -rf $LOCKfile $tmp2
      exit
    else
      echo "/a" >>$tmp1
      mkdir /a
    fi
  done
 }

#config /etc/fstab and mount device
main()
{
  for i in `echo $disk_list`
  do
    echo -e "33[40;32mStep 3.Begin to fdisk free disk.33[40;37m"
    fdisk_fun $i
    echo "${i}1" >>$tmp2
  done
  make_dir
  >$LOCKfile
  paste $tmp2 $tmp1 >$LOCKfile
  echo -e "33[40;32mStep 5.Begin to write configuration to /etc/fstab and mount device.33[40;37m"
  while read a b
  do
    if grep -v ^# $fstab_file|grep ${a} >/dev/null
    then
      sed -i "s=${a}*=#&=" $fstab_file
    fi
    echo "${a}             $b                 ext3    defaults        0 0" >>$fstab_file
  done <$LOCKfile
  mount -a
}

#=========start script===========
echo -e "33[40;32mStep 2.Begin to check free disk.33[40;37m"
check_disk
main
df -h
rm -rf $LOCKfile $tmp1 $tmp2

相关文章

精彩推荐