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