1 /* $NetBSD: swapctl.c,v 1.34 2008/12/28 20:18:53 christos Exp $ */
4 * Copyright (c) 1996, 1997, 1999 Matthew R. Green
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * -A add all devices listed as `sw' in /etc/fstab (also
32 * (sets the dump device, if listed in fstab)
33 * -D [<dev>|none] set dumpdev to <dev> or disable dumps
35 * -U remove all devices listed as `sw' in /etc/fstab.
37 * if -A or -U , add (remove) either all block device
38 * or all non-block devices, or all swap partitions
39 * -q check if any swap or dump devices are defined in
41 * -a <dev> add this device
42 * -d <dev> remove this swap device
43 * -f with -A -t auto, use the first swap as dump device
45 * -h use humanize_number(3) for listing
46 * -l list swap devices
48 * -n print actions, but do not add/remove swap or
50 * -o with -A -t auto only configure the first swap as dump,
51 * (similar to -f), but do not add any further swap devs
52 * -s short listing of swap devices
54 * -p <pri> use this priority
57 * or, if invoked as "swapon" (compatibility mode):
59 * -a all devices listed as `sw' in /etc/fstab
60 * -t same as -t above (feature not present in old
62 * <dev> add this device
64 #include <sys/cdefs.h>
67 __RCSID("$NetBSD: swapctl.c,v 1.34 2008/12/28 20:18:53 christos Exp $");
71 #include <sys/param.h>
74 #include <sys/sysctl.h>
76 #include <sys/disklabel.h>
94 * Commands for swapctl(8). These are mutually exclusive.
96 #define CMD_A 0x01 /* process /etc/fstab for adding */
97 #define CMD_D 0x02 /* set dumpdev */
98 #define CMD_U 0x04 /* process /etc/fstab for removing */
99 #define CMD_a 0x08 /* add a swap file/device */
100 #define CMD_c 0x10 /* change priority of a swap file/device */
101 #define CMD_d 0x20 /* delete a swap file/device */
102 #define CMD_l 0x40 /* list swap files/devices */
103 #define CMD_s 0x80 /* summary of swap files/devices */
104 #define CMD_z 0x100 /* show dump device */
105 #define CMD_q 0x200 /* check for dump/swap in /etc/fstab */
107 #define SET_COMMAND(cmd) \
115 * Commands that require a "path" argument at the end of the command
116 * line, and the ones which require that none exist.
118 #define REQUIRE_PATH (CMD_D | CMD_a | CMD_c | CMD_d)
119 #define REQUIRE_NOPATH (CMD_A | CMD_U | CMD_l | CMD_s | CMD_z | CMD_q)
122 * Option flags, and the commands with which they are valid.
124 int kflag
; /* display in 1K^x blocks */
125 #define KFLAG_CMDS (CMD_l | CMD_s)
126 #define MFLAG_CMDS (CMD_l | CMD_s)
127 #define GFLAG_CMDS (CMD_l | CMD_s)
129 int hflag
; /* display with humanize_number */
130 #define HFLAG_CMDS (CMD_l | CMD_s)
132 int pflag
; /* priority was specified */
133 #define PFLAG_CMDS (CMD_A | CMD_a | CMD_c)
135 char *tflag
; /* swap device type (blk, noblk, auto) */
136 int autoflag
; /* 1, if tflag is "auto" */
137 #define TFLAG_CMDS (CMD_A | CMD_U)
139 int fflag
; /* first swap becomes dump */
140 #define FFLAG_CMDS (CMD_A)
142 int oflag
; /* only autoset dump device */
143 #define OFLAG_CMDS (CMD_A)
145 int nflag
; /* no execute, just print actions */
146 #define NFLAG_CMDS (CMD_A | CMD_U)
148 int pri
; /* uses 0 as default pri */
150 static void change_priority(char *);
151 static int add_swap(char *, int);
152 static int delete_swap(char *);
153 static void set_dumpdev(char *);
154 static int get_dumpdev(void);
155 static void do_fstab(int);
156 static int check_fstab(void);
157 static void do_localdevs(int);
158 static void do_localdisk(const char *, int);
159 static int do_wedgesofdisk(int fd
, int);
160 static int do_partitionsofdisk(const char *, int fd
, int);
161 static void usage(void);
162 static void swapon_command(int, char **);
164 static void swapoff_command(int, char **);
168 main(int argc
, char *argv
[])
172 if (strcmp(getprogname(), "swapon") == 0) {
173 swapon_command(argc
, argv
);
178 if (strcmp(getprogname(), "swapoff") == 0) {
179 swapoff_command(argc
, argv
);
184 while ((c
= getopt(argc
, argv
, "ADUacdfghklmnop:qst:z")) != -1) {
215 kflag
= 3; /* 1k ^ 3 */
231 kflag
= 2; /* 1k ^ 2 */
260 if (strcmp(tflag
, "auto") == 0)
274 /* Did the user specify a command? */
283 if (command
& REQUIRE_PATH
)
288 if (command
& REQUIRE_NOPATH
)
296 /* To change priority, you have to specify one. */
297 if ((command
== CMD_c
) && pflag
== 0)
300 /* -f and -o are mutualy exclusive */
304 /* Sanity-check -t */
306 if (command
!= CMD_A
&& command
!= CMD_U
)
308 if (strcmp(tflag
, "blk") != 0 &&
309 strcmp(tflag
, "noblk") != 0 &&
310 strcmp(tflag
, "auto") != 0)
314 /* Dispatch the command. */
317 if (!list_swap(pri
, kflag
, pflag
, 0, 1, hflag
))
322 list_swap(pri
, kflag
, pflag
, 0, 0, hflag
);
326 change_priority(argv
[0]);
330 if (! add_swap(argv
[0], pri
))
335 if (! delete_swap(argv
[0]))
347 set_dumpdev(argv
[0]);
363 printf("%s: there are swap or dump devices defined in "
364 _PATH_FSTAB
"\n", getprogname());
367 printf("%s: no swap or dump devices in "
368 _PATH_FSTAB
"\n", getprogname());
377 * swapon_command: emulate the old swapon(8) program.
380 swapon_command(int argc
, char **argv
)
384 while ((ch
= getopt(argc
, argv
, "at:")) != -1) {
404 /* Sanity-check -t */
406 if (strcmp(tflag
, "blk") != 0 &&
407 strcmp(tflag
, "noblk") != 0)
412 } else if (argc
== 0 || tflag
!= NULL
)
416 if (! add_swap(argv
[0], pri
))
425 fprintf(stderr
, "usage: %s -a [-t blk|noblk]\n", getprogname());
426 fprintf(stderr
, " %s <path> ...\n", getprogname());
431 * change_priority: change the priority of a swap device.
434 change_priority(char *path
)
437 if (swapctl(SWAP_CTL
, path
, pri
) < 0)
442 * add_swap: add the pathname to the list of swap devices.
445 add_swap(char *path
, int priority
)
449 if (stat(path
, &sb
) < 0)
452 if (sb
.st_mode
& S_IROTH
)
453 warnx("WARNING: %s is readable by the world", path
);
454 if (sb
.st_mode
& S_IWOTH
)
455 warnx("WARNING: %s is writable by the world", path
);
457 if (fflag
|| oflag
) {
468 if (swapctl(SWAP_ON
, path
, priority
) < 0) {
476 * delete_swap: remove the pathname to the list of swap devices.
479 delete_swap(char *path
)
485 if (swapctl(SWAP_OFF
, path
, pri
) < 0)
491 set_dumpdev(char *path
)
496 if (strcmp(path
, "none") == 0)
497 rv
= swapctl(SWAP_DUMPOFF
, NULL
, 0);
499 rv
= swapctl(SWAP_DUMPDEV
, path
, 0);
503 err(1, "could not set dump device to %s", path
);
505 printf("%s: setting dump device to %s\n", getprogname(), path
);
514 if (swapctl(SWAP_GETDUMPDEV
, &dev
, 0) == -1) {
515 warn("could not get dump device");
517 } else if (dev
== NODEV
) {
518 printf("no dump device set\n");
521 name
= devname(dev
, S_IFBLK
);
522 printf("dump device is ");
524 printf("%s\n", name
);
526 printf("major %llu minor %llu\n",
527 (unsigned long long)major(dev
),
528 (unsigned long long)minor(dev
));
534 do_localdevs(int add
)
537 char *disknames
, *disk
;
538 static const char mibname
[] = "hw.disknames";
541 if (sysctlbyname(mibname
, NULL
, &ressize
, NULL
, 0))
543 ressize
+= 200; /* add some arbitrary slope */
544 disknames
= malloc(ressize
);
545 if (sysctlbyname(mibname
, disknames
, &ressize
, NULL
, 0) == 0) {
546 for (disk
= strtok(disknames
, " "); disk
;
547 disk
= strtok(NULL
, " "))
548 do_localdisk(disk
, add
);
554 do_localdisk(const char *disk
, int add
)
557 char dvname
[MAXPATHLEN
];
559 if ((fd
= opendisk(disk
, O_RDONLY
, dvname
, sizeof(dvname
), 0)) == -1)
562 if (!do_wedgesofdisk(fd
, add
))
563 do_partitionsofdisk(disk
, fd
, add
);
569 do_wedgesofdisk(int fd
, int add
)
571 char devicename
[MAXPATHLEN
];
572 struct dkwedge_info
*dkw
;
573 struct dkwedge_list dkwl
;
579 dkwl
.dkwl_bufsize
= 0;
582 if (ioctl(fd
, DIOCLWEDGES
, &dkwl
) == -1)
584 if (dkwl
.dkwl_nwedges
== dkwl
.dkwl_ncopied
)
586 bufsize
= dkwl
.dkwl_nwedges
* sizeof(*dkw
);
587 if (dkwl
.dkwl_bufsize
< bufsize
) {
588 dkw
= realloc(dkwl
.dkwl_buf
, bufsize
);
592 dkwl
.dkwl_bufsize
= bufsize
;
596 for (i
= 0; i
< dkwl
.dkwl_ncopied
; i
++) {
597 if (strcmp(dkw
[i
].dkw_ptype
, DKW_PTYPE_SWAP
) != 0)
599 snprintf(devicename
, sizeof(devicename
), "%s%s", _PATH_DEV
,
601 devicename
[sizeof(devicename
)-1] = '\0';
604 if (add_swap(devicename
, pri
)) {
606 "%s: adding %s as swap device at priority 0\n",
607 getprogname(), devicename
);
610 if (delete_swap(devicename
)) {
612 "%s: removing %s as swap device\n",
613 getprogname(), devicename
);
620 return dkwl
.dkwl_nwedges
!= 0;
624 do_partitionsofdisk(const char *prefix
, int fd
, int add
)
626 char devicename
[MAXPATHLEN
];
627 struct disklabel lab
;
630 if (ioctl(fd
, DIOCGDINFO
, &lab
) != 0)
633 for (i
= 0; i
< lab
.d_npartitions
; i
++) {
634 if (lab
.d_partitions
[i
].p_fstype
!= FS_SWAP
)
636 snprintf(devicename
, sizeof(devicename
), "%s%s%c", _PATH_DEV
,
638 devicename
[sizeof(devicename
)-1] = '\0';
641 if (add_swap(devicename
, pri
)) {
643 "%s: adding %s as swap device at priority 0\n",
644 getprogname(), devicename
);
647 if (delete_swap(devicename
)) {
649 "%s: removing %s as swap device\n",
650 getprogname(), devicename
);
663 while ((fp
= getfsent()) != NULL
) {
664 if (strcmp(fp
->fs_type
, "dp") == 0)
667 if (strcmp(fp
->fs_type
, "sw") == 0)
682 int success
= 0; /* set to 1 after a successful operation */
683 int error
= 0; /* set to 1 after an error */
686 #define PATH_MOUNT RESCUEDIR "/mount_nfs"
687 #define PATH_UMOUNT RESCUEDIR "/umount"
689 #define PATH_MOUNT "/sbin/mount_nfs"
690 #define PATH_UMOUNT "/sbin/umount"
693 char cmd
[2*PATH_MAX
+sizeof(PATH_MOUNT
)+2];
695 #define PRIORITYEQ "priority="
696 #define NFSMNTPT "nfsmntpt="
697 while ((fp
= getfsent()) != NULL
) {
703 if (strcmp(fp
->fs_type
, "dp") == 0 && add
) {
708 if (strcmp(fp
->fs_type
, "sw") != 0)
711 /* handle dp as mnt option */
712 if (strstr(fp
->fs_mntops
, "dp") && add
)
717 if ((s
= strstr(fp
->fs_mntops
, PRIORITYEQ
)) != NULL
) {
718 s
+= sizeof(PRIORITYEQ
) - 1;
723 if ((s
= strstr(fp
->fs_mntops
, NFSMNTPT
)) != NULL
) {
727 * Skip this song and dance if we're only
728 * doing block devices.
730 if (tflag
!= NULL
&& strcmp(tflag
, "blk") == 0)
736 spec
= strdup(s
+ strlen(NFSMNTPT
));
741 errx(1, "Out of memory");
743 if (strlen(spec
) == 0) {
744 warnx("empty mountpoint");
749 snprintf(cmd
, sizeof(cmd
), "%s %s %s",
750 PATH_MOUNT
, fp
->fs_spec
, spec
);
751 if (system(cmd
) != 0) {
752 warnx("%s: mount failed", fp
->fs_spec
);
756 snprintf(cmd
, sizeof(cmd
), "%s %s",
757 PATH_UMOUNT
, fp
->fs_spec
);
761 * Determine blk-ness.
763 if (stat(spec
, &st
) < 0) {
767 if (S_ISBLK(st
.st_mode
))
772 * Skip this type if we're told to.
775 if (strcmp(tflag
, "blk") == 0 && isblk
== 0)
777 if (strcmp(tflag
, "noblk") == 0 && isblk
== 1)
782 if (add_swap(spec
, (int)priority
)) {
785 "%s: adding %s as swap device at priority %d\n",
786 getprogname(), fp
->fs_spec
, (int)priority
);
790 "%s: failed to add %s as swap device\n",
791 getprogname(), fp
->fs_spec
);
794 if (delete_swap(spec
)) {
797 "%s: removing %s as swap device\n",
798 getprogname(), fp
->fs_spec
);
802 "%s: failed to remove %s as swap device\n",
803 getprogname(), fp
->fs_spec
);
806 if (system(cmd
) != 0) {
807 warnx("%s: umount failed", fp
->fs_spec
);
814 if (spec
!= fp
->fs_spec
)
822 exit(2); /* not really an error, but no swap devices found */
828 const char *progname
= getprogname();
830 fprintf(stderr
, "usage: %s -A [-f|-o] [-n] [-p priority] "
831 "[-t blk|noblk|auto]\n", progname
);
832 fprintf(stderr
, " %s -a [-p priority] path\n", progname
);
833 fprintf(stderr
, " %s -q\n", progname
);
834 fprintf(stderr
, " %s -c -p priority path\n", progname
);
835 fprintf(stderr
, " %s -D dumpdev|none\n", progname
);
836 fprintf(stderr
, " %s -d path\n", progname
);
837 fprintf(stderr
, " %s -l | -s [-k|-m|-g|-h]\n", progname
);
838 fprintf(stderr
, " %s -U [-n] [-t blk|noblk|auto]\n", progname
);
839 fprintf(stderr
, " %s -z\n", progname
);