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 "dirent-util.h"
16 #include "dissect-image.h"
20 #include "fstab-util.h"
21 #include "generator.h"
25 #include "mount-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
= "/tmp";
39 static bool arg_enabled
= true;
40 static bool arg_root_enabled
= true;
41 static bool arg_root_rw
= false;
43 static int add_cryptsetup(const char *id
, const char *what
, bool rw
, bool require
, char **device
) {
44 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *d
= NULL
, *id_escaped
= NULL
, *what_escaped
= NULL
;
45 _cleanup_fclose_
FILE *f
= NULL
;
52 r
= unit_name_from_path(what
, ".device", &d
);
54 return log_error_errno(r
, "Failed to generate unit name: %m");
56 e
= unit_name_escape(id
);
60 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
62 return log_error_errno(r
, "Failed to generate unit name: %m");
64 id_escaped
= specifier_escape(id
);
68 what_escaped
= specifier_escape(what
);
72 p
= strjoina(arg_dest
, "/", n
);
75 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
78 "# Automatically generated by systemd-gpt-auto-generator\n\n"
80 "Description=Cryptography Setup for %%I\n"
81 "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
82 "DefaultDependencies=no\n"
83 "Conflicts=umount.target\n"
84 "BindsTo=dev-mapper-%%i.device %s\n"
85 "Before=umount.target cryptsetup.target\n"
87 "IgnoreOnIsolate=true\n"
90 "RemainAfterExit=yes\n"
91 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
92 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
93 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '' '%s'\n"
94 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
96 id_escaped
, what_escaped
, rw
? "" : "read-only",
99 r
= fflush_and_check(f
);
101 return log_error_errno(r
, "Failed to write file %s: %m", p
);
103 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
110 r
= generator_add_symlink(arg_dest
, "cryptsetup.target", "requires", n
);
114 dmname
= strjoina("dev-mapper-", e
, ".device");
115 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
120 p
= strjoina(arg_dest
, "/dev-mapper-", e
, ".device.d/50-job-timeout-sec-0.conf");
121 mkdir_parents_label(p
, 0755);
122 r
= write_string_file(p
,
123 "# Automatically generated by systemd-gpt-auto-generator\n\n"
126 WRITE_STRING_FILE_CREATE
); /* the binary handles timeouts anyway */
128 return log_error_errno(r
, "Failed to write device drop-in: %m");
133 ret
= strappend("/dev/mapper/", id
);
143 static int add_mount(
150 const char *description
,
153 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *p
= NULL
;
154 _cleanup_fclose_
FILE *f
= NULL
;
157 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
158 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
159 * could potentially be understood as specifiers. */
166 log_debug("Adding %s: %s %s", where
, what
, strna(fstype
));
168 if (streq_ptr(fstype
, "crypto_LUKS")) {
170 r
= add_cryptsetup(id
, what
, rw
, true, &crypto_what
);
178 r
= unit_name_from_path(where
, ".mount", &unit
);
180 return log_error_errno(r
, "Failed to generate unit name: %m");
182 p
= strjoin(arg_dest
, "/", unit
);
188 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
191 "# Automatically generated by systemd-gpt-auto-generator\n\n"
194 "Documentation=man:systemd-gpt-auto-generator(8)\n",
198 fprintf(f
, "Before=%s\n", post
);
200 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
212 fprintf(f
, "Type=%s\n", fstype
);
215 fprintf(f
, "Options=%s,%s\n", options
, rw
? "rw" : "ro");
217 fprintf(f
, "Options=%s\n", rw
? "rw" : "ro");
219 r
= fflush_and_check(f
);
221 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
224 return generator_add_symlink(arg_dest
, post
, "requires", unit
);
228 static int path_is_busy(const char *where
) {
231 /* already a mountpoint; generators run during reload */
232 r
= path_is_mount_point(where
, NULL
, AT_SYMLINK_FOLLOW
);
236 /* the directory might not exist on a stateless system */
241 return log_warning_errno(r
, "Cannot check if \"%s\" is a mount point: %m", where
);
243 /* not a mountpoint but it contains files */
244 r
= dir_is_empty(where
);
246 return log_warning_errno(r
, "Cannot check if \"%s\" is empty: %m", where
);
250 log_debug("\"%s\" already populated, ignoring.", where
);
254 static int add_partition_mount(
255 DissectedPartition
*p
,
258 const char *description
) {
263 r
= path_is_busy(where
);
265 return r
< 0 ? r
: 0;
275 SPECIAL_LOCAL_FS_TARGET
);
278 static int add_swap(const char *path
) {
279 _cleanup_free_
char *name
= NULL
, *unit
= NULL
;
280 _cleanup_fclose_
FILE *f
= NULL
;
285 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
286 r
= fstab_has_fstype("swap");
288 return log_error_errno(r
, "Failed to parse fstab: %m");
290 log_debug("swap specified in fstab, ignoring.");
294 log_debug("Adding swap: %s", path
);
296 r
= unit_name_from_path(path
, ".swap", &name
);
298 return log_error_errno(r
, "Failed to generate unit name: %m");
300 unit
= strjoin(arg_dest
, "/", name
);
304 f
= fopen(unit
, "wxe");
306 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
309 "# Automatically generated by systemd-gpt-auto-generator\n\n"
311 "Description=Swap Partition\n"
312 "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
317 r
= fflush_and_check(f
);
319 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
321 return generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
, "wants", name
);
325 static int add_automount(
332 const char *description
,
335 _cleanup_free_
char *unit
= NULL
;
336 _cleanup_fclose_
FILE *f
= NULL
;
337 const char *opt
= "noauto", *p
;
345 opt
= strjoina(options
, ",", opt
);
358 r
= unit_name_from_path(where
, ".automount", &unit
);
360 return log_error_errno(r
, "Failed to generate unit name: %m");
362 p
= strjoina(arg_dest
, "/", unit
);
365 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
368 "# Automatically generated by systemd-gpt-auto-generator\n\n"
371 "Documentation=man:systemd-gpt-auto-generator(8)\n"
374 "TimeoutIdleSec="USEC_FMT
"\n",
377 timeout
/ USEC_PER_SEC
);
379 r
= fflush_and_check(f
);
381 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
383 return generator_add_symlink(arg_dest
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
386 static int add_esp(DissectedPartition
*p
) {
393 log_debug("In initrd, ignoring the ESP.");
397 /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice */
398 esp
= access("/efi/", F_OK
) >= 0 ? "/efi" : "/boot";
400 /* We create an .automount which is not overridden by the .mount from the fstab generator. */
401 r
= fstab_is_mount_point(esp
);
403 return log_error_errno(r
, "Failed to parse fstab: %m");
405 log_debug("%s specified in fstab, ignoring.", esp
);
409 r
= path_is_busy(esp
);
411 return r
< 0 ? r
: 0;
414 sd_id128_t loader_uuid
;
416 /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */
418 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
420 log_debug("EFI loader partition unknown.");
424 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
426 if (!sd_id128_equal(p
->uuid
, loader_uuid
)) {
427 log_debug("Partition for %s does not appear to be the partition we are booted from.", esp
);
431 log_debug("Not an EFI boot, skipping ESP check.");
433 return add_automount("boot",
439 "EFI System Partition Automount",
443 static int add_esp(DissectedPartition
*p
) {
448 static int open_parent(dev_t devnum
, int *ret
) {
449 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
450 const char *name
, *devtype
, *node
;
457 r
= sd_device_new_from_devnum(&d
, 'b', devnum
);
459 return log_debug_errno(r
, "Failed to open device: %m");
461 if (sd_device_get_devname(d
, &name
) < 0) {
462 r
= sd_device_get_syspath(d
, &name
);
464 log_debug_errno(r
, "Device %u:%u does not have a name, ignoring: %m", major(devnum
), minor(devnum
));
469 r
= sd_device_get_parent(d
, &parent
);
471 log_debug_errno(r
, "%s: not a partitioned device, ignoring: %m", name
);
475 /* Does it have a devtype? */
476 r
= sd_device_get_devtype(parent
, &devtype
);
478 log_debug_errno(r
, "%s: parent doesn't have a device type, ignoring: %m", name
);
482 /* Is this a disk or a partition? We only care for disks... */
483 if (!streq(devtype
, "disk")) {
484 log_debug("%s: parent isn't a raw disk, ignoring.", name
);
488 /* Does it have a device node? */
489 r
= sd_device_get_devname(parent
, &node
);
491 log_debug_errno(r
, "%s: parent device does not have device node, ignoring: %m", name
);
495 log_debug("%s: root device %s.", name
, node
);
497 r
= sd_device_get_devnum(parent
, &pn
);
499 log_debug_errno(r
, "%s: parent device is not a proper block device, ignoring: %m", name
);
503 fd
= open(node
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
505 return log_error_errno(errno
, "Failed to open %s: %m", node
);
511 static int enumerate_partitions(dev_t devnum
) {
512 _cleanup_close_
int fd
= -1;
513 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
516 r
= open_parent(devnum
, &fd
);
520 r
= dissect_image(fd
, NULL
, 0, DISSECT_IMAGE_GPT_ONLY
, &m
);
522 log_debug_errno(r
, "No suitable partition table found, ignoring.");
526 return log_error_errno(r
, "Failed to dissect: %m");
528 if (m
->partitions
[PARTITION_SWAP
].found
) {
529 k
= add_swap(m
->partitions
[PARTITION_SWAP
].node
);
534 if (m
->partitions
[PARTITION_ESP
].found
) {
535 k
= add_esp(m
->partitions
+ PARTITION_ESP
);
540 if (m
->partitions
[PARTITION_HOME
].found
) {
541 k
= add_partition_mount(m
->partitions
+ PARTITION_HOME
, "home", "/home", "Home Partition");
546 if (m
->partitions
[PARTITION_SRV
].found
) {
547 k
= add_partition_mount(m
->partitions
+ PARTITION_SRV
, "srv", "/srv", "Server Data Partition");
555 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
560 if (STR_IN_SET(key
, "systemd.gpt_auto", "rd.systemd.gpt_auto")) {
562 r
= value
? parse_boolean(value
) : 1;
564 log_warning("Failed to parse gpt-auto switch \"%s\". Ignoring.", value
);
568 } else if (streq(key
, "root")) {
570 if (proc_cmdline_value_missing(key
, value
))
573 /* Disable root disk logic if there's a root= value
574 * specified (unless it happens to be "gpt-auto") */
576 arg_root_enabled
= streq(value
, "gpt-auto");
578 } else if (streq(key
, "roothash")) {
580 if (proc_cmdline_value_missing(key
, value
))
583 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
585 arg_root_enabled
= false;
587 } else if (streq(key
, "rw") && !value
)
589 else if (streq(key
, "ro") && !value
)
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) {
610 if (!is_efi_boot()) {
611 log_debug("Not a EFI boot, not creating root mount.");
615 r
= efi_loader_get_device_part_uuid(NULL
);
617 log_debug("EFI loader partition unknown, exiting.");
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();
638 "/dev/gpt-auto-root",
639 in_initrd() ? "/sysroot" : "/",
644 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
650 static int add_mounts(void) {
654 r
= get_block_device_harder("/", &devno
);
656 return log_error_errno(r
, "Failed to determine block device of root file system: %m");
658 r
= get_block_device_harder("/usr", &devno
);
660 return log_error_errno(r
, "Failed to determine block device of /usr file system: %m");
662 log_debug("Neither root nor /usr file system are on a (single) block device.");
667 return enumerate_partitions(devno
);
670 int main(int argc
, char *argv
[]) {
673 if (argc
> 1 && argc
!= 4) {
674 log_error("This program takes three or no arguments.");
681 log_set_prohibit_ipc(true);
682 log_set_target(LOG_TARGET_AUTO
);
683 log_parse_environment();
688 if (detect_container() > 0) {
689 log_debug("In a container, exiting.");
693 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
695 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
698 log_debug("Disabled, exiting.");
702 if (arg_root_enabled
)
703 r
= add_root_mount();
713 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;