How to Move Raspbian swapfile to USB

usb drives
USB drives are much more reliable than SDcards!

Running your raspberry pi from SDcard works, but has its foibles. For one, consistent reading and writing to and from an SDcard is a sure recipe for failure. Sure, the little memory cards are fast and cheap, but they don’t take well to this kind of abuse. Especially the unrelenting i/o of a swapfile. Better to move your swapfile from the SDcard to a USB device. From Ubuntu 18.04 onwards, a swapfile rather than a dedicated swap partition is used. The swap file is named “swapfile”. First, find the swap file and delete it.

root@pi4:~# swapon -s
Filename Type Size Used Priority
/var/swap file 102396 0 -2
root@pi4:~# cd /var
root@pi4:/var# ls -la
total 102448
drwxr-xr-x 11 root root 4096 Feb 13 11:31 .
drwxr-xr-x 21 root root 4096 Feb 13 11:31 ..
drwxr-xr-x 2 root root 4096 Apr 16 07:56 backups
drwxr-xr-x 15 root root 4096 Apr 15 15:04 cache
drwxr-xr-x 47 root root 4096 Apr 15 15:04 lib
drwxrwsr-x 2 root staff 4096 Feb 8 21:47 local
lrwxrwxrwx 1 root root 9 Feb 13 10:51 lock -> /run/lock
drwxr-xr-x 9 root root 4096 Apr 16 00:01 log
drwxrwsr-x 2 root mail 4096 Feb 13 10:51 mail
drwxr-xr-x 2 root root 4096 Feb 13 10:51 opt
lrwxrwxrwx 1 root root 4 Feb 13 10:51 run -> /run
drwxr-xr-x 5 root root 4096 Feb 13 11:09 spool
-rw------- 1 root root 104857600 Feb 13 11:31 swap   <--- Thar she blows!!!
drwxrwxrwt 4 root root 4096 Apr 16 13:14 tmp
root@pi4:/var# swapoff /var/swap
root@pi4:/var# rm /var/swap

Find your USB drive. Mine is /dev/sda1 that I’ve got mounted as ‘/’. You can move your entire filesystem to USB, by following these directions.

root@pi4:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 1 7.5G 0 disk
└─sda1 8:1 1 7.5G 0 part /
mmcblk0 179:0 0 29.8G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /boot
└─mmcblk0p2 179:2 0 29.6G 0 part
root@pi4:~#

Create a new swap file of the desired size. I generally use a 4GB swap. Determine the size of your swap file. If you want to make a 4 GB swap file, you will need to write 4 * 1024 blocks of 10242 bytes (= 1 MiB). That will make your count equal to 4 * 1024 = 4096. Create the file of this size with the command:

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

Assign it read/write permissions for root only (not strictly needed, but it tightens security)

sudo chmod 600 /swapfile

Format the file as swap:

sudo mkswap /swapfile

The file will be activated on the next reboot. If you want to activate it for the current session:

sudo swapon /swapfile

You can check the swap that is available with the command swapon -s (no root permissions needed). And there you have it! You’re swapping to and from USB thumb-drive. Congratulations for not having to worry about blowing out your SDcard!

Running on USB
Running on USB!

One thought on “How to Move Raspbian swapfile to USB”

  1. The title says Raspbian but then you talk about swap file in Ubuntu not Raspbian.

    Better decide what you are writing about

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.