1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "blockdev-util.h"
9 #include "device-util.h"
10 #include "devnum-util.h"
11 #include "dissect-image.h"
13 #include "efi-loader.h"
15 #include "errno-util.h"
16 #include "factory-reset.h"
19 #include "fstab-util.h"
20 #include "generator.h"
22 #include "image-policy.h"
23 #include "initrd-util.h"
24 #include "loop-util.h"
25 #include "mountpoint-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "proc-cmdline.h"
30 #include "stat-util.h"
31 #include "string-util.h"
33 #include "time-util.h"
34 #include "unit-name.h"
37 typedef enum MountPointFlags
{
39 MOUNT_GROWFS
= 1 << 1,
40 MOUNT_MEASURE
= 1 << 2,
41 MOUNT_VALIDATEFS
= 1 << 3,
44 static const char *arg_dest
= NULL
;
45 static const char *arg_dest_late
= NULL
;
46 static bool arg_enabled
= true;
47 static GptAutoRoot arg_auto_root
= _GPT_AUTO_ROOT_INVALID
;
48 static GptAutoRoot arg_auto_usr
= _GPT_AUTO_ROOT_INVALID
;
49 static bool arg_swap_enabled
= true;
50 static char *arg_root_fstype
= NULL
;
51 static char *arg_root_options
= NULL
;
52 static int arg_root_rw
= -1;
53 static char *arg_usr_fstype
= NULL
;
54 static char *arg_usr_options
= NULL
;
55 static ImagePolicy
*arg_image_policy
= NULL
;
56 static ImageFilter
*arg_image_filter
= NULL
;
58 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype
, freep
);
59 STATIC_DESTRUCTOR_REGISTER(arg_root_options
, freep
);
60 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype
, freep
);
61 STATIC_DESTRUCTOR_REGISTER(arg_usr_options
, freep
);
62 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
63 STATIC_DESTRUCTOR_REGISTER(arg_image_filter
, image_filter_freep
);
65 #define LOADER_PARTITION_IDLE_USEC (120 * USEC_PER_SEC)
67 static int add_cryptsetup(
70 const char *mount_opts
,
71 MountPointFlags flags
,
75 #if HAVE_LIBCRYPTSETUP
76 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *options
= NULL
;
77 _cleanup_fclose_
FILE *f
= NULL
;
83 r
= unit_name_from_path(what
, ".device", &d
);
85 return log_error_errno(r
, "Failed to generate unit name: %m");
87 e
= unit_name_escape(id
);
91 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
93 return log_error_errno(r
, "Failed to generate unit name: %m");
95 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, n
, &f
);
99 r
= generator_write_cryptsetup_unit_section(f
, NULL
);
104 "Before=umount.target cryptsetup.target\n"
105 "Conflicts=umount.target\n"
110 if (!FLAGS_SET(flags
, MOUNT_RW
)) {
111 options
= strdup("read-only");
116 r
= efi_measured_uki(LOG_WARNING
);
118 /* Enable TPM2 based unlocking automatically, if we have a TPM. See #30176. */
119 if (!strextend_with_separator(&options
, ",", "tpm2-device=auto"))
122 if (FLAGS_SET(flags
, MOUNT_MEASURE
)) {
123 /* We only measure the root volume key into PCR 15 if we are booted with sd-stub (i.e. in a
124 * UKI), and sd-stub measured the UKI. We do this in order not to step into people's own PCR
125 * assignment, under the assumption that people who are fine to use sd-stub with its PCR
126 * assignments are also OK with our PCR 15 use here. */
128 if (!strextend_with_separator(&options
, ",", "tpm2-measure-pcr=yes"))
131 log_debug("Will not measure volume key of volume '%s', not booted via systemd-stub with measurements enabled.", id
);
134 r
= generator_write_cryptsetup_service_section(f
, id
, what
, NULL
, options
);
138 r
= fflush_and_check(f
);
140 return log_error_errno(r
, "Failed to write file %s: %m", n
);
142 r
= generator_write_device_timeout(arg_dest_late
, what
, mount_opts
, /* filtered = */ NULL
);
146 r
= generator_add_symlink(arg_dest_late
, d
, "wants", n
);
150 const char *dmname
= strjoina("dev-mapper-", e
, ".device");
153 r
= generator_add_symlink(arg_dest_late
, "cryptsetup.target", "requires", n
);
157 r
= generator_add_symlink(arg_dest_late
, dmname
, "requires", n
);
162 r
= write_drop_in_format(arg_dest_late
, dmname
, 50, "job-timeout",
163 "# Automatically generated by systemd-gpt-auto-generator\n\n"
165 "JobTimeoutSec=infinity"); /* the binary handles timeouts anyway */
167 log_warning_errno(r
, "Failed to write device timeout drop-in, ignoring: %m");
172 s
= path_join("/dev/mapper", id
);
181 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
182 "Partition is encrypted, but systemd-gpt-auto-generator was compiled without libcryptsetup support.");
187 static int add_veritysetup(
189 const char *data_what
,
190 const char *hash_what
,
191 const char *mount_opts
) {
193 #if HAVE_LIBCRYPTSETUP
200 _cleanup_free_
char *dd
= NULL
;
201 r
= unit_name_from_path(data_what
, ".device", &dd
);
203 return log_error_errno(r
, "Failed to generate data device unit name: %m");
205 _cleanup_free_
char *dh
= NULL
;
206 r
= unit_name_from_path(hash_what
, ".device", &dh
);
208 return log_error_errno(r
, "Failed to generate hash device unit name: %m");
210 _cleanup_free_
char *e
= unit_name_escape(id
);
214 _cleanup_free_
char *n
= NULL
;
215 r
= unit_name_build("systemd-veritysetup", e
, ".service", &n
);
217 return log_error_errno(r
, "Failed to generate unit name: %m");
219 _cleanup_fclose_
FILE *f
= NULL
;
220 r
= generator_open_unit_file(arg_dest_late
, /* source= */ NULL
, n
, &f
);
224 r
= generator_write_veritysetup_unit_section(f
, /* source= */ NULL
);
229 "Before=veritysetup.target\n"
230 "BindsTo=%1$s %2$s\n"
234 r
= generator_write_veritysetup_service_section(
239 /* roothash= */ NULL
, /* NULL means: derive root hash from udev property ID_DISSECT_PART_ROOTHASH */
240 "root-hash-signature=auto"); /* auto means: derive signature from udev property ID_DISSECT_PART_ROOTHASH_SIG */
244 r
= fflush_and_check(f
);
246 return log_error_errno(r
, "Failed to write file %s: %m", n
);
248 r
= generator_write_device_timeout(arg_dest_late
, data_what
, mount_opts
, /* filtered= */ NULL
);
252 r
= generator_write_device_timeout(arg_dest_late
, hash_what
, mount_opts
, /* filtered= */ NULL
);
256 r
= generator_add_symlink(arg_dest_late
, dd
, "wants", n
);
260 r
= generator_add_symlink(arg_dest_late
, dh
, "wants", n
);
264 _cleanup_free_
char *dmname
= NULL
;
265 dmname
= strjoin("dev-mapper-", e
, ".device");
269 r
= write_drop_in_format(
271 dmname
, 50, "job-timeout",
272 "# Automatically generated by systemd-gpt-auto-generator\n\n"
274 "JobTimeoutSec=infinity"); /* the binary handles timeouts anyway */
276 return log_error_errno(r
, "Failed to write device timeout drop-in: %m");
280 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
281 "Partition is Verity protected, but systemd-gpt-auto-generator was compiled without libcryptsetup support.");
286 static int add_mount(
291 MountPointFlags flags
,
293 const char *description
,
296 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *opts_filtered
= NULL
;
297 _cleanup_fclose_
FILE *f
= NULL
;
300 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
301 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
302 * could potentially be understood as specifiers. */
309 log_debug("Adding %s: %s fstype=%s", where
, what
, fstype
?: "(any)");
311 if (streq_ptr(fstype
, "crypto_LUKS")) {
312 /* Mount options passed are determined by partition_pick_mount_options(), whose result
313 * is known to not contain timeout options. */
314 r
= add_cryptsetup(id
, what
, /* mount_opts = */ NULL
, flags
, /* require= */ true, &crypto_what
);
321 r
= dissect_fstype_ok(fstype
);
323 return log_error_errno(r
, "Unable to determine of dissected file system type '%s' is permitted: %m", fstype
);
325 return log_error_errno(
326 SYNTHETIC_ERRNO(EIDRM
),
327 "Refusing to automatically mount uncommon file system '%s' to '%s'.",
331 r
= generator_write_device_timeout(arg_dest_late
, what
, options
, &opts_filtered
);
335 r
= unit_name_from_path(where
, ".mount", &unit
);
337 return log_error_errno(r
, "Failed to generate unit name: %m");
339 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, unit
, &f
);
346 "Documentation=man:systemd-gpt-auto-generator(8)\n",
350 fprintf(f
, "Before=%s\n", post
);
352 /* NB: here we do not write to arg_dest_late, but to arg_dest! We typically leave the normal
353 * generator drop-in dir for explicit configuration via systemd-fstab-generator or similar, and put
354 * out automatic configuration in the arg_dest_late directory. But this one is an exception, since we
355 * need to override the static version of the fsck root service file. */
356 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
, opts_filtered
);
360 r
= generator_write_blockdev_dependency(f
, what
);
372 fprintf(f
, "Type=%s\n", fstype
);
375 fprintf(f
, "Options=%s\n", opts_filtered
);
377 r
= generator_write_mount_timeout(f
, where
, opts_filtered
);
381 r
= fflush_and_check(f
);
383 return log_error_errno(r
, "Failed to write unit %s: %m", unit
);
385 if (FLAGS_SET(flags
, MOUNT_VALIDATEFS
)) {
386 r
= generator_hook_up_validatefs(arg_dest_late
, where
, post
);
391 if (FLAGS_SET(flags
, MOUNT_GROWFS
)) {
392 r
= generator_hook_up_growfs(arg_dest_late
, where
, post
);
397 if (FLAGS_SET(flags
, MOUNT_MEASURE
)) {
398 r
= generator_hook_up_pcrfs(arg_dest_late
, where
, post
);
404 r
= generator_add_symlink(arg_dest_late
, post
, "requires", unit
);
412 static int path_is_busy(const char *where
) {
417 /* already a mountpoint; generators run during reload */
418 r
= path_is_mount_point_full(where
, /* root = */ NULL
, AT_SYMLINK_FOLLOW
);
421 /* The directory will be created by the mount or automount unit when it is started. */
426 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
428 /* not a mountpoint but it contains files */
429 r
= dir_is_empty(where
, /* ignore_hidden_or_backup= */ false);
431 log_debug("\"%s\" is not a directory, ignoring.", where
);
434 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
436 log_debug("\"%s\" already populated, ignoring.", where
);
443 static int add_partition_mount(
444 PartitionDesignator d
,
445 DissectedPartition
*p
,
448 const char *description
) {
450 _cleanup_free_
char *options
= NULL
;
455 r
= path_is_busy(where
);
457 return r
< 0 ? r
: 0;
459 r
= partition_pick_mount_options(
461 dissected_partition_fstype(p
),
465 /* ret_ms_flags= */ NULL
);
474 (p
->rw
? MOUNT_RW
: 0) |
476 (p
->growfs
? MOUNT_GROWFS
: 0) |
477 (STR_IN_SET(id
, "root", "var") ? MOUNT_MEASURE
: 0), /* by default measure rootfs and /var, since they contain the "identity" of the system */
480 SPECIAL_LOCAL_FS_TARGET
);
483 static int add_partition_swap(DissectedPartition
*p
) {
485 _cleanup_free_
char *name
= NULL
, *crypto_what
= NULL
;
486 _cleanup_fclose_
FILE *f
= NULL
;
492 if (!arg_swap_enabled
)
495 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
496 r
= fstab_has_fstype("swap");
498 return log_error_errno(r
, "Failed to parse fstab: %m");
500 log_debug("swap specified in fstab, ignoring.");
504 if (streq_ptr(p
->fstype
, "crypto_LUKS")) {
505 r
= add_cryptsetup("swap", p
->node
, /* mount_opts = */ NULL
, MOUNT_RW
, /* require= */ true, &crypto_what
);
512 log_debug("Adding swap: %s", what
);
514 r
= unit_name_from_path(what
, ".swap", &name
);
516 return log_error_errno(r
, "Failed to generate unit name: %m");
518 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, name
, &f
);
524 "Description=Swap Partition\n"
525 "Documentation=man:systemd-gpt-auto-generator(8)\n");
527 r
= generator_write_blockdev_dependency(f
, what
);
537 r
= fflush_and_check(f
);
539 return log_error_errno(r
, "Failed to write unit %s: %m", name
);
541 return generator_add_symlink(arg_dest_late
, SPECIAL_SWAP_TARGET
, "wants", name
);
544 static int add_automount(
549 MountPointFlags flags
,
551 const char *description
,
554 _cleanup_free_
char *unit
= NULL
;
555 _cleanup_fclose_
FILE *f
= NULL
;
573 r
= unit_name_from_path(where
, ".automount", &unit
);
575 return log_error_errno(r
, "Failed to generate unit name: %m");
577 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, unit
, &f
);
584 "Documentation=man:systemd-gpt-auto-generator(8)\n"
587 "TimeoutIdleSec="USEC_FMT
"\n",
590 timeout
/ USEC_PER_SEC
);
592 r
= fflush_and_check(f
);
594 return log_error_errno(r
, "Failed to write unit %s: %m", unit
);
596 return generator_add_symlink(arg_dest_late
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
599 static int add_partition_xbootldr(DissectedPartition
*p
) {
600 _cleanup_free_
char *options
= NULL
;
604 assert(!in_initrd());
606 r
= path_is_busy("/boot");
612 r
= partition_pick_mount_options(
614 dissected_partition_fstype(p
),
616 /* discard= */ false,
618 /* ret_ms_flags= */ NULL
);
620 return log_error_errno(r
, "Failed to determine default mount options for /boot/: %m");
622 return add_automount(
629 "Boot Loader Partition",
630 LOADER_PARTITION_IDLE_USEC
);
634 static int add_partition_esp(DissectedPartition
*p
, bool has_xbootldr
) {
635 const char *esp_path
= NULL
, *id
= NULL
;
636 _cleanup_free_
char *options
= NULL
;
640 assert(!in_initrd());
642 /* Check if there's an existing fstab entry for ESP. If so, we just skip the gpt-auto logic. */
643 r
= fstab_has_node(p
->node
);
645 log_warning_errno(r
, "Failed to check if fstab entry for device '%s' exists, ignoring: %m",
650 /* If XBOOTLDR partition is not present and /boot/ is unused and empty, we'll take that.
651 * Otherwise, if /efi/ is unused and empty (or missing), we'll take that.
652 * Otherwise, we do nothing. */
654 r
= path_is_busy("/boot");
664 r
= path_is_busy("/efi");
674 r
= partition_pick_mount_options(
676 dissected_partition_fstype(p
),
678 /* discard= */ false,
680 /* ret_ms_flags= */ NULL
);
682 return log_error_errno(r
, "Failed to determine default mount options for %s: %m", esp_path
);
684 return add_automount(
691 "EFI System Partition Automount",
692 LOADER_PARTITION_IDLE_USEC
);
695 static int add_partition_esp(DissectedPartition
*p
, bool has_xbootldr
) {
700 static int add_partition_root_rw(DissectedPartition
*p
) {
705 assert(!in_initrd());
707 /* Invoked on the main system (not initrd), to honour GPT flag 60 on the root fs (ro) */
709 if (arg_root_rw
>= 0) {
710 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
715 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
719 r
= generator_enable_remount_fs_service(arg_dest_late
);
723 path
= strjoina(arg_dest_late
, "/systemd-remount-fs.service.d/50-remount-rw.conf");
725 r
= write_string_file(path
,
726 "# Automatically generated by systemd-gpt-auto-generator\n\n"
728 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
729 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
|WRITE_STRING_FILE_MKDIR_0755
);
731 return log_error_errno(r
, "Failed to write drop-in file %s: %m", path
);
736 static int add_partition_root_growfs(DissectedPartition
*p
) {
739 assert(!in_initrd());
741 /* Invoked on the main system (not initrd), to honour GPT flag 59 on the root fs (growfs) */
744 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.");
748 return generator_hook_up_growfs(arg_dest_late
, "/", SPECIAL_LOCAL_FS_TARGET
);
751 static int add_partition_root_flags(DissectedPartition
*p
) {
755 assert(!in_initrd());
757 RET_GATHER(r
, add_partition_root_growfs(p
));
758 RET_GATHER(r
, add_partition_root_rw(p
));
764 static int add_root_cryptsetup(void) {
765 #if HAVE_LIBCRYPTSETUP
767 assert(arg_auto_root
!= GPT_AUTO_ROOT_OFF
);
769 /* If a device /dev/gpt-auto-root-luks or /dev/disk/by-designator/root-luks appears, then make it
770 * pull in systemd-cryptsetup@root.service, which sets it up, and causes /dev/gpt-auto-root or
771 * /dev/disk/by-designator/root to appear which is all we are looking for. */
774 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
775 "/dev/disk/by-designator/root-luks" : "/dev/gpt-auto-root-luks";
777 if (IN_SET(arg_auto_root
, GPT_AUTO_ROOT_FORCE
, GPT_AUTO_ROOT_DISSECT_FORCE
)) {
778 /* Similar logic as in add_root_mount(), see below */
779 FactoryResetMode f
= factory_reset_mode();
781 log_warning_errno(f
, "Failed to determine whether we are in factory reset mode, assuming not: %m");
783 if (IN_SET(f
, FACTORY_RESET_ON
, FACTORY_RESET_COMPLETE
))
785 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
786 "/dev/disk/by-designator/root-luks-ignore-factory-reset" :
787 "/dev/gpt-auto-root-luks-ignore-factory-reset";
790 return add_cryptsetup(
794 MOUNT_RW
|MOUNT_MEASURE
,
795 /* require= */ false,
796 /* ret_device= */ NULL
);
803 static int add_root_mount(void) {
805 _cleanup_free_
char *options
= NULL
;
808 /* Explicitly disabled? Then exit immediately */
809 if (arg_auto_root
== GPT_AUTO_ROOT_OFF
)
812 /* Neither explicitly enabled nor disabled? Then decide based on the EFI partition variables to be set */
813 if (arg_auto_root
< 0) {
814 if (!is_efi_boot()) {
815 log_debug("Not an EFI boot, not creating root mount.");
819 r
= efi_loader_get_device_part_uuid(/* ret= */ NULL
);
821 log_notice("EFI loader partition unknown, not processing %s.\n"
822 "(The boot loader did not set EFI variable LoaderDevicePartUUID.)",
823 in_initrd() ? "/sysroot" : "/");
827 return log_error_errno(r
, "Failed to read loader partition UUID: %m");
830 /* OK, we shall look for a root device, so let's wait for a root device to show up. A udev rule will
831 * create the link for us under the right name.
833 * There are two distinct names: the /dev/gpt-auto-root-ignore-factory-reset symlink is created for
834 * the root partition if factory reset mode is enabled or complete, and the /dev/gpt-auto-root
835 * symlink is only created if factory reset mode is off or already complete (thus taking factory
836 * reset state into account). In scenarios where the root disk is partially reformatted during
837 * factory reset the latter is the link to use, otherwise the former (so that we don't accidentally
838 * mount a root partition too early that is about to be wiped and replaced by another one). */
841 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
842 "/dev/disk/by-designator/root" : "/dev/gpt-auto-root";
843 if (IN_SET(arg_auto_root
, GPT_AUTO_ROOT_FORCE
, GPT_AUTO_ROOT_DISSECT_FORCE
)) {
844 FactoryResetMode f
= factory_reset_mode();
846 log_warning_errno(f
, "Failed to determine whether we are in factory reset mode, assuming not: %m");
848 if (IN_SET(f
, FACTORY_RESET_ON
, FACTORY_RESET_COMPLETE
))
850 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
851 "/dev/disk/by-designator/root-ignore-factory-reset" :
852 "/dev/gpt-auto-root-ignore-factory-reset";
856 r
= generator_write_initrd_root_device_deps(arg_dest_late
, bdev
);
860 r
= add_root_cryptsetup();
864 /* If a device /dev/disk/by-designator/root-verity or
865 * /dev/disk/by-designator/root-verity-data appears, then make it pull in
866 * systemd-cryptsetup@root.service, which sets it up, and causes /dev/disk/by-designator/root
870 "/dev/disk/by-designator/root-verity-data",
871 "/dev/disk/by-designator/root-verity",
877 /* Note that we do not need to enable systemd-remount-fs.service here. If /etc/fstab exists,
878 * systemd-fstab-generator will pull it in for us, and otherwise add_partition_root_flags() will do
879 * it, after the initrd transition. */
881 r
= partition_pick_mount_options(
887 /* ret_ms_flags= */ NULL
);
889 return log_error_errno(r
, "Failed to pick root mount options: %m");
891 if (arg_root_options
)
892 if (!strextend_with_separator(&options
, ",", arg_root_options
))
898 in_initrd() ? "/sysroot" : "/",
900 (arg_root_rw
> 0 ? MOUNT_RW
: 0) |
901 (in_initrd() ? MOUNT_VALIDATEFS
: 0) |
905 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
911 static int add_usr_mount(void) {
915 /* /usr/ discovery must be enabled explicitly. */
916 if (arg_auto_usr
<= 0)
919 /* We do not support the other gpt-auto modes for /usr/, but the parser should already have checked that. */
920 assert(arg_auto_usr
== GPT_AUTO_ROOT_DISSECT
);
922 if (arg_root_fstype
&& !arg_usr_fstype
) {
923 arg_usr_fstype
= strdup(arg_root_fstype
);
928 if (arg_root_options
&& !arg_usr_options
) {
929 arg_usr_options
= strdup(arg_root_options
);
930 if (!arg_usr_options
)
937 "/dev/disk/by-designator/usr-luks",
939 MOUNT_RW
|MOUNT_MEASURE
,
940 /* require= */ false,
941 /* ret_device= */ NULL
);
945 /* If a device /dev/disk/by-designator/usr-verity or
946 * /dev/disk/by-designator/usr-verity-data appears, then make it pull in
947 * systemd-cryptsetup@usr.service, which sets it up, and causes /dev/disk/by-designator/usr
951 "/dev/disk/by-designator/usr-verity-data",
952 "/dev/disk/by-designator/usr-verity",
958 _cleanup_free_
char *options
= NULL
;
959 r
= partition_pick_mount_options(
965 /* ret_ms_flags= */ NULL
);
967 return log_error_errno(r
, "Failed to pick /usr/ mount options: %m");
970 if (!strextend_with_separator(&options
, ",", arg_usr_options
))
974 "/dev/disk/by-designator/usr",
975 in_initrd() ? "/sysusr/usr" : "/usr",
980 in_initrd() ? SPECIAL_INITRD_USR_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
985 log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
987 r
= add_mount("usr-bind",
993 "/usr/ Partition (Final)",
994 SPECIAL_INITRD_FS_TARGET
);
1002 static int process_loader_partitions(DissectedPartition
*esp
, DissectedPartition
*xbootldr
) {
1003 sd_id128_t loader_uuid
;
1009 /* If any paths in fstab look similar to our favorite paths for ESP or XBOOTLDR, we just exit
1010 * early. We also don't bother with cases where one is configured explicitly and the other shall be
1011 * mounted automatically. */
1013 r
= fstab_has_mount_point_prefix_strv(STRV_MAKE("/boot", "/efi"));
1015 log_debug("Found mount entries in the /boot/ or /efi/ hierarchies in fstab, not generating ESP or XBOOTLDR mounts.");
1019 log_debug_errno(r
, "Failed to check fstab existing paths, ignoring: %m");
1021 if (!is_efi_boot()) {
1022 log_debug("Not an EFI boot, skipping loader partition UUID check.");
1026 /* Let's check if LoaderDevicePartUUID points to either ESP or XBOOTLDR. We prefer it pointing
1027 * to the ESP, but we accept XBOOTLDR too. If it points to neither of them, don't mount any
1028 * loader partitions, since they are not the ones used for booting. */
1030 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
1032 log_debug_errno(r
, "EFI loader partition unknown, skipping ESP and XBOOTLDR mounts.");
1036 return log_debug_errno(r
, "Failed to read loader partition UUID, ignoring: %m");
1038 if (esp
->found
&& sd_id128_equal(esp
->uuid
, loader_uuid
))
1041 if (xbootldr
->found
&& sd_id128_equal(xbootldr
->uuid
, loader_uuid
)) {
1042 log_debug("LoaderDevicePartUUID points to XBOOTLDR partition.");
1046 log_debug("LoaderDevicePartUUID points to neither ESP nor XBOOTLDR, ignoring.");
1052 if (xbootldr
->found
)
1053 RET_GATHER(r
, add_partition_xbootldr(xbootldr
));
1055 RET_GATHER(r
, add_partition_esp(esp
, xbootldr
->found
));
1060 static int enumerate_partitions(dev_t devnum
) {
1061 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1062 _cleanup_(loop_device_unrefp
) LoopDevice
*loop
= NULL
;
1063 _cleanup_free_
char *devname
= NULL
;
1066 static const PartitionDesignator ignore_designators
[] = {
1068 PARTITION_ROOT_VERITY
,
1069 PARTITION_ROOT_VERITY_SIG
,
1071 PARTITION_USR_VERITY
,
1072 PARTITION_USR_VERITY_SIG
,
1075 assert(!in_initrd());
1077 /* Run on the final root fs (not in the initrd), to mount auxiliary partitions, and hook in rw
1078 * remount and growfs of the root partition */
1080 r
= block_get_whole_disk(devnum
, &devnum
);
1082 return log_debug_errno(r
, "Failed to get whole block device for " DEVNUM_FORMAT_STR
": %m",
1083 DEVNUM_FORMAT_VAL(devnum
));
1085 r
= devname_from_devnum(S_IFBLK
, devnum
, &devname
);
1087 return log_debug_errno(r
, "Failed to get device node of " DEVNUM_FORMAT_STR
": %m",
1088 DEVNUM_FORMAT_VAL(devnum
));
1090 _cleanup_(image_policy_freep
) ImagePolicy
*image_policy
= NULL
;
1091 r
= image_policy_ignore_designators(
1092 arg_image_policy
?: &image_policy_host
,
1093 ignore_designators
, ELEMENTSOF(ignore_designators
),
1096 return log_debug_errno(r
, "Failed to mark root/usr designators as ignore in image policy: %m");
1098 /* Let's take a LOCK_SH lock on the block device, in case udevd is already running. If we don't take
1099 * the lock, udevd might end up issuing BLKRRPART in the middle, and we don't want that, since that
1100 * might remove all partitions while we are operating on them. */
1101 r
= loop_device_open_from_path(devname
, O_RDONLY
, LOCK_SH
, &loop
);
1103 return log_debug_errno(r
, "Failed to open %s: %m", devname
);
1105 r
= dissect_loop_device(
1108 /* mount_options= */ NULL
,
1111 DISSECT_IMAGE_GPT_ONLY
|
1112 DISSECT_IMAGE_USR_NO_ROOT
|
1113 DISSECT_IMAGE_DISKSEQ_DEVNODE
|
1114 DISSECT_IMAGE_ALLOW_EMPTY
,
1115 /* NB! Unlike most other places where we dissect block devices we do not use
1116 * DISSECT_IMAGE_ADD_PARTITION_DEVICES here: we want that the kernel finds the
1117 * devices, and udev probes them before we mount them via .mount units much later
1118 * on. And thus we also don't set DISSECT_IMAGE_PIN_PARTITION_DEVICES here, because
1119 * we don't actually mount anything immediately. */
1122 bool ok
= r
== -ENOPKG
;
1123 dissect_log_error(ok
? LOG_DEBUG
: LOG_ERR
, r
, devname
, NULL
);
1127 if (m
->partitions
[PARTITION_SWAP
].found
)
1128 RET_GATHER(r
, add_partition_swap(m
->partitions
+ PARTITION_SWAP
));
1130 RET_GATHER(r
, process_loader_partitions(m
->partitions
+ PARTITION_ESP
, m
->partitions
+ PARTITION_XBOOTLDR
));
1132 if (m
->partitions
[PARTITION_HOME
].found
)
1133 RET_GATHER(r
, add_partition_mount(PARTITION_HOME
, m
->partitions
+ PARTITION_HOME
,
1134 "home", "/home", "Home Partition"));
1136 if (m
->partitions
[PARTITION_SRV
].found
)
1137 RET_GATHER(r
, add_partition_mount(PARTITION_SRV
, m
->partitions
+ PARTITION_SRV
,
1138 "srv", "/srv", "Server Data Partition"));
1140 if (m
->partitions
[PARTITION_VAR
].found
)
1141 RET_GATHER(r
, add_partition_mount(PARTITION_VAR
, m
->partitions
+ PARTITION_VAR
,
1142 "var", "/var", "Variable Data Partition"));
1144 if (m
->partitions
[PARTITION_TMP
].found
)
1145 RET_GATHER(r
, add_partition_mount(PARTITION_TMP
, m
->partitions
+ PARTITION_TMP
,
1146 "var-tmp", "/var/tmp", "Temporary Data Partition"));
1148 if (m
->partitions
[PARTITION_ROOT
].found
)
1149 RET_GATHER(r
, add_partition_root_flags(m
->partitions
+ PARTITION_ROOT
));
1154 static int add_mounts(void) {
1161 r
= blockdev_get_root(LOG_ERR
, &devno
);
1165 log_debug("Skipping automatic GPT dissection logic, root file system not backed by a (single) whole block device.");
1169 return enumerate_partitions(devno
);
1172 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
1177 if (proc_cmdline_key_streq(key
, "systemd.gpt_auto") ||
1178 proc_cmdline_key_streq(key
, "rd.systemd.gpt_auto")) {
1180 r
= value
? parse_boolean(value
) : 1;
1182 log_warning_errno(r
, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value
);
1186 } else if (streq(key
, "root")) {
1188 if (proc_cmdline_value_missing(key
, value
))
1191 /* Disable root disk logic if there's a root= value specified (unless it happens to be
1192 * "gpt-auto" or "gpt-auto-force") */
1194 arg_auto_root
= parse_gpt_auto_root("root=", value
);
1195 assert(arg_auto_root
>= 0);
1197 } else if (streq(key
, "roothash")) {
1199 if (proc_cmdline_value_missing(key
, value
))
1202 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
1204 arg_auto_root
= GPT_AUTO_ROOT_OFF
;
1205 log_debug("Disabling root partition auto-detection, roothash= is set.");
1207 } else if (streq(key
, "rootfstype")) {
1209 if (proc_cmdline_value_missing(key
, value
))
1212 return free_and_strdup_warn(&arg_root_fstype
, value
);
1214 } else if (streq(key
, "rootflags")) {
1216 if (proc_cmdline_value_missing(key
, value
))
1219 if (!strextend_with_separator(&arg_root_options
, ",", value
))
1222 } else if (streq(key
, "mount.usr")) {
1224 if (proc_cmdline_value_missing(key
, value
))
1227 /* Disable root disk logic if there's a root= value specified (unless it happens to be
1228 * "gpt-auto" or "gpt-auto-force") */
1230 arg_auto_usr
= parse_gpt_auto_root("mount.usr=", value
);
1231 assert(arg_auto_usr
>= 0);
1233 if (IN_SET(arg_auto_usr
, GPT_AUTO_ROOT_ON
, GPT_AUTO_ROOT_FORCE
, GPT_AUTO_ROOT_DISSECT_FORCE
)) {
1234 log_warning("'gpt-auto', 'gpt-auto-force' and 'dissect-force' are not supported for mount.usr=. Automatically resorting to mount.usr=dissect mode instead.");
1235 arg_auto_usr
= GPT_AUTO_ROOT_DISSECT
;
1238 } else if (streq(key
, "mount.usrfstype")) {
1240 if (proc_cmdline_value_missing(key
, value
))
1243 return free_and_strdup_warn(&arg_usr_fstype
, empty_to_null(value
));
1245 } else if (streq(key
, "mount.usrflags")) {
1247 if (proc_cmdline_value_missing(key
, value
))
1250 if (!strextend_with_separator(&arg_usr_options
, ",", value
))
1253 } else if (streq(key
, "rw") && !value
)
1255 else if (streq(key
, "ro") && !value
)
1256 arg_root_rw
= false;
1257 else if (proc_cmdline_key_streq(key
, "systemd.image_policy"))
1258 return parse_image_policy_argument(value
, &arg_image_policy
);
1259 else if (proc_cmdline_key_streq(key
, "systemd.image_filter")) {
1260 _cleanup_(image_filter_freep
) ImageFilter
*f
= NULL
;
1262 r
= image_filter_parse(value
, &f
);
1264 return log_error_errno(r
, "Failed to parse image filter: %s", value
);
1266 image_filter_free(arg_image_filter
);
1267 arg_image_filter
= TAKE_PTR(f
);
1269 } else if (streq(key
, "systemd.swap")) {
1271 r
= value
? parse_boolean(value
) : 1;
1273 log_warning_errno(r
, "Failed to parse swap switch \"%s\", ignoring: %m", value
);
1275 arg_swap_enabled
= r
;
1277 if (!arg_swap_enabled
)
1278 log_debug("Disabling swap partitions auto-detection, systemd.swap=no is defined.");
1285 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
1288 assert_se(arg_dest
= dest
);
1289 assert_se(arg_dest_late
= dest_late
);
1291 if (detect_container() > 0) {
1292 log_debug("In a container, exiting.");
1296 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
1298 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
1301 log_debug("Disabled, exiting.");
1306 RET_GATHER(r
, add_root_mount());
1307 RET_GATHER(r
, add_usr_mount());
1308 RET_GATHER(r
, add_mounts());
1313 DEFINE_MAIN_GENERATOR_FUNCTION(run
);