libvirt.c 43.3 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 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 "xml.h"
32
#include "test.h"
33

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

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

    /*
     * 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();
74
    testRegister();
75 76 77 78 79
    return(0);
}



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

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

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

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

140 141 142
    if (!initialized)
        virInitialize();

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

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

182 183 184
    if (!initialized)
        virInitialize();

185
    if (libVer == NULL)
186
        return (-1);
187 188 189
    *libVer = LIBVIR_VERSION_NUMBER;

    if (typeVer != NULL) {
190 191 192 193 194 195 196 197 198 199
        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) {
200
            *typeVer = 0;
201
            virLibConnError(NULL, VIR_ERR_NO_SUPPORT, type);
202 203 204 205
            return (-1);
        }
    }
    return (0);
206 207
}

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

223 224 225
    if (!initialized)
        virInitialize();

226 227 228
    if (name == NULL) {
        name = "Xen";
	for_xen = 1;
229
    } else if (!strncasecmp(name, "xen", 3)) {
230 231 232
	for_xen = 1;
    }

233
    ret = virGetConnect();
234 235 236 237 238
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
        goto failed;
    }

239 240 241 242 243 244 245
    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.
	     */
246 247
	    if ((res < 0) && (for_xen) &&
	        (!strncasecmp(virDriverTab[i]->name, "xen", 3)))
248 249 250 251 252 253 254 255 256 257 258
		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;
    }
259

260
    return (ret);
261 262 263

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

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

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

292 293 294
    if (name == NULL)
        name = "Xen";

295
    ret = virGetConnect();
D
Daniel Veillard 已提交
296 297
    if (ret == NULL) {
        virLibConnError(NULL, VIR_ERR_NO_MEMORY, "Allocating connection");
298
        goto failed;
D
Daniel Veillard 已提交
299
    }
300

301 302 303 304 305 306
    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];
307

308 309 310 311 312 313 314 315 316 317
	}
    }
    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 已提交
318
    }
319 320
    ret->flags = VIR_CONNECT_RO;

321
    return (ret);
322 323

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

/**
335
 * virConnectClose:
D
Daniel Veillard 已提交
336 337 338 339 340 341 342 343 344 345
 * @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
346 347
virConnectClose(virConnectPtr conn)
{
K
Karel Zak 已提交
348
    if (!VIR_IS_CONNECT(conn))
349
        return (-1);
350 351
    if (virFreeConnect(conn) < 0)
        return (-1);
352
    return (0);
D
Daniel Veillard 已提交
353 354
}

355 356 357 358 359 360 361 362 363
/**
 * 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 *
364 365
virConnectGetType(virConnectPtr conn)
{
366
    int i;
367
    const char *ret;
368

D
Daniel Veillard 已提交
369
    if (!VIR_IS_CONNECT(conn)) {
370
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
371
        return (NULL);
D
Daniel Veillard 已提交
372
    }
373 374 375 376 377 378 379 380
    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);
	}
    }
381 382 383 384 385 386 387
    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);
388 389
}

D
Daniel Veillard 已提交
390
/**
391
 * virConnectGetVersion:
D
Daniel Veillard 已提交
392
 * @conn: pointer to the hypervisor connection
393
 * @hvVer: return value for the version of the running hypervisor (OUT)
D
Daniel Veillard 已提交
394
 *
395 396 397
 * 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 已提交
398
 *
399 400 401
 * 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 已提交
402
 */
403
int
404 405
virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
406
    int ret, i;
407

D
Daniel Veillard 已提交
408 409
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
410
        return (-1);
D
Daniel Veillard 已提交
411
    }
412

D
Daniel Veillard 已提交
413 414
    if (hvVer == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
415
        return (-1);
D
Daniel Veillard 已提交
416
    }
417

418
    *hvVer = 0;
419

420 421 422 423 424 425 426 427 428
    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);
429 430 431
}

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

D
Daniel Veillard 已提交
449 450
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
451
        return (-1);
D
Daniel Veillard 已提交
452
    }
453

D
Daniel Veillard 已提交
454 455
    if ((ids == NULL) || (maxids <= 0)) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
456
        return (-1);
D
Daniel Veillard 已提交
457
    }
458

459 460 461 462 463 464 465 466 467 468
    /* 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);
	}
    }

469
    /*
470
     * try then though the Xen Daemon
471
     */
472
    idlist = xenDaemonListDomains(conn);
473
    if (idlist != NULL) {
474
        for (ret = 0, i = 0; (idlist[i] != NULL) && (ret < maxids); i++) {
475
            id = xenDaemonDomainLookupByName_ids(conn, idlist[i], NULL);
476 477 478
            if (id >= 0)
                ids[ret++] = (int) id;
        }
479 480
	free(idlist);
        return(ret);
481
    }
482

483 484 485 486
    /*
     * Then fallback to the XenStore
     */
    ret = xenStoreListDomains(conn, ids, maxids);
487
    return (ret);
D
Daniel Veillard 已提交
488 489
}

K
 
Karel Zak 已提交
490 491 492 493
/**
 * virConnectNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
494 495
 * Provides the number of active domains.
 *
K
 
Karel Zak 已提交
496 497 498
 * Returns the number of domain found or -1 in case of error
 */
int
499 500
virConnectNumOfDomains(virConnectPtr conn)
{
K
 
Karel Zak 已提交
501
    int ret = -1;
502
    int i;
K
 
Karel Zak 已提交
503 504
    char **idlist = NULL;

D
Daniel Veillard 已提交
505 506
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
507
        return (-1);
D
Daniel Veillard 已提交
508
    }
K
Karel Zak 已提交
509

510 511 512 513 514 515 516 517 518 519
    /* 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);
	}
    }

520
    /* 
521
     * try then with Xend interface
522
     */
523
    idlist = xenDaemonListDomains(conn);
524 525 526 527 528
    if (idlist != NULL) {
        char **tmp = idlist;

        ret = 0;
        while (*tmp != NULL) {
529 530 531
            tmp++;
            ret++;
        }
532 533
	free(idlist);
	return(ret);
K
 
Karel Zak 已提交
534
    }
535 536
    /* Then Xen Store */
    return(xenStoreNumOfDomains(conn));
K
 
Karel Zak 已提交
537 538
}

D
Daniel Veillard 已提交
539
/**
540
 * virDomainCreateLinux:
D
Daniel Veillard 已提交
541
 * @conn: pointer to the hypervisor connection
542
 * @xmlDesc: an XML description of the domain
543
 * @flags: an optional set of virDomainFlags
D
Daniel Veillard 已提交
544
 *
545 546 547
 * 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 已提交
548 549 550
 * 
 * Returns a new domain object or NULL in case of failure
 */
551
virDomainPtr
552
virDomainCreateLinux(virConnectPtr conn,
553
                     const char *xmlDesc,
554 555
                     unsigned int flags ATTRIBUTE_UNUSED)
{
556 557 558 559
    int ret;
    char *sexpr;
    char *name = NULL;
    virDomainPtr dom;
K
Karel Zak 已提交
560

D
Daniel Veillard 已提交
561 562
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
563
        return (NULL);
D
Daniel Veillard 已提交
564 565 566
    }
    if (xmlDesc == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
567
        return (NULL);
D
Daniel Veillard 已提交
568 569
    }

570 571 572
    sexpr = virDomainParseXMLDesc(xmlDesc, &name);
    if ((sexpr == NULL) || (name == NULL)) {
        if (sexpr != NULL)
573
            free(sexpr);
574
        if (name != NULL)
575
            free(name);
576

577
        return (NULL);
578 579
    }

580
    ret = xenDaemonDomainCreateLinux(conn, sexpr);
581 582 583
    free(sexpr);
    if (ret != 0) {
        fprintf(stderr, "Failed to create domain %s\n", name);
584
        goto error;
585 586 587 588 589
    }

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

593 594 595 596 597 598
    dom = virDomainLookupByName(conn, name);
    if (dom == NULL) {
        goto error;
    }

    ret = xenDaemonDomainResume(dom);
599 600
    if (ret != 0) {
        fprintf(stderr, "Failed to resume new domain %s\n", name);
601
        xenDaemonDomainDestroy(dom);
602
        goto error;
603 604 605 606 607
    }

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

608 609
    return (dom);
  error:
610 611
    if (name != NULL)
        free(name);
612
    return (NULL);
D
Daniel Veillard 已提交
613 614
}

615

616
/**
617
 * virDomainLookupByID:
618 619 620 621 622 623 624
 * @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
 */
625
virDomainPtr
626 627
virDomainLookupByID(virConnectPtr conn, int id)
{
628
    char *path = NULL;
D
Daniel Veillard 已提交
629 630 631
    char **names;
    char **tmp;
    int ident;
632
    virDomainPtr ret;
633
    char *name = NULL;
634
    unsigned char uuid[16];
635
    int i;
636

D
Daniel Veillard 已提交
637 638
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
639
        return (NULL);
D
Daniel Veillard 已提交
640 641 642
    }
    if (id < 0) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
643
        return (NULL);
D
Daniel Veillard 已提交
644
    }
645

646 647 648 649 650 651 652 653 654 655
    /* 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);
	}
    }

D
Daniel Veillard 已提交
656
    /* retrieve home path of the domain */
657
    if (conn->xshandle != NULL) {
658
        path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
659
    }
D
Daniel Veillard 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

    /* path does not contain name, use xend API to retrieve name */
    names = xenDaemonListDomains(conn);
    tmp = names;

    if (names != NULL) {
       while (*tmp != NULL) {
          ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]);
          if (ident == id) {
             name = strdup(*tmp);
             break;
          }
          tmp++;
       }
       free(names);
675
    }
676 677
    if (name == NULL)
        goto error;
678

679
    ret = virGetDomain(conn, name, uuid);
680
    if (ret == NULL) {
681
        virLibConnError(conn, VIR_ERR_NO_MEMORY, "Allocating domain");
682
        goto error;
683 684
    }
    ret->handle = id;
685
    ret->path = path;
686 687
    if (name != NULL)
        free(name);
688

689
    return (ret);
690 691 692
error:
    if (name != NULL)
        free(name);
693
    if (path != NULL)
694 695
        free(path);
    return (NULL);
696 697 698 699 700
}

/**
 * virDomainLookupByUUID:
 * @conn: pointer to the hypervisor connection
K
Karel Zak 已提交
701
 * @uuid: the raw UUID for the domain
702 703 704 705 706 707
 *
 * 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
708 709
virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
{
710 711 712 713 714
    virDomainPtr ret;
    char *name = NULL;
    char **names;
    char **tmp;
    unsigned char ident[16];
715
    int id = -1;
716
    int i;
717

D
Daniel Veillard 已提交
718 719
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
720
        return (NULL);
D
Daniel Veillard 已提交
721 722 723
    }
    if (uuid == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
724
        return (NULL);
D
Daniel Veillard 已提交
725
    }
726 727 728 729 730 731 732 733 734 735 736

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

737
    names = xenDaemonListDomains(conn);
738 739 740
    tmp = names;

    if (names == NULL) {
741 742
        TODO                    /* try to fallback to xenstore lookup */
            return (NULL);
743
    }
744
    while (*tmp != NULL) {
745
        id = xenDaemonDomainLookupByName_ids(conn, *tmp, &ident[0]);
746 747 748 749 750 751 752
        if (id >= 0) {
            if (!memcmp(uuid, ident, 16)) {
                name = strdup(*tmp);
                break;
            }
        }
        tmp++;
753 754 755 756
    }
    free(names);

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

759
    ret = virGetDomain(conn, name, uuid);
760 761
    if (ret == NULL) {
        if (name != NULL)
762 763
            free(name);
        return (NULL);
764 765
    }
    ret->handle = id;
766

767
    return (ret);
768 769
}

K
Karel Zak 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
/**
 * 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]);
}

819 820 821 822 823
/**
 * virDomainLookupByName:
 * @conn: pointer to the hypervisor connection
 * @name: name for the domain
 *
824
 * Try to lookup a domain on the given hypervisor based on its name.
825 826 827 828
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
829 830
virDomainLookupByName(virConnectPtr conn, const char *name)
{
831
    virDomainPtr ret = NULL;
832
    int i;
833

D
Daniel Veillard 已提交
834 835
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
836
        return (NULL);
D
Daniel Veillard 已提交
837 838 839
    }
    if (name == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
840
        return (NULL);
D
Daniel Veillard 已提交
841
    }
842

843 844 845 846 847 848 849 850 851 852
    /* 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);
	}
    }

853
    /* try first though Xend */
854 855 856
    ret = xenDaemonDomainLookupByName(conn, name);
    if (ret != NULL) {
        return(ret);
857 858 859
    }

    /* then though the XenStore */
860 861 862
    ret = xenStoreDomainLookupByName(conn, name);
    if (ret != NULL) {
        return(ret);
863 864 865
    }

    return (ret);
866 867
}

D
Daniel Veillard 已提交
868
/**
869
 * virDomainDestroy:
D
Daniel Veillard 已提交
870 871 872 873
 * @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.
874 875
 * The data structure is freed and should not be used thereafter if the
 * call does not return an error.
876
 * This function may requires priviledged access
D
Daniel Veillard 已提交
877 878
 *
 * Returns 0 in case of success and -1 in case of failure.
879
 */
D
Daniel Veillard 已提交
880
int
881 882
virDomainDestroy(virDomainPtr domain)
{
883 884
    int ret;

D
Daniel Veillard 已提交
885 886
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
887
        return (-1);
D
Daniel Veillard 已提交
888
    }
889 890 891 892

    /*
     * try first with the xend method
     */
893
    ret = xenDaemonDomainDestroy(domain);
894 895
    if (ret == 0) {
        virDomainFree(domain);
896
        return (0);
897 898
    }

899
    ret = xenHypervisorDestroyDomain(domain);
900
    if (ret < 0)
901 902
        return (-1);

903
    virDomainFree(domain);
904
    return (0);
905 906 907 908 909 910 911 912 913 914 915 916
}

/**
 * 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
917 918
virDomainFree(virDomainPtr domain)
{
D
Daniel Veillard 已提交
919 920
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
921
        return (-1);
D
Daniel Veillard 已提交
922
    }
923 924 925
    if (virFreeDomain(domain->conn, domain) < 0)
        return (-1);
    return(0);
D
Daniel Veillard 已提交
926 927 928
}

/**
929
 * virDomainSuspend:
D
Daniel Veillard 已提交
930 931 932 933
 * @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 
934
 * hypervisor level will stay allocated. Use virDomainResume() to reactivate
D
Daniel Veillard 已提交
935
 * the domain.
936
 * This function may requires priviledged access.
D
Daniel Veillard 已提交
937 938 939 940
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
941 942
virDomainSuspend(virDomainPtr domain)
{
943 944
    int ret;

D
Daniel Veillard 已提交
945 946
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
947
        return (-1);
D
Daniel Veillard 已提交
948 949
    }

950
    /* first try though the Xen daemon */
951
    ret = xenDaemonDomainSuspend(domain);
952
    if (ret == 0)
953
        return (0);
954 955

    /* then try a direct hypervisor access */
956
    return (xenHypervisorPauseDomain(domain));
D
Daniel Veillard 已提交
957 958 959
}

/**
960
 * virDomainResume:
D
Daniel Veillard 已提交
961 962 963
 * @domain: a domain object
 *
 * Resume an suspended domain, the process is restarted from the state where
964
 * it was frozen by calling virSuspendDomain().
965
 * This function may requires priviledged access
D
Daniel Veillard 已提交
966 967 968 969
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
970 971
virDomainResume(virDomainPtr domain)
{
972 973
    int ret;

D
Daniel Veillard 已提交
974 975
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
976
        return (-1);
D
Daniel Veillard 已提交
977 978
    }

979
    /* first try though the Xen daemon */
980
    ret = xenDaemonDomainResume(domain);
981
    if (ret == 0)
982
        return (0);
983 984

    /* then try a direct hypervisor access */
985
    return (xenHypervisorResumeDomain(domain));
D
Daniel Veillard 已提交
986 987
}

988 989 990 991 992 993
/**
 * virDomainSave:
 * @domain: a domain object
 * @to: path for the output file
 *
 * This method will suspend a domain and save its memory contents to
994 995 996
 * 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.
997 998 999 1000
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1001 1002
virDomainSave(virDomainPtr domain, const char *to)
{
1003
    int ret;
1004
    char filepath[4096];
1005

D
Daniel Veillard 已提交
1006 1007
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1008
        return (-1);
D
Daniel Veillard 已提交
1009 1010 1011
    }
    if (to == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1012
        return (-1);
D
Daniel Veillard 已提交
1013
    }
1014

1015 1016 1017 1018 1019
    /*
     * 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] != '/') {
1020
        unsigned int len, t;
1021

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
        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];
1032 1033 1034

    }

1035
    ret = xenDaemonDomainSave(domain, to);
1036
    return (ret);
1037 1038 1039 1040
}

/**
 * virDomainRestore:
1041
 * @conn: pointer to the hypervisor connection
1042 1043 1044 1045 1046 1047 1048
 * @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
1049 1050
virDomainRestore(virConnectPtr conn, const char *from)
{
1051
    int ret;
1052
    char filepath[4096];
1053

D
Daniel Veillard 已提交
1054 1055
    if (!VIR_IS_CONNECT(conn)) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
1056
        return (-1);
D
Daniel Veillard 已提交
1057 1058 1059
    }
    if (from == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
1060
        return (-1);
D
Daniel Veillard 已提交
1061 1062
    }

1063 1064 1065 1066 1067
    /*
     * 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] != '/') {
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
        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];
    }

1082
    ret = xenDaemonDomainRestore(conn, from);
1083
    return (ret);
1084 1085
}

1086 1087 1088 1089 1090
/**
 * virDomainShutdown:
 * @domain: a domain object
 *
 * Shutdown a domain, the domain object is still usable there after but
1091 1092
 * the domain OS is being stopped. Note that the guest OS may ignore the
 * request.
1093 1094 1095 1096 1097 1098 1099
 *
 * 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
1100 1101
virDomainShutdown(virDomainPtr domain)
{
1102 1103
    int ret;

D
Daniel Veillard 已提交
1104 1105
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1106
        return (-1);
D
Daniel Veillard 已提交
1107
    }
1108

1109 1110 1111
    /*
     * try first with the xend daemon
     */
1112
    ret = xenDaemonDomainShutdown(domain);
1113 1114
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
1115
        return (0);
1116
    }
1117

1118
    /*
1119 1120 1121
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
1122
    ret = xenDaemonDomainShutdown(domain);
1123 1124 1125
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
    }
1126
    return (ret);
1127 1128
}

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
/**
 * virDomainReboot:
 * @domain: a domain object
 * @flags: extra flags for the reboot operation, not used yet
 *
 * Reboot a domain, the domain object is still usable there after but
 * the domain OS is being stopped for a restart.
 * Note that the guest OS may ignore the request.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
virDomainReboot(virDomainPtr domain, unsigned int flags)
{
    int ret;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }

    /*
     * try first with the xend daemon
     */
    ret = xenDaemonDomainReboot(domain, flags);
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
        return (0);
    }

    /*
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
    ret = xenDaemonDomainReboot(domain, flags);
    if (ret == 0) {
        domain->flags |= DOMAIN_IS_SHUTDOWN;
    }
    return (ret);
}

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

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

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

K
Karel Zak 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
/**
 * 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];
    
    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);
}

1264
/**
1265
 * virDomainGetID:
1266 1267 1268 1269 1270 1271 1272
 * @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
1273 1274
virDomainGetID(virDomainPtr domain)
{
D
Daniel Veillard 已提交
1275 1276
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1277
        return ((unsigned int) -1);
D
Daniel Veillard 已提交
1278
    }
1279
    return (domain->handle);
1280 1281
}

1282 1283 1284 1285 1286 1287
/**
 * virDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
1288 1289
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
1290 1291
 */
char *
1292 1293
virDomainGetOSType(virDomainPtr domain)
{
1294
    char *vm, *str = NULL;
1295

D
Daniel Veillard 已提交
1296 1297
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1298
        return (NULL);
D
Daniel Veillard 已提交
1299
    }
1300

1301 1302
    vm = virDomainGetVM(domain);
    if (vm) {
1303 1304
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
        free(vm);
1305
    }
1306 1307 1308
    if (str == NULL)
        str = strdup("linux");

1309
    return (str);
1310 1311
}

1312
/**
1313
 * virDomainGetMaxMemory:
1314 1315 1316 1317 1318 1319 1320 1321 1322
 * @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
1323 1324
virDomainGetMaxMemory(virDomainPtr domain)
{
1325 1326
    unsigned long ret = 0;

D
Daniel Veillard 已提交
1327 1328
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1329
        return (0);
D
Daniel Veillard 已提交
1330
    }
1331

1332 1333 1334 1335
    /*
     * try first with the hypervisor if available
     */
    if (!(domain->conn->flags & VIR_CONNECT_RO)) {
1336
        virDomainInfo dominfo;
1337
        int tmp;
1338

1339
        tmp = xenHypervisorGetDomainInfo(domain, &dominfo);
1340
        if (tmp >= 0)
1341
	    return(dominfo.maxMem);
1342
    }
1343 1344 1345 1346
    ret = xenStoreDomainGetMaxMemory(domain);
    if (ret > 0)
        return(ret);
    ret = xenDaemonDomainGetMaxMemory(domain);
1347
    return (ret);
1348 1349
}

D
Daniel Veillard 已提交
1350
/**
1351
 * virDomainSetMaxMemory:
D
Daniel Veillard 已提交
1352 1353 1354 1355 1356 1357
 * @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.
1358
 * This function requires priviledged access to the hypervisor.
D
Daniel Veillard 已提交
1359 1360 1361 1362
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
int
1363 1364
virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
{
1365 1366
    int ret = -1 , i;
    virConnectPtr conn;
1367

D
Daniel Veillard 已提交
1368 1369
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1370
        return (-1);
D
Daniel Veillard 已提交
1371
    }
1372 1373 1374 1375 1376 1377
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1378
        return (-1);
1379
    }
1380
    conn = domain->conn;
1381
    if (domain->conn->flags & VIR_CONNECT_RO)
1382
        return (-1);
1383

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    /*
     * 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__);
1397
        return (-1);
1398 1399 1400
    }
    return (ret);
}
1401

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
/**
 * 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;
1419

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    if (memory < 4096) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return (-1);
    }
    if (domain == NULL) {
        TODO
	return (-1);
    }
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
1431
    }
1432 1433 1434
    conn = domain->conn;
    if (domain->conn->flags & VIR_CONNECT_RO)
        return (-1);
1435

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
    /*
     * 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);
    }
1451
    return (ret);
D
Daniel Veillard 已提交
1452 1453
}

1454 1455
/**
 * virDomainGetInfo:
1456
 * @domain: a domain object
1457 1458 1459 1460 1461 1462 1463 1464 1465
 * @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
1466 1467
virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
{
1468
    int ret;
1469
    int i;
1470

D
Daniel Veillard 已提交
1471 1472
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1473
        return (-1);
D
Daniel Veillard 已提交
1474 1475 1476
    }
    if (info == NULL) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1477
        return (-1);
D
Daniel Veillard 已提交
1478
    }
1479

1480
    memset(info, 0, sizeof(virDomainInfo));
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]->domainGetInfo != NULL)) {
	    if (domain->conn->drivers[i]->domainGetInfo(domain, info) == 0)
	        return 0;
	}
    }

1490 1491 1492 1493
    /*
     * if we have direct access though the hypervisor do a direct call
     */
    if (domain->conn->handle >= 0) {
1494
        ret = xenHypervisorGetDomainInfo(domain, info);
1495 1496
        if (ret == 0)
	    return (0);
1497 1498 1499 1500 1501
    }

    /*
     * try to extract the informations though access to the Xen Daemon
     */
1502
    if (xenDaemonDomainGetInfo(domain, info) == 0)
1503
        return (0);
1504 1505

    /*
1506
     * last fallback, try to get the informations from the Xen store
1507
     */
1508 1509
    if (xenStoreGetDomainInfo(domain, info) == 0)
        return (0);
1510

1511
    return (-1);
1512
}
1513

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
/**
 * 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 *
1526 1527
virDomainGetXMLDesc(virDomainPtr domain, int flags)
{
D
Daniel Veillard 已提交
1528 1529
    if (!VIR_IS_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1530
        return (NULL);
D
Daniel Veillard 已提交
1531 1532 1533
    }
    if (flags != 0) {
        virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
1534
        return (NULL);
D
Daniel Veillard 已提交
1535
    }
1536

1537
    return (xenDaemonDomainDumpXML(domain));
1538
}
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576

/**
 * 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);
}
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 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 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734

/************************************************************************
 *									*
 *		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;
    const char *name = NULL;
    xmlDocPtr doc = NULL;
    xmlXPathObjectPtr obj = NULL;
    xmlXPathContextPtr ctxt = NULL;

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

    /*
     * Check the XML description is at least well formed and extract the
     * name.
     * TODO: a full validation based on RNG for example should be done there
     */
    doc = xmlReadMemory(xml, strlen(xml), "domain_define.xml", NULL, 0);
    if (doc == NULL) {
        virLibConnError(conn, VIR_ERR_XML_ERROR, __FUNCTION__);
	goto done;
    }
    ctxt = xmlXPathNewContext(doc);
    if (ctxt == NULL) {
        goto done;
    }
    obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        virLibConnError(conn, VIR_ERR_NO_NAME, xml);
        goto done;
    }
    name = (const char *) obj->stringval;

    /*
     * Now look it up in the domain pool and check it's not an already run
     * domain.
     */
    ret = virGetDomain(conn, name, NULL);
    if (ret == NULL) {
        goto done;
    }
    /*
     * TODO: the lifecycle of domains, especially predefined ones need to be
     *       explicitely written down
     */
    if (ret->handle != -1) {
        virLibConnError(conn, VIR_ERR_DOM_EXIST, name);
        virFreeDomain(conn, ret);
	ret = NULL;
	goto done;
    }
    if ((ret->uses > 1) && (!(ret->flags & DOMAIN_IS_DEFINED))) {
        virLibConnError(conn, VIR_ERR_DOM_EXIST, name);
        virFreeDomain(conn, ret);
	ret = NULL;
	goto done;
    }
    ret->flags |= DOMAIN_IS_DEFINED;
    if (ret->xml != NULL) {
        free(ret->xml);
    }
    ret->xml = strdup(xml);
    if (ret->xml == NULL) {
        virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
	virFreeDomain(conn, ret);
	ret = NULL;
	goto done;
    }
    /* TODO shall we keep a list of defined domains there ? */

done:
    if (obj != NULL)
	xmlXPathFreeObject(obj);
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (doc != NULL)
        xmlFreeDoc(doc);
    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) {
    int ret;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
        virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
    }
    /* TODO shall we keep a list of defined domains there ? */

    ret = virFreeDomain(domain->conn, domain);
    if (ret < 0)
        return(-1);
    return(0);
}

/**
 * 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) {
    TODO
    return(-1);
}

/**
 * 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) {
    
    return(-1);
}