惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
L
LangChain Blog
美团技术团队
N
Netflix TechBlog - Medium
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
博客园 - 司徒正美
爱范儿
爱范儿
D
DataBreaches.Net
月光博客
月光博客
U
Unit 42
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
腾讯CDC

草东日记

在这里,世界很大,烦恼很小 - 草东日记 魔兽世界插件 · 队伍查找器 - 草东日记 魔兽世界插件 · 队伍查找器 - 草东日记 魔兽世界插件 · 大秘境钥石助手 - 草东日记 魔兽世界插件 · 大秘境钥石助手 - 草东日记 魔兽世界插件 · 目标标记助手 - 草东日记 网易云音乐播放器插件 - 草东日记 一只未熄灭的烟头,照出了四川控烟新规的尴尬 - 草东日记 CRON 表达式 - 草东日记 这一刻,万象更新 - 草东日记 零下 42 度,冻醒了心事 - 草东日记 NetEase Music Player Demo - 草东日记 FavLink Demo - 草东日记 MacOS Dock Demo - 草东日记 在你的Linux设备上搭建Docker - 草东日记 [骗子] 陈庆洋 · 流窜于成都 - 草东日记 [骗子] 赵永奇 · 流窜于陕、川、渝 - 草东日记 小米路由器BE7000 · 解锁SSH开启发烧之旅 - 草东日记 小米路由器BE6500 Pro · 解锁SSH开启发烧之旅 - 草东日记 小米万兆路由器 · 解锁SSH开启发烧之旅 - 草东日记 日常训练 #10 - 草东日记 日常训练 #9 - 草东日记 日常训练 #8 - 草东日记 日常训练 #7 - 草东日记
Linux 服务器挂载云硬盘保姆教程 - 草东日记
草东日记 · 2024-04-21 · via 草东日记

引言

挂载磁盘是 Linux 系统中一个基本的操作,它可以帮助扩展存储空间,管理数据文件,备份和存储重要文件等。

重要提示

  • 以下教程是以无数据的数据盘基础上编写,如果服务器已经存在数据,请先保存好数据后再尝试操作。
  • 若对命令行完全不熟悉,建议使用文末提供的一键挂载脚本,效果等同。
  • 本文中的磁盘 /dev/vdb 为作者测试服务器上的磁盘命名,在您的服务器中可能是/dev/xdb/dev/vdb/dev/xvdb等,请根据实际情况对代码进行修改。

创建磁盘挂载目录

mkdir -p /www

查询服务器磁盘信息

fdisk -l

[root@tencentos ~]# fdisk -l Disk /dev/vda: 65 GiB, 69793218560 bytes, 136314880 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 262144 bytes / 262144 bytes Disklabel type: dos Disk identifier: 0xbd11af09 Disk /dev/vdb: 100 GiB, 107374182400 bytes, 209715200 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 262144 bytes / 262144 bytes Disklabel type: dos Disk identifier: 0xc293dd74

[root@TencentOS ~] #

若上述代码中 红色 部分为空,则说明尚未分区,未分区的磁盘是 /dev/vdb,在您的服务器中可能是 /dev/vdb1,请注意按照实际名称修改。

查询服务器磁盘信息

提示信息

若上述代码中存在红色部分内容,则表示已经分区成功,则跳过此步骤。

fdisk /dev/vdb

[root@tencentos ~]# fdisk /dev/vdb Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x811d7be1.

Command (m for help) : ~

输入 n 开始创建分区

Command (m for help):n
Partition type: p  primary (0 primary, 0 extended, 4 Free) e  extended

n

输入 p 创建主分区

输入 1 设定分区编号

Partition number (1-4, default 1) : 1

按下 回车 确认分区起始位置

First sector (2048-41943039, default 2048) : ~

按下 回车 确认分区结束位置

Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039) : ~

输入 wq 结束分区

First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039 Partition 1 of type Linux and of size 100 GiB is set
Command (m for help): wq The partition table has been alterrd!
Calling ioctrl() to re-read partition table.

Syncing disks.

检查分区是否成功

fdisk -l

[root@tencentos ~]# fdisk -l Disk /dev/vda: 65 GiB, 69793218560 bytes, 136314880 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 262144 bytes / 262144 bytes Disklabel type: dos Disk identifier: 0xbd11af09 Disk /dev/vdb: 100 GiB, 107374182400 bytes, 209715200 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 262144 bytes / 262144 bytes Disklabel type: dos Disk identifier: 0xc293dd74

[root@TencentOS ~] #

如果出现上述 红色 部分信息,说明已经分区成功。

格式化分区

mkfs.ext4 /dev/vdb1

这里输入看到的磁盘加分区号,如上一步红色部分信息,/dev/vdb1

[root@tencentos ~]# mkfs.ext4 /dev/vdb1 mke2fs 1.42.9(28-Dec-2013) Discarding device blocks: done Filesystem label= OS type: Linux Block size=4096 (1og=2) Fragment size=4096 (1og=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes,5242624 blocks 262131 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2153775104 160 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768,98304,163840,229376,294912,819200,884736,1605632,2654208,4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

开机自动挂载磁盘

echo "/dev/sdb1 /www ext4 defaults 0 0" >> /etc/fstab

将分区挂载信息添加到配置文件 /etc/fstab 中,实现开机/重启自动挂载。


[root@TencentOS ~] # echo "/dev/sdb1 /www ext4 defaults 0 0" >> /etc/fstab

[root@TencentOS ~] #

重新挂载所有分区

mount -a

检查是否挂载成功

df -h

一键挂载脚本

工具使用说明

  • 本工具默认将数据盘挂载到 /www 目录;
  • 若您的磁盘已分区,且未挂载,工具会自动将分区挂载到 /www
  • 若您的磁盘是新磁盘,工具会自动分区并格式化成 xfs/ext4 文件系统。
【1.5】 更新内容

1:添加文字提醒;

2:只有一个磁盘或www目录已被挂载的情况下,自动退出脚本,不执行任何操作;

3:修复对宝塔面板6.x/7.x的支持

【1.4】 更新内容

1:对宝塔面板提供迁移支持,当已安装面板时,暂停所有服务,自动重命名 /www,并迁移数据到新硬盘(全新硬盘未挂载分区) 。

注: 仅限3.x/4.x/5.x/6.x/7.x面板支持迁移;

注:迁移过程中会将系统盘原有面板数据备份至/bt-backup目录。

【1.3】 更新内容

1:添加文字提醒;

2:只有一个磁盘或www目录已被挂载的情况下,自动退出脚本,不执行任何操作;

3:修复对宝塔面板6.x/7.x的支持

【1.2】 更新内容

1:修正腾讯云部分节点自动挂载为只读的问题。

【1.1】 更新内容

1:增加指定挂载目录功能;

2:修正部分有独立引导分区的系统无法正确挂载的问题。

CentOS

yum install wget -y && wget -O auto_disk.sh https://oss.gaicas.com/linux/tools/auto_disk.sh && bash auto_disk.sh

Ubuntu

wget -O auto_disk.sh https://oss.gaicas.com/linux/tools/auto_disk.sh && sudo bash auto_disk.sh

Debian

wget -O auto_disk.sh https://oss.gaicas.com/linux/tools/auto_disk.sh && bash auto_disk.sh