[libvirt] [PATCH] trivial libvirt example code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Dave
commit 14531c0fa3680890086d61ae4d66aec525f9c9a0
Author: David Allan <dallan@xxxxxxxxxx>
Date:   Fri Jan 23 14:27:39 2009 -0500

    Added a trivial example program to illustrate connecting to the hypervisor and making some simple API calls.

diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
new file mode 100644
index 0000000..aae79b8
--- /dev/null
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -0,0 +1,137 @@
+/* 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(void)
+{
+    int ret = 0;
+    virConnectPtr conn = NULL;
+
+    printf("Attempting to connect to hypervisor\n");
+
+    /* virConnectOpenAuth called here with all default parameters */
+    conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0);
+
+    if (NULL == conn) {
+        ret = 1;
+        printf("No connection to hypervisor\n");
+        goto out;
+    }
+
+    printf("Connected to hypervisor\n");
+
+    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;
+}
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]