commit dc7c3d658782f6c2a409c6e76e32db3afc86c4ed
Author: Karel Zak <kzak@redhat.com>
Date:   Tue Feb 16 15:44:31 2016 +0100

    libmount: fix mnt_table_is_fs_mounted() loopdev use
    
    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>

Index: util-linux-2.25/libmount/src/tab.c
===================================================================
--- util-linux-2.25.orig/libmount/src/tab.c
+++ util-linux-2.25/libmount/src/tab.c
@@ -1543,7 +1543,7 @@ int mnt_table_is_fs_mounted(struct libmn
 	assert(tb);
 	assert(fstab_fs);
 
-	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)));
 
@@ -1587,7 +1587,7 @@ int mnt_table_is_fs_mounted(struct libmn
 	}
 	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) {
 
@@ -1603,19 +1603,19 @@ int mnt_table_is_fs_mounted(struct libmn
 			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)));
 
@@ -1866,9 +1866,14 @@ static int test_is_mounted(struct libmnt
 	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;
@@ -1888,7 +1893,7 @@ static int test_is_mounted(struct libmnt
 	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),
@@ -1958,7 +1963,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: util-linux-2.25/libmount/src/tab_parse.c
===================================================================
--- util-linux-2.25.orig/libmount/src/tab_parse.c
+++ util-linux-2.25/libmount/src/tab_parse.c
@@ -821,6 +821,7 @@ struct libmnt_table *__mnt_new_table_fro
 		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: util-linux-2.25/libmount/src/utils.c
===================================================================
--- util-linux-2.25.orig/libmount/src/utils.c
+++ util-linux-2.25/libmount/src/utils.c
@@ -789,6 +789,7 @@ int mnt_has_regular_mtab(const char **mt
 		if (S_ISREG(st.st_mode)) {
 			if (writable)
 				*writable = !try_write(filename);
+			DBG(UTILS, ul_debug("%s: writable", filename));
 			return 1;
 		}
 		goto done;
@@ -797,8 +798,10 @@ int mnt_has_regular_mtab(const char **mt
 	/* 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:
