提交 07e371fc 编写于 作者: R Roman Bogorodskiy

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.
上级 a65871aa
...@@ -39,7 +39,7 @@ ...@@ -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,6 +78,7 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd) ...@@ -78,6 +78,7 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd)
} }
} }
if (!dryRun)
if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac, if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
def->uuid, tapfd, 1, def->uuid, tapfd, 1,
virDomainNetGetActualVirtPortProfile(net), virDomainNetGetActualVirtPortProfile(net),
...@@ -89,6 +90,7 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd) ...@@ -89,6 +90,7 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd)
} }
} }
if (!dryRun) {
realifname = virNetDevTapGetRealDeviceName(net->ifname); realifname = virNetDevTapGetRealDeviceName(net->ifname);
if (realifname == NULL) { if (realifname == NULL) {
...@@ -107,6 +109,11 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd) ...@@ -107,6 +109,11 @@ bhyveBuildNetArgStr(const virDomainDef *def, virCommandPtr cmd)
VIR_FREE(brname); VIR_FREE(brname);
return -1; return -1;
} }
} else {
if (VIR_STRDUP(realifname, "tap0") < 0)
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) ...@@ -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, ...@@ -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, ...@@ -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, ...@@ -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, ...@@ -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;
} }
...@@ -28,14 +28,14 @@ ...@@ -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__ */
...@@ -128,7 +128,8 @@ virBhyveProcessStart(virConnectPtr conn, ...@@ -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, ...@@ -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, ...@@ -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, ...@@ -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)
......
...@@ -33,7 +33,7 @@ static int testCompareXMLToArgvFiles(const char *xml, ...@@ -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)))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册