18.3.Â Resizing and Growing Disks

Originally contributed by AllanJude.

A disk's capacity can increase without any changes to the
data already present. This happens commonly with virtual
machines, when the virtual disk turns out to be too small and is
enlarged. Sometimes a disk image is written to a
USB memory stick, but does not use the full
capacity. Here we describe how to resize or
grow disk contents to take advantage of
increased capacity.

Determine the device name of the disk to be resized by
inspecting /var/run/dmesg.boot. In this
example, there is only one SATA disk in the
system, so the drive will appear as
ada0.

Note:

If the disk was formatted with the GPT partitioning scheme, it may show
as “corrupted” because the GPT
backup partition table is no longer at the end of the
drive. Fix the backup
partition table with
gpart:

#gpart recover ada0
ada0 recovered

Now the additional space on the disk is available for
use by a new partition, or an existing partition can be
expanded:

Partitions can only be resized into contiguous free space.
Here, the last partition on the disk is the swap partition, but
the second partition is the one that needs to be resized. Swap
partitions only contain temporary data, so it can safely be
unmounted, deleted, and then recreated after resizing other
partitions.

Warning:

There is risk of data loss when modifying the partition
table of a mounted file system. It is best to perform the
following steps on an unmounted file system while running from
a live CD-ROM or USB
device. However, if absolutely necessary, a mounted file
system can be resized after disabling GEOM safety
features:

#sysctl kern.geom.debugflags=16

Resize the partition, leaving room to recreate a swap
partition of the desired size. This only modifies the size of
the partition. The file system in the partition will be
expanded in a separate step.