blob: f432ce18012c63447ef9c5cbfc6600dbfb01c333 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen87590061999-10-18 21:22:59 +00002/*
3 * Mini swapon/swapoff implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen87590061999-10-18 21:22:59 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersen87590061999-10-18 21:22:59 +00008 */
Denys Vlasenkof417ffd2016-11-14 17:30:50 +01009//config:config SWAPON
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "swapon (4.9 kb)"
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Once you have created some swap space using 'mkswap', you also need
15//config: to enable your swap space with the 'swapon' utility. The 'swapoff'
16//config: utility is used, typically at system shutdown, to disable any swap
17//config: space. If you are not using any swap space, you can leave this
18//config: option disabled.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010019//config:
20//config:config FEATURE_SWAPON_DISCARD
21//config: bool "Support discard option -d"
22//config: default y
23//config: depends on SWAPON
24//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: Enable support for discarding swap area blocks at swapon and/or as
26//config: the kernel frees them. This option enables both the -d option on
27//config: 'swapon' and the 'discard' option for swap entries in /etc/fstab.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010028//config:
29//config:config FEATURE_SWAPON_PRI
30//config: bool "Support priority option -p"
31//config: default y
32//config: depends on SWAPON
33//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020034//config: Enable support for setting swap device priority in swapon.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010035//config:
36//config:config SWAPOFF
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020037//config: bool "swapoff (4.3 kb)"
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010038//config: default y
39//config: select PLATFORM_LINUX
Denys Vlasenko8cae43c2017-07-27 10:58:08 +020040//config:
41//config:config FEATURE_SWAPONOFF_LABEL
42//config: bool "Support specifying devices by label or UUID"
43//config: default y
44//config: depends on SWAPON || SWAPOFF
45//config: select VOLUMEID
46//config: help
47//config: This allows for specifying a device by label or uuid, rather than by
48//config: name. This feature utilizes the same functionality as blkid/findfs.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010049
Denys Vlasenko205d48e2017-01-29 14:57:33 +010050// APPLET_ODDNAME:name main location suid_type help
51//applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010052//applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
53
54//kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
55//kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
56
Pere Orga5bc8c002011-04-11 03:29:49 +020057//usage:#define swapon_trivial_usage
René Rhéaumee7695772015-01-05 20:35:00 +010058//usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020059//usage:#define swapon_full_usage "\n\n"
60//usage: "Start swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020061//usage: "\n -a Start swapping on all swap devices"
Matt Whitlock0a53b202014-03-22 19:21:01 -040062//usage: IF_FEATURE_SWAPON_DISCARD(
63//usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
64//usage: "\n as freed (POL=pages), or both (POL omitted)"
65//usage: )
René Rhéaumee7695772015-01-05 20:35:00 +010066//usage: "\n -e Silently skip devices that do not exist"
Pere Orga5bc8c002011-04-11 03:29:49 +020067//usage: IF_FEATURE_SWAPON_PRI(
68//usage: "\n -p PRI Set swap device priority"
69//usage: )
70//usage:
71//usage:#define swapoff_trivial_usage
Mike Frysinger5f11ec32015-12-16 12:59:08 -050072//usage: "[-a] [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020073//usage:#define swapoff_full_usage "\n\n"
74//usage: "Stop swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020075//usage: "\n -a Stop swapping on all swap devices"
76
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000077#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020078#include "common_bufsiz.h"
Eric Andersen87590061999-10-18 21:22:59 +000079#include <mntent.h>
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020080#ifndef __BIONIC__
81# include <sys/swap.h>
82#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000083
Denys Vlasenko8cae43c2017-07-27 10:58:08 +020084#if ENABLE_FEATURE_SWAPONOFF_LABEL
Natanael Copa9aff2992009-09-20 04:28:22 +020085# include "volume_id.h"
86#else
87# define resolve_mount_spec(fsname) ((void)0)
88#endif
89
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020090#ifndef MNTTYPE_SWAP
91# define MNTTYPE_SWAP "swap"
92#endif
93
Matt Whitlock0a53b202014-03-22 19:21:01 -040094#if ENABLE_FEATURE_SWAPON_DISCARD
95#ifndef SWAP_FLAG_DISCARD
96#define SWAP_FLAG_DISCARD 0x10000
97#endif
98#ifndef SWAP_FLAG_DISCARD_ONCE
99#define SWAP_FLAG_DISCARD_ONCE 0x20000
100#endif
101#ifndef SWAP_FLAG_DISCARD_PAGES
102#define SWAP_FLAG_DISCARD_PAGES 0x40000
103#endif
104#define SWAP_FLAG_DISCARD_MASK \
105 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
106#endif
107
108
109#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000110struct globals {
111 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100112} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200113#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000114#define g_flags (G.flags)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200115#define save_g_flags() int save_g_flags = g_flags
116#define restore_g_flags() g_flags = save_g_flags
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000117#else
118#define g_flags 0
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200119#define save_g_flags() ((void)0)
120#define restore_g_flags() ((void)0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000121#endif
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200122#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000123
Denys Vlasenkof417ffd2016-11-14 17:30:50 +0100124#if ENABLE_SWAPOFF
125# if ENABLE_SWAPON
126# define do_swapoff (applet_name[5] == 'f')
127# else
128# define do_swapoff 1
129# endif
130#else
131# define do_swapoff 0
132#endif
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200133
134/* Command line options */
135enum {
136 OPTBIT_a, /* -a all */
René Rhéaumee7695772015-01-05 20:35:00 +0100137 OPTBIT_e, /* -e ifexists */
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200138 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
139 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
140 OPT_a = 1 << OPTBIT_a,
René Rhéaumee7695772015-01-05 20:35:00 +0100141 OPT_e = 1 << OPTBIT_e,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200142 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
143 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
144};
145
146#define OPT_ALL (option_mask32 & OPT_a)
147#define OPT_DISCARD (option_mask32 & OPT_d)
René Rhéaumee7695772015-01-05 20:35:00 +0100148#define OPT_IFEXISTS (option_mask32 & OPT_e)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200149#define OPT_PRIO (option_mask32 & OPT_p)
150
Denys Vlasenko6c634f72015-12-18 19:02:31 +0100151static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +0000152{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200153 int err = 0;
154 int quiet = 0;
Eric Andersen97b141a2002-11-03 00:25:23 +0000155
Natanael Copa9aff2992009-09-20 04:28:22 +0200156 resolve_mount_spec(&device);
Eric Andersen97b141a2002-11-03 00:25:23 +0000157
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200158 if (do_swapoff) {
159 err = swapoff(device);
160 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
161 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
162 } else {
163 /* swapon */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500164 struct stat st;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200165 err = stat(device, &st);
166 if (!err) {
167 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
168 if (st.st_blocks * (off_t)512 < st.st_size) {
169 bb_error_msg("%s: file has holes", device);
170 return 1;
171 }
172 }
173 err = swapon(device, g_flags);
174 /* Don't complain on swapon -a if device is already in use */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500175 quiet = (OPT_ALL && errno == EBUSY);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200176 }
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500177 /* Don't complain if file does not exist with -e option */
178 if (err && OPT_IFEXISTS && errno == ENOENT)
179 err = 0;
Eric Andersendb1df5e2002-10-26 10:27:42 +0000180 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000181
Tito Ragusac9a67132014-04-01 09:51:27 +0200182 if (err && !quiet) {
183 bb_simple_perror_msg(device);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200184 return 1;
185 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000186 return 0;
Eric Andersen87590061999-10-18 21:22:59 +0000187}
188
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200189#if ENABLE_FEATURE_SWAPON_DISCARD
190static void set_discard_flag(char *s)
Eric Andersen87590061999-10-18 21:22:59 +0000191{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200192 /* Unset the flag first to allow fstab options to override */
193 /* options set on the command line */
194 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
195
196 if (!s) /* No optional policy value on the commandline */
197 return;
198 /* Skip prepended '=' */
199 if (*s == '=')
200 s++;
201 /* For fstab parsing: remove other appended options */
202 *strchrnul(s, ',') = '\0';
203
204 if (strcmp(s, "once") == 0)
205 g_flags |= SWAP_FLAG_DISCARD_ONCE;
206 if (strcmp(s, "pages") == 0)
207 g_flags |= SWAP_FLAG_DISCARD_PAGES;
208}
209#else
210#define set_discard_flag(s) ((void)0)
Matt Whitlockb9bbd4d2014-03-22 19:10:08 -0400211#endif
Eric Andersen87590061999-10-18 21:22:59 +0000212
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200213#if ENABLE_FEATURE_SWAPON_PRI
214static void set_priority_flag(char *s)
215{
216 unsigned prio;
Mike Frysinger6943a942005-09-13 02:29:39 +0000217
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200218 /* For fstab parsing: remove other appended options */
219 *strchrnul(s, ',') = '\0';
220 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
221 prio = bb_strtou(s, NULL, 10);
222 if (!errno) {
223 /* Unset the flag first to allow fstab options to override */
224 /* options set on the command line */
225 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
226 MIN(prio, SWAP_FLAG_PRIO_MASK);
227 }
228}
229#else
230#define set_priority_flag(s) ((void)0)
231#endif
232
233static int do_em_all_in_fstab(void)
234{
235 struct mntent *m;
236 int err = 0;
237 FILE *f = xfopen_for_read("/etc/fstab");
238
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200239 while ((m = getmntent(f)) != NULL) {
240 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
241 /* swapon -a should ignore entries with noauto,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200242 * but swapoff -a should process them
243 */
244 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
245 /* each swap space might have different flags */
246 /* save global flags for the next round */
247 save_g_flags();
248 if (ENABLE_FEATURE_SWAPON_DISCARD) {
249 char *p = hasmntopt(m, "discard");
250 if (p) {
251 /* move to '=' or to end of string */
252 p += 7;
253 set_discard_flag(p);
Tito Ragusa8c7fcbd2013-08-08 10:21:27 +0200254 }
255 }
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200256 if (ENABLE_FEATURE_SWAPON_PRI) {
257 char *p = hasmntopt(m, "pri");
258 if (p) {
259 set_priority_flag(p + 4);
260 }
261 }
262 err |= swap_enable_disable(m->mnt_fsname);
263 restore_g_flags();
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200264 }
265 }
266 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000267
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200268 if (ENABLE_FEATURE_CLEAN_UP)
269 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000270
Eric Andersendb1df5e2002-10-26 10:27:42 +0000271 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000272}
273
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200274static int do_all_in_proc_swaps(void)
275{
276 char *line;
277 int err = 0;
278 FILE *f = fopen_for_read("/proc/swaps");
279 /* Don't complain if missing */
280 if (f) {
281 while ((line = xmalloc_fgetline(f)) != NULL) {
282 if (line[0] == '/') {
283 *strchrnul(line, ' ') = '\0';
284 err |= swap_enable_disable(line);
285 }
286 free(line);
287 }
288 if (ENABLE_FEATURE_CLEAN_UP)
289 fclose(f);
290 }
291
292 return err;
293}
294
René Rhéaumee7695772015-01-05 20:35:00 +0100295#define OPTSTR_SWAPON "ae" \
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200296 IF_FEATURE_SWAPON_DISCARD("d::") \
297 IF_FEATURE_SWAPON_PRI("p:")
298
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000299int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000300int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000301{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200302 IF_FEATURE_SWAPON_PRI(char *prio;)
303 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
304 int ret = 0;
Mike Frysinger6943a942005-09-13 02:29:39 +0000305
Denys Vlasenko16714242011-09-21 01:59:15 +0200306 INIT_G();
307
René Rhéaumee7695772015-01-05 20:35:00 +0100308 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200309 IF_FEATURE_SWAPON_DISCARD(, &discard)
310 IF_FEATURE_SWAPON_PRI(, &prio)
311 );
Mike Frysinger6943a942005-09-13 02:29:39 +0000312
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000313 argv += optind;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200314
315 if (OPT_DISCARD) {
316 set_discard_flag(discard);
317 }
318 if (OPT_PRIO) {
319 set_priority_flag(prio);
320 }
321
322 if (OPT_ALL) {
323 /* swapoff -a does also /proc/swaps */
324 if (do_swapoff)
325 ret = do_all_in_proc_swaps();
326 ret |= do_em_all_in_fstab();
327 } else if (!*argv) {
328 /* if not -a we need at least one arg */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000329 bb_show_usage();
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200330 }
331 /* Unset -a now to allow for more messages in swap_enable_disable */
332 option_mask32 = option_mask32 & ~OPT_a;
333 /* Now process devices on the commandline if any */
334 while (*argv) {
335 ret |= swap_enable_disable(*argv++);
336 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000337 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000338}