This page looks best with JavaScript enabled

Resize Disk for a Virutual Machine Kvm

 ·  ☕ 2 min read

OMG no space left on device

check the disk

qemu-img info /var/lib/libvirt/images/pm1/pm1.qcow2
image: pm1.qcow2
file format: qcow2
virtual size: 2 GiB (2147483648 bytes)
disk size: 1.44 GiB
cluster_size: 65536
Format specific information:
    compat: 0.10
    compression type: zlib
    refcount bits: 16

virtual size is max size of the disk defined on creation meanwhile disk size is the current disk usage.

Expand disk

add +5G to the existing disk, notice + sign to add (2+5)G.
We can also set size using 5G in total or to reduce size with -1G (2-1)G.

qemu-img info /var/lib/libvirt/images/pm1/pm1.qcow2 +5G
image: pm1.qcow2
file format: qcow2
virtual size: 7 GiB (7516192768 bytes)
disk size: 1.44 GiB
cluster_size: 65536
Format specific information:
    compat: 0.10
    compression type: zlib
    refcount bits: 16

Resize partition

run this command inside the VM to identify the device

fdisk -l
Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 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: 0x4cbfd56f

Device     Boot Start     End Sectors Size Id Type
/dev/vda1  *     2048 4194303 4192256   2G 83 Linux

ok as we have seen we have /dev/vda with 2Gb space and no space on device

du -Th
/dev/vda1      ext4      2.0G  1.9G     0 100% /

remove the partition and recreate

fdisk /dev/vda

# d ( delete partition )
# n ( new primary partition with max size )
# w ( write )

then

resize2fs /dev/vda1

now we have more space left on device

/dev/vda1      ext4      6.9G  2.0G  4.6G  31% /