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>
|
|
|
|
|
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-08-13 10:52:15 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/poll.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-06-09 22:51:32 +00:00
|
|
|
#include "memory.h"
|
2008-04-10 07:30:52 +00:00
|
|
|
#include "util.h"
|
2008-06-26 16:09:48 +00:00
|
|
|
#include "bridge.h"
|
|
|
|
#include "veth.h"
|
2008-08-13 10:52:15 +00:00
|
|
|
#include "event.h"
|
2008-10-03 16:46:01 +00:00
|
|
|
#include "cgroup.h"
|
2008-08-13 10:52:15 +00:00
|
|
|
|
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;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByID(driver->domains, id);
|
2008-03-21 15:03:37 +00:00
|
|
|
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;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByUUID(driver->domains, uuid);
|
2008-03-21 15:03:37 +00:00
|
|
|
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;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByName(driver->domains, name);
|
2008-03-21 15:03:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
static int lxcListDomains(virConnectPtr conn, int *ids, int nids) {
|
2008-03-21 15:03:37 +00:00
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = driver->domains;
|
|
|
|
int got = 0;
|
|
|
|
while (vm && got < nids) {
|
|
|
|
if (virDomainIsActive(vm)) {
|
|
|
|
ids[got] = vm->def->id;
|
|
|
|
got++;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
vm = vm->next;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
return got;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
static int lxcNumDomains(virConnectPtr conn) {
|
2008-03-21 15:03:37 +00:00
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
int n = 0;
|
|
|
|
virDomainObjPtr dom = driver->domains;
|
|
|
|
while (dom) {
|
|
|
|
if (virDomainIsActive(dom))
|
|
|
|
n++;
|
|
|
|
dom = dom->next;
|
|
|
|
}
|
|
|
|
return n;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcListDefinedDomains(virConnectPtr conn,
|
2008-08-13 12:50:55 +00:00
|
|
|
char **const names, int nnames) {
|
2008-03-21 15:03:37 +00:00
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = driver->domains;
|
|
|
|
int got = 0, i;
|
|
|
|
while (vm && got < nnames) {
|
|
|
|
if (!virDomainIsActive(vm)) {
|
|
|
|
if (!(names[got] = strdup(vm->def->name))) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
|
2008-03-21 15:03:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
got++;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
vm = vm->next;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
return got;
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
cleanup:
|
2008-08-13 12:50:55 +00:00
|
|
|
for (i = 0 ; i < got ; i++)
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(names[i]);
|
2008-03-21 15:03:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
static int lxcNumDefinedDomains(virConnectPtr conn) {
|
2008-03-21 15:03:37 +00:00
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
int n = 0;
|
|
|
|
virDomainObjPtr dom = driver->domains;
|
|
|
|
while (dom) {
|
|
|
|
if (!virDomainIsActive(dom))
|
|
|
|
n++;
|
|
|
|
dom = dom->next;
|
|
|
|
}
|
|
|
|
return n;
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
|
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainDefPtr def;
|
|
|
|
virDomainObjPtr vm;
|
2008-03-21 15:03:37 +00:00
|
|
|
virDomainPtr dom;
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
|
2008-03-21 15:03:37 +00:00
|
|
|
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"));
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainDefFree(def);
|
2008-06-26 16:08:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (!(vm = virDomainAssignDef(conn, &driver->domains, def))) {
|
|
|
|
virDomainDefFree(def);
|
2008-03-21 15:03:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-20 19:42:36 +00:00
|
|
|
vm->persistent = 1;
|
2008-03-21 15:03:37 +00:00
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (virDomainSaveConfig(conn,
|
|
|
|
driver->configDir,
|
2008-08-20 19:42:36 +00:00
|
|
|
vm->newDef ? vm->newDef : vm->def) < 0) {
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainRemoveInactive(&driver->domains, vm);
|
2008-03-21 15:03:37 +00:00
|
|
|
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;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with matching uuid"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (virDomainIsActive(vm)) {
|
2008-03-21 15:03:37 +00:00
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot delete active domain"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-20 19:42:36 +00:00
|
|
|
if (!vm->persistent) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("cannot undefine transient domain"));
|
2008-03-21 15:03:37 +00:00
|
|
|
return -1;
|
2008-08-20 19:42:36 +00:00
|
|
|
}
|
2008-03-21 15:03:37 +00:00
|
|
|
|
2008-08-20 19:42:36 +00:00
|
|
|
if (virDomainDeleteConfig(dom->conn,
|
|
|
|
driver->configDir,
|
|
|
|
driver->autostartDir,
|
|
|
|
vm) <0)
|
|
|
|
return -1;
|
2008-03-21 15:03:37 +00:00
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainRemoveInactive(&driver->domains, vm);
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lxcDomainGetInfo(virDomainPtr dom,
|
|
|
|
virDomainInfoPtr info)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with matching uuid"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->state = vm->state;
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (!virDomainIsActive(vm)) {
|
2008-03-21 15:03:37 +00:00
|
|
|
info->cpuTime = 0;
|
|
|
|
} else {
|
|
|
|
info->cpuTime = 0;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
info->maxMem = vm->def->maxmem;
|
|
|
|
info->memory = vm->def->memory;
|
2008-03-21 15:03:37 +00:00
|
|
|
info->nrVirtCpu = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
static char *lxcGetOSType(virDomainPtr dom)
|
2008-03-21 15:03:37 +00:00
|
|
|
{
|
2008-08-13 12:50:55 +00:00
|
|
|
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 strdup(vm->def->os.type);
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *lxcDomainDumpXML(virDomainPtr dom,
|
2008-08-13 12:50:55 +00:00
|
|
|
int flags)
|
2008-03-21 15:03:37 +00:00
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t *)dom->conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByUUID(driver->domains, dom->uuid);
|
2008-03-21 15:03:37 +00:00
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with matching uuid"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
return virDomainDefFormat(dom->conn,
|
|
|
|
(flags & VIR_DOMAIN_XML_INACTIVE) &&
|
|
|
|
vm->newDef ? vm->newDef : vm->def,
|
|
|
|
flags);
|
2008-03-21 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* lxcVmCleanup:
|
|
|
|
* @vm: Ptr to VM to clean up
|
|
|
|
*
|
|
|
|
* waitpid() on the container process. kill and wait the tty process
|
|
|
|
* This is called by both lxcDomainDestroy and lxcSigHandler when a
|
|
|
|
* container exits.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 in case of error
|
|
|
|
*/
|
|
|
|
static int lxcVMCleanup(virConnectPtr conn,
|
|
|
|
lxc_driver_t *driver,
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm)
|
2008-08-13 10:52:15 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
int waitRc;
|
|
|
|
int childStatus = -1;
|
2008-08-22 15:35:37 +00:00
|
|
|
virDomainNetDefPtr net;
|
2008-10-03 16:46:01 +00:00
|
|
|
virCgroupPtr cgroup;
|
2008-08-13 10:52:15 +00:00
|
|
|
|
|
|
|
while (((waitRc = waitpid(vm->pid, &childStatus, 0)) == -1) &&
|
|
|
|
errno == EINTR)
|
|
|
|
; /* empty */
|
|
|
|
|
|
|
|
if ((waitRc != vm->pid) && (errno != ECHILD)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("waitpid failed to wait for container %d: %d %s"),
|
|
|
|
vm->pid, waitRc, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
if (WIFEXITED(childStatus)) {
|
|
|
|
rc = WEXITSTATUS(childStatus);
|
|
|
|
DEBUG("container exited with rc: %d", rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
virEventRemoveHandle(vm->monitor);
|
|
|
|
close(vm->monitor);
|
|
|
|
|
|
|
|
virFileDeletePid(driver->stateDir, vm->def->name);
|
2008-08-20 20:55:32 +00:00
|
|
|
virDomainDeleteConfig(conn, driver->stateDir, NULL, vm);
|
2008-08-13 10:52:15 +00:00
|
|
|
|
|
|
|
vm->state = VIR_DOMAIN_SHUTOFF;
|
|
|
|
vm->pid = -1;
|
|
|
|
vm->def->id = -1;
|
|
|
|
vm->monitor = -1;
|
|
|
|
|
2008-08-22 15:35:37 +00:00
|
|
|
for (net = vm->def->nets; net; net = net->next) {
|
|
|
|
vethInterfaceUpOrDown(net->ifname, 0);
|
|
|
|
vethDelete(net->ifname);
|
|
|
|
}
|
|
|
|
|
2008-10-03 16:46:01 +00:00
|
|
|
if (virCgroupForDomain(vm->def, "lxc", &cgroup) == 0) {
|
|
|
|
virCgroupRemove(cgroup);
|
|
|
|
virCgroupFree(&cgroup);
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-06-26 16:09:48 +00:00
|
|
|
/**
|
|
|
|
* lxcSetupInterfaces:
|
2008-08-13 10:52:15 +00:00
|
|
|
* @def: pointer to virtual machine structure
|
2008-06-26 16:09:48 +00:00
|
|
|
*
|
|
|
|
* 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,
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainDefPtr def,
|
2008-08-13 10:52:15 +00:00
|
|
|
unsigned int *nveths,
|
|
|
|
char ***veths)
|
2008-06-26 16:09:48 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainNetDefPtr net;
|
2008-08-13 10:52:15 +00:00
|
|
|
char *bridge = NULL;
|
2008-06-26 16:09:48 +00:00
|
|
|
char parentVeth[PATH_MAX] = "";
|
|
|
|
char containerVeth[PATH_MAX] = "";
|
2008-08-13 10:52:15 +00:00
|
|
|
brControl *brctl = NULL;
|
2008-06-26 16:09:48 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (brInit(&brctl) != 0)
|
2008-06-26 16:09:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
for (net = def->nets; net; net = net->next) {
|
2008-08-13 12:50:55 +00:00
|
|
|
switch (net->type) {
|
|
|
|
case VIR_DOMAIN_NET_TYPE_NETWORK:
|
|
|
|
{
|
|
|
|
virNetworkPtr network = virNetworkLookupByName(conn,
|
|
|
|
net->data.network.name);
|
2008-06-26 16:09:48 +00:00
|
|
|
if (!network) {
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
bridge = virNetworkGetBridgeName(network);
|
|
|
|
|
|
|
|
virNetworkFree(network);
|
2008-08-13 12:50:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VIR_DOMAIN_NET_TYPE_BRIDGE:
|
|
|
|
bridge = net->data.bridge.brname;
|
|
|
|
break;
|
2008-06-26 16:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()");
|
2008-08-13 12:50:55 +00:00
|
|
|
if (NULL != net->ifname) {
|
|
|
|
strcpy(parentVeth, net->ifname);
|
2008-06-26 16:09:48 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
if (NULL == net->ifname) {
|
|
|
|
net->ifname = strdup(parentVeth);
|
2008-06-26 16:09:48 +00:00
|
|
|
}
|
2008-08-13 10:52:15 +00:00
|
|
|
if (VIR_REALLOC_N(*veths, (*nveths)+1) < 0)
|
|
|
|
goto error_exit;
|
|
|
|
if (((*veths)[(*nveths)++] = strdup(containerVeth)) == NULL)
|
2008-06-26 16:09:48 +00:00
|
|
|
goto error_exit;
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (NULL == net->ifname) {
|
2008-08-13 10:52:15 +00:00
|
|
|
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("failed to allocate veth names"));
|
2008-06-26 16:09:48 +00:00
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (0 != (rc = brAddInterface(brctl, bridge, parentVeth))) {
|
2008-06-26 16:09:48 +00:00
|
|
|
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))) {
|
2008-08-13 10:52:15 +00:00
|
|
|
lxcError(NULL, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-06-26 16:09:48 +00:00
|
|
|
_("failed to enable parent ns veth device: %d"), rc);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
error_exit:
|
2008-08-13 10:52:15 +00:00
|
|
|
brShutdown(brctl);
|
2008-06-26 16:09:48 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
static int lxcMonitorClient(virConnectPtr conn,
|
|
|
|
lxc_driver_t * driver,
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm)
|
2008-04-10 07:30:52 +00:00
|
|
|
{
|
2008-08-13 10:52:15 +00:00
|
|
|
char *sockpath = NULL;
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_un addr;
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (asprintf(&sockpath, "%s/%s.sock",
|
|
|
|
driver->stateDir, vm->def->name) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-08-13 10:52:15 +00:00
|
|
|
_("failed to create client socket: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
goto error;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-08-13 10:52:15 +00:00
|
|
|
_("failed to connect to client socket: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
goto error;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
VIR_FREE(sockpath);
|
|
|
|
return fd;
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
error:
|
|
|
|
VIR_FREE(sockpath);
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int lxcVmTerminate(virConnectPtr conn,
|
|
|
|
lxc_driver_t *driver,
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm,
|
2008-08-13 10:52:15 +00:00
|
|
|
int signum)
|
|
|
|
{
|
|
|
|
if (signum == 0)
|
|
|
|
signum = SIGINT;
|
2008-08-13 10:14:47 +00:00
|
|
|
|
2008-08-20 20:55:32 +00:00
|
|
|
if (vm->pid <= 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("invalid PID %d for container"), vm->pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (kill(vm->pid, signum) < 0) {
|
|
|
|
if (errno != ESRCH) {
|
2008-08-13 10:14:47 +00:00
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-08-13 10:52:15 +00:00
|
|
|
_("failed to kill pid %d: %s"),
|
|
|
|
vm->pid, strerror(errno));
|
|
|
|
return -1;
|
2008-08-13 10:14:47 +00:00
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
vm->state = VIR_DOMAIN_SHUTDOWN;
|
2008-08-13 10:14:47 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
return lxcVMCleanup(conn, driver, vm);
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
static void lxcMonitorEvent(int fd,
|
|
|
|
int events ATTRIBUTE_UNUSED,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = data;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = driver->domains;
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
while (vm) {
|
|
|
|
if (vm->monitor == fd)
|
|
|
|
break;
|
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
if (!vm) {
|
|
|
|
virEventRemoveHandle(fd);
|
|
|
|
return;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (lxcVmTerminate(NULL, driver, vm, SIGINT) < 0)
|
|
|
|
virEventRemoveHandle(fd);
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-20 20:55:32 +00:00
|
|
|
static int lxcControllerStart(virConnectPtr conn,
|
|
|
|
virDomainObjPtr vm,
|
|
|
|
int nveths,
|
|
|
|
char **veths,
|
|
|
|
int appPty,
|
|
|
|
int logfd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int rc;
|
|
|
|
int ret = -1;
|
|
|
|
int largc = 0, larga = 0;
|
|
|
|
const char **largv = NULL;
|
|
|
|
pid_t child;
|
|
|
|
int status;
|
2008-08-27 11:42:52 +00:00
|
|
|
fd_set keepfd;
|
|
|
|
char appPtyStr[30];
|
2008-09-05 11:52:12 +00:00
|
|
|
const char *emulator;
|
|
|
|
lxc_driver_t *driver = conn->privateData;
|
2008-08-27 11:42:52 +00:00
|
|
|
|
|
|
|
FD_ZERO(&keepfd);
|
2008-08-20 20:55:32 +00:00
|
|
|
|
|
|
|
#define ADD_ARG_SPACE \
|
|
|
|
do { \
|
|
|
|
if (largc == larga) { \
|
|
|
|
larga += 10; \
|
|
|
|
if (VIR_REALLOC_N(largv, larga) < 0) \
|
|
|
|
goto no_memory; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ADD_ARG(thisarg) \
|
|
|
|
do { \
|
|
|
|
ADD_ARG_SPACE; \
|
|
|
|
largv[largc++] = thisarg; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ADD_ARG_LIT(thisarg) \
|
|
|
|
do { \
|
|
|
|
ADD_ARG_SPACE; \
|
|
|
|
if ((largv[largc++] = strdup(thisarg)) == NULL) \
|
|
|
|
goto no_memory; \
|
|
|
|
} while (0)
|
|
|
|
|
2008-08-27 11:42:52 +00:00
|
|
|
snprintf(appPtyStr, sizeof(appPtyStr), "%d", appPty);
|
|
|
|
|
2008-09-05 11:52:12 +00:00
|
|
|
emulator = vm->def->emulator;
|
|
|
|
if (!emulator)
|
|
|
|
emulator = virDomainDefDefaultEmulator(conn, vm->def, driver->caps);
|
|
|
|
if (!emulator)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ADD_ARG_LIT(emulator);
|
2008-08-20 20:55:32 +00:00
|
|
|
ADD_ARG_LIT("--name");
|
|
|
|
ADD_ARG_LIT(vm->def->name);
|
|
|
|
ADD_ARG_LIT("--console");
|
2008-08-27 11:42:52 +00:00
|
|
|
ADD_ARG_LIT(appPtyStr);
|
2008-08-20 20:55:32 +00:00
|
|
|
ADD_ARG_LIT("--background");
|
|
|
|
|
|
|
|
for (i = 0 ; i < nveths ; i++) {
|
|
|
|
ADD_ARG_LIT("--veth");
|
|
|
|
ADD_ARG_LIT(veths[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
ADD_ARG(NULL);
|
|
|
|
|
2008-08-27 11:42:52 +00:00
|
|
|
vm->stdin_fd = -1;
|
2008-08-20 20:55:32 +00:00
|
|
|
vm->stdout_fd = vm->stderr_fd = logfd;
|
|
|
|
|
2008-08-27 11:42:52 +00:00
|
|
|
FD_SET(appPty, &keepfd);
|
|
|
|
|
|
|
|
if (virExec(conn, largv, NULL, &keepfd, &child,
|
2008-08-20 20:55:32 +00:00
|
|
|
vm->stdin_fd, &vm->stdout_fd, &vm->stderr_fd,
|
|
|
|
VIR_EXEC_NONE) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* We now wait for the process to exit - the controller
|
|
|
|
* will fork() itself into the background - waiting for
|
|
|
|
* it to exit thus guarentees it has written its pidfile
|
|
|
|
*/
|
|
|
|
while ((rc = waitpid(child, &status, 0) == -1) && errno == EINTR);
|
|
|
|
if (rc == -1) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot wait for '%s': %s"),
|
|
|
|
largv[0], strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("container '%s' unexpectedly shutdown during startup"),
|
|
|
|
largv[0]);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADD_ARG
|
|
|
|
#undef ADD_ARG_LIT
|
|
|
|
#undef ADD_ARG_SPACE
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for (i = 0 ; i < largc ; i++)
|
|
|
|
VIR_FREE(largv[i]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm)
|
2008-04-10 07:30:52 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
2008-08-13 10:52:15 +00:00
|
|
|
unsigned int i;
|
|
|
|
int parentTty;
|
2008-08-13 12:50:55 +00:00
|
|
|
char *parentTtyPath = NULL;
|
2008-08-13 10:52:15 +00:00
|
|
|
char *logfile = NULL;
|
|
|
|
int logfd = -1;
|
|
|
|
unsigned int nveths = 0;
|
|
|
|
char **veths = NULL;
|
|
|
|
|
|
|
|
if (virFileMakePath(driver->logDir) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot create log directory %s: %s"),
|
|
|
|
driver->logDir, strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (asprintf(&logfile, "%s/%s.log",
|
|
|
|
driver->logDir, vm->def->name) < 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, NULL);
|
|
|
|
return -1;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
/* open parent tty */
|
2008-08-13 12:50:55 +00:00
|
|
|
if (virFileOpenTty(&parentTty, &parentTtyPath, 1) < 0) {
|
2008-04-10 07:30:52 +00:00
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-08-13 10:52:15 +00:00
|
|
|
_("failed to allocate tty: %s"),
|
2008-04-10 07:30:52 +00:00
|
|
|
strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-08-13 12:50:55 +00:00
|
|
|
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);
|
|
|
|
}
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (lxcSetupInterfaces(conn, vm->def, &nveths, &veths) != 0)
|
2008-06-26 16:09:48 +00:00
|
|
|
goto cleanup;
|
2008-04-10 07:30:52 +00:00
|
|
|
|
2008-08-20 20:55:32 +00:00
|
|
|
/* Persist the live configuration now we have veth & tty info */
|
|
|
|
if (virDomainSaveConfig(conn, driver->stateDir, vm->def) < 0) {
|
|
|
|
rc = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if ((logfd = open(logfile, O_WRONLY | O_TRUNC | O_CREAT,
|
|
|
|
S_IRUSR|S_IWUSR)) < 0) {
|
2008-06-26 16:09:48 +00:00
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-08-13 10:52:15 +00:00
|
|
|
_("failed to open %s: %s"), logfile,
|
|
|
|
strerror(errno));
|
2008-06-26 16:09:48 +00:00
|
|
|
goto cleanup;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 20:55:32 +00:00
|
|
|
if (lxcControllerStart(conn,
|
|
|
|
vm,
|
|
|
|
nveths, veths,
|
|
|
|
parentTty, logfd) < 0)
|
2008-06-26 16:09:48 +00:00
|
|
|
goto cleanup;
|
2008-08-13 10:52:15 +00:00
|
|
|
|
|
|
|
/* Connect to the controller as a client *first* because
|
|
|
|
* this will block until the child has written their
|
|
|
|
* pid file out to disk */
|
|
|
|
if ((vm->monitor = lxcMonitorClient(conn, driver, vm)) < 0)
|
2008-06-26 16:09:48 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
/* And get its pid */
|
|
|
|
if ((rc = virFileReadPid(driver->stateDir, vm->def->name, &vm->pid)) != 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to read pid file %s/%s.pid: %s"),
|
|
|
|
driver->stateDir, vm->def->name, strerror(rc));
|
|
|
|
rc = -1;
|
2008-06-26 16:09:48 +00:00
|
|
|
goto cleanup;
|
2008-08-13 10:52:15 +00:00
|
|
|
}
|
2008-06-26 16:09:48 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
vm->def->id = vm->pid;
|
2008-06-26 16:09:48 +00:00
|
|
|
vm->state = VIR_DOMAIN_RUNNING;
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (virEventAddHandle(vm->monitor,
|
|
|
|
POLLERR | POLLHUP,
|
|
|
|
lxcMonitorEvent,
|
|
|
|
driver) < 0) {
|
|
|
|
lxcVmTerminate(conn, driver, vm, 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-06-26 16:09:48 +00:00
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for (i = 0 ; i < nveths ; i++) {
|
|
|
|
if (rc != 0)
|
|
|
|
vethDelete(veths[i]);
|
|
|
|
VIR_FREE(veths[i]);
|
|
|
|
}
|
|
|
|
if (rc != 0 && vm->monitor != -1) {
|
|
|
|
close(vm->monitor);
|
|
|
|
vm->monitor = -1;
|
|
|
|
}
|
|
|
|
if (parentTty != -1)
|
|
|
|
close(parentTty);
|
|
|
|
if (logfd != -1)
|
|
|
|
close(logfd);
|
|
|
|
VIR_FREE(logfile);
|
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);
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByName(driver->domains, dom->name);
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
"no domain with uuid");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if ((vm->def->nets != NULL) && !(driver->have_netns)) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
|
|
|
|
_("System lacks NETNS support"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2008-04-10 07:30:52 +00:00
|
|
|
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;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm;
|
|
|
|
virDomainDefPtr def;
|
2008-04-10 07:30:52 +00:00
|
|
|
virDomainPtr dom = NULL;
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (!(def = virDomainDefParseString(conn, driver->caps, xml)))
|
2008-04-10 07:30:52 +00:00
|
|
|
goto return_point;
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if ((def->nets != NULL) && !(driver->have_netns)) {
|
|
|
|
virDomainDefFree(def);
|
|
|
|
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
|
|
|
|
_("System lacks NETNS support"));
|
2008-04-10 07:30:52 +00:00
|
|
|
goto return_point;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
|
|
|
|
if (!(vm = virDomainAssignDef(conn, &driver->domains, def))) {
|
|
|
|
virDomainDefFree(def);
|
|
|
|
goto return_point;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lxcVmStart(conn, driver, vm) < 0) {
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainRemoveInactive(&driver->domains, vm);
|
2008-04-10 07:30:52 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByID(driver->domains, dom->id);
|
2008-04-10 07:30:52 +00:00
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with id %d"), dom->id);
|
2008-08-13 10:52:15 +00:00
|
|
|
return -1;
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
return lxcVmTerminate(dom->conn, driver, vm, 0);
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 06:30:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
lxc_driver_t *driver = (lxc_driver_t*)dom->conn->privateData;
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm = virDomainFindByID(driver->domains, dom->id);
|
2008-05-13 06:30:58 +00:00
|
|
|
|
|
|
|
if (!vm) {
|
|
|
|
lxcError(dom->conn, dom, VIR_ERR_INVALID_DOMAIN,
|
|
|
|
_("no domain with id %d"), dom->id);
|
2008-08-13 10:52:15 +00:00
|
|
|
return -1;
|
2008-05-13 06:30:58 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
return lxcVmTerminate(dom->conn, driver, vm, SIGKILL);
|
2008-04-10 07:30:52 +00:00
|
|
|
}
|
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();
|
2008-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm;
|
2008-03-31 12:02:12 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if ((lxc_driver->caps = lxcCapsInit()) == NULL) {
|
2008-03-27 09:34:06 +00:00
|
|
|
lxcShutdown();
|
2008-03-21 15:03:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:50:55 +00:00
|
|
|
if (virDomainLoadAllConfigs(NULL,
|
|
|
|
lxc_driver->caps,
|
|
|
|
&lxc_driver->domains,
|
|
|
|
lxc_driver->configDir,
|
|
|
|
lxc_driver->autostartDir) < 0) {
|
|
|
|
lxcShutdown();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vm = lxc_driver->domains;
|
2008-08-13 10:52:15 +00:00
|
|
|
while (vm) {
|
2008-08-20 20:55:32 +00:00
|
|
|
char *config = NULL;
|
|
|
|
virDomainDefPtr tmp;
|
2008-08-13 10:52:15 +00:00
|
|
|
int rc;
|
|
|
|
if ((vm->monitor = lxcMonitorClient(NULL, lxc_driver, vm)) < 0) {
|
|
|
|
vm = vm->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read pid from controller */
|
|
|
|
if ((rc = virFileReadPid(lxc_driver->stateDir, vm->def->name, &vm->pid)) != 0) {
|
|
|
|
close(vm->monitor);
|
|
|
|
vm->monitor = -1;
|
|
|
|
vm = vm->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-08-20 20:55:32 +00:00
|
|
|
if ((config = virDomainConfigFile(NULL,
|
|
|
|
lxc_driver->stateDir,
|
|
|
|
vm->def->name)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Try and load the live config */
|
|
|
|
tmp = virDomainDefParseFile(NULL, lxc_driver->caps, config);
|
|
|
|
VIR_FREE(config);
|
|
|
|
if (tmp) {
|
|
|
|
vm->newDef = vm->def;
|
|
|
|
vm->def = tmp;
|
|
|
|
}
|
|
|
|
|
2008-08-13 10:52:15 +00:00
|
|
|
if (vm->pid != 0) {
|
|
|
|
vm->def->id = vm->pid;
|
|
|
|
vm->state = VIR_DOMAIN_RUNNING;
|
|
|
|
} else {
|
|
|
|
vm->def->id = -1;
|
|
|
|
close(vm->monitor);
|
|
|
|
vm->monitor = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vm = vm->next;
|
|
|
|
}
|
|
|
|
|
2008-03-21 15:03:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lxcFreeDriver(lxc_driver_t *driver)
|
|
|
|
{
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(driver->configDir);
|
2008-08-13 12:50:55 +00:00
|
|
|
VIR_FREE(driver->autostartDir);
|
2008-06-06 11:09:57 +00:00
|
|
|
VIR_FREE(driver->stateDir);
|
2008-08-13 10:52:15 +00:00
|
|
|
VIR_FREE(driver->logDir);
|
2008-06-06 11:09:57 +00:00
|
|
|
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-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr vm;
|
2008-03-31 12:02:12 +00:00
|
|
|
if (lxc_driver == NULL)
|
2008-03-31 14:38:12 +00:00
|
|
|
return(-1);
|
2008-08-13 12:50:55 +00:00
|
|
|
vm = lxc_driver->domains;
|
|
|
|
while (vm) {
|
|
|
|
virDomainObjPtr next = vm->next;
|
|
|
|
virDomainObjFree(vm);
|
|
|
|
vm = next;
|
|
|
|
}
|
2008-03-27 09:34:06 +00:00
|
|
|
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-08-13 12:50:55 +00:00
|
|
|
virDomainObjPtr dom;
|
|
|
|
|
2008-03-31 12:02:12 +00:00
|
|
|
if (lxc_driver == NULL)
|
|
|
|
return(0);
|
2008-08-13 12:50:55 +00:00
|
|
|
|
|
|
|
dom = lxc_driver->domains;
|
|
|
|
while (dom) {
|
|
|
|
if (virDomainIsActive(dom))
|
|
|
|
return 1;
|
|
|
|
dom = dom->next;
|
|
|
|
}
|
2008-03-27 09:34:06 +00:00
|
|
|
|
|
|
|
/* Otherwise we're happy to deal with a shutdown */
|
2008-03-21 15:03:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-03 17:21:27 +00:00
|
|
|
static int lxcVersion(virConnectPtr conn, unsigned long *version)
|
|
|
|
{
|
|
|
|
struct utsname ver;
|
|
|
|
int maj;
|
|
|
|
int min;
|
|
|
|
int rev;
|
|
|
|
|
|
|
|
if (uname(&ver) != 0) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("uname(): %m"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sscanf(ver.release, "%i.%i.%i", &maj, &min, &rev) != 3) {
|
|
|
|
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown release: %s"), ver.release);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*version = (maj * 1000 * 1000) + (min * 1000) + rev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
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 */
|
2008-09-03 17:21:27 +00:00
|
|
|
lxcVersion, /* version */
|
2008-03-21 15:03:37 +00:00
|
|
|
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-08-13 10:52:15 +00:00
|
|
|
NULL,
|
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;
|
|
|
|
}
|