vnconfig.c revision 1.9 1 /* $NetBSD: vnconfig.c,v 1.9 1997/06/23 21:11:08 thorpej 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 REGENTS OR CONTRIBUTORS BE
30 * 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
98 #define VND_CONFIG 1
99 #define VND_UNCONFIG 2
100
101 int verbose = 0;
102 char *tabname;
103
104 char *rawdevice __P((char *));
105 int getgeom __P((struct vndgeom *, char *));
106 void usage __P((void));
107
108 int
109 main(argc, argv)
110 int argc;
111 char *argv[];
112 {
113 int ch, rv, action = VND_CONFIG;
114
115 while ((ch = getopt(argc, argv, "ct:uv")) != -1) {
116 switch (ch) {
117 case 'c':
118 action = VND_CONFIG;
119 break;
120 case 't':
121 tabname = optarg;
122 break;
123 case 'u':
124 action = VND_UNCONFIG;
125 break;
126 case 'v':
127 verbose = 1;
128 break;
129 default:
130 case '?':
131 usage();
132 /* NOTREACHED */
133 }
134 }
135 argc -= optind;
136 argv += optind;
137
138 if (action == VND_CONFIG) {
139 if ((argc < 2 || argc > 3) ||
140 (argc == 3 && tabname != NULL))
141 usage();
142 rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL,
143 action);
144 } else {
145 if (argc != 1 || tabname != NULL)
146 usage();
147 rv = config(argv[0], NULL, NULL, action);
148 }
149 exit(rv);
150 }
151
152 int
153 config(dev, file, geom, action)
154 char *dev, *file, *geom;
155 int action;
156 {
157 struct vnd_ioctl vndio;
158 struct disklabel *lp;
159 char *rdev;
160 int fd, rv;
161
162 rdev = rawdevice(dev);
163 fd = open(rdev, O_RDWR, 0666);
164 if (fd < 0) {
165 warn(rdev);
166 return (1);
167 }
168
169 memset(&vndio, 0, sizeof(vndio));
170
171 vndio.vnd_file = file;
172 if (geom != NULL) {
173 rv = getgeom(&vndio.vnd_geom, geom);
174 vndio.vnd_flags = VNDIOF_HASGEOM;
175 } else if (tabname != NULL) {
176 lp = getdiskbyname(tabname);
177 if (lp == NULL)
178 errx(1, "unknown disk type: %s", tabname);
179 vndio.vnd_geom.vng_secsize = lp->d_secsize;
180 vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
181 vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
182 vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
183 vndio.vnd_flags = VNDIOF_HASGEOM;
184 }
185
186 /*
187 * Clear (un-configure) the device
188 */
189 if (action == VND_UNCONFIG) {
190 rv = ioctl(fd, VNDIOCCLR, &vndio);
191 if (rv)
192 warn("VNDIOCCLR");
193 else if (verbose)
194 printf("%s: cleared\n", dev);
195 }
196 /*
197 * Configure the device
198 */
199 if (action == VND_CONFIG) {
200 rv = ioctl(fd, VNDIOCSET, &vndio);
201 if (rv)
202 warn("VNDIOCSET");
203 else if (verbose) {
204 printf("%s: %d bytes on %s", dev, vndio.vnd_size,
205 file);
206 if (vndio.vnd_flags & VNDIOF_HASGEOM)
207 printf(" using geometry %d/%d/%d/%d",
208 vndio.vnd_geom.vng_secsize,
209 vndio.vnd_geom.vng_nsectors,
210 vndio.vnd_geom.vng_ntracks,
211 vndio.vnd_geom.vng_ncylinders);
212 printf("\n");
213 }
214
215 }
216 done:
217 (void) close(fd);
218 fflush(stdout);
219 return (rv < 0);
220 }
221
222 int
223 getgeom(vng, cp)
224 struct vndgeom *vng;
225 char *cp;
226 {
227 char *secsize, *nsectors, *ntracks, *ncylinders;
228
229 #define GETARG(arg) \
230 do { \
231 if (*cp == '\0') \
232 return (1); \
233 arg = strsep(&cp, "/"); \
234 if (arg == NULL) \
235 return (1); \
236 } while (0)
237
238 GETARG(secsize);
239 GETARG(nsectors);
240 GETARG(ntracks);
241 GETARG(ncylinders);
242
243 #undef GETARG
244
245 /* Too many? */
246 if (cp != NULL)
247 return (1);
248
249 #define CVTARG(str, num) \
250 do { \
251 num = strtol(str, &cp, 10); \
252 if (*cp != '\0') \
253 return (1); \
254 } while (0)
255
256 CVTARG(secsize, vng->vng_secsize);
257 CVTARG(nsectors, vng->vng_nsectors);
258 CVTARG(ntracks, vng->vng_ntracks);
259 CVTARG(ncylinders, vng->vng_ncylinders);
260
261 #undef CVTARG
262
263 return (0);
264 }
265
266 char *
267 rawdevice(dev)
268 char *dev;
269 {
270 register char *rawbuf, *dp, *ep;
271 struct stat sb;
272 int len;
273
274 len = strlen(dev);
275 rawbuf = malloc(len + 2);
276 strcpy(rawbuf, dev);
277 if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
278 dp = rindex(rawbuf, '/');
279 if (dp) {
280 for (ep = &rawbuf[len]; ep > dp; --ep)
281 *(ep+1) = *ep;
282 *++ep = 'r';
283 }
284 }
285 return (rawbuf);
286 }
287
288 void
289 usage()
290 {
291
292 (void)fprintf(stderr, "%s%s",
293 "usage: vnconfig [-c] [-t typename] [-v] special-file"
294 " regular-file [geomspec]\n",
295 " vnconfig -u [-v] special-file\n");
296 exit(1);
297 }
298