// singlemount() can loop through /etc/filesystems for fstype detection.
// mount_it_now() does the actual mount.
//
+
+//usage:#define mount_trivial_usage
+//usage: "[OPTIONS] [-o OPTS] DEVICE NODE"
+//usage:#define mount_full_usage "\n\n"
+//usage: "Mount a filesystem. Filesystem autodetection requires /proc.\n"
+//usage: "\n -a Mount all filesystems in fstab"
+//usage: IF_FEATURE_MOUNT_FAKE(
+//usage: IF_FEATURE_MTAB_SUPPORT(
+//usage: "\n -f Update /etc/mtab, but don't mount"
+//usage: )
+//usage: IF_NOT_FEATURE_MTAB_SUPPORT(
+//usage: "\n -f Dry run"
+//usage: )
+//usage: )
+//usage: IF_FEATURE_MOUNT_HELPERS(
+//usage: "\n -i Don't run mount helper"
+//usage: )
+//usage: IF_FEATURE_MTAB_SUPPORT(
+//usage: "\n -n Don't update /etc/mtab"
+//usage: )
+//usage: IF_FEATURE_MOUNT_VERBOSE(
+//usage: "\n -v Verbose"
+//usage: )
+////usage: "\n -s Sloppy (ignored)"
+//usage: "\n -r Read-only mount"
+//usage: "\n -w Read-write mount (default)"
+//usage: "\n -t FSTYPE[,...] Filesystem type(s)"
+//usage: "\n -O OPT Mount only filesystems with option OPT (-a only)"
+//usage: "\n-o OPT:"
+//usage: IF_FEATURE_MOUNT_LOOP(
+//usage: "\n loop Ignored (loop devices are autodetected)"
+//usage: )
+//usage: IF_FEATURE_MOUNT_FLAGS(
+//usage: "\n [a]sync Writes are [a]synchronous"
+//usage: "\n [no]atime Disable/enable updates to inode access times"
+//usage: "\n [no]diratime Disable/enable atime updates to directories"
+//usage: "\n [no]relatime Disable/enable atime updates relative to modification time"
+//usage: "\n [no]dev (Dis)allow use of special device files"
+//usage: "\n [no]exec (Dis)allow use of executable files"
+//usage: "\n [no]suid (Dis)allow set-user-id-root programs"
+//usage: "\n [r]shared Convert [recursively] to a shared subtree"
+//usage: "\n [r]slave Convert [recursively] to a slave subtree"
+//usage: "\n [r]private Convert [recursively] to a private subtree"
+//usage: "\n [un]bindable Make mount point [un]able to be bind mounted"
+//usage: "\n [r]bind Bind a file or directory [recursively] to another location"
+//usage: "\n move Relocate an existing mount point"
+//usage: )
+//usage: "\n remount Remount a mounted filesystem, changing flags"
+//usage: "\n ro/rw Same as -r/-w"
+//usage: "\n"
+//usage: "\nThere are filesystem-specific -o flags."
+//usage:
+//usage:#define mount_example_usage
+//usage: "$ mount\n"
+//usage: "/dev/hda3 on / type minix (rw)\n"
+//usage: "proc on /proc type proc (rw)\n"
+//usage: "devpts on /dev/pts type devpts (rw)\n"
+//usage: "$ mount /dev/fd0 /mnt -t msdos -o ro\n"
+//usage: "$ mount /tmp/diskimage /opt -t ext2 -o loop\n"
+//usage: "$ mount cd_image.iso mydir\n"
+//usage:#define mount_notes_usage
+//usage: "Returns 0 for success, number of failed mounts for -a, or errno for one mount."
+
#include <mntent.h>
#include <syslog.h>
#include <sys/mount.h>
#ifndef MS_RELATIME
# define MS_RELATIME (1 << 21)
#endif
+#ifndef MS_STRICTATIME
+# define MS_STRICTATIME (1 << 24)
+#endif
+
+/* Any ~MS_FOO value has this bit set: */
+#define BB_MS_INVERTED_VALUE (1u << 31)
#include "libbb.h"
#if ENABLE_FEATURE_MOUNT_LABEL
IF_DESKTOP(/* "user" */ MOUNT_USERS,)
IF_DESKTOP(/* "users" */ MOUNT_USERS,)
/* "_netdev" */ 0,
+ IF_DESKTOP(/* "comment=" */ 0,) /* systemd uses this in fstab */
)
IF_FEATURE_MOUNT_FLAGS(
/* "nomand" */ ~MS_MANDLOCK,
/* "relatime" */ MS_RELATIME,
/* "norelatime" */ ~MS_RELATIME,
+ /* "strictatime" */ MS_STRICTATIME,
/* "loud" */ ~MS_SILENT,
/* "rbind" */ MS_BIND|MS_RECURSIVE,
IF_DESKTOP("user\0")
IF_DESKTOP("users\0")
"_netdev\0"
+ IF_DESKTOP("comment=\0") /* systemd uses this in fstab */
)
IF_FEATURE_MOUNT_FLAGS(
// vfs flags
"nomand\0"
"relatime\0"
"norelatime\0"
+ "strictatime\0"
"loud\0"
"rbind\0"
#endif
#define fslist (G.fslist )
#define getmntent_buf (G.getmntent_buf )
+#define INIT_G() do { } while (0)
#if ENABLE_FEATURE_MTAB_SUPPORT
/*
// Use the mount_options list to parse options into flags.
// Also update list of unrecognized options if unrecognized != NULL
-static long parse_mount_options(char *options, char **unrecognized)
+static unsigned long parse_mount_options(char *options, char **unrecognized)
{
- long flags = MS_SILENT;
+ unsigned long flags = MS_SILENT;
// Loop through options
for (;;) {
// FIXME: use hasmntopt()
// Find this option in mount_options
for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
- if (strcasecmp(option_str, options) == 0) {
- long fl = mount_options[i];
- if (fl < 0)
+ unsigned opt_len = strlen(option_str);
+
+ if (strncasecmp(option_str, options, opt_len) == 0
+ && (options[opt_len] == '\0'
+ /* or is it "comment=" thingy in fstab? */
+ IF_FEATURE_MOUNT_FSTAB(IF_DESKTOP( || option_str[opt_len-1] == '=' ))
+ )
+ ) {
+ unsigned long fl = mount_options[i];
+ if (fl & BB_MS_INVERTED_VALUE)
flags &= fl;
else
flags |= fl;
goto found;
}
- option_str += strlen(option_str) + 1;
+ option_str += opt_len + 1;
}
// We did not recognize this option.
// If "unrecognized" is not NULL, append option there.
while ((buf = xmalloc_fgetline(f)) != NULL) {
if (strncmp(buf, "nodev", 5) == 0 && isspace(buf[5]))
- continue;
+ goto next;
fs = skip_whitespace(buf);
if (*fs == '#' || *fs == '*' || !*fs)
- continue;
+ goto next;
llist_add_to_end(&list, xstrdup(fs));
+ next:
free(buf);
}
if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
// Perform actual mount of specific filesystem at specific location.
// NB: mp->xxx fields may be trashed on exit
-static int mount_it_now(struct mntent *mp, long vfsflags, char *filteropts)
+static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filteropts)
{
int rc = 0;
*/
struct nfs2_fh {
- char data[32];
+ char data[32];
};
struct nfs3_fh {
- unsigned short size;
- unsigned char data[64];
+ unsigned short size;
+ unsigned char data[64];
};
struct nfs_mount_data {
- int version; /* 1 */
- int fd; /* 1 */
- struct nfs2_fh old_root; /* 1 */
- int flags; /* 1 */
- int rsize; /* 1 */
- int wsize; /* 1 */
- int timeo; /* 1 */
- int retrans; /* 1 */
- int acregmin; /* 1 */
- int acregmax; /* 1 */
- int acdirmin; /* 1 */
- int acdirmax; /* 1 */
- struct sockaddr_in addr; /* 1 */
- char hostname[256]; /* 1 */
- int namlen; /* 2 */
- unsigned int bsize; /* 3 */
- struct nfs3_fh root; /* 4 */
+ int version; /* 1 */
+ int fd; /* 1 */
+ struct nfs2_fh old_root; /* 1 */
+ int flags; /* 1 */
+ int rsize; /* 1 */
+ int wsize; /* 1 */
+ int timeo; /* 1 */
+ int retrans; /* 1 */
+ int acregmin; /* 1 */
+ int acregmax; /* 1 */
+ int acdirmin; /* 1 */
+ int acdirmax; /* 1 */
+ struct sockaddr_in addr; /* 1 */
+ char hostname[256]; /* 1 */
+ int namlen; /* 2 */
+ unsigned int bsize; /* 3 */
+ struct nfs3_fh root; /* 4 */
};
/* bits in the flags field */
NFS_MOUNT_VER3 = 0x0080, /* 3 */
NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
NFS_MOUNT_NONLM = 0x0200, /* 3 */
+ NFS_MOUNT_NOACL = 0x0800, /* 4 */
NFS_MOUNT_NORDIRPLUS = 0x4000
};
static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
{
if (!xdr_u_int(xdrs, &objp->fhs_status))
- return FALSE;
+ return FALSE;
if (objp->fhs_status == 0)
return xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle);
return TRUE;
static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
{
return xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
- (unsigned int *) &objp->fhandle3_len,
- FHSIZE3);
+ (unsigned int *) &objp->fhandle3_len,
+ FHSIZE3);
}
static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
if (!xdr_fhandle3(xdrs, &objp->fhandle))
return FALSE;
return xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val),
- &(objp->auth_flavours.auth_flavours_len),
- ~0,
- sizeof(int),
- (xdrproc_t) xdr_int);
+ &(objp->auth_flavours.auth_flavours_len),
+ ~0,
+ sizeof(int),
+ (xdrproc_t) xdr_int);
}
static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
}
/* NB: mp->xxx fields may be trashed on exit */
-static NOINLINE int nfsmount(struct mntent *mp, long vfsflags, char *filteropts)
+static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
{
CLIENT *mclient;
char *hostname;
int noac;
int nordirplus;
int nolock;
+ int noacl;
find_kernel_nfs_mount_version();
pathname = s + 1;
*s = '\0';
/* Ignore all but first hostname in replicated mounts
- until they can be fully supported. (mack@sgi.com) */
+ * until they can be fully supported. (mack@sgi.com) */
s = strchr(hostname, ',');
if (s) {
*s = '\0';
nolock = 0;
noac = 0;
nordirplus = 0;
+ noacl = 0;
retry = 10000; /* 10000 minutes ~ 1 week */
- tcp = 0;
+ tcp = 1; /* nfs-utils uses tcp per default */
mountprog = MOUNTPROG;
mountvers = 0;
"tcp\0"
"udp\0"
"lock\0"
- "rdirplus\0";
+ "rdirplus\0"
+ "acl\0";
int val = 1;
if (!strncmp(opt, "no", 2)) {
val = 0;
case 11: //rdirplus
nordirplus = !val;
break;
+ case 12: // acl
+ noacl = !val;
+ break;
default:
bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
goto fail;
| (posix ? NFS_MOUNT_POSIX : 0)
| (nocto ? NFS_MOUNT_NOCTO : 0)
| (noac ? NFS_MOUNT_NOAC : 0)
- | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0);
+ | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0)
+ | (noacl ? NFS_MOUNT_NOACL : 0);
if (nfs_mount_version >= 2)
data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
if (nfs_mount_version >= 3)
switch (pm_mnt.pm_prot) {
case IPPROTO_UDP:
mclient = clntudp_create(&mount_server_addr,
- pm_mnt.pm_prog,
- pm_mnt.pm_vers,
- retry_timeout,
- &msock);
+ pm_mnt.pm_prog,
+ pm_mnt.pm_vers,
+ retry_timeout,
+ &msock);
if (mclient)
break;
mount_server_addr.sin_port = htons(pm_mnt.pm_port);
msock = RPC_ANYSOCK;
case IPPROTO_TCP:
mclient = clnttcp_create(&mount_server_addr,
- pm_mnt.pm_prog,
- pm_mnt.pm_vers,
- &msock, 0, 0);
+ pm_mnt.pm_prog,
+ pm_mnt.pm_vers,
+ &msock, 0, 0);
break;
default:
mclient = NULL;
if (pm_mnt.pm_vers == 3)
clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
- (xdrproc_t) xdr_dirpath,
- (caddr_t) &pathname,
- (xdrproc_t) xdr_mountres3,
- (caddr_t) &status,
- total_timeout);
+ (xdrproc_t) xdr_dirpath,
+ (caddr_t) &pathname,
+ (xdrproc_t) xdr_mountres3,
+ (caddr_t) &status,
+ total_timeout);
else
clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
- (xdrproc_t) xdr_dirpath,
- (caddr_t) &pathname,
- (xdrproc_t) xdr_fhstatus,
- (caddr_t) &status,
- total_timeout);
+ (xdrproc_t) xdr_dirpath,
+ (caddr_t) &pathname,
+ (xdrproc_t) xdr_fhstatus,
+ (caddr_t) &status,
+ total_timeout);
if (clnt_stat == RPC_SUCCESS)
goto prepare_kernel_data; /* we're done */
/* Perform actual mount */
do_mount:
- mp->mnt_type = (char*)"nfs";
retval = mount_it_now(mp, vfsflags, (char*)&data);
goto ret;
#else // !ENABLE_FEATURE_MOUNT_NFS
-// Never called. Call should be optimized out.
-int nfsmount(struct mntent *mp, long vfsflags, char *filteropts);
+/* Linux 2.6.23+ supports nfs mounts with options passed as a string.
+ * For older kernels, you must build busybox with ENABLE_FEATURE_MOUNT_NFS.
+ * (However, note that then you lose any chances that NFS over IPv6 would work).
+ */
+static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
+{
+ len_and_sockaddr *lsa;
+ char *opts;
+ char *end;
+ char *dotted;
+ int ret;
+
+# if ENABLE_FEATURE_IPV6
+ end = strchr(mp->mnt_fsname, ']');
+ if (end && end[1] == ':')
+ end++;
+ else
+# endif
+ /* mount_main() guarantees that ':' is there */
+ end = strchr(mp->mnt_fsname, ':');
+
+ *end = '\0';
+ lsa = xhost2sockaddr(mp->mnt_fsname, /*port:*/ 0);
+ *end = ':';
+ dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
+ if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
+ opts = xasprintf("%s%saddr=%s",
+ filteropts ? filteropts : "",
+ filteropts ? "," : "",
+ dotted
+ );
+ if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
+ ret = mount_it_now(mp, vfsflags, opts);
+ if (ENABLE_FEATURE_CLEAN_UP) free(opts);
+
+ return ret;
+}
#endif // !ENABLE_FEATURE_MOUNT_NFS
static int singlemount(struct mntent *mp, int ignore_busy)
{
int rc = -1;
- long vfsflags;
+ unsigned long vfsflags;
char *loopFile = NULL, *filteropts = NULL;
llist_t *fl = NULL;
struct stat st;
) {
int len;
char c;
+ char *hostname, *share;
+ char *dotted, *ip;
len_and_sockaddr *lsa;
- char *hostname, *dotted, *ip;
+
+ // Parse mp->mnt_fsname of the form "//hostname/share[/dir1/dir2]"
hostname = mp->mnt_fsname + 2;
len = strcspn(hostname, "/\\");
- if (len == 0 || hostname[len] == '\0')
+ share = hostname + len + 1;
+ if (len == 0 // 3rd char is a [back]slash (IOW: empty hostname)
+ || share[-1] == '\0' // no [back]slash after hostname
+ || share[0] == '\0' // empty share name
+ ) {
goto report_error;
- c = hostname[len];
- hostname[len] = '\0';
+ }
+ c = share[-1];
+ share[-1] = '\0';
+ len = strcspn(share, "/\\");
+
+ // "unc=\\hostname\share" option is mandatory
+ // after CIFS option parsing was rewritten in Linux 3.4.
+ // Must use backslashes.
+ // If /dir1/dir2 is present, also add "prefixpath=dir1/dir2"
+ {
+ char *unc = xasprintf(
+ share[len] != '\0' /* "/dir1/dir2" exists? */
+ ? "unc=\\\\%s\\%.*s,prefixpath=%s"
+ : "unc=\\\\%s\\%.*s",
+ hostname,
+ len, share,
+ share + len + 1 /* "dir1/dir2" */
+ );
+ parse_mount_options(unc, &filteropts);
+ if (ENABLE_FEATURE_CLEAN_UP) free(unc);
+ }
+
lsa = host2sockaddr(hostname, 0);
- hostname[len] = c;
+ share[-1] = c;
if (!lsa)
goto report_error;
parse_mount_options(ip, &filteropts);
if (ENABLE_FEATURE_CLEAN_UP) free(ip);
- // "-o mand" is required [why?]
- vfsflags |= MS_MANDLOCK;
mp->mnt_type = (char*)"cifs";
rc = mount_it_now(mp, vfsflags, filteropts);
}
// Might this be an NFS filesystem?
- if (ENABLE_FEATURE_MOUNT_NFS
- && (!mp->mnt_type || strcmp(mp->mnt_type, "nfs") == 0)
+ if ((!mp->mnt_type || strncmp(mp->mnt_type, "nfs", 3) == 0)
&& strchr(mp->mnt_fsname, ':') != NULL
) {
+ if (!mp->mnt_type)
+ mp->mnt_type = (char*)"nfs";
rc = nfsmount(mp, vfsflags, filteropts);
goto report_error;
}
if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
loopFile = bb_simplify_path(mp->mnt_fsname);
mp->mnt_fsname = NULL; // will receive malloced loop dev name
- if (set_loop(&mp->mnt_fsname, loopFile, 0) < 0) {
+ if (set_loop(&mp->mnt_fsname, loopFile, 0, /*ro:*/ (vfsflags & MS_RDONLY)) < 0) {
if (errno == EPERM || errno == EACCES)
bb_error_msg(bb_msg_perm_denied_are_you_root);
else
// If we know the fstype (or don't need to), jump straight
// to the actual mount.
if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
- rc = mount_it_now(mp, vfsflags, filteropts);
+ char *next;
+ for (;;) {
+ next = mp->mnt_type ? strchr(mp->mnt_type, ',') : NULL;
+ if (next)
+ *next = '\0';
+ rc = mount_it_now(mp, vfsflags, filteropts);
+ if (rc == 0 || !next)
+ break;
+ mp->mnt_type = next + 1;
+ }
} else {
// Loop through filesystem types until mount succeeds
// or we run out
for (fl = fslist; fl; fl = fl->link) {
mp->mnt_type = fl->data;
rc = mount_it_now(mp, vfsflags, filteropts);
- if (!rc)
+ if (rc == 0)
break;
}
}
FILE *fstab;
int i, j;
int rc = EXIT_SUCCESS;
+ unsigned long cmdopt_flags;
unsigned opt;
struct mntent mtpair[2], *mtcur = mtpair;
IF_NOT_DESKTOP(const int nonroot = 0;)
IF_DESKTOP(int nonroot = ) sanitize_env_if_suid();
+ INIT_G();
+
// Parse long options, like --bind and --move. Note that -o option
// and --option are synonymous. Yes, this means --remount,rw works.
for (i = j = 1; argv[i]; i++) {
// Past this point, we are handling either "mount -a [opts]"
// or "mount [opts] single_param"
- i = parse_mount_options(cmdopts, NULL); // FIXME: should be "long", not "int"
- if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
+ cmdopt_flags = parse_mount_options(cmdopts, NULL);
+ if (nonroot && (cmdopt_flags & ~MS_SILENT)) // Non-root users cannot specify flags
bb_error_msg_and_die(bb_msg_you_must_be_root);
// If we have a shared subtree flag, don't worry about fstab or mtab.
if (ENABLE_FEATURE_MOUNT_FLAGS
- && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
+ && (cmdopt_flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
) {
// verbose_mount(source, target, type, flags, data)
- rc = verbose_mount("", argv[0], "", i, "");
+ rc = verbose_mount("", argv[0], "", cmdopt_flags, "");
if (rc)
bb_simple_perror_msg_and_die(argv[0]);
return rc;
// Open either fstab or mtab
fstabname = "/etc/fstab";
- if (i & MS_REMOUNT) {
+ if (cmdopt_flags & MS_REMOUNT) {
// WARNING. I am not sure this matches util-linux's
// behavior. It's possible util-linux does not
// take -o opts from mtab (takes only mount source).
// End of fstab/mtab is reached.
// Were we looking for something specific?
if (argv[0]) { // yes
- long l;
+ unsigned long l;
// If we didn't find anything, complain
if (!mtcur->mnt_fsname)