libvirt.c 50.0 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/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

21 22 23
#include <libxml/parser.h>
#include <libxml/xpath.h>

24
#include <xs.h>
25

D
Daniel Veillard 已提交
26
#include "internal.h"
27
#include "driver.h"
28
#include "xen_internal.h"
29
#include "xend_internal.h"
30
#include "xs_internal.h"
31
#include "proxy_internal.h"
32
#include "xml.h"
33
#include "test.h"
34

D
Daniel Veillard 已提交
35 36 37 38
/*
 * TODO:
 * - use lock to protect against concurrent accesses ?
 * - use reference counting to garantee coherent pointer state ?
39
 * - memory wrappers for malloc/free ?
D
Daniel Veillard 已提交
40 41
 */

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static virDriverPtr virDriverTab[MAX_DRIVERS];
static int initialized = 0;

/**
 * virInitialize:
 *
 * Initialize the library. It's better to call this routine at startup
 * in multithreaded applications to avoid potential race when initializing
 * the library.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virInitialize(void)
{
    int i;

    if (initialized)
        return(0);
61
    initialized = 1;
62

63 64 65
    if (!bindtextdomain(GETTEXT_PACKAGE, LOCALEBASEDIR))
        return (-1);

66 67 68
    /*
     * should not be needed but...
     */
69
    for (i = 0;i < MAX_DRIVERS;i++)
70 71 72 73 74 75
         virDriverTab[i] = NULL;

    /*
     * Note that the order is important the first ones have a higher priority
     */
    xenHypervisorRegister();
76
    xenProxyRegister();
77 78
    xenDaemonRegister();
    xenStoreRegister();
79
    testRegister();
80 81 82 83 84
    return(0);
}



D
Daniel Veillard 已提交
85 86 87 88 89 90 91 92 93
/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
94 95
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
96
    const char *errmsg;
97

D
Daniel Veillard 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    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
115 116 117
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
118 119
    virConnectPtr conn = NULL;
    const char *errmsg;
120

D
Daniel Veillard 已提交
121 122 123 124 125 126 127 128 129 130 131
    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);
}

132 133 134 135 136 137 138 139 140 141 142 143 144
/**
 * virRegisterDriver:
 * @driver: pointer to a driver block
 *
 * Register a virtualization driver
 *
 * Returns the driver priority or -1 in case of error.
 */
int
virRegisterDriver(virDriverPtr driver)
{
    int i;

145
    if (!initialized)
146 147
        if (virInitialize() < 0)
	    return -1;
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    if (driver == NULL) {
        virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
    for (i = 0;i < MAX_DRIVERS;i++) {
        if (virDriverTab[i] == driver)
	    return(i);
    }
    for (i = 0;i < MAX_DRIVERS;i++) {
        if (virDriverTab[i] == NULL) {
	    virDriverTab[i] = driver;
	    return(i);
	}
    }
    virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
    return(-1);
}

167 168 169
/**
 * virGetVersion:
 * @libVer: return value for the library version (OUT)
170
 * @type: the type of connection/driver looked at
171 172 173 174 175 176 177 178 179 180 181 182
 * @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
183 184 185
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
186 187
    int i;

188
    if (!initialized)
189 190
        if (virInitialize() < 0)
	    return -1;
191

192
    if (libVer == NULL)
193
        return (-1);
194 195 196
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
197 198 199 200 201 202 203 204 205 206
        if (type == NULL)
	    type = "Xen";
	for (i = 0;i < MAX_DRIVERS;i++) {
	    if ((virDriverTab[i] != NULL) &&
	        (!strcmp(virDriverTab[i]->name, type))) {
		*typeVer = virDriverTab[i]->ver;
		break;
	    }
	}
        if (i >= MAX_DRIVERS) {
207
            *typeVer = 0;
208
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
209 210 211 212
            return (-1);
        }
    }
    return (0);
213 214
}

D
Daniel Veillard 已提交
215
/**
216
 * virConnectOpen:
D
Daniel Veillard 已提交
217 218 219
 * @name: optional argument currently unused, pass NULL
 *
 * This function should be called first to get a connection to the 
220
 * Hypervisor and xen store
D
Daniel Veillard 已提交
221 222 223
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
224
virConnectPtr
225 226
virConnectOpen(const char *name)
{
227
    int i, res, for_xen = 0;
228
    virConnectPtr ret = NULL;
229

230
    if (!initialized)
231 232
        if (virInitialize() < 0)
	    return NULL;
233

234 235 236
    if (name == NULL) {
        name = "Xen";
	for_xen = 1;
237
    } else if (!strncasecmp(name, "xen", 3)) {
238 239 240
	for_xen = 1;
    }

241
    ret = virGetConnect();
242
    if (ret == NULL) {
243
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
244 245 246
        goto failed;
    }

247 248
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
249
	    res = virDriverTab[i]->open(ret, name, VIR_DRV_OPEN_QUIET);
250 251 252 253
	    /*
	     * For a default connect to Xen make sure we manage to contact
	     * all related drivers.
	     */
254
	    if ((res < 0) && (for_xen) &&
255 256
	        (!strncasecmp(virDriverTab[i]->name, "xen", 3)) &&
		(virDriverTab[i]->no != VIR_DRV_XEN_PROXY))
257 258 259 260 261 262 263 264 265 266 267
		goto failed;
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
	}
    }

    if (ret->nb_drivers == 0) {
	/* we failed to find an adequate driver */
	virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
    }
268

269
    return (ret);
270 271 272

failed:
    if (ret != NULL) {
273 274 275 276
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
277
	virFreeConnect(ret);
278
    }
279
    return (NULL);
280 281 282
}

/**
283
 * virConnectOpenReadOnly:
284 285
 * @name: optional argument currently unused, pass NULL
 *
286 287 288
 * 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.
289 290 291
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
292
virConnectPtr
293 294
virConnectOpenReadOnly(const char *name)
{
295
    int i, res;
296
    virConnectPtr ret = NULL;
297

298
    if (!initialized)
299 300
        if (virInitialize() < 0)
	    return NULL;
301

302 303 304
    if (name == NULL)
        name = "Xen";

305
    ret = virGetConnect();
D
Daniel Veillard 已提交
306
    if (ret == NULL) {
307
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, _("allocating connection"));
308
        goto failed;
D
Daniel Veillard 已提交
309
    }
310

311 312 313 314 315 316
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
	    res = virDriverTab[i]->open(ret, name,
	                                VIR_DRV_OPEN_QUIET | VIR_DRV_OPEN_RO);
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
317

318 319 320 321 322
	}
    }
    if (ret->nb_drivers == 0) {
	if (name == NULL)
	    virLibConnError(NULL, VIR_ERR_NO_CONNECT,
323
			    _("Xen Daemon or Xen Store"));
324 325 326 327
	else
	    /* we failed to find an adequate driver */
	    virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
D
Daniel Veillard 已提交
328
    }
329 330
    ret->flags = VIR_CONNECT_RO;

331
    return (ret);
332 333

failed:
334
    if (ret != NULL) {
335 336 337 338
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
339
	virFreeConnect(ret);
340
    }
341
    return (NULL);
D
Daniel Veillard 已提交
342 343 344
}

/**
345
 * virConnectClose:
D
Daniel Veillard 已提交
346 347 348 349 350 351 352 353 354 355
 * @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
356 357
virConnectClose(virConnectPtr conn)
{
358 359
    int i;

K
Karel Zak 已提交
360
    if (!VIR_IS_CONNECT(conn))
361
        return (-1);
362 363 364 365
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL))
	    conn->drivers[i]->close(conn);
    }
366 367
    if (virFreeConnect(conn) < 0)
        return (-1);
368
    return (0);
D
Daniel Veillard 已提交
369 370
}

371 372 373 374 375 376 377 378 379
/**
 * 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 *
380 381
virConnectGetType(virConnectPtr conn)
{
382
    int i;
383
    const char *ret;
384

D
Daniel Veillard 已提交
385
    if (!VIR_IS_CONNECT(conn)) {
386
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
387
        return (NULL);
D
Daniel Veillard 已提交
388
    }
389 390 391 392 393 394 395 396
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->type != NULL)) {
	    ret = conn->drivers[i]->type(conn);
	    if (ret != NULL)
	        return(ret);
	}
    }
397 398 399 400 401 402 403
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->name != NULL)) {
	    return(conn->drivers[i]->name);
	}
    }
    return(NULL);
404 405
}

D
Daniel Veillard 已提交
406
/**
407
 * virConnectGetVersion:
D
Daniel Veillard 已提交
408
 * @conn: pointer to the hypervisor connection
409
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
410
 *
411 412 413
 * 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 已提交
414
 *
415 416 417
 * 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 已提交
418
 */
419
int
420 421
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
422
    int ret, i;
423

D
Daniel Veillard 已提交
424 425
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
426
        return (-1);
D
Daniel Veillard 已提交
427
    }
428

D
Daniel Veillard 已提交
429 430
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
431
        return (-1);
D
Daniel Veillard 已提交
432
    }
433

434 435 436 437 438 439 440 441 442
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->version != NULL)) {
	    ret = conn->drivers[i]->version(conn, hvVer);
	    if (ret == 0)
	        return(0);
	}
    }
    return (-1);
443 444 445
}

/**
446
 * virConnectListDomains:
447 448 449 450 451 452 453 454 455
 * @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
456 457
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
458
    int ret = -1;
459
    int i;
460

D
Daniel Veillard 已提交
461 462
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
463
        return (-1);
D
Daniel Veillard 已提交
464
    }
465

D
Daniel Veillard 已提交
466 467
    if ((ids == NULL) || (maxids <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
468
        return (-1);
D
Daniel Veillard 已提交
469
    }
470

471 472 473 474 475 476 477 478 479 480
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->listDomains != NULL)) {
	    ret = conn->drivers[i]->listDomains(conn, ids, maxids);
	    if (ret >= 0)
	        return(ret);
	}
    }

481
    return (-1);
D
Daniel Veillard 已提交
482 483
}

K
 
Karel Zak 已提交
484 485 486 487
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
488 489
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
490 491 492
 * Returns the number of domain found or -1 in case of error
 */
int
493 494
virConnectNumOfDomains(virConnectPtr conn)
{
K
 
Karel Zak 已提交
495
    int ret = -1;
496
    int i;
K
 
Karel Zak 已提交
497

D
Daniel Veillard 已提交
498 499
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
500
        return (-1);
D
Daniel Veillard 已提交
501
    }
K
Karel Zak 已提交
502

503 504 505 506 507 508 509 510 511 512
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->numOfDomains != NULL)) {
	    ret = conn->drivers[i]->numOfDomains(conn);
	    if (ret >= 0)
	        return(ret);
	}
    }

513
    return(-1);
K
 
Karel Zak 已提交
514 515
}

D
Daniel Veillard 已提交
516
/**
517
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
518
 * @conn: pointer to the hypervisor connection
519
 * @xmlDesc: an XML description of the domain
520
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
521
 *
522 523 524
 * 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 已提交
525 526 527
 * 
 * Returns a new domain object or NULL in case of failure
 */
528
virDomainPtr
529 530
virDomainCreateLinux(virConnectPtr conn, const char *xmlDesc,
                     unsigned int flags)
531
{
532 533
    virDomainPtr ret;
    int i;
K
Karel Zak 已提交
534

D
Daniel Veillard 已提交
535 536
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
537
        return (NULL);
D
Daniel Veillard 已提交
538 539 540
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
541
        return (NULL);
D
Daniel Veillard 已提交
542
    }
543 544 545 546
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
D
Daniel Veillard 已提交
547

548 549 550 551 552 553 554
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainCreateLinux != NULL)) {
	    ret = conn->drivers[i]->domainCreateLinux(conn, xmlDesc, flags);
	    if (ret != NULL)
	        return(ret);
	}
555
    }
556
    return(NULL);
D
Daniel Veillard 已提交
557 558
}

559

560
/**
561
 * virDomainLookupByID:
562 563 564 565 566 567 568
 * @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
 */
569
virDomainPtr
570 571
virDomainLookupByID(virConnectPtr conn, int id)
{
572
    virDomainPtr ret;
573
    int i;
574

D
Daniel Veillard 已提交
575 576
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
577
        return (NULL);
D
Daniel Veillard 已提交
578 579 580
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
581
        return (NULL);
D
Daniel Veillard 已提交
582
    }
583

584 585 586 587 588 589 590 591 592 593
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainLookupByID != NULL)) {
	    ret = conn->drivers[i]->domainLookupByID(conn, id);
	    if (ret)
	        return(ret);
	}
    }

594
    return (NULL);
595 596 597 598 599
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
600
 * @uuid: the raw UUID for the domain
601 602 603 604 605 606
 *
 * 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
607 608
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
609
    virDomainPtr ret;
610
    int i;
611

D
Daniel Veillard 已提交
612 613
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
614
        return (NULL);
D
Daniel Veillard 已提交
615 616 617
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
618
        return (NULL);
D
Daniel Veillard 已提交
619
    }
620 621 622 623 624 625 626 627 628 629 630

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainLookupByUUID != NULL)) {
	    ret = conn->drivers[i]->domainLookupByUUID(conn, uuid);
	    if (ret)
	        return(ret);
	}
    }

631
    return (NULL);
632 633
}

K
Karel Zak 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
/**
 * virDomainLookupByUUIDString:
 * @conn: pointer to the hypervisor connection
 * @uuidstr: the string UUID 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
virDomainLookupByUUIDString(virConnectPtr conn, const char *uuidstr)
{
    int raw[16], i;
    unsigned char uuid[16];
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuidstr == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
	
    }
    /* XXX: sexpr_uuid() also supports 'xxxx-xxxx-xxxx-xxxx' format. 
     *      We needn't it here. Right? 
     */
    ret = sscanf(uuidstr,
                 "%02x%02x%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x-"
                 "%02x%02x%02x%02x%02x%02x",
                 raw + 0, raw + 1, raw + 2, raw + 3,
                 raw + 4, raw + 5, raw + 6, raw + 7,
                 raw + 8, raw + 9, raw + 10, raw + 11,
                 raw + 12, raw + 13, raw + 14, raw + 15);
    
    if (ret!=16) {
	virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
    for (i = 0; i < 16; i++)
        uuid[i] = raw[i] & 0xFF;
    
    return virDomainLookupByUUID(conn, &uuid[0]);
}

683 684 685 686 687
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
688
 * Try to lookup a domain on the given hypervisor based on its name.
689 690 691 692
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
693 694
virDomainLookupByName(virConnectPtr conn, const char *name)
{
695
    virDomainPtr ret = NULL;
696
    int i;
697

D
Daniel Veillard 已提交
698 699
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
700
        return (NULL);
D
Daniel Veillard 已提交
701 702 703
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
704
        return (NULL);
D
Daniel Veillard 已提交
705
    }
706

707 708 709 710 711 712 713 714 715
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainLookupByName != NULL)) {
	    ret = conn->drivers[i]->domainLookupByName(conn, name);
	    if (ret)
	        return(ret);
	}
    }
716
    return (NULL);
717 718
}

D
Daniel Veillard 已提交
719
/**
720
 * virDomainDestroy:
D
Daniel Veillard 已提交
721 722 723 724
 * @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.
725 726
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
727
 * This function may requires priviledged access
D
Daniel Veillard 已提交
728 729
 *
 * Returns 0 in case of success and -1 in case of failure.
730
 */
D
Daniel Veillard 已提交
731
int
732 733
virDomainDestroy(virDomainPtr domain)
{
734
    int i;
735
    virConnectPtr conn;
736

D
Daniel Veillard 已提交
737 738
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
739
        return (-1);
D
Daniel Veillard 已提交
740
    }
741

742
    conn = domain->conn;
743 744 745 746
    if (conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
747

748 749 750 751
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
752 753
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
754 755 756
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainDestroy != NULL)) {
	    if (conn->drivers[i]->domainDestroy(domain) == 0)
757
	        return (0);
758 759 760 761 762
	}
    }
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
763 764
	    (conn->drivers[i]->domainDestroy != NULL)) {
	    if (conn->drivers[i]->domainDestroy(domain) == 0)
765
	        return (0);
766
	}
767 768
    }

769
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
770
    return (-1);
771 772 773 774 775 776 777 778 779 780 781 782
}

/**
 * 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
783 784
virDomainFree(virDomainPtr domain)
{
D
Daniel Veillard 已提交
785 786
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
787
        return (-1);
D
Daniel Veillard 已提交
788
    }
789 790 791
    if (virFreeDomain(domain->conn, domain) < 0)
        return (-1);
    return(0);
D
Daniel Veillard 已提交
792 793 794
}

/**
795
 * virDomainSuspend:
D
Daniel Veillard 已提交
796 797 798 799
 * @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 
800
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
801
 * the domain.
802
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
803 804 805 806
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
807 808
virDomainSuspend(virDomainPtr domain)
{
809
    int i;
810
    virConnectPtr conn;
811

D
Daniel Veillard 已提交
812 813
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
814
        return (-1);
D
Daniel Veillard 已提交
815
    }
816 817 818 819
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
820

821 822
    conn = domain->conn;

823 824 825 826 827 828 829 830 831
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainSuspend != NULL)) {
	    if (conn->drivers[i]->domainSuspend(domain) == 0)
832
	        return (0);
833 834
	}
    }
835 836
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
837
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
838 839
	    (conn->drivers[i]->domainSuspend != NULL)) {
	    if (conn->drivers[i]->domainSuspend(domain) == 0)
840
	        return (0);
841 842 843 844
	}
    }

        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
845
    return (-1);
D
Daniel Veillard 已提交
846 847 848
}

/**
849
 * virDomainResume:
D
Daniel Veillard 已提交
850 851 852
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
853
 * it was frozen by calling virSuspendDomain().
854
 * This function may requires priviledged access
D
Daniel Veillard 已提交
855 856 857 858
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
859 860
virDomainResume(virDomainPtr domain)
{
861
    int i;
862
    virConnectPtr conn;
863

D
Daniel Veillard 已提交
864 865
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
866
        return (-1);
D
Daniel Veillard 已提交
867
    }
868 869 870 871
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
872

873 874
    conn = domain->conn;

875 876 877 878 879 880 881 882 883 884 885 886
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainResume != NULL)) {
	    if (conn->drivers[i]->domainResume(domain) == 0)
	        return(0);
	}
    }
887 888
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
889
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
890 891
	    (conn->drivers[i]->domainResume != NULL)) {
	    if (conn->drivers[i]->domainResume(domain) == 0)
892
	        return(0);
893 894 895
	}
    }

896 897
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
D
Daniel Veillard 已提交
898 899
}

900 901 902 903 904 905
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
906 907 908
 * 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.
909 910 911 912
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
913 914
virDomainSave(virDomainPtr domain, const char *to)
{
915
    int ret, i;
916
    char filepath[4096];
917
    virConnectPtr conn;
918

D
Daniel Veillard 已提交
919 920
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
921
        return (-1);
D
Daniel Veillard 已提交
922
    }
923 924 925 926
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
927
    conn = domain->conn;
D
Daniel Veillard 已提交
928 929
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
930
        return (-1);
D
Daniel Veillard 已提交
931
    }
932

933 934 935 936 937
    /*
     * 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] != '/') {
938
        unsigned int len, t;
939

940 941 942 943 944 945 946 947 948 949
        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];
950 951 952

    }

953 954 955 956 957 958 959 960 961 962 963
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSave != NULL)) {
	    ret = conn->drivers[i]->domainSave(domain, to);
	    if (ret == 0)
	        return(0);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
964 965 966 967
}

/**
 * virDomainRestore:
968
 * @conn: pointer to the hypervisor connection
969 970 971 972 973 974 975
 * @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
976 977
virDomainRestore(virConnectPtr conn, const char *from)
{
978
    int ret, i;
979
    char filepath[4096];
980

D
Daniel Veillard 已提交
981 982
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
983
        return (-1);
D
Daniel Veillard 已提交
984
    }
985 986 987 988
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
D
Daniel Veillard 已提交
989 990
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
991
        return (-1);
D
Daniel Veillard 已提交
992 993
    }

994 995 996 997 998
    /*
     * 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] != '/') {
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
        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];
    }

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSave != NULL)) {
	    ret = conn->drivers[i]->domainRestore(conn, from);
	    if (ret == 0)
	        return(0);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1024 1025
}

1026 1027 1028 1029 1030
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1031 1032
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1033 1034 1035 1036 1037 1038 1039
 *
 * 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
1040 1041
virDomainShutdown(virDomainPtr domain)
{
1042 1043
    int ret = -1, i;
    virConnectPtr conn;
1044

D
Daniel Veillard 已提交
1045 1046
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1047
        return (-1);
D
Daniel Veillard 已提交
1048
    }
1049 1050 1051 1052
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1053

1054 1055 1056 1057 1058 1059 1060 1061 1062
    conn = domain->conn;

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainShutdown != NULL)) {
	    if (conn->drivers[i]->domainShutdown(domain) == 0)
	        ret = 0;
	}
1063
    }
1064

1065 1066 1067
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (ret);
1068
    }
1069

1070
    return (ret);
1071 1072
}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
/**
 * virDomainReboot:
 * @domain: a domain object
 * @flags: extra flags for the reboot operation, not used yet
 *
 * Reboot a domain, the domain object is still usable there after but
 * the domain OS is being stopped for a restart.
 * Note that the guest OS may ignore the request.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainReboot(virDomainPtr domain, unsigned int flags)
{
1087 1088
    int ret = -1, i;
    virConnectPtr conn;
1089 1090 1091 1092 1093

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1094 1095 1096 1097
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107
    conn = domain->conn;

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainReboot != NULL)) {
	    if (conn->drivers[i]->domainReboot(domain, flags) == 0)
	        ret = 0;
	}
1108 1109
    }

1110 1111 1112
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (ret);
1113
    }
1114

1115 1116 1117
    return (ret);
}

1118
/**
1119
 * virDomainGetName:
1120 1121 1122 1123 1124 1125 1126 1127
 * @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 *
1128 1129
virDomainGetName(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1130 1131
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1132
        return (NULL);
D
Daniel Veillard 已提交
1133
    }
1134
    return (domain->name);
1135 1136
}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
/**
 * 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
1147 1148
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
D
Daniel Veillard 已提交
1149 1150
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1151
        return (-1);
D
Daniel Veillard 已提交
1152 1153 1154
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1155
        return (-1);
D
Daniel Veillard 已提交
1156 1157 1158
    }

    if (domain->handle == 0) {
1159
        memset(uuid, 0, 16);
D
Daniel Veillard 已提交
1160
    } else {
1161 1162 1163 1164 1165 1166 1167 1168
        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))
1169
            xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
1170 1171 1172 1173
                                &domain->uuid[0]);
        memcpy(uuid, &domain->uuid[0], 16);
    }
    return (0);
1174 1175
}

K
Karel Zak 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
/**
 * virDomainGetUUIDString:
 * @domain: a domain object
 * @buf: pointer to a 37 bytes array
 *
 * Get the UUID for a domain as string. For more information about 
 * UUID see RFC4122.
 *
 * Returns -1 in case of error, 0 in case of success
 */
int
virDomainGetUUIDString(virDomainPtr domain, char *buf)
{
    unsigned char uuid[16];
1190

K
Karel Zak 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if (buf == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    
    if (virDomainGetUUID(domain, &uuid[0]))
	return (-1);

    snprintf(buf, 37, 
	"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
                      uuid[0], uuid[1], uuid[2], uuid[3],
                      uuid[4], uuid[5], uuid[6], uuid[7],
                      uuid[8], uuid[9], uuid[10], uuid[11],
                      uuid[12], uuid[13], uuid[14], uuid[15]);
    return (0);
}

1212
/**
1213
 * virDomainGetID:
1214 1215 1216 1217 1218 1219 1220
 * @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
1221 1222
virDomainGetID(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1223 1224
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1225
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1226
    }
1227
    return (domain->handle);
1228 1229
}

1230 1231 1232 1233 1234 1235
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1236 1237
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1238 1239
 */
char *
1240 1241
virDomainGetOSType(virDomainPtr domain)
{
1242 1243
    char *str = NULL;
    int i;
1244

D
Daniel Veillard 已提交
1245 1246
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1247
        return (NULL);
D
Daniel Veillard 已提交
1248
    }
1249

1250 1251 1252 1253 1254 1255 1256
    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainGetOSType != NULL)) {
	    str = domain->conn->drivers[i]->domainGetOSType(domain);
	    if (str != NULL)
	        break;
	}
1257
    }
1258

1259 1260 1261
    if (str == NULL)
        str = strdup("linux");

1262
    return (str);
1263 1264
}

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

D
Daniel Veillard 已提交
1282 1283
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1284
        return (0);
D
Daniel Veillard 已提交
1285
    }
1286

1287 1288
    conn = domain->conn;

1289
    /*
1290 1291
     * in that case instead of trying only though one method try all availble.
     * If needed that can be changed back if it's a performcance problem.
1292
     */
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainGetMaxMemory != NULL)) {
	    ret = conn->drivers[i]->domainGetMaxMemory(domain);
	    if (ret != 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1303 1304
}

D
Daniel Veillard 已提交
1305
/**
1306
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1307 1308 1309 1310 1311 1312
 * @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.
1313
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1314 1315 1316 1317
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1318 1319
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1320 1321
    int ret = -1 , i;
    virConnectPtr conn;
1322

1323 1324 1325 1326 1327 1328
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1329
        return (-1);
1330
    }
1331 1332 1333 1334 1335 1336 1337 1338
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1339
    conn = domain->conn;
1340

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
    /*
     * in that case instead of trying only though one method try all availble.
     * If needed that can be changed back if it's a performcance problem.
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSetMaxMemory != NULL)) {
	    if (conn->drivers[i]->domainSetMaxMemory(domain, memory) == 0)
	        ret = 0;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
1354
        return (-1);
1355 1356 1357
    }
    return (ret);
}
1358

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
/**
 * virDomainSetMemory:
 * @domain: a domain object or NULL
 * @memory: the memory size in kilobytes
 * 
 * Dynamically change the target 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.
 * This function may requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainSetMemory(virDomainPtr domain, unsigned long memory)
{
    int ret = -1 , i;
    virConnectPtr conn;
1376

1377 1378 1379 1380 1381 1382 1383
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
1384
    }
1385 1386 1387 1388 1389 1390
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1391
        return (-1);
1392 1393 1394
    }

    conn = domain->conn;
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
    /*
     * in that case instead of trying only though one method try all availble.
     * If needed that can be changed back if it's a performcance problem.
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainSetMemory != NULL)) {
	    if (conn->drivers[i]->domainSetMemory(domain, memory) == 0)
	        ret = 0;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (-1);
    }
1411
    return (ret);
D
Daniel Veillard 已提交
1412 1413
}

1414 1415
/**
 * virDomainGetInfo:
1416
 * @domain: a domain object
1417 1418 1419
 * @info: pointer to a virDomainInfo structure allocated by the user
 * 
 * Extract information about a domain. Note that if the connection
1420
 * used to get the domain is limited only a partial set of the information
1421 1422 1423 1424 1425
 * can be extracted.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1426 1427
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1428
    int i;
1429

D
Daniel Veillard 已提交
1430 1431
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1432
        return (-1);
D
Daniel Veillard 已提交
1433 1434 1435
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1436
        return (-1);
D
Daniel Veillard 已提交
1437
    }
1438

1439
    memset(info, 0, sizeof(virDomainInfo));
1440

1441 1442 1443 1444 1445 1446 1447 1448
    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainGetInfo != NULL)) {
	    if (domain->conn->drivers[i]->domainGetInfo(domain, info) == 0)
	        return 0;
	}
    }

1449
    return (-1);
1450
}
1451

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
/**
 * 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 *
1464 1465
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
1466 1467
    int i;
    char *ret = NULL;
D
Daniel Veillard 已提交
1468 1469
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1470
        return (NULL);
D
Daniel Veillard 已提交
1471 1472 1473
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1474
        return (NULL);
D
Daniel Veillard 已提交
1475
    }
1476

1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
    for (i = 0;i < domain->conn->nb_drivers;i++) {
	if ((domain->conn->drivers[i] != NULL) &&
	    (domain->conn->drivers[i]->domainDumpXML != NULL)) {
            ret = domain->conn->drivers[i]->domainDumpXML(domain, flags);
	    if (ret)
	        break;
	}
    }
    if (!ret) {
        virLibConnError(domain->conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (NULL);
    }
    return(ret);
1490
}
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528

/**
 * virNodeGetInfo:
 * @conn: pointer to the hypervisor connection
 * @info: pointer to a virNodeInfo structure allocated by the user
 * 
 * Extract hardware information about the node.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info) {
    int i;
    int ret = -1;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (info == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->nodeGetInfo != NULL)) {
	    ret = conn->drivers[i]->nodeGetInfo(conn, info);
	    if (ret == 0)
	        break;
	}
    }
    if (ret != 0) {
        virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
        return (-1);
    }
    return(0);
}
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

/************************************************************************
 *									*
 *		Handling of defined but not running domains		*
 *									*
 ************************************************************************/

/**
 * virDomainDefineXML:
 * @conn: pointer to the hypervisor connection
 * @xml: the XML description for the domain, preferably in UTF-8
 *
 * define a domain, but does not start it
 *
 * Returns NULL in case of error, a pointer to the domain otherwise
 */
virDomainPtr
virDomainDefineXML(virConnectPtr conn, const char *xml) {
    virDomainPtr ret = NULL;
1548
    int i;
1549 1550 1551 1552 1553

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
1554 1555 1556 1557
    if (conn->flags & VIR_CONNECT_RO) {
        virLibConnError(conn, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (NULL);
    }
1558 1559 1560 1561 1562
    if (xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (NULL);
    }

1563 1564 1565 1566 1567 1568 1569 1570
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainDefineXML != NULL)) {
            ret = conn->drivers[i]->domainDefineXML(conn, xml);
	    if (ret)
	        return(ret);
	}
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
    }

    return(ret);
}

/**
 * virDomainUndefine:
 * @domain: pointer to a defined domain
 *
 * undefine a domain but does not stop it if it is running
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainUndefine(virDomainPtr domain) {
1586 1587
    int ret, i;
    virConnectPtr conn;
1588 1589 1590 1591 1592

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1593 1594
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1595 1596 1597 1598
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }

1599 1600 1601 1602 1603 1604 1605 1606 1607
    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainUndefine != NULL)) {
	    ret = conn->drivers[i]->domainUndefine(domain);
	    if (ret >= 0)
	        return(ret);
	}
    }
1608

1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
    return(-1);
}

/**
 * virConnectNumOfDefinedDomains:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of active domains.
 *
 * Returns the number of domain found or -1 in case of error
 */
int
virConnectNumOfDefinedDomains(virConnectPtr conn)
{
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->numOfDefinedDomains != NULL)) {
	    ret = conn->drivers[i]->numOfDefinedDomains(conn);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return(-1);
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
}

/**
 * virConnectListDefinedDomains:
 * @conn: pointer to the hypervisor connection
 * @names: pointer to an array to store the names
 * @maxnames: size of the array
 *
 * list the defined domains, stores the pointers to the names in @names
 * 
 * Returns the number of names provided in the array or -1 in case of error
 */
int
virConnectListDefinedDomains(virConnectPtr conn, const char **names,
                             int maxnames) {
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    int ret = -1;
    int i;

    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }

    if ((names == NULL) || (maxnames <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }

    /* Go though the driver registered entry points */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->listDefinedDomains != NULL)) {
	    ret = conn->drivers[i]->listDefinedDomains(conn, names, maxnames);
	    if (ret >= 0)
	        return(ret);
	}
    }

    return (-1);
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
}

/**
 * virDomainCreate:
 * @domain: pointer to a defined domain
 *
 * launch a defined domain. If the call succeed the domain moves from the
 * defined to the running domains pools.
 *
 * Returns 0 in case of success, -1 in case of error
 */
int
virDomainCreate(virDomainPtr domain) {
1694 1695 1696 1697 1698 1699
    int i, ret = -1;
    virConnectPtr conn;
    if (domain == NULL) {
        TODO
	return (-1);
    }
1700 1701 1702 1703
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1704 1705
    conn = domain->conn;
    if (conn->flags & VIR_CONNECT_RO) {
1706 1707 1708
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainCreate != NULL)) {
	    ret = conn->drivers[i]->domainCreate(domain);
	    if (ret == 0)
	        return(ret);
	}
    }
    return(ret);
1719 1720
}

1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
/**
 * virDomainSetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @nvcpus: the new number of virtual CPUs for this domain
 *
 * Dynamically change the number of virtual CPUs used by the domain.
 * Note that this call may fail if the underlying virtualization hypervisor
 * does not support it or if growing the number is arbitrary limited.
 * This function requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */

int
virDomainSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
1737 1738 1739
    int i;
    virConnectPtr conn;

1740 1741 1742 1743 1744 1745 1746 1747
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1748 1749 1750 1751
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1752

1753 1754 1755 1756
    if (nvcpus < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1757
    conn = domain->conn;
1758

1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
    /*
     * Go though the driver registered entry points but use the 
     * XEN_HYPERVISOR directly only as a last mechanism
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no != VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainSetVcpus != NULL)) {
	    if (conn->drivers[i]->domainSetVcpus(domain, nvcpus) == 0)
	        return(0);
	}
    }
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->no == VIR_DRV_XEN_HYPERVISOR) &&
	    (conn->drivers[i]->domainSetVcpus != NULL)) {
	    if (conn->drivers[i]->domainSetVcpus(domain, nvcpus) == 0)
	        return(0);
	}
    }

    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
}

/**
 * virDomainPinVcpu:
 * @domain: pointer to domain object, or NULL for Domain0
 * @vcpu: virtual CPU number
 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes) (IN)
 * 	Each bit set to 1 means that corresponding CPU is usable.
 * 	Bytes are stored in little-endian order: CPU0-7, 8-15...
 * 	In each byte, lowest CPU number is least significant bit.
 * @maplen: number of bytes in cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 *	If maplen < size, missing bytes are set to zero.
 *	If maplen > size, failure code is returned.
 * 
 * Dynamically change the real CPUs which can be allocated to a virtual CPU.
 * This function requires priviledged access to the hypervisor.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainPinVcpu(virDomainPtr domain, unsigned int vcpu,
                 unsigned char *cpumap, int maplen)
{
1806 1807 1808
    int i;
    virConnectPtr conn;

1809 1810 1811 1812 1813 1814 1815 1816
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
1817 1818 1819 1820
    if (domain->conn->flags & VIR_CONNECT_RO) {
        virLibDomainError(domain, VIR_ERR_OPERATION_DENIED, __FUNCTION__);
	return (-1);
    }
1821

1822
    if ((vcpu > 32000) || (cpumap == NULL) || (maplen < 1)) {
1823 1824 1825
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
    conn = domain->conn;

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainPinVcpu != NULL)) {
	    if (conn->drivers[i]->domainPinVcpu(domain, vcpu,
	                                        cpumap, maplen) == 0)
	        return(0);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
}

/**
 * virDomainGetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @info: pointer to an array of virVcpuInfo structures (OUT)
 * @maxinfo: number of structures in info array
 * @cpumaps: pointer to an bit map of real CPUs for all vcpus of this
 *      domain (in 8-bit bytes) (OUT)
 *	If cpumaps is NULL, then no cupmap information is returned by the API.
 *	It's assumed there is <maxinfo> cpumap in cpumaps array.
 *	The memory allocated to cpumaps must be (maxinfo * maplen) bytes
 *	(ie: calloc(maxinfo, maplen)).
 *	One cpumap inside cpumaps has the format described in
 *      virDomainPinVcpu() API.
 * @maplen: number of bytes in one cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 * 
 * Extract information about virtual CPUs of domain, store it in info array
 * and also in cpumaps if this pointer is'nt NULL.
 *
 * Returns the number of info filled in case of success, -1 in case of failure.
 */
int
virDomainGetVcpus(virDomainPtr domain, virVcpuInfoPtr info, int maxinfo,
		  unsigned char *cpumaps, int maplen)
{
    int ret;
1869 1870
    int i;
    virConnectPtr conn;
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887

    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    if ((info == NULL) || (maxinfo < 1)) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    if (cpumaps != NULL && maplen < 1) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    conn = domain->conn;

    /*
     * Go though the driver registered entry points
     */
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) &&
	    (conn->drivers[i]->domainGetVcpus != NULL)) {
	    ret = conn->drivers[i]->domainGetVcpus(domain, info, maxinfo,
	                                           cpumaps, maplen);
	    if (ret >= 0)
	        return(ret);
	}
    }
    virLibConnError(conn, VIR_ERR_CALL_FAILED, __FUNCTION__);
    return (-1);
1904
}