From f7cf3431c7260635d9d2fa0886af05e56261c5df Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 19 May 2015 20:32:44 +0200 Subject: [PATCH] core,nspawn: unify code that moves the root dir --- src/shared/util.c | 18 ++++++++++++++++++ src/shared/util.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/shared/util.c b/src/shared/util.c index ceb88d54c..5efb9591a 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -6144,3 +6144,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 a6f536754..4cea62758 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -904,3 +904,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); -- 2.30.2