Switch LXC driver over to generic domain XML processing APIs

This commit is contained in:
Daniel P. Berrange 2008-08-13 12:50:55 +00:00
parent 763dacda97
commit 2e7a373646
8 changed files with 234 additions and 1273 deletions

View File

@ -1,3 +1,12 @@
Wed Aug 13 13:40:36 BST 2008 Daniel Berrange <berrange@redhat.com>
* src/lxc_conf.c, src/lxc_conf.h: Remove all domain XML
parsing / formatting methods, and all helpers for loading
and saving files on disk. Added capabilities data object
* src/lxc_container.c, src/lxc_container.h, src/lxc_driver.c,
src/lxc_controller.h, src/lxc_controller.c: Switch over
to use generic domain XML processing APIs.
Wed Aug 13 11:48:36 BST 2008 Daniel Berrange <berrange@redhat.com> Wed Aug 13 11:48:36 BST 2008 Daniel Berrange <berrange@redhat.com>
* configure.in: Add check for termios.h * configure.in: Add check for termios.h

File diff suppressed because it is too large Load Diff

View File

@ -29,130 +29,23 @@
#ifdef WITH_LXC #ifdef WITH_LXC
#include "internal.h" #include "internal.h"
#include "domain_conf.h"
/* Defines */ #include "capabilities.h"
#define LXC_MAX_TTY_NAME 32
#define LXC_MAX_XML_LENGTH 16384
#define LXC_MAX_ERROR_LEN 1024
#define LXC_DOMAIN_TYPE "lxc"
/* types of networks for containers */
enum lxc_net_type {
LXC_NET_NETWORK,
LXC_NET_BRIDGE
};
typedef struct __lxc_net_def lxc_net_def_t;
struct __lxc_net_def {
int type;
char *parentVeth; /* veth device in parent namespace */
char *txName; /* bridge or network name */
lxc_net_def_t *next;
};
typedef struct __lxc_mount lxc_mount_t;
struct __lxc_mount {
char source[PATH_MAX]; /* user's directory */
char target[PATH_MAX];
lxc_mount_t *next;
};
typedef struct __lxc_vm_def lxc_vm_def_t;
struct __lxc_vm_def {
unsigned char uuid[VIR_UUID_BUFLEN];
char* name;
int id;
/* init command string */
char *init;
int maxMemory;
/* mounts - list of mount structs */
int nmounts;
lxc_mount_t *mounts;
/* tty device */
char *tty;
/* network devices */
int numNets;
lxc_net_def_t *nets;
};
typedef struct __lxc_vm lxc_vm_t;
struct __lxc_vm {
int pid;
int state;
int monitor;
char configFile[PATH_MAX];
char configFileBase[PATH_MAX];
lxc_vm_def_t *def;
lxc_vm_t *next;
};
typedef struct __lxc_driver lxc_driver_t; typedef struct __lxc_driver lxc_driver_t;
struct __lxc_driver { struct __lxc_driver {
lxc_vm_t *vms; virCapsPtr caps;
int nactivevms;
int ninactivevms; virDomainObjPtr domains;
char *configDir; char *configDir;
char *autostartDir;
char *stateDir; char *stateDir;
char *logDir; char *logDir;
int have_netns; int have_netns;
}; };
/* Types and structs */
/* Inline Functions */
static inline int lxcIsActiveVM(lxc_vm_t *vm)
{
return vm->def->id != -1;
}
/* Function declarations */
lxc_vm_def_t * lxcParseVMDef(virConnectPtr conn,
const char* xmlString,
const char* fileName);
int lxcSaveVMDef(virConnectPtr conn,
lxc_driver_t *driver,
lxc_vm_t *vm,
lxc_vm_def_t *def);
int lxcLoadDriverConfig(lxc_driver_t *driver); int lxcLoadDriverConfig(lxc_driver_t *driver);
int lxcSaveConfig(virConnectPtr conn, virCapsPtr lxcCapsInit(void);
lxc_driver_t *driver,
lxc_vm_t *vm,
lxc_vm_def_t *def);
int lxcLoadContainerInfo(lxc_driver_t *driver);
int lxcLoadContainerConfigFile(lxc_driver_t *driver,
const char *file);
lxc_vm_t * lxcAssignVMDef(virConnectPtr conn,
lxc_driver_t *driver,
lxc_vm_def_t *def);
char *lxcGenerateXML(virConnectPtr conn,
lxc_driver_t *driver,
lxc_vm_t *vm,
lxc_vm_def_t *def);
lxc_vm_t *lxcFindVMByID(const lxc_driver_t *driver, int id);
lxc_vm_t *lxcFindVMByUUID(const lxc_driver_t *driver,
const unsigned char *uuid);
lxc_vm_t *lxcFindVMByName(const lxc_driver_t *driver,
const char *name);
int lxcCheckContainerProcess(lxc_vm_def_t *vm);
void lxcRemoveInactiveVM(lxc_driver_t *driver,
lxc_vm_t *vm);
void lxcFreeVMs(lxc_vm_t *vms);
void lxcFreeVM(lxc_vm_t *vm);
void lxcFreeVMDef(lxc_vm_def_t *vmdef);
int lxcDeleteConfig(virConnectPtr conn,
lxc_driver_t *driver,
const char *configFile,
const char *name);
void lxcError(virConnectPtr conn, void lxcError(virConnectPtr conn,
virDomainPtr dom, virDomainPtr dom,

View File

@ -68,7 +68,7 @@ typedef char lxc_message_t;
typedef struct __lxc_child_argv lxc_child_argv_t; typedef struct __lxc_child_argv lxc_child_argv_t;
struct __lxc_child_argv { struct __lxc_child_argv {
lxc_vm_def_t *config; virDomainDefPtr config;
unsigned int nveths; unsigned int nveths;
char **veths; char **veths;
int monitor; int monitor;
@ -85,10 +85,10 @@ struct __lxc_child_argv {
* *
* Does not return * Does not return
*/ */
static int lxcContainerExecInit(const lxc_vm_def_t *vmDef) static int lxcContainerExecInit(virDomainDefPtr vmDef)
{ {
const char *const argv[] = { const char *const argv[] = {
vmDef->init, vmDef->os.init,
NULL, NULL,
}; };
@ -269,8 +269,8 @@ static int lxcContainerChild( void *data )
{ {
int rc = -1; int rc = -1;
lxc_child_argv_t *argv = data; lxc_child_argv_t *argv = data;
lxc_vm_def_t *vmDef = argv->config; virDomainDefPtr vmDef = argv->config;
lxc_mount_t *curMount; virDomainFSDefPtr curMount;
int i; int i;
if (NULL == vmDef) { if (NULL == vmDef) {
@ -281,17 +281,20 @@ static int lxcContainerChild( void *data )
/* handle the bind mounts first before doing anything else that may */ /* handle the bind mounts first before doing anything else that may */
/* then access those mounted dirs */ /* then access those mounted dirs */
curMount = vmDef->mounts; curMount = vmDef->fss;
for (i = 0; curMount; curMount = curMount->next) { for (i = 0; curMount; curMount = curMount->next) {
rc = mount(curMount->source, // XXX fix
curMount->target, if (curMount->type != VIR_DOMAIN_FS_TYPE_MOUNT)
continue;
rc = mount(curMount->src,
curMount->dst,
NULL, NULL,
MS_BIND, MS_BIND,
NULL); NULL);
if (0 != rc) { if (0 != rc) {
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("failed to mount %s at %s for container: %s"), _("failed to mount %s at %s for container: %s"),
curMount->source, curMount->target, strerror(errno)); curMount->src, curMount->dst, strerror(errno));
return -1; return -1;
} }
} }
@ -329,7 +332,7 @@ static int lxcContainerChild( void *data )
* *
* Returns PID of container on success or -1 in case of error * Returns PID of container on success or -1 in case of error
*/ */
int lxcContainerStart(lxc_vm_def_t *def, int lxcContainerStart(virDomainDefPtr def,
unsigned int nveths, unsigned int nveths,
char **veths, char **veths,
int control, int control,

View File

@ -34,7 +34,7 @@ enum {
int lxcContainerSendContinue(int control); int lxcContainerSendContinue(int control);
int lxcContainerStart(lxc_vm_def_t *def, int lxcContainerStart(virDomainDefPtr def,
unsigned int nveths, unsigned int nveths,
char **veths, char **veths,
int control, int control,

View File

@ -306,7 +306,7 @@ static int lxcControllerCleanupInterfaces(unsigned int nveths,
static int static int
lxcControllerRun(const char *stateDir, lxcControllerRun(const char *stateDir,
lxc_vm_def_t *def, virDomainDefPtr def,
unsigned int nveths, unsigned int nveths,
char **veths, char **veths,
int monitor, int monitor,
@ -368,7 +368,7 @@ cleanup:
int lxcControllerStart(const char *stateDir, int lxcControllerStart(const char *stateDir,
lxc_vm_def_t *def, virDomainDefPtr def,
unsigned int nveths, unsigned int nveths,
char **veths, char **veths,
int monitor, int monitor,

View File

@ -29,7 +29,7 @@
#include "lxc_conf.h" #include "lxc_conf.h"
int lxcControllerStart(const char *stateDir, int lxcControllerStart(const char *stateDir,
lxc_vm_def_t *def, virDomainDefPtr def,
unsigned int nveths, unsigned int nveths,
char **veths, char **veths,
int monitor, int monitor,

View File

@ -37,7 +37,6 @@
#include <unistd.h> #include <unistd.h>
#include <wait.h> #include <wait.h>
#include "internal.h"
#include "lxc_conf.h" #include "lxc_conf.h"
#include "lxc_container.h" #include "lxc_container.h"
#include "lxc_driver.h" #include "lxc_driver.h"
@ -111,7 +110,7 @@ static virDomainPtr lxcDomainLookupByID(virConnectPtr conn,
int id) int id)
{ {
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_t *vm = lxcFindVMByID(driver, id); virDomainObjPtr vm = virDomainFindByID(driver->domains, id);
virDomainPtr dom; virDomainPtr dom;
if (!vm) { if (!vm) {
@ -131,7 +130,7 @@ static virDomainPtr lxcDomainLookupByUUID(virConnectPtr conn,
const unsigned char *uuid) const unsigned char *uuid)
{ {
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_t *vm = lxcFindVMByUUID(driver, uuid); virDomainObjPtr vm = virDomainFindByUUID(driver->domains, uuid);
virDomainPtr dom; virDomainPtr dom;
if (!vm) { if (!vm) {
@ -151,7 +150,7 @@ static virDomainPtr lxcDomainLookupByName(virConnectPtr conn,
const char *name) const char *name)
{ {
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_t *vm = lxcFindVMByName(driver, name); virDomainObjPtr vm = virDomainFindByName(driver->domains, name);
virDomainPtr dom; virDomainPtr dom;
if (!vm) { if (!vm) {
@ -167,90 +166,96 @@ static virDomainPtr lxcDomainLookupByName(virConnectPtr conn,
return dom; return dom;
} }
static int lxcListDomains(virConnectPtr conn, int *ids, int nids) static int lxcListDomains(virConnectPtr conn, int *ids, int nids) {
{
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_t *vm; virDomainObjPtr vm = driver->domains;
int numDoms = 0; int got = 0;
while (vm && got < nids) {
for (vm = driver->vms; vm && (numDoms < nids); vm = vm->next) { if (virDomainIsActive(vm)) {
if (lxcIsActiveVM(vm)) { ids[got] = vm->def->id;
ids[numDoms] = vm->def->id; got++;
numDoms++;
} }
vm = vm->next;
} }
return got;
return numDoms;
} }
static int lxcNumDomains(virConnectPtr conn) {
static int lxcNumDomains(virConnectPtr conn)
{
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
return driver->nactivevms; int n = 0;
virDomainObjPtr dom = driver->domains;
while (dom) {
if (virDomainIsActive(dom))
n++;
dom = dom->next;
}
return n;
} }
static int lxcListDefinedDomains(virConnectPtr conn, static int lxcListDefinedDomains(virConnectPtr conn,
char **const names, int nnames) char **const names, int nnames) {
{
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_t *vm; virDomainObjPtr vm = driver->domains;
int numDoms = 0; int got = 0, i;
int i; while (vm && got < nnames) {
if (!virDomainIsActive(vm)) {
for (vm = driver->vms; vm && (numDoms < nnames); vm = vm->next) { if (!(names[got] = strdup(vm->def->name))) {
if (!lxcIsActiveVM(vm)) { lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
if (!(names[numDoms] = strdup(vm->def->name))) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "names");
goto cleanup; goto cleanup;
} }
got++;
numDoms++;
} }
vm = vm->next;
} }
return got;
return numDoms;
cleanup: cleanup:
for (i = 0 ; i < numDoms ; i++) { for (i = 0 ; i < got ; i++)
VIR_FREE(names[i]); VIR_FREE(names[i]);
}
return -1; return -1;
} }
static int lxcNumDefinedDomains(virConnectPtr conn) static int lxcNumDefinedDomains(virConnectPtr conn) {
{
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
return driver->ninactivevms; int n = 0;
virDomainObjPtr dom = driver->domains;
while (dom) {
if (!virDomainIsActive(dom))
n++;
dom = dom->next;
}
return n;
} }
static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml) static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml)
{ {
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_def_t *def; virDomainDefPtr def;
lxc_vm_t *vm; virDomainObjPtr vm;
virDomainPtr dom; virDomainPtr dom;
if (!(def = lxcParseVMDef(conn, xml, NULL))) { if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
return NULL; return NULL;
}
if ((def->nets != NULL) && !(driver->have_netns)) { if ((def->nets != NULL) && !(driver->have_netns)) {
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT, lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
_("System lacks NETNS support")); _("System lacks NETNS support"));
lxcFreeVMDef(def); virDomainDefFree(def);
return NULL; return NULL;
} }
if (!(vm = lxcAssignVMDef(conn, driver, def))) { if (!(vm = virDomainAssignDef(conn, &driver->domains, def))) {
lxcFreeVMDef(def); virDomainDefFree(def);
return NULL; return NULL;
} }
if (lxcSaveVMDef(conn, driver, vm, def) < 0) { if (virDomainSaveConfig(conn,
lxcRemoveInactiveVM(driver, vm); driver->configDir,
driver->autostartDir,
vm) < 0) {
virDomainRemoveInactive(&driver->domains, vm);
return NULL; return NULL;
} }
@ -265,7 +270,7 @@ static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml)
static int lxcDomainUndefine(virDomainPtr dom) static int lxcDomainUndefine(virDomainPtr dom)
{ {
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid); virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
if (!vm) { if (!vm) {
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN, lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
@ -273,19 +278,18 @@ static int lxcDomainUndefine(virDomainPtr dom)
return -1; return -1;
} }
if (lxcIsActiveVM(vm)) { if (virDomainIsActive(vm)) {
lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR, lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
_("cannot delete active domain")); _("cannot delete active domain"));
return -1; return -1;
} }
if (lxcDeleteConfig(dom->conn, driver, vm->configFile, vm->def->name) < 0) { if (virDomainDeleteConfig(dom->conn, vm) <0)
return -1; return -1;
}
vm->configFile[0] = '\0'; vm->configFile[0] = '\0';
lxcRemoveInactiveVM(driver, vm); virDomainRemoveInactive(&driver->domains, vm);
return 0; return 0;
} }
@ -294,7 +298,7 @@ static int lxcDomainGetInfo(virDomainPtr dom,
virDomainInfoPtr info) virDomainInfoPtr info)
{ {
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid); virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
if (!vm) { if (!vm) {
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN, lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
@ -304,30 +308,23 @@ static int lxcDomainGetInfo(virDomainPtr dom,
info->state = vm->state; info->state = vm->state;
if (!lxcIsActiveVM(vm)) { if (!virDomainIsActive(vm)) {
info->cpuTime = 0; info->cpuTime = 0;
} else { } else {
info->cpuTime = 0; info->cpuTime = 0;
} }
info->maxMem = vm->def->maxMemory; info->maxMem = vm->def->maxmem;
info->memory = vm->def->maxMemory; info->memory = vm->def->memory;
info->nrVirtCpu = 1; info->nrVirtCpu = 1;
return 0; return 0;
} }
static char *lxcGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED) static char *lxcGetOSType(virDomainPtr dom)
{
/* Linux containers only run on Linux */
return strdup("linux");
}
static char *lxcDomainDumpXML(virDomainPtr dom,
int flags ATTRIBUTE_UNUSED)
{ {
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid); virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
if (!vm) { if (!vm) {
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN, lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
@ -335,7 +332,25 @@ static char *lxcDomainDumpXML(virDomainPtr dom,
return NULL; return NULL;
} }
return lxcGenerateXML(dom->conn, driver, vm, vm->def); return strdup(vm->def->os.type);
}
static char *lxcDomainDumpXML(virDomainPtr dom,
int flags)
{
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
if (!vm) {
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
_("no domain with matching uuid"));
return NULL;
}
return virDomainDefFormat(dom->conn,
(flags & VIR_DOMAIN_XML_INACTIVE) &&
vm->newDef ? vm->newDef : vm->def,
flags);
} }
@ -351,7 +366,7 @@ static char *lxcDomainDumpXML(virDomainPtr dom,
*/ */
static int lxcVMCleanup(virConnectPtr conn, static int lxcVMCleanup(virConnectPtr conn,
lxc_driver_t *driver, lxc_driver_t *driver,
lxc_vm_t * vm) virDomainObjPtr vm)
{ {
int rc = -1; int rc = -1;
int waitRc; int waitRc;
@ -383,8 +398,6 @@ static int lxcVMCleanup(virConnectPtr conn,
vm->pid = -1; vm->pid = -1;
vm->def->id = -1; vm->def->id = -1;
vm->monitor = -1; vm->monitor = -1;
driver->nactivevms--;
driver->ninactivevms++;
return rc; return rc;
} }
@ -400,12 +413,12 @@ static int lxcVMCleanup(virConnectPtr conn,
* Returns 0 on success or -1 in case of error * Returns 0 on success or -1 in case of error
*/ */
static int lxcSetupInterfaces(virConnectPtr conn, static int lxcSetupInterfaces(virConnectPtr conn,
lxc_vm_def_t *def, virDomainDefPtr def,
unsigned int *nveths, unsigned int *nveths,
char ***veths) char ***veths)
{ {
int rc = -1; int rc = -1;
lxc_net_def_t *net; virDomainNetDefPtr net;
char *bridge = NULL; char *bridge = NULL;
char parentVeth[PATH_MAX] = ""; char parentVeth[PATH_MAX] = "";
char containerVeth[PATH_MAX] = ""; char containerVeth[PATH_MAX] = "";
@ -415,8 +428,11 @@ static int lxcSetupInterfaces(virConnectPtr conn,
return -1; return -1;
for (net = def->nets; net; net = net->next) { for (net = def->nets; net; net = net->next) {
if (LXC_NET_NETWORK == net->type) { switch (net->type) {
virNetworkPtr network = virNetworkLookupByName(conn, net->txName); case VIR_DOMAIN_NET_TYPE_NETWORK:
{
virNetworkPtr network = virNetworkLookupByName(conn,
net->data.network.name);
if (!network) { if (!network) {
goto error_exit; goto error_exit;
} }
@ -424,8 +440,11 @@ static int lxcSetupInterfaces(virConnectPtr conn,
bridge = virNetworkGetBridgeName(network); bridge = virNetworkGetBridgeName(network);
virNetworkFree(network); virNetworkFree(network);
} else { break;
bridge = net->txName; }
case VIR_DOMAIN_NET_TYPE_BRIDGE:
bridge = net->data.bridge.brname;
break;
} }
DEBUG("bridge: %s", bridge); DEBUG("bridge: %s", bridge);
@ -436,8 +455,8 @@ static int lxcSetupInterfaces(virConnectPtr conn,
} }
DEBUG0("calling vethCreate()"); DEBUG0("calling vethCreate()");
if (NULL != net->parentVeth) { if (NULL != net->ifname) {
strcpy(parentVeth, net->parentVeth); strcpy(parentVeth, net->ifname);
} }
DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth); DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);
if (0 != (rc = vethCreate(parentVeth, PATH_MAX, containerVeth, PATH_MAX))) { if (0 != (rc = vethCreate(parentVeth, PATH_MAX, containerVeth, PATH_MAX))) {
@ -445,15 +464,15 @@ static int lxcSetupInterfaces(virConnectPtr conn,
_("failed to create veth device pair: %d"), rc); _("failed to create veth device pair: %d"), rc);
goto error_exit; goto error_exit;
} }
if (NULL == net->parentVeth) { if (NULL == net->ifname) {
net->parentVeth = strdup(parentVeth); net->ifname = strdup(parentVeth);
} }
if (VIR_REALLOC_N(*veths, (*nveths)+1) < 0) if (VIR_REALLOC_N(*veths, (*nveths)+1) < 0)
goto error_exit; goto error_exit;
if (((*veths)[(*nveths)++] = strdup(containerVeth)) == NULL) if (((*veths)[(*nveths)++] = strdup(containerVeth)) == NULL)
goto error_exit; goto error_exit;
if (NULL == net->parentVeth) { if (NULL == net->ifname) {
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR, lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("failed to allocate veth names")); _("failed to allocate veth names"));
goto error_exit; goto error_exit;
@ -485,7 +504,7 @@ error_exit:
static int lxcMonitorServer(virConnectPtr conn, static int lxcMonitorServer(virConnectPtr conn,
lxc_driver_t * driver, lxc_driver_t * driver,
lxc_vm_t *vm) virDomainObjPtr vm)
{ {
char *sockpath = NULL; char *sockpath = NULL;
int fd; int fd;
@ -535,7 +554,7 @@ error:
static int lxcMonitorClient(virConnectPtr conn, static int lxcMonitorClient(virConnectPtr conn,
lxc_driver_t * driver, lxc_driver_t * driver,
lxc_vm_t *vm) virDomainObjPtr vm)
{ {
char *sockpath = NULL; char *sockpath = NULL;
int fd; int fd;
@ -578,7 +597,7 @@ error:
static int lxcVmTerminate(virConnectPtr conn, static int lxcVmTerminate(virConnectPtr conn,
lxc_driver_t *driver, lxc_driver_t *driver,
lxc_vm_t *vm, virDomainObjPtr vm,
int signum) int signum)
{ {
if (signum == 0) if (signum == 0)
@ -603,7 +622,7 @@ static void lxcMonitorEvent(int fd,
void *data) void *data)
{ {
lxc_driver_t *driver = data; lxc_driver_t *driver = data;
lxc_vm_t *vm = driver->vms; virDomainObjPtr vm = driver->domains;
while (vm) { while (vm) {
if (vm->monitor == fd) if (vm->monitor == fd)
@ -632,12 +651,13 @@ static void lxcMonitorEvent(int fd,
*/ */
static int lxcVmStart(virConnectPtr conn, static int lxcVmStart(virConnectPtr conn,
lxc_driver_t * driver, lxc_driver_t * driver,
lxc_vm_t * vm) virDomainObjPtr vm)
{ {
int rc = -1; int rc = -1;
unsigned int i; unsigned int i;
int monitor; int monitor;
int parentTty; int parentTty;
char *parentTtyPath = NULL;
char *logfile = NULL; char *logfile = NULL;
int logfd = -1; int logfd = -1;
unsigned int nveths = 0; unsigned int nveths = 0;
@ -660,13 +680,19 @@ static int lxcVmStart(virConnectPtr conn,
goto cleanup; goto cleanup;
/* open parent tty */ /* open parent tty */
VIR_FREE(vm->def->tty); if (virFileOpenTty(&parentTty, &parentTtyPath, 1) < 0) {
if (virFileOpenTty(&parentTty, &vm->def->tty, 1) < 0) {
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR, lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
_("failed to allocate tty: %s"), _("failed to allocate tty: %s"),
strerror(errno)); strerror(errno));
goto cleanup; goto cleanup;
} }
if (vm->def->console &&
vm->def->console->type == VIR_DOMAIN_CHR_TYPE_PTY) {
VIR_FREE(vm->def->console->data.file.path);
vm->def->console->data.file.path = parentTtyPath;
} else {
VIR_FREE(parentTtyPath);
}
if (lxcSetupInterfaces(conn, vm->def, &nveths, &veths) != 0) if (lxcSetupInterfaces(conn, vm->def, &nveths, &veths) != 0)
goto cleanup; goto cleanup;
@ -705,8 +731,6 @@ static int lxcVmStart(virConnectPtr conn,
vm->def->id = vm->pid; vm->def->id = vm->pid;
vm->state = VIR_DOMAIN_RUNNING; vm->state = VIR_DOMAIN_RUNNING;
driver->ninactivevms--;
driver->nactivevms++;
if (virEventAddHandle(vm->monitor, if (virEventAddHandle(vm->monitor,
POLLERR | POLLHUP, POLLERR | POLLHUP,
@ -751,7 +775,7 @@ static int lxcDomainStart(virDomainPtr dom)
int rc = -1; int rc = -1;
virConnectPtr conn = dom->conn; virConnectPtr conn = dom->conn;
lxc_driver_t *driver = (lxc_driver_t *)(conn->privateData); lxc_driver_t *driver = (lxc_driver_t *)(conn->privateData);
lxc_vm_t *vm = lxcFindVMByName(driver, dom->name); virDomainObjPtr vm = virDomainFindByName(driver->domains, dom->name);
if (!vm) { if (!vm) {
lxcError(conn, dom, VIR_ERR_INVALID_DOMAIN, lxcError(conn, dom, VIR_ERR_INVALID_DOMAIN,
@ -759,6 +783,12 @@ static int lxcDomainStart(virDomainPtr dom)
goto cleanup; goto cleanup;
} }
if ((vm->def->nets != NULL) && !(driver->have_netns)) {
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
_("System lacks NETNS support"));
goto cleanup;
}
rc = lxcVmStart(conn, driver, vm); rc = lxcVmStart(conn, driver, vm);
cleanup: cleanup:
@ -780,26 +810,28 @@ lxcDomainCreateAndStart(virConnectPtr conn,
const char *xml, const char *xml,
unsigned int flags ATTRIBUTE_UNUSED) { unsigned int flags ATTRIBUTE_UNUSED) {
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData; lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
lxc_vm_t *vm; virDomainObjPtr vm;
lxc_vm_def_t *def; virDomainDefPtr def;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
if (!(def = lxcParseVMDef(conn, xml, NULL))) { if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
goto return_point;
if ((def->nets != NULL) && !(driver->have_netns)) {
virDomainDefFree(def);
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
_("System lacks NETNS support"));
goto return_point; goto return_point;
} }
if (!(vm = lxcAssignVMDef(conn, driver, def))) {
lxcFreeVMDef(def);
goto return_point;
}
if (lxcSaveVMDef(conn, driver, vm, def) < 0) { if (!(vm = virDomainAssignDef(conn, &driver->domains, def))) {
lxcRemoveInactiveVM(driver, vm); virDomainDefFree(def);
return NULL; goto return_point;
} }
if (lxcVmStart(conn, driver, vm) < 0) { if (lxcVmStart(conn, driver, vm) < 0) {
lxcRemoveInactiveVM(driver, vm); virDomainRemoveInactive(&driver->domains, vm);
goto return_point; goto return_point;
} }
@ -823,7 +855,7 @@ return_point:
static int lxcDomainShutdown(virDomainPtr dom) static int lxcDomainShutdown(virDomainPtr dom)
{ {
lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData; lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
lxc_vm_t *vm = lxcFindVMByID(driver, dom->id); virDomainObjPtr vm = virDomainFindByID(driver->domains, dom->id);
if (!vm) { if (!vm) {
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN, lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
@ -846,7 +878,7 @@ static int lxcDomainShutdown(virDomainPtr dom)
static int lxcDomainDestroy(virDomainPtr dom) static int lxcDomainDestroy(virDomainPtr dom)
{ {
lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData; lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
lxc_vm_t *vm = lxcFindVMByID(driver, dom->id); virDomainObjPtr vm = virDomainFindByID(driver->domains, dom->id);
if (!vm) { if (!vm) {
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN, lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
@ -875,7 +907,7 @@ static int lxcCheckNetNsSupport(void)
static int lxcStartup(void) static int lxcStartup(void)
{ {
uid_t uid = getuid(); uid_t uid = getuid();
lxc_vm_t *vm; virDomainObjPtr vm;
/* Check that the user is root */ /* Check that the user is root */
if (0 != uid) { if (0 != uid) {
@ -898,13 +930,21 @@ static int lxcStartup(void)
return -1; return -1;
} }
/* Call function to load the container configuration files */ if ((lxc_driver->caps = lxcCapsInit()) == NULL) {
if (lxcLoadContainerInfo(lxc_driver) < 0) {
lxcShutdown(); lxcShutdown();
return -1; return -1;
} }
vm = lxc_driver->vms; if (virDomainLoadAllConfigs(NULL,
lxc_driver->caps,
&lxc_driver->domains,
lxc_driver->configDir,
lxc_driver->autostartDir) < 0) {
lxcShutdown();
return -1;
}
vm = lxc_driver->domains;
while (vm) { while (vm) {
int rc; int rc;
if ((vm->monitor = lxcMonitorClient(NULL, lxc_driver, vm)) < 0) { if ((vm->monitor = lxcMonitorClient(NULL, lxc_driver, vm)) < 0) {
@ -923,8 +963,6 @@ static int lxcStartup(void)
if (vm->pid != 0) { if (vm->pid != 0) {
vm->def->id = vm->pid; vm->def->id = vm->pid;
vm->state = VIR_DOMAIN_RUNNING; vm->state = VIR_DOMAIN_RUNNING;
lxc_driver->ninactivevms--;
lxc_driver->nactivevms++;
} else { } else {
vm->def->id = -1; vm->def->id = -1;
close(vm->monitor); close(vm->monitor);
@ -940,6 +978,7 @@ static int lxcStartup(void)
static void lxcFreeDriver(lxc_driver_t *driver) static void lxcFreeDriver(lxc_driver_t *driver)
{ {
VIR_FREE(driver->configDir); VIR_FREE(driver->configDir);
VIR_FREE(driver->autostartDir);
VIR_FREE(driver->stateDir); VIR_FREE(driver->stateDir);
VIR_FREE(driver->logDir); VIR_FREE(driver->logDir);
VIR_FREE(driver); VIR_FREE(driver);
@ -947,10 +986,15 @@ static void lxcFreeDriver(lxc_driver_t *driver)
static int lxcShutdown(void) static int lxcShutdown(void)
{ {
virDomainObjPtr vm;
if (lxc_driver == NULL) if (lxc_driver == NULL)
return(-1); return(-1);
lxcFreeVMs(lxc_driver->vms); vm = lxc_driver->domains;
lxc_driver->vms = NULL; while (vm) {
virDomainObjPtr next = vm->next;
virDomainObjFree(vm);
vm = next;
}
lxcFreeDriver(lxc_driver); lxcFreeDriver(lxc_driver);
lxc_driver = NULL; lxc_driver = NULL;
@ -966,13 +1010,17 @@ static int lxcShutdown(void)
*/ */
static int static int
lxcActive(void) { lxcActive(void) {
virDomainObjPtr dom;
if (lxc_driver == NULL) if (lxc_driver == NULL)
return(0); return(0);
/* If we've any active networks or guests, then we
* mark this driver as active dom = lxc_driver->domains;
*/ while (dom) {
if (lxc_driver->nactivevms) if (virDomainIsActive(dom))
return 1; return 1;
dom = dom->next;
}
/* Otherwise we're happy to deal with a shutdown */ /* Otherwise we're happy to deal with a shutdown */
return 0; return 0;