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