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