1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "blkid-util.h"
11 #include "blockdev-util.h"
12 #include "btrfs-util.h"
13 #include "device-util.h"
14 #include "dirent-util.h"
15 #include "dissect-image.h"
17 #include "efi-loader.h"
21 #include "fstab-util.h"
22 #include "generator.h"
25 #include "mountpoint-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "proc-cmdline.h"
30 #include "specifier.h"
31 #include "stat-util.h"
32 #include "string-util.h"
34 #include "unit-name.h"
38 static const char *arg_dest
= NULL
;
39 static bool arg_enabled
= true;
40 static bool arg_root_enabled
= true;
41 static int arg_root_rw
= -1;
43 static int open_parent_block_device(dev_t devnum
, int *ret_fd
) {
44 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
45 const char *name
, *devtype
, *node
;
52 r
= sd_device_new_from_devnum(&d
, 'b', devnum
);
54 return log_debug_errno(r
, "Failed to open device: %m");
56 if (sd_device_get_devname(d
, &name
) < 0) {
57 r
= sd_device_get_syspath(d
, &name
);
59 log_device_debug_errno(d
, r
, "Device %u:%u does not have a name, ignoring: %m",
60 major(devnum
), minor(devnum
));
65 r
= sd_device_get_parent(d
, &parent
);
67 log_device_debug_errno(d
, r
, "Not a partitioned device, ignoring: %m");
71 /* Does it have a devtype? */
72 r
= sd_device_get_devtype(parent
, &devtype
);
74 log_device_debug_errno(parent
, r
, "Parent doesn't have a device type, ignoring: %m");
78 /* Is this a disk or a partition? We only care for disks... */
79 if (!streq(devtype
, "disk")) {
80 log_device_debug(parent
, "Parent isn't a raw disk, ignoring.");
84 /* Does it have a device node? */
85 r
= sd_device_get_devname(parent
, &node
);
87 log_device_debug_errno(parent
, r
, "Parent device does not have device node, ignoring: %m");
91 log_device_debug(d
, "Root device %s.", node
);
93 r
= sd_device_get_devnum(parent
, &pn
);
95 log_device_debug_errno(parent
, r
, "Parent device is not a proper block device, ignoring: %m");
99 fd
= open(node
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
101 return log_error_errno(errno
, "Failed to open %s: %m", node
);
107 static int add_cryptsetup(const char *id
, const char *what
, bool rw
, bool require
, char **device
) {
108 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *id_escaped
= NULL
, *what_escaped
= NULL
;
109 _cleanup_fclose_
FILE *f
= NULL
;
116 r
= unit_name_from_path(what
, ".device", &d
);
118 return log_error_errno(r
, "Failed to generate unit name: %m");
120 e
= unit_name_escape(id
);
124 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
126 return log_error_errno(r
, "Failed to generate unit name: %m");
128 id_escaped
= specifier_escape(id
);
132 what_escaped
= specifier_escape(what
);
136 p
= prefix_roota(arg_dest
, n
);
139 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
142 "# Automatically generated by systemd-gpt-auto-generator\n\n"
144 "Description=Cryptography Setup for %%I\n"
145 "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
146 "DefaultDependencies=no\n"
147 "Conflicts=umount.target\n"
148 "BindsTo=dev-mapper-%%i.device %s\n"
149 "Before=umount.target cryptsetup.target\n"
151 "IgnoreOnIsolate=true\n"
154 "RemainAfterExit=yes\n"
155 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
156 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
157 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '' '%s'\n"
158 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
160 id_escaped
, what_escaped
, rw
? "" : "read-only",
163 r
= fflush_and_check(f
);
165 return log_error_errno(r
, "Failed to write file %s: %m", p
);
167 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
172 dmname
= strjoina("dev-mapper-", e
, ".device");
175 r
= generator_add_symlink(arg_dest
, "cryptsetup.target", "requires", n
);
179 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
184 r
= write_drop_in_format(arg_dest
, dmname
, 50, "job-timeout",
185 "# Automatically generated by systemd-gpt-auto-generator\n\n"
187 "JobTimeoutSec=0"); /* the binary handles timeouts anyway */
189 log_warning_errno(r
, "Failed to write device timeout drop-in, ignoring: %m");
194 ret
= path_join("/dev/mapper", id
);
204 static int add_mount(
211 const char *description
,
214 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *p
= NULL
;
215 _cleanup_fclose_
FILE *f
= NULL
;
218 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
219 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
220 * could potentially be understood as specifiers. */
227 log_debug("Adding %s: %s fstype=%s", where
, what
, fstype
?: "(any)");
229 if (streq_ptr(fstype
, "crypto_LUKS")) {
231 r
= add_cryptsetup(id
, what
, rw
, true, &crypto_what
);
239 r
= unit_name_from_path(where
, ".mount", &unit
);
241 return log_error_errno(r
, "Failed to generate unit name: %m");
243 p
= path_join(empty_to_root(arg_dest
), unit
);
249 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
252 "# Automatically generated by systemd-gpt-auto-generator\n\n"
255 "Documentation=man:systemd-gpt-auto-generator(8)\n",
259 fprintf(f
, "Before=%s\n", post
);
261 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
273 fprintf(f
, "Type=%s\n", fstype
);
276 fprintf(f
, "Options=%s,%s\n", options
, rw
? "rw" : "ro");
278 fprintf(f
, "Options=%s\n", rw
? "rw" : "ro");
280 r
= fflush_and_check(f
);
282 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
285 return generator_add_symlink(arg_dest
, post
, "requires", unit
);
289 static int path_is_busy(const char *where
) {
292 /* already a mountpoint; generators run during reload */
293 r
= path_is_mount_point(where
, NULL
, AT_SYMLINK_FOLLOW
);
297 /* the directory might not exist on a stateless system */
302 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
304 /* not a mountpoint but it contains files */
305 r
= dir_is_empty(where
);
307 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
311 log_debug("\"%s\" already populated, ignoring.", where
);
315 static int add_partition_mount(
316 DissectedPartition
*p
,
319 const char *description
) {
324 r
= path_is_busy(where
);
326 return r
< 0 ? r
: 0;
336 SPECIAL_LOCAL_FS_TARGET
);
339 static int add_swap(const char *path
) {
340 _cleanup_free_
char *name
= NULL
, *unit
= NULL
;
341 _cleanup_fclose_
FILE *f
= NULL
;
346 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
347 r
= fstab_has_fstype("swap");
349 return log_error_errno(r
, "Failed to parse fstab: %m");
351 log_debug("swap specified in fstab, ignoring.");
355 log_debug("Adding swap: %s", path
);
357 r
= unit_name_from_path(path
, ".swap", &name
);
359 return log_error_errno(r
, "Failed to generate unit name: %m");
361 unit
= path_join(empty_to_root(arg_dest
), name
);
365 f
= fopen(unit
, "wxe");
367 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
370 "# Automatically generated by systemd-gpt-auto-generator\n\n"
372 "Description=Swap Partition\n"
373 "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
378 r
= fflush_and_check(f
);
380 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
382 return generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
, "wants", name
);
385 static int add_automount(
392 const char *description
,
395 _cleanup_free_
char *unit
= NULL
;
396 _cleanup_fclose_
FILE *f
= NULL
;
397 const char *opt
= "noauto", *p
;
405 opt
= strjoina(options
, ",", opt
);
418 r
= unit_name_from_path(where
, ".automount", &unit
);
420 return log_error_errno(r
, "Failed to generate unit name: %m");
422 p
= prefix_roota(arg_dest
, unit
);
425 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
428 "# Automatically generated by systemd-gpt-auto-generator\n\n"
431 "Documentation=man:systemd-gpt-auto-generator(8)\n"
434 "TimeoutIdleSec="USEC_FMT
"\n",
437 timeout
/ USEC_PER_SEC
);
439 r
= fflush_and_check(f
);
441 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
443 return generator_add_symlink(arg_dest
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
446 static int add_xbootldr(DissectedPartition
*p
) {
452 log_debug("In initrd, ignoring the XBOOTLDR partition.");
456 r
= fstab_is_mount_point("/boot");
458 return log_error_errno(r
, "Failed to parse fstab: %m");
460 log_debug("/boot specified in fstab, ignoring XBOOTLDR partition.");
464 r
= path_is_busy("/boot");
470 return add_automount("boot",
476 "Boot Loader Partition",
481 static int add_esp(DissectedPartition
*p
, bool has_xbootldr
) {
482 const char *esp_path
= NULL
, *id
= NULL
;
488 log_debug("In initrd, ignoring the ESP.");
492 /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice, but
493 * only if there's no explicit XBOOTLDR partition around. */
494 if (access("/efi", F_OK
) < 0) {
496 return log_error_errno(errno
, "Failed to determine whether /efi exists: %m");
498 /* Use /boot as fallback, but only if there's no XBOOTLDR partition */
509 /* We create an .automount which is not overridden by the .mount from the fstab generator. */
510 r
= fstab_is_mount_point(esp_path
);
512 return log_error_errno(r
, "Failed to parse fstab: %m");
514 log_debug("%s specified in fstab, ignoring.", esp_path
);
518 r
= path_is_busy(esp_path
);
525 sd_id128_t loader_uuid
;
527 /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */
529 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
531 log_debug("EFI loader partition unknown.");
535 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
537 if (!sd_id128_equal(p
->uuid
, loader_uuid
)) {
538 log_debug("Partition for %s does not appear to be the partition we are booted from.", p
->node
);
542 log_debug("Not an EFI boot, skipping ESP check.");
544 return add_automount(id
,
550 "EFI System Partition Automount",
554 static int add_esp(DissectedPartition
*p
, bool has_xbootldr
) {
559 static int add_root_rw(DissectedPartition
*p
) {
566 log_debug("In initrd, not generating drop-in for systemd-remount-fs.service.");
570 if (arg_root_rw
>= 0) {
571 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
576 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
580 (void) generator_enable_remount_fs_service(arg_dest
);
582 path
= strjoina(arg_dest
, "/systemd-remount-fs.service.d/50-remount-rw.conf");
584 r
= write_string_file(path
,
585 "# Automatically generated by systemd-gpt-generator\n\n"
587 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
588 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
|WRITE_STRING_FILE_MKDIR_0755
);
590 return log_error_errno(r
, "Failed to write drop-in file %s: %m", path
);
596 static int add_root_cryptsetup(void) {
598 /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which
599 * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */
601 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL
);
605 static int add_root_mount(void) {
609 if (!is_efi_boot()) {
610 log_debug("Not a EFI boot, not creating root mount.");
614 r
= efi_loader_get_device_part_uuid(NULL
);
616 log_notice("EFI loader partition unknown, exiting.\n"
617 "(The boot loader did not set EFI variable LoaderDevicePartUUID.)");
620 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
622 /* OK, we have an ESP partition, this is fantastic, so let's
623 * wait for a root device to show up. A udev rule will create
624 * the link for us under the right name. */
627 r
= generator_write_initrd_root_device_deps(arg_dest
, "/dev/gpt-auto-root");
631 r
= add_root_cryptsetup();
636 /* Note that we do not need to enable systemd-remount-fs.service here. If
637 * /etc/fstab exists, systemd-fstab-generator will pull it in for us. */
641 "/dev/gpt-auto-root",
642 in_initrd() ? "/sysroot" : "/",
647 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
653 static int enumerate_partitions(dev_t devnum
) {
654 _cleanup_close_
int fd
= -1;
655 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
658 r
= open_parent_block_device(devnum
, &fd
);
662 r
= dissect_image(fd
, NULL
, 0, DISSECT_IMAGE_GPT_ONLY
|DISSECT_IMAGE_NO_UDEV
, &m
);
664 log_debug_errno(r
, "No suitable partition table found, ignoring.");
668 return log_error_errno(r
, "Failed to dissect: %m");
670 if (m
->partitions
[PARTITION_SWAP
].found
) {
671 k
= add_swap(m
->partitions
[PARTITION_SWAP
].node
);
676 if (m
->partitions
[PARTITION_XBOOTLDR
].found
) {
677 k
= add_xbootldr(m
->partitions
+ PARTITION_XBOOTLDR
);
682 if (m
->partitions
[PARTITION_ESP
].found
) {
683 k
= add_esp(m
->partitions
+ PARTITION_ESP
, m
->partitions
[PARTITION_XBOOTLDR
].found
);
688 if (m
->partitions
[PARTITION_HOME
].found
) {
689 k
= add_partition_mount(m
->partitions
+ PARTITION_HOME
, "home", "/home", "Home Partition");
694 if (m
->partitions
[PARTITION_SRV
].found
) {
695 k
= add_partition_mount(m
->partitions
+ PARTITION_SRV
, "srv", "/srv", "Server Data Partition");
700 if (m
->partitions
[PARTITION_ROOT
].found
) {
701 k
= add_root_rw(m
->partitions
+ PARTITION_ROOT
);
709 static int add_mounts(void) {
713 r
= get_block_device_harder("/", &devno
);
715 return log_error_errno(r
, "Failed to determine block device of root file system: %m");
717 r
= get_block_device_harder("/usr", &devno
);
719 return log_error_errno(r
, "Failed to determine block device of /usr file system: %m");
721 _cleanup_free_
char *p
= NULL
;
724 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
725 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
727 r
= readlink_malloc("/run/systemd/volatile-root", &p
);
729 log_debug("Neither root nor /usr file system are on a (single) block device.");
733 return log_error_errno(r
, "Failed to read symlink /run/systemd/volatile-root: %m");
735 r
= device_path_parse_major_minor(p
, &m
, &devno
);
737 return log_error_errno(r
, "Failed to parse major/minor device node: %m");
739 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Volatile root device is of wrong type.");
743 return enumerate_partitions(devno
);
746 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
751 if (proc_cmdline_key_streq(key
, "systemd.gpt_auto") ||
752 proc_cmdline_key_streq(key
, "rd.systemd.gpt_auto")) {
754 r
= value
? parse_boolean(value
) : 1;
756 log_warning_errno(r
, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value
);
760 } else if (proc_cmdline_key_streq(key
, "root")) {
762 if (proc_cmdline_value_missing(key
, value
))
765 /* Disable root disk logic if there's a root= value
766 * specified (unless it happens to be "gpt-auto") */
768 if (!streq(value
, "gpt-auto")) {
769 arg_root_enabled
= false;
770 log_debug("Disabling root partition auto-detection, root= is defined.");
773 } else if (proc_cmdline_key_streq(key
, "roothash")) {
775 if (proc_cmdline_value_missing(key
, value
))
778 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
780 arg_root_enabled
= false;
782 } else if (proc_cmdline_key_streq(key
, "rw") && !value
)
784 else if (proc_cmdline_key_streq(key
, "ro") && !value
)
790 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
793 assert_se(arg_dest
= dest_late
);
795 if (detect_container() > 0) {
796 log_debug("In a container, exiting.");
800 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
802 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
805 log_debug("Disabled, exiting.");
809 if (arg_root_enabled
)
810 r
= add_root_mount();
821 DEFINE_MAIN_GENERATOR_FUNCTION(run
);