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"
21 #include "fstab-util.h"
22 #include "generator.h"
26 #include "mountpoint-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "proc-cmdline.h"
31 #include "specifier.h"
32 #include "stat-util.h"
33 #include "string-util.h"
35 #include "unit-name.h"
39 static const char *arg_dest
= NULL
;
40 static bool arg_enabled
= true;
41 static bool arg_root_enabled
= true;
42 static int arg_root_rw
= -1;
44 static int add_cryptsetup(const char *id
, const char *what
, bool rw
, bool require
, char **device
) {
45 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *id_escaped
= NULL
, *what_escaped
= NULL
;
46 _cleanup_fclose_
FILE *f
= NULL
;
53 r
= unit_name_from_path(what
, ".device", &d
);
55 return log_error_errno(r
, "Failed to generate unit name: %m");
57 e
= unit_name_escape(id
);
61 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
63 return log_error_errno(r
, "Failed to generate unit name: %m");
65 id_escaped
= specifier_escape(id
);
69 what_escaped
= specifier_escape(what
);
73 p
= strjoina(arg_dest
, "/", n
);
76 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
79 "# Automatically generated by systemd-gpt-auto-generator\n\n"
81 "Description=Cryptography Setup for %%I\n"
82 "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
83 "DefaultDependencies=no\n"
84 "Conflicts=umount.target\n"
85 "BindsTo=dev-mapper-%%i.device %s\n"
86 "Before=umount.target cryptsetup.target\n"
88 "IgnoreOnIsolate=true\n"
91 "RemainAfterExit=yes\n"
92 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
93 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
94 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '' '%s'\n"
95 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
97 id_escaped
, what_escaped
, rw
? "" : "read-only",
100 r
= fflush_and_check(f
);
102 return log_error_errno(r
, "Failed to write file %s: %m", p
);
104 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
111 r
= generator_add_symlink(arg_dest
, "cryptsetup.target", "requires", n
);
115 dmname
= strjoina("dev-mapper-", e
, ".device");
116 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
121 p
= strjoina(arg_dest
, "/dev-mapper-", e
, ".device.d/50-job-timeout-sec-0.conf");
122 mkdir_parents_label(p
, 0755);
123 r
= write_string_file(p
,
124 "# Automatically generated by systemd-gpt-auto-generator\n\n"
127 WRITE_STRING_FILE_CREATE
); /* the binary handles timeouts anyway */
129 return log_error_errno(r
, "Failed to write device drop-in: %m");
134 ret
= strappend("/dev/mapper/", id
);
144 static int add_mount(
151 const char *description
,
154 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *p
= NULL
;
155 _cleanup_fclose_
FILE *f
= NULL
;
158 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
159 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
160 * could potentially be understood as specifiers. */
167 log_debug("Adding %s: %s %s", where
, what
, strna(fstype
));
169 if (streq_ptr(fstype
, "crypto_LUKS")) {
171 r
= add_cryptsetup(id
, what
, rw
, true, &crypto_what
);
179 r
= unit_name_from_path(where
, ".mount", &unit
);
181 return log_error_errno(r
, "Failed to generate unit name: %m");
183 p
= strjoin(arg_dest
, "/", unit
);
189 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
192 "# Automatically generated by systemd-gpt-auto-generator\n\n"
195 "Documentation=man:systemd-gpt-auto-generator(8)\n",
199 fprintf(f
, "Before=%s\n", post
);
201 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
213 fprintf(f
, "Type=%s\n", fstype
);
216 fprintf(f
, "Options=%s,%s\n", options
, rw
? "rw" : "ro");
218 fprintf(f
, "Options=%s\n", rw
? "rw" : "ro");
220 r
= fflush_and_check(f
);
222 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
225 return generator_add_symlink(arg_dest
, post
, "requires", unit
);
229 static int path_is_busy(const char *where
) {
232 /* already a mountpoint; generators run during reload */
233 r
= path_is_mount_point(where
, NULL
, AT_SYMLINK_FOLLOW
);
237 /* the directory might not exist on a stateless system */
242 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
244 /* not a mountpoint but it contains files */
245 r
= dir_is_empty(where
);
247 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
251 log_debug("\"%s\" already populated, ignoring.", where
);
255 static int add_partition_mount(
256 DissectedPartition
*p
,
259 const char *description
) {
264 r
= path_is_busy(where
);
266 return r
< 0 ? r
: 0;
276 SPECIAL_LOCAL_FS_TARGET
);
279 static int add_swap(const char *path
) {
280 _cleanup_free_
char *name
= NULL
, *unit
= NULL
;
281 _cleanup_fclose_
FILE *f
= NULL
;
286 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
287 r
= fstab_has_fstype("swap");
289 return log_error_errno(r
, "Failed to parse fstab: %m");
291 log_debug("swap specified in fstab, ignoring.");
295 log_debug("Adding swap: %s", path
);
297 r
= unit_name_from_path(path
, ".swap", &name
);
299 return log_error_errno(r
, "Failed to generate unit name: %m");
301 unit
= strjoin(arg_dest
, "/", name
);
305 f
= fopen(unit
, "wxe");
307 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
310 "# Automatically generated by systemd-gpt-auto-generator\n\n"
312 "Description=Swap Partition\n"
313 "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
318 r
= fflush_and_check(f
);
320 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
322 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
= strjoina(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_esp(DissectedPartition
*p
) {
394 log_debug("In initrd, ignoring the ESP.");
398 /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice */
399 esp
= access("/efi/", F_OK
) >= 0 ? "/efi" : "/boot";
401 /* We create an .automount which is not overridden by the .mount from the fstab generator. */
402 r
= fstab_is_mount_point(esp
);
404 return log_error_errno(r
, "Failed to parse fstab: %m");
406 log_debug("%s specified in fstab, ignoring.", esp
);
410 r
= path_is_busy(esp
);
412 return r
< 0 ? r
: 0;
415 sd_id128_t loader_uuid
;
417 /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */
419 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
421 log_debug("EFI loader partition unknown.");
425 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
427 if (!sd_id128_equal(p
->uuid
, loader_uuid
)) {
428 log_debug("Partition for %s does not appear to be the partition we are booted from.", esp
);
432 log_debug("Not an EFI boot, skipping ESP check.");
434 return add_automount("boot",
440 "EFI System Partition Automount",
444 static int add_esp(DissectedPartition
*p
) {
449 static int add_root_rw(DissectedPartition
*p
) {
456 log_debug("In initrd, not generating drop-in for systemd-remount-fs.service.");
460 if (arg_root_rw
>= 0) {
461 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
466 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
470 (void) generator_enable_remount_fs_service(arg_dest
);
472 path
= strjoina(arg_dest
, "/systemd-remount-fs.service.d/50-remount-rw.conf");
473 (void) mkdir_parents(path
, 0755);
475 r
= write_string_file(path
,
476 "# Automatically generated by systemd-gpt-generator\n\n"
478 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
479 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
);
481 return log_error_errno(r
, "Failed to write drop-in file %s: %m", path
);
486 static int open_parent(dev_t devnum
, int *ret
) {
487 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
488 const char *name
, *devtype
, *node
;
495 r
= sd_device_new_from_devnum(&d
, 'b', devnum
);
497 return log_debug_errno(r
, "Failed to open device: %m");
499 if (sd_device_get_devname(d
, &name
) < 0) {
500 r
= sd_device_get_syspath(d
, &name
);
502 log_device_debug_errno(d
, r
, "Device %u:%u does not have a name, ignoring: %m", major(devnum
), minor(devnum
));
507 r
= sd_device_get_parent(d
, &parent
);
509 log_device_debug_errno(d
, r
, "Not a partitioned device, ignoring: %m");
513 /* Does it have a devtype? */
514 r
= sd_device_get_devtype(parent
, &devtype
);
516 log_device_debug_errno(parent
, r
, "Parent doesn't have a device type, ignoring: %m");
520 /* Is this a disk or a partition? We only care for disks... */
521 if (!streq(devtype
, "disk")) {
522 log_device_debug(parent
, "Parent isn't a raw disk, ignoring.");
526 /* Does it have a device node? */
527 r
= sd_device_get_devname(parent
, &node
);
529 log_device_debug_errno(parent
, r
, "Parent device does not have device node, ignoring: %m");
533 log_device_debug(d
, "Root device %s.", node
);
535 r
= sd_device_get_devnum(parent
, &pn
);
537 log_device_debug_errno(parent
, r
, "Parent device is not a proper block device, ignoring: %m");
541 fd
= open(node
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
543 return log_error_errno(errno
, "Failed to open %s: %m", node
);
549 static int enumerate_partitions(dev_t devnum
) {
550 _cleanup_close_
int fd
= -1;
551 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
554 r
= open_parent(devnum
, &fd
);
558 r
= dissect_image(fd
, NULL
, 0, DISSECT_IMAGE_GPT_ONLY
|DISSECT_IMAGE_NO_UDEV
, &m
);
560 log_debug_errno(r
, "No suitable partition table found, ignoring.");
564 return log_error_errno(r
, "Failed to dissect: %m");
566 if (m
->partitions
[PARTITION_SWAP
].found
) {
567 k
= add_swap(m
->partitions
[PARTITION_SWAP
].node
);
572 if (m
->partitions
[PARTITION_ESP
].found
) {
573 k
= add_esp(m
->partitions
+ PARTITION_ESP
);
578 if (m
->partitions
[PARTITION_HOME
].found
) {
579 k
= add_partition_mount(m
->partitions
+ PARTITION_HOME
, "home", "/home", "Home Partition");
584 if (m
->partitions
[PARTITION_SRV
].found
) {
585 k
= add_partition_mount(m
->partitions
+ PARTITION_SRV
, "srv", "/srv", "Server Data Partition");
590 if (m
->partitions
[PARTITION_ROOT
].found
) {
591 k
= add_root_rw(m
->partitions
+ PARTITION_ROOT
);
599 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
604 if (proc_cmdline_key_streq(key
, "systemd.gpt_auto") ||
605 proc_cmdline_key_streq(key
, "rd.systemd.gpt_auto")) {
607 r
= value
? parse_boolean(value
) : 1;
609 log_warning_errno(r
, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value
);
613 } else if (proc_cmdline_key_streq(key
, "root")) {
615 if (proc_cmdline_value_missing(key
, value
))
618 /* Disable root disk logic if there's a root= value
619 * specified (unless it happens to be "gpt-auto") */
621 arg_root_enabled
= streq(value
, "gpt-auto");
623 } else if (proc_cmdline_key_streq(key
, "roothash")) {
625 if (proc_cmdline_value_missing(key
, value
))
628 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
630 arg_root_enabled
= false;
632 } else if (proc_cmdline_key_streq(key
, "rw") && !value
)
634 else if (proc_cmdline_key_streq(key
, "ro") && !value
)
641 static int add_root_cryptsetup(void) {
643 /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which
644 * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */
646 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL
);
650 static int add_root_mount(void) {
655 if (!is_efi_boot()) {
656 log_debug("Not a EFI boot, not creating root mount.");
660 r
= efi_loader_get_device_part_uuid(NULL
);
662 log_debug("EFI loader partition unknown, exiting.");
665 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
667 /* OK, we have an ESP partition, this is fantastic, so let's
668 * wait for a root device to show up. A udev rule will create
669 * the link for us under the right name. */
672 r
= generator_write_initrd_root_device_deps(arg_dest
, "/dev/gpt-auto-root");
676 r
= add_root_cryptsetup();
681 /* Note that we do not need to enable systemd-remount-fs.service here. If
682 * /etc/fstab exists, systemd-fstab-generator will pull it in for us. */
686 "/dev/gpt-auto-root",
687 in_initrd() ? "/sysroot" : "/",
692 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
698 static int add_mounts(void) {
702 r
= get_block_device_harder("/", &devno
);
704 return log_error_errno(r
, "Failed to determine block device of root file system: %m");
706 r
= get_block_device_harder("/usr", &devno
);
708 return log_error_errno(r
, "Failed to determine block device of /usr file system: %m");
710 log_debug("Neither root nor /usr file system are on a (single) block device.");
715 return enumerate_partitions(devno
);
718 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
721 assert_se(arg_dest
= dest_late
);
723 if (detect_container() > 0) {
724 log_debug("In a container, exiting.");
728 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
730 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
733 log_debug("Disabled, exiting.");
737 if (arg_root_enabled
)
738 r
= add_root_mount();
749 DEFINE_MAIN_GENERATOR_FUNCTION(run
);