Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

swapon

Enable devices and files for paging and swapping.

Synopsis

swapon [options] device...
swapon -a [options]
swapon --show[=columns]
swapon -s

Operation

Calls swapon(2) to enable swapping on the specified device or file.

Enable mode

Enables swap on one or more specified devices/files. Priority and discard policy can be configured via flags or fstab-style options.

--all mode

Reads /etc/fstab for entries with type swap, enables each one. Entries with the noauto option are skipped. Already-active swap areas are silently skipped.

--show mode (display only)

Reads /proc/swaps and displays a formatted table of active swap areas.

Inputs

SourcePurpose
Device/file pathTarget to enable as swap
/etc/fstabEnumerate swap entries for --all
/proc/swapsList active swap areas for --show/-s

Outputs

  • swapon(2) syscall to enable swap
  • stdout: swap area listing (--show, -s)

Syscalls

SyscallDescription
swapon(2)Enable swapping on a device/file

Flags for swapon(2)

FlagValueDescription
SWAP_FLAG_PREFER0x8000Priority is specified (OR’d with priority in lower 15 bits)
SWAP_FLAG_PRIO_MASK0x7fffPriority value mask (0-32767)
SWAP_FLAG_DISCARD0x10000Enable discard
SWAP_FLAG_DISCARD_ONCE0x20000Discard at swapon time
SWAP_FLAG_DISCARD_PAGES0x40000Discard freed page clusters

Command-line options

OptionDescription
-a, --allEnable all swap entries from /etc/fstab
-d, --discard[=policy]Enable discard (once, pages, or both)
-e, --ifexistsSilently skip non-existent devices
-p, --priority <N>Set swap priority (0-32767)
-s, --summaryDisplay /proc/swaps (deprecated)
--show[=columns]Display formatted table of swap areas
--noheadingsSuppress table headers
--bytesShow sizes in bytes
-v, --verboseVerbose output
-h, --helpDisplay help
-V, --versionDisplay version

--show columns

NAME, TYPE, SIZE, USED, PRIO

Exit codes

CodeMeaning
0Success
nonzeroFailure

Permissions

Requires CAP_SYS_ADMIN.

Not yet implemented

  • LABEL=, UUID=, PARTLABEL=, PARTUUID= device specifiers (needs libblkid)
  • -L, -U flags
  • -f, --fixpgsz (reinitialize swap on page size mismatch)
  • -o, --options (fstab-style option string)
  • -T, --fstab (alternative fstab)
  • --output-all, --raw output modes
  • Suspend data detection
  • Reading priority/discard from fstab options in --all mode