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