core,nspawn: unify code that moves the root dir

This commit is contained in:
Lennart Poettering 2015-05-19 20:32:44 +02:00
parent eba6fd30f2
commit 6458ec20b5
4 changed files with 23 additions and 33 deletions

View file

@ -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"

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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);