Richard W.M. Jones wrote:
On Fri, Jan 23, 2009 at 02:51:02PM -0500, Dave Allan wrote:
The examples directory doesn't have a trivial example of how to connect
to a hypervisor, make a few calls, and disconnect, so I put one
together. I would appreciate any suggestions on anything that I've done
wrong as well as suggestions for other fundamental API calls that should
be illustrated.
Yes, I checked this example code and it is fine. My only comment
would be on:
+ /* virConnectOpenAuth called here with all default parameters */
+ conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0);
It might be better to let people connect to a named URI.
Another possibility is to default to the test URI (test:///default)
since that (almost) always exists.
Hi Rich,
Thanks for taking a look at it. I added a little code to let the user
specify a URI on the command line. Do you think it is worth committing?
Dave
diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
new file mode 100644
index 0000000..22d3309
--- /dev/null
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -0,0 +1,151 @@
+/* This file contains trivial example code to connect to the running
+ * hypervisor and gather a few bits of information. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <libvirt/libvirt.h>
+
+static int
+showHypervisorInfo(virConnectPtr conn)
+{
+ int ret = 0;
+ unsigned long hvVer, major, minor, release;
+ const char *hvType;
+
+ /* virConnectGetType returns a pointer to a static string, so no
+ * allocation or freeing is necessary; it is possible for the call
+ * to fail if, for example, there is no connection to a
+ * hypervisor, so check what it returns. */
+ hvType = virConnectGetType(conn);
+ if (NULL == hvType) {
+ ret = 1;
+ printf("Failed to get hypervisor type\n");
+ goto out;
+ }
+
+ if (0 != virConnectGetVersion(conn, &hvVer)) {
+ ret = 1;
+ printf("Failed to get hypervisor version\n");
+ goto out;
+ }
+
+ major = hvVer / 1000000;
+ hvVer %= 1000000;
+ minor = hvVer / 1000;
+ release = hvVer % 1000;
+
+ printf("Hypervisor: \"%s\" version: %lu.%lu.%lu\n",
+ hvType,
+ major,
+ minor,
+ release);
+
+out:
+ return ret;
+}
+
+
+static int
+showDomains(virConnectPtr conn)
+{
+ int ret = 0, i, numNames, numInactiveDomains, numActiveDomains;
+ char **nameList = NULL;
+
+ numActiveDomains = virConnectNumOfDomains(conn);
+ numInactiveDomains = virConnectNumOfDefinedDomains(conn);
+
+ printf("There are %d active and %d inactive domains\n",
+ numActiveDomains, numInactiveDomains);
+
+ nameList = malloc(sizeof(char *) * (unsigned int)numInactiveDomains);
+
+ if (NULL == nameList) {
+ ret = 1;
+ printf("Could not allocate memory for list of inactive domains\n");
+ goto out;
+ }
+
+ numNames = virConnectListDefinedDomains(conn,
+ nameList,
+ numInactiveDomains);
+
+ if (-1 == numNames) {
+ ret = 1;
+ printf("Could not get list of defined domains from hypervisor\n");
+ goto out;
+ }
+
+ if (numNames > 0) {
+ printf("Inactive domains:\n");
+ }
+
+ for (i = 0 ; i < numNames ; i++) {
+ printf(" %s\n", *(nameList + i));
+ /* The API documentation doesn't say so, but the names
+ * returned by virConnectListDefinedDomains are strdup'd and
+ * must be freed here. */
+ free(*(nameList + i));
+ }
+
+out:
+ if (NULL != nameList) {
+ free(nameList);
+ }
+
+ return ret;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ int ret = 0;
+ virConnectPtr conn = NULL;
+ char *uri = NULL;
+
+ printf("Attempting to connect to hypervisor\n");
+
+ if (argc > 0) {
+ uri = argv[1];
+ }
+
+ /* virConnectOpenAuth is called here with all default parameters,
+ * except, possibly, the URI of the hypervisor. */
+ conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);
+
+ if (NULL == conn) {
+ ret = 1;
+ printf("No connection to hypervisor\n");
+ goto out;
+ }
+
+ uri = virConnectGetURI(conn);
+ if (NULL == uri) {
+ ret = 1;
+ printf("Failed to get URI for hypervisor connection\n");
+ goto disconnect;
+ }
+
+ printf("Connected to hypervisor at \"%s\"\n", uri);
+ free(uri);
+
+ if (0 != showHypervisorInfo(conn)) {
+ ret = 1;
+ goto disconnect;
+ }
+
+ if (0 != showDomains(conn)) {
+ ret = 1;
+ goto disconnect;
+ }
+
+disconnect:
+ if (0 != virConnectClose(conn)) {
+ printf("Failed to disconnect from hypervisor\n");
+ } else {
+ printf("Disconnected from hypervisor\n");
+ }
+
+out:
+ return ret;
+}
commit c8f073fd4ff032b88dff78d0aae93576a8dcf035
Author: David Allan <dallan@xxxxxxxxxx>
Date: Mon Jan 26 23:37:21 2009 -0500
Added a little code to let the user specify the URI of the hypervisor on the command line, per the suggestion of Rich Jones.
diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
index aae79b8..22d3309 100644
--- a/examples/hellolibvirt/hellolibvirt.c
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -97,15 +97,21 @@ out:
int
-main(void)
+main(int argc, char *argv[])
{
int ret = 0;
virConnectPtr conn = NULL;
+ char *uri = NULL;
printf("Attempting to connect to hypervisor\n");
- /* virConnectOpenAuth called here with all default parameters */
- conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0);
+ if (argc > 0) {
+ uri = argv[1];
+ }
+
+ /* virConnectOpenAuth is called here with all default parameters,
+ * except, possibly, the URI of the hypervisor. */
+ conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);
if (NULL == conn) {
ret = 1;
@@ -113,7 +119,15 @@ main(void)
goto out;
}
- printf("Connected to hypervisor\n");
+ uri = virConnectGetURI(conn);
+ if (NULL == uri) {
+ ret = 1;
+ printf("Failed to get URI for hypervisor connection\n");
+ goto disconnect;
+ }
+
+ printf("Connected to hypervisor at \"%s\"\n", uri);
+ free(uri);
if (0 != showHypervisorInfo(conn)) {
ret = 1;
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list