2008-03-21 15:03:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright IBM Corp. 2008
|
|
|
|
*
|
|
|
|
* lxc_driver.c: linux container driver functions
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* David L. Leskovec <dlesko at linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef WITH_LXC
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
#include <fcntl.h>
|
2008-03-21 15:03:37 +00:00
|
|
|
#include <sched.h>
|
|
|
|
#include <sys/utsname.h>
|
2008-05-09 07:16:30 +00:00
|
|
|
#include <stdbool.h>
|
2008-03-21 15:03:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2008-04-10 07:30:52 +00:00
|
|
|
#include <termios.h>
|
2008-03-21 15:03:37 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wait.h>
|
|
|
|
|
|
|
|
#include "lxc_conf.h"
|
2008-04-10 07:30:52 +00:00
|
|
|
#include "lxc_container.h"
|
2008-03-21 15:03:37 +00:00
|
|
|
#include "lxc_driver.h"
|
2008-08-13 10:25:34 +00:00
|
|
|
#include "lxc_controller.h"
|
2008-03-21 15:03:37 +00:00
|
|
|
#include "driver.h"
|
|
|
|
#include "internal.h"
|
2008-06-09 22:51:32 +00:00
|
|
|
#include "memory.h"
|
2008-04-10 07:30:52 +00:00
|
|
|
#include "util.h"
|
2008-06-06 11:09:57 +00:00
|
|
|
#include "memory.h"
|
2008-06-26 16:09:48 +00:00
|
|
|
#include "bridge.h"
|
|
|
|
#include "qemu_conf.h"
|
|
|
|
#include "veth.h"
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
/* debug macros */
|
|
|
|
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
|
|
|
|
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
|
|
|
|
static int lxcStartup(void);
|
|
|
|
static int lxcShutdown(void);
|
2008-03-31 12:02:12 +00:00
|
|
|
static lxc_driver_t *lxc_driver = NULL;
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
|
|
|
|
static const char *lxcProbe(void)
|
|
|
|
{
|
2008-08-13 10:25:34 +00:00
|
|
|
if (lxcContainerAvailable(0) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return("lxc:///");
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static virDrvOpenStatus lxcOpen(virConnectPtr conn,
|
|
|
|
xmlURIPtr uri,
|
|
|
|
virConnectAuthPtr auth ATTRIBUTE_UNUSED,
|
|
|
|
int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
uid_t uid = getuid();
|
|
|
|
|
|
|
|
/* Check that the user is root */
|
|
|
|
if (0 != uid) {
|
|
|
|
goto declineConnection;
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
if (lxc_driver == NULL)
|
|
|
|
goto declineConnection;
|
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
/* Verify uri was specified */
|
|
|
|
if ((NULL == uri) || (NULL == uri->scheme)) {
|
|
|
|
goto declineConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the uri scheme is lxc */
|
|
|
|
if (STRNEQ(uri->scheme, "lxc")) {
|
|
|
|
goto declineConnection;
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
conn->privateData = lxc_driver;
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
return VIR_DRV_OPEN_SUCCESS;
|
|
|
|
|
|
|
|
declineConnection:
|
|
|
|
return VIR_DRV_OPEN_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcClose(virConnectPtr conn)
|
|
|
|
{
|
2008-03-27 09:34:06 +00:00
|
|
|
conn->privateData = NULL;
|
|
|
|
return 0;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static virDomainPtr lxcDomainLookupByID(virConnectPtr conn,
|
|
|
|
int id)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByID(driver, id);
|
|
|
|
virDomainPtr dom;
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
|
|
|
|
if (dom) {
|
|
|
|
dom->id = vm->def->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virDomainPtr lxcDomainLookupByUUID(virConnectPtr conn,
|
|
|
|
const unsigned char *uuid)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByUUID(driver, uuid);
|
|
|
|
virDomainPtr dom;
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
|
|
|
|
if (dom) {
|
|
|
|
dom->id = vm->def->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virDomainPtr lxcDomainLookupByName(virConnectPtr conn,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByName(driver, name);
|
|
|
|
virDomainPtr dom;
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_DOMAIN, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
|
|
|
|
if (dom) {
|
|
|
|
dom->id = vm->def->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcListDomains(virConnectPtr conn, int *ids, int nids)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_t *vm;
|
|
|
|
int numDoms = 0;
|
|
|
|
|
|
|
|
for (vm = driver->vms; vm && (numDoms < nids); vm = vm->next) {
|
|
|
|
if (lxcIsActiveVM(vm)) {
|
|
|
|
ids[numDoms] = vm->def->id;
|
|
|
|
numDoms++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numDoms;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcNumDomains(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
return driver->nactivevms;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcListDefinedDomains(virConnectPtr conn,
|
|
|
|
char **const names, int nnames)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_t *vm;
|
|
|
|
int numDoms = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (vm = driver->vms; vm && (numDoms < nnames); vm = vm->next) {
|
|
|
|
if (!lxcIsActiveVM(vm)) {
|
|
|
|
if (!(names[numDoms] = strdup(vm->def->name))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "names");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
numDoms++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return numDoms;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for (i = 0 ; i < numDoms ; i++) {
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(names[i]);
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int lxcNumDefinedDomains(virConnectPtr conn)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
return driver->ninactivevms;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_def_t *def;
|
|
|
|
lxc_vm_t *vm;
|
|
|
|
virDomainPtr dom;
|
|
|
|
|
|
|
|
if (!(def = lxcParseVMDef(conn, xml, NULL))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-26 16:08:59 +00:00
|
|
|
if ((def->nets != NULL) && !(driver->have_netns)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
|
|
|
|
_("System lacks NETNS support"));
|
|
|
|
lxcFreeVMDef(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
if (!(vm = lxcAssignVMDef(conn, driver, def))) {
|
|
|
|
lxcFreeVMDef(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lxcSaveVMDef(conn, driver, vm, def) < 0) {
|
|
|
|
lxcRemoveInactiveVM(driver, vm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
|
|
|
|
if (dom) {
|
|
|
|
dom->id = vm->def->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcDomainUndefine(virDomainPtr dom)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with matching uuid"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lxcIsActiveVM(vm)) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot delete active domain"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lxcDeleteConfig(dom->conn, driver, vm->configFile, vm->def->name) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vm->configFile[0] = '\0';
|
|
|
|
|
2008-06-05 06:03:00 +00:00
|
|
|
lxcDeleteTtyPidFile(vm);
|
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
lxcRemoveInactiveVM(driver, vm);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcDomainGetInfo(virDomainPtr dom,
|
|
|
|
virDomainInfoPtr info)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with matching uuid"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->state = vm->state;
|
|
|
|
|
|
|
|
if (!lxcIsActiveVM(vm)) {
|
|
|
|
info->cpuTime = 0;
|
|
|
|
} else {
|
|
|
|
info->cpuTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->maxMem = vm->def->maxMemory;
|
|
|
|
info->memory = vm->def->maxMemory;
|
|
|
|
info->nrVirtCpu = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *lxcGetOSType(virDomainPtr dom ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
/* 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_vm_t *vm = lxcFindVMByUUID(driver, dom->uuid);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with matching uuid"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lxcGenerateXML(dom->conn, driver, vm, vm->def);
|
|
|
|
}
|
|
|
|
|
2008-06-26 16:09:48 +00:00
|
|
|
/**
|
|
|
|
* lxcSetupInterfaces:
|
|
|
|
* @conn: pointer to connection
|
|
|
|
* @vm: pointer to virtual machine structure
|
|
|
|
*
|
|
|
|
* Sets up the container interfaces by creating the veth device pairs and
|
|
|
|
* attaching the parent end to the appropriate bridge. The container end
|
|
|
|
* will moved into the container namespace later after clone has been called.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcSetupInterfaces(virConnectPtr conn,
|
|
|
|
lxc_vm_t *vm)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
lxc_driver_t *driver = conn->privateData;
|
|
|
|
struct qemud_driver *networkDriver =
|
|
|
|
(struct qemud_driver *)(conn->networkPrivateData);
|
|
|
|
lxc_net_def_t *net = vm->def->nets;
|
|
|
|
char* bridge;
|
|
|
|
char parentVeth[PATH_MAX] = "";
|
|
|
|
char containerVeth[PATH_MAX] = "";
|
|
|
|
|
|
|
|
if ((vm->def->nets != NULL) && (driver->have_netns == 0)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
|
|
|
|
_("System lacks NETNS support"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (net = vm->def->nets; net; net = net->next) {
|
|
|
|
if (LXC_NET_NETWORK == net->type) {
|
|
|
|
virNetworkPtr network = virNetworkLookupByName(conn, net->txName);
|
|
|
|
if (!network) {
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
bridge = virNetworkGetBridgeName(network);
|
|
|
|
|
|
|
|
virNetworkFree(network);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
bridge = net->txName;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("bridge: %s", bridge);
|
|
|
|
if (NULL == bridge) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to get bridge for interface"));
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG0("calling vethCreate()");
|
|
|
|
if (NULL != net->parentVeth) {
|
|
|
|
strcpy(parentVeth, net->parentVeth);
|
|
|
|
}
|
|
|
|
if (NULL != net->containerVeth) {
|
|
|
|
strcpy(containerVeth, net->containerVeth);
|
|
|
|
}
|
|
|
|
DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVeth);
|
|
|
|
if (0 != (rc = vethCreate(parentVeth, PATH_MAX, containerVeth, PATH_MAX))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to create veth device pair: %d"), rc);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
if (NULL == net->parentVeth) {
|
|
|
|
net->parentVeth = strdup(parentVeth);
|
|
|
|
}
|
|
|
|
if (NULL == net->containerVeth) {
|
|
|
|
net->containerVeth = strdup(containerVeth);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((NULL == net->parentVeth) || (NULL == net->containerVeth)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to allocate veth names"));
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(networkDriver->brctl) && (rc = brInit(&(networkDriver->brctl)))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot initialize bridge support: %s"),
|
|
|
|
strerror(rc));
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != (rc = brAddInterface(networkDriver->brctl, bridge, parentVeth))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to add %s device to %s: %s"),
|
|
|
|
parentVeth,
|
|
|
|
bridge,
|
|
|
|
strerror(rc));
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != (rc = vethInterfaceUpOrDown(parentVeth, 1))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to enable parent ns veth device: %d"), rc);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcMoveInterfacesToNetNs:
|
|
|
|
* @conn: pointer to connection
|
|
|
|
* @vm: pointer to virtual machine structure
|
|
|
|
*
|
|
|
|
* Starts a container process by calling clone() with the namespace flags
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcMoveInterfacesToNetNs(virConnectPtr conn,
|
|
|
|
const lxc_vm_t *vm)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
lxc_net_def_t *net;
|
|
|
|
|
|
|
|
for (net = vm->def->nets; net; net = net->next) {
|
|
|
|
if (0 != moveInterfaceToNetNs(net->containerVeth, vm->def->id)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to move interface %s to ns %d"),
|
|
|
|
net->containerVeth, vm->def->id);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcCleanupInterfaces:
|
|
|
|
* @conn: pointer to connection
|
|
|
|
* @vm: pointer to virtual machine structure
|
|
|
|
*
|
|
|
|
* Cleans up the container interfaces by deleting the veth device pairs.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcCleanupInterfaces(const lxc_vm_t *vm)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
lxc_net_def_t *net;
|
|
|
|
|
|
|
|
for (net = vm->def->nets; net; net = net->next) {
|
|
|
|
if (0 != (rc = vethDelete(net->parentVeth))) {
|
|
|
|
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to delete veth: %s"), net->parentVeth);
|
|
|
|
/* will continue to try to cleanup any other interfaces */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
/**
|
2008-08-13 10:14:47 +00:00
|
|
|
* lxcOpenTty:
|
2008-04-10 07:30:52 +00:00
|
|
|
* @conn: pointer to connection
|
|
|
|
* @ttymaster: pointer to int. On success, set to fd for master end
|
|
|
|
* @ttyName: On success, will point to string slave end of tty. Caller
|
|
|
|
* must free when done (such as in lxcFreeVM).
|
|
|
|
*
|
|
|
|
* Opens and configures container tty.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
2008-08-13 10:14:47 +00:00
|
|
|
static int lxcOpenTty(virConnectPtr conn,
|
|
|
|
int *ttymaster,
|
|
|
|
char **ttyName,
|
|
|
|
int rawmode)
|
2008-04-10 07:30:52 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
|
2008-05-09 07:16:30 +00:00
|
|
|
*ttymaster = posix_openpt(O_RDWR|O_NOCTTY|O_NONBLOCK);
|
2008-04-10 07:30:52 +00:00
|
|
|
if (*ttymaster < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("posix_openpt failed: %s"), strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlockpt(*ttymaster) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("unlockpt failed: %s"), strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:14:47 +00:00
|
|
|
if (rawmode) {
|
|
|
|
struct termios ttyAttr;
|
|
|
|
if (tcgetattr(*ttymaster, &ttyAttr) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"tcgetattr() failed: %s", strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:14:47 +00:00
|
|
|
cfmakeraw(&ttyAttr);
|
|
|
|
|
|
|
|
if (tcsetattr(*ttymaster, TCSADRAIN, &ttyAttr) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"tcsetattr failed: %s", strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:14:47 +00:00
|
|
|
if (ttyName) {
|
|
|
|
char tempTtyName[PATH_MAX];
|
|
|
|
if (0 != ptsname_r(*ttymaster, tempTtyName, sizeof(tempTtyName))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("ptsname_r failed: %s"), strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*ttyName = strdup(tempTtyName)) == NULL) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2008-08-13 10:14:47 +00:00
|
|
|
if (rc != 0 &&
|
|
|
|
*ttymaster != -1) {
|
|
|
|
close(*ttymaster);
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcVmStart:
|
|
|
|
* @conn: pointer to connection
|
|
|
|
* @driver: pointer to driver structure
|
|
|
|
* @vm: pointer to virtual machine structure
|
|
|
|
*
|
|
|
|
* Starts a vm
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcVmStart(virConnectPtr conn,
|
|
|
|
lxc_driver_t * driver,
|
|
|
|
lxc_vm_t * vm)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
2008-08-13 10:14:47 +00:00
|
|
|
int sockpair[2] = { -1, -1 };
|
|
|
|
int containerTty, parentTty;
|
|
|
|
char *containerTtyPath = NULL;
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
/* open parent tty */
|
2008-08-13 10:14:47 +00:00
|
|
|
VIR_FREE(vm->def->tty);
|
|
|
|
if (lxcOpenTty(conn, &parentTty, &vm->def->tty, 1) < 0) {
|
2008-04-10 07:30:52 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open container tty */
|
2008-08-13 10:14:47 +00:00
|
|
|
if (lxcOpenTty(conn, &containerTty, &containerTtyPath, 0) < 0) {
|
2008-04-10 07:30:52 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fork process to handle the tty io forwarding */
|
|
|
|
if ((vm->pid = fork()) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("unable to fork tty forwarding process: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm->pid == 0) {
|
|
|
|
/* child process calls forward routine */
|
2008-08-13 10:25:34 +00:00
|
|
|
lxcControllerMain(parentTty, containerTty);
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 06:03:00 +00:00
|
|
|
if (lxcStoreTtyPid(driver, vm)) {
|
|
|
|
DEBUG0("unable to store tty pid");
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:14:47 +00:00
|
|
|
close(parentTty);
|
|
|
|
close(containerTty);
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-06-26 16:09:48 +00:00
|
|
|
if (0 != (rc = lxcSetupInterfaces(conn, vm))) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-06-26 16:09:48 +00:00
|
|
|
/* create a socket pair to send continue message to the container once */
|
|
|
|
/* we've completed the post clone configuration */
|
2008-08-13 10:14:47 +00:00
|
|
|
if (0 != socketpair(PF_UNIX, SOCK_STREAM, 0, sockpair)) {
|
2008-06-26 16:09:48 +00:00
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("sockpair failed: %s"), strerror(errno));
|
|
|
|
goto cleanup;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-06-26 16:09:48 +00:00
|
|
|
/* check this rc */
|
|
|
|
|
2008-08-13 10:25:34 +00:00
|
|
|
vm->def->id = lxcContainerStart(conn,
|
|
|
|
vm->def,
|
|
|
|
sockpair[1],
|
|
|
|
containerTtyPath);
|
|
|
|
if (vm->def->id == -1)
|
2008-06-26 16:09:48 +00:00
|
|
|
goto cleanup;
|
2008-08-13 10:25:34 +00:00
|
|
|
lxcSaveConfig(conn, driver, vm, vm->def);
|
2008-06-26 16:09:48 +00:00
|
|
|
|
|
|
|
rc = lxcMoveInterfacesToNetNs(conn, vm);
|
|
|
|
if (rc != 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2008-08-13 10:25:34 +00:00
|
|
|
rc = lxcContainerSendContinue(conn, sockpair[0]);
|
2008-06-26 16:09:48 +00:00
|
|
|
if (rc != 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
vm->state = VIR_DOMAIN_RUNNING;
|
|
|
|
driver->ninactivevms--;
|
|
|
|
driver->nactivevms++;
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
cleanup:
|
2008-08-13 10:14:47 +00:00
|
|
|
if (sockpair[0] != -1) close(sockpair[0]);
|
|
|
|
if (sockpair[1] != -1) close(sockpair[1]);
|
|
|
|
VIR_FREE(containerTtyPath);
|
2008-06-26 16:09:48 +00:00
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcDomainStart:
|
|
|
|
* @dom: domain to start
|
|
|
|
*
|
|
|
|
* Looks up domain and starts it.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcDomainStart(virDomainPtr dom)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
virConnectPtr conn = dom->conn;
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)(conn->privateData);
|
|
|
|
lxc_vm_t *vm = lxcFindVMByName(driver, dom->name);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
"no domain with uuid");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = lxcVmStart(conn, driver, vm);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcDomainCreateAndStart:
|
|
|
|
* @conn: pointer to connection
|
|
|
|
* @xml: XML definition of domain
|
|
|
|
* @flags: Unused
|
|
|
|
*
|
|
|
|
* Creates a domain based on xml and starts it
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static virDomainPtr
|
|
|
|
lxcDomainCreateAndStart(virConnectPtr conn,
|
|
|
|
const char *xml,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
|
|
|
lxc_vm_t *vm;
|
|
|
|
lxc_vm_def_t *def;
|
|
|
|
virDomainPtr dom = NULL;
|
|
|
|
|
|
|
|
if (!(def = lxcParseVMDef(conn, xml, NULL))) {
|
|
|
|
goto return_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(vm = lxcAssignVMDef(conn, driver, def))) {
|
|
|
|
lxcFreeVMDef(def);
|
|
|
|
goto return_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lxcSaveVMDef(conn, driver, vm, def) < 0) {
|
|
|
|
lxcRemoveInactiveVM(driver, vm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lxcVmStart(conn, driver, vm) < 0) {
|
|
|
|
lxcRemoveInactiveVM(driver, vm);
|
|
|
|
goto return_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
|
|
|
|
if (dom) {
|
|
|
|
dom->id = vm->def->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return_point:
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcDomainShutdown:
|
|
|
|
* @dom: Ptr to domain to shutdown
|
|
|
|
*
|
|
|
|
* Sends SIGINT to container root process to request it to shutdown
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcDomainShutdown(virDomainPtr dom)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByID(driver, dom->id);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with id %d"), dom->id);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 > (kill(vm->def->id, SIGINT))) {
|
|
|
|
if (ESRCH != errno) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("sending SIGTERM failed: %s"), strerror(errno));
|
|
|
|
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm->state = VIR_DOMAIN_SHUTDOWN;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-13 06:30:58 +00:00
|
|
|
* lxcVmCleanup:
|
|
|
|
* @vm: Ptr to VM to clean up
|
2008-04-10 07:30:52 +00:00
|
|
|
*
|
2008-05-13 06:30:58 +00:00
|
|
|
* waitpid() on the container process. kill and wait the tty process
|
|
|
|
* This is called by boh lxcDomainDestroy and lxcSigHandler when a
|
|
|
|
* container exits.
|
2008-04-10 07:30:52 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
2008-05-13 06:30:58 +00:00
|
|
|
static int lxcVMCleanup(lxc_driver_t *driver, lxc_vm_t * vm)
|
2008-04-10 07:30:52 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
int waitRc;
|
2008-05-13 06:30:58 +00:00
|
|
|
int childStatus = -1;
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-06-26 16:09:48 +00:00
|
|
|
/* if this fails, we'll continue. it will report any errors */
|
|
|
|
lxcCleanupInterfaces(vm);
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
while (((waitRc = waitpid(vm->def->id, &childStatus, 0)) == -1) &&
|
|
|
|
errno == EINTR);
|
|
|
|
|
2008-06-05 06:03:00 +00:00
|
|
|
if ((waitRc != vm->def->id) && (errno != ECHILD)) {
|
2008-05-13 06:30:58 +00:00
|
|
|
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-04-10 07:30:52 +00:00
|
|
|
_("waitpid failed to wait for container %d: %d %s"),
|
|
|
|
vm->def->id, waitRc, strerror(errno));
|
|
|
|
goto kill_tty;
|
|
|
|
}
|
|
|
|
|
2008-05-13 06:30:58 +00:00
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
if (WIFEXITED(childStatus)) {
|
|
|
|
rc = WEXITSTATUS(childStatus);
|
|
|
|
DEBUG("container exited with rc: %d", rc);
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
kill_tty:
|
2008-06-05 06:03:00 +00:00
|
|
|
if (2 > vm->pid) {
|
|
|
|
DEBUG("not killing tty process with pid %d", vm->pid);
|
|
|
|
goto tty_error_out;
|
|
|
|
}
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
if (0 > (kill(vm->pid, SIGKILL))) {
|
|
|
|
if (ESRCH != errno) {
|
2008-05-13 06:30:58 +00:00
|
|
|
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-04-10 07:30:52 +00:00
|
|
|
_("sending SIGKILL to tty process failed: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
goto tty_error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (((waitRc = waitpid(vm->pid, &childStatus, 0)) == -1) &&
|
|
|
|
errno == EINTR);
|
|
|
|
|
2008-06-05 06:03:00 +00:00
|
|
|
if ((waitRc != vm->pid) && (errno != ECHILD)) {
|
2008-05-13 06:30:58 +00:00
|
|
|
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-04-10 07:30:52 +00:00
|
|
|
_("waitpid failed to wait for tty %d: %d %s"),
|
|
|
|
vm->pid, waitRc, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
tty_error_out:
|
|
|
|
vm->state = VIR_DOMAIN_SHUTOFF;
|
|
|
|
vm->pid = -1;
|
2008-06-05 06:03:00 +00:00
|
|
|
lxcDeleteTtyPidFile(vm);
|
2008-04-10 07:30:52 +00:00
|
|
|
vm->def->id = -1;
|
|
|
|
driver->nactivevms--;
|
|
|
|
driver->ninactivevms++;
|
2008-05-13 06:30:58 +00:00
|
|
|
lxcSaveConfig(NULL, driver, vm, vm->def);
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-05-13 06:30:58 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcDomainDestroy:
|
|
|
|
* @dom: Ptr to domain to destroy
|
|
|
|
*
|
|
|
|
* Sends SIGKILL to container root process to terminate the container
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcDomainDestroy(virDomainPtr dom)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
|
|
|
|
lxc_vm_t *vm = lxcFindVMByID(driver, dom->id);
|
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with id %d"), dom->id);
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 > (kill(vm->def->id, SIGKILL))) {
|
|
|
|
if (ESRCH != errno) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("sending SIGKILL failed: %s"), strerror(errno));
|
|
|
|
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm->state = VIR_DOMAIN_SHUTDOWN;
|
|
|
|
|
|
|
|
rc = lxcVMCleanup(driver, vm);
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
error_out:
|
|
|
|
return rc;
|
|
|
|
}
|
2008-03-27 09:34:06 +00:00
|
|
|
|
2008-06-26 16:05:02 +00:00
|
|
|
static int lxcCheckNetNsSupport(void)
|
|
|
|
{
|
|
|
|
const char *argv[] = {"ip", "link", "set", "lo", "netns", "-1", NULL};
|
|
|
|
int ip_rc;
|
|
|
|
|
2008-08-13 10:25:34 +00:00
|
|
|
if (virRun(NULL, argv, &ip_rc) < 0 ||
|
|
|
|
!(WIFEXITED(ip_rc) && (WEXITSTATUS(ip_rc) != 255)))
|
|
|
|
return 0;
|
2008-06-26 16:05:02 +00:00
|
|
|
|
2008-08-13 10:25:34 +00:00
|
|
|
if (lxcContainerAvailable(LXC_CONTAINER_FEATURE_NET) < 0)
|
|
|
|
return 0;
|
2008-06-26 16:05:02 +00:00
|
|
|
|
2008-08-13 10:25:34 +00:00
|
|
|
return 1;
|
2008-06-26 16:05:02 +00:00
|
|
|
}
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
static int lxcStartup(void)
|
2008-03-21 15:03:37 +00:00
|
|
|
{
|
2008-03-31 12:02:12 +00:00
|
|
|
uid_t uid = getuid();
|
|
|
|
|
|
|
|
/* Check that the user is root */
|
|
|
|
if (0 != uid) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_ALLOC(lxc_driver) < 0) {
|
2008-03-27 09:34:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-03-21 15:03:37 +00:00
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
/* Check that this is a container enabled kernel */
|
2008-08-13 10:25:34 +00:00
|
|
|
if(lxcContainerAvailable(0) < 0)
|
2008-03-21 15:03:37 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-06-26 16:05:02 +00:00
|
|
|
lxc_driver->have_netns = lxcCheckNetNsSupport();
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
/* Call function to load lxc driver configuration information */
|
2008-03-27 09:34:06 +00:00
|
|
|
if (lxcLoadDriverConfig(lxc_driver) < 0) {
|
|
|
|
lxcShutdown();
|
2008-03-21 15:03:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call function to load the container configuration files */
|
2008-03-27 09:34:06 +00:00
|
|
|
if (lxcLoadContainerInfo(lxc_driver) < 0) {
|
|
|
|
lxcShutdown();
|
2008-03-21 15:03:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lxcFreeDriver(lxc_driver_t *driver)
|
|
|
|
{
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(driver->configDir);
|
|
|
|
VIR_FREE(driver->stateDir);
|
|
|
|
VIR_FREE(driver);
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
static int lxcShutdown(void)
|
2008-03-21 15:03:37 +00:00
|
|
|
{
|
2008-03-31 12:02:12 +00:00
|
|
|
if (lxc_driver == NULL)
|
2008-03-31 14:38:12 +00:00
|
|
|
return(-1);
|
2008-03-31 12:02:12 +00:00
|
|
|
lxcFreeVMs(lxc_driver->vms);
|
2008-03-27 09:34:06 +00:00
|
|
|
lxc_driver->vms = NULL;
|
|
|
|
lxcFreeDriver(lxc_driver);
|
2008-03-31 12:02:12 +00:00
|
|
|
lxc_driver = NULL;
|
2008-03-27 09:34:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-21 15:03:37 +00:00
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
/**
|
|
|
|
* lxcActive:
|
|
|
|
*
|
|
|
|
* Checks if the LXC daemon is active, i.e. has an active domain
|
|
|
|
*
|
|
|
|
* Returns 1 if active, 0 otherwise
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
lxcActive(void) {
|
2008-03-31 12:02:12 +00:00
|
|
|
if (lxc_driver == NULL)
|
|
|
|
return(0);
|
2008-03-27 09:34:06 +00:00
|
|
|
/* If we've any active networks or guests, then we
|
|
|
|
* mark this driver as active
|
|
|
|
*/
|
|
|
|
if (lxc_driver->nactivevms)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Otherwise we're happy to deal with a shutdown */
|
2008-03-21 15:03:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-13 06:30:58 +00:00
|
|
|
/**
|
|
|
|
* lxcSigHandler:
|
|
|
|
* @siginfo: Pointer to siginfo_t structure
|
|
|
|
*
|
|
|
|
* Handles signals received by libvirtd. Currently this is used to
|
|
|
|
* catch SIGCHLD from an exiting container.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcSigHandler(siginfo_t *siginfo)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
lxc_vm_t *vm;
|
|
|
|
|
|
|
|
if (siginfo->si_signo == SIGCHLD) {
|
|
|
|
vm = lxcFindVMByID(lxc_driver, siginfo->si_pid);
|
|
|
|
|
|
|
|
if (NULL == vm) {
|
|
|
|
DEBUG("Ignoring SIGCHLD from non-container process %d\n",
|
|
|
|
siginfo->si_pid);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = lxcVMCleanup(lxc_driver, vm);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
/* Function Tables */
|
|
|
|
static virDriver lxcDriver = {
|
|
|
|
VIR_DRV_LXC, /* the number virDrvNo */
|
|
|
|
"LXC", /* the name of the driver */
|
|
|
|
LIBVIR_VERSION_NUMBER, /* the version of the backend */
|
|
|
|
lxcProbe, /* probe */
|
|
|
|
lxcOpen, /* open */
|
|
|
|
lxcClose, /* close */
|
|
|
|
NULL, /* supports_feature */
|
|
|
|
NULL, /* type */
|
|
|
|
NULL, /* version */
|
|
|
|
NULL, /* getHostname */
|
|
|
|
NULL, /* getURI */
|
|
|
|
NULL, /* getMaxVcpus */
|
|
|
|
NULL, /* nodeGetInfo */
|
|
|
|
NULL, /* getCapabilities */
|
|
|
|
lxcListDomains, /* listDomains */
|
|
|
|
lxcNumDomains, /* numOfDomains */
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcDomainCreateAndStart, /* domainCreateLinux */
|
2008-03-21 15:03:37 +00:00
|
|
|
lxcDomainLookupByID, /* domainLookupByID */
|
|
|
|
lxcDomainLookupByUUID, /* domainLookupByUUID */
|
|
|
|
lxcDomainLookupByName, /* domainLookupByName */
|
|
|
|
NULL, /* domainSuspend */
|
|
|
|
NULL, /* domainResume */
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcDomainShutdown, /* domainShutdown */
|
2008-03-21 15:03:37 +00:00
|
|
|
NULL, /* domainReboot */
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcDomainDestroy, /* domainDestroy */
|
2008-03-21 15:03:37 +00:00
|
|
|
lxcGetOSType, /* domainGetOSType */
|
|
|
|
NULL, /* domainGetMaxMemory */
|
|
|
|
NULL, /* domainSetMaxMemory */
|
|
|
|
NULL, /* domainSetMemory */
|
|
|
|
lxcDomainGetInfo, /* domainGetInfo */
|
|
|
|
NULL, /* domainSave */
|
|
|
|
NULL, /* domainRestore */
|
|
|
|
NULL, /* domainCoreDump */
|
|
|
|
NULL, /* domainSetVcpus */
|
|
|
|
NULL, /* domainPinVcpu */
|
|
|
|
NULL, /* domainGetVcpus */
|
|
|
|
NULL, /* domainGetMaxVcpus */
|
|
|
|
lxcDomainDumpXML, /* domainDumpXML */
|
|
|
|
lxcListDefinedDomains, /* listDefinedDomains */
|
|
|
|
lxcNumDefinedDomains, /* numOfDefinedDomains */
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcDomainStart, /* domainCreate */
|
2008-03-21 15:03:37 +00:00
|
|
|
lxcDomainDefine, /* domainDefineXML */
|
|
|
|
lxcDomainUndefine, /* domainUndefine */
|
|
|
|
NULL, /* domainAttachDevice */
|
|
|
|
NULL, /* domainDetachDevice */
|
|
|
|
NULL, /* domainGetAutostart */
|
|
|
|
NULL, /* domainSetAutostart */
|
|
|
|
NULL, /* domainGetSchedulerType */
|
|
|
|
NULL, /* domainGetSchedulerParameters */
|
|
|
|
NULL, /* domainSetSchedulerParameters */
|
|
|
|
NULL, /* domainMigratePrepare */
|
|
|
|
NULL, /* domainMigratePerform */
|
|
|
|
NULL, /* domainMigrateFinish */
|
|
|
|
NULL, /* domainBlockStats */
|
|
|
|
NULL, /* domainInterfaceStats */
|
2008-06-12 13:48:29 +00:00
|
|
|
NULL, /* domainBlockPeek */
|
|
|
|
NULL, /* domainMemoryPeek */
|
2008-03-21 15:03:37 +00:00
|
|
|
NULL, /* nodeGetCellsFreeMemory */
|
|
|
|
NULL, /* getFreeMemory */
|
|
|
|
};
|
|
|
|
|
2008-03-27 09:34:06 +00:00
|
|
|
|
|
|
|
static virStateDriver lxcStateDriver = {
|
|
|
|
lxcStartup,
|
|
|
|
lxcShutdown,
|
|
|
|
NULL, /* reload */
|
|
|
|
lxcActive,
|
2008-05-13 06:30:58 +00:00
|
|
|
lxcSigHandler
|
2008-03-27 09:34:06 +00:00
|
|
|
};
|
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
int lxcRegister(void)
|
|
|
|
{
|
|
|
|
virRegisterDriver(&lxcDriver);
|
2008-03-27 09:34:06 +00:00
|
|
|
virRegisterStateDriver(&lxcStateDriver);
|
2008-03-21 15:03:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_LXC */
|