blob: e2ff4b5cc57c7d7560af32723b01c2e4759482f6 [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "swapon (15 kb)"
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010011//config: default y
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Once you have created some swap space using 'mkswap', you also need
14//config: to enable your swap space with the 'swapon' utility. The 'swapoff'
15//config: utility is used, typically at system shutdown, to disable any swap
16//config: space. If you are not using any swap space, you can leave this
17//config: option disabled.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010018//config:
19//config:config FEATURE_SWAPON_DISCARD
20//config: bool "Support discard option -d"
21//config: default y
22//config: depends on SWAPON
23//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020024//config: Enable support for discarding swap area blocks at swapon and/or as
25//config: the kernel frees them. This option enables both the -d option on
26//config: 'swapon' and the 'discard' option for swap entries in /etc/fstab.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010027//config:
28//config:config FEATURE_SWAPON_PRI
29//config: bool "Support priority option -p"
30//config: default y
31//config: depends on SWAPON
32//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020033//config: Enable support for setting swap device priority in swapon.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010034//config:
35//config:config SWAPOFF
Denys Vlasenkob097a842018-12-28 03:20:17 +010036//config: bool "swapoff (14 kb)"
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010037//config: default y
Denys Vlasenko8cae43c2017-07-27 10:58:08 +020038//config:
39//config:config FEATURE_SWAPONOFF_LABEL
40//config: bool "Support specifying devices by label or UUID"
41//config: default y
42//config: depends on SWAPON || SWAPOFF
43//config: select VOLUMEID
44//config: help
45//config: This allows for specifying a device by label or uuid, rather than by
46//config: name. This feature utilizes the same functionality as blkid/findfs.
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010047
Denys Vlasenko205d48e2017-01-29 14:57:33 +010048// APPLET_ODDNAME:name main location suid_type help
49//applet:IF_SWAPON( APPLET_ODDNAME(swapon, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapon))
Denys Vlasenkof417ffd2016-11-14 17:30:50 +010050//applet:IF_SWAPOFF(APPLET_ODDNAME(swapoff, swap_on_off, BB_DIR_SBIN, BB_SUID_DROP, swapoff))
51
52//kbuild:lib-$(CONFIG_SWAPON) += swaponoff.o
53//kbuild:lib-$(CONFIG_SWAPOFF) += swaponoff.o
54
Pere Orga5bc8c002011-04-11 03:29:49 +020055//usage:#define swapon_trivial_usage
René Rhéaumee7695772015-01-05 20:35:00 +010056//usage: "[-a] [-e]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020057//usage:#define swapon_full_usage "\n\n"
58//usage: "Start swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020059//usage: "\n -a Start swapping on all swap devices"
Matt Whitlock0a53b202014-03-22 19:21:01 -040060//usage: IF_FEATURE_SWAPON_DISCARD(
61//usage: "\n -d[POL] Discard blocks at swapon (POL=once),"
62//usage: "\n as freed (POL=pages), or both (POL omitted)"
63//usage: )
René Rhéaumee7695772015-01-05 20:35:00 +010064//usage: "\n -e Silently skip devices that do not exist"
Pere Orga5bc8c002011-04-11 03:29:49 +020065//usage: IF_FEATURE_SWAPON_PRI(
66//usage: "\n -p PRI Set swap device priority"
67//usage: )
68//usage:
69//usage:#define swapoff_trivial_usage
Mike Frysinger5f11ec32015-12-16 12:59:08 -050070//usage: "[-a] [DEVICE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020071//usage:#define swapoff_full_usage "\n\n"
72//usage: "Stop swapping on DEVICE\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020073//usage: "\n -a Stop swapping on all swap devices"
74
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000075#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020076#include "common_bufsiz.h"
Eric Andersen87590061999-10-18 21:22:59 +000077#include <mntent.h>
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020078#ifndef __BIONIC__
79# include <sys/swap.h>
80#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000081
Denys Vlasenko8cae43c2017-07-27 10:58:08 +020082#if ENABLE_FEATURE_SWAPONOFF_LABEL
Natanael Copa9aff2992009-09-20 04:28:22 +020083# include "volume_id.h"
84#else
85# define resolve_mount_spec(fsname) ((void)0)
86#endif
87
Denys Vlasenko14bd16a2011-07-08 08:49:40 +020088#ifndef MNTTYPE_SWAP
89# define MNTTYPE_SWAP "swap"
90#endif
91
Matt Whitlock0a53b202014-03-22 19:21:01 -040092#if ENABLE_FEATURE_SWAPON_DISCARD
93#ifndef SWAP_FLAG_DISCARD
94#define SWAP_FLAG_DISCARD 0x10000
95#endif
96#ifndef SWAP_FLAG_DISCARD_ONCE
97#define SWAP_FLAG_DISCARD_ONCE 0x20000
98#endif
99#ifndef SWAP_FLAG_DISCARD_PAGES
100#define SWAP_FLAG_DISCARD_PAGES 0x40000
101#endif
102#define SWAP_FLAG_DISCARD_MASK \
103 (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
104#endif
105
106
107#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000108struct globals {
109 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100110} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200111#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000112#define g_flags (G.flags)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200113#define save_g_flags() int save_g_flags = g_flags
114#define restore_g_flags() g_flags = save_g_flags
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000115#else
116#define g_flags 0
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200117#define save_g_flags() ((void)0)
118#define restore_g_flags() ((void)0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000119#endif
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200120#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000121
Denys Vlasenkof417ffd2016-11-14 17:30:50 +0100122#if ENABLE_SWAPOFF
123# if ENABLE_SWAPON
124# define do_swapoff (applet_name[5] == 'f')
125# else
126# define do_swapoff 1
127# endif
128#else
129# define do_swapoff 0
130#endif
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200131
132/* Command line options */
133enum {
134 OPTBIT_a, /* -a all */
René Rhéaumee7695772015-01-05 20:35:00 +0100135 OPTBIT_e, /* -e ifexists */
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200136 IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard */
137 IF_FEATURE_SWAPON_PRI ( OPTBIT_p ,) /* -p priority */
138 OPT_a = 1 << OPTBIT_a,
René Rhéaumee7695772015-01-05 20:35:00 +0100139 OPT_e = 1 << OPTBIT_e,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200140 OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
141 OPT_p = IF_FEATURE_SWAPON_PRI ((1 << OPTBIT_p)) + 0,
142};
143
144#define OPT_ALL (option_mask32 & OPT_a)
145#define OPT_DISCARD (option_mask32 & OPT_d)
René Rhéaumee7695772015-01-05 20:35:00 +0100146#define OPT_IFEXISTS (option_mask32 & OPT_e)
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200147#define OPT_PRIO (option_mask32 & OPT_p)
148
Denys Vlasenko6c634f72015-12-18 19:02:31 +0100149static int swap_enable_disable(char *device)
Eric Andersen87590061999-10-18 21:22:59 +0000150{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200151 int err = 0;
152 int quiet = 0;
Eric Andersen97b141a2002-11-03 00:25:23 +0000153
Natanael Copa9aff2992009-09-20 04:28:22 +0200154 resolve_mount_spec(&device);
Eric Andersen97b141a2002-11-03 00:25:23 +0000155
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200156 if (do_swapoff) {
157 err = swapoff(device);
158 /* Don't complain on OPT_ALL if not a swap device or if it doesn't exist */
159 quiet = (OPT_ALL && (errno == EINVAL || errno == ENOENT));
160 } else {
161 /* swapon */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500162 struct stat st;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200163 err = stat(device, &st);
164 if (!err) {
165 if (ENABLE_DESKTOP && S_ISREG(st.st_mode)) {
166 if (st.st_blocks * (off_t)512 < st.st_size) {
167 bb_error_msg("%s: file has holes", device);
168 return 1;
169 }
170 }
171 err = swapon(device, g_flags);
172 /* Don't complain on swapon -a if device is already in use */
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500173 quiet = (OPT_ALL && errno == EBUSY);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200174 }
Mike Frysinger5f11ec32015-12-16 12:59:08 -0500175 /* Don't complain if file does not exist with -e option */
176 if (err && OPT_IFEXISTS && errno == ENOENT)
177 err = 0;
Eric Andersendb1df5e2002-10-26 10:27:42 +0000178 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000179
Tito Ragusac9a67132014-04-01 09:51:27 +0200180 if (err && !quiet) {
181 bb_simple_perror_msg(device);
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200182 return 1;
183 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000184 return 0;
Eric Andersen87590061999-10-18 21:22:59 +0000185}
186
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200187#if ENABLE_FEATURE_SWAPON_DISCARD
188static void set_discard_flag(char *s)
Eric Andersen87590061999-10-18 21:22:59 +0000189{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200190 /* Unset the flag first to allow fstab options to override */
191 /* options set on the command line */
192 g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
193
194 if (!s) /* No optional policy value on the commandline */
195 return;
196 /* Skip prepended '=' */
197 if (*s == '=')
198 s++;
199 /* For fstab parsing: remove other appended options */
200 *strchrnul(s, ',') = '\0';
201
202 if (strcmp(s, "once") == 0)
203 g_flags |= SWAP_FLAG_DISCARD_ONCE;
204 if (strcmp(s, "pages") == 0)
205 g_flags |= SWAP_FLAG_DISCARD_PAGES;
206}
207#else
208#define set_discard_flag(s) ((void)0)
Matt Whitlockb9bbd4d2014-03-22 19:10:08 -0400209#endif
Eric Andersen87590061999-10-18 21:22:59 +0000210
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200211#if ENABLE_FEATURE_SWAPON_PRI
212static void set_priority_flag(char *s)
213{
214 unsigned prio;
Mike Frysinger6943a942005-09-13 02:29:39 +0000215
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200216 /* For fstab parsing: remove other appended options */
217 *strchrnul(s, ',') = '\0';
218 /* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK) */
219 prio = bb_strtou(s, NULL, 10);
220 if (!errno) {
221 /* Unset the flag first to allow fstab options to override */
222 /* options set on the command line */
223 g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
224 MIN(prio, SWAP_FLAG_PRIO_MASK);
225 }
226}
227#else
228#define set_priority_flag(s) ((void)0)
229#endif
230
231static int do_em_all_in_fstab(void)
232{
233 struct mntent *m;
234 int err = 0;
235 FILE *f = xfopen_for_read("/etc/fstab");
236
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200237 while ((m = getmntent(f)) != NULL) {
238 if (strcmp(m->mnt_type, MNTTYPE_SWAP) == 0) {
239 /* swapon -a should ignore entries with noauto,
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200240 * but swapoff -a should process them
241 */
242 if (do_swapoff || hasmntopt(m, MNTOPT_NOAUTO) == NULL) {
243 /* each swap space might have different flags */
244 /* save global flags for the next round */
245 save_g_flags();
246 if (ENABLE_FEATURE_SWAPON_DISCARD) {
247 char *p = hasmntopt(m, "discard");
248 if (p) {
249 /* move to '=' or to end of string */
250 p += 7;
251 set_discard_flag(p);
Tito Ragusa8c7fcbd2013-08-08 10:21:27 +0200252 }
253 }
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200254 if (ENABLE_FEATURE_SWAPON_PRI) {
255 char *p = hasmntopt(m, "pri");
256 if (p) {
257 set_priority_flag(p + 4);
258 }
259 }
260 err |= swap_enable_disable(m->mnt_fsname);
261 restore_g_flags();
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200262 }
263 }
264 }
Mike Frysinger6943a942005-09-13 02:29:39 +0000265
Lauri Kasanend2844fc2010-04-29 22:20:57 +0200266 if (ENABLE_FEATURE_CLEAN_UP)
267 endmntent(f);
Mike Frysinger6943a942005-09-13 02:29:39 +0000268
Eric Andersendb1df5e2002-10-26 10:27:42 +0000269 return err;
Eric Andersen87590061999-10-18 21:22:59 +0000270}
271
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200272static int do_all_in_proc_swaps(void)
273{
274 char *line;
275 int err = 0;
276 FILE *f = fopen_for_read("/proc/swaps");
277 /* Don't complain if missing */
278 if (f) {
279 while ((line = xmalloc_fgetline(f)) != NULL) {
280 if (line[0] == '/') {
281 *strchrnul(line, ' ') = '\0';
282 err |= swap_enable_disable(line);
283 }
284 free(line);
285 }
286 if (ENABLE_FEATURE_CLEAN_UP)
287 fclose(f);
288 }
289
290 return err;
291}
292
René Rhéaumee7695772015-01-05 20:35:00 +0100293#define OPTSTR_SWAPON "ae" \
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200294 IF_FEATURE_SWAPON_DISCARD("d::") \
295 IF_FEATURE_SWAPON_PRI("p:")
296
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000297int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000298int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen87590061999-10-18 21:22:59 +0000299{
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200300 IF_FEATURE_SWAPON_PRI(char *prio;)
301 IF_FEATURE_SWAPON_DISCARD(char *discard = NULL;)
302 int ret = 0;
Mike Frysinger6943a942005-09-13 02:29:39 +0000303
Denys Vlasenko16714242011-09-21 01:59:15 +0200304 INIT_G();
305
René Rhéaumee7695772015-01-05 20:35:00 +0100306 getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200307 IF_FEATURE_SWAPON_DISCARD(, &discard)
308 IF_FEATURE_SWAPON_PRI(, &prio)
309 );
Mike Frysinger6943a942005-09-13 02:29:39 +0000310
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000311 argv += optind;
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200312
313 if (OPT_DISCARD) {
314 set_discard_flag(discard);
315 }
316 if (OPT_PRIO) {
317 set_priority_flag(prio);
318 }
319
320 if (OPT_ALL) {
321 /* swapoff -a does also /proc/swaps */
322 if (do_swapoff)
323 ret = do_all_in_proc_swaps();
324 ret |= do_em_all_in_fstab();
325 } else if (!*argv) {
326 /* if not -a we need at least one arg */
Denis Vlasenkoee56e012008-05-18 23:05:34 +0000327 bb_show_usage();
Tito Ragusaa3f326c2014-03-31 16:39:26 +0200328 }
329 /* Unset -a now to allow for more messages in swap_enable_disable */
330 option_mask32 = option_mask32 & ~OPT_a;
331 /* Now process devices on the commandline if any */
332 while (*argv) {
333 ret |= swap_enable_disable(*argv++);
334 }
Mike Frysinger2d5e4f62005-09-16 04:41:20 +0000335 return ret;
Eric Andersen87590061999-10-18 21:22:59 +0000336}