2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Dima Dorfman.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * mdmfs (md/MFS) is a wrapper around mdconfig(8),
31 * newfs(8), and mount(8) that mimics the command line option set of
32 * the deprecated mount_mfs(8).
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/mdioctl.h>
41 #include <sys/module.h>
42 #include <sys/mount.h>
61 typedef enum { false, true } bool;
73 static bool debug; /* Emit debugging information? */
74 static bool loudsubs; /* Suppress output from helper programs? */
75 static bool norun; /* Actually run the helper programs? */
76 static int unit; /* The unit we're working with. */
77 static const char *mdname; /* Name of memory disk device (e.g., "md"). */
78 static const char *mdsuffix; /* Suffix of memory disk device (e.g., ".uzip"). */
79 static size_t mdnamelen; /* Length of mdname. */
80 static const char *path_mdconfig =_PATH_MDCONFIG;
82 static void argappend(char **, const char *, ...) __printflike(2, 3);
83 static void debugprintf(const char *, ...) __printflike(1, 2);
84 static void do_mdconfig_attach(const char *, const enum md_types);
85 static void do_mdconfig_attach_au(const char *, const enum md_types);
86 static void do_mdconfig_detach(void);
87 static void do_mount_md(const char *, const char *);
88 static void do_mount_tmpfs(const char *, const char *);
89 static void do_mtptsetup(const char *, struct mtpt_info *);
90 static void do_newfs(const char *);
91 static void do_copy(const char *, const char *);
92 static void extract_ugid(const char *, struct mtpt_info *);
93 static int run(int *, const char *, ...) __printflike(2, 3);
94 static void usage(void);
97 main(int argc, char **argv)
99 struct mtpt_info mi; /* Mountpoint info. */
101 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
103 enum md_types mdtype; /* The type of our memory disk. */
104 bool have_mdtype, mlmac;
105 bool detach, softdep, autounit, newfs;
106 const char *mtpoint, *size_arg, *skel, *unitstr;
112 /* Misc. initialization. */
113 (void)memset(&mi, '\0', sizeof(mi));
123 mdnamelen = strlen(mdname);
126 * Can't set these to NULL. They may be passed to the
127 * respective programs without modification. I.e., we may not
128 * receive any command-line options which will caused them to
131 mdconfig_arg = strdup("");
132 newfs_arg = strdup("");
133 mount_arg = strdup("");
136 /* If we were started as mount_mfs or mfs, imply -C. */
137 if (strcmp(getprogname(), "mount_mfs") == 0 ||
138 strcmp(getprogname(), "mfs") == 0) {
139 /* Make compatibility assumptions. */
141 mi.mi_have_mode = true;
144 while ((ch = getopt(argc, argv,
145 "a:b:Cc:Dd:E:e:F:f:hi:k:LlMm:NnO:o:Pp:Ss:tT:Uv:w:X")) != -1)
148 argappend(&newfs_arg, "-a %s", optarg);
151 argappend(&newfs_arg, "-b %s", optarg);
154 /* Ignored for compatibility. */
157 argappend(&newfs_arg, "-c %s", optarg);
163 argappend(&newfs_arg, "-d %s", optarg);
166 path_mdconfig = optarg;
169 argappend(&newfs_arg, "-e %s", optarg);
176 argappend(&mdconfig_arg, "-f %s", optarg);
179 argappend(&newfs_arg, "-f %s", optarg);
185 argappend(&newfs_arg, "-i %s", optarg);
195 argappend(&newfs_arg, "-l");
202 argappend(&mdconfig_arg, "-o reserve");
205 argappend(&newfs_arg, "-m %s", optarg);
211 argappend(&newfs_arg, "-n");
214 argappend(&newfs_arg, "-o %s", optarg);
217 argappend(&mount_arg, "-o %s", optarg);
223 if ((set = setmode(optarg)) == NULL)
225 mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
226 mi.mi_have_mode = true;
227 mi.mi_forced_pw = true;
237 argappend(&newfs_arg, "-t");
240 argappend(&mount_arg, "-t %s", optarg);
246 argappend(&newfs_arg, "-O %s", optarg);
249 extract_ugid(optarg, &mi);
250 mi.mi_forced_pw = true;
264 * Historically our size arg was passed directly to mdconfig, which
265 * treats a number without a suffix as a count of 512-byte sectors;
266 * tmpfs would treat it as a count of bytes. To get predictable
267 * behavior for 'auto' we document that the size always uses mdconfig
268 * rules. To make that work, decode the size here so it can be passed
269 * to either tmpfs or mdconfig as a count of bytes.
271 if (size_arg != NULL) {
272 mdsize = (intmax_t)strtoumax(size_arg, &p, 0);
273 if (p == size_arg || (p[0] != 0 && p[1] != 0) || mdsize < 0)
274 errx(1, "invalid size '%s'", size_arg);
298 errx(1, "invalid size suffix on '%s'", size_arg);
303 * Based on the command line 'md-device' either mount a tmpfs filesystem
304 * or configure the md device then format and mount a filesystem on it.
305 * If the device is 'auto' use tmpfs if it is available and there is no
306 * request for multilabel MAC (which tmpfs does not support).
311 if (strcmp(unitstr, "auto") == 0) {
313 idx = -1; /* Must use md for mlmac. */
314 else if ((idx = modfind("tmpfs")) == -1)
315 idx = kldload("tmpfs");
322 if (strcmp(unitstr, "tmpfs") == 0) {
323 if (size_arg != NULL && mdsize != 0)
324 argappend(&mount_arg, "-o size=%jd", mdsize);
325 do_mount_tmpfs(mount_arg, mtpoint);
327 if (size_arg != NULL)
328 argappend(&mdconfig_arg, "-s %jdB", mdsize);
329 if (strncmp(unitstr, "/dev/", 5) == 0)
331 if (strncmp(unitstr, mdname, mdnamelen) == 0)
332 unitstr += mdnamelen;
333 if (!isdigit(*unitstr)) {
338 ul = strtoul(unitstr, &p, 10);
340 errx(1, "bad device unit: %s", unitstr);
342 mdsuffix = p; /* can be empty */
348 argappend(&newfs_arg, "-U");
349 if (mdtype != MD_VNODE && !newfs)
350 errx(1, "-P requires a vnode-backed disk");
353 if (detach && !autounit)
354 do_mdconfig_detach();
356 do_mdconfig_attach_au(mdconfig_arg, mdtype);
358 do_mdconfig_attach(mdconfig_arg, mdtype);
361 do_mount_md(mount_arg, mtpoint);
364 do_mtptsetup(mtpoint, &mi);
366 do_copy(mtpoint, skel);
372 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
373 * reallocate as required.
376 argappend(char **dstp, const char *fmt, ...)
385 if (vasprintf(&new, fmt,ap) == -1)
386 errx(1, "vasprintf");
390 if (asprintf(&new, "%s %s", old, new) == -1)
399 * If run-time debugging is enabled, print the expansion of 'fmt'.
400 * Otherwise, do nothing.
403 debugprintf(const char *fmt, ...)
409 fprintf(stderr, "DEBUG: ");
411 vfprintf(stderr, fmt, ap);
413 fprintf(stderr, "\n");
418 * Attach a memory disk with a known unit.
421 do_mdconfig_attach(const char *args, const enum md_types mdtype)
424 const char *ta; /* Type arg. */
439 rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
442 errx(1, "mdconfig (attach) exited with error code %d", rv);
446 * Attach a memory disk with an unknown unit; use autounit.
449 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
451 const char *ta; /* Type arg. */
453 char linebuf[12]; /* 32-bit unit (10) + '\n' (1) + '\0' (1) */
454 int fd; /* Standard output of mdconfig invocation. */
474 rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
476 errx(1, "mdconfig (attach) exited with error code %d", rv);
478 /* Receive the unit number. */
479 if (norun) { /* Since we didn't run, we can't read. Fake it. */
483 sfd = fdopen(fd, "r");
486 linep = fgetln(sfd, &linelen);
487 /* If the output format changes, we want to know about it. */
488 if (linep == NULL || linelen <= mdnamelen + 1 ||
489 linelen - mdnamelen >= sizeof(linebuf) ||
490 strncmp(linep, mdname, mdnamelen) != 0)
491 errx(1, "unexpected output from mdconfig (attach)");
493 linelen -= mdnamelen;
494 /* Can't use strlcpy because linep is not NULL-terminated. */
495 strncpy(linebuf, linep, linelen);
496 linebuf[linelen] = '\0';
497 ul = strtoul(linebuf, &p, 10);
498 if (ul == ULONG_MAX || *p != '\n')
499 errx(1, "unexpected output from mdconfig (attach)");
506 * Detach a memory disk.
509 do_mdconfig_detach(void)
513 rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
514 if (rv && debug) /* This is allowed to fail. */
515 warnx("mdconfig (detach) exited with error code %d (ignored)",
520 * Mount the configured memory disk.
523 do_mount_md(const char *args, const char *mtpoint)
527 rv = run(NULL, "%s%s /dev/%s%d%s %s", _PATH_MOUNT, args,
528 mdname, unit, mdsuffix, mtpoint);
530 errx(1, "mount exited with error code %d", rv);
534 * Mount the configured tmpfs.
537 do_mount_tmpfs(const char *args, const char *mtpoint)
541 rv = run(NULL, "%s -t tmpfs %s tmp %s", _PATH_MOUNT, args, mtpoint);
543 errx(1, "tmpfs mount exited with error code %d", rv);
547 * Various configuration of the mountpoint. Mostly, enact 'mip'.
550 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
554 if (!mip->mi_have_mode && !mip->mi_have_uid && !mip->mi_have_gid)
558 if (statfs(mtpoint, &sfs) == -1) {
559 warn("statfs: %s", mtpoint);
562 if ((sfs.f_flags & MNT_RDONLY) != 0) {
563 if (mip->mi_forced_pw) {
565 "Not changing mode/owner of %s since it is read-only",
569 "Not changing mode/owner of %s since it is read-only",
576 if (mip->mi_have_mode) {
577 debugprintf("changing mode of %s to %o.", mtpoint,
580 if (chmod(mtpoint, mip->mi_mode) == -1)
581 err(1, "chmod: %s", mtpoint);
584 * We have to do these separately because the user may have
585 * only specified one of them.
587 if (mip->mi_have_uid) {
588 debugprintf("changing owner (user) or %s to %u.", mtpoint,
591 if (chown(mtpoint, mip->mi_uid, -1) == -1)
592 err(1, "chown %s to %u (user)", mtpoint,
595 if (mip->mi_have_gid) {
596 debugprintf("changing owner (group) or %s to %u.", mtpoint,
599 if (chown(mtpoint, -1, mip->mi_gid) == -1)
600 err(1, "chown %s to %u (group)", mtpoint,
606 * Put a file system on the memory disk.
609 do_newfs(const char *args)
613 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
615 errx(1, "newfs exited with error code %d", rv);
620 * Copy skel into the mountpoint.
623 do_copy(const char *mtpoint, const char *skel)
629 err(1, "chdir to %s", skel);
630 rv = run(NULL, "/bin/pax -rw -pe . %s", mtpoint);
632 errx(1, "skel copy failed");
636 * 'str' should be a user and group name similar to the last argument
637 * to chown(1); i.e., a user, followed by a colon, followed by a
638 * group. The user and group in 'str' may be either a [ug]id or a
639 * name. Upon return, the uid and gid fields in 'mip' will contain
640 * the uid and gid of the user and group name in 'str', respectively.
642 * In other words, this derives a user and group id from a string
643 * formatted like the last argument to chown(1).
645 * Notice: At this point we don't support only a username or only a
646 * group name. do_mtptsetup already does, so when this feature is
647 * desired, this is the only routine that needs to be changed.
650 extract_ugid(const char *str, struct mtpt_info *mip)
652 char *ug; /* Writable 'str'. */
653 char *user, *group; /* Result of extracton. */
662 mip->mi_have_uid = mip->mi_have_gid = false;
664 /* Extract the user and group from 'str'. Format above. */
668 user = strsep(&group, ":");
669 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
673 *uid = strtoul(user, &p, 10);
674 if (*uid == (uid_t)ULONG_MAX)
679 errx(1, "invalid user: %s", user);
682 mip->mi_have_uid = true;
685 *gid = strtoul(group, &p, 10);
686 if (*gid == (gid_t)ULONG_MAX)
689 gr = getgrnam(group);
691 errx(1, "invalid group: %s", group);
694 mip->mi_have_gid = true;
700 * Run a process with command name and arguments pointed to by the
701 * formatted string 'cmdline'. Since system(3) is not used, the first
702 * space-delimited token of 'cmdline' must be the full pathname of the
703 * program to run. The return value is the return code of the process
704 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
705 * the program spawned (i.e., you can read from ofd and get the output
709 run(int *ofd, const char *cmdline, ...)
711 char **argv, **argvp; /* Result of splitting 'cmd'. */
713 char *cmd; /* Expansion of 'cmdline'. */
714 int pid, status; /* Child info. */
715 int pfd[2]; /* Pipe to the child. */
716 int nfd; /* Null (/dev/null) file descriptor. */
717 bool dup2dn; /* Dup /dev/null to stdout? */
723 va_start(ap, cmdline);
724 rv = vasprintf(&cmd, cmdline, ap);
729 /* Split up 'cmd' into 'argv' for use with execve. */
730 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
731 argc++; /* 'argc' generation loop. */
732 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
733 assert(argv != NULL);
734 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
736 if (++argvp >= &argv[argc]) {
741 /* The argv array ends up NULL-terminated here. */
743 /* Make sure the above loop works as expected. */
746 * We can't, but should, use debugprintf here. First,
747 * it appends a trailing newline to the output, and
748 * second it prepends "DEBUG: " to the output. The
749 * former is a problem for this would-be first call,
750 * and the latter for the would-be call inside the
753 (void)fprintf(stderr, "DEBUG: running:");
754 /* Should be equivalent to 'cmd' (before strsep, of course). */
755 for (i = 0; argv[i] != NULL; i++)
756 (void)fprintf(stderr, " %s", argv[i]);
757 (void)fprintf(stderr, "\n");
760 /* Create a pipe if necessary and fork the helper program. */
762 if (pipe(&pfd[0]) == -1)
770 /* XXX can we call err() in here? */
774 if (dup2(pfd[1], STDOUT_FILENO) < 0)
777 nfd = open(_PATH_DEVNULL, O_RDWR);
779 err(1, "open: %s", _PATH_DEVNULL);
780 if (dup2(nfd, STDIN_FILENO) < 0)
783 if (dup2(nfd, STDOUT_FILENO) < 0)
785 if (dup2(nfd, STDERR_FILENO) < 0)
789 (void)execv(argv[0], argv);
790 warn("exec: %s", argv[0]);
798 while (waitpid(pid, &status, 0) != pid)
800 return (WEXITSTATUS(status));
808 "usage: %s [-DLlMNnPStUX] [-a maxcontig] [-b block-size]\n"
809 "\t[-c blocks-per-cylinder-group][-d max-extent-size] [-E path-mdconfig]\n"
810 "\t[-e maxbpg] [-F file] [-f frag-size] [-i bytes] [-k skel]\n"
811 "\t[-m percent-free] [-O optimization] [-o mount-options]\n"
812 "\t[-p permissions] [-s size] [-v version] [-w user:group]\n"
813 "\tmd-device mount-point\n", getprogname());