How to Resize Partitions on FreeBSD

28th May 2017 • Marco Cetica

In this guide I will demonstrate how to resize the root partition of a FreeBSD system using gpart(8) from a live system. This procedure is particular useful when you need to increase the disk size without having to reinstall the whole operating system. Although this process is safe and well tested, I strongly recommend performing it first on a testing environment and creating multiple backup copies of the production system before proceeding.

Since the resize procedure differs when using ZFS, that scenario will be addressed on the final section of this guide.

Resize UFS partitions

Let's start by figuring out which disk we want to resize. We can use gpart show to retrieve the list of available drives. In my setup, since I only have one disk, I get the following output:

root@bsdvm4:~ gpart show => 40 52428720 vtbd0 GPT (25G) 40 1024 1 freebsd-boot (512K) 1064 48234496 2 freebsd-ufs (23G) 48235560 4193200 3 freebsd-swap (2.0G)

Because partitions can only be expanded into contiguous free space, resizing the second partition (i.e., the root partition) requires first deleting the third one. After removing it, we will then proceed by expanding the second partition and then recreating the swap:

root@bsdvm4:~ swapoff /dev/vtbd0p3 # Disable swap partition root@bsdvm4:~ gpart delete -i 3 vtbd0 # Delete swap partition

We now can resize the root partition and recreate the swap area:

root@bsdvm4:~ gpart resize -i 2 -s 43G -a 4k vtbd0 # Resize /dev/vtbd0p2 root@bsdvm4:~ gpart add -t freebsd-swap -a 4k vtbd0 # Add /dev/vtbd0p3

We can now check whether the operation was successful or not by issuing the following command:

root@bsdvm4:~ gpart show => 40 94371760 vtbd0 GPT (45G) 40 1024 1 freebsd-boot (512K) 1064 90177536 2 freebsd-ufs (43G) 90178600 4193200 3 freebsd-swap (2.0G)

Since the partition correctly indicates the expected size (43GiB), we can complete this procedure by resizing the file system:

root@bsdvm4:~ growfs /dev/vtbd0p2 Device is mounted read-write; resizing will result in temporary write suspension for /. It is strongly recommended to make a backup before growing the file system. OK to grow filesystem on /dev/vtbd0p2, mounted on /, from 23GB to 43GB? [yes/no] yes super-block backups (for fsck_ffs -b #) at: 48657216, 49937664, 51218112, 52498560, 53779008, 55059456, 56339904, 57620352, 58900800, 60181248, 61461696, 62742144, 64022592, 65303040, 66583488, 67863936, 69144384, 70424832, 71705280, 72985728, 74266176, 75546624, 76827072, 78107520, 79387968, 80668416, 81948864, 83229312, 84509760, 85790208, 87070656, 88351104, 89631552

Finally, let's activate back the swap partition:

root@bsdvm4:~ swapon /dev/vtbd0p3

Resize ZFS partitions

As mentioned before, the final section of this guide will cover the resizing of ZFS partitions. While the procedure is largely similar to the steps outlined before, certain aspects specific to ZFS pools warrant a separate treatment.

Just like before, let's get started by listing the available disks:

root@bsdvm4:~ gpart show => 40 52428720 vtbd0 GPT (25G) 40 1024 1 freebsd-boot (512K) 1064 4194304 2 freebsd-swap (2.0G) 4195368 48233392 3 freebsd-zfs (23G)

We want to resize the last partition; that is, the root one. In this case, the disk is a virtual one created using QEMU. Therefore, I will use qemu-img(1) to extend it:

$ qemu-img resize bsd.qcow2 +20G

After that, power on the machine and run gpart show to acknowledge that the disk has been successfully expanded:

root@bsdvm4:~ gpart show => 40 52428720 vtbd0 GPT (45G) [CORRUPT] 40 1024 1 freebsd-boot (512K) 1064 4194304 2 freebsd-swap (2.0G) 4195368 48233392 3 freebsd-zfs (23G)

As you can see, the disk has been flagged with a "corrupt" status. This is normal when modifying the size of GPT-partitioned disk, as the backup of the partition scheme is no longer located at the end of the disk, causing the system to be unable to locate it.

In order to fix this issue, we can run the following command:

root@bsdvm4:~ gpart recover vtbd0 vtbd0 recovered => 40 52428720 vtbd0 GPT (45G) [CORRUPT] 40 1024 1 freebsd-boot (512K) 1064 4194304 2 freebsd-swap (2.0G) 4195368 48233392 3 freebsd-zfs (23G) 52428760 41943040 - free - (20G)

Let's now extend the last partition:

root@bsdvm4:~ gpart resize -i 3 -s 44031M vtbd0 vtbd0p3 resized

Since no error were reported, the partition is successfully resized. The last thing we need to do is to check whether the ZFS pool has been informed about this change:

root@bsdvm4:~ zpool list NAME SIZE ALLOC FREE CKPOINT EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT zroot 22.5G 939M 21.6G - - 0% 4% 1.00x ONLINE -

As you can notice, the size has not been automatically updated; therefore, we need to this manually using the following command:

root@bsdvm4:~ zpool online -e zroot vtbd0p3

Let's check again:

root@bsdvm4:~ zpool list NAME SIZE ALLOC FREE CKPOINT EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT zroot 42.5G 940M 41.6G - - 0% 2% 1.00x ONLINE -