libvirt.c 33.8 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

21
#include <xs.h>
22

D
Daniel Veillard 已提交
23
#include "internal.h"
24
#include "driver.h"
25
#include "xen_internal.h"
26
#include "xend_internal.h"
27
#include "xs_internal.h"
28
#include "hash.h"
29
#include "xml.h"
30

D
Daniel Veillard 已提交
31 32 33 34 35

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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);
59
    initialized = 1;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    /*
     * should not be needed but...
     */
    for (i = 0;i < MAX_DRIVERS;i++) 
         virDriverTab[i] = NULL;

    /*
     * Note that the order is important the first ones have a higher priority
     */
    xenHypervisorRegister();
    xenDaemonRegister();
    xenStoreRegister();
    return(0);
}



D
Daniel Veillard 已提交
78 79 80 81 82 83 84 85 86
/**
 * virLibConnError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the connection level
 */
static void
87 88
virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
{
D
Daniel Veillard 已提交
89
    const char *errmsg;
90

D
Daniel Veillard 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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
108 109 110
virLibDomainError(virDomainPtr domain, virErrorNumber error,
                  const char *info)
{
D
Daniel Veillard 已提交
111 112
    virConnectPtr conn = NULL;
    const char *errmsg;
113

D
Daniel Veillard 已提交
114 115 116 117 118 119 120 121 122 123 124
    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);
}

125 126 127 128 129 130 131 132 133 134 135 136 137
/**
 * 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;

138 139 140
    if (!initialized)
        virInitialize();

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    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);
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/**
 * 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
175 176 177
virGetVersion(unsigned long *libVer, const char *type,
              unsigned long *typeVer)
{
178 179 180
    if (!initialized)
        virInitialize();

181
    if (libVer == NULL)
182
        return (-1);
183 184 185
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
186 187 188
        if ((type == NULL) || (!strcasecmp(type, "Xen"))) {
            if ((DOM0_INTERFACE_VERSION & 0xFFFF0000) == (0xAAAA0000)) {
                /* one time glitch hopefully ! */
189
                *typeVer = 2 * 1000000 +
190 191 192 193 194 195 196 197 198 199 200 201 202 203
                    ((DOM0_INTERFACE_VERSION >> 8) & 0xFF) * 1000 +
                    (DOM0_INTERFACE_VERSION & 0xFF);
            } else {
                *typeVer = (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
                    ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
                    (DOM0_INTERFACE_VERSION & 0xFFFF);
            }
        } else {
            *typeVer = 0;
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, "type");
            return (-1);
        }
    }
    return (0);
204 205
}

D
Daniel Veillard 已提交
206
/**
207
 * virConnectOpen:
D
Daniel Veillard 已提交
208 209 210
 * @name: optional argument currently unused, pass NULL
 *
 * This function should be called first to get a connection to the 
211
 * Hypervisor and xen store
D
Daniel Veillard 已提交
212 213 214
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
215
virConnectPtr
216 217
virConnectOpen(const char *name)
{
218
    int i, res;
219
    virConnectPtr ret = NULL;
220

221 222 223
    if (!initialized)
        virInitialize();

224 225 226 227 228 229 230
    ret = (virConnectPtr) malloc(sizeof(virConnect));
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
        goto failed;
    }
    memset(ret, 0, sizeof(virConnect));
    ret->magic = VIR_CONNECT_MAGIC;
231
    ret->nb_drivers = 0;
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
	    res = virDriverTab[i]->open(ret, name, 0);
	    /*
	     * For a default connect to Xen make sure we manage to contact
	     * all related drivers.
	     */
	    if ((res < 0) && (name == NULL) &&
	        (!strcmp(virDriverTab[i]->name, "Xen")))
		goto failed;
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
	}
    }

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

254
    ret->domains = virHashCreate(20);
255 256
    if (ret->domains == NULL)
        goto failed;
257
    ret->flags = 0;
258

259
    return (ret);
260 261 262

failed:
    if (ret != NULL) {
263 264 265 266
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
267
        free(ret);
268
    }
269
    return (NULL);
270 271 272
}

/**
273
 * virConnectOpenReadOnly:
274 275
 * @name: optional argument currently unused, pass NULL
 *
276 277 278
 * 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.
279 280 281
 *
 * Returns a pointer to the hypervisor connection or NULL in case of error
 */
282
virConnectPtr
283 284
virConnectOpenReadOnly(const char *name)
{
285
    int i, res;
286
    virConnectPtr ret = NULL;
287

288 289 290
    if (!initialized)
        virInitialize();

291
    ret = (virConnectPtr) malloc(sizeof(virConnect));
D
Daniel Veillard 已提交
292 293
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
294
        goto failed;
D
Daniel Veillard 已提交
295
    }
296
    memset(ret, 0, sizeof(virConnect));
297
    ret->magic = VIR_CONNECT_MAGIC;
298
    ret->nb_drivers = 0;
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    for (i = 0;i < MAX_DRIVERS;i++) {
        if ((virDriverTab[i] != NULL) && (virDriverTab[i]->open != NULL)) {
	    res = virDriverTab[i]->open(ret, name,
	                                VIR_DRV_OPEN_QUIET | VIR_DRV_OPEN_RO);
	    if (res == 0)
	        ret->drivers[ret->nb_drivers++] = virDriverTab[i];
	}
    }
    if (ret->nb_drivers == 0) {
	if (name == NULL)
	    virLibConnError(NULL, VIR_ERR_NO_CONNECT,
			    "could not connect to Xen Daemon nor Xen Store");
	else
	    /* we failed to find an adequate driver */
	    virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
	goto failed;
D
Daniel Veillard 已提交
316
    }
317

318 319 320 321 322
    ret->domains = virHashCreate(20);
    if (ret->domains == NULL)
        goto failed;
    ret->flags = VIR_CONNECT_RO;

323
    return (ret);
324 325

failed:
326
    if (ret != NULL) {
327 328 329 330
	for (i = 0;i < ret->nb_drivers;i++) {
	    if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
	        ret->drivers[i]->close(ret);
	}
331
        free(ret);
332
    }
333
    return (NULL);
D
Daniel Veillard 已提交
334 335
}

336
/**
337
 * virDomainFreeName:
338 339 340 341 342 343 344
 * @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
345 346 347
virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED)
{
    return (virDomainFree(domain));
348 349
}

D
Daniel Veillard 已提交
350
/**
351
 * virConnectClose:
D
Daniel Veillard 已提交
352 353 354 355 356 357 358 359 360 361
 * @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
362 363
virConnectClose(virConnectPtr conn)
{
364 365
    int i;

K
Karel Zak 已提交
366
    if (!VIR_IS_CONNECT(conn))
367
        return (-1);
368
    virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
369
    conn->domains = NULL;
370 371 372 373
    for (i = 0;i < conn->nb_drivers;i++) {
	if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL))
	    conn->drivers[i]->close(conn);
    }
374
    conn->magic = -1;
D
Daniel Veillard 已提交
375
    free(conn);
376
    return (0);
D
Daniel Veillard 已提交
377 378
}

379 380 381 382 383 384 385 386 387
/**
 * 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 *
388 389
virConnectGetType(virConnectPtr conn)
{
D
Daniel Veillard 已提交
390
    if (!VIR_IS_CONNECT(conn)) {
391
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
392
        return (NULL);
D
Daniel Veillard 已提交
393
    }
394
    return ("Xen");
395 396
}

D
Daniel Veillard 已提交
397
/**
398
 * virConnectGetVersion:
D
Daniel Veillard 已提交
399
 * @conn: pointer to the hypervisor connection
400
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
401
 *
402 403 404
 * 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 已提交
405
 *
406 407 408
 * 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 已提交
409
 */
410
int
411 412
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
413
    unsigned long ver;
414

D
Daniel Veillard 已提交
415 416
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
417
        return (-1);
D
Daniel Veillard 已提交
418
    }
419

D
Daniel Veillard 已提交
420 421
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
422
        return (-1);
D
Daniel Veillard 已提交
423
    }
424

425
    /* this can't be extracted from the Xenstore */
426 427
    if (conn->handle < 0) {
        *hvVer = 0;
428
        return (0);
429
    }
430

431
    ver = xenHypervisorGetVersion(conn, hvVer);
432
    return (0);
433 434 435
}

/**
436
 * virConnectListDomains:
437 438 439 440 441 442 443 444 445
 * @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
446 447
virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
{
448
    int ret = -1;
449
    unsigned int i;
450
    long id;
451
    char **idlist = NULL;
452

D
Daniel Veillard 已提交
453 454
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
455
        return (-1);
D
Daniel Veillard 已提交
456
    }
457

D
Daniel Veillard 已提交
458 459
    if ((ids == NULL) || (maxids <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
460
        return (-1);
D
Daniel Veillard 已提交
461
    }
462

463 464 465
    /*
     * try first though the Xen Daemon
     */
466
    idlist = xenDaemonListDomains(conn);
467
    if (idlist != NULL) {
468
        for (ret = 0, i = 0; (idlist[i] != NULL) && (ret < maxids); i++) {
469
            id = xenDaemonDomainLookupByName_ids(conn, idlist[i], NULL);
470 471 472
            if (id >= 0)
                ids[ret++] = (int) id;
        }
473 474
	free(idlist);
        return(ret);
475
    }
476

477 478 479 480
    /*
     * Then fallback to the XenStore
     */
    ret = xenStoreListDomains(conn, ids, maxids);
481
    return (ret);
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 496 497
    int ret = -1;
    char **idlist = NULL;

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
    /* TODO: there must be a way to do that with an hypervisor call too ! */
504 505 506
    /* 
     * try first with Xend interface
     */
507
    idlist = xenDaemonListDomains(conn);
508 509 510 511 512
    if (idlist != NULL) {
        char **tmp = idlist;

        ret = 0;
        while (*tmp != NULL) {
513 514 515
            tmp++;
            ret++;
        }
516 517
	free(idlist);
	return(ret);
K
 
Karel Zak 已提交
518
    }
519 520
    /* Then Xen Store */
    return(xenStoreNumOfDomains(conn));
K
 
Karel Zak 已提交
521 522
}

D
Daniel Veillard 已提交
523
/**
524
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
525
 * @conn: pointer to the hypervisor connection
526
 * @xmlDesc: an XML description of the domain
527
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
528
 *
529 530 531
 * 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 已提交
532 533 534
 * 
 * Returns a new domain object or NULL in case of failure
 */
535
virDomainPtr
536
virDomainCreateLinux(virConnectPtr conn,
537
                     const char *xmlDesc,
538 539
                     unsigned int flags ATTRIBUTE_UNUSED)
{
540 541 542 543
    int ret;
    char *sexpr;
    char *name = NULL;
    virDomainPtr dom;
K
Karel Zak 已提交
544

D
Daniel Veillard 已提交
545 546
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
547
        return (NULL);
D
Daniel Veillard 已提交
548 549 550
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
551
        return (NULL);
D
Daniel Veillard 已提交
552 553
    }

554 555 556
    sexpr = virDomainParseXMLDesc(xmlDesc, &name);
    if ((sexpr == NULL) || (name == NULL)) {
        if (sexpr != NULL)
557
            free(sexpr);
558
        if (name != NULL)
559
            free(name);
560

561
        return (NULL);
562 563
    }

564
    ret = xenDaemonDomainCreateLinux(conn, sexpr);
565 566 567
    free(sexpr);
    if (ret != 0) {
        fprintf(stderr, "Failed to create domain %s\n", name);
568
        goto error;
569 570 571 572 573
    }

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

577 578 579 580 581 582
    dom = virDomainLookupByName(conn, name);
    if (dom == NULL) {
        goto error;
    }

    ret = xenDaemonDomainResume(dom);
583 584
    if (ret != 0) {
        fprintf(stderr, "Failed to resume new domain %s\n", name);
585
        xenDaemonDomainDestroy(dom);
586
        goto error;
587 588 589 590 591
    }

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

592 593
    return (dom);
  error:
594 595
    if (name != NULL)
        free(name);
596
    return (NULL);
D
Daniel Veillard 已提交
597 598
}

599

600
/**
601
 * virDomainLookupByID:
602 603 604 605 606 607 608
 * @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
 */
609
virDomainPtr
610 611
virDomainLookupByID(virConnectPtr conn, int id)
{
612
    char *path = NULL;
613
    virDomainPtr ret;
614
    char *name = NULL;
615
    unsigned char uuid[16];
616

D
Daniel Veillard 已提交
617 618
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
619
        return (NULL);
D
Daniel Veillard 已提交
620 621 622
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
623
        return (NULL);
D
Daniel Veillard 已提交
624
    }
625

626 627
    /* lookup is easier with the Xen store so try it first */
    if (conn->xshandle != NULL) {
628
        path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
629 630
    }
    /* fallback to xend API then */
631
    if (path == NULL) {
632
        char **names = xenDaemonListDomains(conn);
633 634 635 636 637
        char **tmp = names;
        int ident;

        if (names != NULL) {
            while (*tmp != NULL) {
638
                ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]);
639 640 641 642 643 644 645 646
                if (ident == id) {
                    name = strdup(*tmp);
                    break;
                }
                tmp++;
            }
            free(names);
        }
647
    }
648

649
    ret = (virDomainPtr) malloc(sizeof(virDomain));
650
    if (ret == NULL) {
651
        goto error;
652
    }
653
    memset(ret, 0, sizeof(virDomain));
654
    ret->magic = VIR_DOMAIN_MAGIC;
655 656
    ret->conn = conn;
    ret->handle = id;
657
    ret->path = path;
658 659
    ret->name = name;
    memcpy(&ret->uuid[0], uuid, 16);
660
    if (ret->name == NULL) {
661 662 663
        goto error;
    }

664 665
    return (ret);
  error:
666
    if (ret != NULL)
667
        free(path);
668
    if (path != NULL)
669
        free(path);
670
    if (path != NULL)
671 672
        free(path);
    return (NULL);
673 674 675 676 677 678 679 680 681 682 683 684
}

/**
 * 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
685 686
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
687 688 689 690 691
    virDomainPtr ret;
    char *name = NULL;
    char **names;
    char **tmp;
    unsigned char ident[16];
692
    int id = -1;
693

D
Daniel Veillard 已提交
694 695
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
696
        return (NULL);
D
Daniel Veillard 已提交
697 698 699
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
700
        return (NULL);
D
Daniel Veillard 已提交
701
    }
702
    names = xenDaemonListDomains(conn);
703 704 705
    tmp = names;

    if (names == NULL) {
706 707
        TODO                    /* try to fallback to xenstore lookup */
            return (NULL);
708
    }
709
    while (*tmp != NULL) {
710
        id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]);
711 712 713 714 715 716 717
        if (id >= 0) {
            if (!memcmp(uuid, ident, 16)) {
                name = strdup(*tmp);
                break;
            }
        }
        tmp++;
718 719 720 721
    }
    free(names);

    if (name == NULL)
722
        return (NULL);
723 724 725 726

    ret = (virDomainPtr) malloc(sizeof(virDomain));
    if (ret == NULL) {
        if (name != NULL)
727 728
            free(name);
        return (NULL);
729
    }
730
    memset(ret, 0, sizeof(virDomain));
731 732 733 734
    ret->magic = VIR_DOMAIN_MAGIC;
    ret->conn = conn;
    ret->handle = id;
    ret->name = name;
735
    ret->path = 0;
736
    memcpy(ret->uuid, uuid, 16);
737

738
    return (ret);
739 740
}

741 742 743 744 745
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
746
 * Try to lookup a domain on the given hypervisor based on its name.
747 748 749 750
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
751 752
virDomainLookupByName(virConnectPtr conn, const char *name)
{
753 754
    virDomainPtr ret = NULL;

D
Daniel Veillard 已提交
755 756
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
757
        return (NULL);
D
Daniel Veillard 已提交
758 759 760
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
761
        return (NULL);
D
Daniel Veillard 已提交
762
    }
763

764
    /* try first though Xend */
765 766 767
    ret = xenDaemonDomainLookupByName(conn, name);
    if (ret != NULL) {
        return(ret);
768 769 770
    }

    /* then though the XenStore */
771 772 773
    ret = xenStoreDomainLookupByName(conn, name);
    if (ret != NULL) {
        return(ret);
774 775 776
    }

    return (ret);
777 778
}

D
Daniel Veillard 已提交
779
/**
780
 * virDomainDestroy:
D
Daniel Veillard 已提交
781 782 783 784
 * @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.
785 786
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
787
 * This function may requires priviledged access
D
Daniel Veillard 已提交
788 789
 *
 * Returns 0 in case of success and -1 in case of failure.
790
 */
D
Daniel Veillard 已提交
791
int
792 793
virDomainDestroy(virDomainPtr domain)
{
794 795
    int ret;

D
Daniel Veillard 已提交
796 797
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
798
        return (-1);
D
Daniel Veillard 已提交
799
    }
800 801 802 803

    /*
     * try first with the xend method
     */
804
    ret = xenDaemonDomainDestroy(domain);
805 806
    if (ret == 0) {
        virDomainFree(domain);
807
        return (0);
808 809
    }

810
    ret = xenHypervisorDestroyDomain(domain);
811
    if (ret < 0)
812 813
        return (-1);

814
    virDomainFree(domain);
815
    return (0);
816 817 818 819 820 821 822 823 824 825 826 827
}

/**
 * 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
828 829
virDomainFree(virDomainPtr domain)
{
D
Daniel Veillard 已提交
830 831
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
832
        return (-1);
D
Daniel Veillard 已提交
833
    }
834 835 836 837 838 839 840
    domain->magic = -1;
    domain->handle = -1;
    if (domain->path != NULL)
        free(domain->path);
    if (domain->name)
        free(domain->name);
    free(domain);
841
    return (0);
D
Daniel Veillard 已提交
842 843 844
}

/**
845
 * virDomainSuspend:
D
Daniel Veillard 已提交
846 847 848 849
 * @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 
850
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
851
 * the domain.
852
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
853 854 855 856
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
857 858
virDomainSuspend(virDomainPtr domain)
{
859 860
    int ret;

D
Daniel Veillard 已提交
861 862
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
863
        return (-1);
D
Daniel Veillard 已提交
864 865
    }

866
    /* first try though the Xen daemon */
867
    ret = xenDaemonDomainSuspend(domain);
868
    if (ret == 0)
869
        return (0);
870 871

    /* then try a direct hypervisor access */
872
    return (xenHypervisorPauseDomain(domain));
D
Daniel Veillard 已提交
873 874 875
}

/**
876
 * virDomainResume:
D
Daniel Veillard 已提交
877 878 879
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
880
 * it was frozen by calling virSuspendDomain().
881
 * This function may requires priviledged access
D
Daniel Veillard 已提交
882 883 884 885
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
886 887
virDomainResume(virDomainPtr domain)
{
888 889
    int ret;

D
Daniel Veillard 已提交
890 891
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
892
        return (-1);
D
Daniel Veillard 已提交
893 894
    }

895
    /* first try though the Xen daemon */
896
    ret = xenDaemonDomainResume(domain);
897
    if (ret == 0)
898
        return (0);
899 900

    /* then try a direct hypervisor access */
901
    return (xenHypervisorResumeDomain(domain));
D
Daniel Veillard 已提交
902 903
}

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

D
Daniel Veillard 已提交
922 923
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
924
        return (-1);
D
Daniel Veillard 已提交
925 926 927
    }
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
928
        return (-1);
D
Daniel Veillard 已提交
929
    }
930

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

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

    }

951
    ret = xenDaemonDomainSave(domain, to);
952
    return (ret);
953 954 955 956
}

/**
 * virDomainRestore:
957
 * @conn: pointer to the hypervisor connection
958 959 960 961 962 963 964
 * @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
965 966
virDomainRestore(virConnectPtr conn, const char *from)
{
967
    int ret;
968
    char filepath[4096];
969

D
Daniel Veillard 已提交
970 971
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
972
        return (-1);
D
Daniel Veillard 已提交
973 974 975
    }
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
976
        return (-1);
D
Daniel Veillard 已提交
977 978
    }

979 980 981 982 983
    /*
     * 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] != '/') {
984 985 986 987 988 989 990 991 992 993 994 995 996 997
        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];
    }

998
    ret = xenDaemonDomainRestore(conn, from);
999
    return (ret);
1000 1001
}

1002 1003 1004 1005 1006
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1007 1008
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1009 1010 1011 1012 1013 1014 1015
 *
 * 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
1016 1017
virDomainShutdown(virDomainPtr domain)
{
1018 1019
    int ret;

D
Daniel Veillard 已提交
1020 1021
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1022
        return (-1);
D
Daniel Veillard 已提交
1023
    }
1024

1025 1026 1027
    /*
     * try first with the xend daemon
     */
1028
    ret = xenDaemonDomainShutdown(domain);
1029 1030
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
1031
        return (0);
1032
    }
1033

1034
    /*
1035 1036 1037
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
1038
    ret = xenDaemonDomainShutdown(domain);
1039 1040 1041
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
    }
1042
    return (ret);
1043 1044
}

1045
/**
1046
 * virDomainGetName:
1047 1048 1049 1050 1051 1052 1053 1054
 * @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 *
1055 1056
virDomainGetName(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1057 1058
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1059
        return (NULL);
D
Daniel Veillard 已提交
1060
    }
1061
    return (domain->name);
1062 1063
}

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
/**
 * 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
1074 1075
virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
{
D
Daniel Veillard 已提交
1076 1077
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1078
        return (-1);
D
Daniel Veillard 已提交
1079 1080 1081
    }
    if (uuid == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1082
        return (-1);
D
Daniel Veillard 已提交
1083 1084 1085
    }

    if (domain->handle == 0) {
1086
        memset(uuid, 0, 16);
D
Daniel Veillard 已提交
1087
    } else {
1088 1089 1090 1091 1092 1093 1094 1095
        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))
1096
            xenDaemonDomainLookupByName_ids(domain->conn, domain->name,
1097 1098 1099 1100
                                &domain->uuid[0]);
        memcpy(uuid, &domain->uuid[0], 16);
    }
    return (0);
1101 1102
}

1103
/**
1104
 * virDomainGetID:
1105 1106 1107 1108 1109 1110 1111
 * @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
1112 1113
virDomainGetID(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1114 1115
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1116
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1117
    }
1118
    return (domain->handle);
1119 1120
}

1121 1122 1123 1124 1125 1126
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1127 1128
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1129 1130
 */
char *
1131 1132
virDomainGetOSType(virDomainPtr domain)
{
1133
    char *vm, *str = NULL;
1134

D
Daniel Veillard 已提交
1135 1136
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1137
        return (NULL);
D
Daniel Veillard 已提交
1138
    }
1139

1140 1141
    vm = virDomainGetVM(domain);
    if (vm) {
1142 1143
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
        free(vm);
1144
    }
1145 1146 1147
    if (str == NULL)
        str = strdup("linux");

1148
    return (str);
1149 1150
}

1151
/**
1152
 * virDomainGetMaxMemory:
1153 1154 1155 1156 1157 1158 1159 1160 1161
 * @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
1162 1163
virDomainGetMaxMemory(virDomainPtr domain)
{
1164 1165
    unsigned long ret = 0;

D
Daniel Veillard 已提交
1166 1167
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1168
        return (0);
D
Daniel Veillard 已提交
1169
    }
1170

1171 1172 1173 1174
    /*
     * try first with the hypervisor if available
     */
    if (!(domain->conn->flags & VIR_CONNECT_RO)) {
1175
        virDomainInfo dominfo;
1176
        int tmp;
1177

1178
        tmp = xenHypervisorGetDomainInfo(domain, &dominfo);
1179
        if (tmp >= 0)
1180
	    return(dominfo.maxMem);
1181
    }
1182 1183 1184 1185
    ret = xenStoreDomainGetMaxMemory(domain);
    if (ret > 0)
        return(ret);
    ret = xenDaemonDomainGetMaxMemory(domain);
1186
    return (ret);
1187 1188
}

D
Daniel Veillard 已提交
1189
/**
1190
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1191 1192 1193 1194 1195 1196
 * @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.
1197
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1198 1199 1200 1201
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1202 1203
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1204 1205
    int ret;
    char s[256], v[30];
1206

D
Daniel Veillard 已提交
1207 1208
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1209
        return (-1);
D
Daniel Veillard 已提交
1210
    }
1211 1212 1213 1214 1215 1216
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1217
        return (-1);
1218 1219
    }
    if (domain->conn->flags & VIR_CONNECT_RO)
1220
        return (-1);
1221 1222 1223 1224 1225

    ret = xenDaemonDomainSetMaxMemory(domain, memory);
    if (ret == 0)
        return(0);

1226
    ret = xenHypervisorSetMaxMemory(domain, memory);
1227
    if (ret < 0)
1228
        return (-1);
1229

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    if (domain->conn->xshandle != NULL) {
	/*
	 * try to update at the Xenstore level too
	 * Failing to do so should not be considered fatal though as long
	 * as the hypervisor call succeeded
	 */
	snprintf(s, 255, "/local/domain/%d/memory/target", domain->handle);
	s[255] = 0;
	snprintf(v, 29, "%lu", memory);
	v[30] = 0;
1240

1241 1242 1243
	if (!xs_write(domain->conn->xshandle, 0, &s[0], &v[0], strlen(v)))
	    ret = -1;
    }
1244

1245
    return (ret);
D
Daniel Veillard 已提交
1246 1247
}

1248 1249
/**
 * virDomainGetInfo:
1250
 * @domain: a domain object
1251 1252 1253 1254 1255 1256 1257 1258 1259
 * @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
1260 1261
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1262 1263
    int ret;

D
Daniel Veillard 已提交
1264 1265
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1266
        return (-1);
D
Daniel Veillard 已提交
1267 1268 1269
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1270
        return (-1);
D
Daniel Veillard 已提交
1271
    }
1272

1273
    memset(info, 0, sizeof(virDomainInfo));
1274

1275 1276 1277 1278
    /*
     * if we have direct access though the hypervisor do a direct call
     */
    if (domain->conn->handle >= 0) {
1279
        ret = xenHypervisorGetDomainInfo(domain, info);
1280 1281
        if (ret == 0)
	    return (0);
1282 1283 1284 1285 1286
    }

    /*
     * try to extract the informations though access to the Xen Daemon
     */
1287
    if (xenDaemonDomainGetInfo(domain, info) == 0)
1288
        return (0);
1289 1290

    /*
1291
     * last fallback, try to get the informations from the Xen store
1292
     */
1293 1294
    if (xenStoreGetDomainInfo(domain, info) == 0)
        return (0);
1295

1296
    return (-1);
1297
}
1298

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
/**
 * 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 *
1311 1312
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
D
Daniel Veillard 已提交
1313 1314
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1315
        return (NULL);
D
Daniel Veillard 已提交
1316 1317 1318
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1319
        return (NULL);
D
Daniel Veillard 已提交
1320
    }
1321

1322
    return (xenDaemonDomainDumpXML(domain));
1323
}
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

/**
 * 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);
}