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) {
|
2005-12-05 18:14:37 +00:00
|
|
|
virDomainInfo info;
|
|
|
|
|
2005-12-06 13:47:40 +00:00
|
|
|
printf("id %d: name %s, ", virDomainGetID(dom), virDomainGetName(dom));
|
2005-12-05 18:14:37 +00:00
|
|
|
virDomainGetInfo(dom, &info);
|
|
|
|
if (virDomainGetInfo(dom, &info) < 0) {
|
|
|
|
printf("failed to get informations\n");
|
|
|
|
} else {
|
2005-12-06 13:47:40 +00:00
|
|
|
float mem, maxMem;
|
|
|
|
|
2005-12-05 18:14:37 +00:00
|
|
|
switch (info.state) {
|
|
|
|
case VIR_DOMAIN_RUNNING:
|
|
|
|
printf("running ");
|
|
|
|
break;
|
|
|
|
case VIR_DOMAIN_BLOCKED:
|
|
|
|
printf("blocked ");
|
|
|
|
break;
|
|
|
|
case VIR_DOMAIN_PAUSED:
|
|
|
|
printf("paused ");
|
|
|
|
break;
|
|
|
|
case VIR_DOMAIN_SHUTDOWN:
|
|
|
|
printf("in shutdown ");
|
|
|
|
break;
|
|
|
|
case VIR_DOMAIN_SHUTOFF:
|
|
|
|
printf("shut off ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-12-06 16:12:49 +00:00
|
|
|
printf("%d vCPU, ", info.nrVirtCpu);
|
2005-12-06 13:47:40 +00:00
|
|
|
if (info.cpuTime != 0) {
|
|
|
|
float cpuUsed = info.cpuTime;
|
|
|
|
|
|
|
|
cpuUsed /= 1000000000;
|
2005-12-06 16:12:49 +00:00
|
|
|
printf("%.1fs time, ", cpuUsed);
|
2005-12-06 13:47:40 +00:00
|
|
|
}
|
|
|
|
mem = info.memory;
|
2005-12-06 16:50:31 +00:00
|
|
|
mem /= 1024;
|
2005-12-06 13:47:40 +00:00
|
|
|
maxMem = info.maxMem;
|
2005-12-06 16:50:31 +00:00
|
|
|
maxMem /= 1024;
|
2005-12-06 13:47:40 +00:00
|
|
|
printf("%.0f MB mem used, %.0f MB max_mem\n", mem, maxMem);
|
2005-12-05 18:14:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|