From 6c49c6cff2f51045e7c3878f6a88a1c8ce50db17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 29 Apr 2011 22:40:11 +0000 Subject: [PATCH] Somewhere around the 473rd time I mistyped "mdconfig file" instead of "mdconfig -f file", I decided that it would be easier to make mdconfig DWIM than to teach my fingers to type the correct command line. MFC after: 3 weeks --- sbin/mdconfig/mdconfig.8 | 11 ++++++++ sbin/mdconfig/mdconfig.c | 58 ++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/sbin/mdconfig/mdconfig.8 b/sbin/mdconfig/mdconfig.8 index 5998de5497bb..95fdfe886f9b 100644 --- a/sbin/mdconfig/mdconfig.8 +++ b/sbin/mdconfig/mdconfig.8 @@ -68,6 +68,8 @@ .Op Fl n .Op Fl v .Op Fl u Ar unit +.Nm +.Ar file .Sh DESCRIPTION The .Nm @@ -185,6 +187,15 @@ 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 diff --git a/sbin/mdconfig/mdconfig.c b/sbin/mdconfig/mdconfig.c index 96e2bcbff942..70f725b9e9a0 100644 --- a/sbin/mdconfig/mdconfig.c +++ b/sbin/mdconfig/mdconfig.c @@ -38,6 +38,7 @@ 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); @@ -59,7 +60,8 @@ usage(void) " [-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"); @@ -82,10 +84,7 @@ main(int argc, char **argv) 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) @@ -143,21 +142,7 @@ main(int argc, char **argv) } 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) { @@ -267,6 +252,19 @@ main(int argc, char **argv) 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) @@ -322,6 +320,26 @@ main(int argc, char **argv) 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