由于扩容磁盘的操作非同小可,一旦哪一步出现问题,就会导致分区损坏,数据丢失等一系列严重的问题,因此建议:在进行虚拟机分区扩容之前,一定要备份重要数据文件,并且先在测试机上验证以下步骤,再应用于您的生产机器。如果虚拟机的磁盘不够用了,建议附加新磁盘进行使用,不到万不得已一般不要扩容磁盘分区。
操作系统环境:
[root@hlmcent69n1 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
1.关闭虚拟机
备注:扩容磁盘需要关闭虚拟机,否则无法调整磁盘的大小
2.调整磁盘的大小(示例中将原来的30GB调整为了100GB)
3.等待跟新完成,启动虚拟机,查看到整块磁盘已经变为100G,但/dev/sda1分区还是30G
4.使用命令:fdisk /dev/sda,重新划分分区/dev/sda1
[root@hlmcent69n1 ~]# fdisk /dev/sdaWARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u').Command (m for help): p ##查看目前的分区信息Disk /dev/sda: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0005498f Device Boot Start End Blocks Id System/dev/sda1 * 1 3917 31456256 83 LinuxCommand (m for help): u ##默认以柱面的形式显示,运行该参数后会以扇区的形式显示Changing display/entry units to sectorsCommand (m for help): pDisk /dev/sda: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0005498f Device Boot Start End Blocks Id System/dev/sda1 * 2048 62914559 31456256 83 Linux##记录该标黄的start值Command (m for help): d ##将原来的/dev/sda1分区删除Selected partition 1Command (m for help): pDisk /dev/sda: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0005498f Device Boot Start End Blocks Id SystemCommand (m for help): n ##重新创建分区Command action e extended p primary partition (1-4)pPartition number (1-4): 1First sector (63-209715199, default 63): 2048 ##该值需要与上述标黄的start值相同Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): Using default value 209715199Command (m for help): pDisk /dev/sda: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0005498f Device Boot Start End Blocks Id System/dev/sda1 2048 209715199 104856576 83 LinuxCommand (m for help): a ##激活分区Partition number (1-4): 1Command (m for help): p ##再次查看分区,确认已激活,激活后Boot对应的值为*Disk /dev/sda: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylinders, total 209715200 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0005498f Device Boot Start End Blocks Id System/dev/sda1 * 2048 209715199 104856576 83 LinuxCommand (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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.
5.使用fdisk –l查看/dev/sda1已经变为了100G,但使用df –h查看/dev/sda1还是30G
6.此时拉伸文件系统的话,输出信息类似如下,拉伸没有生效。需要重启电脑以便重读分区表后,才能成功拉伸文件系统。备注:可以使用“blkid”命令查看分区的文件系统
7.重启电脑后,重新使用命令:resize2fs,拉伸Ext4的文件系统,可以看到成功拉伸文件系统
8.重新查看,发现/dev/sda1成功拉伸为了100G,并且之前的数据文件仍然存在并可正常使用。