2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1980, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)swapon.c 8.1 (Berkeley) 6/5/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/mdioctl.h>
48 #include <sys/sysctl.h>
50 #include <vm/vm_param.h>
67 static void usage(void);
68 static const char *swap_on_off(const char *, int, char *);
69 static const char *swap_on_off_gbde(const char *, int);
70 static const char *swap_on_off_geli(const char *, char *, int);
71 static const char *swap_on_off_md(const char *, char *, int);
72 static const char *swap_on_off_sfile(const char *, int);
73 static void swaplist(int, int, int);
74 static int run_cmd(int *, const char *, ...) __printflike(2, 3);
76 static enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
81 main(int argc, char **argv)
87 int sflag, lflag, late, hflag;
88 const char *etc_fstab;
90 sflag = lflag = late = hflag = 0;
91 if ((ptr = strrchr(argv[0], '/')) == NULL)
93 if (strstr(ptr, "swapon") != NULL)
95 else if (strstr(ptr, "swapoff") != NULL)
97 orig_prog = which_prog;
101 while ((ch = getopt(argc, argv, "AadghklLmqsUF:")) != -1) {
104 if (which_prog == SWAPCTL) {
111 if (which_prog == SWAPON || which_prog == SWAPOFF)
117 if (which_prog == SWAPCTL)
118 which_prog = SWAPOFF;
141 if (which_prog == SWAPON || which_prog == SWAPOFF)
148 if (which_prog == SWAPCTL) {
150 which_prog = SWAPOFF;
166 if (etc_fstab != NULL)
168 if (which_prog == SWAPON || which_prog == SWAPOFF) {
170 while ((fsp = getfsent()) != NULL) {
171 if (strcmp(fsp->fs_type, FSTAB_SW) != 0)
173 if (strstr(fsp->fs_mntops, "noauto") != NULL)
175 if (which_prog != SWAPOFF &&
176 strstr(fsp->fs_mntops, "late") &&
179 if (which_prog == SWAPOFF &&
180 strstr(fsp->fs_mntops, "late") == NULL &&
183 swfile = swap_on_off(fsp->fs_spec, 1,
185 if (swfile == NULL) {
190 printf("%s: %sing %s as swap device\n",
192 (which_prog == SWAPOFF) ?
193 "remov" : "add", swfile);
196 } else if (*argv == NULL)
198 for (; *argv; ++argv) {
199 swfile = swap_on_off(*argv, 0, NULL);
200 if (swfile == NULL) {
204 if (orig_prog == SWAPCTL) {
205 printf("%s: %sing %s as swap device\n",
207 (which_prog == SWAPOFF) ? "remov" : "add",
213 swaplist(lflag, sflag, hflag);
221 swap_on_off(const char *name, int doingall, char *mntops)
223 char *base, *basebuf;
225 /* Swap on vnode-backed md(4) device. */
226 if (mntops != NULL &&
227 (fnmatch(_PATH_DEV MD_NAME "[0-9]*", name, 0) == 0 ||
228 fnmatch(MD_NAME "[0-9]*", name, 0) == 0 ||
229 strncmp(_PATH_DEV MD_NAME, name,
230 sizeof(_PATH_DEV) + sizeof(MD_NAME)) == 0 ||
231 strncmp(MD_NAME, name, sizeof(MD_NAME)) == 0))
232 return (swap_on_off_md(name, mntops, doingall));
234 basebuf = strdup(name);
235 base = basename(basebuf);
237 /* Swap on encrypted device by GEOM_BDE. */
238 if (fnmatch("*.bde", base, 0) == 0) {
240 return (swap_on_off_gbde(name, doingall));
243 /* Swap on encrypted device by GEOM_ELI. */
244 if (fnmatch("*.eli", base, 0) == 0) {
246 return (swap_on_off_geli(name, mntops, doingall));
249 /* Swap on special file. */
251 return (swap_on_off_sfile(name, doingall));
254 /* Strip off .bde or .eli suffix from swap device name */
256 swap_basename(const char *name)
260 dname = strdup(name);
261 p = strrchr(dname, '.');
262 /* assert(p != NULL); */
269 swap_on_off_gbde(const char *name, int doingall)
272 char pass[64 * 2 + 1];
273 unsigned char bpass[64];
277 dname = swap_basename(name);
281 if (which_prog == SWAPON) {
282 arc4random_buf(bpass, sizeof(bpass));
283 for (i = 0; i < (int)sizeof(bpass); i++)
284 sprintf(&pass[2 * i], "%02x", bpass[i]);
285 pass[sizeof(pass) - 1] = '\0';
287 error = run_cmd(NULL, "%s init %s -P %s", _PATH_GBDE,
290 /* bde device found. Ignore it. */
293 warnx("%s: Device already in use", name);
296 error = run_cmd(NULL, "%s attach %s -p %s", _PATH_GBDE,
300 warnx("gbde (attach) error: %s", name);
305 ret = swap_on_off_sfile(name, doingall);
307 if (which_prog == SWAPOFF) {
308 error = run_cmd(NULL, "%s detach %s", _PATH_GBDE, dname);
311 /* bde device not found. Ignore it. */
313 warnx("%s: Device not found", name);
321 /* Build geli(8) arguments from mntops */
323 swap_on_geli_args(const char *mntops)
325 const char *aalgo, *ealgo, *keylen_str, *sectorsize_str;
326 const char *aflag, *eflag, *lflag, *Tflag, *sflag;
327 char *p, *args, *token, *string, *ops;
332 /* Use built-in defaults for geli(8). */
333 aalgo = ealgo = keylen_str = "";
334 aflag = eflag = lflag = Tflag = "";
336 /* We will always specify sectorsize. */
338 sectorsize_str = NULL;
340 if (mntops != NULL) {
341 string = ops = strdup(mntops);
343 while ((token = strsep(&string, ",")) != NULL) {
344 if ((p = strstr(token, "aalgo=")) == token) {
345 aalgo = p + sizeof("aalgo=") - 1;
347 } else if ((p = strstr(token, "ealgo=")) == token) {
348 ealgo = p + sizeof("ealgo=") - 1;
350 } else if ((p = strstr(token, "keylen=")) == token) {
351 keylen_str = p + sizeof("keylen=") - 1;
353 ul = strtoul(keylen_str, &p, 10);
355 if (*p != '\0' || ul > INT_MAX)
359 warn("Invalid keylen: %s", keylen_str);
364 } else if ((p = strstr(token, "sectorsize=")) == token) {
365 sectorsize_str = p + sizeof("sectorsize=") - 1;
367 ul = strtoul(sectorsize_str, &p, 10);
369 if (*p != '\0' || ul > INT_MAX)
373 warn("Invalid sectorsize: %s",
378 } else if (strcmp(token, "notrim") == 0) {
380 } else if (strcmp(token, "late") == 0) {
381 /* ignore known option */
382 } else if (strcmp(token, "noauto") == 0) {
383 /* ignore known option */
384 } else if (strcmp(token, "sw") != 0) {
385 warnx("Invalid option: %s", token);
394 * If we do not have a sector size at this point, fill in
395 * pagesize as sector size.
397 if (sectorsize_str == NULL) {
398 /* Use pagesize as default sectorsize. */
399 pagesize = getpagesize();
400 pagesize_len = snprintf(NULL, 0, "%d", pagesize) + 1;
401 p = alloca(pagesize_len);
402 snprintf(p, pagesize_len, "%d", pagesize);
406 (void)asprintf(&args, "%s%s%s%s%s%s%s%s%s -d",
407 aflag, aalgo, eflag, ealgo, lflag, keylen_str, Tflag,
408 sflag, sectorsize_str);
415 swap_on_off_geli(const char *name, char *mntops, int doingall)
421 error = stat(name, &sb);
423 if (which_prog == SWAPON) do {
424 /* Skip if the .eli device already exists. */
428 args = swap_on_geli_args(mntops);
432 dname = swap_basename(name);
438 error = run_cmd(NULL, "%s onetime%s %s", _PATH_GELI, args,
445 /* error occurred during creation. */
447 warnx("%s: Invalid parameters", name);
452 return (swap_on_off_sfile(name, doingall));
456 swap_on_off_md(const char *name, char *mntops, int doingall)
459 int fd, mdunit, error;
461 static char mdpath[PATH_MAX], linebuf[PATH_MAX];
468 if (strlen(name) == (sizeof(MD_NAME) - 1))
472 ul = strtoul(name + 2, &p, 10);
474 if (*p != '\0' || ul > INT_MAX)
478 warn("Bad device unit: %s", name);
485 if ((p = strstr(mntops, "file=")) != NULL) {
486 vnodefile = strdup(p + sizeof("file=") - 1);
487 p = strchr(vnodefile, ',');
491 if (vnodefile == NULL) {
492 warnx("file option not found for %s", name);
496 if (which_prog == SWAPON) {
498 error = run_cmd(&fd, "%s -l -n -f %s",
499 _PATH_MDCONFIG, vnodefile);
501 /* md device found. Ignore it. */
504 warnx("%s: Device already in use",
509 error = run_cmd(&fd, "%s -a -t vnode -n -f %s",
510 _PATH_MDCONFIG, vnodefile);
512 warnx("mdconfig (attach) error: file=%s",
517 sfd = fdopen(fd, "r");
519 warn("mdconfig (attach) fdopen error");
523 p = fgetln(sfd, &linelen);
525 (linelen < 2 || linelen > sizeof(linebuf))) {
526 warn("mdconfig (attach) unexpected output");
530 strncpy(linebuf, p, linelen);
531 linebuf[linelen - 1] = '\0';
533 ul = strtoul(linebuf, &p, 10);
535 if (*p != '\0' || ul > INT_MAX)
539 warn("mdconfig (attach) unexpected output: %s",
546 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
547 _PATH_MDCONFIG, vnodefile, mdunit);
549 /* md device found. Ignore it. */
552 warnx("md%d on %s: Device already "
553 "in use", mdunit, vnodefile);
557 error = run_cmd(NULL, "%s -a -t vnode -u %d -f %s",
558 _PATH_MDCONFIG, mdunit, vnodefile);
560 warnx("mdconfig (attach) error: "
561 "md%d on file=%s", mdunit, vnodefile);
566 } else /* SWAPOFF */ {
568 error = run_cmd(&fd, "%s -l -n -f %s",
569 _PATH_MDCONFIG, vnodefile);
571 /* md device not found. Ignore it. */
574 warnx("md on %s: Device not found",
579 sfd = fdopen(fd, "r");
581 warn("mdconfig (list) fdopen error");
585 p = fgetln(sfd, &linelen);
587 (linelen < 2 || linelen > sizeof(linebuf) - 1)) {
588 warn("mdconfig (list) unexpected output");
592 strncpy(linebuf, p, linelen);
593 linebuf[linelen - 1] = '\0';
594 p = strchr(linebuf, ' ');
598 ul = strtoul(linebuf, &p, 10);
600 if (*p != '\0' || ul > INT_MAX)
604 warn("mdconfig (list) unexpected output: %s",
611 error = run_cmd(&fd, "%s -l -n -f %s -u %d",
612 _PATH_MDCONFIG, vnodefile, mdunit);
614 /* md device not found. Ignore it. */
617 warnx("md%d on %s: Device not found",
624 snprintf(mdpath, sizeof(mdpath), "%s%s%d", _PATH_DEV,
626 mdpath[sizeof(mdpath) - 1] = '\0';
627 ret = swap_on_off_sfile(mdpath, doingall);
629 if (which_prog == SWAPOFF) {
631 error = run_cmd(NULL, "%s -d -u %d",
632 _PATH_MDCONFIG, mdunit);
634 warn("mdconfig (detach) detach failed: %s%s%d",
635 _PATH_DEV, MD_NAME, mdunit);
648 run_cmd(int *ofd, const char *cmdline, ...)
651 char **argv, **argvp, *cmd, *p;
652 int argc, pid, status, rv;
653 int pfd[2], nfd, dup2dn;
655 va_start(ap, cmdline);
656 rv = vasprintf(&cmd, cmdline, ap);
658 warn("%s", __func__);
664 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
666 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
667 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
668 if (**argvp != '\0' && (++argvp > &argv[argc])) {
672 /* The argv array ends up NULL-terminated here. */
677 fprintf(stderr, "DEBUG: running:");
678 /* Should be equivalent to 'cmd' (before strsep, of course). */
679 for (i = 0; argv[i] != NULL; i++)
680 fprintf(stderr, " %s", argv[i]);
681 fprintf(stderr, "\n");
686 if (pipe(&pfd[0]) == -1) {
687 warn("%s: pipe", __func__);
698 if (dup2(pfd[1], STDOUT_FILENO) < 0)
699 err(1, "dup2 in %s", __func__);
700 nfd = open(_PATH_DEVNULL, O_RDWR);
702 err(1, "%s: open %s", __func__, _PATH_DEVNULL);
703 if (dup2(nfd, STDIN_FILENO) < 0)
704 err(1, "%s: dup2", __func__);
705 if (dup2dn && dup2(nfd, STDOUT_FILENO) < 0)
706 err(1, "%s: dup2", __func__);
707 if (dup2(nfd, STDERR_FILENO) < 0)
708 err(1, "%s: dup2", __func__);
709 execv(argv[0], argv);
710 warn("exec: %s", argv[0]);
713 err(1, "%s: fork", __func__);
717 while (waitpid(pid, &status, 0) != pid)
719 return (WEXITSTATUS(status));
723 swap_on_off_sfile(const char *name, int doingall)
727 if (which_prog == SWAPON)
728 error = swapon(name);
730 error = swapoff(name);
736 warnx("%s: Device already in use", name);
739 if (which_prog == SWAPON)
740 warnx("%s: NSWAPDEV limit reached", name);
741 else if (doingall == 0)
757 fprintf(stderr, "usage: %s ", getprogname());
761 fprintf(stderr, "[-F fstab] -aLq | file ...\n");
764 fprintf(stderr, "[-AghklmsU] [-a file ... | -d file ...]\n");
771 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
777 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
778 HN_B | HN_NOSPACE | HN_DECIMAL);
779 snprintf(buf, bufsize, "%*s", hlen, tmp);
781 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
785 swaplist(int lflag, int sflag, int hflag)
787 size_t mibsize, size;
789 int hlen, mib[16], n, pagesize;
797 pagesize = getpagesize();
800 blocksize = 1024 * 1024 * 1024;
801 strlcpy(buf, "1GB-blocks", sizeof(buf));
806 strlcpy(buf, "Bytes", sizeof(buf));
811 strlcpy(buf, "1kB-blocks", sizeof(buf));
815 blocksize = 1024 * 1024;
816 strlcpy(buf, "1MB-blocks", sizeof(buf));
820 getbsize(&hlen, &blocksize);
821 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
825 mibsize = nitems(mib);
826 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
827 err(1, "sysctlnametomib()");
830 printf("%-13s %*s %*s\n",
839 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
841 if (xsw.xsw_version != XSWDEV_VERSION)
842 errx(1, "xswdev version mismatch");
844 tmp_total = (long long)xsw.xsw_nblks * pagesize;
845 tmp_used = (long long)xsw.xsw_used * pagesize;
849 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
851 printf("/dev/%-8s %s ", devname(xsw.xsw_dev, S_IFCHR),
853 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
862 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
863 printf("Total: %s ", buf);
864 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);