前提条件
- 华为云ECS云服务器已挂载数据盘,且该盘或扩容部分未做过初始化处理。
一. 划分分区并挂载磁盘
场景示例:云服务器挂载了一块新的数据盘,需要将该数据盘设为主分区,文件系统设为ext4格式,挂载在“/apps/soft”目录下,并设置开机启动自动挂载。
- 执行以下命令,查看新增数据盘。
fdisk -l
回显类似如下信息,表示当前的云服务器有两块磁盘,“/dev/vda”是系统盘,“/dev/vdb”是新增数据盘。
[root@0001 apps]# fdisk -l Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000b623c Device Boot Start End Blocks Id System /dev/vda1 * 2048 2099199 1048576 83 Linux /dev/vda2 2099200 83886079 40893440 83 Linux Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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
- 执行以下命令,进入fdisk模式,开始对新增数据盘执行分区操作。
fdisk 新增数据盘
以新挂载的数据盘“/dev/vdb”为例:
fdisk /dev/vdb
回显类似如下信息:
[root@0001 apps]# fdisk /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 0x99d30fba. Command (m for help):
- 输入“n”,按“Enter”,开始新建分区。
输入“n”表示新增一个分区。
回显类似如下信息,表示磁盘有两种分区类型:
- “p”表示主要分区。
- “e”表示延伸分区。
Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p):
- 以创建一个主要分区为例,输入“p”,按“Enter”,开始创建一个主分区。
回显类似如下信息,“Partition number”表示主分区编号,可以选择1-4。
Select (default p): p Partition number (1-4, default 1):
- 以分区编号选择“1”为例,输入主分区编号“1”,按“Enter”。
回显类似如下信息,“First sector”表示初始磁柱区域,可以选择2048-20971519,默认为2048。
Partition number (1-4, default 1): 1 First sector (2048-20971519, default 2048):
- 以选择默认初始磁柱编号2048为例,按“Enter”。
回显类似如下信息,“Last sector”表示截止磁柱区域,可以选择2048-20971519,默认为20971519。
First sector (2048-2097151999, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999):
- 以选择默认截止磁柱编号20971519为例,按“Enter”。
回显类似如下信息,表示分区完成,即为10GB的数据盘新建了1个分区。
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): Using default value 2097151999 Partition 1 of type Linux and of size 1000 GiB is set Command (m for help):
- 输入“p”,按“Enter”,查看新建分区的详细信息。
回显类似如下信息,表示新建分区“/dev/vdb1”的详细信息。
Command (m for help): p Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x404beae3 Device Boot Start End Blocks Id System /dev/vdb1 2048 2097151999 1048574976 83 Linux Command (m for help):
- 输入“w”,按“Enter”,将分区结果写入分区表中。
回显类似如下信息,表示分区创建完成。
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
- 执行以下命令,将新的分区表同步到数据盘上。
partprobe
- 执行以下命令,将新建分区文件系统设为系统所需格式。
mkfs -t 文件系统格式 /dev/vdb1
以设置文件系统为“ext4”为例:
mkfs -t ext4 /dev/vdb1
回显类似如下信息:
[root@0001 ~]# 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 65536000 inodes, 262143744 blocks 13107187 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2409627648 8000 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, 71663616, 78675968, 102400000, 214990848 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
格式化需要等待一段时间,请观察系统运行状态,不要退出。
- 执行以下命令,新建挂载路径。
mkdir 挂载路径
以新建挂载路径“/apps/soft”为例:
mkdir /apps/soft
- 执行以下命令,将新建分区挂载到 Step 12中新建的路径下。
mount /dev/vdb1 挂载路径
以挂载新建分区至“/apps/soft”为例:
mount /dev/vdb1 /apps/soft
- 执行以下命令,查看挂载结果。
df -TH
回显类似如下信息,表示新建分区“/dev/vdb1”已挂载至“/apps/soft”。
[root@0001 soft]# df -TH Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 34G 0 34G 0% /dev tmpfs tmpfs 34G 0 34G 0% /dev/shm tmpfs tmpfs 34G 19M 34G 1% /run tmpfs tmpfs 34G 0 34G 0% /sys/fs/cgroup /dev/vda2 ext4 42G 5.1G 34G 14% / /dev/vda1 ext4 1.1G 180M 774M 19% /boot tmpfs tmpfs 6.8G 0 6.8G 0% /run/user/0 /dev/vdb1 ext4 1.1T 80M 1.1T 1% /apps/soft
二. 设置开机自动挂载磁盘
如果需要在华为云ECS系统启动时自动挂载磁盘,不能采用在 /etc/fstab直接指定 /dev/vdb1的方法,因为云中设备的顺序编码在关闭或者开启云服务器过程中可能发生改变。推荐使用UUID来配置自动挂载数据盘。

磁盘的UUID(universally unique identifier)是Linux系统为存储设备提供的唯一的标识字符串。
- 执行如下命令,查询磁盘分区的UUID。
blkid 磁盘分区
以查询磁盘分区“/dev/vdb1”的UUID为例:
blkid /dev/vdb1
回显类似如下信息,表示“/dev/xvdb1”的UUID。
[root@0001 soft]# blkid /dev/vdb1 /dev/vdb1: UUID="d751796e-f913-41eb-b8eb-98f7a1524784" TYPE="ext4"
- 执行以下命令,使用VI编辑器打开“fstab”文件。
vim /etc/fstab
- 按“i”,进入编辑模式。
- 将光标移至文件末尾,按“Enter”,添加如下内容。
UUID=d751796e-f913-41eb-b8eb-98f7a1524784 /apps/soft ext4 defaults 0 2
- 按“ESC”后,输入“:wq”,按“Enter”。
保存设置并退出编辑器。
来源:oschina
链接:https://my.oschina.net/zouxiangrd/blog/4512350