dphys-swapfile
Create and manage swap file on Debian systems
TLDR
Disable the swap file
Enable the swap file
Create a new swap file
SYNOPSIS
dphys-swapfile setup|swapon|swapoff|auto
PARAMETERS
setup
Deletes any existing swap file and creates a new one according to CONF_SWAPSIZE in /etc/dphys-swapfile, then activates it.
swapon
Activates the existing swap file. Does not create or resize it.
swapoff
Deactivates the current swap file, removing it from active swap space.
auto
Performs 'setup' if CONF_SWAPSIZE is greater than zero; otherwise, performs 'swapoff'. This is typically used by system init scripts.
DESCRIPTION
dphys-swapfile is a lightweight script designed to simplify the creation and management of a swap file on Linux systems. It's particularly prevalent in embedded environments like the Raspberry Pi, where it provides an easy way to configure swap space without needing a dedicated swap partition. The utility reads its configuration, primarily the desired swap file size and location, from the /etc/dphys-swapfile configuration file.
When invoked, dphys-swapfile automates the process of allocating space (using fallocate or dd), formatting it as swap (mkswap), and activating/deactivating it (swapon/swapoff). It integrates well with system startup scripts, often configured to run automatically during boot to ensure the system has adequate virtual memory.
CAVEATS
The primary configuration for dphys-swapfile resides in /etc/dphys-swapfile. Changing the CONF_SWAPSIZE variable requires running sudo dphys-swapfile setup or rebooting the system for the changes to take effect.
While convenient, using a swap file on flash-based storage (like SD cards in Raspberry Pi) can lead to reduced performance due to slower I/O speeds and increased wear on the flash media, potentially shortening its lifespan.
CONFIGURATION FILE: /ETC/DPHYS-SWAPFILE
This file contains the key variables that control dphys-swapfile behavior:
- CONF_SWAPFILE: The full path to the swap file (e.g., /var/swap).
- CONF_SWAPSIZE: The desired size of the swap file in megabytes (MB). A value of 0 effectively disables swap.
- CONF_MAXSWAP: (Optional) The maximum allowed swap size, in MB, which can prevent accidental creation of excessively large swap files.
TYPICAL USAGE
To change the swap file size, edit /etc/dphys-swapfile and modify the CONF_SWAPSIZE variable (e.g., CONF_SWAPSIZE=2048 for 2GB). After saving, run sudo dphys-swapfile setup to apply the changes immediately, or simply reboot your system.
HISTORY
dphys-swapfile was developed to simplify swap space management on Linux systems, particularly gaining prominence within the Raspberry Pi ecosystem (Raspberry Pi OS, formerly Raspbian). It emerged as a user-friendly abstraction over more complex low-level utilities like fallocate, dd, mkswap, swapon, and swapoff. Its design aims to provide a consistent and easy way to configure and manage swap files, especially beneficial for single-board computers where dedicated swap partitions are less common.