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