2006-01-31 18:13:27 +00:00
|
|
|
/**
|
2020-10-02 14:07:27 +00:00
|
|
|
* section: Information
|
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:
|
2019-04-02 09:58:33 +00:00
|
|
|
* @name: the name of the domain
|
2006-01-31 18:13:27 +00:00
|
|
|
*
|
2008-03-14 11:08:03 +00:00
|
|
|
* extract the domain 0 information
|
2006-01-31 18:13:27 +00:00
|
|
|
*/
|
|
|
|
static void
|
2019-04-02 09:58:33 +00:00
|
|
|
getDomainInfo(const char *uri, const char *name)
|
2014-03-18 08:19:33 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2019-04-02 09:58:33 +00:00
|
|
|
conn = virConnectOpenReadOnly(uri);
|
2006-01-31 18:13:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-02 09:58:33 +00:00
|
|
|
/* Find the domain of the given name */
|
|
|
|
dom = virDomainLookupByName(conn, name);
|
2006-01-31 18:13:27 +00:00
|
|
|
if (dom == NULL) {
|
2019-04-02 09:58:33 +00:00
|
|
|
fprintf(stderr, "Failed to find Domain %s\n", name);
|
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) {
|
2019-04-02 09:58:33 +00:00
|
|
|
fprintf(stderr, "Failed to get information for Domain %s\n", name);
|
2008-04-10 16:54:54 +00:00
|
|
|
goto error;
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:58:33 +00:00
|
|
|
printf("Domain %s: %d CPUs\n", name, info.nrVirtCpu);
|
2006-01-31 18:13:27 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-02 09:58:33 +00:00
|
|
|
int main(int argc, char **argv)
|
2014-03-18 08:19:33 +00:00
|
|
|
{
|
2019-04-02 09:58:33 +00:00
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "syntax: %s: URI NAME\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
getDomainInfo(argv[1], argv[2]);
|
2006-01-31 18:13:27 +00:00
|
|
|
|
2012-03-22 11:33:35 +00:00
|
|
|
return 0;
|
2006-01-31 18:13:27 +00:00
|
|
|
}
|