[libvirt] [PATCH 1/3] Add a trivial example program

David Allan dallan at redhat.com
Thu Jan 29 03:40:30 UTC 2009


This example code illustrates connecting to the hypervisor and making some simple API calls.

Added a little code to let the user specify the URI of the hypervisor on the command line, per the suggestion of Rich Jones.
---
 examples/hellolibvirt/hellolibvirt.c |  151 ++++++++++++++++++++++++++++++++++
 1 files changed, 151 insertions(+), 0 deletions(-)
 create mode 100644 examples/hellolibvirt/hellolibvirt.c

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;
+}
-- 
1.6.0.6




More information about the libvir-list mailing list