1 /* This file contains trivial example code to connect to the running
2 * hypervisor and gather a few bits of information about domains.
3 * Similar API's exist for storage pools, networks, and interfaces. */
7 #include <libvirt/libvirt.h>
8 #include <libvirt/virterror.h>
11 showHypervisorInfo(virConnectPtr conn
)
14 unsigned long hvVer
, major
, minor
, release
;
17 /* virConnectGetType returns a pointer to a static string, so no
18 * allocation or freeing is necessary; it is possible for the call
19 * to fail if, for example, there is no connection to a
20 * hypervisor, so check what it returns. */
21 hvType
= virConnectGetType(conn
);
24 printf("Failed to get hypervisor type: %s\n",
25 virGetLastErrorMessage());
29 if (0 != virConnectGetVersion(conn
, &hvVer
)) {
31 printf("Failed to get hypervisor version: %s\n",
32 virGetLastErrorMessage());
36 major
= hvVer
/ 1000000;
39 release
= hvVer
% 1000;
41 printf("Hypervisor: \"%s\" version: %lu.%lu.%lu\n",
53 showDomains(virConnectPtr conn
)
55 int ret
= 0, numNames
, numInactiveDomains
, numActiveDomains
;
57 int flags
= VIR_CONNECT_LIST_DOMAINS_ACTIVE
|
58 VIR_CONNECT_LIST_DOMAINS_INACTIVE
;
59 virDomainPtr
*nameList
= NULL
;
61 /* NB: The return from the virConnectNum*() APIs is only useful for
62 * the current call. A domain could be started or stopped and any
63 * assumptions made purely on these return values could result in
64 * unexpected results */
65 numActiveDomains
= virConnectNumOfDomains(conn
);
66 if (numActiveDomains
== -1) {
68 printf("Failed to get number of active domains: %s\n",
69 virGetLastErrorMessage());
73 numInactiveDomains
= virConnectNumOfDefinedDomains(conn
);
74 if (numInactiveDomains
== -1) {
76 printf("Failed to get number of inactive domains: %s\n",
77 virGetLastErrorMessage());
81 printf("There are %d active and %d inactive domains\n",
82 numActiveDomains
, numInactiveDomains
);
84 /* Return a list of all active and inactive domains. Using this API
85 * instead of virConnectListDomains() and virConnectListDefinedDomains()
86 * is preferred since it "solves" an inherit race between separated API
87 * calls if domains are started or stopped between calls */
88 numNames
= virConnectListAllDomains(conn
,
93 printf("Failed to get a list of all domains: %s\n",
94 virGetLastErrorMessage());
98 for (i
= 0; i
< numNames
; i
++) {
99 int active
= virDomainIsActive(nameList
[i
]);
100 printf(" %8s (%s)\n",
101 virDomainGetName(nameList
[i
]),
102 (active
== 1 ? "active" : "non-active"));
103 /* must free the returned named per the API documentation */
104 virDomainFree(nameList
[i
]);
114 main(int argc
, char *argv
[])
120 printf("Attempting to connect to hypervisor\n");
122 uri
= (argc
> 0 ? argv
[1] : NULL
);
124 /* virConnectOpenAuth is called here with all default parameters,
125 * except, possibly, the URI of the hypervisor. */
126 conn
= virConnectOpenAuth(uri
, virConnectAuthPtrDefault
, 0);
130 printf("No connection to hypervisor: %s\n",
131 virGetLastErrorMessage());
135 uri
= virConnectGetURI(conn
);
138 printf("Failed to get URI for hypervisor connection: %s\n",
139 virGetLastErrorMessage());
143 printf("Connected to hypervisor at \"%s\"\n", uri
);
146 if (0 != showHypervisorInfo(conn
)) {
151 if (0 != showDomains(conn
)) {
157 if (0 != virConnectClose(conn
)) {
158 printf("Failed to disconnect from hypervisor: %s\n",
159 virGetLastErrorMessage());
162 printf("Disconnected from hypervisor\n");