2007-02-14 01:40:09 +00:00
|
|
|
/*
|
|
|
|
* driver.c: core driver methods for managing qemu guests
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006, 2007 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2006 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2007-02-14 15:42:55 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2007-02-14 01:40:09 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <libvirt/virterror.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "driver.h"
|
2007-02-14 15:41:03 +00:00
|
|
|
#include "conf.h"
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
void qemudReportError(struct qemud_server *server,
|
|
|
|
int code, const char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
server->errorCode = code;
|
|
|
|
if (fmt) {
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(server->errorMessage, QEMUD_MAX_ERROR_LEN-1, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
} else {
|
|
|
|
server->errorMessage[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudMonitorCommand(struct qemud_server *server ATTRIBUTE_UNUSED,
|
|
|
|
struct qemud_vm *vm,
|
|
|
|
const char *cmd,
|
|
|
|
char **reply) {
|
|
|
|
int size = 0;
|
|
|
|
char *buf = NULL;
|
2007-03-05 17:15:20 +00:00
|
|
|
|
2007-02-14 01:40:09 +00:00
|
|
|
if (write(vm->monitor, cmd, strlen(cmd)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = NULL;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct pollfd fd = { vm->monitor, POLLIN | POLLERR | POLLHUP, 0 };
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
/* Read all the data QEMU has sent thus far */
|
|
|
|
for (;;) {
|
|
|
|
char data[1024];
|
|
|
|
int got = read(vm->monitor, data, sizeof(data));
|
2007-03-05 17:15:20 +00:00
|
|
|
|
|
|
|
if (got == 0) {
|
|
|
|
if (buf)
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-02-14 01:40:09 +00:00
|
|
|
if (got < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
|
2007-03-05 17:15:20 +00:00
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2007-02-14 01:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(buf = realloc(buf, size+got+1)))
|
|
|
|
return -1;
|
|
|
|
memmove(buf+size, data, got);
|
|
|
|
buf[size+got] = '\0';
|
|
|
|
size += got;
|
|
|
|
}
|
|
|
|
if (buf)
|
2007-02-16 18:30:55 +00:00
|
|
|
qemudDebug("Mon [%s]", buf);
|
2007-02-14 01:40:09 +00:00
|
|
|
/* Look for QEMU prompt to indicate completion */
|
2007-03-05 17:15:20 +00:00
|
|
|
if (buf && ((tmp = strstr(buf, "\n(qemu) ")) != NULL)) {
|
2007-02-14 01:40:09 +00:00
|
|
|
tmp[0] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pollagain:
|
|
|
|
/* Need to wait for more data */
|
|
|
|
if (poll(&fd, 1, -1) < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto pollagain;
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = buf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudGetMemInfo(unsigned int *memory) {
|
|
|
|
FILE *meminfo = fopen("/proc/meminfo", "r");
|
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
*memory = 0;
|
|
|
|
|
|
|
|
if (!meminfo) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX NUMA and hyperthreads ? */
|
|
|
|
while (fgets(line, sizeof(line), meminfo) != NULL) {
|
|
|
|
if (!strncmp(line, "MemTotal:", 9)) {
|
|
|
|
*memory = (unsigned int)strtol(line + 10, NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(meminfo);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudGetCPUInfo(unsigned int *cpus, unsigned int *mhz,
|
|
|
|
unsigned int *nodes, unsigned int *sockets,
|
|
|
|
unsigned int *cores, unsigned int *threads) {
|
|
|
|
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
|
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
*cpus = 0;
|
|
|
|
*mhz = 0;
|
|
|
|
*nodes = *sockets = *cores = *threads = 1;
|
|
|
|
|
|
|
|
if (!cpuinfo) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX NUMA and hyperthreads ? */
|
|
|
|
while (fgets(line, sizeof(line), cpuinfo) != NULL) {
|
|
|
|
if (!strncmp(line, "processor\t", 10)) { /* aka a single logical CPU */
|
|
|
|
(*cpus)++;
|
|
|
|
} else if (!strncmp(line, "cpu MHz\t", 8)) {
|
|
|
|
char *offset = index(line, ':');
|
|
|
|
if (!offset)
|
|
|
|
continue;
|
|
|
|
offset++;
|
|
|
|
if (!*offset)
|
|
|
|
continue;
|
|
|
|
*mhz = (unsigned int)strtol(offset, NULL, 10);
|
|
|
|
} else if (!strncmp(line, "physical id\t", 12)) { /* aka socket */
|
|
|
|
unsigned int id;
|
|
|
|
char *offset = index(line, ':');
|
|
|
|
if (!offset)
|
|
|
|
continue;
|
|
|
|
offset++;
|
|
|
|
if (!*offset)
|
|
|
|
continue;
|
|
|
|
id = (unsigned int)strtol(offset, NULL, 10);
|
|
|
|
if ((id+1) > *sockets)
|
|
|
|
*sockets = (id + 1);
|
|
|
|
} else if (!strncmp(line, "cpu cores\t", 9)) { /* aka cores */
|
|
|
|
unsigned int id;
|
|
|
|
char *offset = index(line, ':');
|
|
|
|
if (!offset)
|
|
|
|
continue;
|
|
|
|
offset++;
|
|
|
|
if (!*offset)
|
|
|
|
continue;
|
|
|
|
id = (unsigned int)strtol(offset, NULL, 10);
|
|
|
|
if (id > *cores)
|
|
|
|
*cores = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(cpuinfo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
|
|
|
|
char proc[PATH_MAX];
|
|
|
|
FILE *pidinfo;
|
2007-04-15 19:58:44 +00:00
|
|
|
unsigned long long usertime, systime;
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
if (snprintf(proc, sizeof(proc), "/proc/%d/stat", pid) >= (int)sizeof(proc)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pidinfo = fopen(proc, "r"))) {
|
|
|
|
/*printf("cannnot read pid info");*/
|
|
|
|
/* VM probably shut down, so fake 0 */
|
|
|
|
*cpuTime = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-15 19:58:44 +00:00
|
|
|
if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &usertime, &systime) != 2) {
|
2007-02-16 18:30:55 +00:00
|
|
|
qemudDebug("not enough arg");
|
2007-02-14 01:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We got jiffies
|
|
|
|
* We want nanoseconds
|
|
|
|
* _SC_CLK_TCK is jiffies per second
|
|
|
|
* So calulate thus....
|
|
|
|
*/
|
2007-04-15 19:58:44 +00:00
|
|
|
*cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime) / (unsigned long long)sysconf(_SC_CLK_TCK);
|
2007-02-14 01:40:09 +00:00
|
|
|
|
2007-04-15 19:58:44 +00:00
|
|
|
qemudDebug("Got %llu %llu %llu", usertime, systime, *cpuTime);
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
fclose(pidinfo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct qemud_vm *qemudFindVMByID(const struct qemud_server *server, int id) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_vm *vm = server->vms;
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
while (vm) {
|
2007-02-23 08:39:49 +00:00
|
|
|
if (qemudIsActiveVM(vm) && vm->id == id)
|
2007-02-14 01:40:09 +00:00
|
|
|
return vm;
|
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct qemud_vm *qemudFindVMByUUID(const struct qemud_server *server,
|
|
|
|
const unsigned char *uuid) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_vm *vm = server->vms;
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
while (vm) {
|
2007-02-14 17:05:55 +00:00
|
|
|
if (!memcmp(vm->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
|
2007-02-14 01:40:09 +00:00
|
|
|
return vm;
|
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct qemud_vm *qemudFindVMByName(const struct qemud_server *server,
|
|
|
|
const char *name) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_vm *vm = server->vms;
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
while (vm) {
|
2007-02-14 17:05:55 +00:00
|
|
|
if (!strcmp(vm->def->name, name))
|
2007-02-14 01:40:09 +00:00
|
|
|
return vm;
|
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudGetVersion(struct qemud_server *server) {
|
2007-02-23 17:15:18 +00:00
|
|
|
if (qemudExtractVersion(server) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2007-02-14 01:40:09 +00:00
|
|
|
return server->qemuVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudListDomains(struct qemud_server *server, int *ids, int nids) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_vm *vm = server->vms;
|
2007-02-14 01:40:09 +00:00
|
|
|
int got = 0;
|
|
|
|
while (vm && got < nids) {
|
2007-02-23 08:39:49 +00:00
|
|
|
if (qemudIsActiveVM(vm)) {
|
|
|
|
ids[got] = vm->id;
|
|
|
|
got++;
|
|
|
|
}
|
2007-02-14 01:40:09 +00:00
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
int qemudNumDomains(struct qemud_server *server) {
|
|
|
|
return server->nactivevms;
|
|
|
|
}
|
|
|
|
struct qemud_vm *qemudDomainCreate(struct qemud_server *server, const char *xml) {
|
2007-02-23 08:48:02 +00:00
|
|
|
|
|
|
|
struct qemud_vm_def *def;
|
2007-02-14 01:40:09 +00:00
|
|
|
struct qemud_vm *vm;
|
|
|
|
|
2007-02-23 08:48:02 +00:00
|
|
|
if (!(def = qemudParseVMDef(server, xml, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(vm = qemudAssignVMDef(server, def))) {
|
|
|
|
qemudFreeVMDef(def);
|
2007-02-14 01:40:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:41:23 +00:00
|
|
|
if (qemudStartVMDaemon(server, vm) < 0) {
|
2007-02-23 08:48:02 +00:00
|
|
|
qemudRemoveInactiveVM(server, vm);
|
2007-02-14 01:40:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainSuspend(struct qemud_server *server, int id) {
|
|
|
|
char *info;
|
|
|
|
struct qemud_vm *vm = qemudFindVMByID(server, id);
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-02-23 08:39:49 +00:00
|
|
|
if (!qemudIsActiveVM(vm)) {
|
2007-02-14 01:40:09 +00:00
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not running");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-05 17:15:20 +00:00
|
|
|
if (vm->state == QEMUD_STATE_PAUSED)
|
|
|
|
return 0;
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
if (qemudMonitorCommand(server, vm, "stop\n", &info) < 0) {
|
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "suspend operation failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-05 17:15:20 +00:00
|
|
|
vm->state = QEMUD_STATE_PAUSED;
|
2007-02-16 18:30:55 +00:00
|
|
|
qemudDebug("Reply %s", info);
|
2007-02-14 01:40:09 +00:00
|
|
|
free(info);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainResume(struct qemud_server *server, int id) {
|
|
|
|
char *info;
|
|
|
|
struct qemud_vm *vm = qemudFindVMByID(server, id);
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-02-23 08:39:49 +00:00
|
|
|
if (!qemudIsActiveVM(vm)) {
|
2007-02-14 01:40:09 +00:00
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not running");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-05 17:15:20 +00:00
|
|
|
if (vm->state == QEMUD_STATE_RUNNING)
|
|
|
|
return 0;
|
2007-02-14 01:40:09 +00:00
|
|
|
if (qemudMonitorCommand(server, vm, "cont\n", &info) < 0) {
|
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "resume operation failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-05 17:15:20 +00:00
|
|
|
vm->state = QEMUD_STATE_RUNNING;
|
2007-02-16 18:30:55 +00:00
|
|
|
qemudDebug("Reply %s", info);
|
2007-02-14 01:40:09 +00:00
|
|
|
free(info);
|
2007-03-05 17:15:20 +00:00
|
|
|
return 0;
|
2007-02-14 01:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainDestroy(struct qemud_server *server, int id) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByID(server, id);
|
2007-02-23 08:41:23 +00:00
|
|
|
|
2007-02-14 01:40:09 +00:00
|
|
|
if (!vm) {
|
2007-02-23 08:41:23 +00:00
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
"no domain with matching id %d", id);
|
2007-02-14 01:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-02-23 08:41:23 +00:00
|
|
|
|
|
|
|
return qemudShutdownVMDaemon(server, vm);
|
2007-02-14 01:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainGetInfo(struct qemud_server *server, const unsigned char *uuid,
|
|
|
|
int *runstate,
|
|
|
|
unsigned long long *cputime,
|
|
|
|
unsigned long *maxmem,
|
|
|
|
unsigned long *memory,
|
|
|
|
unsigned int *nrVirtCpu) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-03-05 17:15:20 +00:00
|
|
|
*runstate = vm->state;
|
2007-02-14 01:40:09 +00:00
|
|
|
|
2007-02-23 08:39:49 +00:00
|
|
|
if (!qemudIsActiveVM(vm)) {
|
2007-02-14 01:40:09 +00:00
|
|
|
*cputime = 0;
|
|
|
|
} else {
|
|
|
|
if (qemudGetProcessInfo(cputime, vm->pid) < 0) {
|
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "cannot read cputime for domain");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-14 17:05:55 +00:00
|
|
|
*maxmem = vm->def->maxmem;
|
|
|
|
*memory = vm->def->memory;
|
|
|
|
*nrVirtCpu = vm->def->vcpus;
|
2007-02-14 01:40:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainSave(struct qemud_server *server, int id,
|
|
|
|
const char *path ATTRIBUTE_UNUSED) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByID(server, id);
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-02-23 08:39:49 +00:00
|
|
|
if (!qemudIsActiveVM(vm)) {
|
2007-02-14 01:40:09 +00:00
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not running");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "save is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainRestore(struct qemud_server *server,
|
|
|
|
const char *path ATTRIBUTE_UNUSED) {
|
|
|
|
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "restore is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudDomainDumpXML(struct qemud_server *server, const unsigned char *uuid, char *xml, int xmllen) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
|
|
|
|
char *vmxml;
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:48:02 +00:00
|
|
|
vmxml = qemudGenerateXML(server, vm, vm->def, 1);
|
2007-02-14 01:40:09 +00:00
|
|
|
if (!vmxml)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
strncpy(xml, vmxml, xmllen);
|
|
|
|
xml[xmllen-1] = '\0';
|
|
|
|
|
2007-03-05 12:34:27 +00:00
|
|
|
free(vmxml);
|
2007-02-26 14:21:21 +00:00
|
|
|
|
2007-02-14 01:40:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudListDefinedDomains(struct qemud_server *server, char *const*names, int nnames) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_vm *vm = server->vms;
|
2007-02-14 01:40:09 +00:00
|
|
|
int got = 0;
|
|
|
|
while (vm && got < nnames) {
|
2007-02-23 08:39:49 +00:00
|
|
|
if (!qemudIsActiveVM(vm)) {
|
|
|
|
strncpy(names[got], vm->def->name, QEMUD_MAX_NAME_LEN-1);
|
|
|
|
names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
|
|
|
|
got++;
|
|
|
|
}
|
2007-02-14 01:40:09 +00:00
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qemudNumDefinedDomains(struct qemud_server *server) {
|
|
|
|
return server->ninactivevms;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-23 08:41:23 +00:00
|
|
|
struct qemud_vm *qemudDomainStart(struct qemud_server *server, const unsigned char *uuid) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
"no domain with matching uuid");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return qemudStartVMDaemon(server, vm) < 0 ? NULL : vm;
|
2007-02-14 01:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct qemud_vm *qemudDomainDefine(struct qemud_server *server, const char *xml) {
|
2007-02-23 08:48:02 +00:00
|
|
|
struct qemud_vm_def *def;
|
|
|
|
struct qemud_vm *vm;
|
|
|
|
|
|
|
|
if (!(def = qemudParseVMDef(server, xml, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(vm = qemudAssignVMDef(server, def))) {
|
|
|
|
qemudFreeVMDef(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemudSaveVMDef(server, vm, def) < 0) {
|
|
|
|
qemudRemoveInactiveVM(server, vm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm;
|
2007-02-14 01:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudDomainUndefine(struct qemud_server *server, const unsigned char *uuid) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:39:49 +00:00
|
|
|
if (qemudIsActiveVM(vm)) {
|
2007-02-14 01:40:09 +00:00
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot delete active domain");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-14 17:05:55 +00:00
|
|
|
if (qemudDeleteConfig(server, vm->configFile, vm->def->name) < 0)
|
2007-02-14 01:40:09 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-02-23 09:07:41 +00:00
|
|
|
if (unlink(vm->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR)
|
|
|
|
qemudLog(QEMUD_WARN, "Failed to delete autostart link '%s': %s",
|
|
|
|
vm->autostartLink, strerror(errno));
|
|
|
|
|
2007-02-14 15:58:06 +00:00
|
|
|
vm->configFile[0] = '\0';
|
2007-02-23 09:07:41 +00:00
|
|
|
vm->autostartLink[0] = '\0';
|
2007-02-14 15:58:06 +00:00
|
|
|
|
2007-02-23 08:48:02 +00:00
|
|
|
qemudRemoveInactiveVM(server, vm);
|
2007-02-14 01:40:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-23 09:03:25 +00:00
|
|
|
int qemudDomainGetAutostart(struct qemud_server *server,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
int *autostart) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*autostart = vm->autostart;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudDomainSetAutostart(struct qemud_server *server,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
int autostart) {
|
|
|
|
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
autostart = (autostart != 0);
|
|
|
|
|
|
|
|
if (vm->autostart == autostart)
|
|
|
|
return 0;
|
|
|
|
|
2007-02-23 09:07:41 +00:00
|
|
|
if (autostart) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = qemudEnsureDir(server->autostartDir))) {
|
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"cannot create autostart directory %s: %s",
|
|
|
|
server->autostartDir, strerror(err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symlink(vm->configFile, vm->autostartLink) < 0) {
|
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"Failed to create symlink '%s' to '%s': %s",
|
|
|
|
vm->autostartLink, vm->configFile, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink(vm->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
|
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"Failed to delete symlink '%s': %s",
|
|
|
|
vm->autostartLink, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-23 09:03:25 +00:00
|
|
|
vm->autostart = autostart;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 15:54:10 +00:00
|
|
|
struct qemud_network *qemudFindNetworkByUUID(const struct qemud_server *server,
|
|
|
|
const unsigned char *uuid) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_network *network = server->networks;
|
2007-02-14 15:58:06 +00:00
|
|
|
|
|
|
|
while (network) {
|
2007-02-15 16:00:16 +00:00
|
|
|
if (!memcmp(network->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
|
2007-02-14 15:58:06 +00:00
|
|
|
return network;
|
|
|
|
network = network->next;
|
|
|
|
}
|
|
|
|
|
2007-02-14 15:54:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct qemud_network *qemudFindNetworkByName(const struct qemud_server *server,
|
|
|
|
const char *name) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_network *network = server->networks;
|
2007-02-14 15:58:06 +00:00
|
|
|
|
|
|
|
while (network) {
|
2007-02-15 16:00:16 +00:00
|
|
|
if (!strcmp(network->def->name, name))
|
2007-02-14 15:58:06 +00:00
|
|
|
return network;
|
|
|
|
network = network->next;
|
|
|
|
}
|
|
|
|
|
2007-02-14 15:54:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudNumNetworks(struct qemud_server *server) {
|
2007-02-14 15:58:06 +00:00
|
|
|
return server->nactivenetworks;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudListNetworks(struct qemud_server *server, char *const*names, int nnames) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_network *network = server->networks;
|
2007-02-14 15:58:06 +00:00
|
|
|
int got = 0;
|
|
|
|
while (network && got < nnames) {
|
2007-02-23 08:39:49 +00:00
|
|
|
if (qemudIsActiveNetwork(network)) {
|
|
|
|
strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
|
|
|
|
names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
|
|
|
|
got++;
|
|
|
|
}
|
2007-02-14 15:58:06 +00:00
|
|
|
network = network->next;
|
|
|
|
}
|
|
|
|
return got;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudNumDefinedNetworks(struct qemud_server *server) {
|
2007-02-14 15:58:06 +00:00
|
|
|
return server->ninactivenetworks;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudListDefinedNetworks(struct qemud_server *server, char *const*names, int nnames) {
|
2007-02-23 08:39:49 +00:00
|
|
|
struct qemud_network *network = server->networks;
|
2007-02-14 15:58:06 +00:00
|
|
|
int got = 0;
|
|
|
|
while (network && got < nnames) {
|
2007-02-23 08:39:49 +00:00
|
|
|
if (!qemudIsActiveNetwork(network)) {
|
|
|
|
strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
|
|
|
|
names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
|
|
|
|
got++;
|
|
|
|
}
|
2007-02-14 15:58:06 +00:00
|
|
|
network = network->next;
|
|
|
|
}
|
|
|
|
return got;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct qemud_network *qemudNetworkCreate(struct qemud_server *server, const char *xml) {
|
2007-02-23 08:48:02 +00:00
|
|
|
struct qemud_network_def *def;
|
2007-02-14 15:58:06 +00:00
|
|
|
struct qemud_network *network;
|
|
|
|
|
2007-02-23 08:48:02 +00:00
|
|
|
if (!(def = qemudParseNetworkDef(server, xml, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(network = qemudAssignNetworkDef(server, def))) {
|
|
|
|
qemudFreeNetworkDef(def);
|
2007-02-14 15:58:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:41:23 +00:00
|
|
|
if (qemudStartNetworkDaemon(server, network) < 0) {
|
2007-02-23 08:48:02 +00:00
|
|
|
qemudRemoveInactiveNetwork(server, network);
|
2007-02-14 15:58:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return network;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct qemud_network *qemudNetworkDefine(struct qemud_server *server, const char *xml) {
|
2007-02-23 08:48:02 +00:00
|
|
|
struct qemud_network_def *def;
|
|
|
|
struct qemud_network *network;
|
|
|
|
|
|
|
|
if (!(def = qemudParseNetworkDef(server, xml, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(network = qemudAssignNetworkDef(server, def))) {
|
|
|
|
qemudFreeNetworkDef(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemudSaveNetworkDef(server, network, def) < 0) {
|
|
|
|
qemudRemoveInactiveNetwork(server, network);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return network;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudNetworkUndefine(struct qemud_server *server, const unsigned char *uuid) {
|
2007-02-14 15:58:06 +00:00
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no network with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-15 16:00:16 +00:00
|
|
|
if (qemudDeleteConfig(server, network->configFile, network->def->name) < 0)
|
2007-02-14 15:58:06 +00:00
|
|
|
return -1;
|
|
|
|
|
2007-02-23 09:07:41 +00:00
|
|
|
if (unlink(network->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR)
|
|
|
|
qemudLog(QEMUD_WARN, "Failed to delete autostart link '%s': %s",
|
|
|
|
network->autostartLink, strerror(errno));
|
|
|
|
|
2007-02-14 15:58:06 +00:00
|
|
|
network->configFile[0] = '\0';
|
2007-02-23 09:07:41 +00:00
|
|
|
network->autostartLink[0] = '\0';
|
2007-02-14 15:58:06 +00:00
|
|
|
|
2007-02-23 08:48:02 +00:00
|
|
|
qemudRemoveInactiveNetwork(server, network);
|
2007-02-14 15:58:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 08:41:23 +00:00
|
|
|
struct qemud_network *qemudNetworkStart(struct qemud_server *server, const unsigned char *uuid) {
|
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_NETWORK,
|
|
|
|
"no network with matching uuid");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return qemudStartNetworkDaemon(server, network) < 0 ? NULL : network;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudNetworkDestroy(struct qemud_server *server, const unsigned char *uuid) {
|
2007-02-14 15:58:06 +00:00
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
2007-02-23 08:41:23 +00:00
|
|
|
|
2007-02-14 15:58:06 +00:00
|
|
|
if (!network) {
|
2007-02-23 08:41:23 +00:00
|
|
|
qemudReportError(server, VIR_ERR_INVALID_NETWORK,
|
|
|
|
"no network with matching uuid");
|
2007-02-14 15:58:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:41:23 +00:00
|
|
|
return qemudShutdownNetworkDaemon(server, network);
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemudNetworkDumpXML(struct qemud_server *server, const unsigned char *uuid, char *xml, int xmllen) {
|
2007-02-14 15:58:06 +00:00
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
|
|
|
char *networkxml;
|
|
|
|
if (!network) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-23 08:48:02 +00:00
|
|
|
networkxml = qemudGenerateNetworkXML(server, network, network->def);
|
2007-02-14 15:58:06 +00:00
|
|
|
if (!networkxml)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
strncpy(xml, networkxml, xmllen);
|
|
|
|
xml[xmllen-1] = '\0';
|
|
|
|
|
2007-03-05 12:34:27 +00:00
|
|
|
free(networkxml);
|
2007-02-26 14:21:21 +00:00
|
|
|
|
2007-02-14 15:58:06 +00:00
|
|
|
return 0;
|
2007-02-14 15:54:10 +00:00
|
|
|
}
|
2007-02-14 01:40:09 +00:00
|
|
|
|
2007-02-14 16:20:38 +00:00
|
|
|
int qemudNetworkGetBridgeName(struct qemud_server *server, const unsigned char *uuid, char *ifname, int ifnamelen) {
|
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_NETWORK, "no network with matching id");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(ifname, network->bridge, ifnamelen);
|
|
|
|
ifname[ifnamelen-1] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-23 09:03:25 +00:00
|
|
|
int qemudNetworkGetAutostart(struct qemud_server *server,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
int *autostart) {
|
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*autostart = network->autostart;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemudNetworkSetAutostart(struct qemud_server *server,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
int autostart) {
|
|
|
|
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
|
|
|
|
|
|
|
|
if (!network) {
|
|
|
|
qemudReportError(server, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
autostart = (autostart != 0);
|
|
|
|
|
|
|
|
if (network->autostart == autostart)
|
|
|
|
return 0;
|
|
|
|
|
2007-02-23 09:07:41 +00:00
|
|
|
if (autostart) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = qemudEnsureDir(server->networkAutostartDir))) {
|
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"cannot create autostart directory %s: %s",
|
|
|
|
server->networkAutostartDir, strerror(err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symlink(network->configFile, network->autostartLink) < 0) {
|
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"Failed to create symlink '%s' to '%s': %s",
|
|
|
|
network->autostartLink, network->configFile, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink(network->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
|
|
|
|
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"Failed to delete symlink '%s': %s",
|
|
|
|
network->autostartLink, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-23 09:03:25 +00:00
|
|
|
network->autostart = autostart;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 01:40:09 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* c-indent-level: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 4
|
|
|
|
* End:
|
|
|
|
*/
|