2 * Copyright (c) 1993 University of Utah.
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
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.
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 University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$
43 static char sccsid[] = "@(#)vnconfig.c 8.1 (Berkeley) 12/15/93";
45 static const char rcsid[] =
46 "$FreeBSD: src/usr.sbin/vnconfig/vnconfig.c,v 1.13.2.7 2003/06/02 09:10:27 maxim Exp $";
58 #include <sys/param.h>
59 #include <sys/ioctl.h>
60 #include <sys/linker.h>
61 #include <sys/mount.h>
62 #include <sys/module.h>
64 #include <sys/vnioctl.h>
65 #include <ufs/ufs/ufsmount.h>
68 #define ZBUFSIZE 32768
79 #define VN_CONFIG 0x01
80 #define VN_UNCONFIG 0x02
81 #define VN_ENABLE 0x04
82 #define VN_DISABLE 0x08
84 #define VN_MOUNTRO 0x20
85 #define VN_MOUNTRW 0x40
86 #define VN_IGNORE 0x80
88 #define VN_RESET 0x200
89 #define VN_TRUNCATE 0x400
101 int config __P((struct vndisk *));
102 void getoptions __P((struct vndisk *, char *));
103 char *rawdevice __P((char *));
104 void readconfig __P((int));
105 static void usage __P((void));
106 static int getsize(const char *arg);
107 static void do_autolabel(const char *dev, const char *label);
108 int what_opt __P((char *, u_long *));
117 char *autolabel = NULL;
120 configfile = _PATH_VNTAB;
121 while ((i = getopt(argc, argv, "acdef:gr:s:S:TZL:uv")) != -1)
124 /* all -- use config file */
132 flags &= ~VN_UNCONFIG;
143 flags |= (VN_ENABLE|VN_CONFIG);
144 flags &= ~(VN_DISABLE|VN_UNCONFIG);
147 /* alternate config file */
152 /* fiddle global options */
159 for (s = strtok(optarg, ","); s; s = strtok(NULL, ",")) {
160 if (what_opt(s, &resetopt))
161 errx(1, "invalid options '%s'", s);
168 for (s = strtok(optarg, ","); s; s = strtok(NULL, ",")) {
169 if (what_opt(s, &setopt))
170 errx(1, "invalid options '%s'", s);
177 flags |= (VN_DISABLE|VN_UNCONFIG);
178 flags &= ~(VN_ENABLE|VN_CONFIG);
187 size = getsize(optarg);
189 flags &= ~VN_UNCONFIG;
193 flags |= VN_TRUNCATE;
208 if (modfind("vn") < 0)
209 if (kldload("vn") < 0 || modfind("vn") < 0)
210 warnx( "cannot find or load \"vn\" kernel module");
217 vndisks = calloc(sizeof(struct vndisk), 1);
218 if (argc < optind + 1)
220 vndisks[0].dev = argv[optind++];
221 vndisks[0].file = argv[optind++]; /* may be NULL */
222 vndisks[0].flags = flags;
223 vndisks[0].size = size;
224 vndisks[0].autolabel = autolabel;
226 getoptions(&vndisks[0], argv[optind]);
230 for (i = 0; i < nvndisks; i++)
231 rv += config(&vndisks[i]);
240 if (!strcmp(str,"reserve")) { *p |= VN_RESERVE; return 0; }
241 if (!strcmp(str,"labels")) { *p |= VN_LABELS; return 0; }
242 if (!strcmp(str,"follow")) { *p |= VN_FOLLOW; return 0; }
243 if (!strcmp(str,"debug")) { *p |= VN_DEBUG; return 0; }
244 if (!strcmp(str,"io")) { *p |= VN_IO; return 0; }
245 if (!strcmp(str,"all")) { *p |= ~0; return 0; }
246 if (!strcmp(str,"none")) { *p |= 0; return 0; }
254 char *dev, *file, *oarg;
256 struct vn_ioctl vnio;
261 int pgsize = getpagesize();
266 * Prepend "/dev/" to the specified device name, if necessary.
267 * Operate on vnp->dev because it is used later.
269 if (vnp->dev[0] != '/' && vnp->dev[0] != '.')
270 (void)asprintf(&vnp->dev, "%s%s", _PATH_DEV, vnp->dev);
276 if (flags & VN_IGNORE)
280 * When a regular file has been specified, do any requested setup
281 * of the file. Truncation (also creates the file if necessary),
282 * sizing, and zeroing.
285 if (file && vnp->size != 0 && (flags & VN_CONFIG)) {
289 if (flags & VN_TRUNCATE)
290 fd = open(file, O_RDWR|O_CREAT|O_TRUNC);
292 fd = open(file, O_RDWR);
293 if (fd >= 0 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
294 if (st.st_size < (off_t)vnp->size * pgsize)
295 ftruncate(fd, (off_t)vnp->size * pgsize);
297 st.st_size = (off_t)vnp->size * pgsize;
299 if (flags & VN_ZERO) {
300 char *buf = malloc(ZBUFSIZE);
301 bzero(buf, ZBUFSIZE);
302 while (st.st_size > 0) {
303 int n = (st.st_size > ZBUFSIZE) ?
304 ZBUFSIZE : (int)st.st_size;
305 if (write(fd, buf, n) != n) {
307 printf("Unable to ZERO file %s\n", file);
310 st.st_size -= (off_t)n;
315 printf("Unable to open file %s\n", file);
320 rdev = rawdevice(dev);
321 f = fopen(rdev, "rw");
327 vnio.vn_size = vnp->size; /* non-zero only if swap backed */
332 if (flags & VN_DISABLE) {
333 if (flags & (VN_MOUNTRO|VN_MOUNTRW)) {
334 rv = unmount(oarg, 0);
338 flags &= ~VN_UNCONFIG;
339 if ((flags & VN_UNCONFIG) == 0)
342 printf("%s: unmounted\n", dev);
346 * Clear (un-configure) the device
348 if (flags & VN_UNCONFIG) {
349 rv = ioctl(fileno(f), VNIOCDETACH, &vnio);
351 if (errno == ENODEV) {
353 printf("%s: not configured\n", dev);
360 printf("%s: cleared\n", dev);
363 * Set specified options
365 if (flags & VN_SET) {
368 rv = ioctl(fileno(f), VNIOCGSET, &l);
370 rv = ioctl(fileno(f), VNIOCUSET, &l);
375 printf("%s: flags now=%08x\n",dev,l);
378 * Reset specified options
380 if (flags & VN_RESET) {
383 rv = ioctl(fileno(f), VNIOCGCLEAR, &l);
385 rv = ioctl(fileno(f), VNIOCUCLEAR, &l);
388 warn("VNIO[GU]CLEAR");
390 printf("%s: flags now=%08x\n",dev,l);
393 * Configure the device
395 if (flags & VN_CONFIG) {
396 rv = ioctl(fileno(f), VNIOCATTACH, &vnio);
404 "%s: %d bytes on %s\n",
405 dev, vnio.vn_size, file
411 if (vnp->autolabel) {
412 do_autolabel(vnp->dev, vnp->autolabel);
419 if (flags & VN_SET) {
422 rv = ioctl(fileno(f), VNIOCGSET, &l);
424 rv = ioctl(fileno(f), VNIOCUSET, &l);
429 printf("%s: flags now=%08lx\n",dev,l);
434 if (flags & VN_RESET) {
437 rv = ioctl(fileno(f), VNIOCGCLEAR, &l);
439 rv = ioctl(fileno(f), VNIOCUCLEAR, &l);
442 warn("VNIO[GU]CLEAR");
444 printf("%s: flags now=%08lx\n",dev,l);
448 * Close the device now, as we may want to mount it.
453 * Enable special functions on the device
455 if (flags & VN_ENABLE) {
456 if (flags & VN_SWAP) {
463 printf("%s: swapping enabled\n", dev);
465 if (flags & (VN_MOUNTRO|VN_MOUNTRW)) {
466 struct ufs_args args;
470 mflags = (flags & VN_MOUNTRO) ? MNT_RDONLY : 0;
471 rv = mount("ufs", oarg, mflags, &args);
477 printf("%s: mounted on %s\n", dev, oarg);
485 #define EOL(c) ((c) == '\0' || (c) == '\n')
486 #define WHITE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
494 register char *cp, *sp;
498 f = fopen(configfile, "r");
500 err(1, "%s", configfile);
501 ix = 0; /* number of elements */
502 ax = 0; /* allocated elements */
503 while (fgets(buf, LINESIZE, f) != NULL) {
507 while (!EOL(*cp) && WHITE(*cp))
512 while (!EOL(*cp) && !WHITE(*cp))
520 vndisks = realloc(vndisks, ax * sizeof(struct vndisk));
521 bzero(&vndisks[ix], (ax - ix) * sizeof(struct vndisk));
523 vndisks[ix].dev = malloc(cp - sp);
524 strcpy(vndisks[ix].dev, sp);
525 while (!EOL(*cp) && WHITE(*cp))
530 while (!EOL(*cp) && !WHITE(*cp))
534 if (*sp == '%' && strtol(sp + 1, NULL, 0) > 0) {
535 vndisks[ix].size = getsize(sp + 1);
537 vndisks[ix].file = malloc(cp - sp);
538 strcpy(vndisks[ix].file, sp);
541 while (!EOL(*cp) && WHITE(*cp))
543 vndisks[ix].flags = flags;
546 while (!EOL(*cp) && !WHITE(*cp))
549 getoptions(&vndisks[ix], sp);
557 getoptions(vnp, fstr)
564 if (strcmp(fstr, "swap") == 0)
566 else if (strncmp(fstr, "mount=", 6) == 0) {
569 } else if (strncmp(fstr, "mountrw=", 8) == 0) {
572 } else if (strncmp(fstr, "mountro=", 8) == 0) {
575 } else if (strcmp(fstr, "ignore") == 0)
579 vnp->oarg = malloc(strlen(oarg) + 1);
580 strcpy(vnp->oarg, oarg);
589 register char *rawbuf, *dp, *ep;
594 rawbuf = malloc(len + 2);
596 if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) {
597 dp = rindex(rawbuf, '/');
599 for (ep = &rawbuf[len]; ep > dp; --ep)
610 fprintf(stderr, "%s\n%s\n%s\n",
611 "usage: vnconfig [-cdeguv] [-s option] [-r option] [-S value] special_file",
612 " [regular_file] [feature]",
613 " vnconfig -a [-cdeguv] [-s option] [-r option] [-f config_file]");
618 getsize(const char *arg)
621 int pgsize = getpagesize();
622 quad_t size = strtoq(arg, &ptr, 0);
624 switch(tolower(*ptr)) {
627 * GULP! Terrabytes. It's actually possible to create
628 * a 7.9 TB VN device, though newfs can't handle any single
629 * filesystem larger then 1 TB.
646 size = (size + pgsize - 1) / pgsize;
653 * Automatically label the device. This will wipe any preexisting
658 do_autolabel(const char *dev, const char *label)
660 /* XXX not yet implemented */
661 fprintf(stderr, "autolabel not yet implemented, sorry\n");