2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/mdioctl.h>
50 #include <sys/sysctl.h>
52 #include <vm/vm_param.h>
69 static void usage(void);
70 static const char *swap_on_off(const char *, int, char *);
71 static const char *swap_on_off_gbde(const char *, int);
72 static const char *swap_on_off_geli(const char *, char *, int);
73 static const char *swap_on_off_md(const char *, char *, int);
74 static const char *swap_on_off_sfile(const char *, int);
75 static void swaplist(int, int, int);
76 static int run_cmd(int *, const char *, ...) __printflike(2, 3);
78 static enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
83 main(int argc, char **argv)
89 int sflag, lflag, late, hflag;
90 const char *etc_fstab;
92 sflag = lflag = late = hflag = 0;
93 if ((ptr = strrchr(argv[0], '/')) == NULL)
95 if (strstr(ptr, "swapon") != NULL)
97 else if (strstr(ptr, "swapoff") != NULL)
99 orig_prog = which_prog;
103 while ((ch = getopt(argc, argv, "AadghklLmqsUF:")) != -1) {
106 if (which_prog == SWAPCTL) {
113 if (which_prog == SWAPON || which_prog == SWAPOFF)
119 if (which_prog == SWAPCTL)
120 which_prog = SWAPOFF;
143 if (which_prog == SWAPON || which_prog == SWAPOFF)
150 if (which_prog == SWAPCTL) {
152 which_prog = SWAPOFF;
168 if (etc_fstab != NULL)
170 if (which_prog == SWAPON || which_prog == SWAPOFF) {
172 while ((fsp = getfsent()) != NULL) {
173 if (strcmp(fsp->fs_type, FSTAB_SW) != 0)
175 if (strstr(fsp->fs_mntops, "noauto") != NULL)
177 if (which_prog != SWAPOFF &&
178 strstr(fsp->fs_mntops, "late") &&
181 if (which_prog == SWAPOFF &&
182 strstr(fsp->fs_mntops, "late") == NULL &&
185 swfile = swap_on_off(fsp->fs_spec, 1,
187 if (swfile == NULL) {
192 printf("%s: %sing %s as swap device\n",
194 (which_prog == SWAPOFF) ?
195 "remov" : "add", swfile);
198 } else if (*argv == NULL)
200 for (; *argv; ++argv) {
201 swfile = swap_on_off(*argv, 0, NULL);
202 if (swfile == NULL) {
206 if (orig_prog == SWAPCTL) {
207 printf("%s: %sing %s as swap device\n",
209 (which_prog == SWAPOFF) ? "remov" : "add",
215 swaplist(lflag, sflag, hflag);
223 swap_on_off(const char *name, int doingall, char *mntops)
225 char *base, *basebuf;
227 /* Swap on vnode-backed md(4) device. */
228 if (mntops != NULL &&
229 (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) == 0 ||
230 fnmatch(MD_NAME "[0-9]*", name, 0) == 0 ||
231 strncmp(_PATH_DEV MD_NAME, name,
232 sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 ||
233 strncmp(MD_NAME, name, sizeof(MD_NAME)) == 0))
234 return (swap_on_off_md(name, mntops, doingall));
236 basebuf = strdup(name);
237 base = basename(basebuf);
239 /* Swap on encrypted device by GEOM_BDE. */
240 if (fnmatch("*.bde", base, 0) == 0) {
242 return (swap_on_off_gbde(name, doingall));
245 /* Swap on encrypted device by GEOM_ELI. */
246 if (fnmatch("*.eli", base, 0) == 0) {
248 return (swap_on_off_geli(name, mntops, doingall));
251 /* Swap on special file. */
253 return (swap_on_off_sfile(name, doingall));
256 /* Strip off .bde or .eli suffix from swap device name */
258 swap_basename(const char *name)
262 dname = strdup(name);
263 p = strrchr(dname, '.');
264 /* assert(p != NULL); */
271 swap_on_off_gbde(const char *name, int doingall)
274 char pass[64 * 2 + 1];
275 unsigned char bpass[64];
279 dname = swap_basename(name);
283 if (which_prog == SWAPON) {
284 arc4random_buf(bpass, sizeof(bpass));
285 for (i = 0; i < (int)sizeof(bpass); i++)
286 sprintf(&pass[2 * i], "%02x", bpass[i]);
287 pass[sizeof(pass) - 1] = '\0';
289 error = run_cmd(NULL, "%s init %s -P %s", _PATH_GBDE,
292 /* bde device found. Ignore it. */
295 warnx("%s: Device already in use", name);
298 error = run_cmd(NULL, "%s attach %s -p %s", _PATH_GBDE,
302 warnx("gbde (attach) error: %s", name);
307 ret = swap_on_off_sfile(name, doingall);
309 if (which_prog == SWAPOFF) {
310 error = run_cmd(NULL, "%s detach %s", _PATH_GBDE, dname);
313 /* bde device not found. Ignore it. */
315 warnx("%s: Device not found", name);
323 /* Build geli(8) arguments from mntops */
325 swap_on_geli_args(const char *mntops)
327 const char *aalgo, *ealgo, *keylen_str, *sectorsize_str;
328 const char *aflag, *eflag, *lflag, *Tflag, *sflag;
329 char *p, *args, *token, *string, *ops;
334 /* Use built-in defaults for geli(8). */
335 aalgo = ealgo = keylen_str = "";
336 aflag = eflag = lflag = Tflag = "";
338 /* We will always specify sectorsize. */
340 sectorsize_str = NULL;
342 if (mntops != NULL) {
343 string = ops = strdup(mntops);
345 while ((token = strsep(&string, ",")) != NULL) {
346 if ((p = strstr(token, "aalgo=")) == token) {
347 aalgo = p + sizeof("aalgo=") - 1;
349 } else if ((p = strstr(token, "ealgo=")) == token) {
350 ealgo = p + sizeof("ealgo=") - 1;
352 } else if ((p = strstr(token, "keylen=")) == token) {
353 keylen_str = p + sizeof("keylen=") - 1;
355 ul = strtoul(keylen_str, &p, 10);
357 if (*p != '\0' || ul > INT_MAX)
361 warn("Invalid keylen: %s", keylen_str);
366 } else if ((p = strstr(token, "sectorsize=")) == token) {
367 sectorsize_str = p + sizeof("sectorsize=") - 1;
369 ul = strtoul(sectorsize_str, &p, 10);
371 if (*p != '\0' || ul > INT_MAX)
375 warn("Invalid sectorsize: %s",
380 } else if (strcmp(token, "notrim") == 0) {
382 } else if (strcmp(token, "late") == 0) {
383 /* ignore known option */
384 } else if (strcmp(token, "noauto") == 0) {
385 /* ignore known option */
386 } else if (strcmp(token, "sw") != 0) {
387 warnx("Invalid option: %s", token);
396 * If we do not have a sector size at this point, fill in
397 * pagesize as sector size.
399 if (sectorsize_str == NULL) {
400 /* Use pagesize as default sectorsize. */
401 pagesize = getpagesize();
402 pagesize_len = snprintf(NULL, 0, "%d", pagesize) + 1;
403 p = alloca(pagesize_len);
404 snprintf(p, pagesize_len, "%d", pagesize);
408 (void)asprintf(&args, "%s%s%s%s%s%s%s%s%s -d",
409 aflag, aalgo, eflag, ealgo, lflag, keylen_str, Tflag,
410 sflag, sectorsize_str);
417 swap_on_off_geli(const char *name, char *mntops, int doingall)
423 error = stat(name, &sb);
425 if (which_prog == SWAPON) do {
426 /* Skip if the .eli device already exists. */
430 args = swap_on_geli_args(mntops);
434 dname = swap_basename(name);
440 error = run_cmd(NULL, "%s onetime%s %s", _PATH_GELI, args,
447 /* error occurred during creation. */
449 warnx("%s: Invalid parameters", name);
454 return (swap_on_off_sfile(name, doingall));
458 swap_on_off_md(const char *name, char *mntops, int doingall)
461 int fd, mdunit, error;
463 static char mdpath[PATH_MAX], linebuf[PATH_MAX];
470 if (strlen(name) == (sizeof(MD_NAME) - 1))
474 ul = strtoul(name + 2, &p, 10);
476 if (*p != '\0' || ul > INT_MAX)
480 warn("Bad device unit: %s", name);
487 if ((p = strstr(mntops, "file=")) != NULL) {
488 vnodefile = strdup(p + sizeof("file=") - 1);
489 p = strchr(vnodefile, ',');
493 if (vnodefile == NULL) {
494 warnx("file option not found for %s", name);
498 if (which_prog == SWAPON) {
500 error = run_cmd(&fd, "%s -l -n -f %s",
501 _PATH_MDCONFIG, vnodefile);
503 /* md device found. Ignore it. */
506 warnx("%s: Device already in use",
511 error = run_cmd(&fd, "%s -a -t vnode -n -f %s",
512 _PATH_MDCONFIG, vnodefile);
514 warnx("mdconfig (attach) error: file=%s",
519 sfd = fdopen(fd, "r");
521 warn("mdconfig (attach) fdopen error");
525 p = fgetln(sfd, &linelen);
527 (linelen < 2 || linelen > sizeof(linebuf))) {
528 warn("mdconfig (attach) unexpected output");
532 strncpy(linebuf, p, linelen);
533 linebuf[linelen - 1] = '\0';
535 ul = strtoul(linebuf, &p, 10);
537 if (*p != '\0' || ul > INT_MAX)
541 warn("mdconfig (attach) unexpected output: %s",
548 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
549 _PATH_MDCONFIG, vnodefile, mdunit);
551 /* md device found. Ignore it. */
554 warnx("md%d on %s: Device already "
555 "in use", mdunit, vnodefile);
559 error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s",
560 _PATH_MDCONFIG, mdunit, vnodefile);
562 warnx("mdconfig (attach) error: "
563 "md%d on file=%s", mdunit, vnodefile);
568 } else /* SWAPOFF */ {
570 error = run_cmd(&fd, "%s -l -n -f %s",
571 _PATH_MDCONFIG, vnodefile);
573 /* md device not found. Ignore it. */
576 warnx("md on %s: Device not found",
581 sfd = fdopen(fd, "r");
583 warn("mdconfig (list) fdopen error");
587 p = fgetln(sfd, &linelen);
589 (linelen < 2 || linelen > sizeof(linebuf) - 1)) {
590 warn("mdconfig (list) unexpected output");
594 strncpy(linebuf, p, linelen);
595 linebuf[linelen - 1] = '\0';
596 p = strchr(linebuf, ' ');
600 ul = strtoul(linebuf, &p, 10);
602 if (*p != '\0' || ul > INT_MAX)
606 warn("mdconfig (list) unexpected output: %s",
613 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
614 _PATH_MDCONFIG, vnodefile, mdunit);
616 /* md device not found. Ignore it. */
619 warnx("md%d on %s: Device not found",
626 snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
628 mdpath[sizeof(mdpath) - 1] = '\0';
629 ret = swap_on_off_sfile(mdpath, doingall);
631 if (which_prog == SWAPOFF) {
633 error = run_cmd(NULL, "%s -d -u %d",
634 _PATH_MDCONFIG, mdunit);
636 warn("mdconfig (detach) detach failed: %s%s%d",
637 _PATH_DEV, MD_NAME, mdunit);
650 run_cmd(int *ofd, const char *cmdline, ...)
653 char **argv, **argvp, *cmd, *p;
654 int argc, pid, status, rv;
655 int pfd[2], nfd, dup2dn;
657 va_start(ap, cmdline);
658 rv = vasprintf(&cmd, cmdline, ap);
660 warn("%s", __func__);
666 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
668 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
669 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
670 if (**argvp != '\0' && (++argvp > &argv[argc])) {
674 /* The argv array ends up NULL-terminated here. */
679 fprintf(stderr, "DEBUG: running:");
680 /* Should be equivalent to 'cmd' (before strsep, of course). */
681 for (i = 0; argv[i] != NULL; i++)
682 fprintf(stderr, " %s", argv[i]);
683 fprintf(stderr, "\n");
688 if (pipe(&pfd[0]) == -1) {
689 warn("%s: pipe", __func__);
700 if (dup2(pfd[1], STDOUT_FILENO) < 0)
701 err(1, "dup2 in %s", __func__);
702 nfd = open(_PATH_DEVNULL, O_RDWR);
704 err(1, "%s: open %s", __func__, _PATH_DEVNULL);
705 if (dup2(nfd, STDIN_FILENO) < 0)
706 err(1, "%s: dup2", __func__);
707 if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0)
708 err(1, "%s: dup2", __func__);
709 if (dup2(nfd, STDERR_FILENO) < 0)
710 err(1, "%s: dup2", __func__);
711 execv(argv[0], argv);
712 warn("exec: %s", argv[0]);
715 err(1, "%s: fork", __func__);
719 while (waitpid(pid, &status, 0) != pid)
721 return (WEXITSTATUS(status));
725 swap_on_off_sfile(const char *name, int doingall)
729 if (which_prog == SWAPON)
730 error = swapon(name);
732 error = swapoff(name);
738 warnx("%s: Device already in use", name);
741 if (which_prog == SWAPON)
742 warnx("%s: NSWAPDEV limit reached", name);
743 else if (doingall == 0)
759 fprintf(stderr, "usage: %s ", getprogname());
763 fprintf(stderr, "[-F fstab] -aLq | file ...\n");
766 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
773 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
779 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
780 HN_B | HN_NOSPACE | HN_DECIMAL);
781 snprintf(buf, bufsize, "%*s", hlen, tmp);
783 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
787 swaplist(int lflag, int sflag, int hflag)
789 size_t mibsize, size;
791 int hlen, mib[16], n, pagesize;
799 pagesize = getpagesize();
802 blocksize = 1024 * 1024 * 1024;
803 strlcpy(buf, "1GB-blocks", sizeof(buf));
808 strlcpy(buf, "Bytes", sizeof(buf));
813 strlcpy(buf, "1kB-blocks", sizeof(buf));
817 blocksize = 1024 * 1024;
818 strlcpy(buf, "1MB-blocks", sizeof(buf));
822 getbsize(&hlen, &blocksize);
823 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
827 mibsize = nitems(mib);
828 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
829 err(1, "sysctlnametomib()");
832 printf("%-13s %*s %*s\n",
841 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
843 if (xsw.xsw_version != XSWDEV_VERSION)
844 errx(1, "xswdev version mismatch");
846 tmp_total = (long long)xsw.xsw_nblks * pagesize;
847 tmp_used = (long long)xsw.xsw_used * pagesize;
851 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
853 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
855 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
864 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
865 printf("Total: %s ", buf);
866 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);