xml.c 47.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * xml.c: XML based interfaces for the libvir library
 *
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include "libvirt/libvirt.h"
12 13 14 15 16

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
17
#ifdef WITH_XEN
18
#include <xs.h>
19
#endif
20
#include <math.h> /* for isnan() */
21 22
#include "internal.h"
#include "hash.h"
D
Daniel Veillard 已提交
23
#include "sexpr.h"
24
#include "xml.h"
25
#include "buf.h"
26
#include "xs_internal.h" /* for xenStoreDomainGetNetworkID */
27

28
#ifndef PROXY
29 30 31 32 33 34 35 36 37
/**
 * virXMLError:
 * @conn: a connection if any
 * @error: the error number
 * @info: information/format string
 * @value: extra integer parameter for the error string
 *
 * Report an error coming from the XML module.
 */
38
static void
39
virXMLError(virConnectPtr conn, virErrorNumber error, const char *info, int value)
40
{
41
    const char *errmsg;
42

43 44 45 46
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
47
    __virRaiseError(conn, NULL, NULL, VIR_FROM_XML, error, VIR_ERR_ERROR,
48
                    errmsg, info, NULL, value, 0, errmsg, info, value);
49 50
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * virXPathString:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath string
 *
 * Returns a new string which must be deallocated by the caller or NULL
 *         if the evaluation failed.
 */
char *
virXPathString(const char *xpath, xmlXPathContextPtr ctxt) {
    xmlXPathObjectPtr obj;
    char *ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR, 
	            "Invalid parameter to virXPathString()", 0);
        return(NULL);
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
D
Daniel P. Berrange 已提交
73 74 75
        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
        if (obj)
            xmlXPathFreeObject(obj);
76
        return(NULL);
D
Daniel P. Berrange 已提交
77
    }
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    ret = strdup((char *) obj->stringval);
    xmlXPathFreeObject(obj);
    if (ret == NULL) {
        virXMLError(NULL, VIR_ERR_NO_MEMORY, "strdup", 0);
    }
    return(ret);
}

/**
 * virXPathNumber:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned double value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
 *         or -1 if the evaluation failed.
 */
int
virXPathNumber(const char *xpath, xmlXPathContextPtr ctxt, double *value) {
    xmlXPathObjectPtr obj;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR, 
	            "Invalid parameter to virXPathNumber()", 0);
        return(-1);
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
        (isnan(obj->floatval))) {
	xmlXPathFreeObject(obj);
	return(-1);
    }
    
    *value = obj->floatval;
    xmlXPathFreeObject(obj);
    return(0);
}

/**
 * virXPathLong:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @value: the returned long value
 *
 * Convenience function to evaluate an XPath number
 *
 * Returns 0 in case of success in which case @value is set,
127 128
 *         or -1 if the XPath evaluation failed or -2 if the
 *         value doesn't have a long format.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
 */
int
virXPathLong(const char *xpath, xmlXPathContextPtr ctxt, long *value) {
    xmlXPathObjectPtr obj;
    int ret = 0;

    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR, 
	            "Invalid parameter to virXPathNumber()", 0);
        return(-1);
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
        char *conv = NULL;
	long val;

        val = strtol((const char*)obj->stringval, &conv, 10);
        if (conv == (const char*)obj->stringval) {
            ret = -2;
        } else {
	    *value = val;
	}
    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
               (!(isnan(obj->floatval)))) {
	*value = (long) obj->floatval;
	if (*value != obj->floatval) {
	    ret = -2;
	}
    } else {
	ret = -1;
    }
    
    xmlXPathFreeObject(obj);
    return(ret);
}

/**
 * virXPathBoolean:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath boolean
 *
 * Returns 0 if false, 1 if true, or -1 if the evaluation failed.
 */
int
virXPathBoolean(const char *xpath, xmlXPathContextPtr ctxt) {
    xmlXPathObjectPtr obj;
    int ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR, 
	            "Invalid parameter to virXPathBoolean()", 0);
        return(-1);
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) ||
        (obj->boolval < 0) || (obj->boolval > 1)) {
	xmlXPathFreeObject(obj);
	return(-1);
    }
    ret = obj->boolval;
    
    xmlXPathFreeObject(obj);
    return(ret);
}

/**
 * virXPathNode:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 *
 * Convenience function to evaluate an XPath node set and returning
 * only one node, the first one in the set if any
 *
 * Returns a pointer to the node or NULL if the evaluation failed.
 */
xmlNodePtr
virXPathNode(const char *xpath, xmlXPathContextPtr ctxt) {
    xmlXPathObjectPtr obj;
    xmlNodePtr ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR, 
	            "Invalid parameter to virXPathNode()", 0);
        return(NULL);
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
	(obj->nodesetval->nodeTab == NULL)) {
	xmlXPathFreeObject(obj);
	return(NULL);
    }
    
    ret = obj->nodesetval->nodeTab[0];
    xmlXPathFreeObject(obj);
    return(ret);
}
/**
 * virXPathNodeSet:
 * @xpath: the XPath string to evaluate
 * @ctxt: an XPath context
 * @list: the returned list of nodes (or NULL if only count matters)
 *
 * Convenience function to evaluate an XPath node set
 *
 * Returns the number of nodes found in which case @list is set (and
 *         must be freed) or -1 if the evaluation failed.
 */
int
virXPathNodeSet(const char *xpath, xmlXPathContextPtr ctxt, xmlNodePtr **list) {
    xmlXPathObjectPtr obj;
    int ret;

    if ((ctxt == NULL) || (xpath == NULL)) {
        virXMLError(NULL, VIR_ERR_INTERNAL_ERROR, 
	            "Invalid parameter to virXPathNodeSet()", 0);
        return(-1);
    }
    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
	(obj->nodesetval->nodeTab == NULL)) {
	xmlXPathFreeObject(obj);
	if (list != NULL)
	    *list = NULL;
	return(-1);
    }
    
    ret = obj->nodesetval->nodeNr;
    if (list != NULL) {
	*list = malloc(ret * sizeof(xmlNodePtr));
	if (*list == NULL) {
	    virXMLError(NULL, VIR_ERR_NO_MEMORY, 
	                _("allocate string array"), ret * sizeof(xmlNodePtr));
	} else {
	    memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr));
	}
    }
    xmlXPathFreeObject(obj);
    return(ret);
}

274
/**
275
 * virtDomainParseXMLGraphicsDescImage:
276
 * @conn: pointer to the hypervisor connection
277 278
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
279
 * @xendConfigVersion: xend configuration file format
280
 *
281 282 283
 * Parse the graphics part of the XML description and add it to the S-Expr
 * in buf.  This is a temporary interface as the S-Expr interface will be
 * replaced by XML-RPC in the future. However the XML format should stay
284 285 286 287
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
288
static int virDomainParseXMLGraphicsDescImage(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int xendConfigVersion)
289 290 291 292 293 294 295
{
    xmlChar *graphics_type = NULL;

    graphics_type = xmlGetProp(node, BAD_CAST "type");
    if (graphics_type != NULL) {
        if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) {
            virBufferAdd(buf, "(sdl 1)", 7);
296 297 298 299 300 301
            /* TODO:
             * Need to understand sdl options
             *
             *virBufferAdd(buf, "(display localhost:10.0)", 24);
             *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30);
             */
302
        }
303
        else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
304
            virBufferAdd(buf, "(vnc 1)", 7);
305
            if (xendConfigVersion >= 2) {
306
                xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
307 308
                xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
                xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
309
                xmlChar *keymap = xmlGetProp(node, BAD_CAST "keymap");
310
                if (vncport != NULL) {
311
                    long port = strtol((const char *)vncport, NULL, 10);
312 313
                    if (port == -1)
                        virBufferAdd(buf, "(vncunused 1)", 13);
314
                    else if (port >= 5900)
315
                        virBufferVSprintf(buf, "(vncdisplay %ld)", port - 5900);
316
                    xmlFree(vncport);
317
                }
318 319 320 321 322 323 324 325
                if (vnclisten != NULL) {
                    virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                    xmlFree(vnclisten);
                }
                if (vncpasswd != NULL) {
                    virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                    xmlFree(vncpasswd);
                }
326 327 328 329
                if (keymap != NULL) {
                    virBufferVSprintf(buf, "(keymap %s)", keymap);
                    xmlFree(keymap);
                }
330 331
            }
        }
332 333 334 335 336 337
        xmlFree(graphics_type);
    }
    return 0;
}


338 339
/**
 * virtDomainParseXMLGraphicsDescVFB:
340
 * @conn: pointer to the hypervisor connection
341 342 343
 * @node: node containing graphics description
 * @buf: a buffer for the result S-Expr
 *
344 345 346
 * Parse the graphics part of the XML description and add it to the S-Expr
 * in buf.  This is a temporary interface as the S-Expr interface will be
 * replaced by XML-RPC in the future. However the XML format should stay
347 348 349 350
 * valid over time.
 *
 * Returns 0 in case of success, -1 in case of error
 */
351
static int virDomainParseXMLGraphicsDescVFB(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf)
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
{
    xmlChar *graphics_type = NULL;

    graphics_type = xmlGetProp(node, BAD_CAST "type");
    if (graphics_type != NULL) {
        virBufferAdd(buf, "(device (vkbd))", 15);
        virBufferAdd(buf, "(device (vfb ", 13);
        if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) {
            virBufferAdd(buf, "(type sdl)", 10);
            /* TODO:
             * Need to understand sdl options
             *
             *virBufferAdd(buf, "(display localhost:10.0)", 24);
             *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30);
             */
        }
        else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) {
            virBufferAdd(buf, "(type vnc)", 10);
            xmlChar *vncport = xmlGetProp(node, BAD_CAST "port");
            xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen");
            xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd");
373
            xmlChar *keymap = xmlGetProp(node, BAD_CAST "keymap");
374 375 376 377
            if (vncport != NULL) {
                long port = strtol((const char *)vncport, NULL, 10);
                if (port == -1)
                    virBufferAdd(buf, "(vncunused 1)", 13);
378
                else if (port >= 5900)
379
                    virBufferVSprintf(buf, "(vncdisplay %ld)", port - 5900);
380 381 382 383 384 385 386 387 388 389
                xmlFree(vncport);
            }
            if (vnclisten != NULL) {
                virBufferVSprintf(buf, "(vnclisten %s)", vnclisten);
                xmlFree(vnclisten);
            }
            if (vncpasswd != NULL) {
                virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd);
                xmlFree(vncpasswd);
            }
390 391 392 393
            if (keymap != NULL) {
                virBufferVSprintf(buf, "(keymap %s)", keymap);
                xmlFree(keymap);
            }
394 395 396 397 398 399 400 401
        }
        virBufferAdd(buf, "))", 2);
        xmlFree(graphics_type);
    }
    return 0;
}


402
/**
403
 * virDomainParseXMLOSDescHVM:
404
 * @conn: pointer to the hypervisor connection
405
 * @node: node containing HVM OS description
406
 * @buf: a buffer for the result S-Expr
407
 * @ctxt: a path context representing the XML description
408
 * @vcpus: number of virtual CPUs to configure
409
 * @xendConfigVersion: xend configuration file format
410
 *
411 412
 * Parse the OS part of the XML description for an HVM domain and add it to
 * the S-Expr in buf. This is a temporary interface as the S-Expr interface
413 414 415 416 417 418
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
419
virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int vcpus, int xendConfigVersion)
420 421
{
    xmlNodePtr cur, txt;
422
    xmlNodePtr *nodes = NULL;
423 424
    xmlChar *type = NULL;
    xmlChar *loader = NULL;
425 426
    char bootorder[5];
    int nbootorder = 0;
427
    int res, nb_nodes;
428
    char *str;
429 430 431 432 433 434 435 436

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
437
                    (txt->next == NULL))
438 439 440 441 442
                    type = txt->content;
            } else if ((loader == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "loader"))) {
                txt = cur->children;
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
443
                    (txt->next == NULL))
444
                    loader = txt->content;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
            } else if ((xmlStrEqual(cur->name, BAD_CAST "boot"))) {
                xmlChar *boot_dev = xmlGetProp(cur, BAD_CAST "dev");
                if (nbootorder == ((sizeof(bootorder)/sizeof(bootorder[0]))-1)) {
                    virXMLError(conn, VIR_ERR_XML_ERROR, "too many boot devices", 0);
                    return (-1);
                }
                if (xmlStrEqual(boot_dev, BAD_CAST "fd")) {
                    bootorder[nbootorder++] = 'a';
                } else if (xmlStrEqual(boot_dev, BAD_CAST "cdrom")) {
                    bootorder[nbootorder++] = 'd';
                } else if (xmlStrEqual(boot_dev, BAD_CAST "network")) {
                    bootorder[nbootorder++] = 'n';
                } else if (xmlStrEqual(boot_dev, BAD_CAST "hd")) {
                    bootorder[nbootorder++] = 'c';
                } else {
                    xmlFree(boot_dev);
                    /* Any other type of boot dev is unsupported right now */
                    virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
                    return (-1);
                }
                xmlFree(boot_dev);
466 467 468 469
            }
        }
        cur = cur->next;
    }
470
    bootorder[nbootorder] = '\0';
471 472
    if ((type == NULL) || (!xmlStrEqual(type, BAD_CAST "hvm"))) {
        /* VIR_ERR_OS_TYPE */
473
        virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0);
474 475 476 477
        return (-1);
    }
    virBufferAdd(buf, "(image (hvm ", 12);
    if (loader == NULL) {
478
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0);
479
        goto error;
480
    } else {
481
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader);
482 483 484
    }

    /* get the device emulation model */
485 486
    str = virXPathString("string(/domain/devices/emulator[1])", ctxt);
    if (str == NULL) {
487
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0); /* TODO: error */
488 489
        goto error;
    }
490 491
    virBufferVSprintf(buf, "(device_model '%s')", str);
    xmlFree(str);
492

493 494
    virBufferVSprintf(buf, "(vcpus %d)", vcpus);

495 496
    if (nbootorder)
        virBufferVSprintf(buf, "(boot %s)", bootorder);
497

498 499 500
    /* get the 1st floppy device file */
	cur = virXPathNode("/domain/devices/disk[@device='floppy' and target/@dev='fda']/source",
                       ctxt);
501
	if (cur != NULL) {
502 503
        xmlChar *fdfile;
        fdfile = xmlGetProp(cur, BAD_CAST "file");
504
	    if (fdfile != NULL) {
505 506
            virBufferVSprintf(buf, "(fda '%s')", fdfile);
            free(fdfile);
507
	    }
508
    }
509

510 511 512
    /* get the 2nd floppy device file */
	cur = virXPathNode("/domain/devices/disk[@device='floppy' and target/@dev='fdb']/source",
                       ctxt);
513
	if (cur != NULL) {
514 515
        xmlChar *fdfile;
        fdfile = xmlGetProp(cur, BAD_CAST "file");
516
	    if (fdfile != NULL) {
517 518
            virBufferVSprintf(buf, "(fdb '%s')", fdfile);
            free(fdfile);
519
	    }
520
    }
521 522


523 524 525 526
    /* get the cdrom device file */
    /* Only XenD <= 3.0.2 wants cdrom config here */
    if (xendConfigVersion == 1) {
	    cur = virXPathNode("/domain/devices/disk[@device='cdrom' and target/@dev='hdc']/source",
527 528
	                       ctxt);
	    if (cur != NULL) {
529 530 531 532 533 534 535
            xmlChar *cdfile;

            cdfile = xmlGetProp(cur, BAD_CAST "file");
            if (cdfile != NULL) {
                virBufferVSprintf(buf, "(cdrom '%s')",
                                  (const char *)cdfile);
                xmlFree(cdfile);
536 537
            }
        }
538 539
    }

540 541 542 543 544 545 546
    if (virXPathNode("/domain/features/acpi", ctxt) != NULL)
        virBufferAdd(buf, "(acpi 1)", 8);
    if (virXPathNode("/domain/features/apic", ctxt) != NULL)
        virBufferAdd(buf, "(apic 1)", 8);
    if (virXPathNode("/domain/features/pae", ctxt) != NULL)
        virBufferAdd(buf, "(pae 1)", 7);

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    virBufferAdd(buf, "(usb 1)", 7);
    nb_nodes = virXPathNodeSet("/domain/devices/input", ctxt, &nodes);
    if (nb_nodes > 0) {
        int i;
        for (i = 0; i < nb_nodes; i++) {
            xmlChar *itype = NULL, *bus = NULL;
            int isMouse = 1;

            itype = xmlGetProp(nodes[i], (xmlChar *)"type");

            if (!itype) {
                goto error;
            }
            if (!strcmp((const char *)itype, "tablet"))
                isMouse = 0;
            else if (strcmp((const char*)itype, "mouse")) {
                xmlFree(itype);
                virXMLError(conn, VIR_ERR_XML_ERROR, "input", 0);
                goto error;
            }
            xmlFree(itype);

            bus = xmlGetProp(nodes[i], (xmlChar *)"bus");
            if (!bus) {
                if (!isMouse) {
                    /* Nothing - implicit ps2 */
                } else {
                    virBufferAdd(buf, "(usbdevice tablet)", 13);
                }
            } else {
                if (!strcmp((const char*)bus, "ps2")) {
                    if (!isMouse) {
                        xmlFree(bus);
                        virXMLError(conn, VIR_ERR_XML_ERROR, "input", 0);
                        goto error;
                    }
                    /* Nothing - implicit ps2 */
                } else if (!strcmp((const char*)bus, "usb")) {
                    if (isMouse)
                        virBufferAdd(buf, "(usbdevice mouse)", 17);
                    else
                        virBufferAdd(buf, "(usbdevice tablet)", 18);
                }
            }
            xmlFree(bus);
        }
        free(nodes);
        nodes = NULL;
    }


598 599
    res = virXPathBoolean("count(domain/devices/console) > 0", ctxt);
    if (res < 0) {
600
        virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0);
601
        goto error;
602
    }
603
    if (res) {
604
        virBufferAdd(buf, "(serial pty)", 12);
605
    }
606

607 608 609 610 611 612 613 614 615 616
    /* HVM graphics for xen <= 3.0.5 */
    if (xendConfigVersion < 4) {
        /* Is a graphics device specified? */
        cur = virXPathNode("/domain/devices/graphics[1]", ctxt);
        if (cur != NULL) {
            res = virDomainParseXMLGraphicsDescImage(conn, cur, buf,
                                                     xendConfigVersion);
            if (res != 0) {
                goto error;
            }
617 618 619
        }
    }

620 621 622 623
    str = virXPathString("string(/domain/clock/@offset)", ctxt);
    if (str != NULL && !strcmp(str, "localtime")) {
        virBufferAdd(buf, "(localtime 1)", 13);
    }
D
Daniel P. Berrange 已提交
624 625
    if (str)
        free(str);
626

627 628 629
    virBufferAdd(buf, "))", 2);

    return (0);
630

631
 error:
632 633
    if (nodes)
        free(nodes);
634 635 636 637 638
    return(-1);
}

/**
 * virDomainParseXMLOSDescPV:
639
 * @conn: pointer to the hypervisor connection
640 641
 * @node: node containing PV OS description
 * @buf: a buffer for the result S-Expr
642
 * @ctxt: a path context representing the XML description
643
 * @xendConfigVersion: xend configuration file format
644 645 646 647 648 649 650 651 652
 *
 * Parse the OS part of the XML description for a paravirtualized domain
 * and add it to the S-Expr in buf.  This is a temporary interface as the
 * S-Expr interface will be replaced by XML-RPC in the future. However
 * the XML format should stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
653
virDomainParseXMLOSDescPV(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion)
654
{
655 656 657 658 659 660
    xmlNodePtr cur, txt;
    const xmlChar *type = NULL;
    const xmlChar *root = NULL;
    const xmlChar *kernel = NULL;
    const xmlChar *initrd = NULL;
    const xmlChar *cmdline = NULL;
661
    int res;
662 663 664 665

    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
666 667 668
            if ((type == NULL)
                && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
                txt = cur->children;
669
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
670
                    (txt->next == NULL))
671 672 673 674
                    type = txt->content;
            } else if ((kernel == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
                txt = cur->children;
675
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
676
                    (txt->next == NULL))
677 678 679 680
                    kernel = txt->content;
            } else if ((root == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "root"))) {
                txt = cur->children;
681
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
682
                    (txt->next == NULL))
683 684 685 686
                    root = txt->content;
            } else if ((initrd == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
                txt = cur->children;
687
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
688
                    (txt->next == NULL))
689 690 691 692
                    initrd = txt->content;
            } else if ((cmdline == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
                txt = cur->children;
693
                if ((txt != NULL) && (txt->type == XML_TEXT_NODE) &&
694
                    (txt->next == NULL))
695 696 697
                    cmdline = txt->content;
            }
        }
698 699 700 701
        cur = cur->next;
    }
    if ((type != NULL) && (!xmlStrEqual(type, BAD_CAST "linux"))) {
        /* VIR_ERR_OS_TYPE */
702
        virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0);
703
        return (-1);
704
    }
705
    virBufferAdd(buf, "(image (linux ", 14);
706
    if (kernel == NULL) {
707
        virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0);
708
        return (-1);
709
    } else {
710
        virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);
711 712
    }
    if (initrd != NULL)
713
        virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
714
    if (root != NULL)
715
        virBufferVSprintf(buf, "(root '%s')", (const char *) root);
716
    if (cmdline != NULL)
717
        virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
718

719
    /* PV graphics for xen <= 3.0.4 */
720
    if (xendConfigVersion < 3) {
721 722 723
        cur = virXPathNode("/domain/devices/graphics[1]", ctxt);
        if (cur != NULL) {
            res = virDomainParseXMLGraphicsDescImage(conn, cur, buf,
724
                                                     xendConfigVersion);
725 726 727
            if (res != 0) {
                goto error;
            }
728 729 730 731
        }
    }

 error:
732
    virBufferAdd(buf, "))", 2);
733
    return (0);
734 735
}

736 737 738 739 740 741 742 743 744 745
/**
 * virCatchXMLParseError:
 * @ctx: the context
 * @msg: the error message
 * @...: extra arguments
 *
 * SAX callback on parsing errors, act as a gate for libvirt own
 * error reporting.
 */
static void
746
virCatchXMLParseError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
747 748
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;

749
    if ((ctxt != NULL) &&
750
        (ctxt->lastError.level == XML_ERR_FATAL) &&
751
        (ctxt->lastError.message != NULL)) {
752
        virXMLError(NULL, VIR_ERR_XML_DETAIL, ctxt->lastError.message,
753
                    ctxt->lastError.line);
754 755 756
    }
}

757 758
/**
 * virDomainParseXMLDiskDesc:
759
 * @node: node containing disk description
760
 * @conn: pointer to the hypervisor connection
761
 * @buf: a buffer for the result S-Expr
762
 * @xendConfigVersion: xend configuration file format
763 764 765 766 767 768 769 770 771
 *
 * Parse the one disk in the XML description and add it to the S-Expr in buf
 * This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
772
virDomainParseXMLDiskDesc(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion)
773
{
774 775
    xmlNodePtr cur;
    xmlChar *type = NULL;
776
    xmlChar *device = NULL;
777 778
    xmlChar *source = NULL;
    xmlChar *target = NULL;
779 780
    xmlChar *drvName = NULL;
    xmlChar *drvType = NULL;
781
    int ro = 0;
782
    int shareable = 0;
783
    int typ = 0;
784
    int cdrom = 0;
785
    int isNoSrcCdrom = 0;
786
    int ret = 0;
787 788 789

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
790 791 792 793 794
        if (xmlStrEqual(type, BAD_CAST "file"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "block"))
            typ = 1;
        xmlFree(type);
795
    }
796
    device = xmlGetProp(node, BAD_CAST "device");
797

798 799 800
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
801 802 803 804 805 806 807 808 809 810
            if ((source == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "source"))) {

                if (typ == 0)
                    source = xmlGetProp(cur, BAD_CAST "file");
                else
                    source = xmlGetProp(cur, BAD_CAST "dev");
            } else if ((target == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "target"))) {
                target = xmlGetProp(cur, BAD_CAST "dev");
811 812 813 814 815
            } else if ((drvName == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "driver"))) {
                drvName = xmlGetProp(cur, BAD_CAST "name");
                if (drvName && !strcmp((const char *)drvName, "tap"))
                    drvType = xmlGetProp(cur, BAD_CAST "type");
816 817
            } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
                ro = 1;
818
            } else if (xmlStrEqual(cur->name, BAD_CAST "shareable")) {
819
                shareable = 1;
820 821
            }
        }
822 823 824 825
        cur = cur->next;
    }

    if (source == NULL) {
826 827 828 829 830 831 832 833 834 835
        /* There is a case without the source
         * to the CD-ROM device
         */
        if (hvm &&
            device &&
            !strcmp((const char *)device, "cdrom")) {
            isNoSrcCdrom = 1;
        }
        if (!isNoSrcCdrom) {
            virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) target, 0);
836 837
            ret = -1;
            goto cleanup;
838
        }
839 840
    }
    if (target == NULL) {
841
        virXMLError(conn, VIR_ERR_NO_TARGET, (const char *) source, 0);
842 843
        ret = -1;
        goto cleanup;
844
    }
845

846 847
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
848
     */
849
    if (hvm &&
850
        device &&
851
        !strcmp((const char *)device, "floppy")) {
852
        goto cleanup;
853 854 855
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
856
    if (hvm &&
857 858
        device &&
        !strcmp((const char *)device, "cdrom")) {
859
        if (xendConfigVersion == 1)
860
            goto cleanup;
861 862
        else
            cdrom = 1;
863 864 865 866
    }


    virBufferAdd(buf, "(device ", 8);
867 868 869 870 871 872 873 874
    /* Normally disks are in a (device (vbd ...)) block
       but blktap disks ended up in a differently named
       (device (tap ....)) block.... */
    if (drvName && !strcmp((const char *)drvName, "tap")) {
        virBufferAdd(buf, "(tap ", 5);
    } else {
        virBufferAdd(buf, "(vbd ", 5);
    }
875

876 877
    if (hvm) {
        char *tmp = (char *)target;
878
        /* Just in case user mistakenly still puts ioemu: in their XML */
879 880
        if (!strncmp((const char *) tmp, "ioemu:", 6))
            tmp += 6;
881 882 883

        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
        if (xendConfigVersion == 1)
884
            virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *)tmp);
885
        else /* But newer does not */
886
            virBufferVSprintf(buf, "(dev '%s%s')", (const char *)tmp, cdrom ? ":cdrom" : ":disk");
887
    } else
888 889
        virBufferVSprintf(buf, "(dev '%s')", (const char *)target);

890
    if (drvName && !isNoSrcCdrom) {
891 892 893 894 895 896 897 898 899 900
        if (!strcmp((const char *)drvName, "tap")) {
            virBufferVSprintf(buf, "(uname '%s:%s:%s')",
                              (const char *)drvName,
                              (drvType ? (const char *)drvType : "aio"),
                              (const char *)source);
        } else {
            virBufferVSprintf(buf, "(uname '%s:%s')",
                              (const char *)drvName,
                              (const char *)source);
        }
901
    } else if (!isNoSrcCdrom) {
902 903 904 905 906 907 908 909
        if (typ == 0)
            virBufferVSprintf(buf, "(uname 'file:%s')", source);
        else if (typ == 1) {
            if (source[0] == '/')
                virBufferVSprintf(buf, "(uname 'phy:%s')", source);
            else
                virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source);
        }
910
    }
911
    if (ro == 1)
912
        virBufferVSprintf(buf, "(mode 'r')");
913 914 915 916
    else if (shareable == 1)
        virBufferVSprintf(buf, "(mode 'w!')");
    else
        virBufferVSprintf(buf, "(mode 'w')");
917

918
    virBufferAdd(buf, ")", 1);
919
    virBufferAdd(buf, ")", 1);
920 921

 cleanup:
922 923 924 925 926 927 928 929 930 931 932
    if(drvType)
        xmlFree(drvType);
    if(drvName)
        xmlFree(drvName);
    if(device)
        xmlFree(device);
    if(target)
        xmlFree(target);
    if(source)
        xmlFree(source);
    return (ret);
933 934 935 936
}

/**
 * virDomainParseXMLIfDesc:
937
 * @conn: pointer to the hypervisor connection
938
 * @node: node containing the interface description
939
 * @buf: a buffer for the result S-Expr
940
 * @xendConfigVersion: xend configuration file format
941 942 943 944 945 946 947 948 949
 *
 * Parse the one interface the XML description and add it to the S-Expr in buf
 * This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
static int
950
virDomainParseXMLIfDesc(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion)
951
{
952 953 954 955 956
    xmlNodePtr cur;
    xmlChar *type = NULL;
    xmlChar *source = NULL;
    xmlChar *mac = NULL;
    xmlChar *script = NULL;
957
    xmlChar *ip = NULL;
958
    int typ = 0;
959
    int ret = -1;
960 961 962

    type = xmlGetProp(node, BAD_CAST "type");
    if (type != NULL) {
963 964 965 966
        if (xmlStrEqual(type, BAD_CAST "bridge"))
            typ = 0;
        else if (xmlStrEqual(type, BAD_CAST "ethernet"))
            typ = 1;
967 968
        else if (xmlStrEqual(type, BAD_CAST "network"))
            typ = 2;
969
        xmlFree(type);
970 971 972 973
    }
    cur = node->children;
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
974 975 976 977
            if ((source == NULL) &&
                (xmlStrEqual(cur->name, BAD_CAST "source"))) {
                if (typ == 0)
                    source = xmlGetProp(cur, BAD_CAST "bridge");
978
                else if (typ == 1)
979
                    source = xmlGetProp(cur, BAD_CAST "dev");
980 981
                else
                    source = xmlGetProp(cur, BAD_CAST "network");
982 983 984 985 986 987
            } else if ((mac == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "mac"))) {
                mac = xmlGetProp(cur, BAD_CAST "address");
            } else if ((script == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "script"))) {
                script = xmlGetProp(cur, BAD_CAST "path");
988 989 990 991 992 993 994
            } else if ((ip == NULL) &&
                       (xmlStrEqual(cur->name, BAD_CAST "ip"))) {
                /* XXX in future expect to need to have > 1 ip
                   address element - eg ipv4 & ipv6. For now
                   xen only supports a single address though
                   so lets ignore that complication */
                ip = xmlGetProp(cur, BAD_CAST "address");
995 996
            }
        }
997 998 999 1000
        cur = cur->next;
    }

    virBufferAdd(buf, "(vif ", 5);
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    if (mac != NULL) {
        unsigned int addr[12];
        int tmp = sscanf((const char *)mac,
	        "%01x%01x:%01x%01x:%01x%01x:%01x%01x:%01x%01x:%01x%01x",
                (unsigned int*)&addr[0], (unsigned int*)&addr[1],
		(unsigned int*)&addr[2], (unsigned int*)&addr[3],
		(unsigned int*)&addr[4], (unsigned int*)&addr[5],
                (unsigned int*)&addr[6], (unsigned int*)&addr[7],
		(unsigned int*)&addr[8], (unsigned int*)&addr[9],
		(unsigned int*)&addr[10], (unsigned int*)&addr[11]);
        if (tmp != 12 || strlen((const char *) mac) != 17) {
            virXMLError(conn, VIR_ERR_INVALID_MAC, (const char *) mac, 0);
            goto error;
        }
1015
        virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
1016
    }
1017
    if (source != NULL) {
1018 1019
        if (typ == 0)
            virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
1020
        else if (typ == 1)      /* TODO does that work like that ? */
1021
            virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
        else {
            virNetworkPtr network = virNetworkLookupByName(conn, (const char *) source);
            char *bridge;
            if (!network || !(bridge = virNetworkGetBridgeName(network))) {
                virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) source, 0);
                goto error;
            }
            virBufferVSprintf(buf, "(bridge '%s')", bridge);
            free(bridge);
        }
1032 1033 1034
    }
    if (script != NULL)
        virBufferVSprintf(buf, "(script '%s')", script);
1035 1036
    if (ip != NULL)
        virBufferVSprintf(buf, "(ip '%s')", ip);
1037 1038 1039 1040 1041
    /*
     * apparently (type ioemu) breaks paravirt drivers on HVM so skip this
     * from Xen 3.1.0
     */
    if ((hvm) && (xendConfigVersion < 4))
1042
        virBufferAdd(buf, "(type ioemu)", 12);
1043 1044

    virBufferAdd(buf, ")", 1);
1045 1046
    ret = 0;
 error:
1047
    if (mac != NULL)
1048
        xmlFree(mac);
1049
    if (source != NULL)
1050
        xmlFree(source);
1051
    if (script != NULL)
1052
        xmlFree(script);
1053 1054
    if (ip != NULL)
        xmlFree(ip);
1055
    return (ret);
1056 1057 1058 1059
}

/**
 * virDomainParseXMLDesc:
1060
 * @conn: pointer to the hypervisor connection
1061
 * @xmldesc: string with the XML description
1062
 * @xendConfigVersion: xend configuration file format
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
 *
 * Parse the XML description and turn it into the xend sexp needed to
 * create the comain. This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns the 0 terminatedi S-Expr string or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
1073
virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name, int xendConfigVersion)
1074
{
1075 1076
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
1077
    char *nam = NULL;
1078 1079
    virBuffer buf;
    xmlChar *prop;
1080
    xmlParserCtxtPtr pctxt;
1081 1082
    xmlXPathContextPtr ctxt = NULL;
    int i, res;
1083
    int bootloader = 0;
1084
    int hvm = 0;
1085
    unsigned int vcpus = 1;
1086
    unsigned long mem = 0, max_mem = 0;
1087 1088 1089 1090
    char *str;
    double f;
    xmlNodePtr *nodes;
    int nb_nodes;
1091 1092

    if (name != NULL)
1093
        *name = NULL;
1094 1095
    buf.content = malloc(1000);
    if (buf.content == NULL)
1096
        return (NULL);
1097 1098 1099
    buf.size = 1000;
    buf.use = 0;

1100 1101 1102 1103 1104
    pctxt = xmlNewParserCtxt();
    if ((pctxt == NULL) || (pctxt->sax == NULL)) {
        goto error;
    }

1105 1106 1107
    /* TODO pass the connection point to the error handler:
     *   pctxt->userData = virConnectPtr;
     */
1108 1109 1110 1111 1112
    pctxt->sax->error = virCatchXMLParseError;

    xml = xmlCtxtReadDoc(pctxt, (const xmlChar *) xmldesc, "domain.xml", NULL,
                         XML_PARSE_NOENT | XML_PARSE_NONET |
                         XML_PARSE_NOWARNING);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    if (xml == NULL) {
        goto error;
    }
    node = xmlDocGetRootElement(xml);
    if ((node == NULL) || (!xmlStrEqual(node->name, BAD_CAST "domain")))
        goto error;

    prop = xmlGetProp(node, BAD_CAST "type");
    if (prop != NULL) {
        if (!xmlStrEqual(prop, BAD_CAST "xen")) {
1123 1124 1125 1126
            xmlFree(prop);
            goto error;
        }
        xmlFree(prop);
1127 1128 1129 1130 1131 1132 1133
    }
    virBufferAdd(&buf, "(vm ", 4);
    ctxt = xmlXPathNewContext(xml);
    if (ctxt == NULL) {
        goto error;
    }
    /*
1134
     * extract some of the basics, name, memory, cpus ...
1135
     */
1136
    nam = virXPathString("string(/domain/name[1])", ctxt);
1137
    if (nam == NULL) {
1138
        virXMLError(conn, VIR_ERR_NO_NAME, xmldesc, 0);
1139
        goto error;
1140
    }
1141
    virBufferVSprintf(&buf, "(name '%s')", nam);
1142

1143 1144
    if ((virXPathNumber("number(/domain/memory[1])", ctxt, &f) < 0) ||
        (f < MIN_XEN_GUEST_SIZE * 1024)) {
1145
        max_mem = 128;
1146
    } else {
1147
        max_mem = (f / 1024);
1148
    }
1149 1150 1151

    if ((virXPathNumber("number(/domain/currentMemory[1])", ctxt, &f) < 0) ||
        (f < MIN_XEN_GUEST_SIZE * 1024)) {
1152 1153
        mem = max_mem;
    } else {
1154
        mem = (f / 1024);
1155 1156 1157
        if (mem > max_mem) {
            max_mem = mem;
        }
1158
    }
1159
    virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem);
1160

1161 1162 1163
    if ((virXPathNumber("number(/domain/vcpu[1])", ctxt, &f) == 0) &&
        (f > 0)) {
        vcpus = (unsigned int) f;
1164
    }
1165
    virBufferVSprintf(&buf, "(vcpus %u)", vcpus);
1166

1167 1168 1169 1170
    str = virXPathString("string(/domain/uuid[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(uuid '%s')", str);
	free(str);
1171 1172
    }

1173 1174 1175
    str = virXPathString("string(/domain/bootloader[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(bootloader '%s')", str);
1176
        /*
1177
         * if using a bootloader, the kernel and initrd strings are not
1178 1179
         * significant and should be discarded
         */
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
        bootloader = 1;
	free(str);
    }

    str = virXPathString("string(/domain/bootloader_args[1])", ctxt);
    if (str != NULL && bootloader) {
        /*
         * ignore the bootloader_args value unless a bootloader was specified
         */
        virBufferVSprintf(&buf, "(bootloader_args '%s')", str);
1190
	free(str);
1191 1192
    }

1193 1194 1195 1196
    str = virXPathString("string(/domain/on_poweroff[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(on_poweroff '%s')", str);
	free(str);
1197 1198
    }

1199 1200 1201 1202
    str = virXPathString("string(/domain/on_reboot[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(on_reboot '%s')", str);
	free(str);
1203 1204
    }

1205 1206 1207 1208
    str = virXPathString("string(/domain/on_crash[1])", ctxt);
    if (str != NULL) {
        virBufferVSprintf(&buf, "(on_crash '%s')", str);
	free(str);
1209 1210
    }

1211
    if (!bootloader) {
1212
        if ((node = virXPathNode("/domain/os[1]", ctxt)) != NULL) {
1213
            /* Analyze of the os description, based on HVM or PV. */
1214
            str = virXPathString("string(/domain/os/type[1])", ctxt);
1215

1216 1217
            if ((str == NULL) || (strcmp(str, "hvm"))) {
                res = virDomainParseXMLOSDescPV(conn, node,
1218 1219 1220
                                                &buf, ctxt, xendConfigVersion);
            } else {
                hvm = 1;
1221 1222
                res = virDomainParseXMLOSDescHVM(conn, node, &buf, ctxt,
		                                 vcpus, xendConfigVersion);
1223 1224
            }

1225
            if (str != NULL) free(str);
1226 1227 1228

            if (res != 0)
                goto error;
1229
        } else {
1230
            virXMLError(conn, VIR_ERR_NO_OS, nam, 0);
1231 1232
            goto error;
        }
1233 1234 1235
    }

    /* analyze of the devices */
1236 1237 1238 1239 1240
    nb_nodes = virXPathNodeSet("/domain/devices/disk", ctxt, &nodes);
    if (nb_nodes > 0) {
        for (i = 0; i < nb_nodes; i++) {
            res = virDomainParseXMLDiskDesc(conn, nodes[i], &buf,
	                                    hvm, xendConfigVersion);
1241
            if (res != 0) {
1242
	        free(nodes);
1243 1244 1245
                goto error;
            }
        }
1246
        free(nodes);
1247
    }
1248

1249 1250 1251
    nb_nodes = virXPathNodeSet("/domain/devices/interface", ctxt, &nodes);
    if (nb_nodes > 0) {
        for (i = 0; i < nb_nodes; i++) {
1252
            virBufferAdd(&buf, "(device ", 8);
1253
            res = virDomainParseXMLIfDesc(conn, nodes[i], &buf, hvm, xendConfigVersion);
1254
            if (res != 0) {
1255
	        free(nodes);
1256 1257 1258 1259
                goto error;
            }
            virBufferAdd(&buf, ")", 1);
        }
1260
        free(nodes);
1261 1262
    }

1263 1264 1265 1266
    /* New style PV graphics config xen >= 3.0.4,
     * or HVM graphics config xen >= 3.0.5 */
    if ((xendConfigVersion >= 3 && !hvm) ||
        (xendConfigVersion >= 4 && hvm)) {
1267
        nb_nodes = virXPathNodeSet("/domain/devices/graphics", ctxt, &nodes);
1268
        if (nb_nodes > 0) {
1269 1270
            for (i = 0; i < nb_nodes; i++) {
                res = virDomainParseXMLGraphicsDescVFB(conn, nodes[i], &buf);
1271
                if (res != 0) {
1272
                    free(nodes);
1273 1274 1275
                    goto error;
                }
            }
1276
            free(nodes);
1277 1278 1279
        }
    }

1280

D
Daniel Veillard 已提交
1281
    virBufferAdd(&buf, ")", 1); /* closes (vm */
1282 1283 1284 1285
    buf.content[buf.use] = 0;

    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
1286
    xmlFreeParserCtxt(pctxt);
1287 1288

    if (name != NULL)
1289
        *name = nam;
1290 1291
    else
        free(nam);
1292

1293
    return (buf.content);
1294

1295
 error:
1296
    if (nam != NULL)
1297
        free(nam);
1298
    if (name != NULL)
1299
        *name = NULL;
1300 1301 1302 1303
    if (ctxt != NULL)
        xmlXPathFreeContext(ctxt);
    if (xml != NULL)
        xmlFreeDoc(xml);
1304 1305
    if (pctxt != NULL)
        xmlFreeParserCtxt(pctxt);
1306 1307
    if (buf.content != NULL)
        free(buf.content);
1308
    return (NULL);
1309
}
1310 1311

#endif /* !PROXY */
1312 1313 1314 1315



unsigned char *virParseUUID(char **ptr, const char *uuid) {
1316
    int rawuuid[VIR_UUID_BUFLEN];
1317
    const char *cur;
1318 1319 1320 1321 1322 1323
    unsigned char *dst_uuid = NULL;
    int i;

    if (uuid == NULL)
        goto error;

1324 1325 1326 1327 1328
    /*
     * do a liberal scan allowing '-' and ' ' anywhere between character
     * pairs as long as there is 32 of them in the end.
     */
    cur = uuid;
1329
    for (i = 0;i < VIR_UUID_BUFLEN;) {
1330 1331
        rawuuid[i] = 0;
        if (*cur == 0)
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
            goto error;
        if ((*cur == '-') || (*cur == ' ')) {
            cur++;
            continue;
        }
        if ((*cur >= '0') && (*cur <= '9'))
            rawuuid[i] = *cur - '0';
        else if ((*cur >= 'a') && (*cur <= 'f'))
            rawuuid[i] = *cur - 'a' + 10;
        else if ((*cur >= 'A') && (*cur <= 'F'))
            rawuuid[i] = *cur - 'A' + 10;
        else
            goto error;
        rawuuid[i] *= 16;
        cur++;
1347
        if (*cur == 0)
1348 1349 1350 1351 1352 1353 1354 1355 1356
            goto error;
        if ((*cur >= '0') && (*cur <= '9'))
            rawuuid[i] += *cur - '0';
        else if ((*cur >= 'a') && (*cur <= 'f'))
            rawuuid[i] += *cur - 'a' + 10;
        else if ((*cur >= 'A') && (*cur <= 'F'))
            rawuuid[i] += *cur - 'A' + 10;
        else
            goto error;
1357
        i++;
1358
        cur++;
1359
    }
1360 1361 1362 1363

    dst_uuid = (unsigned char *) *ptr;
    *ptr += 16;

1364
    for (i = 0; i < VIR_UUID_BUFLEN; i++)
1365 1366
        dst_uuid[i] = rawuuid[i] & 0xFF;

1367
 error:
1368
    return(dst_uuid);
1369
}
1370

1371 1372 1373
#ifndef PROXY
/**
 * virParseXMLDevice:
1374
 * @conn: pointer to the hypervisor connection
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
 * @xmldesc: string with the XML description
 * @hvm: 1 for fully virtualized guest, 0 for paravirtualized
 * @xendConfigVersion: xend configuration file format
 *
 * Parse the XML description and turn it into the xend sexp needed to
 * create the device. This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns the 0-terminated S-Expr string, or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
1388
virParseXMLDevice(virConnectPtr conn, char *xmldesc, int hvm, int xendConfigVersion)
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
{
    xmlDocPtr xml = NULL;
    xmlNodePtr node;
    virBuffer buf;

    buf.content = malloc(1000);
    if (buf.content == NULL)
        return (NULL);
    buf.size = 1000;
    buf.use = 0;
1399
    buf.content[0] = 0;
1400 1401 1402 1403 1404 1405 1406 1407 1408
    xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    if (xml == NULL)
        goto error;
    node = xmlDocGetRootElement(xml);
    if (node == NULL)
        goto error;
    if (xmlStrEqual(node->name, BAD_CAST "disk")) {
1409
        if (virDomainParseXMLDiskDesc(conn, node, &buf, hvm, xendConfigVersion) != 0)
1410
            goto error;
1411 1412 1413
        /* SXP is not created when device is "floppy". */
       else if (buf.use == 0)
           goto error;
1414 1415
    }
    else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
1416
        if (virDomainParseXMLIfDesc(conn, node, &buf, hvm, xendConfigVersion) != 0)
1417
            goto error;
1418 1419 1420
    } else {
        virXMLError(conn, VIR_ERR_XML_ERROR, (const char *) node->name, 0);
	goto error;
1421
    }
1422
 cleanup:
1423 1424 1425
    if (xml != NULL)
        xmlFreeDoc(xml);
    return buf.content;
1426
 error:
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
    free(buf.content);
    buf.content = NULL;
    goto cleanup;
}

/**
 * virDomainXMLDevID:
 * @domain: pointer to domain object
 * @xmldesc: string with the XML description
 * @class: Xen device class "vbd" or "vif" (OUT)
 * @ref: Xen device reference (OUT)
 *
 * Set class according to XML root, and:
 *  - if disk, copy in ref the target name from description
 *  - if network, get MAC address from description, scan XenStore and
 *    copy in ref the corresponding vif number.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */
int
virDomainXMLDevID(virDomainPtr domain, char *xmldesc, char *class, char *ref)
{
    xmlDocPtr xml = NULL;
    xmlNodePtr node, cur;
    xmlChar *attr = NULL;
1452
#ifdef WITH_XEN
1453
    char *xref;
1454
#endif /* WITH_XEN */
1455
    int ret = 0;
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

    xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
                     XML_PARSE_NOENT | XML_PARSE_NONET |
                     XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
    if (xml == NULL)
        goto error;
    node = xmlDocGetRootElement(xml);
    if (node == NULL)
        goto error;
    if (xmlStrEqual(node->name, BAD_CAST "disk")) {
        strcpy(class, "vbd");
        for (cur = node->children; cur != NULL; cur = cur->next) {
            if ((cur->type != XML_ELEMENT_NODE) ||
1469
                (!xmlStrEqual(cur->name, BAD_CAST "target"))) continue;
1470 1471 1472
            attr = xmlGetProp(cur, BAD_CAST "dev");
            if (attr == NULL)
                goto error;
1473
            strcpy(ref, (char *)attr);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
            goto cleanup;
        }
    }
    else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
        strcpy(class, "vif");
        for (cur = node->children; cur != NULL; cur = cur->next) {
            if ((cur->type != XML_ELEMENT_NODE) ||
                (!xmlStrEqual(cur->name, BAD_CAST "mac"))) continue;
            attr = xmlGetProp(cur, BAD_CAST "address");
            if (attr == NULL)
                goto error;

1486
#ifdef WITH_XEN
1487
            xref = xenStoreDomainGetNetworkID(domain->conn, domain->id,
1488 1489 1490 1491 1492 1493
                                              (char *) attr);
            if (xref != NULL) {
                strcpy(ref, xref);
                free(xref);
                goto cleanup;
            }
1494 1495 1496 1497
#else /* without xen */
            /* hack to avoid the warning that domain is unused */
            if (domain->id < 0)
	        ret = -1;
1498
#endif /* WITH_XEN */
1499

1500 1501 1502
            goto error;
        }
    }
1503
 error:
1504
    ret = -1;
1505
 cleanup:
1506 1507 1508 1509 1510 1511 1512 1513
    if (xml != NULL)
        xmlFreeDoc(xml);
    if (attr != NULL)
        xmlFree(attr);
    return ret;
}
#endif /* !PROXY */

1514 1515 1516 1517 1518 1519 1520 1521
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */