HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/vmspawn/vmspawn.c
Git blob: 1258b35c7ded8fefe9ea7f21e6147dfd8810fc39
Lines 1–1000 of 4,443
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <poll.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-daemon.h"
#include "sd-event.h"
#include "sd-id128.h"
#include "sd-json.h"
#include "sd-varlink.h"
#include "alloc-util.h"
#include "architecture.h"
#include "bootspec.h"
#include "build-path.h"
#include "build.h"
#include "bus-error.h"
#include "bus-internal.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "capability-util.h"
#include "common-signal.h"
#include "copy.h"
#include "discover-image.h"
#include "dissect-image.h"
#include "dlopen-note.h"
#include "escape.h"
#include "ether-addr-util.h"
#include "event-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "fork-notify.h"
#include "format-util.h"
#include "fs-util.h"
#include "gpt.h"
#include "group-record.h"
#include "hexdecoct.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "id128-util.h"
#include "initrd-cpio.h"
#include "io-util.h"
#include "kernel-image.h"
#include "log.h"
#include "machine-bind-user.h"
#include "machine-credential.h"
#include "machine-register.h"
#include "main-func.h"
#include "memfd-util.h"
#include "mkdir.h"
#include "namespace-util.h"
#include "netif-util.h"
#include "nsresource.h"
#include "osc-context.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "pidref.h"
#include "polkit-agent.h"
#include "pretty-print.h"
#include "process-util.h"
#include "ptyfwd.h"
#include "random-util.h"
#include "rm-rf.h"
#include "set.h"
#include "sha256.h"
#include "signal-util.h"
#include "snapshot-util.h"
#include "socket-util.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "swtpm-util.h"
#include "sync-util.h"
#include "terminal-util.h"
#include "tmpfile-util.h"
#include "uid-classification.h"
#include "unit-name.h"
#include "user-record.h"
#include "user-util.h"
#include "utf8.h"
#include "verbs.h"
#include "vmspawn-bind-volume.h"
#include "vmspawn-mount.h"
#include "vmspawn-qemu-config.h"
#include "vmspawn-qmp.h"
#include "vmspawn-scope.h"
#include "vmspawn-settings.h"
#include "vmspawn-util.h"
#include "vmspawn-varlink.h"
#define VM_TAP_HASH_KEY SD_ID128_MAKE(01,d0,c6,4c,2b,df,24,fb,c0,f8,b2,09,7d,59,b2,93)
#define DISK_SERIAL_MAX_LEN_SCSI 30
#define DISK_SERIAL_MAX_LEN_NVME 20
#define DISK_SERIAL_MAX_LEN_VIRTIO_BLK 20
/* Well-known endpoints for the host's TDX Quote Generation Service (qgsd), auto-discovered so the
* guest can obtain TD Quotes. The Intel reference qgsd listens on a unix socket; the common
* deployment listens on vsock port 4050 (cid 2 = host). */
#define TDX_QGS_UNIX_SOCKET_PATH "/run/tdx-qgs/qgs.socket"
#define TDX_QGS_VSOCK_CID "2"
#define TDX_QGS_VSOCK_PORT "4050"
/* First and one-past-last pcie.0 device-numbers used for multifunction-packed
* pcie-root-ports. Sits above the auto-assigned virtio devices (0x01-0x03) and
* below 0x1f, which q35 reserves for ICH9 LPC at 0x1f.0 (single-function). */
#define VMSPAWN_PCIE_PACK_BASE_SLOT 0x10
#define VMSPAWN_PCIE_PACK_END_SLOT 0x1f
#define VMSPAWN_PCIE_PACK_MAX_PORTS ((VMSPAWN_PCIE_PACK_END_SLOT - VMSPAWN_PCIE_PACK_BASE_SLOT) * 8)
/* An enum controlling how auxiliary state for the VM are maintained, i.e. the TPM state and the EFI variable
* NVRAM. */
typedef enum StateMode {
STATE_OFF, /* keep no state around */
STATE_AUTO, /* keep state around if not ephemeral, derive path from image/directory */
STATE_PATH, /* explicitly specified location */
_STATE_MODE_MAX,
_STATE_MODE_INVALID = -EINVAL,
} StateMode;
typedef struct SSHInfo {
unsigned cid;
char *private_key_path;
unsigned port;
} SSHInfo;
typedef struct ShutdownInfo {
SSHInfo *ssh_info;
PidRef *pidref;
} ShutdownInfo;
static bool arg_quiet = false;
static PagerFlags arg_pager_flags = 0;
static char *arg_directory = NULL;
static char *arg_image = NULL;
static ImageFormat arg_image_format = IMAGE_FORMAT_RAW;
static char *arg_machine = NULL;
static char *arg_slice = NULL;
static char **arg_property = NULL;
static char *arg_cpus = NULL;
static uint64_t arg_ram = UINT64_C(2) * U64_GB;
static uint64_t arg_ram_max = 0;
static unsigned arg_ram_slots = 0;
static int arg_kvm = -1;
static int arg_vsock = -1;
static unsigned arg_vsock_cid = VMADDR_CID_ANY;
static int arg_tpm = -1;
static char *arg_linux = NULL;
static KernelImageType arg_linux_image_type = _KERNEL_IMAGE_TYPE_INVALID;
static char **arg_initrds = NULL;
static ConsoleMode arg_console_mode = CONSOLE_INTERACTIVE;
static ConsoleTransport arg_console_transport = CONSOLE_TRANSPORT_VIRTIO;
static NetworkStack arg_network_stack = NETWORK_STACK_NONE;
static MachineCredentialContext arg_credentials = {};
static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U;
static RuntimeMountContext arg_runtime_mounts = {};
static char *arg_firmware = NULL;
static Firmware arg_firmware_type = _FIRMWARE_INVALID;
static bool arg_firmware_describe = false;
static Set *arg_firmware_features_include = NULL;
static Set *arg_firmware_features_exclude = NULL;
static ConfidentialComputing arg_confidential_computing = COCO_NO;
static char *arg_forward_journal = NULL;
static uint64_t arg_forward_journal_max_use = UINT64_MAX;
static uint64_t arg_forward_journal_keep_free = UINT64_MAX;
static uint64_t arg_forward_journal_max_file_size = UINT64_MAX;
static uint64_t arg_forward_journal_max_files = UINT64_MAX;
static int arg_register = -1;
static bool arg_keep_unit = false;
static sd_id128_t arg_uuid = {};
static char **arg_kernel_cmdline_extra = NULL;
static ExtraDriveContext arg_extra_drives = {};
static BindVolumes arg_bind_volumes = {};
static char *arg_background = NULL;
static bool arg_pass_ssh_key = true;
static char *arg_ssh_key_type = NULL;
static bool arg_discard_disk = true;
static DiskType arg_image_disk_type = DISK_TYPE_VIRTIO_BLK;
static struct ether_addr arg_network_provided_mac = {};
static char **arg_smbios11 = NULL;
static uint64_t arg_grow_image = 0;
static char *arg_tpm_state_path = NULL;
static StateMode arg_tpm_state_mode = STATE_AUTO;
static char *arg_efi_nvram_template = NULL;
static char *arg_efi_nvram_state_path = NULL;
static StateMode arg_efi_nvram_state_mode = STATE_AUTO;
static bool arg_ask_password = true;
static bool arg_notify_ready = true;
static char **arg_bind_user = NULL;
static char *arg_bind_user_shell = NULL;
static bool arg_bind_user_shell_copy = false;
static char **arg_bind_user_groups = NULL;
static bool arg_ephemeral = false;
static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID;
STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
STATIC_DESTRUCTOR_REGISTER(arg_machine, freep);
STATIC_DESTRUCTOR_REGISTER(arg_slice, freep);
STATIC_DESTRUCTOR_REGISTER(arg_cpus, freep);
STATIC_DESTRUCTOR_REGISTER(arg_credentials, machine_credential_context_done);
STATIC_DESTRUCTOR_REGISTER(arg_firmware, freep);
STATIC_DESTRUCTOR_REGISTER(arg_firmware_features_include, set_freep);
STATIC_DESTRUCTOR_REGISTER(arg_firmware_features_exclude, set_freep);
STATIC_DESTRUCTOR_REGISTER(arg_linux, freep);
STATIC_DESTRUCTOR_REGISTER(arg_initrds, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_runtime_mounts, runtime_mount_context_done);
STATIC_DESTRUCTOR_REGISTER(arg_forward_journal, freep);
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline_extra, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_extra_drives, extra_drive_context_done);
STATIC_DESTRUCTOR_REGISTER(arg_bind_volumes, bind_volumes_done);
STATIC_DESTRUCTOR_REGISTER(arg_background, freep);
STATIC_DESTRUCTOR_REGISTER(arg_ssh_key_type, freep);
STATIC_DESTRUCTOR_REGISTER(arg_smbios11, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm_state_path, freep);
STATIC_DESTRUCTOR_REGISTER(arg_efi_nvram_template, freep);
STATIC_DESTRUCTOR_REGISTER(arg_efi_nvram_state_path, freep);
STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user_shell, freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user_groups, strv_freep);
COMMAND(
"systemd-vmspawn\0",
"Spawn a command or OS in a virtual machine.",
.argspec = "[ARGUMENTS…]\0",
.man_pages = "systemd-vmspawn(1)\0",
.pager_flags = &arg_pager_flags,
);
static int parse_environment(void) {
const char *e;
int r;
e = getenv("SYSTEMD_VMSPAWN_NETWORK_MAC");
if (e) {
r = parse_ether_addr(e, &arg_network_provided_mac);
if (r < 0)
return log_error_errno(r, "Failed to parse provided MAC address via environment variable");
}
return 0;
}
static int parse_ram(const char *s) {
_cleanup_free_ char *ram = NULL, *ram_max = NULL, *ram_slots = NULL;
int r;
assert(s);
const char *p = s;
r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &ram, &ram_max, &ram_slots);
if (r < 0)
return log_error_errno(r, "Failed to parse --ram=%s: %m", s);
if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse --ram=%s", s);
if (!isempty(p))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unexpected trailing data in --ram=%s", s);
r = parse_size(ram, 1024, &arg_ram);
if (r < 0)
return log_error_errno(r, "Failed to parse --ram=%s: %m", s);
if (!isempty(ram_max)) {
r = parse_size(ram_max, 1024, &arg_ram_max);
if (r < 0)
return log_error_errno(r, "Failed to parse --ram=%s: %m", s);
} else
arg_ram_max = 0;
if (!isempty(ram_slots)) {
r = safe_atou(ram_slots, &arg_ram_slots);
if (r < 0)
return log_error_errno(r, "Failed to parse --ram=%s: %m", s);
} else
arg_ram_slots = 0;
return 0;
}
static int parse_argv(int argc, char *argv[]) {
int r;
/* Firmware with enrolled keys has been known to cause issues, skip by default */
r = set_put_strdup(&arg_firmware_features_exclude, "enrolled-keys");
if (r < 0)
return log_oom();
assert(argc >= 0);
assert(argv);
/* Our positional arguments are kernel command line arguments rather than a command to
* execute, and those never begin with a dash, so there's no reason to stop looking for
* options at the first of them. "--" still ends option parsing, for the rare argument that
* does look like an option. */
OptionParser opts = { argc, argv };
FOREACH_OPTION_OR_RETURN(c, &opts)
switch (c) {
OPTION_COMMON_HELP:
return command_print_help();
OPTION_COMMON_VERSION:
return version();
OPTION('q', "quiet", NULL, "Do not show status information"):
arg_quiet = true;
break;
OPTION_COMMON_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
OPTION_COMMON_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
OPTION_LONG("system", NULL, "Run in the system service manager scope"):
arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
break;
OPTION_LONG("user", NULL, "Run in the user service manager scope"):
arg_runtime_scope = RUNTIME_SCOPE_USER;
break;
OPTION_GROUP("Image"): {}
OPTION('D', "directory", "PATH", "Root directory for the VM"):
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_directory);
if (r < 0)
return r;
break;
OPTION('x', "ephemeral", NULL, "Run VM with snapshot of the disk or directory"):
arg_ephemeral = true;
break;
OPTION('i', "image", "FILE|DEVICE", "Root file system disk image or device for the VM"):
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
if (r < 0)
return r;
break;
OPTION_LONG("image-format", "FORMAT", "Specify disk image format (raw, qcow2; default: raw)"):
arg_image_format = image_format_from_string(opts.arg);
if (arg_image_format < 0)
return log_error_errno(arg_image_format,
"Invalid image format: %s", opts.arg);
break;
OPTION_LONG("image-disk-type", "TYPE",
"Specify disk type (virtio-blk, virtio-scsi, nvme, scsi-cd; default: virtio-blk)"):
arg_image_disk_type = disk_type_from_string(opts.arg);
if (arg_image_disk_type < 0)
return log_error_errno(arg_image_disk_type,
"Invalid image disk type: %s", opts.arg);
break;
OPTION_LONG("discard-disk", "BOOL", "Control processing of discard requests"):
r = parse_boolean_argument("--discard-disk=", opts.arg, &arg_discard_disk);
if (r < 0)
return r;
break;
OPTION('G', "grow-image", "BYTES", "Grow image file to specified size in bytes"):
if (isempty(opts.arg)) {
arg_grow_image = 0;
break;
}
r = parse_size(opts.arg, 1024, &arg_grow_image);
if (r < 0)
return log_error_errno(r, "Failed to parse --grow-image= parameter: %s", opts.arg);
if (arg_grow_image > UINT64_MAX - 4095)
return log_error_errno(SYNTHETIC_ERRNO(ERANGE),
"Specified --grow-image= size too large: %s", opts.arg);
arg_grow_image = ROUND_UP(arg_grow_image, 4096);
break;
OPTION_GROUP("Host Configuration"): {}
OPTION_LONG("cpus", "CPUS", "Configure number of CPUs in guest"): {}
OPTION_LONG("qemu-smp", "CPUS", /* help= */ NULL): /* Compat alias */
r = free_and_strdup_warn(&arg_cpus, opts.arg);
if (r < 0)
return r;
break;
OPTION_LONG("ram", "BYTES[:MAXBYTES[:SLOTS]]",
"Configure guest's RAM size (and max/slots for hotplug)"): {}
OPTION_LONG("qemu-mem", "BYTES", /* help= */ NULL): /* Compat alias */
r = parse_ram(opts.arg);
if (r < 0)
return r;
break;
OPTION_LONG("kvm", "BOOL", "Enable use of KVM"): {}
OPTION_LONG("qemu-kvm", "BOOL", /* help= */ NULL): /* Compat alias */
r = parse_tristate_argument_with_auto("--kvm=", opts.arg, &arg_kvm);
if (r < 0)
return r;
break;
OPTION_LONG("vsock", "BOOL", "Override autodetection of VSOCK support"): {}
OPTION_LONG("qemu-vsock", "BOOL", /* help= */ NULL): /* Compat alias */
r = parse_tristate_argument_with_auto("--vsock=", opts.arg, &arg_vsock);
if (r < 0)
return r;
break;
OPTION_LONG("vsock-cid", "CID", "Specify the CID to use for the guest's VSOCK support"):
if (isempty(opts.arg))
arg_vsock_cid = VMADDR_CID_ANY;
else {
unsigned cid;
r = vsock_parse_cid(opts.arg, &cid);
if (r < 0)
return log_error_errno(r, "Failed to parse --vsock-cid: %s", opts.arg);
if (!VSOCK_CID_IS_REGULAR(cid))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified CID is not regular, refusing: %u", cid);
arg_vsock_cid = cid;
}
break;
OPTION_LONG("tpm", "BOOL", "Enable use of a virtual TPM"):
r = parse_tristate_argument_with_auto("--tpm=", opts.arg, &arg_tpm);
if (r < 0)
return r;
break;
OPTION_LONG("tpm-state", "off|auto|PATH", "Where to store TPM state"):
r = isempty(opts.arg) ? false :
streq(opts.arg, "auto") ? true :
parse_boolean(opts.arg);
if (r >= 0) {
arg_tpm_state_mode = r ? STATE_AUTO : STATE_OFF;
arg_tpm_state_path = mfree(arg_tpm_state_path);
break;
}
if (!path_is_valid(opts.arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid path in --tpm-state= parameter: %s", opts.arg);
if (!path_is_absolute(opts.arg) && !startswith(opts.arg, "./"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path in --tpm-state= parameter must be absolute or start with './': %s", opts.arg);
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm_state_path);
if (r < 0)
return r;
arg_tpm_state_mode = STATE_PATH;
break;
OPTION_LONG("efi-nvram-template", "PATH", "Set the path to the EFI NVRAM template file to use"):
if (!isempty(opts.arg) && !path_is_absolute(opts.arg) && !startswith(opts.arg, "./"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Absolute path or path starting with './' required.");
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_efi_nvram_template);
if (r < 0)
return r;
break;
OPTION_LONG("efi-nvram-state", "off|auto|PATH", "Where to store EFI Variable NVRAM state"):
r = isempty(opts.arg) ? false :
streq(opts.arg, "auto") ? true :
parse_boolean(opts.arg);
if (r >= 0) {
arg_efi_nvram_state_mode = r ? STATE_AUTO : STATE_OFF;
arg_efi_nvram_state_path = mfree(arg_efi_nvram_state_path);
break;
}
if (!path_is_valid(opts.arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid path in --efi-nvram-state= parameter: %s", opts.arg);
if (!path_is_absolute(opts.arg) && !startswith(opts.arg, "./"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path in --efi-nvram-state= parameter must be absolute or start with './': %s", opts.arg);
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_efi_nvram_state_path);
if (r < 0)
return r;
arg_efi_nvram_state_mode = STATE_PATH;
break;
OPTION_LONG("secure-boot", "BOOL|auto", "Enable searching for firmware supporting SecureBoot"): {
int b;
r = parse_tristate_argument_with_auto("--secure-boot=", opts.arg, &b);
if (r < 0)
return r;
free(set_remove(arg_firmware_features_include, "secure-boot"));
free(set_remove(arg_firmware_features_exclude, "secure-boot"));
if (b >= 0) {
r = set_put_strdup(b > 0 ? &arg_firmware_features_include : &arg_firmware_features_exclude, "secure-boot");
if (r < 0)
return log_oom();
}
break;
}
OPTION_LONG("firmware", "auto|uefi|bios|none|PATH|list|describe",
"Select firmware to use, or a firmware definition file (or list/describe available)"): {
if (isempty(opts.arg) || streq(opts.arg, "auto")) {
arg_firmware = mfree(arg_firmware);
arg_firmware_type = _FIRMWARE_INVALID;
arg_firmware_describe = false;
break;
}
if (streq(opts.arg, "list")) {
_cleanup_strv_free_ char **l = NULL;
r = list_ovmf_config(&l);
if (r < 0)
return log_error_errno(r, "Failed to list firmwares: %m");
bool nl = false;
fputstrv(stdout, l, "\n", &nl);
if (nl)
putchar('\n');
return 0;
}
if (streq(opts.arg, "describe")) {
/* Handled after argument parsing so that --firmware-features= is
* taken into account. */
arg_firmware = mfree(arg_firmware);
/* We only look for UEFI firmware when "describe" is specified. */
arg_firmware_type = FIRMWARE_UEFI;
arg_firmware_describe = true;
break;
}
Firmware f = firmware_from_string(opts.arg);
if (f >= 0) {
arg_firmware = mfree(arg_firmware);
arg_firmware_type = f;
arg_firmware_describe = false;
break;
}
if (!path_is_absolute(opts.arg) && !startswith(opts.arg, "./"))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Expected one of 'auto', 'uefi', 'bios', 'none', 'list', 'describe', or an absolute path or path starting with './', got: %s",
opts.arg);
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_firmware);
if (r < 0)
return r;
arg_firmware_type = FIRMWARE_UEFI;
arg_firmware_describe = false;
break;
}
OPTION_LONG("firmware-features", "FEATURE,...|list",
"Require/exclude specific firmware features"): {
if (isempty(opts.arg)) {
arg_firmware_features_include = set_free(arg_firmware_features_include);
arg_firmware_features_exclude = set_free(arg_firmware_features_exclude);
break;
}
if (streq(opts.arg, "list")) {
_cleanup_strv_free_ char **l = NULL;
r = list_ovmf_firmware_features(&l);
if (r < 0)
return log_error_errno(r, "Failed to list firmware features: %m");
bool nl = false;
fputstrv(stdout, l, "\n", &nl);
if (nl)
putchar('\n');
return 0;
}
_cleanup_strv_free_ char **features = strv_split(opts.arg, ",");
if (!features)
return log_oom();
STRV_FOREACH(feature, features) {
const char *e = startswith(*feature, "~");
r = set_put_strdup(e ? &arg_firmware_features_exclude : &arg_firmware_features_include, e ?: *feature);
if (r < 0)
return log_oom();
}
break;
}
OPTION_LONG("coco", "no|sev-snp|tdx", "Run the guest as a confidential VM"): {
ConfidentialComputing cc = confidential_computing_from_string(opts.arg);
if (cc < 0)
return log_error_errno(cc, "Unknown --coco= value: %s", opts.arg);
arg_confidential_computing = cc;
break;
}
OPTION_GROUP("Networking"): {}
OPTION('n', "network-tap", NULL, "Create a TAP device for networking"):
arg_network_stack = NETWORK_STACK_TAP;
break;
OPTION_LONG("network-user-mode", NULL, "Use user mode networking"):
arg_network_stack = NETWORK_STACK_USER;
break;
OPTION_GROUP("Execution"): {}
OPTION_LONG("linux", "PATH", "Specify the linux kernel for direct kernel boot"):
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_linux);
if (r < 0)
return r;
break;
OPTION_LONG("initrd", "PATH", "Specify the initrd for direct kernel boot"): {
_cleanup_free_ char *initrd_path = NULL;
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &initrd_path);
if (r < 0)
return r;
r = strv_consume(&arg_initrds, TAKE_PTR(initrd_path));
if (r < 0)
return log_oom();
break;
}
OPTION('s', "smbios11", "STRING", "Pass an arbitrary SMBIOS Type #11 string to the VM"):
if (isempty(opts.arg)) {
arg_smbios11 = strv_free(arg_smbios11);
break;
}
if (!utf8_is_valid(opts.arg))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "SMBIOS Type 11 string is not UTF-8 clean, refusing: %s", opts.arg);
if (strv_extend(&arg_smbios11, opts.arg) < 0)
return log_oom();
break;
OPTION_LONG("notify-ready", "BOOL", "Wait for ready notification from the VM"):
r = parse_boolean_argument("--notify-ready=", opts.arg, &arg_notify_ready);
if (r < 0)
return r;
break;
OPTION_GROUP("System Identity"): {}
OPTION('M', "machine", "NAME", "Set the machine name for the VM"):
if (isempty(opts.arg))
arg_machine = mfree(arg_machine);
else {
if (!hostname_is_valid(opts.arg, 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid machine name: %s", opts.arg);
r = free_and_strdup(&arg_machine, opts.arg);
if (r < 0)
return log_oom();
}
break;
OPTION_LONG("uuid", "UUID", "Set a specific machine UUID for the VM"):
r = id128_from_string_nonzero(opts.arg, &arg_uuid);
if (r == -ENXIO)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Machine UUID may not be all zeroes.");
if (r < 0)
return log_error_errno(r, "Invalid UUID: %s", opts.arg);
break;
OPTION_GROUP("Properties"): {}
OPTION('S', "slice", "SLICE", "Place the VM in the specified slice"): {
_cleanup_free_ char *mangled = NULL;
r = unit_name_mangle_with_suffix(opts.arg, /* operation= */ NULL, UNIT_NAME_MANGLE_WARN, ".slice", &mangled);
if (r < 0)
return log_error_errno(r, "Failed to turn '%s' into unit name: %m", opts.arg);
free_and_replace(arg_slice, mangled);
break;
}
OPTION_LONG("property", "NAME=VALUE", "Set scope unit property"):
if (strv_extend(&arg_property, opts.arg) < 0)
return log_oom();
break;
OPTION_LONG("register", "BOOLEAN", "Register VM as machine"):
r = parse_tristate_argument_with_auto("--register=", opts.arg, &arg_register);
if (r < 0)
return r;
break;
OPTION_LONG("keep-unit", NULL,
"Do not register a scope for the machine, reuse the service unit vmspawn is running in"):
arg_keep_unit = true;
break;
OPTION_GROUP("User Namespacing"): {}
OPTION_LONG("private-users", "UIDBASE[:NUIDS]",
"Configure the UID/GID range to map into the virtiofsd namespace"):
r = parse_userns_uid_range(opts.arg, &arg_uid_shift, &arg_uid_range);
if (r < 0)
return r;
break;
OPTION_GROUP("Mounts"): {}
OPTION_LONG("bind", "SOURCE[:TARGET]", "Mount a file or directory from the host into the VM"): {}
OPTION_LONG("bind-ro", "SOURCE[:TARGET]", "Mount a file or directory, but read-only"): {
bool read_only = streq(opts.opt->long_code, "bind-ro");
r = runtime_mount_parse(&arg_runtime_mounts, opts.arg, read_only);
if (r < 0)
return log_error_errno(r, "Failed to parse --%s= argument %s: %m",
opts.opt->long_code, opts.arg);
break;
}
OPTION_LONG("extra-drive", "[FORMAT:][DISKTYPE:]PATH", "Adds an additional disk to the VM"): {
ImageFormat format = IMAGE_FORMAT_RAW;
DiskType extra_disk_type = _DISK_TYPE_INVALID;
_cleanup_free_ char *drive_path = NULL;
r = parse_disk_spec(opts.arg, &format, &extra_disk_type, &drive_path);
if (r < 0)
return r;
if (!GREEDY_REALLOC(arg_extra_drives.drives, arg_extra_drives.n_drives + 1))
return log_oom();
arg_extra_drives.drives[arg_extra_drives.n_drives++] = (ExtraDrive) {
.path = TAKE_PTR(drive_path),
.format = format,
.disk_type = extra_disk_type,
};
break;
}
OPTION_LONG("bind-volume", "PROVIDER:VOLUME[:CONFIG][:KEY=VALUE,...]",
"Acquire a storage volume from a StorageProvider and attach it to the VM"): {
_cleanup_(bind_volume_freep) BindVolume *bv = NULL;
r = bind_volume_parse(opts.arg, &bv);
if (r < 0)
return log_error_errno(r, "Failed to parse --bind-volume= argument '%s': %m", opts.arg);
if (disk_type_from_bind_volume_config(bv->config) < 0) {
_cleanup_free_ char *valid = NULL;
for (DiskType t = 0; t < _DISK_TYPE_MAX; t++)
if (!strextend_with_separator(&valid, ", ", disk_type_to_string(t)))
return log_oom();
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Unknown device type '%s' for --bind-volume=. Valid values: %s.",
bv->config, valid);
}
FOREACH_ARRAY(it, arg_bind_volumes.items, arg_bind_volumes.n_items)
if (streq((*it)->provider, bv->provider) && streq((*it)->volume, bv->volume))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Volume '%s:%s' specified more than once for --bind-volume=.",
bv->provider, bv->volume);
if (!GREEDY_REALLOC(arg_bind_volumes.items, arg_bind_volumes.n_items + 1))
return log_oom();
arg_bind_volumes.items[arg_bind_volumes.n_items++] = TAKE_PTR(bv);
break;
}
OPTION_LONG("bind-user", "NAME", "Bind user from host to virtual machine"):
if (!valid_user_group_name(opts.arg, /* flags= */ 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid user name to bind: %s", opts.arg);
if (strv_extend(&arg_bind_user, opts.arg) < 0)
return log_oom();
break;
OPTION_LONG("bind-user-shell", "BOOL|PATH",
"Configure the shell to use for --bind-user= users"): {
bool copy = false;
char *sh = NULL;
r = parse_user_shell(opts.arg, &sh, ©);
if (r == -ENOMEM)
return log_oom();
if (r < 0)
return log_error_errno(r, "Invalid user shell to bind: %s", opts.arg);
free_and_replace(arg_bind_user_shell, sh);
arg_bind_user_shell_copy = copy;
break;
}
OPTION_LONG("bind-user-group", "GROUP", "Add an auxiliary group to --bind-user= users"):
if (!valid_user_group_name(opts.arg, /* flags= */ 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid bind user auxiliary group name: %s", opts.arg);
if (strv_extend(&arg_bind_user_groups, opts.arg) < 0)
return log_oom();
break;
OPTION_GROUP("Logging"): {}
OPTION_LONG("forward-journal", "FILE|DIR", "Forward the VM's journal to the host"):
r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_forward_journal);
if (r < 0)
return r;
break;
OPTION_LONG("forward-journal-max-use", "BYTES", "Maximum disk space for forwarded journal"):
r = parse_size(opts.arg, 1024, &arg_forward_journal_max_use);
if (r < 0)
return log_error_errno(r, "Failed to parse --forward-journal-max-use= value: %s", opts.arg);
break;
OPTION_LONG("forward-journal-keep-free", "BYTES", "Minimum disk space to keep free"):
r = parse_size(opts.arg, 1024, &arg_forward_journal_keep_free);
if (r < 0)
return log_error_errno(r, "Failed to parse --forward-journal-keep-free= value: %s", opts.arg);
break;
OPTION_LONG("forward-journal-max-file-size", "BYTES", "Maximum size of individual journal files"):
r = parse_size(opts.arg, 1024, &arg_forward_journal_max_file_size);
if (r < 0)
return log_error_errno(r, "Failed to parse --forward-journal-max-file-size= value: %s", opts.arg);
break;
OPTION_LONG("forward-journal-max-files", "N", "Maximum number of journal files to keep"):
r = safe_atou64(opts.arg, &arg_forward_journal_max_files);
if (r < 0)
return log_error_errno(r, "Failed to parse --forward-journal-max-files= value: %s", opts.arg);
break;
OPTION_GROUP("SSH"): {}
OPTION_LONG("pass-ssh-key", "BOOL", "Create an SSH key to access the VM"):
r = parse_boolean_argument("--pass-ssh-key=", opts.arg, &arg_pass_ssh_key);
if (r < 0)
return r;
break;
OPTION_LONG("ssh-key-type", "TYPE", "Choose what type of SSH key to pass"):
if (isempty(opts.arg)) {
arg_ssh_key_type = mfree(arg_ssh_key_type);
break;
}
if (!string_is_safe(opts.arg, STRING_ALLOW_GLOBS))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid value for --ssh-key-type=: %s", opts.arg);
r = free_and_strdup_warn(&arg_ssh_key_type, opts.arg);
if (r < 0)
return r;
break;
OPTION_GROUP("Input/Output"): {}
OPTION_LONG("console", "MODE",
"Console mode (interactive, native, gui, read-only or headless)"):
arg_console_mode = console_mode_from_string(opts.arg);
if (arg_console_mode < 0)
return log_error_errno(arg_console_mode, "Failed to parse specified console mode: %s", opts.arg);
break;
OPTION_LONG("console-transport", "TRANSPORT", "Console transport (virtio or serial)"):
arg_console_transport = console_transport_from_string(opts.arg);
if (arg_console_transport < 0)
return log_error_errno(arg_console_transport, "Failed to parse specified console transport: %s", opts.arg);
break;
OPTION_LONG("qemu-gui", NULL, /* help= */ NULL): /* Compat alias */
arg_console_mode = CONSOLE_GUI;
break;
OPTION_LONG("background", "COLOR", "Set ANSI color for background"):
r = parse_background_argument(opts.arg, &arg_background);
if (r < 0)
return r;
break;
OPTION_GROUP("Credentials"): {}
OPTION_LONG("set-credential", "ID:VALUE", "Pass a credential with literal value to the VM"):
r = machine_credential_set(&arg_credentials, opts.arg);
if (r < 0)
return r;
break;
OPTION_LONG("load-credential", "ID:PATH",
"Load credential for the VM from file or AF_UNIX stream socket"):
r = machine_credential_load(&arg_credentials, opts.arg);
if (r < 0)
return r;
break;
OPTION_COMMON_INTROSPECT_CLI:
return introspect_cli(SD_JSON_FORMAT_OFF);
}
/* Drop duplicate --bind-user= and --bind-user-group= entries */
strv_uniq(arg_bind_user);
strv_uniq(arg_bind_user_groups);
if (arg_bind_user_shell && strv_isempty(arg_bind_user))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --bind-user-shell= without --bind-user=");
if (!strv_isempty(arg_bind_user_groups) && strv_isempty(arg_bind_user))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --bind-user-group= without --bind-user=");
if (arg_ram_max > 0 && arg_ram_max < arg_ram)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Maximum RAM size must be greater than or equal to initial RAM size");
if (arg_ram_slots > 0 && arg_ram_max == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Memory hotplug slots require a maximum RAM size");
if ((arg_forward_journal_max_use != UINT64_MAX ||
arg_forward_journal_keep_free != UINT64_MAX ||
arg_forward_journal_max_file_size != UINT64_MAX ||
arg_forward_journal_max_files != UINT64_MAX) && !arg_forward_journal)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--forward-journal-max-use=/--forward-journal-keep-free=/--forward-journal-max-file-size=/--forward-journal-max-files= require --forward-journal=.");
if (arg_ephemeral && arg_extra_drives.n_drives > 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use --ephemeral with --extra-drive=");
if (arg_uid_shift != UID_INVALID && !arg_directory)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--private-users= is only supported in combination with --directory=.");
if (arg_directory && arg_uid_shift == UID_INVALID) {
struct stat st;
if (stat(arg_directory, &st) < 0)
return log_error_errno(errno, "Failed to stat '%s': %m", arg_directory);
r = stat_verify_directory(&st);
if (r < 0)
return log_error_errno(r, "'%s' is not a directory: %m", arg_directory);
arg_uid_shift = st.st_uid;
arg_uid_range = 0x10000;
}
char **args = option_parser_get_args(&opts);
if (!strv_isempty(args)) {
arg_kernel_cmdline_extra = strv_copy(args);
if (!arg_kernel_cmdline_extra)
return log_oom();
}
return 1;
}
static int open_vsock(void) {
static const union sockaddr_union bind_addr = {
.vm.svm_family = AF_VSOCK,
.vm.svm_cid = VMADDR_CID_ANY,
.vm.svm_port = VMADDR_PORT_ANY,
};
_cleanup_close_ int vsock_fd = socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (vsock_fd < 0)
return log_error_errno(errno, "Failed to open AF_VSOCK socket: %m");
if (bind(vsock_fd, &bind_addr.sa, sizeof(bind_addr.vm)) < 0)
return log_error_errno(errno, "Failed to bind to VSOCK address %u:%u: %m", bind_addr.vm.svm_cid, bind_addr.vm.svm_port);
if (listen(vsock_fd, SOMAXCONN_DELUXE) < 0)
return log_error_errno(errno, "Failed to listen on VSOCK: %m");
return TAKE_FD(vsock_fd);
}
typedef struct NotifyConnectionData {
char buffer[NOTIFY_BUFFER_MAX+1];
size_t full;
int *exit_status;
} NotifyConnectionData;
static int read_vsock_notify(NotifyConnectionData *d, int fd) {