diff options
-rw-r--r-- | src/core/namespace.c | 16 | ||||
-rw-r--r-- | src/nspawn/nspawn.c | 20 | ||||
-rw-r--r-- | src/shared/util.c | 18 | ||||
-rw-r--r-- | src/shared/util.h | 2 |
4 files changed, 23 insertions, 33 deletions
diff --git a/src/core/namespace.c b/src/core/namespace.c index 5b782798c8..c0c64fd706 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -125,22 +125,6 @@ static void drop_duplicates(BindMount *m, unsigned *n) { *n = t - m; } -static int mount_move_root(const char *path) { - if (chdir(path) < 0) - return -errno; - - if (mount(path, "/", NULL, MS_MOVE, NULL) < 0) - return -errno; - - if (chroot(".") < 0) - return -errno; - - if (chdir("/") < 0) - return -errno; - - return 0; -} - static int mount_dev(BindMount *m) { static const char devnodes[] = "/dev/null\0" diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index a38f47dd0a..1f919c082b 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -4391,23 +4391,9 @@ int main(int argc, char *argv[]) { if (mount_cgroup(arg_directory) < 0) _exit(EXIT_FAILURE); - if (chdir(arg_directory) < 0) { - log_error_errno(errno, "chdir(%s) failed: %m", arg_directory); - _exit(EXIT_FAILURE); - } - - if (mount(arg_directory, "/", NULL, MS_MOVE, NULL) < 0) { - log_error_errno(errno, "mount(MS_MOVE) failed: %m"); - _exit(EXIT_FAILURE); - } - - if (chroot(".") < 0) { - log_error_errno(errno, "chroot() failed: %m"); - _exit(EXIT_FAILURE); - } - - if (chdir("/") < 0) { - log_error_errno(errno, "chdir() failed: %m"); + r = mount_move_root(arg_directory); + if (r < 0) { + log_error_errno(r, "Failed to move root directory: %m"); _exit(EXIT_FAILURE); } diff --git a/src/shared/util.c b/src/shared/util.c index e18645f8f1..c3b08bbc43 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -6229,3 +6229,21 @@ int parse_mode(const char *s, mode_t *ret) { *ret = (mode_t) l; return 0; } + +int mount_move_root(const char *path) { + assert(path); + + if (chdir(path) < 0) + return -errno; + + if (mount(path, "/", NULL, MS_MOVE, NULL) < 0) + return -errno; + + if (chroot(".") < 0) + return -errno; + + if (chdir("/") < 0) + return -errno; + + return 0; +} diff --git a/src/shared/util.h b/src/shared/util.h index 0e806cf1a1..f0382f0d68 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -906,3 +906,5 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char char *shell_maybe_quote(const char *s); int parse_mode(const char *s, mode_t *ret); + +int mount_move_root(const char *path); |