]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/gpt-auto-generator/gpt-auto-generator.c
Merge pull request #31181 from fbuihuu/gpt-auto-more-defensive
[thirdparty/systemd.git] / src / gpt-auto-generator / gpt-auto-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/file.h>
5 #include <unistd.h>
6
7 #include "sd-device.h"
8 #include "sd-id128.h"
9
10 #include "alloc-util.h"
11 #include "blkid-util.h"
12 #include "blockdev-util.h"
13 #include "btrfs-util.h"
14 #include "device-util.h"
15 #include "devnum-util.h"
16 #include "dirent-util.h"
17 #include "dissect-image.h"
18 #include "dropin.h"
19 #include "efi-loader.h"
20 #include "fd-util.h"
21 #include "fileio.h"
22 #include "fs-util.h"
23 #include "fstab-util.h"
24 #include "generator.h"
25 #include "gpt.h"
26 #include "image-policy.h"
27 #include "initrd-util.h"
28 #include "mountpoint-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "proc-cmdline.h"
32 #include "special.h"
33 #include "specifier.h"
34 #include "stat-util.h"
35 #include "string-util.h"
36 #include "strv.h"
37 #include "unit-name.h"
38 #include "virt.h"
39
40 static const char *arg_dest = NULL;
41 static bool arg_enabled = true;
42 static bool arg_root_enabled = true;
43 static bool arg_swap_enabled = true;
44 static char *arg_root_fstype = NULL;
45 static char *arg_root_options = NULL;
46 static int arg_root_rw = -1;
47 static ImagePolicy *arg_image_policy = NULL;
48
49 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
50
51 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
52 STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
53
54 static int add_cryptsetup(
55 const char *id,
56 const char *what,
57 bool rw,
58 bool require,
59 bool measure,
60 char **ret_device) {
61
62 #if HAVE_LIBCRYPTSETUP
63 _cleanup_free_ char *e = NULL, *n = NULL, *d = NULL, *options = NULL;
64 _cleanup_fclose_ FILE *f = NULL;
65 int r;
66
67 assert(id);
68 assert(what);
69
70 r = unit_name_from_path(what, ".device", &d);
71 if (r < 0)
72 return log_error_errno(r, "Failed to generate unit name: %m");
73
74 e = unit_name_escape(id);
75 if (!e)
76 return log_oom();
77
78 r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
79 if (r < 0)
80 return log_error_errno(r, "Failed to generate unit name: %m");
81
82 r = generator_open_unit_file(arg_dest, /* source = */ NULL, n, &f);
83 if (r < 0)
84 return r;
85
86 r = generator_write_cryptsetup_unit_section(f, NULL);
87 if (r < 0)
88 return r;
89
90 fprintf(f,
91 "Before=umount.target cryptsetup.target\n"
92 "Conflicts=umount.target\n"
93 "BindsTo=%s\n"
94 "After=%s\n",
95 d, d);
96
97 if (!rw) {
98 options = strdup("read-only");
99 if (!options)
100 return log_oom();
101 }
102
103 r = efi_measured_uki(LOG_WARNING);
104 if (r > 0)
105 /* Enable TPM2 based unlocking automatically, if we have a TPM. See #30176. */
106 if (!strextend_with_separator(&options, ",", "tpm2-device=auto"))
107 return log_oom();
108
109 if (measure) {
110 /* We only measure the root volume key into PCR 15 if we are booted with sd-stub (i.e. in a
111 * UKI), and sd-stub measured the UKI. We do this in order not to step into people's own PCR
112 * assignment, under the assumption that people who are fine to use sd-stub with its PCR
113 * assignments are also OK with our PCR 15 use here. */
114 if (r > 0)
115 if (!strextend_with_separator(&options, ",", "tpm2-measure-pcr=yes"))
116 return log_oom();
117 if (r == 0)
118 log_debug("Will not measure volume key of volume '%s', not booted via systemd-stub with measurements enabled.", id);
119 }
120
121 r = generator_write_cryptsetup_service_section(f, id, what, NULL, options);
122 if (r < 0)
123 return r;
124
125 r = fflush_and_check(f);
126 if (r < 0)
127 return log_error_errno(r, "Failed to write file %s: %m", n);
128
129 r = generator_add_symlink(arg_dest, d, "wants", n);
130 if (r < 0)
131 return r;
132
133 const char *dmname = strjoina("dev-mapper-", e, ".device");
134
135 if (require) {
136 r = generator_add_symlink(arg_dest, "cryptsetup.target", "requires", n);
137 if (r < 0)
138 return r;
139
140 r = generator_add_symlink(arg_dest, dmname, "requires", n);
141 if (r < 0)
142 return r;
143 }
144
145 r = write_drop_in_format(arg_dest, dmname, 50, "job-timeout",
146 "# Automatically generated by systemd-gpt-auto-generator\n\n"
147 "[Unit]\n"
148 "JobTimeoutSec=infinity"); /* the binary handles timeouts anyway */
149 if (r < 0)
150 log_warning_errno(r, "Failed to write device timeout drop-in, ignoring: %m");
151
152 if (ret_device) {
153 char *s;
154
155 s = path_join("/dev/mapper", id);
156 if (!s)
157 return log_oom();
158
159 *ret_device = s;
160 }
161
162 return 0;
163 #else
164 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
165 "Partition is encrypted, but systemd-gpt-auto-generator was compiled without libcryptsetup support");
166 #endif
167 }
168
169 static int add_mount(
170 const char *id,
171 const char *what,
172 const char *where,
173 const char *fstype,
174 bool rw,
175 bool growfs,
176 bool measure,
177 const char *options,
178 const char *description,
179 const char *post) {
180
181 _cleanup_free_ char *unit = NULL, *crypto_what = NULL;
182 _cleanup_fclose_ FILE *f = NULL;
183 int r;
184
185 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
186 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
187 * could potentially be understood as specifiers. */
188
189 assert(id);
190 assert(what);
191 assert(where);
192 assert(description);
193
194 log_debug("Adding %s: %s fstype=%s", where, what, fstype ?: "(any)");
195
196 if (streq_ptr(fstype, "crypto_LUKS")) {
197 r = add_cryptsetup(id, what, rw, /* require= */ true, measure, &crypto_what);
198 if (r < 0)
199 return r;
200
201 what = crypto_what;
202 fstype = NULL;
203 } else if (fstype) {
204 r = dissect_fstype_ok(fstype);
205 if (r < 0)
206 return log_error_errno(r, "Unable to determine of dissected file system type '%s' is permitted: %m", fstype);
207 if (!r)
208 return log_error_errno(
209 SYNTHETIC_ERRNO(EIDRM),
210 "Refusing to automatically mount uncommon file system '%s' to '%s'.",
211 fstype, where);
212 }
213
214 r = unit_name_from_path(where, ".mount", &unit);
215 if (r < 0)
216 return log_error_errno(r, "Failed to generate unit name: %m");
217
218 r = generator_open_unit_file(arg_dest, /* source = */ NULL, unit, &f);
219 if (r < 0)
220 return r;
221
222 fprintf(f,
223 "[Unit]\n"
224 "Description=%s\n"
225 "Documentation=man:systemd-gpt-auto-generator(8)\n",
226 description);
227
228 if (post)
229 fprintf(f, "Before=%s\n", post);
230
231 r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
232 if (r < 0)
233 return r;
234
235 r = generator_write_blockdev_dependency(f, what);
236 if (r < 0)
237 return r;
238
239 fprintf(f,
240 "\n"
241 "[Mount]\n"
242 "What=%s\n"
243 "Where=%s\n",
244 what, where);
245
246 if (fstype)
247 fprintf(f, "Type=%s\n", fstype);
248
249 if (options)
250 fprintf(f, "Options=%s\n", options);
251
252 r = fflush_and_check(f);
253 if (r < 0)
254 return log_error_errno(r, "Failed to write unit %s: %m", unit);
255
256 if (growfs) {
257 r = generator_hook_up_growfs(arg_dest, where, post);
258 if (r < 0)
259 return r;
260 }
261
262 if (measure) {
263 r = generator_hook_up_pcrfs(arg_dest, where, post);
264 if (r < 0)
265 return r;
266 }
267
268 if (post) {
269 r = generator_add_symlink(arg_dest, post, "requires", unit);
270 if (r < 0)
271 return r;
272 }
273
274 return 0;
275 }
276
277 static int path_is_busy(const char *where) {
278 int r;
279
280 assert(where);
281
282 /* already a mountpoint; generators run during reload */
283 r = path_is_mount_point_full(where, /* root = */ NULL, AT_SYMLINK_FOLLOW);
284 if (r > 0)
285 return false;
286 /* The directory will be created by the mount or automount unit when it is started. */
287 if (r == -ENOENT)
288 return false;
289
290 if (r < 0)
291 return log_warning_errno(r, "Cannot check if \"%s\" is a mount point: %m", where);
292
293 /* not a mountpoint but it contains files */
294 r = dir_is_empty(where, /* ignore_hidden_or_backup= */ false);
295 if (r == -ENOTDIR) {
296 log_debug("\"%s\" is not a directory, ignoring.", where);
297 return true;
298 } else if (r < 0)
299 return log_warning_errno(r, "Cannot check if \"%s\" is empty: %m", where);
300 else if (r == 0) {
301 log_debug("\"%s\" already populated, ignoring.", where);
302 return true;
303 }
304
305 return false;
306 }
307
308 static int add_partition_mount(
309 PartitionDesignator d,
310 DissectedPartition *p,
311 const char *id,
312 const char *where,
313 const char *description) {
314
315 _cleanup_free_ char *options = NULL;
316 int r;
317
318 assert(p);
319
320 r = path_is_busy(where);
321 if (r != 0)
322 return r < 0 ? r : 0;
323
324 r = partition_pick_mount_options(
325 d,
326 dissected_partition_fstype(p),
327 p->rw,
328 /* discard= */ true,
329 &options,
330 /* ret_ms_flags= */ NULL);
331 if (r < 0)
332 return r;
333
334 return add_mount(
335 id,
336 p->node,
337 where,
338 p->fstype,
339 p->rw,
340 p->growfs,
341 /* measure= */ STR_IN_SET(id, "root", "var"), /* by default measure rootfs and /var, since they contain the "identity" of the system */
342 options,
343 description,
344 SPECIAL_LOCAL_FS_TARGET);
345 }
346
347 static int add_partition_swap(DissectedPartition *p) {
348 const char *what;
349 _cleanup_free_ char *name = NULL, *crypto_what = NULL;
350 _cleanup_fclose_ FILE *f = NULL;
351 int r;
352
353 assert(p);
354 assert(p->node);
355
356 if (!arg_swap_enabled)
357 return 0;
358
359 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
360 r = fstab_has_fstype("swap");
361 if (r < 0)
362 return log_error_errno(r, "Failed to parse fstab: %m");
363 if (r > 0) {
364 log_debug("swap specified in fstab, ignoring.");
365 return 0;
366 }
367
368 if (streq_ptr(p->fstype, "crypto_LUKS")) {
369 r = add_cryptsetup("swap", p->node, /* rw= */ true, /* require= */ true, /* measure= */ false, &crypto_what);
370 if (r < 0)
371 return r;
372 what = crypto_what;
373 } else
374 what = p->node;
375
376 log_debug("Adding swap: %s", what);
377
378 r = unit_name_from_path(what, ".swap", &name);
379 if (r < 0)
380 return log_error_errno(r, "Failed to generate unit name: %m");
381
382 r = generator_open_unit_file(arg_dest, /* source = */ NULL, name, &f);
383 if (r < 0)
384 return r;
385
386 fprintf(f,
387 "[Unit]\n"
388 "Description=Swap Partition\n"
389 "Documentation=man:systemd-gpt-auto-generator(8)\n");
390
391 r = generator_write_blockdev_dependency(f, what);
392 if (r < 0)
393 return r;
394
395 fprintf(f,
396 "\n"
397 "[Swap]\n"
398 "What=%s\n",
399 what);
400
401 r = fflush_and_check(f);
402 if (r < 0)
403 return log_error_errno(r, "Failed to write unit %s: %m", name);
404
405 return generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, "wants", name);
406 }
407
408 static int add_automount(
409 const char *id,
410 const char *what,
411 const char *where,
412 const char *fstype,
413 bool rw,
414 bool growfs,
415 const char *options,
416 const char *description,
417 usec_t timeout) {
418
419 _cleanup_free_ char *unit = NULL;
420 _cleanup_fclose_ FILE *f = NULL;
421 int r;
422
423 assert(id);
424 assert(where);
425 assert(description);
426
427 r = add_mount(id,
428 what,
429 where,
430 fstype,
431 rw,
432 growfs,
433 /* measure= */ false,
434 options,
435 description,
436 NULL);
437 if (r < 0)
438 return r;
439
440 r = unit_name_from_path(where, ".automount", &unit);
441 if (r < 0)
442 return log_error_errno(r, "Failed to generate unit name: %m");
443
444 r = generator_open_unit_file(arg_dest, /* source = */ NULL, unit, &f);
445 if (r < 0)
446 return r;
447
448 fprintf(f,
449 "[Unit]\n"
450 "Description=%s\n"
451 "Documentation=man:systemd-gpt-auto-generator(8)\n"
452 "[Automount]\n"
453 "Where=%s\n"
454 "TimeoutIdleSec="USEC_FMT"\n",
455 description,
456 where,
457 timeout / USEC_PER_SEC);
458
459 r = fflush_and_check(f);
460 if (r < 0)
461 return log_error_errno(r, "Failed to write unit %s: %m", unit);
462
463 return generator_add_symlink(arg_dest, SPECIAL_LOCAL_FS_TARGET, "wants", unit);
464 }
465
466 static int add_partition_xbootldr(DissectedPartition *p) {
467 _cleanup_free_ char *options = NULL;
468 int r;
469
470 assert(p);
471
472 if (in_initrd()) {
473 log_debug("In initrd, ignoring the XBOOTLDR partition.");
474 return 0;
475 }
476
477 r = path_is_busy("/boot");
478 if (r < 0)
479 return r;
480 if (r > 0)
481 return 0;
482
483 r = partition_pick_mount_options(
484 PARTITION_XBOOTLDR,
485 dissected_partition_fstype(p),
486 /* rw= */ true,
487 /* discard= */ false,
488 &options,
489 /* ret_ms_flags= */ NULL);
490 if (r < 0)
491 return log_error_errno(r, "Failed to determine default mount options for /boot/: %m");
492
493 return add_automount(
494 "boot",
495 p->node,
496 "/boot",
497 p->fstype,
498 /* rw= */ true,
499 /* growfs= */ false,
500 options,
501 "Boot Loader Partition",
502 120 * USEC_PER_SEC);
503 }
504
505 #if ENABLE_EFI
506 static bool slash_boot_exists(void) {
507 static int cache = -1;
508
509 if (cache >= 0)
510 return cache;
511
512 if (access("/boot", F_OK) >= 0)
513 return (cache = true);
514 if (errno != ENOENT)
515 log_error_errno(errno, "Failed to determine whether /boot/ exists, assuming no: %m");
516 else
517 log_debug_errno(errno, "/boot/: %m");
518 return (cache = false);
519 }
520
521 static int add_partition_esp(DissectedPartition *p, bool has_xbootldr) {
522 const char *esp_path = NULL, *id = NULL;
523 _cleanup_free_ char *options = NULL;
524 int r;
525
526 assert(p);
527
528 if (in_initrd()) {
529 log_debug("In initrd, ignoring the ESP.");
530 return 0;
531 }
532
533 /* Check if there's an existing fstab entry for ESP. If so, we just skip the gpt-auto logic. */
534 r = fstab_has_node(p->node);
535 if (r < 0)
536 return log_error_errno(r,
537 "Failed to check if fstab entry for device '%s' exists: %m", p->node);
538 if (r > 0)
539 return 0;
540
541 /* If /boot/ is present, unused, and empty, we'll take that.
542 * Otherwise, if /efi/ is unused and empty (or missing), we'll take that.
543 * Otherwise, we do nothing. */
544 if (!has_xbootldr && slash_boot_exists()) {
545 r = path_is_busy("/boot");
546 if (r < 0)
547 return r;
548 if (r == 0) {
549 esp_path = "/boot";
550 id = "boot";
551 }
552 }
553
554 if (!esp_path) {
555 r = path_is_busy("/efi");
556 if (r < 0)
557 return r;
558 if (r > 0)
559 return 0;
560
561 esp_path = "/efi";
562 id = "efi";
563 }
564
565 r = partition_pick_mount_options(
566 PARTITION_ESP,
567 dissected_partition_fstype(p),
568 /* rw= */ true,
569 /* discard= */ false,
570 &options,
571 /* ret_ms_flags= */ NULL);
572 if (r < 0)
573 return log_error_errno(r, "Failed to determine default mount options for %s: %m", esp_path);
574
575 return add_automount(
576 id,
577 p->node,
578 esp_path,
579 p->fstype,
580 /* rw= */ true,
581 /* growfs= */ false,
582 options,
583 "EFI System Partition Automount",
584 120 * USEC_PER_SEC);
585 }
586 #else
587 static int add_partition_esp(DissectedPartition *p, bool has_xbootldr) {
588 return 0;
589 }
590 #endif
591
592 static int add_partition_root_rw(DissectedPartition *p) {
593 const char *path;
594 int r;
595
596 assert(p);
597 assert(!in_initrd());
598
599 /* Invoked on the main system (not initrd), to honour GPT flag 60 on the root fs (ro) */
600
601 if (arg_root_rw >= 0) {
602 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
603 return 0;
604 }
605
606 if (!p->rw) {
607 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
608 return 0;
609 }
610
611 r = generator_enable_remount_fs_service(arg_dest);
612 if (r < 0)
613 return r;
614
615 path = strjoina(arg_dest, "/systemd-remount-fs.service.d/50-remount-rw.conf");
616
617 r = write_string_file(path,
618 "# Automatically generated by systemd-gpt-auto-generator\n\n"
619 "[Service]\n"
620 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
621 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW|WRITE_STRING_FILE_MKDIR_0755);
622 if (r < 0)
623 return log_error_errno(r, "Failed to write drop-in file %s: %m", path);
624
625 return 0;
626 }
627
628 static int add_partition_root_growfs(DissectedPartition *p) {
629
630 assert(p);
631 assert(!in_initrd());
632
633 /* Invoked on the main system (not initrd), to honour GPT flag 59 on the root fs (growfs) */
634
635 if (!p->growfs) {
636 log_debug("Root partition not marked for growing the file system in the GPT partition table, not generating drop-in for systemd-growfs-root.service.");
637 return 0;
638 }
639
640 return generator_hook_up_growfs(arg_dest, "/", SPECIAL_LOCAL_FS_TARGET);
641 }
642
643 static int add_partition_root_flags(DissectedPartition *p) {
644 int r = 0;
645
646 assert(p);
647 assert(!in_initrd());
648
649 RET_GATHER(r, add_partition_root_growfs(p));
650 RET_GATHER(r, add_partition_root_rw(p));
651
652 return r;
653 }
654
655 #if ENABLE_EFI
656 static int add_root_cryptsetup(void) {
657 #if HAVE_LIBCRYPTSETUP
658
659 /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which
660 * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */
661
662 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", /* rw= */ true, /* require= */ false, /* measure= */ true, NULL);
663 #else
664 return 0;
665 #endif
666 }
667 #endif
668
669 static int add_root_mount(void) {
670 #if ENABLE_EFI
671 _cleanup_free_ char *options = NULL;
672 int r;
673
674 if (!is_efi_boot()) {
675 log_debug("Not an EFI boot, not creating root mount.");
676 return 0;
677 }
678
679 r = efi_loader_get_device_part_uuid(NULL);
680 if (r == -ENOENT) {
681 log_notice("EFI loader partition unknown, exiting.\n"
682 "(The boot loader did not set EFI variable LoaderDevicePartUUID.)");
683 return 0;
684 } else if (r < 0)
685 return log_error_errno(r, "Failed to read loader partition UUID: %m");
686
687 /* OK, we have an ESP/XBOOTLDR partition, this is fantastic, so let's wait for a root device to show up.
688 * A udev rule will create the link for us under the right name. */
689
690 if (in_initrd()) {
691 r = generator_write_initrd_root_device_deps(arg_dest, "/dev/gpt-auto-root");
692 if (r < 0)
693 return 0;
694
695 r = add_root_cryptsetup();
696 if (r < 0)
697 return r;
698 }
699
700 /* Note that we do not need to enable systemd-remount-fs.service here. If /etc/fstab exists,
701 * systemd-fstab-generator will pull it in for us, and otherwise add_partition_root_flags() will do
702 * it, after the initrd transition. */
703
704 r = partition_pick_mount_options(
705 PARTITION_ROOT,
706 arg_root_fstype,
707 arg_root_rw > 0,
708 /* discard= */ true,
709 &options,
710 /* ret_ms_flags= */ NULL);
711 if (r < 0)
712 return log_error_errno(r, "Failed to pick root mount options: %m");
713
714 if (arg_root_options)
715 if (!strextend_with_separator(&options, ",", arg_root_options))
716 return log_oom();
717
718 return add_mount(
719 "root",
720 "/dev/gpt-auto-root",
721 in_initrd() ? "/sysroot" : "/",
722 arg_root_fstype,
723 /* rw= */ arg_root_rw > 0,
724 /* growfs= */ false,
725 /* measure= */ true,
726 options,
727 "Root Partition",
728 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET);
729 #else
730 return 0;
731 #endif
732 }
733
734 static int process_loader_partitions(DissectedPartition *esp, DissectedPartition *xbootldr) {
735 sd_id128_t loader_uuid;
736 int r;
737
738 assert(esp);
739 assert(xbootldr);
740
741 /* If any paths in fstab look similar to our favorite paths for ESP or XBOOTLDR, we just exit
742 * early. We also don't bother with cases where one is configured explicitly and the other shall be
743 * mounted automatically. */
744
745 r = fstab_has_mount_point_prefix_strv(STRV_MAKE("/boot", "/efi"));
746 if (r > 0) {
747 log_debug("Found mount entries in the /boot/ or /efi/ hierarchies in fstab, not generating ESP or XBOOTLDR mounts.");
748 return 0;
749 }
750 if (r < 0)
751 log_debug_errno(r, "Failed to check fstab existing paths, ignoring: %m");
752
753 if (!is_efi_boot()) {
754 log_debug("Not an EFI boot, skipping loader partition UUID check.");
755 goto mount;
756 }
757
758 /* Let's check if LoaderDevicePartUUID points to either ESP or XBOOTLDR. We prefer it pointing
759 * to the ESP, but we accept XBOOTLDR too. If it points to neither of them, don't mount any
760 * loader partitions, since they are not the ones used for booting. */
761
762 r = efi_loader_get_device_part_uuid(&loader_uuid);
763 if (r == -ENOENT) {
764 log_debug_errno(r, "EFI loader partition unknown, skipping ESP and XBOOTLDR mounts.");
765 return 0;
766 }
767 if (r < 0)
768 return log_debug_errno(r, "Failed to read loader partition UUID, ignoring: %m");
769
770 if (esp->found && sd_id128_equal(esp->uuid, loader_uuid))
771 goto mount;
772
773 if (xbootldr->found && sd_id128_equal(xbootldr->uuid, loader_uuid)) {
774 log_debug("LoaderDevicePartUUID points to XBOOTLDR partition.");
775 goto mount;
776 }
777
778 log_debug("LoaderDevicePartUUID points to neither ESP nor XBOOTLDR, ignoring.");
779 return 0;
780
781 mount:
782 r = 0;
783
784 if (xbootldr->found)
785 RET_GATHER(r, add_partition_xbootldr(xbootldr));
786 if (esp->found)
787 RET_GATHER(r, add_partition_esp(esp, xbootldr->found));
788
789 return r;
790 }
791
792 static int enumerate_partitions(dev_t devnum) {
793 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
794 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
795 _cleanup_free_ char *devname = NULL;
796 int r;
797
798 assert(!in_initrd());
799
800 /* Run on the final root fs (not in the initrd), to mount auxiliary partitions, and hook in rw
801 * remount and growfs of the root partition */
802
803 r = block_get_whole_disk(devnum, &devnum);
804 if (r < 0)
805 return log_debug_errno(r, "Failed to get whole block device for " DEVNUM_FORMAT_STR ": %m",
806 DEVNUM_FORMAT_VAL(devnum));
807
808 r = devname_from_devnum(S_IFBLK, devnum, &devname);
809 if (r < 0)
810 return log_debug_errno(r, "Failed to get device node of " DEVNUM_FORMAT_STR ": %m",
811 DEVNUM_FORMAT_VAL(devnum));
812
813 /* Let's take a LOCK_SH lock on the block device, in case udevd is already running. If we don't take
814 * the lock, udevd might end up issuing BLKRRPART in the middle, and we don't want that, since that
815 * might remove all partitions while we are operating on them. */
816 r = loop_device_open_from_path(devname, O_RDONLY, LOCK_SH, &loop);
817 if (r < 0)
818 return log_debug_errno(r, "Failed to open %s: %m", devname);
819
820 r = dissect_loop_device(
821 loop,
822 /* verity= */ NULL,
823 /* mount_options= */ NULL,
824 arg_image_policy ?: &image_policy_host,
825 DISSECT_IMAGE_GPT_ONLY|
826 DISSECT_IMAGE_USR_NO_ROOT|
827 DISSECT_IMAGE_DISKSEQ_DEVNODE|
828 DISSECT_IMAGE_ALLOW_EMPTY,
829 /* NB! Unlike most other places where we dissect block devices we do not use
830 * DISSECT_IMAGE_ADD_PARTITION_DEVICES here: we want that the kernel finds the
831 * devices, and udev probes them before we mount them via .mount units much later
832 * on. And thus we also don't set DISSECT_IMAGE_PIN_PARTITION_DEVICES here, because
833 * we don't actually mount anything immediately. */
834 &m);
835 if (r < 0) {
836 bool ok = r == -ENOPKG;
837 dissect_log_error(ok ? LOG_DEBUG : LOG_ERR, r, devname, NULL);
838 return ok ? 0 : r;
839 }
840
841 if (m->partitions[PARTITION_SWAP].found)
842 RET_GATHER(r, add_partition_swap(m->partitions + PARTITION_SWAP));
843
844 RET_GATHER(r, process_loader_partitions(m->partitions + PARTITION_ESP, m->partitions + PARTITION_XBOOTLDR));
845
846 if (m->partitions[PARTITION_HOME].found)
847 RET_GATHER(r, add_partition_mount(PARTITION_HOME, m->partitions + PARTITION_HOME,
848 "home", "/home", "Home Partition"));
849
850 if (m->partitions[PARTITION_SRV].found)
851 RET_GATHER(r, add_partition_mount(PARTITION_SRV, m->partitions + PARTITION_SRV,
852 "srv", "/srv", "Server Data Partition"));
853
854 if (m->partitions[PARTITION_VAR].found)
855 RET_GATHER(r, add_partition_mount(PARTITION_VAR, m->partitions + PARTITION_VAR,
856 "var", "/var", "Variable Data Partition"));
857
858 if (m->partitions[PARTITION_TMP].found)
859 RET_GATHER(r, add_partition_mount(PARTITION_TMP, m->partitions + PARTITION_TMP,
860 "var-tmp", "/var/tmp", "Temporary Data Partition"));
861
862 if (m->partitions[PARTITION_ROOT].found)
863 RET_GATHER(r, add_partition_root_flags(m->partitions + PARTITION_ROOT));
864
865 return r;
866 }
867
868 static int add_mounts(void) {
869 dev_t devno;
870 int r;
871
872 r = blockdev_get_root(LOG_ERR, &devno);
873 if (r < 0)
874 return r;
875 if (r == 0) {
876 log_debug("Skipping automatic GPT dissection logic, root file system not backed by a (single) whole block device.");
877 return 0;
878 }
879
880 return enumerate_partitions(devno);
881 }
882
883 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
884 int r;
885
886 assert(key);
887
888 if (proc_cmdline_key_streq(key, "systemd.gpt_auto") ||
889 proc_cmdline_key_streq(key, "rd.systemd.gpt_auto")) {
890
891 r = value ? parse_boolean(value) : 1;
892 if (r < 0)
893 log_warning_errno(r, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value);
894 else
895 arg_enabled = r;
896
897 } else if (streq(key, "root")) {
898
899 if (proc_cmdline_value_missing(key, value))
900 return 0;
901
902 /* Disable root disk logic if there's a root= value
903 * specified (unless it happens to be "gpt-auto") */
904
905 if (!streq(value, "gpt-auto")) {
906 arg_root_enabled = false;
907 log_debug("Disabling root partition auto-detection, root= is defined.");
908 }
909
910 } else if (streq(key, "roothash")) {
911
912 if (proc_cmdline_value_missing(key, value))
913 return 0;
914
915 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
916
917 arg_root_enabled = false;
918
919 } else if (streq(key, "rootfstype")) {
920
921 if (proc_cmdline_value_missing(key, value))
922 return 0;
923
924 return free_and_strdup_warn(&arg_root_fstype, value);
925
926 } else if (streq(key, "rootflags")) {
927
928 if (proc_cmdline_value_missing(key, value))
929 return 0;
930
931 if (!strextend_with_separator(&arg_root_options, ",", value))
932 return log_oom();
933
934 } else if (streq(key, "rw") && !value)
935 arg_root_rw = true;
936 else if (streq(key, "ro") && !value)
937 arg_root_rw = false;
938 else if (proc_cmdline_key_streq(key, "systemd.image_policy"))
939 return parse_image_policy_argument(optarg, &arg_image_policy);
940
941 else if (streq(key, "systemd.swap")) {
942
943 r = value ? parse_boolean(value) : 1;
944 if (r < 0)
945 log_warning_errno(r, "Failed to parse swap switch \"%s\", ignoring: %m", value);
946 else
947 arg_swap_enabled = r;
948
949 if (!arg_swap_enabled)
950 log_debug("Disabling swap partitions auto-detection, systemd.swap=no is defined.");
951
952 }
953
954 return 0;
955 }
956
957 static int run(const char *dest, const char *dest_early, const char *dest_late) {
958 int r, k;
959
960 assert_se(arg_dest = dest_late);
961
962 if (detect_container() > 0) {
963 log_debug("In a container, exiting.");
964 return 0;
965 }
966
967 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
968 if (r < 0)
969 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
970
971 if (!arg_enabled) {
972 log_debug("Disabled, exiting.");
973 return 0;
974 }
975
976 if (arg_root_enabled)
977 r = add_root_mount();
978
979 if (!in_initrd()) {
980 k = add_mounts();
981 if (r >= 0)
982 r = k;
983 }
984
985 return r;
986 }
987
988 DEFINE_MAIN_GENERATOR_FUNCTION(run);