1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
19 #include "efi-loader.h"
23 #include "fstab-util.h"
24 #include "generator.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"
33 #include "specifier.h"
34 #include "stat-util.h"
35 #include "string-util.h"
37 #include "unit-name.h"
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
;
49 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
51 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype
, freep
);
52 STATIC_DESTRUCTOR_REGISTER(arg_root_options
, freep
);
54 static int add_cryptsetup(
62 #if HAVE_LIBCRYPTSETUP
63 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *options
= NULL
;
64 _cleanup_fclose_
FILE *f
= NULL
;
70 r
= unit_name_from_path(what
, ".device", &d
);
72 return log_error_errno(r
, "Failed to generate unit name: %m");
74 e
= unit_name_escape(id
);
78 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
80 return log_error_errno(r
, "Failed to generate unit name: %m");
82 r
= generator_open_unit_file(arg_dest
, /* source = */ NULL
, n
, &f
);
86 r
= generator_write_cryptsetup_unit_section(f
, NULL
);
91 "Before=umount.target cryptsetup.target\n"
92 "Conflicts=umount.target\n"
98 options
= strdup("read-only");
103 r
= efi_measured_uki(LOG_WARNING
);
105 /* Enable TPM2 based unlocking automatically, if we have a TPM. See #30176. */
106 if (!strextend_with_separator(&options
, ",", "tpm2-device=auto"))
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. */
115 if (!strextend_with_separator(&options
, ",", "tpm2-measure-pcr=yes"))
118 log_debug("Will not measure volume key of volume '%s', not booted via systemd-stub with measurements enabled.", id
);
121 r
= generator_write_cryptsetup_service_section(f
, id
, what
, NULL
, options
);
125 r
= fflush_and_check(f
);
127 return log_error_errno(r
, "Failed to write file %s: %m", n
);
129 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
133 const char *dmname
= strjoina("dev-mapper-", e
, ".device");
136 r
= generator_add_symlink(arg_dest
, "cryptsetup.target", "requires", n
);
140 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
145 r
= write_drop_in_format(arg_dest
, dmname
, 50, "job-timeout",
146 "# Automatically generated by systemd-gpt-auto-generator\n\n"
148 "JobTimeoutSec=infinity"); /* the binary handles timeouts anyway */
150 log_warning_errno(r
, "Failed to write device timeout drop-in, ignoring: %m");
155 s
= path_join("/dev/mapper", id
);
164 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
165 "Partition is encrypted, but systemd-gpt-auto-generator was compiled without libcryptsetup support");
169 static int add_mount(
178 const char *description
,
181 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
;
182 _cleanup_fclose_
FILE *f
= NULL
;
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. */
194 log_debug("Adding %s: %s fstype=%s", where
, what
, fstype
?: "(any)");
196 if (streq_ptr(fstype
, "crypto_LUKS")) {
197 r
= add_cryptsetup(id
, what
, rw
, /* require= */ true, measure
, &crypto_what
);
204 r
= dissect_fstype_ok(fstype
);
206 return log_error_errno(r
, "Unable to determine of dissected file system type '%s' is permitted: %m", fstype
);
208 return log_error_errno(
209 SYNTHETIC_ERRNO(EIDRM
),
210 "Refusing to automatically mount uncommon file system '%s' to '%s'.",
214 r
= unit_name_from_path(where
, ".mount", &unit
);
216 return log_error_errno(r
, "Failed to generate unit name: %m");
218 r
= generator_open_unit_file(arg_dest
, /* source = */ NULL
, unit
, &f
);
225 "Documentation=man:systemd-gpt-auto-generator(8)\n",
229 fprintf(f
, "Before=%s\n", post
);
231 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
235 r
= generator_write_blockdev_dependency(f
, what
);
247 fprintf(f
, "Type=%s\n", fstype
);
250 fprintf(f
, "Options=%s\n", options
);
252 r
= fflush_and_check(f
);
254 return log_error_errno(r
, "Failed to write unit %s: %m", unit
);
257 r
= generator_hook_up_growfs(arg_dest
, where
, post
);
263 r
= generator_hook_up_pcrfs(arg_dest
, where
, post
);
269 r
= generator_add_symlink(arg_dest
, post
, "requires", unit
);
277 static int path_is_busy(const char *where
) {
282 /* already a mountpoint; generators run during reload */
283 r
= path_is_mount_point_full(where
, /* root = */ NULL
, AT_SYMLINK_FOLLOW
);
286 /* The directory will be created by the mount or automount unit when it is started. */
291 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
293 /* not a mountpoint but it contains files */
294 r
= dir_is_empty(where
, /* ignore_hidden_or_backup= */ false);
296 log_debug("\"%s\" is not a directory, ignoring.", where
);
299 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
301 log_debug("\"%s\" already populated, ignoring.", where
);
308 static int add_partition_mount(
309 PartitionDesignator d
,
310 DissectedPartition
*p
,
313 const char *description
) {
315 _cleanup_free_
char *options
= NULL
;
320 r
= path_is_busy(where
);
322 return r
< 0 ? r
: 0;
324 r
= partition_pick_mount_options(
326 dissected_partition_fstype(p
),
330 /* ret_ms_flags= */ NULL
);
341 /* measure= */ STR_IN_SET(id
, "root", "var"), /* by default measure rootfs and /var, since they contain the "identity" of the system */
344 SPECIAL_LOCAL_FS_TARGET
);
347 static int add_partition_swap(DissectedPartition
*p
) {
349 _cleanup_free_
char *name
= NULL
, *crypto_what
= NULL
;
350 _cleanup_fclose_
FILE *f
= NULL
;
356 if (!arg_swap_enabled
)
359 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
360 r
= fstab_has_fstype("swap");
362 return log_error_errno(r
, "Failed to parse fstab: %m");
364 log_debug("swap specified in fstab, ignoring.");
368 if (streq_ptr(p
->fstype
, "crypto_LUKS")) {
369 r
= add_cryptsetup("swap", p
->node
, /* rw= */ true, /* require= */ true, /* measure= */ false, &crypto_what
);
376 log_debug("Adding swap: %s", what
);
378 r
= unit_name_from_path(what
, ".swap", &name
);
380 return log_error_errno(r
, "Failed to generate unit name: %m");
382 r
= generator_open_unit_file(arg_dest
, /* source = */ NULL
, name
, &f
);
388 "Description=Swap Partition\n"
389 "Documentation=man:systemd-gpt-auto-generator(8)\n");
391 r
= generator_write_blockdev_dependency(f
, what
);
401 r
= fflush_and_check(f
);
403 return log_error_errno(r
, "Failed to write unit %s: %m", name
);
405 return generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
, "wants", name
);
408 static int add_automount(
416 const char *description
,
419 _cleanup_free_
char *unit
= NULL
;
420 _cleanup_fclose_
FILE *f
= NULL
;
433 /* measure= */ false,
440 r
= unit_name_from_path(where
, ".automount", &unit
);
442 return log_error_errno(r
, "Failed to generate unit name: %m");
444 r
= generator_open_unit_file(arg_dest
, /* source = */ NULL
, unit
, &f
);
451 "Documentation=man:systemd-gpt-auto-generator(8)\n"
454 "TimeoutIdleSec="USEC_FMT
"\n",
457 timeout
/ USEC_PER_SEC
);
459 r
= fflush_and_check(f
);
461 return log_error_errno(r
, "Failed to write unit %s: %m", unit
);
463 return generator_add_symlink(arg_dest
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
466 static int add_partition_xbootldr(DissectedPartition
*p
) {
467 _cleanup_free_
char *options
= NULL
;
473 log_debug("In initrd, ignoring the XBOOTLDR partition.");
477 r
= path_is_busy("/boot");
483 r
= partition_pick_mount_options(
485 dissected_partition_fstype(p
),
487 /* discard= */ false,
489 /* ret_ms_flags= */ NULL
);
491 return log_error_errno(r
, "Failed to determine default mount options for /boot/: %m");
493 return add_automount(
501 "Boot Loader Partition",
506 static bool slash_boot_exists(void) {
507 static int cache
= -1;
512 if (access("/boot", F_OK
) >= 0)
513 return (cache
= true);
515 log_error_errno(errno
, "Failed to determine whether /boot/ exists, assuming no: %m");
517 log_debug_errno(errno
, "/boot/: %m");
518 return (cache
= false);
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
;
529 log_debug("In initrd, ignoring the ESP.");
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
);
536 return log_error_errno(r
,
537 "Failed to check if fstab entry for device '%s' exists: %m", p
->node
);
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");
555 r
= path_is_busy("/efi");
565 r
= partition_pick_mount_options(
567 dissected_partition_fstype(p
),
569 /* discard= */ false,
571 /* ret_ms_flags= */ NULL
);
573 return log_error_errno(r
, "Failed to determine default mount options for %s: %m", esp_path
);
575 return add_automount(
583 "EFI System Partition Automount",
587 static int add_partition_esp(DissectedPartition
*p
, bool has_xbootldr
) {
592 static int add_partition_root_rw(DissectedPartition
*p
) {
597 assert(!in_initrd());
599 /* Invoked on the main system (not initrd), to honour GPT flag 60 on the root fs (ro) */
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.");
607 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
611 r
= generator_enable_remount_fs_service(arg_dest
);
615 path
= strjoina(arg_dest
, "/systemd-remount-fs.service.d/50-remount-rw.conf");
617 r
= write_string_file(path
,
618 "# Automatically generated by systemd-gpt-auto-generator\n\n"
620 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
621 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
|WRITE_STRING_FILE_MKDIR_0755
);
623 return log_error_errno(r
, "Failed to write drop-in file %s: %m", path
);
628 static int add_partition_root_growfs(DissectedPartition
*p
) {
631 assert(!in_initrd());
633 /* Invoked on the main system (not initrd), to honour GPT flag 59 on the root fs (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.");
640 return generator_hook_up_growfs(arg_dest
, "/", SPECIAL_LOCAL_FS_TARGET
);
643 static int add_partition_root_flags(DissectedPartition
*p
) {
647 assert(!in_initrd());
649 RET_GATHER(r
, add_partition_root_growfs(p
));
650 RET_GATHER(r
, add_partition_root_rw(p
));
656 static int add_root_cryptsetup(void) {
657 #if HAVE_LIBCRYPTSETUP
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. */
662 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", /* rw= */ true, /* require= */ false, /* measure= */ true, NULL
);
669 static int add_root_mount(void) {
671 _cleanup_free_
char *options
= NULL
;
674 if (!is_efi_boot()) {
675 log_debug("Not an EFI boot, not creating root mount.");
679 r
= efi_loader_get_device_part_uuid(NULL
);
681 log_notice("EFI loader partition unknown, exiting.\n"
682 "(The boot loader did not set EFI variable LoaderDevicePartUUID.)");
685 return log_error_errno(r
, "Failed to read loader partition UUID: %m");
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. */
691 r
= generator_write_initrd_root_device_deps(arg_dest
, "/dev/gpt-auto-root");
695 r
= add_root_cryptsetup();
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. */
704 r
= partition_pick_mount_options(
710 /* ret_ms_flags= */ NULL
);
712 return log_error_errno(r
, "Failed to pick root mount options: %m");
714 if (arg_root_options
)
715 if (!strextend_with_separator(&options
, ",", arg_root_options
))
720 "/dev/gpt-auto-root",
721 in_initrd() ? "/sysroot" : "/",
723 /* rw= */ arg_root_rw
> 0,
728 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
734 static int process_loader_partitions(DissectedPartition
*esp
, DissectedPartition
*xbootldr
) {
735 sd_id128_t loader_uuid
;
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. */
745 r
= fstab_has_mount_point_prefix_strv(STRV_MAKE("/boot", "/efi"));
747 log_debug("Found mount entries in the /boot/ or /efi/ hierarchies in fstab, not generating ESP or XBOOTLDR mounts.");
751 log_debug_errno(r
, "Failed to check fstab existing paths, ignoring: %m");
753 if (!is_efi_boot()) {
754 log_debug("Not an EFI boot, skipping loader partition UUID check.");
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. */
762 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
764 log_debug_errno(r
, "EFI loader partition unknown, skipping ESP and XBOOTLDR mounts.");
768 return log_debug_errno(r
, "Failed to read loader partition UUID, ignoring: %m");
770 if (esp
->found
&& sd_id128_equal(esp
->uuid
, loader_uuid
))
773 if (xbootldr
->found
&& sd_id128_equal(xbootldr
->uuid
, loader_uuid
)) {
774 log_debug("LoaderDevicePartUUID points to XBOOTLDR partition.");
778 log_debug("LoaderDevicePartUUID points to neither ESP nor XBOOTLDR, ignoring.");
785 RET_GATHER(r
, add_partition_xbootldr(xbootldr
));
787 RET_GATHER(r
, add_partition_esp(esp
, xbootldr
->found
));
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
;
798 assert(!in_initrd());
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 */
803 r
= block_get_whole_disk(devnum
, &devnum
);
805 return log_debug_errno(r
, "Failed to get whole block device for " DEVNUM_FORMAT_STR
": %m",
806 DEVNUM_FORMAT_VAL(devnum
));
808 r
= devname_from_devnum(S_IFBLK
, devnum
, &devname
);
810 return log_debug_errno(r
, "Failed to get device node of " DEVNUM_FORMAT_STR
": %m",
811 DEVNUM_FORMAT_VAL(devnum
));
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
);
818 return log_debug_errno(r
, "Failed to open %s: %m", devname
);
820 r
= dissect_loop_device(
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. */
836 bool ok
= r
== -ENOPKG
;
837 dissect_log_error(ok
? LOG_DEBUG
: LOG_ERR
, r
, devname
, NULL
);
841 if (m
->partitions
[PARTITION_SWAP
].found
)
842 RET_GATHER(r
, add_partition_swap(m
->partitions
+ PARTITION_SWAP
));
844 RET_GATHER(r
, process_loader_partitions(m
->partitions
+ PARTITION_ESP
, m
->partitions
+ PARTITION_XBOOTLDR
));
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"));
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"));
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"));
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"));
862 if (m
->partitions
[PARTITION_ROOT
].found
)
863 RET_GATHER(r
, add_partition_root_flags(m
->partitions
+ PARTITION_ROOT
));
868 static int add_mounts(void) {
872 r
= blockdev_get_root(LOG_ERR
, &devno
);
876 log_debug("Skipping automatic GPT dissection logic, root file system not backed by a (single) whole block device.");
880 return enumerate_partitions(devno
);
883 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
888 if (proc_cmdline_key_streq(key
, "systemd.gpt_auto") ||
889 proc_cmdline_key_streq(key
, "rd.systemd.gpt_auto")) {
891 r
= value
? parse_boolean(value
) : 1;
893 log_warning_errno(r
, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value
);
897 } else if (streq(key
, "root")) {
899 if (proc_cmdline_value_missing(key
, value
))
902 /* Disable root disk logic if there's a root= value
903 * specified (unless it happens to be "gpt-auto") */
905 if (!streq(value
, "gpt-auto")) {
906 arg_root_enabled
= false;
907 log_debug("Disabling root partition auto-detection, root= is defined.");
910 } else if (streq(key
, "roothash")) {
912 if (proc_cmdline_value_missing(key
, value
))
915 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
917 arg_root_enabled
= false;
919 } else if (streq(key
, "rootfstype")) {
921 if (proc_cmdline_value_missing(key
, value
))
924 return free_and_strdup_warn(&arg_root_fstype
, value
);
926 } else if (streq(key
, "rootflags")) {
928 if (proc_cmdline_value_missing(key
, value
))
931 if (!strextend_with_separator(&arg_root_options
, ",", value
))
934 } else if (streq(key
, "rw") && !value
)
936 else if (streq(key
, "ro") && !value
)
938 else if (proc_cmdline_key_streq(key
, "systemd.image_policy"))
939 return parse_image_policy_argument(optarg
, &arg_image_policy
);
941 else if (streq(key
, "systemd.swap")) {
943 r
= value
? parse_boolean(value
) : 1;
945 log_warning_errno(r
, "Failed to parse swap switch \"%s\", ignoring: %m", value
);
947 arg_swap_enabled
= r
;
949 if (!arg_swap_enabled
)
950 log_debug("Disabling swap partitions auto-detection, systemd.swap=no is defined.");
957 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
960 assert_se(arg_dest
= dest_late
);
962 if (detect_container() > 0) {
963 log_debug("In a container, exiting.");
967 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
969 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
972 log_debug("Disabled, exiting.");
976 if (arg_root_enabled
)
977 r
= add_root_mount();
988 DEFINE_MAIN_GENERATOR_FUNCTION(run
);