2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2001-07-30 09:11:17 +00:00
|
|
|
* Copyright (c) 2001 Dima Dorfman.
|
2001-06-18 23:46:58 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2002-09-22 09:46:28 +00:00
|
|
|
* mdmfs (md/MFS) is a wrapper around mdconfig(8),
|
2001-06-18 23:46:58 +00:00
|
|
|
* newfs(8), and mount(8) that mimics the command line option set of
|
|
|
|
* the deprecated mount_mfs(8).
|
|
|
|
*/
|
|
|
|
|
2003-05-03 18:41:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
#include <sys/linker.h>
|
2001-06-18 23:46:58 +00:00
|
|
|
#include <sys/mdioctl.h>
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
#include <sys/module.h>
|
2011-09-13 20:16:11 +00:00
|
|
|
#include <sys/mount.h>
|
2001-06-18 23:46:58 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
#include <errno.h>
|
2001-06-18 23:46:58 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
#include <inttypes.h>
|
2001-06-18 23:46:58 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-02-15 13:49:44 +00:00
|
|
|
#include <ctype.h>
|
2001-06-18 23:46:58 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
typedef enum { false, true } bool;
|
|
|
|
|
|
|
|
struct mtpt_info {
|
|
|
|
uid_t mi_uid;
|
|
|
|
bool mi_have_uid;
|
|
|
|
gid_t mi_gid;
|
|
|
|
bool mi_have_gid;
|
|
|
|
mode_t mi_mode;
|
|
|
|
bool mi_have_mode;
|
2011-09-13 20:16:11 +00:00
|
|
|
bool mi_forced_pw;
|
2001-06-18 23:46:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool debug; /* Emit debugging information? */
|
|
|
|
static bool loudsubs; /* Suppress output from helper programs? */
|
|
|
|
static bool norun; /* Actually run the helper programs? */
|
|
|
|
static int unit; /* The unit we're working with. */
|
|
|
|
static const char *mdname; /* Name of memory disk device (e.g., "md"). */
|
2007-02-15 13:49:44 +00:00
|
|
|
static const char *mdsuffix; /* Suffix of memory disk device (e.g., ".uzip"). */
|
2001-06-18 23:46:58 +00:00
|
|
|
static size_t mdnamelen; /* Length of mdname. */
|
2006-02-16 21:28:54 +00:00
|
|
|
static const char *path_mdconfig =_PATH_MDCONFIG;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
2001-07-01 23:24:27 +00:00
|
|
|
static void argappend(char **, const char *, ...) __printflike(2, 3);
|
|
|
|
static void debugprintf(const char *, ...) __printflike(1, 2);
|
2001-06-18 23:46:58 +00:00
|
|
|
static void do_mdconfig_attach(const char *, const enum md_types);
|
|
|
|
static void do_mdconfig_attach_au(const char *, const enum md_types);
|
|
|
|
static void do_mdconfig_detach(void);
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
static void do_mount_md(const char *, const char *);
|
|
|
|
static void do_mount_tmpfs(const char *, const char *);
|
2001-06-18 23:46:58 +00:00
|
|
|
static void do_mtptsetup(const char *, struct mtpt_info *);
|
|
|
|
static void do_newfs(const char *);
|
2019-11-01 03:10:53 +00:00
|
|
|
static void do_copy(const char *, const char *);
|
2001-06-18 23:46:58 +00:00
|
|
|
static void extract_ugid(const char *, struct mtpt_info *);
|
2001-07-01 23:24:27 +00:00
|
|
|
static int run(int *, const char *, ...) __printflike(2, 3);
|
2018-10-20 21:33:00 +00:00
|
|
|
static const char *run_exitstr(int);
|
|
|
|
static int run_exitnumber(int);
|
2001-06-18 23:46:58 +00:00
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
int
|
2001-08-14 14:14:20 +00:00
|
|
|
main(int argc, char **argv)
|
2001-06-18 23:46:58 +00:00
|
|
|
{
|
|
|
|
struct mtpt_info mi; /* Mountpoint info. */
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
intmax_t mdsize;
|
2001-06-18 23:46:58 +00:00
|
|
|
char *mdconfig_arg, *newfs_arg, /* Args to helper programs. */
|
|
|
|
*mount_arg;
|
|
|
|
enum md_types mdtype; /* The type of our memory disk. */
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
bool have_mdtype, mlmac;
|
2006-01-02 01:50:30 +00:00
|
|
|
bool detach, softdep, autounit, newfs;
|
2019-11-01 03:10:53 +00:00
|
|
|
const char *mtpoint, *size_arg, *skel, *unitstr;
|
2004-01-22 07:23:36 +00:00
|
|
|
char *p;
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
int ch, idx;
|
2003-08-05 15:04:39 +00:00
|
|
|
void *set;
|
2005-01-31 04:45:45 +00:00
|
|
|
unsigned long ul;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
/* Misc. initialization. */
|
|
|
|
(void)memset(&mi, '\0', sizeof(mi));
|
|
|
|
detach = true;
|
|
|
|
softdep = true;
|
|
|
|
autounit = false;
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
mlmac = false;
|
2006-01-02 01:50:30 +00:00
|
|
|
newfs = true;
|
2001-06-18 23:46:58 +00:00
|
|
|
have_mdtype = false;
|
2019-11-01 03:10:53 +00:00
|
|
|
skel = NULL;
|
2005-01-25 14:28:31 +00:00
|
|
|
mdtype = MD_SWAP;
|
2001-06-18 23:46:58 +00:00
|
|
|
mdname = MD_NAME;
|
|
|
|
mdnamelen = strlen(mdname);
|
2017-09-30 17:51:10 +00:00
|
|
|
mdsize = 0;
|
2001-06-18 23:46:58 +00:00
|
|
|
/*
|
|
|
|
* Can't set these to NULL. They may be passed to the
|
|
|
|
* respective programs without modification. I.e., we may not
|
|
|
|
* receive any command-line options which will caused them to
|
|
|
|
* be modified.
|
|
|
|
*/
|
|
|
|
mdconfig_arg = strdup("");
|
|
|
|
newfs_arg = strdup("");
|
|
|
|
mount_arg = strdup("");
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
size_arg = NULL;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
2001-09-30 15:14:16 +00:00
|
|
|
/* If we were started as mount_mfs or mfs, imply -C. */
|
|
|
|
if (strcmp(getprogname(), "mount_mfs") == 0 ||
|
2006-11-03 12:02:24 +00:00
|
|
|
strcmp(getprogname(), "mfs") == 0) {
|
|
|
|
/* Make compatibility assumptions. */
|
|
|
|
mi.mi_mode = 01777;
|
|
|
|
mi.mi_have_mode = true;
|
|
|
|
}
|
2001-08-16 02:40:29 +00:00
|
|
|
|
2001-08-14 14:14:20 +00:00
|
|
|
while ((ch = getopt(argc, argv,
|
2019-11-01 03:10:53 +00:00
|
|
|
"a:b:Cc:Dd:E:e:F:f:hi:k:LlMm:NnO:o:Pp:Ss:tT:Uv:w:X")) != -1)
|
2001-06-18 23:46:58 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
argappend(&newfs_arg, "-a %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
argappend(&newfs_arg, "-b %s", optarg);
|
|
|
|
break;
|
2001-08-16 02:40:29 +00:00
|
|
|
case 'C':
|
2006-11-03 12:02:24 +00:00
|
|
|
/* Ignored for compatibility. */
|
2001-08-16 02:40:29 +00:00
|
|
|
break;
|
2001-06-18 23:46:58 +00:00
|
|
|
case 'c':
|
|
|
|
argappend(&newfs_arg, "-c %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
detach = false;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
argappend(&newfs_arg, "-d %s", optarg);
|
|
|
|
break;
|
2006-02-16 21:28:54 +00:00
|
|
|
case 'E':
|
|
|
|
path_mdconfig = optarg;
|
|
|
|
break;
|
2001-06-18 23:46:58 +00:00
|
|
|
case 'e':
|
|
|
|
argappend(&newfs_arg, "-e %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
if (have_mdtype)
|
|
|
|
usage();
|
|
|
|
mdtype = MD_VNODE;
|
|
|
|
have_mdtype = true;
|
|
|
|
argappend(&mdconfig_arg, "-f %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
argappend(&newfs_arg, "-f %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
argappend(&newfs_arg, "-i %s", optarg);
|
|
|
|
break;
|
2019-11-01 03:10:53 +00:00
|
|
|
case 'k':
|
|
|
|
skel = optarg;
|
|
|
|
break;
|
2001-06-18 23:46:58 +00:00
|
|
|
case 'L':
|
|
|
|
loudsubs = true;
|
|
|
|
break;
|
2004-02-26 01:15:47 +00:00
|
|
|
case 'l':
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
mlmac = true;
|
2004-02-26 01:15:47 +00:00
|
|
|
argappend(&newfs_arg, "-l");
|
|
|
|
break;
|
2001-06-18 23:46:58 +00:00
|
|
|
case 'M':
|
|
|
|
if (have_mdtype)
|
|
|
|
usage();
|
|
|
|
mdtype = MD_MALLOC;
|
|
|
|
have_mdtype = true;
|
2019-02-16 23:57:38 +00:00
|
|
|
argappend(&mdconfig_arg, "-o reserve");
|
2001-06-18 23:46:58 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
argappend(&newfs_arg, "-m %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
norun = true;
|
|
|
|
break;
|
|
|
|
case 'n':
|
2007-05-14 19:23:13 +00:00
|
|
|
argappend(&newfs_arg, "-n");
|
2001-06-18 23:46:58 +00:00
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
argappend(&newfs_arg, "-o %s", optarg);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
argappend(&mount_arg, "-o %s", optarg);
|
|
|
|
break;
|
2006-01-02 01:50:30 +00:00
|
|
|
case 'P':
|
|
|
|
newfs = false;
|
|
|
|
break;
|
2001-06-18 23:46:58 +00:00
|
|
|
case 'p':
|
2003-08-05 15:04:39 +00:00
|
|
|
if ((set = setmode(optarg)) == NULL)
|
2001-06-18 23:46:58 +00:00
|
|
|
usage();
|
2003-08-05 15:04:39 +00:00
|
|
|
mi.mi_mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
|
2001-06-18 23:46:58 +00:00
|
|
|
mi.mi_have_mode = true;
|
2011-09-13 20:16:11 +00:00
|
|
|
mi.mi_forced_pw = true;
|
2003-08-05 15:04:39 +00:00
|
|
|
free(set);
|
2001-06-18 23:46:58 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
softdep = false;
|
|
|
|
break;
|
|
|
|
case 's':
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
size_arg = optarg;
|
2001-06-18 23:46:58 +00:00
|
|
|
break;
|
2011-09-06 10:19:01 +00:00
|
|
|
case 't':
|
|
|
|
argappend(&newfs_arg, "-t");
|
|
|
|
break;
|
2016-03-11 06:07:09 +00:00
|
|
|
case 'T':
|
|
|
|
argappend(&mount_arg, "-t %s", optarg);
|
|
|
|
break;
|
2001-08-16 02:40:29 +00:00
|
|
|
case 'U':
|
|
|
|
softdep = true;
|
|
|
|
break;
|
2002-12-01 23:19:57 +00:00
|
|
|
case 'v':
|
|
|
|
argappend(&newfs_arg, "-O %s", optarg);
|
|
|
|
break;
|
2001-06-18 23:46:58 +00:00
|
|
|
case 'w':
|
|
|
|
extract_ugid(optarg, &mi);
|
2011-09-13 20:16:11 +00:00
|
|
|
mi.mi_forced_pw = true;
|
2001-06-18 23:46:58 +00:00
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
debug = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2001-08-14 14:14:20 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 2)
|
2001-06-18 23:46:58 +00:00
|
|
|
usage();
|
|
|
|
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
/*
|
|
|
|
* Historically our size arg was passed directly to mdconfig, which
|
|
|
|
* treats a number without a suffix as a count of 512-byte sectors;
|
|
|
|
* tmpfs would treat it as a count of bytes. To get predictable
|
|
|
|
* behavior for 'auto' we document that the size always uses mdconfig
|
|
|
|
* rules. To make that work, decode the size here so it can be passed
|
|
|
|
* to either tmpfs or mdconfig as a count of bytes.
|
|
|
|
*/
|
|
|
|
if (size_arg != NULL) {
|
|
|
|
mdsize = (intmax_t)strtoumax(size_arg, &p, 0);
|
|
|
|
if (p == size_arg || (p[0] != 0 && p[1] != 0) || mdsize < 0)
|
|
|
|
errx(1, "invalid size '%s'", size_arg);
|
|
|
|
switch (*p) {
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
mdsize *= 1024;
|
|
|
|
case 't':
|
|
|
|
case 'T':
|
|
|
|
mdsize *= 1024;
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
mdsize *= 1024;
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
mdsize *= 1024;
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
mdsize *= 1024;
|
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
mdsize *= 512;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "invalid size suffix on '%s'", size_arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Based on the command line 'md-device' either mount a tmpfs filesystem
|
|
|
|
* or configure the md device then format and mount a filesystem on it.
|
|
|
|
* If the device is 'auto' use tmpfs if it is available and there is no
|
|
|
|
* request for multilabel MAC (which tmpfs does not support).
|
|
|
|
*/
|
2001-08-14 14:14:20 +00:00
|
|
|
unitstr = argv[0];
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
mtpoint = argv[1];
|
|
|
|
|
|
|
|
if (strcmp(unitstr, "auto") == 0) {
|
|
|
|
if (mlmac)
|
|
|
|
idx = -1; /* Must use md for mlmac. */
|
|
|
|
else if ((idx = modfind("tmpfs")) == -1)
|
|
|
|
idx = kldload("tmpfs");
|
|
|
|
if (idx == -1)
|
|
|
|
unitstr = "md";
|
|
|
|
else
|
|
|
|
unitstr = "tmpfs";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(unitstr, "tmpfs") == 0) {
|
|
|
|
if (size_arg != NULL && mdsize != 0)
|
|
|
|
argappend(&mount_arg, "-o size=%jd", mdsize);
|
|
|
|
do_mount_tmpfs(mount_arg, mtpoint);
|
2001-06-18 23:46:58 +00:00
|
|
|
} else {
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
if (size_arg != NULL)
|
|
|
|
argappend(&mdconfig_arg, "-s %jdB", mdsize);
|
|
|
|
if (strncmp(unitstr, "/dev/", 5) == 0)
|
|
|
|
unitstr += 5;
|
|
|
|
if (strncmp(unitstr, mdname, mdnamelen) == 0)
|
|
|
|
unitstr += mdnamelen;
|
|
|
|
if (!isdigit(*unitstr)) {
|
|
|
|
autounit = true;
|
|
|
|
unit = -1;
|
|
|
|
mdsuffix = unitstr;
|
|
|
|
} else {
|
|
|
|
ul = strtoul(unitstr, &p, 10);
|
|
|
|
if (ul == ULONG_MAX)
|
|
|
|
errx(1, "bad device unit: %s", unitstr);
|
|
|
|
unit = ul;
|
|
|
|
mdsuffix = p; /* can be empty */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_mdtype)
|
|
|
|
mdtype = MD_SWAP;
|
|
|
|
if (softdep)
|
|
|
|
argappend(&newfs_arg, "-U");
|
|
|
|
if (mdtype != MD_VNODE && !newfs)
|
|
|
|
errx(1, "-P requires a vnode-backed disk");
|
|
|
|
|
|
|
|
/* Do the work. */
|
|
|
|
if (detach && !autounit)
|
|
|
|
do_mdconfig_detach();
|
|
|
|
if (autounit)
|
|
|
|
do_mdconfig_attach_au(mdconfig_arg, mdtype);
|
|
|
|
else
|
|
|
|
do_mdconfig_attach(mdconfig_arg, mdtype);
|
|
|
|
if (newfs)
|
|
|
|
do_newfs(newfs_arg);
|
|
|
|
do_mount_md(mount_arg, mtpoint);
|
2001-06-18 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do_mtptsetup(mtpoint, &mi);
|
2019-11-01 03:10:53 +00:00
|
|
|
if (skel != NULL)
|
|
|
|
do_copy(mtpoint, skel);
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append the expansion of 'fmt' to the buffer pointed to by '*dstp';
|
|
|
|
* reallocate as required.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
argappend(char **dstp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char *old, *new;
|
|
|
|
va_list ap;
|
2011-09-06 10:19:01 +00:00
|
|
|
|
2001-06-18 23:46:58 +00:00
|
|
|
old = *dstp;
|
|
|
|
assert(old != NULL);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (vasprintf(&new, fmt,ap) == -1)
|
|
|
|
errx(1, "vasprintf");
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
*dstp = new;
|
|
|
|
if (asprintf(&new, "%s %s", old, new) == -1)
|
|
|
|
errx(1, "asprintf");
|
|
|
|
free(*dstp);
|
|
|
|
free(old);
|
|
|
|
|
|
|
|
*dstp = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If run-time debugging is enabled, print the expansion of 'fmt'.
|
|
|
|
* Otherwise, do nothing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
debugprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (!debug)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "DEBUG: ");
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach a memory disk with a known unit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_mdconfig_attach(const char *args, const enum md_types mdtype)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
const char *ta; /* Type arg. */
|
|
|
|
|
|
|
|
switch (mdtype) {
|
|
|
|
case MD_SWAP:
|
|
|
|
ta = "-t swap";
|
|
|
|
break;
|
|
|
|
case MD_VNODE:
|
|
|
|
ta = "-t vnode";
|
|
|
|
break;
|
|
|
|
case MD_MALLOC:
|
|
|
|
ta = "-t malloc";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2006-02-16 21:28:54 +00:00
|
|
|
rv = run(NULL, "%s -a %s%s -u %s%d", path_mdconfig, ta, args,
|
2001-06-18 23:46:58 +00:00
|
|
|
mdname, unit);
|
|
|
|
if (rv)
|
2018-10-20 21:33:00 +00:00
|
|
|
errx(1, "mdconfig (attach) exited %s %d", run_exitstr(rv),
|
|
|
|
run_exitnumber(rv));
|
2001-06-18 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach a memory disk with an unknown unit; use autounit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_mdconfig_attach_au(const char *args, const enum md_types mdtype)
|
|
|
|
{
|
|
|
|
const char *ta; /* Type arg. */
|
2019-02-11 21:31:26 +00:00
|
|
|
char *linep;
|
|
|
|
char linebuf[12]; /* 32-bit unit (10) + '\n' (1) + '\0' (1) */
|
2001-06-18 23:46:58 +00:00
|
|
|
int fd; /* Standard output of mdconfig invocation. */
|
|
|
|
FILE *sfd;
|
|
|
|
int rv;
|
|
|
|
char *p;
|
|
|
|
size_t linelen;
|
2005-01-31 04:45:45 +00:00
|
|
|
unsigned long ul;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
switch (mdtype) {
|
|
|
|
case MD_SWAP:
|
|
|
|
ta = "-t swap";
|
|
|
|
break;
|
|
|
|
case MD_VNODE:
|
|
|
|
ta = "-t vnode";
|
|
|
|
break;
|
|
|
|
case MD_MALLOC:
|
|
|
|
ta = "-t malloc";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2006-02-16 21:28:54 +00:00
|
|
|
rv = run(&fd, "%s -a %s%s", path_mdconfig, ta, args);
|
2001-06-18 23:46:58 +00:00
|
|
|
if (rv)
|
2018-10-20 21:33:00 +00:00
|
|
|
errx(1, "mdconfig (attach) exited %s %d", run_exitstr(rv),
|
|
|
|
run_exitnumber(rv));
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
/* Receive the unit number. */
|
|
|
|
if (norun) { /* Since we didn't run, we can't read. Fake it. */
|
2005-12-22 10:36:58 +00:00
|
|
|
unit = 0;
|
2001-06-18 23:46:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sfd = fdopen(fd, "r");
|
|
|
|
if (sfd == NULL)
|
|
|
|
err(1, "fdopen");
|
|
|
|
linep = fgetln(sfd, &linelen);
|
|
|
|
/* If the output format changes, we want to know about it. */
|
2019-02-11 21:31:26 +00:00
|
|
|
if (linep == NULL || linelen <= mdnamelen + 1 ||
|
|
|
|
linelen - mdnamelen >= sizeof(linebuf) ||
|
|
|
|
strncmp(linep, mdname, mdnamelen) != 0)
|
|
|
|
errx(1, "unexpected output from mdconfig (attach)");
|
|
|
|
linep += mdnamelen;
|
|
|
|
linelen -= mdnamelen;
|
2001-06-18 23:46:58 +00:00
|
|
|
/* Can't use strlcpy because linep is not NULL-terminated. */
|
2019-02-11 21:31:26 +00:00
|
|
|
strncpy(linebuf, linep, linelen);
|
2001-06-18 23:46:58 +00:00
|
|
|
linebuf[linelen] = '\0';
|
2005-01-31 04:45:45 +00:00
|
|
|
ul = strtoul(linebuf, &p, 10);
|
|
|
|
if (ul == ULONG_MAX || *p != '\n')
|
2001-06-18 23:46:58 +00:00
|
|
|
errx(1, "unexpected output from mdconfig (attach)");
|
2005-01-31 04:45:45 +00:00
|
|
|
unit = ul;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
fclose(sfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach a memory disk.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_mdconfig_detach(void)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2006-02-16 21:28:54 +00:00
|
|
|
rv = run(NULL, "%s -d -u %s%d", path_mdconfig, mdname, unit);
|
2001-06-18 23:46:58 +00:00
|
|
|
if (rv && debug) /* This is allowed to fail. */
|
2018-10-20 21:33:00 +00:00
|
|
|
warnx("mdconfig (detach) exited %s %d (ignored)",
|
|
|
|
run_exitstr(rv), run_exitnumber(rv));
|
2001-06-18 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mount the configured memory disk.
|
|
|
|
*/
|
|
|
|
static void
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
do_mount_md(const char *args, const char *mtpoint)
|
2001-06-18 23:46:58 +00:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2007-02-15 13:49:44 +00:00
|
|
|
rv = run(NULL, "%s%s /dev/%s%d%s %s", _PATH_MOUNT, args,
|
|
|
|
mdname, unit, mdsuffix, mtpoint);
|
2001-06-18 23:46:58 +00:00
|
|
|
if (rv)
|
2018-10-20 21:33:00 +00:00
|
|
|
errx(1, "mount exited %s %d", run_exitstr(rv),
|
|
|
|
run_exitnumber(rv));
|
2001-06-18 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
/*
|
|
|
|
* Mount the configured tmpfs.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_mount_tmpfs(const char *args, const char *mtpoint)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = run(NULL, "%s -t tmpfs %s tmp %s", _PATH_MOUNT, args, mtpoint);
|
|
|
|
if (rv)
|
2018-10-20 21:33:00 +00:00
|
|
|
errx(1, "tmpfs mount exited %s %d", run_exitstr(rv),
|
|
|
|
run_exitnumber(rv));
|
Enhance mdmfs(8) to work with tmpfs(5).
Existing scripts and associated config such as rc.initdiskless, rc.d/var,
and others, use mdmfs to create memory filesystems. That program accepts a
size argument which allows SI suffixes and treats an unsuffixed number as a
count of 512 byte sectors. That makes it difficult to convert existing
scripts to use tmpfs instead of mdmfs, because tmpfs treats unsuffixed
numbers as a count of bytes. The script logic to deal with existing user
config that might include suffixed and unsuffixed numbers is... unpleasant.
Also, there is no g'tee that tmpfs will be available. It is sometimes
configured out of small-resource embedded systems to save memory and flash
storage space.
These changes enhance mdmfs(8) so that it accepts two new values for the
'md-device' arg: 'tmpfs' and 'auto'. With tmpfs, the program always uses
tmpfs(5) (and fails if it's not available). With 'auto' the program prefers
tmpfs, but falls back to using md(4) if tmpfs isn't available. It also
handles the -s <size> argument so that the mdconfig interpetation of
unsuffixed numbers applies when tmpfs is used as well, so that existing user
config keeps working after a switch to tmpfs.
A new rc setting, mfs_type, is added to etc/defaults/rc.conf to let users
force the use of tmpfs or md; the default value is "auto".
Differential Revision: https://reviews.freebsd.org/D12301
2017-09-29 22:13:26 +00:00
|
|
|
}
|
|
|
|
|
2001-06-18 23:46:58 +00:00
|
|
|
/*
|
|
|
|
* Various configuration of the mountpoint. Mostly, enact 'mip'.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_mtptsetup(const char *mtpoint, struct mtpt_info *mip)
|
|
|
|
{
|
2011-09-13 20:16:11 +00:00
|
|
|
struct statfs sfs;
|
|
|
|
|
|
|
|
if (!mip->mi_have_mode && !mip->mi_have_uid && !mip->mi_have_gid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!norun) {
|
|
|
|
if (statfs(mtpoint, &sfs) == -1) {
|
|
|
|
warn("statfs: %s", mtpoint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((sfs.f_flags & MNT_RDONLY) != 0) {
|
|
|
|
if (mip->mi_forced_pw) {
|
|
|
|
warnx(
|
|
|
|
"Not changing mode/owner of %s since it is read-only",
|
|
|
|
mtpoint);
|
|
|
|
} else {
|
|
|
|
debugprintf(
|
|
|
|
"Not changing mode/owner of %s since it is read-only",
|
|
|
|
mtpoint);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
if (mip->mi_have_mode) {
|
|
|
|
debugprintf("changing mode of %s to %o.", mtpoint,
|
|
|
|
mip->mi_mode);
|
|
|
|
if (!norun)
|
|
|
|
if (chmod(mtpoint, mip->mi_mode) == -1)
|
|
|
|
err(1, "chmod: %s", mtpoint);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We have to do these separately because the user may have
|
|
|
|
* only specified one of them.
|
|
|
|
*/
|
|
|
|
if (mip->mi_have_uid) {
|
|
|
|
debugprintf("changing owner (user) or %s to %u.", mtpoint,
|
|
|
|
mip->mi_uid);
|
|
|
|
if (!norun)
|
|
|
|
if (chown(mtpoint, mip->mi_uid, -1) == -1)
|
|
|
|
err(1, "chown %s to %u (user)", mtpoint,
|
|
|
|
mip->mi_uid);
|
|
|
|
}
|
|
|
|
if (mip->mi_have_gid) {
|
|
|
|
debugprintf("changing owner (group) or %s to %u.", mtpoint,
|
|
|
|
mip->mi_gid);
|
|
|
|
if (!norun)
|
|
|
|
if (chown(mtpoint, -1, mip->mi_gid) == -1)
|
|
|
|
err(1, "chown %s to %u (group)", mtpoint,
|
|
|
|
mip->mi_gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-08-21 18:11:48 +00:00
|
|
|
* Put a file system on the memory disk.
|
2001-06-18 23:46:58 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_newfs(const char *args)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2003-06-29 18:06:05 +00:00
|
|
|
rv = run(NULL, "%s%s /dev/%s%d", _PATH_NEWFS, args, mdname, unit);
|
2001-06-18 23:46:58 +00:00
|
|
|
if (rv)
|
2018-10-20 21:33:00 +00:00
|
|
|
errx(1, "newfs exited %s %d", run_exitstr(rv),
|
|
|
|
run_exitnumber(rv));
|
2001-06-18 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 03:10:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy skel into the mountpoint.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_copy(const char *mtpoint, const char *skel)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = chdir(skel);
|
|
|
|
if (rv != 0)
|
|
|
|
err(1, "chdir to %s", skel);
|
|
|
|
rv = run(NULL, "/bin/pax -rw -pe . %s", mtpoint);
|
|
|
|
if (rv != 0)
|
|
|
|
errx(1, "skel copy failed");
|
|
|
|
}
|
|
|
|
|
2001-06-18 23:46:58 +00:00
|
|
|
/*
|
|
|
|
* 'str' should be a user and group name similar to the last argument
|
|
|
|
* to chown(1); i.e., a user, followed by a colon, followed by a
|
|
|
|
* group. The user and group in 'str' may be either a [ug]id or a
|
|
|
|
* name. Upon return, the uid and gid fields in 'mip' will contain
|
|
|
|
* the uid and gid of the user and group name in 'str', respectively.
|
|
|
|
*
|
|
|
|
* In other words, this derives a user and group id from a string
|
|
|
|
* formatted like the last argument to chown(1).
|
2005-10-14 11:21:21 +00:00
|
|
|
*
|
|
|
|
* Notice: At this point we don't support only a username or only a
|
|
|
|
* group name. do_mtptsetup already does, so when this feature is
|
|
|
|
* desired, this is the only routine that needs to be changed.
|
2001-06-18 23:46:58 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
extract_ugid(const char *str, struct mtpt_info *mip)
|
|
|
|
{
|
|
|
|
char *ug; /* Writable 'str'. */
|
|
|
|
char *user, *group; /* Result of extracton. */
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
char *p;
|
|
|
|
uid_t *uid;
|
|
|
|
gid_t *gid;
|
|
|
|
|
|
|
|
uid = &mip->mi_uid;
|
|
|
|
gid = &mip->mi_gid;
|
|
|
|
mip->mi_have_uid = mip->mi_have_gid = false;
|
|
|
|
|
|
|
|
/* Extract the user and group from 'str'. Format above. */
|
2001-06-24 18:21:52 +00:00
|
|
|
ug = strdup(str);
|
2001-06-18 23:46:58 +00:00
|
|
|
assert(ug != NULL);
|
|
|
|
group = ug;
|
|
|
|
user = strsep(&group, ":");
|
|
|
|
if (user == NULL || group == NULL || *user == '\0' || *group == '\0')
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* Derive uid. */
|
|
|
|
*uid = strtoul(user, &p, 10);
|
2003-07-11 05:47:05 +00:00
|
|
|
if (*uid == (uid_t)ULONG_MAX)
|
2001-06-18 23:46:58 +00:00
|
|
|
usage();
|
|
|
|
if (*p != '\0') {
|
|
|
|
pw = getpwnam(user);
|
|
|
|
if (pw == NULL)
|
|
|
|
errx(1, "invalid user: %s", user);
|
|
|
|
*uid = pw->pw_uid;
|
|
|
|
}
|
2005-10-14 11:21:21 +00:00
|
|
|
mip->mi_have_uid = true;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
/* Derive gid. */
|
|
|
|
*gid = strtoul(group, &p, 10);
|
2003-07-11 05:47:05 +00:00
|
|
|
if (*gid == (gid_t)ULONG_MAX)
|
2001-06-18 23:46:58 +00:00
|
|
|
usage();
|
|
|
|
if (*p != '\0') {
|
|
|
|
gr = getgrnam(group);
|
|
|
|
if (gr == NULL)
|
|
|
|
errx(1, "invalid group: %s", group);
|
|
|
|
*gid = gr->gr_gid;
|
|
|
|
}
|
2005-10-14 11:21:21 +00:00
|
|
|
mip->mi_have_gid = true;
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
free(ug);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run a process with command name and arguments pointed to by the
|
|
|
|
* formatted string 'cmdline'. Since system(3) is not used, the first
|
|
|
|
* space-delimited token of 'cmdline' must be the full pathname of the
|
2018-10-20 21:33:00 +00:00
|
|
|
* program to run.
|
|
|
|
*
|
|
|
|
* The return value is the return code of the process spawned, or a negative
|
|
|
|
* signal number if the process exited due to an uncaught signal.
|
|
|
|
*
|
|
|
|
* If 'ofd' is non-NULL, it is set to the standard output of
|
2001-06-18 23:46:58 +00:00
|
|
|
* the program spawned (i.e., you can read from ofd and get the output
|
|
|
|
* of the program).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
run(int *ofd, const char *cmdline, ...)
|
|
|
|
{
|
2001-08-14 14:14:20 +00:00
|
|
|
char **argv, **argvp; /* Result of splitting 'cmd'. */
|
|
|
|
int argc;
|
2001-06-18 23:46:58 +00:00
|
|
|
char *cmd; /* Expansion of 'cmdline'. */
|
|
|
|
int pid, status; /* Child info. */
|
|
|
|
int pfd[2]; /* Pipe to the child. */
|
|
|
|
int nfd; /* Null (/dev/null) file descriptor. */
|
|
|
|
bool dup2dn; /* Dup /dev/null to stdout? */
|
|
|
|
va_list ap;
|
|
|
|
char *p;
|
|
|
|
int rv, i;
|
|
|
|
|
|
|
|
dup2dn = true;
|
|
|
|
va_start(ap, cmdline);
|
|
|
|
rv = vasprintf(&cmd, cmdline, ap);
|
|
|
|
if (rv == -1)
|
|
|
|
err(1, "vasprintf");
|
|
|
|
va_end(ap);
|
|
|
|
|
2001-08-14 14:14:20 +00:00
|
|
|
/* Split up 'cmd' into 'argv' for use with execve. */
|
|
|
|
for (argc = 1, p = cmd; (p = strchr(p, ' ')) != NULL; p++)
|
|
|
|
argc++; /* 'argc' generation loop. */
|
|
|
|
argv = (char **)malloc(sizeof(*argv) * (argc + 1));
|
|
|
|
assert(argv != NULL);
|
|
|
|
for (p = cmd, argvp = argv; (*argvp = strsep(&p, " ")) != NULL;)
|
2007-04-30 13:21:43 +00:00
|
|
|
if (**argvp != '\0')
|
2001-08-14 14:14:20 +00:00
|
|
|
if (++argvp >= &argv[argc]) {
|
|
|
|
*argvp = NULL;
|
2001-06-18 23:46:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-08-14 14:14:20 +00:00
|
|
|
assert(*argv);
|
2007-04-30 13:21:43 +00:00
|
|
|
/* The argv array ends up NULL-terminated here. */
|
2001-06-18 23:46:58 +00:00
|
|
|
|
|
|
|
/* Make sure the above loop works as expected. */
|
|
|
|
if (debug) {
|
|
|
|
/*
|
|
|
|
* We can't, but should, use debugprintf here. First,
|
|
|
|
* it appends a trailing newline to the output, and
|
|
|
|
* second it prepends "DEBUG: " to the output. The
|
|
|
|
* former is a problem for this would-be first call,
|
|
|
|
* and the latter for the would-be call inside the
|
|
|
|
* loop.
|
|
|
|
*/
|
|
|
|
(void)fprintf(stderr, "DEBUG: running:");
|
2012-01-07 16:09:33 +00:00
|
|
|
/* Should be equivalent to 'cmd' (before strsep, of course). */
|
2001-08-14 14:14:20 +00:00
|
|
|
for (i = 0; argv[i] != NULL; i++)
|
|
|
|
(void)fprintf(stderr, " %s", argv[i]);
|
2001-06-18 23:46:58 +00:00
|
|
|
(void)fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a pipe if necessary and fork the helper program. */
|
|
|
|
if (ofd != NULL) {
|
|
|
|
if (pipe(&pfd[0]) == -1)
|
|
|
|
err(1, "pipe");
|
|
|
|
*ofd = pfd[0];
|
|
|
|
dup2dn = false;
|
|
|
|
}
|
|
|
|
pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case 0:
|
|
|
|
/* XXX can we call err() in here? */
|
|
|
|
if (norun)
|
|
|
|
_exit(0);
|
|
|
|
if (ofd != NULL)
|
|
|
|
if (dup2(pfd[1], STDOUT_FILENO) < 0)
|
|
|
|
err(1, "dup2");
|
|
|
|
if (!loudsubs) {
|
|
|
|
nfd = open(_PATH_DEVNULL, O_RDWR);
|
|
|
|
if (nfd == -1)
|
|
|
|
err(1, "open: %s", _PATH_DEVNULL);
|
|
|
|
if (dup2(nfd, STDIN_FILENO) < 0)
|
|
|
|
err(1, "dup2");
|
|
|
|
if (dup2dn)
|
|
|
|
if (dup2(nfd, STDOUT_FILENO) < 0)
|
|
|
|
err(1, "dup2");
|
|
|
|
if (dup2(nfd, STDERR_FILENO) < 0)
|
|
|
|
err(1, "dup2");
|
|
|
|
}
|
|
|
|
|
2001-08-14 14:14:20 +00:00
|
|
|
(void)execv(argv[0], argv);
|
|
|
|
warn("exec: %s", argv[0]);
|
2001-06-18 23:46:58 +00:00
|
|
|
_exit(-1);
|
|
|
|
case -1:
|
|
|
|
err(1, "fork");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cmd);
|
2001-08-14 14:14:20 +00:00
|
|
|
free(argv);
|
2001-06-18 23:46:58 +00:00
|
|
|
while (waitpid(pid, &status, 0) != pid)
|
|
|
|
;
|
2018-10-20 21:33:00 +00:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
return (WEXITSTATUS(status));
|
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
return (-WTERMSIG(status));
|
|
|
|
err(1, "unexpected waitpid status: 0x%x", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If run() returns non-zero, provide a string explaining why.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
run_exitstr(int rv)
|
|
|
|
{
|
|
|
|
if (rv > 0)
|
|
|
|
return ("with error code");
|
|
|
|
if (rv < 0)
|
|
|
|
return ("with signal");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If run returns non-zero, provide a relevant number.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
run_exitnumber(int rv)
|
|
|
|
{
|
|
|
|
if (rv < 0)
|
|
|
|
return (-rv);
|
|
|
|
return (rv);
|
2001-06-18 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2006-11-03 12:02:24 +00:00
|
|
|
fprintf(stderr,
|
2011-09-06 10:19:01 +00:00
|
|
|
"usage: %s [-DLlMNnPStUX] [-a maxcontig] [-b block-size]\n"
|
2007-02-15 14:46:04 +00:00
|
|
|
"\t[-c blocks-per-cylinder-group][-d max-extent-size] [-E path-mdconfig]\n"
|
2019-11-01 03:10:53 +00:00
|
|
|
"\t[-e maxbpg] [-F file] [-f frag-size] [-i bytes] [-k skel]\n"
|
|
|
|
"\t[-m percent-free] [-O optimization] [-o mount-options]\n"
|
2007-02-15 14:46:04 +00:00
|
|
|
"\t[-p permissions] [-s size] [-v version] [-w user:group]\n"
|
|
|
|
"\tmd-device mount-point\n", getprogname());
|
2001-06-18 23:46:58 +00:00
|
|
|
exit(1);
|
|
|
|
}
|