Linux挂载数据盘

0.版本说明

环境:centos7.9
设备名:/dev/vdb
数据盘:容量:200 GiB

1.查看数据盘

运行以下命令,获取数据盘的设备名称

fdisk -lu

运行结果如下所示,表示当前ECS实例有两块云盘,/dev/vda是系统盘,/dev/vdb是新增数据盘。

[root@host-10-10-10-14 /]# fdisk -lu
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00019db9

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83886046    41941999+  83  Linux

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

2.为数据盘创建MBR分区

运行以下命令,对数据盘进行分区。其中,/dev/vdb是数据盘的设备名称,根据实际环境替换。

fdisk -u /dev/vdb

运行结果如下所示

[root@host-10-10-10-14 /]# fdisk -u /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

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 0x76259292.

Command (m for help): 

在fdisk交互命令行,输入【p】并按Enter键,查看数据盘的分区情况。
运行结果如下所示

Command (m for help): p

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76259292

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):

在fdisk交互命令行,输入【n】并按Enter键,开始新建分区。
运行结果如下所示

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

云盘有两种分区类型:p表示主分区。e表示扩展分区。
在fdisk交互命令行,输入【p】并按Enter键,开始创建一个主分区。
运行结果如下所示

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

Partition number表示主分区编号,可以选择1-4。默认1。
输入分区编号,按Enter键。如果创建一个主分区,可以直接按Enter键。
运行结果如下所示

Partition number (1-4, default 1): 
First sector (2048-419430399, default 2048):

输入起始扇区编号,按Enter键。不输入直接按Enter键,表示选择默认值。
运行结果如下所示

First sector (2048-419430399, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): 

输入最后一个扇区编号,按Enter键。不输入直接按Enter键,表示选择默认值。
运行结果如下所示

Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): 
Using default value 419430399
Partition 1 of type Linux and of size 200 GiB is set

Command (m for help):

在fdisk交互命令行,输入【p】并按Enter键,查看该数据盘的规划分区情况。
运行结果如下所示,表示新建分区/dev/vdb1的详细信息。

Command (m for help): p

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76259292

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   419430399   209714176   83  Linux

Command (m for help):

在fdisk交互命令行,输入【w】并按Enter键,将分区结果写入分区表中。
运行结果如下所示,表示分区创建完成。

Command (m for help): w                                             
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

运行以下命令,查看新分区信息。/dev/vdb是数据盘的设备名称,根据实际环境替换。

fdisk -lu /dev/vdb

运行结果如下所示,如果出现/dev/vdb1的相关信息,表示新分区已创建完成。

[root@host-10-10-10-14 /]# fdisk -lu /dev/vdb

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76259292

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   419430399   209714176   83  Linux

3.为分区创建ext4文件系统

运行以下命令,安装e2fsprogs工具。

yum install -y e2fsprogs

运行结果如下所示

[root@host-10-10-10-14 /]# yum install -y e2fsprogs
Loaded plugins: fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#7 - "Failed to connect to 2600:1f16:c1:5e01:4180:6610:5482:c1c0: Network is unreachable"
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was
14: curl#7 - "Failed to connect to 2600:1f16:c1:5e01:4180:6610:5482:c1c0: Network is unreachable"
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=stock error was
14: curl#7 - "Failed to connect to 2600:1f16:c1:5e01:4180:6610:5482:c1c0: Network is unreachable"
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                                                                                | 3.6 kB  00:00:00     
epel                                                                                                                                                | 4.7 kB  00:00:00     
extras                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                             | 2.9 kB  00:00:00     
(1/5): epel/x86_64/group_gz                                                                                                                         |  99 kB  00:00:00     
(2/5): extras/7/x86_64/primary_db                                                                                                                   | 249 kB  00:00:00     
(3/5): epel/x86_64/updateinfo                                                                                                                       | 1.0 MB  00:00:01     
(4/5): epel/x86_64/primary_db                                                                                                                       | 7.0 MB  00:00:06     
(5/5): updates/7/x86_64/primary_db                                                                                                                  |  21 MB  00:00:15     
Package e2fsprogs-1.42.9-19.el7.x86_64 already installed and latest version
Nothing to do

运行以下命令,创建ext4文件系统。其中,/dev/vdb1是数据盘的分区名称,根据实际环境替换。

mkfs -t ext4 /dev/vdb1

运行结果如下所示

[root@host-10-10-10-14 /]# mkfs -t ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
13107200 inodes, 52428544 blocks
2621427 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2199912448
1600 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, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done     

4.配置/etc/fstab文件并挂载分区

设置开机自动挂载云盘,需要在/etc/fstab中写入新分区信息,启动开机自动挂载分区。
运行以下命令,备份/etc/fstab文件。

cp /etc/fstab /etc/fstab.bak

在/etc/fstab里写入新分区信息,运行以下命令修改/etc/fstab文件。
其中:
/dev/vdb1:数据盘的分区名称,需要根据实际环境进行替换。
/opt:分区的挂载点,需要根据实际环境进行替换。可以将分区挂载至环境已有目录下,也可以先通过mkdir命令创建新的挂载目录,然后再挂载。
ext4:分区的文件系统类型,需要根据已创建的文件系统类型修改。

echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /opt ext4 defaults 0 0 >> /etc/fstab

运行以下命令,查看/etc/fstab中的新分区信息。

cat /etc/fstab

运行结果如下所示

[root@host-10-10-10-14 /]# cp /etc/fstab /etc/fstab.bak
[root@host-10-10-10-14 /]# echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /opt ext4 defaults 0 0 >> /etc/fstab
[root@host-10-10-10-14 /]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Mon Aug 17 15:16:23 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=f1640169-9ac8-4729-ac36-2ebea6b99120 /                       ext4    defaults        1 1
UUID=434e44fe-a678-4aa1-9f46-fef4bbaf2bc6 /opt ext4 defaults 0 0

运行以下命令,挂载/etc/fstab配置的文件系统。如果无报错输出,表示您的/etc/fstab配置无误。

mount -a

运行以下命令,检查挂载结果是否符合您的预期。

df -h

运行结果如下所示,如果出现新建文件系统/dev/vdb1的信息,表示文件系统挂载成功

[root@host-10-10-10-14 /]# mount -a
[root@host-10-10-10-14 /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  9.3M  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1        40G  2.1G   36G   6% /
tmpfs           798M     0  798M   0% /run/user/0
/dev/vdb1       197G   61M  187G   1% /opt

5.结束语

本篇只记录自己的学习过程,更好的提升自己

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容