1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
10 #include <sys/statfs.h>
16 #include "alloc-util.h"
17 #include "blkid-util.h"
18 #include "blockdev-util.h"
19 #include "btrfs-util.h"
20 #include "dirent-util.h"
21 #include "dissect-image.h"
25 #include "fstab-util.h"
26 #include "generator.h"
30 #include "mount-util.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "proc-cmdline.h"
35 #include "specifier.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38 #include "udev-util.h"
39 #include "unit-name.h"
43 static const char *arg_dest
= "/tmp";
44 static bool arg_enabled
= true;
45 static bool arg_root_enabled
= true;
46 static bool arg_root_rw
= false;
48 static int add_cryptsetup(const char *id
, const char *what
, bool rw
, bool require
, char **device
) {
49 _cleanup_free_
char *e
= NULL
, *n
= NULL
, *p
= NULL
, *d
= NULL
, *id_escaped
= NULL
, *what_escaped
= NULL
;
50 _cleanup_fclose_
FILE *f
= NULL
;
57 r
= unit_name_from_path(what
, ".device", &d
);
59 return log_error_errno(r
, "Failed to generate unit name: %m");
61 e
= unit_name_escape(id
);
65 r
= unit_name_build("systemd-cryptsetup", e
, ".service", &n
);
67 return log_error_errno(r
, "Failed to generate unit name: %m");
69 id_escaped
= specifier_escape(id
);
73 what_escaped
= specifier_escape(what
);
77 p
= strjoin(arg_dest
, "/", n
);
83 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
86 "# Automatically generated by systemd-gpt-auto-generator\n\n"
88 "Description=Cryptography Setup for %%I\n"
89 "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
90 "DefaultDependencies=no\n"
91 "Conflicts=umount.target\n"
92 "BindsTo=dev-mapper-%%i.device %s\n"
93 "Before=umount.target cryptsetup.target\n"
95 "IgnoreOnIsolate=true\n"
98 "RemainAfterExit=yes\n"
99 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
100 "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
101 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '' '%s'\n"
102 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
104 id_escaped
, what_escaped
, rw
? "" : "read-only",
107 r
= fflush_and_check(f
);
109 return log_error_errno(r
, "Failed to write file %s: %m", p
);
111 r
= generator_add_symlink(arg_dest
, d
, "wants", n
);
118 r
= generator_add_symlink(arg_dest
, "cryptsetup.target", "requires", n
);
122 dmname
= strjoina("dev-mapper-", e
, ".device");
123 r
= generator_add_symlink(arg_dest
, dmname
, "requires", n
);
129 p
= strjoin(arg_dest
, "/dev-mapper-", e
, ".device.d/50-job-timeout-sec-0.conf");
133 mkdir_parents_label(p
, 0755);
134 r
= write_string_file(p
,
135 "# Automatically generated by systemd-gpt-auto-generator\n\n"
138 WRITE_STRING_FILE_CREATE
); /* the binary handles timeouts anyway */
140 return log_error_errno(r
, "Failed to write device drop-in: %m");
142 ret
= strappend("/dev/mapper/", id
);
151 static int add_mount(
158 const char *description
,
161 _cleanup_free_
char *unit
= NULL
, *crypto_what
= NULL
, *p
= NULL
;
162 _cleanup_fclose_
FILE *f
= NULL
;
165 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
166 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
167 * could potentially be understood as specifiers. */
174 log_debug("Adding %s: %s %s", where
, what
, strna(fstype
));
176 if (streq_ptr(fstype
, "crypto_LUKS")) {
178 r
= add_cryptsetup(id
, what
, rw
, true, &crypto_what
);
186 r
= unit_name_from_path(where
, ".mount", &unit
);
188 return log_error_errno(r
, "Failed to generate unit name: %m");
190 p
= strjoin(arg_dest
, "/", unit
);
196 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
199 "# Automatically generated by systemd-gpt-auto-generator\n\n"
202 "Documentation=man:systemd-gpt-auto-generator(8)\n",
206 fprintf(f
, "Before=%s\n", post
);
208 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
220 fprintf(f
, "Type=%s\n", fstype
);
223 fprintf(f
, "Options=%s,%s\n", options
, rw
? "rw" : "ro");
225 fprintf(f
, "Options=%s\n", rw
? "rw" : "ro");
227 r
= fflush_and_check(f
);
229 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
232 return generator_add_symlink(arg_dest
, post
, "requires", unit
);
236 static bool path_is_busy(const char *where
) {
239 /* already a mountpoint; generators run during reload */
240 r
= path_is_mount_point(where
, NULL
, AT_SYMLINK_FOLLOW
);
244 /* the directory might not exist on a stateless system */
251 /* not a mountpoint but it contains files */
252 if (dir_is_empty(where
) <= 0)
258 static int add_partition_mount(
259 DissectedPartition
*p
,
262 const char *description
) {
266 if (path_is_busy(where
)) {
267 log_debug("%s already populated, ignoring.", where
);
279 SPECIAL_LOCAL_FS_TARGET
);
282 static int add_swap(const char *path
) {
283 _cleanup_free_
char *name
= NULL
, *unit
= NULL
;
284 _cleanup_fclose_
FILE *f
= NULL
;
289 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
290 r
= fstab_has_fstype("swap");
292 return log_error_errno(r
, "Failed to parse fstab: %m");
294 log_debug("swap specified in fstab, ignoring.");
298 log_debug("Adding swap: %s", path
);
300 r
= unit_name_from_path(path
, ".swap", &name
);
302 return log_error_errno(r
, "Failed to generate unit name: %m");
304 unit
= strjoin(arg_dest
, "/", name
);
308 f
= fopen(unit
, "wxe");
310 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
313 "# Automatically generated by systemd-gpt-auto-generator\n\n"
315 "Description=Swap Partition\n"
316 "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
321 r
= fflush_and_check(f
);
323 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
325 return generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
, "wants", name
);
329 static int add_automount(
336 const char *description
,
339 _cleanup_free_
char *unit
= NULL
;
340 _cleanup_free_
char *opt
, *p
= NULL
;
341 _cleanup_fclose_
FILE *f
= NULL
;
349 opt
= strjoin(options
, ",noauto");
351 opt
= strdup("noauto");
366 r
= unit_name_from_path(where
, ".automount", &unit
);
368 return log_error_errno(r
, "Failed to generate unit name: %m");
370 p
= strjoin(arg_dest
, "/", unit
);
376 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
379 "# Automatically generated by systemd-gpt-auto-generator\n\n"
382 "Documentation=man:systemd-gpt-auto-generator(8)\n"
385 "TimeoutIdleSec="USEC_FMT
"\n",
388 timeout
/ USEC_PER_SEC
);
390 r
= fflush_and_check(f
);
392 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
394 return generator_add_symlink(arg_dest
, SPECIAL_LOCAL_FS_TARGET
, "wants", unit
);
397 static int add_esp(DissectedPartition
*p
) {
404 log_debug("In initrd, ignoring the ESP.");
408 /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice */
409 esp
= access("/efi/", F_OK
) >= 0 ? "/efi" : "/boot";
411 /* We create an .automount which is not overridden by the .mount from the fstab generator. */
412 r
= fstab_is_mount_point(esp
);
414 return log_error_errno(r
, "Failed to parse fstab: %m");
416 log_debug("%s specified in fstab, ignoring.", esp
);
420 if (path_is_busy(esp
)) {
421 log_debug("%s already populated, ignoring.", esp
);
426 sd_id128_t loader_uuid
;
428 /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */
430 r
= efi_loader_get_device_part_uuid(&loader_uuid
);
432 log_debug("EFI loader partition unknown.");
436 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
438 if (!sd_id128_equal(p
->uuid
, loader_uuid
)) {
439 log_debug("Partition for %s does not appear to be the partition we are booted from.", esp
);
443 log_debug("Not an EFI boot, skipping ESP check.");
445 return add_automount("boot",
451 "EFI System Partition Automount",
455 static int add_esp(DissectedPartition
*p
) {
460 static int open_parent(dev_t devnum
, int *ret
) {
461 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
462 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
463 const char *name
, *devtype
, *node
;
464 struct udev_device
*parent
;
474 d
= udev_device_new_from_devnum(udev
, 'b', devnum
);
478 name
= udev_device_get_devnode(d
);
480 name
= udev_device_get_syspath(d
);
482 log_debug("Device %u:%u does not have a name, ignoring.", major(devnum
), minor(devnum
));
486 parent
= udev_device_get_parent(d
);
488 log_debug("%s: not a partitioned device, ignoring.", name
);
492 /* Does it have a devtype? */
493 devtype
= udev_device_get_devtype(parent
);
495 log_debug("%s: parent doesn't have a device type, ignoring.", name
);
499 /* Is this a disk or a partition? We only care for disks... */
500 if (!streq(devtype
, "disk")) {
501 log_debug("%s: parent isn't a raw disk, ignoring.", name
);
505 /* Does it have a device node? */
506 node
= udev_device_get_devnode(parent
);
508 log_debug("%s: parent device does not have device node, ignoring.", name
);
512 log_debug("%s: root device %s.", name
, node
);
514 pn
= udev_device_get_devnum(parent
);
515 if (major(pn
) == 0) {
516 log_debug("%s: parent device is not a proper block device, ignoring.", name
);
520 fd
= open(node
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
522 return log_error_errno(errno
, "Failed to open %s: %m", node
);
532 static int enumerate_partitions(dev_t devnum
) {
534 _cleanup_close_
int fd
= -1;
535 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
538 r
= open_parent(devnum
, &fd
);
542 r
= dissect_image(fd
, NULL
, 0, DISSECT_IMAGE_GPT_ONLY
, &m
);
544 log_debug_errno(r
, "No suitable partition table found, ignoring.");
548 return log_error_errno(r
, "Failed to dissect: %m");
550 if (m
->partitions
[PARTITION_SWAP
].found
) {
551 k
= add_swap(m
->partitions
[PARTITION_SWAP
].node
);
556 if (m
->partitions
[PARTITION_ESP
].found
) {
557 k
= add_esp(m
->partitions
+ PARTITION_ESP
);
562 if (m
->partitions
[PARTITION_HOME
].found
) {
563 k
= add_partition_mount(m
->partitions
+ PARTITION_HOME
, "home", "/home", "Home Partition");
568 if (m
->partitions
[PARTITION_SRV
].found
) {
569 k
= add_partition_mount(m
->partitions
+ PARTITION_SRV
, "srv", "/srv", "Server Data Partition");
577 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
582 if (STR_IN_SET(key
, "systemd.gpt_auto", "rd.systemd.gpt_auto")) {
584 r
= value
? parse_boolean(value
) : 1;
586 log_warning("Failed to parse gpt-auto switch \"%s\". Ignoring.", value
);
590 } else if (streq(key
, "root")) {
592 if (proc_cmdline_value_missing(key
, value
))
595 /* Disable root disk logic if there's a root= value
596 * specified (unless it happens to be "gpt-auto") */
598 arg_root_enabled
= streq(value
, "gpt-auto");
600 } else if (streq(key
, "roothash")) {
602 if (proc_cmdline_value_missing(key
, value
))
605 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
607 arg_root_enabled
= false;
609 } else if (streq(key
, "rw") && !value
)
611 else if (streq(key
, "ro") && !value
)
618 static int add_root_cryptsetup(void) {
620 /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which
621 * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */
623 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL
);
627 static int add_root_mount(void) {
632 if (!is_efi_boot()) {
633 log_debug("Not a EFI boot, not creating root mount.");
637 r
= efi_loader_get_device_part_uuid(NULL
);
639 log_debug("EFI loader partition unknown, exiting.");
642 return log_error_errno(r
, "Failed to read ESP partition UUID: %m");
644 /* OK, we have an ESP partition, this is fantastic, so let's
645 * wait for a root device to show up. A udev rule will create
646 * the link for us under the right name. */
649 r
= generator_write_initrd_root_device_deps(arg_dest
, "/dev/gpt-auto-root");
653 r
= add_root_cryptsetup();
660 "/dev/gpt-auto-root",
661 in_initrd() ? "/sysroot" : "/",
666 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET
: SPECIAL_LOCAL_FS_TARGET
);
672 static int add_mounts(void) {
676 r
= get_block_device_harder("/", &devno
);
678 return log_error_errno(r
, "Failed to determine block device of root file system: %m");
680 r
= get_block_device_harder("/usr", &devno
);
682 return log_error_errno(r
, "Failed to determine block device of /usr file system: %m");
684 log_debug("Neither root nor /usr file system are on a (single) block device.");
689 return enumerate_partitions(devno
);
692 int main(int argc
, char *argv
[]) {
695 if (argc
> 1 && argc
!= 4) {
696 log_error("This program takes three or no arguments.");
703 log_set_prohibit_ipc(true);
704 log_set_target(LOG_TARGET_AUTO
);
705 log_parse_environment();
710 if (detect_container() > 0) {
711 log_debug("In a container, exiting.");
715 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
717 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
720 log_debug("Disabled, exiting.");
724 if (arg_root_enabled
)
725 r
= add_root_mount();
735 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;