1SWAPON(2) Linux Programmer's Manual SWAPON(2)
2
3
4
6 swapon, swapoff - start/stop swapping to file/device
7
9 #include <unistd.h>
10 #include <sys/swap.h>
11
12 int swapon(const char *path, int swapflags);
13 int swapoff(const char *path);
14
16 swapon() sets the swap area to the file or block device specified by
17 path. swapoff() stops swapping to the file or block device specified
18 by path.
19
20 If the SWAP_FLAG_PREFER flag is specified in the swapon() swapflags
21 argument, the new swap area will have a higher priority than default.
22 The priority is encoded within swapflags as:
23
24 (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK
25
26 If the SWAP_FLAG_DISCARD flag is specified in the swapon() swapflags
27 argument, freed swap pages will be discarded before they are reused, if
28 the swap device supports the discard or trim operation. (This may
29 improve performance on some Solid State Devices, but often it does
30 not.) See also NOTES.
31
32 These functions may be used only by a privileged process (one having
33 the CAP_SYS_ADMIN capability).
34
35 Priority
36 Each swap area has a priority, either high or low. The default prior‐
37 ity is low. Within the low-priority areas, newer areas are even lower
38 priority than older areas.
39
40 All priorities set with swapflags are high-priority, higher than
41 default. They may have any nonnegative value chosen by the caller.
42 Higher numbers mean higher priority.
43
44 Swap pages are allocated from areas in priority order, highest priority
45 first. For areas with different priorities, a higher-priority area is
46 exhausted before using a lower-priority area. If two or more areas
47 have the same priority, and it is the highest priority available, pages
48 are allocated on a round-robin basis between them.
49
50 As of Linux 1.3.6, the kernel usually follows these rules, but there
51 are exceptions.
52
54 On success, zero is returned. On error, -1 is returned, and errno is
55 set appropriately.
56
58 EBUSY (for swapon()) The specified path is already being used as a
59 swap area.
60
61 EINVAL The file path exists, but refers neither to a regular file nor
62 to a block device;
63
64 EINVAL (swapon()) The indicated path does not contain a valid swap sig‐
65 nature or resides on an in-memory filesystem such as tmpfs(5).
66
67 EINVAL (since Linux 3.4)
68 (swapon()) An invalid flag value was specified in flags.
69
70 EINVAL (swapoff()) path is not currently a swap area.
71
72 ENFILE The system-wide limit on the total number of open files has been
73 reached.
74
75 ENOENT The file path does not exist.
76
77 ENOMEM The system has insufficient memory to start swapping.
78
79 EPERM The caller does not have the CAP_SYS_ADMIN capability. Alterna‐
80 tively, the maximum number of swap files are already in use; see
81 NOTES below.
82
84 These functions are Linux-specific and should not be used in programs
85 intended to be portable. The second swapflags argument was introduced
86 in Linux 1.3.2.
87
89 The partition or path must be prepared with mkswap(8).
90
91 There is an upper limit on the number of swap files that may be used,
92 defined by the kernel constant MAX_SWAPFILES. Before kernel 2.4.10,
93 MAX_SWAPFILES has the value 8; since kernel 2.4.10, it has the value
94 32. Since kernel 2.6.18, the limit is decreased by 2 (thus: 30) if the
95 kernel is built with the CONFIG_MIGRATION option (which reserves two
96 swap table entries for the page migration features of mbind(2) and
97 migrate_pages(2)). Since kernel 2.6.32, the limit is further decreased
98 by 1 if the kernel is built with the CONFIG_MEMORY_FAILURE option.
99
100 Discard of swap pages was introduced in kernel 2.6.29, then made condi‐
101 tional on the SWAP_FLAG_DISCARD flag in kernel 2.6.36, which still dis‐
102 cards the entire swap area when swapon() is called, even if that flag
103 bit is not set.
104
106 mkswap(8), swapoff(8), swapon(8)
107
109 This page is part of release 5.04 of the Linux man-pages project. A
110 description of the project, information about reporting bugs, and the
111 latest version of this page, can be found at
112 https://www.kernel.org/doc/man-pages/.
113
114
115
116Linux 2017-09-15 SWAPON(2)