How to Expand the Hard Drive Capacity of a FreeBSD Cloud Server?

22-01-2024 01:57:57

FreeBSD utilizes the UFS (Unix File System), also known as freebsd-ufs, as its operating system's file system. When we upgrade the cloud server's hard drive to a larger capacity, the system does not automatically expand the disk size. The following steps are required to complete the expansion. This guide is applicable to all versions of the FreeBSD operating system.

Expanding the System Disk

1.Viewing Hard Drive Partitions

First, remotely log into the Linux cloud server and use the df command and the gpart show command to view the hard drive partition status. As shown below, you can see that this server has only one 25GB system disk.

# df -h  
Filesystem         Size    Used   Avail Capacity  Mounted on
/dev/ufs/rootfs     24G    924M     21G     4%    /
devfs              1.0K    1.0K      0B   100%    /dev

# gpart show
=>    40  52428720  vtbd0  GPT  (25G)
      40      1024      1  freebsd-boot  (512K)
      1064  52427696      2  freebsd-ufs  (25G)

After changing the hard disk capacity to 40GB, we use the above commands again to view the new hard disk partition status. As shown below, the size of the vtbd0 partition has been changed to 40GB and is marked as CORRUPT.

# df -h
Filesystem         Size    Used   Avail Capacity  Mounted on
/dev/ufs/rootfs     24G    924M     21G     4%    /
devfs              1.0K    1.0K      0B   100%    /dev

# gpart show
=>    40  52428720  vtbd0  GPT  (40G) [CORRUPT]
      40      1024      1  freebsd-boot  (512K)
      1064  52427696      2  freebsd-ufs  (25G)

2.Recovering a Damaged Partition

Use the gpart recover command to recover the damaged vtbd0 partition.

# gpart recover vtbd0
vtbd0 recovered

# gpart show
=>    40  83886000  vtbd0  GPT  (40G)
      40      1024      1  freebsd-boot  (512K)
      1064  52427696      2  freebsd-ufs  (25G)
  52428760  31457280         - free -  (15G)

3.Expanding the FreeBSD Partition

According to the FreeBSD official manual, there is a risk of data loss when modifying the partition table of a filesystem in a mounted state. Therefore, if the partition to be expanded contains important data, it is recommended to make a remote backup before proceeding with the expansion. Once ready, use the gpart resize command to expand the partition.

# gpart resize -i 2 vtbd0
vtbd0p2 resized

# growfs /dev/ufs/rootfs

4.Confirming Successful Expansion

Use the gpart show and df commands to view the hard disk partition status and confirm whether the expansion was successful. You can see that the system disk has been successfully expanded to 40GB.

# gpart show
=>    40  83886000  vtbd0  GPT  (40G)
      40      1024      1  freebsd-boot  (512K)
      1064  83884976      2  freebsd-ufs  (40G)

# df -h
Filesystem         Size    Used   Avail Capacity  Mounted on
/dev/ufs/rootfs     39G    924M     35G     3%    /
devfs              1.0K    1.0K      0B   100%    /dev

Creating a Data Disk

After creating a new data disk, it needs to be partitioned, initialized, and mounted to the operating system. In the example below, we will create a 50GB data disk as a single partition and name it vtbd1.

1.Creating a Partition

We use the gpart create command to create it, adopting the GPT partition scheme.

# gpart create -s GPT vtbd1
vtbd1 created

# gpart add -t freebsd-ufs -l vultr\_block\_storage vtbd1
vtbd1p1 added

2.Initializing the Partition

Initialize the partition using the UFS filesystem.

# newfs -U vtbd1p1
/dev/vtbd1p1: 51200.0MB (104857528 sectors) block size 32768, fragment size 4096
    using 82 cylinder groups of 626.09MB, 20035 blks, 80256 inodes.
    with soft updates
super-block backups (for fsck_ffs -b #) at:
 192, 1282432, 2564672, 3846912, 5129152, 6411392, 7693632, 8975872, 10258112, 11540352, 12822592, 14104832, 15387072, 16669312, 17951552, 19233792, 20516032, 21798272, 23080512, 24362752, 25644992, 26927232, 28209472, 29491712,
 30773952, 32056192, 33338432, 34620672, 35902912, 37185152, 38467392, 39749632, 41031872, 42314112, 43596352, 44878592, 46160832, 47443072, 48725312, 50007552, 51289792, 52572032, 53854272, 55136512, 56418752, 57700992, 58983232,
 60265472, 61547712, 62829952, 64112192, 65394432, 66676672, 67958912, 69241152, 70523392, 71805632, 73087872, 74370112, 75652352, 76934592, 78216832, 79499072, 80781312, 82063552, 83345792, 84628032, 85910272, 87192512, 88474752,
 89756992, 91039232, 92321472, 93603712, 94885952, 96168192, 97450432, 98732672, 100014912, 101297152, 102579392, 103861632
Now you're ready to mount the new device for use!

3.Mounting the Partition

In this example, we create a mount directory at /mnt/blockstorage, but this can be adjusted as needed.

# mkdir /mnt/blockstorage
# echo /dev/vtbd1p1 /mnt/blockstorage ufs rw,noatime 0 2 >> /etc/fstab 
# mount /mnt/blockstorage

4.Confirming Successful Creation

Use the df command to view the hard disk partition status and confirm whether the creation was successful.

# df -h /mnt/blockstorage/
Filesystem      Size    Used   Avail Capacity  Mounted on
/dev/vtbd1p1     48G    8.0K     45G     0%    /mnt/blockstorage