virsh.c 1.7 KB
Newer Older
1
/*
2
 * virsh.c: a Xen shell used to exercise the libvir API
3 4 5 6 7 8 9 10
 *
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include "libvir.h"
12
#include <stdio.h>
13 14
#include <unistd.h>
#include <sys/types.h>
15

16
#define MAX_DOM 100
17
int errcode = 0;
18 19
virConnectPtr conn;
virDomainPtr dom0;
20 21
int ids[MAX_DOM];

22 23
static void printDomain(virDomainPtr dom) {
    printf("id %d: name %s\n", virDomainGetID(dom), virDomainGetName(dom));
24
}
25 26

int main(int argc, char **argv) {
27
    int ret, i;
28
    virDomainPtr dom;
29
    
30
    if (getuid() == 0) {
31
	conn = virConnectOpen(NULL);
32
    } else {
33
	conn = virConnectOpenReadOnly(NULL);
34
    }
35 36 37 38 39
    if (conn == NULL) {
        fprintf(stderr, "Failed to connect to the hypervisor\n");
        errcode = 1;
	goto done;
    }
40
    dom0 = virDomainLookupByID(conn, 0);
41 42 43 44 45
    if (dom0 == NULL) {
        fprintf(stderr, "Failed to get domain 0 informations\n");
	errcode = 2;
	goto done;
    }
46

47 48 49
    printf("Dom0: ");
    printDomain(dom0);

50
    ret = virConnectListDomains(conn, &ids[0], MAX_DOM);
51 52 53 54 55 56 57 58 59 60
    if (ret < 0) {
        fprintf(stderr, "Failed to list active domains\n");
	errcode = 3;
	goto done;
    }
    printf("Found %d more active domains\n", ret - 1);
    for (i = 0;i < ret;i++) {
        if (ids[i] == 0)
	    continue;
        printf("  ");
61
	dom = virDomainLookupByID(conn, ids[i]);
62 63 64 65 66 67 68
	if (dom == NULL) {
	    printf("domain %d disapeared\n", ids[i]);
	} else {
	    printDomain(dom);
	}
    }
    
69 70
done:
    if (conn != NULL) {
71
        ret = virConnectClose(conn);
72 73 74 75 76 77
	if (ret != 0) {
	    fprintf(stderr, "Failed to connect to the hypervisor\n");
	    if (errcode == 0)
		errcode = 1;
	}
    }
78
    return(errcode);
79
}