2009-04-17 16:09:07 +00:00
|
|
|
/** @file vbox_driver.c
|
|
|
|
* Core driver methods for managing VirtualBox VM's
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-07-06 20:40:19 +00:00
|
|
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
2009-04-17 16:09:07 +00:00
|
|
|
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of a free software library; you can redistribute
|
|
|
|
* it and/or modify it under the terms of the GNU Lesser General
|
|
|
|
* Public License version 2.1 as published by the Free Software
|
|
|
|
* Foundation and shipped in the "COPYING" file with this library.
|
|
|
|
* The library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY of any kind.
|
|
|
|
*
|
|
|
|
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if
|
|
|
|
* any license choice other than GPL or LGPL is available it will
|
|
|
|
* apply instead, Sun elects to use only the Lesser General Public
|
|
|
|
* License version 2.1 (LGPLv2) at this time for any software where
|
|
|
|
* a choice of LGPL license versions is made available with the
|
|
|
|
* language indicating that LGPLv2 or any later version may be used,
|
|
|
|
* or where a choice of which version of the LGPL is applied is
|
|
|
|
* otherwise unspecified.
|
|
|
|
*
|
|
|
|
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
|
|
|
* Clara, CA 95054 USA or visit http://www.sun.com if you need
|
|
|
|
* additional information or have any questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2010-01-15 09:35:41 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2009-04-17 16:09:07 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "vbox_driver.h"
|
2010-12-14 22:07:57 +00:00
|
|
|
#include "vbox_glue.h"
|
2009-05-12 15:35:18 +00:00
|
|
|
#include "virterror_internal.h"
|
2010-12-14 22:07:57 +00:00
|
|
|
#include "util.h"
|
2009-04-17 16:09:07 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_VBOX
|
|
|
|
|
|
|
|
|
|
|
|
extern virDriver vbox22Driver;
|
2009-05-15 14:00:50 +00:00
|
|
|
extern virNetworkDriver vbox22NetworkDriver;
|
2009-09-04 14:28:52 +00:00
|
|
|
extern virStorageDriver vbox22StorageDriver;
|
2009-07-24 16:12:16 +00:00
|
|
|
extern virDriver vbox30Driver;
|
|
|
|
extern virNetworkDriver vbox30NetworkDriver;
|
2009-09-04 14:28:52 +00:00
|
|
|
extern virStorageDriver vbox30StorageDriver;
|
2009-12-04 13:49:45 +00:00
|
|
|
extern virDriver vbox31Driver;
|
|
|
|
extern virNetworkDriver vbox31NetworkDriver;
|
|
|
|
extern virStorageDriver vbox31StorageDriver;
|
2010-05-26 11:54:16 +00:00
|
|
|
extern virDriver vbox32Driver;
|
|
|
|
extern virNetworkDriver vbox32NetworkDriver;
|
|
|
|
extern virStorageDriver vbox32StorageDriver;
|
2010-12-27 22:35:30 +00:00
|
|
|
extern virDriver vbox40Driver;
|
|
|
|
extern virNetworkDriver vbox40NetworkDriver;
|
|
|
|
extern virStorageDriver vbox40StorageDriver;
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2009-05-12 15:35:18 +00:00
|
|
|
static virDriver vboxDriverDummy;
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_VBOX
|
|
|
|
|
2010-04-02 22:38:57 +00:00
|
|
|
#define vboxError(code, ...) \
|
2011-04-16 08:30:22 +00:00
|
|
|
virReportErrorHelper(VIR_FROM_VBOX, code, __FILE__, \
|
2010-03-01 23:38:28 +00:00
|
|
|
__FUNCTION__, __LINE__, __VA_ARGS__)
|
2009-04-17 16:09:07 +00:00
|
|
|
|
|
|
|
int vboxRegister(void) {
|
2009-05-06 13:51:19 +00:00
|
|
|
virDriverPtr driver;
|
2009-05-15 14:00:50 +00:00
|
|
|
virNetworkDriverPtr networkDriver;
|
2009-09-04 14:28:52 +00:00
|
|
|
virStorageDriverPtr storageDriver;
|
2009-05-06 13:51:19 +00:00
|
|
|
uint32_t uVersion;
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2009-05-12 15:35:18 +00:00
|
|
|
/*
|
|
|
|
* If the glue layer does not initialize, we register a driver
|
|
|
|
* with a dummy open method, so we can report nicer errors
|
|
|
|
* if the user requests a vbox:// URI which we know will
|
|
|
|
* never work
|
2009-05-06 13:51:19 +00:00
|
|
|
*/
|
2009-05-12 15:35:18 +00:00
|
|
|
driver = &vboxDriverDummy;
|
2009-05-15 14:00:50 +00:00
|
|
|
networkDriver = &vbox22NetworkDriver;
|
2009-09-04 14:28:52 +00:00
|
|
|
storageDriver = &vbox22StorageDriver;
|
2009-04-17 16:09:07 +00:00
|
|
|
|
2009-05-06 13:51:19 +00:00
|
|
|
/* Init the glue and get the API version. */
|
2010-12-14 22:07:57 +00:00
|
|
|
if (VBoxCGlueInit(&uVersion) == 0) {
|
2011-02-16 23:37:57 +00:00
|
|
|
VIR_DEBUG("VBoxCGlueInit found API version: %d.%d.%d (%u)",
|
2009-05-06 13:51:19 +00:00
|
|
|
uVersion / 1000000,
|
|
|
|
uVersion % 1000000 / 1000,
|
|
|
|
uVersion % 1000,
|
|
|
|
uVersion);
|
|
|
|
|
|
|
|
/* Select driver implementation based on version.
|
|
|
|
* Note that the VirtualBox development usually happens at build
|
|
|
|
* number 51, thus the version ranges in the if statements below.
|
2009-04-17 16:09:07 +00:00
|
|
|
*/
|
2009-05-06 13:51:19 +00:00
|
|
|
if (uVersion >= 2001052 && uVersion < 2002051) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("VirtualBox API version: 2.2");
|
2009-05-06 13:51:19 +00:00
|
|
|
driver = &vbox22Driver;
|
2009-05-15 14:00:50 +00:00
|
|
|
networkDriver = &vbox22NetworkDriver;
|
2009-09-04 14:28:52 +00:00
|
|
|
storageDriver = &vbox22StorageDriver;
|
2009-07-24 16:12:16 +00:00
|
|
|
} else if (uVersion >= 2002051 && uVersion < 3000051) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("VirtualBox API version: 3.0");
|
2009-07-24 16:12:16 +00:00
|
|
|
driver = &vbox30Driver;
|
|
|
|
networkDriver = &vbox30NetworkDriver;
|
2009-09-04 14:28:52 +00:00
|
|
|
storageDriver = &vbox30StorageDriver;
|
2009-12-04 13:49:45 +00:00
|
|
|
} else if (uVersion >= 3000051 && uVersion < 3001051) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("VirtualBox API version: 3.1");
|
2009-12-04 13:49:45 +00:00
|
|
|
driver = &vbox31Driver;
|
|
|
|
networkDriver = &vbox31NetworkDriver;
|
|
|
|
storageDriver = &vbox31StorageDriver;
|
2010-05-26 11:54:16 +00:00
|
|
|
} else if (uVersion >= 3001051 && uVersion < 3002051) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("VirtualBox API version: 3.2");
|
2010-05-26 11:54:16 +00:00
|
|
|
driver = &vbox32Driver;
|
|
|
|
networkDriver = &vbox32NetworkDriver;
|
|
|
|
storageDriver = &vbox32StorageDriver;
|
2010-12-27 22:35:30 +00:00
|
|
|
} else if (uVersion >= 3002051 && uVersion < 4000051) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("VirtualBox API version: 4.0");
|
2010-12-27 22:35:30 +00:00
|
|
|
driver = &vbox40Driver;
|
|
|
|
networkDriver = &vbox40NetworkDriver;
|
|
|
|
storageDriver = &vbox40StorageDriver;
|
2009-05-06 13:51:19 +00:00
|
|
|
} else {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("Unsupport VirtualBox API version");
|
2009-05-06 13:51:19 +00:00
|
|
|
}
|
|
|
|
|
2009-04-17 16:09:07 +00:00
|
|
|
} else {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("VBoxCGlueInit failed, using dummy driver");
|
2009-04-17 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virRegisterDriver(driver) < 0)
|
|
|
|
return -1;
|
2009-05-15 14:00:50 +00:00
|
|
|
if (virRegisterNetworkDriver(networkDriver) < 0)
|
|
|
|
return -1;
|
2009-09-04 14:28:52 +00:00
|
|
|
if (virRegisterStorageDriver(storageDriver) < 0)
|
|
|
|
return -1;
|
2009-04-17 16:09:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-12 15:35:18 +00:00
|
|
|
|
|
|
|
static virDrvOpenStatus vboxOpenDummy(virConnectPtr conn,
|
|
|
|
virConnectAuthPtr auth ATTRIBUTE_UNUSED,
|
2011-07-06 20:40:19 +00:00
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
2009-05-12 15:35:18 +00:00
|
|
|
uid_t uid = getuid();
|
|
|
|
|
|
|
|
if (conn->uri == NULL ||
|
|
|
|
conn->uri->scheme == NULL ||
|
|
|
|
STRNEQ (conn->uri->scheme, "vbox") ||
|
|
|
|
conn->uri->server != NULL)
|
|
|
|
return VIR_DRV_OPEN_DECLINED;
|
|
|
|
|
|
|
|
if (conn->uri->path == NULL || STREQ(conn->uri->path, "")) {
|
2010-04-02 22:38:57 +00:00
|
|
|
vboxError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2009-05-12 15:35:18 +00:00
|
|
|
_("no VirtualBox driver path specified (try vbox:///session)"));
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uid != 0) {
|
|
|
|
if (STRNEQ (conn->uri->path, "/session")) {
|
2010-04-02 22:38:57 +00:00
|
|
|
vboxError(VIR_ERR_INTERNAL_ERROR,
|
2009-05-12 15:35:18 +00:00
|
|
|
_("unknown driver path '%s' specified (try vbox:///session)"), conn->uri->path);
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
} else { /* root */
|
|
|
|
if (STRNEQ (conn->uri->path, "/system") &&
|
|
|
|
STRNEQ (conn->uri->path, "/session")) {
|
2010-04-02 22:38:57 +00:00
|
|
|
vboxError(VIR_ERR_INTERNAL_ERROR,
|
2009-05-12 15:35:18 +00:00
|
|
|
_("unknown driver path '%s' specified (try vbox:///system)"), conn->uri->path);
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 22:38:57 +00:00
|
|
|
vboxError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2009-05-12 15:35:18 +00:00
|
|
|
_("unable to initialize VirtualBox driver API"));
|
|
|
|
return VIR_DRV_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virDriver vboxDriverDummy = {
|
|
|
|
VIR_DRV_VBOX,
|
|
|
|
"VBOX",
|
|
|
|
.open = vboxOpenDummy,
|
|
|
|
};
|