esx_vmx.c 95.7 KB
Newer Older
1 2

/*
3
 * esx_vmx.c: VMX related functions for the VMware ESX driver
4
 *
5
 * Copyright (C) 2010 Red Hat, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2009 Matthias Bolte <matthias.bolte@googlemail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#include <config.h>

#include "internal.h"
#include "virterror_internal.h"
#include "memory.h"
#include "logging.h"
30
#include "esx_private.h"
31 32 33 34 35 36 37 38 39 40 41
#include "esx_util.h"
#include "esx_vmx.h"

/*

mapping:

domain-xml                        <=>   vmx


                                        config.version = "8"                    # essential
42 43
                                        virtualHW.version = "4"                 # essential for ESX 3.5
                                        virtualHW.version = "7"                 # essential for ESX 4.0
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


???                               <=>   guestOS = "<value>"                     # essential, FIXME: not representable
def->id = <value>                 <=>   ???                                     # not representable
def->uuid = <value>               <=>   uuid.bios = "<value>"
def->name = <value>               <=>   displayName = "<value>"
def->maxmem = <value kilobyte>    <=>   memsize = "<value megabyte>"            # must be a multiple of 4, defaults to 32
def->memory = <value kilobyte>    <=>   sched.mem.max = "<value megabyte>"      # defaults to "unlimited" -> def->memory = def->maxmem
def->vcpus = <value>              <=>   numvcpus = "<value>"                    # must be 1 or a multiple of 2, defaults to 1
def->cpumask = <uint list>        <=>   sched.cpu.affinity = "<uint list>"



################################################################################
## os ##########################################################################

def->os

->type = "hvm"
M
Matthias Bolte 已提交
63
->arch = <arch>                   <=>   guestOS = "<value>"                     # if <value> ends with -64 than <arch> is x86_64, otherwise <arch> is i686
64 65 66 67 68 69 70 71 72 73 74 75 76 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
->machine
->nBootDevs
->bootDevs
->init
->kernel
->initrd
->cmdline
->root
->loader
->bootloader
->bootloaderArgs



################################################################################
## disks #######################################################################

                                        scsi[0..3]:[0..6,8..15] -> <controller>:<id>
                                        ide[0..1]:[0..1]        -> <controller>:<id>
                                        floppy[0..1]            -> <controller>

def->disks[0]...

## disks: scsi hard drive from .vmdk image #####################################

                                        scsi0.present = "true"                  # defaults to "false"
                                        scsi0:0.present = "true"                # defaults to "false"
                                        scsi0:0.startConnected = "true"         # defaults to "true"

???                               <=>   scsi0:0.mode = "persistent"             # defaults to "persistent"
                                        scsi0:0.mode = "undoable"
                                        scsi0:0.mode = "independent-persistent"
                                        scsi0:0.mode = "independent-nonpersistent"

...
->type = _DISK_TYPE_FILE          <=>   scsi0:0.deviceType = "scsi-hardDisk"    # defaults to ?
->device = _DISK_DEVICE_DISK      <=>   scsi0:0.deviceType = "scsi-hardDisk"    # defaults to ?
->bus = _DISK_BUS_SCSI
->src = <value>.vmdk              <=>   scsi0:0.fileName = "<value>.vmdk"
M
Matthias Bolte 已提交
103
->dst = sd[<controller> * 15 + <id> mapped to [a-z]+]
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
->driverName = <driver>           <=>   scsi0.virtualDev = "<driver>"           # default depends on guestOS value
->driverType
->cachemode                       <=>   scsi0:0.writeThrough = "<value>"        # defaults to false, true -> _DISK_CACHE_WRITETHRU, false _DISK_CACHE_DEFAULT
->readonly
->shared
->slotnum


## disks: ide hard drive from .vmdk image ######################################

                                        ide0:0.present = "true"                 # defaults to "false"
                                        ide0:0.startConnected = "true"          # defaults to "true"

???                               <=>   ide0:0.mode = "persistent"              # defaults to "persistent"
                                        ide0:0.mode = "undoable"
                                        ide0:0.mode = "independent-persistent"
                                        ide0:0.mode = "independent-nonpersistent"

...
->type = _DISK_TYPE_FILE          <=>   ide0:0.deviceType = "ata-hardDisk"      # defaults to ?
->device = _DISK_DEVICE_DISK      <=>   ide0:0.deviceType = "ata-hardDisk"      # defaults to ?
->bus = _DISK_BUS_IDE
->src = <value>.vmdk              <=>   ide0:0.fileName = "<value>.vmdk"
->dst = hd[<controller> * 2 + <id> mapped to [a-z]+]
->driverName
->driverType
->cachemode                       <=>   ide0:0.writeThrough = "<value>"         # defaults to false, true -> _DISK_CACHE_WRITETHRU, false _DISK_CACHE_DEFAULT
->readonly
->shared
->slotnum


## disks: scsi cdrom from .iso image ###########################################

                                        scsi0.present = "true"                  # defaults to "false"
                                        scsi0:0.present = "true"                # defaults to "false"
                                        scsi0:0.startConnected = "true"         # defaults to "true"

...
->type = _DISK_TYPE_FILE          <=>   scsi0:0.deviceType = "cdrom-image"      # defaults to ?
->device = _DISK_DEVICE_CDROM     <=>   scsi0:0.deviceType = "cdrom-image"      # defaults to ?
->bus = _DISK_BUS_SCSI
->src = <value>.iso               <=>   scsi0:0.fileName = "<value>.iso"
M
Matthias Bolte 已提交
147
->dst = sd[<controller> * 15 + <id> mapped to [a-z]+]
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
->driverName = <driver>           <=>   scsi0.virtualDev = "<driver>"           # default depends on guestOS value
->driverType
->cachemode
->readonly
->shared
->slotnum


## disks: ide cdrom from .iso image ############################################

                                        ide0:0.present = "true"                 # defaults to "false"
                                        ide0:0.startConnected = "true"          # defaults to "true"

...
->type = _DISK_TYPE_FILE          <=>   ide0:0.deviceType = "cdrom-image"       # defaults to ?
->device = _DISK_DEVICE_CDROM     <=>   ide0:0.deviceType = "cdrom-image"       # defaults to ?
->bus = _DISK_BUS_IDE
->src = <value>.iso               <=>   ide0:0.fileName = "<value>.iso"
->dst = hd[<controller> * 2 + <id> mapped to [a-z]+]
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum


## disks: scsi cdrom from host device ##########################################

                                        scsi0.present = "true"                  # defaults to "false"
                                        scsi0:0.present = "true"                # defaults to "false"
                                        scsi0:0.startConnected = "true"         # defaults to "true"

...
->type = _DISK_TYPE_BLOCK         <=>   scsi0:0.deviceType = "atapi-cdrom"      # defaults to ?
->device = _DISK_DEVICE_CDROM     <=>   scsi0:0.deviceType = "atapi-cdrom"      # defaults to ?
->bus = _DISK_BUS_SCSI
->src = <value>                   <=>   scsi0:0.fileName = "<value>"            # e.g. "/dev/scd0" ?
->dst = sd[<controller> * 16 + <id> mapped to [a-z]+]
->driverName = <driver>           <=>   scsi0.virtualDev = "<driver>"           # default depends on guestOS value
->driverType
->cachemode
->readonly
->shared
->slotnum


## disks: ide cdrom from host device ###########################################

                                        ide0:0.present = "true"                 # defaults to "false"
                                        ide0:0.startConnected = "true"          # defaults to "true"
                                        ide0:0.clientDevice = "false"           # defaults to "false"

...
->type = _DISK_TYPE_BLOCK         <=>   ide0:0.deviceType = "atapi-cdrom"       # defaults to ?
->device = _DISK_DEVICE_CDROM     <=>   ide0:0.deviceType = "atapi-cdrom"       # defaults to ?
->bus = _DISK_BUS_IDE
->src = <value>                   <=>   ide0:0.fileName = "<value>"             # e.g. "/dev/scd0"
->dst = hd[<controller> * 2 + <id> mapped to [a-z]+]
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum


## disks: floppy from .flp image ###############################################

                                        floppy0.present = "true"                # defaults to "false"
                                        floppy0.startConnected = "true"         # defaults to "true"
                                        floppy0.clientDevice = "false"          # defaults to "false"

...
->type = _DISK_TYPE_FILE          <=>   floppy0.fileType = "file"               # defaults to ?
->device = _DISK_DEVICE_FLOPPY
->bus = _DISK_BUS_FDC
->src = <value>.flp               <=>   floppy0.fileName = "<value>.flp"
->dst = fd[<controller> mapped to [a-z]+]
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum


## disks: floppy from host device ##############################################

                                        floppy0.present = "true"                # defaults to "false"
                                        floppy0.startConnected = "true"         # defaults to "true"
                                        floppy0.clientDevice = "false"          # defaults to "false"

...
->type = _DISK_TYPE_BLOCK         <=>   floppy0.fileType = "device"             # defaults to ?
->device = _DISK_DEVICE_FLOPPY
->bus = _DISK_BUS_FDC
->src = <value>                   <=>   floppy0.fileName = "<value>"            # e.g. "/dev/fd0"
->dst = fd[<controller> mapped to [a-z]+]
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum



################################################################################
## nets ########################################################################

                                        ethernet[0..3] -> <controller>

                                        ethernet0.present = "true"              # defaults to "false"
                                        ethernet0.startConnected = "true"       # defaults to "true"

                                        ethernet0.networkName = "VM Network"    # FIXME

def->nets[0]...
->model = <model>                 <=>   ethernet0.virtualDev = "<model>"        # default depends on guestOS value


                                        ethernet0.addressType = "generated"     # default to "generated"
->mac = <value>                   <=>   ethernet0.generatedAddress = "<value>"
272
                                        ethernet0.generatedAddressOffset = "0"  # ?
273 274 275 276 277


                                        ethernet0.addressType = "static"        # default to "generated"
->mac = <value>                   <=>   ethernet0.address = "<value>"

278 279 280 281

                                        ethernet0.addressType = "vpx"           # default to "generated"
->mac = <value>                   <=>   ethernet0.generatedAddress = "<value>"

282 283 284 285 286

                                        ethernet0.addressType = "static"        # default to "generated"
->mac = <value>                   <=>   ethernet0.address = "<value>"
                                        ethernet0.checkMACAddress = "false"     # mac address outside the VMware prefixes

M
Matthias Bolte 已提交
287
                                                                                # 00:0c:29 prefix for autogenerated mac's -> ethernet0.addressType = "generated"
288
                                                                                # 00:50:56 prefix for manual configured mac's
M
Matthias Bolte 已提交
289
                                                                                #          00:50:56:00:00:00 - 00:50:56:3f:ff:ff -> ethernet0.addressType = "static"
290
                                                                                #          00:50:56:80:00:00 - 00:50:56:bf:ff:ff -> ethernet0.addressType = "vpx"
291 292 293 294 295 296 297
                                                                                # 00:05:69 old prefix from esx 1.5


## nets: bridged ###############################################################

...
->type = _NET_TYPE_BRIDGE         <=>   ethernet0.connectionType = "bridged"    # defaults to "bridged"
M
Matthias Bolte 已提交
298
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
299 300 301 302


## nets: hostonly ##############################################################

M
Matthias Bolte 已提交
303
...                                                                             # FIXME: Investigate if ESX supports this
304 305 306 307 308
->type = _NET_TYPE_NETWORK        <=>   ethernet0.connectionType = "hostonly"   # defaults to "bridged"


## nets: nat ###################################################################

M
Matthias Bolte 已提交
309
...                                                                             # FIXME: Investigate if ESX supports this
310 311 312 313 314 315 316
->type = _NET_TYPE_USER           <=>   ethernet0.connectionType = "nat"        # defaults to "bridged"


## nets: custom ################################################################

...
->type = _NET_TYPE_BRIDGE         <=>   ethernet0.connectionType = "custom"     # defaults to "bridged"
M
Matthias Bolte 已提交
317 318
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
->ifname = <value>                <=>   ethernet0.vnet = "<value>"
319 320 321 322 323 324 325 326 327 328 329 330



################################################################################
## serials #####################################################################

                                        serial[0..3] -> <port>

                                        serial0.present = "true"                # defaults to "false"
                                        serial0.startConnected = "true"         # defaults to "true"

def->serials[0]...
331
->target.port = <port>
332 333 334 335 336 337 338


## serials: device #############################################################

->type = _CHR_TYPE_DEV            <=>   serial0.fileType = "device"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "/dev/ttyS0"
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
339
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
340 341 342 343 344 345 346


## serials: file ###############################################################

->type = _CHR_TYPE_FILE           <=>   serial0.fileType = "file"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.file"
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
347
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
348 349 350 351 352 353


## serials: pipe, far end -> app ###############################################

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
354
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
355
???                               <=>   serial0.tryNoRxLoss = "true"            # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
356
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
357 358 359

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
360
???                               <=>   serial0.pipe.endPoint = "server"        # defaults to ?, FIXME: not representable
361
???                               <=>   serial0.tryNoRxLoss = "true"            # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
362
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
363 364 365 366 367 368


## serials: pipe, far end -> vm ################################################

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
369
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
370
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
371
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
372 373 374

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
375
???                               <=>   serial0.pipe.endPoint = "server"        # defaults to ?, FIXME: not representable
376
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
377
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
378 379 380 381 382 383 384 385 386 387 388 389



################################################################################
## parallels ###################################################################

                                        parallel[0..2] -> <port>

                                        parallel0.present = "true"              # defaults to "false"
                                        parallel0.startConnected = "true"       # defaults to "true"

def->parallels[0]...
390
->target.port = <port>
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405


## parallels: device #############################################################

->type = _CHR_TYPE_DEV            <=>   parallel0.fileType = "device"
->data.file.path = <value>        <=>   parallel0.fileName = "<value>"          # e.g. "/dev/parport0"
???                               <=>   parallel0.bidirectional = "<value>"     # defaults to ?, FIXME: not representable


## parallels: file #############################################################

->type = _CHR_TYPE_FILE           <=>   parallel0.fileType = "file"
->data.file.path = <value>        <=>   parallel0.fileName = "<value>"          # e.g. "parallel0.file"
???                               <=>   parallel0.bidirectional = "<value>"     # must be "false" for fileType = "file", FIXME: not representable

M
Matthias Bolte 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420


################################################################################
## sound #######################################################################

                                        sound.present = "true"                  # defaults to "false"
                                        sound.startConnected = "true"           # defaults to "true"
                                        sound.autodetect = "true"
                                        sound.fileName = "-1"

                                        FIXME: Investigate if ESX supports this,
                                               at least the VI Client GUI has no
                                               options to add a sound device, but
                                               the VI API contains a VirtualSoundCard

421 422 423 424 425
*/

#define VIR_FROM_THIS VIR_FROM_ESX

#define ESX_BUILD_VMX_NAME(_suffix)                                           \
426
    snprintf(_suffix##_name, sizeof(_suffix##_name), "%s."#_suffix, prefix);
427 428 429



M
Matthias Bolte 已提交
430
int
431
esxVMX_SCSIDiskNameToControllerAndID(const char *name, int *controller, int *id)
M
Matthias Bolte 已提交
432 433 434 435
{
    int idx;

    if (! STRPREFIX(name, "sd")) {
436 437 438
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'sd'"));
M
Matthias Bolte 已提交
439 440 441 442 443 444
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
445
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
446
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
447 448 449 450 451
        return -1;
    }

    /* Each of the 4 SCSI controllers offers 15 IDs for devices */
    if (idx >= (4 * 15)) {
452
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
453
                  _("SCSI disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
        return -1;
    }

    *controller = idx / 15;
    *id = idx % 15;

    /* Skip the controller ifself with ID 7 */
    if (*id >= 7) {
        ++(*id);
    }

    return 0;
}



int
471
esxVMX_IDEDiskNameToControllerAndID(const char *name, int *controller, int *id)
M
Matthias Bolte 已提交
472 473 474 475
{
    int idx;

    if (! STRPREFIX(name, "hd")) {
476 477 478
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'hd'"));
M
Matthias Bolte 已提交
479 480 481 482 483 484
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
485
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
486
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
487 488 489 490 491
        return -1;
    }

    /* Each of the 2 IDE controllers offers 2 IDs for devices */
    if (idx >= (2 * 2)) {
492
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
493
                  _("IDE disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
494 495 496 497 498 499 500 501 502 503 504 505
        return -1;
    }

    *controller = idx / 2;
    *id = idx % 2;

    return 0;
}



int
506
esxVMX_FloppyDiskNameToController(const char *name, int *controller)
M
Matthias Bolte 已提交
507 508 509 510
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
511 512 513
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
514 515 516 517 518 519
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
520
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
521
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
522 523 524 525
        return -1;
    }

    if (idx >= 2) {
526
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
527
                  _("Floppy disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
528 529 530 531 532 533 534 535 536 537 538
        return -1;
    }

    *controller = idx;

    return 0;
}



int
539 540
esxVMX_GatherSCSIControllers(virDomainDefPtr def, char *virtualDev[4],
                             int present[4])
M
Matthias Bolte 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554
{
    virDomainDiskDefPtr disk;
    int i, controller, id;

    /* Check for continuous use of the same virtualDev per SCSI controller */
    for (i = 0; i < def->ndisks; ++i) {
        disk = def->disks[i];

        if (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI) {
            continue;
        }

        if (disk->driverName != NULL &&
            STRCASENEQ(disk->driverName, "buslogic") &&
555 556
            STRCASENEQ(disk->driverName, "lsilogic") &&
            STRCASENEQ(disk->driverName, "lsisas1068")) {
557
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
558
                      _("Expecting domain XML entry 'devices/disk/target' to "
559
                        "be 'buslogic' or 'lsilogic' or 'lsisas1068' but found '%s'"),
M
Matthias Bolte 已提交
560 561 562 563
                      disk->driverName);
            return -1;
        }

564 565
        if (esxVMX_SCSIDiskNameToControllerAndID(disk->dst, &controller,
                                                 &id) < 0) {
M
Matthias Bolte 已提交
566 567 568 569 570 571 572
            return -1;
        }

        present[controller] = 1;

        if (virtualDev[controller] == NULL) {
            virtualDev[controller] = disk->driverName;
573 574
        } else if (disk->driverName == NULL ||
                   STRCASENEQ(virtualDev[controller], disk->driverName)) {
575
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
576 577
                      _("Inconsistent driver usage ('%s' is not '%s') on SCSI "
                        "controller index %d"), virtualDev[controller],
578
                      disk->driverName ? disk->driverName : "?", controller);
M
Matthias Bolte 已提交
579 580 581 582 583 584 585 586 587
            return -1;
        }
    }

    return 0;
}



M
Matthias Bolte 已提交
588
char *
589
esxVMX_AbsolutePathToDatastoreRelatedPath(esxVI_Context *ctx,
M
Matthias Bolte 已提交
590 591
                                          const char *absolutePath)
{
592 593 594
    char *copyOfAbsolutePath = NULL;
    char *tmp = NULL;
    char *saveptr = NULL;
M
Matthias Bolte 已提交
595 596 597 598 599 600 601
    char *datastoreRelatedPath = NULL;
    char *preliminaryDatastoreName = NULL;
    char *directoryAndFileName = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_ObjectContent *datastore = NULL;
    const char *datastoreName = NULL;

602 603 604 605 606 607 608 609
    if (esxVI_String_DeepCopyValue(&copyOfAbsolutePath, absolutePath) < 0) {
        goto failure;
    }

    /* Expected format: '/vmfs/volumes/<datastore>/<path>' */
    if ((tmp = STRSKIP(copyOfAbsolutePath, "/vmfs/volumes/")) == NULL ||
        (preliminaryDatastoreName = strtok_r(tmp, "/", &saveptr)) == NULL ||
        (directoryAndFileName = strtok_r(NULL, "", &saveptr)) == NULL) {
610
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
611 612
                  _("Absolute path '%s' doesn't have expected format "
                    "'/vmfs/volumes/<datastore>/<path>'"), absolutePath);
M
Matthias Bolte 已提交
613 614 615 616
        goto failure;
    }

    if (ctx != NULL) {
617
        if (esxVI_LookupDatastoreByName(ctx, preliminaryDatastoreName,
M
Matthias Bolte 已提交
618
                                        NULL, &datastore,
M
Matthias Bolte 已提交
619
                                        esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
620 621 622
            goto failure;
        }

623 624 625 626 627 628
        if (datastore != NULL) {
            for (dynamicProperty = datastore->propSet; dynamicProperty != NULL;
                 dynamicProperty = dynamicProperty->_next) {
                if (STREQ(dynamicProperty->name, "summary.accessible")) {
                    /* Ignore it */
                } else if (STREQ(dynamicProperty->name, "summary.name")) {
629
                    if (esxVI_AnyType_ExpectType(dynamicProperty->val,
630 631 632 633 634 635 636 637 638 639
                                                 esxVI_Type_String) < 0) {
                        goto failure;
                    }

                    datastoreName = dynamicProperty->val->string;
                    break;
                } else if (STREQ(dynamicProperty->name, "summary.url")) {
                    /* Ignore it */
                } else {
                    VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
M
Matthias Bolte 已提交
640 641 642 643 644
                }
            }
        }

        if (datastoreName == NULL) {
645 646 647 648 649
            VIR_WARN("Could not retrieve datastore name for absolute "
                     "path '%s', falling back to preliminary name '%s'",
                     absolutePath, preliminaryDatastoreName);

            datastoreName = preliminaryDatastoreName;
M
Matthias Bolte 已提交
650 651 652 653 654 655 656
        }
    } else {
        datastoreName = preliminaryDatastoreName;
    }

    if (virAsprintf(&datastoreRelatedPath, "[%s] %s", datastoreName,
                    directoryAndFileName) < 0) {
657
        virReportOOMError();
M
Matthias Bolte 已提交
658 659 660 661 662 663
        goto failure;
    }

    /* FIXME: Check if referenced path/file really exists */

  cleanup:
664
    VIR_FREE(copyOfAbsolutePath);
M
Matthias Bolte 已提交
665 666 667 668 669 670 671 672 673 674 675 676
    esxVI_ObjectContent_Free(&datastore);

    return datastoreRelatedPath;

  failure:
    VIR_FREE(datastoreRelatedPath);

    goto cleanup;
}



M
Matthias Bolte 已提交
677 678 679 680
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * VMX -> Domain XML
 */

M
Matthias Bolte 已提交
681
char *
682 683
esxVMX_ParseFileName(esxVI_Context *ctx, const char *fileName,
                     const char *datastoreName, const char *directoryName)
M
Matthias Bolte 已提交
684 685 686 687 688
{
    char *src = NULL;

    if (STRPREFIX(fileName, "/vmfs/volumes/")) {
        /* Found absolute path referencing a file inside a datastore */
689
        return esxVMX_AbsolutePathToDatastoreRelatedPath(ctx, fileName);
M
Matthias Bolte 已提交
690 691 692 693 694
    } else if (STRPREFIX(fileName, "/")) {
        /* Found absolute path referencing a file outside a datastore */
        src = strdup(fileName);

        if (src == NULL) {
695
            virReportOOMError();
M
Matthias Bolte 已提交
696 697 698 699 700 701 702 703
            return NULL;
        }

        /* FIXME: Check if referenced path/file really exists */

        return src;
    } else if (strchr(fileName, '/') != NULL) {
        /* Found relative path, this is not supported */
704
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
705 706
                  _("Found relative path '%s' in VMX file, this is not "
                    "supported"), fileName);
M
Matthias Bolte 已提交
707 708 709 710 711
        return NULL;
    } else {
        /* Found single file name referencing a file inside a datastore */
        if (virAsprintf(&src, "[%s] %s/%s", datastoreName, directoryName,
                        fileName) < 0) {
712
            virReportOOMError();
M
Matthias Bolte 已提交
713 714 715 716 717 718 719 720 721 722 723
            return NULL;
        }

        /* FIXME: Check if referenced path/file really exists */

        return src;
    }
}



724
virDomainDefPtr
725
esxVMX_ParseConfig(esxVI_Context *ctx, const char *vmx,
M
Matthias Bolte 已提交
726
                   const char *datastoreName, const char *directoryName,
727
                   esxVI_APIVersion apiVersion)
728 729 730 731 732 733 734 735 736
{
    virConfPtr conf = NULL;
    virDomainDefPtr def = NULL;
    long long config_version = 0;
    long long virtualHW_version = 0;
    long long memsize = 0;
    long long memory = 0;
    long long numvcpus = 0;
    char *sched_cpu_affinity = NULL;
M
Matthias Bolte 已提交
737
    char *guestOS = NULL;
738 739
    int controller;
    int port;
M
Matthias Bolte 已提交
740
    int present; // boolean
741 742 743 744 745 746 747 748 749 750
    char *scsi_virtualDev = NULL;
    int id;

    conf = virConfReadMem(vmx, strlen(vmx), VIR_CONF_FLAG_VMX_FORMAT);

    if (conf == NULL) {
        return NULL;
    }

    if (VIR_ALLOC(def) < 0) {
751
        virReportOOMError();
752 753 754 755 756 757
        goto failure;
    }

    def->virtType = VIR_DOMAIN_VIRT_VMWARE; /* FIXME: maybe add VIR_DOMAIN_VIRT_ESX ? */
    def->id = -1;

M
Matthias Bolte 已提交
758
    /* vmx:config.version */
759 760
    if (esxUtil_GetConfigLong(conf, "config.version", &config_version, 0,
                              0) < 0) {
761 762 763 764
        goto failure;
    }

    if (config_version != 8) {
765
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
766 767
                  _("Expecting VMX entry 'config.version' to be 8 but found "
                    "%lld"), config_version);
768 769 770
        goto failure;
    }

M
Matthias Bolte 已提交
771
    /* vmx:virtualHW.version */
772 773
    if (esxUtil_GetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
                              0) < 0) {
774 775 776
        goto failure;
    }

777 778
    switch (apiVersion) {
      case esxVI_APIVersion_25:
779
        if (virtualHW_version != 4) {
780
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
781 782 783
                      _("Expecting VMX entry 'virtualHW.version' to be 4 for "
                        "VI API version 2.5 but found %lld"),
                      virtualHW_version);
784 785 786 787 788
            goto failure;
        }

        break;

789
      case esxVI_APIVersion_40:
790
        if (virtualHW_version != 4 && virtualHW_version != 7) {
791
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
792 793 794
                      _("Expecting VMX entry 'virtualHW.version' to be 4 or 7 "
                        "for VI API version 4.0 but found %lld"),
                      virtualHW_version);
795 796 797 798 799
            goto failure;
        }

        break;

800
      case esxVI_APIVersion_Unknown:
801
        if (virtualHW_version != 4 && virtualHW_version != 7) {
802
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
803 804
                      _("Expecting VMX entry 'virtualHW.version' to be 4 or 7 "
                        "but found %lld"), virtualHW_version);
805 806 807 808 809 810
            goto failure;
        }

        break;

      default:
811 812
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting VI API version 2.5 or 4.0"));
813 814 815
        goto failure;
    }

M
Matthias Bolte 已提交
816
    /* vmx:uuid.bios -> def:uuid */
817
    /* FIXME: Need to handle 'uuid.action = "create"' */
818
    if (esxUtil_GetConfigUUID(conf, "uuid.bios", def->uuid, 1) < 0) {
819 820 821
        goto failure;
    }

M
Matthias Bolte 已提交
822
    /* vmx:displayName -> def:name */
823
    if (esxUtil_GetConfigString(conf, "displayName", &def->name, 1) < 0) {
824 825 826
        goto failure;
    }

M
Matthias Bolte 已提交
827
    /* vmx:memsize -> def:maxmem */
828
    if (esxUtil_GetConfigLong(conf, "memsize", &memsize, 32, 1) < 0) {
829 830 831 832
        goto failure;
    }

    if (memsize <= 0 || memsize % 4 != 0) {
833
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
834 835
                  _("Expecting VMX entry 'memsize' to be an unsigned "
                    "integer (multiple of 4) but found %lld"), memsize);
836 837 838 839 840
        goto failure;
    }

    def->maxmem = memsize * 1024; /* Scale from megabytes to kilobytes */

M
Matthias Bolte 已提交
841
    /* vmx:sched.mem.max -> def:memory */
842
    if (esxUtil_GetConfigLong(conf, "sched.mem.max", &memory, memsize, 1) < 0) {
843 844 845 846 847 848 849 850 851 852 853 854 855
        goto failure;
    }

    if (memory < 0) {
        memory = memsize;
    }

    def->memory = memory * 1024; /* Scale from megabytes to kilobytes */

    if (def->memory > def->maxmem) {
        def->memory = def->maxmem;
    }

M
Matthias Bolte 已提交
856
    /* vmx:numvcpus -> def:vcpus */
857
    if (esxUtil_GetConfigLong(conf, "numvcpus", &numvcpus, 1, 1) < 0) {
858 859 860 861
        goto failure;
    }

    if (numvcpus <= 0 || (numvcpus % 2 != 0 && numvcpus != 1)) {
862
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
863 864
                  _("Expecting VMX entry 'numvcpus' to be an unsigned "
                    "integer (1 or a multiple of 2) but found %lld"), numvcpus);
865 866 867 868 869
        goto failure;
    }

    def->vcpus = numvcpus;

M
Matthias Bolte 已提交
870
    /* vmx:sched.cpu.affinity -> def:cpumask */
871
    // VirtualMachine:config.cpuAffinity.affinitySet
872 873
    if (esxUtil_GetConfigString(conf, "sched.cpu.affinity", &sched_cpu_affinity,
                                1) < 0) {
874 875 876 877 878 879 880 881 882 883
        goto failure;
    }

    if (sched_cpu_affinity != NULL && STRNEQ(sched_cpu_affinity, "all")) {
        const char *current = sched_cpu_affinity;
        int number, count = 0;

        def->cpumasklen = 0;

        if (VIR_ALLOC_N(def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) {
884
            virReportOOMError();
885 886 887 888 889 890 891 892 893
            goto failure;
        }

        while (*current != '\0') {
            virSkipSpaces(&current);

            number = virParseNumber(&current);

            if (number < 0) {
894
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
895 896 897
                          _("Expecting VMX entry 'sched.cpu.affinity' to be "
                            "a comma separated list of unsigned integers but "
                            "found '%s'"), sched_cpu_affinity);
898 899 900 901
                goto failure;
            }

            if (number >= VIR_DOMAIN_CPUMASK_LEN) {
902
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
903 904
                          _("VMX entry 'sched.cpu.affinity' contains a %d, "
                            "this value is too large"), number);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
                goto failure;
            }

            if (number + 1 > def->cpumasklen) {
                def->cpumasklen = number + 1;
            }

            def->cpumask[number] = 1;
            ++count;

            virSkipSpaces(&current);

            if (*current == ',') {
                ++current;
            } else if (*current == '\0') {
                break;
            } else {
922
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
923 924 925
                          _("Expecting VMX entry 'sched.cpu.affinity' to be "
                            "a comma separated list of unsigned integers but "
                            "found '%s'"), sched_cpu_affinity);
926 927 928 929 930 931 932
                goto failure;
            }

            virSkipSpaces(&current);
        }

        if (count < numvcpus) {
933
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
934 935 936
                      _("Expecting VMX entry 'sched.cpu.affinity' to contain "
                        "at least as many values as 'numvcpus' (%lld) but "
                        "found only %d value(s)"), numvcpus, count);
937 938 939 940 941 942 943 944 945 946 947 948 949
            goto failure;
        }
    }

    /* def:lifecycle */
    def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
    def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
    def->onCrash = VIR_DOMAIN_LIFECYCLE_DESTROY;

    /* def:os */
    def->os.type = strdup("hvm");

    if (def->os.type == NULL) {
950
        virReportOOMError();
951 952 953
        goto failure;
    }

M
Matthias Bolte 已提交
954
    /* vmx:guestOS -> def:os.arch */
955
    if (esxUtil_GetConfigString(conf, "guestOS", &guestOS, 1) < 0) {
M
Matthias Bolte 已提交
956 957 958
        goto failure;
    }

959
    if (guestOS != NULL && virFileHasSuffix(guestOS, "-64")) {
M
Matthias Bolte 已提交
960 961 962 963 964 965
        def->os.arch = strdup("x86_64");
    } else {
        def->os.arch = strdup("i686");
    }

    if (def->os.arch == NULL) {
966
        virReportOOMError();
M
Matthias Bolte 已提交
967 968 969
        goto failure;
    }

970 971 972 973 974 975 976 977
/*
    def->emulator
    def->features*/

/*
    def->localtime*/

    /* def:graphics */
M
Matthias Bolte 已提交
978
    if (VIR_ALLOC_N(def->graphics, 1) < 0) {
979
        virReportOOMError();
M
Matthias Bolte 已提交
980 981 982 983 984
        goto failure;
    }

    def->ngraphics = 0;

985
    if (esxVMX_ParseVNC(conf, &def->graphics[def->ngraphics]) < 0) {
M
Matthias Bolte 已提交
986 987 988 989 990 991
        goto failure;
    }

    if (def->graphics[def->ngraphics] != NULL) {
        ++def->ngraphics;
    }
992

M
Matthias Bolte 已提交
993 994
    /* def:disks: 4 * 15 scsi + 2 * 2 ide + 2 floppy = 66 */
    if (VIR_ALLOC_N(def->disks, 66) < 0) {
995
        virReportOOMError();
996 997 998 999 1000 1001 1002 1003 1004
        goto failure;
    }

    def->ndisks = 0;

    /* def:disks (scsi) */
    for (controller = 0; controller < 4; ++controller) {
        VIR_FREE(scsi_virtualDev);

1005 1006
        if (esxVMX_ParseSCSIController(conf, controller, &present,
                                       &scsi_virtualDev) < 0) {
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
            goto failure;
        }

        if (! present) {
            continue;
        }

        for (id = 0; id < 16; ++id) {
            if (id == 7) {
                /*
                 * SCSI ID 7 is assigned to the SCSI controller and cannot be
                 * used for disk devices.
                 */
                continue;
            }

1023
            if (esxVMX_ParseDisk(ctx, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
1024
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, id,
M
Matthias Bolte 已提交
1025
                                 scsi_virtualDev, datastoreName, directoryName,
1026 1027 1028 1029 1030 1031 1032 1033 1034
                                 &def->disks[def->ndisks]) < 0) {
                goto failure;
            }

            if (def->disks[def->ndisks] != NULL) {
                ++def->ndisks;
                continue;
            }

1035
            if (esxVMX_ParseDisk(ctx, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1036
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, id,
M
Matthias Bolte 已提交
1037
                                 scsi_virtualDev, datastoreName, directoryName,
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
                                 &def->disks[def->ndisks]) < 0) {
                goto failure;
            }

            if (def->disks[def->ndisks] != NULL) {
                ++def->ndisks;
            }
        }
    }

    /* def:disks (ide) */
    for (controller = 0; controller < 2; ++controller) {
        for (id = 0; id < 2; ++id) {
1051
            if (esxVMX_ParseDisk(ctx, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
1052
                                 VIR_DOMAIN_DISK_BUS_IDE, controller, id,
M
Matthias Bolte 已提交
1053 1054
                                 NULL, datastoreName, directoryName,
                                 &def->disks[def->ndisks]) < 0) {
1055 1056 1057 1058 1059 1060 1061 1062
                goto failure;
            }

            if (def->disks[def->ndisks] != NULL) {
                ++def->ndisks;
                continue;
            }

1063
            if (esxVMX_ParseDisk(ctx, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1064
                                 VIR_DOMAIN_DISK_BUS_IDE, controller, id,
M
Matthias Bolte 已提交
1065 1066
                                 NULL, datastoreName, directoryName,
                                 &def->disks[def->ndisks]) < 0) {
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
                goto failure;
            }

            if (def->disks[def->ndisks] != NULL) {
                ++def->ndisks;
            }
        }
    }

    /* def:disks (floppy) */
    for (controller = 0; controller < 2; ++controller) {
1078
        if (esxVMX_ParseDisk(ctx, conf, VIR_DOMAIN_DISK_DEVICE_FLOPPY,
1079
                             VIR_DOMAIN_DISK_BUS_FDC, controller, -1, NULL,
M
Matthias Bolte 已提交
1080
                             datastoreName, directoryName,
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
                             &def->disks[def->ndisks]) < 0) {
            goto failure;
        }

        if (def->disks[def->ndisks] != NULL) {
            ++def->ndisks;
        }
    }

    /* def:fss */
    /* FIXME */

    /* def:nets */
    if (VIR_ALLOC_N(def->nets, 4) < 0) {
1095
        virReportOOMError();
1096 1097 1098 1099 1100 1101
        goto failure;
    }

    def->nnets = 0;

    for (controller = 0; controller < 4; ++controller) {
1102
        if (esxVMX_ParseEthernet(conf, controller,
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
                                 &def->nets[def->nnets]) < 0) {
            goto failure;
        }

        if (def->nets[def->nnets] != NULL) {
            ++def->nnets;
        }
    }

    /* def:inputs */
    /* FIXME */

    /* def:sounds */
    /* FIXME */

    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    if (VIR_ALLOC_N(def->serials, 4) < 0) {
1123
        virReportOOMError();
1124 1125 1126 1127 1128 1129
        goto failure;
    }

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1130 1131
        if (esxVMX_ParseSerial(ctx, conf, port, datastoreName,
                               directoryName,
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
                               &def->serials[def->nserials]) < 0) {
            goto failure;
        }

        if (def->serials[def->nserials] != NULL) {
            ++def->nserials;
        }
    }

    /* def:parallels */
    if (VIR_ALLOC_N(def->parallels, 3) < 0) {
1143
        virReportOOMError();
1144 1145 1146 1147 1148 1149
        goto failure;
    }

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1150 1151
        if (esxVMX_ParseParallel(ctx, conf, port, datastoreName,
                                 directoryName,
1152 1153 1154 1155 1156 1157 1158 1159 1160
                                 &def->parallels[def->nparallels]) < 0) {
            goto failure;
        }

        if (def->parallels[def->nparallels] != NULL) {
            ++def->nparallels;
        }
    }

M
Matthias Bolte 已提交
1161
  cleanup:
1162 1163
    virConfFree(conf);
    VIR_FREE(sched_cpu_affinity);
M
Matthias Bolte 已提交
1164
    VIR_FREE(guestOS);
1165 1166 1167 1168
    VIR_FREE(scsi_virtualDev);

    return def;

M
Matthias Bolte 已提交
1169
  failure:
1170 1171 1172 1173 1174 1175 1176 1177
    virDomainDefFree(def);
    def = NULL;

    goto cleanup;
}



M
Matthias Bolte 已提交
1178
int
1179
esxVMX_ParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1180 1181 1182 1183 1184
{
    int enabled = 0; // boolean
    long long port = 0;

    if (def == NULL || *def != NULL) {
1185
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1186 1187 1188
        return -1;
    }

1189 1190
    if (esxUtil_GetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
                                 0, 1) < 0) {
M
Matthias Bolte 已提交
1191 1192 1193 1194 1195 1196 1197 1198
        return -1;
    }

    if (! enabled) {
        return 0;
    }

    if (VIR_ALLOC(*def) < 0) {
1199
        virReportOOMError();
M
Matthias Bolte 已提交
1200 1201 1202 1203 1204
        goto failure;
    }

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1205 1206 1207
    if (esxUtil_GetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
                               1) < 0 ||
        esxUtil_GetConfigString(conf, "RemoteDisplay.vnc.ip",
M
Matthias Bolte 已提交
1208
                                &(*def)->data.vnc.listenAddr, 1) < 0 ||
1209
        esxUtil_GetConfigString(conf, "RemoteDisplay.vnc.keymap",
M
Matthias Bolte 已提交
1210
                                &(*def)->data.vnc.keymap, 1) < 0 ||
1211
        esxUtil_GetConfigString(conf, "RemoteDisplay.vnc.password",
M
Matthias Bolte 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
                                &(*def)->data.vnc.passwd, 1) < 0) {
        goto failure;
    }

    if (port < 0) {
        VIR_WARN0("VNC is enabled but VMX entry 'RemoteDisplay.vnc.port' "
                  "is missing, the VNC port is unknown");

        (*def)->data.vnc.port = 0;
        (*def)->data.vnc.autoport = 1;
    } else {
        if (port < 5900 || port > 5964) {
            VIR_WARN("VNC port %lld it out of [5900..5964] range", port);
        }

        (*def)->data.vnc.port = port;
        (*def)->data.vnc.autoport = 0;
    }

    return 0;

  failure:
    virDomainGraphicsDefFree(*def);
    *def = NULL;

    return -1;
}



1242
int
1243 1244
esxVMX_ParseSCSIController(virConfPtr conf, int controller, int *present,
                           char **virtualDev)
1245 1246 1247 1248 1249
{
    char present_name[32];
    char virtualDev_name[32];

    if (virtualDev == NULL || *virtualDev != NULL) {
1250
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1251
        return -1;
1252 1253 1254
    }

    if (controller < 0 || controller > 3) {
1255
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1256
                  _("SCSI controller index %d out of [0..3] range"),
1257
                  controller);
1258
        return -1;
1259 1260
    }

1261 1262 1263
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1264

1265
    if (esxUtil_GetConfigBoolean(conf, present_name, present, 0, 1) < 0) {
1266 1267 1268 1269 1270 1271 1272
        goto failure;
    }

    if (! *present) {
        return 0;
    }

1273
    if (esxUtil_GetConfigString(conf, virtualDev_name, virtualDev, 1) < 0) {
1274 1275 1276 1277 1278
        goto failure;
    }

    if (*virtualDev != NULL &&
        STRCASENEQ(*virtualDev, "buslogic") &&
1279 1280
        STRCASENEQ(*virtualDev, "lsilogic") &&
        STRCASENEQ(*virtualDev, "lsisas1068")) {
1281
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1282 1283
                  _("Expecting VMX entry '%s' to be 'buslogic' or 'lsilogic' or "
                    "'lsisas1068' but found '%s'"), virtualDev_name, *virtualDev);
1284 1285 1286 1287 1288
        goto failure;
    }

    return 0;

M
Matthias Bolte 已提交
1289
  failure:
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
    VIR_FREE(*virtualDev);

    return -1;
}



/*
struct _virDomainDiskDef {
    int type;               // partly done
    int device;             // done
    int bus;                // done
    char *src;              // done
    char *dst;              // done
    char *driverName;       // done
    char *driverType;
    int cachemode;          // done
    unsigned int readonly : 1;
    unsigned int shared : 1;
    int slotnum;
};*/

int
1313 1314 1315 1316
esxVMX_ParseDisk(esxVI_Context *ctx, virConfPtr conf, int device, int bus,
                 int controller, int id, const char *virtualDev,
                 const char *datastoreName, const char *directoryName,
                 virDomainDiskDefPtr *def)
1317 1318 1319 1320 1321 1322
{
    /*
     *     device = {VIR_DOMAIN_DISK_DEVICE_DISK, VIR_DOMAIN_DISK_DEVICE_CDROM}
     *        bus = VIR_DOMAIN_DISK_BUS_SCSI
     * controller = [0..3]
     *         id = [0..6,8..15]
1323
     * virtualDev = {'buslogic', 'lsilogic', 'lsisas1068'}
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
     *
     *     device = {VIR_DOMAIN_DISK_DEVICE_DISK, VIR_DOMAIN_DISK_DEVICE_CDROM}
     *        bus = VIR_DOMAIN_DISK_BUS_IDE
     * controller = [0..1]
     *         id = [0..1]
     * virtualDev = NULL
     *
     *     device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *        bus = VIR_DOMAIN_DISK_BUS_FDC
     * controller = [0..1]
     *         id = -1
     * virtualDev = NULL
     */

    int result = 0;
    char *prefix = NULL;

    char present_name[32] = "";
    int present = 0;

    char startConnected_name[32] = "";
    int startConnected = 0;

    char deviceType_name[32] = "";
    char *deviceType = NULL;

    char clientDevice_name[32] = "";
    int clientDevice = 0;

    char fileType_name[32] = "";
    char *fileType = NULL;

    char fileName_name[32] = "";
    char *fileName = NULL;

    char writeThrough_name[32] = "";
    int writeThrough = 0;

    if (def == NULL || *def != NULL) {
1363
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1364 1365 1366 1367
        return -1;
    }

    if (VIR_ALLOC(*def) < 0) {
1368
        virReportOOMError();
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
        goto failure;
    }

    (*def)->device = device;
    (*def)->bus = bus;

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
        if (bus == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controller < 0 || controller > 3) {
1380
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1381
                          _("SCSI controller index %d out of [0..3] range"),
1382 1383 1384 1385 1386
                          controller);
                goto failure;
            }

            if (id < 0 || id > 15 || id == 7) {
1387
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1388
                          _("SCSI ID %d out of [0..6,8..15] range"), id);
1389 1390 1391 1392
                goto failure;
            }

            if (virAsprintf(&prefix, "scsi%d:%d", controller, id) < 0) {
1393
                virReportOOMError();
1394 1395 1396
                goto failure;
            }

M
Matthias Bolte 已提交
1397
            (*def)->dst =
1398 1399
               virIndexToDiskName
                 (controller * 15 + (id < 7 ? id : id - 1), "sd");
1400 1401 1402 1403 1404 1405 1406 1407 1408

            if ((*def)->dst == NULL) {
                goto failure;
            }

            if (virtualDev != NULL) {
                (*def)->driverName = strdup(virtualDev);

                if ((*def)->driverName == NULL) {
1409
                    virReportOOMError();
1410 1411 1412 1413 1414
                    goto failure;
                }
            }
        } else if (bus == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controller < 0 || controller > 1) {
1415
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1416
                          _("IDE controller index %d out of [0..1] range"),
1417 1418 1419 1420 1421
                          controller);
                goto failure;
            }

            if (id < 0 || id > 1) {
1422
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1423
                          _("IDE ID %d out of [0..1] range"), id);
1424 1425 1426 1427
                goto failure;
            }

            if (virAsprintf(&prefix, "ide%d:%d", controller, id) < 0) {
1428
                virReportOOMError();
1429 1430 1431
                goto failure;
            }

1432
            (*def)->dst = virIndexToDiskName(controller * 2 + id, "hd");
1433 1434 1435 1436 1437

            if ((*def)->dst == NULL) {
                goto failure;
            }
        } else {
1438
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1439
                      _("Unsupported bus type '%s' for device type '%s'"),
M
Matthias Bolte 已提交
1440 1441
                      virDomainDiskBusTypeToString(bus),
                      virDomainDiskDeviceTypeToString(device));
1442 1443 1444 1445 1446
            goto failure;
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
        if (bus == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controller < 0 || controller > 1) {
1447
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1448
                          _("Floppy controller index %d out of [0..1] range"),
1449 1450 1451 1452 1453
                          controller);
                goto failure;
            }

            if (virAsprintf(&prefix, "floppy%d", controller) < 0) {
1454
                virReportOOMError();
1455 1456 1457
                goto failure;
            }

1458
            (*def)->dst = virIndexToDiskName(controller, "fd");
1459 1460 1461 1462 1463

            if ((*def)->dst == NULL) {
                goto failure;
            }
        } else {
1464
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1465
                      _("Unsupported bus type '%s' for device type '%s'"),
M
Matthias Bolte 已提交
1466 1467
                      virDomainDiskBusTypeToString(bus),
                      virDomainDiskDeviceTypeToString(device));
1468 1469 1470
            goto failure;
        }
    } else {
1471
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1472
                  _("Unsupported device type '%s'"),
M
Matthias Bolte 已提交
1473
                  virDomainDiskDeviceTypeToString(device));
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
        goto failure;
    }

    ESX_BUILD_VMX_NAME(present);
    ESX_BUILD_VMX_NAME(startConnected);
    ESX_BUILD_VMX_NAME(deviceType);
    ESX_BUILD_VMX_NAME(clientDevice);
    ESX_BUILD_VMX_NAME(fileType);
    ESX_BUILD_VMX_NAME(fileName);
    ESX_BUILD_VMX_NAME(writeThrough);

    /* vmx:present */
1486
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, 0, 1) < 0) {
1487 1488 1489 1490
        goto failure;
    }

    /* vmx:startConnected */
1491 1492
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected,
                                 1, 1) < 0) {
1493 1494 1495 1496 1497 1498 1499 1500 1501
        goto failure;
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
    if (! present/* && ! startConnected*/) {
        goto ignore;
    }

    /* vmx:deviceType -> def:type */
1502
    if (esxUtil_GetConfigString(conf, deviceType_name, &deviceType, 1) < 0) {
1503 1504 1505 1506
        goto failure;
    }

    /* vmx:clientDevice */
1507 1508
    if (esxUtil_GetConfigBoolean(conf, clientDevice_name, &clientDevice, 0,
                                 1) < 0) {
1509 1510 1511 1512 1513 1514
        goto failure;
    }

    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
1515
         * handle them (e.g. assign an image) without the VI Client GUI.
1516 1517 1518 1519 1520
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
1521
    if (esxUtil_GetConfigString(conf, fileType_name, &fileType, 1) < 0) {
1522 1523 1524 1525
        goto failure;
    }

    /* vmx:fileName -> def:src, def:type */
1526
    if (esxUtil_GetConfigString(conf, fileName_name, &fileName, 0) < 0) {
1527 1528 1529 1530
        goto failure;
    }

    /* vmx:writeThrough -> def:cachemode */
1531 1532
    if (esxUtil_GetConfigBoolean(conf, writeThrough_name, &writeThrough, 0,
                                 1) < 0) {
1533 1534 1535 1536 1537
        goto failure;
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
1538
        if (virFileHasSuffix(fileName, ".vmdk")) {
1539 1540 1541
            if (deviceType != NULL) {
                if (bus == VIR_DOMAIN_DISK_BUS_SCSI &&
                    STRCASENEQ(deviceType, "scsi-hardDisk")) {
1542
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1543 1544
                              _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
                                "but found '%s'"), deviceType_name, deviceType);
1545 1546 1547
                    goto failure;
                } else if (bus == VIR_DOMAIN_DISK_BUS_IDE &&
                           STRCASENEQ(deviceType, "ata-hardDisk")) {
1548
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1549 1550
                              _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
                                "but found '%s'"), deviceType_name, deviceType);
1551 1552 1553 1554
                    goto failure;
                }
            }

M
Matthias Bolte 已提交
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
            if (writeThrough && virtualDev == NULL) {
                /*
                 * FIXME: If no virtualDev is explicit specified need to get
                 *        the default based on the guestOS. The mechanism to
                 *        obtain the default is currently missing
                 */
                VIR_WARN0("No explicit SCSI driver specified in VMX config, "
                          "cannot represent explicit specified cachemode");
            }

1565
            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
1566 1567
            (*def)->src = esxVMX_ParseFileName(ctx, fileName, datastoreName,
                                               directoryName);
1568 1569 1570
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;

M
Matthias Bolte 已提交
1571 1572 1573
            if ((*def)->src == NULL) {
                goto failure;
            }
1574
        } else if (virFileHasSuffix(fileName, ".iso") ||
1575 1576 1577 1578 1579 1580 1581 1582 1583
                   STREQ(deviceType, "atapi-cdrom")) {
            /*
             * This function was called in order to parse a harddisk device,
             * but .iso files and 'atapi-cdrom' devices are for CDROM devices
             * only. Just ignore it, another call to this function to parse a
             * CDROM device may handle it.
             */
            goto ignore;
        } else {
1584
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1585 1586
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
1587 1588 1589
            goto failure;
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
1590
        if (virFileHasSuffix(fileName, ".iso")) {
1591 1592
            if (deviceType != NULL) {
                if (STRCASENEQ(deviceType, "cdrom-image")) {
1593
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1594 1595
                              _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                "but found '%s'"), deviceType_name, deviceType);
1596 1597 1598 1599 1600
                    goto failure;
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
1601 1602
            (*def)->src = esxVMX_ParseFileName(ctx, fileName, datastoreName,
                                               directoryName);
1603

M
Matthias Bolte 已提交
1604 1605 1606
            if ((*def)->src == NULL) {
                goto failure;
            }
1607
        } else if (virFileHasSuffix(fileName, ".vmdk")) {
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
            /*
             * This function was called in order to parse a CDROM device, but
             * .vmdk files are for harddisk devices only. Just ignore it,
             * another call to this function to parse a harddisk device may
             * handle it.
             */
            goto ignore;
        } else if (STREQ(deviceType, "atapi-cdrom")) {
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
1621
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1622 1623
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
1624 1625 1626
            goto failure;
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
1627
        if (virFileHasSuffix(fileName, ".flp")) {
1628 1629
            if (fileType != NULL) {
                if (STRCASENEQ(fileType, "file")) {
1630
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1631 1632
                              _("Expecting VMX entry '%s' to be 'file' but "
                                "found '%s'"), fileType_name, fileType);
1633 1634 1635 1636 1637
                    goto failure;
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
1638 1639
            (*def)->src = esxVMX_ParseFileName(ctx, fileName, datastoreName,
                                               directoryName);
1640

M
Matthias Bolte 已提交
1641 1642 1643
            if ((*def)->src == NULL) {
                goto failure;
            }
1644 1645 1646 1647 1648 1649
        } else if (fileType != NULL && STREQ(fileType, "device")) {
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
1650
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1651 1652
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
1653 1654 1655
            goto failure;
        }
    } else {
1656
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, _("Unsupported device type '%s'"),
M
Matthias Bolte 已提交
1657
                  virDomainDiskDeviceTypeToString(device));
1658 1659 1660
        goto failure;
    }

M
Matthias Bolte 已提交
1661
  cleanup:
1662 1663 1664 1665 1666 1667 1668
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
1669
  failure:
1670 1671
    result = -1;

M
Matthias Bolte 已提交
1672
  ignore:
1673 1674 1675 1676 1677 1678 1679 1680 1681
    virDomainDiskDefFree(*def);
    *def = NULL;

    goto cleanup;
}



int
1682
esxVMX_ParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
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
{
    int result = 0;
    char prefix[48] = "";

    char present_name[48] = "";
    int present = 0;

    char startConnected_name[48] = "";
    int startConnected = 0;

    char connectionType_name[48] = "";
    char *connectionType = NULL;

    char addressType_name[48] = "";
    char *addressType = NULL;

    char generatedAddress_name[48] = "";
    char *generatedAddress = NULL;

    char address_name[48] = "";
    char *address = NULL;

    char virtualDev_name[48] = "";
    char *virtualDev = NULL;

    char vnet_name[48] = "";
    char *vnet = NULL;

M
Matthias Bolte 已提交
1711 1712 1713
    char networkName_name[48] = "";
    char *networkName = NULL;

1714
    if (def == NULL || *def != NULL) {
1715
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1716 1717 1718 1719
        return -1;
    }

    if (controller < 0 || controller > 3) {
1720
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1721
                  _("Ethernet controller index %d out of [0..3] range"),
1722
                  controller);
1723
        return -1;
1724 1725 1726
    }

    if (VIR_ALLOC(*def) < 0) {
1727
        virReportOOMError();
1728 1729 1730
        goto failure;
    }

1731
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
1732 1733 1734 1735 1736 1737 1738 1739

    ESX_BUILD_VMX_NAME(present);
    ESX_BUILD_VMX_NAME(startConnected);
    ESX_BUILD_VMX_NAME(connectionType);
    ESX_BUILD_VMX_NAME(addressType);
    ESX_BUILD_VMX_NAME(generatedAddress);
    ESX_BUILD_VMX_NAME(address);
    ESX_BUILD_VMX_NAME(virtualDev);
M
Matthias Bolte 已提交
1740
    ESX_BUILD_VMX_NAME(networkName);
1741
    ESX_BUILD_VMX_NAME(vnet);
1742 1743

    /* vmx:present */
1744
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, 0, 1) < 0) {
1745 1746 1747 1748
        goto failure;
    }

    /* vmx:startConnected */
1749 1750
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected, 1,
                                 1) < 0) {
1751 1752 1753 1754 1755 1756 1757 1758 1759
        goto failure;
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
    if (! present/* && ! startConnected*/) {
        goto ignore;
    }

    /* vmx:connectionType -> def:type */
1760 1761
    if (esxUtil_GetConfigString(conf, connectionType_name, &connectionType,
                                1) < 0) {
1762 1763 1764 1765
        goto failure;
    }

    /* vmx:addressType, vmx:generatedAddress, vmx:address -> def:mac */
1766 1767 1768 1769
    if (esxUtil_GetConfigString(conf, addressType_name, &addressType, 1) < 0 ||
        esxUtil_GetConfigString(conf, generatedAddress_name, &generatedAddress,
                                1) < 0 ||
        esxUtil_GetConfigString(conf, address_name, &address, 1) < 0) {
1770 1771 1772
        goto failure;
    }

1773 1774
    if (addressType == NULL || STRCASEEQ(addressType, "generated") ||
        STRCASEEQ(addressType, "vpx")) {
1775 1776
        if (generatedAddress != NULL) {
            if (virParseMacAddr(generatedAddress, (*def)->mac) < 0) {
1777
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1778 1779
                          _("Expecting VMX entry '%s' to be MAC address but "
                            "found '%s'"), generatedAddress_name,
1780 1781 1782 1783 1784 1785 1786
                          generatedAddress);
                goto failure;
            }
        }
    } else if (STRCASEEQ(addressType, "static")) {
        if (address != NULL) {
            if (virParseMacAddr(address, (*def)->mac) < 0) {
1787
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1788 1789
                          _("Expecting VMX entry '%s' to be MAC address but "
                            "found '%s'"), address_name, address);
1790 1791 1792 1793
                goto failure;
            }
        }
    } else {
1794
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1795 1796
                  _("Expecting VMX entry '%s' to be 'generated' or 'static' or "
                    "'vpx' but found '%s'"), addressType_name, addressType);
1797 1798 1799 1800
        goto failure;
    }

    /* vmx:virtualDev -> def:model */
1801
    if (esxUtil_GetConfigString(conf, virtualDev_name, &virtualDev, 1) < 0) {
1802 1803 1804 1805 1806 1807
        goto failure;
    }

    if (virtualDev != NULL &&
        STRCASENEQ(virtualDev, "vlance") &&
        STRCASENEQ(virtualDev, "vmxnet") &&
1808
        STRCASENEQ(virtualDev, "vmxnet3") &&
1809
        STRCASENEQ(virtualDev, "e1000")) {
1810
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1811 1812
                  _("Expecting VMX entry '%s' to be 'vlance' or 'vmxnet' or "
                    "'vmxnet3' or 'e1000' but found '%s'"), virtualDev_name,
1813
                  virtualDev);
1814 1815 1816
        goto failure;
    }

M
Matthias Bolte 已提交
1817 1818 1819 1820
    /* vmx:networkName -> def:data.bridge.brname */
    if ((connectionType == NULL ||
         STRCASEEQ(connectionType, "bridged") ||
         STRCASEEQ(connectionType, "custom")) &&
1821
        esxUtil_GetConfigString(conf, networkName_name, &networkName, 0) < 0) {
M
Matthias Bolte 已提交
1822 1823 1824 1825
        goto failure;
    }

    /* vmx:vnet -> def:data.ifname */
1826
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
1827
        esxUtil_GetConfigString(conf, vnet_name, &vnet, 0) < 0) {
1828 1829 1830 1831 1832 1833 1834
        goto failure;
    }

    /* Setup virDomainNetDef */
    if (connectionType == NULL || STRCASEEQ(connectionType, "bridged")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
1835
        (*def)->data.bridge.brname = networkName;
1836 1837

        virtualDev = NULL;
M
Matthias Bolte 已提交
1838
        networkName = NULL;
1839 1840
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
1841
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1842
                  _("No yet handled value '%s' for VMX entry '%s'"),
1843 1844 1845 1846
                  connectionType, connectionType_name);
        goto failure;
    } else if (STRCASEEQ(connectionType, "nat")) {
        /* FIXME */
1847
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1848
                  _("No yet handled value '%s' for VMX entry '%s'"),
1849 1850 1851 1852 1853
                  connectionType, connectionType_name);
        goto failure;
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
1854 1855
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
1856 1857

        virtualDev = NULL;
M
Matthias Bolte 已提交
1858
        networkName = NULL;
1859 1860
        vnet = NULL;
    } else {
1861
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1862
                  _("Invalid value '%s' for VMX entry '%s'"), connectionType,
1863 1864 1865 1866
                  connectionType_name);
        goto failure;
    }

M
Matthias Bolte 已提交
1867
  cleanup:
1868 1869 1870 1871 1872 1873 1874 1875 1876
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;

M
Matthias Bolte 已提交
1877
  failure:
1878 1879
    result = -1;

M
Matthias Bolte 已提交
1880
  ignore:
1881 1882 1883 1884 1885 1886 1887 1888 1889
    virDomainNetDefFree(*def);
    *def = NULL;

    goto cleanup;
}



int
1890 1891 1892
esxVMX_ParseSerial(esxVI_Context *ctx, virConfPtr conf, int port,
                   const char *datastoreName, const char *directoryName,
                   virDomainChrDefPtr *def)
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
{
    int result = 0;
    char prefix[48] = "";

    char present_name[48] = "";
    int present = 0;

    char startConnected_name[48] = "";
    int startConnected = 0;

    char fileType_name[48] = "";
    char *fileType = NULL;

    char fileName_name[48] = "";
    char *fileName = NULL;

    if (def == NULL || *def != NULL) {
1910
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1911 1912 1913 1914
        return -1;
    }

    if (port < 0 || port > 3) {
1915
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1916
                  _("Serial port index %d out of [0..3] range"), port);
1917
        return -1;
1918 1919 1920
    }

    if (VIR_ALLOC(*def) < 0) {
1921
        virReportOOMError();
1922 1923 1924
        goto failure;
    }

1925 1926
    (*def)->targetType = VIR_DOMAIN_CHR_TARGET_TYPE_SERIAL;

1927
    snprintf(prefix, sizeof(prefix), "serial%d", port);
1928 1929 1930 1931 1932 1933 1934

    ESX_BUILD_VMX_NAME(present);
    ESX_BUILD_VMX_NAME(startConnected);
    ESX_BUILD_VMX_NAME(fileType);
    ESX_BUILD_VMX_NAME(fileName);

    /* vmx:present */
1935
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, 0, 1) < 0) {
1936 1937 1938 1939
        goto failure;
    }

    /* vmx:startConnected */
1940 1941
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected, 1,
                                 1) < 0) {
1942 1943 1944 1945 1946 1947 1948 1949 1950
        goto failure;
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
    if (! present/* && ! startConnected*/) {
        goto ignore;
    }

    /* vmx:fileType -> def:type */
1951
    if (esxUtil_GetConfigString(conf, fileType_name, &fileType, 0) < 0) {
1952 1953 1954 1955
        goto failure;
    }

    /* vmx:fileName -> def:data.file.path */
1956
    if (esxUtil_GetConfigString(conf, fileName_name, &fileName, 0) < 0) {
1957 1958 1959 1960 1961
        goto failure;
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
1962
        (*def)->target.port = port;
1963 1964 1965 1966 1967
        (*def)->type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->data.file.path = fileName;

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
1968
        (*def)->target.port = port;
1969
        (*def)->type = VIR_DOMAIN_CHR_TYPE_FILE;
1970
        (*def)->data.file.path = esxVMX_ParseFileName(ctx, fileName,
M
Matthias Bolte 已提交
1971 1972
                                                      datastoreName,
                                                      directoryName);
1973

M
Matthias Bolte 已提交
1974 1975 1976
        if ((*def)->data.file.path == NULL) {
            goto failure;
        }
1977
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
1978 1979 1980 1981
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
1982
        (*def)->target.port = port;
M
Matthias Bolte 已提交
1983 1984 1985 1986
        (*def)->type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->data.file.path = fileName;

        fileName = NULL;
1987
    } else {
1988
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1989 1990
                  _("Expecting VMX entry '%s' to be 'device', 'file' or 'pipe' "
                    "but found '%s'"), fileType_name, fileType);
1991 1992 1993
        goto failure;
    }

M
Matthias Bolte 已提交
1994
  cleanup:
1995 1996 1997 1998 1999
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2000
  failure:
2001 2002
    result = -1;

M
Matthias Bolte 已提交
2003
  ignore:
2004 2005 2006 2007 2008 2009 2010 2011 2012
    virDomainChrDefFree(*def);
    *def = NULL;

    goto cleanup;
}



int
2013 2014 2015
esxVMX_ParseParallel(esxVI_Context *ctx, virConfPtr conf, int port,
                     const char *datastoreName, const char *directoryName,
                     virDomainChrDefPtr *def)
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
{
    int result = 0;
    char prefix[48] = "";

    char present_name[48] = "";
    int present = 0;

    char startConnected_name[48] = "";
    int startConnected = 0;

    char fileType_name[48] = "";
    char *fileType = NULL;

    char fileName_name[48] = "";
    char *fileName = NULL;

    if (def == NULL || *def != NULL) {
2033
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2034 2035 2036 2037
        return -1;
    }

    if (port < 0 || port > 2) {
2038
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2039
                  _("Parallel port index %d out of [0..2] range"), port);
2040
        return -1;
2041 2042 2043
    }

    if (VIR_ALLOC(*def) < 0) {
2044
        virReportOOMError();
2045 2046 2047
        goto failure;
    }

2048 2049
    (*def)->targetType = VIR_DOMAIN_CHR_TARGET_TYPE_PARALLEL;

2050
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2051 2052 2053 2054 2055 2056 2057

    ESX_BUILD_VMX_NAME(present);
    ESX_BUILD_VMX_NAME(startConnected);
    ESX_BUILD_VMX_NAME(fileType);
    ESX_BUILD_VMX_NAME(fileName);

    /* vmx:present */
2058
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, 0, 1) < 0) {
2059 2060 2061 2062
        goto failure;
    }

    /* vmx:startConnected */
2063 2064
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected, 1,
                                 1) < 0) {
2065 2066 2067 2068 2069 2070 2071 2072 2073
        goto failure;
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
    if (! present/* && ! startConnected*/) {
        goto ignore;
    }

    /* vmx:fileType -> def:type */
2074
    if (esxUtil_GetConfigString(conf, fileType_name, &fileType, 0) < 0) {
2075 2076 2077 2078
        goto failure;
    }

    /* vmx:fileName -> def:data.file.path */
2079
    if (esxUtil_GetConfigString(conf, fileName_name, &fileName, 0) < 0) {
2080 2081 2082 2083 2084
        goto failure;
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2085
        (*def)->target.port = port;
2086 2087 2088 2089 2090
        (*def)->type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->data.file.path = fileName;

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2091
        (*def)->target.port = port;
2092
        (*def)->type = VIR_DOMAIN_CHR_TYPE_FILE;
2093
        (*def)->data.file.path = esxVMX_ParseFileName(ctx, fileName,
M
Matthias Bolte 已提交
2094 2095
                                                      datastoreName,
                                                      directoryName);
2096

M
Matthias Bolte 已提交
2097 2098 2099
        if ((*def)->data.file.path == NULL) {
            goto failure;
        }
2100
    } else {
2101
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2102 2103
                  _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                    "found '%s'"), fileType_name, fileType);
2104 2105 2106
        goto failure;
    }

M
Matthias Bolte 已提交
2107
  cleanup:
2108 2109 2110 2111 2112
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2113
  failure:
2114 2115
    result = -1;

M
Matthias Bolte 已提交
2116
  ignore:
2117 2118 2119 2120 2121
    virDomainChrDefFree(*def);
    *def = NULL;

    goto cleanup;
}
M
Matthias Bolte 已提交
2122 2123 2124 2125 2126 2127 2128 2129



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Domain XML -> VMX
 */

char *
2130
esxVMX_FormatFileName(esxVI_Context *ctx ATTRIBUTE_UNUSED, const char *src)
M
Matthias Bolte 已提交
2131 2132 2133 2134 2135 2136 2137 2138
{
    char *datastoreName = NULL;
    char *directoryName = NULL;
    char *fileName = NULL;
    char *absolutePath = NULL;

    if (STRPREFIX(src, "[")) {
        /* Found potential datastore related path */
2139
        if (esxUtil_ParseDatastoreRelatedPath(src, &datastoreName,
M
Matthias Bolte 已提交
2140 2141 2142 2143 2144 2145 2146
                                              &directoryName, &fileName) < 0) {
            goto failure;
        }

        if (directoryName == NULL) {
            if (virAsprintf(&absolutePath, "/vmfs/volumes/%s/%s",
                            datastoreName, fileName) < 0) {
2147
                virReportOOMError();
M
Matthias Bolte 已提交
2148 2149 2150 2151 2152
                goto failure;
            }
        } else {
            if (virAsprintf(&absolutePath, "/vmfs/volumes/%s/%s/%s",
                            datastoreName, directoryName, fileName) < 0) {
2153
                virReportOOMError();
M
Matthias Bolte 已提交
2154 2155 2156 2157 2158 2159 2160 2161
                goto failure;
            }
        }
    } else if (STRPREFIX(src, "/")) {
        /* Found absolute path */
        absolutePath = strdup(src);

        if (absolutePath == NULL) {
2162
            virReportOOMError();
M
Matthias Bolte 已提交
2163 2164 2165 2166
            goto failure;
        }
    } else {
        /* Found relative path, this is not supported */
2167
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2168 2169
                  _("Found relative path '%s' in domain XML, this is not "
                    "supported"), src);
M
Matthias Bolte 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
        goto failure;
    }

    /* FIXME: Check if referenced path/file really exists */

  cleanup:
    VIR_FREE(datastoreName);
    VIR_FREE(directoryName);
    VIR_FREE(fileName);

    return absolutePath;

  failure:
    VIR_FREE(absolutePath);

    goto cleanup;
}



char *
2191 2192
esxVMX_FormatConfig(esxVI_Context *ctx, virDomainDefPtr def,
                    esxVI_APIVersion apiVersion)
M
Matthias Bolte 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201
{
    int i;
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;

    memset(zero, 0, VIR_UUID_BUFLEN);

    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) { /* FIXME: maybe add VIR_DOMAIN_VIRT_ESX ? */
2202
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2203
                  _("Expecting virt type to be '%s' but found '%s'"),
M
Matthias Bolte 已提交
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
                  virDomainVirtTypeToString(VIR_DOMAIN_VIRT_VMWARE),
                  virDomainVirtTypeToString(def->virtType));
        return NULL;
    }

    /* vmx:config.version */
    virBufferAddLit(&buffer, "config.version = \"8\"\n");

    /* vmx:virtualHW.version */
    switch (apiVersion) {
      case esxVI_APIVersion_25:
        virBufferAddLit(&buffer, "virtualHW.version = \"4\"\n");
        break;

      case esxVI_APIVersion_40:
        virBufferAddLit(&buffer, "virtualHW.version = \"7\"\n");
        break;

      default:
2223 2224
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting VI API version 2.5 or 4.0"));
M
Matthias Bolte 已提交
2225 2226 2227 2228 2229 2230 2231 2232 2233
        goto failure;
    }

    /* def:arch -> vmx:guestOS */
    if (def->os.arch == NULL || STRCASEEQ(def->os.arch, "i686")) {
        virBufferAddLit(&buffer, "guestOS = \"other\"\n");
    } else if (STRCASEEQ(def->os.arch, "x86_64")) {
        virBufferAddLit(&buffer, "guestOS = \"other-64\"\n");
    } else {
2234
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2235 2236
                  _("Expecting domain XML attribute 'arch' of entry 'os/type' "
                    "to be 'i686' or 'x86_64' but found '%s'"), def->os.arch);
M
Matthias Bolte 已提交
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
        goto failure;
    }

    /* def:uuid -> vmx:uuid.action, vmx:uuid.bios */
    if (memcmp(def->uuid, zero, VIR_UUID_BUFLEN) == 0) {
        virBufferAddLit(&buffer, "uuid.action = \"create\"\n");
    } else {
        virBufferVSprintf(&buffer, "uuid.bios = \"%02x %02x %02x %02x %02x %02x "
                          "%02x %02x-%02x %02x %02x %02x %02x %02x %02x %02x\"\n",
                          def->uuid[0], def->uuid[1], def->uuid[2], def->uuid[3],
                          def->uuid[4], def->uuid[5], def->uuid[6], def->uuid[7],
                          def->uuid[8], def->uuid[9], def->uuid[10], def->uuid[11],
                          def->uuid[12], def->uuid[13], def->uuid[14],
                          def->uuid[15]);
    }

    /* def:name -> vmx:displayName */
    virBufferVSprintf(&buffer, "displayName = \"%s\"\n", def->name);

    /* def:maxmem -> vmx:memsize */
    if (def->maxmem <= 0 || def->maxmem % 4096 != 0) {
2258
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2259 2260
                  _("Expecting domain XML entry 'memory' to be an unsigned "
                    "integer (multiple of 4096) but found %lld"),
M
Matthias Bolte 已提交
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
                  (unsigned long long)def->maxmem);
        goto failure;
    }

    /* Scale from kilobytes to megabytes */
    virBufferVSprintf(&buffer, "memsize = \"%d\"\n",
                      (int)(def->maxmem / 1024));

    /* def:memory -> vmx:sched.mem.max */
    if (def->memory < def->maxmem) {
        if (def->memory <= 0 || def->memory % 1024 != 0) {
2272
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2273 2274
                      _("Expecting domain XML entry 'currentMemory' to be an "
                        "unsigned integer (multiple of 1024) but found %lld"),
M
Matthias Bolte 已提交
2275 2276 2277 2278 2279 2280 2281 2282 2283
                      (unsigned long long)def->memory);
            goto failure;
        }

        /* Scale from kilobytes to megabytes */
        virBufferVSprintf(&buffer, "sched.mem.max = \"%d\"\n",
                          (int)(def->memory / 1024));
    }

M
Matthias Bolte 已提交
2284
    /* def:vcpus -> vmx:numvcpus */
M
Matthias Bolte 已提交
2285
    if (def->vcpus <= 0 || (def->vcpus % 2 != 0 && def->vcpus != 1)) {
2286
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2287 2288
                  _("Expecting domain XML entry 'vcpu' to be an unsigned "
                    "integer (1 or a multiple of 2) but found %d"),
M
Matthias Bolte 已提交
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
                  (int)def->vcpus);
        goto failure;
    }

    virBufferVSprintf(&buffer, "numvcpus = \"%d\"\n", (int)def->vcpus);

    /* def:cpumask -> vmx:sched.cpu.affinity */
    if (def->cpumasklen > 0) {
        virBufferAddLit(&buffer, "sched.cpu.affinity = \"");

        sched_cpu_affinity_length = 0;

        for (i = 0; i < def->cpumasklen; ++i) {
            if (def->cpumask[i]) {
                ++sched_cpu_affinity_length;
            }
        }

        if (sched_cpu_affinity_length < def->vcpus) {
2308
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2309 2310
                      _("Expecting domain XML attribute 'cpuset' of entry "
                        "'vcpu' to contains at least %d CPU(s)"),
M
Matthias Bolte 已提交
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
                      (int)def->vcpus);
            goto failure;
        }

        for (i = 0; i < def->cpumasklen; ++i) {
            if (def->cpumask[i]) {
                virBufferVSprintf(&buffer, "%d", i);

                if (sched_cpu_affinity_length > 1) {
                    virBufferAddChar(&buffer, ',');
                }

                --sched_cpu_affinity_length;
            }
        }

        virBufferAddLit(&buffer, "\"\n");
    }

M
Matthias Bolte 已提交
2330 2331 2332 2333
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
2334
            if (esxVMX_FormatVNC(def->graphics[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2335 2336 2337 2338 2339 2340
                goto failure;
            }

            break;

          default:
2341
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2342
                      _("Unsupported graphics type '%s'"),
M
Matthias Bolte 已提交
2343 2344 2345 2346 2347
                      virDomainGraphicsTypeToString(def->graphics[i]->type));
            goto failure;
        }
    }

M
Matthias Bolte 已提交
2348 2349 2350 2351
    /* def:disks */
    int scsi_present[4] = { 0, 0, 0, 0 };
    char *scsi_virtualDev[4] = { NULL, NULL, NULL, NULL };

2352
    if (esxVMX_GatherSCSIControllers(def, scsi_virtualDev, scsi_present) < 0) {
M
Matthias Bolte 已提交
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
        goto failure;
    }

    for (i = 0; i < 4; ++i) {
        if (scsi_present[i]) {
            virBufferVSprintf(&buffer, "scsi%d.present = \"true\"\n", i);

            if (scsi_virtualDev[i] != NULL) {
                virBufferVSprintf(&buffer, "scsi%d.virtualDev = \"%s\"\n", i,
                                  scsi_virtualDev[i]);
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
2370
            if (esxVMX_FormatHardDisk(ctx, def->disks[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2371 2372 2373 2374 2375 2376
                goto failure;
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_CDROM:
2377
            if (esxVMX_FormatCDROM(ctx, def->disks[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2378 2379 2380 2381 2382 2383
                goto failure;
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
2384
            if (esxVMX_FormatFloppy(ctx, def->disks[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2385 2386 2387 2388 2389 2390
                goto failure;
            }

            break;

          default:
2391
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2392
                      _("Unsupported disk device type '%s'"),
M
Matthias Bolte 已提交
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
                      virDomainDiskDeviceTypeToString(def->disks[i]->device));
            goto failure;
        }
    }

    /* def:fss */
    /* FIXME */

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
2403
        if (esxVMX_FormatEthernet(def->nets[i], i, &buffer) < 0) {
M
Matthias Bolte 已提交
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
            goto failure;
        }
    }

    /* def:inputs */
    /* FIXME */

    /* def:sounds */
    /* FIXME */

    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
2419
        if (esxVMX_FormatSerial(ctx, def->serials[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2420 2421 2422 2423 2424 2425
            goto failure;
        }
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
2426
        if (esxVMX_FormatParallel(ctx, def->parallels[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2427 2428 2429 2430 2431 2432
            goto failure;
        }
    }

    /* Get final VMX output */
    if (virBufferError(&buffer)) {
2433
        virReportOOMError();
M
Matthias Bolte 已提交
2434 2435 2436
        goto failure;
    }

2437
    return virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
2438 2439

  failure:
2440
    virBufferFreeAndReset(&buffer);
M
Matthias Bolte 已提交
2441 2442 2443 2444 2445 2446

    return NULL;
}



M
Matthias Bolte 已提交
2447
int
2448
esxVMX_FormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
2449 2450
{
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
2451
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
        return -1;
    }

    virBufferVSprintf(buffer, "RemoteDisplay.vnc.enabled = \"true\"\n");

    if (def->data.vnc.autoport) {
        VIR_WARN0("VNC autoport is enabled, but the automatically assigned "
                  "VNC port cannot be read back");
    } else {
        if (def->data.vnc.port < 5900 || def->data.vnc.port > 5964) {
            VIR_WARN("VNC port %d it out of [5900..5964] range",
                     def->data.vnc.port);
        }

        virBufferVSprintf(buffer, "RemoteDisplay.vnc.port = \"%d\"\n",
                          def->data.vnc.port);
    }

    if (def->data.vnc.listenAddr != NULL) {
        virBufferVSprintf(buffer, "RemoteDisplay.vnc.ip = \"%s\"\n",
                          def->data.vnc.listenAddr);
    }

    if (def->data.vnc.keymap != NULL) {
        virBufferVSprintf(buffer, "RemoteDisplay.vnc.keymap = \"%s\"\n",
                          def->data.vnc.keymap);
    }

    if (def->data.vnc.passwd != NULL) {
        virBufferVSprintf(buffer, "RemoteDisplay.vnc.password = \"%s\"\n",
                          def->data.vnc.passwd);
    }

    return 0;
}



M
Matthias Bolte 已提交
2490
int
2491 2492
esxVMX_FormatHardDisk(esxVI_Context *ctx, virDomainDiskDefPtr def,
                      virBufferPtr buffer)
M
Matthias Bolte 已提交
2493 2494 2495 2496 2497
{
    int controller, id;
    const char *busName = NULL;
    const char *entryPrefix = NULL;
    const char *deviceTypePrefix = NULL;
M
Matthias Bolte 已提交
2498
    char *fileName = NULL;
M
Matthias Bolte 已提交
2499 2500

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
2501
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
2502 2503 2504 2505 2506 2507 2508 2509
        return -1;
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        busName = "SCSI";
        entryPrefix = "scsi";
        deviceTypePrefix = "scsi";

2510 2511
        if (esxVMX_SCSIDiskNameToControllerAndID(def->dst, &controller,
                                                 &id) < 0) {
M
Matthias Bolte 已提交
2512 2513 2514 2515 2516 2517 2518
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";
        deviceTypePrefix = "ata";

2519 2520
        if (esxVMX_IDEDiskNameToControllerAndID(def->dst, &controller,
                                                &id) < 0) {
M
Matthias Bolte 已提交
2521 2522 2523
            return -1;
        }
    } else {
2524
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2525
                  _("Unsupported bus type '%s' for harddisk"),
M
Matthias Bolte 已提交
2526 2527 2528 2529 2530
                  virDomainDiskBusTypeToString(def->bus));
        return -1;
    }

    if (def->type != VIR_DOMAIN_DISK_TYPE_FILE) {
2531
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2532
                  _("%s harddisk '%s' has unsupported type '%s', expecting '%s'"),
M
Matthias Bolte 已提交
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
                  busName, def->dst, virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE));
        return -1;
    }

    virBufferVSprintf(buffer, "%s%d:%d.present = \"true\"\n",
                      entryPrefix, controller, id);
    virBufferVSprintf(buffer, "%s%d:%d.deviceType = \"%s-hardDisk\"\n",
                      entryPrefix, controller, id, deviceTypePrefix);

    if (def->src != NULL) {
2544
        if (! virFileHasSuffix(def->src, ".vmdk")) {
2545
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2546 2547
                      _("Image file for %s harddisk '%s' has unsupported suffix, "
                        "expecting '.vmdk'"), busName, def->dst);
M
Matthias Bolte 已提交
2548 2549 2550
            return -1;
        }

2551
        fileName = esxVMX_FormatFileName(ctx, def->src);
M
Matthias Bolte 已提交
2552 2553 2554 2555 2556

        if (fileName == NULL) {
            return -1;
        }

M
Matthias Bolte 已提交
2557
        virBufferVSprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
M
Matthias Bolte 已提交
2558 2559 2560
                          entryPrefix, controller, id, fileName);

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
2561 2562 2563 2564 2565 2566 2567
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
            virBufferVSprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
                              entryPrefix, controller, id);
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
2568
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2569
                      _("%s harddisk '%s' has unsupported cache mode '%s'"),
M
Matthias Bolte 已提交
2570 2571
                      busName, def->dst,
                      virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
2572
            return -1;
M
Matthias Bolte 已提交
2573 2574 2575 2576 2577 2578 2579 2580 2581
        }
    }

    return 0;
}



int
2582 2583
esxVMX_FormatCDROM(esxVI_Context *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
2584 2585 2586 2587
{
    int controller, id;
    const char *busName = NULL;
    const char *entryPrefix = NULL;
M
Matthias Bolte 已提交
2588
    char *fileName = NULL;
M
Matthias Bolte 已提交
2589 2590

    if (def->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
2591
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
2592 2593 2594 2595 2596 2597 2598
        return -1;
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        busName = "SCSI";
        entryPrefix = "scsi";

2599 2600
        if (esxVMX_SCSIDiskNameToControllerAndID(def->dst, &controller,
                                                 &id) < 0) {
M
Matthias Bolte 已提交
2601 2602 2603 2604 2605 2606
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";

2607 2608
        if (esxVMX_IDEDiskNameToControllerAndID(def->dst, &controller,
                                                &id) < 0) {
M
Matthias Bolte 已提交
2609 2610 2611
            return -1;
        }
    } else {
2612 2613
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Unsupported bus type '%s' for cdrom"),
M
Matthias Bolte 已提交
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
                  virDomainDiskBusTypeToString(def->bus));
        return -1;
    }

    virBufferVSprintf(buffer, "%s%d:%d.present = \"true\"\n",
                      entryPrefix, controller, id);

    if (def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
        virBufferVSprintf(buffer, "%s%d:%d.deviceType = \"cdrom-image\"\n",
                          entryPrefix, controller, id);
M
Matthias Bolte 已提交
2624 2625

        if (def->src != NULL) {
2626
            if (! virFileHasSuffix(def->src, ".iso")) {
2627
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2628 2629
                          _("Image file for %s cdrom '%s' has unsupported "
                            "suffix, expecting '.iso'"), busName, def->dst);
M
Matthias Bolte 已提交
2630 2631 2632
                return -1;
            }

2633
            fileName = esxVMX_FormatFileName(ctx, def->src);
M
Matthias Bolte 已提交
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643

            if (fileName == NULL) {
                return -1;
            }

            virBufferVSprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
                              entryPrefix, controller, id, fileName);

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
2644 2645 2646
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
        virBufferVSprintf(buffer, "%s%d:%d.deviceType = \"atapi-cdrom\"\n",
                          entryPrefix, controller, id);
M
Matthias Bolte 已提交
2647 2648 2649 2650 2651

        if (def->src != NULL) {
            virBufferVSprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
                              entryPrefix, controller, id, def->src);
        }
M
Matthias Bolte 已提交
2652
    } else {
2653
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2654 2655
                  _("%s cdrom '%s' has unsupported type '%s', expecting '%s' "
                    "or '%s'"), busName, def->dst,
M
Matthias Bolte 已提交
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
                  virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }

    return 0;
}



int
2668 2669
esxVMX_FormatFloppy(esxVI_Context *ctx, virDomainDiskDefPtr def,
                    virBufferPtr buffer)
M
Matthias Bolte 已提交
2670 2671
{
    int controller;
M
Matthias Bolte 已提交
2672
    char *fileName = NULL;
M
Matthias Bolte 已提交
2673 2674

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2675
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
2676 2677 2678
        return -1;
    }

2679
    if (esxVMX_FloppyDiskNameToController(def->dst, &controller) < 0) {
M
Matthias Bolte 已提交
2680 2681 2682 2683 2684 2685 2686 2687
        return -1;
    }

    virBufferVSprintf(buffer, "floppy%d.present = \"true\"\n", controller);

    if (def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
        virBufferVSprintf(buffer, "floppy%d.fileType = \"file\"\n",
                          controller);
M
Matthias Bolte 已提交
2688 2689

        if (def->src != NULL) {
2690
            if (! virFileHasSuffix(def->src, ".flp")) {
2691
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2692 2693
                          _("Image file for floppy '%s' has unsupported "
                            "suffix, expecting '.flp'"), def->dst);
M
Matthias Bolte 已提交
2694 2695 2696
                return -1;
            }

2697
            fileName = esxVMX_FormatFileName(ctx, def->src);
M
Matthias Bolte 已提交
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707

            if (fileName == NULL) {
                return -1;
            }

            virBufferVSprintf(buffer, "floppy%d.fileName = \"%s\"\n",
                              controller, fileName);

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
2708 2709 2710
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
        virBufferVSprintf(buffer, "floppy%d.fileType = \"device\"\n",
                          controller);
M
Matthias Bolte 已提交
2711 2712 2713 2714 2715

        if (def->src != NULL) {
            virBufferVSprintf(buffer, "floppy%d.fileName = \"%s\"\n",
                              controller, def->src);
        }
M
Matthias Bolte 已提交
2716
    } else {
2717
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2718 2719
                  _("Floppy '%s' has unsupported type '%s', expecting '%s' "
                    "or '%s'"), def->dst,
M
Matthias Bolte 已提交
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
                  virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }

    return 0;
}



int
2732 2733
esxVMX_FormatEthernet(virDomainNetDefPtr def, int controller,
                      virBufferPtr buffer)
M
Matthias Bolte 已提交
2734 2735
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
2736
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
2737 2738

    if (controller < 0 || controller > 3) {
2739
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2740
                  _("Ethernet controller index %d out of [0..3] range"),
M
Matthias Bolte 已提交
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
                  controller);
        return -1;
    }

    virBufferVSprintf(buffer, "ethernet%d.present = \"true\"\n", controller);

    /* def:model -> vmx:virtualDev */
    if (def->model != NULL) {
        if (STRCASENEQ(def->model, "vlance") &&
            STRCASENEQ(def->model, "vmxnet") &&
2751
            STRCASENEQ(def->model, "vmxnet3") &&
M
Matthias Bolte 已提交
2752
            STRCASENEQ(def->model, "e1000")) {
2753
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2754 2755 2756
                      _("Expecting domain XML entry 'devices/interfase/model' "
                        "to be 'vlance' or 'vmxnet' or 'vmxnet3' or 'e1000' "
                        "but found '%s'"), def->model);
M
Matthias Bolte 已提交
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
            return -1;
        }

        virBufferVSprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
                          controller, def->model);
    }

    /* def:type, def:ifname -> vmx:connectionType */
    switch (def->type) {
      case VIR_DOMAIN_NET_TYPE_BRIDGE:
        virBufferVSprintf(buffer, "ethernet%d.networkName = \"%s\"\n",
                          controller, def->data.bridge.brname);

        if (def->ifname != NULL) {
            virBufferVSprintf(buffer, "ethernet%d.connectionType = \"custom\"\n",
                              controller);
            virBufferVSprintf(buffer, "ethernet%d.vnet = \"%s\"\n",
                              controller, def->ifname);
        } else {
            virBufferVSprintf(buffer, "ethernet%d.connectionType = \"bridged\"\n",
                              controller);
        }

        break;

      default:
2783
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, _("Unsupported net type '%s'"),
M
Matthias Bolte 已提交
2784 2785 2786 2787
                  virDomainNetTypeToString(def->type));
        return -1;
    }

2788
    /* def:mac -> vmx:addressType, vmx:(generated)Address, vmx:checkMACAddress */
M
Matthias Bolte 已提交
2789 2790
    virFormatMacAddr(def->mac, mac_string);

2791 2792 2793 2794
    prefix = (def->mac[0] << 16) | (def->mac[1] << 8) | def->mac[2];
    suffix = (def->mac[3] << 16) | (def->mac[4] << 8) | def->mac[5];

    if (prefix == 0x000c29) {
M
Matthias Bolte 已提交
2795 2796 2797 2798
        virBufferVSprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
                          controller);
        virBufferVSprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
                          controller, mac_string);
M
Matthias Bolte 已提交
2799 2800
        virBufferVSprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
                          controller);
2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
    } else if (prefix == 0x005056 && suffix <= 0x3fffff) {
        virBufferVSprintf(buffer, "ethernet%d.addressType = \"static\"\n",
                          controller);
        virBufferVSprintf(buffer, "ethernet%d.address = \"%s\"\n",
                          controller, mac_string);
    } else if (prefix == 0x005056 && suffix >= 0x800000 && suffix <= 0xbfffff) {
        virBufferVSprintf(buffer, "ethernet%d.addressType = \"vpx\"\n",
                          controller);
        virBufferVSprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
                          controller, mac_string);
M
Matthias Bolte 已提交
2811
    } else {
2812 2813 2814 2815 2816 2817
        virBufferVSprintf(buffer, "ethernet%d.addressType = \"static\"\n",
                          controller);
        virBufferVSprintf(buffer, "ethernet%d.address = \"%s\"\n",
                          controller, mac_string);
        virBufferVSprintf(buffer, "ethernet%d.checkMACAddress = \"false\"\n",
                          controller);
M
Matthias Bolte 已提交
2818 2819 2820 2821 2822 2823 2824 2825
    }

    return 0;
}



int
2826 2827
esxVMX_FormatSerial(esxVI_Context *ctx, virDomainChrDefPtr def,
                    virBufferPtr buffer)
M
Matthias Bolte 已提交
2828
{
M
Matthias Bolte 已提交
2829 2830
    char *fileName = NULL;

2831
    if (def->target.port < 0 || def->target.port > 3) {
2832
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2833 2834
                  _("Serial port index %d out of [0..3] range"),
                  def->target.port);
M
Matthias Bolte 已提交
2835 2836 2837 2838
        return -1;
    }

    if (def->data.file.path == NULL) {
2839 2840 2841
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'path' of entry "
                    "'devices/serial/source' to be present"));
M
Matthias Bolte 已提交
2842 2843 2844
        return -1;
    }

2845
    virBufferVSprintf(buffer, "serial%d.present = \"true\"\n", def->target.port);
M
Matthias Bolte 已提交
2846

M
Matthias Bolte 已提交
2847
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
M
Matthias Bolte 已提交
2848 2849 2850
    switch (def->type) {
      case VIR_DOMAIN_CHR_TYPE_DEV:
        virBufferVSprintf(buffer, "serial%d.fileType = \"device\"\n",
2851
                          def->target.port);
M
Matthias Bolte 已提交
2852
        virBufferVSprintf(buffer, "serial%d.fileName = \"%s\"\n",
2853
                          def->target.port, def->data.file.path);
M
Matthias Bolte 已提交
2854 2855 2856 2857
        break;

      case VIR_DOMAIN_CHR_TYPE_FILE:
        virBufferVSprintf(buffer, "serial%d.fileType = \"file\"\n",
2858
                          def->target.port);
M
Matthias Bolte 已提交
2859

2860
        fileName = esxVMX_FormatFileName(ctx, def->data.file.path);
M
Matthias Bolte 已提交
2861 2862 2863 2864 2865 2866

        if (fileName == NULL) {
            return -1;
        }

        virBufferVSprintf(buffer, "serial%d.fileName = \"%s\"\n",
2867
                          def->target.port, fileName);
M
Matthias Bolte 已提交
2868 2869

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
2870 2871 2872 2873
        break;

      case VIR_DOMAIN_CHR_TYPE_PIPE:
        virBufferVSprintf(buffer, "serial%d.fileType = \"pipe\"\n",
2874
                          def->target.port);
M
Matthias Bolte 已提交
2875 2876
        /* FIXME: Based on VI Client GUI default */
        virBufferVSprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
2877
                          def->target.port);
M
Matthias Bolte 已提交
2878 2879
        /* FIXME: Based on VI Client GUI default */
        virBufferVSprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
2880
                          def->target.port);
M
Matthias Bolte 已提交
2881
        virBufferVSprintf(buffer, "serial%d.fileName = \"%s\"\n",
2882
                          def->target.port, def->data.file.path);
M
Matthias Bolte 已提交
2883 2884 2885
        break;

      default:
2886
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2887
                  _("Unsupported character device type '%s'"),
M
Matthias Bolte 已提交
2888 2889 2890 2891 2892 2893 2894
                  virDomainChrTypeToString(def->type));
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
    virBufferVSprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
2895
                      def->target.port);
M
Matthias Bolte 已提交
2896 2897 2898 2899 2900 2901 2902

    return 0;
}



int
2903 2904
esxVMX_FormatParallel(esxVI_Context *ctx, virDomainChrDefPtr def,
                      virBufferPtr buffer)
M
Matthias Bolte 已提交
2905
{
M
Matthias Bolte 已提交
2906 2907
    char *fileName = NULL;

2908
    if (def->target.port < 0 || def->target.port > 2) {
2909
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2910
                  _("Parallel port index %d out of [0..2] range"),
2911
                  def->target.port);
M
Matthias Bolte 已提交
2912 2913 2914 2915
        return -1;
    }

    if (def->data.file.path == NULL) {
2916 2917 2918
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'path' of entry "
                    "'devices/parallel/source' to be present"));
M
Matthias Bolte 已提交
2919 2920 2921
        return -1;
    }

2922 2923
    virBufferVSprintf(buffer, "parallel%d.present = \"true\"\n",
                      def->target.port);
M
Matthias Bolte 已提交
2924

M
Matthias Bolte 已提交
2925
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
M
Matthias Bolte 已提交
2926 2927 2928
    switch (def->type) {
      case VIR_DOMAIN_CHR_TYPE_DEV:
        virBufferVSprintf(buffer, "parallel%d.fileType = \"device\"\n",
2929
                          def->target.port);
M
Matthias Bolte 已提交
2930
        virBufferVSprintf(buffer, "parallel%d.fileName = \"%s\"\n",
2931
                          def->target.port, def->data.file.path);
M
Matthias Bolte 已提交
2932 2933 2934 2935
        break;

      case VIR_DOMAIN_CHR_TYPE_FILE:
        virBufferVSprintf(buffer, "parallel%d.fileType = \"file\"\n",
2936
                          def->target.port);
M
Matthias Bolte 已提交
2937

2938
        fileName = esxVMX_FormatFileName(ctx, def->data.file.path);
M
Matthias Bolte 已提交
2939 2940 2941 2942 2943 2944

        if (fileName == NULL) {
            return -1;
        }

        virBufferVSprintf(buffer, "parallel%d.fileName = \"%s\"\n",
2945
                          def->target.port, fileName);
M
Matthias Bolte 已提交
2946 2947

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
2948 2949 2950
        break;

      default:
2951
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2952
                  _("Unsupported character device type '%s'"),
M
Matthias Bolte 已提交
2953 2954 2955 2956 2957 2958
                  virDomainChrTypeToString(def->type));
        return -1;
    }

    return 0;
}