swapon, swapoff

enable/disable devices and files for paging and swapping


/sbin/swapon [-h -V]
/sbin/swapon -a [-v] [-e]
/sbin/swapon [-v] [-p priority] specialfile
/sbin/swapon [-s]
/sbin/swapoff [-h -V]
/sbin/swapoff -a
/sbin/swapoff specialfile

specialfile may be of the form -L label or -U uuid

Calls to swapon normally occur in the system multi-user initialization file /etc/rc making all swap devices available, so that the paging and swapping activity is interleaved across several devices and files.

-a All devices marked as swap in /etc/fstab are made available, except for those with the noauto option.
-e skip devices that do not exist when -a
-L label Use the partition with label. (access to /proc/partitions is needed.)
-U uuid Use the partition with uuid. (access to /proc/partitions is needed.)
-p priority between 0 and 32767 with higher numbers indicating higher priority. Swap pages are allocated from areas in priority order, highest priority first. higher priority area is exhausted before using a lower-priority area.
If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.. Add pri=value to the option field of a /etc/fstab for use with swapon -a.
-s Display swap usage summary . Equivalent to cat /proc/swaps.
-v verbose.
-V Display version
-h help
usage: swapon [-hV]
       swapon -a [-e] [-v]
       swapon [-v] [-p priority] special|LABEL=volume_name ...
       swapon [-s]

swapoff disables swapping on the specified devices and files. WIth -a , swapping is disabled on all swap devices and files (found in /proc/swaps or /etc/fstab).

Do not use swapon on a file with holes.
Swap over NFS may not work.

SEE swapon(2), swapoff(2), fstab, init(8), mkswap(8), rc(8), mount(8)

/dev/hd?? standard paging devices
/dev/sd?? standard (SCSI) paging devices
/etc/fstab ascii filesystem description table


major minor  #blocks  name 
   8     0  104846336 sda
   8     1     104391 sda1
   8     2  103683510 sda2
   8     3    1052257 sda3
   8    16 2823802880 sdb
   8    17 2823802846 sdb1
   7     0     512000 loop0


set up, mount/unmount, and delete an swap file

dphys-swapfile setup|install|swapon|swapoff|uninstall

Computes the size and resizes an existing swap file if necessary,
mounts, unmounts and deletes it if no nolonger needed.
re-generate an swap file as $CONF_SWAPFACTOR times RAM size.
Called at boot time, file is resized if RAM is resized
Direct swapon/off from /etc/fstab is not possible, as that is done in the same script that mounts /var (a usual location for swap file) we need to do our setup between those actions. So we pass up on /etc/fstab, and do our own swapon/off here.
uninstall remove unwanted swap file


/etc/dphys-swapfile is a sh script fragment of assignments, which is sourced. Standard sh syntax rules apply. Assignments are:
CONF_SWAPFILE Set where the swap file should be placed. Defaults to /var/swap. It is unlikely that you will need to change this, unless you have very strange partitioning, and then you will most likely be using an swap partition anyway.
CONF_SWAPSIZE Set size to this absolute value, in MBytes. Leaving this empty (which is the Default) uses an computed value as size instead. This is unlikely to ever be needed, unless in some strange diskspace situations. Note, that swap enabled and size smaller than RAM may causes kernal-internal VM troubles on some kernels/systems.
CONF_SWAPFACTOR Set size to computed value. Uses this value times RAM size. It so dynamically adapts to RAM size, guarantees enough swap on large RAM, without going into excess diskspace on small RAM (and disk?) systems. Defaults to 2, which gives swap size = 2 * RAM size.
CONF_MAXSWAP Set size restriction of maximal computed and absolute(!) values, in MBytes. Defaults to 2048 which was a former 32bit kernel limit for the swapfile size and is now a limit to prevent unusually and senselessly big swap files on systems with a lot of RAM.


/etc/dphys-swapfile config


       # /etc/dphys-swapfile settings for dphys-swapfile  run from /etc/init.d 
       # sourced with . so full normal sh syntax applies 
       # location of swapfile
       CONF_SWAPFILE=/varnew/swap   #  default  /var/swap
       # For setup aka install: size is  computed as CONF_SWAPFACTOR times RAM size, dynamically adapts if RAM size changes
       # really not the best way to determine a good size
       # set size to absolute value, if empty (default) uses computed value
       # restrict size (computed and absolute!) 
       #  set to empty for no limit. Beware of filling up  partitions where CONF_SWAPFILE lives

Usually run at system startup and shutdown from an /etc/init.d (or /etc/rc.d)

       # /etc/init.d/dphys-swapfile - automatically set up an swapfile
       # author franklin, last modification 2004.06.04
       # This script is copyright ETH Zuerich Physics Departement,
       #   use under either modified/non-advertising BSD or GPL license
       case "$1" in
           /sbin/dphys-swapfile setup
           /sbin/dphys-swapfile swapon
           /sbin/dphys-swapfile swapoff
       exit 0


swapfile has holes
Use of f2fs requires filling up the file via
on RaspberryPi Zero:
sudo dd if=/dev/zero of=swapfile bs=1M count= mb
200+0 records in
200+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 16.7755 s, 12.5 MB/s

read swap header failed
sudo mkswap /varnew/swap