1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <sys/statfs.h>
11 #include "alloc-util.h"
12 #include "blkid-util.h"
13 #include "blockdev-util.h"
14 #include "btrfs-util.h"
15 #include "device-util.h"
16 #include "dirent-util.h"
17 #include "dissect-image.h"
22 #include "fstab-util.h"
23 #include "generator.h"
27 #include "mountpoint-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "proc-cmdline.h"
32 #include "specifier.h"
33 #include "stat-util.h"
34 #include "string-util.h"
36 #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 int arg_root_rw
= -1;
45 static int add_cryptsetup(const char *id
, const char *what
, bool rw
, bool require
, char **device
) {
46 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *id_escaped
= NULL
, *what_escaped
= NULL
;
47 _cleanup_fclose_
FILE *f
= NULL
;
54 r
= unit_name_from_path(what
, ".device", &d
);
56 return log_error_errno(r
, "Failed to generate unit name: %m");
58 e
= unit_name_escape(id
);
62 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
64 return log_error_errno(r
, "Failed to generate unit name: %m");
66 id_escaped
= specifier_escape(id
);
70 what_escaped
= specifier_escape(what
);
74 p
= prefix_roota(arg_dest
, n
);
77 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
80 "# Automatically generated by systemd-gpt-auto-generator\n\n"
82 "Description=Cryptography Setup for %%I\n"
83 "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
84 "DefaultDependencies=no\n"
85 "Conflicts=umount.target\n"
86 "BindsTo=dev-mapper-%%i.device %s\n"
87 "Before=umount.target cryptsetup.target\n"
89 "IgnoreOnIsolate=true\n"
92 "RemainAfterExit=yes\n"
93 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
94 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
95 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '' '%s'\n"
96 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
98 id_escaped
, what_escaped
, rw
? "" : "read-only",
101 r
= fflush_and_check(f
);
103 return log_error_errno(r
, "Failed to write file %s: %m", p
);
105 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
112 r
= generator_add_symlink(arg_dest
, "cryptsetup.target", "requires", n
);
116 dmname
= strjoina("dev-mapper-", e
, ".device");
117 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
122 p
= strjoina(arg_dest
, "/dev-mapper-", e
, ".device.d/50-job-timeout-sec-0.conf");
123 mkdir_parents_label(p
, 0755);
124 r
= write_string_file(p
,
125 "# Automatically generated by systemd-gpt-auto-generator\n\n"
128 WRITE_STRING_FILE_CREATE
); /* the binary handles timeouts anyway */
130 return log_error_errno(r
, "Failed to write device drop-in: %m");
135 ret
= path_join("/dev/mapper", id
);
145 static int add_mount(
152 const char *description
,
155 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *p
= NULL
;
156 _cleanup_fclose_
FILE *f
= NULL
;
159 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
160 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
161 * could potentially be understood as specifiers. */
168 log_debug("Adding %s: %s %s", where
, what
, strna(fstype
));
170 if (streq_ptr(fstype
, "crypto_LUKS")) {
172 r
= add_cryptsetup(id
, what
, rw
, true, &crypto_what
);
180 r
= unit_name_from_path(where
, ".mount", &unit
);
182 return log_error_errno(r
, "Failed to generate unit name: %m");
184 p
= path_join(empty_to_root(arg_dest
), unit
);
190 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
193 "# Automatically generated by systemd-gpt-auto-generator\n\n"
196 "Documentation=man:systemd-gpt-auto-generator(8)\n",
200 fprintf(f
, "Before=%s\n", post
);
202 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
214 fprintf(f
, "Type=%s\n", fstype
);
217 fprintf(f
, "Options=%s,%s\n", options
, rw
? "rw" : "ro");
219 fprintf(f
, "Options=%s\n", rw
? "rw" : "ro");
221 r
= fflush_and_check(f
);
223 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
226 return generator_add_symlink(arg_dest
, post
, "requires", unit
);
230 static int path_is_busy(const char *where
) {
233 /* already a mountpoint; generators run during reload */
234 r
= path_is_mount_point(where
, NULL
, AT_SYMLINK_FOLLOW
);
238 /* the directory might not exist on a stateless system */
243 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
245 /* not a mountpoint but it contains files */
246 r
= dir_is_empty(where
);
248 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
252 log_debug("\"%s\" already populated, ignoring.", where
);
256 static int add_partition_mount(
257 DissectedPartition
*p
,
260 const char *description
) {
265 r
= path_is_busy(where
);
267 return r
< 0 ? r
: 0;
277 SPECIAL_LOCAL_FS_TARGET
);
280 static int add_swap(const char *path
) {
281 _cleanup_free_
char *name
= NULL
, *unit
= NULL
;
282 _cleanup_fclose_
FILE *f
= NULL
;
287 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
288 r
= fstab_has_fstype("swap");
290 return log_error_errno(r
, "Failed to parse fstab: %m");
292 log_debug("swap specified in fstab, ignoring.");
296 log_debug("Adding swap: %s", path
);
298 r
= unit_name_from_path(path
, ".swap", &name
);
300 return log_error_errno(r
, "Failed to generate unit name: %m");
302 unit
= path_join(empty_to_root(arg_dest
), name
);
306 f
= fopen(unit
, "wxe");
308 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
311 "# Automatically generated by systemd-gpt-auto-generator\n\n"
313 "Description=Swap Partition\n"
314 "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
319 r
= fflush_and_check(f
);
321 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
323 return generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
, "wants", name
);
326 static int add_automount(
333 const char *description
,
336 _cleanup_free_
char *unit
= NULL
;
337 _cleanup_fclose_
FILE *f
= NULL
;
338 const char *opt
= "noauto", *p
;
346 opt
= strjoina(options
, ",", opt
);
359 r
= unit_name_from_path(where
, ".automount", &unit
);
361 return log_error_errno(r
, "Failed to generate unit name: %m");
363 p
= prefix_roota(arg_dest
, unit
);
366 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
369 "# Automatically generated by systemd-gpt-auto-generator\n\n"
372 "Documentation=man:systemd-gpt-auto-generator(8)\n"
375 "TimeoutIdleSec="USEC_FMT
"\n",
378 timeout
/ USEC_PER_SEC
);
380 r
= fflush_and_check(f
);
382 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
384 return generator_add_symlink(arg_dest
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
387 static int add_xbootldr(DissectedPartition
*p
) {
393 log_debug("In initrd, ignoring the XBOOTLDR partition.");
397 r
= fstab_is_mount_point("/boot");
399 return log_error_errno(r
, "Failed to parse fstab: %m");
401 log_debug("/boot specified in fstab, ignoring XBOOTLDR partition.");
405 r
= path_is_busy("/boot");
411 return add_automount("boot",
417 "Boot Loader Partition",
422 static int add_esp(DissectedPartition
*p
, bool has_xbootldr
) {
423 const char *esp_path
= NULL
, *id
= NULL
;
429 log_debug("In initrd, ignoring the ESP.");
433 /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice, but
434 * only if there's no explicit XBOOTLDR partition around. */
435 if (access("/efi", F_OK
) < 0) {
437 return log_error_errno(errno
, "Failed to determine whether /efi exists: %m");
439 /* Use /boot as fallback, but only if there's no XBOOTLDR partition */
450 /* We create an .automount which is not overridden by the .mount from the fstab generator. */
451 r
= fstab_is_mount_point(esp_path
);
453 return log_error_errno(r
, "Failed to parse fstab: %m");
455 log_debug("%s specified in fstab, ignoring.", esp_path
);
459 r
= path_is_busy(esp_path
);
466 sd_id128_t loader_uuid
;
468 /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */
470 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
472 log_debug("EFI loader partition unknown.");
476 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
478 if (!sd_id128_equal(p
->uuid
, loader_uuid
)) {
479 log_debug("Partition for %s does not appear to be the partition we are booted from.", p
->node
);
483 log_debug("Not an EFI boot, skipping ESP check.");
485 return add_automount(id
,
491 "EFI System Partition Automount",
495 static int add_esp(DissectedPartition
*p
, bool has_xbootldr
) {
500 static int add_root_rw(DissectedPartition
*p
) {
507 log_debug("In initrd, not generating drop-in for systemd-remount-fs.service.");
511 if (arg_root_rw
>= 0) {
512 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
517 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
521 (void) generator_enable_remount_fs_service(arg_dest
);
523 path
= strjoina(arg_dest
, "/systemd-remount-fs.service.d/50-remount-rw.conf");
525 r
= write_string_file(path
,
526 "# Automatically generated by systemd-gpt-generator\n\n"
528 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
529 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
|WRITE_STRING_FILE_MKDIR_0755
);
531 return log_error_errno(r
, "Failed to write drop-in file %s: %m", path
);
536 static int open_parent_devno(dev_t devnum
, int *ret
) {
537 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
538 const char *name
, *devtype
, *node
;
545 r
= sd_device_new_from_devnum(&d
, 'b', devnum
);
547 return log_debug_errno(r
, "Failed to open device: %m");
549 if (sd_device_get_devname(d
, &name
) < 0) {
550 r
= sd_device_get_syspath(d
, &name
);
552 log_device_debug_errno(d
, r
, "Device %u:%u does not have a name, ignoring: %m", major(devnum
), minor(devnum
));
557 r
= sd_device_get_parent(d
, &parent
);
559 log_device_debug_errno(d
, r
, "Not a partitioned device, ignoring: %m");
563 /* Does it have a devtype? */
564 r
= sd_device_get_devtype(parent
, &devtype
);
566 log_device_debug_errno(parent
, r
, "Parent doesn't have a device type, ignoring: %m");
570 /* Is this a disk or a partition? We only care for disks... */
571 if (!streq(devtype
, "disk")) {
572 log_device_debug(parent
, "Parent isn't a raw disk, ignoring.");
576 /* Does it have a device node? */
577 r
= sd_device_get_devname(parent
, &node
);
579 log_device_debug_errno(parent
, r
, "Parent device does not have device node, ignoring: %m");
583 log_device_debug(d
, "Root device %s.", node
);
585 r
= sd_device_get_devnum(parent
, &pn
);
587 log_device_debug_errno(parent
, r
, "Parent device is not a proper block device, ignoring: %m");
591 fd
= open(node
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
593 return log_error_errno(errno
, "Failed to open %s: %m", node
);
599 static int enumerate_partitions(dev_t devnum
) {
600 _cleanup_close_
int fd
= -1;
601 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
604 r
= open_parent_devno(devnum
, &fd
);
608 r
= dissect_image(fd
, NULL
, 0, DISSECT_IMAGE_GPT_ONLY
|DISSECT_IMAGE_NO_UDEV
, &m
);
610 log_debug_errno(r
, "No suitable partition table found, ignoring.");
614 return log_error_errno(r
, "Failed to dissect: %m");
616 if (m
->partitions
[PARTITION_SWAP
].found
) {
617 k
= add_swap(m
->partitions
[PARTITION_SWAP
].node
);
622 if (m
->partitions
[PARTITION_XBOOTLDR
].found
) {
623 k
= add_xbootldr(m
->partitions
+ PARTITION_XBOOTLDR
);
628 if (m
->partitions
[PARTITION_ESP
].found
) {
629 k
= add_esp(m
->partitions
+ PARTITION_ESP
, m
->partitions
[PARTITION_XBOOTLDR
].found
);
634 if (m
->partitions
[PARTITION_HOME
].found
) {
635 k
= add_partition_mount(m
->partitions
+ PARTITION_HOME
, "home", "/home", "Home Partition");
640 if (m
->partitions
[PARTITION_SRV
].found
) {
641 k
= add_partition_mount(m
->partitions
+ PARTITION_SRV
, "srv", "/srv", "Server Data Partition");
646 if (m
->partitions
[PARTITION_ROOT
].found
) {
647 k
= add_root_rw(m
->partitions
+ PARTITION_ROOT
);
655 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
660 if (proc_cmdline_key_streq(key
, "systemd.gpt_auto") ||
661 proc_cmdline_key_streq(key
, "rd.systemd.gpt_auto")) {
663 r
= value
? parse_boolean(value
) : 1;
665 log_warning_errno(r
, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value
);
669 } else if (proc_cmdline_key_streq(key
, "root")) {
671 if (proc_cmdline_value_missing(key
, value
))
674 /* Disable root disk logic if there's a root= value
675 * specified (unless it happens to be "gpt-auto") */
677 arg_root_enabled
= streq(value
, "gpt-auto");
679 } else if (proc_cmdline_key_streq(key
, "roothash")) {
681 if (proc_cmdline_value_missing(key
, value
))
684 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
686 arg_root_enabled
= false;
688 } else if (proc_cmdline_key_streq(key
, "rw") && !value
)
690 else if (proc_cmdline_key_streq(key
, "ro") && !value
)
697 static int add_root_cryptsetup(void) {
699 /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which
700 * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */
702 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL
);
706 static int add_root_mount(void) {
711 if (!is_efi_boot()) {
712 log_debug("Not a EFI boot, not creating root mount.");
716 r
= efi_loader_get_device_part_uuid(NULL
);
718 log_debug("EFI loader partition unknown, exiting.");
721 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
723 /* OK, we have an ESP partition, this is fantastic, so let's
724 * wait for a root device to show up. A udev rule will create
725 * the link for us under the right name. */
728 r
= generator_write_initrd_root_device_deps(arg_dest
, "/dev/gpt-auto-root");
732 r
= add_root_cryptsetup();
737 /* Note that we do not need to enable systemd-remount-fs.service here. If
738 * /etc/fstab exists, systemd-fstab-generator will pull it in for us. */
742 "/dev/gpt-auto-root",
743 in_initrd() ? "/sysroot" : "/",
748 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
754 static int add_mounts(void) {
758 r
= get_block_device_harder("/", &devno
);
760 return log_error_errno(r
, "Failed to determine block device of root file system: %m");
762 r
= get_block_device_harder("/usr", &devno
);
764 return log_error_errno(r
, "Failed to determine block device of /usr file system: %m");
766 _cleanup_free_
char *p
= NULL
;
769 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
770 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
772 r
= readlink_malloc("/run/systemd/volatile-root", &p
);
774 log_debug("Neither root nor /usr file system are on a (single) block device.");
778 return log_error_errno(r
, "Failed to read symlink /run/systemd/volatile-root: %m");
780 r
= device_path_parse_major_minor(p
, &m
, &devno
);
782 return log_error_errno(r
, "Failed to parse major/minor device node: %m");
784 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Volatile root device is of wrong type.");
788 return enumerate_partitions(devno
);
791 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
794 assert_se(arg_dest
= dest_late
);
796 if (detect_container() > 0) {
797 log_debug("In a container, exiting.");
801 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
803 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
806 log_debug("Disabled, exiting.");
810 if (arg_root_enabled
)
811 r
= add_root_mount();
822 DEFINE_MAIN_GENERATOR_FUNCTION(run
);