mount-util: make mount_switch_root() take a mount propagation flag

This commit is contained in:
Yu Watanabe
2022-12-15 14:17:22 +09:00
parent edac5c4636
commit 9d50f8508b
4 changed files with 12 additions and 47 deletions
+2 -2
View File
@@ -2488,7 +2488,7 @@ int setup_namespace(
goto finish;
/* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */
r = mount_switch_root(root, MOUNT_ATTR_PROPAGATION_INHERIT);
r = mount_switch_root(root, /* mount_propagation_flag = */ 0);
if (r == -EINVAL && root_directory) {
/* If we are using root_directory and we don't have privileges (ie: user manager in a user
* namespace) and the root_directory is already a mount point in the parent namespace,
@@ -2498,7 +2498,7 @@ int setup_namespace(
r = mount_nofollow_verbose(LOG_DEBUG, root, root, NULL, MS_BIND|MS_REC, NULL);
if (r < 0)
goto finish;
r = mount_switch_root(root, MOUNT_ATTR_PROPAGATION_INHERIT);
r = mount_switch_root(root, /* mount_propagation_flag = */ 0);
}
if (r < 0) {
log_debug_errno(r, "Failed to mount root with MS_MOVE: %m");
+1 -1
View File
@@ -3973,7 +3973,7 @@ static int outer_child(
* directory mount to root later on.
* https://github.com/systemd/systemd/issues/3847#issuecomment-562735251
*/
r = mount_switch_root(directory, MOUNT_ATTR_PROPAGATION_SHARED);
r = mount_switch_root(directory, MS_SHARED);
if (r < 0)
return log_error_errno(r, "Failed to move root directory: %m");
+8 -30
View File
@@ -430,30 +430,6 @@ int bind_remount_one_with_mountinfo(
return 0;
}
static const char *const mount_attr_propagation_type_table[_MOUNT_ATTR_PROPAGATION_TYPE_MAX] = {
[MOUNT_ATTR_PROPAGATION_INHERIT] = "inherited",
[MOUNT_ATTR_PROPAGATION_PRIVATE] = "private",
[MOUNT_ATTR_PROPAGATION_DEPENDENT] = "dependent",
[MOUNT_ATTR_PROPAGATION_SHARED] = "shared",
};
DEFINE_STRING_TABLE_LOOKUP(mount_attr_propagation_type, MountAttrPropagationType);
static unsigned long mount_attr_propagation_type_to_flag(MountAttrPropagationType t) {
switch (t) {
case MOUNT_ATTR_PROPAGATION_INHERIT:
return 0;
case MOUNT_ATTR_PROPAGATION_PRIVATE:
return MS_PRIVATE;
case MOUNT_ATTR_PROPAGATION_DEPENDENT:
return MS_SLAVE;
case MOUNT_ATTR_PROPAGATION_SHARED:
return MS_SHARED;
default:
assert_not_reached();
}
}
static int mount_switch_root_pivot(const char *path, int fd_newroot) {
_cleanup_close_ int fd_oldroot = -EBADF;
@@ -497,12 +473,12 @@ static int mount_switch_root_move(const char *path) {
return 0;
}
int mount_switch_root(const char *path, MountAttrPropagationType type) {
int mount_switch_root(const char *path, unsigned long mount_propagation_flag) {
_cleanup_close_ int fd_newroot = -EBADF;
unsigned long flags;
int r;
assert(path);
assert(mount_propagation_flag_is_valid(mount_propagation_flag));
fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
if (fd_newroot < 0)
@@ -522,11 +498,13 @@ int mount_switch_root(const char *path, MountAttrPropagationType type) {
if (r < 0)
return log_debug_errno(r, "Failed to switch to new rootfs '%s': %m", path);
/* Finally, let's establish the requested propagation type. */
flags = mount_attr_propagation_type_to_flag(type);
if ((flags != 0) && mount(NULL, ".", NULL, flags|MS_REC, 0) < 0)
/* Finally, let's establish the requested propagation flags. */
if (mount_propagation_flag == 0)
return 0;
if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, 0) < 0)
return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m",
mount_attr_propagation_type_to_string(type), path);
mount_propagation_flag_to_string(mount_propagation_flag), path);
return 0;
}
+1 -14
View File
@@ -11,19 +11,6 @@
#include "errno-util.h"
#include "macro.h"
typedef enum MountAttrPropagationType {
MOUNT_ATTR_PROPAGATION_INHERIT, /* no special MS_* propagation flags */
MOUNT_ATTR_PROPAGATION_PRIVATE, /* MS_PRIVATE */
MOUNT_ATTR_PROPAGATION_DEPENDENT, /* MS_SLAVE */
MOUNT_ATTR_PROPAGATION_SHARED, /* MS_SHARE */
_MOUNT_ATTR_PROPAGATION_TYPE_MAX,
_MOUNT_ATTR_PROPAGATION_TYPE_INVALID = -EINVAL,
} MountAttrPropagationType;
const char* mount_attr_propagation_type_to_string(MountAttrPropagationType t) _const_;
MountAttrPropagationType mount_attr_propagation_type_from_string(const char *s) _pure_;
int repeat_unmount(const char *path, int flags);
int umount_recursive(const char *target, int flags);
@@ -34,7 +21,7 @@ static inline int bind_remount_recursive(const char *prefix, unsigned long new_f
int bind_remount_one_with_mountinfo(const char *path, unsigned long new_flags, unsigned long flags_mask, FILE *proc_self_mountinfo);
int mount_switch_root(const char *path, MountAttrPropagationType type);
int mount_switch_root(const char *path, unsigned long mount_propagation_flag);
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, endmntent, NULL);
#define _cleanup_endmntent_ _cleanup_(endmntentp)