Resize Ubuntu disk in VirtualBox

So after developing for a couple of years on my VirtualBox Ubuntu server, the 32GB I had allocated wasn’t enough anymore. So I wanted to add some additional space, without loosing my files of course. I have a Ubuntu Server 18.04 with a LVM partition. So I would see something with /dev/mapper/blabla in the df -h.

As with a lot of things that I wrote on this blog, I couldn’t find an easy step-by-step thingy to follow for my specific case. Most of the examples used gparted, but I have Ubuntu Server, so no GUI at all. Others used the VBoxManager command to resize the disk.

In hindsight it was pretty easy, so, let’s go.

Step 1: resize the VirtualBox image

You can use the VirtualBox UI to do this pretty easily. Make sure your virtual machine is shut down. Then go to File, Virtual Media Manager. Select the disk image that is bound to your virtualmachine, and use the slider to add space to your disk image.

Step 2: resize the partition

I couldn’t find a decent way to resize the partition using the console, that I would dare to execute without being afraid to loose all my files. Eventually I found the link to the gparted live cd.

Download it, and mount it as a live CD to your virtual machine.

Now just boot your virtual machine, and boot with the default settings. You will end up with a bare GUI, start gparted. In my case, my swap partition was at the beginning of the partition table. That was easy; because I could just resize the existing partitions. If your swap partition is in the way, just remember the size, delete it, and create it afterwards (as suggested in this youtube video).

As in this video, just select the partition, right click, and select Resize. I did this for both the extended file system on /dev/sda2 as the LVM partition.

After you selected Resize, just move the slider to the right. So first for /dev/sda2, then for /dev/sda5.

Now remove the live CD, and boot your virtual machine again.

Step 3: expand LVM

Now you need to expand the LVM partition. That’s just two commands.

and after that:

Of course you need to change the /dev/mapper/blabla thingy to whatever your LVM partition is.

That’s it! df -h now shows the new size.

Show date in other timezone with correct locale

We want to show the date / time in the timezone of the user, but also in the right locale. Unfortunately, strftotime doesn’t seem available for the DateTime object. So I ended up with something like this… I assume you did setlocale somewhere else in your project to make sure PHP is in the right locale.