随着托管磁盘的上线,虚拟机支持的单块磁盘容量从1TB到达了4TB,客户对单块磁盘容量的需求量也会变的很大。
操作之前需要重点查看:
由于扩容磁盘的操作非同小可,一旦哪一步出现问题,就会导致分区损坏,数据丢失等一系列严重的问题,因此建议:在进行虚拟机分区扩容之前,一定要备份重要数据文件,并且先在测试机上验证以下步骤,再应用于您的生产机器。如果虚拟机的磁盘不够用了,建议附加新磁盘进行使用,不到万不得已一般不要扩容磁盘分区。
操作环境:
[[email protected] sdc1]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
1.关闭虚拟机
备注:扩容磁盘需要关闭虚拟机,否则无法调整磁盘的大小
2.调整磁盘的大小(示例中将原来的1023G调整为了2047G)
3.启动虚拟机,查看到整块磁盘已经变为2047G,但/dev/sdc1分区还是1023G
4.使用命令:fdisk /dev/sdc,重新划分分区/dev/sdc1
[[email protected] sdc1]# fdisk /dev/sdc
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.
Command (m for help): p ##查看目前的分区信息
Disk /dev/sdc: 2197.9 GB, 2197949513728 bytes, 4292870144 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: 0x1b9a454e
Device Boot Start End Blocks Id System
/dev/sdc1 2048 2145386495 1072692224 83 Linux
Command (m for help): d ##将原来的/dev/sdc1分区删除
Selected partition 1
Partition 1 is deleted
Command (m for help): n ##重新创建分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-4292870143, default 2048): 2048 ##该值需要与上述标黄的start值相同
Last sector, +sectors or +size{K,M,G} (2048-4292870143, default 4292870143):
Using default value 4292870143
Partition 1 of type Linux and of size 2 TiB is set
Command (m for help): p
Disk /dev/sdc: 2197.9 GB, 2197949513728 bytes, 4292870144 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: 0x1b9a454e
Device Boot Start End Blocks Id System
/dev/sdc1 2048 4292870143 2146434048 83 Linux
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.
5.使用fdisk –l查看/dev/sdc1已经变为了2047G,但使用df –h查看/dev/sdc1还是1023G
6.此时拉伸文件系统的话,会提示如下。需要重启电脑以便重读分区表后,才能成功拉伸文件系统
7.重启电脑后,重新使用命令:resize2fs拉伸文件系统,可以看到可以成功拉伸文件系统
8.重新挂载使用,已发现/dev/sdc1成功拉伸为了2047G(2T),并且之前的数据文件仍然正常存在。
[[email protected] ~]# mount /dev/sdc1 /mnt/sdc1/