Expanding a disk in FreeBSD

The process of expanding the file system in FreeBSD is examined here using the example of the popular OPNsense virtual router distribution. Before undertaking to expand the file system it is necessary to increase the size of the volume using the functions of the cloud Dashboard.

Once the size of the volume (block structure) has been increased, further operations can be tackled.

In this example the file system ‘/dev/ufs/OPNsense’ is expanded from 1.9GB to the maximum available.

Note

When changing the partition table of the file system currently set, there is a risk of data loss. Before making any changes, be sure to make a backup copy of the instance.

  1. Go to the dashboard of the router via the cloud web console or via the ssh connection.
*** OPNsense.localdomain: OPNsense 17.7.11 (amd64/OpenSSL) ***

 WAN (vtnet0)    -> v4/DHCP4: 172.16.0.15/20

  0) Logout                              7) Ping host
  1) Assign interfaces                   8) Shell
  2) Set interface IP address            9) pfTop
  3) Reset the root password            10) Firewall log
  4) Reset to factory defaults          11) Reload all services
  5) Power off system                   12) Upgrade from console
  6) Reboot system                      13) Restore a backup

Enter an option:

  1. Select point ‘8) Shell’ (Enter an option: 8). If necessary, perform authorisation first.
  2. Check the version and name of the Linux distribution:
root@OPNsense:~ # uname -mrs
FreeBSD 11.0-RELEASE-p12 amd64
  1. Use the df utility to output a list of file systems, their size as well as used and available space:
root@OPNsense:~ # df -h
Filesystem           Size    Used   Avail Capacity  Mounted on
/dev/ufs/OPNsense    1.9G    1.2G    605M    67%    /
devfs                1.0K    1.0K      0B   100%    /dev
devfs                1.0K    1.0K      0B   100%    /var/dhcpd/dev

In this case the file system ‘/dev/ufs/OPNsense’ should be expanded.

  1. The file system currently set can be changed after disabling the GEOM security functions:
root@OPNsense:~ # sysctl kern.geom.debugflags=16
kern.geom.debugflags: 16 -> 16
  1. Output a partition table for disks:
root@OPNsense:~ # gpart show
=>      63  41942977  vtbd0  MBR  (20G)
        63   4194225      1  freebsd  (2.0G)
   4194288  37748752         - free -  (18G)

=>      0  4194225  vtbd0s1  BSD  (2.0G)
        0       16           - free -  (8.0K)
       16  4194209        1  !0  (2.0G)

As can be seen from the output of the ‘gpart show’ command, the disk has a size of 20 GB, of which there is 18GB available disk space, which enables us to expand the slice, partition and file system.

  1. Extend the first slice of the disk, ‘vtbd0’, from 2 GB to 20 GB (the maximum).

The size of the partition before change is specified with the key ‘-i’, and the desired new size with the key ‘-s’. The adjustment of the partition is monitored with the key ‘-a’. If the size of the partition is not stated (key ‘-s’), all remaining available space is used.

Note

A successfully executed ‘gpart resize -i 1 -a 4k vtbd0’ command changes only the size of the partition. The file system in this partition is extended and described in the next steps of this section.

root@OPNsense:~ # gpart resize -i 1 -a 4k vtbd0
vtbd0s1 resized
  1. Carry out a check after extending the partition:
root@OPNsense:~ # gpart show
=>      63  41942977  vtbd0  MBR  (20G)
        63  41942977      1  freebsd  (20G)

=>       0  41942977  vtbd0s1  BSD  (20G)
         0        16           - free -  (8.0K)
        16   4194209        1  !0  (2.0G)
   4194225  37748752           - free -  (18G)
  1. Extend the first partition of the disk from 2 GB to 20 GB (the maximum).
root@OPNsense:~ # gpart resize -i 1 -a 4k vtbd0s1
vtbd0s1a resized
  1. Carry out a check after extending the partition:
root@OPNsense:~ # gpart show
=>      63  41942977  vtbd0  MBR  (20G)
        63  41942977      1  freebsd  (20G)

=>       0  41942977  vtbd0s1  BSD  (20G)
         0        16           - free -  (8.0K)
        16  41942961        1  !0  (20G)
  1. Once all operations have been executed, check that the size of the file system has not changed:
root@OPNsense:~ # df -h
Filesystem           Size    Used   Avail Capacity  Mounted on
/dev/ufs/OPNsense    1.9G    1.2G    605M    67%    /
devfs                1.0K    1.0K      0B   100%    /dev
devfs                1.0K    1.0K      0B   100%    /var/dhcpd/dev
root@OPNsense:~ #

As can be seen from the results of the output of the ‘df -h’ command, the size of the file system has not changed.

  1. Expand the file system of the partition:
root@OPNsense:~ # growfs -y /dev/ufs/OPNsense
super-block backups (for fsck_ffs -b #) at:
4194496, 5243072, 6291648, 7340224, 8388800, 9437376, 10485952, 11534528,
12583104, 13631680, 14680256, 15728832, 16777408, 17825984, 18874560,
19923136, 20971712,     22020288, 23068864, 24117440, 25166016, 26214592,
27263168, 28311744, 29360320, 30408896, 31457472, 32506048, 33554624,
34603200, 35651776, 36700352, 37748928,     38797504, 39846080, 40894656
  1. Carry out a check after extending the file system:
root@OPNsense:~ # df -h
Filesystem           Size    Used   Avail Capacity  Mounted on
/dev/ufs/OPNsense     19G    1.2G     17G     7%    /
devfs                1.0K    1.0K      0B   100%    /dev
devfs                1.0K    1.0K      0B   100%    /var/dhcpd/dev

The file system has been successfully extended to the required volume. To learn more about changing the disk volume in FreeBSD, see the vendor documentation Resizing and Growing Disks.