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\n"); showError(conn); goto out; } uri = virConnectGetURI(conn); if (!uri) { ret = 1; printf("Failed to get URI for hypervisor connection\n"); showError(conn); 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\n"); showError(conn); ret = 1; } else { printf("Disconnected from hypervisor\n"); } out: return ret; }
static PromiseResult VerifyVirtDomain(EvalContext *ctx, char *uri, enum cfhypervisors envtype, Attributes a, const Promise *pp) { int num, i; /* set up the library error handler */ virSetErrorFunc(NULL, (void *) EnvironmentErrorHandler); if (CFVC[envtype] == NULL) { if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL) { Log(LOG_LEVEL_ERR, "Failed to connect to virtualization monitor '%s'", uri); return PROMISE_RESULT_NOOP; } } for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { CF_RUNNING[i] = -1; CF_SUSPENDED[i] = NULL; } num = virConnectListDomains(CFVC[envtype], CF_RUNNING, CF_MAX_CONCURRENT_ENVIRONMENTS); Log(LOG_LEVEL_VERBOSE, "Found %d running guest environments on this host (including enclosure)", num); ShowRunList(CFVC[envtype]); num = virConnectListDefinedDomains(CFVC[envtype], CF_SUSPENDED, CF_MAX_CONCURRENT_ENVIRONMENTS); Log(LOG_LEVEL_VERBOSE, "Found %d dormant guest environments on this host", num); ShowDormant(); PromiseResult result = PROMISE_RESULT_NOOP; switch (a.env.state) { case ENVIRONMENT_STATE_CREATE: result = PromiseResultUpdate(result, CreateVirtDom(ctx, CFVC[envtype], a, pp)); break; case ENVIRONMENT_STATE_DELETE: result = PromiseResultUpdate(result, DeleteVirt(ctx, CFVC[envtype], a, pp)); break; case ENVIRONMENT_STATE_RUNNING: result = PromiseResultUpdate(result, RunningVirt(ctx, CFVC[envtype], a, pp)); break; case ENVIRONMENT_STATE_SUSPENDED: result = PromiseResultUpdate(result, SuspendedVirt(ctx, CFVC[envtype], a, pp)); break; case ENVIRONMENT_STATE_DOWN: result = PromiseResultUpdate(result, DownVirt(ctx, CFVC[envtype], a, pp)); break; default: Log(LOG_LEVEL_INFO, "No state specified for this environment"); break; } return result; }
int main(int argc, char *argv[]) { int ret = EXIT_FAILURE; virConnectPtr conn = NULL; const char *uri = NULL; const char *dom_name = NULL; unsigned int seconds = 1; /* Suspend domain for this long */ const int connect_flags = 0; /* No connect flags for now */ if (parse_argv(argc, argv, &uri, &dom_name, &seconds) < 0) goto cleanup; DEBUG("Proceeding with uri=%s dom_name=%s seconds=%u", uri, dom_name, seconds); if (!(conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, connect_flags))) { ERROR("Failed to connect to hypervisor"); goto cleanup; } DEBUG("Successfully connected"); if (!dom_name) { if (fetch_domains(conn) == 0) ret = EXIT_SUCCESS; goto cleanup; } if (suspend_and_resume(conn, dom_name, seconds) < 0) goto cleanup; ret = EXIT_SUCCESS; cleanup: if (conn) { int tmp; tmp = virConnectClose(conn); if (tmp < 0) { ERROR("Failed to disconnect from the hypervisor"); ret = EXIT_FAILURE; } else if (tmp > 0) { ERROR("One or more references were leaked after " "disconnect from the hypervisor"); ret = EXIT_FAILURE; } else { DEBUG("Connection successfully closed"); } } return ret; }
static void VerifyVirtDomain(EvalContext *ctx, char *uri, enum cfhypervisors envtype, Attributes a, Promise *pp) { int num, i; /* set up the library error handler */ virSetErrorFunc(NULL, (void *) EnvironmentErrorHandler); if (CFVC[envtype] == NULL) { if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL) { CfOut(OUTPUT_LEVEL_ERROR, "", " !! Failed to connect to virtualization monitor \"%s\"", uri); return; } } for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { CF_RUNNING[i] = -1; CF_SUSPENDED[i] = NULL; } num = virConnectListDomains(CFVC[envtype], CF_RUNNING, CF_MAX_CONCURRENT_ENVIRONMENTS); CfOut(OUTPUT_LEVEL_VERBOSE, "", " -> Found %d running guest environments on this host (including enclosure)", num); ShowRunList(CFVC[envtype]); num = virConnectListDefinedDomains(CFVC[envtype], CF_SUSPENDED, CF_MAX_CONCURRENT_ENVIRONMENTS); CfOut(OUTPUT_LEVEL_VERBOSE, "", " -> Found %d dormant guest environments on this host", num); ShowDormant(); switch (a.env.state) { case ENVIRONMENT_STATE_CREATE: CreateVirtDom(ctx, CFVC[envtype], a, pp); break; case ENVIRONMENT_STATE_DELETE: DeleteVirt(ctx, CFVC[envtype], a, pp); break; case ENVIRONMENT_STATE_RUNNING: RunningVirt(ctx, CFVC[envtype], a, pp); break; case ENVIRONMENT_STATE_SUSPENDED: SuspendedVirt(ctx, CFVC[envtype], a, pp); break; case ENVIRONMENT_STATE_DOWN: DownVirt(ctx, CFVC[envtype], a, pp); break; default: CfOut(OUTPUT_LEVEL_INFORM, "", " !! No state specified for this environment"); break; } }
static void VerifyVirtDomain(char *uri, enum cfhypervisors envtype, Attributes a, Promise *pp) { int num, i; /* set up the library error handler */ virSetErrorFunc(NULL, (void *) EnvironmentErrorHandler); if (CFVC[envtype] == NULL) { if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL) { CfOut(cf_error, "", " !! Failed to connect to virtualization monitor \"%s\"", uri); return; } } for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { CF_RUNNING[i] = -1; CF_SUSPENDED[i] = NULL; } num = virConnectListDomains(CFVC[envtype], CF_RUNNING, CF_MAX_CONCURRENT_ENVIRONMENTS); CfOut(cf_verbose, "", " -> Found %d running guest environments on this host (including enclosure)", num); ShowRunList(CFVC[envtype]); num = virConnectListDefinedDomains(CFVC[envtype], CF_SUSPENDED, CF_MAX_CONCURRENT_ENVIRONMENTS); CfOut(cf_verbose, "", " -> Found %d dormant guest environments on this host", num); ShowDormant(CFVC[envtype]); switch (a.env.state) { case cfvs_create: CreateVirtDom(CFVC[envtype], uri, a, pp); break; case cfvs_delete: DeleteVirt(CFVC[envtype], uri, a, pp); break; case cfvs_running: RunningVirt(CFVC[envtype], uri, a, pp); break; case cfvs_suspended: SuspendedVirt(CFVC[envtype], uri, a, pp); break; case cfvs_down: DownVirt(CFVC[envtype], uri, a, pp); break; default: CfOut(cf_inform, "", " !! No state specified for this environment"); break; } }
static PromiseResult VerifyVirtNetwork(EvalContext *ctx, char *uri, enum cfhypervisors envtype, Attributes a, const Promise *pp) { int num, i; char *networks[CF_MAX_CONCURRENT_ENVIRONMENTS]; if (CFVC[envtype] == NULL) { if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL) { Log(LOG_LEVEL_ERR, "Failed to connect to virtualization monitor '%s'", uri); return PROMISE_RESULT_NOOP; } } for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { networks[i] = NULL; } num = virConnectListNetworks(CFVC[envtype], networks, CF_MAX_CONCURRENT_ENVIRONMENTS); Log(LOG_LEVEL_VERBOSE, "Detected %d active networks", num); PromiseResult result = PROMISE_RESULT_NOOP; switch (a.env.state) { case ENVIRONMENT_STATE_CREATE: result = PromiseResultUpdate(result, CreateVirtNetwork(ctx, CFVC[envtype], networks, a, pp)); break; case ENVIRONMENT_STATE_DELETE: result = PromiseResultUpdate(result, DeleteVirtNetwork(ctx, CFVC[envtype], a, pp)); break; default: Log(LOG_LEVEL_INFO, "No recognized state specified for this network environment"); break; } return result; }
static void VerifyVirtNetwork(char *uri, enum cfhypervisors envtype, Attributes a, Promise *pp) { int num, i; char *networks[CF_MAX_CONCURRENT_ENVIRONMENTS]; if (CFVC[envtype] == NULL) { if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL) { CfOut(cf_error, "", " !! Failed to connect to virtualization monitor \"%s\"", uri); return; } } for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { networks[i] = NULL; } num = virConnectListNetworks(CFVC[envtype], networks, CF_MAX_CONCURRENT_ENVIRONMENTS); CfOut(cf_verbose, "", " -> Detected %d active networks", num); switch (a.env.state) { case cfvs_create: CreateVirtNetwork(CFVC[envtype], networks, a, pp); break; case cfvs_delete: DeleteVirtNetwork(CFVC[envtype], networks, a, pp); break; default: CfOut(cf_inform, "", " !! No recogized state specified for this network environment"); break; } }
static void VerifyVirtNetwork(EvalContext *ctx, char *uri, enum cfhypervisors envtype, Attributes a, Promise *pp) { int num, i; char *networks[CF_MAX_CONCURRENT_ENVIRONMENTS]; if (CFVC[envtype] == NULL) { if ((CFVC[envtype] = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0)) == NULL) { CfOut(OUTPUT_LEVEL_ERROR, "", " !! Failed to connect to virtualization monitor \"%s\"", uri); return; } } for (i = 0; i < CF_MAX_CONCURRENT_ENVIRONMENTS; i++) { networks[i] = NULL; } num = virConnectListNetworks(CFVC[envtype], networks, CF_MAX_CONCURRENT_ENVIRONMENTS); CfOut(OUTPUT_LEVEL_VERBOSE, "", " -> Detected %d active networks", num); switch (a.env.state) { case ENVIRONMENT_STATE_CREATE: CreateVirtNetwork(ctx, CFVC[envtype], networks, a, pp); break; case ENVIRONMENT_STATE_DELETE: DeleteVirtNetwork(ctx, CFVC[envtype], a, pp); break; default: CfOut(OUTPUT_LEVEL_INFORM, "", " !! No recogized state specified for this network environment"); break; } }
void ConnAliveThread::openConnection() { //*ptr_ConnPtr = virConnectOpen(URI.toUtf8().constData()); auth.cb = authCallback; auth.cbdata = this; _connPtr = virConnectOpenAuth(URI.toUtf8().constData(), &auth, 0); ptr_ConnPtr = &_connPtr; if (*ptr_ConnPtr==NULL) { sendConnErrors(); keep_alive = false; emit connMsg( "Connection to the Hypervisor is failed." ); emit changeConnState(FAILED); } else { //qDebug()<<" openConnection"<<*ptr_ConnPtr<<URI; keep_alive = true; emit connMsg( QString("connect opened: %1") .arg(QVariant(*ptr_ConnPtr!=NULL) .toString()) ); emit changeConnState(RUNNING); registerConnEvents(); }; //qDebug()<<"virConnectRef +1"<<"ConnAliveThread"<<URI<<(*ptr_ConnPtr!=NULL); }
/* Main Function which should be used for connecting. * This function properly handles keepalive settings. */ virConnectPtr virshConnect(vshControl *ctl, const char *uri, bool readonly) { virConnectPtr c = NULL; int interval = 5; /* Default */ int count = 6; /* Default */ bool keepalive_forced = false; if (ctl->keepalive_interval >= 0) { interval = ctl->keepalive_interval; keepalive_forced = true; } if (ctl->keepalive_count >= 0) { count = ctl->keepalive_count; keepalive_forced = true; } c = virConnectOpenAuth(uri, virConnectAuthPtrDefault, readonly ? VIR_CONNECT_RO : 0); if (!c) return NULL; if (interval > 0 && virConnectSetKeepAlive(c, interval, count) != 0) { if (keepalive_forced) { vshError(ctl, "%s", _("Cannot setup keepalive on connection " "as requested, disconnecting")); virConnectClose(c); return NULL; } vshDebug(ctl, VSH_ERR_INFO, "%s", _("Failed to setup keepalive on connection\n")); } return c; }
void get_all_libvirt_domains (const char *libvirt_uri) { virErrorPtr err; int n; size_t i; /* Get the list of all domains. */ conn = virConnectOpenAuth (libvirt_uri, virConnectAuthPtrDefault, VIR_CONNECT_RO); if (!conn) { err = virGetLastError (); fprintf (stderr, _("%s: could not connect to libvirt (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } n = virConnectNumOfDomains (conn); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not get number of running domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } int ids[n]; n = virConnectListDomains (conn, ids, n); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not list running domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } add_domains_by_id (conn, ids, n); n = virConnectNumOfDefinedDomains (conn); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not get number of inactive domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } char *names[n]; n = virConnectListDefinedDomains (conn, names, n); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not list inactive domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } add_domains_by_name (conn, names, n); /* You must free these even though the libvirt documentation doesn't * mention it. */ for (i = 0; i < (size_t) n; ++i) free (names[i]); /* No domains? */ if (nr_domains == 0) return; /* Sort the domains alphabetically by name for display. */ qsort (domains, nr_domains, sizeof (struct domain), compare_domain_names); }
/* Main Function which should be used for connecting. * This function properly handles keepalive settings. */ virConnectPtr virshConnect(vshControl *ctl, const char *uri, bool readonly) { virConnectPtr c = NULL; int interval = 5; /* Default */ int count = 6; /* Default */ bool keepalive_forced = false; virPolkitAgentPtr pkagent = NULL; int authfail = 0; bool agentCreated = false; if (ctl->keepalive_interval >= 0) { interval = ctl->keepalive_interval; keepalive_forced = true; } if (ctl->keepalive_count >= 0) { count = ctl->keepalive_count; keepalive_forced = true; } do { virErrorPtr err; if ((c = virConnectOpenAuth(uri, virConnectAuthPtrDefault, readonly ? VIR_CONNECT_RO : 0))) break; if (readonly) goto cleanup; err = virGetLastError(); if (!agentCreated && err && err->domain == VIR_FROM_POLKIT && err->code == VIR_ERR_AUTH_UNAVAILABLE) { if (!pkagent && !(pkagent = virPolkitAgentCreate())) goto cleanup; agentCreated = true; } else if (err && err->domain == VIR_FROM_POLKIT && err->code == VIR_ERR_AUTH_FAILED) { authfail++; } else { goto cleanup; } virResetLastError(); /* Failure to authenticate 5 times should be enough. * No sense prolonging the agony. */ } while (authfail < 5); if (!c) goto cleanup; if (interval > 0 && virConnectSetKeepAlive(c, interval, count) != 0) { if (keepalive_forced) { vshError(ctl, "%s", _("Cannot setup keepalive on connection " "as requested, disconnecting")); virConnectClose(c); c = NULL; goto cleanup; } vshDebug(ctl, VSH_ERR_INFO, "%s", _("Failed to setup keepalive on connection\n")); } cleanup: virPolkitAgentDestroy(pkagent); return c; }
int main(int argc, char *argv[]) { int ret = 0; virConnectPtr conn; char *uri; AuthData authData; if (argc != 4) { ret = 1; printf("Usage: %s <uri> <username> <password>\n", argv[0]); goto out; } uri = argv[1]; authData.username = argv[2]; authData.password = argv[3]; auth.cbdata = &authData; printf("Attempting to connect to hypervisor\n"); conn = virConnectOpenAuth(uri, &auth, 0); if (NULL == conn) { ret = 1; printf("No connection to hypervisor\n"); showError(conn); goto out; } uri = virConnectGetURI(conn); if (uri == NULL) { ret = 1; printf("Failed to get URI for hypervisor connection\n"); showError(conn); goto disconnect; } printf("Connected to hypervisor at \"%s\"\n", uri); free(uri); if (showHypervisorInfo(conn) != 0) { ret = 1; goto disconnect; } if (showDomains(conn) != 0) { ret = 1; goto disconnect; } disconnect: if (virConnectClose(conn) != 0) { printf("Failed to disconnect from hypervisor\n"); showError(conn); ret = 1; } else { printf("Disconnected from hypervisor\n"); } out: 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; }