From: Christian Brauner Date: Wed, 22 Jul 2020 09:45:15 +0000 (+0200) Subject: mount_utils: add mount_filesystem() helper X-Git-Tag: lxc-5.0.0~383^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14df702190f8bb7693c65a36392e09a1dd66e96b;p=thirdparty%2Flxc.git mount_utils: add mount_filesystem() helper that translates between the two mount apis. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/mount_utils.c b/src/lxc/mount_utils.c index f8e0f6e00..f07cddd8a 100644 --- a/src/lxc/mount_utils.c +++ b/src/lxc/mount_utils.c @@ -3,16 +3,22 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE 1 #endif +#include #include #include #include +#include +#include +#include "log.h" #include "macro.h" #include "memory_utils.h" #include "mount_utils.h" #include "syscall_numbers.h" #include "syscall_wrappers.h" +lxc_log_define(mount_utils, lxc); + int mnt_attributes_new(unsigned int old_flags, unsigned int *new_flags) { unsigned int flags = 0; @@ -108,3 +114,27 @@ int mnt_attributes_old(unsigned int new_flags, unsigned int *old_flags) *old_flags |= flags; return new_flags; } + +int mount_filesystem(const char *fs_name, const char *path, unsigned int attr_flags) +{ + __do_close int fsfd = -EBADF; + unsigned int old_flags = 0; + + fsfd = fsopen(fs_name, FSOPEN_CLOEXEC); + if (fsfd >= 0) { + __do_close int mfd = -EBADF; + + if (fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)) + return -1; + + mfd = fsmount(fsfd, FSMOUNT_CLOEXEC, attr_flags); + if (mfd < 0) + return -1; + + return move_mount(mfd, "", AT_FDCWD, path, MOVE_MOUNT_F_EMPTY_PATH); + } + + TRACE("Falling back to old mount api"); + mnt_attributes_old(attr_flags, &old_flags); + return mount("none", path, fs_name, old_flags, NULL); +} diff --git a/src/lxc/mount_utils.h b/src/lxc/mount_utils.h index 19d7b95cc..bdcf8a390 100644 --- a/src/lxc/mount_utils.h +++ b/src/lxc/mount_utils.h @@ -148,4 +148,6 @@ __hidden extern int mnt_attributes_new(unsigned int old_flags, unsigned int *new __hidden extern int mnt_attributes_old(unsigned int new_flags, unsigned int *old_flags); +__hidden extern int mount_filesystem(const char *fs_name, const char *path, unsigned int attr_flags); + #endif /* __LXC_MOUNT_UTILS_H */