How to resize and growing disks in FreeBSD

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

I work a lot with virtual nachines. In fact, most of my servers are virtual machines (last time I’ve counted, there where around 100vms) running on top of four physical servers and couple of SAN/NAS. Sometimes you -or the one who asked for the machine- underestimate the hardware resources needed. Or simply after a while you end up with a nice “Filesystem full” error.
In this article I’ll show how to resize a partition and grow the filesystem to get more free space.

Presumption

I’m going to presume that you already have free space on your disk, it can be one of:

  • Your FreeBSD it’s on a physicall disk, but you didn’t assign the whole disk (maybe for dualboot with another OS) at install time.
  • You have decided that didn’t need the swap anymore (i.e. added some more ram memory) and want to assign those Gbs to your filesystem.
  • You can grow the virtual hard drive of your FreeBSD VM. This is my case, right now I’m working with VirtualBox, but I know that at least VMware, Xen, and gnome-boxes (kvm I think) allow to resize disk images. Just make sure the virtual disk isn’t in use before resizing.
  • Any other way you can have spare space on your disk to distribute

Let’s do it

Make sure your disk isn’t in use. Unmount all the partitions if you can, and disable the swap if it resides on the disk you want to modify.

I can’t unmount because I’ve only made one big partition for the whole system, so I’m gonna use the FreeBSD installer CD image to boot the VM and work in live-cd mode:

screenshot of booting the install media in live-cd mode
Login with root user, without a password

Fix the partition table

First list all the partitions on the disk to see the current configuration:

screenshot of my hard drive partitions
Screenshot of my hard drive partitions
# gpart show ada0
=>       40   33554352  ada0  GPT  (17G) [CORRUPT]
         34       1024    1  freebsd-boot  (512K)
       1064   25164800    2  freebsd-ufs  (12G)
   25165864    8388528    3  freebsd-swap  (4.0G)

Notice the [Corrupt] I’ve highlighted in red. The disk image is fine, but using the GPT schema the backup partition table isn’t anymore at the end of the drive. Fix the partition table with gpart recover:

# gpart recover
ada0 recovered
# gpart show ada0
=>       40  356541504  ada0  GPT  (17G) [CORRUPT]
         34       1024    1  freebsd-boot  (512K)
       1064   25164800    2  freebsd-ufs  (12G)
   25165864    8388528    3  freebsd-swap  (4.0G)
   33554392    2097152       - freebsd -  (1.0G)
screenshot of corrected partition configuration with spare space
Now we have enough free space for the purpose of this article

Delete the swap partition and resize the partition

Wait, why? Swap partition only contains temporary data. Also partitions can only be resized into contiguous free space and we don’t have free space after the partition. Remember to leave room to recreate a swap partition later.

# gpart delete -i 3 ada0
ada0p3 deleted
# gpart resize -i 2 -s 14G -a 4k ada0
ada0p2 resized

The flags means: -i index, or the partition number; -s size; and -a 4k controls the alignment.

Note: You will probably see some weird errors, we will back later

Now we can recreate the swap partition in the remaning space, that I’m doing a little smaller.

# gpart add -t freebsd-swap -a 4k ada0
ada0p3 added

List all the partition to see our new configuration with gpart show:

screenshot of the partition configuration after the resize
Our partition table is done.

Grow and check the filesystem

The last remaining tasks are grow the UFS filesystem:

# growfs /dev/da0p2

And finally, check the filesystem with fsck. This is not mandatory, but highly recommended, specially if you got errors like the ones on my screenshot:

# fsck -y /dev/da0p2
screenshot of the fsck output after growing the filesystem

Now you know how to resize a partition and grow a filesystem.

Reboot if you where working with a live-cd or remount the affected partition and continue with more free space.

Final words

You can resize a mounted filesystem if you temporarily disable… but don’t, just don’t. It’s always better to lose a couple of minutes -even hours- rearranging your data than lose your data in a couple of minutes.

If you are using MBR partitions the syntax is slightly different because of the partition and ‘slices’ schema.

On large filesystems fsck could last a really long time. Consider migrate to zfs and adding individual disks to the raid z.

FreeBSD has and excellent handbook, you can learn more about storage management here: https://docs.freebsd.org/en/books/handbook/disks/. The ZFS filesystem have is own chapter here https://docs.freebsd.org/en/books/handbook/zfs/

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Gonzalo Rivero
Gonzalo Rivero
I am Gonzalo, I live in Salta, a city located in the NW of Argentina. I play the guitar and a little harmonica. I also like to bike.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook