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/linker.h>
38 #include <sys/mdioctl.h>
39 #include <sys/module.h>
40 #include <sys/mount.h>
59 typedef enum { false, true } bool;
71 static bool debug; /* Emit debugging information? */
72 static bool loudsubs; /* Suppress output from helper programs? */
73 static bool norun; /* Actually run the helper programs? */
74 static int unit; /* The unit we're working with. */
75 static const char *mdname; /* Name of memory disk device (e.g., "md"). */
76 static const char *mdsuffix; /* Suffix of memory disk device (e.g., ".uzip"). */
77 static size_t mdnamelen; /* Length of mdname. */
78 static const char *path_mdconfig =_PATH_MDCONFIG;
80 static void argappend(char **, const char *, ...) __printflike(2, 3);
81 static void debugprintf(const char *, ...) __printflike(1, 2);
82 static void do_mdconfig_attach(const char *, const enum md_types);
83 static void do_mdconfig_attach_au(const char *, const enum md_types);
84 static void do_mdconfig_detach(void);
85 static void do_mount_md(const char *, const char *);
86 static void do_mount_tmpfs(const char *, const char *);
87 static void do_mtptsetup(const char *, struct mtpt_info *);
88 static void do_newfs(const char *);
89 static void extract_ugid(const char *, struct mtpt_info *);
90 static int run(int *, const char *, ...) __printflike(2, 3);
91 static void usage(void);
94 main(int argc, char **argv)
96 struct mtpt_info mi; /* Mountpoint info. */
98 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
100 enum md_types mdtype; /* The type of our memory disk. */
101 bool have_mdtype, mlmac;
102 bool detach, softdep, autounit, newfs;
103 const char *mtpoint, *size_arg, *unitstr;
109 /* Misc. initialization. */
110 (void)memset(&mi, '\0', sizeof(mi));
119 mdnamelen = strlen(mdname);
122 * Can't set these to NULL. They may be passed to the
123 * respective programs without modification. I.e., we may not
124 * receive any command-line options which will caused them to
127 mdconfig_arg = strdup("");
128 newfs_arg = strdup("");
129 mount_arg = strdup("");
132 /* If we were started as mount_mfs or mfs, imply -C. */
133 if (strcmp(getprogname(), "mount_mfs") == 0 ||
134 strcmp(getprogname(), "mfs") == 0) {
135 /* Make compatibility assumptions. */
137 mi.mi_have_mode = true;
140 while ((ch = getopt(argc, argv,
141 "a:b:Cc:Dd:E:e:F:f:hi:LlMm:NnO:o:Pp:Ss:tT:Uv:w:X")) != -1)
144 argappend(&newfs_arg, "-a %s", optarg);
147 argappend(&newfs_arg, "-b %s", optarg);
150 /* Ignored for compatibility. */
153 argappend(&newfs_arg, "-c %s", optarg);
159 argappend(&newfs_arg, "-d %s", optarg);
162 path_mdconfig = optarg;
165 argappend(&newfs_arg, "-e %s", optarg);
172 argappend(&mdconfig_arg, "-f %s", optarg);
175 argappend(&newfs_arg, "-f %s", optarg);
181 argappend(&newfs_arg, "-i %s", optarg);
188 argappend(&newfs_arg, "-l");
197 argappend(&newfs_arg, "-m %s", optarg);
203 argappend(&newfs_arg, "-n");
206 argappend(&newfs_arg, "-o %s", optarg);
209 argappend(&mount_arg, "-o %s", optarg);
215 if ((set = setmode(optarg)) == NULL)
217 mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
218 mi.mi_have_mode = true;
219 mi.mi_forced_pw = true;
229 argappend(&newfs_arg, "-t");
232 argappend(&mount_arg, "-t %s", optarg);
238 argappend(&newfs_arg, "-O %s", optarg);
241 extract_ugid(optarg, &mi);
242 mi.mi_forced_pw = true;
256 * Historically our size arg was passed directly to mdconfig, which
257 * treats a number without a suffix as a count of 512-byte sectors;
258 * tmpfs would treat it as a count of bytes. To get predictable
259 * behavior for 'auto' we document that the size always uses mdconfig
260 * rules. To make that work, decode the size here so it can be passed
261 * to either tmpfs or mdconfig as a count of bytes.
263 if (size_arg != NULL) {
264 mdsize = (intmax_t)strtoumax(size_arg, &p, 0);
265 if (p == size_arg || (p[0] != 0 && p[1] != 0) || mdsize < 0)
266 errx(1, "invalid size '%s'", size_arg);
290 errx(1, "invalid size suffix on '%s'", size_arg);
295 * Based on the command line 'md-device' either mount a tmpfs filesystem
296 * or configure the md device then format and mount a filesystem on it.
297 * If the device is 'auto' use tmpfs if it is available and there is no
298 * request for multilabel MAC (which tmpfs does not support).
303 if (strcmp(unitstr, "auto") == 0) {
305 idx = -1; /* Must use md for mlmac. */
306 else if ((idx = modfind("tmpfs")) == -1)
307 idx = kldload("tmpfs");
314 if (strcmp(unitstr, "tmpfs") == 0) {
315 if (size_arg != NULL && mdsize != 0)
316 argappend(&mount_arg, "-o size=%jd", mdsize);
317 do_mount_tmpfs(mount_arg, mtpoint);
319 if (size_arg != NULL)
320 argappend(&mdconfig_arg, "-s %jdB", mdsize);
321 if (strncmp(unitstr, "/dev/", 5) == 0)
323 if (strncmp(unitstr, mdname, mdnamelen) == 0)
324 unitstr += mdnamelen;
325 if (!isdigit(*unitstr)) {
330 ul = strtoul(unitstr, &p, 10);
332 errx(1, "bad device unit: %s", unitstr);
334 mdsuffix = p; /* can be empty */
340 argappend(&newfs_arg, "-U");
341 if (mdtype != MD_VNODE && !newfs)
342 errx(1, "-P requires a vnode-backed disk");
345 if (detach && !autounit)
346 do_mdconfig_detach();
348 do_mdconfig_attach_au(mdconfig_arg, mdtype);
350 do_mdconfig_attach(mdconfig_arg, mdtype);
353 do_mount_md(mount_arg, mtpoint);
356 do_mtptsetup(mtpoint, &mi);
362 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
363 * reallocate as required.
366 argappend(char **dstp, const char *fmt, ...)
375 if (vasprintf(&new, fmt,ap) == -1)
376 errx(1, "vasprintf");
380 if (asprintf(&new, "%s %s", old, new) == -1)
389 * If run-time debugging is enabled, print the expansion of 'fmt'.
390 * Otherwise, do nothing.
393 debugprintf(const char *fmt, ...)
399 fprintf(stderr, "DEBUG: ");
401 vfprintf(stderr, fmt, ap);
403 fprintf(stderr, "\n");
408 * Attach a memory disk with a known unit.
411 do_mdconfig_attach(const char *args, const enum md_types mdtype)
414 const char *ta; /* Type arg. */
429 rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
432 errx(1, "mdconfig (attach) exited with error code %d", rv);
436 * Attach a memory disk with an unknown unit; use autounit.
439 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
441 const char *ta; /* Type arg. */
442 char *linep, *linebuf; /* Line pointer, line buffer. */
443 int fd; /* Standard output of mdconfig invocation. */
463 rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
465 errx(1, "mdconfig (attach) exited with error code %d", rv);
467 /* Receive the unit number. */
468 if (norun) { /* Since we didn't run, we can't read. Fake it. */
472 sfd = fdopen(fd, "r");
475 linep = fgetln(sfd, &linelen);
476 if (linep == NULL && linelen < mdnamelen + 1)
477 errx(1, "unexpected output from mdconfig (attach)");
478 /* If the output format changes, we want to know about it. */
479 assert(strncmp(linep, mdname, mdnamelen) == 0);
480 linebuf = malloc(linelen - mdnamelen + 1);
481 assert(linebuf != NULL);
482 /* Can't use strlcpy because linep is not NULL-terminated. */
483 strncpy(linebuf, linep + mdnamelen, linelen);
484 linebuf[linelen] = '\0';
485 ul = strtoul(linebuf, &p, 10);
486 if (ul == ULONG_MAX || *p != '\n')
487 errx(1, "unexpected output from mdconfig (attach)");
495 * Detach a memory disk.
498 do_mdconfig_detach(void)
502 rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
503 if (rv && debug) /* This is allowed to fail. */
504 warnx("mdconfig (detach) exited with error code %d (ignored)",
509 * Mount the configured memory disk.
512 do_mount_md(const char *args, const char *mtpoint)
516 rv = run(NULL, "%s%s /dev/%s%d%s %s", _PATH_MOUNT, args,
517 mdname, unit, mdsuffix, mtpoint);
519 errx(1, "mount exited with error code %d", rv);
523 * Mount the configured tmpfs.
526 do_mount_tmpfs(const char *args, const char *mtpoint)
530 rv = run(NULL, "%s -t tmpfs %s tmp %s", _PATH_MOUNT, args, mtpoint);
532 errx(1, "tmpfs mount exited with error code %d", rv);
536 * Various configuration of the mountpoint. Mostly, enact 'mip'.
539 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
543 if (!mip->mi_have_mode && !mip->mi_have_uid && !mip->mi_have_gid)
547 if (statfs(mtpoint, &sfs) == -1) {
548 warn("statfs: %s", mtpoint);
551 if ((sfs.f_flags & MNT_RDONLY) != 0) {
552 if (mip->mi_forced_pw) {
554 "Not changing mode/owner of %s since it is read-only",
558 "Not changing mode/owner of %s since it is read-only",
565 if (mip->mi_have_mode) {
566 debugprintf("changing mode of %s to %o.", mtpoint,
569 if (chmod(mtpoint, mip->mi_mode) == -1)
570 err(1, "chmod: %s", mtpoint);
573 * We have to do these separately because the user may have
574 * only specified one of them.
576 if (mip->mi_have_uid) {
577 debugprintf("changing owner (user) or %s to %u.", mtpoint,
580 if (chown(mtpoint, mip->mi_uid, -1) == -1)
581 err(1, "chown %s to %u (user)", mtpoint,
584 if (mip->mi_have_gid) {
585 debugprintf("changing owner (group) or %s to %u.", mtpoint,
588 if (chown(mtpoint, -1, mip->mi_gid) == -1)
589 err(1, "chown %s to %u (group)", mtpoint,
595 * Put a file system on the memory disk.
598 do_newfs(const char *args)
602 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
604 errx(1, "newfs exited with error code %d", rv);
608 * 'str' should be a user and group name similar to the last argument
609 * to chown(1); i.e., a user, followed by a colon, followed by a
610 * group. The user and group in 'str' may be either a [ug]id or a
611 * name. Upon return, the uid and gid fields in 'mip' will contain
612 * the uid and gid of the user and group name in 'str', respectively.
614 * In other words, this derives a user and group id from a string
615 * formatted like the last argument to chown(1).
617 * Notice: At this point we don't support only a username or only a
618 * group name. do_mtptsetup already does, so when this feature is
619 * desired, this is the only routine that needs to be changed.
622 extract_ugid(const char *str, struct mtpt_info *mip)
624 char *ug; /* Writable 'str'. */
625 char *user, *group; /* Result of extracton. */
634 mip->mi_have_uid = mip->mi_have_gid = false;
636 /* Extract the user and group from 'str'. Format above. */
640 user = strsep(&group, ":");
641 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
645 *uid = strtoul(user, &p, 10);
646 if (*uid == (uid_t)ULONG_MAX)
651 errx(1, "invalid user: %s", user);
654 mip->mi_have_uid = true;
657 *gid = strtoul(group, &p, 10);
658 if (*gid == (gid_t)ULONG_MAX)
661 gr = getgrnam(group);
663 errx(1, "invalid group: %s", group);
666 mip->mi_have_gid = true;
672 * Run a process with command name and arguments pointed to by the
673 * formatted string 'cmdline'. Since system(3) is not used, the first
674 * space-delimited token of 'cmdline' must be the full pathname of the
675 * program to run. The return value is the return code of the process
676 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
677 * the program spawned (i.e., you can read from ofd and get the output
681 run(int *ofd, const char *cmdline, ...)
683 char **argv, **argvp; /* Result of splitting 'cmd'. */
685 char *cmd; /* Expansion of 'cmdline'. */
686 int pid, status; /* Child info. */
687 int pfd[2]; /* Pipe to the child. */
688 int nfd; /* Null (/dev/null) file descriptor. */
689 bool dup2dn; /* Dup /dev/null to stdout? */
695 va_start(ap, cmdline);
696 rv = vasprintf(&cmd, cmdline, ap);
701 /* Split up 'cmd' into 'argv' for use with execve. */
702 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
703 argc++; /* 'argc' generation loop. */
704 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
705 assert(argv != NULL);
706 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
708 if (++argvp >= &argv[argc]) {
713 /* The argv array ends up NULL-terminated here. */
715 /* Make sure the above loop works as expected. */
718 * We can't, but should, use debugprintf here. First,
719 * it appends a trailing newline to the output, and
720 * second it prepends "DEBUG: " to the output. The
721 * former is a problem for this would-be first call,
722 * and the latter for the would-be call inside the
725 (void)fprintf(stderr, "DEBUG: running:");
726 /* Should be equivalent to 'cmd' (before strsep, of course). */
727 for (i = 0; argv[i] != NULL; i++)
728 (void)fprintf(stderr, " %s", argv[i]);
729 (void)fprintf(stderr, "\n");
732 /* Create a pipe if necessary and fork the helper program. */
734 if (pipe(&pfd[0]) == -1)
742 /* XXX can we call err() in here? */
746 if (dup2(pfd[1], STDOUT_FILENO) < 0)
749 nfd = open(_PATH_DEVNULL, O_RDWR);
751 err(1, "open: %s", _PATH_DEVNULL);
752 if (dup2(nfd, STDIN_FILENO) < 0)
755 if (dup2(nfd, STDOUT_FILENO) < 0)
757 if (dup2(nfd, STDERR_FILENO) < 0)
761 (void)execv(argv[0], argv);
762 warn("exec: %s", argv[0]);
770 while (waitpid(pid, &status, 0) != pid)
772 return (WEXITSTATUS(status));
780 "usage: %s [-DLlMNnPStUX] [-a maxcontig] [-b block-size]\n"
781 "\t[-c blocks-per-cylinder-group][-d max-extent-size] [-E path-mdconfig]\n"
782 "\t[-e maxbpg] [-F file] [-f frag-size] [-i bytes] [-m percent-free]\n"
783 "\t[-O optimization] [-o mount-options]\n"
784 "\t[-p permissions] [-s size] [-v version] [-w user:group]\n"
785 "\tmd-device mount-point\n", getprogname());