2005-06-12 20:24:20 +04:00
|
|
|
/* $NetBSD: swapctl.c,v 1.29 2005/06/12 16:24:20 christos Exp $ */
|
1997-06-12 17:14:11 +04:00
|
|
|
|
|
|
|
/*
|
1999-04-26 05:02:25 +04:00
|
|
|
* Copyright (c) 1996, 1997, 1999 Matthew R. Green
|
1997-06-12 17:14:11 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1997-10-10 09:39:47 +04:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
1997-06-12 17:14:11 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swapctl command:
|
1999-02-23 20:00:53 +03:00
|
|
|
* -A add all devices listed as `sw' in /etc/fstab (also
|
2000-03-14 01:55:05 +03:00
|
|
|
* (sets the dump device, if listed in fstab)
|
1999-02-23 20:00:53 +03:00
|
|
|
* -D <dev> set dumpdev to <dev>
|
2000-11-17 14:43:41 +03:00
|
|
|
* -z show dumpdev
|
2000-03-05 14:35:22 +03:00
|
|
|
* -U remove all devices listed as `sw' in /etc/fstab.
|
|
|
|
* -t [blk|noblk] if -A or -U , add (remove) either all block device
|
|
|
|
* or all non-block devices
|
1997-06-12 17:14:11 +04:00
|
|
|
* -a <dev> add this device
|
2000-11-17 15:35:09 +03:00
|
|
|
* -d <dev> remove this swap device
|
2003-12-20 16:31:42 +03:00
|
|
|
* -g use gigabytes
|
2003-12-20 14:22:25 +03:00
|
|
|
* -h use humanize_number(3) for listing
|
1997-06-12 17:14:11 +04:00
|
|
|
* -l list swap devices
|
2003-12-20 16:31:42 +03:00
|
|
|
* -m use megabytes
|
1997-06-12 17:14:11 +04:00
|
|
|
* -s short listing of swap devices
|
|
|
|
* -k use kilobytes
|
|
|
|
* -p <pri> use this priority
|
|
|
|
* -c change priority
|
1997-06-15 07:47:53 +04:00
|
|
|
*
|
|
|
|
* or, if invoked as "swapon" (compatibility mode):
|
|
|
|
*
|
|
|
|
* -a all devices listed as `sw' in /etc/fstab
|
1997-06-26 03:18:10 +04:00
|
|
|
* -t same as -t above (feature not present in old
|
|
|
|
* swapon(8) command)
|
1997-06-15 07:47:53 +04:00
|
|
|
* <dev> add this device
|
1997-06-12 17:14:11 +04:00
|
|
|
*/
|
2003-06-23 15:53:35 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
2005-06-12 20:24:20 +04:00
|
|
|
__RCSID("$NetBSD: swapctl.c,v 1.29 2005/06/12 16:24:20 christos Exp $");
|
2003-06-23 15:53:35 +04:00
|
|
|
#endif
|
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
1997-06-26 03:18:10 +04:00
|
|
|
#include <sys/stat.h>
|
2000-04-13 12:29:56 +04:00
|
|
|
#include <sys/swap.h>
|
1997-06-12 17:14:11 +04:00
|
|
|
|
|
|
|
#include <unistd.h>
|
1997-06-24 09:22:38 +04:00
|
|
|
#include <err.h>
|
1997-06-12 17:14:11 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
|
|
|
|
#include "swapctl.h"
|
|
|
|
|
1997-06-15 07:47:53 +04:00
|
|
|
int command;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Commands for swapctl(8). These are mutually exclusive.
|
|
|
|
*/
|
2000-03-05 14:35:22 +03:00
|
|
|
#define CMD_A 0x01 /* process /etc/fstab for adding */
|
1999-02-23 20:00:53 +03:00
|
|
|
#define CMD_D 0x02 /* set dumpdev */
|
2000-03-05 14:35:22 +03:00
|
|
|
#define CMD_U 0x04 /* process /etc/fstab for removing */
|
|
|
|
#define CMD_a 0x08 /* add a swap file/device */
|
|
|
|
#define CMD_c 0x10 /* change priority of a swap file/device */
|
|
|
|
#define CMD_d 0x20 /* delete a swap file/device */
|
|
|
|
#define CMD_l 0x40 /* list swap files/devices */
|
|
|
|
#define CMD_s 0x80 /* summary of swap files/devices */
|
2000-11-17 14:43:41 +03:00
|
|
|
#define CMD_z 0x100 /* show dump device */
|
1997-06-15 07:47:53 +04:00
|
|
|
|
|
|
|
#define SET_COMMAND(cmd) \
|
|
|
|
do { \
|
|
|
|
if (command) \
|
|
|
|
usage(); \
|
|
|
|
command = (cmd); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Commands that require a "path" argument at the end of the command
|
|
|
|
* line, and the ones which require that none exist.
|
|
|
|
*/
|
1999-02-23 20:00:53 +03:00
|
|
|
#define REQUIRE_PATH (CMD_D | CMD_a | CMD_c | CMD_d)
|
2000-11-17 14:43:41 +03:00
|
|
|
#define REQUIRE_NOPATH (CMD_A | CMD_U | CMD_l | CMD_s | CMD_z)
|
1997-06-15 07:47:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Option flags, and the commands with which they are valid.
|
|
|
|
*/
|
2003-12-20 16:31:42 +03:00
|
|
|
int kflag; /* display in 1K^x blocks */
|
1997-06-15 07:47:53 +04:00
|
|
|
#define KFLAG_CMDS (CMD_l | CMD_s)
|
2003-12-20 16:31:42 +03:00
|
|
|
#define MFLAG_CMDS (CMD_l | CMD_s)
|
|
|
|
#define GFLAG_CMDS (CMD_l | CMD_s)
|
1997-06-15 07:47:53 +04:00
|
|
|
|
2003-12-20 14:22:25 +03:00
|
|
|
int hflag; /* display with humanize_number */
|
|
|
|
#define HFLAG_CMDS (CMD_l | CMD_s)
|
|
|
|
|
1997-06-15 07:47:53 +04:00
|
|
|
int pflag; /* priority was specified */
|
|
|
|
#define PFLAG_CMDS (CMD_A | CMD_a | CMD_c)
|
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
char *tflag; /* swap device type (blk or noblk) */
|
2000-11-17 14:43:41 +03:00
|
|
|
#define TFLAG_CMDS (CMD_A | CMD_U)
|
1997-06-26 03:18:10 +04:00
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
int pri; /* uses 0 as default pri */
|
|
|
|
|
2005-06-12 20:24:20 +04:00
|
|
|
static void change_priority(char *);
|
|
|
|
static int add_swap(char *, int);
|
|
|
|
static int delete_swap(char *);
|
|
|
|
static void set_dumpdev(char *);
|
2005-02-09 08:51:38 +03:00
|
|
|
static void get_dumpdev(void);
|
|
|
|
static void do_fstab(int);
|
|
|
|
static void usage(void);
|
|
|
|
static void swapon_command(int, char **);
|
1997-06-15 07:47:53 +04:00
|
|
|
#if 0
|
2005-02-09 08:51:38 +03:00
|
|
|
static void swapoff_command(int, char **);
|
1997-06-15 07:47:53 +04:00
|
|
|
#endif
|
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
int
|
2005-02-09 08:51:38 +03:00
|
|
|
main(int argc, char *argv[])
|
1997-06-12 17:14:11 +04:00
|
|
|
{
|
|
|
|
int c;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
2001-02-20 01:48:57 +03:00
|
|
|
if (strcmp(getprogname(), "swapon") == 0) {
|
1997-06-15 07:47:53 +04:00
|
|
|
swapon_command(argc, argv);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2001-02-20 01:48:57 +03:00
|
|
|
if (strcmp(getprogname(), "swapoff") == 0) {
|
1997-06-15 07:47:53 +04:00
|
|
|
swapoff_command(argc, argv);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-12-20 16:31:42 +03:00
|
|
|
while ((c = getopt(argc, argv, "ADUacdghklmp:st:z")) != -1) {
|
1997-06-15 07:47:53 +04:00
|
|
|
switch (c) {
|
1997-06-12 17:14:11 +04:00
|
|
|
case 'A':
|
1997-06-15 07:47:53 +04:00
|
|
|
SET_COMMAND(CMD_A);
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1999-02-23 20:00:53 +03:00
|
|
|
case 'D':
|
|
|
|
SET_COMMAND(CMD_D);
|
|
|
|
break;
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
case 'U':
|
|
|
|
SET_COMMAND(CMD_U);
|
|
|
|
break;
|
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
case 'a':
|
1997-06-15 07:47:53 +04:00
|
|
|
SET_COMMAND(CMD_a);
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
case 'c':
|
1997-06-15 07:47:53 +04:00
|
|
|
SET_COMMAND(CMD_c);
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
case 'd':
|
1997-06-15 07:47:53 +04:00
|
|
|
SET_COMMAND(CMD_d);
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
2003-12-20 16:31:42 +03:00
|
|
|
case 'g':
|
|
|
|
kflag = 3; /* 1k ^ 3 */
|
|
|
|
break;
|
|
|
|
|
2003-12-20 14:22:25 +03:00
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
|
|
|
|
2003-12-20 16:31:42 +03:00
|
|
|
case 'k':
|
|
|
|
kflag = 1;
|
|
|
|
break;
|
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
case 'l':
|
1997-06-15 07:47:53 +04:00
|
|
|
SET_COMMAND(CMD_l);
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
2003-12-20 16:31:42 +03:00
|
|
|
case 'm':
|
|
|
|
kflag = 2; /* 1k ^ 2 */
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
1997-06-15 07:47:53 +04:00
|
|
|
/* XXX strtol() */
|
1997-06-12 17:14:11 +04:00
|
|
|
pri = atoi(optarg);
|
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
case 's':
|
1997-06-15 07:47:53 +04:00
|
|
|
SET_COMMAND(CMD_s);
|
1997-06-12 17:14:11 +04:00
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
case 't':
|
|
|
|
if (tflag != NULL)
|
|
|
|
usage();
|
|
|
|
tflag = optarg;
|
|
|
|
break;
|
|
|
|
|
2000-11-17 14:43:41 +03:00
|
|
|
case 'z':
|
|
|
|
SET_COMMAND(CMD_z);
|
|
|
|
break;
|
|
|
|
|
1997-06-15 07:47:53 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
|
|
|
}
|
1997-06-15 07:47:53 +04:00
|
|
|
|
|
|
|
/* Did the user specify a command? */
|
|
|
|
if (command == 0)
|
1997-06-12 17:14:11 +04:00
|
|
|
usage();
|
|
|
|
|
|
|
|
argv += optind;
|
1997-06-15 07:47:53 +04:00
|
|
|
argc -= optind;
|
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
|
|
|
if (command & REQUIRE_PATH)
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if (command & REQUIRE_NOPATH)
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-06-12 17:14:11 +04:00
|
|
|
usage();
|
1997-06-15 07:47:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* To change priority, you have to specify one. */
|
|
|
|
if ((command == CMD_c) && pflag == 0)
|
1997-06-12 17:14:11 +04:00
|
|
|
usage();
|
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
/* Sanity-check -t */
|
|
|
|
if (tflag != NULL) {
|
2000-03-05 14:35:22 +03:00
|
|
|
if (command != CMD_A && command != CMD_U)
|
1997-06-26 03:18:10 +04:00
|
|
|
usage();
|
|
|
|
if (strcmp(tflag, "blk") != 0 &&
|
|
|
|
strcmp(tflag, "noblk") != 0)
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1997-06-15 07:47:53 +04:00
|
|
|
/* Dispatch the command. */
|
|
|
|
switch (command) {
|
|
|
|
case CMD_l:
|
2003-12-20 14:22:25 +03:00
|
|
|
list_swap(pri, kflag, pflag, 0, 1, hflag);
|
1997-06-15 07:47:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_s:
|
2003-12-20 14:22:25 +03:00
|
|
|
list_swap(pri, kflag, pflag, 0, 0, hflag);
|
1997-06-15 07:47:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_c:
|
1997-06-12 17:14:11 +04:00
|
|
|
change_priority(argv[0]);
|
1997-06-15 07:47:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_a:
|
2000-03-05 14:35:22 +03:00
|
|
|
if (! add_swap(argv[0], pri))
|
|
|
|
exit(1);
|
1997-06-15 07:47:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_d:
|
2000-03-05 14:35:22 +03:00
|
|
|
if (! delete_swap(argv[0]))
|
|
|
|
exit(1);
|
1997-06-15 07:47:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CMD_A:
|
2000-03-05 14:35:22 +03:00
|
|
|
do_fstab(1);
|
1997-06-15 07:47:53 +04:00
|
|
|
break;
|
1999-02-23 20:00:53 +03:00
|
|
|
|
|
|
|
case CMD_D:
|
|
|
|
set_dumpdev(argv[0]);
|
|
|
|
break;
|
2000-03-05 14:35:22 +03:00
|
|
|
|
2000-11-17 14:43:41 +03:00
|
|
|
case CMD_z:
|
|
|
|
get_dumpdev();
|
|
|
|
break;
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
case CMD_U:
|
|
|
|
do_fstab(0);
|
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* swapon_command: emulate the old swapon(8) program.
|
|
|
|
*/
|
2000-03-05 14:35:22 +03:00
|
|
|
static void
|
2005-02-09 08:51:38 +03:00
|
|
|
swapon_command(int argc, char **argv)
|
1997-06-15 07:47:53 +04:00
|
|
|
{
|
|
|
|
int ch, fiztab = 0;
|
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
while ((ch = getopt(argc, argv, "at:")) != -1) {
|
1997-06-15 07:47:53 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
fiztab = 1;
|
|
|
|
break;
|
1997-06-26 03:18:10 +04:00
|
|
|
case 't':
|
|
|
|
if (tflag != NULL)
|
|
|
|
usage();
|
|
|
|
tflag = optarg;
|
|
|
|
break;
|
1997-06-15 07:47:53 +04:00
|
|
|
default:
|
|
|
|
goto swapon_usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (fiztab) {
|
|
|
|
if (argc)
|
|
|
|
goto swapon_usage;
|
1997-06-26 03:18:10 +04:00
|
|
|
/* Sanity-check -t */
|
|
|
|
if (tflag != NULL) {
|
|
|
|
if (strcmp(tflag, "blk") != 0 &&
|
|
|
|
strcmp(tflag, "noblk") != 0)
|
|
|
|
usage();
|
|
|
|
}
|
2000-03-05 14:35:22 +03:00
|
|
|
do_fstab(1);
|
1997-06-15 07:47:53 +04:00
|
|
|
exit(0);
|
1997-06-26 03:18:10 +04:00
|
|
|
} else if (argc == 0 || tflag != NULL)
|
1997-06-15 07:47:53 +04:00
|
|
|
goto swapon_usage;
|
|
|
|
|
|
|
|
while (argc) {
|
2000-03-05 14:35:22 +03:00
|
|
|
if (! add_swap(argv[0], pri))
|
|
|
|
exit(1);
|
1997-06-15 07:47:53 +04:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
1997-06-12 17:14:11 +04:00
|
|
|
exit(0);
|
1997-06-15 07:47:53 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
swapon_usage:
|
2001-02-20 01:48:57 +03:00
|
|
|
fprintf(stderr, "usage: %s -a [-t blk|noblk]\n", getprogname());
|
|
|
|
fprintf(stderr, " %s <path> ...\n", getprogname());
|
1997-06-15 07:47:53 +04:00
|
|
|
exit(1);
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* change_priority: change the priority of a swap device.
|
|
|
|
*/
|
2000-03-05 14:35:22 +03:00
|
|
|
static void
|
2005-06-12 20:24:20 +04:00
|
|
|
change_priority(char *path)
|
1997-06-12 17:14:11 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (swapctl(SWAP_CTL, path, pri) < 0)
|
|
|
|
warn("%s", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add_swap: add the pathname to the list of swap devices.
|
|
|
|
*/
|
2000-03-05 14:35:22 +03:00
|
|
|
static int
|
2005-06-12 20:24:20 +04:00
|
|
|
add_swap(char *path, int priority)
|
1997-06-12 17:14:11 +04:00
|
|
|
{
|
1999-02-23 20:00:53 +03:00
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(path, &sb) < 0)
|
|
|
|
goto oops;
|
|
|
|
|
|
|
|
if (sb.st_mode & S_IROTH)
|
2001-11-26 18:23:38 +03:00
|
|
|
warnx("WARNING: %s is readable by the world", path);
|
1999-02-23 20:00:53 +03:00
|
|
|
if (sb.st_mode & S_IWOTH)
|
2001-11-26 18:23:38 +03:00
|
|
|
warnx("WARNING: %s is writable by the world", path);
|
1997-06-12 17:14:11 +04:00
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
if (swapctl(SWAP_ON, path, priority) < 0) {
|
1999-02-23 20:00:53 +03:00
|
|
|
oops:
|
2000-03-05 14:35:22 +03:00
|
|
|
warn("%s", path);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-04-26 05:02:25 +04:00
|
|
|
* delete_swap: remove the pathname to the list of swap devices.
|
1997-06-12 17:14:11 +04:00
|
|
|
*/
|
2000-03-05 14:35:22 +03:00
|
|
|
static int
|
2005-06-12 20:24:20 +04:00
|
|
|
delete_swap(char *path)
|
1997-06-12 17:14:11 +04:00
|
|
|
{
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
if (swapctl(SWAP_OFF, path, pri) < 0) {
|
|
|
|
warn("%s", path);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
static void
|
2005-06-12 20:24:20 +04:00
|
|
|
set_dumpdev(char *path)
|
1999-02-23 20:00:53 +03:00
|
|
|
{
|
|
|
|
|
2003-10-21 06:32:54 +04:00
|
|
|
if (swapctl(SWAP_DUMPDEV, path, 0) == -1)
|
1999-02-23 20:00:53 +03:00
|
|
|
warn("could not set dump device to %s", path);
|
|
|
|
else
|
2001-02-20 01:48:57 +03:00
|
|
|
printf("%s: setting dump device to %s\n", getprogname(), path);
|
1999-02-23 20:00:53 +03:00
|
|
|
}
|
|
|
|
|
2000-11-17 14:43:41 +03:00
|
|
|
static void
|
2005-02-09 08:51:38 +03:00
|
|
|
get_dumpdev(void)
|
2000-11-17 14:43:41 +03:00
|
|
|
{
|
|
|
|
dev_t dev;
|
|
|
|
char *name;
|
|
|
|
|
2003-10-21 06:32:54 +04:00
|
|
|
if (swapctl(SWAP_GETDUMPDEV, &dev, 0) == -1)
|
2000-11-17 14:43:41 +03:00
|
|
|
warn("could not get dump device");
|
2002-09-18 14:18:17 +04:00
|
|
|
else if (dev == NODEV)
|
|
|
|
printf("no dump device set\n");
|
2000-11-17 14:43:41 +03:00
|
|
|
else {
|
|
|
|
name = devname(dev, S_IFBLK);
|
|
|
|
printf("dump device is ");
|
|
|
|
if (name)
|
|
|
|
printf("%s\n", name);
|
|
|
|
else
|
|
|
|
printf("major %d minor %d\n", major(dev), minor(dev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
static void
|
2005-02-09 08:51:38 +03:00
|
|
|
do_fstab(int add)
|
1997-06-12 17:14:11 +04:00
|
|
|
{
|
|
|
|
struct fstab *fp;
|
|
|
|
char *s;
|
|
|
|
long priority;
|
1997-06-26 03:18:10 +04:00
|
|
|
struct stat st;
|
|
|
|
int isblk;
|
1998-02-10 06:14:32 +03:00
|
|
|
int gotone = 0;
|
2005-01-10 23:53:48 +03:00
|
|
|
|
|
|
|
#ifdef RESCUEDIR
|
|
|
|
#define PATH_MOUNT RESCUEDIR "/mount_nfs"
|
|
|
|
#define PATH_UMOUNT RESCUEDIR "/umount"
|
|
|
|
#else
|
2000-03-05 14:35:22 +03:00
|
|
|
#define PATH_MOUNT "/sbin/mount_nfs"
|
|
|
|
#define PATH_UMOUNT "/sbin/umount"
|
2005-01-10 23:53:48 +03:00
|
|
|
#endif
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
char cmd[2*PATH_MAX+sizeof(PATH_MOUNT)+2];
|
1997-06-12 17:14:11 +04:00
|
|
|
|
|
|
|
#define PRIORITYEQ "priority="
|
|
|
|
#define NFSMNTPT "nfsmntpt="
|
1997-07-11 07:36:27 +04:00
|
|
|
while ((fp = getfsent()) != NULL) {
|
2005-02-09 08:51:38 +03:00
|
|
|
char *spec;
|
1997-06-12 17:14:11 +04:00
|
|
|
|
1999-02-23 20:00:53 +03:00
|
|
|
spec = fp->fs_spec;
|
2000-03-05 14:35:22 +03:00
|
|
|
cmd[0] = '\0';
|
1999-02-23 20:00:53 +03:00
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
if (strcmp(fp->fs_type, "dp") == 0 && add) {
|
1999-02-23 20:00:53 +03:00
|
|
|
set_dumpdev(spec);
|
1997-06-12 17:14:11 +04:00
|
|
|
continue;
|
1999-02-23 20:00:53 +03:00
|
|
|
}
|
1997-06-12 17:14:11 +04:00
|
|
|
|
1999-02-23 20:00:53 +03:00
|
|
|
if (strcmp(fp->fs_type, "sw") != 0)
|
|
|
|
continue;
|
2002-06-21 13:04:16 +04:00
|
|
|
|
|
|
|
/* handle dp as mnt option */
|
|
|
|
if (strstr(fp->fs_mntops, "dp") && add)
|
|
|
|
set_dumpdev(spec);
|
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
isblk = 0;
|
1997-06-12 17:14:11 +04:00
|
|
|
|
1997-07-11 07:36:27 +04:00
|
|
|
if ((s = strstr(fp->fs_mntops, PRIORITYEQ)) != NULL) {
|
1997-06-12 17:14:11 +04:00
|
|
|
s += sizeof(PRIORITYEQ) - 1;
|
|
|
|
priority = atol(s);
|
|
|
|
} else
|
|
|
|
priority = pri;
|
|
|
|
|
1997-07-11 07:36:27 +04:00
|
|
|
if ((s = strstr(fp->fs_mntops, NFSMNTPT)) != NULL) {
|
2000-03-05 14:35:22 +03:00
|
|
|
char *t;
|
1997-06-12 17:14:11 +04:00
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
/*
|
|
|
|
* Skip this song and dance if we're only
|
|
|
|
* doing block devices.
|
|
|
|
*/
|
2000-03-05 14:35:22 +03:00
|
|
|
if (tflag != NULL && strcmp(tflag, "blk") == 0)
|
1997-06-26 03:18:10 +04:00
|
|
|
continue;
|
|
|
|
|
1997-06-12 17:14:11 +04:00
|
|
|
t = strpbrk(s, ",");
|
|
|
|
if (t != 0)
|
|
|
|
*t = '\0';
|
|
|
|
spec = strdup(s + strlen(NFSMNTPT));
|
|
|
|
if (t != 0)
|
|
|
|
*t = ',';
|
|
|
|
|
|
|
|
if (spec == NULL)
|
|
|
|
errx(1, "Out of memory");
|
|
|
|
|
|
|
|
if (strlen(spec) == 0) {
|
|
|
|
warnx("empty mountpoint");
|
2005-02-09 08:51:38 +03:00
|
|
|
free(spec);
|
1997-06-12 17:14:11 +04:00
|
|
|
continue;
|
|
|
|
}
|
2000-03-05 14:35:22 +03:00
|
|
|
if (add) {
|
|
|
|
snprintf(cmd, sizeof(cmd), "%s %s %s",
|
|
|
|
PATH_MOUNT, fp->fs_spec, spec);
|
|
|
|
if (system(cmd) != 0) {
|
|
|
|
warnx("%s: mount failed", fp->fs_spec);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf(cmd, sizeof(cmd), "%s %s",
|
|
|
|
PATH_UMOUNT, fp->fs_spec);
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
1997-06-26 03:18:10 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Determine blk-ness.
|
|
|
|
*/
|
|
|
|
if (stat(spec, &st) < 0) {
|
2000-07-07 16:29:09 +04:00
|
|
|
warn("%s", spec);
|
1997-06-26 03:18:10 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S_ISBLK(st.st_mode))
|
|
|
|
isblk = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip this type if we're told to.
|
|
|
|
*/
|
|
|
|
if (tflag != NULL) {
|
|
|
|
if (strcmp(tflag, "blk") == 0 && isblk == 0)
|
|
|
|
continue;
|
|
|
|
if (strcmp(tflag, "noblk") == 0 && isblk == 1)
|
|
|
|
continue;
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
if (add) {
|
|
|
|
if (add_swap(spec, (int)priority)) {
|
|
|
|
gotone = 1;
|
|
|
|
printf(
|
|
|
|
"%s: adding %s as swap device at priority %d\n",
|
2001-02-20 01:48:57 +03:00
|
|
|
getprogname(), fp->fs_spec, (int)priority);
|
2000-03-05 14:35:22 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (delete_swap(spec)) {
|
|
|
|
gotone = 1;
|
|
|
|
printf(
|
|
|
|
"%s: removing %s as swap device\n",
|
2001-02-20 01:48:57 +03:00
|
|
|
getprogname(), fp->fs_spec);
|
2000-03-05 14:35:22 +03:00
|
|
|
}
|
|
|
|
if (cmd[0]) {
|
|
|
|
if (system(cmd) != 0) {
|
|
|
|
warnx("%s: umount failed", fp->fs_spec);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
1998-02-10 06:14:32 +03:00
|
|
|
}
|
1997-06-12 17:14:11 +04:00
|
|
|
|
|
|
|
if (spec != fp->fs_spec)
|
2005-02-09 08:51:38 +03:00
|
|
|
free(spec);
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
1998-02-10 06:14:32 +03:00
|
|
|
if (gotone == 0)
|
|
|
|
exit(1);
|
1997-06-12 17:14:11 +04:00
|
|
|
}
|
|
|
|
|
2000-03-05 14:35:22 +03:00
|
|
|
static void
|
2005-02-09 08:51:38 +03:00
|
|
|
usage(void)
|
1997-06-12 17:14:11 +04:00
|
|
|
{
|
2001-02-20 01:48:57 +03:00
|
|
|
const char *progname = getprogname();
|
1997-06-15 07:47:53 +04:00
|
|
|
|
1997-06-26 03:18:10 +04:00
|
|
|
fprintf(stderr, "usage: %s -A [-p priority] [-t blk|noblk]\n",
|
2001-02-20 01:48:57 +03:00
|
|
|
progname);
|
|
|
|
fprintf(stderr, " %s -D dumppath\n", progname);
|
|
|
|
fprintf(stderr, " %s -U [-t blk|noblk]\n", progname);
|
|
|
|
fprintf(stderr, " %s -a [-p priority] path\n", progname);
|
|
|
|
fprintf(stderr, " %s -c -p priority path\n", progname);
|
|
|
|
fprintf(stderr, " %s -d path\n", progname);
|
2003-12-20 16:31:42 +03:00
|
|
|
fprintf(stderr, " %s -l | -s [-k|-m|-g|-h]\n", progname);
|
2004-06-08 12:13:16 +04:00
|
|
|
fprintf(stderr, " %s -z\n", progname);
|
1997-06-12 17:14:11 +04:00
|
|
|
exit(1);
|
|
|
|
}
|