Index: head/sbin/mdconfig/mdconfig.8 =================================================================== --- head/sbin/mdconfig/mdconfig.8 (revision 221231) +++ head/sbin/mdconfig/mdconfig.8 (revision 221232) @@ -1,268 +1,279 @@ .\" Copyright (c) 1993 University of Utah. .\" Copyright (c) 1980, 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" Copyright (c) 2000 .\" Poul-Henning Kamp All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the Systems Programming Group of the University of Utah Computer .\" Science Department. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)vnconfig.8 8.1 (Berkeley) 6/5/93 .\" from: src/usr.sbin/vnconfig/vnconfig.8,v 1.19 2000/12/27 15:30:29 .\" .\" $FreeBSD$ .\" .Dd June 21, 2008 .Dt MDCONFIG 8 .Os .Sh NAME .Nm mdconfig .Nd configure and enable memory disks .Sh SYNOPSIS .Nm .Fl a .Fl t Ar type .Op Fl n .Oo Fl o Oo Cm no Oc Ns Ar option Oc ... .Op Fl f Ar file .Op Fl s Ar size .Op Fl S Ar sectorsize .Op Fl u Ar unit .Op Fl x Ar sectors/track .Op Fl y Ar heads/cylinder .Nm .Fl d .Fl u Ar unit .Op Fl o Oo Cm no Oc Ns Ar force .Nm .Fl l .Op Fl n .Op Fl v .Op Fl u Ar unit +.Nm +.Ar file .Sh DESCRIPTION The .Nm utility configures and enables .Xr md 4 devices. .Pp Options indicate an action to be performed: .Bl -tag -width indent .It Fl a Attach a memory disk. This will configure and attach a memory disk with the parameters specified and attach it to the system. .It Fl d Detach a memory disk from the system and release all resources. .It Fl t Ar type Select the type of the memory disk. .Bl -tag -width "preload" .It Cm malloc Storage for this type of memory disk is allocated with .Xr malloc 9 . This limits the size to the malloc bucket limit in the kernel. If the .Fl o Cm reserve option is not set, creating and filling a large malloc-backed memory disk is a very easy way to panic a system. .It Cm vnode A file specified with .Fl f Ar file becomes the backing store for this memory disk. .It Cm swap Storage for this type of memory disk is allocated from buffer memory. Pages get pushed out to the swap when the system is under memory pressure, otherwise they stay in the operating memory. Using .Cm swap backing is generally preferable over .Cm malloc backing. .El .It Fl f Ar file Filename to use for the vnode type memory disk. Options .Fl a and .Fl t Ar vnode are implied if not specified. .It Fl l List configured devices. If given with .Fl u , display details about that particular device. If .Fl v option specified, show all details. .It Fl n When printing md device names, print only the unit number without the md prefix. .It Fl s Ar size Size of the memory disk. .Ar Size is the number of 512 byte sectors unless suffixed with a .Cm b , k , m , g , or .Cm t which denotes byte, kilobyte, megabyte, gigabyte and terabyte respectively. Options .Fl a and .Fl t Ar swap are implied if not specified. .It Fl S Ar sectorsize Sectorsize to use for malloc backed device. .It Fl x Ar sectors/track See the description of the .Fl y option below. .It Fl y Ar heads/cylinder For .Cm malloc or .Cm vnode backed devices, the .Fl x and .Fl y options can be used to specify a synthetic geometry. This is useful for constructing bootable images for later download to other devices. .It Fl o Oo Cm no Oc Ns Ar option Set or reset options. .Bl -tag -width indent .It Oo Cm no Oc Ns Cm async For .Cm vnode backed devices: avoid .Dv IO_SYNC for increased performance but at the risk of deadlocking the entire kernel. .It Oo Cm no Oc Ns Cm reserve Allocate and reserve all needed storage from the start, rather than as needed. .It Oo Cm no Oc Ns Cm cluster Enable clustering on this disk. .It Oo Cm no Oc Ns Cm compress Enable/Disable compression features to reduce memory usage. .It Oo Cm no Oc Ns Cm force Disable/Enable extra sanity checks to prevent the user from doing something that might adversely affect the system. .It Oo Cm no Oc Ns Cm readonly Enable/Disable readonly mode. .El .It Fl u Ar unit Request a specific unit number for the .Xr md 4 device instead of automatic allocation. .El +.Pp +The last form, +.Nm +.Ar file , +is provided for convenience as an abbreviation of +.Nm +.Fl a +.Fl t Ar vnode +.Fl f Ar file . .Sh EXAMPLES To create a 4 megabyte .Xr malloc 9 backed memory disk. The name of the allocated unit will be output on stdout like .Dq Li md3 : .Pp .Dl mdconfig -a -t malloc -s 4m .Pp To create a disk named .Pa /dev/md4 with .Pa /tmp/boot.flp as backing storage: .Pp .Dl mdconfig -a -t vnode -f /tmp/boot.flp -u 4 .Pp To detach and free all resources used by .Pa /dev/md4 : .Pp .Dl mdconfig -d -u 4 .Pp To create a 128MByte swap backed disk, initialize an .Xr ffs 7 file system on it, and mount it on .Pa /tmp : .Bd -literal -offset indent mdconfig -a -t swap -s 128M -u 10 newfs -U /dev/md10 mount /dev/md10 /tmp chmod 1777 /tmp .Ed .Pp To create a 5MB file-backed disk .Ns ( Fl a and .Fl t Ar vnode are implied): .Bd -literal -offset indent dd if=/dev/zero of=somebackingfile bs=1k count=5k mdconfig -f somebackingfile -u 0 bsdlabel -w md0 auto newfs md0c mount /dev/md0c /mnt .Ed .Pp To create an .Xr md 4 device out of an ISO 9660 CD image file .Ns ( Fl a and .Fl t Ar vnode are implied), using the first available .Xr md 4 device, and then mount the new memory disk: .Bd -literal -offset indent mount -t cd9660 /dev/`mdconfig -f cdimage.iso` /mnt .Ed .Sh SEE ALSO .Xr md 4 , .Xr ffs 7 , .Xr bsdlabel 8 , .Xr fdisk 8 , .Xr mdmfs 8 , .Xr malloc 9 .Sh HISTORY The .Nm utility first appeared in .Fx 5.0 as a cleaner replacement for the .Xr vn 4 and .Xr vnconfig 8 combo. .Sh AUTHORS The .Nm utility was written by .An Poul-Henning Kamp .Aq phk@FreeBSD.org . Index: head/sbin/mdconfig/mdconfig.c =================================================================== --- head/sbin/mdconfig/mdconfig.c (revision 221231) +++ head/sbin/mdconfig/mdconfig.c (revision 221232) @@ -1,476 +1,494 @@ /* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * * $FreeBSD$ * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static struct md_ioctl mdio; static enum {UNSET, ATTACH, DETACH, LIST} action = UNSET; static int nflag; static void usage(void); +static void md_set_file(const char *); static int md_find(char *, const char *); static int md_query(char *name); static int md_list(char *units, int opt); static char *geom_config_get(struct gconf *g, const char *name); static void md_prthumanval(char *length); #define OPT_VERBOSE 0x01 #define OPT_UNIT 0x02 #define OPT_DONE 0x04 #define OPT_LIST 0x10 #define CLASS_NAME_MD "MD" static void usage(void) { fprintf(stderr, "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" " [-s size] [-S sectorsize] [-u unit]\n" " [-x sectors/track] [-y heads/cylinder]\n" " mdconfig -d -u unit [-o [no]force]\n" -" mdconfig -l [-v] [-n] [-u unit]\n"); +" mdconfig -l [-v] [-n] [-u unit]\n" +" mdconfig file\n"); fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n"); fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); fprintf(stderr, "\t\tsize = %%d (512 byte blocks), %%db (B),\n"); fprintf(stderr, "\t\t %%dk (kB), %%dm (MB), %%dg (GB) or\n"); fprintf(stderr, "\t\t %%dt (TB)\n"); exit(1); } int main(int argc, char **argv) { int ch, fd, i, vflag; char *p; int cmdline = 0; char *mdunit = NULL; bzero(&mdio, sizeof(mdio)); mdio.md_file = malloc(PATH_MAX); if (mdio.md_file == NULL) err(1, "could not allocate memory"); vflag = 0; bzero(mdio.md_file, PATH_MAX); - for (;;) { - ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:"); - if (ch == -1) - break; + while ((ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:vx:y:")) != -1) { switch (ch) { case 'a': if (cmdline != 0) usage(); action = ATTACH; cmdline = 1; break; case 'd': if (cmdline != 0) usage(); action = DETACH; mdio.md_options = MD_AUTOUNIT; cmdline = 3; break; case 'l': if (cmdline != 0) usage(); action = LIST; mdio.md_options = MD_AUTOUNIT; cmdline = 3; break; case 'n': nflag = 1; break; case 't': if (cmdline != 1) usage(); if (!strcmp(optarg, "malloc")) { mdio.md_type = MD_MALLOC; mdio.md_options = MD_AUTOUNIT | MD_COMPRESS; } else if (!strcmp(optarg, "preload")) { mdio.md_type = MD_PRELOAD; mdio.md_options = 0; } else if (!strcmp(optarg, "vnode")) { mdio.md_type = MD_VNODE; mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; } else if (!strcmp(optarg, "swap")) { mdio.md_type = MD_SWAP; mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; } else { usage(); } cmdline=2; break; case 'f': if (cmdline == 0) { action = ATTACH; cmdline = 1; } if (cmdline == 1) { /* Imply ``-t vnode'' */ mdio.md_type = MD_VNODE; mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; cmdline = 2; } if (cmdline != 2) usage(); - if (realpath(optarg, mdio.md_file) == NULL) { - err(1, "could not find full path for %s", - optarg); - } - fd = open(mdio.md_file, O_RDONLY); - if (fd < 0) - err(1, "could not open %s", optarg); - else if (mdio.md_mediasize == 0) { - struct stat sb; - - if (fstat(fd, &sb) == -1) - err(1, "could not stat %s", optarg); - mdio.md_mediasize = sb.st_size; - } - close(fd); + md_set_file(optarg); break; case 'o': if (action == DETACH) { if (!strcmp(optarg, "force")) mdio.md_options |= MD_FORCE; else if (!strcmp(optarg, "noforce")) mdio.md_options &= ~MD_FORCE; else errx(1, "Unknown option: %s.", optarg); break; } if (cmdline != 2) usage(); if (!strcmp(optarg, "async")) mdio.md_options |= MD_ASYNC; else if (!strcmp(optarg, "noasync")) mdio.md_options &= ~MD_ASYNC; else if (!strcmp(optarg, "cluster")) mdio.md_options |= MD_CLUSTER; else if (!strcmp(optarg, "nocluster")) mdio.md_options &= ~MD_CLUSTER; else if (!strcmp(optarg, "compress")) mdio.md_options |= MD_COMPRESS; else if (!strcmp(optarg, "nocompress")) mdio.md_options &= ~MD_COMPRESS; else if (!strcmp(optarg, "force")) mdio.md_options |= MD_FORCE; else if (!strcmp(optarg, "noforce")) mdio.md_options &= ~MD_FORCE; else if (!strcmp(optarg, "readonly")) mdio.md_options |= MD_READONLY; else if (!strcmp(optarg, "noreadonly")) mdio.md_options &= ~MD_READONLY; else if (!strcmp(optarg, "reserve")) mdio.md_options |= MD_RESERVE; else if (!strcmp(optarg, "noreserve")) mdio.md_options &= ~MD_RESERVE; else errx(1, "Unknown option: %s.", optarg); break; case 'S': if (cmdline != 2) usage(); mdio.md_sectorsize = strtoul(optarg, &p, 0); break; case 's': if (cmdline == 0) { /* Imply ``-a'' */ action = ATTACH; cmdline = 1; } if (cmdline == 1) { /* Imply ``-t swap'' */ mdio.md_type = MD_SWAP; mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; cmdline = 2; } if (cmdline != 2) usage(); mdio.md_mediasize = (off_t)strtoumax(optarg, &p, 0); if (p == NULL || *p == '\0') mdio.md_mediasize *= DEV_BSIZE; else if (*p == 'b' || *p == 'B') ; /* do nothing */ else if (*p == 'k' || *p == 'K') mdio.md_mediasize <<= 10; else if (*p == 'm' || *p == 'M') mdio.md_mediasize <<= 20; else if (*p == 'g' || *p == 'G') mdio.md_mediasize <<= 30; else if (*p == 't' || *p == 'T') { mdio.md_mediasize <<= 30; mdio.md_mediasize <<= 10; } else errx(1, "Unknown suffix on -s argument"); break; case 'u': if (cmdline != 2 && cmdline != 3) usage(); if (!strncmp(optarg, "/dev/", 5)) optarg += 5; if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) optarg += sizeof(MD_NAME) - 1; mdio.md_unit = strtoul(optarg, &p, 0); if (mdio.md_unit == (unsigned)ULONG_MAX || *p != '\0') errx(1, "bad unit: %s", optarg); mdunit = optarg; mdio.md_options &= ~MD_AUTOUNIT; break; case 'v': if (cmdline != 3) usage(); vflag = OPT_VERBOSE; break; case 'x': if (cmdline != 2) usage(); mdio.md_fwsectors = strtoul(optarg, &p, 0); break; case 'y': if (cmdline != 2) usage(); mdio.md_fwheads = strtoul(optarg, &p, 0); break; default: usage(); } } + + argc -= optind; + argv += optind; + if (action == UNSET) { + if (argc != 1) + usage(); + action = ATTACH; + mdio.md_type = MD_VNODE; + mdio.md_options = MD_CLUSTER | MD_AUTOUNIT | MD_COMPRESS; + cmdline = 2; + md_set_file(*argv); + } + mdio.md_version = MDIOVERSION; if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) err(1, "failed to load geom_md module"); fd = open("/dev/" MDCTL_NAME, O_RDWR, 0); if (fd < 0) err(1, "open(/dev/%s)", MDCTL_NAME); if (cmdline == 2 && (mdio.md_type == MD_MALLOC || mdio.md_type == MD_SWAP)) if (mdio.md_mediasize == 0) errx(1, "must specify -s for -t malloc or -t swap"); if (cmdline == 2 && mdio.md_type == MD_VNODE) if (mdio.md_file[0] == '\0') errx(1, "must specify -f for -t vnode"); if (mdio.md_type == MD_VNODE && (mdio.md_options & MD_READONLY) == 0) { if (access(mdio.md_file, W_OK) < 0 && (errno == EACCES || errno == EPERM || errno == EROFS)) { fprintf(stderr, "WARNING: opening backing store: %s readonly\n", mdio.md_file); mdio.md_options |= MD_READONLY; } } if (action == LIST) { if (mdio.md_options & MD_AUTOUNIT) { /* * Listing all devices. This is why we pass NULL * together with OPT_LIST. */ md_list(NULL, OPT_LIST | vflag); } else { return (md_query(mdunit)); } } else if (action == ATTACH) { if (cmdline < 2) usage(); i = ioctl(fd, MDIOCATTACH, &mdio); if (i < 0) err(1, "ioctl(/dev/%s)", MDCTL_NAME); if (mdio.md_options & MD_AUTOUNIT) printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); } else if (action == DETACH) { if (mdio.md_options & MD_AUTOUNIT) usage(); i = ioctl(fd, MDIOCDETACH, &mdio); if (i < 0) err(1, "ioctl(/dev/%s)", MDCTL_NAME); } else usage(); close (fd); return (0); +} + +static void +md_set_file(const char *fn) +{ + struct stat sb; + int fd; + + if (realpath(fn, mdio.md_file) == NULL) + err(1, "could not find full path for %s", fn); + fd = open(mdio.md_file, O_RDONLY); + if (fd < 0) + err(1, "could not open %s", fn); + if (fstat(fd, &sb) == -1) + err(1, "could not stat %s", fn); + if (!S_ISREG(sb.st_mode)) + errx(1, "%s is not a regular file", fn); + if (mdio.md_mediasize == 0) + mdio.md_mediasize = sb.st_size; + close(fd); } /* * Lists md(4) disks. Is used also as a query routine, since it handles XML * interface. 'units' can be NULL for listing memory disks. It might be * coma-separated string containing md(4) disk names. 'opt' distinguished * between list and query mode. */ static int md_list(char *units, int opt) { struct gmesh gm; struct gprovider *pp; struct gconf *gc; struct gident *gid; struct devstat *gsp; struct ggeom *gg; struct gclass *gcl; void *sq; int retcode, found; char *type, *file, *length; type = file = length = NULL; retcode = geom_gettree(&gm); if (retcode != 0) return (-1); retcode = geom_stats_open(); if (retcode != 0) return (-1); sq = geom_stats_snapshot_get(); if (sq == NULL) return (-1); found = 0; while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { gid = geom_lookupid(&gm, gsp->id); if (gid == NULL) continue; if (gid->lg_what == ISPROVIDER) { pp = gid->lg_ptr; gg = pp->lg_geom; gcl = gg->lg_class; if (strcmp(gcl->lg_name, CLASS_NAME_MD) != 0) continue; if ((opt & OPT_UNIT) && (units != NULL)) { retcode = md_find(units, pp->lg_name); if (retcode != 1) continue; else found = 1; } gc = &pp->lg_config; if (nflag && strncmp(pp->lg_name, "md", 2) == 0) printf("%s", pp->lg_name + 2); else printf("%s", pp->lg_name); if (opt & OPT_VERBOSE || opt & OPT_UNIT) { type = geom_config_get(gc, "type"); if (strcmp(type, "vnode") == 0) file = geom_config_get(gc, "file"); length = geom_config_get(gc, "length"); printf("\t%s\t", type); if (length != NULL) md_prthumanval(length); if (file != NULL) { printf("\t%s", file); file = NULL; } } opt |= OPT_DONE; if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE)) printf(" "); else printf("\n"); } } if ((opt & OPT_LIST) && (opt & OPT_DONE) && !(opt & OPT_VERBOSE)) printf("\n"); /* XXX: Check if it's enough to clean everything. */ geom_stats_snapshot_free(sq); if ((opt & OPT_UNIT) && found) return (0); else return (-1); } /* * Returns value of 'name' from gconfig structure. */ static char * geom_config_get(struct gconf *g, const char *name) { struct gconfig *gce; LIST_FOREACH(gce, g, lg_config) { if (strcmp(gce->lg_name, name) == 0) return (gce->lg_val); } return (NULL); } /* * List is comma separated list of MD disks. name is a * device name we look for. Returns 1 if found and 0 * otherwise. */ static int md_find(char *list, const char *name) { int ret; char num[16]; char *ptr, *p, *u; ret = 0; ptr = strdup(list); if (ptr == NULL) return (-1); for (p = ptr; (u = strsep(&p, ",")) != NULL;) { if (strncmp(u, "/dev/", 5) == 0) u += 5; /* Just in case user specified number instead of full name */ snprintf(num, sizeof(num), "md%s", u); if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { ret = 1; break; } } free(ptr); return (ret); } static void md_prthumanval(char *length) { char buf[6]; uintmax_t bytes; char *endptr; errno = 0; bytes = strtoumax(length, &endptr, 10); if (errno != 0 || *endptr != '\0' || bytes > INT64_MAX) return; humanize_number(buf, sizeof(buf), (int64_t)bytes, "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); (void)printf("%6s", buf); } int md_query(char *name) { return (md_list(name, OPT_UNIT)); }