Skip to content

Commit

Permalink
vmspawn: rename "qemu" specific switches to not carry the "qemu" prefix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
poettering committed Feb 21, 2024
1 parent cf9de8e commit 14b7464
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 63 deletions.
12 changes: 6 additions & 6 deletions man/systemd-vmspawn.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@

<variablelist>
<varlistentry>
<term><option>--qemu-smp=</option><replaceable>SMP</replaceable></term>
<term><option>--cpus=</option><replaceable>CPUS</replaceable></term>

<listitem><para>Configures the number of CPUs to start the virtual machine with.
Defaults to 1.</para>
Expand All @@ -114,7 +114,7 @@
</varlistentry>

<varlistentry>
<term><option>--qemu-mem=</option><replaceable>MEM</replaceable></term>
<term><option>--ram=</option><replaceable>BYTES</replaceable></term>

<listitem><para>Configures the amount of memory to start the virtual machine with.
Defaults to 2G.</para>
Expand All @@ -124,7 +124,7 @@
</varlistentry>

<varlistentry>
<term><option>--qemu-kvm=</option><replaceable>BOOL</replaceable></term>
<term><option>--kvm=</option><replaceable>BOOL</replaceable></term>

<listitem><para>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.</para>
Expand All @@ -133,7 +133,7 @@
</varlistentry>

<varlistentry>
<term><option>--qemu-vsock=</option><replaceable>BOOL</replaceable></term>
<term><option>--vsock=</option><replaceable>BOOL</replaceable></term>

<listitem>
<para>Configure whether to use VSOCK networking.</para>
Expand Down Expand Up @@ -163,7 +163,7 @@
<term><option>--tpm=</option><replaceable>BOOL</replaceable></term>

<listitem>
<para>Configure whether to use qemu with a virtual TPM or not.</para>
<para>Configure whether to use VM with a virtual TPM or not.</para>

<para>If the option is not specified vmspawn will detect the presence of <citerefentry project='man-pages'>
<refentrytitle>swtpm</refentrytitle><manvolnum>8</manvolnum></citerefentry> and use it if available.
Expand Down Expand Up @@ -235,7 +235,7 @@
<varlistentry>
<term><option>--network-user-mode</option></term>

<listitem><para>Use user mode networking with QEMU.</para>
<listitem><para>Use user mode networking.</para>

<xi:include href="version-info.xml" xpointer="v255"/></listitem>
</varlistentry>
Expand Down
2 changes: 1 addition & 1 deletion src/vmspawn/vmspawn-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions src/vmspawn/vmspawn-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
90 changes: 47 additions & 43 deletions src/vmspawn/vmspawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 guest's SMP settings\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"
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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"
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 14b7464

Please sign in to comment.