]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/switch-root.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / shared / switch-root.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
41669317 2/***
96b2fb93 3 Copyright © 2012 Harald Hoyer
41669317
LP
4***/
5
41669317 6#include <errno.h>
07630cea 7#include <fcntl.h>
a8fbdf54 8#include <limits.h>
07630cea 9#include <stdbool.h>
a8fbdf54 10#include <stdio.h>
41669317 11#include <sys/mount.h>
07630cea 12#include <sys/stat.h>
41669317 13#include <unistd.h>
41669317 14
971ff8c7 15#include "base-filesystem.h"
3ffd4af2 16#include "fd-util.h"
e5b42203 17#include "fs-util.h"
a8fbdf54 18#include "log.h"
891a4918 19#include "missing.h"
07630cea 20#include "mkdir.h"
e5b42203 21#include "mount-util.h"
07630cea
LP
22#include "path-util.h"
23#include "rm-rf.h"
d054f0a4 24#include "stdio-util.h"
07630cea 25#include "string-util.h"
e5b42203 26#include "strv.h"
c6878637 27#include "switch-root.h"
ee104e11 28#include "user-util.h"
3ffd4af2 29#include "util.h"
41669317 30
e5b42203
LP
31int switch_root(const char *new_root,
32 const char *old_root_after, /* path below the new root, where to place the old root after the transition */
33 bool unmount_old_root,
34 unsigned long mount_flags) { /* MS_MOVE or MS_BIND */
41669317 35
e5b42203 36 _cleanup_free_ char *resolved_old_root_after = NULL;
03e334a1 37 _cleanup_close_ int old_root_fd = -1;
41669317 38 bool old_root_remove;
e5b42203
LP
39 const char *i;
40 int r;
41
42 assert(new_root);
43 assert(old_root_after);
41669317
LP
44
45 if (path_equal(new_root, "/"))
46 return 0;
47
e5b42203 48 /* Check if we shall remove the contents of the old root */
41669317 49 old_root_remove = in_initrd();
e5b42203
LP
50 if (old_root_remove) {
51 old_root_fd = open("/", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
52 if (old_root_fd < 0)
53 return log_error_errno(errno, "Failed to open root directory: %m");
54 }
41669317 55
e5b42203
LP
56 /* Determine where we shall place the old root after the transition */
57 r = chase_symlinks(old_root_after, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved_old_root_after);
58 if (r < 0)
59 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, old_root_after);
60 if (r == 0) /* Doesn't exist yet. Let's create it */
61 (void) mkdir_p_label(resolved_old_root_after, 0755);
41669317 62
e5b42203 63 /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted MS_SHARED. Hence
d677d4df 64 * remount them MS_PRIVATE here as a work-around.
f47fc355
LP
65 *
66 * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
67 if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
e5b42203
LP
68 return log_error_errno(errno, "Failed to set \"/\" mount propagation to private: %m");
69
70 FOREACH_STRING(i, "/sys", "/dev", "/run", "/proc") {
71 _cleanup_free_ char *chased = NULL;
72
73 r = chase_symlinks(i, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &chased);
74 if (r < 0)
75 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, i);
76 if (r > 0) {
77 /* Already exists. Let's see if it is a mount point already. */
78 r = path_is_mount_point(chased, NULL, 0);
79 if (r < 0)
80 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", chased);
81 if (r > 0) /* If it is already mounted, then do nothing */
82 continue;
83 } else
84 /* Doesn't exist yet? */
85 (void) mkdir_p_label(chased, 0755);
86
87 if (mount(i, chased, NULL, mount_flags, NULL) < 0)
88 return log_error_errno(r, "Failed to mount %s to %s: %m", i, chased);
41669317
LP
89 }
90
e5b42203
LP
91 /* Do not fail if base_filesystem_create() fails. Not all switch roots are like base_filesystem_create() wants
92 * them to look like. They might even boot, if they are RO and don't have the FS layout. Just ignore the error
93 * and switch_root() nevertheless. */
03cfe0d5 94 (void) base_filesystem_create(new_root, UID_INVALID, GID_INVALID);
971ff8c7 95
4a62c710
MS
96 if (chdir(new_root) < 0)
97 return log_error_errno(errno, "Failed to change directory to %s: %m", new_root);
41669317 98
e5b42203
LP
99 /* We first try a pivot_root() so that we can umount the old root dir. In many cases (i.e. where rootfs is /),
100 * that's not possible however, and hence we simply overmount root */
101 if (pivot_root(new_root, resolved_old_root_after) >= 0) {
891a4918 102
5a4bf02f
HH
103 /* Immediately get rid of the old root, if detach_oldroot is set.
104 * Since we are running off it we need to do this lazily. */
e5b42203
LP
105 if (unmount_old_root) {
106 r = umount_recursive(old_root_after, MNT_DETACH);
107 if (r < 0)
108 log_warning_errno(r, "Failed to unmount old root directory tree, ignoring: %m");
109 }
891a4918 110
4a62c710 111 } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0)
e5b42203 112 return log_error_errno(errno, "Failed to move %s to /: %m", new_root);
41669317 113
4a62c710
MS
114 if (chroot(".") < 0)
115 return log_error_errno(errno, "Failed to change root: %m");
41669317 116
4a62c710
MS
117 if (chdir("/") < 0)
118 return log_error_errno(errno, "Failed to change directory: %m");
ac0930c8 119
41669317
LP
120 if (old_root_fd >= 0) {
121 struct stat rb;
122
123 if (fstat(old_root_fd, &rb) < 0)
56f64d95 124 log_warning_errno(errno, "Failed to stat old root directory, leaving: %m");
b46178e5 125 else {
c6878637 126 (void) rm_rf_children(old_root_fd, 0, &rb);
b46178e5
HH
127 old_root_fd = -1;
128 }
41669317
LP
129 }
130
03e334a1 131 return 0;
41669317 132}