]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: use mount ID to merge utab and mountinfo files
authorKarel Zak <kzak@redhat.com>
Tue, 29 Nov 2022 09:13:39 +0000 (10:13 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 3 Jan 2023 12:14:28 +0000 (13:14 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/hook_mount.c
libmount/src/tab_parse.c
libmount/src/tab_update.c

index 299be87a882b98bdb1769e303a641db8db23601b..1fd31d94735401045c1c1681bf6850d739e9bcc5 100644 (file)
@@ -254,10 +254,16 @@ static int hook_create_mount(struct libmnt_context *cxt,
 
                rc = statx(api->fd_tree, "", AT_EMPTY_PATH, STATX_MNT_ID, &st);
                cxt->fs->id = (int) st.stx_mnt_id;
+
+               if (cxt->update) {
+                       struct libmnt_fs *fs = mnt_update_get_fs(cxt->update);
+                       if (fs)
+                               fs->id = cxt->fs->id;
+               }
        }
 
 done:
-       DBG(HOOK, ul_debugobj(hs, "create FS done [rc=%d]", rc));
+       DBG(HOOK, ul_debugobj(hs, "create FS done [rc=%d, id=%d]", rc, cxt->fs ? cxt->fs->id : -1));
        return rc;
 }
 
index 6e3d5ef9f731be012355ad0f045f869363fdbcac..526a0078bb676b17c1abd1f099c54deb38456a8f 100644 (file)
@@ -323,7 +323,14 @@ static int mnt_parse_utab_line(struct libmnt_fs *fs, const char *s)
                if (!*p)
                        break;
 
-               if (!fs->source && !strncmp(p, "SRC=", 4)) {
+               if (!fs->id && !strncmp(p, "ID=", 3)) {
+                       int rc = 0;
+
+                       end = next_s32(p + 3, &fs->id, &rc);
+                       if (!end || rc)
+                               return rc;
+
+               } else if (!fs->source && !strncmp(p, "SRC=", 4)) {
                        char *v = unmangle(p + 4, &end);
                        if (!v)
                                goto enomem;
@@ -1171,6 +1178,7 @@ static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct
        struct libmnt_fs *fs;
        struct libmnt_iter itr;
        const char *optstr, *src, *target, *root, *attrs;
+       int id;
 
        if (!tb || !uf)
                return NULL;
@@ -1182,6 +1190,7 @@ static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct
        optstr = mnt_fs_get_user_options(uf);
        attrs = mnt_fs_get_attributes(uf);
        root = mnt_fs_get_root(uf);
+       id = mnt_fs_get_id(uf);
 
        if (!src || !target || !root || (!attrs && !optstr))
                return NULL;
@@ -1194,20 +1203,23 @@ static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct
                if (fs->flags & MNT_FS_MERGED)
                        continue;
 
-               if (r && strcmp(r, root) == 0
+               if (id > 0 && mnt_fs_get_id(fs)) {
+                       DBG(TAB, ul_debugobj(tb, " using ID"));
+                       if (mnt_fs_get_id(fs) == id)
+                               break;
+               } else if (r && strcmp(r, root) == 0
                    && mnt_fs_streq_target(fs, target)
                    && mnt_fs_streq_srcpath(fs, src))
                        break;
        }
 
        if (fs) {
-               DBG(TAB, ul_debugobj(tb, "found fs -- appending user optstr"));
+               DBG(TAB, ul_debugobj(tb, " found"));
                mnt_fs_append_options(fs, optstr);
                mnt_fs_append_attributes(fs, attrs);
                mnt_fs_set_bindsrc(fs, mnt_fs_get_bindsrc(uf));
                fs->flags |= MNT_FS_MERGED;
 
-               DBG(TAB, ul_debugobj(tb, "found fs:"));
                DBG(TAB, mnt_fs_print_debug(fs, stderr));
        }
        return fs;
index f914df4122f7c88779a9cd481783c08edf102aed..f5e5d3045cf8c540c7bfb708e7dc849ba8c19dc1 100644 (file)
@@ -435,10 +435,15 @@ static int fprintf_utab_fs(FILE *f, struct libmnt_fs *fs)
        if (!fs || !f)
                return -EINVAL;
 
-       p = mangle(mnt_fs_get_source(fs));
-       if (p) {
-               rc = fprintf(f, "SRC=%s ", p);
-               free(p);
+       if (mnt_fs_get_id(fs) > 0) {
+               rc = fprintf(f, "ID=%d ", mnt_fs_get_id(fs));
+       }
+       if (rc >= 0) {
+               p = mangle(mnt_fs_get_source(fs));
+               if (p) {
+                       rc = fprintf(f, "SRC=%s ", p);
+                       free(p);
+               }
        }
        if (rc >= 0) {
                p = mangle(mnt_fs_get_target(fs));