Recently, I had to add some space to my test FreeBSD box. I’ve used this tutorial from the BSD handbook to do it.
First, we have to identify the name of the disk. In my case it was da0. I have two FreeBSD VMs at home, one running under VMWare Workstation and the other under Hyper-V. In both cases, the disk name was da0. Use dmesg to check the name of your disk.
dmesg
Once you’ve identified the disk, check the partitions with the gpart command.
NOTE: You can expand only adjacent partitions.
gpart show da0
As you can see from the screenshot above, my partition that I want to extend is 7.6GB and the available space for extension is 991K. Technically, I can’t extend my partition because the swap partition (410M) is in between.
But, we can easily destroy and recreate this partition. Just make sure you are not doing this on a heavily used server.
First, let’s see how to destroy the partition. Use swapinfo to see where the swap partition is mounted.
swapinfo
In my case, it’s /dev/da0p3. Let’s turn it off.
swapoff /dev/da0p3
If you do gpart show da0, you’ll notice that the ID of the swap partition is number 3. So, we have to destroy partition number 3.
gpart delete -i 3 da0
If you get this error that table ‘da0’ is corrupt, fix it with gpart recover da0.
gpart recover da0
Then delete the swap again with gpart delete -i 3 da0.
If you show the disk one more time, you’ll see that the swap is gone and that the disk partition (7.6G) is now adjacent with the free partition (12G).
The 12G partition didn’t show up before because we got that “corrupt” error before. Now, we have to “grow” the original partition.
gpart resize -i 2 -a 4k -s 19G da0
-i 2 means the ID number 2, -s is the total size (19G = 12GB that I’ve added + about 7GB that I had originally).
We also have to plan for the swap partition. Always start with the smaller size than the total sum and then you can grow it if needed.
You can also specify the size in megabytes.
Let’s recreate the swap partition and mount it back.
gpart add -t freebsd-swap -a 4k da0 swapon /dev/da0p3 gpart show da0
If you check your partition now, you’ll see that the size is still the same.
df -k
This means that you have to grow the file system now.
growfs /dev/da0p2