swapctl.c revision 1.32 1 1.32 martin /* $NetBSD: swapctl.c,v 1.32 2006/08/27 21:07:39 martin Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.11 mrg * Copyright (c) 1996, 1997, 1999 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.7 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg * swapctl command:
33 1.10 mrg * -A add all devices listed as `sw' in /etc/fstab (also
34 1.13 soren * (sets the dump device, if listed in fstab)
35 1.30 martin * -D [<dev>|none] set dumpdev to <dev> or disable dumps
36 1.16 mrg * -z show dumpdev
37 1.12 lukem * -U remove all devices listed as `sw' in /etc/fstab.
38 1.32 martin * -t [blk|noblk|auto]
39 1.32 martin * if -A or -U , add (remove) either all block device
40 1.32 martin * or all non-block devices, or all swap partitions
41 1.32 martin * -q check if any swap or dump devices are defined in
42 1.32 martin * /etc/fstab
43 1.1 mrg * -a <dev> add this device
44 1.17 lukem * -d <dev> remove this swap device
45 1.32 martin * -f with -A -t auto, use the first swap as dump device
46 1.25 mrg * -g use gigabytes
47 1.24 mrg * -h use humanize_number(3) for listing
48 1.1 mrg * -l list swap devices
49 1.25 mrg * -m use megabytes
50 1.32 martin * -n print actions, but do not add/remove swap or
51 1.32 martin * with -A/-U
52 1.32 martin * -o with -A -t auto only configure the first swap as dump,
53 1.32 martin * (similar to -f), but do not add any further swap devs
54 1.1 mrg * -s short listing of swap devices
55 1.1 mrg * -k use kilobytes
56 1.1 mrg * -p <pri> use this priority
57 1.1 mrg * -c change priority
58 1.2 thorpej *
59 1.2 thorpej * or, if invoked as "swapon" (compatibility mode):
60 1.2 thorpej *
61 1.2 thorpej * -a all devices listed as `sw' in /etc/fstab
62 1.4 thorpej * -t same as -t above (feature not present in old
63 1.4 thorpej * swapon(8) command)
64 1.2 thorpej * <dev> add this device
65 1.1 mrg */
66 1.22 agc #include <sys/cdefs.h>
67 1.22 agc
68 1.22 agc #ifndef lint
69 1.32 martin __RCSID("$NetBSD: swapctl.c,v 1.32 2006/08/27 21:07:39 martin Exp $");
70 1.22 agc #endif
71 1.22 agc
72 1.1 mrg
73 1.1 mrg #include <sys/param.h>
74 1.4 thorpej #include <sys/stat.h>
75 1.14 mrg #include <sys/swap.h>
76 1.32 martin #include <sys/sysctl.h>
77 1.32 martin #include <sys/disk.h>
78 1.32 martin #include <sys/disklabel.h>
79 1.1 mrg
80 1.1 mrg #include <unistd.h>
81 1.3 mikel #include <err.h>
82 1.1 mrg #include <errno.h>
83 1.1 mrg #include <stdio.h>
84 1.1 mrg #include <stdlib.h>
85 1.1 mrg #include <string.h>
86 1.1 mrg #include <fstab.h>
87 1.32 martin #include <fcntl.h>
88 1.32 martin #include <util.h>
89 1.32 martin #include <paths.h>
90 1.1 mrg
91 1.1 mrg #include "swapctl.h"
92 1.1 mrg
93 1.2 thorpej int command;
94 1.2 thorpej
95 1.2 thorpej /*
96 1.2 thorpej * Commands for swapctl(8). These are mutually exclusive.
97 1.2 thorpej */
98 1.12 lukem #define CMD_A 0x01 /* process /etc/fstab for adding */
99 1.10 mrg #define CMD_D 0x02 /* set dumpdev */
100 1.12 lukem #define CMD_U 0x04 /* process /etc/fstab for removing */
101 1.12 lukem #define CMD_a 0x08 /* add a swap file/device */
102 1.12 lukem #define CMD_c 0x10 /* change priority of a swap file/device */
103 1.12 lukem #define CMD_d 0x20 /* delete a swap file/device */
104 1.12 lukem #define CMD_l 0x40 /* list swap files/devices */
105 1.12 lukem #define CMD_s 0x80 /* summary of swap files/devices */
106 1.16 mrg #define CMD_z 0x100 /* show dump device */
107 1.32 martin #define CMD_q 0x200 /* check for dump/swap in /etc/fstab */
108 1.2 thorpej
109 1.2 thorpej #define SET_COMMAND(cmd) \
110 1.2 thorpej do { \
111 1.2 thorpej if (command) \
112 1.2 thorpej usage(); \
113 1.2 thorpej command = (cmd); \
114 1.2 thorpej } while (0)
115 1.2 thorpej
116 1.2 thorpej /*
117 1.2 thorpej * Commands that require a "path" argument at the end of the command
118 1.2 thorpej * line, and the ones which require that none exist.
119 1.2 thorpej */
120 1.10 mrg #define REQUIRE_PATH (CMD_D | CMD_a | CMD_c | CMD_d)
121 1.32 martin #define REQUIRE_NOPATH (CMD_A | CMD_U | CMD_l | CMD_s | CMD_z | CMD_q)
122 1.2 thorpej
123 1.2 thorpej /*
124 1.2 thorpej * Option flags, and the commands with which they are valid.
125 1.2 thorpej */
126 1.25 mrg int kflag; /* display in 1K^x blocks */
127 1.2 thorpej #define KFLAG_CMDS (CMD_l | CMD_s)
128 1.25 mrg #define MFLAG_CMDS (CMD_l | CMD_s)
129 1.25 mrg #define GFLAG_CMDS (CMD_l | CMD_s)
130 1.2 thorpej
131 1.24 mrg int hflag; /* display with humanize_number */
132 1.24 mrg #define HFLAG_CMDS (CMD_l | CMD_s)
133 1.24 mrg
134 1.2 thorpej int pflag; /* priority was specified */
135 1.2 thorpej #define PFLAG_CMDS (CMD_A | CMD_a | CMD_c)
136 1.2 thorpej
137 1.32 martin char *tflag; /* swap device type (blk, noblk, auto) */
138 1.32 martin int autoflag; /* 1, if tflag is "auto" */
139 1.16 mrg #define TFLAG_CMDS (CMD_A | CMD_U)
140 1.4 thorpej
141 1.32 martin int fflag; /* first swap becomes dump */
142 1.32 martin #define FFLAG_CMDS (CMD_A)
143 1.32 martin
144 1.32 martin int oflag; /* only autoset dump device */
145 1.32 martin #define OFLAG_CMDS (CMD_A)
146 1.32 martin
147 1.32 martin int nflag; /* no execute, just print actions */
148 1.32 martin #define NFLAG_CMDS (CMD_A | CMD_U)
149 1.32 martin
150 1.1 mrg int pri; /* uses 0 as default pri */
151 1.1 mrg
152 1.29 christos static void change_priority(char *);
153 1.29 christos static int add_swap(char *, int);
154 1.29 christos static int delete_swap(char *);
155 1.29 christos static void set_dumpdev(char *);
156 1.30 martin static int get_dumpdev(void);
157 1.28 xtraeme static void do_fstab(int);
158 1.32 martin static int check_fstab(void);
159 1.32 martin static void do_localdevs(int);
160 1.32 martin static void do_localdisk(const char *, int);
161 1.32 martin static int do_wedgesofdisk(int fd, int);
162 1.32 martin static int do_partitionsofdisk(const char *, int fd, int);
163 1.28 xtraeme static void usage(void);
164 1.28 xtraeme static void swapon_command(int, char **);
165 1.2 thorpej #if 0
166 1.28 xtraeme static void swapoff_command(int, char **);
167 1.2 thorpej #endif
168 1.2 thorpej
169 1.1 mrg int
170 1.28 xtraeme main(int argc, char *argv[])
171 1.1 mrg {
172 1.1 mrg int c;
173 1.2 thorpej
174 1.18 cgd if (strcmp(getprogname(), "swapon") == 0) {
175 1.2 thorpej swapon_command(argc, argv);
176 1.2 thorpej /* NOTREACHED */
177 1.2 thorpej }
178 1.2 thorpej
179 1.2 thorpej #if 0
180 1.18 cgd if (strcmp(getprogname(), "swapoff") == 0) {
181 1.2 thorpej swapoff_command(argc, argv);
182 1.2 thorpej /* NOTREACHED */
183 1.2 thorpej }
184 1.2 thorpej #endif
185 1.2 thorpej
186 1.32 martin while ((c = getopt(argc, argv, "ADUacdfghklmnop:qst:z")) != -1) {
187 1.2 thorpej switch (c) {
188 1.1 mrg case 'A':
189 1.2 thorpej SET_COMMAND(CMD_A);
190 1.1 mrg break;
191 1.2 thorpej
192 1.10 mrg case 'D':
193 1.10 mrg SET_COMMAND(CMD_D);
194 1.10 mrg break;
195 1.10 mrg
196 1.12 lukem case 'U':
197 1.12 lukem SET_COMMAND(CMD_U);
198 1.12 lukem break;
199 1.12 lukem
200 1.1 mrg case 'a':
201 1.2 thorpej SET_COMMAND(CMD_a);
202 1.1 mrg break;
203 1.2 thorpej
204 1.1 mrg case 'c':
205 1.2 thorpej SET_COMMAND(CMD_c);
206 1.1 mrg break;
207 1.2 thorpej
208 1.1 mrg case 'd':
209 1.2 thorpej SET_COMMAND(CMD_d);
210 1.1 mrg break;
211 1.2 thorpej
212 1.32 martin case 'f':
213 1.32 martin fflag = 1;
214 1.32 martin break;
215 1.32 martin
216 1.25 mrg case 'g':
217 1.25 mrg kflag = 3; /* 1k ^ 3 */
218 1.25 mrg break;
219 1.25 mrg
220 1.24 mrg case 'h':
221 1.24 mrg hflag = 1;
222 1.24 mrg break;
223 1.24 mrg
224 1.25 mrg case 'k':
225 1.25 mrg kflag = 1;
226 1.25 mrg break;
227 1.25 mrg
228 1.1 mrg case 'l':
229 1.2 thorpej SET_COMMAND(CMD_l);
230 1.1 mrg break;
231 1.2 thorpej
232 1.25 mrg case 'm':
233 1.25 mrg kflag = 2; /* 1k ^ 2 */
234 1.1 mrg break;
235 1.2 thorpej
236 1.32 martin case 'n':
237 1.32 martin nflag = 1;
238 1.32 martin break;
239 1.32 martin
240 1.32 martin case 'o':
241 1.32 martin oflag = 1;
242 1.32 martin break;
243 1.32 martin
244 1.1 mrg case 'p':
245 1.1 mrg pflag = 1;
246 1.2 thorpej /* XXX strtol() */
247 1.1 mrg pri = atoi(optarg);
248 1.1 mrg break;
249 1.2 thorpej
250 1.32 martin case 'q':
251 1.32 martin SET_COMMAND(CMD_q);
252 1.32 martin break;
253 1.32 martin
254 1.1 mrg case 's':
255 1.2 thorpej SET_COMMAND(CMD_s);
256 1.1 mrg break;
257 1.2 thorpej
258 1.4 thorpej case 't':
259 1.4 thorpej if (tflag != NULL)
260 1.4 thorpej usage();
261 1.4 thorpej tflag = optarg;
262 1.32 martin if (strcmp(tflag, "auto") == 0)
263 1.32 martin autoflag = 1;
264 1.4 thorpej break;
265 1.4 thorpej
266 1.16 mrg case 'z':
267 1.16 mrg SET_COMMAND(CMD_z);
268 1.16 mrg break;
269 1.16 mrg
270 1.2 thorpej default:
271 1.2 thorpej usage();
272 1.2 thorpej /* NOTREACHED */
273 1.1 mrg }
274 1.1 mrg }
275 1.2 thorpej
276 1.2 thorpej /* Did the user specify a command? */
277 1.2 thorpej if (command == 0)
278 1.1 mrg usage();
279 1.1 mrg
280 1.1 mrg argv += optind;
281 1.2 thorpej argc -= optind;
282 1.2 thorpej
283 1.2 thorpej switch (argc) {
284 1.2 thorpej case 0:
285 1.2 thorpej if (command & REQUIRE_PATH)
286 1.2 thorpej usage();
287 1.2 thorpej break;
288 1.2 thorpej
289 1.2 thorpej case 1:
290 1.2 thorpej if (command & REQUIRE_NOPATH)
291 1.2 thorpej usage();
292 1.2 thorpej break;
293 1.2 thorpej
294 1.2 thorpej default:
295 1.1 mrg usage();
296 1.2 thorpej }
297 1.2 thorpej
298 1.2 thorpej /* To change priority, you have to specify one. */
299 1.2 thorpej if ((command == CMD_c) && pflag == 0)
300 1.1 mrg usage();
301 1.1 mrg
302 1.32 martin /* -f and -o are mutualy exclusive */
303 1.32 martin if (fflag && oflag)
304 1.32 martin usage();
305 1.32 martin
306 1.4 thorpej /* Sanity-check -t */
307 1.4 thorpej if (tflag != NULL) {
308 1.12 lukem if (command != CMD_A && command != CMD_U)
309 1.4 thorpej usage();
310 1.4 thorpej if (strcmp(tflag, "blk") != 0 &&
311 1.32 martin strcmp(tflag, "noblk") != 0 &&
312 1.32 martin strcmp(tflag, "auto") != 0)
313 1.4 thorpej usage();
314 1.4 thorpej }
315 1.4 thorpej
316 1.2 thorpej /* Dispatch the command. */
317 1.2 thorpej switch (command) {
318 1.2 thorpej case CMD_l:
319 1.30 martin if (!list_swap(pri, kflag, pflag, 0, 1, hflag))
320 1.30 martin exit(1);
321 1.2 thorpej break;
322 1.2 thorpej
323 1.2 thorpej case CMD_s:
324 1.24 mrg list_swap(pri, kflag, pflag, 0, 0, hflag);
325 1.2 thorpej break;
326 1.2 thorpej
327 1.2 thorpej case CMD_c:
328 1.1 mrg change_priority(argv[0]);
329 1.2 thorpej break;
330 1.2 thorpej
331 1.2 thorpej case CMD_a:
332 1.12 lukem if (! add_swap(argv[0], pri))
333 1.12 lukem exit(1);
334 1.2 thorpej break;
335 1.2 thorpej
336 1.2 thorpej case CMD_d:
337 1.12 lukem if (! delete_swap(argv[0]))
338 1.12 lukem exit(1);
339 1.2 thorpej break;
340 1.2 thorpej
341 1.2 thorpej case CMD_A:
342 1.32 martin if (autoflag)
343 1.32 martin do_localdevs(1);
344 1.32 martin else
345 1.32 martin do_fstab(1);
346 1.2 thorpej break;
347 1.10 mrg
348 1.10 mrg case CMD_D:
349 1.10 mrg set_dumpdev(argv[0]);
350 1.10 mrg break;
351 1.12 lukem
352 1.16 mrg case CMD_z:
353 1.30 martin if (!get_dumpdev())
354 1.30 martin exit(1);
355 1.16 mrg break;
356 1.16 mrg
357 1.12 lukem case CMD_U:
358 1.32 martin if (autoflag)
359 1.32 martin do_localdevs(0);
360 1.32 martin else
361 1.32 martin do_fstab(0);
362 1.12 lukem break;
363 1.32 martin case CMD_q:
364 1.32 martin if (check_fstab()) {
365 1.32 martin printf("%s: there are swap or dump devices defined in "
366 1.32 martin _PATH_FSTAB "\n", getprogname());
367 1.32 martin exit(0);
368 1.32 martin } else {
369 1.32 martin printf("%s: no swap or dump devices in "
370 1.32 martin _PATH_FSTAB "\n", getprogname());
371 1.32 martin exit(1);
372 1.32 martin }
373 1.2 thorpej }
374 1.2 thorpej
375 1.2 thorpej exit(0);
376 1.2 thorpej }
377 1.2 thorpej
378 1.2 thorpej /*
379 1.2 thorpej * swapon_command: emulate the old swapon(8) program.
380 1.2 thorpej */
381 1.12 lukem static void
382 1.28 xtraeme swapon_command(int argc, char **argv)
383 1.2 thorpej {
384 1.2 thorpej int ch, fiztab = 0;
385 1.2 thorpej
386 1.4 thorpej while ((ch = getopt(argc, argv, "at:")) != -1) {
387 1.2 thorpej switch (ch) {
388 1.2 thorpej case 'a':
389 1.2 thorpej fiztab = 1;
390 1.2 thorpej break;
391 1.4 thorpej case 't':
392 1.4 thorpej if (tflag != NULL)
393 1.4 thorpej usage();
394 1.4 thorpej tflag = optarg;
395 1.4 thorpej break;
396 1.2 thorpej default:
397 1.2 thorpej goto swapon_usage;
398 1.2 thorpej }
399 1.2 thorpej }
400 1.2 thorpej argc -= optind;
401 1.2 thorpej argv += optind;
402 1.2 thorpej
403 1.2 thorpej if (fiztab) {
404 1.2 thorpej if (argc)
405 1.2 thorpej goto swapon_usage;
406 1.4 thorpej /* Sanity-check -t */
407 1.4 thorpej if (tflag != NULL) {
408 1.4 thorpej if (strcmp(tflag, "blk") != 0 &&
409 1.4 thorpej strcmp(tflag, "noblk") != 0)
410 1.4 thorpej usage();
411 1.4 thorpej }
412 1.12 lukem do_fstab(1);
413 1.2 thorpej exit(0);
414 1.4 thorpej } else if (argc == 0 || tflag != NULL)
415 1.2 thorpej goto swapon_usage;
416 1.2 thorpej
417 1.2 thorpej while (argc) {
418 1.12 lukem if (! add_swap(argv[0], pri))
419 1.12 lukem exit(1);
420 1.2 thorpej argc--;
421 1.2 thorpej argv++;
422 1.2 thorpej }
423 1.1 mrg exit(0);
424 1.2 thorpej /* NOTREACHED */
425 1.2 thorpej
426 1.2 thorpej swapon_usage:
427 1.18 cgd fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname());
428 1.18 cgd fprintf(stderr, " %s <path> ...\n", getprogname());
429 1.2 thorpej exit(1);
430 1.1 mrg }
431 1.1 mrg
432 1.1 mrg /*
433 1.1 mrg * change_priority: change the priority of a swap device.
434 1.1 mrg */
435 1.12 lukem static void
436 1.29 christos change_priority(char *path)
437 1.1 mrg {
438 1.1 mrg
439 1.1 mrg if (swapctl(SWAP_CTL, path, pri) < 0)
440 1.30 martin err(1, "%s", path);
441 1.1 mrg }
442 1.1 mrg
443 1.1 mrg /*
444 1.1 mrg * add_swap: add the pathname to the list of swap devices.
445 1.1 mrg */
446 1.12 lukem static int
447 1.29 christos add_swap(char *path, int priority)
448 1.1 mrg {
449 1.10 mrg struct stat sb;
450 1.10 mrg
451 1.10 mrg if (stat(path, &sb) < 0)
452 1.10 mrg goto oops;
453 1.10 mrg
454 1.10 mrg if (sb.st_mode & S_IROTH)
455 1.19 pooka warnx("WARNING: %s is readable by the world", path);
456 1.10 mrg if (sb.st_mode & S_IWOTH)
457 1.19 pooka warnx("WARNING: %s is writable by the world", path);
458 1.1 mrg
459 1.32 martin if (fflag || oflag) {
460 1.32 martin set_dumpdev(path);
461 1.32 martin if (oflag)
462 1.32 martin exit(0);
463 1.32 martin else
464 1.32 martin fflag = 0;
465 1.32 martin }
466 1.32 martin
467 1.32 martin if (nflag)
468 1.32 martin return 1;
469 1.32 martin
470 1.12 lukem if (swapctl(SWAP_ON, path, priority) < 0) {
471 1.10 mrg oops:
472 1.30 martin err(1, "%s", path);
473 1.12 lukem }
474 1.12 lukem return (1);
475 1.1 mrg }
476 1.1 mrg
477 1.1 mrg /*
478 1.11 mrg * delete_swap: remove the pathname to the list of swap devices.
479 1.1 mrg */
480 1.12 lukem static int
481 1.29 christos delete_swap(char *path)
482 1.1 mrg {
483 1.1 mrg
484 1.32 martin if (nflag)
485 1.32 martin return 1;
486 1.32 martin
487 1.30 martin if (swapctl(SWAP_OFF, path, pri) < 0)
488 1.30 martin err(1, "%s", path);
489 1.12 lukem return (1);
490 1.1 mrg }
491 1.1 mrg
492 1.12 lukem static void
493 1.29 christos set_dumpdev(char *path)
494 1.10 mrg {
495 1.32 martin int rv = 0;
496 1.10 mrg
497 1.32 martin if (!nflag) {
498 1.32 martin if (strcmp(path, "none") == 0)
499 1.32 martin rv = swapctl(SWAP_DUMPOFF, NULL, 0);
500 1.32 martin else
501 1.32 martin rv = swapctl(SWAP_DUMPDEV, path, 0);
502 1.32 martin }
503 1.30 martin
504 1.30 martin if (rv == -1)
505 1.30 martin err(1, "could not set dump device to %s", path);
506 1.10 mrg else
507 1.18 cgd printf("%s: setting dump device to %s\n", getprogname(), path);
508 1.16 mrg }
509 1.16 mrg
510 1.30 martin static int
511 1.28 xtraeme get_dumpdev(void)
512 1.16 mrg {
513 1.16 mrg dev_t dev;
514 1.16 mrg char *name;
515 1.16 mrg
516 1.30 martin if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1) {
517 1.16 mrg warn("could not get dump device");
518 1.30 martin return 0;
519 1.30 martin } else if (dev == NODEV) {
520 1.21 drochner printf("no dump device set\n");
521 1.30 martin return 0;
522 1.30 martin } else {
523 1.16 mrg name = devname(dev, S_IFBLK);
524 1.16 mrg printf("dump device is ");
525 1.16 mrg if (name)
526 1.16 mrg printf("%s\n", name);
527 1.16 mrg else
528 1.16 mrg printf("major %d minor %d\n", major(dev), minor(dev));
529 1.16 mrg }
530 1.30 martin return 1;
531 1.10 mrg }
532 1.10 mrg
533 1.12 lukem static void
534 1.32 martin do_localdevs(int add)
535 1.32 martin {
536 1.32 martin size_t ressize;
537 1.32 martin char *disknames, *disk;
538 1.32 martin static const char mibname[] = "hw.disknames";
539 1.32 martin
540 1.32 martin ressize = 0;
541 1.32 martin if (sysctlbyname(mibname, NULL, &ressize, NULL, 0))
542 1.32 martin return;
543 1.32 martin ressize += 200; /* add some arbitrary slope */
544 1.32 martin disknames = malloc(ressize);
545 1.32 martin if (sysctlbyname(mibname, disknames, &ressize, NULL, 0) == 0) {
546 1.32 martin for (disk = strtok(disknames, " "); disk;
547 1.32 martin disk = strtok(NULL, " "))
548 1.32 martin do_localdisk(disk, add);
549 1.32 martin }
550 1.32 martin free(disknames);
551 1.32 martin }
552 1.32 martin
553 1.32 martin static void
554 1.32 martin do_localdisk(const char *disk, int add)
555 1.32 martin {
556 1.32 martin int fd;
557 1.32 martin char dvname[MAXPATHLEN];
558 1.32 martin
559 1.32 martin if ((fd = opendisk(disk, O_RDONLY, dvname, sizeof(dvname), 0)) == -1)
560 1.32 martin return;
561 1.32 martin
562 1.32 martin if (!do_wedgesofdisk(fd, add))
563 1.32 martin do_partitionsofdisk(disk, fd, add);
564 1.32 martin
565 1.32 martin close(fd);
566 1.32 martin }
567 1.32 martin
568 1.32 martin static int
569 1.32 martin do_wedgesofdisk(int fd, int add)
570 1.32 martin {
571 1.32 martin char devicename[MAXPATHLEN];
572 1.32 martin struct dkwedge_info *dkw;
573 1.32 martin struct dkwedge_list dkwl;
574 1.32 martin size_t bufsize;
575 1.32 martin u_int i;
576 1.32 martin
577 1.32 martin dkw = NULL;
578 1.32 martin dkwl.dkwl_buf = dkw;
579 1.32 martin dkwl.dkwl_bufsize = 0;
580 1.32 martin
581 1.32 martin for (;;) {
582 1.32 martin if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
583 1.32 martin return 0;
584 1.32 martin if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
585 1.32 martin break;
586 1.32 martin bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
587 1.32 martin if (dkwl.dkwl_bufsize < bufsize) {
588 1.32 martin dkw = realloc(dkwl.dkwl_buf, bufsize);
589 1.32 martin if (dkw == NULL)
590 1.32 martin return 0;
591 1.32 martin dkwl.dkwl_buf = dkw;
592 1.32 martin dkwl.dkwl_bufsize = bufsize;
593 1.32 martin }
594 1.32 martin }
595 1.32 martin
596 1.32 martin for (i = 0; i < dkwl.dkwl_ncopied; i++) {
597 1.32 martin if (strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) != 0)
598 1.32 martin continue;
599 1.32 martin snprintf(devicename, sizeof(devicename), "%s%s", _PATH_DEV,
600 1.32 martin dkw[i].dkw_devname);
601 1.32 martin devicename[sizeof(devicename)-1] = '\0';
602 1.32 martin
603 1.32 martin if (add) {
604 1.32 martin if (add_swap(devicename, pri)) {
605 1.32 martin printf(
606 1.32 martin "%s: adding %s as swap device at priority 0\n",
607 1.32 martin getprogname(), devicename);
608 1.32 martin }
609 1.32 martin } else {
610 1.32 martin if (delete_swap(devicename)) {
611 1.32 martin printf(
612 1.32 martin "%s: removing %s as swap device\n",
613 1.32 martin getprogname(), devicename);
614 1.32 martin }
615 1.32 martin }
616 1.32 martin
617 1.32 martin }
618 1.32 martin
619 1.32 martin free(dkw);
620 1.32 martin return dkwl.dkwl_nwedges != 0;
621 1.32 martin }
622 1.32 martin
623 1.32 martin static int
624 1.32 martin do_partitionsofdisk(const char *prefix, int fd, int add)
625 1.32 martin {
626 1.32 martin char devicename[MAXPATHLEN];
627 1.32 martin struct disklabel lab;
628 1.32 martin uint i;
629 1.32 martin
630 1.32 martin if (ioctl(fd, DIOCGDINFO, &lab) != 0)
631 1.32 martin return 0;
632 1.32 martin
633 1.32 martin for (i = 0; i < lab.d_npartitions; i++) {
634 1.32 martin if (lab.d_partitions[i].p_fstype != FS_SWAP)
635 1.32 martin continue;
636 1.32 martin snprintf(devicename, sizeof(devicename), "%s%s%c", _PATH_DEV,
637 1.32 martin prefix, 'a'+i);
638 1.32 martin devicename[sizeof(devicename)-1] = '\0';
639 1.32 martin
640 1.32 martin if (add) {
641 1.32 martin if (add_swap(devicename, pri)) {
642 1.32 martin printf(
643 1.32 martin "%s: adding %s as swap device at priority 0\n",
644 1.32 martin getprogname(), devicename);
645 1.32 martin }
646 1.32 martin } else {
647 1.32 martin if (delete_swap(devicename)) {
648 1.32 martin printf(
649 1.32 martin "%s: removing %s as swap device\n",
650 1.32 martin getprogname(), devicename);
651 1.32 martin }
652 1.32 martin }
653 1.32 martin }
654 1.32 martin
655 1.32 martin return 1;
656 1.32 martin }
657 1.32 martin
658 1.32 martin static int
659 1.32 martin check_fstab(void)
660 1.32 martin {
661 1.32 martin struct fstab *fp;
662 1.32 martin
663 1.32 martin while ((fp = getfsent()) != NULL) {
664 1.32 martin if (strcmp(fp->fs_type, "dp") == 0)
665 1.32 martin return 1;
666 1.32 martin
667 1.32 martin if (strcmp(fp->fs_type, "sw") == 0)
668 1.32 martin return 1;
669 1.32 martin }
670 1.32 martin
671 1.32 martin return 0;
672 1.32 martin }
673 1.32 martin
674 1.32 martin static void
675 1.28 xtraeme do_fstab(int add)
676 1.1 mrg {
677 1.1 mrg struct fstab *fp;
678 1.1 mrg char *s;
679 1.1 mrg long priority;
680 1.4 thorpej struct stat st;
681 1.4 thorpej int isblk;
682 1.8 mrg int gotone = 0;
683 1.27 lukem
684 1.27 lukem #ifdef RESCUEDIR
685 1.27 lukem #define PATH_MOUNT RESCUEDIR "/mount_nfs"
686 1.27 lukem #define PATH_UMOUNT RESCUEDIR "/umount"
687 1.27 lukem #else
688 1.12 lukem #define PATH_MOUNT "/sbin/mount_nfs"
689 1.12 lukem #define PATH_UMOUNT "/sbin/umount"
690 1.27 lukem #endif
691 1.27 lukem
692 1.12 lukem char cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
693 1.1 mrg
694 1.1 mrg #define PRIORITYEQ "priority="
695 1.1 mrg #define NFSMNTPT "nfsmntpt="
696 1.5 mikel while ((fp = getfsent()) != NULL) {
697 1.28 xtraeme char *spec;
698 1.1 mrg
699 1.10 mrg spec = fp->fs_spec;
700 1.12 lukem cmd[0] = '\0';
701 1.10 mrg
702 1.12 lukem if (strcmp(fp->fs_type, "dp") == 0 && add) {
703 1.10 mrg set_dumpdev(spec);
704 1.10 mrg continue;
705 1.10 mrg }
706 1.10 mrg
707 1.1 mrg if (strcmp(fp->fs_type, "sw") != 0)
708 1.1 mrg continue;
709 1.20 jdolecek
710 1.20 jdolecek /* handle dp as mnt option */
711 1.20 jdolecek if (strstr(fp->fs_mntops, "dp") && add)
712 1.20 jdolecek set_dumpdev(spec);
713 1.20 jdolecek
714 1.4 thorpej isblk = 0;
715 1.1 mrg
716 1.5 mikel if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
717 1.1 mrg s += sizeof(PRIORITYEQ) - 1;
718 1.1 mrg priority = atol(s);
719 1.1 mrg } else
720 1.1 mrg priority = pri;
721 1.1 mrg
722 1.5 mikel if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
723 1.12 lukem char *t;
724 1.1 mrg
725 1.4 thorpej /*
726 1.4 thorpej * Skip this song and dance if we're only
727 1.4 thorpej * doing block devices.
728 1.4 thorpej */
729 1.12 lukem if (tflag != NULL && strcmp(tflag, "blk") == 0)
730 1.4 thorpej continue;
731 1.4 thorpej
732 1.1 mrg t = strpbrk(s, ",");
733 1.1 mrg if (t != 0)
734 1.1 mrg *t = '\0';
735 1.1 mrg spec = strdup(s + strlen(NFSMNTPT));
736 1.1 mrg if (t != 0)
737 1.1 mrg *t = ',';
738 1.1 mrg
739 1.1 mrg if (spec == NULL)
740 1.1 mrg errx(1, "Out of memory");
741 1.1 mrg
742 1.1 mrg if (strlen(spec) == 0) {
743 1.1 mrg warnx("empty mountpoint");
744 1.28 xtraeme free(spec);
745 1.1 mrg continue;
746 1.1 mrg }
747 1.12 lukem if (add) {
748 1.12 lukem snprintf(cmd, sizeof(cmd), "%s %s %s",
749 1.12 lukem PATH_MOUNT, fp->fs_spec, spec);
750 1.12 lukem if (system(cmd) != 0) {
751 1.12 lukem warnx("%s: mount failed", fp->fs_spec);
752 1.12 lukem continue;
753 1.12 lukem }
754 1.12 lukem } else {
755 1.12 lukem snprintf(cmd, sizeof(cmd), "%s %s",
756 1.12 lukem PATH_UMOUNT, fp->fs_spec);
757 1.1 mrg }
758 1.4 thorpej } else {
759 1.4 thorpej /*
760 1.4 thorpej * Determine blk-ness.
761 1.4 thorpej */
762 1.4 thorpej if (stat(spec, &st) < 0) {
763 1.15 itojun warn("%s", spec);
764 1.4 thorpej continue;
765 1.4 thorpej }
766 1.4 thorpej if (S_ISBLK(st.st_mode))
767 1.4 thorpej isblk = 1;
768 1.4 thorpej }
769 1.4 thorpej
770 1.4 thorpej /*
771 1.4 thorpej * Skip this type if we're told to.
772 1.4 thorpej */
773 1.4 thorpej if (tflag != NULL) {
774 1.4 thorpej if (strcmp(tflag, "blk") == 0 && isblk == 0)
775 1.4 thorpej continue;
776 1.4 thorpej if (strcmp(tflag, "noblk") == 0 && isblk == 1)
777 1.4 thorpej continue;
778 1.1 mrg }
779 1.1 mrg
780 1.12 lukem if (add) {
781 1.12 lukem if (add_swap(spec, (int)priority)) {
782 1.12 lukem gotone = 1;
783 1.12 lukem printf(
784 1.12 lukem "%s: adding %s as swap device at priority %d\n",
785 1.18 cgd getprogname(), fp->fs_spec, (int)priority);
786 1.12 lukem }
787 1.12 lukem } else {
788 1.12 lukem if (delete_swap(spec)) {
789 1.12 lukem gotone = 1;
790 1.12 lukem printf(
791 1.12 lukem "%s: removing %s as swap device\n",
792 1.18 cgd getprogname(), fp->fs_spec);
793 1.12 lukem }
794 1.12 lukem if (cmd[0]) {
795 1.12 lukem if (system(cmd) != 0) {
796 1.12 lukem warnx("%s: umount failed", fp->fs_spec);
797 1.12 lukem continue;
798 1.12 lukem }
799 1.12 lukem }
800 1.8 mrg }
801 1.1 mrg
802 1.1 mrg if (spec != fp->fs_spec)
803 1.28 xtraeme free(spec);
804 1.1 mrg }
805 1.8 mrg if (gotone == 0)
806 1.8 mrg exit(1);
807 1.1 mrg }
808 1.1 mrg
809 1.12 lukem static void
810 1.28 xtraeme usage(void)
811 1.1 mrg {
812 1.18 cgd const char *progname = getprogname();
813 1.1 mrg
814 1.32 martin fprintf(stderr, "usage: %s -A [-f|-o] [-n] [-p priority] "
815 1.32 martin "[-t blk|noblk|auto]\n", progname);
816 1.18 cgd fprintf(stderr, " %s -a [-p priority] path\n", progname);
817 1.32 martin fprintf(stderr, " %s -q\n", progname);
818 1.18 cgd fprintf(stderr, " %s -c -p priority path\n", progname);
819 1.31 wiz fprintf(stderr, " %s -D dumpdev|none\n", progname);
820 1.18 cgd fprintf(stderr, " %s -d path\n", progname);
821 1.25 mrg fprintf(stderr, " %s -l | -s [-k|-m|-g|-h]\n", progname);
822 1.32 martin fprintf(stderr, " %s -U [-n] [-t blk|noblk|auto]\n", progname);
823 1.26 cjep fprintf(stderr, " %s -z\n", progname);
824 1.1 mrg exit(1);
825 1.1 mrg }
826