HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/gpt-auto-generator/gpt-auto-generator.c

51,914 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 5305957bd93c71ca6cf37a6029b168e77cab4210

Lines 1001–1394 of 1,394

                if (!strextend_with_separator(&options, ",", arg_usr_options))
                        return log_oom();
 
        r = add_mount("usr",
                      "/dev/disk/by-designator/usr",
                      in_initrd() ? "/sysusr/usr" : "/usr",
                      arg_usr_fstype,
                      /* flags= */ 0,
                      options,
                      "/usr/ Partition",
                      in_initrd() ? SPECIAL_INITRD_USR_FS_TARGET : SPECIAL_LOCAL_FS_TARGET,
                      /* conflicts= */ NULL);
        if (r < 0)
                return r;
 
        if (in_initrd()) {
                log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
 
                r = add_mount("usr-bind",
                              "/sysusr/usr",
                              "/sysroot/usr",
                              /* fstype= */ NULL,
                              MOUNT_VALIDATEFS,
                              "bind",
                              "/usr/ Partition (Final)",
                              SPECIAL_INITRD_FS_TARGET,
                              /* conflicts= */ NULL);
                if (r < 0)
                        return r;
        }
#endif
        return 0;
}
 
static int add_early_esp_mount(void) {
        int r;
 
        /* Early ESP discovery is a bit different than the other mounts here: it's purely about the initrd,
         * and goes away during the transition to the host (where it might likely be mounted again, but then
         * via autofs, hence lazily). Moreover, the location is fixed → /sysefi/, i.e. we do not bother with
         * XBOOTLDR vs. ESP for this. Also, the mount is not pulled in by default, but is expected to be
         * pulled in by the component that uses it.
         *
         * The initial usecase for this is software TPM that needs a place to store its state before the root
         * file system can be mounted.
         *
         * Or in other words: this is much simpler, more focussed on a short-lived boot-time operation than
         * the regular logic during later boot. */
 
        if (!in_initrd())
                return 0;
 
        if (!is_efi_boot())
                return 0;
 
        _cleanup_free_ char *options = NULL;
        r = partition_pick_mount_options(
                        PARTITION_ESP,
                        "vfat",
                        /* rw= */ true,
                        /* discard= */ false,
                        &options,
                        /* ret_ms_flags= */ NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to pick ESP mount options: %m");
 
        return add_mount("esp",
                         "/dev/disk/by-designator/esp",
                         "/sysefi/",
                         "vfat",
                         MOUNT_RW,
                         options,
                         "EFI System Partition (Early)",
                         /* post= */ NULL,
                         /* conflicts= */ "initrd-switch-root.target");
}
 
static int process_loader_partitions(DissectedPartition *esp, DissectedPartition *xbootldr) {
        sd_id128_t loader_uuid;
        int r;
 
        assert(esp);
        assert(xbootldr);
 
        /* If any paths in fstab look similar to our favorite paths for ESP or XBOOTLDR, we just exit
         * early. We also don't bother with cases where one is configured explicitly and the other shall be
         * mounted automatically. */
 
        r = fstab_has_mount_point_prefix_strv(STRV_MAKE("/boot", "/efi"));
        if (r > 0) {
                log_debug("Found mount entries in the /boot/ or /efi/ hierarchies in fstab, not generating ESP or XBOOTLDR mounts.");
                return 0;
        }
        if (r < 0)
                log_debug_errno(r, "Failed to check fstab existing paths, ignoring: %m");
 
        if (!is_efi_boot()) {
                log_debug("Not an EFI boot, skipping loader partition UUID check.");
                goto mount;
        }
 
        /* Let's check if LoaderDevicePartUUID points to either ESP or XBOOTLDR. We prefer it pointing
         * to the ESP, but we accept XBOOTLDR too. If it points to neither of them, don't mount any
         * loader partitions, since they are not the ones used for booting. */
 
        r = efi_loader_get_device_part_uuid(&loader_uuid);
        if (r == -ENOENT) {
                log_debug_errno(r, "EFI loader partition unknown, skipping ESP and XBOOTLDR mounts.");
                return 0;
        }
        if (r < 0)
                return log_debug_errno(r, "Failed to read loader partition UUID, ignoring: %m");
 
        if (esp->found && sd_id128_equal(esp->uuid, loader_uuid))
                goto mount;
 
        if (xbootldr->found && sd_id128_equal(xbootldr->uuid, loader_uuid)) {
                log_debug("LoaderDevicePartUUID points to XBOOTLDR partition.");
                goto mount;
        }
 
        log_debug("LoaderDevicePartUUID points to neither ESP nor XBOOTLDR, ignoring.");
        return 0;
 
mount:
        r = 0;
 
        if (xbootldr->found)
                RET_GATHER(r, add_partition_xbootldr(xbootldr));
        if (esp->found)
                RET_GATHER(r, add_partition_esp(esp, xbootldr->found));
 
        return r;
}
 
static int enumerate_partitions(dev_t devnum) {
        _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
        _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
        _cleanup_free_ char *devname = NULL;
        int r;
 
        assert(!in_initrd());
 
        /* Run on the final root fs (not in the initrd), to mount auxiliary partitions, and hook in rw
         * remount and growfs of the root partition */
 
        r = block_get_whole_disk(devnum, &devnum);
        if (r < 0)
                return log_debug_errno(r, "Failed to get whole block device for " DEVNUM_FORMAT_STR ": %m",
                                       DEVNUM_FORMAT_VAL(devnum));
 
        r = devname_from_devnum(S_IFBLK, devnum, &devname);
        if (r < 0)
                return log_debug_errno(r, "Failed to get device node of " DEVNUM_FORMAT_STR ": %m",
                                       DEVNUM_FORMAT_VAL(devnum));
 
        /* Let's take a LOCK_SH lock on the block device, in case udevd is already running. If we don't take
         * the lock, udevd might end up issuing BLKRRPART in the middle, and we don't want that, since that
         * might remove all partitions while we are operating on them. */
        r = loop_device_open_from_path(devname, O_RDONLY, LOCK_SH, &loop);
        if (r < 0)
                return log_debug_errno(r, "Failed to open %s: %m", devname);
 
        r = dissect_loop_device(
                        loop,
                        &arg_verity_settings,
                        /* mount_options= */ NULL,
                        arg_image_policy ?: &image_policy_host,
                        arg_image_filter,
                        DISSECT_IMAGE_GPT_ONLY|
                        DISSECT_IMAGE_USR_NO_ROOT|
                        DISSECT_IMAGE_DISKSEQ_DEVNODE|
                        DISSECT_IMAGE_ALLOW_EMPTY,
                        /* NB! Unlike most other places where we dissect block devices we do not use
                         * DISSECT_IMAGE_ADD_PARTITION_DEVICES here: we want that the kernel finds the
                         * devices, and udev probes them before we mount them via .mount units much later
                         * on. And thus we also don't set DISSECT_IMAGE_PIN_PARTITION_DEVICES here, because
                         * we don't actually mount anything immediately. */
                        &m);
        if (r < 0) {
                bool ok = r == -ENOPKG;
                dissect_log_error(ok ? LOG_DEBUG : LOG_ERR, r, devname, NULL);
                return ok ? 0 : r;
        }
 
        if (m->partitions[PARTITION_SWAP].found)
                RET_GATHER(r, add_partition_swap(m->partitions + PARTITION_SWAP));
 
        RET_GATHER(r, process_loader_partitions(m->partitions + PARTITION_ESP, m->partitions + PARTITION_XBOOTLDR));
 
        if (m->partitions[PARTITION_HOME].found)
                RET_GATHER(r, add_partition_mount(PARTITION_HOME, m->partitions + PARTITION_HOME,
                                                  "home", "/home", "Home Partition"));
 
        if (m->partitions[PARTITION_SRV].found)
                RET_GATHER(r, add_partition_mount(PARTITION_SRV, m->partitions + PARTITION_SRV,
                                                  "srv", "/srv", "Server Data Partition"));
 
        if (m->partitions[PARTITION_VAR].found)
                RET_GATHER(r, add_partition_mount(PARTITION_VAR, m->partitions + PARTITION_VAR,
                                                  "var", "/var", "Variable Data Partition"));
 
        if (m->partitions[PARTITION_TMP].found)
                RET_GATHER(r, add_partition_mount(PARTITION_TMP, m->partitions + PARTITION_TMP,
                                                  "var-tmp", "/var/tmp", "Temporary Data Partition"));
 
        if (m->partitions[PARTITION_ROOT].found)
                RET_GATHER(r, add_partition_root_flags(m->partitions + PARTITION_ROOT));
 
        return r;
}
 
static int add_mounts(void) {
        dev_t devno;
        int r;
 
        if (in_initrd())
                return 0;
 
        r = blockdev_get_root(LOG_ERR, &devno);
        if (r < 0)
                return r;
        if (r == 0) {
                log_debug("Skipping automatic GPT dissection logic, root file system not backed by a (single) whole block device.");
                return 0;
        }
 
        return enumerate_partitions(devno);
}
 
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
        int r;
 
        assert(key);
 
        if (proc_cmdline_key_streq(key, "systemd.gpt_auto") ||
            proc_cmdline_key_streq(key, "rd.systemd.gpt_auto")) {
 
                r = value ? parse_boolean(value) : 1;
                if (r < 0)
                        log_warning_errno(r, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value);
                else
                        arg_enabled = r;
 
        } else if (streq(key, "root")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                /* Disable root disk logic if there's a root= value specified (unless it happens to be
                 * "gpt-auto", "gpt-auto-force", "dissect", "dissect-force") */
 
                arg_auto_root = parse_gpt_auto_root("root=", value);
                assert(arg_auto_root >= 0);
 
        } else if (streq(key, "roothash")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
 
                arg_auto_root = GPT_AUTO_ROOT_OFF;
                log_debug("Disabling root partition auto-detection, roothash= is set.");
 
                arg_verity_settings.designator = PARTITION_ROOT;
 
                iovec_done(&arg_verity_settings.root_hash);
                r = unhexmem(value, &arg_verity_settings.root_hash.iov_base, &arg_verity_settings.root_hash.iov_len);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse roothash= from kernel command line: %m");
 
        } else if (streq(key, "usrhash")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                arg_verity_settings.designator = PARTITION_USR;
 
                iovec_done(&arg_verity_settings.root_hash);
                r = unhexmem(value, &arg_verity_settings.root_hash.iov_base, &arg_verity_settings.root_hash.iov_len);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse usrhash= from kernel command line: %m");
 
        } else if (streq(key, "rootfstype")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                return free_and_strdup_warn(&arg_root_fstype, value);
 
        } else if (streq(key, "rootflags")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                if (!strextend_with_separator(&arg_root_options, ",", value))
                        return log_oom();
 
        } else if (streq(key, "mount.usr")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                arg_auto_usr = parse_gpt_auto_root("mount.usr=", value);
                assert(arg_auto_usr >= 0);
 
                if (IN_SET(arg_auto_usr, GPT_AUTO_ROOT_ON, GPT_AUTO_ROOT_FORCE, GPT_AUTO_ROOT_DISSECT_FORCE)) {
                        log_warning("'gpt-auto', 'gpt-auto-force' and 'dissect-force' are not supported for mount.usr=. Automatically resorting to mount.usr=dissect mode instead.");
                        arg_auto_usr = GPT_AUTO_ROOT_DISSECT;
                }
 
        } else if (streq(key, "mount.usrfstype")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                return free_and_strdup_warn(&arg_usr_fstype, empty_to_null(value));
 
        } else if (streq(key, "mount.usrflags")) {
 
                if (proc_cmdline_value_missing(key, value))
                        return 0;
 
                if (!strextend_with_separator(&arg_usr_options, ",", value))
                        return log_oom();
 
        } else if (streq(key, "rw") && !value)
                arg_root_rw = true;
        else if (streq(key, "ro") && !value)
                arg_root_rw = false;
        else if (proc_cmdline_key_streq(key, "systemd.image_policy"))
                return parse_image_policy_argument(value, &arg_image_policy);
        else if (proc_cmdline_key_streq(key, "systemd.image_filter")) {
                _cleanup_(image_filter_freep) ImageFilter *f = NULL;
 
                r = image_filter_parse(value, &f);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse image filter: %s", value);
 
                image_filter_free(arg_image_filter);
                arg_image_filter = TAKE_PTR(f);
 
        } else if (streq(key, "systemd.swap")) {
 
                r = value ? parse_boolean(value) : 1;
                if (r < 0)
                        log_warning_errno(r, "Failed to parse swap switch \"%s\", ignoring: %m", value);
                else
                        arg_swap_enabled = r;
 
                if (!arg_swap_enabled)
                        log_debug("Disabling swap partitions auto-detection, systemd.swap=no is defined.");
 
        }
 
        return 0;
}
 
static int run(const char *dest, const char *dest_early, const char *dest_late) {
        int r;
 
        LIBBLKID_NOTE(recommended);
        LIBMOUNT_NOTE(recommended);
        LIBSELINUX_NOTE(recommended);
 
        assert_se(arg_dest = dest);
        assert_se(arg_dest_late = dest_late);
 
        if (detect_container() > 0) {
                log_debug("In a container, exiting.");
                return 0;
        }
 
        r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
        if (r < 0)
                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
 
        if (!arg_enabled) {
                log_debug("Disabled, exiting.");
                return 0;
        }
 
        r = 0;
        RET_GATHER(r, add_root_mount());
        RET_GATHER(r, add_usr_mount());
        RET_GATHER(r, add_early_esp_mount());
        RET_GATHER(r, add_mounts());
 
        return r;
}
 
DEFINE_MAIN_GENERATOR_FUNCTION(run);