mount: optional support for -vv verbosity
authorDenis Vlasenko <vda.linux@googlemail.com>
Mon, 18 Feb 2008 12:07:49 +0000 (12:07 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Mon, 18 Feb 2008 12:07:49 +0000 (12:07 -0000)
mount: do "struct globals" trick

With -vv on:

function                                             old     new   delta
verbose_mount                                          -      83     +83
mount_main                                           970     988     +18
mount_it_now                                         219     229     +10
singlemount                                         4564    4570      +6
mount_option_str                                     227     233      +6
nfs_mount_version                                      1       -      -1
fslist                                                 4       -      -4
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 4/0 up/down: 123/-5)            Total: 118 bytes

util-linux/Config.in
util-linux/mount.c

index 5a47318fefb3c6bb597760095db508d11f1bd956..3b0f778cbdba21e9bca25c989d74ed4152d48017 100644 (file)
@@ -393,6 +393,15 @@ config FEATURE_MOUNT_FAKE
        help
          Enable support for faking a file system mount.
 
+config FEATURE_MOUNT_VERBOSE
+       bool "mount -v option"
+       default n
+       depends on MOUNT
+       help
+         Enable multi-level -v[vv...] verbose messages. Useful if you
+         debug mount problems and want to see what is exactly passed
+         to the kernel.
+
 config FEATURE_MOUNT_HELPERS
        bool "Support mount helpers"
        default n
index 054db57b34da21c7d587e575c260ace10f9a9f3a..cd8fef9ee3e3a9ba6db451199b8199b06fa7b6ba 100644 (file)
@@ -50,8 +50,6 @@ static struct mntent *getmntent_r(FILE* stream, struct mntent* result, char* buf
 }
 #endif
 
-#define getmntent_buf bb_common_bufsiz1
-
 
 // Not real flags, but we want to be able to check for this.
 enum {
@@ -204,6 +202,44 @@ static const char mount_option_str[] =
        "remount" "\0"   // action flag
 ;
 
+
+struct globals {
+#if ENABLE_FEATURE_MOUNT_NFS
+       smalluint nfs_mount_version;
+#endif
+#if ENABLE_FEATURE_MOUNT_VERBOSE
+       unsigned verbose;
+#endif
+       llist_t *fslist;
+       char getmntent_buf[sizeof(bb_common_bufsiz1) - 8*3];
+
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define nfs_mount_version (G.nfs_mount_version)
+#define verbose           (G.verbose          )
+#define fslist            (G.fslist           )
+#define getmntent_buf     (G.getmntent_buf    )
+
+
+#if ENABLE_FEATURE_MOUNT_VERBOSE
+static int verbose_mount(const char *source, const char *target,
+               const char *filesystemtype,
+               unsigned long mountflags, const void *data)
+{
+       int rc;
+
+       errno = 0;
+       rc = mount(source, target, filesystemtype, mountflags, data);
+       if (verbose >= 2)
+               bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
+                               source, target, filesystemtype,
+                               mountflags, (char*)data, rc);
+       return rc;
+}
+#else
+#define verbose_mount(...) mount(__VA_ARGS__)
+#endif
+
 /* Append mount options to string */
 static void append_mount_options(char **oldopts, const char *newopts)
 {
@@ -313,8 +349,6 @@ static llist_t *get_block_backed_filesystems(void)
        return list;
 }
 
-static llist_t *fslist;
-
 #if ENABLE_FEATURE_CLEAN_UP
 static void delete_block_backed_filesystems(void)
 {
@@ -333,9 +367,9 @@ static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
        if (fakeIt) goto mtab;
 
        // Mount, with fallback to read-only if necessary.
-
        for (;;) {
-               rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
+               errno = 0;
+               rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
                                vfsflags, filteropts);
 
                // If mount failed, try
@@ -738,8 +772,6 @@ static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
 
 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
 
-static smalluint nfs_mount_version;
-
 /*
  * Unfortunately, the kernel prints annoying console messages
  * in case of an unexpected nfs mount version (instead of
@@ -1674,8 +1706,8 @@ int mount_main(int argc, char **argv)
 
        sanitize_env_if_suid();
 
-       /* parse long options, like --bind and --move.  Note that -o option
-        * and --option are synonymous.  Yes, this means --remount,rw works. */
+       // Parse long options, like --bind and --move.  Note that -o option
+       // and --option are synonymous.  Yes, this means --remount,rw works.
 
        for (i = j = 0; i < argc; i++) {
                if (argv[i][0] == '-' && argv[i][1] == '-') {
@@ -1687,7 +1719,11 @@ int mount_main(int argc, char **argv)
 
        // Parse remaining options
 
-       opt = getopt32(argv, OPTION_STR, &opt_o, &fstype);
+#if ENABLE_FEATURE_MOUNT_VERBOSE
+       opt_complementary = "vv"; // -v is a counter
+#endif
+       opt = getopt32(argv, OPTION_STR, &opt_o, &fstype
+                       USE_FEATURE_MOUNT_VERBOSE(, &verbose));
        if (opt & OPT_o) append_mount_options(&cmdopts, opt_o); // -o
        if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
        if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w
@@ -1747,7 +1783,7 @@ int mount_main(int argc, char **argv)
        if (ENABLE_FEATURE_MOUNT_FLAGS
         && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
        ) {
-               rc = mount("", argv[0], "", i, "");
+               rc = verbose_mount("", argv[0], "", i, "");
                if (rc) bb_simple_perror_msg_and_die(argv[0]);
                goto clean_up;
        }