From cf9de8ef213a1cb154233715d8e0e6fe68f0a9ad Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 21 Feb 2024 16:18:24 +0100 Subject: [PATCH 1/2] vspawn: prefer "VSOCK" spelling over "vsock" and "VSock" The vsock(7) man page appears to prefer "VSOCK" spelling for the concept, let's also use it as much as we can. --- man/systemd-vmspawn.xml | 9 +++++---- src/vmspawn/vmspawn.c | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/man/systemd-vmspawn.xml b/man/systemd-vmspawn.xml index b618999b8ea..c11809bc5a6 100644 --- a/man/systemd-vmspawn.xml +++ b/man/systemd-vmspawn.xml @@ -42,7 +42,8 @@ file descriptors must be passed with the names kvm and vhost-vsock respectively. - Note: on Ubuntu/Debian derivatives systemd-vmspawn requires the user to be in the kvm group to use the VSock options. + Note: on Ubuntu/Debian derivatives systemd-vmspawn requires the user to be in the + kvm group to use the VSOCK options. @@ -135,10 +136,10 @@ BOOL - Configure whether to use VSock networking. + Configure whether to use VSOCK networking. - If the option is not specified VSock support will be detected automatically. - If yes is specified VSocks are always used, and vice versa if no is set VSocks are never used. + If the option is not specified VSOCK support will be detected automatically. If yes is + specified VSOCK is always used, and vice versa if no is set VSOCK are never used. diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index dd72f240024..925b56abfe8 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -467,11 +467,11 @@ static int open_vsock(void) { r = bind(vsock_fd, &bind_addr.sa, sizeof(bind_addr.vm)); if (r < 0) - return log_error_errno(errno, "Failed to bind to vsock to address %u:%u: %m", bind_addr.vm.svm_cid, bind_addr.vm.svm_port); + return log_error_errno(errno, "Failed to bind to VSOCK address %u:%u: %m", bind_addr.vm.svm_cid, bind_addr.vm.svm_port); r = listen(vsock_fd, SOMAXCONN_DELUXE); if (r < 0) - return log_error_errno(errno, "Failed to listen on vsock: %m"); + return log_error_errno(errno, "Failed to listen on VSOCK: %m"); return TAKE_FD(vsock_fd); } @@ -545,13 +545,13 @@ static int vmspawn_dispatch_vsock_connections(sd_event_source *source, int fd, u assert(userdata); if (revents != EPOLLIN) { - log_warning("Got unexpected poll event for vsock fd."); + log_warning("Got unexpected poll event for VSOCK fd."); return 0; } conn_fd = accept4(fd, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); if (conn_fd < 0) { - log_warning_errno(errno, "Failed to accept connection from vsock fd (%m), ignoring..."); + log_warning_errno(errno, "Failed to accept connection from VSOCK fd (%m), ignoring..."); return 0; } @@ -1134,7 +1134,7 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { if (arg_qemu_vsock < 0) { r = qemu_check_vsock_support(); if (r < 0) - return log_error_errno(r, "Failed to check for VSock support: %m"); + return log_error_errno(r, "Failed to check for VSOCK support: %m"); use_vsock = r; } @@ -1185,7 +1185,7 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { r = vsock_fix_child_cid(device_fd, &child_cid, arg_machine); if (r < 0) - return log_error_errno(r, "Failed to fix CID for the guest vsock socket: %m"); + return log_error_errno(r, "Failed to fix CID for the guest VSOCK socket: %m"); r = strv_extend(&cmdline, "-device"); if (r < 0) @@ -1549,13 +1549,13 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { 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"); + return log_error_errno(notify_sock_fd, "Failed to open VSOCK: %m"); r = cmdline_add_vsock(&cmdline, notify_sock_fd); if (r == -ENOMEM) return log_oom(); if (r < 0) - return log_error_errno(r, "Failed to call getsockname on vsock: %m"); + return log_error_errno(r, "Failed to call getsockname on VSOCK: %m"); } if (DEBUG_LOGGING) { @@ -1606,7 +1606,7 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { if (use_vsock) { r = setup_notify_parent(event, notify_sock_fd, &exit_status, ¬ify_event_source); if (r < 0) - return log_error_errno(r, "Failed to setup event loop to handle vsock notify events: %m"); + return log_error_errno(r, "Failed to setup event loop to handle VSOCK notify events: %m"); } /* shutdown qemu when we are shutdown */ @@ -1624,7 +1624,7 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { if (use_vsock) { if (exit_status == INT_MAX) { - log_debug("Couldn't retrieve inner EXIT_STATUS from vsock"); + log_debug("Couldn't retrieve inner EXIT_STATUS from VSOCK"); return EXIT_SUCCESS; } if (exit_status != 0) From 2c0061c78477b5042fa328b3d088448152077123 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 21 Feb 2024 16:23:55 +0100 Subject: [PATCH 2/2] vmspawn: rename "qemu" specific switches to not carry the "qemu" prefix This renames a few of the switches vmspawn takes, such as --qemu-mem= and --qemu-smp= to names without the "qemu" moniker and uses less cryptic names (i.e. --ram= and --cpus=). I think it's a bit unsystematic that so far we use the "qemu" prefix for some options but not for others. At least I could not figure out a system when we use it and when we don't. Hence let's clean it up and just use simpler names without suffix. After all we might want to plug other hypervisors behind vmspawn one day, hence I think there's value in sticking to generic names for these switches that allow us to switch out backends easily. In particular for --ram= and --cpus= which are probably the most fundamental of VM settings there are. The old switches are support for compat, but not advertised in man page or --help text anymore. I left "--qemu-gui" under its current name, since it fundamentally is a a qemu concept, exposing a qemu specific graphical UI. --- man/systemd-vmspawn.xml | 12 ++--- src/vmspawn/vmspawn-util.c | 2 +- src/vmspawn/vmspawn-util.h | 26 +++++------ src/vmspawn/vmspawn.c | 90 ++++++++++++++++++++------------------ 4 files changed, 67 insertions(+), 63 deletions(-) diff --git a/man/systemd-vmspawn.xml b/man/systemd-vmspawn.xml index c11809bc5a6..ed4dfc8bfab 100644 --- a/man/systemd-vmspawn.xml +++ b/man/systemd-vmspawn.xml @@ -104,7 +104,7 @@ - SMP + CPUS Configures the number of CPUs to start the virtual machine with. Defaults to 1. @@ -114,7 +114,7 @@ - MEM + BYTES Configures the amount of memory to start the virtual machine with. Defaults to 2G. @@ -124,7 +124,7 @@ - BOOL + BOOL Configures whether to use KVM. If the option is not specified KVM support will be detected automatically. If true, KVM is always used, and if false, KVM is never used. @@ -133,7 +133,7 @@ - BOOL + BOOL Configure whether to use VSOCK networking. @@ -163,7 +163,7 @@ BOOL - Configure whether to use qemu with a virtual TPM or not. + Configure whether to use VM with a virtual TPM or not. If the option is not specified vmspawn will detect the presence of swtpm8 and use it if available. @@ -235,7 +235,7 @@ - Use user mode networking with QEMU. + Use user mode networking. diff --git a/src/vmspawn/vmspawn-util.c b/src/vmspawn/vmspawn-util.c index 9ebaa9a2236..a98040a213a 100644 --- a/src/vmspawn/vmspawn-util.c +++ b/src/vmspawn/vmspawn-util.c @@ -37,7 +37,7 @@ OvmfConfig* ovmf_config_free(OvmfConfig *config) { return mfree(config); } -DEFINE_STRING_TABLE_LOOKUP(qemu_network_stack, QemuNetworkStack); +DEFINE_STRING_TABLE_LOOKUP(network_stack, NetworkStack); int qemu_check_kvm_support(void) { if (access("/dev/kvm", F_OK) >= 0) diff --git a/src/vmspawn/vmspawn-util.h b/src/vmspawn/vmspawn-util.h index 1e9da8530cf..cd1f91d0329 100644 --- a/src/vmspawn/vmspawn-util.h +++ b/src/vmspawn/vmspawn-util.h @@ -63,22 +63,22 @@ static inline const char *ovmf_config_vars_format(const OvmfConfig *c) { OvmfConfig* ovmf_config_free(OvmfConfig *ovmf_config); DEFINE_TRIVIAL_CLEANUP_FUNC(OvmfConfig*, ovmf_config_free); -typedef enum QemuNetworkStack { - QEMU_NET_TAP, - QEMU_NET_USER, - QEMU_NET_NONE, - _QEMU_NET_MAX, - _QEMU_NET_INVALID = -EINVAL, -} QemuNetworkStack; +typedef enum NetworkStack { + NETWORK_STACK_TAP, + NETWORK_STACK_USER, + NETWORK_STACK_NONE, + _NETWORK_STACK_MAX, + _NETWORK_STACK_INVALID = -EINVAL, +} NetworkStack; -static const char* const qemu_network_stack_table[_QEMU_NET_MAX] = { - [QEMU_NET_TAP] = "tap", - [QEMU_NET_USER] = "user", - [QEMU_NET_NONE] = "none", +static const char* const network_stack_table[_NETWORK_STACK_MAX] = { + [NETWORK_STACK_TAP] = "tap", + [NETWORK_STACK_USER] = "user", + [NETWORK_STACK_NONE] = "none", }; -const char* qemu_network_stack_to_string(QemuNetworkStack type) _const_; -QemuNetworkStack qemu_network_stack_from_string(const char *s) _pure_; +const char* network_stack_to_string(NetworkStack type) _const_; +NetworkStack network_stack_from_string(const char *s) _pure_; int qemu_check_kvm_support(void); int qemu_check_vsock_support(void); diff --git a/src/vmspawn/vmspawn.c b/src/vmspawn/vmspawn.c index 925b56abfe8..f67c6a51bcc 100644 --- a/src/vmspawn/vmspawn.c +++ b/src/vmspawn/vmspawn.c @@ -65,16 +65,16 @@ static PagerFlags arg_pager_flags = 0; static char *arg_directory = NULL; static char *arg_image = NULL; static char *arg_machine = NULL; -static char *arg_qemu_smp = NULL; -static uint64_t arg_qemu_mem = UINT64_C(2) * U64_GB; -static int arg_qemu_kvm = -1; -static int arg_qemu_vsock = -1; +static char *arg_cpus = NULL; +static uint64_t arg_ram = UINT64_C(2) * U64_GB; +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 char **arg_initrds = NULL; static bool arg_qemu_gui = false; -static QemuNetworkStack arg_network_stack = QEMU_NET_NONE; +static NetworkStack arg_network_stack = NETWORK_STACK_NONE; static int arg_secure_boot = -1; static MachineCredentialContext arg_credentials = {}; static uid_t arg_uid_shift = UID_INVALID, arg_uid_range = 0x10000U; @@ -91,7 +91,7 @@ static char **arg_extra_drives = NULL; STATIC_DESTRUCTOR_REGISTER(arg_directory, freep); STATIC_DESTRUCTOR_REGISTER(arg_image, freep); STATIC_DESTRUCTOR_REGISTER(arg_machine, freep); -STATIC_DESTRUCTOR_REGISTER(arg_qemu_smp, freep); +STATIC_DESTRUCTOR_REGISTER(arg_cpus, freep); STATIC_DESTRUCTOR_REGISTER(arg_runtime_directory, freep); STATIC_DESTRUCTOR_REGISTER(arg_credentials, machine_credential_context_done); STATIC_DESTRUCTOR_REGISTER(arg_firmware, freep); @@ -122,17 +122,17 @@ static int help(void) { " -D --directory=PATH Root directory for the VM\n" " -i --image=FILE|DEVICE Root file system disk image or device for the VM\n" "\n%3$sHost Configuration:%4$s\n" - " --qemu-smp=SMP Configure guest's SMP settings\n" - " --qemu-mem=MEM Configure guest's RAM size\n" - " --qemu-kvm=BOOL Enable use of KVM\n" - " --qemu-vsock=BOOL Override autodetection of VSock support in QEMU\n" - " --vsock-cid=CID Specify the CID to use for the qemu guest's VSock\n" + " --cpus=CPUS Configure number of CPUs in guest\n" + " --ram=BYTES Configure guest's RAM size\n" + " --kvm=BOOL Enable use of KVM\n" + " --vsock=BOOL Override autodetection of VSOCK support\n" + " --vsock-cid=CID Specify the CID to use for the guest's VSOCK support\n" " --tpm=BOOL Enable use of a virtual TPM\n" " --linux=PATH Specify the linux kernel for direct kernel boot\n" " --initrd=PATH Specify the initrd for direct kernel boot\n" " --qemu-gui Start QEMU in graphical mode\n" - " -n --network-tap Create a TAP device for networking with QEMU\n" - " --network-user-mode Use user mode networking with QEMU\n" + " -n --network-tap Create a TAP device for networking\n" + " --network-user-mode Use user mode networking\n" " --secure-boot=BOOL Enable searching for firmware supporting SecureBoot\n" " --firmware=PATH|list Select firmware definition file (or list available)\n" "\n%3$sSystem Identity:%4$s\n" @@ -171,10 +171,10 @@ static int parse_argv(int argc, char *argv[]) { enum { ARG_VERSION = 0x100, ARG_NO_PAGER, - ARG_QEMU_SMP, - ARG_QEMU_MEM, - ARG_QEMU_KVM, - ARG_QEMU_VSOCK, + ARG_CPUS, + ARG_RAM, + ARG_KVM, + ARG_VSOCK, ARG_VSOCK_CID, ARG_TPM, ARG_LINUX, @@ -200,10 +200,14 @@ static int parse_argv(int argc, char *argv[]) { { "image", required_argument, NULL, 'i' }, { "directory", required_argument, NULL, 'D' }, { "machine", required_argument, NULL, 'M' }, - { "qemu-smp", required_argument, NULL, ARG_QEMU_SMP }, - { "qemu-mem", required_argument, NULL, ARG_QEMU_MEM }, - { "qemu-kvm", required_argument, NULL, ARG_QEMU_KVM }, - { "qemu-vsock", required_argument, NULL, ARG_QEMU_VSOCK }, + { "cpus", required_argument, NULL, ARG_CPUS }, + { "qemu-smp", required_argument, NULL, ARG_CPUS }, /* Compat alias */ + { "ram", required_argument, NULL, ARG_RAM }, + { "qemu-mem", required_argument, NULL, ARG_RAM }, /* Compat alias */ + { "kvm", required_argument, NULL, ARG_KVM }, + { "qemu-kvm", required_argument, NULL, ARG_KVM }, /* Compat alias */ + { "vsock", required_argument, NULL, ARG_VSOCK }, + { "qemu-vsock", required_argument, NULL, ARG_VSOCK }, /* Compat alias */ { "vsock-cid", required_argument, NULL, ARG_VSOCK_CID }, { "tpm", required_argument, NULL, ARG_TPM }, { "linux", required_argument, NULL, ARG_LINUX }, @@ -275,28 +279,28 @@ static int parse_argv(int argc, char *argv[]) { arg_pager_flags |= PAGER_DISABLE; break; - case ARG_QEMU_SMP: - r = free_and_strdup_warn(&arg_qemu_smp, optarg); + case ARG_CPUS: + r = free_and_strdup_warn(&arg_cpus, optarg); if (r < 0) return r; break; - case ARG_QEMU_MEM: - r = parse_size(optarg, 1024, &arg_qemu_mem); + case ARG_RAM: + r = parse_size(optarg, 1024, &arg_ram); if (r < 0) - return log_error_errno(r, "Failed to parse --qemu-mem=%s: %m", optarg); + return log_error_errno(r, "Failed to parse --ram=%s: %m", optarg); break; - case ARG_QEMU_KVM: - r = parse_tristate(optarg, &arg_qemu_kvm); + case ARG_KVM: + r = parse_tristate(optarg, &arg_kvm); if (r < 0) - return log_error_errno(r, "Failed to parse --qemu-kvm=%s: %m", optarg); + return log_error_errno(r, "Failed to parse --kvm=%s: %m", optarg); break; - case ARG_QEMU_VSOCK: - r = parse_tristate(optarg, &arg_qemu_vsock); + case ARG_VSOCK: + r = parse_tristate(optarg, &arg_vsock); if (r < 0) - return log_error_errno(r, "Failed to parse --qemu-vsock=%s: %m", optarg); + return log_error_errno(r, "Failed to parse --vsock=%s: %m", optarg); break; case ARG_VSOCK_CID: @@ -345,11 +349,11 @@ static int parse_argv(int argc, char *argv[]) { break; case 'n': - arg_network_stack = QEMU_NET_TAP; + arg_network_stack = NETWORK_STACK_TAP; break; case ARG_NETWORK_USER_MODE: - arg_network_stack = QEMU_NET_USER; + arg_network_stack = NETWORK_STACK_USER; break; case ARG_BIND: @@ -1035,8 +1039,8 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { if (r < 0) return r; - bool use_kvm = arg_qemu_kvm > 0; - if (arg_qemu_kvm < 0) { + 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"); @@ -1082,13 +1086,13 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { if (r < 0) return log_error_errno(r, "Failed to find QEMU binary: %m"); - if (asprintf(&mem, "%" PRIu64 "M", DIV_ROUND_UP(arg_qemu_mem, U64_MB)) < 0) + if (asprintf(&mem, "%" PRIu64 "M", DIV_ROUND_UP(arg_ram, U64_MB)) < 0) return log_oom(); cmdline = strv_new( qemu_binary, "-machine", machine, - "-smp", arg_qemu_smp ?: "1", + "-smp", arg_cpus ?: "1", "-m", mem, "-object", "rng-random,filename=/dev/urandom,id=rng0", "-device", "virtio-rng-pci,rng=rng0,id=rng-device0" @@ -1110,9 +1114,9 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { } } - if (arg_network_stack == QEMU_NET_TAP) + if (arg_network_stack == NETWORK_STACK_TAP) r = strv_extend_many(&cmdline, "-nic", "tap,script=no,model=virtio-net-pci"); - else if (arg_network_stack == QEMU_NET_USER) + else if (arg_network_stack == NETWORK_STACK_USER) r = strv_extend_many(&cmdline, "-nic", "user,model=virtio-net-pci"); else r = strv_extend_many(&cmdline, "-nic", "none"); @@ -1130,8 +1134,8 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) { return log_oom(); } - bool use_vsock = arg_qemu_vsock > 0 && ARCHITECTURE_SUPPORTS_SMBIOS; - if (arg_qemu_vsock < 0) { + bool use_vsock = arg_vsock > 0 && ARCHITECTURE_SUPPORTS_SMBIOS; + if (arg_vsock < 0) { r = qemu_check_vsock_support(); if (r < 0) return log_error_errno(r, "Failed to check for VSOCK support: %m"); @@ -1694,7 +1698,7 @@ static int determine_names(void) { } static int verify_arguments(void) { - if (arg_network_stack == QEMU_NET_TAP && !arg_privileged) + if (arg_network_stack == NETWORK_STACK_TAP && !arg_privileged) return log_error_errno(SYNTHETIC_ERRNO(EPERM), "--network-tap requires root privileges, refusing."); if (!strv_isempty(arg_initrds) && !arg_linux)