2006-01-31 18:13:27 +00:00
|
|
|
/**
|
|
|
|
* section: Informations
|
2008-03-14 11:08:03 +00:00
|
|
|
* synopsis: Extract information about Xen domain 0
|
2006-01-31 18:13:27 +00:00
|
|
|
* purpose: Demonstrate the basic use of the library to connect to the
|
2008-03-14 11:08:03 +00:00
|
|
|
* hypervisor and extract domain information.
|
2006-01-31 18:13:27 +00:00
|
|
|
* usage: info1
|
|
|
|
* test: info1
|
|
|
|
* copy: see Copyright for the status of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-06-26 15:02:18 +00:00
|
|
|
#include <libvirt/libvirt.h>
|
2006-01-31 18:13:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getDomainInfo:
|
|
|
|
* @id: the id of the domain
|
|
|
|
*
|
2008-03-14 11:08:03 +00:00
|
|
|
* extract the domain 0 information
|
2006-01-31 18:13:27 +00:00
|
|
|
*/
|
|
|
|
static void
|
2014-03-18 08:19:33 +00:00
|
|
|
getDomainInfo(int id)
|
|
|
|
{
|
2006-01-31 18:13:27 +00:00
|
|
|
virConnectPtr conn = NULL; /* the hypervisor connection */
|
|
|
|
virDomainPtr dom = NULL; /* the domain being checked */
|
2008-03-14 11:08:03 +00:00
|
|
|
virDomainInfo info; /* the information being fetched */
|
2006-01-31 18:13:27 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* NULL means connect to local Xen hypervisor */
|
|
|
|
conn = virConnectOpenReadOnly(NULL);
|
|
|
|
if (conn == NULL) {
|
|
|
|
fprintf(stderr, "Failed to connect to hypervisor\n");
|
2008-04-10 16:54:54 +00:00
|
|
|
goto error;
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the domain of the given id */
|
|
|
|
dom = virDomainLookupByID(conn, id);
|
|
|
|
if (dom == NULL) {
|
|
|
|
fprintf(stderr, "Failed to find Domain %d\n", id);
|
2008-04-10 16:54:54 +00:00
|
|
|
goto error;
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|
|
|
|
|
2008-03-14 11:08:03 +00:00
|
|
|
/* Get the information */
|
2006-01-31 18:13:27 +00:00
|
|
|
ret = virDomainGetInfo(dom, &info);
|
|
|
|
if (ret < 0) {
|
2008-03-14 11:08:03 +00:00
|
|
|
fprintf(stderr, "Failed to get information for Domain %d\n", id);
|
2008-04-10 16:54:54 +00:00
|
|
|
goto error;
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Domains %d: %d CPUs\n", id, info.nrVirtCpu);
|
|
|
|
|
2014-03-25 06:48:10 +00:00
|
|
|
error:
|
2006-01-31 18:13:27 +00:00
|
|
|
if (dom != NULL)
|
|
|
|
virDomainFree(dom);
|
|
|
|
if (conn != NULL)
|
2008-04-10 16:54:54 +00:00
|
|
|
virConnectClose(conn);
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 08:19:33 +00:00
|
|
|
int main()
|
|
|
|
{
|
2006-01-31 18:13:27 +00:00
|
|
|
getDomainInfo(0);
|
|
|
|
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|