Ejemplo n.º 1
0
struct domain_interface_info_list *libvirt_domain_interface_info_list() {
	int i;

	struct domain_interface_info_list *domain_interface_info_list = (struct domain_interface_info_list *) calloc(1, 
	sizeof(struct domain_interface_info_list));
	domain_interface_info_list->num_domains = nr_domains;
	domain_interface_info_list->domain_interface_info = (struct domain_interface_info *) calloc(nr_domains, sizeof(struct domain_interface_info));

	for(i = 0; i < nr_interface_devices; ++i) {
		struct _virDomainInterfaceStats stats;
		if(virDomainInterfaceStats(interface_devices[i].dom, interface_devices[i].path, &stats, sizeof stats) != 0)
			continue;
		domain_interface_info_list->domain_interface_info[i].domain_name = strdup(virDomainGetName(domains[i]));
		domain_interface_info_list->domain_interface_info[i].domain_id = virDomainGetID(domains[i]);
		domain_interface_info_list->domain_interface_info[i].domain_if_rxbytes = (unsigned long long) stats.rx_bytes;
		domain_interface_info_list->domain_interface_info[i].domain_if_txbytes = (unsigned long long) stats.tx_bytes;
		domain_interface_info_list->domain_interface_info[i].domain_if_rxpackets = (unsigned long long) stats.rx_packets;
		domain_interface_info_list->domain_interface_info[i].domain_if_txpackets = (unsigned long long) stats.tx_packets;
		domain_interface_info_list->domain_interface_info[i].domain_if_rxerrors = (unsigned long long) stats.rx_errs;
		domain_interface_info_list->domain_interface_info[i].domain_if_txerrors = (unsigned long long) stats.tx_errs;
		domain_interface_info_list->domain_interface_info[i].domain_if_rxdrops = (unsigned long long) stats.rx_drop;
		domain_interface_info_list->domain_interface_info[i].domain_if_txdrops = (unsigned long long) stats.tx_drop;
	}
	return domain_interface_info_list;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	virConnectPtr conn;
	int i;
	int numDomains;
	int *activeDomains;
	char **inactiveDomains;
	
	conn = virConnectOpen("qemu:///system");
	if (conn == NULL) 
	{
		fprintf(stderr, "Failed to open connection to qemu:///system\n");
		return -1;
	}

	numDomains = virConnectNumOfDomains(conn);
	if (numDomains == -1) 
	{
		fprintf(stderr, "Failed to get domian num of qemu\n");
		return -1;
	}
	
	activeDomains = malloc(sizeof(int) * numDomains);
	numDomains = virConnectListDomains(conn, activeDomains, numDomains);
	
	printf("%d Active domain list:\n", numDomains);
	for (i = 0 ; i < numDomains ; i++) 
	{
		printf("ID = %d, Name = %s\n", activeDomains[i], virDomainGetName(virDomainLookupByID(conn, activeDomains[i])));
	}
	free(activeDomains);
	
	printf("----------------------------\n");
	numDomains = virConnectNumOfDefinedDomains(conn);
	
	if (numDomains == -1) 
	{
		fprintf(stderr, "Failed to get defined domian num of qemu\n");
		return -1;
	}
	
	inactiveDomains = malloc(sizeof(char *) * numDomains);
	numDomains = virConnectListDefinedDomains(conn, inactiveDomains, numDomains);
	printf("%d Inactive domain list:\n", numDomains);
	for (i = 0 ; i < numDomains ; i++) 
	{
		/*All inactive domains's id should be 0*/
		printf("ID = %d, Name = %s\n", virDomainGetID(virDomainLookupByName(conn, inactiveDomains[i])), inactiveDomains[i]);
		free(inactiveDomains[i]);
	}
	free(inactiveDomains);
	
	virConnectClose(conn);
	return 0;
}
Ejemplo n.º 3
0
int find_a_domain(virConnectPtr conn, char *domain){
	virDomainPtr dom;

	// Search for a virtual machine (aka domain) on the hypervisor
	// connection
	dom = virDomainLookupByName(conn, domain);
        if (!dom) {
                return 0;
        } else {
		return virDomainGetID(dom);
	}
}
Ejemplo n.º 4
0
struct domain_disk_info_list *libvirt_domain_disk_info_list() {
	int i;

	struct domain_disk_info_list *domain_disk_info_list = (struct domain_disk_info_list *) calloc(1, sizeof(struct domain_disk_info_list));
	domain_disk_info_list->num_domains = nr_domains;
	domain_disk_info_list->domain_disk_info = (struct domain_disk_info *) calloc(nr_domains, sizeof(struct domain_disk_info));

	for(i = 0; i < nr_block_devices; ++i) {
		struct _virDomainBlockStats stats;
		if(virDomainBlockStats(block_devices[i].dom, block_devices[i].path, &stats, sizeof stats) != 0)
			continue;
		domain_disk_info_list->domain_disk_info[i].domain_name = strdup(virDomainGetName(domains[i]));
		domain_disk_info_list->domain_disk_info[i].domain_id = virDomainGetID(domains[i]);
		domain_disk_info_list->domain_disk_info[i].domain_vblock_rreq = (unsigned long long) stats.rd_req;
		domain_disk_info_list->domain_disk_info[i].domain_vblock_wreq = (unsigned long long) stats.wr_req;
		domain_disk_info_list->domain_disk_info[i].domain_vblock_rbytes = (unsigned long long) stats.rd_bytes;
		domain_disk_info_list->domain_disk_info[i].domain_vblock_wbytes = (unsigned long long) stats.wr_bytes;
	}
	return domain_disk_info_list;
}
Ejemplo n.º 5
0
struct domain_info_list *libvirt_domain_info_list() {
	int i;

	struct domain_info_list *domain_info_list = (struct domain_info_list *) calloc(1, sizeof(struct domain_info_list));
	domain_info_list->num_domains = nr_domains;
	domain_info_list->domain_info = (struct domain_info *) calloc(nr_domains, sizeof(struct domain_info));

	for(i = 0; i < nr_domains; ++i) {
		if(virDomainGetInfo(domains[i], &vir_domain_info) != 0) 
			continue;

		domain_info_list->domain_info[i].domain_name = strdup(virDomainGetName(domains[i]));
		domain_info_list->domain_info[i].domain_id = virDomainGetID(domains[i]);
		domain_info_list->domain_info[i].domain_state = vir_domain_info.state;
		domain_info_list->domain_info[i].domain_mem_max = vir_domain_info.maxMem;
		domain_info_list->domain_info[i].domain_mem_used = vir_domain_info.memory;
		domain_info_list->domain_info[i].domain_num_vcpus = vir_domain_info.nrVirtCpu;
		domain_info_list->domain_info[i].domain_cpu_total_time = vir_domain_info.cpuTime;
	}
	return domain_info_list;
}
Ejemplo n.º 6
0
Archivo: virt.cpp Proyecto: kohn/rsi
std::string VM_Controller::open_vm(std::string name){
    Json::Value j(Json::objectValue);
    j["status"] = "ok";
    
    virDomainPtr dom = virDomainLookupByName(_conn, name.c_str());
    if(dom == NULL){
        j["status"] = "no such domain";
        return j.toStyledString();
    }
    if(virDomainCreate(dom) < 0){
        j["status"] = "could not open domain";
        return j.toStyledString();
    }

    int dom_id;
    if( (dom_id = virDomainGetID(dom)) < 0){
        j["status"] = "could not get domain id";
        return j.toStyledString();
    }
    j["vm_id"] = dom_id;
    return j.toStyledString();
}
Ejemplo n.º 7
0
Archivo: virt.cpp Proyecto: kohn/rsi
std::string VM_Controller::get_vm_detail(virDomainPtr dom){
    Json::Value j;
    j["status"] = "ok";
    virDomainInfo info;
    if(virDomainGetInfo(dom, &info) < 0){
        LOG_ERROR("could not get domain info");
        j["status"] = "no such domain";
        return j.toStyledString();
    }
    // basic info
    int state;
    virDomainGetState(dom, &state, NULL, 0);
    j["vm_status"] = state_code2string(state);
    if(virDomainIsActive(dom)){
        j["id"] = virDomainGetID(dom);
    }
    j["name"] = virDomainGetName(dom);
    j["vcpu"] = virDomainGetMaxVcpus(dom);
    j["mem_total"] = (unsigned long long)virDomainGetMaxMemory(dom);

    // more detailed info
    char *domain_xml = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_SECURE);
    tinyxml2::XMLDocument doc;
    doc.Parse(domain_xml);
    j["img_path"] = doc.RootElement()
        ->FirstChildElement("devices")
        ->FirstChildElement("disk")
        ->FirstChildElement("source")
        ->Attribute("file");
    tinyxml2::XMLElement *graphics = doc.RootElement()
        ->FirstChildElement("devices")
        ->FirstChildElement("graphics");
    j["vnc_port"] = graphics->Attribute("port");

    virDomainFree(dom);
    
    return j.toStyledString();
}
Ejemplo n.º 8
0
                ret = "Snapshot";
                break;
            }
            break;
    }
    return ret;
}

static int myDomainEventCallback1(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
    return 0;
}

static int myDomainEventCallback2(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
    return 0;
}
Ejemplo n.º 9
0
int startDomain(char *xml, char *uri)
{
	int id, tries, res = 0;
	char *port = NULL;
	char *xmldesc = NULL;
	virDomainPtr dp = NULL;

	if (cp == NULL) {
		cp = libvirtConnect(uri);
		if (cp == NULL) {
			DPRINTF("Connection to %s failed\n", uri);
			return -EIO;
		}
	}

	DPRINTF("Starting domain\n");
	dp = virDomainCreateXML(cp, xml, 0);
	if (dp == NULL) {
		DPRINTF("virDomainCreateXML call failed\n");
		DPRINTF("XML File data:\n%s\n", xml);
		return -EINVAL;
	}

	DPRINTF("Domain started\n");

	tries = 0;
	xmldesc = virDomainGetXMLDesc(dp, 0);
	if (xmldesc == NULL) {
		if (tries > 5) {
			DPRINTF("Cannot get domain XML description\n");
			virDomainFree(dp);
			return -EIO;
		}

		sleep(1);
		tries++;
	}

	port = xml_query(xmldesc, "//domain/devices/graphics/@port");
	free(xmldesc);

	if (port == NULL) {
		DPRINTF("Port lookup failed, node not accessible\n");
		virDomainFree(dp);
		return -ENOENT;
	}

	DPRINTF("Graphics port number: %s\n", port);

	id = virDomainGetID(dp);
	DPRINTF("Domain created with ID %d\n", id);
#ifdef USE_HACK
	if (startVNCViewer(NULL, NULL, 1) != VNC_STATUS_UNSUPPORTED) {
		char path[1024] = { 0 };
		char buf[2048] = { 0 };
		char cmd[4096] = { 0 };

		snprintf(path, sizeof(path), "/proc/%d/exe", getpid());
		readlink(path, buf, sizeof(buf));
		snprintf(cmd, sizeof(cmd), "%s -v localhost:%s -f -l console 2> /dev/null", buf, port);
		DPRINTF("About to run '%s'\n", cmd);
		res = WEXITSTATUS(system(cmd));
	}
	else
		res = VNC_STATUS_NO_CONNECTION;
#else
	res = startVNCViewer("localhost", port, 1);
#endif
	if (((virDomainIsActive(dp)) && (!domainIsOff))
		|| (res != VNC_STATUS_SHUTDOWN)) {
		DPRINTF("Domain is active, destroying...\n");
		virDomainDestroy(dp);
	}

	DPRINTF("Domain %d done.\n", id);
	virDomainFree(dp);

	DPRINTF("Returning with %d\n", lastErrorCode);
	return lastErrorCode;
}