qemu: command: Refactor memballoon command line formatting

Now that there is just one format of the memory balloon command line
used the code can be merged into a single function.

Additionally with some tweaks to the control flow the code is easier to
read.
This commit is contained in:
Peter Krempa 2016-04-06 13:41:31 +02:00
parent 388b356e5d
commit 33b9598c41
2 changed files with 29 additions and 51 deletions

View File

@ -3470,14 +3470,29 @@ qemuBuildWatchdogCommandLine(virCommandPtr cmd,
} }
char * static int
qemuBuildMemballoonDevStr(const virDomainDef *def, qemuBuildMemballoonCommandLine(virCommandPtr cmd,
virDomainMemballoonDefPtr dev, const virDomainDef *def,
virQEMUCapsPtr qemuCaps) virQEMUCapsPtr qemuCaps)
{ {
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
switch (dev->info.type) { if (STREQLEN(def->os.machine, "s390-virtio", 10) &&
virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_S390) && def->memballoon)
def->memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_NONE;
if (!def->memballoon ||
def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_NONE)
return 0;
if (def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Memory balloon device type '%s' is not supported by this version of qemu"),
virDomainMemballoonModelTypeToString(def->memballoon->model));
return -1;
}
switch (def->memballoon->info.type) {
case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI: case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI:
virBufferAddLit(&buf, "virtio-balloon-pci"); virBufferAddLit(&buf, "virtio-balloon-pci");
break; break;
@ -3490,15 +3505,15 @@ qemuBuildMemballoonDevStr(const virDomainDef *def,
default: default:
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("memballoon unsupported with address type '%s'"), _("memballoon unsupported with address type '%s'"),
virDomainDeviceAddressTypeToString(dev->info.type)); virDomainDeviceAddressTypeToString(def->memballoon->info.type));
goto error; goto error;
} }
virBufferAsprintf(&buf, ",id=%s", dev->info.alias); virBufferAsprintf(&buf, ",id=%s", def->memballoon->info.alias);
if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0) if (qemuBuildDeviceAddressStr(&buf, def, &def->memballoon->info, qemuCaps) < 0)
goto error; goto error;
if (dev->autodeflate != VIR_TRISTATE_SWITCH_ABSENT) { if (def->memballoon->autodeflate != VIR_TRISTATE_SWITCH_ABSENT) {
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BALLOON_AUTODEFLATE)) { if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BALLOON_AUTODEFLATE)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("deflate-on-oom is not supported by this QEMU binary")); _("deflate-on-oom is not supported by this QEMU binary"));
@ -3506,49 +3521,16 @@ qemuBuildMemballoonDevStr(const virDomainDef *def,
} }
virBufferAsprintf(&buf, ",deflate-on-oom=%s", virBufferAsprintf(&buf, ",deflate-on-oom=%s",
virTristateSwitchTypeToString(dev->autodeflate)); virTristateSwitchTypeToString(def->memballoon->autodeflate));
} }
if (virBufferCheckError(&buf) < 0) virCommandAddArg(cmd, "-device");
goto error; virCommandAddArgBuffer(cmd, &buf);
return 0;
return virBufferContentAndReset(&buf);
error: error:
virBufferFreeAndReset(&buf); virBufferFreeAndReset(&buf);
return NULL; return -1;
}
static int
qemuBuildMemballoonCommandLine(virCommandPtr cmd,
const virDomainDef *def,
virQEMUCapsPtr qemuCaps)
{
char *optstr;
if (STREQLEN(def->os.machine, "s390-virtio", 10) &&
virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_S390) && def->memballoon)
def->memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_NONE;
if (def->memballoon &&
def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_NONE) {
if (def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Memory balloon device type '%s' is not supported by this version of qemu"),
virDomainMemballoonModelTypeToString(def->memballoon->model));
return -1;
}
virCommandAddArg(cmd, "-device");
optstr = qemuBuildMemballoonDevStr(def, def->memballoon, qemuCaps);
if (!optstr)
return -1;
virCommandAddArg(cmd, optstr);
VIR_FREE(optstr);
}
return 0;
} }

View File

@ -117,10 +117,6 @@ char *qemuBuildControllerDevStr(const virDomainDef *domainDef,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
int *nusbcontroller); int *nusbcontroller);
char *qemuBuildMemballoonDevStr(const virDomainDef *domainDef,
virDomainMemballoonDefPtr dev,
virQEMUCapsPtr qemuCaps);
int qemuBuildMemoryBackendStr(unsigned long long size, int qemuBuildMemoryBackendStr(unsigned long long size,
unsigned long long pagesize, unsigned long long pagesize,
int guestNode, int guestNode,