How to resize VM disk size
This note provides steps to resize the KVM based VM images. In this situation, VM disk is raw format and filesystem is ext4.
Expand disk size
In order to expand disk size, VM must be shutdown
Increase raw disk size
qemu-img resize vm-1.img +10G
virt-rescue -a vm-1.img
At rescue environment
><rescue> fdisk /dev/sdx
Use normal fdisk command to delete partition and re-create the partition with bigger size. For this case, I would like to expand /dev/sda3 to have additional 10G
><rescue> fdisk /dev/sda d (select 3 to delete parition sda3) n (create new partition) select as primary select default start and end><rescue> e2fsck -f /dev/sda3 ><rescue> resize2fs /dev/sda3 ><rescue> sync ><rescue> exit
w (save the changes and exit from fdisk menu)
I selected default start and end of disk because I want to use the rest of disk space for /dev/sda3
Start the VM and check the new disk size
Shrink disk size
Reduce disk size process needs to make sure that you have enough free space before shrink. Take a backup of disk image.
In this article, I tested with VM image which was build by CentOS 6.9.
List the disk partitons
virt-list-partitions -lh vm-1.img
/dev/sda1 ext4 512.0M /dev/sda2 swap 2.0G /dev/sda3 ext4 37.5G
Into rescue mode
Check parition that going to shrink
><rescue> e2fsck -f /dev/sda3
e2fsck 1.41.12 (17-May-2010) /dev/sda3: recovering journal Clearing orphaned inode 1445694 (uid=0, gid=0, mode=0100666, size=0) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sda3: ***** FILE SYSTEM WAS MODIFIED ***** /dev/sda3: 85525/2457600 files (0.1% non-contiguous), 1052831/9830144 blocks
><rescue> resize2fs -p /dev/sda3 27G
resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/sda3 to 7077888 (4k) blocks. Begin pass 2 (max = 60469) Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Begin pass 3 (max = 300) Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Begin pass 4 (max = 13267) Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The filesystem on /dev/sda3 is now 7077888 blocks long.
Exit from rescue mode
Create smaller raw disk. For this case, I am trying to shrink 10G from 40G disk image. I will create 30G raw disk.
truncate -s 30G vm-1-new.img
Copy content from original disk to smaller disk
virt-resize --shrink /dev/sda3 vm-1.img vm-1-new.img
Examining vm-1.img ... ********** Summary of changes: /dev/sda1: This partition will be left alone. /dev/sda2: This partition will be left alone. /dev/sda3: This partition will be resized from 37.5G to 27.5G. The filesystem ext4 on /dev/sda3 will be expanded using the 'resize2fs' method. ********** Setting up initial partition table on cloud-1-new.img ... Copying /dev/sda1 ... Copying /dev/sda2 ... 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 Copying /dev/sda3 ... 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 Expanding /dev/sda3 using the 'resize2fs' method ... Resize operation completed with no errors. Before deleting the old disk, carefully check that the resized disk boots and works correctly.
Start the VM with new disk. You should able detect smaller disk.
Note: Backup your original disk image before manipulate