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 "hexdecoct.h"
23 #include "image-policy.h"
24 #include "initrd-util.h"
25 #include "loop-util.h"
26 #include "mountpoint-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "proc-cmdline.h"
31 #include "stat-util.h"
32 #include "string-util.h"
34 #include "time-util.h"
35 #include "unit-name.h"
38 typedef enum MountPointFlags
{
40 MOUNT_GROWFS
= 1 << 1,
41 MOUNT_MEASURE
= 1 << 2,
42 MOUNT_VALIDATEFS
= 1 << 3,
45 static const char *arg_dest
= NULL
;
46 static const char *arg_dest_late
= NULL
;
47 static bool arg_enabled
= true;
48 static GptAutoRoot arg_auto_root
= _GPT_AUTO_ROOT_INVALID
;
49 static GptAutoRoot arg_auto_usr
= _GPT_AUTO_ROOT_INVALID
;
50 static VeritySettings arg_verity_settings
= VERITY_SETTINGS_DEFAULT
;
51 static bool arg_swap_enabled
= true;
52 static char *arg_root_fstype
= NULL
;
53 static char *arg_root_options
= NULL
;
54 static int arg_root_rw
= -1;
55 static char *arg_usr_fstype
= NULL
;
56 static char *arg_usr_options
= NULL
;
57 static ImagePolicy
*arg_image_policy
= NULL
;
58 static ImageFilter
*arg_image_filter
= NULL
;
60 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype
, freep
);
61 STATIC_DESTRUCTOR_REGISTER(arg_root_options
, freep
);
62 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype
, freep
);
63 STATIC_DESTRUCTOR_REGISTER(arg_usr_options
, freep
);
64 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
65 STATIC_DESTRUCTOR_REGISTER(arg_image_filter
, image_filter_freep
);
67 #define LOADER_PARTITION_IDLE_USEC (120 * USEC_PER_SEC)
69 static int add_cryptsetup(
72 const char *mount_opts
,
73 MountPointFlags flags
,
77 #if HAVE_LIBCRYPTSETUP
78 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *options
= NULL
;
79 _cleanup_fclose_
FILE *f
= NULL
;
85 r
= unit_name_from_path(what
, ".device", &d
);
87 return log_error_errno(r
, "Failed to generate unit name: %m");
89 e
= unit_name_escape(id
);
93 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
95 return log_error_errno(r
, "Failed to generate unit name: %m");
97 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, n
, &f
);
101 r
= generator_write_cryptsetup_unit_section(f
, NULL
);
106 "Before=umount.target cryptsetup.target\n"
107 "Conflicts=umount.target\n"
112 if (!FLAGS_SET(flags
, MOUNT_RW
)) {
113 options
= strdup("read-only");
118 r
= efi_measured_uki(LOG_WARNING
);
120 /* Enable TPM2 based unlocking automatically, if we have a TPM. See #30176. */
121 if (!strextend_with_separator(&options
, ",", "tpm2-device=auto"))
124 if (FLAGS_SET(flags
, MOUNT_MEASURE
)) {
125 /* We only measure the root volume key into PCR 15 if we are booted with sd-stub (i.e. in a
126 * UKI), and sd-stub measured the UKI. We do this in order not to step into people's own PCR
127 * assignment, under the assumption that people who are fine to use sd-stub with its PCR
128 * assignments are also OK with our PCR 15 use here. */
130 if (!strextend_with_separator(&options
, ",", "tpm2-measure-pcr=yes"))
133 log_debug("Will not measure volume key of volume '%s', not booted via systemd-stub with measurements enabled.", id
);
136 r
= generator_write_cryptsetup_service_section(f
, id
, what
, NULL
, options
);
140 r
= fflush_and_check(f
);
142 return log_error_errno(r
, "Failed to write file %s: %m", n
);
144 r
= generator_write_device_timeout(arg_dest_late
, what
, mount_opts
, /* filtered = */ NULL
);
148 r
= generator_add_symlink(arg_dest_late
, d
, "wants", n
);
152 const char *dmname
= strjoina("dev-mapper-", e
, ".device");
155 r
= generator_add_symlink(arg_dest_late
, "cryptsetup.target", "requires", n
);
159 r
= generator_add_symlink(arg_dest_late
, dmname
, "requires", n
);
164 r
= write_drop_in_format(arg_dest_late
, dmname
, 50, "job-timeout",
165 "# Automatically generated by systemd-gpt-auto-generator\n\n"
167 "JobTimeoutSec=infinity"); /* the binary handles timeouts anyway */
169 log_warning_errno(r
, "Failed to write device timeout drop-in, ignoring: %m");
174 s
= path_join("/dev/mapper", id
);
183 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
184 "Partition is encrypted, but systemd-gpt-auto-generator was compiled without libcryptsetup support.");
189 static int add_veritysetup(
191 const char *data_what
,
192 const char *hash_what
,
193 const char *mount_opts
) {
195 #if HAVE_LIBCRYPTSETUP
202 _cleanup_free_
char *dd
= NULL
;
203 r
= unit_name_from_path(data_what
, ".device", &dd
);
205 return log_error_errno(r
, "Failed to generate data device unit name: %m");
207 _cleanup_free_
char *dh
= NULL
;
208 r
= unit_name_from_path(hash_what
, ".device", &dh
);
210 return log_error_errno(r
, "Failed to generate hash device unit name: %m");
212 _cleanup_free_
char *e
= unit_name_escape(id
);
216 _cleanup_free_
char *n
= NULL
;
217 r
= unit_name_build("systemd-veritysetup", e
, ".service", &n
);
219 return log_error_errno(r
, "Failed to generate unit name: %m");
221 _cleanup_fclose_
FILE *f
= NULL
;
222 r
= generator_open_unit_file(arg_dest_late
, /* source= */ NULL
, n
, &f
);
226 r
= generator_write_veritysetup_unit_section(f
, /* source= */ NULL
);
231 "Before=veritysetup.target\n"
232 "BindsTo=%1$s %2$s\n"
236 r
= generator_write_veritysetup_service_section(
241 /* roothash= */ NULL
, /* NULL means: derive root hash from udev property ID_DISSECT_PART_ROOTHASH */
242 "root-hash-signature=auto"); /* auto means: derive signature from udev property ID_DISSECT_PART_ROOTHASH_SIG */
246 r
= fflush_and_check(f
);
248 return log_error_errno(r
, "Failed to write file %s: %m", n
);
250 r
= generator_write_device_timeout(arg_dest_late
, data_what
, mount_opts
, /* filtered= */ NULL
);
254 r
= generator_write_device_timeout(arg_dest_late
, hash_what
, mount_opts
, /* filtered= */ NULL
);
258 r
= generator_add_symlink(arg_dest_late
, dd
, "wants", n
);
262 r
= generator_add_symlink(arg_dest_late
, dh
, "wants", n
);
266 _cleanup_free_
char *dmname
= NULL
;
267 dmname
= strjoin("dev-mapper-", e
, ".device");
271 r
= write_drop_in_format(
273 dmname
, 50, "job-timeout",
274 "# Automatically generated by systemd-gpt-auto-generator\n\n"
276 "JobTimeoutSec=infinity"); /* the binary handles timeouts anyway */
278 return log_error_errno(r
, "Failed to write device timeout drop-in: %m");
282 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
283 "Partition is Verity protected, but systemd-gpt-auto-generator was compiled without libcryptsetup support.");
288 static int add_mount(
293 MountPointFlags flags
,
295 const char *description
,
298 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *opts_filtered
= NULL
;
299 _cleanup_fclose_
FILE *f
= NULL
;
302 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
303 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
304 * could potentially be understood as specifiers. */
311 log_debug("Adding %s: %s fstype=%s", where
, what
, fstype
?: "(any)");
313 if (streq_ptr(fstype
, "crypto_LUKS")) {
314 /* Mount options passed are determined by partition_pick_mount_options(), whose result
315 * is known to not contain timeout options. */
316 r
= add_cryptsetup(id
, what
, /* mount_opts = */ NULL
, flags
, /* require= */ true, &crypto_what
);
323 r
= dissect_fstype_ok(fstype
);
325 return log_error_errno(r
, "Unable to determine of dissected file system type '%s' is permitted: %m", fstype
);
327 return log_error_errno(
328 SYNTHETIC_ERRNO(EIDRM
),
329 "Refusing to automatically mount uncommon file system '%s' to '%s'.",
333 r
= generator_write_device_timeout(arg_dest_late
, what
, options
, &opts_filtered
);
337 r
= unit_name_from_path(where
, ".mount", &unit
);
339 return log_error_errno(r
, "Failed to generate unit name: %m");
341 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, unit
, &f
);
348 "Documentation=man:systemd-gpt-auto-generator(8)\n",
352 fprintf(f
, "Before=%s\n", post
);
354 /* NB: here we do not write to arg_dest_late, but to arg_dest! We typically leave the normal
355 * generator drop-in dir for explicit configuration via systemd-fstab-generator or similar, and put
356 * out automatic configuration in the arg_dest_late directory. But this one is an exception, since we
357 * need to override the static version of the fsck root service file. */
358 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
, opts_filtered
);
362 r
= generator_write_blockdev_dependency(f
, what
);
374 fprintf(f
, "Type=%s\n", fstype
);
377 fprintf(f
, "Options=%s\n", opts_filtered
);
379 r
= generator_write_mount_timeout(f
, where
, opts_filtered
);
383 r
= fflush_and_check(f
);
385 return log_error_errno(r
, "Failed to write unit %s: %m", unit
);
387 if (FLAGS_SET(flags
, MOUNT_VALIDATEFS
)) {
388 r
= generator_hook_up_validatefs(arg_dest_late
, where
, post
);
393 if (FLAGS_SET(flags
, MOUNT_GROWFS
)) {
394 r
= generator_hook_up_growfs(arg_dest_late
, where
, post
);
399 if (FLAGS_SET(flags
, MOUNT_MEASURE
)) {
400 r
= generator_hook_up_pcrfs(arg_dest_late
, where
, post
);
406 r
= generator_add_symlink(arg_dest_late
, post
, "requires", unit
);
414 static int path_is_busy(const char *where
) {
419 /* already a mountpoint; generators run during reload */
420 r
= path_is_mount_point_full(where
, /* root = */ NULL
, AT_SYMLINK_FOLLOW
);
423 /* The directory will be created by the mount or automount unit when it is started. */
428 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
430 /* not a mountpoint but it contains files */
431 r
= dir_is_empty(where
, /* ignore_hidden_or_backup= */ false);
433 log_debug("\"%s\" is not a directory, ignoring.", where
);
436 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
438 log_debug("\"%s\" already populated, ignoring.", where
);
445 static int add_partition_mount(
446 PartitionDesignator d
,
447 DissectedPartition
*p
,
450 const char *description
) {
452 _cleanup_free_
char *options
= NULL
;
457 r
= path_is_busy(where
);
459 return r
< 0 ? r
: 0;
461 r
= partition_pick_mount_options(
463 dissected_partition_fstype(p
),
467 /* ret_ms_flags= */ NULL
);
476 (p
->rw
? MOUNT_RW
: 0) |
478 (p
->growfs
? MOUNT_GROWFS
: 0) |
479 (STR_IN_SET(id
, "root", "var") ? MOUNT_MEASURE
: 0), /* by default measure rootfs and /var, since they contain the "identity" of the system */
482 SPECIAL_LOCAL_FS_TARGET
);
485 static int add_partition_swap(DissectedPartition
*p
) {
487 _cleanup_free_
char *name
= NULL
, *crypto_what
= NULL
;
488 _cleanup_fclose_
FILE *f
= NULL
;
494 if (!arg_swap_enabled
)
497 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
498 r
= fstab_has_fstype("swap");
500 return log_error_errno(r
, "Failed to parse fstab: %m");
502 log_debug("swap specified in fstab, ignoring.");
506 if (streq_ptr(p
->fstype
, "crypto_LUKS")) {
507 r
= add_cryptsetup("swap", p
->node
, /* mount_opts = */ NULL
, MOUNT_RW
, /* require= */ true, &crypto_what
);
514 log_debug("Adding swap: %s", what
);
516 r
= unit_name_from_path(what
, ".swap", &name
);
518 return log_error_errno(r
, "Failed to generate unit name: %m");
520 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, name
, &f
);
526 "Description=Swap Partition\n"
527 "Documentation=man:systemd-gpt-auto-generator(8)\n");
529 r
= generator_write_blockdev_dependency(f
, what
);
539 r
= fflush_and_check(f
);
541 return log_error_errno(r
, "Failed to write unit %s: %m", name
);
543 return generator_add_symlink(arg_dest_late
, SPECIAL_SWAP_TARGET
, "wants", name
);
546 static int add_automount(
551 MountPointFlags flags
,
553 const char *description
,
556 _cleanup_free_
char *unit
= NULL
;
557 _cleanup_fclose_
FILE *f
= NULL
;
575 r
= unit_name_from_path(where
, ".automount", &unit
);
577 return log_error_errno(r
, "Failed to generate unit name: %m");
579 r
= generator_open_unit_file(arg_dest_late
, /* source = */ NULL
, unit
, &f
);
586 "Documentation=man:systemd-gpt-auto-generator(8)\n"
589 "TimeoutIdleSec="USEC_FMT
"\n",
592 timeout
/ USEC_PER_SEC
);
594 r
= fflush_and_check(f
);
596 return log_error_errno(r
, "Failed to write unit %s: %m", unit
);
598 return generator_add_symlink(arg_dest_late
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
601 static int add_partition_xbootldr(DissectedPartition
*p
) {
602 _cleanup_free_
char *options
= NULL
;
606 assert(!in_initrd());
608 r
= path_is_busy("/boot");
614 r
= partition_pick_mount_options(
616 dissected_partition_fstype(p
),
618 /* discard= */ false,
620 /* ret_ms_flags= */ NULL
);
622 return log_error_errno(r
, "Failed to determine default mount options for /boot/: %m");
624 return add_automount(
631 "Boot Loader Partition",
632 LOADER_PARTITION_IDLE_USEC
);
636 static int add_partition_esp(DissectedPartition
*p
, bool has_xbootldr
) {
637 const char *esp_path
= NULL
, *id
= NULL
;
638 _cleanup_free_
char *options
= NULL
;
642 assert(!in_initrd());
644 /* Check if there's an existing fstab entry for ESP. If so, we just skip the gpt-auto logic. */
645 r
= fstab_has_node(p
->node
);
647 log_warning_errno(r
, "Failed to check if fstab entry for device '%s' exists, ignoring: %m",
652 /* If XBOOTLDR partition is not present and /boot/ is unused and empty, we'll take that.
653 * Otherwise, if /efi/ is unused and empty (or missing), we'll take that.
654 * Otherwise, we do nothing. */
656 r
= path_is_busy("/boot");
666 r
= path_is_busy("/efi");
676 r
= partition_pick_mount_options(
678 dissected_partition_fstype(p
),
680 /* discard= */ false,
682 /* ret_ms_flags= */ NULL
);
684 return log_error_errno(r
, "Failed to determine default mount options for %s: %m", esp_path
);
686 return add_automount(
693 "EFI System Partition Automount",
694 LOADER_PARTITION_IDLE_USEC
);
697 static int add_partition_esp(DissectedPartition
*p
, bool has_xbootldr
) {
702 static int add_partition_root_rw(DissectedPartition
*p
) {
707 assert(!in_initrd());
709 /* Invoked on the main system (not initrd), to honour GPT flag 60 on the root fs (ro) */
711 if (arg_root_rw
>= 0) {
712 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
717 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
721 r
= generator_enable_remount_fs_service(arg_dest_late
);
725 path
= strjoina(arg_dest_late
, "/systemd-remount-fs.service.d/50-remount-rw.conf");
727 r
= write_string_file(path
,
728 "# Automatically generated by systemd-gpt-auto-generator\n\n"
730 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
731 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
|WRITE_STRING_FILE_MKDIR_0755
);
733 return log_error_errno(r
, "Failed to write drop-in file %s: %m", path
);
738 static int add_partition_root_growfs(DissectedPartition
*p
) {
741 assert(!in_initrd());
743 /* Invoked on the main system (not initrd), to honour GPT flag 59 on the root fs (growfs) */
746 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.");
750 return generator_hook_up_growfs(arg_dest_late
, "/", SPECIAL_LOCAL_FS_TARGET
);
753 static int add_partition_root_flags(DissectedPartition
*p
) {
757 assert(!in_initrd());
759 RET_GATHER(r
, add_partition_root_growfs(p
));
760 RET_GATHER(r
, add_partition_root_rw(p
));
766 static int add_root_cryptsetup(void) {
767 #if HAVE_LIBCRYPTSETUP
769 assert(arg_auto_root
!= GPT_AUTO_ROOT_OFF
);
771 /* If a device /dev/gpt-auto-root-luks or /dev/disk/by-designator/root-luks appears, then make it
772 * pull in systemd-cryptsetup@root.service, which sets it up, and causes /dev/gpt-auto-root or
773 * /dev/disk/by-designator/root to appear which is all we are looking for. */
776 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
777 "/dev/disk/by-designator/root-luks" : "/dev/gpt-auto-root-luks";
779 if (IN_SET(arg_auto_root
, GPT_AUTO_ROOT_FORCE
, GPT_AUTO_ROOT_DISSECT_FORCE
)) {
780 /* Similar logic as in add_root_mount(), see below */
781 FactoryResetMode f
= factory_reset_mode();
783 log_warning_errno(f
, "Failed to determine whether we are in factory reset mode, assuming not: %m");
785 if (IN_SET(f
, FACTORY_RESET_ON
, FACTORY_RESET_COMPLETE
))
787 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
788 "/dev/disk/by-designator/root-luks-ignore-factory-reset" :
789 "/dev/gpt-auto-root-luks-ignore-factory-reset";
792 return add_cryptsetup(
796 MOUNT_RW
|MOUNT_MEASURE
,
797 /* require= */ false,
798 /* ret_device= */ NULL
);
805 static int add_root_mount(void) {
807 _cleanup_free_
char *options
= NULL
;
810 /* Explicitly disabled? Then exit immediately */
811 if (arg_auto_root
== GPT_AUTO_ROOT_OFF
)
814 /* Neither explicitly enabled nor disabled? Then decide based on the EFI partition variables to be set */
815 if (arg_auto_root
< 0) {
816 if (!is_efi_boot()) {
817 log_debug("Not an EFI boot, not creating root mount.");
821 r
= efi_loader_get_device_part_uuid(/* ret= */ NULL
);
823 log_notice("EFI loader partition unknown, not processing %s.\n"
824 "(The boot loader did not set EFI variable LoaderDevicePartUUID.)",
825 in_initrd() ? "/sysroot" : "/");
829 return log_error_errno(r
, "Failed to read loader partition UUID: %m");
832 /* OK, we shall look for a root device, so let's wait for a root device to show up. A udev rule will
833 * create the link for us under the right name.
835 * There are two distinct names: the /dev/gpt-auto-root-ignore-factory-reset symlink is created for
836 * the root partition if factory reset mode is enabled or complete, and the /dev/gpt-auto-root
837 * symlink is only created if factory reset mode is off or already complete (thus taking factory
838 * reset state into account). In scenarios where the root disk is partially reformatted during
839 * factory reset the latter is the link to use, otherwise the former (so that we don't accidentally
840 * mount a root partition too early that is about to be wiped and replaced by another one). */
843 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
844 "/dev/disk/by-designator/root" : "/dev/gpt-auto-root";
845 if (IN_SET(arg_auto_root
, GPT_AUTO_ROOT_FORCE
, GPT_AUTO_ROOT_DISSECT_FORCE
)) {
846 FactoryResetMode f
= factory_reset_mode();
848 log_warning_errno(f
, "Failed to determine whether we are in factory reset mode, assuming not: %m");
850 if (IN_SET(f
, FACTORY_RESET_ON
, FACTORY_RESET_COMPLETE
))
852 IN_SET(arg_auto_root
, GPT_AUTO_ROOT_DISSECT
, GPT_AUTO_ROOT_DISSECT_FORCE
) ?
853 "/dev/disk/by-designator/root-ignore-factory-reset" :
854 "/dev/gpt-auto-root-ignore-factory-reset";
858 r
= generator_write_initrd_root_device_deps(arg_dest_late
, bdev
);
862 r
= add_root_cryptsetup();
866 /* If a device /dev/disk/by-designator/root-verity or
867 * /dev/disk/by-designator/root-verity-data appears, then make it pull in
868 * systemd-cryptsetup@root.service, which sets it up, and causes /dev/disk/by-designator/root
872 "/dev/disk/by-designator/root-verity-data",
873 "/dev/disk/by-designator/root-verity",
879 /* Note that we do not need to enable systemd-remount-fs.service here. If /etc/fstab exists,
880 * systemd-fstab-generator will pull it in for us, and otherwise add_partition_root_flags() will do
881 * it, after the initrd transition. */
883 r
= partition_pick_mount_options(
889 /* ret_ms_flags= */ NULL
);
891 return log_error_errno(r
, "Failed to pick root mount options: %m");
893 if (arg_root_options
)
894 if (!strextend_with_separator(&options
, ",", arg_root_options
))
900 in_initrd() ? "/sysroot" : "/",
902 (arg_root_rw
> 0 ? MOUNT_RW
: 0) |
903 (in_initrd() ? MOUNT_VALIDATEFS
: 0) |
907 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
913 static int add_usr_mount(void) {
917 /* /usr/ discovery must be enabled explicitly. */
918 if (arg_auto_usr
<= 0)
921 /* We do not support the other gpt-auto modes for /usr/, but the parser should already have checked that. */
922 assert(arg_auto_usr
== GPT_AUTO_ROOT_DISSECT
);
924 if (arg_root_fstype
&& !arg_usr_fstype
) {
925 arg_usr_fstype
= strdup(arg_root_fstype
);
930 if (arg_root_options
&& !arg_usr_options
) {
931 arg_usr_options
= strdup(arg_root_options
);
932 if (!arg_usr_options
)
939 "/dev/disk/by-designator/usr-luks",
941 MOUNT_RW
|MOUNT_MEASURE
,
942 /* require= */ false,
943 /* ret_device= */ NULL
);
947 /* If a device /dev/disk/by-designator/usr-verity or
948 * /dev/disk/by-designator/usr-verity-data appears, then make it pull in
949 * systemd-cryptsetup@usr.service, which sets it up, and causes /dev/disk/by-designator/usr
953 "/dev/disk/by-designator/usr-verity-data",
954 "/dev/disk/by-designator/usr-verity",
960 _cleanup_free_
char *options
= NULL
;
961 r
= partition_pick_mount_options(
967 /* ret_ms_flags= */ NULL
);
969 return log_error_errno(r
, "Failed to pick /usr/ mount options: %m");
972 if (!strextend_with_separator(&options
, ",", arg_usr_options
))
976 "/dev/disk/by-designator/usr",
977 in_initrd() ? "/sysusr/usr" : "/usr",
982 in_initrd() ? SPECIAL_INITRD_USR_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
987 log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
989 r
= add_mount("usr-bind",
995 "/usr/ Partition (Final)",
996 SPECIAL_INITRD_FS_TARGET
);
1004 static int process_loader_partitions(DissectedPartition
*esp
, DissectedPartition
*xbootldr
) {
1005 sd_id128_t loader_uuid
;
1011 /* If any paths in fstab look similar to our favorite paths for ESP or XBOOTLDR, we just exit
1012 * early. We also don't bother with cases where one is configured explicitly and the other shall be
1013 * mounted automatically. */
1015 r
= fstab_has_mount_point_prefix_strv(STRV_MAKE("/boot", "/efi"));
1017 log_debug("Found mount entries in the /boot/ or /efi/ hierarchies in fstab, not generating ESP or XBOOTLDR mounts.");
1021 log_debug_errno(r
, "Failed to check fstab existing paths, ignoring: %m");
1023 if (!is_efi_boot()) {
1024 log_debug("Not an EFI boot, skipping loader partition UUID check.");
1028 /* Let's check if LoaderDevicePartUUID points to either ESP or XBOOTLDR. We prefer it pointing
1029 * to the ESP, but we accept XBOOTLDR too. If it points to neither of them, don't mount any
1030 * loader partitions, since they are not the ones used for booting. */
1032 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
1034 log_debug_errno(r
, "EFI loader partition unknown, skipping ESP and XBOOTLDR mounts.");
1038 return log_debug_errno(r
, "Failed to read loader partition UUID, ignoring: %m");
1040 if (esp
->found
&& sd_id128_equal(esp
->uuid
, loader_uuid
))
1043 if (xbootldr
->found
&& sd_id128_equal(xbootldr
->uuid
, loader_uuid
)) {
1044 log_debug("LoaderDevicePartUUID points to XBOOTLDR partition.");
1048 log_debug("LoaderDevicePartUUID points to neither ESP nor XBOOTLDR, ignoring.");
1054 if (xbootldr
->found
)
1055 RET_GATHER(r
, add_partition_xbootldr(xbootldr
));
1057 RET_GATHER(r
, add_partition_esp(esp
, xbootldr
->found
));
1062 static int enumerate_partitions(dev_t devnum
) {
1063 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1064 _cleanup_(loop_device_unrefp
) LoopDevice
*loop
= NULL
;
1065 _cleanup_free_
char *devname
= NULL
;
1068 assert(!in_initrd());
1070 /* Run on the final root fs (not in the initrd), to mount auxiliary partitions, and hook in rw
1071 * remount and growfs of the root partition */
1073 r
= block_get_whole_disk(devnum
, &devnum
);
1075 return log_debug_errno(r
, "Failed to get whole block device for " DEVNUM_FORMAT_STR
": %m",
1076 DEVNUM_FORMAT_VAL(devnum
));
1078 r
= devname_from_devnum(S_IFBLK
, devnum
, &devname
);
1080 return log_debug_errno(r
, "Failed to get device node of " DEVNUM_FORMAT_STR
": %m",
1081 DEVNUM_FORMAT_VAL(devnum
));
1083 /* Let's take a LOCK_SH lock on the block device, in case udevd is already running. If we don't take
1084 * the lock, udevd might end up issuing BLKRRPART in the middle, and we don't want that, since that
1085 * might remove all partitions while we are operating on them. */
1086 r
= loop_device_open_from_path(devname
, O_RDONLY
, LOCK_SH
, &loop
);
1088 return log_debug_errno(r
, "Failed to open %s: %m", devname
);
1090 r
= dissect_loop_device(
1092 &arg_verity_settings
,
1093 /* mount_options= */ NULL
,
1094 arg_image_policy
?: &image_policy_host
,
1096 DISSECT_IMAGE_GPT_ONLY
|
1097 DISSECT_IMAGE_USR_NO_ROOT
|
1098 DISSECT_IMAGE_DISKSEQ_DEVNODE
|
1099 DISSECT_IMAGE_ALLOW_EMPTY
,
1100 /* NB! Unlike most other places where we dissect block devices we do not use
1101 * DISSECT_IMAGE_ADD_PARTITION_DEVICES here: we want that the kernel finds the
1102 * devices, and udev probes them before we mount them via .mount units much later
1103 * on. And thus we also don't set DISSECT_IMAGE_PIN_PARTITION_DEVICES here, because
1104 * we don't actually mount anything immediately. */
1107 bool ok
= r
== -ENOPKG
;
1108 dissect_log_error(ok
? LOG_DEBUG
: LOG_ERR
, r
, devname
, NULL
);
1112 if (m
->partitions
[PARTITION_SWAP
].found
)
1113 RET_GATHER(r
, add_partition_swap(m
->partitions
+ PARTITION_SWAP
));
1115 RET_GATHER(r
, process_loader_partitions(m
->partitions
+ PARTITION_ESP
, m
->partitions
+ PARTITION_XBOOTLDR
));
1117 if (m
->partitions
[PARTITION_HOME
].found
)
1118 RET_GATHER(r
, add_partition_mount(PARTITION_HOME
, m
->partitions
+ PARTITION_HOME
,
1119 "home", "/home", "Home Partition"));
1121 if (m
->partitions
[PARTITION_SRV
].found
)
1122 RET_GATHER(r
, add_partition_mount(PARTITION_SRV
, m
->partitions
+ PARTITION_SRV
,
1123 "srv", "/srv", "Server Data Partition"));
1125 if (m
->partitions
[PARTITION_VAR
].found
)
1126 RET_GATHER(r
, add_partition_mount(PARTITION_VAR
, m
->partitions
+ PARTITION_VAR
,
1127 "var", "/var", "Variable Data Partition"));
1129 if (m
->partitions
[PARTITION_TMP
].found
)
1130 RET_GATHER(r
, add_partition_mount(PARTITION_TMP
, m
->partitions
+ PARTITION_TMP
,
1131 "var-tmp", "/var/tmp", "Temporary Data Partition"));
1133 if (m
->partitions
[PARTITION_ROOT
].found
)
1134 RET_GATHER(r
, add_partition_root_flags(m
->partitions
+ PARTITION_ROOT
));
1139 static int add_mounts(void) {
1146 r
= blockdev_get_root(LOG_ERR
, &devno
);
1150 log_debug("Skipping automatic GPT dissection logic, root file system not backed by a (single) whole block device.");
1154 return enumerate_partitions(devno
);
1157 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
1162 if (proc_cmdline_key_streq(key
, "systemd.gpt_auto") ||
1163 proc_cmdline_key_streq(key
, "rd.systemd.gpt_auto")) {
1165 r
= value
? parse_boolean(value
) : 1;
1167 log_warning_errno(r
, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value
);
1171 } else if (streq(key
, "root")) {
1173 if (proc_cmdline_value_missing(key
, value
))
1176 /* Disable root disk logic if there's a root= value specified (unless it happens to be
1177 * "gpt-auto" or "gpt-auto-force") */
1179 arg_auto_root
= parse_gpt_auto_root("root=", value
);
1180 assert(arg_auto_root
>= 0);
1182 } else if (streq(key
, "roothash")) {
1184 if (proc_cmdline_value_missing(key
, value
))
1187 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
1189 arg_auto_root
= GPT_AUTO_ROOT_OFF
;
1190 log_debug("Disabling root partition auto-detection, roothash= is set.");
1192 arg_verity_settings
.designator
= PARTITION_ROOT
;
1194 free(arg_verity_settings
.root_hash
);
1195 r
= unhexmem(value
, &arg_verity_settings
.root_hash
, &arg_verity_settings
.root_hash_size
);
1197 return log_error_errno(r
, "Failed to parse roothash= from kernel command line: %m");
1199 } else if (streq(key
, "usrhash")) {
1201 if (proc_cmdline_value_missing(key
, value
))
1204 arg_verity_settings
.designator
= PARTITION_USR
;
1206 free(arg_verity_settings
.root_hash
);
1207 r
= unhexmem(value
, &arg_verity_settings
.root_hash
, &arg_verity_settings
.root_hash_size
);
1209 return log_error_errno(r
, "Failed to parse usrhash= from kernel command line: %m");
1211 } else if (streq(key
, "rootfstype")) {
1213 if (proc_cmdline_value_missing(key
, value
))
1216 return free_and_strdup_warn(&arg_root_fstype
, value
);
1218 } else if (streq(key
, "rootflags")) {
1220 if (proc_cmdline_value_missing(key
, value
))
1223 if (!strextend_with_separator(&arg_root_options
, ",", value
))
1226 } else if (streq(key
, "mount.usr")) {
1228 if (proc_cmdline_value_missing(key
, value
))
1231 /* Disable root disk logic if there's a root= value specified (unless it happens to be
1232 * "gpt-auto" or "gpt-auto-force") */
1234 arg_auto_usr
= parse_gpt_auto_root("mount.usr=", value
);
1235 assert(arg_auto_usr
>= 0);
1237 if (IN_SET(arg_auto_usr
, GPT_AUTO_ROOT_ON
, GPT_AUTO_ROOT_FORCE
, GPT_AUTO_ROOT_DISSECT_FORCE
)) {
1238 log_warning("'gpt-auto', 'gpt-auto-force' and 'dissect-force' are not supported for mount.usr=. Automatically resorting to mount.usr=dissect mode instead.");
1239 arg_auto_usr
= GPT_AUTO_ROOT_DISSECT
;
1242 } else if (streq(key
, "mount.usrfstype")) {
1244 if (proc_cmdline_value_missing(key
, value
))
1247 return free_and_strdup_warn(&arg_usr_fstype
, empty_to_null(value
));
1249 } else if (streq(key
, "mount.usrflags")) {
1251 if (proc_cmdline_value_missing(key
, value
))
1254 if (!strextend_with_separator(&arg_usr_options
, ",", value
))
1257 } else if (streq(key
, "rw") && !value
)
1259 else if (streq(key
, "ro") && !value
)
1260 arg_root_rw
= false;
1261 else if (proc_cmdline_key_streq(key
, "systemd.image_policy"))
1262 return parse_image_policy_argument(value
, &arg_image_policy
);
1263 else if (proc_cmdline_key_streq(key
, "systemd.image_filter")) {
1264 _cleanup_(image_filter_freep
) ImageFilter
*f
= NULL
;
1266 r
= image_filter_parse(value
, &f
);
1268 return log_error_errno(r
, "Failed to parse image filter: %s", value
);
1270 image_filter_free(arg_image_filter
);
1271 arg_image_filter
= TAKE_PTR(f
);
1273 } else if (streq(key
, "systemd.swap")) {
1275 r
= value
? parse_boolean(value
) : 1;
1277 log_warning_errno(r
, "Failed to parse swap switch \"%s\", ignoring: %m", value
);
1279 arg_swap_enabled
= r
;
1281 if (!arg_swap_enabled
)
1282 log_debug("Disabling swap partitions auto-detection, systemd.swap=no is defined.");
1289 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
1292 assert_se(arg_dest
= dest
);
1293 assert_se(arg_dest_late
= dest_late
);
1295 if (detect_container() > 0) {
1296 log_debug("In a container, exiting.");
1300 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
1302 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
1305 log_debug("Disabled, exiting.");
1310 RET_GATHER(r
, add_root_mount());
1311 RET_GATHER(r
, add_usr_mount());
1312 RET_GATHER(r
, add_mounts());
1317 DEFINE_MAIN_GENERATOR_FUNCTION(run
);