1 /* $NetBSD: vnconfig.c,v 1.37 2009/04/19 00:56:32 lukem Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * the Systems Programming Group of the University of Utah Computer
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
66 * @(#)vnconfig.c 8.1 (Berkeley) 12/15/93
70 * Copyright (c) 1993 University of Utah.
72 * This code is derived from software contributed to Berkeley by
73 * the Systems Programming Group of the University of Utah Computer
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. All advertising materials mentioning features or use of this software
85 * must display the following acknowledgement:
86 * This product includes software developed by the University of
87 * California, Berkeley and its contributors.
88 * 4. Neither the name of the University nor the names of its contributors
89 * may be used to endorse or promote products derived from this software
90 * without specific prior written permission.
92 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
93 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
96 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
106 * @(#)vnconfig.c 8.1 (Berkeley) 12/15/93
109 #include <sys/param.h>
110 #include <sys/ioctl.h>
111 #include <sys/mount.h>
113 #include <sys/disklabel.h>
114 #include <sys/disk.h>
116 #include <dev/vndvar.h>
130 #define VND_UNCONFIG 2
139 int config
__P((char *, char *, char *, int));
140 int getgeom
__P((struct vndgeom
*, char *));
141 int main
__P((int, char **));
142 char *rawdevice
__P((char *));
143 void usage
__P((void));
150 int ch
, rv
, action
= VND_CONFIG
;
152 while ((ch
= getopt(argc
, argv
, "Fcf:lrt:uvz")) != -1) {
161 if (setdisktab(optarg
) == -1)
174 action
= VND_UNCONFIG
;
192 if (action
== VND_CONFIG
) {
193 if ((argc
< 2 || argc
> 3) ||
194 (argc
== 3 && tabname
!= NULL
))
196 rv
= config(argv
[0], argv
[1], (argc
== 3) ? argv
[2] : NULL
,
198 } else if (action
== VND_UNCONFIG
) {
199 if (argc
!= 1 || tabname
!= NULL
)
201 rv
= config(argv
[0], NULL
, NULL
, action
);
202 } else { /* VND_GET */
208 if (argc
!= 0 && argc
!= 1)
211 vn
= argc
? argv
[0] : "vnd0";
213 v
= opendisk(vn
, O_RDONLY
, path
, sizeof(path
), 0);
215 err(1, "open: %s", vn
);
218 vnu
.vnu_unit
= argc
? -1 : n
;
219 rv
= ioctl(v
, VNDIOCGET
, &vnu
);
226 if (vnu
.vnu_ino
== 0)
227 printf("vnd%d: not in use\n",
231 struct statvfs
*mnt
= NULL
;
234 nmount
= 0; /* XXXGCC -Wuninitialized */
236 printf("vnd%d: ", vnu
.vnu_unit
);
238 dev
= devname(vnu
.vnu_dev
, S_IFBLK
);
240 nmount
= getmntinfo(&mnt
, MNT_NOWAIT
);
244 for (i
= 0; i
< nmount
; i
++) {
246 mnt
[i
].f_mntfromname
,
249 mnt
[i
].f_mntfromname
+ 5,
256 mnt
[i
].f_mntfromname
);
260 else if (dev
!= NULL
)
263 printf("dev %llu,%llu ",
264 (unsigned long long)major(vnu
.vnu_dev
),
265 (unsigned long long)minor(vnu
.vnu_dev
));
267 printf("inode %llu\n",
268 (unsigned long long)vnu
.vnu_ino
);
280 config(dev
, file
, geom
, action
)
281 char *dev
, *file
, *geom
;
284 struct vnd_ioctl vndio
;
285 struct disklabel
*lp
;
286 char rdev
[MAXPATHLEN
+ 1];
289 fd
= opendisk(dev
, O_RDWR
, rdev
, sizeof(rdev
), 0);
291 warn("%s: opendisk", rdev
);
295 memset(&vndio
, 0, sizeof(vndio
));
300 vndio
.vnd_file
= file
;
302 rv
= getgeom(&vndio
.vnd_geom
, geom
);
304 errx(1, "invalid geometry: %s", geom
);
305 vndio
.vnd_flags
= VNDIOF_HASGEOM
;
306 } else if (tabname
!= NULL
) {
307 lp
= getdiskbyname(tabname
);
309 errx(1, "unknown disk type: %s", tabname
);
310 vndio
.vnd_geom
.vng_secsize
= lp
->d_secsize
;
311 vndio
.vnd_geom
.vng_nsectors
= lp
->d_nsectors
;
312 vndio
.vnd_geom
.vng_ntracks
= lp
->d_ntracks
;
313 vndio
.vnd_geom
.vng_ncylinders
= lp
->d_ncylinders
;
314 vndio
.vnd_flags
= VNDIOF_HASGEOM
;
318 vndio
.vnd_flags
|= VNDIOF_READONLY
;
321 vndio
.vnd_flags
|= VNF_COMP
;
324 * Clear (un-configure) the device
326 if (action
== VND_UNCONFIG
) {
328 vndio
.vnd_flags
|= VNDIOF_FORCE
;
329 rv
= ioctl(fd
, VNDIOCCLR
, &vndio
);
331 if (rv
&& errno
== ENOTTY
)
332 rv
= ioctl(fd
, VNDIOOCCLR
, &vndio
);
335 warn("%s: VNDIOCCLR", rdev
);
337 printf("%s: cleared\n", rdev
);
340 * Configure the device
342 if (action
== VND_CONFIG
) {
345 ffd
= open(file
, readonly
? O_RDONLY
: O_RDWR
);
351 rv
= ioctl(fd
, VNDIOCSET
, &vndio
);
353 if (rv
&& errno
== ENOTTY
) {
354 rv
= ioctl(fd
, VNDIOOCSET
, &vndio
);
355 vndio
.vnd_size
= vndio
.vnd_osize
;
359 warn("%s: VNDIOCSET", rdev
);
361 printf("%s: %" PRIu64
" bytes on %s", rdev
,
362 vndio
.vnd_size
, file
);
363 if (vndio
.vnd_flags
& VNDIOF_HASGEOM
)
364 printf(" using geometry %d/%d/%d/%d",
365 vndio
.vnd_geom
.vng_secsize
,
366 vndio
.vnd_geom
.vng_nsectors
,
367 vndio
.vnd_geom
.vng_ntracks
,
368 vndio
.vnd_geom
.vng_ncylinders
);
384 char *secsize
, *nsectors
, *ntracks
, *ncylinders
;
386 #define GETARG(arg) \
388 if (cp == NULL || *cp == '\0') \
390 arg = strsep(&cp, "/"); \
406 #define CVTARG(str, num) \
408 num = strtol(str, &cp, 10); \
413 CVTARG(secsize
, vng
->vng_secsize
);
414 CVTARG(nsectors
, vng
->vng_nsectors
);
415 CVTARG(ntracks
, vng
->vng_ntracks
);
416 CVTARG(ncylinders
, vng
->vng_ncylinders
);
427 (void)fprintf(stderr
, "%s%s",
428 "usage: vnconfig [-crvz] [-f disktab] [-t typename] vnode_disk"
429 " regular-file [geomspec]\n",
430 " vnconfig -u [-Fv] vnode_disk\n"
431 " vnconfig -l [vnode_disk]\n");