swapon, swapoff

enable/disable devices and files for paging and swapping

dphys-swapfile

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

sudo /sbin/swapoff [-h -V]
sudo /sbin/swapoff -a
sudo /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 exclude 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 0..32767 higher numbers, higher priority.
Swap pages are allocated from areas in priority order, highest priority first, exhausted before using a lower-priority area.
If multiple 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 /etc/fstab for use with swapon -a.
-s show swap usage summary . Equivalent to cat /proc/swaps.
-v verbose.
-V version
-h help
usage: swapon [-hV]
       swapon -a [-e] [-v]
       swapon [-v] [-p priority] special|LABEL=volume_name ...
       swapon [-s]

 swapon -v -s
Filename                Type        Size       Used    Priority
/dev/mmcblk0p4          partition   2,097,148 23,040   -2

> sudo swapoff -v -a
swapoff /dev/mmcblk0p4 

> top
KiB Mem :   443080 total,    35980 free,    59072 used,   348028 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   312116 avail Mem 
If Swap is not in use swapon -a -s -v do not report anything swapoff disables swapping on the specified devices and files.

Do not use swapon on a file with holes.(like f2fs)
Swap over NFS may not work.

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

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

/proc/partitions

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


dphys-swapfile

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

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

Called from /etc/init.d, computes the size and resizes an existing swap file if necessary,
mounts, unmounts and deletes it if it's no nolonger needed.
setup
install
Called at boot time,
if CONF_SWAPSIZE is not set file, generate an swap file as /etc/dphys-swapfile CONF_SWAPFACTOR times RAM size.
Resized if RAM is resized
swapon
swapoff
uninstall remove swap file (like when switching to a swap partition)

CONFIG

/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.

Files

/etc/dphys-swapfile config

Example

       # /etc/dphys-swapfile settings for dphys-swapfile  run from /etc/init.d 
       
       # sourced with . so full normal sh syntax applies 
       
       # location of swapfile  (not partition)
       CONF_SWAPFILE=/varnew/swap   #  default  /var/swap
       
       # set size to absolute value, if empty (default) uses computed value
       #CONF_SWAPSIZE=100
       CONF_SWAPSIZE=200
       
       # restrict size (computed and absolute!) 
       #  set to empty for no limit. Beware of filling up  partitions where CONF_SWAPFILE lives
       #CONF_MAXSWAP=2048
       CONF_MAXSWAP=400

        # For setup aka install: size is  computed as CONF_SWAPFACTOR times RAM size, dynamically adapts if RAM size changes
        # Not the best way to determine a good size
        #CONF_SWAPFACTOR=2 

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

       #!/bin/sh
       # /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
         start)
           /sbin/dphys-swapfile setup
           /sbin/dphys-swapfile swapon
           ;;
         stop)
           /sbin/dphys-swapfile swapoff
           ;;
       esac
       exit 0

Sample run

 sudo dphys-swapfile setup
want /var/swap=200MByte, generating swapfile ... of 200MBytes

Errors

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


From:SevereFault paraphrased If a swapfile isn't fragmented (as if created initially), it's as if there were a swap partition at its same location, i.e. same performance