2 * Copyright (c) 2001 Dima Dorfman.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * mdmfs (md/MFS) is a wrapper around mdconfig(8),
29 * newfs(8), and mount(8) that mimics the command line option set of
30 * the deprecated mount_mfs(8).
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/mdioctl.h>
53 typedef enum { false, true } bool;
64 static bool compat; /* Full compatibility with mount_mfs? */
65 static bool debug; /* Emit debugging information? */
66 static bool loudsubs; /* Suppress output from helper programs? */
67 static bool norun; /* Actually run the helper programs? */
68 static int unit; /* The unit we're working with. */
69 static const char *mdname; /* Name of memory disk device (e.g., "md"). */
70 static size_t mdnamelen; /* Length of mdname. */
72 static void argappend(char **, const char *, ...) __printflike(2, 3);
73 static void debugprintf(const char *, ...) __printflike(1, 2);
74 static void do_mdconfig_attach(const char *, const enum md_types);
75 static void do_mdconfig_attach_au(const char *, const enum md_types);
76 static void do_mdconfig_detach(void);
77 static void do_mount(const char *, const char *);
78 static void do_mtptsetup(const char *, struct mtpt_info *);
79 static void do_newfs(const char *);
80 static void extract_ugid(const char *, struct mtpt_info *);
81 static int run(int *, const char *, ...) __printflike(2, 3);
82 static void usage(void);
85 main(int argc, char **argv)
87 struct mtpt_info mi; /* Mountpoint info. */
88 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
90 enum md_types mdtype; /* The type of our memory disk. */
92 bool detach, softdep, autounit, newfs;
93 char *mtpoint, *unitstr;
99 /* Misc. initialization. */
100 (void)memset(&mi, '\0', sizeof(mi));
108 mdnamelen = strlen(mdname);
110 * Can't set these to NULL. They may be passed to the
111 * respective programs without modification. I.e., we may not
112 * receive any command-line options which will caused them to
115 mdconfig_arg = strdup("");
116 newfs_arg = strdup("");
117 mount_arg = strdup("");
119 /* If we were started as mount_mfs or mfs, imply -C. */
120 if (strcmp(getprogname(), "mount_mfs") == 0 ||
121 strcmp(getprogname(), "mfs") == 0)
124 while ((ch = getopt(argc, argv,
125 "a:b:Cc:Dd:e:F:f:hi:LlMm:Nn:O:o:p:PSs:t:Uv:w:X")) != -1)
128 argappend(&newfs_arg, "-a %s", optarg);
131 argappend(&newfs_arg, "-b %s", optarg);
139 argappend(&newfs_arg, "-c %s", optarg);
147 argappend(&newfs_arg, "-d %s", optarg);
150 argappend(&newfs_arg, "-e %s", optarg);
157 argappend(&mdconfig_arg, "-f %s", optarg);
160 argappend(&newfs_arg, "-f %s", optarg);
166 argappend(&newfs_arg, "-i %s", optarg);
174 argappend(&newfs_arg, "-l");
183 argappend(&newfs_arg, "-m %s", optarg);
191 argappend(&newfs_arg, "-n %s", optarg);
194 argappend(&newfs_arg, "-o %s", optarg);
197 argappend(&mount_arg, "-o %s", optarg);
207 if ((set = setmode(optarg)) == NULL)
209 mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
210 mi.mi_have_mode = true;
219 argappend(&mdconfig_arg, "-s %s", optarg);
225 argappend(&newfs_arg, "-O %s", optarg);
230 extract_ugid(optarg, &mi);
245 /* Make compatibility assumptions. */
248 mi.mi_have_mode = true;
251 /* Derive 'unit' (global). */
253 if (strncmp(unitstr, "/dev/", 5) == 0)
255 if (strncmp(unitstr, mdname, mdnamelen) == 0)
256 unitstr += mdnamelen;
257 if (*unitstr == '\0') {
261 ul = strtoul(unitstr, &p, 10);
262 if (ul == ULONG_MAX || *p != '\0')
263 errx(1, "bad device unit: %s", unitstr);
271 argappend(&newfs_arg, "-U");
272 if (mdtype != MD_VNODE && !newfs)
273 errx(1, "-P requires a vnode-backed disk");
276 if (detach && !autounit)
277 do_mdconfig_detach();
279 do_mdconfig_attach_au(mdconfig_arg, mdtype);
281 do_mdconfig_attach(mdconfig_arg, mdtype);
284 do_mount(mount_arg, mtpoint);
285 do_mtptsetup(mtpoint, &mi);
291 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
292 * reallocate as required.
295 argappend(char **dstp, const char *fmt, ...)
304 if (vasprintf(&new, fmt,ap) == -1)
305 errx(1, "vasprintf");
309 if (asprintf(&new, "%s %s", old, new) == -1)
318 * If run-time debugging is enabled, print the expansion of 'fmt'.
319 * Otherwise, do nothing.
322 debugprintf(const char *fmt, ...)
328 fprintf(stderr, "DEBUG: ");
330 vfprintf(stderr, fmt, ap);
332 fprintf(stderr, "\n");
337 * Attach a memory disk with a known unit.
340 do_mdconfig_attach(const char *args, const enum md_types mdtype)
343 const char *ta; /* Type arg. */
358 rv = run(NULL, "%s -a %s%s -u %s%d", _PATH_MDCONFIG, ta, args,
361 errx(1, "mdconfig (attach) exited with error code %d", rv);
365 * Attach a memory disk with an unknown unit; use autounit.
368 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
370 const char *ta; /* Type arg. */
371 char *linep, *linebuf; /* Line pointer, line buffer. */
372 int fd; /* Standard output of mdconfig invocation. */
392 rv = run(&fd, "%s -a %s%s", _PATH_MDCONFIG, ta, args);
394 errx(1, "mdconfig (attach) exited with error code %d", rv);
396 /* Receive the unit number. */
397 if (norun) { /* Since we didn't run, we can't read. Fake it. */
401 sfd = fdopen(fd, "r");
404 linep = fgetln(sfd, &linelen);
405 if (linep == NULL && linelen < mdnamelen + 1)
406 errx(1, "unexpected output from mdconfig (attach)");
407 /* If the output format changes, we want to know about it. */
408 assert(strncmp(linep, mdname, mdnamelen) == 0);
409 linebuf = malloc(linelen - mdnamelen + 1);
410 assert(linebuf != NULL);
411 /* Can't use strlcpy because linep is not NULL-terminated. */
412 strncpy(linebuf, linep + mdnamelen, linelen);
413 linebuf[linelen] = '\0';
414 ul = strtoul(linebuf, &p, 10);
415 if (ul == ULONG_MAX || *p != '\n')
416 errx(1, "unexpected output from mdconfig (attach)");
424 * Detach a memory disk.
427 do_mdconfig_detach(void)
431 rv = run(NULL, "%s -d -u %s%d", _PATH_MDCONFIG, mdname, unit);
432 if (rv && debug) /* This is allowed to fail. */
433 warnx("mdconfig (detach) exited with error code %d (ignored)",
438 * Mount the configured memory disk.
441 do_mount(const char *args, const char *mtpoint)
445 rv = run(NULL, "%s%s /dev/%s%d %s", _PATH_MOUNT, args,
446 mdname, unit, mtpoint);
448 errx(1, "mount exited with error code %d", rv);
452 * Various configuration of the mountpoint. Mostly, enact 'mip'.
455 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
458 if (mip->mi_have_mode) {
459 debugprintf("changing mode of %s to %o.", mtpoint,
462 if (chmod(mtpoint, mip->mi_mode) == -1)
463 err(1, "chmod: %s", mtpoint);
466 * We have to do these separately because the user may have
467 * only specified one of them.
469 if (mip->mi_have_uid) {
470 debugprintf("changing owner (user) or %s to %u.", mtpoint,
473 if (chown(mtpoint, mip->mi_uid, -1) == -1)
474 err(1, "chown %s to %u (user)", mtpoint,
477 if (mip->mi_have_gid) {
478 debugprintf("changing owner (group) or %s to %u.", mtpoint,
481 if (chown(mtpoint, -1, mip->mi_gid) == -1)
482 err(1, "chown %s to %u (group)", mtpoint,
488 * Put a file system on the memory disk.
491 do_newfs(const char *args)
495 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
497 errx(1, "newfs exited with error code %d", rv);
501 * 'str' should be a user and group name similar to the last argument
502 * to chown(1); i.e., a user, followed by a colon, followed by a
503 * group. The user and group in 'str' may be either a [ug]id or a
504 * name. Upon return, the uid and gid fields in 'mip' will contain
505 * the uid and gid of the user and group name in 'str', respectively.
507 * In other words, this derives a user and group id from a string
508 * formatted like the last argument to chown(1).
510 * Notice: At this point we don't support only a username or only a
511 * group name. do_mtptsetup already does, so when this feature is
512 * desired, this is the only routine that needs to be changed.
515 extract_ugid(const char *str, struct mtpt_info *mip)
517 char *ug; /* Writable 'str'. */
518 char *user, *group; /* Result of extracton. */
527 mip->mi_have_uid = mip->mi_have_gid = false;
529 /* Extract the user and group from 'str'. Format above. */
533 user = strsep(&group, ":");
534 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
538 *uid = strtoul(user, &p, 10);
539 if (*uid == (uid_t)ULONG_MAX)
544 errx(1, "invalid user: %s", user);
547 mip->mi_have_uid = true;
550 *gid = strtoul(group, &p, 10);
551 if (*gid == (gid_t)ULONG_MAX)
554 gr = getgrnam(group);
556 errx(1, "invalid group: %s", group);
559 mip->mi_have_gid = true;
565 * Run a process with command name and arguments pointed to by the
566 * formatted string 'cmdline'. Since system(3) is not used, the first
567 * space-delimited token of 'cmdline' must be the full pathname of the
568 * program to run. The return value is the return code of the process
569 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
570 * the program spawned (i.e., you can read from ofd and get the output
574 run(int *ofd, const char *cmdline, ...)
576 char **argv, **argvp; /* Result of splitting 'cmd'. */
578 char *cmd; /* Expansion of 'cmdline'. */
579 int pid, status; /* Child info. */
580 int pfd[2]; /* Pipe to the child. */
581 int nfd; /* Null (/dev/null) file descriptor. */
582 bool dup2dn; /* Dup /dev/null to stdout? */
588 va_start(ap, cmdline);
589 rv = vasprintf(&cmd, cmdline, ap);
594 /* Split up 'cmd' into 'argv' for use with execve. */
595 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
596 argc++; /* 'argc' generation loop. */
597 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
598 assert(argv != NULL);
599 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
601 if (++argvp >= &argv[argc]) {
607 /* Make sure the above loop works as expected. */
610 * We can't, but should, use debugprintf here. First,
611 * it appends a trailing newline to the output, and
612 * second it prepends "DEBUG: " to the output. The
613 * former is a problem for this would-be first call,
614 * and the latter for the would-be call inside the
617 (void)fprintf(stderr, "DEBUG: running:");
618 /* Should be equivilent to 'cmd' (before strsep, of course). */
619 for (i = 0; argv[i] != NULL; i++)
620 (void)fprintf(stderr, " %s", argv[i]);
621 (void)fprintf(stderr, "\n");
624 /* Create a pipe if necessary and fork the helper program. */
626 if (pipe(&pfd[0]) == -1)
634 /* XXX can we call err() in here? */
638 if (dup2(pfd[1], STDOUT_FILENO) < 0)
641 nfd = open(_PATH_DEVNULL, O_RDWR);
643 err(1, "open: %s", _PATH_DEVNULL);
644 if (dup2(nfd, STDIN_FILENO) < 0)
647 if (dup2(nfd, STDOUT_FILENO) < 0)
649 if (dup2(nfd, STDERR_FILENO) < 0)
653 (void)execv(argv[0], argv);
654 warn("exec: %s", argv[0]);
662 while (waitpid(pid, &status, 0) != pid)
664 return (WEXITSTATUS(status));
673 name = getprogname();
678 "usage: %s [-DLlMNPSUX] [-a maxcontig] [-b block-size] [-c cylinders]\n"
679 "\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
680 "\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
681 "\t[-o mount-options] [-p permissions] [-s size] [-v version]\n"
682 "\t[-w user:group] md-device mount-point\n", name);
684 "usage: %s -C [-lNU] [-a maxcontig] [-b block-size] [-c cylinders]\n"
685 "\t[-d rotdelay] [-e maxbpg] [-F file] [-f frag-size] [-i bytes]\n"
686 "\t[-m percent-free] [-n rotational-positions] [-O optimization]\n"
687 "\t[-o mount-options] [-s size] [-v version] md-device mount-point\n", name);