HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/vmspawn/vmspawn.c
Git blob: 1258b35c7ded8fefe9ea7f21e6147dfd8810fc39
Lines 3001–4000 of 4,443
if (r < 0)
return log_oom();
}
/* A shared memory backend might increase ram usage so only add one if actually necessary for virtiofsd. */
if (arg_directory || arg_runtime_mounts.n_mounts != 0) {
r = qemu_config_section(config_file, "object", "mem",
"qom-type", "memory-backend-memfd",
"size", mem,
"share", "on");
if (r < 0)
return r;
}
bool use_vsock = arg_vsock > 0;
if (arg_vsock < 0) {
r = qemu_check_vsock_support();
if (r < 0)
return log_error_errno(r, "Failed to check for VSOCK support: %m");
use_vsock = r;
}
if (!use_kvm && kvm_device_fd >= 0) {
log_warning("KVM is disabled but fd for /dev/kvm was passed, closing fd and ignoring");
kvm_device_fd = safe_close(kvm_device_fd);
}
if (use_kvm && kvm_device_fd >= 0) {
r = cmdline_add_fdset(&cmdline, &pass_fds, &n_pass_fds, QEMU_FDSET_KVM, kvm_device_fd, "/dev/kvm");
if (r < 0)
return r;
r = qemu_config_section(config_file, "accel", /* id= */ NULL,
"accel", "kvm",
"device", FORMAT_QEMU_FDSET_PATH(QEMU_FDSET_KVM));
if (r < 0)
return r;
} else {
r = qemu_config_section(config_file, "accel", /* id= */ NULL,
"accel", use_kvm ? "kvm" : "tcg");
if (r < 0)
return r;
}
if (arg_confidential_computing == COCO_SEV_SNP) {
/* SNP marks encrypted guest pages via the "C-bit" in the page table entry. On all
* SNP-capable processors (Milan and later) the C-bit lives at bit 51, which reduces
* the usable guest physical address space by one bit.
* Embed the hashes of kernel, initrd and cmdline into the firmware
* so they are covered by the launch measurement and the guest's
* boot chain starts from a measured state. */
r = qemu_config_section(config_file, "object", "snp0",
"qom-type", "sev-snp-guest",
"cbitpos", "51",
"reduced-phys-bits", "1",
"kernel-hashes", "on");
if (r < 0)
return r;
} else if (arg_confidential_computing == COCO_TDX) {
r = qemu_config_section(config_file, "object", "tdx0",
"qom-type", "tdx-guest");
if (r < 0)
return r;
/* The guest needs a connection to the TDX Quote Generation Service (QGS) running on
* the host to obtain TD Quotes (e.g. via the systemd-report-sign-tsm). There are two
* well-known interfaces for QGS: a well-known unix socket, or vsock port 4050. QEMU
* only connects on a GetQuote request, and pointing at an absent QGS is harmless,
* the request just fails and the guest gets no quote. */
r = is_socket(TDX_QGS_UNIX_SOCKET_PATH);
if (r < 0)
log_debug_errno(r, "Failed to check for QGS socket '%s', falling back to vsock: %m", TDX_QGS_UNIX_SOCKET_PATH);
if (r > 0) {
r = qemu_config_key(config_file, "quote-generation-socket.type", "unix");
if (r < 0)
return r;
r = qemu_config_key(config_file, "quote-generation-socket.path", TDX_QGS_UNIX_SOCKET_PATH);
if (r < 0)
return r;
log_debug("Using TDX Quote Generation Service at unix socket %s.", TDX_QGS_UNIX_SOCKET_PATH);
} else {
r = qemu_config_key(config_file, "quote-generation-socket.type", "vsock");
if (r < 0)
return r;
r = qemu_config_key(config_file, "quote-generation-socket.cid", TDX_QGS_VSOCK_CID);
if (r < 0)
return r;
r = qemu_config_key(config_file, "quote-generation-socket.port", TDX_QGS_VSOCK_PORT);
if (r < 0)
return r;
log_debug("No QGS unix socket found, pointing TDX quote generation at vsock %s:%s.",
TDX_QGS_VSOCK_CID, TDX_QGS_VSOCK_PORT);
}
}
unsigned child_cid = arg_vsock_cid;
if (use_vsock) {
config.vsock.fd = TAKE_FD(vhost_device_fd);
if (config.vsock.fd < 0) {
config.vsock.fd = open("/dev/vhost-vsock", O_RDWR|O_CLOEXEC);
if (config.vsock.fd < 0)
return log_error_errno(errno, "Failed to open /dev/vhost-vsock as read/write: %m");
}
r = vsock_fix_child_cid(config.vsock.fd, &child_cid, arg_machine);
if (r < 0)
return log_error_errno(r, "Failed to fix CID for the guest VSOCK socket: %m");
config.vsock.cid = child_cid;
}
/* -cpu stays on cmdline since not all flags are supported in config. SNP needs a stable,
* named CPU model so the launch measurement is reproducible across hosts; EPYC-v4 is the
* baseline that covers all SNP-capable processors (Milan and later). TDX requires host
* CPU model. */
const char *cpu_model =
#ifdef __x86_64__
arg_confidential_computing == COCO_SEV_SNP ? "EPYC-v4" :
arg_confidential_computing == COCO_TDX ? "host" :
"max,hv_relaxed,hv-vapic,hv-time";
#else
"max";
#endif
r = strv_extend_many(&cmdline, "-cpu", cpu_model);
if (r < 0)
return log_oom();
_cleanup_close_ int master = -EBADF;
PTYForwardFlags ptyfwd_flags = 0;
switch (arg_console_mode) {
case CONSOLE_NATIVE:
/* Use a PTY instead of chardev stdio to prevent QEMU from setting O_NONBLOCK on
* our stdio file descriptions (see qemu's chardev/char-stdio.c and char-fd.c).
* Use PTY_FORWARD_DUMB_TERMINAL|PTY_FORWARD_TRANSPARENT so the forwarder just
* shovels bytes without any terminal manipulation or escape sequence handling. */
ptyfwd_flags |= PTY_FORWARD_DUMB_TERMINAL|PTY_FORWARD_TRANSPARENT;
_fallthrough_;
case CONSOLE_READ_ONLY:
if (arg_console_mode == CONSOLE_READ_ONLY)
ptyfwd_flags |= PTY_FORWARD_READ_ONLY;
_fallthrough_;
case CONSOLE_INTERACTIVE: {
_cleanup_free_ char *pty_path = NULL;
master = openpt_allocate(O_RDWR|O_NONBLOCK, &pty_path);
if (master < 0)
return log_error_errno(master, "Failed to setup pty: %m");
r = strv_extend_many(&cmdline, "-nographic", "-nodefaults");
if (r < 0)
return log_oom();
/* Enable mux for native console so the QEMU monitor is accessible via Ctrl-a c */
r = qemu_config_section(config_file, "chardev", "console",
"backend", "serial",
"path", pty_path,
"mux", on_off(arg_console_mode == CONSOLE_NATIVE));
if (r < 0)
return r;
if (arg_console_mode == CONSOLE_NATIVE) {
r = qemu_config_section(config_file, "mon", "mon0",
"chardev", "console");
if (r < 0)
return r;
}
break;
}
case CONSOLE_GUI:
/* -vga is a convenience option, keep on cmdline */
r = strv_extend_many(&cmdline, "-vga", "virtio");
if (r < 0)
return log_oom();
r = qemu_config_section(config_file, "device", "virtio-serial0",
"driver", "virtio-serial");
if (r < 0)
return r;
r = qemu_config_section(config_file, "chardev", "vdagent",
"backend", "qemu-vdagent",
"clipboard", "on",
"debug", "0");
if (r < 0)
return r;
r = qemu_config_section(config_file, "device", "vdagent-port0",
"driver", "virtserialport",
"chardev", "vdagent",
"name", "org.qemu.guest_agent.0");
if (r < 0)
return r;
/* Attach a USB xHCI controller and a USB keyboard. We prefer USB over the implicit PS/2
* keyboard so that EDK2's UsbKbDxe driver runs, which registers the default HII keyboard
* layout package — the PS/2 driver does not. That makes
* EFI_HII_DATABASE_PROTOCOL.GetKeyboardLayout() return a usable layout, which systemd-boot
* then exports via the LoaderKeyboardLayout EFI variable, which is useful for testing that
* codepath actually works. */
r = qemu_config_section(config_file, "device", "xhci0",
"driver", "qemu-xhci");
if (r < 0)
return r;
r = qemu_config_section(config_file, "device", "usb-kbd0",
"driver", "usb-kbd",
"bus", "xhci0.0");
if (r < 0)
return r;
/* When using --console=gui the QEMU window closes immediately when the VM has stopped, so
* any console output at shutdown is lost, which makes debugging difficult. Ensure the VM
* stays booted for a minimum of 15s. */
r = strv_prepend(&arg_kernel_cmdline_extra, "systemd.minimum_uptime_sec=15");
if (r < 0)
return log_oom();
break;
case CONSOLE_HEADLESS:
r = strv_extend_many(&cmdline, "-nographic", "-nodefaults");
if (r < 0)
return log_oom();
break;
default:
assert_not_reached();
}
if (!IN_SET(arg_console_mode, CONSOLE_GUI, CONSOLE_HEADLESS)) {
if (arg_console_transport == CONSOLE_TRANSPORT_SERIAL) {
/* Use -serial to connect the chardev to the platform's default serial
* device (e.g. isa-serial on x86, PL011 on ARM). On some platforms the
* serial device is a sysbus device that can only be connected via
* serial_hd() which is populated by -serial, not via the config file. */
r = strv_extend_many(&cmdline, "-serial", "chardev:console");
if (r < 0)
return log_oom();
} else {
r = qemu_config_section(config_file, "device", "vmspawn-virtio-serial-pci",
"driver", "virtio-serial-pci");
if (r < 0)
return r;
r = qemu_config_section(config_file, "device", "virtconsole0",
"driver", "virtconsole",
"chardev", "console");
if (r < 0)
return r;
}
}
/* Memory-mapped firmware asks for -bios loading by definition. Under
* confidential computing -bios is used even for (stateless) flash firmware. */
_cleanup_close_pair_ int ovmf_vars_fds[2] = EBADF_PAIR;
if (ovmf_config && (arg_confidential_computing != COCO_NO || streq_ptr(ovmf_config->device, "memory"))) {
r = strv_extend_many(&cmdline, "-bios", ovmf_config->path);
if (r < 0)
return r;
} else {
r = cmdline_add_ovmf(config_file, &cmdline, &pass_fds, &n_pass_fds, ovmf_config, ovmf_vars_fds);
if (r < 0)
return r;
}
if (arg_linux) {
r = strv_extend_many(&cmdline, "-kernel", arg_linux);
if (r < 0)
return log_oom();
/* We can't rely on gpt-auto-generator when direct kernel booting so synthesize a root=
* kernel argument instead. */
if (arg_linux_image_type != KERNEL_IMAGE_TYPE_UKI && arg_image) {
r = kernel_cmdline_maybe_append_root();
if (r < 0)
return r;
}
}
if (arg_image) {
assert(!arg_directory);
if (arg_image_format == IMAGE_FORMAT_QCOW2) {
r = verify_regular_at(AT_FDCWD, arg_image, /* follow= */ true);
if (r < 0)
return log_error_errno(r,
"Block device '%s' cannot be used with 'qcow2' format, only 'raw' is supported: %m",
arg_image);
}
if (arg_image_disk_type == DISK_TYPE_VIRTIO_SCSI_CDROM) {
/* CD-ROMs are read-only, so override any "rw" on the kernel command line. */
if (strv_contains(arg_kernel_cmdline_extra, "rw") &&
strv_extend(&arg_kernel_cmdline_extra, "ro") < 0)
return log_oom();
} else if (!arg_ephemeral) {
/* In ephemeral mode the original image is never written to, the requested size is
* applied to the qcow2 overlay instead, see prepare_primary_drive(). */
r = grow_image(arg_image, arg_grow_image);
if (r < 0)
return r;
}
}
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
r = sd_event_new(&event);
if (r < 0)
return log_error_errno(r, "Failed to get default event loop: %m");
(void) sd_event_set_watchdog(event, true);
_cleanup_free_ char *unit = NULL;
r = unit_name_mangle_with_suffix(arg_machine, "as machine name", /* flags= */ 0, ".scope", &unit);
if (r < 0)
return log_error_errno(r, "Failed to mangle scope name: %m");
_cleanup_free_ char *sd_socket_activate = NULL;
r = find_executable("systemd-socket-activate", &sd_socket_activate);
if (r < 0)
return log_error_errno(r, "Failed to find systemd-socket-activate binary: %m");
if (arg_directory) {
_cleanup_free_ char *listen_address = NULL;
_cleanup_(fork_notify_terminate) PidRef child = PIDREF_NULL;
if (!GREEDY_REALLOC(children, n_children + 1))
return log_oom();
if (arg_ephemeral) {
r = create_ephemeral_snapshot(arg_directory,
arg_runtime_scope,
/* read-only */ false,
&tree_global_lock,
&tree_local_lock,
&snapshot_directory);
if (r < 0)
return log_error_errno(r, "Failed to create ephemeral snapshot of '%s': %m", arg_directory);
r = free_and_strdup(&arg_directory, snapshot_directory);
if (r < 0)
return log_oom();
}
r = start_virtiofsd(
arg_directory,
/* source_uid= */ arg_uid_shift,
/* target_uid= */ 0,
/* uid_range= */ arg_uid_range,
runtime_dir,
&listen_address,
&child);
if (r < 0)
return r;
_cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
r = event_add_child_pidref(event, &source, &child, WEXITED, on_child_exit, /* userdata= */ NULL);
if (r < 0)
return r;
pidref_done(&child);
children[n_children++] = TAKE_PTR(source);
_cleanup_free_ char *id = strdup("rootdir"), *tag = strdup("root");
if (!id || !tag)
return log_oom();
if (!GREEDY_REALLOC(config.virtiofs.entries, config.virtiofs.n_entries + 1))
return log_oom();
config.virtiofs.entries[config.virtiofs.n_entries++] = (VirtiofsInfo) {
.id = TAKE_PTR(id),
.socket_path = TAKE_PTR(listen_address),
.tag = TAKE_PTR(tag),
};
if (strv_extend(&arg_kernel_cmdline_extra, "root=root rootfstype=virtiofs rw") < 0)
return log_oom();
}
/* Extra drive validation is done in the post-fork drive info construction loop
* to avoid stat()'ing each drive twice. */
if (!IN_SET(arg_console_mode, CONSOLE_GUI, CONSOLE_HEADLESS)) {
r = strv_prepend(&arg_kernel_cmdline_extra,
arg_console_transport == CONSOLE_TRANSPORT_SERIAL ?
"console=" QEMU_SERIAL_CONSOLE_NAME : "console=hvc0");
if (r < 0)
return log_oom();
/* Propagate the host's $TERM into the VM via the kernel command line. TERM= is
* picked up by PID 1 and inherited by services on /dev/console, and
* systemd.tty.term.hvc0= is used by services directly attached to /dev/hvc0 (such
* as serial-getty). While systemd can auto-detect the terminal type via DCS
* XTGETTCAP, not all terminal emulators implement this, so let's always propagate
* $TERM if we have it. */
const char *term = getenv("TERM");
if (term_env_valid(term)) {
FOREACH_STRING(tty_key, "systemd.tty.term.hvc0", "TERM") {
_cleanup_free_ char *p = strjoin(tty_key, "=", term);
if (!p)
return log_oom();
if (strv_consume_prepend(&arg_kernel_cmdline_extra, TAKE_PTR(p)) < 0)
return log_oom();
}
}
}
_cleanup_free_ char *fstab_extra = NULL, *fstab_extra_initrd = NULL;
for (size_t j = 0; j < arg_runtime_mounts.n_mounts; j++) {
RuntimeMount *m = arg_runtime_mounts.mounts + j;
_cleanup_free_ char *listen_address = NULL, *id = NULL, *tag = NULL;
_cleanup_(fork_notify_terminate) PidRef child = PIDREF_NULL;
if (!GREEDY_REALLOC(children, n_children + 1))
return log_oom();
r = start_virtiofsd(
m->source,
/* source_uid= */ m->source_uid,
/* target_uid= */ m->target_uid,
/* uid_range= */ 1U,
runtime_dir,
&listen_address,
&child);
if (r < 0)
return r;
_cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
r = event_add_child_pidref(event, &source, &child, WEXITED, on_child_exit, /* userdata= */ NULL);
if (r < 0)
return r;
pidref_done(&child);
children[n_children++] = TAKE_PTR(source);
if (asprintf(&id, "mnt%zu", j) < 0)
return log_oom();
tag = strdup(id);
if (!tag)
return log_oom();
/* fstab uses whitespace as field separator, so octal-escape spaces in paths */
_cleanup_free_ char *escaped_target = octescape_full(m->target, SIZE_MAX, " \t");
if (!escaped_target)
return log_oom();
/* switch-root moves the initrd's /run onto /sysroot/run. The moved /run hides any mount that
* the initrd placed below /sysroot/run. We therefore mount targets below /run at /run in the
* initrd. switch-root then moves the mount together with /run. fstab-generator reads
* fstab.extra.initrd only in the initrd. The fstab.extra entry without x-initrd.mount mounts
* the target when the VM boots without an initrd. A target such as /run/x/../../srv resolves
* to /srv. An fstab.extra.initrd entry for that target would mount over /srv in the initrd.
* path_startswith() doesn't check the components after the prefix. path_is_safe() therefore
* refuses every ".." component in the target. */
bool below_run = path_is_safe(m->target) && !isempty(path_startswith(m->target, "/run"));
if (below_run &&
strextendf(&fstab_extra_initrd, "%s %s virtiofs %s\n",
id, escaped_target, m->read_only ? "ro" : "rw") < 0)
return log_oom();
if (strextendf(&fstab_extra, "%s %s virtiofs %s%s\n",
id, escaped_target, m->read_only ? "ro" : "rw",
below_run ? "" : ",x-initrd.mount") < 0)
return log_oom();
if (!GREEDY_REALLOC(config.virtiofs.entries, config.virtiofs.n_entries + 1))
return log_oom();
config.virtiofs.entries[config.virtiofs.n_entries++] = (VirtiofsInfo) {
.id = TAKE_PTR(id),
.socket_path = TAKE_PTR(listen_address),
.tag = TAKE_PTR(tag),
};
}
if (fstab_extra) {
r = append_credential("fstab.extra", fstab_extra);
if (r < 0)
return r;
}
if (fstab_extra_initrd) {
r = append_credential("fstab.extra.initrd", fstab_extra_initrd);
if (r < 0)
return r;
}
_cleanup_(rm_rf_physical_and_freep) char *smbios_dir = NULL;
_cleanup_close_ int smbios_dir_fd = mkdtemp_open("/var/tmp/vmspawn-smbios-XXXXXX", /* flags= */ 0, &smbios_dir);
if (smbios_dir_fd < 0)
return log_error_errno(smbios_dir_fd, "Failed to create temporary directory: %m");
r = cmdline_add_smbios11(&cmdline, smbios_dir_fd, smbios_dir);
if (r < 0)
return r;
/* disable TPM autodetection if the user's hardware doesn't support it */
if (!ARCHITECTURE_SUPPORTS_TPM) {
if (arg_tpm > 0)
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM not supported on %s, refusing", architecture_to_string(native_architecture()));
if (arg_tpm < 0) {
arg_tpm = false;
log_debug("TPM not supported on %s, disabling tpm autodetection and continuing", architecture_to_string(native_architecture()));
}
}
_cleanup_free_ char *swtpm = NULL;
if (arg_tpm != 0) {
if (arg_tpm_state_mode == STATE_AUTO && !arg_ephemeral) {
assert(!arg_tpm_state_path);
r = make_sidecar_path(".tpmstate", &arg_tpm_state_path);
if (r < 0)
return r;
log_debug("Storing TPM state persistently under '%s'.", arg_tpm_state_path);
}
r = find_executable("swtpm", &swtpm);
if (r < 0) {
/* log if the user asked for swtpm and we cannot find it */
if (arg_tpm > 0)
return log_error_errno(r, "Failed to find swtpm binary: %m");
/* also log if we got an error other than ENOENT from find_executable */
if (r != -ENOENT && arg_tpm < 0)
return log_error_errno(r, "Error detecting swtpm: %m");
}
}
_cleanup_free_ char *tpm_socket_address = NULL;
if (swtpm) {
_cleanup_(fork_notify_terminate) PidRef child = PIDREF_NULL;
if (!GREEDY_REALLOC(children, n_children + 1))
return log_oom();
r = start_tpm(swtpm, runtime_dir, sd_socket_activate, &tpm_socket_address, &child);
if (r < 0) {
/* only bail if the user asked for a tpm */
if (arg_tpm > 0)
return log_error_errno(r, "Failed to start tpm: %m");
log_debug_errno(r, "Failed to start tpm, ignoring: %m");
} else {
_cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
r = event_add_child_pidref(event, &source, &child, WEXITED, on_child_exit, /* userdata= */ NULL);
if (r < 0)
return r;
pidref_done(&child);
children[n_children++] = TAKE_PTR(source);
}
}
if (tpm_socket_address) {
r = qemu_config_section(config_file, "chardev", "chrtpm",
"backend", "socket",
"path", tpm_socket_address);
if (r < 0)
return r;
r = qemu_config_section(config_file, "tpmdev", "tpm0",
"type", "emulator",
"chardev", "chrtpm");
if (r < 0)
return r;
const char *tpm_driver;
if (native_architecture() == ARCHITECTURE_X86_64)
tpm_driver = "tpm-tis";
else if (IN_SET(native_architecture(), ARCHITECTURE_ARM64, ARCHITECTURE_ARM64_BE))
tpm_driver = "tpm-tis-device";
else
tpm_driver = NULL;
if (tpm_driver) {
r = qemu_config_section(config_file, "device", "tpmdev0",
"driver", tpm_driver,
"tpmdev", "tpm0");
if (r < 0)
return r;
}
}
if (arg_forward_journal) {
_cleanup_free_ char *listen_address = NULL;
if (asprintf(&listen_address, "vsock:2:%u", child_cid) < 0)
return log_oom();
if (!GREEDY_REALLOC(children, n_children + 1))
return log_oom();
_cleanup_(fork_notify_terminate) PidRef child = PIDREF_NULL;
r = fork_journal_remote(
listen_address,
arg_forward_journal,
arg_forward_journal_max_use,
arg_forward_journal_keep_free,
arg_forward_journal_max_file_size,
arg_forward_journal_max_files,
&child);
if (r < 0)
return r;
_cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL;
r = event_add_child_pidref(event, &source, &child, WEXITED, on_child_exit, /* userdata= */ NULL);
if (r < 0)
return r;
pidref_done(&child);
children[n_children++] = TAKE_PTR(source);
r = machine_credential_add(&arg_credentials, "journal.forward_to_socket", listen_address, SIZE_MAX);
if (r < 0)
return r;
}
if (arg_pass_ssh_key) {
_cleanup_free_ char *privkey_path = NULL, *pubkey_path = NULL;
const char *key_type = arg_ssh_key_type ?: "ed25519";
privkey_path = path_join(runtime_dir, key_type);
if (!privkey_path)
return log_oom();
pubkey_path = strjoin(privkey_path, ".pub");
if (!pubkey_path)
return log_oom();
r = generate_ssh_keypair(privkey_path, key_type);
if (r < 0)
return r;
ssh_private_key_path = TAKE_PTR(privkey_path);
ssh_public_key_path = TAKE_PTR(pubkey_path);
}
if (ssh_public_key_path && ssh_private_key_path) {
_cleanup_free_ char *cred_path = NULL;
cred_path = strjoin("ssh.ephemeral-authorized_keys-all:", ssh_public_key_path);
if (!cred_path)
return log_oom();
r = machine_credential_load(&arg_credentials, cred_path);
if (r < 0)
return log_error_errno(r, "Failed to load credential %s: %m", cred_path);
/* on distros that provide their own sshd@.service file we need to provide a dropin which
* picks up our public key credential */
/* sshd reads AuthorizedKeysFile after dropping to the authenticating user's UID, so the
* 0400 credential file under %d/ is unreadable for non-root users. Materialize a 0444
* copy in a RuntimeDirectory so the ephemeral key works for any user. */
r = machine_credential_add(
&arg_credentials,
"systemd.unit-dropin.sshd-vsock@.service",
"[Service]\n"
"ExecStartPre=systemd-tmpfiles --create --inline 'f^ /run/sshd-vsock-%i/authorized_keys 0444 root root - ssh.ephemeral-authorized_keys-all'\n"
"ExecStart=\n"
"ExecStart=-sshd -i -o 'AuthorizedKeysFile=/run/sshd-vsock-%i/authorized_keys .ssh/authorized_keys'\n"
"ImportCredential=ssh.ephemeral-authorized_keys-all\n"
"RuntimeDirectory=sshd-vsock-%i\n",
SIZE_MAX);
if (r < 0)
return log_error_errno(r, "Failed to set credential systemd.unit-dropin.sshd-vsock@.service: %m");
}
if (use_vsock) {
notify_sock_fd = open_vsock();
if (notify_sock_fd < 0)
return log_error_errno(notify_sock_fd, "Failed to open VSOCK: %m");
r = add_vsock_credential(notify_sock_fd);
if (r < 0)
return log_error_errno(r, "Failed to add VSOCK credential: %m");
}
/* Under --coco=sev-snp the SMBIOS and fw_cfg channels normally used to deliver credentials are
* not covered by the launch measurement and are silently discarded by the guest PID1 in
* confidential VMs. Instead, package credentials into a cpio archive appended to the initrd
* (mirroring what systemd-stub does for ESP credentials) so they enter the launch measurement
* via QEMU's "kernel-hashes=on".
* Under --coco=tdx the guest PID1 accepts SMBIOS credentials only if systemd-stub or systemd-boot
* measured the SMBIOS table (the firmware's own SMBIOS measurement zeroes the OEM strings), so
* direct linux boot uses the cpio as well. The Linux EFI stub measures the initrd into RTMR 2
* there.
* The initrd path requires a guest PID1 that knows about /.extra/system_credentials/, so it stays
* limited to these cases and everything else continues to use the SMBIOS path below, which works
* with older systemd versions too.
* Must run after all credential-mutating calls above so the cpio captures the complete set. */
bool use_initrd_cpio = arg_credentials.n_credentials > 0 &&
(arg_confidential_computing == COCO_SEV_SNP ||
(arg_confidential_computing == COCO_TDX && arg_linux && arg_linux_image_type != KERNEL_IMAGE_TYPE_UKI));
_cleanup_(unlink_and_freep) char *credentials_cpio_path = NULL;
if (use_initrd_cpio) {
r = initrd_cpio_credentials_to_tempfile(&arg_credentials, &credentials_cpio_path);
if (r < 0)
return r;
r = strv_extend(&arg_initrds, credentials_cpio_path);
if (r < 0)
return log_oom();
}
char *initrd = NULL;
_cleanup_(rm_rf_physical_and_freep) char *merged_initrd = NULL;
size_t n_initrds = strv_length(arg_initrds);
if (n_initrds == 1)
initrd = arg_initrds[0];
else if (n_initrds > 1) {
r = merge_initrds(&merged_initrd);
if (r < 0)
return r;
initrd = merged_initrd;
}
if (initrd) {
r = strv_extend_many(&cmdline, "-initrd", initrd);
if (r < 0)
return log_oom();
}
/* Where the initrd cpio carries the credentials (see above), skip the host channels. For
* everyone else, use the SMBIOS/fw_cfg/cmdline path. */
if (!use_initrd_cpio) {
r = cmdline_add_credentials(&cmdline, smbios_dir_fd, smbios_dir);
if (r < 0)
return r;
}
r = cmdline_add_kernel_cmdline(&cmdline, smbios_dir_fd, smbios_dir);
if (r < 0)
return r;
_cleanup_close_pair_ int bridge_fds[2] = EBADF_PAIR;
r = qemu_config_add_qmp_monitor(config_file, bridge_fds, &pass_fds, &n_pass_fds);
if (r < 0)
return r;
/* Pre-allocate PCIe root ports for QMP device_add hotplug. On PCIe machine types
* (q35, virt), QMP device_add is always hotplug — the root bus (pcie.0) does not support
* it. Each root port provides one slot for hotplug. We create enough ports for all devices
* that will be set up via QMP, plus VMSPAWN_PCIE_HOTPLUG_SPARES spare ports for future
* runtime hotplug. */
if (ARCHITECTURE_NEEDS_PCIE_ROOT_PORTS) {
/* Count the PCI devices that assign_pcie_ports() will place on a builtin port:
* one per non-SCSI drive (root + extras + bind volumes; SCSI drives share a
* virtio-scsi-pci controller drawn from the hotplug pool, see
* assign_pcie_ports()), one if network is configured, one per virtiofs entry,
* one if vsock is in use. Plus a fixed pool of hotplug spares for runtime
* device_add. */
size_t n_drive_ports = 0;
if (!IN_SET(arg_image_disk_type, DISK_TYPE_VIRTIO_SCSI, DISK_TYPE_VIRTIO_SCSI_CDROM))
n_drive_ports++;
FOREACH_ARRAY(d, arg_extra_drives.drives, arg_extra_drives.n_drives) {
DiskType dt = d->disk_type >= 0 ? d->disk_type : arg_image_disk_type;
if (!IN_SET(dt, DISK_TYPE_VIRTIO_SCSI, DISK_TYPE_VIRTIO_SCSI_CDROM))
n_drive_ports++;
}
FOREACH_ARRAY(bv, arg_bind_volumes.items, arg_bind_volumes.n_items) {
DiskType dt = disk_type_from_bind_volume_config((*bv)->config);
if (dt < 0)
continue; /* unreachable: parser rejects invalid configs */
if (!IN_SET(dt, DISK_TYPE_VIRTIO_SCSI, DISK_TYPE_VIRTIO_SCSI_CDROM))
n_drive_ports++;
}
size_t n_pcie_ports =
n_drive_ports + /* non-SCSI drives */
(arg_network_stack != NETWORK_STACK_NONE ? 1 : 0) + /* network */
(arg_directory ? 1 : 0) + /* rootdir virtiofs */
arg_runtime_mounts.n_mounts + /* runtime virtiofs */
(use_vsock ? 1 : 0) + /* vsock */
VMSPAWN_PCIE_HOTPLUG_SPARES; /* hotplug pool */
/* Guard the unsigned subtraction below against future refactors that might drop the
* fixed additions. */
assert(n_pcie_ports >= VMSPAWN_PCIE_HOTPLUG_SPARES);
/* Cap derived from the packing range: cannot exceed VMSPAWN_PCIE_PACK_MAX_PORTS
* (= 15 slots × 8 functions = 120) without running into the 0x1f LPC slot. */
if (n_pcie_ports > VMSPAWN_PCIE_PACK_MAX_PORTS)
return log_error_errno(SYNTHETIC_ERRNO(E2BIG),
"Too many PCIe root ports requested (%zu, max %u). "
"Reduce the number of extra drives or runtime mounts.",
n_pcie_ports, (unsigned) VMSPAWN_PCIE_PACK_MAX_PORTS);
size_t n_builtin_ports = n_pcie_ports - VMSPAWN_PCIE_HOTPLUG_SPARES;
for (size_t i = 0; i < n_pcie_ports; i++) {
char id[STRLEN("vmspawn-hotplug-pci-root-port-") + DECIMAL_STR_MAX(size_t)];
if (i < n_builtin_ports)
xsprintf(id, "vmspawn-pcieport-%zu", i);
else
xsprintf(id, "vmspawn-hotplug-pci-root-port-%zu", i - n_builtin_ports);
r = qemu_config_section(config_file, "device", id,
"driver", "pcie-root-port");
if (r < 0)
return r;
/* chassis/slot are the PCIe-chassis identity (ACPI hotplug paths),
* independent of the PCI bus address below. */
r = qemu_config_keyf(config_file, "chassis", "%zu", i + 1);
if (r < 0)
return r;
r = qemu_config_keyf(config_file, "slot", "%zu", i + 1);
if (r < 0)
return r;
/* Pack 8 root ports per pcie.0 device-number as multifunction, so 14
* ports cost 2 slots on pcie.0 instead of 14. Each function remains
* independently hot-pluggable (QEMU docs/pcie.txt §5.1). */
size_t pci_slot = VMSPAWN_PCIE_PACK_BASE_SLOT + i / 8;
size_t pci_fn = i % 8;
assert(pci_slot < VMSPAWN_PCIE_PACK_END_SLOT);
r = qemu_config_keyf(config_file, "addr", "0x%zx.%zu", pci_slot, pci_fn);
if (r < 0)
return r;
if (pci_fn == 0) {
r = qemu_config_key(config_file, "multifunction", "on");
if (r < 0)
return r;
}
}
}
/* Finalize the config file and add -readconfig to the cmdline */
r = fflush_and_check(config_file);
if (r < 0)
return log_error_errno(r, "Failed to write QEMU config file: %m");
config_file = safe_fclose(config_file);
r = strv_extend_many(&cmdline, "-readconfig", config_path);
if (r < 0)
return log_oom();
const char *e = secure_getenv("SYSTEMD_VMSPAWN_QEMU_EXTRA");
if (e) {
r = strv_split_and_extend_full(&cmdline, e,
/* separators= */ NULL, /* filter_duplicates= */ false,
EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
if (r < 0)
return log_error_errno(r, "Failed to parse $SYSTEMD_VMSPAWN_QEMU_EXTRA: %m");
}
if (DEBUG_LOGGING) {
_cleanup_free_ char *config_contents = NULL;
r = read_full_file(config_path, &config_contents, /* ret_size= */ NULL);
if (r < 0)
log_debug_errno(r, "Failed to read back QEMU config file, ignoring: %m");
else
log_debug("QEMU config file %s:\n%s", config_path, config_contents);
_cleanup_free_ char *joined = quote_command_line(cmdline, SHELL_ESCAPE_EMPTY);
if (!joined)
return log_oom();
log_debug("Executing: %s", joined);
}
_cleanup_close_ int child_pty = -EBADF;
if (master >= 0) {
child_pty = pty_open_peer(master, O_RDWR|O_CLOEXEC|O_NOCTTY);
if (child_pty < 0)
return log_error_errno(child_pty, "Failed to open PTY slave: %m");
}
/* SIGTERM, not SIGKILL — let QEMU flush state on error-path early exits. */
_cleanup_(pidref_done_sigterm_wait) PidRef child_pidref = PIDREF_NULL;
r = pidref_safe_fork_full(
qemu_binary,
child_pty >= 0 ? (const int[]) { child_pty, child_pty, child_pty } : NULL,
pass_fds, n_pass_fds,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_CLOEXEC_OFF|FORK_RLIMIT_NOFILE_SAFE|
(child_pty >= 0 ? FORK_REARRANGE_STDIO : 0),
&child_pidref);
if (r < 0)
return r;
if (r == 0) {
if (setenv("LANG", "C.UTF-8", 0) < 0) {
log_oom();
goto fail;
}
execv(qemu_binary, cmdline);
log_error_errno(errno, "Failed to execve %s: %m", qemu_binary);
fail:
_exit(EXIT_FAILURE);
}
/* Not owned. Disarmed once the guest runs, as then the console has its output instead of QEMU's. */
_cleanup_(flush_console_to_stderrp) int early_console = master;
/* Close QEMU's end of the QMP socketpair in the parent. We don't need it anymore. */
child_pty = safe_close(child_pty);
bridge_fds[1] = safe_close(bridge_fds[1]);
r = prepare_device_info(runtime_dir, &config);
if (r < 0)
return r;
/* Connect to VMM backend */
_cleanup_(vmspawn_qmp_bridge_freep) VmspawnQmpBridge *bridge = NULL;
r = vmspawn_qmp_init(&bridge, TAKE_FD(bridge_fds[0]), event);
if (r < 0)
return r;
/* Probe QEMU feature availability synchronously before device setup consumes the flags. */
r = vmspawn_qmp_probe_features(bridge);
if (r < 0)
return r;
/* Device setup — all before resuming vCPUs */
r = vmspawn_qmp_setup_drives(bridge, &config.drives);
if (r < 0)
return r;
if (config.network.type) {
r = vmspawn_qmp_setup_network(bridge, &config.network);
if (r < 0)
return r;
}
r = vmspawn_qmp_setup_virtiofs(bridge, &config.virtiofs);
if (r < 0)
return r;
r = vmspawn_qmp_setup_vsock(bridge, &config.vsock);
if (r < 0)
return r;
/* Resume vCPUs and switch to async event processing */
r = vmspawn_qmp_start(bridge);
if (r < 0)
return r;
early_console = -EBADF;
/* Varlink server for VM control */
_cleanup_(vmspawn_varlink_context_freep) VmspawnVarlinkContext *varlink_ctx = NULL;
_cleanup_free_ char *control_address = NULL;
r = vmspawn_varlink_setup(&varlink_ctx, bridge, runtime_dir, &control_address);
if (r < 0)
return r;
TAKE_PTR(bridge);
if (!arg_keep_unit) {
/* When a new scope is created for this container, then we'll be registered as its controller, in which
* case PID 1 will send us a friendly RequestStop signal, when it is asked to terminate the
* scope. Let's hook into that, and cleanly shut down the container, and print a friendly message. */
r = sd_bus_match_signal_async(
runtime_bus,
/* ret= */ NULL,
"org.freedesktop.systemd1",
/* path= */ NULL,
"org.freedesktop.systemd1.Scope",
"RequestStop",
on_request_stop,
/* install_callback= */ NULL,
/* userdata= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to request RequestStop match: %m");
}
bool scope_allocated = false;
if (!arg_keep_unit && (arg_register == 0 || arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)) {
r = allocate_scope(
runtime_bus,
arg_machine,
&child_pidref,
children,
n_children,
unit,
arg_slice,
arg_property,
/* allow_pidfd= */ true);
if (r < 0)
return r;
scope_allocated = true;