Extending Disk Space of a KVM File Backed Storage (VM Disk Image)

NOTE – this example extends a disk that has a single partition. When extending a disk, you ONLY create space at the end of a partition table. If you have a virtual disk carved up into multiple partitions, you can only extend the last one.
Make a backup copy of the disk first: (use cp when the virtual machine is booted down/offline)
Note: Anything in this
fonts
or
fonts
means that you’ll be typing in the terminal or what you’ll be seeing in the terminal.

Step 1. Grow the on-disk file:

In this example we are growing the disk by 400M; Use G or M to specify the amount.
Remember: 1G=1024M
root@mudhens 10:34:08
qemu-img resize sgw-tumcu-ovpn.img +400M
Image resized. If the disk is using a partition table, edit the table to grow the last partition.
Note: KVM disks used as a boot disk will normally have a partition table. KVM disks used as raw storage space will often NOT have a partition table (it’s not needed). Instead, the raw device will be formatted with an ext4 filesystem OR perhaps an LVM layer will exist on top (and then filesystems on top of the LVM volumes).

Step 2. Locate which server or NFS storage cluster the disk lives on (use EMS):

Use fdisk to open the disk and “p” to print the partition table:
 > /nfssrv8/sgw # fdisk sgw-tumcu-ovpn.img

Command (m for help): p
Disk sgw-tumcu-ovpn.img: 2.4 GiB, 2566914048 bytes, 5013504 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
Disklabel type: dos
Disk identifier: 0x00074758

Device              Boot Start     End Sectors Size Id Type
sgw-tumcu-ovpn.img1 *     2048 4194303 4192256   2G 83 Linux

Step 3. Delete and recreate the partition:

This disk only has 1 partition and it’s using the defaults (first sector is 2048). So, we can delete it and recreate it as a larger partition to use up all of the newly added disk space. We use the a option to activate the disk as bootable (set the bootable flag) and the w key to write our changes.
IF THE PARTITION HAS AN EXT4 SIGNATURE DO NOT DELETE IT. The disk will be ruined.
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-5013503, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-5013503, default 5013503):

Created a new partition 1 of type 'Linux' and of size 2.4 GiB.
Partition #1 contains a ext4 signature.
PAY ATTENTION TO THIS MESSAGE:
Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.


Command (m for help): w
The partition table has been altered.
Syncing disks.

Step 4. Boot the VM back up and use the resize utility to grow out the FS into the newly allocate space. The partition was extended in the step above so the VM will be able to resize the filesystem.

IF YOU GET AN ERROR message about the filesystem not being able to be extended its likely the step above where the partition was deleted and re-created was carried out WHILE the disk was attached to a loop device (so the FS could be mounted). YOU MUST DETACH the loop device before extending the disk with the qemu command.
We are now back inside the VM guest. We can grow the fs easily:
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vda1 is now 626432 (4k) blocks long.
Use df to see your newly enlarged disk space.
Note: you can also mount the device as a loopback device and run resize2fs from the host. Its generally safer to resize the VM guest from the VM guest, using the same version of ext4 tools that the guest is going to be using.