]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
1a14a53c | 2 | |
1a14a53c | 3 | #include <stdlib.h> |
cf0fbc49 | 4 | #include <unistd.h> |
1a14a53c | 5 | |
85624f01 | 6 | #include "sd-device.h" |
07630cea LP |
7 | #include "sd-id128.h" |
8 | ||
b5efdb8a | 9 | #include "alloc-util.h" |
07630cea | 10 | #include "blkid-util.h" |
18c528e9 | 11 | #include "blockdev-util.h" |
07630cea | 12 | #include "btrfs-util.h" |
133432cc | 13 | #include "device-util.h" |
a0956174 | 14 | #include "dirent-util.h" |
72e18a98 | 15 | #include "dissect-image.h" |
1fac34b9 | 16 | #include "dropin.h" |
0bb2f0f1 | 17 | #include "efi-loader.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
07630cea | 19 | #include "fileio.h" |
2bef2582 | 20 | #include "fs-util.h" |
07630cea LP |
21 | #include "fstab-util.h" |
22 | #include "generator.h" | |
23 | #include "gpt.h" | |
07630cea | 24 | #include "mkdir.h" |
049af8ad | 25 | #include "mountpoint-util.h" |
6bedfcbb | 26 | #include "parse-util.h" |
07630cea | 27 | #include "path-util.h" |
4e731273 | 28 | #include "proc-cmdline.h" |
1a14a53c | 29 | #include "special.h" |
98bad05e | 30 | #include "specifier.h" |
8fcde012 | 31 | #include "stat-util.h" |
07630cea | 32 | #include "string-util.h" |
85624f01 | 33 | #include "strv.h" |
1a14a53c | 34 | #include "unit-name.h" |
07630cea | 35 | #include "util.h" |
9a5cb137 | 36 | #include "virt.h" |
1a14a53c | 37 | |
ec6e9597 | 38 | static const char *arg_dest = NULL; |
73b80ec2 LP |
39 | static bool arg_enabled = true; |
40 | static bool arg_root_enabled = true; | |
c94b2417 | 41 | static int arg_root_rw = -1; |
1a14a53c | 42 | |
3d92aa45 | 43 | static int open_parent_block_device(dev_t devnum, int *ret_fd) { |
9fe6f5cc ZJS |
44 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
45 | const char *name, *devtype, *node; | |
46 | sd_device *parent; | |
47 | dev_t pn; | |
48 | int fd, r; | |
49 | ||
3d92aa45 | 50 | assert(ret_fd); |
9fe6f5cc ZJS |
51 | |
52 | r = sd_device_new_from_devnum(&d, 'b', devnum); | |
53 | if (r < 0) | |
54 | return log_debug_errno(r, "Failed to open device: %m"); | |
55 | ||
56 | if (sd_device_get_devname(d, &name) < 0) { | |
57 | r = sd_device_get_syspath(d, &name); | |
58 | if (r < 0) { | |
3d92aa45 ZJS |
59 | log_device_debug_errno(d, r, "Device %u:%u does not have a name, ignoring: %m", |
60 | major(devnum), minor(devnum)); | |
9fe6f5cc ZJS |
61 | return 0; |
62 | } | |
63 | } | |
64 | ||
65 | r = sd_device_get_parent(d, &parent); | |
66 | if (r < 0) { | |
67 | log_device_debug_errno(d, r, "Not a partitioned device, ignoring: %m"); | |
68 | return 0; | |
69 | } | |
70 | ||
71 | /* Does it have a devtype? */ | |
72 | r = sd_device_get_devtype(parent, &devtype); | |
73 | if (r < 0) { | |
74 | log_device_debug_errno(parent, r, "Parent doesn't have a device type, ignoring: %m"); | |
75 | return 0; | |
76 | } | |
77 | ||
78 | /* Is this a disk or a partition? We only care for disks... */ | |
79 | if (!streq(devtype, "disk")) { | |
80 | log_device_debug(parent, "Parent isn't a raw disk, ignoring."); | |
81 | return 0; | |
82 | } | |
83 | ||
84 | /* Does it have a device node? */ | |
85 | r = sd_device_get_devname(parent, &node); | |
86 | if (r < 0) { | |
87 | log_device_debug_errno(parent, r, "Parent device does not have device node, ignoring: %m"); | |
88 | return 0; | |
89 | } | |
90 | ||
91 | log_device_debug(d, "Root device %s.", node); | |
92 | ||
93 | r = sd_device_get_devnum(parent, &pn); | |
94 | if (r < 0) { | |
95 | log_device_debug_errno(parent, r, "Parent device is not a proper block device, ignoring: %m"); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | fd = open(node, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
100 | if (fd < 0) | |
101 | return log_error_errno(errno, "Failed to open %s: %m", node); | |
102 | ||
3d92aa45 | 103 | *ret_fd = fd; |
9fe6f5cc ZJS |
104 | return 1; |
105 | } | |
106 | ||
01af8c01 | 107 | static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) { |
5b137503 | 108 | #if HAVE_LIBCRYPTSETUP |
a7e88558 | 109 | _cleanup_free_ char *e = NULL, *n = NULL, *d = NULL; |
1af72119 | 110 | _cleanup_fclose_ FILE *f = NULL; |
1af72119 LP |
111 | int r; |
112 | ||
113 | assert(id); | |
114 | assert(what); | |
1af72119 | 115 | |
7410616c LP |
116 | r = unit_name_from_path(what, ".device", &d); |
117 | if (r < 0) | |
118 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
1af72119 LP |
119 | |
120 | e = unit_name_escape(id); | |
121 | if (!e) | |
122 | return log_oom(); | |
123 | ||
7410616c LP |
124 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
125 | if (r < 0) | |
126 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
1af72119 | 127 | |
a7e88558 LP |
128 | r = generator_open_unit_file(arg_dest, NULL, n, &f); |
129 | if (r < 0) | |
130 | return r; | |
98bad05e | 131 | |
a7e88558 LP |
132 | r = generator_write_cryptsetup_unit_section(f, NULL); |
133 | if (r < 0) | |
134 | return r; | |
1af72119 LP |
135 | |
136 | fprintf(f, | |
1af72119 | 137 | "Before=umount.target cryptsetup.target\n" |
a7e88558 LP |
138 | "Conflicts=umount.target\n" |
139 | "BindsTo=%s\n" | |
140 | "After=%s\n", | |
141 | d, d); | |
142 | ||
143 | r = generator_write_cryptsetup_service_section(f, id, what, NULL, rw ? NULL : "read-only"); | |
144 | if (r < 0) | |
145 | return r; | |
1af72119 | 146 | |
dacd6cee LP |
147 | r = fflush_and_check(f); |
148 | if (r < 0) | |
a7e88558 | 149 | return log_error_errno(r, "Failed to write file %s: %m", n); |
1af72119 | 150 | |
9cdcf368 ZJS |
151 | r = generator_add_symlink(arg_dest, d, "wants", n); |
152 | if (r < 0) | |
153 | return r; | |
1af72119 | 154 | |
1fac34b9 ZJS |
155 | const char *dmname; |
156 | dmname = strjoina("dev-mapper-", e, ".device"); | |
1af72119 | 157 | |
1fac34b9 | 158 | if (require) { |
9cdcf368 ZJS |
159 | r = generator_add_symlink(arg_dest, "cryptsetup.target", "requires", n); |
160 | if (r < 0) | |
161 | return r; | |
01af8c01 | 162 | |
9cdcf368 ZJS |
163 | r = generator_add_symlink(arg_dest, dmname, "requires", n); |
164 | if (r < 0) | |
165 | return r; | |
01af8c01 | 166 | } |
1af72119 | 167 | |
1fac34b9 ZJS |
168 | r = write_drop_in_format(arg_dest, dmname, 50, "job-timeout", |
169 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
170 | "[Unit]\n" | |
171 | "JobTimeoutSec=0"); /* the binary handles timeouts anyway */ | |
23bbb0de | 172 | if (r < 0) |
1fac34b9 | 173 | log_warning_errno(r, "Failed to write device timeout drop-in, ignoring: %m"); |
1af72119 | 174 | |
2aa2860b ZJS |
175 | if (device) { |
176 | char *ret; | |
177 | ||
b910cc72 | 178 | ret = path_join("/dev/mapper", id); |
2aa2860b ZJS |
179 | if (!ret) |
180 | return log_oom(); | |
1af72119 | 181 | |
01af8c01 | 182 | *device = ret; |
2aa2860b ZJS |
183 | } |
184 | ||
1af72119 | 185 | return 0; |
5b137503 YG |
186 | #else |
187 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Partition is encrypted, but the project was compiled without libcryptsetup support"); | |
188 | #endif | |
1af72119 LP |
189 | } |
190 | ||
73b80ec2 LP |
191 | static int add_mount( |
192 | const char *id, | |
193 | const char *what, | |
194 | const char *where, | |
195 | const char *fstype, | |
cca1dfdd | 196 | bool rw, |
400c1e8f | 197 | bool growfs, |
59512f21 | 198 | const char *options, |
73b80ec2 LP |
199 | const char *description, |
200 | const char *post) { | |
201 | ||
9cdcf368 | 202 | _cleanup_free_ char *unit = NULL, *crypto_what = NULL, *p = NULL; |
1a14a53c | 203 | _cleanup_fclose_ FILE *f = NULL; |
e48fdd84 | 204 | int r; |
1a14a53c | 205 | |
98bad05e LP |
206 | /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured |
207 | * externally, but all originate from our own sources here, and hence we know they contain no % characters that | |
208 | * could potentially be understood as specifiers. */ | |
209 | ||
1af72119 LP |
210 | assert(id); |
211 | assert(what); | |
212 | assert(where); | |
1af72119 LP |
213 | assert(description); |
214 | ||
074cdb95 | 215 | log_debug("Adding %s: %s fstype=%s", where, what, fstype ?: "(any)"); |
1a14a53c | 216 | |
73b80ec2 | 217 | if (streq_ptr(fstype, "crypto_LUKS")) { |
01af8c01 | 218 | r = add_cryptsetup(id, what, rw, true, &crypto_what); |
1af72119 LP |
219 | if (r < 0) |
220 | return r; | |
221 | ||
222 | what = crypto_what; | |
223 | fstype = NULL; | |
224 | } | |
225 | ||
7410616c LP |
226 | r = unit_name_from_path(where, ".mount", &unit); |
227 | if (r < 0) | |
228 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
1a14a53c | 229 | |
657ee2d8 | 230 | p = path_join(empty_to_root(arg_dest), unit); |
e48fdd84 LP |
231 | if (!p) |
232 | return log_oom(); | |
233 | ||
234 | f = fopen(p, "wxe"); | |
4a62c710 MS |
235 | if (!f) |
236 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
1a14a53c LP |
237 | |
238 | fprintf(f, | |
239 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
240 | "[Unit]\n" | |
c3834f9b LP |
241 | "Description=%s\n" |
242 | "Documentation=man:systemd-gpt-auto-generator(8)\n", | |
e48fdd84 LP |
243 | description); |
244 | ||
73b80ec2 LP |
245 | if (post) |
246 | fprintf(f, "Before=%s\n", post); | |
247 | ||
e48fdd84 LP |
248 | r = generator_write_fsck_deps(f, arg_dest, what, where, fstype); |
249 | if (r < 0) | |
250 | return r; | |
251 | ||
a7e88558 LP |
252 | r = generator_write_blockdev_dependency(f, what); |
253 | if (r < 0) | |
254 | return r; | |
255 | ||
e48fdd84 LP |
256 | fprintf(f, |
257 | "\n" | |
1a14a53c LP |
258 | "[Mount]\n" |
259 | "What=%s\n" | |
1af72119 LP |
260 | "Where=%s\n", |
261 | what, where); | |
262 | ||
73b80ec2 LP |
263 | if (fstype) |
264 | fprintf(f, "Type=%s\n", fstype); | |
265 | ||
59512f21 KS |
266 | if (options) |
267 | fprintf(f, "Options=%s,%s\n", options, rw ? "rw" : "ro"); | |
268 | else | |
269 | fprintf(f, "Options=%s\n", rw ? "rw" : "ro"); | |
1a14a53c | 270 | |
dacd6cee LP |
271 | r = fflush_and_check(f); |
272 | if (r < 0) | |
273 | return log_error_errno(r, "Failed to write unit file %s: %m", p); | |
1a14a53c | 274 | |
400c1e8f LP |
275 | if (growfs) { |
276 | r = generator_hook_up_growfs(arg_dest, where, post); | |
277 | if (r < 0) | |
278 | return r; | |
279 | } | |
280 | ||
281 | if (post) { | |
282 | r = generator_add_symlink(arg_dest, post, "requires", unit); | |
283 | if (r < 0) | |
284 | return r; | |
285 | } | |
286 | ||
1a14a53c LP |
287 | return 0; |
288 | } | |
289 | ||
e137880b | 290 | static int path_is_busy(const char *where) { |
59512f21 KS |
291 | int r; |
292 | ||
293 | /* already a mountpoint; generators run during reload */ | |
e1873695 | 294 | r = path_is_mount_point(where, NULL, AT_SYMLINK_FOLLOW); |
59512f21 KS |
295 | if (r > 0) |
296 | return false; | |
297 | ||
298 | /* the directory might not exist on a stateless system */ | |
299 | if (r == -ENOENT) | |
300 | return false; | |
301 | ||
302 | if (r < 0) | |
e137880b | 303 | return log_warning_errno(r, "Cannot check if \"%s\" is a mount point: %m", where); |
59512f21 KS |
304 | |
305 | /* not a mountpoint but it contains files */ | |
e137880b ZJS |
306 | r = dir_is_empty(where); |
307 | if (r < 0) | |
308 | return log_warning_errno(r, "Cannot check if \"%s\" is empty: %m", where); | |
309 | if (r > 0) | |
310 | return false; | |
59512f21 | 311 | |
e137880b ZJS |
312 | log_debug("\"%s\" already populated, ignoring.", where); |
313 | return true; | |
59512f21 KS |
314 | } |
315 | ||
72e18a98 LP |
316 | static int add_partition_mount( |
317 | DissectedPartition *p, | |
61331eab | 318 | const char *id, |
61331eab | 319 | const char *where, |
72e18a98 | 320 | const char *description) { |
61331eab | 321 | |
e137880b | 322 | int r; |
72e18a98 | 323 | assert(p); |
61331eab | 324 | |
e137880b ZJS |
325 | r = path_is_busy(where); |
326 | if (r != 0) | |
327 | return r < 0 ? r : 0; | |
61331eab | 328 | |
61331eab LP |
329 | return add_mount( |
330 | id, | |
72e18a98 | 331 | p->node, |
61331eab | 332 | where, |
72e18a98 LP |
333 | p->fstype, |
334 | p->rw, | |
400c1e8f | 335 | p->growfs, |
59512f21 | 336 | NULL, |
61331eab | 337 | description, |
72e18a98 | 338 | SPECIAL_LOCAL_FS_TARGET); |
61331eab LP |
339 | } |
340 | ||
59512f21 | 341 | static int add_swap(const char *path) { |
9cdcf368 | 342 | _cleanup_free_ char *name = NULL, *unit = NULL; |
59512f21 KS |
343 | _cleanup_fclose_ FILE *f = NULL; |
344 | int r; | |
345 | ||
346 | assert(path); | |
347 | ||
fc5bc384 FB |
348 | /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */ |
349 | r = fstab_has_fstype("swap"); | |
350 | if (r < 0) | |
351 | return log_error_errno(r, "Failed to parse fstab: %m"); | |
1a680ae3 | 352 | if (r > 0) { |
fc5bc384 FB |
353 | log_debug("swap specified in fstab, ignoring."); |
354 | return 0; | |
355 | } | |
356 | ||
59512f21 KS |
357 | log_debug("Adding swap: %s", path); |
358 | ||
359 | r = unit_name_from_path(path, ".swap", &name); | |
360 | if (r < 0) | |
361 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
362 | ||
657ee2d8 | 363 | unit = path_join(empty_to_root(arg_dest), name); |
59512f21 KS |
364 | if (!unit) |
365 | return log_oom(); | |
366 | ||
367 | f = fopen(unit, "wxe"); | |
368 | if (!f) | |
369 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
370 | ||
371 | fprintf(f, | |
372 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
373 | "[Unit]\n" | |
374 | "Description=Swap Partition\n" | |
a7e88558 LP |
375 | "Documentation=man:systemd-gpt-auto-generator(8)\n"); |
376 | ||
377 | r = generator_write_blockdev_dependency(f, path); | |
378 | if (r < 0) | |
379 | return r; | |
380 | ||
381 | fprintf(f, | |
382 | "\n" | |
59512f21 KS |
383 | "[Swap]\n" |
384 | "What=%s\n", | |
385 | path); | |
386 | ||
dacd6cee LP |
387 | r = fflush_and_check(f); |
388 | if (r < 0) | |
389 | return log_error_errno(r, "Failed to write unit file %s: %m", unit); | |
59512f21 | 390 | |
9cdcf368 | 391 | return generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, "wants", name); |
59512f21 KS |
392 | } |
393 | ||
7a1494aa TG |
394 | static int add_automount( |
395 | const char *id, | |
396 | const char *what, | |
397 | const char *where, | |
398 | const char *fstype, | |
399 | bool rw, | |
400c1e8f | 400 | bool growfs, |
7a1494aa TG |
401 | const char *options, |
402 | const char *description, | |
403 | usec_t timeout) { | |
404 | ||
9cdcf368 | 405 | _cleanup_free_ char *unit = NULL; |
7a1494aa | 406 | _cleanup_fclose_ FILE *f = NULL; |
2aa2860b | 407 | const char *opt = "noauto", *p; |
7a1494aa TG |
408 | int r; |
409 | ||
410 | assert(id); | |
411 | assert(where); | |
412 | assert(description); | |
413 | ||
414 | if (options) | |
2aa2860b | 415 | opt = strjoina(options, ",", opt); |
7a1494aa TG |
416 | |
417 | r = add_mount(id, | |
418 | what, | |
419 | where, | |
420 | fstype, | |
421 | rw, | |
400c1e8f | 422 | growfs, |
7a1494aa TG |
423 | opt, |
424 | description, | |
425 | NULL); | |
426 | if (r < 0) | |
427 | return r; | |
428 | ||
429 | r = unit_name_from_path(where, ".automount", &unit); | |
430 | if (r < 0) | |
431 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
432 | ||
270384b2 | 433 | p = prefix_roota(arg_dest, unit); |
7a1494aa TG |
434 | f = fopen(p, "wxe"); |
435 | if (!f) | |
436 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
437 | ||
438 | fprintf(f, | |
439 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
440 | "[Unit]\n" | |
441 | "Description=%s\n" | |
442 | "Documentation=man:systemd-gpt-auto-generator(8)\n" | |
443 | "[Automount]\n" | |
444 | "Where=%s\n" | |
70887c5f | 445 | "TimeoutIdleSec="USEC_FMT"\n", |
7a1494aa TG |
446 | description, |
447 | where, | |
70887c5f | 448 | timeout / USEC_PER_SEC); |
7a1494aa TG |
449 | |
450 | r = fflush_and_check(f); | |
451 | if (r < 0) | |
452 | return log_error_errno(r, "Failed to write unit file %s: %m", p); | |
453 | ||
9cdcf368 | 454 | return generator_add_symlink(arg_dest, SPECIAL_LOCAL_FS_TARGET, "wants", unit); |
7a1494aa TG |
455 | } |
456 | ||
4f084066 LP |
457 | static const char *esp_or_xbootldr_options(const DissectedPartition *p) { |
458 | assert(p); | |
459 | ||
460 | /* if we probed vfat or have no idea about the file system then assume these file systems are vfat | |
461 | * and thus understand "umask=0077". If we detected something else then don't specify any options and | |
462 | * use kernel defaults. */ | |
463 | ||
464 | if (!p->fstype || streq(p->fstype, "vfat")) | |
465 | return "umask=0077"; | |
466 | ||
467 | return NULL; | |
468 | } | |
469 | ||
9f1cb0c1 LP |
470 | static int add_xbootldr(DissectedPartition *p) { |
471 | int r; | |
472 | ||
473 | assert(p); | |
474 | ||
475 | if (in_initrd()) { | |
476 | log_debug("In initrd, ignoring the XBOOTLDR partition."); | |
477 | return 0; | |
478 | } | |
479 | ||
480 | r = fstab_is_mount_point("/boot"); | |
481 | if (r < 0) | |
482 | return log_error_errno(r, "Failed to parse fstab: %m"); | |
483 | if (r > 0) { | |
484 | log_debug("/boot specified in fstab, ignoring XBOOTLDR partition."); | |
485 | return 0; | |
486 | } | |
487 | ||
488 | r = path_is_busy("/boot"); | |
489 | if (r < 0) | |
490 | return r; | |
491 | if (r > 0) | |
492 | return 0; | |
493 | ||
494 | return add_automount("boot", | |
495 | p->node, | |
496 | "/boot", | |
497 | p->fstype, | |
400c1e8f LP |
498 | /* rw= */ true, |
499 | /* growfs= */ false, | |
4f084066 | 500 | esp_or_xbootldr_options(p), |
9f1cb0c1 LP |
501 | "Boot Loader Partition", |
502 | 120 * USEC_PER_SEC); | |
503 | } | |
504 | ||
505 | #if ENABLE_EFI | |
506 | static int add_esp(DissectedPartition *p, bool has_xbootldr) { | |
507 | const char *esp_path = NULL, *id = NULL; | |
59512f21 KS |
508 | int r; |
509 | ||
72e18a98 | 510 | assert(p); |
59512f21 | 511 | |
59512f21 | 512 | if (in_initrd()) { |
b52a109a | 513 | log_debug("In initrd, ignoring the ESP."); |
59512f21 KS |
514 | return 0; |
515 | } | |
516 | ||
9f1cb0c1 LP |
517 | /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice, but |
518 | * only if there's no explicit XBOOTLDR partition around. */ | |
519 | if (access("/efi", F_OK) < 0) { | |
520 | if (errno != ENOENT) | |
521 | return log_error_errno(errno, "Failed to determine whether /efi exists: %m"); | |
522 | ||
523 | /* Use /boot as fallback, but only if there's no XBOOTLDR partition */ | |
524 | if (!has_xbootldr) { | |
525 | esp_path = "/boot"; | |
526 | id = "boot"; | |
527 | } | |
528 | } | |
529 | if (!esp_path) | |
530 | esp_path = "/efi"; | |
531 | if (!id) | |
532 | id = "efi"; | |
59512f21 | 533 | |
0b6b6787 | 534 | /* We create an .automount which is not overridden by the .mount from the fstab generator. */ |
9f1cb0c1 | 535 | r = fstab_is_mount_point(esp_path); |
b9088048 FB |
536 | if (r < 0) |
537 | return log_error_errno(r, "Failed to parse fstab: %m"); | |
39b6a511 | 538 | if (r > 0) { |
9f1cb0c1 | 539 | log_debug("%s specified in fstab, ignoring.", esp_path); |
59512f21 KS |
540 | return 0; |
541 | } | |
542 | ||
9f1cb0c1 LP |
543 | r = path_is_busy(esp_path); |
544 | if (r < 0) | |
545 | return r; | |
546 | if (r > 0) | |
547 | return 0; | |
59512f21 | 548 | |
7ba25ab5 | 549 | if (is_efi_boot()) { |
72e18a98 | 550 | sd_id128_t loader_uuid; |
59512f21 | 551 | |
7ba25ab5 | 552 | /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */ |
59512f21 | 553 | |
7ba25ab5 LP |
554 | r = efi_loader_get_device_part_uuid(&loader_uuid); |
555 | if (r == -ENOENT) { | |
556 | log_debug("EFI loader partition unknown."); | |
557 | return 0; | |
558 | } | |
e28973ee ZJS |
559 | if (r < 0) |
560 | return log_error_errno(r, "Failed to read ESP partition UUID: %m"); | |
7ba25ab5 | 561 | |
72e18a98 | 562 | if (!sd_id128_equal(p->uuid, loader_uuid)) { |
9f1cb0c1 | 563 | log_debug("Partition for %s does not appear to be the partition we are booted from.", p->node); |
7ba25ab5 LP |
564 | return 0; |
565 | } | |
566 | } else | |
567 | log_debug("Not an EFI boot, skipping ESP check."); | |
568 | ||
9f1cb0c1 | 569 | return add_automount(id, |
72e18a98 | 570 | p->node, |
9f1cb0c1 | 571 | esp_path, |
72e18a98 | 572 | p->fstype, |
400c1e8f LP |
573 | /* rw= */ true, |
574 | /* growfs= */ false, | |
4f084066 | 575 | esp_or_xbootldr_options(p), |
72e18a98 LP |
576 | "EFI System Partition Automount", |
577 | 120 * USEC_PER_SEC); | |
7a1494aa | 578 | } |
59512f21 | 579 | #else |
9f1cb0c1 | 580 | static int add_esp(DissectedPartition *p, bool has_xbootldr) { |
59512f21 | 581 | return 0; |
59512f21 | 582 | } |
7a1494aa | 583 | #endif |
59512f21 | 584 | |
fd89051e LP |
585 | static int add_root_rw(DissectedPartition *p) { |
586 | const char *path; | |
587 | int r; | |
588 | ||
589 | assert(p); | |
590 | ||
591 | if (in_initrd()) { | |
592 | log_debug("In initrd, not generating drop-in for systemd-remount-fs.service."); | |
593 | return 0; | |
594 | } | |
595 | ||
596 | if (arg_root_rw >= 0) { | |
597 | log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service."); | |
598 | return 0; | |
599 | } | |
600 | ||
601 | if (!p->rw) { | |
602 | log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service."); | |
603 | return 0; | |
604 | } | |
605 | ||
9b69569d ZJS |
606 | (void) generator_enable_remount_fs_service(arg_dest); |
607 | ||
fd89051e | 608 | path = strjoina(arg_dest, "/systemd-remount-fs.service.d/50-remount-rw.conf"); |
fd89051e LP |
609 | |
610 | r = write_string_file(path, | |
611 | "# Automatically generated by systemd-gpt-generator\n\n" | |
fd89051e LP |
612 | "[Service]\n" |
613 | "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n", | |
e82e549f | 614 | WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW|WRITE_STRING_FILE_MKDIR_0755); |
fd89051e LP |
615 | if (r < 0) |
616 | return log_error_errno(r, "Failed to write drop-in file %s: %m", path); | |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
9fe6f5cc ZJS |
621 | #if ENABLE_EFI |
622 | static int add_root_cryptsetup(void) { | |
1a14a53c | 623 | |
9fe6f5cc ZJS |
624 | /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which |
625 | * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */ | |
1a14a53c | 626 | |
9fe6f5cc ZJS |
627 | return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL); |
628 | } | |
629 | #endif | |
d2a62382 | 630 | |
9fe6f5cc ZJS |
631 | static int add_root_mount(void) { |
632 | #if ENABLE_EFI | |
633 | int r; | |
1a14a53c | 634 | |
9fe6f5cc | 635 | if (!is_efi_boot()) { |
387f6955 | 636 | log_debug("Not an EFI boot, not creating root mount."); |
8090b41e | 637 | return 0; |
fa041593 | 638 | } |
61331eab | 639 | |
9fe6f5cc ZJS |
640 | r = efi_loader_get_device_part_uuid(NULL); |
641 | if (r == -ENOENT) { | |
b50a3a15 ZJS |
642 | log_notice("EFI loader partition unknown, exiting.\n" |
643 | "(The boot loader did not set EFI variable LoaderDevicePartUUID.)"); | |
8090b41e | 644 | return 0; |
9fe6f5cc ZJS |
645 | } else if (r < 0) |
646 | return log_error_errno(r, "Failed to read ESP partition UUID: %m"); | |
61331eab | 647 | |
9fe6f5cc ZJS |
648 | /* OK, we have an ESP partition, this is fantastic, so let's |
649 | * wait for a root device to show up. A udev rule will create | |
650 | * the link for us under the right name. */ | |
61331eab | 651 | |
9fe6f5cc ZJS |
652 | if (in_initrd()) { |
653 | r = generator_write_initrd_root_device_deps(arg_dest, "/dev/gpt-auto-root"); | |
654 | if (r < 0) | |
655 | return 0; | |
61331eab | 656 | |
9fe6f5cc ZJS |
657 | r = add_root_cryptsetup(); |
658 | if (r < 0) | |
659 | return r; | |
61331eab LP |
660 | } |
661 | ||
9fe6f5cc ZJS |
662 | /* Note that we do not need to enable systemd-remount-fs.service here. If |
663 | * /etc/fstab exists, systemd-fstab-generator will pull it in for us. */ | |
61331eab | 664 | |
9fe6f5cc ZJS |
665 | return add_mount( |
666 | "root", | |
667 | "/dev/gpt-auto-root", | |
668 | in_initrd() ? "/sysroot" : "/", | |
669 | NULL, | |
400c1e8f LP |
670 | /* rw= */ arg_root_rw > 0, |
671 | /* growfs= */ false, | |
9fe6f5cc ZJS |
672 | NULL, |
673 | "Root Partition", | |
674 | in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET); | |
675 | #else | |
676 | return 0; | |
677 | #endif | |
72e18a98 | 678 | } |
cb971249 | 679 | |
72e18a98 | 680 | static int enumerate_partitions(dev_t devnum) { |
72e18a98 LP |
681 | _cleanup_close_ int fd = -1; |
682 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
683 | int r, k; | |
61331eab | 684 | |
3d92aa45 | 685 | r = open_parent_block_device(devnum, &fd); |
72e18a98 LP |
686 | if (r <= 0) |
687 | return r; | |
61331eab | 688 | |
d04faa4e LP |
689 | r = dissect_image( |
690 | fd, | |
691 | NULL, NULL, | |
75dc190d | 692 | UINT64_MAX, |
4a62257d | 693 | USEC_INFINITY, |
d04faa4e LP |
694 | DISSECT_IMAGE_GPT_ONLY| |
695 | DISSECT_IMAGE_NO_UDEV| | |
696 | DISSECT_IMAGE_USR_NO_ROOT, | |
697 | &m); | |
72e18a98 LP |
698 | if (r == -ENOPKG) { |
699 | log_debug_errno(r, "No suitable partition table found, ignoring."); | |
700 | return 0; | |
61331eab | 701 | } |
23bbb0de | 702 | if (r < 0) |
72e18a98 | 703 | return log_error_errno(r, "Failed to dissect: %m"); |
0238d4c6 | 704 | |
72e18a98 LP |
705 | if (m->partitions[PARTITION_SWAP].found) { |
706 | k = add_swap(m->partitions[PARTITION_SWAP].node); | |
707 | if (k < 0) | |
708 | r = k; | |
1a14a53c LP |
709 | } |
710 | ||
9f1cb0c1 LP |
711 | if (m->partitions[PARTITION_XBOOTLDR].found) { |
712 | k = add_xbootldr(m->partitions + PARTITION_XBOOTLDR); | |
713 | if (k < 0) | |
714 | r = k; | |
715 | } | |
716 | ||
72e18a98 | 717 | if (m->partitions[PARTITION_ESP].found) { |
9f1cb0c1 | 718 | k = add_esp(m->partitions + PARTITION_ESP, m->partitions[PARTITION_XBOOTLDR].found); |
59512f21 KS |
719 | if (k < 0) |
720 | r = k; | |
721 | } | |
722 | ||
72e18a98 LP |
723 | if (m->partitions[PARTITION_HOME].found) { |
724 | k = add_partition_mount(m->partitions + PARTITION_HOME, "home", "/home", "Home Partition"); | |
73b80ec2 LP |
725 | if (k < 0) |
726 | r = k; | |
727 | } | |
e48fdd84 | 728 | |
72e18a98 LP |
729 | if (m->partitions[PARTITION_SRV].found) { |
730 | k = add_partition_mount(m->partitions + PARTITION_SRV, "srv", "/srv", "Server Data Partition"); | |
73b80ec2 LP |
731 | if (k < 0) |
732 | r = k; | |
733 | } | |
1a14a53c | 734 | |
d4dffb85 LP |
735 | if (m->partitions[PARTITION_VAR].found) { |
736 | k = add_partition_mount(m->partitions + PARTITION_VAR, "var", "/var", "Variable Data Partition"); | |
737 | if (k < 0) | |
738 | r = k; | |
739 | } | |
740 | ||
741 | if (m->partitions[PARTITION_TMP].found) { | |
742 | k = add_partition_mount(m->partitions + PARTITION_TMP, "var-tmp", "/var/tmp", "Temporary Data Partition"); | |
743 | if (k < 0) | |
744 | r = k; | |
745 | } | |
746 | ||
fd89051e LP |
747 | if (m->partitions[PARTITION_ROOT].found) { |
748 | k = add_root_rw(m->partitions + PARTITION_ROOT); | |
749 | if (k < 0) | |
750 | r = k; | |
751 | } | |
752 | ||
1a14a53c LP |
753 | return r; |
754 | } | |
755 | ||
9fe6f5cc ZJS |
756 | static int add_mounts(void) { |
757 | dev_t devno; | |
758 | int r; | |
759 | ||
760 | r = get_block_device_harder("/", &devno); | |
67f0ac8c LP |
761 | if (r == -EUCLEAN) |
762 | return btrfs_log_dev_root(LOG_ERR, r, "root file system"); | |
9fe6f5cc ZJS |
763 | if (r < 0) |
764 | return log_error_errno(r, "Failed to determine block device of root file system: %m"); | |
d161680e | 765 | if (r == 0) { /* Not backed by block device */ |
9fe6f5cc | 766 | r = get_block_device_harder("/usr", &devno); |
67f0ac8c LP |
767 | if (r == -EUCLEAN) |
768 | return btrfs_log_dev_root(LOG_ERR, r, "/usr"); | |
9fe6f5cc ZJS |
769 | if (r < 0) |
770 | return log_error_errno(r, "Failed to determine block device of /usr file system: %m"); | |
771 | if (r == 0) { | |
772 | _cleanup_free_ char *p = NULL; | |
773 | mode_t m; | |
774 | ||
775 | /* If the root mount has been replaced by some form of volatile file system (overlayfs), the | |
776 | * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that | |
777 | * here. */ | |
778 | r = readlink_malloc("/run/systemd/volatile-root", &p); | |
779 | if (r == -ENOENT) { | |
780 | log_debug("Neither root nor /usr file system are on a (single) block device."); | |
781 | return 0; | |
782 | } | |
783 | if (r < 0) | |
784 | return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m"); | |
785 | ||
786 | r = device_path_parse_major_minor(p, &m, &devno); | |
787 | if (r < 0) | |
788 | return log_error_errno(r, "Failed to parse major/minor device node: %m"); | |
789 | if (!S_ISBLK(m)) | |
790 | return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type."); | |
791 | } | |
792 | } | |
793 | ||
794 | return enumerate_partitions(devno); | |
795 | } | |
796 | ||
96287a49 | 797 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
73b80ec2 | 798 | int r; |
1a14a53c | 799 | |
73b80ec2 | 800 | assert(key); |
1a14a53c | 801 | |
8a9c44ed LP |
802 | if (proc_cmdline_key_streq(key, "systemd.gpt_auto") || |
803 | proc_cmdline_key_streq(key, "rd.systemd.gpt_auto")) { | |
1a14a53c | 804 | |
1d84ad94 | 805 | r = value ? parse_boolean(value) : 1; |
73b80ec2 | 806 | if (r < 0) |
0a1b9449 | 807 | log_warning_errno(r, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value); |
8086ffac ZJS |
808 | else |
809 | arg_enabled = r; | |
1a14a53c | 810 | |
8a9c44ed | 811 | } else if (proc_cmdline_key_streq(key, "root")) { |
1d84ad94 LP |
812 | |
813 | if (proc_cmdline_value_missing(key, value)) | |
814 | return 0; | |
73b80ec2 LP |
815 | |
816 | /* Disable root disk logic if there's a root= value | |
817 | * specified (unless it happens to be "gpt-auto") */ | |
818 | ||
074cdb95 ZJS |
819 | if (!streq(value, "gpt-auto")) { |
820 | arg_root_enabled = false; | |
821 | log_debug("Disabling root partition auto-detection, root= is defined."); | |
822 | } | |
73b80ec2 | 823 | |
8a9c44ed | 824 | } else if (proc_cmdline_key_streq(key, "roothash")) { |
2f3dfc6f LP |
825 | |
826 | if (proc_cmdline_value_missing(key, value)) | |
827 | return 0; | |
828 | ||
829 | /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */ | |
830 | ||
831 | arg_root_enabled = false; | |
832 | ||
8a9c44ed | 833 | } else if (proc_cmdline_key_streq(key, "rw") && !value) |
73b80ec2 | 834 | arg_root_rw = true; |
8a9c44ed | 835 | else if (proc_cmdline_key_streq(key, "ro") && !value) |
73b80ec2 | 836 | arg_root_rw = false; |
73b80ec2 LP |
837 | |
838 | return 0; | |
839 | } | |
840 | ||
ec6e9597 | 841 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
8f50e86a | 842 | int r, k; |
73b80ec2 | 843 | |
ec6e9597 | 844 | assert_se(arg_dest = dest_late); |
73b80ec2 | 845 | |
75f86906 | 846 | if (detect_container() > 0) { |
73b80ec2 | 847 | log_debug("In a container, exiting."); |
ec6e9597 | 848 | return 0; |
1a14a53c | 849 | } |
3db604b9 | 850 | |
1d84ad94 | 851 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
b5884878 | 852 | if (r < 0) |
da927ba9 | 853 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
1a14a53c | 854 | |
73b80ec2 LP |
855 | if (!arg_enabled) { |
856 | log_debug("Disabled, exiting."); | |
ec6e9597 | 857 | return 0; |
73b80ec2 LP |
858 | } |
859 | ||
860 | if (arg_root_enabled) | |
861 | r = add_root_mount(); | |
862 | ||
863 | if (!in_initrd()) { | |
73b80ec2 | 864 | k = add_mounts(); |
ec6e9597 | 865 | if (r >= 0) |
73b80ec2 LP |
866 | r = k; |
867 | } | |
868 | ||
ec6e9597 | 869 | return r; |
1a14a53c | 870 | } |
ec6e9597 ZJS |
871 | |
872 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |