Expanding Virtual Disk and the Linux Filesystem Inside the Guest
Expandation of a virtual File System
Due to the aspect that the filesystem of a virtual machine grows with his lifetime (e.g. new dependencies/packages, bigger logs, more user data, …) it can happen that the virtual disk runs out of space and therefore must be expanded. Please be aware, that it can always happen that your guest filesystem get destroyed during the operation. To be prepared for this case, as always, DO BACKUPS!
Expanding under libvirt (virsh)
Under linux based systems, kvm virtualisation using libvirt[1] is a frequently used solution. To edit the disk space here you have to find the affected virtual image on the host system first. If you know the position, power off the virtual machine otherwise it can happen that the file system inside the image can be damaged!
# Expand the disk image.qcow2 by 1337 Gigabyte
$ qemu-img resize /path/to/image.qcow2 +1337G
If you have done that you can boot your guest system again and follow the steps inside the vm to edit the partition table.
Expanding under Proxmox
Proxmox is a Hypervisor using kvm virtualisation based on Debian, a GNU/Linux distribution. It has the big advantage of a modern web interface which allows the system administrator to manage the host system, hardware properties and more using a cool user interface.
# Expand the disk inside the webinterface
Select Your VM where you want to expand the disk -> Switch to the Hardware Tab -> Select the virtual disk which should be incremented -> click on resize disk and specify the amount of space on which should the disk be expanded
Part 2: Linux Guest expand filesystem
To expand the filesystem, we have to alter the partition table first. This can be done using fdisk on the command line of your guest operating system. > All of the following commands need root privileges either run the commands directly inside a root shell or append “sudo” to every command if available
Recreating of the virtual partition (part 1)
# Check which disk you have to expand (use df -h to find out which is the right one) in my case its sda
root@hostname:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
# Print the current partition layout
Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 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: 0xfffffffffff
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 1953791 1951744 953M 82 Linux swap / Solaris
/dev/sda2 * 1953792 83884031 81930240 39.1G 83 Linux
# Quit fdisk if the swap file is behind your partition which should be altered otherwise skip that and the following step
Command (m for help): q
Deactivating and temporary deleting the swap partition
# To delete the swap partition it has to be deactivated first this can be done with the following command
$ swapoff --all
# Going back to fdisk
root@hostname:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
# Print the current partition layout
Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 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: 0xfffffffffff
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 1953791 1951744 953G 82 Linux
/dev/sda2 1953792 83884031 81930240 3G 83 Linux swap / Solaris
# Delete the swap partition
Command (m for help): d
Partition number (1,2, default 2):
Partition 2 has been deleted.
# Write the changed to disk
Command (m for help): w
Expanding of the virtual partition (part 2)
# Open fdisk again if you are not already in
# Going back to fdisk
root@hostname:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 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: 0xfffffffff
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 1953791 1951744 953M 82 Linux swap / Solaris
/dev/sda2 * 1953792 83884031 81930240 39.1G 83 Linux
# Delete the linux partition that it can be recreated with the new size in the next step
Command (m for help): d
Partition number (1,2, default 2):
Partition 2 has been deleted.
Command (m for help):
# Recreate the partition we have deleted earlier (check if the start position is the same! That's important otherwise the filesystem can not be found afterwards)
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2):
First sector (1953792-125829119, default 1953792):
# If you have removed your swap partition during the earlier step please mention to let some free space for the swap partition
Last sector, +sectors or +size{K,M,G,T,P} (1953792-125829119, default 125829119):
Created a new partition 2 of type 'Linux' and of size 59.1 GiB.
Partition #2 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: N
# Create a new swap partition if you have deleted it, the swap partition can be directly behind the new position with all of the unpartitioned sectors left inside your virtual image.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
Congratulations you have successfully expanded your partition, if you had the same error message as me above you have to update your kernel partition table otherwise you can skip the next section.
Update Kernel Partition Table
# To update the kernel partition table you can use partx which is installed on the most systems by default:
# The parameter -u means that the kernel cached partition layout should be updated [2]
$ partx -u /dev/sda
Expanding the filesystem
# Now where the partition has new space the filesystem must be informed about it.
# On my case my partition was on sda2
$ resize2fs /dev/sda2
Recreating swap partition
# To create and turn on the swap again initialise the partition with the swap layout and turn the swap on again
$ mkswap /dev/sdaX
$ swapon --all
[1] https://libvirt.org/index.html [2] https://man7.org/linux/man-pages/man8/partx.8.html