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 extract_ugid(const char *, struct mtpt_info *);
92 static int run(int *, const char *, ...) __printflike(2, 3);
93 static void usage(void);
96 main(int argc, char **argv)
98 struct mtpt_info mi; /* Mountpoint info. */
100 char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
102 enum md_types mdtype; /* The type of our memory disk. */
103 bool have_mdtype, mlmac;
104 bool detach, softdep, autounit, newfs;
105 const char *mtpoint, *size_arg, *unitstr;
111 /* Misc. initialization. */
112 (void)memset(&mi, '\0', sizeof(mi));
121 mdnamelen = strlen(mdname);
124 * Can't set these to NULL. They may be passed to the
125 * respective programs without modification. I.e., we may not
126 * receive any command-line options which will caused them to
129 mdconfig_arg = strdup("");
130 newfs_arg = strdup("");
131 mount_arg = strdup("");
134 /* If we were started as mount_mfs or mfs, imply -C. */
135 if (strcmp(getprogname(), "mount_mfs") == 0 ||
136 strcmp(getprogname(), "mfs") == 0) {
137 /* Make compatibility assumptions. */
139 mi.mi_have_mode = true;
142 while ((ch = getopt(argc, argv,
143 "a:b:Cc:Dd:E:e:F:f:hi:LlMm:NnO:o:Pp:Ss:tT:Uv:w:X")) != -1)
146 argappend(&newfs_arg, "-a %s", optarg);
149 argappend(&newfs_arg, "-b %s", optarg);
152 /* Ignored for compatibility. */
155 argappend(&newfs_arg, "-c %s", optarg);
161 argappend(&newfs_arg, "-d %s", optarg);
164 path_mdconfig = optarg;
167 argappend(&newfs_arg, "-e %s", optarg);
174 argappend(&mdconfig_arg, "-f %s", optarg);
177 argappend(&newfs_arg, "-f %s", optarg);
183 argappend(&newfs_arg, "-i %s", optarg);
190 argappend(&newfs_arg, "-l");
199 argappend(&newfs_arg, "-m %s", optarg);
205 argappend(&newfs_arg, "-n");
208 argappend(&newfs_arg, "-o %s", optarg);
211 argappend(&mount_arg, "-o %s", optarg);
217 if ((set = setmode(optarg)) == NULL)
219 mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
220 mi.mi_have_mode = true;
221 mi.mi_forced_pw = true;
231 argappend(&newfs_arg, "-t");
234 argappend(&mount_arg, "-t %s", optarg);
240 argappend(&newfs_arg, "-O %s", optarg);
243 extract_ugid(optarg, &mi);
244 mi.mi_forced_pw = true;
258 * Historically our size arg was passed directly to mdconfig, which
259 * treats a number without a suffix as a count of 512-byte sectors;
260 * tmpfs would treat it as a count of bytes. To get predictable
261 * behavior for 'auto' we document that the size always uses mdconfig
262 * rules. To make that work, decode the size here so it can be passed
263 * to either tmpfs or mdconfig as a count of bytes.
265 if (size_arg != NULL) {
266 mdsize = (intmax_t)strtoumax(size_arg, &p, 0);
267 if (p == size_arg || (p[0] != 0 && p[1] != 0) || mdsize < 0)
268 errx(1, "invalid size '%s'", size_arg);
292 errx(1, "invalid size suffix on '%s'", size_arg);
297 * Based on the command line 'md-device' either mount a tmpfs filesystem
298 * or configure the md device then format and mount a filesystem on it.
299 * If the device is 'auto' use tmpfs if it is available and there is no
300 * request for multilabel MAC (which tmpfs does not support).
305 if (strcmp(unitstr, "auto") == 0) {
307 idx = -1; /* Must use md for mlmac. */
308 else if ((idx = modfind("tmpfs")) == -1)
309 idx = kldload("tmpfs");
316 if (strcmp(unitstr, "tmpfs") == 0) {
317 if (size_arg != NULL && mdsize != 0)
318 argappend(&mount_arg, "-o size=%jd", mdsize);
319 do_mount_tmpfs(mount_arg, mtpoint);
321 if (size_arg != NULL)
322 argappend(&mdconfig_arg, "-s %jdB", mdsize);
323 if (strncmp(unitstr, "/dev/", 5) == 0)
325 if (strncmp(unitstr, mdname, mdnamelen) == 0)
326 unitstr += mdnamelen;
327 if (!isdigit(*unitstr)) {
332 ul = strtoul(unitstr, &p, 10);
334 errx(1, "bad device unit: %s", unitstr);
336 mdsuffix = p; /* can be empty */
342 argappend(&newfs_arg, "-U");
343 if (mdtype != MD_VNODE && !newfs)
344 errx(1, "-P requires a vnode-backed disk");
347 if (detach && !autounit)
348 do_mdconfig_detach();
350 do_mdconfig_attach_au(mdconfig_arg, mdtype);
352 do_mdconfig_attach(mdconfig_arg, mdtype);
355 do_mount_md(mount_arg, mtpoint);
358 do_mtptsetup(mtpoint, &mi);
364 * Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
365 * reallocate as required.
368 argappend(char **dstp, const char *fmt, ...)
377 if (vasprintf(&new, fmt,ap) == -1)
378 errx(1, "vasprintf");
382 if (asprintf(&new, "%s %s", old, new) == -1)
391 * If run-time debugging is enabled, print the expansion of 'fmt'.
392 * Otherwise, do nothing.
395 debugprintf(const char *fmt, ...)
401 fprintf(stderr, "DEBUG: ");
403 vfprintf(stderr, fmt, ap);
405 fprintf(stderr, "\n");
410 * Attach a memory disk with a known unit.
413 do_mdconfig_attach(const char *args, const enum md_types mdtype)
416 const char *ta; /* Type arg. */
431 rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
434 errx(1, "mdconfig (attach) exited with error code %d", rv);
438 * Attach a memory disk with an unknown unit; use autounit.
441 do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
443 const char *ta; /* Type arg. */
445 char linebuf[12]; /* 32-bit unit (10) + '\n' (1) + '\0' (1) */
446 int fd; /* Standard output of mdconfig invocation. */
466 rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
468 errx(1, "mdconfig (attach) exited with error code %d", rv);
470 /* Receive the unit number. */
471 if (norun) { /* Since we didn't run, we can't read. Fake it. */
475 sfd = fdopen(fd, "r");
478 linep = fgetln(sfd, &linelen);
479 /* If the output format changes, we want to know about it. */
480 if (linep == NULL || linelen <= mdnamelen + 1 ||
481 linelen - mdnamelen >= sizeof(linebuf) ||
482 strncmp(linep, mdname, mdnamelen) != 0)
483 errx(1, "unexpected output from mdconfig (attach)");
485 linelen -= mdnamelen;
486 /* Can't use strlcpy because linep is not NULL-terminated. */
487 strncpy(linebuf, linep, linelen);
488 linebuf[linelen] = '\0';
489 ul = strtoul(linebuf, &p, 10);
490 if (ul == ULONG_MAX || *p != '\n')
491 errx(1, "unexpected output from mdconfig (attach)");
498 * Detach a memory disk.
501 do_mdconfig_detach(void)
505 rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
506 if (rv && debug) /* This is allowed to fail. */
507 warnx("mdconfig (detach) exited with error code %d (ignored)",
512 * Mount the configured memory disk.
515 do_mount_md(const char *args, const char *mtpoint)
519 rv = run(NULL, "%s%s /dev/%s%d%s %s", _PATH_MOUNT, args,
520 mdname, unit, mdsuffix, mtpoint);
522 errx(1, "mount exited with error code %d", rv);
526 * Mount the configured tmpfs.
529 do_mount_tmpfs(const char *args, const char *mtpoint)
533 rv = run(NULL, "%s -t tmpfs %s tmp %s", _PATH_MOUNT, args, mtpoint);
535 errx(1, "tmpfs mount exited with error code %d", rv);
539 * Various configuration of the mountpoint. Mostly, enact 'mip'.
542 do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
546 if (!mip->mi_have_mode && !mip->mi_have_uid && !mip->mi_have_gid)
550 if (statfs(mtpoint, &sfs) == -1) {
551 warn("statfs: %s", mtpoint);
554 if ((sfs.f_flags & MNT_RDONLY) != 0) {
555 if (mip->mi_forced_pw) {
557 "Not changing mode/owner of %s since it is read-only",
561 "Not changing mode/owner of %s since it is read-only",
568 if (mip->mi_have_mode) {
569 debugprintf("changing mode of %s to %o.", mtpoint,
572 if (chmod(mtpoint, mip->mi_mode) == -1)
573 err(1, "chmod: %s", mtpoint);
576 * We have to do these separately because the user may have
577 * only specified one of them.
579 if (mip->mi_have_uid) {
580 debugprintf("changing owner (user) or %s to %u.", mtpoint,
583 if (chown(mtpoint, mip->mi_uid, -1) == -1)
584 err(1, "chown %s to %u (user)", mtpoint,
587 if (mip->mi_have_gid) {
588 debugprintf("changing owner (group) or %s to %u.", mtpoint,
591 if (chown(mtpoint, -1, mip->mi_gid) == -1)
592 err(1, "chown %s to %u (group)", mtpoint,
598 * Put a file system on the memory disk.
601 do_newfs(const char *args)
605 rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
607 errx(1, "newfs exited with error code %d", rv);
611 * 'str' should be a user and group name similar to the last argument
612 * to chown(1); i.e., a user, followed by a colon, followed by a
613 * group. The user and group in 'str' may be either a [ug]id or a
614 * name. Upon return, the uid and gid fields in 'mip' will contain
615 * the uid and gid of the user and group name in 'str', respectively.
617 * In other words, this derives a user and group id from a string
618 * formatted like the last argument to chown(1).
620 * Notice: At this point we don't support only a username or only a
621 * group name. do_mtptsetup already does, so when this feature is
622 * desired, this is the only routine that needs to be changed.
625 extract_ugid(const char *str, struct mtpt_info *mip)
627 char *ug; /* Writable 'str'. */
628 char *user, *group; /* Result of extracton. */
637 mip->mi_have_uid = mip->mi_have_gid = false;
639 /* Extract the user and group from 'str'. Format above. */
643 user = strsep(&group, ":");
644 if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
648 *uid = strtoul(user, &p, 10);
649 if (*uid == (uid_t)ULONG_MAX)
654 errx(1, "invalid user: %s", user);
657 mip->mi_have_uid = true;
660 *gid = strtoul(group, &p, 10);
661 if (*gid == (gid_t)ULONG_MAX)
664 gr = getgrnam(group);
666 errx(1, "invalid group: %s", group);
669 mip->mi_have_gid = true;
675 * Run a process with command name and arguments pointed to by the
676 * formatted string 'cmdline'. Since system(3) is not used, the first
677 * space-delimited token of 'cmdline' must be the full pathname of the
678 * program to run. The return value is the return code of the process
679 * spawned. If 'ofd' is non-NULL, it is set to the standard output of
680 * the program spawned (i.e., you can read from ofd and get the output
684 run(int *ofd, const char *cmdline, ...)
686 char **argv, **argvp; /* Result of splitting 'cmd'. */
688 char *cmd; /* Expansion of 'cmdline'. */
689 int pid, status; /* Child info. */
690 int pfd[2]; /* Pipe to the child. */
691 int nfd; /* Null (/dev/null) file descriptor. */
692 bool dup2dn; /* Dup /dev/null to stdout? */
698 va_start(ap, cmdline);
699 rv = vasprintf(&cmd, cmdline, ap);
704 /* Split up 'cmd' into 'argv' for use with execve. */
705 for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
706 argc++; /* 'argc' generation loop. */
707 argv = (char **)malloc(sizeof(*argv) * (argc + 1));
708 assert(argv != NULL);
709 for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
711 if (++argvp >= &argv[argc]) {
716 /* The argv array ends up NULL-terminated here. */
718 /* Make sure the above loop works as expected. */
721 * We can't, but should, use debugprintf here. First,
722 * it appends a trailing newline to the output, and
723 * second it prepends "DEBUG: " to the output. The
724 * former is a problem for this would-be first call,
725 * and the latter for the would-be call inside the
728 (void)fprintf(stderr, "DEBUG: running:");
729 /* Should be equivalent to 'cmd' (before strsep, of course). */
730 for (i = 0; argv[i] != NULL; i++)
731 (void)fprintf(stderr, " %s", argv[i]);
732 (void)fprintf(stderr, "\n");
735 /* Create a pipe if necessary and fork the helper program. */
737 if (pipe(&pfd[0]) == -1)
745 /* XXX can we call err() in here? */
749 if (dup2(pfd[1], STDOUT_FILENO) < 0)
752 nfd = open(_PATH_DEVNULL, O_RDWR);
754 err(1, "open: %s", _PATH_DEVNULL);
755 if (dup2(nfd, STDIN_FILENO) < 0)
758 if (dup2(nfd, STDOUT_FILENO) < 0)
760 if (dup2(nfd, STDERR_FILENO) < 0)
764 (void)execv(argv[0], argv);
765 warn("exec: %s", argv[0]);
773 while (waitpid(pid, &status, 0) != pid)
775 return (WEXITSTATUS(status));
783 "usage: %s [-DLlMNnPStUX] [-a maxcontig] [-b block-size]\n"
784 "\t[-c blocks-per-cylinder-group][-d max-extent-size] [-E path-mdconfig]\n"
785 "\t[-e maxbpg] [-F file] [-f frag-size] [-i bytes] [-m percent-free]\n"
786 "\t[-O optimization] [-o mount-options]\n"
787 "\t[-p permissions] [-s size] [-v version] [-w user:group]\n"
788 "\tmd-device mount-point\n", getprogname());