2005-11-10 16:12:31 +00:00
|
|
|
/*
|
2005-12-05 11:16:07 +00:00
|
|
|
* virsh.c: a Xen shell used to exercise the libvir API
|
2005-11-10 16:12:31 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* See COPYING.LIB for the License of this software
|
|
|
|
*
|
|
|
|
* Daniel Veillard <veillard@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
#include "libvir.h"
|
2005-11-10 16:12:31 +00:00
|
|
|
#include <stdio.h>
|
2005-12-01 16:35:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2005-11-10 16:12:31 +00:00
|
|
|
|
2005-12-02 14:16:21 +00:00
|
|
|
#define MAX_DOM 100
|
2005-11-10 16:12:31 +00:00
|
|
|
int errcode = 0;
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectPtr conn;
|
|
|
|
virDomainPtr dom0;
|
2005-12-02 14:16:21 +00:00
|
|
|
int ids[MAX_DOM];
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
static void printDomain(virDomainPtr dom) {
|
|
|
|
printf("id %d: name %s\n", virDomainGetID(dom), virDomainGetName(dom));
|
2005-12-02 14:16:21 +00:00
|
|
|
}
|
2005-11-10 16:12:31 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2005-12-02 14:16:21 +00:00
|
|
|
int ret, i;
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainPtr dom;
|
2005-11-10 16:12:31 +00:00
|
|
|
|
2005-12-01 16:35:42 +00:00
|
|
|
if (getuid() == 0) {
|
2005-12-05 11:16:07 +00:00
|
|
|
conn = virConnectOpen(NULL);
|
2005-12-01 16:35:42 +00:00
|
|
|
} else {
|
2005-12-05 11:16:07 +00:00
|
|
|
conn = virConnectOpenReadOnly(NULL);
|
2005-12-01 16:35:42 +00:00
|
|
|
}
|
2005-11-10 16:12:31 +00:00
|
|
|
if (conn == NULL) {
|
|
|
|
fprintf(stderr, "Failed to connect to the hypervisor\n");
|
|
|
|
errcode = 1;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-12-05 11:16:07 +00:00
|
|
|
dom0 = virDomainLookupByID(conn, 0);
|
2005-11-30 13:20:53 +00:00
|
|
|
if (dom0 == NULL) {
|
|
|
|
fprintf(stderr, "Failed to get domain 0 informations\n");
|
|
|
|
errcode = 2;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-11-10 16:12:31 +00:00
|
|
|
|
2005-12-02 14:16:21 +00:00
|
|
|
printf("Dom0: ");
|
|
|
|
printDomain(dom0);
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
ret = virConnectListDomains(conn, &ids[0], MAX_DOM);
|
2005-12-02 14:16:21 +00:00
|
|
|
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(" ");
|
2005-12-05 11:16:07 +00:00
|
|
|
dom = virDomainLookupByID(conn, ids[i]);
|
2005-12-02 14:16:21 +00:00
|
|
|
if (dom == NULL) {
|
|
|
|
printf("domain %d disapeared\n", ids[i]);
|
|
|
|
} else {
|
|
|
|
printDomain(dom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-10 16:12:31 +00:00
|
|
|
done:
|
|
|
|
if (conn != NULL) {
|
2005-12-05 11:16:07 +00:00
|
|
|
ret = virConnectClose(conn);
|
2005-11-10 16:12:31 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "Failed to connect to the hypervisor\n");
|
|
|
|
if (errcode == 0)
|
|
|
|
errcode = 1;
|
|
|
|
}
|
|
|
|
}
|
2005-11-30 13:20:53 +00:00
|
|
|
return(errcode);
|
2005-11-10 16:12:31 +00:00
|
|
|
}
|