]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
41669317 LP |
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 | ||
41669317 | 21 | #include <errno.h> |
07630cea | 22 | #include <fcntl.h> |
a8fbdf54 | 23 | #include <limits.h> |
07630cea | 24 | #include <stdbool.h> |
a8fbdf54 | 25 | #include <stdio.h> |
41669317 | 26 | #include <sys/mount.h> |
07630cea | 27 | #include <sys/stat.h> |
41669317 | 28 | #include <unistd.h> |
41669317 | 29 | |
971ff8c7 | 30 | #include "base-filesystem.h" |
3ffd4af2 | 31 | #include "fd-util.h" |
e5b42203 | 32 | #include "fs-util.h" |
a8fbdf54 | 33 | #include "log.h" |
891a4918 | 34 | #include "missing.h" |
07630cea | 35 | #include "mkdir.h" |
e5b42203 | 36 | #include "mount-util.h" |
07630cea LP |
37 | #include "path-util.h" |
38 | #include "rm-rf.h" | |
d054f0a4 | 39 | #include "stdio-util.h" |
07630cea | 40 | #include "string-util.h" |
e5b42203 | 41 | #include "strv.h" |
c6878637 | 42 | #include "switch-root.h" |
ee104e11 | 43 | #include "user-util.h" |
3ffd4af2 | 44 | #include "util.h" |
41669317 | 45 | |
e5b42203 LP |
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 */ | |
41669317 | 50 | |
e5b42203 | 51 | _cleanup_free_ char *resolved_old_root_after = NULL; |
03e334a1 | 52 | _cleanup_close_ int old_root_fd = -1; |
41669317 | 53 | bool old_root_remove; |
e5b42203 LP |
54 | const char *i; |
55 | int r; | |
56 | ||
57 | assert(new_root); | |
58 | assert(old_root_after); | |
41669317 LP |
59 | |
60 | if (path_equal(new_root, "/")) | |
61 | return 0; | |
62 | ||
e5b42203 | 63 | /* Check if we shall remove the contents of the old root */ |
41669317 | 64 | old_root_remove = in_initrd(); |
e5b42203 LP |
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 | } | |
41669317 | 70 | |
e5b42203 LP |
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); | |
41669317 | 77 | |
e5b42203 | 78 | /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted MS_SHARED. Hence |
d677d4df | 79 | * remount them MS_PRIVATE here as a work-around. |
f47fc355 LP |
80 | * |
81 | * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */ | |
82 | if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0) | |
e5b42203 LP |
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); | |
41669317 LP |
104 | } |
105 | ||
e5b42203 LP |
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. */ | |
03cfe0d5 | 109 | (void) base_filesystem_create(new_root, UID_INVALID, GID_INVALID); |
971ff8c7 | 110 | |
4a62c710 MS |
111 | if (chdir(new_root) < 0) |
112 | return log_error_errno(errno, "Failed to change directory to %s: %m", new_root); | |
41669317 | 113 | |
e5b42203 LP |
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) { | |
891a4918 | 117 | |
5a4bf02f HH |
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. */ | |
e5b42203 LP |
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 | } | |
891a4918 | 125 | |
4a62c710 | 126 | } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0) |
e5b42203 | 127 | return log_error_errno(errno, "Failed to move %s to /: %m", new_root); |
41669317 | 128 | |
4a62c710 MS |
129 | if (chroot(".") < 0) |
130 | return log_error_errno(errno, "Failed to change root: %m"); | |
41669317 | 131 | |
4a62c710 MS |
132 | if (chdir("/") < 0) |
133 | return log_error_errno(errno, "Failed to change directory: %m"); | |
ac0930c8 | 134 | |
41669317 LP |
135 | if (old_root_fd >= 0) { |
136 | struct stat rb; | |
137 | ||
138 | if (fstat(old_root_fd, &rb) < 0) | |
56f64d95 | 139 | log_warning_errno(errno, "Failed to stat old root directory, leaving: %m"); |
b46178e5 | 140 | else { |
c6878637 | 141 | (void) rm_rf_children(old_root_fd, 0, &rb); |
b46178e5 HH |
142 | old_root_fd = -1; |
143 | } | |
41669317 LP |
144 | } |
145 | ||
03e334a1 | 146 | return 0; |
41669317 | 147 | } |