diff options
author | 2015-05-19 20:32:44 +0200 | |
---|---|---|
committer | 2015-05-20 14:38:12 +0200 | |
commit | 6458ec20b574edf7170fda61c51ccd3c6e73937f (patch) | |
tree | 0baed30f599b2e01b9e929ad483a23572c2e7187 | |
parent | TODO: add --merge support for sd-j-remote (diff) | |
download | systemd-6458ec20b574edf7170fda61c51ccd3c6e73937f.tar.gz systemd-6458ec20b574edf7170fda61c51ccd3c6e73937f.tar.bz2 systemd-6458ec20b574edf7170fda61c51ccd3c6e73937f.zip |
core,nspawn: unify code that moves the root dir
-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 5b782798c..c0c64fd70 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 a38f47dd0..1f919c082 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 e18645f8f..c3b08bbc4 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 0e806cf1a..f0382f0d6 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); |