]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/switch-root.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / shared / switch-root.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
41669317 2
41669317 3#include <errno.h>
07630cea 4#include <fcntl.h>
a8fbdf54 5#include <limits.h>
07630cea 6#include <stdbool.h>
41669317 7#include <sys/mount.h>
07630cea 8#include <sys/stat.h>
41669317 9#include <unistd.h>
41669317 10
971ff8c7 11#include "base-filesystem.h"
f4351959 12#include "chase-symlinks.h"
3ffd4af2 13#include "fd-util.h"
baa6a42d 14#include "initrd-util.h"
a8fbdf54 15#include "log.h"
f5947a5e 16#include "missing_syscall.h"
35cd0ba5 17#include "mkdir-label.h"
e5b42203 18#include "mount-util.h"
049af8ad 19#include "mountpoint-util.h"
07630cea
LP
20#include "path-util.h"
21#include "rm-rf.h"
d054f0a4 22#include "stdio-util.h"
07630cea 23#include "string-util.h"
e5b42203 24#include "strv.h"
c6878637 25#include "switch-root.h"
ee104e11 26#include "user-util.h"
41669317 27
e5b42203
LP
28int 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 */
41669317 32
e5b42203 33 _cleanup_free_ char *resolved_old_root_after = NULL;
254d1313 34 _cleanup_close_ int old_root_fd = -EBADF;
e5b42203
LP
35 int r;
36
37 assert(new_root);
38 assert(old_root_after);
41669317
LP
39
40 if (path_equal(new_root, "/"))
41 return 0;
42
e5b42203 43 /* Check if we shall remove the contents of the old root */
a940f507
ZJS
44 old_root_fd = open("/", O_RDONLY | O_CLOEXEC | O_DIRECTORY);
45 if (old_root_fd < 0)
46 return log_error_errno(errno, "Failed to open root directory: %m");
47 r = fd_is_temporary_fs(old_root_fd);
48 if (r < 0)
49 return log_error_errno(r, "Failed to stat root directory: %m");
50 if (r > 0)
51 log_debug("Root directory is on tmpfs, will do cleanup later.");
52 else
53 old_root_fd = safe_close(old_root_fd);
41669317 54
e5b42203 55 /* Determine where we shall place the old root after the transition */
a5648b80 56 r = chase_symlinks(old_root_after, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved_old_root_after, NULL);
e5b42203
LP
57 if (r < 0)
58 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, old_root_after);
59 if (r == 0) /* Doesn't exist yet. Let's create it */
60 (void) mkdir_p_label(resolved_old_root_after, 0755);
41669317 61
e5b42203 62 /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted MS_SHARED. Hence
d677d4df 63 * remount them MS_PRIVATE here as a work-around.
f47fc355
LP
64 *
65 * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
66 if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
e5b42203
LP
67 return log_error_errno(errno, "Failed to set \"/\" mount propagation to private: %m");
68
5980d463 69 FOREACH_STRING(path, "/sys", "/dev", "/run", "/proc") {
e5b42203
LP
70 _cleanup_free_ char *chased = NULL;
71
5980d463 72 r = chase_symlinks(path, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &chased, NULL);
e5b42203 73 if (r < 0)
5980d463 74 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, path);
e5b42203
LP
75 if (r > 0) {
76 /* Already exists. Let's see if it is a mount point already. */
77 r = path_is_mount_point(chased, NULL, 0);
78 if (r < 0)
79 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", chased);
80 if (r > 0) /* If it is already mounted, then do nothing */
81 continue;
82 } else
83 /* Doesn't exist yet? */
84 (void) mkdir_p_label(chased, 0755);
85
5980d463
ZJS
86 if (mount(path, chased, NULL, mount_flags, NULL) < 0)
87 return log_error_errno(errno, "Failed to mount %s to %s: %m", path, chased);
41669317
LP
88 }
89
e5b42203
LP
90 /* Do not fail if base_filesystem_create() fails. Not all switch roots are like base_filesystem_create() wants
91 * them to look like. They might even boot, if they are RO and don't have the FS layout. Just ignore the error
92 * and switch_root() nevertheless. */
03cfe0d5 93 (void) base_filesystem_create(new_root, UID_INVALID, GID_INVALID);
971ff8c7 94
4a62c710
MS
95 if (chdir(new_root) < 0)
96 return log_error_errno(errno, "Failed to change directory to %s: %m", new_root);
41669317 97
e5b42203
LP
98 /* We first try a pivot_root() so that we can umount the old root dir. In many cases (i.e. where rootfs is /),
99 * that's not possible however, and hence we simply overmount root */
100 if (pivot_root(new_root, resolved_old_root_after) >= 0) {
891a4918 101
5a4bf02f
HH
102 /* Immediately get rid of the old root, if detach_oldroot is set.
103 * Since we are running off it we need to do this lazily. */
e5b42203
LP
104 if (unmount_old_root) {
105 r = umount_recursive(old_root_after, MNT_DETACH);
106 if (r < 0)
107 log_warning_errno(r, "Failed to unmount old root directory tree, ignoring: %m");
108 }
891a4918 109
4a62c710 110 } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0)
e5b42203 111 return log_error_errno(errno, "Failed to move %s to /: %m", new_root);
41669317 112
4a62c710
MS
113 if (chroot(".") < 0)
114 return log_error_errno(errno, "Failed to change root: %m");
41669317 115
4a62c710
MS
116 if (chdir("/") < 0)
117 return log_error_errno(errno, "Failed to change directory: %m");
ac0930c8 118
41669317
LP
119 if (old_root_fd >= 0) {
120 struct stat rb;
121
122 if (fstat(old_root_fd, &rb) < 0)
a940f507
ZJS
123 return log_error_errno(errno, "Failed to stat old root directory: %m");
124 (void) rm_rf_children(TAKE_FD(old_root_fd), 0, &rb); /* takes possession of the dir fd, even on failure */
41669317
LP
125 }
126
03e334a1 127 return 0;
41669317 128}