libvirt.c 37.6 KB
Newer Older
D
Daniel Veillard 已提交
1
/*
2
 * libvirt.c: Main interfaces for the libvirt library to handle virtualization
D
Daniel Veillard 已提交
3 4
 *           domains from a process running in domain 0
 *
5
 * Copyright (C) 2005,2006 Red Hat, Inc.
D
Daniel Veillard 已提交
6 7 8 9 10 11
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

12
#include "libvirt.h"
D
Daniel Veillard 已提交
13

14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <string.h>
17 18 19
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
20
#include <xs.h>
D
Daniel Veillard 已提交
21
#include "internal.h"
22
#include "xen_internal.h"
23
#include "xend_internal.h"
24
#include "hash.h"
25
#include "xml.h"
26

D
Daniel Veillard 已提交
27 28 29 30 31

/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
32 33
 * - error reporting layer
 * - memory wrappers for malloc/free ?
D
Daniel Veillard 已提交
34 35
 */

D
Daniel Veillard 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info) {
    const char *errmsg;
    
    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
virLibDomainError(virDomainPtr domain, virErrorNumber error, const char *info) {
    virConnectPtr conn = NULL;
    const char *errmsg;
    
    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);
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/**
 * 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;
D
Daniel Veillard 已提交
115
	    virLibConnError(NULL, VIR_ERR_NO_SUPPORT, "type");
116 117 118 119 120 121
	    return(-1);
	}
    }
    return(0);
}

D
Daniel Veillard 已提交
122
/**
123
 * virConnectOpen:
D
Daniel Veillard 已提交
124 125 126
 * @name: optional argument currently unused, pass NULL
 *
 * This function should be called first to get a connection to the 
127
 * Hypervisor and xen store
D
Daniel Veillard 已提交
128 129 130
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
131 132 133
virConnectPtr
virConnectOpen(const char *name) {
    virConnectPtr ret = NULL;
134
    int handle = -1;
135 136 137
    struct xs_handle *xshandle = NULL;

    /* we can only talk to the local Xen supervisor ATM */
D
Daniel Veillard 已提交
138 139
    if (name != NULL) {
        virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name); 
140
        return(NULL);
D
Daniel Veillard 已提交
141
    }
142

143
    handle = xenHypervisorOpen(0);
D
Daniel Veillard 已提交
144
    if (handle == -1) {
145
        goto failed;
D
Daniel Veillard 已提交
146
    }
147
    xshandle = xs_daemon_open();
D
Daniel Veillard 已提交
148 149
    if (xshandle == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_CONNECT, "XenStore"); 
150
        goto failed;
D
Daniel Veillard 已提交
151
    }
152

153
    ret = (virConnectPtr) malloc(sizeof(virConnect));
D
Daniel Veillard 已提交
154 155
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
156
        goto failed;
D
Daniel Veillard 已提交
157
    }
158
    memset(ret, 0, sizeof(virConnect));
159
    ret->magic = VIR_CONNECT_MAGIC;
160
    ret->handle = handle;
161
    ret->xshandle = xshandle;
162 163
    if (xend_setup(ret) < 0)
        goto failed;
164
    ret->domains = virHashCreate(20);
165
    ret->flags = 0;
166 167
    if (ret->domains == NULL)
        goto failed;
168 169

    return(ret);
170 171
failed:
    if (handle >= 0)
172
        xenHypervisorClose(handle);
173
    if (xshandle != NULL)
174
        xs_daemon_close(xshandle);
175 176 177 178 179 180
    if (ret != NULL)
        free(ret);
    return(NULL);
}

/**
181
 * virConnectOpenReadOnly:
182 183
 * @name: optional argument currently unused, pass NULL
 *
184 185 186
 * 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.
187 188 189
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
190 191
virConnectPtr
virConnectOpenReadOnly(const char *name) {
192 193
    int method = 0;
    int handle;
194
    virConnectPtr ret = NULL;
195 196 197
    struct xs_handle *xshandle = NULL;

    /* we can only talk to the local Xen supervisor ATM */
D
Daniel Veillard 已提交
198 199
    if (name != NULL) {
        virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name); 
200
        return(NULL);
D
Daniel Veillard 已提交
201
    }
202

203
    handle = xenHypervisorOpen(1);
204 205
    if (handle >= 0)
        method++;
206 207 208
    else
        handle = -1;

209
    xshandle = xs_daemon_open_readonly();
210 211
    if (xshandle != NULL)
        method++;
212

213
    ret = (virConnectPtr) malloc(sizeof(virConnect));
D
Daniel Veillard 已提交
214 215
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
216
        goto failed;
D
Daniel Veillard 已提交
217
    }
218
    memset(ret, 0, sizeof(virConnect));
219
    ret->magic = VIR_CONNECT_MAGIC;
220
    ret->handle = handle;
221
    ret->xshandle = xshandle;
222 223
    if (xend_setup(ret) == 0)
        method++;
224
    ret->domains = virHashCreate(20);
D
Daniel Veillard 已提交
225 226
    if (ret->domains == NULL)
        goto failed;
227
    ret->flags = VIR_CONNECT_RO;
D
Daniel Veillard 已提交
228 229 230
    if (method == 0) {
        virLibConnError(NULL, VIR_ERR_NO_CONNECT,
	                "could not connect to Xen Daemon nor Xen Store");
231
        goto failed;
D
Daniel Veillard 已提交
232
    }
233 234 235

    return(ret);
failed:
236 237
    if (handle >= 0)
        xenHypervisorClose(handle);
238 239
    if (xshandle != NULL)
        xs_daemon_close(xshandle);
240 241 242
    if (ret != NULL) {
        if (ret->domains != NULL)
	    virHashFree(ret->domains, NULL);
243
        free(ret);
244
    }
245
    return(NULL);
D
Daniel Veillard 已提交
246 247
}

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/**
 * 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);
}

272
/**
273
 * virDomainFreeName:
274 275 276 277 278 279 280
 * @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
281 282
virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED) {
    return(virDomainFree(domain));
283 284
}

D
Daniel Veillard 已提交
285
/**
286
 * virConnectClose:
D
Daniel Veillard 已提交
287 288 289 290 291 292 293 294 295 296
 * @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
297
virConnectClose(virConnectPtr conn) {
298
    xend_cleanup(conn);
K
Karel Zak 已提交
299
    if (!VIR_IS_CONNECT(conn))
D
Daniel Veillard 已提交
300
        return(-1);
301
    virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
D
Daniel Veillard 已提交
302
    conn->magic = -1;
303 304
    if (conn->xshandle != NULL)
	xs_daemon_close(conn->xshandle);
305
    conn->xshandle = NULL;
306
    if (conn->handle != -1)
307
	xenHypervisorClose(conn->handle);
308
    conn->handle = -1;
D
Daniel Veillard 已提交
309 310 311 312
    free(conn);
    return(0);
}

313 314 315 316 317 318 319 320 321 322
/**
 * 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) {
D
Daniel Veillard 已提交
323 324
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, "in virConnectGetType");
325
        return(NULL);
D
Daniel Veillard 已提交
326
    }
327 328 329
    return("Xen");
}

D
Daniel Veillard 已提交
330
/**
331
 * virConnectGetVersion:
D
Daniel Veillard 已提交
332
 * @conn: pointer to the hypervisor connection
333
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
334
 *
335 336 337
 * 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.
D
Daniel Veillard 已提交
338
 *
339 340 341
 * 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
D
Daniel Veillard 已提交
342
 */
343 344 345
int
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer) {
    unsigned long ver;
346

D
Daniel Veillard 已提交
347 348
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
349
	return(-1);
D
Daniel Veillard 已提交
350
    }
K
Karel Zak 已提交
351
    
D
Daniel Veillard 已提交
352 353
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
D
Daniel Veillard 已提交
354
        return(-1);
D
Daniel Veillard 已提交
355
    }
356 357
    
    /* this can't be extracted from the Xenstore */
358 359
    if (conn->handle < 0) {
        *hvVer = 0;
360
        return(0);
361
    }
362 363

    ver = xenHypervisorGetVersion(conn->handle);
364 365
    *hvVer = (ver >> 16) * 1000000 + (ver & 0xFFFF) * 1000;
    return(0);
366 367 368
}

/**
369
 * virConnectListDomains:
370 371 372 373 374 375 376 377 378
 * @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
379
virConnectListDomains(virConnectPtr conn, int *ids, int maxids) {
380 381 382 383 384
    int ret = -1;
    unsigned int num, i;
    long id;
    char **idlist = NULL, *endptr;

D
Daniel Veillard 已提交
385 386
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
387
	return(-1);
D
Daniel Veillard 已提交
388
    }
K
Karel Zak 已提交
389
    
D
Daniel Veillard 已提交
390 391
    if ((ids == NULL) || (maxids <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
392
        return(-1);
D
Daniel Veillard 已提交
393
    }
394
    
395 396 397
    idlist = xend_get_domains(conn);
    if (idlist != NULL) {
        for (ret = 0,i = 0;(idlist[i] != NULL) && (ret < maxids);i++) {
398
	    id = xend_get_domain_ids(conn, idlist[i], NULL);
399 400 401 402 403 404
	    if (id >= 0)
	        ids[ret++] = (int) id;
	}
	goto done;
    }
    if (conn->xshandle != NULL) {
405
	idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
406
	if (idlist == NULL)
407
	    goto done;
408

409 410 411 412 413 414 415 416 417 418
	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;
	}
419 420 421 422 423 424 425
    }

done:
    if (idlist != NULL)
        free(idlist);

    return(ret);
D
Daniel Veillard 已提交
426 427
}

K
 
Karel Zak 已提交
428 429 430 431
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
432 433
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
434 435 436 437 438 439 440 441
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDomains(virConnectPtr conn) {
    int ret = -1;
    unsigned int num;
    char **idlist = NULL;

D
Daniel Veillard 已提交
442 443
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
444
	return(-1);
D
Daniel Veillard 已提交
445
    }
K
Karel Zak 已提交
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460
    /* 
     * 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) {
461 462 463 464
        idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
	if (idlist) {
	    free(idlist);
	    ret = num;
465
	}
K
 
Karel Zak 已提交
466 467 468 469
    }
    return(ret);
}

D
Daniel Veillard 已提交
470
/**
471
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
472
 * @conn: pointer to the hypervisor connection
473
 * @xmlDesc: an XML description of the domain
474
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
475
 *
476 477 478
 * 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.
D
Daniel Veillard 已提交
479 480 481
 * 
 * Returns a new domain object or NULL in case of failure
 */
482
virDomainPtr
483 484
virDomainCreateLinux(virConnectPtr conn, 
                     const char *xmlDesc,
485
		     unsigned int flags ATTRIBUTE_UNUSED) {
486 487 488 489
    int ret;
    char *sexpr;
    char *name = NULL;
    virDomainPtr dom;
K
Karel Zak 已提交
490

D
Daniel Veillard 已提交
491 492
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
493
	return(NULL);
D
Daniel Veillard 已提交
494 495 496
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
D
Daniel Veillard 已提交
497
        return(NULL);
D
Daniel Veillard 已提交
498 499
    }

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    sexpr = virDomainParseXMLDesc(xmlDesc, &name);
    if ((sexpr == NULL) || (name == NULL)) {
        if (sexpr != NULL)
	    free(sexpr);
        if (name != NULL)
	    free(name);

        return(NULL);
    }

    ret = xend_create_sexpr(conn, sexpr);
    free(sexpr);
    if (ret != 0) {
        fprintf(stderr, "Failed to create domain %s\n", name);
	goto error;
    }

    ret = xend_wait_for_devices(conn, name);
    if (ret != 0) {
        fprintf(stderr, "Failed to get devices for domain %s\n", name);
	xend_destroy(conn, name);
	goto error;
    }

    ret = xend_unpause(conn, name);
    if (ret != 0) {
        fprintf(stderr, "Failed to resume new domain %s\n", name);
	xend_destroy(conn, name);
	goto error;
    }

    dom = virDomainLookupByName(conn, name);
    free(name);

    return(dom);
error:
    if (name != NULL)
        free(name);
D
Daniel Veillard 已提交
538 539 540
    return(NULL);
}

541 542 543 544 545 546 547 548 549 550 551 552
/**
 * 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) {
553 554 555 556
    if ((conn == NULL) || (conn->xshandle == NULL) || (path == NULL) ||
        (nb == NULL))
        return(NULL);

557
    return xs_directory(conn->xshandle, 0, path, nb);
558 559 560
}

/**
561
 * virDomainDoStoreQuery:
562 563 564 565 566 567 568 569
 * @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 *
570
virDomainDoStoreQuery(virDomainPtr domain, const char *path) {
571 572
    char s[256];
    unsigned int len = 0;
K
Karel Zak 已提交
573 574 575
    
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
	return(NULL);
576 577
    if (domain->conn->xshandle == NULL)
        return(NULL);
578 579 580 581

    snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
    s[255] = 0;

582
    return xs_read(domain->conn->xshandle, 0, &s[0], &len);
583 584
}

585

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
/**
 * 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) {
    char s[256];

    int ret = -1;

K
Karel Zak 已提交
603
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
604
	return(-1);
605 606
    if (domain->conn->xshandle == NULL)
        return(-1);
K
Karel Zak 已提交
607
    if (domain->conn->flags & VIR_CONNECT_RO)
608 609 610 611 612
        return(-1);

    snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
    s[255] = 0;

613
    if (xs_write(domain->conn->xshandle, 0, &s[0], value, strlen(value)))
614 615 616 617 618
        ret = 0;

    return(ret);
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
/**
 * 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)
{
    char *vm;
    char query[200];
    unsigned int len;
    
K
Karel Zak 已提交
634
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
635
	return(NULL);
636 637
    if (domain->conn->xshandle == NULL)
        return(NULL);
K
Karel Zak 已提交
638
    
639 640 641 642
    snprintf(query, 199, "/local/domain/%d/vm", 
             virDomainGetID(domain));
    query[199] = 0;

643
    vm = xs_read(domain->conn->xshandle, 0, &query[0], &len);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

    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) {
    char s[256];
    char *ret = NULL;
    unsigned int len = 0;
    
K
Karel Zak 已提交
666
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
667
	return(NULL);
668 669
    if (domain->conn->xshandle==NULL)
	return(NULL);
K
Karel Zak 已提交
670
    
671 672 673
    snprintf(s, 255, "%s/%s", vm, name);
    s[255] = 0;

674
    ret = xs_read(domain->conn->xshandle, 0, &s[0], &len);
675 676 677 678

    return(ret);
}

679
/**
680
 * virDomainLookupByID:
681 682 683 684 685 686 687
 * @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
 */
688 689
virDomainPtr
virDomainLookupByID(virConnectPtr conn, int id) {
690
    char *path = NULL;
691
    virDomainPtr ret;
692
    char *name = NULL;
693
    unsigned char uuid[16];
694

D
Daniel Veillard 已提交
695 696
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
697
	return(NULL);
D
Daniel Veillard 已提交
698 699 700
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
701
        return(NULL);
D
Daniel Veillard 已提交
702
    }
703

704 705 706 707 708
    /* 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 */
709
    if (path == NULL) {
710 711 712 713 714 715
        char **names = xend_get_domains(conn);
	char **tmp = names;
	int ident;

	if (names != NULL) {
	    while (*tmp != NULL) {
716
		ident = xend_get_domain_ids(conn, *tmp, &uuid[0]);
717 718 719 720 721 722 723 724
		if (ident == id) {
		    name = strdup(*tmp);
		    break;
		}
		tmp++;
	    }
	    free(names);
	}
725
    }
726

727
    ret = (virDomainPtr) malloc(sizeof(virDomain));
728
    if (ret == NULL) {
729
        goto error;
730
    }
731
    memset(ret, 0, sizeof(virDomain));
732
    ret->magic = VIR_DOMAIN_MAGIC;
733 734
    ret->conn = conn;
    ret->handle = id;
735
    ret->path = path;
736
    if (name == NULL) {
737
	ret->name = virDomainDoStoreQuery(ret, "name");
738
    } else {
739
        ret->name = name;
740 741
        memcpy(&ret->uuid[0], uuid, 16);
    }
742
    if (ret->name == NULL) {
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
        goto error;
    }

    return(ret);
error:
    if (ret != NULL)
	free(path);
    if (path != NULL)
	free(path);
    if (path != NULL)
	free(path);
    return(NULL);
}

/**
 * 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
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid) {
    virDomainPtr ret;
    char *name = NULL;
    char **names;
    char **tmp;
    unsigned char ident[16];
773
    int id = -1;
774

D
Daniel Veillard 已提交
775 776
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
777
	return(NULL);
D
Daniel Veillard 已提交
778 779 780
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
781
        return(NULL);
D
Daniel Veillard 已提交
782
    }
783 784 785 786 787
    names = xend_get_domains(conn);
    tmp = names;

    if (names == NULL) {
        TODO /* try to fallback to xenstore lookup */
788 789
	return(NULL);
    }
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    while (*tmp != NULL) {
	id = xend_get_domain_ids(conn, *tmp, &ident[0]);
	if (id >= 0) {
	    if (!memcmp(uuid, ident, 16)) {
		name = strdup(*tmp);
		break;
	    }
	}
	tmp++;
    }
    free(names);

    if (name == NULL)
        return(NULL);

    ret = (virDomainPtr) malloc(sizeof(virDomain));
    if (ret == NULL) {
        if (name != NULL)
	    free(name);
	return(NULL);
    }
811
    memset(ret, 0, sizeof(virDomain));
812 813 814 815
    ret->magic = VIR_DOMAIN_MAGIC;
    ret->conn = conn;
    ret->handle = id;
    ret->name = name;
816
    ret->path = 0;
817
    memcpy(ret->uuid, uuid, 16);
818 819 820 821

    return(ret);
}

822 823 824 825 826
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
827
 * Try to lookup a domain on the given hypervisor based on its name.
828 829 830 831 832 833 834
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
virDomainLookupByName(virConnectPtr conn, const char *name) {
    virDomainPtr ret = NULL;
    unsigned int num, i, len;
835
    long id = -1;
836
    char **idlist = NULL, *endptr;
837 838
    char prop[200], *tmp, *path = NULL;
    unsigned char *uuid = NULL;
839
    int found = 0;
840
    struct xend_domain *xenddomain = NULL;
841

D
Daniel Veillard 已提交
842 843
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
K
Karel Zak 已提交
844
	return(NULL);
D
Daniel Veillard 已提交
845 846 847
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
848
        return(NULL);
D
Daniel Veillard 已提交
849
    }
850

851 852 853 854 855 856 857 858 859 860
    /* 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 */
861
    if (conn->xshandle != NULL) {
862
        idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
863 864 865 866 867 868 869 870 871 872 873 874
        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;
875
	     tmp = xs_read(conn->xshandle, 0, prop, &len);
876 877 878 879 880 881 882 883
	     if (tmp != NULL) {
	         found = !strcmp(name, tmp);
	         free(tmp);
	         if (found)
	             break;
	     }
        }
        path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
884
    }
885 886 887

do_found:

888 889 890 891
    if (found) {
	ret = (virDomainPtr) malloc(sizeof(virDomain));
	if (ret == NULL)
	    goto done;
892
	memset(ret, 0, sizeof(virDomain));
893 894 895
	ret->magic = VIR_DOMAIN_MAGIC;
	ret->conn = conn;
	ret->handle = id;
896 897 898
	ret->path = path;
	if (uuid != NULL)
	    memcpy(ret->uuid, uuid, 16);
899 900 901 902
	ret->name = strdup(name);
    }

done:
903 904
    if (xenddomain != NULL)
	free(xenddomain);
905 906 907 908 909 910
    if (idlist != NULL)
        free(idlist);

    return(ret);
}

D
Daniel Veillard 已提交
911
/**
912
 * virDomainDestroy:
D
Daniel Veillard 已提交
913 914 915 916
 * @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.
917 918
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
919
 * This function may requires priviledged access
D
Daniel Veillard 已提交
920 921
 *
 * Returns 0 in case of success and -1 in case of failure.
922
 */
D
Daniel Veillard 已提交
923
int
924
virDomainDestroy(virDomainPtr domain) {
925 926
    int ret;

D
Daniel Veillard 已提交
927 928
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
929
	return(-1);
D
Daniel Veillard 已提交
930
    }
931 932 933 934 935 936 937 938 939 940

    /*
     * try first with the xend method
     */
    ret = xend_destroy(domain->conn, domain->name);
    if (ret == 0) {
        virDomainFree(domain);
	return(0);
    }

941 942 943
    ret = xenHypervisorDestroyDomain(domain->conn->handle, domain->handle);
    if (ret < 0)
        return(-1);
944
    
945 946
    virDomainFree(domain);
    return(0);
947 948 949 950 951 952 953 954 955 956 957 958 959
}

/**
 * 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) {
D
Daniel Veillard 已提交
960 961
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
962
	return(-1);
D
Daniel Veillard 已提交
963
    }
964 965 966 967 968 969 970 971
    domain->magic = -1;
    domain->handle = -1;
    if (domain->path != NULL)
        free(domain->path);
    if (domain->name)
        free(domain->name);
    free(domain);
    return(0);
D
Daniel Veillard 已提交
972 973 974
}

/**
975
 * virDomainSuspend:
D
Daniel Veillard 已提交
976 977 978 979
 * @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 
980
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
981
 * the domain.
982
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
983 984 985 986
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
987
virDomainSuspend(virDomainPtr domain) {
988 989
    int ret;

D
Daniel Veillard 已提交
990 991
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
992
	return(-1);
D
Daniel Veillard 已提交
993 994
    }

995 996 997 998 999 1000
    /* first try though the Xen daemon */
    ret = xend_pause(domain->conn, domain->name);
    if (ret == 0)
        return(0);

    /* then try a direct hypervisor access */
1001
    return(xenHypervisorPauseDomain(domain->conn->handle, domain->handle));
D
Daniel Veillard 已提交
1002 1003 1004
}

/**
1005
 * virDomainResume:
D
Daniel Veillard 已提交
1006 1007 1008
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
1009
 * it was frozen by calling virSuspendDomain().
1010
 * This function may requires priviledged access
D
Daniel Veillard 已提交
1011 1012 1013 1014
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1015
virDomainResume(virDomainPtr domain) {
1016 1017
    int ret;

D
Daniel Veillard 已提交
1018 1019
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1020
	return(-1);
D
Daniel Veillard 已提交
1021 1022
    }

1023 1024 1025 1026 1027 1028
    /* first try though the Xen daemon */
    ret = xend_unpause(domain->conn, domain->name);
    if (ret == 0)
        return(0);

    /* then try a direct hypervisor access */
1029
    return(xenHypervisorResumeDomain(domain->conn->handle, domain->handle));
D
Daniel Veillard 已提交
1030 1031
}

1032 1033 1034 1035 1036 1037
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
1038 1039 1040
 * 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.
1041 1042 1043 1044 1045 1046
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainSave(virDomainPtr domain, const char *to) {
    int ret;
1047
    char filepath[4096];
1048

D
Daniel Veillard 已提交
1049 1050
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1051
	return(-1);
D
Daniel Veillard 已提交
1052 1053 1054 1055 1056
    }
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    /*
     * 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];

    }

1078 1079 1080 1081 1082 1083
    ret = xend_save(domain->conn, domain->name, to);
    return(ret);
}

/**
 * virDomainRestore:
1084
 * @conn: pointer to the hypervisor connection
1085 1086 1087 1088 1089 1090 1091
 * @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
1092
virDomainRestore(virConnectPtr conn, const char *from) {
1093
    int ret;
1094
    char filepath[4096];
1095

D
Daniel Veillard 已提交
1096 1097
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
1098
	return(-1);
D
Daniel Veillard 已提交
1099 1100 1101 1102 1103 1104
    }
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    /*
     * 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];
    }
1123
    
1124
    ret = xend_restore(conn, from);
1125 1126 1127
    return(ret);
}

1128 1129 1130 1131 1132
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1133 1134
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
 *
 * 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;

D
Daniel Veillard 已提交
1145 1146
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1147
	return(-1);
D
Daniel Veillard 已提交
1148
    }
K
Karel Zak 已提交
1149
    
1150 1151 1152 1153 1154 1155
    /*
     * try first with the xend daemon
     */
    ret = xend_shutdown(domain->conn, domain->name);
    if (ret == 0)
        return(0);
1156

1157
    /*
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
     * 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);
}

1168
/**
1169
 * virDomainGetName:
1170 1171 1172 1173 1174 1175 1176 1177
 * @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 *
1178
virDomainGetName(virDomainPtr domain) {
D
Daniel Veillard 已提交
1179 1180
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1181
	return(NULL);
D
Daniel Veillard 已提交
1182
    }
1183 1184 1185
    return(domain->name);
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
/**
 * 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
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid) {
D
Daniel Veillard 已提交
1197 1198 1199 1200 1201 1202
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
	return(-1);
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1203
	return(-1);
D
Daniel Veillard 已提交
1204 1205 1206
    }

    if (domain->handle == 0) {
1207
        memset(uuid, 0, 16);
D
Daniel Veillard 已提交
1208
    } else {
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	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))
	    xend_get_domain_ids(domain->conn, domain->name, &domain->uuid[0]);
	memcpy(uuid, &domain->uuid[0], 16);
    }
    return(0);
}

1223
/**
1224
 * virDomainGetID:
1225 1226 1227 1228 1229 1230 1231
 * @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
1232
virDomainGetID(virDomainPtr domain) {
D
Daniel Veillard 已提交
1233 1234
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1235
	return((unsigned int) -1);
D
Daniel Veillard 已提交
1236
    }
1237
    return(domain->handle);
1238 1239
}

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
/**
 * 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;
    
D
Daniel Veillard 已提交
1252 1253
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1254
	return(NULL);
D
Daniel Veillard 已提交
1255
    }
K
Karel Zak 已提交
1256
    
1257 1258 1259 1260 1261
    vm = virDomainGetVM(domain);
    if (vm) {
    	str = virDomainGetVMInfo(domain, vm, "image/ostype");
	free(vm);
    }
1262 1263 1264
    if (str == NULL)
        str = strdup("linux");

1265 1266 1267
    return(str);
}

1268
/**
1269
 * virDomainGetMaxMemory:
1270 1271 1272 1273 1274 1275 1276 1277 1278
 * @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
1279
virDomainGetMaxMemory(virDomainPtr domain) {
1280 1281
    unsigned long ret = 0;

D
Daniel Veillard 已提交
1282 1283
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1284
	return(0);
D
Daniel Veillard 已提交
1285
    }
K
Karel Zak 已提交
1286
    
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    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);
1306 1307
}

D
Daniel Veillard 已提交
1308
/**
1309
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1310 1311 1312 1313 1314 1315
 * @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.
1316
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1317 1318 1319 1320
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1321
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory) {
1322 1323 1324
    int ret;
    char s[256], v[30];
    
D
Daniel Veillard 已提交
1325 1326
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
K
Karel Zak 已提交
1327
	return(-1);
D
Daniel Veillard 已提交
1328 1329 1330
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
D
Daniel Veillard 已提交
1331
        return(-1);
D
Daniel Veillard 已提交
1332
    }
1333 1334
    if (domain->conn->flags & VIR_CONNECT_RO)
        return(-1);
1335 1336
    if (domain->conn->xshandle==NULL)
	return(-1);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
    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;

1352 1353
    if (!xs_write(domain->conn->xshandle, 0, &s[0], &v[0], strlen(v)))
        ret = -1;
1354 1355

    return(ret);
D
Daniel Veillard 已提交
1356 1357
}

1358 1359
/**
 * virDomainGetInfo:
1360
 * @domain: a domain object
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
 * @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;
1372 1373 1374 1375
    char *tmp, **tmp2;
    unsigned int nb_vcpus;
    char request[200];

1376

D
Daniel Veillard 已提交
1377 1378
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1379
	return(-1);
D
Daniel Veillard 已提交
1380 1381 1382
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
K
Karel Zak 已提交
1383
	return(-1);
D
Daniel Veillard 已提交
1384
    }
K
Karel Zak 已提交
1385
    
1386
    memset(info, 0, sizeof(virDomainInfo));
K
Karel Zak 已提交
1387
    
1388 1389 1390 1391
    /*
     * if we have direct access though the hypervisor do a direct call
     */
    if (domain->conn->handle >= 0) {
1392
        dom0_getdomaininfo_t dominfo;
1393 1394

	dominfo.domain = domain->handle;
1395 1396
        ret = xenHypervisorGetDomainInfo(domain->conn->handle, domain->handle,
	                                 &dominfo);
1397
        if (ret < 0)
1398 1399
	    goto xend_info;

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	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;
	}
1419 1420 1421 1422

	/*
	 * the API brings back the cpu time in nanoseconds,
	 * convert to microseconds, same thing convert to
1423
         * kilobytes from page counts
1424
	 */
1425
	info->cpuTime = dominfo.cpu_time;
1426 1427
	info->memory = dominfo.tot_pages * 4;
	info->maxMem = dominfo.max_pages * 4;
1428
	info->nrVirtCpu = dominfo.nr_online_vcpus;
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
	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);
1476 1477 1478
    }
    return(0);
}
1479

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
/**
 * 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 *
virDomainGetXMLDesc(virDomainPtr domain, int flags) {
D
Daniel Veillard 已提交
1493 1494
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1495
	return(NULL);
D
Daniel Veillard 已提交
1496 1497 1498
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1499
	return(NULL);
D
Daniel Veillard 已提交
1500
    }
1501 1502 1503

    return(xend_get_domain_xml(domain));
}