]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: fix mnt_table_is_fs_mounted() loopdev use
authorKarel Zak <kzak@redhat.com>
Tue, 16 Feb 2016 14:44:31 +0000 (15:44 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 16 Feb 2016 14:44:31 +0000 (15:44 +0100)
The function does not detect already mounted loop devices on systems
with regular /etc/mtab file.

The patch also improves test_is_mounted() to be useful with mtab.

Reported-by: Ruediger Meier <sweet_f_a@gmx.de>
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/tab.c
libmount/src/tab_parse.c
libmount/src/utils.c

index 729a99fc0833f19e96370edba40975b296b1b857..f9d8b064f89bd5ac33630b768ddd635e516ca6e2 100644 (file)
@@ -1485,7 +1485,7 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs)
        int rc = 0;
        dev_t devno = 0;
 
-       DBG(FS, ul_debugobj(fstab_fs, "is FS mounted? [target=%s, source=%s]",
+       DBG(FS, ul_debugobj(fstab_fs, "mnt_table_is_fs_mounted: target=%s, source=%s",
                                mnt_fs_get_target(fstab_fs),
                                mnt_fs_get_source(fstab_fs)));
 
@@ -1529,7 +1529,7 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs)
        }
        mnt_reset_iter(&itr, MNT_ITER_FORWARD);
 
-       DBG(FS, ul_debugobj(fstab_fs, "is mounted: src=%s, tgt=%s, root=%s", src, tgt, root));
+       DBG(FS, ul_debugobj(fstab_fs, "mnt_table_is_fs_mounted: src=%s, tgt=%s, root=%s", src, tgt, root));
 
        while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
 
@@ -1545,19 +1545,19 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs)
                        uint64_t offset = 0;
                        char *val;
                        size_t len;
-                       int flags;
+                       int flags = 0;
 
-                       if (!mnt_fs_is_kernel(fs) ||
-                           !mnt_fs_get_srcpath(fs) ||
+                       if (!mnt_fs_get_srcpath(fs) ||
                            !startswith(mnt_fs_get_srcpath(fs), "/dev/loop"))
                                continue;       /* does not look like loopdev */
 
-                       if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0 &&
-                           mnt_parse_offset(val, len, &offset)) {
-                               DBG(FS, ul_debugobj(fstab_fs, "failed to parse offset="));
-                               continue;
-                       } else
+                       if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0) {
+                               if (mnt_parse_offset(val, len, &offset)) {
+                                       DBG(FS, ul_debugobj(fstab_fs, "failed to parse offset="));
+                                       continue;
+                               }
                                flags = LOOPDEV_FL_OFFSET;
+                       }
 
                        DBG(FS, ul_debugobj(fs, "checking for loop: src=%s", mnt_fs_get_srcpath(fs)));
 #if __linux__
@@ -1808,9 +1808,14 @@ static int test_is_mounted(struct libmnt_test *ts, int argc, char *argv[])
        struct libmnt_fs *fs;
        struct libmnt_iter *itr = NULL;
        struct libmnt_cache *mpc = NULL;
-       int rc;
+       int rc, writable = 0;
+       const char *path = NULL;
+
+       if (mnt_has_regular_mtab(&path, &writable) == 1 && writable == 0)
+               tb = mnt_new_table_from_file(path);
+       else
+               tb = mnt_new_table_from_file("/proc/self/mountinfo");
 
-       tb = mnt_new_table_from_file("/proc/self/mountinfo");
        if (!tb) {
                fprintf(stderr, "failed to parse mountinfo\n");
                return -1;
@@ -1830,7 +1835,7 @@ static int test_is_mounted(struct libmnt_test *ts, int argc, char *argv[])
        mnt_table_set_cache(tb, mpc);
        mnt_unref_cache(mpc);
 
-       while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
+       while (mnt_table_next_fs(fstab, itr, &fs) == 0) {
                if (mnt_table_is_fs_mounted(tb, fs))
                        printf("%s already mounted on %s\n",
                                        mnt_fs_get_source(fs),
@@ -1900,7 +1905,7 @@ int main(int argc, char *argv[])
        { "--find-pair",     test_find_pair, "<file> <source> <target>" },
        { "--find-mountpoint", test_find_mountpoint, "<path>" },
        { "--copy-fs",       test_copy_fs, "<file>  copy root FS from the file" },
-       { "--is-mounted",    test_is_mounted, "<fstab> check what from <file> are already mounted" },
+       { "--is-mounted",    test_is_mounted, "<fstab> check what from fstab is already mounted" },
        { NULL }
        };
 
index 4c473905f2d72495a8388662eaefe004969a3ce6..c67479b3b9b3bf51a4d4e49306d2ce67bd5aeb17 100644 (file)
@@ -843,6 +843,7 @@ struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt)
                return NULL;
        tb = mnt_new_table();
        if (tb) {
+               DBG(TAB, ul_debugobj(tb, "new tab for file: %s", filename));
                tb->fmt = fmt;
                if (mnt_table_parse_file(tb, filename) != 0) {
                        mnt_unref_table(tb);
index 39f6c851e4c2925b27cc3324e6e44b468291fc84..50cca0671dca24c1dfa5e571fd20e8417282c4ef 100644 (file)
@@ -785,6 +785,7 @@ int mnt_has_regular_mtab(const char **mtab, int *writable)
                if (S_ISREG(st.st_mode)) {
                        if (writable)
                                *writable = !try_write(filename);
+                       DBG(UTILS, ul_debug("%s: writable", filename));
                        return 1;
                }
                goto done;
@@ -793,8 +794,10 @@ int mnt_has_regular_mtab(const char **mtab, int *writable)
        /* try to create the file */
        if (writable) {
                *writable = !try_write(filename);
-               if (*writable)
+               if (*writable) {
+                       DBG(UTILS, ul_debug("%s: writable", filename));
                        return 1;
+               }
        }
 
 done: