HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/vmspawn/vmspawn.c

198,981 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 1258b35c7ded8fefe9ea7f21e6147dfd8810fc39

Lines 2001–3000 of 4,443

                STRV_FOREACH(initrd, boot_entry->initrd) {
                        _cleanup_free_ char *initrd_path = path_join(boot_entry->root, *initrd);
                        if (!initrd_path)
                                return log_oom();
 
                        r = strv_consume(&initrds, TAKE_PTR(initrd_path));
                        if (r < 0)
                                return log_oom();
                }
        } else
                assert_not_reached();
 
        *ret_linux = TAKE_PTR(linux_kernel);
        *ret_initrds = TAKE_PTR(initrds);
 
        return 0;
}
 
static int merge_initrds(char **ret) {
        _cleanup_(rm_rf_physical_and_freep) char *merged_initrd = NULL;
        _cleanup_close_ int ofd = -EBADF;
        int r;
 
        assert(ret);
 
        r = tempfn_random_child(NULL, "vmspawn-initrd-", &merged_initrd);
        if (r < 0)
                return log_error_errno(r, "Failed to create temporary file: %m");
 
        ofd = open(merged_initrd, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0600);
        if (ofd < 0)
                return log_error_errno(errno, "Failed to create regular file %s: %m", merged_initrd);
 
        STRV_FOREACH(i, arg_initrds) {
                _cleanup_close_ int ifd = -EBADF;
                off_t off, to_seek;
 
                off = lseek(ofd, 0, SEEK_CUR);
                if (off < 0)
                        return log_error_errno(errno, "Failed to get file offset of %s: %m", merged_initrd);
 
                to_seek = (4 - (off % 4)) % 4;
 
                /* seek to assure 4 byte alignment for each initrd */
                if (to_seek != 0 && lseek(ofd, to_seek, SEEK_CUR) < 0)
                        return log_error_errno(errno, "Failed to seek %s: %m", merged_initrd);
 
                ifd = open(*i, O_RDONLY|O_CLOEXEC);
                if (ifd < 0)
                        return log_error_errno(errno, "Failed to open %s: %m", *i);
 
                r = copy_bytes(ifd, ofd, UINT64_MAX, /* copy_flags= */ 0);
                if (r < 0)
                        return log_error_errno(r, "Failed to copy bytes from %s to %s: %m", *i, merged_initrd);
        }
 
        *ret = TAKE_PTR(merged_initrd);
        return 0;
}
 
static int generate_ssh_keypair(const char *key_path, const char *key_type) {
        _cleanup_free_ char *ssh_keygen = NULL;
        _cleanup_strv_free_ char **cmdline = NULL;
        int r;
 
        assert(key_path);
 
        r = find_executable("ssh-keygen", &ssh_keygen);
        if (r < 0)
                return log_error_errno(r, "Failed to find ssh-keygen: %m");
 
        cmdline = strv_new(ssh_keygen, "-f", key_path, /* don't encrypt the key */ "-N", "");
        if (!cmdline)
                return log_oom();
 
        if (key_type) {
                r = strv_extend_many(&cmdline, "-t", key_type);
                if (r < 0)
                        return log_oom();
        }
 
        if (DEBUG_LOGGING) {
                _cleanup_free_ char *joined = quote_command_line(cmdline, SHELL_ESCAPE_EMPTY);
                if (!joined)
                        return log_oom();
 
                log_debug("Executing: %s", joined);
        }
 
        r = pidref_safe_fork_full(
                        ssh_keygen,
                        (int[]) { -EBADF, -EBADF, STDERR_FILENO },
                        /* except_fds= */ NULL, /* n_except_fds= */ 0,
                        FORK_WAIT|FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO|FORK_REOPEN_LOG,
                        /* ret= */ NULL);
        if (r < 0)
                return r;
        if (r == 0) {
                execv(ssh_keygen, cmdline);
                log_error_errno(errno, "Failed to execve %s: %m", ssh_keygen);
                _exit(EXIT_FAILURE);
        }
 
        return 0;
}
 
static int grow_image(const char *path, uint64_t size) {
        int r;
 
        assert(path);
 
        if (size == 0)
                return 0;
 
        _cleanup_close_ int fd = xopenat_full(AT_FDCWD, path, O_RDWR|O_CLOEXEC, XO_REGULAR, /* mode= */ 0);
        if (fd < 0)
                return log_error_errno(fd, "Failed to open image file '%s': %m", path);
 
        struct stat st;
        if (fstat(fd, &st) < 0)
                return log_error_errno(errno, "Failed to stat '%s': %m", path);
        if ((uint64_t) st.st_size >= size) {
                log_debug("Not growing image '%s' to %s, size already at %s.", path,
                          FORMAT_BYTES(size), FORMAT_BYTES(st.st_size));
                return 0;
        }
 
        if (ftruncate(fd, size) < 0)
                return log_error_errno(errno, "Failed to grow image file '%s' from %s to %s: %m", path,
                                       FORMAT_BYTES(st.st_size), FORMAT_BYTES(size));
 
        r = fsync_full(fd);
        if (r < 0)
                return log_error_errno(r, "Failed to sync image file '%s' after growing to %s: %m", path, FORMAT_BYTES(size));
 
        if (!arg_quiet)
                log_info("Image file '%s' successfully grown from %s to %s.", path, FORMAT_BYTES(st.st_size), FORMAT_BYTES(size));
 
        return 1;
}
 
static int on_request_stop(sd_bus_message *m, void *userdata, sd_bus_error *error) {
        assert(m);
 
        log_info("VM termination requested. Exiting.");
        sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), 0);
 
        return 0;
}
 
static int make_sidecar_path(const char *suffix, char **ret) {
        int r;
 
        assert(suffix);
        assert(ret);
 
        const char *p = ASSERT_PTR(arg_image ?: arg_directory);
 
        _cleanup_free_ char *parent = NULL, *filename = NULL;
        r = path_split_prefix_filename(p, &parent, &filename);
        if (r < 0)
                return log_error_errno(r, "Failed to extract parent directory and filename from '%s': %m", p);
 
        if (!strextend(&filename, suffix))
                return log_oom();
 
        _cleanup_free_ char *j = path_join(parent, filename);
        if (!j)
                return log_oom();
 
        *ret = TAKE_PTR(j);
        return 0;
}
 
/* Device serial numbers have length limits (e.g. 20 for NVMe, 30 for SCSI).
 * If the filename fits, use it directly; otherwise hash it with SHA-256 and
 * take the first max_len hex characters. max_len must be even and <= 64.
 * The filename should already be QEMU-escaped (commas doubled) so that the
 * result can be embedded directly in a -device argument. */
static int disk_serial(const char *filename, size_t max_len, char **ret) {
        assert(filename);
        assert(ret);
        assert(max_len % 2 == 0);
        assert(max_len <= SHA256_DIGEST_SIZE * 2);
 
        if (strlen(filename) <= max_len)
                return strdup_to(ret, filename);
 
        uint8_t hash[SHA256_DIGEST_SIZE];
        sha256_direct(filename, strlen(filename), hash);
 
        _cleanup_free_ char *serial = hexmem(hash, max_len / 2);
        if (!serial)
                return -ENOMEM;
 
        *ret = TAKE_PTR(serial);
        return 0;
}
 
/* Passes an fd to QEMU in the given fdset on its command line. The caller keeps owning the fd. */
static int cmdline_add_fdset(char ***cmdline, int **pass_fds, size_t *n_pass_fds, QemuFdset fdset, int fd, const char *opaque) {
        int r;
 
        assert(cmdline);
        assert(pass_fds);
        assert(n_pass_fds);
        assert(fd >= 0);
        assert(opaque);
 
        r = strv_extend(cmdline, "--add-fd");
        if (r < 0)
                return log_oom();
 
        r = strv_extendf(cmdline, "fd=%d,set=%i,opaque=%s", fd, fdset, opaque);
        if (r < 0)
                return log_oom();
 
        if (!GREEDY_REALLOC(*pass_fds, *n_pass_fds + 1))
                return log_oom();
 
        (*pass_fds)[(*n_pass_fds)++] = fd;
        return 0;
}
 
static int cmdline_add_ovmf(
                FILE *config_file,
                char ***cmdline,
                int **pass_fds,
                size_t *n_pass_fds,
                const OvmfConfig *ovmf_config,
                int ret_vars_fds[static 2]) {
        int r;
 
        assert(config_file);
        assert(ret_vars_fds);
 
        if (!ovmf_config) {
                ret_vars_fds[0] = ret_vars_fds[1] = -EBADF;
                return 0;
        }
 
        r = qemu_config_section(config_file, "drive", "ovmf-code",
                                "if", "pflash",
                                "format", ovmf_config_format(ovmf_config),
                                "readonly", "on",
                                "file", ovmf_config->path);
        if (r < 0)
                return r;
 
        if (!ovmf_config->vars && !arg_efi_nvram_template) {
                ret_vars_fds[0] = ret_vars_fds[1] = -EBADF;
                return 0;
        }
 
        if (arg_efi_nvram_state_mode == STATE_AUTO && !arg_ephemeral) {
                assert(!arg_efi_nvram_state_path);
 
                r = make_sidecar_path(".efinvramstate", &arg_efi_nvram_state_path);
                if (r < 0)
                        return r;
 
                log_debug("Storing EFI NVRAM state persistently under '%s'.", arg_efi_nvram_state_path);
        }
 
        const char *vars_source = arg_efi_nvram_template ?: ovmf_config->vars;
        _cleanup_close_pair_ int vars_fds[2] = EBADF_PAIR;
        _cleanup_(unlink_and_freep) char *destroy_path = NULL;
        bool newly_created;
        if (arg_efi_nvram_state_path) {
                _cleanup_free_ char *d = strdup(arg_efi_nvram_state_path);
                if (!d)
                        return log_oom();
 
                vars_fds[0] = openat_report_new(AT_FDCWD, arg_efi_nvram_state_path, O_RDWR|O_CREAT|O_CLOEXEC, 0600, &newly_created);
                if (vars_fds[0] < 0)
                        return log_error_errno(vars_fds[0], "Failed to open file for OVMF vars at %s: %m", arg_efi_nvram_state_path);
 
                if (newly_created)
                        destroy_path = TAKE_PTR(d);
 
                r = fd_verify_regular(vars_fds[0]);
                if (r < 0)
                        return log_error_errno(r, "Not a regular file for OVMF variables at %s: %m", arg_efi_nvram_state_path);
        } else {
                vars_fds[0] = open_tmpfile_unlinkable(/* directory= */ NULL, O_RDWR|O_CLOEXEC);
                if (vars_fds[0] < 0)
                        return log_error_errno(vars_fds[0], "Failed to create temporary file for OVMF vars: %m");
 
                newly_created = true;
        }
 
        if (newly_created) {
                _cleanup_close_ int source_fd = open(vars_source, O_RDONLY|O_CLOEXEC);
                if (source_fd < 0)
                        return log_error_errno(errno, "Failed to open OVMF vars file %s: %m", vars_source);
 
                r = copy_bytes(source_fd, vars_fds[0], UINT64_MAX, /* copy_flags= */ 0);
                if (r < 0)
                        return log_error_errno(r, "Failed to copy OVMF vars file %s: %m", vars_source);
 
                /* This isn't always available so don't raise an error if it fails */
                (void) copy_times(source_fd, vars_fds[0], 0);
        }
 
        destroy_path = mfree(destroy_path); /* disarm auto-destroy */
 
        /* QEMU's block layer opens the drive read-only first, so it needs a matching fd in the fdset. */
        vars_fds[1] = fd_reopen(vars_fds[0], O_RDONLY|O_CLOEXEC);
        if (vars_fds[1] < 0)
                return log_error_errno(vars_fds[1], "Failed to reopen OVMF vars file read-only: %m");
 
        FOREACH_ELEMENT(fd, vars_fds) {
                r = cmdline_add_fdset(cmdline, pass_fds, n_pass_fds, QEMU_FDSET_EFI_VARS, *fd, "efi-vars");
                if (r < 0)
                        return r;
        }
 
        /* Mark the UEFI variable store pflash as requiring SMM access. This
         * prevents the guest OS from writing to pflash directly, ensuring all
         * variable updates go through the firmware's validation checks. Without
         * this, secure boot keys could be overwritten by the OS. Firmware built
         * without SMM support could not write its variables at all then. */
        if (ARCHITECTURE_SUPPORTS_SMM && ovmf_config_has_feature(ovmf_config, "requires-smm")) {
                r = qemu_config_section(config_file, "global", /* id= */ NULL,
                                        "driver", "cfi.pflash01",
                                        "property", "secure",
                                        "value", "on");
                if (r < 0)
                        return r;
        }
 
        r = qemu_config_section(config_file, "drive", "ovmf-vars",
                                "file", FORMAT_QEMU_FDSET_PATH(QEMU_FDSET_EFI_VARS),
                                "if", "pflash",
                                "format", ovmf_config_format(ovmf_config));
        if (r < 0)
                return r;
 
        ret_vars_fds[0] = TAKE_FD(vars_fds[0]);
        ret_vars_fds[1] = TAKE_FD(vars_fds[1]);
        return 0;
}
 
/* Create a QMP control socketpair, add QEMU's end to pass_fds, and write the chardev + monitor
 * config sections. Returns with bridge_fds populated: [0] is vmspawn's end, [1] is QEMU's end
 * (also in pass_fds). FORK_CLOEXEC_OFF clears CLOEXEC on pass_fds in the child. */
static int qemu_config_add_qmp_monitor(FILE *config_file, int bridge_fds[2], int **pass_fds, size_t *n_pass_fds) {
        int r;
 
        assert(config_file);
        assert(bridge_fds);
        assert(pass_fds);
        assert(n_pass_fds);
 
        if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, bridge_fds) < 0)
                return log_error_errno(errno, "Failed to create QMP socketpair: %m");
 
        if (!GREEDY_REALLOC(*pass_fds, *n_pass_fds + 1))
                return log_oom();
        (*pass_fds)[(*n_pass_fds)++] = bridge_fds[1];
 
        r = qemu_config_section(config_file, "chardev", "qmp",
                                "backend", "socket");
        if (r < 0)
                return r;
 
        r = qemu_config_keyf(config_file, "fd", "%d", bridge_fds[1]);
        if (r < 0)
                return r;
 
        return qemu_config_section(config_file, "mon", "qmp",
                                   "chardev", "qmp",
                                   "mode", "control");
}
 
static int resolve_disk_driver(DiskType dt, const char *filename, DriveInfo *info) {
        size_t serial_max;
        int r;
 
        assert(filename);
        assert(info);
 
        switch (dt) {
        case DISK_TYPE_VIRTIO_BLK:
                serial_max = DISK_SERIAL_MAX_LEN_VIRTIO_BLK;
                break;
        case DISK_TYPE_VIRTIO_SCSI:
                serial_max = DISK_SERIAL_MAX_LEN_SCSI;
                break;
        case DISK_TYPE_NVME:
                serial_max = DISK_SERIAL_MAX_LEN_NVME;
                break;
        case DISK_TYPE_VIRTIO_SCSI_CDROM:
                serial_max = DISK_SERIAL_MAX_LEN_SCSI;
                info->flags |= QMP_DRIVE_READ_ONLY;
                break;
        default:
                assert_not_reached();
        }
 
        info->disk_driver = strdup(ASSERT_PTR(qemu_device_driver_to_string(dt)));
        if (!info->disk_driver)
                return log_oom();
 
        r = disk_serial(filename, serial_max, &info->serial);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int prepare_primary_drive(const char *runtime_dir, DriveInfos *drives) {
        int r;
 
        assert(runtime_dir);
        assert(drives);
 
        if (!arg_image)
                return 0;
 
        _cleanup_free_ char *image_fn = NULL;
        r = path_extract_filename(arg_image, &image_fn);
        if (r < 0)
                return log_error_errno(r, "Failed to extract filename from path '%s': %m", arg_image);
 
        _cleanup_(drive_info_unrefp) DriveInfo *d = drive_info_new();
        if (!d)
                return log_oom();
 
        r = resolve_disk_driver(arg_image_disk_type, image_fn, d);
        if (r < 0)
                return log_error_errno(r, "Failed to resolve disk driver for '%s': %m", image_fn);
 
        int open_flags = ((arg_ephemeral || FLAGS_SET(d->flags, QMP_DRIVE_READ_ONLY)) ? O_RDONLY : O_RDWR) | O_CLOEXEC | O_NOCTTY;
 
        _cleanup_close_ int image_fd = open(arg_image, open_flags);
        if (image_fd < 0)
                return log_error_errno(errno, "Failed to open '%s': %m", arg_image);
 
        struct stat st;
        if (fstat(image_fd, &st) < 0)
                return log_error_errno(errno, "Failed to stat '%s': %m", arg_image);
 
        r = stat_verify_regular_or_block(&st);
        if (r < 0)
                return log_error_errno(r, "Expected regular file or block device for image: %s", arg_image);
 
        d->path = strdup(arg_image);
        d->format = strdup(ASSERT_PTR(image_format_to_string(arg_image_format)));
        if (!d->path || !d->format)
                return log_oom();
        d->fd = TAKE_FD(image_fd);
        if (S_ISBLK(st.st_mode))
                d->flags |= QMP_DRIVE_BLOCK_DEVICE;
        if (arg_discard_disk && !FLAGS_SET(d->flags, QMP_DRIVE_READ_ONLY))
                d->flags |= QMP_DRIVE_DISCARD;
        d->flags |= QMP_DRIVE_BOOT;
 
        /* For ephemeral mode, create an anonymous overlay file. QEMU will format it
         * as qcow2 via blockdev-create, so no filesystem path is needed.
         * Skip for read-only drives (e.g. CDROM) where overlays are not meaningful. */
        if (arg_ephemeral && !FLAGS_SET(d->flags, QMP_DRIVE_READ_ONLY)) {
                _cleanup_close_ int overlay_fd = open(runtime_dir, O_TMPFILE | O_RDWR | O_CLOEXEC, 0600);
                if (overlay_fd < 0) {
                        if (!ERRNO_IS_NOT_SUPPORTED(errno))
                                return log_error_errno(errno, "Failed to create ephemeral overlay in '%s': %m", runtime_dir);
 
                        /* Fallback to memfd if O_TMPFILE is not supported */
                        overlay_fd = memfd_new("vmspawn-overlay");
                        if (overlay_fd < 0)
                                return log_error_errno(overlay_fd, "Failed to create ephemeral overlay via memfd: %m");
                }
                d->overlay_fd = TAKE_FD(overlay_fd);
                d->flags |= QMP_DRIVE_NO_FLUSH;
                d->grow_to = arg_grow_image;
        }
 
        drives->drives[drives->n_drives++] = TAKE_PTR(d);
        return 0;
}
 
static int prepare_extra_drives(DriveInfos *drives) {
        int r;
 
        assert(drives);
 
        FOREACH_ARRAY(drive, arg_extra_drives.drives, arg_extra_drives.n_drives) {
                _cleanup_free_ char *drive_fn = NULL;
                r = path_extract_filename(drive->path, &drive_fn);
                if (r < 0)
                        return log_error_errno(r, "Failed to extract filename from path '%s': %m", drive->path);
 
                DiskType dt = drive->disk_type >= 0 ? drive->disk_type : arg_image_disk_type;
 
                _cleanup_(drive_info_unrefp) DriveInfo *d = drive_info_new();
                if (!d)
                        return log_oom();
 
                r = resolve_disk_driver(dt, drive_fn, d);
                if (r < 0)
                        return log_error_errno(r, "Failed to resolve disk driver for '%s': %m", drive_fn);
 
                _cleanup_close_ int drive_fd = open(drive->path, (FLAGS_SET(d->flags, QMP_DRIVE_READ_ONLY) ? O_RDONLY : O_RDWR) | O_CLOEXEC | O_NOCTTY);
                if (drive_fd < 0)
                        return log_error_errno(errno, "Failed to open '%s': %m", drive->path);
 
                struct stat drive_st;
                if (fstat(drive_fd, &drive_st) < 0)
                        return log_error_errno(errno, "Failed to stat '%s': %m", drive->path);
                r = stat_verify_regular_or_block(&drive_st);
                if (r < 0)
                        return log_error_errno(r, "Expected regular file or block device, not '%s'.", drive->path);
                if (S_ISBLK(drive_st.st_mode) && drive->format == IMAGE_FORMAT_QCOW2)
                        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                               "Block device '%s' cannot be used with 'qcow2' format, only 'raw' is supported.",
                                               drive->path);
 
                d->path = strdup(drive->path);
                d->format = strdup(ASSERT_PTR(image_format_to_string(drive->format)));
                if (!d->path || !d->format)
                        return log_oom();
                d->fd = TAKE_FD(drive_fd);
                if (S_ISBLK(drive_st.st_mode))
                        d->flags |= QMP_DRIVE_BLOCK_DEVICE;
                d->flags |= QMP_DRIVE_NO_FLUSH;
 
                drives->drives[drives->n_drives++] = TAKE_PTR(d);
        }
 
        return 0;
}
 
/* Assign PCIe root port names to devices. The ports were pre-allocated in the config
 * file. Each PCI device that will be hotplugged via QMP device_add gets a port. */
static int assign_pcie_ports(MachineConfig *c) {
        assert(c);
 
        if (!ARCHITECTURE_NEEDS_PCIE_ROOT_PORTS)
                return 0;
 
        DriveInfos *drives = &c->drives;
        NetworkInfo *network = &c->network;
        VirtiofsInfos *virtiofs = &c->virtiofs;
        VsockInfo *vsock = &c->vsock;
 
        size_t port = 0;
 
        /* Non-SCSI drives get individual ports. SCSI controllers (if any) allocate
         * from the hotplug-spares pool on demand at device-add time. */
        FOREACH_ARRAY(d, drives->drives, drives->n_drives) {
                DriveInfo *drive = *d;
                if (STR_IN_SET(drive->disk_driver, "scsi-hd", "scsi-cd"))
                        continue;
                if (asprintf(&drive->pcie_port, "vmspawn-pcieport-%zu", port++) < 0)
                        return log_oom();
        }
 
        if (network->type)
                if (asprintf(&network->pcie_port, "vmspawn-pcieport-%zu", port++) < 0)
                        return log_oom();
 
        FOREACH_ARRAY(v, virtiofs->entries, virtiofs->n_entries)
                if (asprintf(&v->pcie_port, "vmspawn-pcieport-%zu", port++) < 0)
                        return log_oom();
 
        if (vsock->fd >= 0)
                if (asprintf(&vsock->pcie_port, "vmspawn-pcieport-%zu", port++) < 0)
                        return log_oom();
 
        return 0;
}
 
static int prepare_device_info(const char *runtime_dir, MachineConfig *c) {
        int r;
 
        assert(runtime_dir);
        assert(c);
 
        DriveInfos *drives = &c->drives;
 
        /* Build drive info for QMP-based setup. vmspawn opens all image files and
         * passes fds to QEMU via add-fd — QEMU never needs filesystem access. */
        drives->drives = new0(DriveInfo*, 1 + arg_extra_drives.n_drives + arg_bind_volumes.n_items);
        if (!drives->drives)
                return log_oom();
 
        r = prepare_primary_drive(runtime_dir, drives);
        if (r < 0)
                return r;
 
        r = prepare_extra_drives(drives);
        if (r < 0)
                return r;
 
        r = vmspawn_bind_volume_prepare_boot(arg_runtime_scope, &arg_bind_volumes, drives);
        if (r < 0)
                return r;
 
        return assign_pcie_ports(c);
}
 
/* Maps a confidential computing mode to the firmware descriptor feature that firmware must declare
 * to support it. Note that the feature names are defined by the QEMU firmware interop spec and
 * deviate slightly from our own names for the same modes (cf. confidential_computing_to_string()):
 * "sev-snp" vs. "amd-sev-snp", and "tdx" vs. "intel-tdx". */
static const char* const coco_firmware_feature_table[_COCO_MAX] = {
        [COCO_SEV_SNP] = "amd-sev-snp",
        [COCO_TDX]     = "intel-tdx",
};
 
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(coco_firmware_feature, ConfidentialComputing);
 
static int discover_ovmf_config(OvmfConfig **ret, sd_json_variant **ret_firmware_json) {
        int r;
 
        assert(ret);
 
        const char *coco_feature = coco_firmware_feature_to_string(arg_confidential_computing);
        if (coco_feature) {
                r = set_put_strdup(&arg_firmware_features_include, coco_feature);
                if (r < 0)
                        return log_oom();
 
                /* CoCo firmware is stateless, so Secure Boot keys cannot be enrolled at runtime
                 * but must have baked-in, enrolled keys, which we avoid in other cases. */
                if (set_contains(arg_firmware_features_include, "secure-boot")) {
                        r = set_put_strdup(&arg_firmware_features_include, "enrolled-keys");
                        if (r < 0)
                                return log_oom();
                }
        }
 
        FindOvmfConfigFlags flags =
                arg_confidential_computing != COCO_NO ? FIND_OVMF_STATELESS|FIND_OVMF_REQUIRE_RAW :
                                                        0;
 
        r = find_ovmf_config(arg_firmware_features_include, arg_firmware_features_exclude, flags, ret, ret_firmware_json);
        if (r == -ENOENT && coco_feature)
                return log_error_errno(r, "No suitable firmware descriptor found for --coco=%s "
                                       "(requires stateless firmware in raw format with the '%s' firmware feature). "
                                       "Install a suitable firmware, select a firmware descriptor with --firmware=, or "
                                       "opt in with --secure-boot=yes if the installed firmware enforces Secure Boot.",
                                       confidential_computing_to_string(arg_confidential_computing), coco_feature);
        if (r < 0)
                return log_error_errno(r, "Failed to find OVMF config: %m");
 
        return 0;
}
 
static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
        _cleanup_(ovmf_config_freep) OvmfConfig *ovmf_config = NULL;
        _cleanup_free_ char *qemu_binary = NULL, *mem = NULL;
        /* Always assigned paths below the runtime directory, whose removal takes them along. */
        _cleanup_free_ char *ssh_private_key_path = NULL, *ssh_public_key_path = NULL;
        _cleanup_(rm_rf_subvolume_and_freep) char *snapshot_directory = NULL;
        _cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
        _cleanup_close_ int notify_sock_fd = -EBADF;
        _cleanup_strv_free_ char **cmdline = NULL;
        _cleanup_free_ int *pass_fds = NULL;
        _cleanup_(machine_config_done) MachineConfig config = {
                .network = { .fd = -EBADF },
                .vsock   = { .fd = -EBADF },
        };
        /* Declared before the CLEANUP_ARRAY() below, so that cleanup order (reverse of declaration) lets
         * fork_notify_terminate_many() reap the helpers before this directory goes away: their sockets,
         * state, and the QEMU config file all live below it, and pulling it out from under them gives
         * spurious errors and can leave the directory behind. */
        _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
        sd_event_source **children = NULL;
        size_t n_children = 0, n_pass_fds = 0;
        int r;
 
        CLEANUP_ARRAY(children, n_children, fork_notify_terminate_many);
 
        polkit_agent_open();
 
        /* Registration always happens on the system bus */
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
        if (arg_register != 0 || arg_runtime_scope == RUNTIME_SCOPE_SYSTEM) {
                r = sd_bus_default_system(&system_bus);
                if (r < 0)
                        return log_error_errno(r, "Failed to open system bus: %m");
 
                r = sd_bus_set_close_on_exit(system_bus, false);
                if (r < 0)
                        return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
 
                (void) sd_bus_set_allow_interactive_authorization(system_bus, arg_ask_password);
        }
 
        /* Scope allocation and machine registration happen on the user bus if we are unpriv, otherwise system bus. */
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *user_bus = NULL;
        _cleanup_(sd_bus_unrefp) sd_bus *runtime_bus = NULL;
        if (arg_register != 0 || !arg_keep_unit) {
                if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM)
                        runtime_bus = sd_bus_ref(system_bus);
                else {
                        r = sd_bus_default_user(&user_bus);
                        if (r < 0)
                                return log_error_errno(r, "Failed to open user bus: %m");
 
                        r = sd_bus_set_close_on_exit(user_bus, false);
                        if (r < 0)
                                return log_error_errno(r, "Failed to disable close-on-exit behaviour: %m");
 
                        runtime_bus = sd_bus_ref(user_bus);
                }
        }
 
        bool use_kvm = arg_kvm > 0;
        if (arg_kvm < 0) {
                r = qemu_check_kvm_support();
                if (r < 0)
                        return log_error_errno(r, "Failed to check for KVM support: %m");
                use_kvm = r;
        }
 
        if (arg_confidential_computing != COCO_NO && !use_kvm)
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                       "--coco= requires KVM, but KVM is not available.");
 
        if (arg_firmware_type == FIRMWARE_UEFI) {
                if (arg_firmware) {
                        r = load_ovmf_config(arg_firmware, &ovmf_config);
                        if (r < 0)
                                return log_error_errno(r, "Failed to load firmware descriptor '%s': %m", arg_firmware);
                } else {
                        r = discover_ovmf_config(&ovmf_config, /* ret_firmware_json= */ NULL);
                        if (r < 0)
                                return r;
                }
 
                /* Flash mode "combined" places the variable store in the (writable) executable,
                 * which would have to be cloned for each guest. */
                if (streq_ptr(ovmf_config->mode, "combined"))
                        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                               "Firmware descriptor '%s' declares flash mode 'combined', which is not supported.",
                                               arg_firmware ?: ovmf_config->path);
 
                if (arg_confidential_computing != COCO_NO) {
                        const char *coco_feature = coco_firmware_feature_to_string(arg_confidential_computing);
 
                        if (!ovmf_config_has_feature(ovmf_config, coco_feature))
                                return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE),
                                                       "Firmware descriptor '%s' does not declare the '%s' feature, but "
                                                       "--coco=%s requires firmware built specifically for it.",
                                                       arg_firmware ?: ovmf_config->path, coco_feature,
                                                       confidential_computing_to_string(arg_confidential_computing));
                        if (!ovmf_config_is_stateless(ovmf_config))
                                return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE),
                                                       "Firmware descriptor '%s' does not describe stateless firmware, "
                                                       "but --coco=%s requires stateless firmware.",
                                                       arg_firmware ?: ovmf_config->path,
                                                       confidential_computing_to_string(arg_confidential_computing));
                        if (!streq(ovmf_config_format(ovmf_config), "raw"))
                                return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE),
                                                       "Firmware image '%s' is in %s format, "
                                                       "but --coco=%s requires a raw image.",
                                                       ovmf_config->path, ovmf_config_format(ovmf_config),
                                                       confidential_computing_to_string(arg_confidential_computing));
                }
 
                bool sb = ovmf_config_has_feature(ovmf_config, "secure-boot");
                if (set_contains(arg_firmware_features_include, "secure-boot") && !sb)
                        return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE),
                                               "Secure Boot requested, but selected OVMF firmware doesn't support it.");
 
                log_debug("Using OVMF firmware %s Secure Boot support.", sb ? "with" : "without");
        }
 
        _cleanup_(machine_bind_user_context_freep) MachineBindUserContext *bind_user_context = NULL;
        r = machine_bind_user_prepare(
                        /* directory= */ NULL,
                        arg_bind_user,
                        arg_bind_user_shell,
                        arg_bind_user_shell_copy,
                        "/run/vmhost/home",
                        arg_bind_user_groups,
                        &bind_user_context);
        if (r < 0)
                return r;
 
        r = bind_user_setup(bind_user_context, &arg_credentials, &arg_runtime_mounts);
        if (r < 0)
                return r;
 
        r = find_qemu_binary(&qemu_binary);
        if (r == -EOPNOTSUPP)
                return log_error_errno(r, "Native architecture is not supported by qemu.");
        if (r < 0)
                return log_error_errno(r, "Failed to find QEMU binary: %m");
 
        if (asprintf(&mem, "%" PRIu64 "M", DIV_ROUND_UP(arg_ram, U64_MB)) < 0)
                return log_oom();
 
        /* Create our runtime directory. We need this for the QMP varlink control socket, the QEMU
         * config file, TPM state, virtiofsd sockets, runtime mounts, and SSH key material. */
        r = runtime_directory_make(arg_runtime_scope, "systemd/vmspawn", arg_machine, &runtime_dir);
        if (r < 0)
                return log_error_errno(r, "Failed to create runtime directory: %m");
 
        /* If a previous vmspawn instance was killed without cleanup (e.g. SIGKILL), the directory may
         * already exist with stale contents. This is harmless: varlink's sockaddr_un_unlink() removes stale
         * sockets before bind(), and other files (QEMU config, SSH keys) are created fresh. This matches
         * nspawn's approach of not proactively cleaning stale runtime directories. */
 
        log_debug("Using runtime directory: %s", runtime_dir);
 
        /* Build a QEMU config file for -readconfig. Items that can be expressed as QemuOpts sections go
         * here; things that require cmdline-only switches (e.g. -kernel, -smbios, -nographic, --add-fd)
         * are added to the cmdline strv below. */
        _cleanup_fclose_ FILE *config_file = NULL;
        _cleanup_(unlink_and_freep) char *config_path = NULL;
        r = fopen_temporary_child(runtime_dir, &config_file, &config_path);
        if (r < 0)
                return log_error_errno(r, "Failed to create QEMU config file: %m");
 
        r = qemu_config_section(config_file, "machine", /* id= */ NULL,
                                "type", QEMU_MACHINE_TYPE);
        if (r < 0)
                return r;
 
        if (arg_confidential_computing != COCO_NO) {
                r = qemu_config_key(config_file, "kernel-irqchip", "split");
                if (r < 0)
                        return r;
        }
 
        if (ovmf_config && ARCHITECTURE_SUPPORTS_SMM && arg_confidential_computing == COCO_NO) {
                r = qemu_config_key(config_file, "smm", on_off(ovmf_config_has_feature(ovmf_config, "requires-smm")));
                if (r < 0)
                        return r;
        }
 
        if (ARCHITECTURE_SUPPORTS_CXL && arg_confidential_computing == COCO_NO) {
                r = qemu_config_key(config_file, "cxl", "on");
                if (r < 0)
                        return r;
        }
 
        if (arg_directory || arg_runtime_mounts.n_mounts != 0) {
                r = qemu_config_key(config_file, "memory-backend", "mem");
                if (r < 0)
                        return r;
        }
 
        if (ARCHITECTURE_SUPPORTS_HPET) {
                r = qemu_config_key(config_file, "hpet", "off");
                if (r < 0)
                        return r;
        }
 
        if (arg_confidential_computing == COCO_SEV_SNP) {
                r = qemu_config_key(config_file, "confidential-guest-support", "snp0");
                if (r < 0)
                        return r;
        } else if (arg_confidential_computing == COCO_TDX) {
                r = qemu_config_key(config_file, "confidential-guest-support", "tdx0");
                if (r < 0)
                        return r;
        }
 
        r = qemu_config_section(config_file, "smp-opts", /* id= */ NULL,
                                "cpus", arg_cpus ?: "1");
        if (r < 0)
                return r;
 
        r = qemu_config_section(config_file, "memory", /* id= */ NULL,
                                "size", mem);
        if (r < 0)
                return r;
 
        if (arg_ram_max > 0) {
                r = qemu_config_keyf(config_file, "maxmem", "%" PRIu64 "M", DIV_ROUND_UP(arg_ram_max, U64_MB));
                if (r < 0)
                        return r;
 
                r = qemu_config_keyf(config_file, "slots", "%u", arg_ram_slots > 0 ? arg_ram_slots : 1u);
                if (r < 0)
                        return r;
        }
 
        r = qemu_config_section(config_file, "object", "rng0",
                                "qom-type", "rng-random",
                                "filename", "/dev/urandom");
        if (r < 0)
                return r;
 
        r = qemu_config_section(config_file, "device", "rng-device0",
                                "driver", "virtio-rng-pci",
                                "rng", "rng0");
        if (r < 0)
                return r;
 
        if (arg_confidential_computing == COCO_NO) {
                r = qemu_config_section(config_file, "device", "balloon0",
                                        "driver", "virtio-balloon",
                                        "free-page-reporting", "on");
                if (r < 0)
                        return r;
        }
 
        if (ARCHITECTURE_SUPPORTS_VMGENID) {
                sd_id128_t vmgenid;
                r = sd_id128_get_invocation_app_specific(SD_ID128_MAKE(bd,84,6d,e3,e4,7d,4b,6c,a6,85,4a,87,0f,3c,a3,a0), &vmgenid);
                if (r < 0) {
                        log_debug_errno(r, "Failed to get invocation ID, making up randomized vmgenid: %m");
 
                        r = sd_id128_randomize(&vmgenid);
                        if (r < 0)
                                return log_error_errno(r, "Failed to make up randomized vmgenid: %m");
                }
 
                r = qemu_config_section(config_file, "device", "vmgenid0",
                                        "driver", "vmgenid");
                if (r < 0)
                        return r;
 
                r = qemu_config_keyf(config_file, "guid", SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(vmgenid));
                if (r < 0)
                        return r;
        }
 
        /* Start building the cmdline for items that must remain as command line arguments.
         * -S starts QEMU with vCPUs paused — we set up devices via QMP then resume with "cont". */
        cmdline = strv_new(qemu_binary,
                           "-S",
                           "-no-user-config");
        if (!cmdline)
                return log_oom();
 
        if (!sd_id128_is_null(arg_uuid))
                if (strv_extend_many(&cmdline, "-uuid", SD_ID128_TO_UUID_STRING(arg_uuid)) < 0)
                        return log_oom();
 
        _cleanup_close_ int delegate_userns_fd = -EBADF, tap_fd = -EBADF;
        _cleanup_free_ char *tap_name = NULL;
        struct ether_addr mac_vm = {};
 
        if (arg_network_stack == NETWORK_STACK_TAP) {
                if (have_effective_cap(CAP_NET_ADMIN) <= 0) {
                        /* Without CAP_NET_ADMIN we use nsresourced to create a TAP device.
                         * The TAP fd is passed to QEMU via QMP getfd + SCM_RIGHTS after
                         * the handshake, then referenced by name in netdev_add. */
                        delegate_userns_fd = userns_acquire_self_root();
                        if (delegate_userns_fd < 0)
                                return log_error_errno(delegate_userns_fd, "Failed to acquire userns: %m");
 
                        _cleanup_free_ char *userns_name = NULL;
                        if (asprintf(&userns_name, "vmspawn-" PID_FMT "-%s", getpid_cached(), arg_machine) < 0)
                                return log_oom();
 
                        _cleanup_(sd_varlink_unrefp) sd_varlink *nsresource_link = NULL;
                        r = nsresource_connect(&nsresource_link);
                        if (r < 0)
                                return log_error_errno(r, "Failed to connect to nsresourced: %m");
 
                        r = nsresource_register_userns(nsresource_link, userns_name, delegate_userns_fd);
                        if (r < 0)
                                return log_error_errno(r, "Failed to register user namespace with systemd-nsresourced: %m");
 
                        tap_fd = nsresource_add_netif_tap(nsresource_link, delegate_userns_fd, /* ret_host_ifname= */ NULL);
                        if (tap_fd < 0)
                                return log_error_errno(tap_fd, "Failed to allocate network tap device: %m");
 
                        config.network = (NetworkInfo) {
                                .type = "tap",
                                .fd   = TAKE_FD(tap_fd),
                        };
                } else {
                        /* With CAP_NET_ADMIN we create the TAP interface by name.
                         * Configure via QMP after QEMU starts. */
                        tap_name = strjoin("vt-", arg_machine);
                        if (!tap_name)
                                return log_oom();
 
                        (void) net_shorten_ifname(tap_name, /* check_naming_scheme= */ false);
 
                        if (ether_addr_is_null(&arg_network_provided_mac)) {
                                r = net_generate_mac(arg_machine, &mac_vm, VM_TAP_HASH_KEY, 0);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to generate predictable MAC address for VM side: %m");
                        } else
                                mac_vm = arg_network_provided_mac;
 
                        config.network = (NetworkInfo) {
                                .type    = "tap",
                                .ifname  = TAKE_PTR(tap_name),
                                .mac     = mac_vm,
                                .mac_set = true,
                                .fd      = -EBADF,
                        };
                }
        } else if (arg_network_stack == NETWORK_STACK_USER) {
                config.network = (NetworkInfo) {
                        .type = "user",
                        .fd   = -EBADF,
                };
        } else {
                r = strv_extend_many(&cmdline, "-nic", "none");