2005-11-02 12:50:21 +00:00
|
|
|
/*
|
2006-02-09 17:45:11 +00:00
|
|
|
* libvirt.c: Main interfaces for the libvirt library to handle virtualization
|
2005-11-02 12:50:21 +00:00
|
|
|
* domains from a process running in domain 0
|
|
|
|
*
|
2006-02-09 17:45:11 +00:00
|
|
|
* Copyright (C) 2005,2006 Red Hat, Inc.
|
2005-11-02 12:50:21 +00:00
|
|
|
*
|
|
|
|
* See COPYING.LIB for the License of this software
|
|
|
|
*
|
|
|
|
* Daniel Veillard <veillard@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2006-02-09 17:45:11 +00:00
|
|
|
#include "libvirt.h"
|
2005-11-02 12:50:21 +00:00
|
|
|
|
2005-11-02 13:19:10 +00:00
|
|
|
#include <stdio.h>
|
2005-11-30 13:20:53 +00:00
|
|
|
#include <stdlib.h>
|
2005-12-06 13:47:40 +00:00
|
|
|
#include <string.h>
|
2006-01-18 10:37:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2006-03-23 15:42:10 +00:00
|
|
|
|
2005-11-30 13:20:53 +00:00
|
|
|
#include <xs.h>
|
2006-03-23 15:42:10 +00:00
|
|
|
|
2005-11-02 12:50:21 +00:00
|
|
|
#include "internal.h"
|
2006-03-20 17:49:28 +00:00
|
|
|
#include "driver.h"
|
2006-01-23 22:55:41 +00:00
|
|
|
#include "xen_internal.h"
|
2006-01-17 16:56:17 +00:00
|
|
|
#include "xend_internal.h"
|
2006-03-23 15:42:10 +00:00
|
|
|
#include "xs_internal.h"
|
2005-11-30 13:20:53 +00:00
|
|
|
#include "hash.h"
|
2006-02-16 22:50:52 +00:00
|
|
|
#include "xml.h"
|
2005-11-30 13:20:53 +00:00
|
|
|
|
2005-11-02 12:50:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* - use lock to protect against concurrent accesses ?
|
|
|
|
* - use reference counting to garantee coherent pointer state ?
|
2005-11-10 16:12:31 +00:00
|
|
|
* - memory wrappers for malloc/free ?
|
2005-11-02 12:50:21 +00:00
|
|
|
*/
|
|
|
|
|
2006-04-05 09:31:29 +00:00
|
|
|
static int virDomainFreeName(virDomainPtr domain, const char *name);
|
|
|
|
|
2006-03-27 15:24:36 +00:00
|
|
|
static virDriverPtr virDriverTab[MAX_DRIVERS];
|
|
|
|
static int initialized = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virInitialize:
|
|
|
|
*
|
|
|
|
* Initialize the library. It's better to call this routine at startup
|
|
|
|
* in multithreaded applications to avoid potential race when initializing
|
|
|
|
* the library.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success, -1 in case of error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virInitialize(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (initialized)
|
|
|
|
return(0);
|
2006-03-28 14:41:04 +00:00
|
|
|
initialized = 1;
|
2006-03-27 15:24:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* should not be needed but...
|
|
|
|
*/
|
|
|
|
for (i = 0;i < MAX_DRIVERS;i++)
|
|
|
|
virDriverTab[i] = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that the order is important the first ones have a higher priority
|
|
|
|
*/
|
|
|
|
xenHypervisorRegister();
|
|
|
|
xenDaemonRegister();
|
|
|
|
xenStoreRegister();
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
/**
|
|
|
|
* virLibConnError:
|
|
|
|
* @conn: the connection if available
|
|
|
|
* @error: the error noumber
|
|
|
|
* @info: extra information string
|
|
|
|
*
|
|
|
|
* Handle an error at the connection level
|
|
|
|
*/
|
|
|
|
static void
|
2006-03-15 12:13:25 +00:00
|
|
|
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
const char *errmsg;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (error == VIR_ERR_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
errmsg = __virErrorMsg(error, info);
|
|
|
|
__virRaiseError(conn, NULL, VIR_FROM_NONE, error, VIR_ERR_ERROR,
|
|
|
|
errmsg, info, NULL, 0, 0, errmsg, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virLibConnError:
|
|
|
|
* @conn: the connection if available
|
|
|
|
* @error: the error noumber
|
|
|
|
* @info: extra information string
|
|
|
|
*
|
|
|
|
* Handle an error at the connection level
|
|
|
|
*/
|
|
|
|
static void
|
2006-03-15 12:13:25 +00:00
|
|
|
virLibDomainError(virDomainPtr domain, virErrorNumber error,
|
|
|
|
const char *info)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
virConnectPtr conn = NULL;
|
|
|
|
const char *errmsg;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (error == VIR_ERR_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
errmsg = __virErrorMsg(error, info);
|
|
|
|
if (error != VIR_ERR_INVALID_DOMAIN) {
|
|
|
|
conn = domain->conn;
|
|
|
|
}
|
|
|
|
__virRaiseError(conn, domain, VIR_FROM_DOM, error, VIR_ERR_ERROR,
|
|
|
|
errmsg, info, NULL, 0, 0, errmsg, info);
|
|
|
|
}
|
|
|
|
|
2006-03-27 15:24:36 +00:00
|
|
|
/**
|
|
|
|
* virRegisterDriver:
|
|
|
|
* @driver: pointer to a driver block
|
|
|
|
*
|
|
|
|
* Register a virtualization driver
|
|
|
|
*
|
|
|
|
* Returns the driver priority or -1 in case of error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virRegisterDriver(virDriverPtr driver)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-03-28 14:41:04 +00:00
|
|
|
if (!initialized)
|
|
|
|
virInitialize();
|
|
|
|
|
2006-03-27 15:24:36 +00:00
|
|
|
if (driver == NULL) {
|
|
|
|
virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
for (i = 0;i < MAX_DRIVERS;i++) {
|
|
|
|
if (virDriverTab[i] == driver)
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
for (i = 0;i < MAX_DRIVERS;i++) {
|
|
|
|
if (virDriverTab[i] == NULL) {
|
|
|
|
virDriverTab[i] = driver;
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2005-12-08 15:08:46 +00:00
|
|
|
/**
|
|
|
|
* virGetVersion:
|
|
|
|
* @libVer: return value for the library version (OUT)
|
|
|
|
* @type: hypervisor type
|
|
|
|
* @typeVer: return value for the version of the hypervisor (OUT)
|
|
|
|
*
|
|
|
|
* Provides two information back, @libVer is the version of the library
|
|
|
|
* while @typeVer will be the version of the hypervisor type @type against
|
|
|
|
* which the library was compiled. If @type is NULL, "Xen" is assumed, if
|
|
|
|
* @type is unknown or not availble, an error code will be returned and
|
|
|
|
* @typeVer will be 0.
|
|
|
|
*
|
|
|
|
* Returns -1 in case of failure, 0 otherwise, and values for @libVer and
|
|
|
|
* @typeVer have the format major * 1,000,000 + minor * 1,000 + release.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virGetVersion(unsigned long *libVer, const char *type,
|
|
|
|
unsigned long *typeVer)
|
|
|
|
{
|
2006-03-28 14:41:04 +00:00
|
|
|
if (!initialized)
|
|
|
|
virInitialize();
|
|
|
|
|
2005-12-08 15:08:46 +00:00
|
|
|
if (libVer == NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2005-12-08 15:08:46 +00:00
|
|
|
*libVer = LIBVIR_VERSION_NUMBER;
|
|
|
|
|
|
|
|
if (typeVer != NULL) {
|
2006-03-15 12:13:25 +00:00
|
|
|
if ((type == NULL) || (!strcasecmp(type, "Xen"))) {
|
|
|
|
if ((DOM0_INTERFACE_VERSION & 0xFFFF0000) == (0xAAAA0000)) {
|
|
|
|
/* one time glitch hopefully ! */
|
2005-12-08 15:08:46 +00:00
|
|
|
*typeVer = 2 * 1000000 +
|
2006-03-15 12:13:25 +00:00
|
|
|
((DOM0_INTERFACE_VERSION >> 8) & 0xFF) * 1000 +
|
|
|
|
(DOM0_INTERFACE_VERSION & 0xFF);
|
|
|
|
} else {
|
|
|
|
*typeVer = (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
|
|
|
|
((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
|
|
|
|
(DOM0_INTERFACE_VERSION & 0xFFFF);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*typeVer = 0;
|
|
|
|
virLibConnError(NULL, VIR_ERR_NO_SUPPORT, "type");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
2005-12-08 15:08:46 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 12:50:21 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virConnectOpen:
|
2005-11-02 12:50:21 +00:00
|
|
|
* @name: optional argument currently unused, pass NULL
|
|
|
|
*
|
|
|
|
* This function should be called first to get a connection to the
|
2005-11-23 07:47:13 +00:00
|
|
|
* Hypervisor and xen store
|
2005-11-02 12:50:21 +00:00
|
|
|
*
|
|
|
|
* Returns a pointer to the hypervisor connection or NULL in case of error
|
|
|
|
*/
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectPtr
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectOpen(const char *name)
|
|
|
|
{
|
2006-03-29 12:46:03 +00:00
|
|
|
int i, res;
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectPtr ret = NULL;
|
2005-11-30 13:20:53 +00:00
|
|
|
|
2006-03-28 14:41:04 +00:00
|
|
|
if (!initialized)
|
|
|
|
virInitialize();
|
|
|
|
|
2006-03-20 17:49:28 +00:00
|
|
|
ret = (virConnectPtr) malloc(sizeof(virConnect));
|
|
|
|
if (ret == NULL) {
|
|
|
|
virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
memset(ret, 0, sizeof(virConnect));
|
|
|
|
ret->magic = VIR_CONNECT_MAGIC;
|
2006-03-29 12:46:03 +00:00
|
|
|
ret->nb_drivers = 0;
|
2006-03-20 17:49:28 +00:00
|
|
|
|
2006-03-29 12:46:03 +00:00
|
|
|
for (i = 0;i < MAX_DRIVERS;i++) {
|
|
|
|
if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
|
|
|
|
res = virDriverTab[i]->open(ret, name, 0);
|
|
|
|
/*
|
|
|
|
* For a default connect to Xen make sure we manage to contact
|
|
|
|
* all related drivers.
|
|
|
|
*/
|
|
|
|
if ((res < 0) && (name == NULL) &&
|
|
|
|
(!strcmp(virDriverTab[i]->name, "Xen")))
|
|
|
|
goto failed;
|
|
|
|
if (res == 0)
|
|
|
|
ret->drivers[ret->nb_drivers++] = virDriverTab[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret->nb_drivers == 0) {
|
|
|
|
/* we failed to find an adequate driver */
|
|
|
|
virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
|
|
|
|
goto failed;
|
|
|
|
}
|
2006-03-23 15:42:10 +00:00
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->domains = virHashCreate(20);
|
2005-11-30 13:20:53 +00:00
|
|
|
if (ret->domains == NULL)
|
|
|
|
goto failed;
|
2006-04-05 09:31:29 +00:00
|
|
|
ret->domains_mux = xmlNewMutex();
|
|
|
|
if (ret->domains_mux == NULL)
|
|
|
|
goto failed;
|
2006-03-23 15:42:10 +00:00
|
|
|
ret->flags = 0;
|
2005-11-10 16:12:31 +00:00
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2006-03-23 15:42:10 +00:00
|
|
|
|
|
|
|
failed:
|
|
|
|
if (ret != NULL) {
|
2006-03-29 12:46:03 +00:00
|
|
|
for (i = 0;i < ret->nb_drivers;i++) {
|
|
|
|
if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
|
|
|
|
ret->drivers[i]->close(ret);
|
|
|
|
}
|
2006-04-05 09:31:29 +00:00
|
|
|
if (ret->domains != NULL)
|
|
|
|
virHashFree(ret->domains, (virHashDeallocator) virDomainFreeName);
|
|
|
|
if (ret->domains_mux != NULL)
|
|
|
|
xmlFreeMutex(ret->domains_mux);
|
2005-12-01 16:35:42 +00:00
|
|
|
free(ret);
|
2006-03-23 15:42:10 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2005-12-01 16:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virConnectOpenReadOnly:
|
2005-12-01 16:35:42 +00:00
|
|
|
* @name: optional argument currently unused, pass NULL
|
|
|
|
*
|
2006-01-19 10:23:15 +00:00
|
|
|
* This function should be called first to get a restricted connection to the
|
|
|
|
* libbrary functionalities. The set of APIs usable are then restricted
|
|
|
|
* on the available methods to control the domains.
|
2005-12-01 16:35:42 +00:00
|
|
|
*
|
|
|
|
* Returns a pointer to the hypervisor connection or NULL in case of error
|
|
|
|
*/
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectPtr
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectOpenReadOnly(const char *name)
|
|
|
|
{
|
2006-03-29 12:46:03 +00:00
|
|
|
int i, res;
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectPtr ret = NULL;
|
2005-12-01 16:35:42 +00:00
|
|
|
|
2006-03-28 14:41:04 +00:00
|
|
|
if (!initialized)
|
|
|
|
virInitialize();
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
ret = (virConnectPtr) malloc(sizeof(virConnect));
|
2006-02-27 16:27:18 +00:00
|
|
|
if (ret == NULL) {
|
|
|
|
virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
|
2005-12-01 16:35:42 +00:00
|
|
|
goto failed;
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-02-23 11:26:17 +00:00
|
|
|
memset(ret, 0, sizeof(virConnect));
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->magic = VIR_CONNECT_MAGIC;
|
2006-03-29 12:46:03 +00:00
|
|
|
ret->nb_drivers = 0;
|
2006-03-20 17:49:28 +00:00
|
|
|
|
2006-03-29 12:46:03 +00:00
|
|
|
for (i = 0;i < MAX_DRIVERS;i++) {
|
|
|
|
if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
|
|
|
|
res = virDriverTab[i]->open(ret, name,
|
|
|
|
VIR_DRV_OPEN_QUIET | VIR_DRV_OPEN_RO);
|
|
|
|
if (res == 0)
|
|
|
|
ret->drivers[ret->nb_drivers++] = virDriverTab[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret->nb_drivers == 0) {
|
|
|
|
if (name == NULL)
|
|
|
|
virLibConnError(NULL, VIR_ERR_NO_CONNECT,
|
|
|
|
"could not connect to Xen Daemon nor Xen Store");
|
|
|
|
else
|
|
|
|
/* we failed to find an adequate driver */
|
|
|
|
virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
|
|
|
|
goto failed;
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2005-12-01 16:35:42 +00:00
|
|
|
|
2006-03-23 15:42:10 +00:00
|
|
|
ret->domains = virHashCreate(20);
|
|
|
|
if (ret->domains == NULL)
|
|
|
|
goto failed;
|
2006-04-05 09:31:29 +00:00
|
|
|
ret->domains_mux = xmlNewMutex();
|
|
|
|
if (ret->domains_mux == NULL)
|
|
|
|
goto failed;
|
2006-03-23 15:42:10 +00:00
|
|
|
ret->flags = VIR_CONNECT_RO;
|
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2006-03-23 15:42:10 +00:00
|
|
|
|
|
|
|
failed:
|
2006-01-19 10:23:15 +00:00
|
|
|
if (ret != NULL) {
|
2006-03-29 12:46:03 +00:00
|
|
|
for (i = 0;i < ret->nb_drivers;i++) {
|
|
|
|
if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
|
|
|
|
ret->drivers[i]->close(ret);
|
|
|
|
}
|
2006-04-05 09:31:29 +00:00
|
|
|
if (ret->domains != NULL)
|
|
|
|
virHashFree(ret->domains, (virHashDeallocator) virDomainFreeName);
|
|
|
|
if (ret->domains_mux != NULL)
|
|
|
|
xmlFreeMutex(ret->domains_mux);
|
2005-12-01 16:35:42 +00:00
|
|
|
free(ret);
|
2006-01-19 10:23:15 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2005-11-02 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2005-11-30 13:20:53 +00:00
|
|
|
/**
|
2005-12-08 16:42:56 +00:00
|
|
|
* virDomainFreeName:
|
2005-11-30 13:20:53 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Destroy the domain object, this is just used by the domain hash callback.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
static int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return (virDomainFree(domain));
|
2005-11-30 13:20:53 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 12:50:21 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virConnectClose:
|
2005-11-02 12:50:21 +00:00
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
*
|
|
|
|
* This function closes the connection to the Hypervisor. This should
|
|
|
|
* not be called if further interaction with the Hypervisor are needed
|
|
|
|
* especially if there is running domain which need further monitoring by
|
|
|
|
* the application.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or -1 in case of error.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectClose(virConnectPtr conn)
|
|
|
|
{
|
2006-03-29 12:46:03 +00:00
|
|
|
int i;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2005-12-08 16:42:56 +00:00
|
|
|
virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
|
2006-03-23 15:42:10 +00:00
|
|
|
conn->domains = NULL;
|
2006-04-05 09:31:29 +00:00
|
|
|
xmlFreeMutex(conn->domains_mux);
|
|
|
|
conn->domains_mux = NULL;
|
2006-03-29 12:46:03 +00:00
|
|
|
for (i = 0;i < conn->nb_drivers;i++) {
|
|
|
|
if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL))
|
|
|
|
conn->drivers[i]->close(conn);
|
|
|
|
}
|
2006-03-23 15:42:10 +00:00
|
|
|
conn->magic = -1;
|
2005-11-02 12:50:21 +00:00
|
|
|
free(conn);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2005-11-02 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 13:26:52 +00:00
|
|
|
/**
|
|
|
|
* virConnectGetType:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
*
|
|
|
|
* Get the name of the Hypervisor software used.
|
|
|
|
*
|
|
|
|
* Returns NULL in case of error, a static zero terminated string otherwise.
|
|
|
|
*/
|
|
|
|
const char *
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectGetType(virConnectPtr conn)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
2006-03-29 12:46:03 +00:00
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
return ("Xen");
|
2005-12-08 13:26:52 +00:00
|
|
|
}
|
|
|
|
|
2005-11-02 12:50:21 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virConnectGetVersion:
|
2005-11-02 12:50:21 +00:00
|
|
|
* @conn: pointer to the hypervisor connection
|
2005-12-08 15:08:46 +00:00
|
|
|
* @hvVer: return value for the version of the running hypervisor (OUT)
|
2005-11-02 12:50:21 +00:00
|
|
|
*
|
2005-12-08 13:26:52 +00:00
|
|
|
* Get the version level of the Hypervisor running. This may work only with
|
|
|
|
* hypervisor call, i.e. with priviledged access to the hypervisor, not
|
|
|
|
* with a Read-Only connection.
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
2005-12-08 15:08:46 +00:00
|
|
|
* Returns -1 in case of error, 0 otherwise. if the version can't be
|
|
|
|
* extracted by lack of capacities returns 0 and @hvVer is 0, otherwise
|
|
|
|
* @hvVer value is major * 1,000,000 + minor * 1,000 + release
|
2005-11-07 17:16:18 +00:00
|
|
|
*/
|
2005-12-08 15:08:46 +00:00
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
|
|
|
|
{
|
2005-12-08 15:08:46 +00:00
|
|
|
unsigned long ver;
|
2005-12-08 13:26:52 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (hvVer == NULL) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2005-12-08 13:26:52 +00:00
|
|
|
/* this can't be extracted from the Xenstore */
|
2005-12-08 15:08:46 +00:00
|
|
|
if (conn->handle < 0) {
|
|
|
|
*hvVer = 0;
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2005-12-08 15:08:46 +00:00
|
|
|
}
|
2005-12-08 13:26:52 +00:00
|
|
|
|
2006-03-20 17:49:28 +00:00
|
|
|
ver = xenHypervisorGetVersion(conn, hvVer);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2005-12-02 14:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virConnectListDomains:
|
2005-12-02 14:16:21 +00:00
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @ids: array to collect the list of IDs of active domains
|
|
|
|
* @maxids: size of @ids
|
|
|
|
*
|
|
|
|
* Collect the list of active domains, and store their ID in @maxids
|
|
|
|
*
|
|
|
|
* Returns the number of domain found or -1 in case of error
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
|
|
|
|
{
|
2005-12-02 14:16:21 +00:00
|
|
|
int ret = -1;
|
2006-03-23 15:42:10 +00:00
|
|
|
unsigned int i;
|
2005-12-02 14:16:21 +00:00
|
|
|
long id;
|
2006-03-23 15:42:10 +00:00
|
|
|
char **idlist = NULL;
|
2005-12-02 14:16:21 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if ((ids == NULL) || (maxids <= 0)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-03-23 15:42:10 +00:00
|
|
|
/*
|
|
|
|
* try first though the Xen Daemon
|
|
|
|
*/
|
2006-03-22 13:44:01 +00:00
|
|
|
idlist = xenDaemonListDomains(conn);
|
2006-01-19 10:23:15 +00:00
|
|
|
if (idlist != NULL) {
|
2006-03-15 12:13:25 +00:00
|
|
|
for (ret = 0, i = 0; (idlist[i] != NULL) && (ret < maxids); i++) {
|
2006-03-22 13:44:01 +00:00
|
|
|
id = xenDaemonDomainLookupByName_ids(conn, idlist[i], NULL);
|
2006-03-15 12:13:25 +00:00
|
|
|
if (id >= 0)
|
|
|
|
ids[ret++] = (int) id;
|
|
|
|
}
|
2006-03-23 15:42:10 +00:00
|
|
|
free(idlist);
|
|
|
|
return(ret);
|
2006-01-19 10:23:15 +00:00
|
|
|
}
|
2005-12-02 14:16:21 +00:00
|
|
|
|
2006-03-23 15:42:10 +00:00
|
|
|
/*
|
|
|
|
* Then fallback to the XenStore
|
|
|
|
*/
|
|
|
|
ret = xenStoreListDomains(conn, ids, maxids);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2005-11-07 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2005-12-07 11:03:24 +00:00
|
|
|
/**
|
|
|
|
* virConnectNumOfDomains:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
*
|
2005-12-08 16:42:56 +00:00
|
|
|
* Provides the number of active domains.
|
|
|
|
*
|
2005-12-07 11:03:24 +00:00
|
|
|
* Returns the number of domain found or -1 in case of error
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virConnectNumOfDomains(virConnectPtr conn)
|
|
|
|
{
|
2005-12-07 11:03:24 +00:00
|
|
|
int ret = -1;
|
|
|
|
char **idlist = NULL;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2005-12-16 18:41:46 +00:00
|
|
|
|
2006-03-23 15:42:10 +00:00
|
|
|
/* TODO: there must be a way to do that with an hypervisor call too ! */
|
2006-01-19 10:23:15 +00:00
|
|
|
/*
|
|
|
|
* try first with Xend interface
|
|
|
|
*/
|
2006-03-22 13:44:01 +00:00
|
|
|
idlist = xenDaemonListDomains(conn);
|
2006-01-19 10:23:15 +00:00
|
|
|
if (idlist != NULL) {
|
|
|
|
char **tmp = idlist;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (*tmp != NULL) {
|
2006-03-15 12:13:25 +00:00
|
|
|
tmp++;
|
|
|
|
ret++;
|
|
|
|
}
|
2006-03-23 15:42:10 +00:00
|
|
|
free(idlist);
|
|
|
|
return(ret);
|
2005-12-07 11:03:24 +00:00
|
|
|
}
|
2006-03-23 15:42:10 +00:00
|
|
|
/* Then Xen Store */
|
|
|
|
return(xenStoreNumOfDomains(conn));
|
2005-12-07 11:03:24 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 17:16:18 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainCreateLinux:
|
2005-11-07 17:16:18 +00:00
|
|
|
* @conn: pointer to the hypervisor connection
|
2006-02-16 22:50:52 +00:00
|
|
|
* @xmlDesc: an XML description of the domain
|
2005-12-05 11:16:07 +00:00
|
|
|
* @flags: an optional set of virDomainFlags
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
2006-02-20 23:08:47 +00:00
|
|
|
* Launch a new Linux guest domain, based on an XML description similar
|
|
|
|
* to the one returned by virDomainGetXMLDesc()
|
|
|
|
* This function may requires priviledged access to the hypervisor.
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
|
|
|
* Returns a new domain object or NULL in case of failure
|
|
|
|
*/
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainPtr
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainCreateLinux(virConnectPtr conn,
|
2006-02-16 22:50:52 +00:00
|
|
|
const char *xmlDesc,
|
2006-03-15 12:13:25 +00:00
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2006-02-16 22:50:52 +00:00
|
|
|
int ret;
|
|
|
|
char *sexpr;
|
|
|
|
char *name = NULL;
|
|
|
|
virDomainPtr dom;
|
2005-12-16 18:41:46 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (xmlDesc == NULL) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
2006-02-16 22:50:52 +00:00
|
|
|
sexpr = virDomainParseXMLDesc(xmlDesc, &name);
|
|
|
|
if ((sexpr == NULL) || (name == NULL)) {
|
|
|
|
if (sexpr != NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
free(sexpr);
|
2006-02-16 22:50:52 +00:00
|
|
|
if (name != NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
free(name);
|
2006-02-16 22:50:52 +00:00
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-16 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainCreateLinux(conn, sexpr);
|
2006-02-16 22:50:52 +00:00
|
|
|
free(sexpr);
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "Failed to create domain %s\n", name);
|
2006-03-15 12:13:25 +00:00
|
|
|
goto error;
|
2006-02-16 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = xend_wait_for_devices(conn, name);
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "Failed to get devices for domain %s\n", name);
|
2006-03-15 12:13:25 +00:00
|
|
|
goto error;
|
2006-02-16 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
dom = virDomainLookupByName(conn, name);
|
|
|
|
if (dom == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = xenDaemonDomainResume(dom);
|
2006-02-16 22:50:52 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "Failed to resume new domain %s\n", name);
|
2006-03-22 13:44:01 +00:00
|
|
|
xenDaemonDomainDestroy(dom);
|
2006-03-15 12:13:25 +00:00
|
|
|
goto error;
|
2006-02-16 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dom = virDomainLookupByName(conn, name);
|
|
|
|
free(name);
|
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (dom);
|
|
|
|
error:
|
2006-02-16 22:50:52 +00:00
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2005-11-07 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
|
2005-11-30 13:20:53 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainLookupByID:
|
2005-11-30 13:20:53 +00:00
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @id: the domain ID number
|
|
|
|
*
|
|
|
|
* Try to find a domain based on the hypervisor ID number
|
|
|
|
*
|
|
|
|
* Returns a new domain object or NULL in case of failure
|
|
|
|
*/
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainPtr
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainLookupByID(virConnectPtr conn, int id)
|
|
|
|
{
|
2006-01-19 10:23:15 +00:00
|
|
|
char *path = NULL;
|
2006-04-05 09:31:29 +00:00
|
|
|
char **names;
|
|
|
|
char **tmp;
|
|
|
|
int ident;
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainPtr ret;
|
2006-01-19 10:23:15 +00:00
|
|
|
char *name = NULL;
|
2006-02-23 10:13:55 +00:00
|
|
|
unsigned char uuid[16];
|
2005-11-30 13:20:53 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (id < 0) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2005-11-30 13:20:53 +00:00
|
|
|
|
2006-04-05 09:31:29 +00:00
|
|
|
/* retrieve home path of the domain */
|
2006-01-19 10:23:15 +00:00
|
|
|
if (conn->xshandle != NULL) {
|
2006-03-15 12:13:25 +00:00
|
|
|
path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
|
2006-01-19 10:23:15 +00:00
|
|
|
}
|
2006-04-05 09:31:29 +00:00
|
|
|
|
|
|
|
/* path does not contain name, use xend API to retrieve name */
|
|
|
|
names = xenDaemonListDomains(conn);
|
|
|
|
tmp = names;
|
|
|
|
|
|
|
|
if (names != NULL) {
|
|
|
|
while (*tmp != NULL) {
|
|
|
|
ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]);
|
|
|
|
if (ident == id) {
|
|
|
|
name = strdup(*tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
free(names);
|
2005-11-30 13:20:53 +00:00
|
|
|
}
|
2006-01-19 10:23:15 +00:00
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
ret = (virDomainPtr) malloc(sizeof(virDomain));
|
2005-11-30 13:20:53 +00:00
|
|
|
if (ret == NULL) {
|
2006-02-23 10:13:55 +00:00
|
|
|
goto error;
|
2005-11-30 13:20:53 +00:00
|
|
|
}
|
2006-02-23 11:26:17 +00:00
|
|
|
memset(ret, 0, sizeof(virDomain));
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->magic = VIR_DOMAIN_MAGIC;
|
2005-11-30 13:20:53 +00:00
|
|
|
ret->conn = conn;
|
|
|
|
ret->handle = id;
|
2005-12-01 16:35:42 +00:00
|
|
|
ret->path = path;
|
2006-03-23 15:42:10 +00:00
|
|
|
ret->name = name;
|
|
|
|
memcpy(&ret->uuid[0], uuid, 16);
|
2005-12-13 16:22:05 +00:00
|
|
|
if (ret->name == NULL) {
|
2006-02-23 10:13:55 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
|
|
|
error:
|
2006-02-23 10:13:55 +00:00
|
|
|
if (ret != NULL)
|
2006-04-03 13:46:43 +00:00
|
|
|
free(ret);
|
2006-02-23 10:13:55 +00:00
|
|
|
if (path != NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
free(path);
|
|
|
|
return (NULL);
|
2006-02-23 10:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDomainLookupByUUID:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @uuid: the UUID string for the domain
|
|
|
|
*
|
|
|
|
* Try to lookup a domain on the given hypervisor based on its UUID.
|
|
|
|
*
|
|
|
|
* Returns a new domain object or NULL in case of failure
|
|
|
|
*/
|
|
|
|
virDomainPtr
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
|
|
|
|
{
|
2006-02-23 10:13:55 +00:00
|
|
|
virDomainPtr ret;
|
|
|
|
char *name = NULL;
|
|
|
|
char **names;
|
|
|
|
char **tmp;
|
|
|
|
unsigned char ident[16];
|
2006-02-24 22:36:10 +00:00
|
|
|
int id = -1;
|
2006-02-23 10:13:55 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (uuid == NULL) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-22 13:44:01 +00:00
|
|
|
names = xenDaemonListDomains(conn);
|
2006-02-23 10:13:55 +00:00
|
|
|
tmp = names;
|
|
|
|
|
|
|
|
if (names == NULL) {
|
2006-03-15 12:13:25 +00:00
|
|
|
TODO /* try to fallback to xenstore lookup */
|
|
|
|
return (NULL);
|
2005-12-13 16:22:05 +00:00
|
|
|
}
|
2006-02-23 10:13:55 +00:00
|
|
|
while (*tmp != NULL) {
|
2006-03-22 13:44:01 +00:00
|
|
|
id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]);
|
2006-03-15 12:13:25 +00:00
|
|
|
if (id >= 0) {
|
|
|
|
if (!memcmp(uuid, ident, 16)) {
|
|
|
|
name = strdup(*tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp++;
|
2006-02-23 10:13:55 +00:00
|
|
|
}
|
|
|
|
free(names);
|
|
|
|
|
|
|
|
if (name == NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-23 10:13:55 +00:00
|
|
|
|
|
|
|
ret = (virDomainPtr) malloc(sizeof(virDomain));
|
|
|
|
if (ret == NULL) {
|
|
|
|
if (name != NULL)
|
2006-03-15 12:13:25 +00:00
|
|
|
free(name);
|
|
|
|
return (NULL);
|
2006-02-23 10:13:55 +00:00
|
|
|
}
|
2006-02-23 11:26:17 +00:00
|
|
|
memset(ret, 0, sizeof(virDomain));
|
2006-02-23 10:13:55 +00:00
|
|
|
ret->magic = VIR_DOMAIN_MAGIC;
|
|
|
|
ret->conn = conn;
|
|
|
|
ret->handle = id;
|
|
|
|
ret->name = name;
|
2006-02-23 11:26:17 +00:00
|
|
|
ret->path = 0;
|
2006-02-23 10:13:55 +00:00
|
|
|
memcpy(ret->uuid, uuid, 16);
|
2005-11-30 13:20:53 +00:00
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2005-11-30 13:20:53 +00:00
|
|
|
}
|
|
|
|
|
2005-12-12 13:22:20 +00:00
|
|
|
/**
|
|
|
|
* virDomainLookupByName:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @name: name for the domain
|
|
|
|
*
|
2005-12-13 16:22:05 +00:00
|
|
|
* Try to lookup a domain on the given hypervisor based on its name.
|
2005-12-12 13:22:20 +00:00
|
|
|
*
|
|
|
|
* Returns a new domain object or NULL in case of failure
|
|
|
|
*/
|
|
|
|
virDomainPtr
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainLookupByName(virConnectPtr conn, const char *name)
|
|
|
|
{
|
2005-12-12 13:22:20 +00:00
|
|
|
virDomainPtr ret = NULL;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (name == NULL) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2006-01-17 16:56:17 +00:00
|
|
|
/* try first though Xend */
|
2006-03-23 15:42:10 +00:00
|
|
|
ret = xenDaemonDomainLookupByName(conn, name);
|
|
|
|
if (ret != NULL) {
|
|
|
|
return(ret);
|
2006-01-17 16:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* then though the XenStore */
|
2006-03-23 15:42:10 +00:00
|
|
|
ret = xenStoreDomainLookupByName(conn, name);
|
|
|
|
if (ret != NULL) {
|
|
|
|
return(ret);
|
2006-03-15 12:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
2005-12-12 13:22:20 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 17:16:18 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainDestroy:
|
2005-11-07 17:16:18 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Destroy the domain object. The running instance is shutdown if not down
|
|
|
|
* already and all resources used by it are given back to the hypervisor.
|
2005-12-08 16:42:56 +00:00
|
|
|
* The data structure is freed and should not be used thereafter if the
|
|
|
|
* call does not return an error.
|
2006-01-18 10:37:08 +00:00
|
|
|
* This function may requires priviledged access
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
2005-11-02 13:19:10 +00:00
|
|
|
*/
|
2005-11-07 17:16:18 +00:00
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainDestroy(virDomainPtr domain)
|
|
|
|
{
|
2005-12-08 17:16:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-01-18 10:37:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* try first with the xend method
|
|
|
|
*/
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainDestroy(domain);
|
2006-01-18 10:37:08 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
virDomainFree(domain);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2006-01-18 10:37:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 17:49:28 +00:00
|
|
|
ret = xenHypervisorDestroyDomain(domain);
|
2005-12-08 17:16:24 +00:00
|
|
|
if (ret < 0)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
virDomainFree(domain);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2005-12-08 16:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDomainFree:
|
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Free the domain object. The running instance is kept alive.
|
|
|
|
* The data structure is freed and should not be used thereafter.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainFree(virDomainPtr domain)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2005-12-08 16:42:56 +00:00
|
|
|
domain->magic = -1;
|
|
|
|
domain->handle = -1;
|
|
|
|
if (domain->path != NULL)
|
|
|
|
free(domain->path);
|
|
|
|
if (domain->name)
|
|
|
|
free(domain->name);
|
|
|
|
free(domain);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2005-11-07 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainSuspend:
|
2005-11-07 17:16:18 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Suspends an active domain, the process is frozen without further access
|
|
|
|
* to CPU resources and I/O but the memory used by the domain at the
|
2005-12-05 11:16:07 +00:00
|
|
|
* hypervisor level will stay allocated. Use virDomainResume() to reactivate
|
2005-11-07 17:16:18 +00:00
|
|
|
* the domain.
|
2006-01-18 10:37:08 +00:00
|
|
|
* This function may requires priviledged access.
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainSuspend(virDomainPtr domain)
|
|
|
|
{
|
2006-01-18 10:37:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
/* first try though the Xen daemon */
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainSuspend(domain);
|
2006-01-18 10:37:08 +00:00
|
|
|
if (ret == 0)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2006-01-18 10:37:08 +00:00
|
|
|
|
|
|
|
/* then try a direct hypervisor access */
|
2006-03-20 17:49:28 +00:00
|
|
|
return (xenHypervisorPauseDomain(domain));
|
2005-11-07 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainResume:
|
2005-11-07 17:16:18 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Resume an suspended domain, the process is restarted from the state where
|
2005-12-05 11:16:07 +00:00
|
|
|
* it was frozen by calling virSuspendDomain().
|
2006-01-18 10:37:08 +00:00
|
|
|
* This function may requires priviledged access
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainResume(virDomainPtr domain)
|
|
|
|
{
|
2006-01-18 10:37:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
/* first try though the Xen daemon */
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainResume(domain);
|
2006-01-18 10:37:08 +00:00
|
|
|
if (ret == 0)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2006-01-18 10:37:08 +00:00
|
|
|
|
|
|
|
/* then try a direct hypervisor access */
|
2006-03-20 17:49:28 +00:00
|
|
|
return (xenHypervisorResumeDomain(domain));
|
2005-11-07 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
/**
|
|
|
|
* virDomainSave:
|
|
|
|
* @domain: a domain object
|
|
|
|
* @to: path for the output file
|
|
|
|
*
|
|
|
|
* This method will suspend a domain and save its memory contents to
|
2006-01-20 10:00:08 +00:00
|
|
|
* a file on disk. After the call, if successful, the domain is not
|
|
|
|
* listed as running anymore (this may be a problem).
|
|
|
|
* Use virDomainRestore() to restore a domain after saving.
|
2006-01-18 10:37:08 +00:00
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainSave(virDomainPtr domain, const char *to)
|
|
|
|
{
|
2006-01-18 10:37:08 +00:00
|
|
|
int ret;
|
2006-01-20 10:00:08 +00:00
|
|
|
char filepath[4096];
|
2006-01-18 10:37:08 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (to == NULL) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-01-18 10:37:08 +00:00
|
|
|
|
2006-01-20 10:00:08 +00:00
|
|
|
/*
|
|
|
|
* We must absolutize the file path as the save is done out of process
|
|
|
|
* TODO: check for URI when libxml2 is linked in.
|
|
|
|
*/
|
|
|
|
if (to[0] != '/') {
|
2006-03-15 12:13:25 +00:00
|
|
|
unsigned int len, t;
|
2006-01-20 10:00:08 +00:00
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
t = strlen(to);
|
|
|
|
if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
len = strlen(filepath);
|
|
|
|
/* that should be covered by getcwd() semantic, but be 100% sure */
|
|
|
|
if (len > sizeof(filepath) - (t + 3))
|
|
|
|
return (-1);
|
|
|
|
filepath[len] = '/';
|
|
|
|
strcpy(&filepath[len + 1], to);
|
|
|
|
to = &filepath[0];
|
2006-01-20 10:00:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainSave(domain, to);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2006-01-18 10:37:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDomainRestore:
|
2006-01-20 10:00:08 +00:00
|
|
|
* @conn: pointer to the hypervisor connection
|
2006-01-18 10:37:08 +00:00
|
|
|
* @from: path to the
|
|
|
|
*
|
|
|
|
* This method will restore a domain saved to disk by virDomainSave().
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainRestore(virConnectPtr conn, const char *from)
|
|
|
|
{
|
2006-01-18 10:37:08 +00:00
|
|
|
int ret;
|
2006-01-20 10:00:08 +00:00
|
|
|
char filepath[4096];
|
2006-01-18 10:37:08 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (from == NULL) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
2006-01-20 10:00:08 +00:00
|
|
|
/*
|
|
|
|
* We must absolutize the file path as the restore is done out of process
|
|
|
|
* TODO: check for URI when libxml2 is linked in.
|
|
|
|
*/
|
|
|
|
if (from[0] != '/') {
|
2006-03-15 12:13:25 +00:00
|
|
|
unsigned int len, t;
|
|
|
|
|
|
|
|
t = strlen(from);
|
|
|
|
if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
len = strlen(filepath);
|
|
|
|
/* that should be covered by getcwd() semantic, but be 100% sure */
|
|
|
|
if (len > sizeof(filepath) - (t + 3))
|
|
|
|
return (-1);
|
|
|
|
filepath[len] = '/';
|
|
|
|
strcpy(&filepath[len + 1], from);
|
|
|
|
from = &filepath[0];
|
|
|
|
}
|
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainRestore(conn, from);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2006-01-18 10:37:08 +00:00
|
|
|
}
|
|
|
|
|
2005-12-16 12:16:41 +00:00
|
|
|
/**
|
|
|
|
* virDomainShutdown:
|
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Shutdown a domain, the domain object is still usable there after but
|
2006-01-18 10:37:08 +00:00
|
|
|
* the domain OS is being stopped. Note that the guest OS may ignore the
|
|
|
|
* request.
|
2005-12-16 12:16:41 +00:00
|
|
|
*
|
|
|
|
* TODO: should we add an option for reboot, knowing it may not be doable
|
|
|
|
* in the general case ?
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainShutdown(virDomainPtr domain)
|
|
|
|
{
|
2005-12-16 12:16:41 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
/*
|
|
|
|
* try first with the xend daemon
|
|
|
|
*/
|
2006-03-22 13:44:01 +00:00
|
|
|
ret = xenDaemonDomainShutdown(domain);
|
2006-03-23 15:42:10 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
domain->flags |= DOMAIN_IS_SHUTDOWN;
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2006-03-23 15:42:10 +00:00
|
|
|
}
|
2006-01-18 18:58:35 +00:00
|
|
|
|
2006-02-17 13:18:57 +00:00
|
|
|
/*
|
2005-12-16 12:16:41 +00:00
|
|
|
* this is very hackish, the domU kernel probes for a special
|
|
|
|
* node in the xenstore and launch the shutdown command if found.
|
|
|
|
*/
|
2006-03-23 15:42:10 +00:00
|
|
|
ret = xenDaemonDomainShutdown(domain);
|
2005-12-16 12:16:41 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
domain->flags |= DOMAIN_IS_SHUTDOWN;
|
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2005-12-16 12:16:41 +00:00
|
|
|
}
|
|
|
|
|
2006-04-03 13:46:43 +00:00
|
|
|
/**
|
|
|
|
* virDomainReboot:
|
|
|
|
* @domain: a domain object
|
|
|
|
* @flags: extra flags for the reboot operation, not used yet
|
|
|
|
*
|
|
|
|
* Reboot a domain, the domain object is still usable there after but
|
|
|
|
* the domain OS is being stopped for a restart.
|
|
|
|
* Note that the guest OS may ignore the request.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virDomainReboot(virDomainPtr domain, unsigned int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* try first with the xend daemon
|
|
|
|
*/
|
|
|
|
ret = xenDaemonDomainReboot(domain, flags);
|
|
|
|
if (ret == 0) {
|
|
|
|
domain->flags |= DOMAIN_IS_SHUTDOWN;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is very hackish, the domU kernel probes for a special
|
|
|
|
* node in the xenstore and launch the shutdown command if found.
|
|
|
|
*/
|
|
|
|
ret = xenDaemonDomainReboot(domain, flags);
|
|
|
|
if (ret == 0) {
|
|
|
|
domain->flags |= DOMAIN_IS_SHUTDOWN;
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2005-11-23 07:47:13 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainGetName:
|
2005-11-23 07:47:13 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Get the public name for that domain
|
|
|
|
*
|
|
|
|
* Returns a pointer to the name or NULL, the string need not be deallocated
|
|
|
|
* its lifetime will be the same as the domain object.
|
|
|
|
*/
|
|
|
|
const char *
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetName(virDomainPtr domain)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
return (domain->name);
|
2005-11-30 13:20:53 +00:00
|
|
|
}
|
|
|
|
|
2006-02-23 10:13:55 +00:00
|
|
|
/**
|
|
|
|
* virDomainGetUUID:
|
|
|
|
* @domain: a domain object
|
|
|
|
* @uuid: pointer to a 16 bytes array
|
|
|
|
*
|
|
|
|
* Get the UUID for a domain
|
|
|
|
*
|
|
|
|
* Returns -1 in case of error, 0 in case of success
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (uuid == NULL) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (domain->handle == 0) {
|
2006-02-23 10:13:55 +00:00
|
|
|
memset(uuid, 0, 16);
|
2006-02-27 16:27:18 +00:00
|
|
|
} else {
|
2006-03-15 12:13:25 +00:00
|
|
|
if ((domain->uuid[0] == 0) && (domain->uuid[1] == 0) &&
|
|
|
|
(domain->uuid[2] == 0) && (domain->uuid[3] == 0) &&
|
|
|
|
(domain->uuid[4] == 0) && (domain->uuid[5] == 0) &&
|
|
|
|
(domain->uuid[6] == 0) && (domain->uuid[7] == 0) &&
|
|
|
|
(domain->uuid[8] == 0) && (domain->uuid[9] == 0) &&
|
|
|
|
(domain->uuid[10] == 0) && (domain->uuid[11] == 0) &&
|
|
|
|
(domain->uuid[12] == 0) && (domain->uuid[13] == 0) &&
|
|
|
|
(domain->uuid[14] == 0) && (domain->uuid[15] == 0))
|
2006-03-22 13:44:01 +00:00
|
|
|
xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
|
2006-03-15 12:13:25 +00:00
|
|
|
&domain->uuid[0]);
|
|
|
|
memcpy(uuid, &domain->uuid[0], 16);
|
|
|
|
}
|
|
|
|
return (0);
|
2006-02-23 10:13:55 +00:00
|
|
|
}
|
|
|
|
|
2005-11-30 13:20:53 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainGetID:
|
2005-11-30 13:20:53 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Get the hypervisor ID number for the domain
|
|
|
|
*
|
|
|
|
* Returns the domain ID number or (unsigned int) -1 in case of error
|
|
|
|
*/
|
|
|
|
unsigned int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetID(virDomainPtr domain)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return ((unsigned int) -1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
return (domain->handle);
|
2005-11-23 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
/**
|
|
|
|
* virDomainGetOSType:
|
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Get the type of domain operation system.
|
|
|
|
*
|
2006-03-20 17:49:28 +00:00
|
|
|
* Returns the new string or NULL in case of error, the string must be
|
|
|
|
* freed by the caller.
|
2005-12-16 00:51:27 +00:00
|
|
|
*/
|
|
|
|
char *
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetOSType(virDomainPtr domain)
|
|
|
|
{
|
2005-12-16 00:51:27 +00:00
|
|
|
char *vm, *str = NULL;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
vm = virDomainGetVM(domain);
|
|
|
|
if (vm) {
|
2006-03-15 12:13:25 +00:00
|
|
|
str = virDomainGetVMInfo(domain, vm, "image/ostype");
|
|
|
|
free(vm);
|
2005-12-16 00:51:27 +00:00
|
|
|
}
|
2006-01-19 10:23:15 +00:00
|
|
|
if (str == NULL)
|
|
|
|
str = strdup("linux");
|
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (str);
|
2005-12-16 00:51:27 +00:00
|
|
|
}
|
|
|
|
|
2005-11-23 07:47:13 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainGetMaxMemory:
|
2005-11-23 07:47:13 +00:00
|
|
|
* @domain: a domain object or NULL
|
|
|
|
*
|
|
|
|
* Retrieve the maximum amount of physical memory allocated to a
|
|
|
|
* domain. If domain is NULL, then this get the amount of memory reserved
|
|
|
|
* to Domain0 i.e. the domain where the application runs.
|
|
|
|
*
|
|
|
|
* Returns the memory size in kilobytes or 0 in case of error.
|
|
|
|
*/
|
|
|
|
unsigned long
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetMaxMemory(virDomainPtr domain)
|
|
|
|
{
|
2005-12-12 13:22:20 +00:00
|
|
|
unsigned long ret = 0;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-03-23 15:42:10 +00:00
|
|
|
/*
|
|
|
|
* try first with the hypervisor if available
|
|
|
|
*/
|
|
|
|
if (!(domain->conn->flags & VIR_CONNECT_RO)) {
|
2006-03-20 17:49:28 +00:00
|
|
|
virDomainInfo dominfo;
|
2006-03-15 12:13:25 +00:00
|
|
|
int tmp;
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2006-03-20 17:49:28 +00:00
|
|
|
tmp = xenHypervisorGetDomainInfo(domain, &dominfo);
|
2006-03-15 12:13:25 +00:00
|
|
|
if (tmp >= 0)
|
2006-03-23 15:42:10 +00:00
|
|
|
return(dominfo.maxMem);
|
2005-12-12 13:22:20 +00:00
|
|
|
}
|
2006-03-23 15:42:10 +00:00
|
|
|
ret = xenStoreDomainGetMaxMemory(domain);
|
|
|
|
if (ret > 0)
|
|
|
|
return(ret);
|
|
|
|
ret = xenDaemonDomainGetMaxMemory(domain);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2005-11-23 07:47:13 +00:00
|
|
|
}
|
|
|
|
|
2005-11-07 17:16:18 +00:00
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainSetMaxMemory:
|
2005-11-07 17:16:18 +00:00
|
|
|
* @domain: a domain object or NULL
|
|
|
|
* @memory: the memory size in kilobytes
|
|
|
|
*
|
|
|
|
* Dynamically change the maximum amount of physical memory allocated to a
|
|
|
|
* domain. If domain is NULL, then this change the amount of memory reserved
|
|
|
|
* to Domain0 i.e. the domain where the application runs.
|
2005-12-15 14:51:18 +00:00
|
|
|
* This function requires priviledged access to the hypervisor.
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
|
|
|
|
{
|
2005-12-12 13:22:20 +00:00
|
|
|
int ret;
|
|
|
|
char s[256], v[30];
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (memory < 4096) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-22 13:44:01 +00:00
|
|
|
if (domain == NULL) {
|
|
|
|
TODO
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-03-22 13:44:01 +00:00
|
|
|
}
|
|
|
|
if (domain->conn->flags & VIR_CONNECT_RO)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-03-22 13:44:01 +00:00
|
|
|
|
|
|
|
ret = xenDaemonDomainSetMaxMemory(domain, memory);
|
|
|
|
if (ret == 0)
|
|
|
|
return(0);
|
|
|
|
|
2006-03-20 17:49:28 +00:00
|
|
|
ret = xenHypervisorSetMaxMemory(domain, memory);
|
2005-12-12 13:22:20 +00:00
|
|
|
if (ret < 0)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
if (domain->conn->xshandle != NULL) {
|
|
|
|
/*
|
|
|
|
* try to update at the Xenstore level too
|
|
|
|
* Failing to do so should not be considered fatal though as long
|
|
|
|
* as the hypervisor call succeeded
|
|
|
|
*/
|
|
|
|
snprintf(s, 255, "/local/domain/%d/memory/target", domain->handle);
|
|
|
|
s[255] = 0;
|
|
|
|
snprintf(v, 29, "%lu", memory);
|
|
|
|
v[30] = 0;
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
if (!xs_write(domain->conn->xshandle, 0, &s[0], &v[0], strlen(v)))
|
|
|
|
ret = -1;
|
|
|
|
}
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2006-03-15 12:13:25 +00:00
|
|
|
return (ret);
|
2005-11-07 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
2005-12-05 18:14:37 +00:00
|
|
|
/**
|
|
|
|
* virDomainGetInfo:
|
2005-12-13 16:22:05 +00:00
|
|
|
* @domain: a domain object
|
2005-12-05 18:14:37 +00:00
|
|
|
* @info: pointer to a virDomainInfo structure allocated by the user
|
|
|
|
*
|
|
|
|
* Extract information about a domain. Note that if the connection
|
|
|
|
* used to get the domain is limited only a partial set of the informations
|
|
|
|
* can be extracted.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
|
|
|
|
{
|
2005-12-05 18:14:37 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (info == NULL) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (-1);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2005-12-06 13:47:40 +00:00
|
|
|
memset(info, 0, sizeof(virDomainInfo));
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-01-17 16:56:17 +00:00
|
|
|
/*
|
|
|
|
* if we have direct access though the hypervisor do a direct call
|
|
|
|
*/
|
|
|
|
if (domain->conn->handle >= 0) {
|
2006-03-20 17:49:28 +00:00
|
|
|
ret = xenHypervisorGetDomainInfo(domain, info);
|
2006-03-22 13:44:01 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return (0);
|
2006-01-17 16:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* try to extract the informations though access to the Xen Daemon
|
|
|
|
*/
|
2006-03-22 13:44:01 +00:00
|
|
|
if (xenDaemonDomainGetInfo(domain, info) == 0)
|
2006-03-15 12:13:25 +00:00
|
|
|
return (0);
|
2006-01-17 16:56:17 +00:00
|
|
|
|
|
|
|
/*
|
2006-03-23 15:42:10 +00:00
|
|
|
* last fallback, try to get the informations from the Xen store
|
2006-01-17 16:56:17 +00:00
|
|
|
*/
|
2006-03-23 15:42:10 +00:00
|
|
|
if (xenStoreGetDomainInfo(domain, info) == 0)
|
|
|
|
return (0);
|
2006-01-17 16:56:17 +00:00
|
|
|
|
2006-03-23 15:42:10 +00:00
|
|
|
return (-1);
|
2005-12-05 18:14:37 +00:00
|
|
|
}
|
2005-12-13 16:22:05 +00:00
|
|
|
|
2006-02-20 23:08:47 +00:00
|
|
|
/**
|
|
|
|
* virDomainGetXMLDesc:
|
|
|
|
* @domain: a domain object
|
|
|
|
* @flags: and OR'ed set of extraction flags, not used yet
|
|
|
|
*
|
|
|
|
* Provide an XML description of the domain. The description may be reused
|
|
|
|
* later to relaunch the domain with virDomainCreateLinux().
|
|
|
|
*
|
|
|
|
* Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
|
|
|
|
* the caller must free() the returned value.
|
|
|
|
*/
|
|
|
|
char *
|
2006-03-15 12:13:25 +00:00
|
|
|
virDomainGetXMLDesc(virDomainPtr domain, int flags)
|
|
|
|
{
|
2006-02-27 16:27:18 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain)) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
|
|
|
if (flags != 0) {
|
|
|
|
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
2006-03-15 12:13:25 +00:00
|
|
|
return (NULL);
|
2006-02-27 16:27:18 +00:00
|
|
|
}
|
2006-02-20 23:08:47 +00:00
|
|
|
|
2006-03-22 13:44:01 +00:00
|
|
|
return (xenDaemonDomainDumpXML(domain));
|
2006-02-20 23:08:47 +00:00
|
|
|
}
|
2006-03-29 12:46:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virNodeGetInfo:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @info: pointer to a virNodeInfo structure allocated by the user
|
|
|
|
*
|
|
|
|
* Extract hardware information about the node.
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success and -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info) {
|
|
|
|
int i;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!VIR_IS_CONNECT(conn)) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (info == NULL) {
|
|
|
|
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0;i < conn->nb_drivers;i++) {
|
|
|
|
if ((conn->drivers[i] != NULL) &&
|
|
|
|
(conn->drivers[i]->nodeGetInfo != NULL)) {
|
|
|
|
ret = conn->drivers[i]->nodeGetInfo(conn, info);
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|