vnconfig.c revision 1.18 1 /* $NetBSD: vnconfig.c,v 1.18 2000/02/16 06:52:31 enami 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 #define __POOL_EXPOSE /* dev/vndvar.h uses struct pool */
82
83 #include <sys/param.h>
84 #include <sys/ioctl.h>
85 #include <sys/mount.h>
86 #include <sys/stat.h>
87 #include <sys/buf.h>
88 #include <sys/disklabel.h>
89 #include <sys/disk.h>
90
91 #include <dev/vndvar.h>
92
93 #include <disktab.h>
94 #include <err.h>
95 #include <errno.h>
96 #include <fcntl.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <util.h>
102
103 #define VND_CONFIG 1
104 #define VND_UNCONFIG 2
105
106 int verbose = 0;
107 char *tabname;
108
109 int config __P((char *, char *, char *, int));
110 int getgeom __P((struct vndgeom *, char *));
111 int main __P((int, char **));
112 char *rawdevice __P((char *));
113 void usage __P((void));
114
115 int
116 main(argc, argv)
117 int argc;
118 char *argv[];
119 {
120 int ch, rv, action = VND_CONFIG;
121
122 while ((ch = getopt(argc, argv, "ct:uv")) != -1) {
123 switch (ch) {
124 case 'c':
125 action = VND_CONFIG;
126 break;
127 case 't':
128 tabname = optarg;
129 break;
130 case 'u':
131 action = VND_UNCONFIG;
132 break;
133 case 'v':
134 verbose = 1;
135 break;
136 default:
137 case '?':
138 usage();
139 /* NOTREACHED */
140 }
141 }
142 argc -= optind;
143 argv += optind;
144
145 if (action == VND_CONFIG) {
146 if ((argc < 2 || argc > 3) ||
147 (argc == 3 && tabname != NULL))
148 usage();
149 rv = config(argv[0], argv[1], (argc == 3) ? argv[2] : NULL,
150 action);
151 } else {
152 if (argc != 1 || tabname != NULL)
153 usage();
154 rv = config(argv[0], NULL, NULL, action);
155 }
156 exit(rv);
157 }
158
159 int
160 config(dev, file, geom, action)
161 char *dev, *file, *geom;
162 int action;
163 {
164 struct vnd_ioctl vndio;
165 struct disklabel *lp;
166 char rdev[MAXPATHLEN + 1];
167 int fd, rv;
168
169 fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0);
170 if (fd < 0) {
171 warn("%s: opendisk", rdev);
172 return (1);
173 }
174
175 memset(&vndio, 0, sizeof(vndio));
176 #ifdef __GNUC__
177 rv = 0; /* XXX */
178 #endif
179
180 vndio.vnd_file = file;
181 if (geom != NULL) {
182 rv = getgeom(&vndio.vnd_geom, geom);
183 if (rv != 0)
184 errx(1, "invalid geometry: %s", geom);
185 vndio.vnd_flags = VNDIOF_HASGEOM;
186 } else if (tabname != NULL) {
187 lp = getdiskbyname(tabname);
188 if (lp == NULL)
189 errx(1, "unknown disk type: %s", tabname);
190 vndio.vnd_geom.vng_secsize = lp->d_secsize;
191 vndio.vnd_geom.vng_nsectors = lp->d_nsectors;
192 vndio.vnd_geom.vng_ntracks = lp->d_ntracks;
193 vndio.vnd_geom.vng_ncylinders = lp->d_ncylinders;
194 vndio.vnd_flags = VNDIOF_HASGEOM;
195 }
196
197 /*
198 * Clear (un-configure) the device
199 */
200 if (action == VND_UNCONFIG) {
201 rv = ioctl(fd, VNDIOCCLR, &vndio);
202 if (rv)
203 warn("%s: VNDIOCCLR", rdev);
204 else if (verbose)
205 printf("%s: cleared\n", rdev);
206 }
207 /*
208 * Configure the device
209 */
210 if (action == VND_CONFIG) {
211 rv = ioctl(fd, VNDIOCSET, &vndio);
212 if (rv)
213 warn("%s: VNDIOCSET", rdev);
214 else if (verbose) {
215 printf("%s: %d bytes on %s", rdev, vndio.vnd_size,
216 file);
217 if (vndio.vnd_flags & VNDIOF_HASGEOM)
218 printf(" using geometry %d/%d/%d/%d",
219 vndio.vnd_geom.vng_secsize,
220 vndio.vnd_geom.vng_nsectors,
221 vndio.vnd_geom.vng_ntracks,
222 vndio.vnd_geom.vng_ncylinders);
223 printf("\n");
224 }
225
226 }
227
228 (void) close(fd);
229 fflush(stdout);
230 return (rv < 0);
231 }
232
233 int
234 getgeom(vng, cp)
235 struct vndgeom *vng;
236 char *cp;
237 {
238 char *secsize, *nsectors, *ntracks, *ncylinders;
239
240 #define GETARG(arg) \
241 do { \
242 if (cp == NULL || *cp == '\0') \
243 return (1); \
244 arg = strsep(&cp, "/"); \
245 if (arg == NULL) \
246 return (1); \
247 } while (0)
248
249 GETARG(secsize);
250 GETARG(nsectors);
251 GETARG(ntracks);
252 GETARG(ncylinders);
253
254 #undef GETARG
255
256 /* Too many? */
257 if (cp != NULL)
258 return (1);
259
260 #define CVTARG(str, num) \
261 do { \
262 num = strtol(str, &cp, 10); \
263 if (*cp != '\0') \
264 return (1); \
265 } while (0)
266
267 CVTARG(secsize, vng->vng_secsize);
268 CVTARG(nsectors, vng->vng_nsectors);
269 CVTARG(ntracks, vng->vng_ntracks);
270 CVTARG(ncylinders, vng->vng_ncylinders);
271
272 #undef CVTARG
273
274 return (0);
275 }
276
277 void
278 usage()
279 {
280
281 (void)fprintf(stderr, "%s%s",
282 "usage: vnconfig [-c] [-t typename] [-v] special-file"
283 " regular-file [geomspec]\n",
284 " vnconfig -u [-v] special-file\n");
285 exit(1);
286 }
287