mount_utils: add support for bind-mounts through the new mount api

fd_bind_mount() Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent ca9055b4
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#define _GNU_SOURCE 1 #define _GNU_SOURCE 1
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/mount.h> #include <sys/mount.h>
...@@ -302,3 +303,57 @@ int mount_from_at(int dfd_from, const char *path_from, ...@@ -302,3 +303,57 @@ int mount_from_at(int dfd_from, const char *path_from,
return ret; return ret;
} }
int fd_bind_mount(int dfd_from, const char *path_from,
__u64 o_flags_from, __u64 resolve_flags_from,
int dfd_to, const char *path_to,
__u64 o_flags_to, __u64 resolve_flags_to,
unsigned int attr_flags, bool recursive)
{
__do_close int __fd_from = -EBADF, __fd_to = -EBADF;
__do_close int fd_tree_from = -EBADF;
unsigned int open_tree_flags = AT_EMPTY_PATH | OPEN_TREE_CLONE | OPEN_TREE_CLONE;
int fd_from, fd_to, ret;
if (!is_empty_string(path_from)) {
struct lxc_open_how how = {
.flags = o_flags_from,
.resolve = resolve_flags_from,
};
__fd_from = openat2(dfd_from, path_from, &how, sizeof(how));
if (__fd_from < 0)
return -errno;
fd_from = __fd_from;
} else {
fd_from = dfd_from;
}
if (recursive)
open_tree_flags |= AT_RECURSIVE;
fd_tree_from = open_tree(fd_from, "", open_tree_flags);
if (fd_tree_from < 0)
return log_error_errno(-errno, errno, "Failed to create detached mount");
if (!is_empty_string(path_to)) {
struct lxc_open_how how = {
.flags = o_flags_to,
.resolve = resolve_flags_to,
};
__fd_to = openat2(dfd_to, path_to, &how, sizeof(how));
if (__fd_to < 0)
return -errno;
fd_to = __fd_to;
} else {
fd_to = dfd_to;
}
ret = move_mount(fd_tree_from, "", fd_to, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH);
if (ret)
return log_error_errno(-errno, errno, "Failed to attach detached mount %d to filesystem at %d", fd_tree_from, fd_to);
TRACE("Attach detached mount %d to filesystem at %d", fd_tree_from, fd_to);
return 0;
}
...@@ -12,6 +12,11 @@ ...@@ -12,6 +12,11 @@
#include "memory_utils.h" #include "memory_utils.h"
/* open_tree() flags */ /* open_tree() flags */
#ifndef AT_RECURSIVE
#define AT_RECURSIVE 0x8000 /* Apply to the entire subtree */
#endif
#ifndef OPEN_TREE_CLONE #ifndef OPEN_TREE_CLONE
#define OPEN_TREE_CLONE 1 #define OPEN_TREE_CLONE 1
#endif #endif
...@@ -181,4 +186,10 @@ static inline int fs_mount(const char *fs_name, int dfd_from, ...@@ -181,4 +186,10 @@ static inline int fs_mount(const char *fs_name, int dfd_from,
return fs_attach(fd_fs, dfd_to, path_to, o_flags_to, resolve_flags_to, attr_flags); return fs_attach(fd_fs, dfd_to, path_to, o_flags_to, resolve_flags_to, attr_flags);
} }
__hidden extern int fd_bind_mount(int dfd_from, const char *path_from,
__u64 o_flags_from, __u64 resolve_flags_from,
int dfd_to, const char *path_to,
__u64 o_flags_to, __u64 resolve_flags_to,
unsigned int attr_flags, bool recursive);
#endif /* __LXC_MOUNT_UTILS_H */ #endif /* __LXC_MOUNT_UTILS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment