Home | History | Annotate | Line # | Download | only in vnconfig
vnconfig.c revision 1.11
      1 /*	$NetBSD: vnconfig.c,v 1.11 1997/07/30 22:54:48 jtc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1993 University of Utah.
     41  * Copyright (c) 1990, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * the Systems Programming Group of the University of Utah Computer
     46  * Science Department.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
     77  *
     78  *	@(#)vnconfig.c	8.1 (Berkeley) 12/15/93
     79  */
     80 
     81 #include <sys/param.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/mount.h>
     84 #include <sys/stat.h>
     85 #include <sys/buf.h>
     86 #include <sys/disklabel.h>
     87 #include <sys/disk.h>
     88 
     89 #include <dev/vndvar.h>
     90 
     91 #include <err.h>
     92 #include <errno.h>
     93 #include <fcntl.h>
     94 #include <stdio.h>
     95 #include <stdlib.h>
     96 #include <string.h>
     97 #include <unistd.h>
     98 
     99 #define VND_CONFIG	1
    100 #define VND_UNCONFIG	2
    101 
    102 int	verbose = 0;
    103 char	*tabname;
    104 
    105 int	config __P((char *, char *, char *, int));
    106 char	*rawdevice __P((char *));
    107 int	getgeom __P((struct vndgeom *, char *));
    108 void	usage __P((void));
    109 
    110 int
    111 main(argc, argv)
    112 	int argc;
    113 	char *argv[];
    114 {
    115 	int ch, rv, action = VND_CONFIG;
    116 
    117 	while ((ch = getopt(argc, argv, "ct:uv")) != -1) {
    118 		switch (ch) {
    119 		case 'c':
    120 			action = VND_CONFIG;
    121 			break;
    122 		case 't':
    123 			tabname = optarg;
    124 			break;
    125 		case 'u':
    126 			action = VND_UNCONFIG;
    127 			break;
    128 		case 'v':
    129 			verbose = 1;
    130 			break;
    131 		default:
    132 		case '?':
    133 			usage();
    134 			/* NOTREACHED */
    135 		}
    136 	}
    137 	argc -= optind;
    138 	argv += optind;
    139 
    140 	if (action == VND_CONFIG) {
    141 		if ((argc < 2 || argc > 3) ||
    142 		    (argc == 3 && tabname != NULL))
    143 			usage();
    144 		rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL,
    145 		    action);
    146 	} else {
    147 		if (argc != 1 || tabname != NULL)
    148 			usage();
    149 		rv = config(argv[0], NULL, NULL, action);
    150 	}
    151 	exit(rv);
    152 }
    153 
    154 int
    155 config(dev, file, geom, action)
    156 	char *dev, *file, *geom;
    157 	int action;
    158 {
    159 	struct vnd_ioctl vndio;
    160 	struct disklabel *lp;
    161 	char *rdev;
    162 	int fd, rv;
    163 
    164 	rdev = rawdevice(dev);
    165 	fd = open(rdev, O_RDWR, 0666);
    166 	if (fd < 0) {
    167 		warn(rdev);
    168 		(void) free(rdev);
    169 		return (1);
    170 	}
    171 	(void) free(rdev);
    172 
    173 	memset(&vndio, 0, sizeof(vndio));
    174 #ifdef __GNUC__
    175 	rv = 0;			/* XXX */
    176 #endif
    177 
    178 	vndio.vnd_file = file;
    179 	if (geom != NULL) {
    180 		rv = getgeom(&vndio.vnd_geom, geom);
    181 		if (rv != 0)
    182 			errx(1, "invalid geometry: %s", geom);
    183 		vndio.vnd_flags = VNDIOF_HASGEOM;
    184 	} else if (tabname != NULL) {
    185 		lp = getdiskbyname(tabname);
    186 		if (lp == NULL)
    187 			errx(1, "unknown disk type: %s", tabname);
    188 		vndio.vnd_geom.vng_secsize = lp->d_secsize;
    189 		vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
    190 		vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
    191 		vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
    192 		vndio.vnd_flags = VNDIOF_HASGEOM;
    193 	}
    194 
    195 	/*
    196 	 * Clear (un-configure) the device
    197 	 */
    198 	if (action == VND_UNCONFIG) {
    199 		rv = ioctl(fd, VNDIOCCLR, &vndio);
    200 		if (rv)
    201 			warn("VNDIOCCLR");
    202 		else if (verbose)
    203 			printf("%s: cleared\n", dev);
    204 	}
    205 	/*
    206 	 * Configure the device
    207 	 */
    208 	if (action == VND_CONFIG) {
    209 		rv = ioctl(fd, VNDIOCSET, &vndio);
    210 		if (rv)
    211 			warn("VNDIOCSET");
    212 		else if (verbose) {
    213 			printf("%s: %d bytes on %s", dev, vndio.vnd_size,
    214 			    file);
    215 			if (vndio.vnd_flags & VNDIOF_HASGEOM)
    216 				printf(" using geometry %d/%d/%d/%d",
    217 				    vndio.vnd_geom.vng_secsize,
    218 				    vndio.vnd_geom.vng_nsectors,
    219 				    vndio.vnd_geom.vng_ntracks,
    220 				    vndio.vnd_geom.vng_ncylinders);
    221 			printf("\n");
    222 		}
    223 
    224 	}
    225 
    226 	(void) close(fd);
    227 	fflush(stdout);
    228 	return (rv < 0);
    229 }
    230 
    231 int
    232 getgeom(vng, cp)
    233 	struct vndgeom *vng;
    234 	char *cp;
    235 {
    236 	char *secsize, *nsectors, *ntracks, *ncylinders;
    237 
    238 #define	GETARG(arg) \
    239 	do { \
    240 		if (cp == NULL || *cp == '\0') \
    241 			return (1); \
    242 		arg = strsep(&cp, "/"); \
    243 		if (arg == NULL) \
    244 			return (1); \
    245 	} while (0)
    246 
    247 	GETARG(secsize);
    248 	GETARG(nsectors);
    249 	GETARG(ntracks);
    250 	GETARG(ncylinders);
    251 
    252 #undef GETARG
    253 
    254 	/* Too many? */
    255 	if (cp != NULL)
    256 		return (1);
    257 
    258 #define	CVTARG(str, num) \
    259 	do { \
    260 		num = strtol(str, &cp, 10); \
    261 		if (*cp != '\0') \
    262 			return (1); \
    263 	} while (0)
    264 
    265 	CVTARG(secsize, vng->vng_secsize);
    266 	CVTARG(nsectors, vng->vng_nsectors);
    267 	CVTARG(ntracks, vng->vng_ntracks);
    268 	CVTARG(ncylinders, vng->vng_ncylinders);
    269 
    270 #undef CVTARG
    271 
    272 	return (0);
    273 }
    274 
    275 char *
    276 rawdevice(dev)
    277 	char *dev;
    278 {
    279 	register char *rawbuf, *dp, *ep;
    280 	struct stat sb;
    281 	size_t len;
    282 
    283 	len = strlen(dev);
    284 	rawbuf = malloc(len + 2);
    285 	strcpy(rawbuf, dev);
    286 	if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
    287 		dp = strrchr(rawbuf, '/');
    288 		if (dp) {
    289 			for (ep = &rawbuf[len]; ep > dp; --ep)
    290 				*(ep+1) = *ep;
    291 			*++ep = 'r';
    292 		}
    293 	}
    294 	return (rawbuf);
    295 }
    296 
    297 void
    298 usage()
    299 {
    300 
    301 	(void)fprintf(stderr, "%s%s",
    302 	    "usage: vnconfig [-c] [-t typename] [-v] special-file"
    303 		" regular-file [geomspec]\n",
    304 	    "       vnconfig -u [-v] special-file\n");
    305 	exit(1);
    306 }
    307