2005-11-02 12:50:21 +00:00
|
|
|
/*
|
2005-12-07 18:10:51 +00:00
|
|
|
* libvir.c: Main interfaces for the libvir library to handle virtualization
|
2005-11-02 12:50:21 +00:00
|
|
|
* domains from a process running in domain 0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* See COPYING.LIB for the License of this software
|
|
|
|
*
|
|
|
|
* Daniel Veillard <veillard@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
#include "libvir.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>
|
2005-11-30 13:20:53 +00:00
|
|
|
#include <xs.h>
|
2005-11-02 12:50:21 +00:00
|
|
|
#include "internal.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"
|
2005-11-30 13:20:53 +00:00
|
|
|
#include "hash.h"
|
|
|
|
|
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
|
|
|
* - error reporting layer
|
|
|
|
* - memory wrappers for malloc/free ?
|
2005-11-02 12:50:21 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
virGetVersion(unsigned long *libVer, const char *type, unsigned long *typeVer) {
|
|
|
|
if (libVer == NULL)
|
|
|
|
return(-1);
|
|
|
|
*libVer = LIBVIR_VERSION_NUMBER;
|
|
|
|
|
|
|
|
if (typeVer != NULL) {
|
|
|
|
if ((type == NULL) || (!strcasecmp(type, "Xen"))) {
|
|
|
|
if ((DOM0_INTERFACE_VERSION & 0xFFFF0000) == (0xAAAA0000)) {
|
|
|
|
/* one time glitch hopefully ! */
|
|
|
|
*typeVer = 2 * 1000000 +
|
|
|
|
((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;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
virConnectOpen(const char *name) {
|
|
|
|
virConnectPtr ret = NULL;
|
2005-11-23 07:47:13 +00:00
|
|
|
int handle = -1;
|
2005-11-30 13:20:53 +00:00
|
|
|
struct xs_handle *xshandle = NULL;
|
|
|
|
|
|
|
|
/* we can only talk to the local Xen supervisor ATM */
|
|
|
|
if (name != NULL)
|
|
|
|
return(NULL);
|
2005-11-10 16:12:31 +00:00
|
|
|
|
2005-12-07 18:10:51 +00:00
|
|
|
handle = xenHypervisorOpen();
|
2005-11-23 07:47:13 +00:00
|
|
|
if (handle == -1)
|
|
|
|
goto failed;
|
|
|
|
xshandle = xs_daemon_open();
|
2005-11-30 13:20:53 +00:00
|
|
|
if (xshandle == NULL)
|
2005-11-23 07:47:13 +00:00
|
|
|
goto failed;
|
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
ret = (virConnectPtr) malloc(sizeof(virConnect));
|
2005-11-23 07:47:13 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
goto failed;
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->magic = VIR_CONNECT_MAGIC;
|
2005-11-10 16:12:31 +00:00
|
|
|
ret->handle = handle;
|
2005-11-23 07:47:13 +00:00
|
|
|
ret->xshandle = xshandle;
|
2006-01-13 16:41:01 +00:00
|
|
|
if (xend_setup(ret) < 0)
|
|
|
|
goto failed;
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->domains = virHashCreate(20);
|
2005-12-01 16:35:42 +00:00
|
|
|
ret->flags = 0;
|
2005-11-30 13:20:53 +00:00
|
|
|
if (ret->domains == NULL)
|
|
|
|
goto failed;
|
2005-11-10 16:12:31 +00:00
|
|
|
|
|
|
|
return(ret);
|
2005-11-23 07:47:13 +00:00
|
|
|
failed:
|
|
|
|
if (handle >= 0)
|
2005-12-07 18:10:51 +00:00
|
|
|
xenHypervisorClose(handle);
|
2005-11-30 13:20:53 +00:00
|
|
|
if (xshandle != NULL)
|
2005-11-23 07:47:13 +00:00
|
|
|
xs_daemon_close(xshandle);
|
2005-12-01 16:35:42 +00:00
|
|
|
if (ret != NULL)
|
|
|
|
free(ret);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
virConnectOpenReadOnly(const char *name) {
|
2006-01-19 10:23:15 +00:00
|
|
|
int method = 0;
|
|
|
|
int handle;
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectPtr ret = NULL;
|
2005-12-01 16:35:42 +00:00
|
|
|
struct xs_handle *xshandle = NULL;
|
|
|
|
|
|
|
|
/* we can only talk to the local Xen supervisor ATM */
|
|
|
|
if (name != NULL)
|
|
|
|
return(NULL);
|
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
handle = xenHypervisorOpen();
|
|
|
|
if (handle >= 0)
|
|
|
|
method++;
|
2006-01-24 13:10:39 +00:00
|
|
|
else
|
|
|
|
handle = -1;
|
|
|
|
|
2005-12-01 16:35:42 +00:00
|
|
|
xshandle = xs_daemon_open_readonly();
|
2006-01-19 10:23:15 +00:00
|
|
|
if (xshandle != NULL)
|
|
|
|
method++;
|
2005-12-01 16:35:42 +00:00
|
|
|
|
2005-12-05 11:16:07 +00:00
|
|
|
ret = (virConnectPtr) malloc(sizeof(virConnect));
|
2005-12-01 16:35:42 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
goto failed;
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->magic = VIR_CONNECT_MAGIC;
|
2006-01-24 13:10:39 +00:00
|
|
|
ret->handle = handle;
|
2005-12-01 16:35:42 +00:00
|
|
|
ret->xshandle = xshandle;
|
2006-01-19 10:23:15 +00:00
|
|
|
if (xend_setup(ret) == 0)
|
|
|
|
method++;
|
2005-12-05 11:16:07 +00:00
|
|
|
ret->domains = virHashCreate(20);
|
|
|
|
ret->flags = VIR_CONNECT_RO;
|
2006-01-19 10:23:15 +00:00
|
|
|
if ((ret->domains == NULL) || (method == 0))
|
2005-12-01 16:35:42 +00:00
|
|
|
goto failed;
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
failed:
|
2006-01-19 10:23:15 +00:00
|
|
|
if (handle >= 0)
|
|
|
|
xenHypervisorClose(handle);
|
2005-12-01 16:35:42 +00:00
|
|
|
if (xshandle != NULL)
|
|
|
|
xs_daemon_close(xshandle);
|
2006-01-19 10:23:15 +00:00
|
|
|
if (ret != NULL) {
|
|
|
|
if (ret->domains != NULL)
|
|
|
|
virHashFree(ret->domains, NULL);
|
2005-12-01 16:35:42 +00:00
|
|
|
free(ret);
|
2006-01-19 10:23:15 +00:00
|
|
|
}
|
2005-11-23 07:47:13 +00:00
|
|
|
return(NULL);
|
2005-11-02 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2005-12-12 13:22:20 +00:00
|
|
|
/**
|
|
|
|
* virConnectCheckStoreID:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @id: the id number as returned from Xenstore
|
|
|
|
*
|
|
|
|
* the xenstore sometimes list non-running domains, double check
|
|
|
|
* from the hypervisor if we have direct access
|
|
|
|
*
|
|
|
|
* Returns -1 if the check failed, 0 if successful or not possible to check
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virConnectCheckStoreID(virConnectPtr conn, int id) {
|
|
|
|
if (conn->handle >= 0) {
|
|
|
|
dom0_getdomaininfo_t dominfo;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
dominfo.domain = id;
|
|
|
|
tmp = xenHypervisorGetDomainInfo(conn->handle, id, &dominfo);
|
|
|
|
if (tmp < 0)
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
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
|
2005-12-08 16:42:56 +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
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectClose(virConnectPtr conn) {
|
2006-01-13 16:41:01 +00:00
|
|
|
xend_cleanup(conn);
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
2005-11-02 12:50:21 +00:00
|
|
|
return(-1);
|
2005-12-08 16:42:56 +00:00
|
|
|
virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
|
2005-11-02 12:50:21 +00:00
|
|
|
conn->magic = -1;
|
2006-01-19 10:23:15 +00:00
|
|
|
if (conn->xshandle != NULL)
|
|
|
|
xs_daemon_close(conn->xshandle);
|
2005-11-30 13:20:53 +00:00
|
|
|
conn->xshandle = NULL;
|
2005-12-01 16:35:42 +00:00
|
|
|
if (conn->handle != -1)
|
2005-12-07 18:10:51 +00:00
|
|
|
xenHypervisorClose(conn->handle);
|
2005-11-10 16:12:31 +00:00
|
|
|
conn->handle = -1;
|
2005-11-02 12:50:21 +00:00
|
|
|
free(conn);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
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 *
|
|
|
|
virConnectGetType(virConnectPtr conn) {
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
2005-12-08 13:26:52 +00:00
|
|
|
return(NULL);
|
|
|
|
return("Xen");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer) {
|
|
|
|
unsigned long ver;
|
2005-12-08 13:26:52 +00:00
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
if (hvVer == NULL)
|
2005-11-07 17:16:18 +00:00
|
|
|
return(-1);
|
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;
|
2005-12-08 13:26:52 +00:00
|
|
|
return(0);
|
2005-12-08 15:08:46 +00:00
|
|
|
}
|
2005-12-08 13:26:52 +00:00
|
|
|
|
|
|
|
ver = xenHypervisorGetVersion(conn->handle);
|
2005-12-08 15:08:46 +00:00
|
|
|
*hvVer = (ver >> 16) * 1000000 + (ver & 0xFFFF) * 1000;
|
|
|
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virConnectListDomains(virConnectPtr conn, int *ids, int maxids) {
|
2006-01-19 10:23:15 +00:00
|
|
|
struct xs_transaction_handle* t = NULL;
|
2005-12-02 14:16:21 +00:00
|
|
|
int ret = -1;
|
|
|
|
unsigned int num, i;
|
|
|
|
long id;
|
|
|
|
char **idlist = NULL, *endptr;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
if ((ids == NULL) || (maxids <= 0))
|
2005-12-02 14:16:21 +00:00
|
|
|
return(-1);
|
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
/* TODO: implement the API with Xend interfaces */
|
|
|
|
idlist = xend_get_domains(conn);
|
|
|
|
if (idlist != NULL) {
|
|
|
|
for (ret = 0,i = 0;(idlist[i] != NULL) && (ret < maxids);i++) {
|
|
|
|
id = xend_get_domain_id(conn, idlist[i]);
|
|
|
|
if (id >= 0)
|
|
|
|
ids[ret++] = (int) id;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (conn->xshandle != NULL) {
|
|
|
|
t = xs_transaction_start(conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
goto done;
|
2005-12-02 14:16:21 +00:00
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
idlist = xs_directory(conn->xshandle, t, "/local/domain", &num);
|
|
|
|
if (idlist == NULL)
|
2005-12-02 14:16:21 +00:00
|
|
|
goto done;
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
for (ret = 0,i = 0;(i < num) && (ret < maxids);i++) {
|
|
|
|
id = strtol(idlist[i], &endptr, 10);
|
|
|
|
if ((endptr == idlist[i]) || (*endptr != 0)) {
|
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virConnectCheckStoreID(conn, (int) id) < 0)
|
|
|
|
continue;
|
|
|
|
ids[ret++] = (int) id;
|
|
|
|
}
|
2005-12-02 14:16:21 +00:00
|
|
|
}
|
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
|
2005-12-02 14:16:21 +00:00
|
|
|
done:
|
2006-01-19 10:23:15 +00:00
|
|
|
if ((t != NULL) && (conn->xshandle != NULL))
|
2005-12-02 14:16:21 +00:00
|
|
|
xs_transaction_end(conn->xshandle, t, 0);
|
|
|
|
if (idlist != NULL)
|
|
|
|
free(idlist);
|
|
|
|
|
|
|
|
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
|
|
|
|
virConnectNumOfDomains(virConnectPtr conn) {
|
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
int ret = -1;
|
|
|
|
unsigned int num;
|
|
|
|
char **idlist = NULL;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
|
|
|
return(-1);
|
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
/*
|
|
|
|
* try first with Xend interface
|
|
|
|
*/
|
|
|
|
idlist = xend_get_domains(conn);
|
|
|
|
if (idlist != NULL) {
|
|
|
|
char **tmp = idlist;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (*tmp != NULL) {
|
|
|
|
tmp++;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (conn->xshandle != NULL) {
|
|
|
|
t = xs_transaction_start(conn->xshandle);
|
|
|
|
if (t) {
|
|
|
|
idlist = xs_directory(conn->xshandle, t, "/local/domain", &num);
|
|
|
|
if (idlist) {
|
|
|
|
free(idlist);
|
|
|
|
ret = num;
|
|
|
|
}
|
|
|
|
xs_transaction_end(conn->xshandle, t, 0);
|
|
|
|
}
|
2005-12-07 11:03:24 +00:00
|
|
|
}
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @kernel_path: the file path to the kernel image
|
|
|
|
* @initrd_path: an optional file path to an initrd
|
|
|
|
* @cmdline: optional command line parameters for the kernel
|
|
|
|
* @memory: the memory size in kilobytes
|
2005-12-05 11:16:07 +00:00
|
|
|
* @flags: an optional set of virDomainFlags
|
2005-11-07 17:16:18 +00:00
|
|
|
*
|
2005-12-15 14:51:18 +00:00
|
|
|
* Launch a new Linux guest domain, unimplemented yet, API to be defined.
|
|
|
|
* This would function 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
|
|
|
|
virDomainCreateLinux(virConnectPtr conn, const char *kernel_path,
|
2005-12-08 16:42:56 +00:00
|
|
|
const char *initrd_path ATTRIBUTE_UNUSED,
|
|
|
|
const char *cmdline ATTRIBUTE_UNUSED,
|
|
|
|
unsigned long memory,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
2005-12-16 18:41:46 +00:00
|
|
|
|
|
|
|
if (!VIR_IS_CONNECT(conn))
|
|
|
|
return(NULL);
|
|
|
|
if ((kernel_path == NULL) || (memory < 4096))
|
2005-11-07 17:16:18 +00:00
|
|
|
return(NULL);
|
|
|
|
TODO
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2005-12-06 16:12:49 +00:00
|
|
|
/**
|
|
|
|
* virConnectDoStoreList:
|
|
|
|
* @conn: pointer to the hypervisor connection
|
|
|
|
* @path: the absolute path of the directory in the store to list
|
|
|
|
* @nb: OUT pointer to the number of items found
|
|
|
|
*
|
|
|
|
* Internal API querying the Xenstore for a list
|
|
|
|
*
|
|
|
|
* Returns a string which must be freed by the caller or NULL in case of error
|
|
|
|
*/
|
|
|
|
static char **
|
|
|
|
virConnectDoStoreList(virConnectPtr conn, const char *path, unsigned int *nb) {
|
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
char **ret = NULL;
|
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
if ((conn == NULL) || (conn->xshandle == NULL) || (path == NULL) ||
|
|
|
|
(nb == NULL))
|
|
|
|
return(NULL);
|
|
|
|
|
2005-12-06 16:12:49 +00:00
|
|
|
t = xs_transaction_start(conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = xs_directory(conn->xshandle, t, path, nb);
|
|
|
|
|
2005-12-01 16:35:42 +00:00
|
|
|
done:
|
|
|
|
if (t != NULL)
|
|
|
|
xs_transaction_end(conn->xshandle, t, 0);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-05 11:16:07 +00:00
|
|
|
* virDomainDoStoreQuery:
|
2005-12-01 16:35:42 +00:00
|
|
|
* @domain: a domain object
|
|
|
|
* @path: the relative path of the data in the store to retrieve
|
|
|
|
*
|
|
|
|
* Internal API querying the Xenstore for a string value.
|
|
|
|
*
|
|
|
|
* Returns a string which must be freed by the caller or NULL in case of error
|
|
|
|
*/
|
|
|
|
static char *
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainDoStoreQuery(virDomainPtr domain, const char *path) {
|
2005-12-01 16:35:42 +00:00
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
char s[256];
|
|
|
|
char *ret = NULL;
|
|
|
|
unsigned int len = 0;
|
2005-12-16 18:41:46 +00:00
|
|
|
|
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(NULL);
|
2006-01-19 10:23:15 +00:00
|
|
|
if (domain->conn->xshandle == NULL)
|
|
|
|
return(NULL);
|
2005-12-01 16:35:42 +00:00
|
|
|
|
|
|
|
snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
|
|
|
|
s[255] = 0;
|
|
|
|
|
|
|
|
t = xs_transaction_start(domain->conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = xs_read(domain->conn->xshandle, t, &s[0], &len);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (t != NULL)
|
|
|
|
xs_transaction_end(domain->conn->xshandle, t, 0);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
|
2005-12-16 12:16:41 +00:00
|
|
|
/**
|
|
|
|
* virDomainDoStoreWrite:
|
|
|
|
* @domain: a domain object
|
|
|
|
* @path: the relative path of the data in the store to retrieve
|
|
|
|
*
|
|
|
|
* Internal API setting up a string value in the Xenstore
|
|
|
|
* Requires write access to the XenStore
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success, -1 in case of failure
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virDomainDoStoreWrite(virDomainPtr domain, const char *path,
|
|
|
|
const char *value) {
|
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
char s[256];
|
|
|
|
|
|
|
|
int ret = -1;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
2005-12-16 12:16:41 +00:00
|
|
|
return(-1);
|
2006-01-19 10:23:15 +00:00
|
|
|
if (domain->conn->xshandle == NULL)
|
|
|
|
return(-1);
|
2005-12-16 18:41:46 +00:00
|
|
|
if (domain->conn->flags & VIR_CONNECT_RO)
|
2005-12-16 12:16:41 +00:00
|
|
|
return(-1);
|
|
|
|
|
|
|
|
snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
|
|
|
|
s[255] = 0;
|
|
|
|
|
|
|
|
t = xs_transaction_start(domain->conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (xs_write(domain->conn->xshandle, t, &s[0], value, strlen(value)))
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (t != NULL)
|
|
|
|
xs_transaction_end(domain->conn->xshandle, t, 0);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
/**
|
|
|
|
* virDomainGetVM:
|
|
|
|
* @domain: a domain object
|
|
|
|
*
|
|
|
|
* Internal API extracting a xenstore vm path.
|
|
|
|
*
|
|
|
|
* Returns the new string or NULL in case of error
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virDomainGetVM(virDomainPtr domain)
|
|
|
|
{
|
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
char *vm;
|
|
|
|
char query[200];
|
|
|
|
unsigned int len;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
2005-12-16 00:51:27 +00:00
|
|
|
return(NULL);
|
2006-01-19 10:23:15 +00:00
|
|
|
if (domain->conn->xshandle == NULL)
|
|
|
|
return(NULL);
|
2005-12-16 18:41:46 +00:00
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
t = xs_transaction_start(domain->conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
return(NULL);
|
|
|
|
|
|
|
|
snprintf(query, 199, "/local/domain/%d/vm",
|
|
|
|
virDomainGetID(domain));
|
|
|
|
query[199] = 0;
|
|
|
|
|
|
|
|
vm = xs_read(domain->conn->xshandle, t, &query[0], &len);
|
|
|
|
|
|
|
|
if (t != NULL)
|
|
|
|
xs_transaction_end(domain->conn->xshandle, t, 0);
|
|
|
|
|
|
|
|
return(vm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virDomainGetVMInfo:
|
|
|
|
* @domain: a domain object
|
|
|
|
* @vm: the xenstore vm path
|
|
|
|
* @name: the value's path
|
|
|
|
*
|
|
|
|
* Internal API extracting one information the device used
|
|
|
|
* by the domain from xensttore
|
|
|
|
*
|
|
|
|
* Returns the new string or NULL in case of error
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virDomainGetVMInfo(virDomainPtr domain, const char *vm,
|
|
|
|
const char *name) {
|
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
char s[256];
|
|
|
|
char *ret = NULL;
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
2005-12-16 00:51:27 +00:00
|
|
|
return(NULL);
|
2005-12-16 18:41:46 +00:00
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
snprintf(s, 255, "%s/%s", vm, name);
|
|
|
|
s[255] = 0;
|
|
|
|
|
|
|
|
t = xs_transaction_start(domain->conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = xs_read(domain->conn->xshandle, t, &s[0], &len);
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (t != NULL)
|
|
|
|
xs_transaction_end(domain->conn->xshandle, t, 0);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
virDomainLookupByID(virConnectPtr conn, int id) {
|
2006-01-19 10:23:15 +00:00
|
|
|
char *path = NULL;
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainPtr ret;
|
2006-01-19 10:23:15 +00:00
|
|
|
char *name = NULL;
|
2005-11-30 13:20:53 +00:00
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
|
|
|
return(NULL);
|
|
|
|
if (id < 0)
|
2005-11-30 13:20:53 +00:00
|
|
|
return(NULL);
|
|
|
|
|
2006-01-19 10:23:15 +00:00
|
|
|
/* lookup is easier with the Xen store so try it first */
|
|
|
|
if (conn->xshandle != NULL) {
|
|
|
|
path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
|
|
|
|
}
|
|
|
|
/* fallback to xend API then */
|
2005-11-30 13:20:53 +00:00
|
|
|
if (path == NULL) {
|
2006-01-19 10:23:15 +00:00
|
|
|
char **names = xend_get_domains(conn);
|
|
|
|
char **tmp = names;
|
|
|
|
int ident;
|
|
|
|
|
|
|
|
if (names != NULL) {
|
|
|
|
while (*tmp != NULL) {
|
|
|
|
ident = xend_get_domain_id(conn, *tmp);
|
|
|
|
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-01-19 10:23:15 +00:00
|
|
|
if (path != NULL)
|
|
|
|
free(path);
|
2005-11-30 13:20:53 +00:00
|
|
|
return(NULL);
|
|
|
|
}
|
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-01-19 10:23:15 +00:00
|
|
|
if (name == NULL)
|
|
|
|
ret->name = virDomainDoStoreQuery(ret, "name");
|
|
|
|
else
|
|
|
|
ret->name = name;
|
2005-12-13 16:22:05 +00:00
|
|
|
if (ret->name == NULL) {
|
2006-01-19 10:23:15 +00:00
|
|
|
if (path != NULL)
|
|
|
|
free(path);
|
2005-12-13 16:22:05 +00:00
|
|
|
free(ret);
|
|
|
|
return(NULL);
|
|
|
|
}
|
2005-11-30 13:20:53 +00:00
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
virDomainLookupByName(virConnectPtr conn, const char *name) {
|
2006-01-17 16:56:17 +00:00
|
|
|
struct xs_transaction_handle* t = NULL;
|
2005-12-12 13:22:20 +00:00
|
|
|
virDomainPtr ret = NULL;
|
|
|
|
unsigned int num, i, len;
|
2006-01-11 13:58:32 +00:00
|
|
|
long id = -1;
|
2005-12-12 13:22:20 +00:00
|
|
|
char **idlist = NULL, *endptr;
|
2006-01-17 16:56:17 +00:00
|
|
|
char prop[200], *tmp, *path = NULL;
|
|
|
|
unsigned char *uuid = NULL;
|
2005-12-12 13:22:20 +00:00
|
|
|
int found = 0;
|
2006-01-17 16:56:17 +00:00
|
|
|
struct xend_domain *xenddomain = NULL;
|
2005-12-12 13:22:20 +00:00
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECT(conn))
|
|
|
|
return(NULL);
|
|
|
|
if (name == NULL)
|
2005-12-12 13:22:20 +00:00
|
|
|
return(NULL);
|
|
|
|
|
2006-01-17 16:56:17 +00:00
|
|
|
/* try first though Xend */
|
|
|
|
xenddomain = xend_get_domain(conn, name);
|
|
|
|
if (xenddomain != NULL) {
|
|
|
|
id = xenddomain->live->id;
|
|
|
|
uuid = xenddomain->uuid;
|
|
|
|
found = 1;
|
|
|
|
goto do_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* then though the XenStore */
|
|
|
|
|
2005-12-12 13:22:20 +00:00
|
|
|
t = xs_transaction_start(conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
idlist = xs_directory(conn->xshandle, t, "/local/domain", &num);
|
|
|
|
if (idlist == NULL)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (i = 0;i < num;i++) {
|
|
|
|
id = strtol(idlist[i], &endptr, 10);
|
|
|
|
if ((endptr == idlist[i]) || (*endptr != 0)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (virConnectCheckStoreID(conn, (int) id) < 0)
|
|
|
|
continue;
|
|
|
|
snprintf(prop, 199, "/local/domain/%s/name", idlist[i]);
|
|
|
|
prop[199] = 0;
|
|
|
|
tmp = xs_read(conn->xshandle, t, prop, &len);
|
|
|
|
if (tmp != NULL) {
|
|
|
|
found = !strcmp(name, tmp);
|
|
|
|
free(tmp);
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-01-17 16:56:17 +00:00
|
|
|
path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
|
|
|
|
|
|
|
|
do_found:
|
|
|
|
|
2005-12-12 13:22:20 +00:00
|
|
|
if (found) {
|
|
|
|
ret = (virDomainPtr) malloc(sizeof(virDomain));
|
|
|
|
if (ret == NULL)
|
|
|
|
goto done;
|
|
|
|
ret->magic = VIR_DOMAIN_MAGIC;
|
|
|
|
ret->conn = conn;
|
|
|
|
ret->handle = id;
|
2006-01-17 16:56:17 +00:00
|
|
|
ret->path = path;
|
|
|
|
if (uuid != NULL)
|
|
|
|
memcpy(ret->uuid, uuid, 16);
|
2005-12-12 13:22:20 +00:00
|
|
|
ret->name = strdup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2006-01-17 16:56:17 +00:00
|
|
|
if (xenddomain != NULL)
|
|
|
|
free(xenddomain);
|
2005-12-12 13:22:20 +00:00
|
|
|
if (t != NULL)
|
|
|
|
xs_transaction_end(conn->xshandle, t, 0);
|
|
|
|
if (idlist != NULL)
|
|
|
|
free(idlist);
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainDestroy(virDomainPtr domain) {
|
2005-12-08 17:16:24 +00:00
|
|
|
int ret;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(-1);
|
2006-01-18 10:37:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* try first with the xend method
|
|
|
|
*/
|
|
|
|
ret = xend_destroy(domain->conn, domain->name);
|
|
|
|
if (ret == 0) {
|
|
|
|
virDomainFree(domain);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2005-12-08 17:16:24 +00:00
|
|
|
ret = xenHypervisorDestroyDomain(domain->conn->handle, domain->handle);
|
|
|
|
if (ret < 0)
|
|
|
|
return(-1);
|
2005-12-08 16:42:56 +00:00
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
virDomainFree(domain);
|
|
|
|
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
|
|
|
|
virDomainFree(virDomainPtr domain) {
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain))
|
|
|
|
return(-1);
|
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);
|
|
|
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainSuspend(virDomainPtr domain) {
|
2006-01-18 10:37:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(-1);
|
2006-01-18 10:37:08 +00:00
|
|
|
/* first try though the Xen daemon */
|
|
|
|
ret = xend_pause(domain->conn, domain->name);
|
|
|
|
if (ret == 0)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
/* then try a direct hypervisor access */
|
2005-12-08 17:16:24 +00:00
|
|
|
return(xenHypervisorPauseDomain(domain->conn->handle, domain->handle));
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainResume(virDomainPtr domain) {
|
2006-01-18 10:37:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(-1);
|
2006-01-18 10:37:08 +00:00
|
|
|
/* first try though the Xen daemon */
|
|
|
|
ret = xend_unpause(domain->conn, domain->name);
|
|
|
|
if (ret == 0)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
/* then try a direct hypervisor access */
|
2005-12-08 17:16:24 +00:00
|
|
|
return(xenHypervisorResumeDomain(domain->conn->handle, domain->handle));
|
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
|
|
|
|
virDomainSave(virDomainPtr domain, const char *to) {
|
|
|
|
int ret;
|
2006-01-20 10:00:08 +00:00
|
|
|
char filepath[4096];
|
2006-01-18 10:37:08 +00:00
|
|
|
|
2006-01-20 10:00:08 +00:00
|
|
|
if ((!VIR_IS_CONNECTED_DOMAIN(domain)) || (to == NULL))
|
2006-01-18 10:37:08 +00:00
|
|
|
return(-1);
|
|
|
|
|
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] != '/') {
|
|
|
|
unsigned int len, t;
|
|
|
|
|
|
|
|
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-18 10:37:08 +00:00
|
|
|
ret = xend_save(domain->conn, domain->name, to);
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-01-20 10:00:08 +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-01-20 10:00:08 +00:00
|
|
|
if ((!VIR_IS_CONNECT(conn)) || (from == NULL))
|
2006-01-18 10:37:08 +00:00
|
|
|
return(-1);
|
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] != '/') {
|
|
|
|
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-01-18 10:37:08 +00:00
|
|
|
|
2006-01-20 10:00:08 +00:00
|
|
|
ret = xend_restore(conn, from);
|
2006-01-18 10:37:08 +00:00
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
virDomainShutdown(virDomainPtr domain) {
|
|
|
|
int ret;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(-1);
|
|
|
|
|
2006-01-18 10:37:08 +00:00
|
|
|
/*
|
|
|
|
* try first with the xend daemon
|
|
|
|
*/
|
|
|
|
ret = xend_shutdown(domain->conn, domain->name);
|
2006-01-18 18:58:35 +00:00
|
|
|
/* disabled as this seems to not work ...
|
2006-01-18 10:37:08 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return(0);
|
2006-01-18 18:58:35 +00:00
|
|
|
*/
|
2005-12-16 12:16:41 +00:00
|
|
|
/*
|
2006-01-18 18:58:35 +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.
|
|
|
|
*/
|
|
|
|
ret = virDomainDoStoreWrite(domain, "control/shutdown", "halt");
|
|
|
|
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 *
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainGetName(virDomainPtr domain) {
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain))
|
|
|
|
return(NULL);
|
2005-11-30 13:20:53 +00:00
|
|
|
return(domain->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainGetID(virDomainPtr domain) {
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain))
|
|
|
|
return((unsigned int) -1);
|
2005-11-30 13:20:53 +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.
|
|
|
|
*
|
|
|
|
* Returns the new string or NULL in case of error
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virDomainGetOSType(virDomainPtr domain) {
|
|
|
|
char *vm, *str = NULL;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_DOMAIN(domain))
|
|
|
|
return(NULL);
|
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
vm = virDomainGetVM(domain);
|
|
|
|
if (vm) {
|
|
|
|
str = virDomainGetVMInfo(domain, vm, "image/ostype");
|
|
|
|
free(vm);
|
|
|
|
}
|
2006-01-19 10:23:15 +00:00
|
|
|
if (str == NULL)
|
|
|
|
str = strdup("linux");
|
|
|
|
|
2005-12-16 00:51:27 +00:00
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainGetMaxMemory(virDomainPtr domain) {
|
2005-12-12 13:22:20 +00:00
|
|
|
unsigned long ret = 0;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(0);
|
|
|
|
|
2005-12-12 13:22:20 +00:00
|
|
|
if (domain->conn->flags & VIR_CONNECT_RO) {
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = virDomainDoStoreQuery(domain, "memory/target");
|
|
|
|
if (tmp != NULL) {
|
|
|
|
ret = (unsigned long) atol(tmp);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dom0_getdomaininfo_t dominfo;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
dominfo.domain = domain->handle;
|
|
|
|
tmp = xenHypervisorGetDomainInfo(domain->conn->handle, domain->handle,
|
|
|
|
&dominfo);
|
|
|
|
if (tmp >= 0)
|
|
|
|
ret = dominfo.max_pages * 4;
|
|
|
|
}
|
|
|
|
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
|
2005-12-05 11:16:07 +00:00
|
|
|
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory) {
|
2005-12-12 13:22:20 +00:00
|
|
|
int ret;
|
|
|
|
char s[256], v[30];
|
|
|
|
struct xs_transaction_handle* t;
|
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
|
|
|
return(-1);
|
|
|
|
if (memory < 4096)
|
2005-11-07 17:16:18 +00:00
|
|
|
return(-1);
|
2005-12-12 13:22:20 +00:00
|
|
|
if (domain->conn->flags & VIR_CONNECT_RO)
|
|
|
|
return(-1);
|
|
|
|
ret = xenHypervisorSetMaxMemory(domain->conn->handle, domain->handle,
|
|
|
|
memory);
|
|
|
|
if (ret < 0)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
t = xs_transaction_start(domain->conn->xshandle);
|
|
|
|
if (t == NULL)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
xs_write(domain->conn->xshandle, t, &s[0], &v[0], strlen(v));
|
|
|
|
xs_transaction_end(domain->conn->xshandle, t, 0);
|
|
|
|
|
|
|
|
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
|
|
|
|
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) {
|
|
|
|
int ret;
|
2006-01-17 16:56:17 +00:00
|
|
|
char *tmp, **tmp2;
|
|
|
|
unsigned int nb_vcpus;
|
|
|
|
char request[200];
|
|
|
|
|
2005-12-05 18:14:37 +00:00
|
|
|
|
2005-12-16 18:41:46 +00:00
|
|
|
if (!VIR_IS_CONNECTED_DOMAIN(domain))
|
2005-12-05 18:14:37 +00:00
|
|
|
return(-1);
|
2005-12-16 18:41:46 +00:00
|
|
|
if (info == NULL)
|
|
|
|
return(-1);
|
|
|
|
|
2005-12-06 13:47:40 +00:00
|
|
|
memset(info, 0, sizeof(virDomainInfo));
|
2005-12-16 18:41:46 +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) {
|
2005-12-07 18:10:51 +00:00
|
|
|
dom0_getdomaininfo_t dominfo;
|
2005-12-05 18:14:37 +00:00
|
|
|
|
|
|
|
dominfo.domain = domain->handle;
|
2005-12-07 18:10:51 +00:00
|
|
|
ret = xenHypervisorGetDomainInfo(domain->conn->handle, domain->handle,
|
|
|
|
&dominfo);
|
2005-12-08 10:12:18 +00:00
|
|
|
if (ret < 0)
|
2006-01-17 16:56:17 +00:00
|
|
|
goto xend_info;
|
|
|
|
|
2005-12-05 18:14:37 +00:00
|
|
|
switch (dominfo.flags & 0xFF) {
|
|
|
|
case DOMFLAGS_DYING:
|
|
|
|
info->state = VIR_DOMAIN_SHUTDOWN;
|
|
|
|
break;
|
|
|
|
case DOMFLAGS_SHUTDOWN:
|
|
|
|
info->state = VIR_DOMAIN_SHUTOFF;
|
|
|
|
break;
|
|
|
|
case DOMFLAGS_PAUSED:
|
|
|
|
info->state = VIR_DOMAIN_PAUSED;
|
|
|
|
break;
|
|
|
|
case DOMFLAGS_BLOCKED:
|
|
|
|
info->state = VIR_DOMAIN_BLOCKED;
|
|
|
|
break;
|
|
|
|
case DOMFLAGS_RUNNING:
|
|
|
|
info->state = VIR_DOMAIN_RUNNING;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
info->state = VIR_DOMAIN_NONE;
|
|
|
|
}
|
2005-12-06 13:47:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* the API brings back the cpu time in nanoseconds,
|
|
|
|
* convert to microseconds, same thing convert to
|
2006-01-18 10:37:08 +00:00
|
|
|
* kilobytes from page counts
|
2005-12-06 13:47:40 +00:00
|
|
|
*/
|
2005-12-05 18:14:37 +00:00
|
|
|
info->cpuTime = dominfo.cpu_time;
|
2005-12-06 16:50:31 +00:00
|
|
|
info->memory = dominfo.tot_pages * 4;
|
|
|
|
info->maxMem = dominfo.max_pages * 4;
|
2005-12-06 16:12:49 +00:00
|
|
|
info->nrVirtCpu = dominfo.nr_online_vcpus;
|
2006-01-17 16:56:17 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
xend_info:
|
|
|
|
/*
|
|
|
|
* try to extract the informations though access to the Xen Daemon
|
|
|
|
*/
|
|
|
|
if (xend_get_domain_info(domain, info) == 0)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* last fallback, try to get the inforamtions from the Xen store
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = virDomainDoStoreQuery(domain, "running");
|
|
|
|
if (tmp != NULL) {
|
|
|
|
if (tmp[0] == '1')
|
|
|
|
info->state = VIR_DOMAIN_RUNNING;
|
|
|
|
free(tmp);
|
|
|
|
} else {
|
|
|
|
info->state = VIR_DOMAIN_NONE;
|
|
|
|
}
|
|
|
|
tmp = virDomainDoStoreQuery(domain, "memory/target");
|
|
|
|
if (tmp != NULL) {
|
|
|
|
info->memory = atol(tmp);
|
|
|
|
info->maxMem = atol(tmp);
|
|
|
|
free(tmp);
|
|
|
|
} else {
|
|
|
|
info->memory = 0;
|
|
|
|
info->maxMem = 0;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
/* doesn't seems to work */
|
|
|
|
tmp = virDomainDoStoreQuery(domain, "cpu_time");
|
|
|
|
if (tmp != NULL) {
|
|
|
|
info->cpuTime = atol(tmp);
|
|
|
|
free(tmp);
|
|
|
|
} else {
|
|
|
|
info->cpuTime = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
snprintf(request, 199, "/local/domain/%d/cpu", domain->handle);
|
|
|
|
request[199] = 0;
|
|
|
|
tmp2 = virConnectDoStoreList(domain->conn, request, &nb_vcpus);
|
|
|
|
if (tmp2 != NULL) {
|
|
|
|
info->nrVirtCpu = nb_vcpus;
|
|
|
|
free(tmp2);
|
2005-12-05 18:14:37 +00:00
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
2005-12-13 16:22:05 +00:00
|
|
|
|