If you’re like me, sometimes you create a virtual machine without planning ahead with regard to space needs. If you aren’t like me, and all 4 partitions aren’t used up on your particular VM, you could easily add an additional partition without having to enlarge an existing one. However, if you are like me, your virtual hard-drive already has 4 partitions and it’s impossible to add another one. In this case, the easiest solution is to resize the drive and reformat. But what about all the data! If reformatting isn’t feasible for you, do not fret! It’s still possible to resize your virtual Linux drive without losing your data!
- Resize the virtual drive in VM management software; once this is done, you’ll notice that it doesn’t yet translate into a larger linux drive — this is because although the new drive has grown, the filesystem hasn’t grown to match it.
- fdisk -l (see what your drives are)
- fdisk /dev/sda (or whatever the virtual disk’s ID is — main disk, not an individual partition)
- p (print the partition table and take note of the 4th partition’s starting sector)
- d then 4 (delete the 4th partition)
- n then 4 (should default to 4 — create a new partition over the old one)
- p (make it a primary partition and make sure the first sector matches; the last should default to the new last sector of the “drive“)
- w (write the new partition table)
- resize2fs /dev/sda4 (re-size the file system to match the partition table for the 4th partition)
And poof! You’re all done! You should now have re-sized the underlying file system to match your new virtual drive’s size.