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 * 3. 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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)swapon.c 8.1 (Berkeley) 6/5/93
31 * $FreeBSD: src/sbin/swapon/swapon.c,v 1.8.2.2 2001/07/30 10:30:11 dd Exp $
34 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/linker.h>
38 #include <sys/diskslice.h>
39 #include <vm/vm_param.h>
51 #include <bus/cam/scsi/scsi_daio.h>
53 static void usage(void);
54 static int swap_on_off(char *name, int doingall, int trim, int ask);
55 static char *docrypt(char *fs_spec, int pass);
56 static void swaplist(int lflag, int sflag, int hflag);
60 enum { SWAPON, SWAPOFF, SWAPCTL } orig_prog, which_prog = SWAPCTL;
63 main(int argc, char **argv)
69 int doall, sflag, lflag, hflag, eflag, cflag, iflag;
71 if ((ptr = strrchr(argv[0], '/')) == NULL)
73 if (strstr(ptr, "swapon"))
75 else if (strstr(ptr, "swapoff"))
77 orig_prog = which_prog;
79 sflag = lflag = hflag = doall = eflag = cflag = iflag = 0;
80 while ((ch = getopt(argc, argv, "acdeghiklmqsAEU")) != -1) {
83 if (which_prog == SWAPCTL) {
91 if (which_prog == SWAPON || which_prog == SWAPOFF)
97 if (which_prog == SWAPCTL)
128 if (which_prog == SWAPON || which_prog == SWAPOFF)
135 if (which_prog == SWAPCTL) {
137 which_prog = SWAPOFF;
150 if (which_prog == SWAPON || which_prog == SWAPOFF) {
152 while ((fsp = getfsent()) != NULL) {
156 if (strcmp(fsp->fs_type, FSTAB_SW))
158 if (strstr(fsp->fs_mntops, "noauto"))
161 if (strstr(fsp->fs_mntops, "notrim"))
163 else if (strstr(fsp->fs_mntops, "trim"))
166 if (cflag || strstr(fsp->fs_mntops, "crypt"))
167 fs_spec = docrypt(fsp->fs_spec, 1);
169 fs_spec = strdup(fsp->fs_spec);
170 if (swap_on_off(fs_spec, 1, dotrim, iflag)) {
174 strstr(fsp->fs_mntops, "crypt")) {
175 docrypt(fsp->fs_spec, 2);
178 printf("%s: %sing %s as swap "
181 (which_prog == SWAPOFF ?
188 } else if (*argv == NULL) {
191 for (; *argv; ++argv) {
192 char *ospec = getdevpath(*argv, 0);
195 fs_spec = docrypt(ospec, 1);
197 fs_spec = strdup(ospec);
198 if (swap_on_off(fs_spec, 0, eflag, iflag)) {
204 printf("%s: %sing %s as swap device\n",
206 (which_prog == SWAPOFF ?
215 swaplist(lflag, sflag, hflag);
224 docrypt(char *fs_spec, int pass)
230 if ((id = strrchr(fs_spec, '/')) == NULL)
234 asprintf(&id, "swap-%s", id);
235 asprintf(&res, "/dev/mapper/%s", id);
241 asprintf(&buf, "/sbin/cryptsetup remove %s", id);
249 if (kldfind("dm_target_crypt") < 0)
250 kldload("dm_target_crypt");
253 "/sbin/cryptsetup --key-file /dev/urandom "
254 "--key-size 256 create %s %s",
263 * NOTE: Don't revert to /dev/da* on error because this could
264 * inadvertently add both /dev/da* and
265 * /dev/mapper/swap-da*.
267 * Allow the swapon operation to report failure or
268 * report a duplicate.
274 res = strdup(fs_spec);
290 trim_volume(char * name)
292 struct partinfo pinfo;
294 size_t bytes = 0,ksize;
300 * Determine if this device is already being used by swap without
303 if ((sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0) ||
305 err(1, "sysctlbyname()");
308 xswbuf = malloc(bytes);
309 if ((sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) ||
312 err(1, "sysctlbyname()");
315 ksize = ((struct xswdev *)xswbuf)->xsw_size;
316 n = (int)(bytes / ksize);
317 for (i = 0; i < n; ++i) {
318 xsw = (void *)((char *)xswbuf + i * ksize);
320 if (xsw->xsw_dev == NODEV )
322 if(!strcmp(devname(xsw->xsw_dev, S_IFCHR),
323 name + strlen("/dev/"))) {
324 warnx("%s: device already a swap device", name);
330 * Get the size and offset of this parititon/device
332 fd = open(name, O_RDWR);
334 err(1, "Unable to open %s R+W", name);
335 if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
336 printf("Cannot trim regular file\n");
342 ioarg[0] = pinfo.media_offset;
343 ioarg[1] = pinfo.media_size;
344 printf("Trimming Device:%s, start=%jd bytes=%jd)\n",
345 name, (intmax_t)ioarg[0], (intmax_t)ioarg[1]);
346 if (ioctl(fd, DAIOCTRIM, ioarg) < 0) {
347 printf("Device trim failed\n");
353 swap_on_off(char *name, int doingall, int trim, int ask)
356 if (ask && which_prog == SWAPON) {
357 printf("Do you really want to use device %s as a swap device ?\n", name);
358 printf("You might loose data. [Y/N]");
360 int c = fgetc(stdin);
362 if (c != 'y' && c != 'Y')
366 if (which_prog == SWAPON && trim) {
369 if ((which_prog == SWAPOFF ? swapoff(name) : swapon(name)) == -1) {
373 warnx("%s: device already in use", name);
376 if (which_prog == SWAPON)
377 warnx("%s: NSWAPDEV limit reached", name);
393 fprintf(stderr, "usage: %s ", getprogname());
397 fprintf(stderr, "-aeiq | file ...\n");
400 fprintf(stderr, "[-AeghiklmsU] [-a file ... | -d file ...]\n");
407 sizetobuf(char *buf, size_t bufsize, int hflag, long long val, int hlen,
413 humanize_number(tmp, 5, (int64_t)val, "", HN_AUTOSCALE,
414 HN_B | HN_NOSPACE | HN_DECIMAL);
415 snprintf(buf, bufsize, "%*s", hlen, tmp);
417 snprintf(buf, bufsize, "%*lld", hlen, val / blocksize);
422 swaplist(int lflag, int sflag, int hflag)
424 size_t ksize, bytes = 0;
430 long long total, used, tmp_total, tmp_used;
433 pagesize = getpagesize();
436 blocksize = 1024 * 1024 * 1024;
437 strlcpy(buf, "1GB-blocks", sizeof(buf));
442 strlcpy(buf, "Bytes", sizeof(buf));
447 strlcpy(buf, "1kB-blocks", sizeof(buf));
451 blocksize = 1024 * 1024;
452 strlcpy(buf, "1MB-blocks", sizeof(buf));
456 getbsize(&hlen, &blocksize);
457 snprintf(buf, sizeof(buf), "%ld-blocks", blocksize);
461 if (sysctlbyname("vm.swap_info_array", NULL, &bytes, NULL, 0) < 0)
462 err(1, "sysctlbyname()");
464 err(1, "sysctlbyname()");
466 xswbuf = malloc(bytes);
467 if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
469 err(1, "sysctlbyname()");
473 err(1, "sysctlbyname()");
477 * Calculate size of xsw entry returned by kernel (it can be larger
478 * than the one we have if there is a version mismatch).
480 ksize = ((struct xswdev *)xswbuf)->xsw_size;
481 n = (int)(bytes / ksize);
484 printf("%-13s %*s %*s\n",
490 total = used = tmp_total = tmp_used = 0;
491 for (i = 0; i < n; ++i) {
492 xsw = (void *)((char *)xswbuf + i * ksize);
494 if (xsw->xsw_nblks == 0)
497 tmp_total = (long long)xsw->xsw_nblks * pagesize;
498 tmp_used = (long long)xsw->xsw_used * pagesize;
502 sizetobuf(buf, sizeof(buf), hflag, tmp_total, hlen,
504 if (xsw->xsw_dev == NODEV) {
505 printf("%-13s %s ", "[NFS swap]", buf);
507 printf("/dev/%-8s %s ",
508 devname(xsw->xsw_dev, S_IFCHR), buf);
511 sizetobuf(buf, sizeof(buf), hflag, tmp_used, hlen,
518 sizetobuf(buf, sizeof(buf), hflag, total, hlen, blocksize);
519 printf("Total: %s ", buf);
520 sizetobuf(buf, sizeof(buf), hflag, used, hlen, blocksize);