Increase a virtual machine disk size it’s so easy, on almost all hypervisor: just click and increase the space on the virtual disk. But it’s just one part of the game, the second part it does the same inside the virtual machine, and maybe it’s not so easy or fast increase the partition and the filesystem inside the related guest OS.
For Windows OSes, starting with 2008 (Windows Server 2008 and Vista) it’s quite easy and totally manageable from the GUI (with the Disk Manager MMC). For Linux, it could be easy with LVM, but a little tricky without it.
If you have a Linux installed without LVM (you can easily check it using df command and looking at the device names) then you have to manually modify your partition table.
First to all, check the geometry of your disk:
- If you have MBR partition table using fdisk command (or parted)
- If you have GPT partition table you may need other specific tools, depending on your distribution (but in most cases parted or also fdisk may still work).
Then you need to umount the filesystem inside of the partition that you need to extend. This is needed to avoid inconsistency, but also I/O errors during the operation. For the root filesystem could be more difficult umount the filesystem, in this case, you can use a live distribution or simply mount the filesystem in read-only… you may have some I/O errors, but you preserve the integrity.
At this point, you can resize the partition by defining the new end block. One simple way just deletes the old partition and create a new one (but a better way could modify the partition end). For example, using fdisk command:
- Run fdisk on the right disk (X is the correct letter of your disk): fdisk /dev/sdX
- Check the partition table by pressing p (to view partition details)
- Delete the partition by pressing d (to delete the partition)
- Select the right partition that you want to delete, in most cases will be the only on the disk, so press 1 (to select the partition)
- Create a new partition by pressing n (to create a new partition) and select the partition type whether Primary (by pressing p) and the right number
- Get the first block from the partition details in point 2
- Press enter to accept the default as the last block.
- Check the partition table by pressing p (to list the partition and confirm)
- Save your new partition table
Finally, you need to re-read the partition table using this command:
[root@linux ~]# partprobe
Your partition resize is now completed. Note that the partition that you want to resize must be the last one on disk so it can grow contiguously into the free space.
Now it’s the turn of the filesystem inside the partition: you also need to resize it on the extended partition.
First could be useful to check the filesystem. Then you can resize it and this can vary on the filesystem type. For ext3 and ext4 you can use this command:
[root@linux ~]# resize2fs /dev/sdX
If your filesystem is very old (around 2007) and created with an older build of mkfs.ext2, it may not be configured for online resize (missing -O resize_inode flag). In this case, the online resize will not work.
More recent versions of mkfs.ext2 automatically reserve space for block group descriptor tables to enable online resize of the filesystem.