]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
mount_utils: add mount_filesystem() helper
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 22 Jul 2020 09:45:15 +0000 (11:45 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 23 Jul 2020 08:20:50 +0000 (10:20 +0200)
that translates between the two mount apis.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/mount_utils.c
src/lxc/mount_utils.h

index f8e0f6e0069f4f38bdac4db16fcdf7fbabb907a7..f07cddd8a0c60b0c143e8883a66ea83a2f411276 100644 (file)
@@ -3,16 +3,22 @@
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE 1
 #endif
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
+#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);
+}
index 19d7b95cceb71fccbccb35d1ad456481e6e12a7e..bdcf8a3907b7fce38392f2a0ed6bf2325b07c735 100644 (file)
@@ -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 */