]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/switch-root.c
Merge pull request #27450 from poettering/switch-root-modernize
[thirdparty/systemd.git] / src / shared / switch-root.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <limits.h>
6 #include <stdbool.h>
7 #include <sys/mount.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10
11 #include "base-filesystem.h"
12 #include "chase.h"
13 #include "fd-util.h"
14 #include "initrd-util.h"
15 #include "log.h"
16 #include "missing_syscall.h"
17 #include "mkdir-label.h"
18 #include "mount-util.h"
19 #include "mountpoint-util.h"
20 #include "path-util.h"
21 #include "rm-rf.h"
22 #include "stdio-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "switch-root.h"
26 #include "user-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; may be NULL to unmount it */
30 unsigned long mount_flags) { /* MS_MOVE or MS_BIND used for /proc/, /dev/, /run/, /sys/ */
31
32 _cleanup_close_ int old_root_fd = -EBADF, new_root_fd = -EBADF;
33 _cleanup_free_ char *resolved_old_root_after = NULL;
34 int r, istmp;
35
36 assert(new_root);
37 assert(IN_SET(mount_flags, MS_MOVE, MS_BIND));
38
39 if (path_equal(new_root, "/"))
40 return 0;
41
42 /* Check if we shall remove the contents of the old root */
43 old_root_fd = open("/", O_DIRECTORY|O_CLOEXEC);
44 if (old_root_fd < 0)
45 return log_error_errno(errno, "Failed to open root directory: %m");
46
47 istmp = fd_is_temporary_fs(old_root_fd);
48 if (istmp < 0)
49 return log_error_errno(istmp, "Failed to stat root directory: %m");
50 if (istmp > 0)
51 log_debug("Root directory is on tmpfs, will do cleanup later.");
52
53 new_root_fd = open(new_root, O_DIRECTORY|O_CLOEXEC);
54 if (new_root_fd < 0)
55 return log_error_errno(errno, "Failed to open target directory '%s': %m", new_root);
56
57 if (old_root_after) {
58 /* Determine where we shall place the old root after the transition */
59 r = chase(old_root_after, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved_old_root_after, NULL);
60 if (r < 0)
61 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, old_root_after);
62 if (r == 0) /* Doesn't exist yet. Let's create it */
63 (void) mkdir_p_label(resolved_old_root_after, 0755);
64 }
65
66 /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted
67 * MS_SHARED. Hence remount them MS_PRIVATE here as a work-around.
68 *
69 * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
70 if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
71 return log_error_errno(errno, "Failed to set \"/\" mount propagation to private: %m");
72
73 FOREACH_STRING(path, "/sys", "/dev", "/run", "/proc") {
74 _cleanup_free_ char *chased = NULL;
75
76 r = chase(path, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &chased, NULL);
77 if (r < 0)
78 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, path);
79 if (r > 0) {
80 /* Already exists. Let's see if it is a mount point already. */
81 r = path_is_mount_point(chased, NULL, 0);
82 if (r < 0)
83 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", chased);
84 if (r > 0) /* If it is already mounted, then do nothing */
85 continue;
86 } else
87 /* Doesn't exist yet? */
88 (void) mkdir_p_label(chased, 0755);
89
90 if (mount(path, chased, NULL, mount_flags, NULL) < 0)
91 return log_error_errno(errno, "Failed to mount %s to %s: %m", path, chased);
92 }
93
94 /* Do not fail if base_filesystem_create() fails. Not all switch roots are like base_filesystem_create() wants
95 * them to look like. They might even boot, if they are RO and don't have the FS layout. Just ignore the error
96 * and switch_root() nevertheless. */
97 (void) base_filesystem_create_fd(new_root_fd, new_root, UID_INVALID, GID_INVALID);
98
99 if (fchdir(new_root_fd) < 0)
100 return log_error_errno(errno, "Failed to change directory to %s: %m", new_root);
101
102 /* We first try a pivot_root() so that we can umount the old root dir. In many cases (i.e. where rootfs is /),
103 * that's not possible however, and hence we simply overmount root */
104 if (resolved_old_root_after)
105 r = RET_NERRNO(pivot_root(".", resolved_old_root_after));
106 else {
107 r = RET_NERRNO(pivot_root(".", "."));
108 if (r >= 0) {
109 /* Now unmount the upper of the two stacked file systems */
110 if (umount2(".", MNT_DETACH) < 0)
111 return log_error_errno(errno, "Failed to unmount the old root: %m");
112 }
113 }
114 if (r < 0) {
115 log_debug_errno(r, "Pivoting root file system failed, moving mounts instead: %m");
116
117 if (mount(".", "/", NULL, MS_MOVE, NULL) < 0)
118 return log_error_errno(errno, "Failed to move %s to /: %m", new_root);
119
120 if (chroot(".") < 0)
121 return log_error_errno(errno, "Failed to change root: %m");
122
123 if (chdir(".") < 0)
124 return log_error_errno(errno, "Failed to change directory: %m");
125 }
126
127 if (istmp) {
128 struct stat rb;
129
130 if (fstat(old_root_fd, &rb) < 0)
131 return log_error_errno(errno, "Failed to stat old root directory: %m");
132
133 (void) rm_rf_children(TAKE_FD(old_root_fd), 0, &rb); /* takes possession of the dir fd, even on failure */
134 }
135
136 return 0;
137 }