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
.
List the partitions on the disk to see the current configuration:
#
gpart show
=> 34 83886013 ada0 GPT (48G) [CORRUPT] 34 128 1 freebsd-boot (64k) 162 79691648 2 freebsd-ufs (38G) 79691810 4194236 3 freebsd-swap (2G) 83886046 1 - free - (512B)ada0
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 recoveredada0
Now the additional space on the disk is available for use by a new partition, or an existing partition can be expanded:
#
gpart show
=> 34 102399933 ada0 GPT (48G) 34 128 1 freebsd-boot (64k) 162 79691648 2 freebsd-ufs (38G) 79691810 4194236 3 freebsd-swap (2G) 83886046 18513921 - free - (8.8G)ada0
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 recreate the third partition after resizing the second partition.
Disable the swap partition:
#
swapoff
/dev/ada0p3
Delete the third partition, specified by the
-i
flag, from the disk
ada0
.
#
gpart delete -i
ada0p3 deleted3
ada0
#
gpart show
=> 34 102399933 ada0 GPT (48G) 34 128 1 freebsd-boot (64k) 162 79691648 2 freebsd-ufs (38G) 79691810 22708157 - free - (10G)ada0
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. The partition to resize is
specified with -i
, and the new desired size
with -s
. Optionally, alignment of the
partition is controlled with -a
. This only
modifies the size of the partition. The file system in the
partition will be expanded in a separate step.
#
gpart resize -i
ada0p2 resized2
-s47G
-a 4kada0
#
gpart show
=> 34 102399933 ada0 GPT (48G) 34 128 1 freebsd-boot (64k) 162 98566144 2 freebsd-ufs (47G) 98566306 3833661 - free - (1.8G)ada0
Recreate the swap partition and activate it. If no size
is specified with -s
, all remaining space is
used:
#
gpart add -t freebsd-swap -a 4k
ada0p3 addedada0
#
gpart show
=> 34 102399933 ada0 GPT (48G) 34 128 1 freebsd-boot (64k) 162 98566144 2 freebsd-ufs (47G) 98566306 3833661 3 freebsd-swap (1.8G)ada0
#
swapon
/dev/ada0p3
Grow the UFS file system to use the new capacity of the resized partition:
#
growfs
Device is mounted read-write; resizing will result in temporary write suspension for /. It's strongly recommended to make a backup before growing the file system. OK to grow file system on /dev/ada0p2, mounted on /, from 38GB to 47GB? [Yes/No]/dev/ada0p2
Yes
super-block backups (for fsck -b #) at: 80781312, 82063552, 83345792, 84628032, 85910272, 87192512, 88474752, 89756992, 91039232, 92321472, 93603712, 94885952, 96168192, 97450432
If the file system is ZFS, the resize is
triggered by running the online
subcommand with
-e
:
#
zpool online -e
zroot
/dev/ada0p2
Both the partition and the file system on it have now been resized to use the newly-available disk space.
All FreeBSD documents are available for download at https://download.freebsd.org/ftp/doc/
Questions that are not answered by the
documentation may be
sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.