2.6 扩展磁盘分区和文件系统(Linux)
2.6.3 扩展数据盘的分区和文件系统(Linux)
操作场景
通过云服务管理控制台扩容成功后,仅扩大了云硬盘的存储容量,因此需要参考本章 节操作扩展分区和文件系统。
对于Linux操作系统而言,需要将扩容部分的容量划分至已有分区内,或者为扩容部分 的云硬盘分配新的分区。
本文以“CentOS 7.4 64位”操作系统为例,提供MBR分区和GPT分区的操作指导。不 同操作系统的操作可能不同,本文仅供参考,具体操作步骤和差异请参考对应操作系 统的产品文档。
● 扩大已有MBR分区或GPT分区
● 新增MBR分区
● Linux操作系统内核版本不低于3.6.0。
您可以使用uname -a命令查看Linux内核版本。内核版本低于3.6.0时,请参考扩 展磁盘分区和文件系统(Linux系统内核低于3.6.0)。
● 扩容前的云硬盘已分区。
步骤1 执行以下命令,安装growpart扩容工具。
yum install cloud-utils-growpart 说明
可以用growpart命令检查当前系统是否已安装growpart扩容工具,若回显为工具使用介绍,则 表示已安装,无需重复安装。
步骤2 执行以下命令,安装gdisk软件包。
yum install gdisk 系统提示
Is this ok [y/d/N]:
输入“y”,按“Enter”,完成安装。
步骤3 执行以下命令,查看磁盘的分区信息。
fdisk -l
回显类似如下信息:
[root@ecs-test-0001 ~]# fdisk -l
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: 0x000bcb4e
Device Boot Start End Blocks Id System /dev/vda1 * 2048 83886079 41942016 83 Linux Disk /dev/vdb: 161.1 GB, 161061273600 bytes, 314572800 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: 0x38717fc1
Device Boot Start End Blocks Id System /dev/vdb1 2048 209715199 104856576 83 Linux
步骤4 执行以下命令,查看数据盘分区“/dev/vdb1”的容量。
df -TH
回显类似如下信息:
tmpfs tmpfs 520M 0 520M 0% /dev/shm
步骤5 执行以下命令,指定数据盘待扩容的分区,通过growpart进行扩容。
growpart 数据盘 分区编号 命令示例:
growpart /dev/vdb 1 回显类似如下信息:
[root@ecs-test-0001 ~]# growpart /dev/vdb 1
CHANGED: partition=1 start=2048 old: size=209713152 end=209715200 new:
size=314570719,end=314572767 说明
若回显为
no tools available to resize disk with 'gpt' FAILED: failed to get a resizer for id '' 请参考步骤2安装gdisk软件包。
步骤6 根据磁盘的文件系统,选择不同方法扩展磁盘分区文件系统的大小。
● 若磁盘文件系统为ext*,请执行以下命令。
resize2fs 磁盘分区 命令示例:
resize2fs /dev/vdb1 回显类似如下信息:
[root@ecs-test-0001 ~]# resize2fs /dev/vdb1 resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vdb1 is mounted on /mnt/sdc; on-line resizing required old_desc_blocks = 13, new_desc_blocks = 19
The filesystem on /dev/vdb1 is now 39321339 blocks long.
● 若磁盘文件系统为xfs,请执行以下命令。
sudo xfs_growfs 磁盘分区 命令示例:
sudo xfs_growfs /dev/vdb1 回显类似如下信息:
[root@ecs-test-0001 ~]# sudo xfs_growfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=6553472 blks data blocks changed from 26213888 to 39321339
步骤7 执行以下命令,查看扩容后数据盘分区“/dev/vdb1”的容量。
df -TH
回显类似如下信息:
[root@ecs-test-0001 ~]# df -TH
fdisk -l
回显类似如下信息:
[root@ecs-test-0001 ~]# fdisk -l
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: 0x000bcb4e
Device Boot Start End Blocks Id System /dev/vda1 * 2048 83886079 41942016 83 Linux Disk /dev/vdb: 161.1 GB, 161061273600 bytes, 314572800 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: 0x38717fc1
Device Boot Start End Blocks Id System /dev/vdb1 2048 209715199 104856576 83 Linux
步骤2 执行以下命令,进入fdisk分区工具。
fdisk 磁盘 命令示例:
fdisk /dev/vdb 回显类似如下信息:
[root@ecs-test-0001 ~]# 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.
步骤3 输入“n”,按“Enter”,开始新建分区。
回显类似如下信息:
Command (m for help): n Partition type:
p primary (1 primary, 0 extended, 3 free) e extended
Select (default p):
表示磁盘有两种分区类型:
步骤4 以创建一个主分区为例,输入“p”,按“Enter”。
回显类似如下信息:
Select (default p): p
Partition number (2-4, default 2):
“Partition number”表示主分区编号,可以选择2-4,由于1已被使用,此处从2开 始。
步骤5 以分区编号选择“2”为例,输入分区编号“2”,按“Enter”。
回显类似如下信息:
Partition number (2-4, default 2): 2
First sector (209715200-314572799, default 209715200):
“First sector”表示起始磁柱值,可以选择209715200-314572799,默认为 209715200。
步骤6 输入新分区的起始磁柱值,以使用默认起始磁柱值为例,按“Enter”。
系统会自动提示分区可用空间的起始磁柱值和截止磁柱值,可以在该区间内自定义,
或者使用默认值。起始磁柱值必须小于分区的截止磁柱值。
回显类似如下信息:
First sector (209715200-314572799, default 209715200):
Using default value 209715200
Last sector, +sectors or +size{K,M,G} (209715200-314572799, default 314572799):
“Last sector”表示截止磁柱值,可以选择209715200-314572799,默认为 314572799。
步骤7 输入新分区的截止磁柱值,以使用默认截止磁柱值为例,按“Enter”。
系统会自动提示分区可用空间的起始磁柱值和截止磁柱值,可以在该区间内自定义,
或者使用默认值。起始磁柱值必须小于分区的截止磁柱值。
回显类似如下信息:
Last sector, +sectors or +size{K,M,G} (209715200-314572799, default 314572799):
Using default value 314572799
Partition 2 of type Linux and of size 50 GiB is set Command (m for help):
步骤8 输入“p”,按“Enter”,查看新建分区。
回显类似如下信息:
Command (m for help): p
Disk /dev/vdb: 161.1 GB, 161061273600 bytes, 314572800 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: 0x38717fc1
Device Boot Start End Blocks Id System /dev/vdb1 2048 209715199 104856576 83 Linux /dev/vdb2 209715200 314572799 52428800 83 Linux Command (m for help):
步骤9 输入“w”,按“Enter”,将分区结果写入分区表中。
回显类似如下信息:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
说明
mkfs -t ext4 /dev/vdb2 回显类似如下信息:
[root@ecs-test-0001 ~]# mkfs -t ext4 /dev/vdb2 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 3276800 inodes, 13107200 blocks
655360 blocks (5.00%) reserved for the super user First data block=0
Maximum filesystem blocks=2162163712 400 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
Allocating group tables: done
● xfs文件系统命令示例:
mkfs -t xfs /dev/vdb2 回显类似如下信息:
[root@ecs-test-0001 ~]# mkfs -t xfs /dev/vdb2
meta-data=/dev/vdb2 isize=512 agcount=4, agsize=3276800 blks
mkdir /mnt/test
步骤13 执行以下命令,挂载新建分区。
mount 磁盘分区挂载目录
以挂载新建分区“/dev/vdb2”至“/mn/test”为例:
mount /dev/vdb2 /mnt/test 说明
[root@ecs-test-0001 ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
新增 GPT 分区
[root@ecs-test-0001 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 40G 0 disk
└─vda1 253:1 0 40G 0 part / vdb 253:16 0 150G 0 disk
└─vdb1 253:17 0 100G 0 part /mnt/sdc
步骤2 执行以下命令,进入parted分区工具。
parted 磁盘 命令示例:
parted /dev/vdb 回显类似如下信息:
[root@ecs-test-0001 ~]# parted /dev/vdb GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
步骤3 输入“unit s”,按“Enter”,设置磁盘的计量单位为磁柱。
步骤4 输入“p”,按“Enter”,查看当前磁盘分区情况。
回显类似如下信息:
(parted) unit s (parted) p
Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the
disk is smaller. Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? Fix
Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 104857600
blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: Virtio Block Device (virtblk) Disk /dev/vdb: 314572800s
Sector size (logical/physical): 512B/512B Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags 1 2048s 209713151s 209711104s ext4 test (parted)
记录已有分区的截止磁柱值(End),本示例中已有分区“/dev/vdb1”的截止磁柱值 为209713151s。
● 若系统出现以下Error,请输入“Fix”。
GPT分区表信息存储在磁盘开头,为了减少分区表损坏的风险,同时在磁盘末尾 会备份一份。当磁盘容量扩大后,末尾位置也会随之变化,因此需要根据系统提 示输入“Fix”,将分区表信息的备份文件挪到新的磁盘末尾位置。
● 若系统出现以下Warning,请输入“Fix”。
Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 104857600
blocks) or continue with the current setting?
Fix/Ignore? Fix
根据系统提示输入“Fix”,系统会自动将磁盘扩容部分的容量设置为GPT。
步骤5 以为新增容量分配一整个分区为例,执行以下命令,按“Enter”。
mkpart 磁盘分区名称 起始磁柱值截止磁柱值 命令示例:
mkpart data 209713152s 100%
由于步骤4中,已有分区“dev/vdb1”的截止磁柱值为“209713151s”,因此对于新 增分区“dev/vdb2”,起始磁柱值设置为“209713152s”,截止磁柱值设置为
“100%”。此处仅供参考,您可以根据业务需要自行规划磁盘分区数量及容量。
回显类似如下信息:
(parted) mkpart data 209713152s 100%
(parted)
说明
获取最大截止磁柱值的方法如下:
● 参考步骤2~步骤4,查询磁盘的最大截止磁柱值。
● 可以输入-1s或者100%,即默认为磁盘的最大截止磁柱值。
步骤6 输入“p”,按“Enter”,查看新建分区。
回显类似如下信息:
(parted) p
Model: Virtio Block Device (virtblk) Disk /dev/vdb: 314572800s
Sector size (logical/physical): 512B/512B Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags 1 2048s 209713151s 209711104s ext4 test 2 209713152s 314570751s 104857600s data (parted)
步骤7 输入“q”,按“Enter”,退出parted分区工具。
回显类似如下信息:
(parted) q
Information: You may need to update /etc/fstab.
“/etc/fstab”文件控制磁盘开机自动挂载,请先参考以下步骤为磁盘分区设置文件系 统和挂载目录后,再根据文档指导更新“/etc/fstab”文件。
步骤8 执行以下命令,为新建分区设置文件系统。
mkfs -t 文件系统磁盘分区
● ext*文件系统命令示例:
mkfs -t ext4 /dev/vdb2 回显类似如下信息:
[root@ecs-test-0001 ~]# mkfs -t ext4 /dev/vdb2 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 3276800 inodes, 13107200 blocks
655360 blocks (5.00%) reserved for the super user First data block=0
Maximum filesystem blocks=2162163712 400 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
Allocating group tables: done Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
● xfs文件系统命令示例:
mkfs -t xfs /dev/vdb2 回显类似如下信息:
[root@ecs-test-0001 ~]# mkfs -t xfs /dev/vdb2
meta-data=/dev/vdb2 isize=512 agcount=4, agsize=3276800 blks
mkdir /mnt/test
步骤10 执行以下命令,挂载新建分区。
mount 磁盘分区挂载目录
以挂载新建分区“/dev/vdb2”至“/mn/test”为例:
mount /dev/vdb2 /mnt/test 说明
步骤11 执行以下命令,查看挂载结果。
df -TH
回显类似如下信息:
[root@ecs-test-0001 ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
UUID(universally unique identifier)是Linux系统为磁盘分区提供的唯一的标识字符串。
步骤1 执行如下命令,查询磁盘分区的UUID。
blkid 磁盘分区
以查询磁盘分区“/dev/vdb1”的UUID为例:
blkid /dev/vdb1 回显类似如下信息:
[root@ecs-test-0001 ~]# blkid /dev/vdb1
/dev/vdb1: UUID="0b3040e2-1367-4abb-841d-ddb0b92693df" TYPE="ext4"
表示“/dev/vdb1”的UUID。
步骤2 执行以下命令,使用VI编辑器打开“fstab”文件。
vi /etc/fstab
步骤3 按“i”,进入编辑模式。
步骤4 将光标移至文件末尾,按“Enter”,添加如下内容。
UUID=0b3040e2-1367-4abb-841d-ddb0b92693df /mnt/sdc ext4 defaults 0 2
以内容上仅为示例,具体请以实际情况为准,参数说明如下:
● 第一列为UUID,此处填写步骤1中查询到的磁盘分区的UUID。
● 第二列为磁盘分区的挂载目录,可以通过df -TH命令查询。
● 第三列为磁盘分区的文件系统格式, 可以通过df -TH命令查询。
● 第四列为磁盘分区的挂载选项,此处通常设置为defaults即可。
● 第五列为Linux dump备份选项。
– 0表示不使用Linux dump备份。现在通常不使用dump备份,此处设置为0即 可。
– 1表示使用Linux dump备份。
● 第六列为fsck选项,即开机时是否使用fsck检查磁盘。
– 0表示不检验。
– 挂载点为(/)根目录的分区,此处必须填写1。
根分区设置为1,其他分区只能从2开始,系统会按照数字从小到大依次检查 下去。
步骤5 按“ESC”后,输入“:wq”,按“Enter”。
保存设置并退出编辑器。
步骤6 执行以下步骤,验证自动挂载功能。
1. 执行如下命令,卸载已挂载的分区。
umount 磁盘分区 命令示例:
umount /dev/vdb1
2. 执行如下命令,将“/etc/fstab”文件所有内容重新加载。
mount -a
3. 执行如下命令,查询文件系统挂载信息。
mount | grep 挂载目录 命令示例:
mount | grep 挂载目录 命令示例: