How to resize a VM and its partitions?

· Read in about 3 min · (468 words) ·

In this post I am going to list the steps to resize a Virtual Machine image created using virt-install

There are three steps:

  • Resize the VM image
  • Resize the LVM volume inside the VM ( both physical volume and logical volume )
  • Resize the filesystem on the LVM volume

First locate where the image for your VM is stored.

# virsh dumpxml vm2 | xpath /domain/devices/disk/source
Found 1 nodes:
-- NODE --
<source file="/export/vmimgs/vm2.img" />

Mine is stored at /export/vmimgs/vm2.img

It is 5GB VM which I want to resize to 10GB. And then, I basically want more space for root partition ( mount point / ).

Step 1: Resize the VM image

Shutdown the VM and take a backup;

# virsh shutdown vm2
# cd /export/vmimgs
# cp vm2.img vm2.img.backup

First lets find the disk devices within our VM:

# virt-filesystems --long -h -a  /export/vmimgs/vm2.img
Name                   Type        VFS   Label  Size
/dev/sda1              filesystem  ext4  -      500M
/dev/VolGroup/lv_root  filesystem  ext4  -      1.5G

# virt-filesystems --long --parts --blkdevs -h -a  /export/vmimgs/vm2.img
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   500M  /dev/sda
/dev/sda2  partition  8e   1.5G  /dev/sda
/dev/sda   device     -    5G    -

Now we resize our VM image ( +5GB ):

# cd /export/vmimgs
# truncate -r vm2.img vm2.img.new
# truncate -s +5G vm2.img.new 
# virt-resize --expand /dev/sda2 vm2.img vm2.img.new
Examining vm2.img ...
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 4.5G to 9.5G.  The LVM 
    PV on /dev/sda2 will be expanded using the 'pvresize' method.

**********
Setting up initial partition table on vm2.img.new ...
Copying /dev/sda1 ...
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
Copying /dev/sda2 ...
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ --:--
Expanding /dev/sda2 using the 'pvresize' method ...

Resize operation completed with no errors.  Before deleting the old 
disk, carefully check that the resized disk boots and works correctly.

Did you notice that /dev/sda2 was resized using ‘pvresize’. Thats because its a LVM physical volume.

Now we move the new image vm2.img.new to the actuall image, taking backup for safety:

# chmod +x vm2.img.new
# mv vm2.img vm2.img.original
# mv vm2.img.new vm2.img

Step 2: Resize the LVM volume inside the VM - logical volume

Login into guest and resize the logical volume:

[root@localhost ~]# lvresize -L +5G /dev/VolGroup/lv_root

Step 3: Resize the filesystem on the LVM volume.

Resize the ext4 partition:

[root@localhost ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/lv_root to 1708032 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 1708032 blocks long.

[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  6.5G  1.4G  4.8G  23% /
tmpfs                        1004M     0 1004M   0% /dev/shm
/dev/vda1                     485M   31M  429M   7% /boot

Thats it. I hope that helps.

References