小沨的天空

  • 首页
  • 图床
  • 赞助
  • 网盘
  • 关于本站
私人小天地
谈天说地,学习笔记
  1. 首页
  2. Linux
  3. 正文

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

2018年12月20日 1996点热度 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年4月24日

mikj

这个人很懒,什么都没留下

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

文章评论

  • antior

    嘿嘿,这个我也写过

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

      @antior 666

      2019年1月1日
      登录以回复
  • 您需要 登录 之后才可以评论
    站内搜索
    最新 热点 随机
    最新 热点 随机
    满5美元提现,每天签到和购物返现的网站 Win10蓝牙链接wifi热点网络和Win10使用蓝牙共享网络分享 Gravatar头像的镜像使用Cloudflare Workers反代自建 如何在论文中使用证据 acme申请google Public Certificate 免费ssl 证书应用LNMP Ubuntu的远程桌面环境使用Docker与Xfce Azure100美金使用开源的Azure 管理面板 网站Cloudflare使用CDN后如何获取前端真实客户IP Win10 共享文件夹(设置账户密码与终止所有连接) 戴尔PowerEdge R430设置RAID0 安装WINDOWS10
    Win10 共享文件夹(设置账户密码与终止所有连接) 利用crontab每个一个小时检查一次frpc监控状态,转寥寥 Vista,win7下的ipv6设置 nginx目录浏览配置第三方插件fancyindex 免费的FTP服务器资源下载 linux查看系统信息的命令 谷歌浏览器chrome官方离线安装包下载地址 directadmin升级和降级php cpanel简易安装教程(转) 设置IIS中最小权限分配的批处理文件(安全配置)
    标签聚合
    月捐 ubuntu linux centos 域名 Cloudflare 插件 网盘 DirectAdmin 代码 服务器 升级 php 免费 wordpress vps LNMP debian 安装 系统
    分类
    • Linux
    • VPS服务器
    • windows
    • wordpress
    • 域名相关
    • 建站分享
    • 教程学习
    • 数据库类
    • 未分类
    • 资源分享
    友情链接
    • 2.int.ru
    • Shucheng Li
    • QQTM论坛
    • 李子博客
    • 我的图库
    • logdns.free.fr
    • 天下无鱼
    • vzone
    • 夜狐
    • 小沨记事本
    • 寥寥后花园
    • 豆博
    • 回到未来博客
    • 死老鬼
    • 娃娃博客
    • 我喔喔喔
    • 爱购啦
    • 傻子-跸西的blog
    • semnew
    • 麦麦同学
    • 撸羊毛
    • 朱志瑞
    • xiaofengsky.free.fr
    • 陈否否
    • 怪鸟博客
    • QQPCC
    • 轨迹博客
    • 午夜客
    AD

    COPYRIGHT © 2022 Xinai.De. ALL RIGHTS RESERVED.@ 网站运行:@ 服务器运行状态

    Theme Kratos Made By Seaton Jiang