Debian Codegraph @

src/volatile-root/volatile-root.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mount.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "blockdev-util.h"
8 #include "chase.h"
9 #include "devnum-util.h"
10 #include "escape.h"
11 #include "log.h"
12 #include "main-func.h"
13 #include "mkdir.h"
14 #include "mount-util.h"
15 #include "mountpoint-util.h"
16 #include "path-util.h"
17 #include "stat-util.h"
18 #include "string-util.h"
19 #include "volatile-util.h"
20
21 static int make_volatile(const char *path) {
22 _cleanup_free_ char *old_usr = NULL;
23 int r;
24
25 assert(path);
26
27 r = chase("/usr", path, CHASE_PREFIX_ROOT, &old_usr, NULL);
28 if (r < 0)
29 return log_error_errno(r, "/usr not available in old root: %m");
30
31 r = mkdir_p("/run/systemd/volatile-sysroot", 0700);
32 if (r < 0)
33 return log_error_errno(r, "Couldn't generate volatile sysroot directory: %m");
34
35 r = mount_nofollow_verbose(LOG_ERR, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME, "mode=0755" TMPFS_LIMITS_ROOTFS);
36 if (r < 0)
37 goto finish_rmdir;
38
39 if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
40 r = log_error_errno(errno, "Failed to create /usr directory: %m");
41 goto finish_umount;
42 }
43
44 r = mount_nofollow_verbose(LOG_ERR, old_usr, "/run/systemd/volatile-sysroot/usr", NULL, MS_BIND|MS_REC, NULL);
45 if (r < 0)
46 goto finish_umount;
47
48 r = bind_remount_recursive("/run/systemd/volatile-sysroot/usr", MS_RDONLY, MS_RDONLY, NULL);
49 if (r < 0) {
50 log_error_errno(r, "Failed to remount /usr read-only: %m");
51 goto finish_umount;
52 }
53
54 r = umount_recursive(path, 0);
55 if (r < 0) {
56 log_error_errno(r, "Failed to unmount %s: %m", path);
57 goto finish_umount;
58 }
59
60 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
61 log_warning_errno(errno, "Failed to remount %s MS_SLAVE|MS_REC, ignoring: %m", path);
62
63 r = mount_nofollow_verbose(LOG_ERR, "/run/systemd/volatile-sysroot", path, NULL, MS_MOVE, NULL);
64
65 finish_umount:
66 (void) umount_recursive("/run/systemd/volatile-sysroot", 0);
67
68 finish_rmdir:
69 (void) rmdir("/run/systemd/volatile-sysroot");
70
71 return r;
72 }
73
74 static int make_overlay(const char *path) {
75 _cleanup_free_ char *escaped_path = NULL;
76 bool tmpfs_mounted = false;
77 const char *options = NULL;
78 int r;
79
80 assert(path);
81
82 r = mkdir_p("/run/systemd/overlay-sysroot", 0700);
83 if (r < 0)
84 return log_error_errno(r, "Couldn't create overlay sysroot directory: %m");
85
86 r = mount_nofollow_verbose(LOG_ERR, "tmpfs", "/run/systemd/overlay-sysroot", "tmpfs", MS_STRICTATIME, "mode=0755" TMPFS_LIMITS_ROOTFS);
87 if (r < 0)
88 goto finish;
89
90 tmpfs_mounted = true;
91
92 if (mkdir("/run/systemd/overlay-sysroot/upper", 0755) < 0) {
93 r = log_error_errno(errno, "Failed to create /run/systemd/overlay-sysroot/upper: %m");
94 goto finish;
95 }
96
97 if (mkdir("/run/systemd/overlay-sysroot/work", 0755) < 0) {
98 r = log_error_errno(errno, "Failed to create /run/systemd/overlay-sysroot/work: %m");
99 goto finish;
100 }
101
102 escaped_path = shell_escape(path, ",:");
103 if (!escaped_path) {
104 r = log_oom();
105 goto finish;
106 }
107
108 options = strjoina("lowerdir=", escaped_path, ",upperdir=/run/systemd/overlay-sysroot/upper,workdir=/run/systemd/overlay-sysroot/work");
109 r = mount_nofollow_verbose(LOG_ERR, "overlay", path, "overlay", 0, options);
110
111 finish:
112 if (tmpfs_mounted)
113 (void) umount_verbose(LOG_ERR, "/run/systemd/overlay-sysroot", UMOUNT_NOFOLLOW);
114
115 (void) rmdir("/run/systemd/overlay-sysroot");
116 return r;
117 }
118
119 static int run(int argc, char *argv[]) {
120 VolatileMode m = _VOLATILE_MODE_INVALID;
121 const char *path;
122 dev_t devt;
123 int r;
124
125 log_setup();
126
127 if (argc > 3)
128 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
129 "Too many arguments. Expected directory and mode.");
130
131 r = query_volatile_mode(&m);
132 if (r < 0)
133 return log_error_errno(r, "Failed to determine volatile mode from kernel command line: %m");
134 if (r == 0 && argc >= 2) {
135 /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
136 m = volatile_mode_from_string(argv[1]);
137 if (m < 0)
138 return log_error_errno(m, "Couldn't parse volatile mode: %s", argv[1]);
139 }
140
141 if (argc < 3)
142 path = "/sysroot";
143 else {
144 path = argv[2];
145
146 if (isempty(path))
147 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
148 "Directory name cannot be empty.");
149 if (!path_is_absolute(path))
150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
151 "Directory must be specified as absolute path.");
152 if (path_equal(path, "/"))
153 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
154 "Directory cannot be the root directory.");
155 }
156
157 if (!IN_SET(m, VOLATILE_YES, VOLATILE_OVERLAY))
158 return 0;
159
160 r = path_is_mount_point_full(path, /* root= */ NULL, AT_SYMLINK_FOLLOW);
161 if (r < 0)
162 return log_error_errno(r, "Couldn't determine whether %s is a mount point: %m", path);
163 if (r == 0)
164 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a mount point.", path);
165
166 r = path_is_temporary_fs(path);
167 if (r < 0)
168 return log_error_errno(r, "Couldn't determine whether %s is a temporary file system: %m", path);
169 if (r > 0) {
170 log_info("%s already is a temporary file system.", path);
171 return 0;
172 }
173
174 /* We are about to replace the root directory with something else. Later code might want to know what we
175 * replaced here, hence let's save that information as a symlink we can later use. (This is particularly
176 * relevant for the overlayfs case where we'll fully obstruct the view onto the underlying device, hence
177 * querying the backing device node from the file system directly is no longer possible. */
178 r = get_block_device_harder(path, &devt);
179 if (r < 0)
180 return log_error_errno(r, "Failed to determine device major/minor of %s: %m", path);
181 else if (r > 0) { /* backed by block device */
182 _cleanup_free_ char *dn = NULL;
183
184 r = device_path_make_major_minor(S_IFBLK, devt, &dn);
185 if (r < 0)
186 return log_error_errno(r, "Failed to format device node path: %m");
187
188 if (symlink(dn, "/run/systemd/volatile-root") < 0)
189 log_warning_errno(errno, "Failed to create symlink /run/systemd/volatile-root: %m");
190 }
191
192 if (m == VOLATILE_YES)
193 return make_volatile(path);
194 else {
195 assert(m == VOLATILE_OVERLAY);
196 return make_overlay(path);
197 }
198 }
199
200 DEFINE_MAIN_FUNCTION(run);