swapon, swapoff

enable/disable devices and files for paging and swapping

dphys-swapfile

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

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

Computes the size and resizes an existing swap file if necessary,
mounts, unmounts and deletes it if no nolonger needed.
setup
install
re-generate an swap file as $CONF_SWAPFACTOR times RAM size.
Called at boot time, file is resized if RAM is resized
swapon
swapoff
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

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
       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
       #CONF_SWAPFACTOR=2
       
       # 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=

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

Errors

swapfile has holes