1. Home
  2. Linux

How to use Swap space on a USB drive in Rasbian Linux

The Raspberry Pi has a tiny amount of SWAP space available for users to utilize when they run out of physical RAM in Raspbian. The exact amount of SWAP on the Pi is about 100 MB, which is not enough, and when it’s used up, it can cause massive lag.

A great way to improve the overall performance of a Raspberry Pi device is to increase the size of the SWAP space. However, understand that just increasing the size is not enough. The reason? Raspberry Pi runs off of an SD card, and by expanding the SWAP on the SD card, the SD card will die faster. Instead, you should move it to an external USB drive. In this guide, we will show you how to do just that!

Formatting the USB flash drive

Before removing the SWAP space from the SD card and placing it on to the USB flash drive, you must format the USB to the correct format (EXT4.) To start the process, plug in any USB flash drive into the USB port and open up a terminal.

Note: if your Raspberry Pi device is a server, log in over SSH rather than accessing a terminal on it directly.

Inside of the terminal, run the lsblk command in the terminal. The output of the lsblk command will show you all storage devices connected to the Pi. Locate the USB drive’s label to confirm that the Pi is reading the device. The label should be /dev/sda.

lsblk

After finding the device label for the USB flash drive, open it up in the Cfdisk partitioning tool with the command below.

Note: the USB flash drive only has the /dev/sda label if it is the only USB storage device connected. If you have other devices connected to the Raspberry Pi in addition to the USB flash drive, it may not show up as /dev/sda, but as /dev/sdb, /dev/sdc, or even /dev/sdd. If you need help finding the label of the drive, follow this guide.

sudo cfdisk /dev/sda

Inside of the Cfdisk partition editor, you must delete all partitions on the flash drive. To do this, follow the step-by-step instructions below.

Step 1: Highlight the primary partition on the USB device with the Up/Down Arrows on the keyboard. Then, use the Left/Right Arrows to select “Delete” and delete the partition.

Step 2: Select “Free space” in Cfkdisk using the Up/Down Arrows on the keyboard. Then, select “New” with the Left/Right Arrows on the keyboard and press the Enter button to create a new partition.

Step 3: Upon selecting “New,” Cfdisk will prompt to specify the partition size. By default, Cfdisk will fill in the maximum size of the disk. For example, if your USB flash drive is 14.6 GB, it will suggest creating a partition that is 14.6 GB.

Press Enter to use the suggested partition size. Otherwise, specify a different size in the prompt and press Enter.

Step 4: After pressing Enter to create a new partition in Cfdisk, you will need to specify the partition type. There are two types: primary and extended. Select the “primary” option, and press Enter.

Step 5: Select “Type” with the Left/Right Arrows on the keyboard, and press Enter. Then, select “83 Linux” to set the new partition type as a Linux file system.

Step 6: Select “Write” with the Left/Right Arrows on the keyboard, and press Enter to write the changes in Cfdisk to the USB flash drive.

Step 7: Select “Quit” with the Left/Right Arrows to close Cfdisk.

Step 8: Now that Cfdisk is closed, run the mkfs.ext4 command below to write the Ext4 file system to the USB device.

sudo mkfs.ext4 -F /dev/sda1

Mounting the USB drive

The USB drive must be mounted to be used as a SWAP space. To mount your USB in Raspbian, follow the step-by-step instructions below.

Step 1: Using the terminal, create a new folder in the /mnt directory with the name “usb-flash.”

sudo mkdir -p /mnt/usb-flash/

Step 2: You must add the USB device to the /etc/fstab file so that it can be loaded at boot. Using the commands below, add the code to /etc/fstab.

sudo -s

echo ' ' >> /etc/fstab

echo '# USB Flash for SWAP space' >> /etc/fstab

echo '/dev/sda1 /mnt/usb-flash/ ext4 noatime,defaults 0 2' >> /etc/fstab

Step 3: Run the mount command to load the USB flash drive into the /mnt/usb-flash/ directory.

sudo mount /mnt/usb-flash/

Use SWAP space on a USB drive

Setting up SWAP space on Raspberry Pi is easy thanks to the straightforward configuration the developers offer. Follow the step-by-step instructions below to get it done on your device.

Step 1: Shut off the SWAP on your Rasbian SD card with the stop command in the terminal.

sudo /etc/init.d/dphys-swapfile stop

Step 2: Open up Raspbian’s SWAP configuration file in the Nano text editor.

sudo nano -w /etc/dphys-swapfile

Step 3: Locate CONF_SWAPSIZE=100 and change the 100 to 1024.

Step 4: Locate #CONF_SWAPFILE=, and remove the # symbol. It should look like the example below.

CONF_SWAPFILE=

Step 5: Locate CONF_SWAPFILE= and erase the contents after the = sign. Then, add in /mnt/usb-flash/swap.file. It should look exactly like the example below.

CONF_SWAPFILE=/mnt/usb-flash/swap.file

Step 6: Start Raspbian’s SWAP back up with the following command. If the command is successful, you will be running SWAP from your USB flash drive!

sudo /etc/init.d/dphys-swapfile start

1 Comment

  1. This was very helpful and easy to follow. There were 3 steps that may catch noobs like me 🙂
    1. the usb drive is already mounted, so it must be umount before you can do anything.
    2. exit sudo -s “shell”
    3. save and exit nano

    I’ve a follow up question: if the usb is ~14gig why are you setting the swap to 1024? is this a speed issue or a max? i see the file has a #’d max setting of 2024 what’s the point of this?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.