小沨的天空

  • 首页
  • Code
  • Dns
  • 图床
  • 店铺
私人小天地
谈天说地,学习笔记
  1. 首页
  2. Linux
  3. 正文

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

2018年12月20日 900点热度 0人点赞 2条评论

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
标签: linux系统 vps 挂载数据 目录
最后更新:2019年04月24日

小沨

一个网络白痴,爱好网络生活和技术。 梦想成为一个网络糕手.喜欢简单的一个80后男人! Life is better when shared !

打赏 点赞
< 上一篇
下一篇 >

文章评论

  • antior

    嘿嘿,这个我也写过

    2018年12月31日
    登录以回复
    • mikj

      @antior 666

      2019年01月01日
      登录以回复
  • 您需要 登录 之后才可以评论

    小沨

    一个网络白痴,爱好网络生活和技术。 梦想成为一个网络糕手.喜欢简单的一个80后男人! Life is better when shared !

    小程序
    广告
    最新 热点 随机
    最新 热点 随机
    Server酱和Qmsg酱两个好用的推送服务 linux的ssh服务升级后无法启动修复 linux服务器的硬盘使用时间查询 启用chrome浏览器内置的二维码生成插件 宝塔面板或者LNMP下安装php的mongodb扩展 ShareX 对接tu.my图床实现截图和图片自动上传 帝国CMS的sitemap.xml设置和生成 hetzner服务器购买和安装debian系统 使用nginx的stream模块实现端口转发和TCP流量 服务器装Proxmox VE/多IP设置和单IP设置Nat转发
    linux的ssh服务升级后无法启动修复Server酱和Qmsg酱两个好用的推送服务
    最新openssl漏洞ccs注入及升级修复(OpenSSL CCS vuln. (CVE-2014-0224)) 一键安装最新内核并开启 BBR 脚本,转摘 衷心感谢蓝冰帮助我,建立起这个CGI博客!! Python setuptools import error WordPress博客更换域名,ssl域名301跳转 Nginx 502解决方法(www.qvob.com测试可用) 忘记 升级到Win10 Build 14316 中启用 Linux Bash 环境体验 感谢FREE8的OOTT123帮助我搞好appspot lynx文本浏览器的seo优化功能
    标签聚合
    DirectAdmin 网盘 ubuntu 升级 vps 服务器 php linux 系统 wordpress centos 免费
    开源相关
    • do mirrors
    • fedoraproject
    • centos vault
    • 阿里开源镜像站
    • linode mirrors
    • elrepo.org
    • Debian Mirror
    • CentOS mirrors
    • owncloud
    • Packages Search
    • RepoForge
    • Ubuntu Mirrors
    友情链接
    • QQPCC
    • 爱购啦
    • 傻子-跸西的blog
    • 寥寥后花园
    • 豆博
    • 麦麦同学
    • 撸羊毛
    • 回到未来博客
    • 我喔喔喔
    • 轨迹博客
    • 天下无鱼
    • 朱志瑞
    • 死老鬼
    • 夜狐
    • LiCEO
    • 李子博客
    • semnew
    • 娃娃博客
    • 午夜客
    • 陈否否
    • 我的图库
    • 怪鸟博客
    • 2.int.ru
    • vzone

    COPYRIGHT © 2020 小沨的天空. ALL RIGHTS RESERVED. @ 网站运行: @ 服务器运行状态

    THEME KRATOS MADE BY VTROIS