Increase disk space on debian VM filesystem with command line

If, like me, you’re a bit of a linux n00b and you’ve made the mistake of setting up a development VM-based linux server (in my case Debian) that ended up being a production server but you failed to provision enough disk space (it was only a dev server am I right?!) then you’ll probably have tried increasing the disk space in vSphere or similar and wondered how to make the OS see that extra disk space…

What you need to do is rebuild the partitions and then get the filesystem to fill the space, particularly the rootfs, which is probably where you are see 90%+ when you run:

$ df -h

First thing you need to do is have a read of this excellent guide: Live resizing of an ext4 filesytem on linux

Pay particular attention to the disclaimer about data loss. You MUST create a backup in case you mess this up.

This is actually magic. After adding additional disk via your VM management tool, you’re going to log in to your linux system and using fdisk you’ll delete active partitions, all of them. While you are logged on! Then you’re going to create new partitions and then get use resize2fs to get the OS to recognise the new space.

A colleague and I practiced this on a clone, and this morning I came in early and we did it on the production server.

The only slightly complicated bits were picking the right number of sectors to give to sda1 (we went with 60000000) leaving enough room for sda2 (Extended) and sda5 (Linux swap / Solaris).

The other bit we stumbled on while trying to follow the codesilence recipe was that the code section for resize2fs at the bottom of the post didn’t show the command to edit fstab, and we tried to do that last… that didn’t work 🙂 We reread it and realised the correct order is stated in this bit:

run mkswap, adjust /etc/fstab to the new UUID and turn the swap on

After you run mkswap, it will give you a UUID that you need to copy (or screenshot) which you then enter into /etc/fstab using your preferred editor (that’s nano for me, but you may prefer vim or vi… you sick puppy) THEN you do the swapon bit…

Enjoy your new space!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.