1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

bhyve: improve bhyve_command.c testability

* bhyve_command.c (bhyveBuildNetArgStr, virBhyveProcessBuildBhyveCmd):
  add dryRun mode which doesn't create any devices when enabled
* bhyve_command.c (virBhyveProcessBuildBhyveCmd,
  virBhyveProcessBuildDestroyCmd, virBhyveProcessBuildLoadCmd): accept
  virDomainDefPtr instead of virDomainObjPtr.
This commit is contained in:
Roman Bogorodskiy 2014-04-13 13:27:03 +04:00
parent a65871aaae
commit 07e371fc36
4 changed files with 57 additions and 48 deletions

View File

@ -39,7 +39,7 @@
VIR_LOG_INIT("bhyve.bhyve_command"); VIR_LOG_INIT("bhyve.bhyve_command");
static int static int
bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd) bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd, bool dryRun)
{ {
virDomainNetDefPtr net = NULL; virDomainNetDefPtr net = NULL;
char *brname = NULL; char *brname = NULL;
@ -78,35 +78,42 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd)
} }
} }
if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac, if (!dryRun)
def->uuid, tapfd, 1, if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
virDomainNetGetActualVirtPortProfile(net), def->uuid, tapfd, 1,
virDomainNetGetActualVlan(net), virDomainNetGetActualVirtPortProfile(net),
VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) < 0) { virDomainNetGetActualVlan(net),
VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
VIR_FREE(net->ifname);
VIR_FREE(brname);
return -1;
}
}
if (!dryRun) {
realifname = virNetDevTapGetRealDeviceName(net->ifname);
if (realifname == NULL) {
VIR_FREE(net->ifname); VIR_FREE(net->ifname);
VIR_FREE(brname); VIR_FREE(brname);
return -1; return -1;
} }
VIR_DEBUG("%s -> %s", net->ifname, realifname);
/* hack on top of other hack: we need to set
* interface to 'UP' again after re-opening to find its
* name
*/
if (virNetDevSetOnline(net->ifname, true) != 0) {
VIR_FREE(net->ifname);
VIR_FREE(brname);
return -1;
}
} else {
if (VIR_STRDUP(realifname, "tap0") < 0)
return -1;
} }
realifname = virNetDevTapGetRealDeviceName(net->ifname);
if (realifname == NULL) {
VIR_FREE(net->ifname);
VIR_FREE(brname);
return -1;
}
VIR_DEBUG("%s -> %s", net->ifname, realifname);
/* hack on top of other hack: we need to set
* interface to 'UP' again after re-opening to find its
* name
*/
if (virNetDevSetOnline(net->ifname, true) != 0) {
VIR_FREE(net->ifname);
VIR_FREE(brname);
return -1;
}
virCommandAddArg(cmd, "-s"); virCommandAddArg(cmd, "-s");
virCommandAddArgFormat(cmd, "1:0,virtio-net,%s,mac=%s", virCommandAddArgFormat(cmd, "1:0,virtio-net,%s,mac=%s",
@ -195,7 +202,7 @@ bhyveBuildDiskArgStr(const virDomainDef *def, virCommandPtr cmd)
virCommandPtr virCommandPtr
virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED, virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
virDomainObjPtr vm) virDomainDefPtr def, bool dryRun)
{ {
/* /*
* /usr/sbin/bhyve -c 2 -m 256 -AI -H -P \ * /usr/sbin/bhyve -c 2 -m 256 -AI -H -P \
@ -209,17 +216,17 @@ virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
/* CPUs */ /* CPUs */
virCommandAddArg(cmd, "-c"); virCommandAddArg(cmd, "-c");
virCommandAddArgFormat(cmd, "%d", vm->def->vcpus); virCommandAddArgFormat(cmd, "%d", def->vcpus);
/* Memory */ /* Memory */
virCommandAddArg(cmd, "-m"); virCommandAddArg(cmd, "-m");
virCommandAddArgFormat(cmd, "%llu", virCommandAddArgFormat(cmd, "%llu",
VIR_DIV_UP(vm->def->mem.max_balloon, 1024)); VIR_DIV_UP(def->mem.max_balloon, 1024));
/* Options */ /* Options */
if (vm->def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_DOMAIN_FEATURE_STATE_ON) if (def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_DOMAIN_FEATURE_STATE_ON)
virCommandAddArg(cmd, "-A"); /* Create an ACPI table */ virCommandAddArg(cmd, "-A"); /* Create an ACPI table */
if (vm->def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_DOMAIN_FEATURE_STATE_ON) if (def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_DOMAIN_FEATURE_STATE_ON)
virCommandAddArg(cmd, "-I"); /* Present ioapic to the guest */ virCommandAddArg(cmd, "-I"); /* Present ioapic to the guest */
/* Clarification about -H and -P flags from Peter Grehan: /* Clarification about -H and -P flags from Peter Grehan:
@ -238,13 +245,13 @@ virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
virCommandAddArgList(cmd, "-s", "0:0,hostbridge", NULL); virCommandAddArgList(cmd, "-s", "0:0,hostbridge", NULL);
/* Devices */ /* Devices */
if (bhyveBuildNetArgStr(vm->def, cmd) < 0) if (bhyveBuildNetArgStr(def, cmd, dryRun) < 0)
goto error; goto error;
if (bhyveBuildDiskArgStr(vm->def, cmd) < 0) if (bhyveBuildDiskArgStr(def, cmd) < 0)
goto error; goto error;
if (bhyveBuildConsoleArgStr(vm->def, cmd) < 0) if (bhyveBuildConsoleArgStr(def, cmd) < 0)
goto error; goto error;
virCommandAddArg(cmd, vm->def->name); virCommandAddArg(cmd, def->name);
return cmd; return cmd;
@ -255,30 +262,30 @@ virBhyveProcessBuildBhyveCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
virCommandPtr virCommandPtr
virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED, virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
virDomainObjPtr vm) virDomainDefPtr def)
{ {
virCommandPtr cmd = virCommandNew(BHYVECTL); virCommandPtr cmd = virCommandNew(BHYVECTL);
virCommandAddArg(cmd, "--destroy"); virCommandAddArg(cmd, "--destroy");
virCommandAddArgPair(cmd, "--vm", vm->def->name); virCommandAddArgPair(cmd, "--vm", def->name);
return cmd; return cmd;
} }
virCommandPtr virCommandPtr
virBhyveProcessBuildLoadCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED, virBhyveProcessBuildLoadCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
virDomainObjPtr vm) virDomainDefPtr def)
{ {
virCommandPtr cmd; virCommandPtr cmd;
virDomainDiskDefPtr disk; virDomainDiskDefPtr disk;
if (vm->def->ndisks != 1) { if (def->ndisks != 1) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("domain should have one and only one disk defined")); _("domain should have one and only one disk defined"));
return NULL; return NULL;
} }
disk = vm->def->disks[0]; disk = def->disks[0];
if (disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) { if (disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
@ -297,14 +304,14 @@ virBhyveProcessBuildLoadCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
/* Memory */ /* Memory */
virCommandAddArg(cmd, "-m"); virCommandAddArg(cmd, "-m");
virCommandAddArgFormat(cmd, "%llu", virCommandAddArgFormat(cmd, "%llu",
VIR_DIV_UP(vm->def->mem.max_balloon, 1024)); VIR_DIV_UP(def->mem.max_balloon, 1024));
/* Image path */ /* Image path */
virCommandAddArg(cmd, "-d"); virCommandAddArg(cmd, "-d");
virCommandAddArg(cmd, virDomainDiskGetSource(disk)); virCommandAddArg(cmd, virDomainDiskGetSource(disk));
/* VM name */ /* VM name */
virCommandAddArg(cmd, vm->def->name); virCommandAddArg(cmd, def->name);
return cmd; return cmd;
} }

View File

@ -28,14 +28,14 @@
# include "vircommand.h" # include "vircommand.h"
virCommandPtr virBhyveProcessBuildBhyveCmd(bhyveConnPtr, virCommandPtr virBhyveProcessBuildBhyveCmd(bhyveConnPtr,
virDomainObjPtr vm); virDomainDefPtr def, bool dryRun);
virCommandPtr virCommandPtr
virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver, virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver,
virDomainObjPtr vm); virDomainDefPtr def);
virCommandPtr virCommandPtr
virBhyveProcessBuildLoadCmd(bhyveConnPtr driver, virBhyveProcessBuildLoadCmd(bhyveConnPtr driver,
virDomainObjPtr vm); virDomainDefPtr def);
#endif /* __BHYVE_COMMAND_H__ */ #endif /* __BHYVE_COMMAND_H__ */

View File

@ -128,7 +128,8 @@ virBhyveProcessStart(virConnectPtr conn,
/* Call bhyve to start the VM */ /* Call bhyve to start the VM */
if (!(cmd = virBhyveProcessBuildBhyveCmd(driver, if (!(cmd = virBhyveProcessBuildBhyveCmd(driver,
vm))) vm->def,
false)))
goto cleanup; goto cleanup;
virCommandSetOutputFD(cmd, &logfd); virCommandSetOutputFD(cmd, &logfd);
@ -140,7 +141,7 @@ virBhyveProcessStart(virConnectPtr conn,
/* Now bhyve command is constructed, meaning the /* Now bhyve command is constructed, meaning the
* domain is ready to be started, so we can build * domain is ready to be started, so we can build
* and execute bhyveload command */ * and execute bhyveload command */
if (!(load_cmd = virBhyveProcessBuildLoadCmd(driver, vm))) if (!(load_cmd = virBhyveProcessBuildLoadCmd(driver, vm->def)))
goto cleanup; goto cleanup;
virCommandSetOutputFD(load_cmd, &logfd); virCommandSetOutputFD(load_cmd, &logfd);
virCommandSetErrorFD(load_cmd, &logfd); virCommandSetErrorFD(load_cmd, &logfd);
@ -179,7 +180,8 @@ virBhyveProcessStart(virConnectPtr conn,
cleanup: cleanup:
if (ret < 0) { if (ret < 0) {
virCommandPtr destroy_cmd; virCommandPtr destroy_cmd;
if ((destroy_cmd = virBhyveProcessBuildDestroyCmd(driver, vm)) != NULL) { if ((destroy_cmd = virBhyveProcessBuildDestroyCmd(driver,
vm->def)) != NULL) {
virCommandSetOutputFD(load_cmd, &logfd); virCommandSetOutputFD(load_cmd, &logfd);
virCommandSetErrorFD(load_cmd, &logfd); virCommandSetErrorFD(load_cmd, &logfd);
ignore_value(virCommandRun(destroy_cmd, NULL)); ignore_value(virCommandRun(destroy_cmd, NULL));
@ -227,7 +229,7 @@ virBhyveProcessStop(bhyveConnPtr driver,
/* No matter if shutdown was successful or not, we /* No matter if shutdown was successful or not, we
* need to unload the VM */ * need to unload the VM */
if (!(cmd = virBhyveProcessBuildDestroyCmd(driver, vm))) if (!(cmd = virBhyveProcessBuildDestroyCmd(driver, vm->def)))
goto cleanup; goto cleanup;
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)

View File

@ -33,7 +33,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
vm.def = vmdef; vm.def = vmdef;
if (!(cmd = virBhyveProcessBuildBhyveCmd(&driver, &vm))) if (!(cmd = virBhyveProcessBuildBhyveCmd(&driver, vmdef, false)))
goto out; goto out;
if (!(actualargv = virCommandToString(cmd))) if (!(actualargv = virCommandToString(cmd)))