//qDebug()<<URIs; foreach (QString uri, URIs) { virConnect *connPtr = virConnectOpenReadOnly(uri.toUtf8().data()); if ( Q_NULLPTR!=connPtr ) { // don't work for VBox // int num = virConnectNumOfDefinedDomains(connPtr); virDomainPtr *domains; int ret = virConnectListAllDomains( connPtr, &domains, 0); if ( ret+1 ) { for (int i = 0; i < ret; i++) { if ( virDomainFree(domains[i]) <0 ) sendConnErrors(); }; if (domains) free(domains); } else { sendConnErrors(); }; virConnectClose(connPtr); if ( ret ) { emit localConnFound(uri); }; } else { sendConnErrors(); }; //msleep(333); };
static int fetch_domains(virConnectPtr conn) { int num_domains, ret = -1; virDomainPtr *domains = NULL; ssize_t i; const int list_flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE; DEBUG("Fetching list of running domains"); num_domains = virConnectListAllDomains(conn, &domains, list_flags); DEBUG("num_domains=%d", num_domains); if (num_domains < 0) { ERROR("Unable to fetch list of running domains"); goto cleanup; } printf("Running domains:\n"); printf("----------------\n"); for (i = 0; i < num_domains; i++) { virDomainPtr dom = domains[i]; const char *dom_name = virDomainGetName(dom); printf("%s\n", dom_name); virDomainFree(dom); } ret = 0; cleanup: free(domains); return ret; }
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; }
void main(int argc, char *argv[]) { if(argc <= 1) printf("usage: deletedom domname\n"),exit(-1); char **hosts = gethosts(); char **hostsptr = hosts; virConnectPtr *dests = (virConnectPtr*)malloc(sizeof(virConnectPtr)*1024); virConnectPtr *destsptr = dests; while(*hostsptr != NULL) { printf("Adding vmhost %s\n", *hostsptr); char URL[512]; sprintf(URL, "qemu+ssh://%s/system", *hostsptr); virConnectPtr dest = virConnectOpen(URL); *(destsptr++) = dest; *destsptr = NULL; hostsptr++; } char *searched_name = argv[1]; virConnectPtr src = virConnectOpen("qemu:///system"); virDomainPtr *domains, *domainsptr; virConnectListAllDomains(src, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE|VIR_CONNECT_LIST_DOMAINS_INACTIVE|VIR_CONNECT_LIST_DOMAINS_RUNNING|VIR_CONNECT_LIST_DOMAINS_SHUTOFF|VIR_CONNECT_LIST_DOMAINS_PERSISTENT|VIR_CONNECT_LIST_DOMAINS_OTHER|VIR_CONNECT_LIST_DOMAINS_TRANSIENT|VIR_CONNECT_LIST_DOMAINS_PAUSED); domainsptr = domains; while(*domainsptr != NULL) { virDomainPtr d = *domainsptr; const char *name = virDomainGetName(d); if (!strcmp(name, searched_name)) { printf("Found it!\n"); destsptr = dests; char uuid[VIR_UUID_BUFLEN]; virDomainGetUUID(*domainsptr, uuid); while(*destsptr != NULL) { virDomainPtr target=virDomainLookupByUUID(*destsptr, uuid); virDomainSetMetadata(target, VIR_DOMAIN_METADATA_ELEMENT, "<value>1</value>", "do_delete", "/do_delete", VIR_DOMAIN_AFFECT_CURRENT); destsptr++; } virDomainSetMetadata(d, VIR_DOMAIN_METADATA_ELEMENT, "<value>1</value>", "do_delete", "/do_delete", VIR_DOMAIN_AFFECT_CURRENT); } domainsptr++; } }
int get_domain_list(virConnectPtr conn, virDomainPtr **_list) { virDomainPtr *nameList = NULL; int n_names; int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE | VIR_CONNECT_LIST_DOMAINS_INACTIVE; n_names = virConnectListAllDomains(conn, &nameList, flags); if (n_names > 0) { *_list = nameList; } else if (n_names == 0) { /* Since there are no elements, no domain ptrs to free * but still must free the nameList returned */ free(nameList); } return n_names; }
static gint list_domains_work(gpointer data) { VmonRequest *req = data; virDomainPtr *domains; size_t i; int ret = -1; int err = 0; ret = virConnectListAllDomains(req->ctx->conn, &domains, req->ctx->flags); if (ret < 0) { collect_error(req, ret, FALSE); return ret; } for (i = 0; i < (size_t)ret; i++) { VmonRequest vreq; memcpy(&vreq, req, sizeof(vreq)); vreq.dom = domains[i]; err = executor_dispatch(vreq.ctx->executor, sample_domain_work, sampling_collect, &vreq, sizeof(vreq), vreq.ctx->conf.timeout); if (err) { collect_error(&vreq, err, FALSE); } } free(domains); return (err) ?err :0; }
int ConnAliveThread::domEventCallback(virConnectPtr _conn, virDomainPtr dom, int event, int detail, void *opaque) { //qDebug()<<"domEventCallback"<<_conn; ConnAliveThread *obj = static_cast<ConnAliveThread*>(opaque); if ( NULL==obj || *(obj->ptr_ConnPtr)!=_conn ) return 0; bool end = false; QString msg, domainName; domainName = QString(virDomainGetName(dom)); msg = QString("<b>'%1'</b> Domain %2 %3\n") .arg(domainName) .arg(obj->domEventToString(event)) .arg(obj->domEventDetailToString(event, detail, &end)); if ( end ) emit obj->domainEnd(domainName); emit obj->connMsg(msg); if ( obj->onView ) { Result result; QStringList domainList; if ( _conn!=NULL && obj->keep_alive ) { virDomainPtr *domains = NULL; unsigned int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE | VIR_CONNECT_LIST_DOMAINS_INACTIVE; // the number of domains found or -1 and sets domains to NULL in case of error. int ret = virConnectListAllDomains(_conn, &domains, flags); if ( ret<0 ) { obj->sendConnErrors(); return 0; }; // therefore correctly to use for() command, because domains[0] can not exist. for (int i = 0; i < ret; i++) { QStringList currentAttr; QString autostartStr; int is_autostart = 0; if (virDomainGetAutostart(domains[i], &is_autostart) < 0) { autostartStr.append("no autostart"); } else autostartStr.append( is_autostart ? "yes" : "no" ); int state; int reason; // flags : extra flags; not used yet, so callers should always pass 0 flags = 0; QString domainState; if ( virDomainGetState(domains[i], &state, &reason, flags)+1 ) { switch (state) { case VIR_DOMAIN_NOSTATE: domainState.append("NOSTATE"); break; case VIR_DOMAIN_RUNNING: domainState.append("RUNNING"); break; case VIR_DOMAIN_BLOCKED: domainState.append("BLOCKED"); break; case VIR_DOMAIN_PAUSED: domainState.append("PAUSED"); break; case VIR_DOMAIN_SHUTDOWN: domainState.append("SHUTDOWN"); break; case VIR_DOMAIN_SHUTOFF: domainState.append("SHUTOFF"); break; case VIR_DOMAIN_CRASHED: domainState.append("CRASHED"); break; case VIR_DOMAIN_PMSUSPENDED: domainState.append("PMSUSPENDED"); break; default: domainState.append("UNKNOWN"); break; } } else domainState.append("ERROR"); currentAttr<< QString().fromUtf8( virDomainGetName(domains[i]) ) << QString("%1:%2") .arg( virDomainIsActive(domains[i]) ? "active" : "inactive" ) .arg(domainState) << autostartStr << QString( virDomainIsPersistent(domains[i]) ? "yes" : "no" ); domainList.append(currentAttr.join(DFR)); virDomainFree(domains[i]); }; free(domains); }; //result.name = ; result.type = "domain"; //result.number = number; result.action = GET_ALL_ENTITY_STATE; result.result = true; result.msg = domainList; emit obj->domStateChanged(result); }; return 0; }
virt_list_t *vl_get(virConnectPtr vp, int my_id) { virt_list_t *vl = NULL; int d_count, x, saved_errno; virDomainPtr *dom_list; errno = EINVAL; if (!vp) return NULL; d_count = virConnectListAllDomains(vp, &dom_list, 0); if (d_count <= 0) goto out_fail; vl = malloc(sizeof(uint32_t) + sizeof(virt_state_t) * d_count); if (!vl) goto out_fail; vl->vm_count = d_count; /* Ok, we have the domain IDs - let's get their names and states */ for (x = 0; x < d_count; x++) { char *d_name; virDomainInfo d_info; char d_uuid[MAX_DOMAINNAME_LENGTH]; virDomainPtr dom = dom_list[x]; if (!(d_name = (char *)virDomainGetName(dom))) goto out_fail; if (virDomainGetUUIDString(dom, d_uuid) != 0) goto out_fail; if (virDomainGetInfo(dom, &d_info) < 0) goto out_fail; /* Store the name & state */ strncpy(vl->vm_states[x].v_name, d_name, MAX_DOMAINNAME_LENGTH); strncpy(vl->vm_states[x].v_uuid, d_uuid, MAX_DOMAINNAME_LENGTH); vl->vm_states[x].v_state.s_state = d_info.state; vl->vm_states[x].v_state.s_owner = my_id; } for (x = 0 ; x < d_count; x++) virDomainFree(dom_list[x]); free(dom_list); /* We have all the locally running domains & states now */ /* Sort */ qsort(&vl->vm_states[0], vl->vm_count, sizeof(vl->vm_states[0]), _compare_virt); return vl; out_fail: saved_errno = errno; for (x = 0 ; x < d_count; x++) virDomainFree(dom_list[x]); free(dom_list); if (vl) free(vl); errno = saved_errno; return NULL; }
void run(char *myIP, virConnectPtr localhost) { printf("*** New run\n"); virDomainPtr *domains, *domainsptr; virConnectListAllDomains(localhost, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE|VIR_CONNECT_LIST_DOMAINS_INACTIVE|VIR_CONNECT_LIST_DOMAINS_RUNNING|VIR_CONNECT_LIST_DOMAINS_SHUTOFF); domainsptr = domains; char **domainuuids = malloc(sizeof(char*)*1024); char **domainuuidsptr = domainuuids; *domainuuids = NULL; printf("Domains\n"); while(*domainsptr != NULL) { char buf[VIR_UUID_STRING_BUFLEN]; virDomainGetUUIDString(*domainsptr, buf); const char *name = virDomainGetName(*domainsptr); if (!strncmp("ignore",name,strlen("ignore"))) { domainsptr++; continue; } printf("%s\n", buf); *(domainuuidsptr++) = strdup(buf); *domainuuidsptr = NULL; domainsptr++; } domainuuidsptr = domainuuids; size_t domainuuids_count=0; while(*domainuuidsptr != NULL) domainuuidsptr++, domainuuids_count++; domainuuidsptr = domainuuids; domainsptr = domains; char **hosts = gethosts(); char **hostsptr=hosts; size_t hosts_count = 0; while (*hostsptr != NULL) hosts_count++, hostsptr++; hostsptr = hosts; struct hostwithvmhash **cs = malloc(sizeof(struct hostwithvmhash)*1024); *cs = NULL; struct hostwithvmhash **csptr = cs; int sign=0; while(*hostsptr != NULL) { if (!strcmp(*hostsptr, myIP)) printf("ME! "); sign = !sign; size_t k=1; while(*domainuuidsptr != NULL) { /* char *num = "$6$hest"; char *hash = crypt(*domainuuidsptr, num); char *hash2 = crypt(*hostsptr, num);*/ struct hostwithvmhash *c=malloc(sizeof(struct hostwithvmhash)); c->host = *hostsptr; // c->host_hash = strdup(hash+10); c->vm = *domainuuidsptr; // c->vm_hash = strdup(hash2+10); c->combined = malloc(10000); strcpy(c->combined, ""); strcat(c->combined, c->host); strcat(c->combined, c->vm); //c->combined = strdup(crypt(c->combined, num)); *(csptr++) = c; *csptr = NULL; domainuuidsptr++; } domainuuidsptr = domainuuids; printf("%s\n", *hostsptr); /* char URL[512]; sprintf(URL, "qemu+ssh://%s/system", *hosts); virConnectPtr dest = virConnectOpen(URL); *(destsptr++) = dest; *destsptr = NULL;*/ hostsptr++; } csptr = cs; printf("\n"); size_t count=0; while(*csptr != NULL) count++, csptr++; csptr = cs; qsort(csptr, count, sizeof(struct hostwithvmhash*), (__compar_fn_t)cscompare); struct hostwithvmhash **csptrptr = csptr; size_t i=0,k=0,j=0,q=1,m=0; char *last_host=NULL,*last_vm=NULL; struct hostwithvmhash **seen=malloc(sizeof(struct hostwithvmhash*)*1024); *seen = NULL; struct hostwithvmhash **seenptr=seen; struct hostwithvmhash **seenaddptr=seen; while(*csptrptr != NULL) { if (last_host == NULL || strcmp(last_host, (*csptrptr)->host)) last_host = (*csptrptr)->host,k++; seenptr=seen; int is_seen=0; while(*seenptr != NULL) { if (!strcmp((*seenptr)->vm, (*csptrptr)->vm)) is_seen=1; seenptr++; } if (!is_seen) { m++,*(seenaddptr++) = *csptrptr, *seenaddptr = NULL; (*csptrptr)->prio = 100*m+(k+m)%hosts_count; if (m == domainuuids_count) m = 0, seenaddptr=seenptr=seen,*seen=NULL; } else (*csptrptr)->prio = k; csptrptr++; } qsort(csptr, count, sizeof(struct hostwithvmhash*), (__compar_fn_t)cscompareprio); struct hostwithvmhash **cleaned = cleanlist(csptr); struct hostwithvmhash **cleanedstart = cleaned; while(*cleaned != NULL) { virDomainPtr d = virDomainLookupByUUIDString(localhost, (*cleaned)->vm); const char *name = virDomainGetName(d); int state, reason; virDomainGetState(d, &state, &reason, 0); if (!strcmp((*cleaned)->host, myIP)) { printf("I should be running %s (%s)\n", name, (*cleaned)->vm); if (state == VIR_DOMAIN_RUNNING) printf("...and it's running.\n"); else virDomainCreate(d); } else { printf("I should NOT be running %s (%s)\n", name, (*cleaned)->vm); if (state == VIR_DOMAIN_RUNNING) { printf("Destroying.\n"); virDomainDestroy(d); } else printf("...and it's not running.\n"); } if (d) { virDomainFree(d); } //printf("prio:%d host:%s, vm:%s combined:%s\n", (*cleaned)->prio, (*cleaned)->host, (*cleaned)->vm, (*cleaned)->combined); cleaned++; } domainuuidsptr = domainuuids; while(*domainuuidsptr != NULL) { free(*domainuuidsptr); domainuuidsptr++; } free(domainuuids); free(cleanedstart); csptr = cs; while(*csptr != NULL) { free((*csptr)->combined); free(*csptr); csptr++; } free(cs); free(seen); hostsptr = hosts; while(*hostsptr != NULL) { free(*hostsptr); hostsptr++; } free(hosts); domainsptr = domains; while(*domainsptr != NULL) { virDomainFree(*domainsptr); domainsptr++; } free(domains); /* virDomainPtr *domains; virConnectListAllDomains(src, &domains, VIR_CONNECT_LIST_DOMAINS_ACTIVE); while(*domains != NULL) { char *config = virDomainGetXMLDesc(*domains, 0); char uuid[VIR_UUID_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN]; virDomainGetUUIDString(*domains, uuidstr); virDomainGetUUID(*domains, uuid); destsptr = dests; while (*destsptr != NULL) { virConnectPtr dest = *destsptr; virDomainPtr d = virDomainLookupByUUID(dest, uuid); if (d) printf("%s already in dest\n", uuidstr); if (!d) { printf("%s\n", config); printf("Injecting domain on dest\n"); virDomainPtr new = virDomainDefineXML(dest, config); } fflush(stdout); destsptr++; } domains++; }*/ }