]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/swapon.c
libblkid: (bcache) cleanup magic string definition
[thirdparty/util-linux.git] / sys-utils / swapon.c
CommitLineData
e076b552 1#include <assert.h>
fd6b7a7f
KZ
2#include <stdlib.h>
3#include <stdio.h>
4#include <getopt.h>
5#include <string.h>
fd6b7a7f 6#include <errno.h>
5c36a0eb 7#include <sys/stat.h>
ffa63a3d 8#include <unistd.h>
6274b987
KC
9#include <sys/types.h>
10#include <sys/wait.h>
11#include <fcntl.h>
3399a218 12#include <stdint.h>
57a60bf0 13#include <ctype.h>
81772d3c 14
248a7f70 15#include <libsmartcols.h>
b5b2c388 16
d335c9bd
KZ
17#include "c.h"
18#include "nls.h"
3399a218
MK
19#include "bitops.h"
20#include "blkdev.h"
4e270e3f 21#include "pathnames.h"
bfbb12d2 22#include "xalloc.h"
18b3e549 23#include "strutils.h"
e07fdac4 24#include "optutils.h"
efb8854f 25#include "closestream.h"
167eaed7 26
d335c9bd 27#include "swapheader.h"
18b3e549 28#include "swapprober.h"
0b0c231f
KZ
29#include "swapon-common.h"
30
ffa63a3d
MF
31#ifdef HAVE_SYS_SWAP_H
32# include <sys/swap.h>
33#endif
34
c2301380 35#ifndef SWAP_FLAG_DISCARD
d6387c98 36# define SWAP_FLAG_DISCARD 0x10000 /* enable discard for swap */
c2301380
HD
37#endif
38
d6387c98
RA
39#ifndef SWAP_FLAG_DISCARD_ONCE
40# define SWAP_FLAG_DISCARD_ONCE 0x20000 /* discard swap area at swapon-time */
41#endif
42
43#ifndef SWAP_FLAG_DISCARD_PAGES
44# define SWAP_FLAG_DISCARD_PAGES 0x40000 /* discard page-clusters after use */
45#endif
46
47#define SWAP_FLAGS_DISCARD_VALID (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \
48 SWAP_FLAG_DISCARD_PAGES)
49
9514a612
KZ
50#ifndef SWAP_FLAG_PREFER
51# define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */
52#endif
53
54#ifndef SWAP_FLAG_PRIO_MASK
55# define SWAP_FLAG_PRIO_MASK 0x7fff
56#endif
57
58#ifndef SWAP_FLAG_PRIO_SHIFT
59# define SWAP_FLAG_PRIO_SHIFT 0
60#endif
61
ffa63a3d
MF
62#ifndef SWAPON_HAS_TWO_ARGS
63/* libc is insane, let's call the kernel */
64# include <sys/syscall.h>
65# define swapon(path, flags) syscall(SYS_swapon, path, flags)
ffa63a3d
MF
66#endif
67
3399a218
MK
68#define MAX_PAGESIZE (64 * 1024)
69
b443c177 70#ifndef UUID_STR_LEN
9f20d800 71# define UUID_STR_LEN 37
b443c177
PP
72#endif
73
e8be80dd
KZ
74enum {
75 SIG_SWAPSPACE = 1,
76 SIG_SWSUSPEND
77};
78
e076b552
SK
79/* column names */
80struct colinfo {
81 const char *name; /* header */
82 double whint; /* width hint (N < 1 is in percent of termwidth) */
248a7f70 83 int flags; /* SCOLS_FL_* */
e076b552
SK
84 const char *help;
85};
248a7f70 86
5db57cfc
SK
87enum {
88 COL_PATH,
89 COL_TYPE,
90 COL_SIZE,
91 COL_USED,
92 COL_PRIO,
93 COL_UUID,
94 COL_LABEL
95};
2ba641e5 96static struct colinfo infos[] = {
e076b552 97 [COL_PATH] = { "NAME", 0.20, 0, N_("device file or partition path") },
248a7f70
OO
98 [COL_TYPE] = { "TYPE", 0.20, SCOLS_FL_TRUNC, N_("type of the device")},
99 [COL_SIZE] = { "SIZE", 0.20, SCOLS_FL_RIGHT, N_("size of the swap area")},
100 [COL_USED] = { "USED", 0.20, SCOLS_FL_RIGHT, N_("bytes in use")},
101 [COL_PRIO] = { "PRIO", 0.20, SCOLS_FL_RIGHT, N_("swap priority")},
5db57cfc
SK
102 [COL_UUID] = { "UUID", 0.20, 0, N_("swap uuid")},
103 [COL_LABEL] = { "LABEL", 0.20, 0, N_("swap label")},
e076b552 104};
a560a348 105
6c44094a
KZ
106
107/* swap area properties */
108struct swap_prop {
109 int discard; /* discard policy */
110 int priority; /* non-prioritized swap by default */
111 int no_fail; /* skip device if not exist */
112};
113
114/* device description */
115struct swap_device {
116 const char *path; /* device or file to be turned on */
9eeba70e
SK
117 const char *label; /* swap label */
118 const char *uuid; /* unique identifier */
6c44094a
KZ
119 unsigned int pagesize;
120};
121
122/* control struct */
123struct swapon_ctl {
9eeba70e 124 int columns[ARRAY_SIZE(infos) * 2]; /* --show columns */
6c44094a
KZ
125 int ncolumns; /* number of columns */
126
127 struct swap_prop props; /* global settings for all devices */
128
9eeba70e
SK
129 unsigned int
130 all:1, /* turn on all swap devices */
131 bytes:1, /* display --show in bytes */
132 fix_page_size:1, /* reinitialize page size */
9eeba70e
SK
133 no_heading:1, /* toggle --show headers */
134 raw:1, /* toggle --show alignment */
135 show:1, /* display --show information */
136 verbose:1; /* be chatty */
137};
e076b552
SK
138
139static int column_name_to_id(const char *name, size_t namesz)
140{
141 size_t i;
142
143 assert(name);
144
a560a348 145 for (i = 0; i < ARRAY_SIZE(infos); i++) {
e076b552
SK
146 const char *cn = infos[i].name;
147
148 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
149 return i;
150 }
151 warnx(_("unknown column: %s"), name);
152 return -1;
153}
154
9eeba70e 155static inline int get_column_id(const struct swapon_ctl *ctl, int num)
e076b552 156{
9eeba70e
SK
157 assert(num < ctl->ncolumns);
158 assert(ctl->columns[num] < (int) ARRAY_SIZE(infos));
e076b552 159
9eeba70e 160 return ctl->columns[num];
e076b552
SK
161}
162
9eeba70e 163static inline struct colinfo *get_column_info(const struct swapon_ctl *ctl, unsigned num)
e076b552 164{
9eeba70e 165 return &infos[get_column_id(ctl, num)];
e076b552
SK
166}
167
9eeba70e 168static void add_scols_line(const struct swapon_ctl *ctl, struct libscols_table *table, struct libmnt_fs *fs)
e076b552
SK
169{
170 int i;
248a7f70 171 struct libscols_line *line;
5db57cfc
SK
172 blkid_probe pr = NULL;
173 const char *data;
e076b552 174
248a7f70 175 assert(table);
e076b552
SK
176 assert(fs);
177
248a7f70 178 line = scols_table_new_line(table, NULL);
e5166348 179 if (!line)
780ce22c
KZ
180 err(EXIT_FAILURE, _("failed to allocate output line"));
181
5db57cfc
SK
182 data = mnt_fs_get_source(fs);
183 if (access(data, R_OK) == 0)
184 pr = get_swap_prober(data);
9eeba70e 185 for (i = 0; i < ctl->ncolumns; i++) {
e076b552 186 char *str = NULL;
7ae8b469 187 off_t size;
e076b552 188
9eeba70e 189 switch (get_column_id(ctl, i)) {
e076b552 190 case COL_PATH:
a0e35e30 191 xasprintf(&str, "%s", mnt_fs_get_source(fs));
e076b552
SK
192 break;
193 case COL_TYPE:
a0e35e30 194 xasprintf(&str, "%s", mnt_fs_get_swaptype(fs));
e076b552
SK
195 break;
196 case COL_SIZE:
7ae8b469
SK
197 size = mnt_fs_get_size(fs);
198 size *= 1024; /* convert to bytes */
9eeba70e 199 if (ctl->bytes)
a0e35e30 200 xasprintf(&str, "%jd", size);
7ae8b469
SK
201 else
202 str = size_to_human_string(SIZE_SUFFIX_1LETTER, size);
e076b552
SK
203 break;
204 case COL_USED:
7ae8b469
SK
205 size = mnt_fs_get_usedsize(fs);
206 size *= 1024; /* convert to bytes */
9eeba70e 207 if (ctl->bytes)
a0e35e30 208 xasprintf(&str, "%jd", size);
7ae8b469
SK
209 else
210 str = size_to_human_string(SIZE_SUFFIX_1LETTER, size);
e076b552
SK
211 break;
212 case COL_PRIO:
a0e35e30 213 xasprintf(&str, "%d", mnt_fs_get_priority(fs));
e076b552 214 break;
5db57cfc
SK
215 case COL_UUID:
216 if (pr && !blkid_probe_lookup_value(pr, "UUID", &data, NULL))
217 xasprintf(&str, "%s", data);
5db57cfc
SK
218 break;
219 case COL_LABEL:
220 if (pr && !blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
221 xasprintf(&str, "%s", data);
5db57cfc 222 break;
e076b552
SK
223 default:
224 break;
225 }
226
780ce22c
KZ
227 if (str && scols_line_refer_data(line, i, str))
228 err(EXIT_FAILURE, _("failed to add output data"));
e076b552 229 }
5db57cfc
SK
230 if (pr)
231 blkid_free_probe(pr);
e076b552
SK
232 return;
233}
234
d335c9bd 235static int display_summary(void)
fd6b7a7f 236{
5072b90a 237 struct libmnt_table *st = get_swaps();
64b58881
KZ
238 struct libmnt_iter *itr;
239 struct libmnt_fs *fs;
fd6b7a7f 240
64b58881 241 if (!st)
57a60bf0 242 return -1;
e8f26419 243
a2f17bb2
KZ
244 if (mnt_table_is_empty(st))
245 return 0;
246
64b58881
KZ
247 itr = mnt_new_iter(MNT_ITER_FORWARD);
248 if (!itr)
249 err(EXIT_FAILURE, _("failed to initialize libmount iterator"));
57a60bf0 250
a2f17bb2 251 printf(_("%s\t\t\t\tType\t\tSize\tUsed\tPriority\n"), _("Filename"));
64b58881
KZ
252
253 while (mnt_table_next_fs(st, itr, &fs) == 0) {
74712441 254 printf("%-39s\t%-8s\t%jd\t%jd\t%d\n",
64b58881
KZ
255 mnt_fs_get_source(fs),
256 mnt_fs_get_swaptype(fs),
257 mnt_fs_get_size(fs),
258 mnt_fs_get_usedsize(fs),
259 mnt_fs_get_priority(fs));
57a60bf0 260 }
fd6b7a7f 261
64b58881
KZ
262 mnt_free_iter(itr);
263 return 0;
fd6b7a7f
KZ
264}
265
9eeba70e 266static int show_table(struct swapon_ctl *ctl)
e076b552
SK
267{
268 struct libmnt_table *st = get_swaps();
880f09c8 269 struct libmnt_iter *itr = NULL;
e076b552 270 struct libmnt_fs *fs;
e5166348 271 int i;
248a7f70 272 struct libscols_table *table = NULL;
e076b552
SK
273
274 if (!st)
275 return -1;
276
277 itr = mnt_new_iter(MNT_ITER_FORWARD);
278 if (!itr)
279 err(EXIT_FAILURE, _("failed to initialize libmount iterator"));
280
710ed55d
KZ
281 scols_init_debug(0);
282
0925a9dd 283 table = scols_new_table();
e5166348 284 if (!table)
780ce22c 285 err(EXIT_FAILURE, _("failed to allocate output table"));
e5166348 286
9eeba70e
SK
287 scols_table_enable_raw(table, ctl->raw);
288 scols_table_enable_noheadings(table, ctl->no_heading);
e076b552 289
9eeba70e
SK
290 for (i = 0; i < ctl->ncolumns; i++) {
291 struct colinfo *col = get_column_info(ctl, i);
e076b552 292
e5166348 293 if (!scols_table_new_column(table, col->name, col->whint, col->flags))
780ce22c 294 err(EXIT_FAILURE, _("failed to allocate output column"));
e076b552
SK
295 }
296
297 while (mnt_table_next_fs(st, itr, &fs) == 0)
9eeba70e 298 add_scols_line(ctl, table, fs);
e076b552 299
248a7f70 300 scols_print_table(table);
248a7f70 301 scols_unref_table(table);
e5166348
KZ
302 mnt_free_iter(itr);
303 return 0;
e076b552
SK
304}
305
6274b987 306/* calls mkswap */
6c44094a 307static int swap_reinitialize(struct swap_device *dev)
b5b2c388 308{
6274b987
KC
309 pid_t pid;
310 int status, ret;
2dd3ba2c 311 char const *cmd[7];
6274b987
KC
312 int idx=0;
313
6c44094a
KZ
314 assert(dev);
315 assert(dev->path);
316
317 warnx(_("%s: reinitializing the swap."), dev->path);
0d761944 318
a125123f 319 switch ((pid=fork())) {
6274b987 320 case -1: /* fork error */
d51d05d3 321 warn(_("fork failed"));
6274b987
KC
322 return -1;
323
324 case 0: /* child */
eac478dc
KZ
325 if (geteuid() != getuid()) {
326 /* in case someone uses swapon as setuid binary */
327 if (setgid(getgid()) < 0)
328 exit(EXIT_FAILURE);
329 if (setuid(getuid()) < 0)
330 exit(EXIT_FAILURE);
331 }
332
f1bc5996 333 cmd[idx++] = "mkswap";
6c44094a 334 if (dev->label) {
6274b987 335 cmd[idx++] = "-L";
6c44094a 336 cmd[idx++] = dev->label;
6274b987 337 }
6c44094a 338 if (dev->uuid) {
6274b987 339 cmd[idx++] = "-U";
6c44094a 340 cmd[idx++] = dev->uuid;
6274b987 341 }
6c44094a 342 cmd[idx++] = dev->path;
6274b987 343 cmd[idx++] = NULL;
2dd3ba2c 344 execvp(cmd[0], (char * const *) cmd);
07ff972e 345 err(EXIT_FAILURE, _("failed to execute %s"), cmd[0]);
6274b987
KC
346
347 default: /* parent */
348 do {
048811c9
KZ
349 ret = waitpid(pid, &status, 0);
350 } while (ret == -1 && errno == EINTR);
351
352 if (ret < 0) {
353 warn(_("waitpid failed"));
354 return -1;
355 }
6274b987
KC
356
357 /* mkswap returns: 0=suss, 1=error */
358 if (WIFEXITED(status) && WEXITSTATUS(status)==0)
359 return 0; /* ok */
f8ff4ebb 360 break;
6274b987
KC
361 }
362 return -1; /* error */
363}
364
6c44094a
KZ
365/* Replaces unwanted SWSUSPEND signature with swap signature */
366static int swap_rewrite_signature(const struct swap_device *dev)
617d8a3b
KZ
367{
368 int fd, rc = -1;
369
6c44094a
KZ
370 assert(dev);
371 assert(dev->path);
372 assert(dev->pagesize);
373
374 fd = open(dev->path, O_WRONLY);
617d8a3b 375 if (fd == -1) {
6c44094a 376 warn(_("cannot open %s"), dev->path);
617d8a3b
KZ
377 return -1;
378 }
379
6c44094a
KZ
380 if (lseek(fd, dev->pagesize - SWAP_SIGNATURE_SZ, SEEK_SET) < 0) {
381 warn(_("%s: lseek failed"), dev->path);
617d8a3b 382 goto err;
5dd53f43 383 }
617d8a3b
KZ
384
385 if (write(fd, (void *) SWAP_SIGNATURE,
5dd53f43 386 SWAP_SIGNATURE_SZ) != SWAP_SIGNATURE_SZ) {
6c44094a 387 warn(_("%s: write signature failed"), dev->path);
617d8a3b 388 goto err;
5dd53f43 389 }
617d8a3b
KZ
390
391 rc = 0;
392err:
14699715 393 if (close_fd(fd) != 0) {
6c44094a 394 warn(_("write failed: %s"), dev->path);
14699715
SK
395 rc = -1;
396 }
617d8a3b
KZ
397 return rc;
398}
399
d335c9bd 400static int swap_detect_signature(const char *buf, int *sig)
3399a218 401{
6c44094a
KZ
402 assert(buf);
403 assert(sig);
404
eb397d19 405 if (memcmp(buf, SWAP_SIGNATURE, SWAP_SIGNATURE_SZ) == 0)
e8be80dd
KZ
406 *sig = SIG_SWAPSPACE;
407
408 else if (memcmp(buf, "S1SUSPEND", 9) == 0 ||
409 memcmp(buf, "S2SUSPEND", 9) == 0 ||
410 memcmp(buf, "ULSUSPEND", 9) == 0 ||
5a16af58
KZ
411 memcmp(buf, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8) == 0 ||
412 memcmp(buf, "LINHIB0001", 10) == 0)
e8be80dd
KZ
413 *sig = SIG_SWSUSPEND;
414 else
415 return 0;
3399a218 416
e8be80dd 417 return 1;
3399a218
MK
418}
419
6c44094a 420static char *swap_get_header(int fd, int *sig, unsigned int *pagesize)
3399a218 421{
3399a218 422 char *buf;
e8be80dd
KZ
423 ssize_t datasz;
424 unsigned int page;
3399a218 425
6c44094a
KZ
426 assert(sig);
427 assert(pagesize);
428
429 *pagesize = 0;
e8be80dd 430 *sig = 0;
3399a218 431
bfbb12d2 432 buf = xmalloc(MAX_PAGESIZE);
3399a218
MK
433
434 datasz = read(fd, buf, MAX_PAGESIZE);
e8be80dd
KZ
435 if (datasz == (ssize_t) -1)
436 goto err;
3399a218
MK
437
438 for (page = 0x1000; page <= MAX_PAGESIZE; page <<= 1) {
439 /* skip 32k pagesize since this does not seem to
440 * be supported */
441 if (page == 0x8000)
442 continue;
443 /* the smallest swap area is PAGE_SIZE*10, it means
444 * 40k, that's less than MAX_PAGESIZE */
24f010e8 445 if (datasz < 0 || (size_t) datasz < (page - SWAP_SIGNATURE_SZ))
3399a218 446 break;
617d8a3b 447 if (swap_detect_signature(buf + page - SWAP_SIGNATURE_SZ, sig)) {
6c44094a 448 *pagesize = page;
3399a218
MK
449 break;
450 }
451 }
452
6c44094a 453 if (*pagesize)
e8be80dd 454 return buf;
3399a218 455err:
e8be80dd
KZ
456 free(buf);
457 return NULL;
458}
459
460/* returns real size of swap space */
6c44094a
KZ
461static unsigned long long swap_get_size(const struct swap_device *dev,
462 const char *hdr)
e8be80dd
KZ
463{
464 unsigned int last_page = 0;
8a101b14 465 const unsigned int swap_version = SWAP_VERSION;
e8be80dd
KZ
466 struct swap_header_v1_2 *s;
467
6c44094a
KZ
468 assert(dev);
469 assert(dev->pagesize > 0);
470
e8be80dd 471 s = (struct swap_header_v1_2 *) hdr;
6c44094a
KZ
472
473 if (s->version == swap_version)
e8be80dd 474 last_page = s->last_page;
6c44094a 475 else if (swab32(s->version) == swap_version)
e8be80dd 476 last_page = swab32(s->last_page);
6c44094a
KZ
477
478 return ((unsigned long long) last_page + 1) * dev->pagesize;
3399a218
MK
479}
480
6c44094a 481static void swap_get_info(struct swap_device *dev, const char *hdr)
b5b2c388
KZ
482{
483 struct swap_header_v1_2 *s = (struct swap_header_v1_2 *) hdr;
484
6c44094a
KZ
485 assert(dev);
486
a125123f 487 if (s && *s->volume_name)
6c44094a 488 dev->label = xstrdup(s->volume_name);
b5b2c388 489
a125123f 490 if (s && *s->uuid) {
b5b2c388 491 const unsigned char *u = s->uuid;
b443c177 492 char str[UUID_STR_LEN];
b5b2c388
KZ
493
494 snprintf(str, sizeof(str),
495 "%02x%02x%02x%02x-"
496 "%02x%02x-%02x%02x-"
497 "%02x%02x-%02x%02x%02x%02x%02x%02x",
498 u[0], u[1], u[2], u[3],
499 u[4], u[5], u[6], u[7],
500 u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]);
6c44094a 501 dev->uuid = xstrdup(str);
b5b2c388
KZ
502 }
503}
504
6c44094a 505static int swapon_checks(const struct swapon_ctl *ctl, struct swap_device *dev)
05c79b78 506{
63cccae4 507 struct stat st;
1b504263 508 int fd, sig;
05c79b78 509 char *hdr = NULL;
e8be80dd 510 unsigned long long devsize = 0;
cd04b26b 511 int permMask;
63cccae4 512
6c44094a
KZ
513 assert(ctl);
514 assert(dev);
515 assert(dev->path);
516
517 fd = open(dev->path, O_RDONLY);
8df370d3 518 if (fd == -1) {
6c44094a 519 warn(_("cannot open %s"), dev->path);
8df370d3
SK
520 goto err;
521 }
522
523 if (fstat(fd, &st) < 0) {
6c44094a 524 warn(_("stat of %s failed"), dev->path);
05c79b78 525 goto err;
63cccae4
KZ
526 }
527
cd04b26b
KZ
528 permMask = S_ISBLK(st.st_mode) ? 07007 : 07077;
529 if ((st.st_mode & permMask) != 0)
530 warnx(_("%s: insecure permissions %04o, %04o suggested."),
6c44094a 531 dev->path, st.st_mode & 07777,
88530f9f 532 ~permMask & 0666);
306c1df2 533
cd04b26b
KZ
534 if (S_ISREG(st.st_mode) && st.st_uid != 0)
535 warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
6c44094a 536 dev->path, st.st_uid);
88530f9f
KZ
537
538 /* test for holes by LBT */
539 if (S_ISREG(st.st_mode)) {
540 if (st.st_blocks * 512 < st.st_size) {
d51d05d3 541 warnx(_("%s: skipping - it appears to have holes."),
6c44094a 542 dev->path);
05c79b78 543 goto err;
88530f9f 544 }
e8be80dd
KZ
545 devsize = st.st_size;
546 }
547
05c79b78 548 if (S_ISBLK(st.st_mode) && blkdev_get_size(fd, &devsize)) {
6c44094a 549 warnx(_("%s: get size failed"), dev->path);
05c79b78 550 goto err;
e8be80dd
KZ
551 }
552
6c44094a 553 hdr = swap_get_header(fd, &sig, &dev->pagesize);
e8be80dd 554 if (!hdr) {
6c44094a 555 warnx(_("%s: read swap header failed"), dev->path);
05c79b78 556 goto err;
88530f9f
KZ
557 }
558
6c44094a
KZ
559 if (ctl->verbose)
560 warnx(_("%s: found signature [pagesize=%d, signature=%s]"),
561 dev->path,
562 dev->pagesize,
563 sig == SIG_SWAPSPACE ? "swap" :
564 sig == SIG_SWSUSPEND ? "suspend" : "unknown");
565
566 if (sig == SIG_SWAPSPACE && dev->pagesize) {
567 unsigned long long swapsize = swap_get_size(dev, hdr);
24f010e8
KZ
568 int syspg = getpagesize();
569
9eeba70e 570 if (ctl->verbose)
e6b0611b 571 warnx(_("%s: pagesize=%d, swapsize=%llu, devsize=%llu"),
6c44094a 572 dev->path, dev->pagesize, swapsize, devsize);
e8be80dd
KZ
573
574 if (swapsize > devsize) {
9eeba70e 575 if (ctl->verbose)
d51d05d3
KZ
576 warnx(_("%s: last_page 0x%08llx is larger"
577 " than actual size of swapspace"),
6c44094a
KZ
578 dev->path, swapsize);
579
580 } else if (syspg < 0 || (unsigned int) syspg != dev->pagesize) {
9eeba70e 581 if (ctl->fix_page_size) {
b5b2c388
KZ
582 int rc;
583
6c44094a 584 swap_get_info(dev, hdr);
b5b2c388 585
93bfcc94 586 warnx(_("%s: swap format pagesize does not match."),
6c44094a
KZ
587 dev->path);
588 rc = swap_reinitialize(dev);
b5b2c388 589 if (rc < 0)
0d761944
KZ
590 goto err;
591 } else
93bfcc94
KZ
592 warnx(_("%s: swap format pagesize does not match. "
593 "(Use --fixpgsz to reinitialize it.)"),
6c44094a 594 dev->path);
e8be80dd
KZ
595 }
596 } else if (sig == SIG_SWSUSPEND) {
597 /* We have to reinitialize swap with old (=useless) software suspend
598 * data. The problem is that if we don't do it, then we get data
599 * corruption the next time an attempt at unsuspending is made.
600 */
d51d05d3 601 warnx(_("%s: software suspend data detected. "
617d8a3b 602 "Rewriting the swap signature."),
6c44094a
KZ
603 dev->path);
604 if (swap_rewrite_signature(dev) < 0)
05c79b78 605 goto err;
6274b987
KC
606 }
607
05c79b78
KZ
608 free(hdr);
609 close(fd);
610 return 0;
05c79b78
KZ
611err:
612 if (fd != -1)
613 close(fd);
614 free(hdr);
615 return -1;
616}
63cccae4 617
6c44094a
KZ
618static int do_swapon(const struct swapon_ctl *ctl,
619 const struct swap_prop *prop,
620 const char *spec,
621 int canonic)
d335c9bd 622{
6c44094a 623 struct swap_device dev = { .path = NULL };
05c79b78 624 int status;
05c79b78 625 int flags = 0;
6c44094a 626 int priority;
63cccae4 627
6c44094a
KZ
628 assert(ctl);
629 assert(prop);
05c79b78 630
a125123f 631 if (!canonic) {
6c44094a
KZ
632 dev.path = mnt_resolve_spec(spec, mntcache);
633 if (!dev.path)
a125123f 634 return cannot_find(spec);
2dd3ba2c 635 } else
6c44094a 636 dev.path = spec;
ffa63a3d 637
6c44094a
KZ
638 priority = prop->priority;
639
640 if (swapon_checks(ctl, &dev))
05c79b78
KZ
641 return -1;
642
643#ifdef SWAP_FLAG_PREFER
6c44094a
KZ
644 if (priority >= 0) {
645 if (priority > SWAP_FLAG_PRIO_MASK)
646 priority = SWAP_FLAG_PRIO_MASK;
647
05c79b78 648 flags = SWAP_FLAG_PREFER
6c44094a 649 | ((priority & SWAP_FLAG_PRIO_MASK)
05c79b78
KZ
650 << SWAP_FLAG_PRIO_SHIFT);
651 }
652#endif
d6387c98
RA
653 /*
654 * Validate the discard flags passed and set them
655 * accordingly before calling sys_swapon.
656 */
6c44094a 657 if (prop->discard && !(prop->discard & ~SWAP_FLAGS_DISCARD_VALID)) {
d6387c98
RA
658 /*
659 * If we get here with both discard policy flags set,
660 * we just need to tell the kernel to enable discards
661 * and it will do correctly, just as we expect.
662 */
6c44094a
KZ
663 if ((prop->discard & SWAP_FLAG_DISCARD_ONCE) &&
664 (prop->discard & SWAP_FLAG_DISCARD_PAGES))
d6387c98
RA
665 flags |= SWAP_FLAG_DISCARD;
666 else
6c44094a 667 flags |= prop->discard;
d6387c98 668 }
c2301380 669
6c44094a
KZ
670 if (ctl->verbose)
671 printf(_("swapon %s\n"), dev.path);
672
673 status = swapon(dev.path, flags);
d51d05d3 674 if (status < 0)
6c44094a 675 warn(_("%s: swapon failed"), dev.path);
63cccae4
KZ
676
677 return status;
678}
679
a125123f 680static int swapon_by_label(struct swapon_ctl *ctl, const char *label)
d335c9bd 681{
a125123f 682 char *device = mnt_resolve_tag("LABEL", label, mntcache);
6c44094a 683 return device ? do_swapon(ctl, &ctl->props, device, TRUE) : cannot_find(label);
756bfd01
KZ
684}
685
a125123f 686static int swapon_by_uuid(struct swapon_ctl *ctl, const char *uuid)
d335c9bd 687{
a125123f 688 char *device = mnt_resolve_tag("UUID", uuid, mntcache);
6c44094a 689 return device ? do_swapon(ctl, &ctl->props, device, TRUE) : cannot_find(uuid);
756bfd01
KZ
690}
691
e07fdac4 692/* -o <options> or fstab */
6c44094a 693static int parse_options(struct swap_prop *props, const char *options)
e07fdac4
KZ
694{
695 char *arg = NULL;
07332bfa 696 size_t argsz = 0;
e07fdac4 697
6c44094a
KZ
698 assert(props);
699 assert(options);
e07fdac4 700
87918040 701 if (mnt_optstr_get_option(options, "nofail", NULL, NULL) == 0)
6c44094a 702 props->no_fail = 1;
e07fdac4 703
07332bfa 704 if (mnt_optstr_get_option(options, "discard", &arg, &argsz) == 0) {
6c44094a 705 props->discard |= SWAP_FLAG_DISCARD;
e07fdac4
KZ
706
707 if (arg) {
708 /* only single-time discards are wanted */
07332bfa 709 if (strncmp(arg, "once", argsz) == 0)
6c44094a 710 props->discard |= SWAP_FLAG_DISCARD_ONCE;
e07fdac4
KZ
711
712 /* do discard for every released swap page */
07332bfa 713 if (strncmp(arg, "pages", argsz) == 0)
6c44094a
KZ
714 props->discard |= SWAP_FLAG_DISCARD_PAGES;
715 }
e07fdac4
KZ
716 }
717
718 arg = NULL;
6c44094a
KZ
719 if (mnt_optstr_get_option(options, "pri", &arg, NULL) == 0 && arg)
720 props->priority = atoi(arg);
e07fdac4
KZ
721
722 return 0;
723}
724
725
9eeba70e 726static int swapon_all(struct swapon_ctl *ctl)
d335c9bd 727{
5072b90a
KZ
728 struct libmnt_table *tb = get_fstab();
729 struct libmnt_iter *itr;
730 struct libmnt_fs *fs;
63cccae4 731 int status = 0;
63cccae4 732
5072b90a 733 if (!tb)
d335c9bd 734 err(EXIT_FAILURE, _("failed to parse %s"), mnt_get_fstab_path());
756bfd01 735
5072b90a
KZ
736 itr = mnt_new_iter(MNT_ITER_FORWARD);
737 if (!itr)
738 err(EXIT_FAILURE, _("failed to initialize libmount iterator"));
756bfd01 739
5072b90a
KZ
740 while (mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) {
741 /* defaults */
2dd3ba2c 742 const char *opts;
a125123f 743 const char *device;
6c44094a 744 struct swap_prop prop; /* per device setting */
1b414d89 745
6c44094a
KZ
746 if (mnt_fs_get_option(fs, "noauto", NULL, NULL) == 0) {
747 if (ctl->verbose)
748 warnx(_("%s: noauto option -- ignored"), mnt_fs_get_source(fs));
1b414d89 749 continue;
6c44094a
KZ
750 }
751
752 /* default setting */
753 prop = ctl->props;
e07fdac4 754
6c44094a 755 /* overwrite default by setting from fstab */
e07fdac4
KZ
756 opts = mnt_fs_get_options(fs);
757 if (opts)
6c44094a 758 parse_options(&prop, opts);
5072b90a 759
6c44094a 760 /* convert LABEL=, UUID= etc. from fstab to device name */
a125123f
KZ
761 device = mnt_resolve_spec(mnt_fs_get_source(fs), mntcache);
762 if (!device) {
6c44094a 763 if (!prop.no_fail)
5072b90a 764 status |= cannot_find(mnt_fs_get_source(fs));
756bfd01 765 continue;
1b414d89 766 }
756bfd01 767
6c44094a
KZ
768 if (is_active_swap(device)) {
769 if (ctl->verbose)
770 warnx(_("%s: already active -- ignored"), device);
771 continue;
772 }
773
774 if (prop.no_fail && access(device, R_OK) != 0) {
775 if (ctl->verbose)
9e930041 776 warnx(_("%s: inaccessible -- ignored"), device);
6c44094a
KZ
777 continue;
778 }
779
780 /* swapon */
781 status |= do_swapon(ctl, &prop, device, TRUE);
756bfd01 782 }
756bfd01 783
5072b90a 784 mnt_free_iter(itr);
756bfd01
KZ
785 return status;
786}
787
e07fdac4 788
6e1eda6f 789static void __attribute__((__noreturn__)) usage(void)
8b0d5341 790{
6e1eda6f 791 FILE *out = stdout;
e09bd8d0 792 size_t i;
6e1eda6f 793
8b0d5341 794 fputs(USAGE_HEADER, out);
8b0d5341
KZ
795 fprintf(out, _(" %s [options] [<spec>]\n"), program_invocation_short_name);
796
451dbcfa
BS
797 fputs(USAGE_SEPARATOR, out);
798 fputs(_("Enable devices and files for paging and swapping.\n"), out);
799
8b0d5341 800 fputs(USAGE_OPTIONS, out);
6ee8e194
BS
801 fputs(_(" -a, --all enable all swaps from /etc/fstab\n"), out);
802 fputs(_(" -d, --discard[=<policy>] enable swap discards, if supported by device\n"), out);
803 fputs(_(" -e, --ifexists silently skip devices that do not exist\n"), out);
804 fputs(_(" -f, --fixpgsz reinitialize the swap space if necessary\n"), out);
805 fputs(_(" -o, --options <list> comma-separated list of swap options\n"), out);
806 fputs(_(" -p, --priority <prio> specify the priority of the swap device\n"), out);
807 fputs(_(" -s, --summary display summary about used swap devices (DEPRECATED)\n"), out);
808 fputs(_(" --show[=<columns>] display summary in definable table\n"), out);
809 fputs(_(" --noheadings don't print table heading (with --show)\n"), out);
810 fputs(_(" --raw use the raw output format (with --show)\n"), out);
811 fputs(_(" --bytes display swap size in bytes in --show output\n"), out);
812 fputs(_(" -v, --verbose verbose mode\n"), out);
8b0d5341
KZ
813
814 fputs(USAGE_SEPARATOR, out);
f45f3ec3 815 printf(USAGE_HELP_OPTIONS(26));
8b0d5341
KZ
816
817 fputs(_("\nThe <spec> parameter:\n" \
9f9d0f21
KZ
818 " -L <label> synonym for LABEL=<label>\n"
819 " -U <uuid> synonym for UUID=<uuid>\n"
820 " LABEL=<label> specifies device by swap area label\n"
821 " UUID=<uuid> specifies device by swap area UUID\n"
822 " PARTLABEL=<label> specifies device by partition label\n"
823 " PARTUUID=<uuid> specifies device by partition UUID\n"
824 " <device> name of device to be used\n"
8b0d5341
KZ
825 " <file> name of file to be used\n"), out);
826
d6387c98 827 fputs(_("\nAvailable discard policy types (for --discard):\n"
54fefa07
BS
828 " once : only single-time area discards are issued\n"
829 " pages : freed pages are discarded before they are reused\n"
830 "If no policy is selected, both discard types are enabled (default).\n"), out);
d6387c98 831
c3a4cfc5 832 fputs(USAGE_COLUMNS, out);
a560a348 833 for (i = 0; i < ARRAY_SIZE(infos); i++)
a12124ce 834 fprintf(out, " %-5s %s\n", infos[i].name, _(infos[i].help));
e076b552 835
f45f3ec3 836 printf(USAGE_MAN_TAIL("swapon(8)"));
6e1eda6f 837 exit(EXIT_SUCCESS);
8b0d5341
KZ
838}
839
d9f07b10
KZ
840int main(int argc, char *argv[])
841{
8b0d5341 842 int status = 0, c;
0b0c231f 843 size_t i;
6c44094a 844 char *options = NULL;
756bfd01 845
e076b552 846 enum {
e07fdac4 847 BYTES_OPTION = CHAR_MAX + 1,
7ae8b469 848 NOHEADINGS_OPTION,
e07fdac4
KZ
849 RAW_OPTION,
850 SHOW_OPTION
e076b552
SK
851 };
852
8b0d5341 853 static const struct option long_opts[] = {
87918040
SK
854 { "priority", required_argument, NULL, 'p' },
855 { "discard", optional_argument, NULL, 'd' },
856 { "ifexists", no_argument, NULL, 'e' },
857 { "options", optional_argument, NULL, 'o' },
858 { "summary", no_argument, NULL, 's' },
859 { "fixpgsz", no_argument, NULL, 'f' },
860 { "all", no_argument, NULL, 'a' },
861 { "help", no_argument, NULL, 'h' },
862 { "verbose", no_argument, NULL, 'v' },
863 { "version", no_argument, NULL, 'V' },
864 { "show", optional_argument, NULL, SHOW_OPTION },
865 { "noheadings", no_argument, NULL, NOHEADINGS_OPTION },
866 { "raw", no_argument, NULL, RAW_OPTION },
867 { "bytes", no_argument, NULL, BYTES_OPTION },
868 { NULL, 0, NULL, 0 }
8b0d5341
KZ
869 };
870
a7349ee3 871 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
e07fdac4
KZ
872 { 'a','o','s', SHOW_OPTION },
873 { 'a','o', BYTES_OPTION },
874 { 'a','o', NOHEADINGS_OPTION },
875 { 'a','o', RAW_OPTION },
876 { 0 }
877 };
878 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
879
6c44094a 880 struct swapon_ctl ctl;
9eeba70e 881
d9f07b10
KZ
882 setlocale(LC_ALL, "");
883 bindtextdomain(PACKAGE, LOCALEDIR);
884 textdomain(PACKAGE);
885 atexit(close_stdout);
886
6c44094a
KZ
887 memset(&ctl, 0, sizeof(struct swapon_ctl));
888 ctl.props.priority = -1;
889
d9f07b10
KZ
890 mnt_init_debug(0);
891 mntcache = mnt_new_cache();
892
e07fdac4 893 while ((c = getopt_long(argc, argv, "ahd::efo:p:svVL:U:",
8b0d5341 894 long_opts, NULL)) != -1) {
e07fdac4
KZ
895
896 err_exclusive_options(c, long_opts, excl, excl_st);
897
63cccae4
KZ
898 switch (c) {
899 case 'a': /* all */
9eeba70e 900 ctl.all = 1;
63cccae4
KZ
901 break;
902 case 'h': /* help */
6e1eda6f 903 usage();
63cccae4 904 break;
e07fdac4 905 case 'o':
6c44094a 906 options = optarg;
e07fdac4 907 break;
63cccae4 908 case 'p': /* priority */
6c44094a 909 ctl.props.priority = strtos16_or_err(optarg,
9db6a749 910 _("failed to parse priority"));
63cccae4 911 break;
756bfd01 912 case 'L':
0b0c231f 913 add_label(optarg);
756bfd01
KZ
914 break;
915 case 'U':
0b0c231f 916 add_uuid(optarg);
756bfd01 917 break;
c2301380 918 case 'd':
6c44094a 919 ctl.props.discard |= SWAP_FLAG_DISCARD;
d6387c98
RA
920 if (optarg) {
921 if (*optarg == '=')
922 optarg++;
923
924 if (strcmp(optarg, "once") == 0)
6c44094a 925 ctl.props.discard |= SWAP_FLAG_DISCARD_ONCE;
d6387c98 926 else if (strcmp(optarg, "pages") == 0)
6c44094a 927 ctl.props.discard |= SWAP_FLAG_DISCARD_PAGES;
d6387c98
RA
928 else
929 errx(EXIT_FAILURE, _("unsupported discard policy: %s"), optarg);
930 }
c2301380 931 break;
95f1bdee 932 case 'e': /* ifexists */
6c44094a 933 ctl.props.no_fail = 1;
95f1bdee 934 break;
0d761944 935 case 'f':
9eeba70e 936 ctl.fix_page_size = 1;
0d761944 937 break;
63cccae4
KZ
938 case 's': /* status report */
939 status = display_summary();
8b0d5341 940 return status;
63cccae4 941 case 'v': /* be chatty */
9eeba70e 942 ctl.verbose = 1;
63cccae4 943 break;
e076b552
SK
944 case SHOW_OPTION:
945 if (optarg) {
9eeba70e
SK
946 ctl.ncolumns = string_to_idarray(optarg,
947 ctl.columns,
948 ARRAY_SIZE(ctl.columns),
e076b552 949 column_name_to_id);
9eeba70e 950 if (ctl.ncolumns < 0)
e076b552
SK
951 return EXIT_FAILURE;
952 }
9eeba70e 953 ctl.show = 1;
e076b552
SK
954 break;
955 case NOHEADINGS_OPTION:
9eeba70e 956 ctl.no_heading = 1;
e076b552
SK
957 break;
958 case RAW_OPTION:
9eeba70e 959 ctl.raw = 1;
e076b552 960 break;
7ae8b469 961 case BYTES_OPTION:
9eeba70e 962 ctl.bytes = 1;
7ae8b469 963 break;
63cccae4 964 case 'V': /* version */
d9f07b10
KZ
965 printf(UTIL_LINUX_VERSION);
966 return EXIT_SUCCESS;
63cccae4
KZ
967 case 0:
968 break;
63cccae4 969 default:
677ec86c 970 errtryhelp(EXIT_FAILURE);
63cccae4
KZ
971 }
972 }
973 argv += optind;
974
9eeba70e
SK
975 if (ctl.show || (!ctl.all && !numof_labels() && !numof_uuids() && *argv == NULL)) {
976 if (!ctl.ncolumns) {
e076b552 977 /* default columns */
9eeba70e
SK
978 ctl.columns[ctl.ncolumns++] = COL_PATH;
979 ctl.columns[ctl.ncolumns++] = COL_TYPE;
980 ctl.columns[ctl.ncolumns++] = COL_SIZE;
981 ctl.columns[ctl.ncolumns++] = COL_USED;
982 ctl.columns[ctl.ncolumns++] = COL_PRIO;
e076b552 983 }
9eeba70e 984 status = show_table(&ctl);
e076b552
SK
985 return status;
986 }
987
6e1eda6f
RM
988 if (ctl.props.no_fail && !ctl.all) {
989 warnx(_("bad usage"));
990 errtryhelp(EXIT_FAILURE);
991 }
95f1bdee 992
9eeba70e
SK
993 if (ctl.all)
994 status |= swapon_all(&ctl);
63cccae4 995
6c44094a
KZ
996 if (options)
997 parse_options(&ctl.props, options);
e07fdac4 998
a125123f
KZ
999 for (i = 0; i < numof_labels(); i++)
1000 status |= swapon_by_label(&ctl, get_label(i));
63cccae4 1001
a125123f
KZ
1002 for (i = 0; i < numof_uuids(); i++)
1003 status |= swapon_by_uuid(&ctl, get_uuid(i));
1004
1005 while (*argv != NULL)
6c44094a 1006 status |= do_swapon(&ctl, &ctl.props, *argv++, FALSE);
63cccae4 1007
0b0c231f 1008 free_tables();
6195f9e6 1009 mnt_unref_cache(mntcache);
d9f07b10 1010
64b58881 1011 return status;
6dbe3af9 1012}