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