1997-07-08 10:37:16 +04:00
|
|
|
/* $NetBSD: vnconfig.c,v 1.10 1997/07/08 06:37:16 mikel Exp $ */
|
1997-06-24 01:11:06 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 REGENTS OR CONTRIBUTORS 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.
|
|
|
|
*/
|
1995-12-30 21:14:01 +03:00
|
|
|
|
1993-12-21 07:13:39 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993 University of Utah.
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* the Systems Programming Group of the University of Utah Computer
|
|
|
|
* Science Department.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
* from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
|
|
|
|
*
|
|
|
|
* @(#)vnconfig.c 8.1 (Berkeley) 12/15/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
1997-06-24 01:11:06 +04:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/disk.h>
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
#include <dev/vndvar.h>
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1995-01-22 09:18:12 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1997-06-24 01:11:06 +04:00
|
|
|
#include <fcntl.h>
|
1995-01-22 09:18:12 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-07-08 10:37:16 +04:00
|
|
|
#include <unistd.h>
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1995-01-25 07:32:37 +03:00
|
|
|
#define VND_CONFIG 1
|
|
|
|
#define VND_UNCONFIG 2
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
int verbose = 0;
|
|
|
|
char *tabname;
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1997-07-08 10:37:16 +04:00
|
|
|
int config __P((char *, char *, char *, int));
|
1997-06-24 01:11:06 +04:00
|
|
|
char *rawdevice __P((char *));
|
|
|
|
int getgeom __P((struct vndgeom *, char *));
|
|
|
|
void usage __P((void));
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
int
|
1993-12-21 07:13:39 +03:00
|
|
|
main(argc, argv)
|
1995-01-22 09:18:12 +03:00
|
|
|
int argc;
|
1997-06-24 01:11:06 +04:00
|
|
|
char *argv[];
|
1993-12-21 07:13:39 +03:00
|
|
|
{
|
1995-01-25 07:32:37 +03:00
|
|
|
int ch, rv, action = VND_CONFIG;
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
while ((ch = getopt(argc, argv, "ct:uv")) != -1) {
|
1995-01-22 10:06:44 +03:00
|
|
|
switch (ch) {
|
1993-12-21 07:13:39 +03:00
|
|
|
case 'c':
|
1995-01-25 07:32:37 +03:00
|
|
|
action = VND_CONFIG;
|
1993-12-21 07:13:39 +03:00
|
|
|
break;
|
1997-06-24 01:11:06 +04:00
|
|
|
case 't':
|
|
|
|
tabname = optarg;
|
|
|
|
break;
|
1993-12-21 07:13:39 +03:00
|
|
|
case 'u':
|
1995-01-25 07:32:37 +03:00
|
|
|
action = VND_UNCONFIG;
|
1993-12-21 07:13:39 +03:00
|
|
|
break;
|
|
|
|
case 'v':
|
1995-01-22 10:06:44 +03:00
|
|
|
verbose = 1;
|
1993-12-21 07:13:39 +03:00
|
|
|
break;
|
|
|
|
default:
|
1995-01-22 10:06:44 +03:00
|
|
|
case '?':
|
1993-12-21 07:13:39 +03:00
|
|
|
usage();
|
1995-01-22 09:18:12 +03:00
|
|
|
/* NOTREACHED */
|
1993-12-21 07:13:39 +03:00
|
|
|
}
|
1995-01-22 10:06:44 +03:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-12-21 07:13:39 +03:00
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
if (action == VND_CONFIG) {
|
|
|
|
if ((argc < 2 || argc > 3) ||
|
|
|
|
(argc == 3 && tabname != NULL))
|
|
|
|
usage();
|
|
|
|
rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL,
|
|
|
|
action);
|
|
|
|
} else {
|
|
|
|
if (argc != 1 || tabname != NULL)
|
|
|
|
usage();
|
|
|
|
rv = config(argv[0], NULL, NULL, action);
|
|
|
|
}
|
1993-12-21 07:13:39 +03:00
|
|
|
exit(rv);
|
|
|
|
}
|
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
int
|
|
|
|
config(dev, file, geom, action)
|
|
|
|
char *dev, *file, *geom;
|
1995-01-22 10:06:44 +03:00
|
|
|
int action;
|
1995-01-22 09:18:12 +03:00
|
|
|
{
|
1995-01-25 07:32:37 +03:00
|
|
|
struct vnd_ioctl vndio;
|
1997-06-24 01:11:06 +04:00
|
|
|
struct disklabel *lp;
|
1995-01-22 09:18:12 +03:00
|
|
|
char *rdev;
|
1997-06-24 01:11:06 +04:00
|
|
|
int fd, rv;
|
1993-12-21 07:13:39 +03:00
|
|
|
|
|
|
|
rdev = rawdevice(dev);
|
1997-06-24 01:11:06 +04:00
|
|
|
fd = open(rdev, O_RDWR, 0666);
|
|
|
|
if (fd < 0) {
|
1995-01-22 10:06:44 +03:00
|
|
|
warn(rdev);
|
1997-07-08 10:37:16 +04:00
|
|
|
(void) free(rdev);
|
1995-01-22 10:06:44 +03:00
|
|
|
return (1);
|
1993-12-21 07:13:39 +03:00
|
|
|
}
|
1997-07-08 10:37:16 +04:00
|
|
|
(void) free(rdev);
|
1997-06-24 01:11:06 +04:00
|
|
|
|
|
|
|
memset(&vndio, 0, sizeof(vndio));
|
1997-07-08 10:37:16 +04:00
|
|
|
#ifdef __GNUC__
|
|
|
|
rv = 0; /* XXX */
|
|
|
|
#endif
|
1997-06-24 01:11:06 +04:00
|
|
|
|
1995-01-25 07:32:37 +03:00
|
|
|
vndio.vnd_file = file;
|
1997-06-24 01:11:06 +04:00
|
|
|
if (geom != NULL) {
|
|
|
|
rv = getgeom(&vndio.vnd_geom, geom);
|
1997-07-08 10:37:16 +04:00
|
|
|
if (rv != 0)
|
|
|
|
errx(1, "invalid geometry: %s", geom);
|
1997-06-24 01:11:06 +04:00
|
|
|
vndio.vnd_flags = VNDIOF_HASGEOM;
|
|
|
|
} else if (tabname != NULL) {
|
|
|
|
lp = getdiskbyname(tabname);
|
|
|
|
if (lp == NULL)
|
|
|
|
errx(1, "unknown disk type: %s", tabname);
|
|
|
|
vndio.vnd_geom.vng_secsize = lp->d_secsize;
|
|
|
|
vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
|
|
|
|
vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
|
|
|
|
vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
|
|
|
|
vndio.vnd_flags = VNDIOF_HASGEOM;
|
|
|
|
}
|
1993-12-21 07:13:39 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear (un-configure) the device
|
|
|
|
*/
|
1995-01-25 07:32:37 +03:00
|
|
|
if (action == VND_UNCONFIG) {
|
1997-06-24 01:11:06 +04:00
|
|
|
rv = ioctl(fd, VNDIOCCLR, &vndio);
|
1995-01-22 10:09:54 +03:00
|
|
|
if (rv)
|
1995-01-25 07:32:37 +03:00
|
|
|
warn("VNDIOCCLR");
|
1995-01-22 10:09:54 +03:00
|
|
|
else if (verbose)
|
1993-12-21 07:13:39 +03:00
|
|
|
printf("%s: cleared\n", dev);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Configure the device
|
|
|
|
*/
|
1995-01-25 07:32:37 +03:00
|
|
|
if (action == VND_CONFIG) {
|
1997-06-24 01:11:06 +04:00
|
|
|
rv = ioctl(fd, VNDIOCSET, &vndio);
|
1995-01-22 10:06:44 +03:00
|
|
|
if (rv)
|
1995-01-25 07:32:37 +03:00
|
|
|
warn("VNDIOCSET");
|
1997-06-24 01:11:06 +04:00
|
|
|
else if (verbose) {
|
|
|
|
printf("%s: %d bytes on %s", dev, vndio.vnd_size,
|
1995-01-25 07:32:37 +03:00
|
|
|
file);
|
1997-06-24 01:11:06 +04:00
|
|
|
if (vndio.vnd_flags & VNDIOF_HASGEOM)
|
|
|
|
printf(" using geometry %d/%d/%d/%d",
|
|
|
|
vndio.vnd_geom.vng_secsize,
|
|
|
|
vndio.vnd_geom.vng_nsectors,
|
|
|
|
vndio.vnd_geom.vng_ntracks,
|
|
|
|
vndio.vnd_geom.vng_ncylinders);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
1993-12-21 07:13:39 +03:00
|
|
|
}
|
1997-07-08 10:37:16 +04:00
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
(void) close(fd);
|
1993-12-21 07:13:39 +03:00
|
|
|
fflush(stdout);
|
1995-01-22 10:06:44 +03:00
|
|
|
return (rv < 0);
|
1993-12-21 07:13:39 +03:00
|
|
|
}
|
|
|
|
|
1997-06-24 01:11:06 +04:00
|
|
|
int
|
|
|
|
getgeom(vng, cp)
|
|
|
|
struct vndgeom *vng;
|
|
|
|
char *cp;
|
|
|
|
{
|
|
|
|
char *secsize, *nsectors, *ntracks, *ncylinders;
|
|
|
|
|
|
|
|
#define GETARG(arg) \
|
|
|
|
do { \
|
1997-07-08 10:37:16 +04:00
|
|
|
if (cp == NULL || *cp == '\0') \
|
1997-06-24 01:11:06 +04:00
|
|
|
return (1); \
|
|
|
|
arg = strsep(&cp, "/"); \
|
|
|
|
if (arg == NULL) \
|
|
|
|
return (1); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
GETARG(secsize);
|
|
|
|
GETARG(nsectors);
|
|
|
|
GETARG(ntracks);
|
|
|
|
GETARG(ncylinders);
|
|
|
|
|
|
|
|
#undef GETARG
|
|
|
|
|
|
|
|
/* Too many? */
|
|
|
|
if (cp != NULL)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
#define CVTARG(str, num) \
|
|
|
|
do { \
|
|
|
|
num = strtol(str, &cp, 10); \
|
|
|
|
if (*cp != '\0') \
|
|
|
|
return (1); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CVTARG(secsize, vng->vng_secsize);
|
|
|
|
CVTARG(nsectors, vng->vng_nsectors);
|
|
|
|
CVTARG(ntracks, vng->vng_ntracks);
|
|
|
|
CVTARG(ncylinders, vng->vng_ncylinders);
|
|
|
|
|
|
|
|
#undef CVTARG
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1993-12-21 07:13:39 +03:00
|
|
|
char *
|
|
|
|
rawdevice(dev)
|
|
|
|
char *dev;
|
|
|
|
{
|
|
|
|
register char *rawbuf, *dp, *ep;
|
|
|
|
struct stat sb;
|
1997-07-08 10:37:16 +04:00
|
|
|
size_t len;
|
1993-12-21 07:13:39 +03:00
|
|
|
|
|
|
|
len = strlen(dev);
|
|
|
|
rawbuf = malloc(len + 2);
|
|
|
|
strcpy(rawbuf, dev);
|
|
|
|
if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
|
1997-07-08 10:37:16 +04:00
|
|
|
dp = strrchr(rawbuf, '/');
|
1993-12-21 07:13:39 +03:00
|
|
|
if (dp) {
|
|
|
|
for (ep = &rawbuf[len]; ep > dp; --ep)
|
|
|
|
*(ep+1) = *ep;
|
|
|
|
*++ep = 'r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (rawbuf);
|
|
|
|
}
|
|
|
|
|
1995-01-22 09:18:12 +03:00
|
|
|
void
|
1993-12-21 07:13:39 +03:00
|
|
|
usage()
|
|
|
|
{
|
1995-01-22 10:06:44 +03:00
|
|
|
|
1995-01-25 07:32:37 +03:00
|
|
|
(void)fprintf(stderr, "%s%s",
|
1997-06-24 01:11:06 +04:00
|
|
|
"usage: vnconfig [-c] [-t typename] [-v] special-file"
|
|
|
|
" regular-file [geomspec]\n",
|
1995-01-25 07:32:37 +03:00
|
|
|
" vnconfig -u [-v] special-file\n");
|
1993-12-21 07:13:39 +03:00
|
|
|
exit(1);
|
|
|
|
}
|