From 6458ec20b574edf7170fda61c51ccd3c6e73937f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 19 May 2015 20:32:44 +0200 Subject: core,nspawn: unify code that moves the root dir --- src/shared/util.c | 18 ++++++++++++++++++ src/shared/util.h | 2 ++ 2 files changed, 20 insertions(+) (limited to 'src/shared') 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); -- cgit v1.2.3-54-g00ecf