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