linux系统挂载数据盘到VPS自定义目录

作者: mikj 分类: google, linux, vps, 教程使用, 资源分享 发布时间: 2018-12-20 11:43 ė 62条评论

linux系统挂载数据盘到VPS自定义目录

如果挂载数据盘做网站的目录,最好先备份好站点文件及数据,然后去挂载,不然挂载到原来有数据的目录会丢失,查看数据盘名称。其中/dev/vdb为数据盘。

fdisk -l

可以看到10G是系统盘,新加(vdb)的250G是数据盘,没有挂载的,根据你自己的数据盘名字变化,,现在来操作一下,看截图:

fdisk /dev/vdb1

因为我之前已经操作过一次了,看图来操作就好了,然后再次fdisk -l。

分区好了就可以格式化了.

mkfs.ext3 /dev/vdb1

如果有网站目录就直接设置,没有就创建/home/wwwroot,然后加到启动项挂载.

mkdir /home/wwwroot
echo '/dev/vdb1 /home/wwwroot ext3 defaults 0 0' >> /etc/fstab
mount -a

end,最后分享个自动分区脚本,root下操作

bash auto_fdisk.sh
#/bin/bash
#########################################
#Function:   auto fdisk
#Usage:      bash auto_fdisk.sh
#dbdir:     /home/wwwroot
#########################################

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 "\033[1;40;31mThe script is already exist,please next time to run this script.\033[0m"
  exit
else
  echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\033[40;37m"
  touch $LOCKfile
fi

#check user
if [ $(id -u) != "0" ]
then
  echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\033[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 "\033[40;32mThe $i disk is mounted.\033[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 "\033[1;40;31mNo free disk need to be fdisk.Exit script.\033[0m"
    rm -rf $LOCKfile
    exit 0
  else
    echo -e "\033[40;32mThis system have free disk :\033[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 "\033[40;32mStep 4.Begin to make directory\033[40;37m"
  for j in `seq $count`
  do
    if [ -d "/home/wwwroot$j" ]
    then
      echo -e "\033[1;40;31m/home/wwwroot$j is exists.This script will exit,you must to choose a directory for mount.\033[0m"
      rm -rf $LOCKfile $tmp2
      exit
    else
      echo "/home/wwwroot$j" >>$tmp1
      mkdir /home/wwwroot$j
    fi
  done
 }

#config /etc/fstab and mount device
main()
{
  for i in `echo $disk_list`
  do
    echo -e "\033[40;32mStep 3.Begin to fdisk free disk.\033[40;37m"
    fdisk_fun $i
    echo "${i}1" >>$tmp2
  done
  make_dir
  >$LOCKfile
  paste $tmp2 $tmp1 >$LOCKfile
  echo -e "\033[40;32mStep 5.Begin to write configuration to /etc/fstab and mount device.\033[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 "\033[40;32mStep 2.Begin to check free disk.\033[40;37m"
check_disk
main
df -h
rm -rf $LOCKfile $tmp1 $tmp2

本文出自 小沨的天空,转载时请注明出处及相应链接。

本文永久链接: https://www.xinai.de/83082.html

2条评论

  1. antior 2018年12月31日 下午12:17 回复

    嘿嘿,这个我也写过

    1. mikj 2019年1月1日 下午4:55 回复

      666

发表评论

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

You must enable javascript to see captcha here!

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Ɣ回顶部