hellolibvirt.c 4.6 KB
Newer Older
D
Daniel Veillard 已提交
1
/* This file contains trivial example code to connect to the running
2 3
 * hypervisor and gather a few bits of information about domains.
 * Similar API's exist for storage pools, networks, and interfaces. */
D
Daniel Veillard 已提交
4

J
Ján Tomko 已提交
5 6
#include <config.h>

D
Daniel Veillard 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.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);
24
    if (!hvType) {
D
Daniel Veillard 已提交
25
        ret = 1;
26 27
        printf("Failed to get hypervisor type: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
28 29 30 31 32
        goto out;
    }

    if (0 != virConnectGetVersion(conn, &hvVer)) {
        ret = 1;
33 34
        printf("Failed to get hypervisor version: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48
        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);

49
 out:
D
Daniel Veillard 已提交
50 51 52 53 54 55 56
    return ret;
}


static int
showDomains(virConnectPtr conn)
{
57
    int ret = 0, numNames, numInactiveDomains, numActiveDomains;
58
    ssize_t i;
59 60 61 62 63 64 65 66
    int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
                VIR_CONNECT_LIST_DOMAINS_INACTIVE;
    virDomainPtr *nameList = NULL;

    /* NB: The return from the virConnectNum*() APIs is only useful for
     * the current call.  A domain could be started or stopped and any
     * assumptions made purely on these return values could result in
     * unexpected results */
D
Daniel Veillard 已提交
67
    numActiveDomains = virConnectNumOfDomains(conn);
68
    if (numActiveDomains == -1) {
D
Daniel Veillard 已提交
69
        ret = 1;
70 71
        printf("Failed to get number of active domains: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
72 73 74 75
        goto out;
    }

    numInactiveDomains = virConnectNumOfDefinedDomains(conn);
76
    if (numInactiveDomains == -1) {
D
Daniel Veillard 已提交
77
        ret = 1;
78 79
        printf("Failed to get number of inactive domains: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
80 81 82 83 84 85
        goto out;
    }

    printf("There are %d active and %d inactive domains\n",
           numActiveDomains, numInactiveDomains);

86 87 88 89 90 91 92
    /* Return a list of all active and inactive domains. Using this API
     * instead of virConnectListDomains() and virConnectListDefinedDomains()
     * is preferred since it "solves" an inherit race between separated API
     * calls if domains are started or stopped between calls */
    numNames = virConnectListAllDomains(conn,
                                        &nameList,
                                        flags);
93 94 95 96 97 98 99
    if (numNames == -1) {
        ret = 1;
        printf("Failed to get a list of all domains: %s\n",
               virGetLastErrorMessage());
        goto out;
    }

100 101 102 103 104 105 106
    for (i = 0; i < numNames; i++) {
        int active = virDomainIsActive(nameList[i]);
        printf("  %8s (%s)\n",
               virDomainGetName(nameList[i]),
               (active == 1 ? "active" : "non-active"));
        /* must free the returned named per the API documentation */
        virDomainFree(nameList[i]);
D
Daniel Veillard 已提交
107
    }
108
    free(nameList);
D
Daniel Veillard 已提交
109

110
 out:
D
Daniel Veillard 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    return ret;
}


int
main(int argc, char *argv[])
{
    int ret = 0;
    virConnectPtr conn;
    char *uri;

    printf("Attempting to connect to hypervisor\n");

    uri = (argc > 0 ? argv[1] : NULL);

    /* virConnectOpenAuth is called here with all default parameters,
     * except, possibly, the URI of the hypervisor. */
    conn = virConnectOpenAuth(uri, virConnectAuthPtrDefault, 0);

130
    if (!conn) {
D
Daniel Veillard 已提交
131
        ret = 1;
132 133
        printf("No connection to hypervisor: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
134 135 136 137
        goto out;
    }

    uri = virConnectGetURI(conn);
138
    if (!uri) {
D
Daniel Veillard 已提交
139
        ret = 1;
140 141
        printf("Failed to get URI for hypervisor connection: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        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;
    }

158
 disconnect:
D
Daniel Veillard 已提交
159
    if (0 != virConnectClose(conn)) {
160 161
        printf("Failed to disconnect from hypervisor: %s\n",
               virGetLastErrorMessage());
D
Daniel Veillard 已提交
162 163 164 165 166
        ret = 1;
    } else {
        printf("Disconnected from hypervisor\n");
    }

167
 out:
D
Daniel Veillard 已提交
168 169
    return ret;
}