mount: support "-O option"; stop trying to mount swap partitions
authorDenis Vlasenko <vda.linux@googlemail.com>
Sat, 14 Mar 2009 22:57:20 +0000 (22:57 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sat, 14 Mar 2009 22:57:20 +0000 (22:57 -0000)
function                                             old     new   delta
mount_main                                           975    1152    +177
umount_main                                          640     636      -4
packed_usage                                       25666   25662      -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 177/-8)            Total: 169 bytes

include/usage.h
libbb/match_fstype.c
util-linux/mount.c
util-linux/umount.c

index e945840..25a7162 100644 (file)
        "$ dmesg | more\n"
 
 #define mount_trivial_usage \
-       "[flags] DEVICE NODE [-o options,more-options]"
+       "[flags] DEVICE NODE [-o OPT,OPT]"
 #define mount_full_usage "\n\n" \
        "Mount a filesystem. Filesystem autodetection requires /proc be mounted.\n" \
      "\nOptions:" \
      "\n       -a              Mount all filesystems in fstab" \
        USE_FEATURE_MOUNT_FAKE( \
-     "\n       -f              "USE_FEATURE_MTAB_SUPPORT("Update /etc/mtab, but ")"don't mount" \
+       USE_FEATURE_MTAB_SUPPORT( \
+     "\n       -f              Update /etc/mtab, but don't mount" \
+       ) \
+       SKIP_FEATURE_MTAB_SUPPORT( \
+     "\n       -f              Dry run" \
+       ) \
        ) \
        USE_FEATURE_MTAB_SUPPORT( \
      "\n       -n              Don't update /etc/mtab" \
        ) \
      "\n       -r              Read-only mount" \
-     "\n       -t fs-type      Filesystem type" \
      "\n       -w              Read-write mount (default)" \
-       "\n" \
-       "-o option:\n" \
+     "\n       -t FSTYPE       Filesystem type" \
+     "\n       -O OPT          Mount only filesystems with option OPT (-a only)" \
+     "\n-o OPT:" \
        USE_FEATURE_MOUNT_LOOP( \
-       "       loop            Ignored (loop devices are autodetected)\n" \
+     "\n       loop            Ignored (loop devices are autodetected)" \
        ) \
        USE_FEATURE_MOUNT_FLAGS( \
-       "       [a]sync         Writes are asynchronous / synchronous\n" \
-       "       [no]atime       Disable / enable updates to inode access times\n" \
-       "       [no]diratime    Disable / enable atime updates to directories\n" \
-       "       [no]relatime    Disable / enable atime updates relative to modification time\n" \
-       "       [no]dev         Allow use of special device files / disallow them\n" \
-       "       [no]exec        Allow use of executable files / disallow them\n" \
-       "       [no]suid        Allow set-user-id-root programs / disallow them\n" \
-       "       [r]shared       Convert [recursively] to a shared subtree\n" \
-       "       [r]slave        Convert [recursively] to a slave subtree\n" \
-       "       [r]private      Convert [recursively] to a private subtree\n" \
-       "       [un]bindable    Make mount point [un]able to be bind mounted\n" \
-       "       bind            Bind a directory to an additional location\n" \
-       "       move            Relocate an existing mount point\n" \
+     "\n       [a]sync         Writes are [a]synchronous" \
+     "\n       [no]atime       Disable/enable updates to inode access times" \
+     "\n       [no]diratime    Disable/enable atime updates to directories" \
+     "\n       [no]relatime    Disable/enable atime updates relative to modification time" \
+     "\n       [no]dev         (Dis)allow use of special device files" \
+     "\n       [no]exec        (Dis)allow use of executable files" \
+     "\n       [no]suid        (Dis)allow set-user-id-root programs" \
+     "\n       [r]shared       Convert [recursively] to a shared subtree" \
+     "\n       [r]slave        Convert [recursively] to a slave subtree" \
+     "\n       [r]private      Convert [recursively] to a private subtree" \
+     "\n       [un]bindable    Make mount point [un]able to be bind mounted" \
+     "\n       bind            Bind a directory to an additional location" \
+     "\n       move            Relocate an existing mount point" \
        ) \
-       "       remount         Remount a mounted filesystem, changing its flags\n" \
-       "       ro/rw           Mount for read-only / read-write\n" \
-       "\n" \
-       "There are EVEN MORE flags that are specific to each filesystem\n" \
-       "You'll have to see the written documentation for those filesystems" \
+     "\n       remount         Remount a mounted filesystem, changing its flags" \
+     "\n       ro/rw           Read-only/read-write mount" \
+     "\n" \
+     "\nThere are EVEN MORE flags that are specific to each filesystem" \
+     "\nYou'll have to see the written documentation for those filesystems" \
 
 #define mount_example_usage \
        "$ mount\n" \
index 99e2767..9360e75 100644 (file)
@@ -5,40 +5,38 @@
  * This allows us to match fstypes that start with no like so
  *   mount -at ,noddy
  *
- * Returns 0 for a match, otherwise -1
+ * Returns 1 for a match, otherwise 0
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include "libbb.h"
 
-int FAST_FUNC match_fstype(const struct mntent *mt, const char *fstype)
+int FAST_FUNC match_fstype(const struct mntent *mt, const char *t_fstype)
 {
-       int no = 0;
+       int match = 1;
        int len;
 
-       if (!mt)
-               return -1;
+       if (!t_fstype)
+               return match;
 
-       if (!fstype)
-               return 0;
-
-       if (fstype[0] == 'n' && fstype[1] == 'o') {
-               no = -1;
-               fstype += 2;
+       if (t_fstype[0] == 'n' && t_fstype[1] == 'o') {
+               match--;
+               t_fstype += 2;
        }
 
        len = strlen(mt->mnt_type);
-       while (fstype) {
-               if (!strncmp(mt->mnt_type, fstype, len)
-                && (!fstype[len] || fstype[len] == ',')
+       while (1) {
+               if (strncmp(mt->mnt_type, t_fstype, len) == 0
+                && (t_fstype[len] == '\0' || t_fstype[len] == ',')
                ) {
-                       return no;
+                       return match;
                }
-               fstype = strchr(fstype, ',');
-               if (fstype)
-                       fstype++;
+               t_fstype = strchr(t_fstype, ',');
+               if (!t_fstype)
+                       break;
+               t_fstype++;
        }
 
-       return -(no + 1);
+       return !match;
 }
index 3b83113..d647c71 100644 (file)
@@ -63,7 +63,7 @@ enum {
 };
 
 
-#define OPTION_STR "o:t:rwanfvsi"
+#define OPTION_STR "o:t:rwanfvsiO:"
 enum {
        OPT_o = (1 << 0),
        OPT_t = (1 << 1),
@@ -75,6 +75,7 @@ enum {
        OPT_v = (1 << 7),
        OPT_s = (1 << 8),
        OPT_i = (1 << 9),
+       OPT_O = (1 << 10),
 };
 
 #if ENABLE_FEATURE_MTAB_SUPPORT
@@ -1721,6 +1722,45 @@ static int singlemount(struct mntent *mp, int ignore_busy)
        return rc;
 }
 
+/* -O support
+ * Unlike -t, -O should interpret "no" prefix differently:
+ * -t noa,b,c = -t no(a,b,c) = mount all except fs'es with types a,b, and c
+ * -O noa,b,c = -O noa,b,c = mount all with without option a,
+ * or with option b or c.
+ * But for now we do not support -O a,b,c at all (only -O a).
+ *
+ * Another difference from -t support (match_fstype) is that
+ * we need to examine the _list_ of options in fsopt, not just a string.
+ */
+static int match_opt(const char *fs_opt, const char *O_opt)
+{
+       int match = 1;
+       int len;
+
+       if (!O_opt)
+               return match;
+
+       if (O_opt[0] == 'n' && O_opt[1] == 'o') {
+               match--;
+               O_opt += 2;
+       }
+
+       len = strlen(O_opt);
+       while (1) {
+               if (strncmp(fs_opt, O_opt, len) == 0
+                && (fs_opt[len] == '\0' || fs_opt[len] == ',')
+               ) {
+                       return match;
+               }
+               fs_opt = strchr(fs_opt, ',');
+               if (!fs_opt)
+                       break;
+               fs_opt++;
+       }
+
+       return !match;
+}
+
 // Parse options, if necessary parse fstab/mtab, and call singlemount for
 // each directory to be mounted.
 static const char must_be_root[] ALIGN1 = "you must be root";
@@ -1728,8 +1768,9 @@ static const char must_be_root[] ALIGN1 = "you must be root";
 int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int mount_main(int argc UNUSED_PARAM, char **argv)
 {
-       char *cmdopts = xstrdup("");
+       char *cmdopts = xzalloc(1);
        char *fstype = NULL;
+       char *O_optmatch = NULL;
        char *storage_path;
        llist_t *lst_o = NULL;
        const char *fstabname;
@@ -1752,9 +1793,9 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
        argv[j] = NULL;
 
        // Parse remaining options
-       // Max 2 params; -v is a counter
-       opt_complementary = "?2o::" USE_FEATURE_MOUNT_VERBOSE(":vv");
-       opt = getopt32(argv, OPTION_STR, &lst_o, &fstype
+       // Max 2 params; -o is a list, -v is a counter
+       opt_complementary = "?2o::" USE_FEATURE_MOUNT_VERBOSE("vv");
+       opt = getopt32(argv, OPTION_STR, &lst_o, &fstype, &O_optmatch
                        USE_FEATURE_MOUNT_VERBOSE(, &verbose));
        while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
        if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
@@ -1807,7 +1848,7 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
        // Past this point, we are handling either "mount -a [opts]"
        // or "mount [opts] single_param"
 
-       i = parse_mount_options(cmdopts, 0); // FIXME: should be "long", not "int"
+       i = parse_mount_options(cmdopts, NULL); // FIXME: should be "long", not "int"
        if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
                bb_error_msg_and_die(must_be_root);
 
@@ -1865,20 +1906,29 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
 
                // If we're mounting all
                } else {
-                       // Do we need to match a filesystem type?
-                       if (fstype && match_fstype(mtcur, fstype))
+                       // No, mount -a won't mount anything,
+                       // even user mounts, for mere humans
+                       if (nonroot)
+                               bb_error_msg_and_die(must_be_root);
+
+                       // Does type match? (NULL matches always)
+                       if (!match_fstype(mtcur, fstype))
                                continue;
 
                        // Skip noauto and swap anyway.
-                       if (parse_mount_options(mtcur->mnt_opts, 0) & (MOUNT_NOAUTO | MOUNT_SWAP))
+                       if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
+                       // swap is bogus "fstype", parse_mount_options can't check fstypes
+                        || strcasecmp(mtcur->mnt_type, "swap") == 0
+                       ) {
                                continue;
+                       }
 
-                       // No, mount -a won't mount anything,
-                       // even user mounts, for mere humans
-                       if (nonroot)
-                               bb_error_msg_and_die(must_be_root);
+                       // Does (at least one) option match?
+                       // (NULL matches always)
+                       if (!match_opt(mtcur->mnt_opts, O_optmatch))
+                               continue;
 
-                       resolve_mount_spec(&mtpair->mnt_fsname);
+                       resolve_mount_spec(&mtcur->mnt_fsname);
 
                        // NFS mounts want this to be xrealloc-able
                        mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
@@ -1895,13 +1945,35 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
        // End of fstab/mtab is reached.
        // Were we looking for something specific?
        if (argv[0]) {
+               long l;
+
                // If we didn't find anything, complain
                if (!mtcur->mnt_fsname)
                        bb_error_msg_and_die("can't find %s in %s",
                                argv[0], fstabname);
+
+               // What happens when we try to "mount swap_partition"?
+               // (fstab containts "swap_partition swap swap defaults 0 0")
+               // util-linux-ng 2.13.1 does this:
+               // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
+               // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
+               // lstat("swap", 0x7fff62a3a640)           = -1 ENOENT (No such file or directory)
+               // write(2, "mount: mount point swap does not exist\n", 39) = 39
+               // exit_group(32)                          = ?
+#if 0
+               // In case we want to simply skip swap partitions:
+               l = parse_mount_options(mtcur->mnt_opts, NULL);
+               if ((l & MOUNT_SWAP)
+               // swap is bogus "fstype", parse_mount_options can't check fstypes
+                || strcasecmp(mtcur->mnt_type, "swap") == 0
+               ) {
+                       goto ret;
+               }
+#endif
                if (nonroot) {
                        // fstab must have "users" or "user"
-                       if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
+                       l = parse_mount_options(mtcur->mnt_opts, NULL);
+                       if (!(l & MOUNT_USERS))
                                bb_error_msg_and_die(must_be_root);
                }
 
@@ -1914,6 +1986,7 @@ int mount_main(int argc UNUSED_PARAM, char **argv)
                        free(mtcur->mnt_opts);
        }
 
+ //ret:
        if (ENABLE_FEATURE_CLEAN_UP)
                endmntent(fstab);
        if (ENABLE_FEATURE_CLEAN_UP) {
index 901c909..5b22bfa 100644 (file)
@@ -73,9 +73,9 @@ int umount_main(int argc UNUSED_PARAM, char **argv)
        } else {
                while (getmntent_r(fp, &me, path, PATH_MAX)) {
                        /* Match fstype if passed */
-                       if (fstype && match_fstype(&me, fstype))
+                       if (!match_fstype(&me, fstype))
                                continue;
-                       m = xmalloc(sizeof(struct mtab_list));
+                       m = xzalloc(sizeof(*m));
                        m->next = mtl;
                        m->device = xstrdup(me.mnt_fsname);
                        m->dir = xstrdup(me.mnt_dir);