引言

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

创建磁盘挂载目录

mkdir -p /www

查询服务器磁盘信息

fdisk -l

root@TencentOS: ~

[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 您的浏览器不支持 SVG 格式文件,建议升级您的浏览器 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: ~

[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 开始创建分区

root@TencentOS: ~

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

输入 p 创建主分区

root@TencentOS: ~


Select (default p) : p

输入 1 设定分区编号

root@TencentOS: ~


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

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

root@TencentOS: ~


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

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

root@TencentOS:~


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

输入 wq 结束分区

root@TencentOS: ~

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: ~

[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 您的浏览器不支持 SVG 格式文件,建议升级您的浏览器 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 您的浏览器不支持 SVG 格式文件,建议升级您的浏览器
[root@TencentOS ~] #

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

格式化分区

mkfs.ext4 /dev/vdb1

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

root@TencentOS: ~

[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: ~


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

重新挂载所有分区

mount -a

检查是否挂载成功

df -h

root@TencentOS: ~

您的浏览器不支持 SVG 格式文件,建议升级您的浏览器
[root@TencentOS ~] #

一键挂载脚本

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