Expanding FreeBSD root filesystem (UFS)I use FreeBSD whenever I can as a server machine because I find it very reliable, with a very good amount of non invasive features and whit a very extensive documentation and toll chain.
It happens that sometime you end up your disk space, and usually you have two choices:
- add another disk and mount and link it somewhere within the root filesystem;
- expand the disk size directly, of course only if you are running on a virtual machine environment.
This is a rewriting of my previous post about partitioning resizing, with images and more detailed steps.
This post is focused on the second environment: adding space to a virtual machine root disk.
But before I continue with this post, allow me to make it clear the environment:
- I use UFS as root filesystem, and even if this approach works for ZFS too, I haven’t tried it on such a filesystem;
- I recommend you to make a backup of the filesystem before proceed. In my case, since I’m running a virutal machine configured thru ansible I’m not scared of loosing data, but I recommend you to evaluate and do a backup before proceeding;
- I use GPT partitions;
- my root filesystem is mounted over
ada0, second partition therefore
You can always follow carefully the excellent FreeBSD documentation on resizing disks.
Doing the Disk ResizeThe first step is to provide more space on the root disk, and I will not discuss here how I did because it depends on the actual virtual machine manager you are using.
Therefore, do stop the machine (you are not expecting to apply this on a live system, do you?) and enlarge the filesystem thru your virtual media manager.
Steps to make FreeBSD aware of the new space
Step 1: boot in single user modeFirst of all, boot the machine in single user mode and get to a shell.
Step 2: print the list of partitionsUse
gpart showwith the disk name to get the list of partitions and find out the one you need to resize:
In the above example we need to resize
Please note that between the free space and the
ada0p2there is the swap partition, so we need to erase that first.
Step 3: delete partitions between the free space and your resizing partitionAs already stated, between the free space available on disk and the partition I need to resize there is the swap partition
ada0p3, so I need to erase that first.
In my case don’t need to
swapoffthe swap space, so I can go for the partition destruction.
However, before I’m able to act on the partition table there is the need to recover it. In fact, GPT scheme stores the partition data (backup) to the end of the disk, and since the end has grown,
gpartis not able to find such copy of the data and assumes the partition is corrupted. It does suffice to issue a
gpart recover ada0to make
gparthappy again. After that, it is possible to
gpart delete -i 3 ada0to erase the partition.
Step 4: resize the partition and resume swapNow it is possible to resize the partition and add again a swap partition. The first step is to instrument the kernel to do so, and this requires to shut down extra dangerous partition protection by setting a kernel flag value.
Then you can resize the partition to less than the overall space, in order to let room for the swap partition, and then add the latter to the disk.
# sysctl kern.geom.debugflags=16 # gpart resize -i 2 -s 15G -a 4k ada0 # gpart add -t freebsd-swap -a 4k ada0
resizecommand is the crucial part, and expands the partition up to
15 GB(change with your value) with a data alignment to
Step 4: grow the root filesystemIt is amost done, but there is the need to grow the root filesystem. This can be easily done, on UFS, by means of
growfs(1)displays the resizing from aorund seven gigabytes to fifteen, giving me a chance to check I’m doing the correct resizing.
Step 5: rebootThe easiest part (if your machine starts over again!**.
Just issue a reboot and check the new filesystem once you can login again.
ConclusionsGEOM is too much superior of any other filesystem I’ve ever used, and the above steps emphasize how simple it can be to resize even the root partition!
I remember doing the same resizing on a Linux machine with a volume manager, and it was a lot much more complicated and obscure, at least to me.