由于扩容磁盘的操作非同小可,一旦哪一步出现问题,就会导致分区损坏,数据丢失等一系列严重的问题,因此建议:在进行虚拟机分区扩容之前,一定要备份重要数据文件,并且先在测试机上验证以下步骤,再应用于您的生产机器。如果虚拟机的磁盘不够用了,建议附加新磁盘进行使用,不到万不得已一般不要扩容磁盘分区。

 

操作系统环境:

[root@hlmc73t ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

 

1.关闭虚拟机

备注:扩容磁盘需要关闭虚拟机,否则无法调整磁盘的大小

 

2.调整磁盘的大小(示例中将原来的30GB调整为了150GB)

 

 

3.等待跟新完成,启动虚拟机,查看到整块磁盘已经变为150G,但/dev/sda2分区还是30G

 

 

4.使用命令:fdisk /dev/sda,重新划分分区/dev/sda2

[root@hlmc73t ~]# fdisk -l /dev/sda

Disk /dev/sda: 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: 0x000c1fec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    62914559    30944256   83  Linux
[root@hlmc73t ~]# clear
[root@hlmc73t ~]# fdisk /dev/sda
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/sda: 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: 0x000c1fec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    62914559    30944256   83  Linux

Command (m for help): d      ##将原来的/dev/sda2分区删除
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): p

Disk /dev/sda: 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: 0x000c1fec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux

Command (m for help): n      ##重新创建分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (1026048-314572799, default 1026048):       ##该值需要与上述标黄的start值相同
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-314572799, default 314572799): 
Using default value 314572799
Partition 2 of type Linux and of size 149.5 GiB is set

Command (m for help): p

Disk /dev/sda: 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: 0x000c1fec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   314572799   156773376   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/sda2已经变为了150G,但使用df –h查看/dev/sda2还是30G

 

6.此时拉伸文件系统的话,输出信息类似如下,拉伸没有生效。需要重启电脑以便重读分区表后,才能成功拉伸文件系统。备注:可以使用“blkid”命令查看分区的文件系统

 

7.重启电脑后,重新使用命令:xfs_growfs,拉伸文件系统,可以看到成功拉伸文件系统

 

 

8.重新查看,发现/dev/sda2成功拉伸为了150G,并且之前的数据文件仍然存在并可正常使用。

 

 

posted on 2018-01-12 00:18  铭的随记  阅读(855)  评论(0编辑  收藏  举报