static int showDomains(virConnectPtr conn) { int ret = 0, numNames, numInactiveDomains, numActiveDomains; ssize_t i; int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE | VIR_CONNECT_LIST_DOMAINS_INACTIVE; virDomainPtr *nameList = NULL; /* NB: The return from the virConnectNum*() APIs is only useful for * the current call. A domain could be started or stopped and any * assumptions made purely on these return values could result in * unexpected results */ numActiveDomains = virConnectNumOfDomains(conn); if (numActiveDomains == -1) { ret = 1; printf("Failed to get number of active domains: %s\n", virGetLastErrorMessage()); goto out; } numInactiveDomains = virConnectNumOfDefinedDomains(conn); if (numInactiveDomains == -1) { ret = 1; printf("Failed to get number of inactive domains: %s\n", virGetLastErrorMessage()); goto out; } printf("There are %d active and %d inactive domains\n", numActiveDomains, numInactiveDomains); /* Return a list of all active and inactive domains. Using this API * instead of virConnectListDomains() and virConnectListDefinedDomains() * is preferred since it "solves" an inherit race between separated API * calls if domains are started or stopped between calls */ numNames = virConnectListAllDomains(conn, &nameList, flags); if (numNames == -1) { ret = 1; printf("Failed to get a list of all domains: %s\n", virGetLastErrorMessage()); goto out; } for (i = 0; i < numNames; i++) { int active = virDomainIsActive(nameList[i]); printf(" %8s (%s)\n", virDomainGetName(nameList[i]), (active == 1 ? "active" : "non-active")); /* must free the returned named per the API documentation */ virDomainFree(nameList[i]); } free(nameList); out: return ret; }
int main(int argc, char *argv[]) { int ret = 0; virConnectPtr conn; char *uri; printf("Attempting to connect to hypervisor\n"); uri = (argc > 0 ? argv[1] : NULL); /* virConnectOpenAuth is called here with all default parameters, * except, possibly, the URI of the hypervisor. */ conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0); if (!conn) { ret = 1; printf("No connection to hypervisor: %s\n", virGetLastErrorMessage()); goto out; } uri = virConnectGetURI(conn); if (!uri) { ret = 1; printf("Failed to get URI for hypervisor connection: %s\n", virGetLastErrorMessage()); goto disconnect; } printf("Connected to hypervisor at \"%s\"\n", uri); free(uri); if (0 != showHypervisorInfo(conn)) { ret = 1; goto disconnect; } if (0 != showDomains(conn)) { ret = 1; goto disconnect; } disconnect: if (0 != virConnectClose(conn)) { printf("Failed to disconnect from hypervisor: %s\n", virGetLastErrorMessage()); ret = 1; } else { printf("Disconnected from hypervisor\n"); } out: return ret; }
static int testLogParseFilters(const void *opaque) { int ret = -1; int nfilters; const struct testLogData *data = opaque; nfilters = virLogParseFilters(data->str); if (nfilters < 0) { if (!data->pass) { VIR_TEST_DEBUG("Got expected error: %s\n", virGetLastErrorMessage()); virResetLastError(); ret = 0; goto cleanup; } } else if (nfilters != data->count) { VIR_TEST_DEBUG("Expected number of parsed outputs is %d, " "but got %d\n", data->count, nfilters); goto cleanup; } else if (!data->pass) { VIR_TEST_DEBUG("Test should have failed\n"); goto cleanup; } ret = 0; cleanup: virLogReset(); return ret; }
virCapsPtr virBhyveCapsBuild(void) { virCapsPtr caps; virCapsGuestPtr guest; if ((caps = virCapabilitiesNew(virArchFromHost(), false, false)) == NULL) return NULL; if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_X86_64, "bhyve", NULL, 0, NULL)) == NULL) goto error; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_BHYVE, NULL, NULL, 0, NULL) == NULL) goto error; if (virBhyveCapsInitCPU(caps, virArchFromHost()) < 0) VIR_WARN("Failed to get host CPU: %s", virGetLastErrorMessage()); return caps; error: virObjectUnref(caps); return NULL; }
static int showHypervisorInfo(virConnectPtr conn) { int ret = 0; unsigned long hvVer, major, minor, release; const char *hvType; /* virConnectGetType returns a pointer to a static string, so no * allocation or freeing is necessary; it is possible for the call * to fail if, for example, there is no connection to a * hypervisor, so check what it returns. */ hvType = virConnectGetType(conn); if (!hvType) { ret = 1; printf("Failed to get hypervisor type: %s\n", virGetLastErrorMessage()); goto out; } if (0 != virConnectGetVersion(conn, &hvVer)) { ret = 1; printf("Failed to get hypervisor version: %s\n", virGetLastErrorMessage()); goto out; } major = hvVer / 1000000; hvVer %= 1000000; minor = hvVer / 1000; release = hvVer % 1000; printf("Hypervisor: \"%s\" version: %lu.%lu.%lu\n", hvType, major, minor, release); out: return ret; }
static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, unsigned int flags, testCompareNetXML2XMLResult expectResult) { char *actual = NULL; int ret; testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS; virNetworkDefPtr dev = NULL; if (!(dev = virNetworkDefParseFile(inxml))) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE) goto cleanup; if (!(actual = virNetworkDefFormat(dev, flags))) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT) goto cleanup; if (virTestCompareToFile(actual, outxml) < 0) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE) goto cleanup; cleanup: if (result == expectResult) { ret = 0; if (expectResult != TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS) { VIR_TEST_DEBUG("Got expected failure code=%d msg=%s", result, virGetLastErrorMessage()); } } else { ret = -1; VIR_TEST_DEBUG("Expected result code=%d but received code=%d", expectResult, result); } virResetLastError(); VIR_FREE(actual); virNetworkDefFree(dev); return ret; }
/* * This tests sanity checking of our own certificates * * This code is done when libvirtd starts up, or before * a libvirt client connects. The test is ensuring that * the creation of virNetTLSContextPtr fails if we * give bogus certs, or succeeds for good certs */ static int testTLSContextInit(const void *opaque) { struct testTLSContextData *data = (struct testTLSContextData *)opaque; virNetTLSContextPtr ctxt = NULL; int ret = -1; if (data->isServer) { ctxt = virNetTLSContextNewServer(data->cacrt, NULL, data->crt, KEYFILE, NULL, "NORMAL", true, true); } else { ctxt = virNetTLSContextNewClient(data->cacrt, NULL, data->crt, KEYFILE, "NORMAL", true, true); } if (ctxt) { if (data->expectFail) { VIR_WARN("Expected failure %s against %s", data->cacrt, data->crt); goto cleanup; } } else { if (!data->expectFail) { VIR_WARN("Unexpected failure %s against %s", data->cacrt, data->crt); goto cleanup; } VIR_DEBUG("Got error %s", virGetLastErrorMessage()); } ret = 0; cleanup: virObjectUnref(ctxt); return ret; }
static int testSELinuxLabeling(const void *opaque) { const char *testname = opaque; int ret = -1; testSELinuxFile *files = NULL; size_t nfiles = 0; size_t i; virDomainDefPtr def = NULL; if (testSELinuxLoadFileList(testname, &files, &nfiles) < 0) goto cleanup; if (testSELinuxCreateDisks(files, nfiles) < 0) goto cleanup; if (!(def = testSELinuxLoadDef(testname))) goto cleanup; if (virSecurityManagerSetAllLabel(mgr, def, NULL, false) < 0) goto cleanup; if (testSELinuxCheckLabels(files, nfiles) < 0) goto cleanup; ret = 0; cleanup: if (testSELinuxDeleteDisks(files, nfiles) < 0) VIR_WARN("unable to fully clean up"); virDomainDefFree(def); for (i = 0; i < nfiles; i++) { VIR_FREE(files[i].file); VIR_FREE(files[i].context); } VIR_FREE(files); if (ret < 0) VIR_TEST_VERBOSE("%s\n", virGetLastErrorMessage()); return ret; }
static int mymain(void) { int ret = 0; int rc = testUserXattrEnabled(); if (rc < 0) return EXIT_FAILURE; if (!rc) return EXIT_AM_SKIP; if (!(mgr = virSecurityManagerNew("selinux", "QEMU", VIR_SECURITY_MANAGER_DEFAULT_CONFINED | VIR_SECURITY_MANAGER_PRIVILEGED))) { VIR_TEST_VERBOSE("Unable to initialize security driver: %s\n", virGetLastErrorMessage()); return EXIT_FAILURE; } if ((caps = testQemuCapsInit()) == NULL) return EXIT_FAILURE; if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; #define DO_TEST_LABELING(name) \ if (virTestRun("Labelling " # name, testSELinuxLabeling, name) < 0) \ ret = -1; setcon((security_context_t)"system_r:system_u:libvirtd_t:s0:c0.c1023"); DO_TEST_LABELING("disks"); DO_TEST_LABELING("kernel"); DO_TEST_LABELING("chardev"); DO_TEST_LABELING("nfs"); qemuTestDriverFree(&driver); return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }
#include "internal.h" #include "virthread.h" #include "qemu/qemu_capabilities.h" #define __QEMU_CAPSRIV_H_ALLOW__ 1 #include "qemu/qemu_capspriv.h" #define VIR_FROM_THIS VIR_FROM_NONE static void eventLoop(void *opaque ATTRIBUTE_UNUSED) { while (1) { if (virEventRunDefaultImpl() < 0) { fprintf(stderr, "Failed to run event loop: %s\n", virGetLastErrorMessage()); } } } int main(int argc, char **argv) { virThread thread; virQEMUCapsPtr caps; VIRT_TEST_PRELOAD(abs_builddir "/.libs/qemucapsprobemock.so"); if (argc != 2) { fprintf(stderr, "%s QEMU_binary\n", argv[0]);
static int testFDStreamReadCommon(const char *scratchdir, bool blocking) { int fd = -1; char *file = NULL; int ret = -1; char *pattern = NULL; char *buf = NULL; virStreamPtr st = NULL; size_t i; virConnectPtr conn = NULL; int flags = 0; if (!blocking) flags |= VIR_STREAM_NONBLOCK; if (!(conn = virConnectOpen("test:///default"))) goto cleanup; if (VIR_ALLOC_N(pattern, PATTERN_LEN) < 0 || VIR_ALLOC_N(buf, PATTERN_LEN) < 0) goto cleanup; for (i = 0; i < PATTERN_LEN; i++) pattern[i] = i; if (virAsprintf(&file, "%s/input.data", scratchdir) < 0) goto cleanup; if ((fd = open(file, O_CREAT|O_WRONLY|O_EXCL, 0600)) < 0) goto cleanup; for (i = 0; i < 10; i++) { if (safewrite(fd, pattern, PATTERN_LEN) != PATTERN_LEN) goto cleanup; } if (VIR_CLOSE(fd) < 0) goto cleanup; if (!(st = virStreamNew(conn, flags))) goto cleanup; /* Start reading 1/2 way through first pattern * and end 1/2 way through last pattern */ if (virFDStreamOpenFile(st, file, PATTERN_LEN / 2, PATTERN_LEN * 9, O_RDONLY) < 0) goto cleanup; for (i = 0; i < 10; i++) { size_t offset = 0; size_t want; if (i == 0) want = PATTERN_LEN / 2; else want = PATTERN_LEN; while (want > 0) { int got; reread: got = st->driver->streamRecv(st, buf + offset, want); if (got < 0) { if (got == -2 && !blocking) { usleep(20 * 1000); goto reread; } virFilePrintf(stderr, "Failed to read stream: %s\n", virGetLastErrorMessage()); goto cleanup; } if (got == 0) { /* Expect EOF 1/2 through last pattern */ if (i == 9 && want == (PATTERN_LEN / 2)) break; virFilePrintf(stderr, "Unexpected EOF block %zu want %zu\n", i, want); goto cleanup; } offset += got; want -= got; } if (i == 0) { if (memcmp(buf, pattern + (PATTERN_LEN / 2), PATTERN_LEN / 2) != 0) { virFilePrintf(stderr, "Mismatched pattern data iteration %zu\n", i); goto cleanup; } } else if (i == 9) { if (memcmp(buf, pattern, PATTERN_LEN / 2) != 0) { virFilePrintf(stderr, "Mismatched pattern data iteration %zu\n", i); goto cleanup; } } else { if (memcmp(buf, pattern, PATTERN_LEN) != 0) { virFilePrintf(stderr, "Mismatched pattern data iteration %zu\n", i); goto cleanup; } } } if (st->driver->streamFinish(st) != 0) { virFilePrintf(stderr, "Failed to finish stream: %s\n", virGetLastErrorMessage()); goto cleanup; } ret = 0; cleanup: if (st) virStreamFree(st); VIR_FORCE_CLOSE(fd); if (file != NULL) unlink(file); if (conn) virConnectClose(conn); VIR_FREE(file); VIR_FREE(pattern); VIR_FREE(buf); return ret; }
static int testCompareXMLToArgvFiles(const char *xml, const char *cmdline, virQEMUCapsPtr extraFlags, const char *migrateFrom, int migrateFd, virQemuXML2ArgvTestFlags flags) { char *expectargv = NULL; int len; char *actualargv = NULL; int ret = -1; virDomainDefPtr vmdef = NULL; virDomainChrSourceDef monitor_chr; virConnectPtr conn; char *log = NULL; virCommandPtr cmd = NULL; if (!(conn = virGetConnect())) goto out; conn->secretDriver = &fakeSecretDriver; if (!(vmdef = virDomainDefParseFile(xml, driver.caps, driver.xmlopt, QEMU_EXPECTED_VIRT_TYPES, VIR_DOMAIN_XML_INACTIVE))) { if (flags & FLAG_EXPECT_PARSE_ERROR) goto ok; goto out; } if (virQEMUCapsGet(extraFlags, QEMU_CAPS_DOMID)) vmdef->id = 6; else vmdef->id = -1; memset(&monitor_chr, 0, sizeof(monitor_chr)); monitor_chr.type = VIR_DOMAIN_CHR_TYPE_UNIX; monitor_chr.data.nix.path = (char *)"/tmp/test-monitor"; monitor_chr.data.nix.listen = true; virQEMUCapsSetList(extraFlags, QEMU_CAPS_VNC_COLON, QEMU_CAPS_NO_REBOOT, QEMU_CAPS_NO_ACPI, QEMU_CAPS_LAST); if (STREQ(vmdef->os.machine, "pc") && STREQ(vmdef->emulator, "/usr/bin/qemu-system-x86_64")) { VIR_FREE(vmdef->os.machine); if (VIR_STRDUP(vmdef->os.machine, "pc-0.11") < 0) goto out; } if (virQEMUCapsGet(extraFlags, QEMU_CAPS_DEVICE)) { if (qemuDomainAssignAddresses(vmdef, extraFlags, NULL)) { if (flags & FLAG_EXPECT_ERROR) goto ok; goto out; } } log = virtTestLogContentAndReset(); VIR_FREE(log); virResetLastError(); if (vmdef->os.arch == VIR_ARCH_X86_64 || vmdef->os.arch == VIR_ARCH_I686) { virQEMUCapsSet(extraFlags, QEMU_CAPS_PCI_MULTIBUS); } if (qemuAssignDeviceAliases(vmdef, extraFlags) < 0) goto out; if (!(cmd = qemuBuildCommandLine(conn, &driver, vmdef, &monitor_chr, (flags & FLAG_JSON), extraFlags, migrateFrom, migrateFd, NULL, VIR_NETDEV_VPORT_PROFILE_OP_NO_OP, &testCallbacks))) { if (flags & FLAG_EXPECT_FAILURE) { ret = 0; if (virTestGetDebug() > 1) fprintf(stderr, "Got expected error: %s\n", virGetLastErrorMessage()); virResetLastError(); } goto out; } else if (flags & FLAG_EXPECT_FAILURE) { if (virTestGetDebug()) fprintf(stderr, "qemuBuildCommandLine should have failed\n"); goto out; } if (!!virGetLastError() != !!(flags & FLAG_EXPECT_ERROR)) { if (virTestGetDebug() && (log = virtTestLogContentAndReset())) fprintf(stderr, "\n%s", log); goto out; } if (!(actualargv = virCommandToString(cmd))) goto out; len = virtTestLoadFile(cmdline, &expectargv); if (len < 0) goto out; if (len && expectargv[len - 1] == '\n') expectargv[len - 1] = '\0'; if (STRNEQ(expectargv, actualargv)) { virtTestDifference(stderr, expectargv, actualargv); goto out; } ok: if (flags & FLAG_EXPECT_ERROR) { /* need to suppress the errors */ virResetLastError(); } ret = 0; out: VIR_FREE(log); VIR_FREE(expectargv); VIR_FREE(actualargv); virCommandFree(cmd); virDomainDefFree(vmdef); virObjectUnref(conn); return ret; }
int main(int argc, char *argv[]) { int ret = 0; virConnectPtr conn; char *uri; char inpFileName[256] = { 0 }; char lz4FileName[256] = { 0 }; char decFileName[256] = { 0 }; char pepe1[] = "someFile.txt"; printf("Attempting to connect to hypervisor\n"); uri = (argc > 0 ? argv[1] : NULL); /* virConnectOpenAuth is called here with all default parameters, * except, possibly, the URI of the hypervisor. */ conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0); if (!conn) { ret = 1; printf("No connection to hypervisor: %s\n", virGetLastErrorMessage()); goto out; } uri = virConnectGetURI(conn); if (!uri) { ret = 1; printf("Failed to get URI for hypervisor connection: %s\n", virGetLastErrorMessage()); goto disconnect; } printf("Connected to hypervisor at \"%s\"\n", uri); // snprintf(inpFilename, 256, "%s", argv[1]); // snprintf(lz4Filename, 256, "%s.lz4s-%d", argv[1], BLOCK_BYTES); // snprintf(decFilename, 256, "%s.lz4s-%d.dec", argv[1], BLOCK_BYTES); snprintf(inpFileName, 256, "%s", pepe1); snprintf(lz4FileName, 256, "%s.lz4s-%d", pepe1, BLOCK_BYTES); snprintf(decFileName, 256, "%s.lz4s-%d.dec", pepe1, BLOCK_BYTES); printf("inp = [%s]\n", inpFileName); printf("lz4 = [%s]\n", lz4FileName); printf("dec = [%s]\n", decFileName); compress(inpFileName, lz4FileName); decompress(lz4FileName, decFileName); free(uri); disconnect: if (0 != virConnectClose(conn)) { printf("Failed to disconnect from hypervisor: %s\n", virGetLastErrorMessage()); ret = 1; } else { printf("Disconnected from hypervisor\n"); } out: return ret; }