Skip to content

Commit

Permalink
libmount: use mount ID to merge utab and mountinfo files
Browse files Browse the repository at this point in the history
Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Jan 3, 2023
1 parent f25e8d2 commit 8cf6c50
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
8 changes: 7 additions & 1 deletion libmount/src/hook_mount.c
Expand Up @@ -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;
}

Expand Down
20 changes: 16 additions & 4 deletions libmount/src/tab_parse.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions libmount/src/tab_update.c
Expand Up @@ -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));
Expand Down

0 comments on commit 8cf6c50

Please sign in to comment.