Bromania Wiki

Scribblings of a Brit in Romania

User Tools

Site Tools


tech:unix:freebsd:swappartition

Using a USB Flash Drive as Swap

Environment

  • FreeBSD 13
  • Single Board Computer

Overview

Single board computers (and older computers) often have small amounts of RAM by today's standards. Four of the SBCs I own only have 1GB, and two of them only have 512MB. If you intend to perform any function that requires a reasonable amount of RAM, for example building ports from source, you should have swap space configured to avoid running out of RAM.

Although this guide can be applied to many configurations, it is written from the point-of-view of running FreeBSD on an SBC that boots from an SD card (or perhaps an SSD). Flash memory has a limited amount of write cycles, so you will want to avoid unneccessary writes to the SD card. I have managed to wear out a 128GB SD card, which was very expensive at the time.

One way of avoiding this is to buy a cheap USB flash drive, and configure the entire drive as flash. This way, if it dies, you can simply buy another one, configure it using this guide, and you won't have to worry about rebuilding your system or restoring it from backup.

Guide

Obtain Device Name

First we need to get the device name of the flash drive we are going to configure. It is important to correctly identify the device as subsequent commands will erase everything on the specified device.

Insert the USB stick into one of the USB ports. Once you have chosen the USB port I recommend that you do not move the drive to another port as, depending on which other drives are connected at boot, the device name may change, causing an unbootable system.

Run the following command:

# dmesg

At the bottom of the output, you should see something similar to that below. In the example, you can see that da0 is the device name of my flash drive.

ugen0.4: <SanDisk Ultra Fit> at usbus0
umass0 on uhub0
umass0: <SanDisk Ultra Fit, class 0/0, rev 3.00/1.00, addr 3> on usbus0
umass0:  SCSI over Bulk-Only; quirks = 0x8100
umass0:0:0: Attached to scbus0
da0 at umass-sim0 bus 0 scbus0 target 0 lun 0
da0: <SanDisk Ultra Fit 1.00> Removable Direct Access SPC-4 SCSI device
da0: Serial Number 4C530001110413122090
da0: 400.000MB/s transfers
da0: 14663MB (30031250 512 byte sectors)
da0: quirks=0x2<NO_6_BYTE>

Now you have identified the device name, make sure you substitute it for all instances of da0 below.

Remove Existing Partition Table

Regardless of whether the flash drive has been used before, or if it is brand new and has a FAT32 partition, we will remove the existing partition table and start again in order to ensure compatibility with FreeBSD.

# gpart destroy -F da0

Create New Partition Table and Swap Partition

Now we create a modern GUID partition table (GPT):

# gpart create -s GPT da0

And one big swap partition using all available disk space:

# gpart add -t freebsd-swap da0

Enable the Swap Partition and Make it Permanent

Add the new swap partition:

# swapon /dev/da0p1

And make sure that is enabled:

swapctl -l

This should show the new swap device in its list:

Device:       1024-blocks     Used:
/dev/da0p1     15015588         0

If you see any other swap partitions that you no longer wish to use, you can use the following command to disable them, substituting mmcsd0s3 for the name of the swap partition that is no longer required:

swapoff /dev/mmcsd0s3

Assuming all is good, you should now add the following line to your /etc/fstab file to make sure the new swap partition is enabled at every boot:

/dev/da0p1             none            swap    sw                      0 0

Make sure you remove, or comment out with #, any other swap partitions in /etc/fstab that you no longer wish to use.

tech/unix/freebsd/swappartition.txt · Last modified: 2023/01/05 10:47 by puisor