esx_vmx.c 112.8 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
 * 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>

26 27
#include <c-ctype.h>

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

/*

mapping:

domain-xml                        <=>   vmx


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


???                               <=>   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 已提交
66
->arch = <arch>                   <=>   guestOS = "<value>"                     # if <value> ends with -64 than <arch> is x86_64, otherwise <arch> is i686
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
->machine
->nBootDevs
->bootDevs
->init
->kernel
->initrd
->cmdline
->root
->loader
->bootloader
->bootloaderArgs



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

84 85 86
                                        scsi[0..3]:[0..6,8..15] -> <controller>:<unit> with 1 bus per controller
                                        ide[0..1]:[0..1]        -> <bus>:<unit> with 1 controller
                                        floppy[0..1]            -> <unit> with 1 controller and 1 bus per controller
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

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"
106
->dst = sd[<controller> * 15 + <unit> mapped to [a-z]+]
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
->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"
130
->dst = hd[<bus> * 2 + <unit> mapped to [a-z]+]
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
->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"
150
->dst = sd[<controller> * 15 + <unit> mapped to [a-z]+]
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
->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"
169
->dst = hd[<bus> * 2 + <unit> mapped to [a-z]+]
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
->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" ?
189
->dst = sd[<controller> * 15 + <unit> mapped to [a-z]+]
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
->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"
209
->dst = hd[<bus> * 2 + <unit> mapped to [a-z]+]
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
->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"
229
->dst = fd[<unit> mapped to [a-z]+]
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
->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"
249
->dst = fd[<unit> mapped to [a-z]+]
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
->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
271
                                        ethernet0.features = "15"               # if present and virtualDev is "vmxnet" => vmxnet2 (enhanced)
272 273 274 275


                                        ethernet0.addressType = "generated"     # default to "generated"
->mac = <value>                   <=>   ethernet0.generatedAddress = "<value>"
276
                                        ethernet0.generatedAddressOffset = "0"  # ?
277 278 279 280 281


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

282 283 284 285

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

286 287 288 289 290

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

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


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

...
->type = _NET_TYPE_BRIDGE         <=>   ethernet0.connectionType = "bridged"    # defaults to "bridged"
M
Matthias Bolte 已提交
302
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
303 304 305 306


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

M
Matthias Bolte 已提交
307
...                                                                             # FIXME: Investigate if ESX supports this
308 309 310 311 312
->type = _NET_TYPE_NETWORK        <=>   ethernet0.connectionType = "hostonly"   # defaults to "bridged"


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

M
Matthias Bolte 已提交
313
...                                                                             # FIXME: Investigate if ESX supports this
314 315 316 317 318 319 320
->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 已提交
321 322
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
->ifname = <value>                <=>   ethernet0.vnet = "<value>"
323 324 325 326 327 328 329 330 331 332 333 334



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

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

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

def->serials[0]...
335
->target.port = <port>
336 337 338 339 340 341 342


## 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 已提交
343
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
344 345 346 347 348 349 350


## 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 已提交
351
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
352 353 354 355 356 357


## 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 已提交
358
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
359
???                               <=>   serial0.tryNoRxLoss = "true"            # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
360
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
361 362 363

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


## 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 已提交
373
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
374
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
375
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
376 377 378

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



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

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

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

def->parallels[0]...
394
->target.port = <port>
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409


## 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 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424


################################################################################
## 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

425 426 427 428 429
*/

#define VIR_FROM_THIS VIR_FROM_ESX

#define ESX_BUILD_VMX_NAME(_suffix)                                           \
430
    snprintf(_suffix##_name, sizeof(_suffix##_name), "%s."#_suffix, prefix);
431

432 433 434 435 436
/* directly map the virDomainControllerModel to esxVMX_SCSIControllerModel,
 * this is good enough for now because all virDomainControllerModel values
 * are actually SCSI controller models in the ESX case */
VIR_ENUM_DECL(esxVMX_SCSIControllerModel)
VIR_ENUM_IMPL(esxVMX_SCSIControllerModel, VIR_DOMAIN_CONTROLLER_MODEL_LAST,
437
              "auto", /* just to match virDomainControllerModel, will never be used */
438 439 440 441
              "buslogic",
              "lsilogic",
              "lsisas1068",
              "pvscsi");
442 443


M
Matthias Bolte 已提交
444
int
445
esxVMX_SCSIDiskNameToControllerAndUnit(const char *name, int *controller, int *unit)
M
Matthias Bolte 已提交
446 447 448 449
{
    int idx;

    if (! STRPREFIX(name, "sd")) {
450 451 452
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'sd'"));
M
Matthias Bolte 已提交
453 454 455 456 457 458
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
459
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
460
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
461 462 463
        return -1;
    }

464
    /* Each of the 4 SCSI controllers has 1 bus with 15 units each for devices */
M
Matthias Bolte 已提交
465
    if (idx >= (4 * 15)) {
466
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
467
                  _("SCSI disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
468 469 470 471
        return -1;
    }

    *controller = idx / 15;
472
    *unit = idx % 15;
M
Matthias Bolte 已提交
473

474 475 476
    /* Skip the controller ifself at unit 7 */
    if (*unit >= 7) {
        ++(*unit);
M
Matthias Bolte 已提交
477 478 479 480 481 482 483 484
    }

    return 0;
}



int
485
esxVMX_IDEDiskNameToBusAndUnit(const char *name, int *bus, int *unit)
M
Matthias Bolte 已提交
486 487 488 489
{
    int idx;

    if (! STRPREFIX(name, "hd")) {
490 491 492
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'hd'"));
M
Matthias Bolte 已提交
493 494 495 496 497 498
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
499
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
500
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
501 502 503
        return -1;
    }

504
    /* The IDE controller has 2 buses with 2 units each for devices */
M
Matthias Bolte 已提交
505
    if (idx >= (2 * 2)) {
506
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
507
                  _("IDE disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
508 509 510
        return -1;
    }

511 512
    *bus = idx / 2;
    *unit = idx % 2;
M
Matthias Bolte 已提交
513 514 515 516 517 518 519

    return 0;
}



int
520
esxVMX_FloppyDiskNameToUnit(const char *name, int *unit)
M
Matthias Bolte 已提交
521 522 523 524
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
525 526 527
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
528 529 530 531 532 533
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
534
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
535
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
536 537 538
        return -1;
    }

539
    /* The FDC controller has 1 bus with 2 units for devices */
M
Matthias Bolte 已提交
540
    if (idx >= 2) {
541
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
542
                  _("Floppy disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
543 544 545
        return -1;
    }

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    *unit = idx;

    return 0;
}



int
esxVMX_VerifyDiskAddress(virCapsPtr caps, virDomainDiskDefPtr disk)
{
    virDomainDiskDef def;
    virDomainDeviceDriveAddressPtr drive;

    memset(&def, 0, sizeof(def));

    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Unsupported disk address type '%s'"),
                  virDomainDeviceAddressTypeToString(disk->info.type));
        return -1;
    }

    drive = &disk->info.addr.drive;

    def.dst = disk->dst;
    def.bus = disk->bus;

    if (virDomainDiskDefAssignAddress(caps, &def) < 0) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Could not verify disk address"));
        return -1;
    }

    if (def.info.addr.drive.controller != drive->controller ||
        def.info.addr.drive.bus != drive->bus ||
        def.info.addr.drive.unit != drive->unit) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Disk address %d:%d:%d doesn't match target device '%s'"),
                  drive->controller, drive->bus, drive->unit, disk->dst);
        return -1;
    }

    /* drive->{controller|bus|unit} is unsigned, no >= 0 checks are necessary */
    if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (drive->controller > 3) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("SCSI controller index %d out of [0..3] range"),
                      drive->controller);
            return -1;
        }

        if (drive->bus != 0) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("SCSI bus index %d out of [0] range"),
                      drive->bus);
            return -1;
        }

        if (drive->unit > 15 || drive->unit == 7) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("SCSI unit index %d out of [0..6,8..15] range"),
                      drive->unit);
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        if (drive->controller != 0) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("IDE controller index %d out of [0] range"),
                      drive->controller);
            return -1;
        }

        if (drive->bus > 1) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("IDE bus index %d out of [0..1] range"),
                      drive->bus);
            return -1;
        }

        if (drive->unit > 1) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("IDE unit index %d out of [0..1] range"),
                      drive->unit);
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
        if (drive->controller != 0) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("FDC controller index %d out of [0] range"),
                      drive->controller);
            return -1;
        }

        if (drive->bus != 0) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("FDC bus index %d out of [0] range"),
                      drive->bus);
            return -1;
        }

        if (drive->unit > 1) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("FDC unit index %d out of [0..1] range"),
                      drive->unit);
            return -1;
        }
    } else {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Unsupported bus type '%s'"),
                  virDomainDiskBusTypeToString(disk->bus));
        return -1;
    }

    return 0;
}



int
esxVMX_HandleLegacySCSIDiskDriverName(virDomainDefPtr def,
                                      virDomainDiskDefPtr disk)
{
    char *tmp;
    int model, i;
    virDomainControllerDefPtr controller = NULL;

    if (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI || disk->driverName == NULL) {
        return 0;
    }

    tmp = disk->driverName;

    for (; *tmp != '\0'; ++tmp) {
        *tmp = c_tolower(*tmp);
    }

    model = virDomainControllerModelTypeFromString(disk->driverName);

    if (model < 0) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Unknown driver name '%s'"), disk->driverName);
        return -1;
    }

    for (i = 0; i < def->ncontrollers; ++i) {
        if (def->controllers[i]->idx == disk->info.addr.drive.controller) {
            controller = def->controllers[i];
            break;
        }
    }

    if (controller == NULL) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Missing SCSI controller for index %d"),
                  disk->info.addr.drive.controller);
        return -1;
    }

    if (controller->model == -1) {
        controller->model = model;
    } else if (controller->model != model) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Inconsistent SCSI controller model ('%s' is not '%s') "
                    "for SCSI controller index %d"), disk->driverName,
                  virDomainControllerModelTypeToString(controller->model),
                  controller->idx);
        return -1;
    }
M
Matthias Bolte 已提交
714 715 716 717 718 719 720

    return 0;
}



int
721 722
esxVMX_AutodetectSCSIControllerModel(esxVI_Context *ctx,
                                     virDomainDiskDefPtr def, int *model)
M
Matthias Bolte 已提交
723
{
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    int result = -1;
    char *datastoreName = NULL;
    char *directoryName = NULL;
    char *fileName = NULL;
    char *datastorePath = NULL;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *datastore = NULL;
    esxVI_ManagedObjectReference *hostDatastoreBrowser = NULL;
    esxVI_HostDatastoreBrowserSearchSpec *searchSpec = NULL;
    esxVI_VmDiskFileQuery *vmDiskFileQuery = NULL;
    esxVI_ManagedObjectReference *task = NULL;
    esxVI_TaskInfoState taskInfoState;
    esxVI_TaskInfo *taskInfo = NULL;
    esxVI_HostDatastoreBrowserSearchResults *searchResults = NULL;
    esxVI_VmDiskFileInfo *vmDiskFileInfo = NULL;

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK ||
        def->bus != VIR_DOMAIN_DISK_BUS_SCSI ||
        def->type != VIR_DOMAIN_DISK_TYPE_FILE ||
        def->src == NULL ||
        ! STRPREFIX(def->src, "[")) {
        /*
         * This isn't a file-based SCSI disk device with a datastore related
         * source path => do nothing.
         */
        return 0;
    }

    if (esxUtil_ParseDatastoreRelatedPath(def->src, &datastoreName,
                                          &directoryName, &fileName) < 0) {
        goto cleanup;
    }

    if (directoryName == NULL) {
        if (virAsprintf(&datastorePath, "[%s]", datastoreName) < 0) {
            virReportOOMError();
            goto cleanup;
        }
    } else {
        if (virAsprintf(&datastorePath, "[%s] %s", datastoreName,
                        directoryName) < 0) {
            virReportOOMError();
            goto cleanup;
        }
    }

    /* Lookup HostDatastoreBrowser */
    if (esxVI_String_AppendValueToList(&propertyNameList, "browser") < 0 ||
        esxVI_LookupDatastoreByName(ctx, datastoreName, propertyNameList,
                                    &datastore,
                                    esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetManagedObjectReference(datastore, "browser",
                                        &hostDatastoreBrowser,
                                        esxVI_Occurrence_RequiredItem) < 0) {
        goto cleanup;
    }

    /* Build HostDatastoreBrowserSearchSpec */
    if (esxVI_HostDatastoreBrowserSearchSpec_Alloc(&searchSpec) < 0 ||
        esxVI_FileQueryFlags_Alloc(&searchSpec->details) < 0) {
        goto cleanup;
    }

    searchSpec->details->fileType = esxVI_Boolean_True;
    searchSpec->details->fileSize = esxVI_Boolean_False;
    searchSpec->details->modification = esxVI_Boolean_False;

    if (esxVI_VmDiskFileQuery_Alloc(&vmDiskFileQuery) < 0 ||
        esxVI_VmDiskFileQueryFlags_Alloc(&vmDiskFileQuery->details) < 0 ||
        esxVI_FileQuery_AppendToList
          (&searchSpec->query,
           esxVI_FileQuery_DynamicCast(vmDiskFileQuery)) < 0) {
        goto cleanup;
    }

    vmDiskFileQuery->details->diskType = esxVI_Boolean_False;
    vmDiskFileQuery->details->capacityKb = esxVI_Boolean_False;
    vmDiskFileQuery->details->hardwareVersion = esxVI_Boolean_False;
    vmDiskFileQuery->details->controllerType = esxVI_Boolean_True;
    vmDiskFileQuery->details->diskExtents = esxVI_Boolean_False;

    if (esxVI_String_Alloc(&searchSpec->matchPattern) < 0) {
        goto cleanup;
    }

    searchSpec->matchPattern->value = fileName;

    /* Search datastore for file */
    if (esxVI_SearchDatastore_Task(ctx, hostDatastoreBrowser, datastorePath,
                                   searchSpec, &task) < 0 ||
        esxVI_WaitForTaskCompletion(ctx, task, NULL, esxVI_Boolean_False,
                                    &taskInfoState) < 0) {
        goto cleanup;
    }

    if (taskInfoState != esxVI_TaskInfoState_Success) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Could not serach in datastore '%s'"), datastoreName);
        goto cleanup;
    }

    if (esxVI_LookupTaskInfoByTask(ctx, task, &taskInfo) < 0 ||
        esxVI_HostDatastoreBrowserSearchResults_CastFromAnyType
          (taskInfo->result, &searchResults) < 0) {
        goto cleanup;
    }

    /* Interpret search result */
    vmDiskFileInfo = esxVI_VmDiskFileInfo_DynamicCast(searchResults->file);

    if (vmDiskFileInfo == NULL || vmDiskFileInfo->controllerType == NULL) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Could not lookup controller model for '%s'"), def->src);
        goto cleanup;
    }

    if (STRCASEEQ(vmDiskFileInfo->controllerType,
                  "VirtualBusLogicController")) {
        *model = VIR_DOMAIN_CONTROLLER_MODEL_BUSLOGIC;
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "VirtualLsiLogicController")) {
        *model = VIR_DOMAIN_CONTROLLER_MODEL_LSILOGIC;
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "VirtualLsiLogicSASController")) {
        *model = VIR_DOMAIN_CONTROLLER_MODEL_LSISAS1068;
    } else if (STRCASEEQ(vmDiskFileInfo->controllerType,
                         "ParaVirtualSCSIController")) {
        *model = VIR_DOMAIN_CONTROLLER_MODEL_VMPVSCSI;
    } else {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Found unexpected controller model '%s' for disk '%s'"),
                  vmDiskFileInfo->controllerType, def->src);
        goto cleanup;
    }

    result = 0;

  cleanup:
    /* Don't double free fileName */
    if (searchSpec != NULL && searchSpec->matchPattern != NULL) {
        searchSpec->matchPattern->value = NULL;
    }

    VIR_FREE(datastoreName);
    VIR_FREE(directoryName);
    VIR_FREE(fileName);
    VIR_FREE(datastorePath);
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&datastore);
    esxVI_ManagedObjectReference_Free(&hostDatastoreBrowser);
    esxVI_HostDatastoreBrowserSearchSpec_Free(&searchSpec);
    esxVI_ManagedObjectReference_Free(&task);
    esxVI_TaskInfo_Free(&taskInfo);
    esxVI_HostDatastoreBrowserSearchResults_Free(&searchResults);

    return result;
}



int
esxVMX_GatherSCSIControllers(esxVI_Context *ctx, virDomainDefPtr def,
                             int virtualDev[4], bool present[4])
{
    int result = -1;
    int i, k;
M
Matthias Bolte 已提交
890
    virDomainDiskDefPtr disk;
891 892 893 894
    virDomainControllerDefPtr controller;
    bool controllerHasDisksAttached;
    int count = 0;
    int *autodetectedModels;
M
Matthias Bolte 已提交
895

896 897 898 899 900 901 902
    if (VIR_ALLOC_N(autodetectedModels, def->ndisks) < 0) {
        virReportOOMError();
        return -1;
    }

    for (i = 0; i < def->ncontrollers; ++i) {
        controller = def->controllers[i];
M
Matthias Bolte 已提交
903

904 905
        if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
            // skip non-SCSI controllers
M
Matthias Bolte 已提交
906 907 908
            continue;
        }

909
        controllerHasDisksAttached = false;
910

911 912 913 914 915 916
        for (k = 0; k < def->ndisks; ++k) {
            disk = def->disks[k];

            if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI &&
                disk->info.addr.drive.controller == controller->idx) {
                controllerHasDisksAttached = true;
917 918
                break;
            }
M
Matthias Bolte 已提交
919 920
        }

921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
        if (! controllerHasDisksAttached) {
            // skip SCSI controllers without attached disks
            continue;
        }

        if (ctx != NULL &&
            controller->model == VIR_DOMAIN_CONTROLLER_MODEL_AUTO) {
            count = 0;

            // try to autodetect the SCSI controller model by collecting
            // SCSI controller model of all disks attached to this controller
            for (k = 0; k < def->ndisks; ++k) {
                disk = def->disks[k];

                if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI &&
                    disk->info.addr.drive.controller == controller->idx) {
                    if (esxVMX_AutodetectSCSIControllerModel
                          (ctx, disk, &autodetectedModels[count]) < 0) {
                        goto cleanup;
                    }

                    ++count;
                }
            }

            // autodetection fails when the disks attached to one controller
            // have inconsistent SCSI controller models
            for (k = 0; k < count; ++k) {
                if (autodetectedModels[k] != autodetectedModels[0]) {
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                              _("Disks on SCSI controller %d have inconsistent "
                                "controller models, cannot autodetect model"),
                              controller->idx);
                    goto cleanup;
                }
            }

            controller->model = autodetectedModels[0];
M
Matthias Bolte 已提交
959 960
        }

961 962 963
        if (controller->model != -1 &&
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_BUSLOGIC &&
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_LSILOGIC &&
964 965
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_LSISAS1068 &&
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_VMPVSCSI) {
966
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
967 968
                      _("Expecting domain XML attribute 'model' of entry "
                        "'controller' to be 'buslogic' or 'lsilogic' or "
969
                        "'lsisas1068' or 'vmpvscsi' but found '%s'"),
970
                      virDomainControllerModelTypeToString(controller->model));
971
            goto cleanup;
M
Matthias Bolte 已提交
972
        }
973 974 975

        present[controller->idx] = true;
        virtualDev[controller->idx] = controller->model;
M
Matthias Bolte 已提交
976 977
    }

978 979 980 981 982 983
    result = 0;

  cleanup:
    VIR_FREE(autodetectedModels);

    return result;
M
Matthias Bolte 已提交
984 985 986 987
}



M
Matthias Bolte 已提交
988
char *
989
esxVMX_AbsolutePathToDatastoreRelatedPath(esxVI_Context *ctx,
M
Matthias Bolte 已提交
990 991
                                          const char *absolutePath)
{
M
Matthias Bolte 已提交
992
    bool success = false;
993 994 995
    char *copyOfAbsolutePath = NULL;
    char *tmp = NULL;
    char *saveptr = NULL;
M
Matthias Bolte 已提交
996 997 998 999 1000 1001 1002
    char *datastoreRelatedPath = NULL;
    char *preliminaryDatastoreName = NULL;
    char *directoryAndFileName = NULL;
    esxVI_DynamicProperty *dynamicProperty = NULL;
    esxVI_ObjectContent *datastore = NULL;
    const char *datastoreName = NULL;

1003
    if (esxVI_String_DeepCopyValue(&copyOfAbsolutePath, absolutePath) < 0) {
M
Matthias Bolte 已提交
1004
        return NULL;
1005 1006 1007 1008 1009 1010
    }

    /* 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) {
1011
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1012 1013
                  _("Absolute path '%s' doesn't have expected format "
                    "'/vmfs/volumes/<datastore>/<path>'"), absolutePath);
M
Matthias Bolte 已提交
1014
        goto cleanup;
M
Matthias Bolte 已提交
1015 1016 1017
    }

    if (ctx != NULL) {
1018
        if (esxVI_LookupDatastoreByName(ctx, preliminaryDatastoreName,
M
Matthias Bolte 已提交
1019
                                        NULL, &datastore,
M
Matthias Bolte 已提交
1020
                                        esxVI_Occurrence_OptionalItem) < 0) {
M
Matthias Bolte 已提交
1021
            goto cleanup;
M
Matthias Bolte 已提交
1022 1023
        }

1024 1025 1026 1027 1028 1029
        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")) {
1030
                    if (esxVI_AnyType_ExpectType(dynamicProperty->val,
1031
                                                 esxVI_Type_String) < 0) {
M
Matthias Bolte 已提交
1032
                        goto cleanup;
1033 1034 1035 1036 1037 1038 1039 1040
                    }

                    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 已提交
1041 1042 1043 1044 1045
                }
            }
        }

        if (datastoreName == NULL) {
1046 1047 1048 1049 1050
            VIR_WARN("Could not retrieve datastore name for absolute "
                     "path '%s', falling back to preliminary name '%s'",
                     absolutePath, preliminaryDatastoreName);

            datastoreName = preliminaryDatastoreName;
M
Matthias Bolte 已提交
1051 1052 1053 1054 1055 1056 1057
        }
    } else {
        datastoreName = preliminaryDatastoreName;
    }

    if (virAsprintf(&datastoreRelatedPath, "[%s] %s", datastoreName,
                    directoryAndFileName) < 0) {
1058
        virReportOOMError();
M
Matthias Bolte 已提交
1059
        goto cleanup;
M
Matthias Bolte 已提交
1060 1061 1062 1063
    }

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

M
Matthias Bolte 已提交
1064 1065
    success = true;

M
Matthias Bolte 已提交
1066
  cleanup:
M
Matthias Bolte 已提交
1067 1068 1069 1070
    if (! success) {
        VIR_FREE(datastoreRelatedPath);
    }

1071
    VIR_FREE(copyOfAbsolutePath);
M
Matthias Bolte 已提交
1072 1073 1074 1075 1076 1077 1078
    esxVI_ObjectContent_Free(&datastore);

    return datastoreRelatedPath;
}



M
Matthias Bolte 已提交
1079 1080 1081 1082
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * VMX -> Domain XML
 */

M
Matthias Bolte 已提交
1083
char *
1084 1085
esxVMX_ParseFileName(esxVI_Context *ctx, const char *fileName,
                     const char *datastoreName, const char *directoryName)
M
Matthias Bolte 已提交
1086 1087 1088 1089 1090
{
    char *src = NULL;

    if (STRPREFIX(fileName, "/vmfs/volumes/")) {
        /* Found absolute path referencing a file inside a datastore */
1091
        return esxVMX_AbsolutePathToDatastoreRelatedPath(ctx, fileName);
M
Matthias Bolte 已提交
1092 1093 1094 1095 1096
    } else if (STRPREFIX(fileName, "/")) {
        /* Found absolute path referencing a file outside a datastore */
        src = strdup(fileName);

        if (src == NULL) {
1097
            virReportOOMError();
M
Matthias Bolte 已提交
1098 1099 1100 1101 1102 1103 1104 1105
            return NULL;
        }

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

        return src;
    } else if (strchr(fileName, '/') != NULL) {
        /* Found relative path, this is not supported */
1106
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1107 1108
                  _("Found relative path '%s' in VMX file, this is not "
                    "supported"), fileName);
M
Matthias Bolte 已提交
1109 1110 1111 1112 1113
        return NULL;
    } else {
        /* Found single file name referencing a file inside a datastore */
        if (virAsprintf(&src, "[%s] %s/%s", datastoreName, directoryName,
                        fileName) < 0) {
1114
            virReportOOMError();
M
Matthias Bolte 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
            return NULL;
        }

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

        return src;
    }
}



1126
virDomainDefPtr
1127
esxVMX_ParseConfig(esxVI_Context *ctx, virCapsPtr caps, const char *vmx,
M
Matthias Bolte 已提交
1128
                   const char *datastoreName, const char *directoryName,
1129
                   esxVI_ProductVersion productVersion)
1130
{
M
Matthias Bolte 已提交
1131
    bool success = false;
1132 1133 1134 1135 1136 1137 1138 1139
    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 已提交
1140
    char *guestOS = NULL;
1141
    int controller;
1142
    int bus;
1143
    int port;
1144
    bool present;
1145 1146
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
    int unit;
1147 1148 1149 1150 1151 1152 1153 1154

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

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

    if (VIR_ALLOC(def) < 0) {
1155
        virReportOOMError();
M
Matthias Bolte 已提交
1156
        return NULL;
1157 1158 1159 1160 1161
    }

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

M
Matthias Bolte 已提交
1162
    /* vmx:config.version */
1163
    if (esxUtil_GetConfigLong(conf, "config.version", &config_version, 0,
1164
                              false) < 0) {
M
Matthias Bolte 已提交
1165
        goto cleanup;
1166 1167 1168
    }

    if (config_version != 8) {
1169
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1170 1171
                  _("Expecting VMX entry 'config.version' to be 8 but found "
                    "%lld"), config_version);
M
Matthias Bolte 已提交
1172
        goto cleanup;
1173 1174
    }

M
Matthias Bolte 已提交
1175
    /* vmx:virtualHW.version */
1176
    if (esxUtil_GetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
1177
                              false) < 0) {
M
Matthias Bolte 已提交
1178
        goto cleanup;
1179 1180
    }

1181 1182 1183 1184 1185 1186
    /*
     * virtualHW.version compatibility matrix:
     *
     *              4 7    API
     *   ESX 3.5    +      2.5
     *   ESX 4.0    + +    4.0
M
Matthias Bolte 已提交
1187
     *   ESX 4.1    + +    4.1
1188 1189 1190 1191
     *   GSX 2.0    + +    2.5
     */
    switch (productVersion) {
      case esxVI_ProductVersion_ESX35:
1192
      case esxVI_ProductVersion_VPX25:
1193
        if (virtualHW_version != 4) {
1194
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1195 1196
                      _("Expecting VMX entry 'virtualHW.version' to be 4 "
                        "but found %lld"),
1197
                      virtualHW_version);
M
Matthias Bolte 已提交
1198
            goto cleanup;
1199 1200 1201 1202
        }

        break;

1203 1204
      case esxVI_ProductVersion_GSX20:
      case esxVI_ProductVersion_ESX40:
M
Matthias Bolte 已提交
1205
      case esxVI_ProductVersion_ESX41:
1206
      case esxVI_ProductVersion_VPX40:
M
Matthias Bolte 已提交
1207
      case esxVI_ProductVersion_VPX41:
1208
        if (virtualHW_version != 4 && virtualHW_version != 7) {
1209
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1210
                      _("Expecting VMX entry 'virtualHW.version' to be 4 or 7 "
1211
                        "but found %lld"),
1212
                      virtualHW_version);
M
Matthias Bolte 已提交
1213
            goto cleanup;
1214 1215 1216 1217
        }

        break;

M
Matthias Bolte 已提交
1218 1219 1220 1221
      case esxVI_ProductVersion_ESX4x:
      case esxVI_ProductVersion_VPX4x:
        break;

1222
      default:
1223
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
1224
                  _("Unexpected product version"));
M
Matthias Bolte 已提交
1225
        goto cleanup;
1226 1227
    }

M
Matthias Bolte 已提交
1228
    /* vmx:uuid.bios -> def:uuid */
1229
    /* FIXME: Need to handle 'uuid.action = "create"' */
1230
    if (esxUtil_GetConfigUUID(conf, "uuid.bios", def->uuid, true) < 0) {
M
Matthias Bolte 已提交
1231
        goto cleanup;
1232 1233
    }

M
Matthias Bolte 已提交
1234
    /* vmx:displayName -> def:name */
1235
    if (esxUtil_GetConfigString(conf, "displayName", &def->name, true) < 0) {
M
Matthias Bolte 已提交
1236
        goto cleanup;
1237 1238
    }

M
Matthias Bolte 已提交
1239
    /* vmx:memsize -> def:maxmem */
1240
    if (esxUtil_GetConfigLong(conf, "memsize", &memsize, 32, true) < 0) {
M
Matthias Bolte 已提交
1241
        goto cleanup;
1242 1243 1244
    }

    if (memsize <= 0 || memsize % 4 != 0) {
1245
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1246 1247
                  _("Expecting VMX entry 'memsize' to be an unsigned "
                    "integer (multiple of 4) but found %lld"), memsize);
M
Matthias Bolte 已提交
1248
        goto cleanup;
1249 1250 1251 1252
    }

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

M
Matthias Bolte 已提交
1253
    /* vmx:sched.mem.max -> def:memory */
1254 1255
    if (esxUtil_GetConfigLong(conf, "sched.mem.max", &memory, memsize,
                              true) < 0) {
M
Matthias Bolte 已提交
1256
        goto cleanup;
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    }

    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 已提交
1269
    /* vmx:numvcpus -> def:vcpus */
1270
    if (esxUtil_GetConfigLong(conf, "numvcpus", &numvcpus, 1, true) < 0) {
M
Matthias Bolte 已提交
1271
        goto cleanup;
1272 1273 1274
    }

    if (numvcpus <= 0 || (numvcpus % 2 != 0 && numvcpus != 1)) {
1275
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1276 1277
                  _("Expecting VMX entry 'numvcpus' to be an unsigned "
                    "integer (1 or a multiple of 2) but found %lld"), numvcpus);
M
Matthias Bolte 已提交
1278
        goto cleanup;
1279 1280 1281 1282
    }

    def->vcpus = numvcpus;

M
Matthias Bolte 已提交
1283
    /* vmx:sched.cpu.affinity -> def:cpumask */
1284
    // VirtualMachine:config.cpuAffinity.affinitySet
1285
    if (esxUtil_GetConfigString(conf, "sched.cpu.affinity", &sched_cpu_affinity,
1286
                                true) < 0) {
M
Matthias Bolte 已提交
1287
        goto cleanup;
1288 1289 1290 1291 1292 1293 1294 1295 1296
    }

    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) {
1297
            virReportOOMError();
M
Matthias Bolte 已提交
1298
            goto cleanup;
1299 1300 1301 1302 1303 1304 1305 1306
        }

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

            number = virParseNumber(&current);

            if (number < 0) {
1307
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1308 1309 1310
                          _("Expecting VMX entry 'sched.cpu.affinity' to be "
                            "a comma separated list of unsigned integers but "
                            "found '%s'"), sched_cpu_affinity);
M
Matthias Bolte 已提交
1311
                goto cleanup;
1312 1313 1314
            }

            if (number >= VIR_DOMAIN_CPUMASK_LEN) {
1315
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1316 1317
                          _("VMX entry 'sched.cpu.affinity' contains a %d, "
                            "this value is too large"), number);
M
Matthias Bolte 已提交
1318
                goto cleanup;
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
            }

            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 {
1335
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1336 1337 1338
                          _("Expecting VMX entry 'sched.cpu.affinity' to be "
                            "a comma separated list of unsigned integers but "
                            "found '%s'"), sched_cpu_affinity);
M
Matthias Bolte 已提交
1339
                goto cleanup;
1340 1341 1342 1343 1344 1345
            }

            virSkipSpaces(&current);
        }

        if (count < numvcpus) {
1346
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1347 1348 1349
                      _("Expecting VMX entry 'sched.cpu.affinity' to contain "
                        "at least as many values as 'numvcpus' (%lld) but "
                        "found only %d value(s)"), numvcpus, count);
M
Matthias Bolte 已提交
1350
            goto cleanup;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
        }
    }

    /* 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) {
1363
        virReportOOMError();
M
Matthias Bolte 已提交
1364
        goto cleanup;
1365 1366
    }

M
Matthias Bolte 已提交
1367
    /* vmx:guestOS -> def:os.arch */
1368
    if (esxUtil_GetConfigString(conf, "guestOS", &guestOS, true) < 0) {
M
Matthias Bolte 已提交
1369
        goto cleanup;
M
Matthias Bolte 已提交
1370 1371
    }

1372
    if (guestOS != NULL && virFileHasSuffix(guestOS, "-64")) {
M
Matthias Bolte 已提交
1373 1374 1375 1376 1377 1378
        def->os.arch = strdup("x86_64");
    } else {
        def->os.arch = strdup("i686");
    }

    if (def->os.arch == NULL) {
1379
        virReportOOMError();
M
Matthias Bolte 已提交
1380
        goto cleanup;
M
Matthias Bolte 已提交
1381 1382
    }

1383 1384
    /* def:features */
    /* FIXME */
1385

1386 1387
    /* def:clock */
    /* FIXME */
1388 1389

    /* def:graphics */
M
Matthias Bolte 已提交
1390
    if (VIR_ALLOC_N(def->graphics, 1) < 0) {
1391
        virReportOOMError();
M
Matthias Bolte 已提交
1392
        goto cleanup;
M
Matthias Bolte 已提交
1393 1394 1395 1396
    }

    def->ngraphics = 0;

1397
    if (esxVMX_ParseVNC(conf, &def->graphics[def->ngraphics]) < 0) {
M
Matthias Bolte 已提交
1398
        goto cleanup;
M
Matthias Bolte 已提交
1399 1400 1401 1402 1403
    }

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

M
Matthias Bolte 已提交
1405 1406
    /* def:disks: 4 * 15 scsi + 2 * 2 ide + 2 floppy = 66 */
    if (VIR_ALLOC_N(def->disks, 66) < 0) {
1407
        virReportOOMError();
M
Matthias Bolte 已提交
1408
        goto cleanup;
1409 1410 1411 1412 1413 1414
    }

    def->ndisks = 0;

    /* def:disks (scsi) */
    for (controller = 0; controller < 4; ++controller) {
1415
        if (esxVMX_ParseSCSIController(conf, controller, &present,
1416
                                       &scsi_virtualDev[controller]) < 0) {
M
Matthias Bolte 已提交
1417
            goto cleanup;
1418 1419 1420 1421 1422 1423
        }

        if (! present) {
            continue;
        }

1424 1425
        for (unit = 0; unit < 16; ++unit) {
            if (unit == 7) {
1426
                /*
1427
                 * SCSI unit 7 is assigned to the SCSI controller and cannot be
1428 1429 1430 1431 1432
                 * used for disk devices.
                 */
                continue;
            }

1433 1434 1435
            if (esxVMX_ParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
                                 datastoreName, directoryName,
1436
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1437
                goto cleanup;
1438 1439 1440 1441 1442 1443 1444
            }

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

1445 1446 1447
            if (esxVMX_ParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
                                 datastoreName, directoryName,
1448
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1449
                goto cleanup;
1450 1451 1452 1453 1454 1455 1456 1457 1458
            }

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

    /* def:disks (ide) */
1459 1460 1461 1462 1463
    for (bus = 0; bus < 2; ++bus) {
        for (unit = 0; unit < 2; ++unit) {
            if (esxVMX_ParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
                                 VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
                                 datastoreName, directoryName,
M
Matthias Bolte 已提交
1464
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1465
                goto cleanup;
1466 1467 1468 1469 1470 1471 1472
            }

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

1473 1474 1475
            if (esxVMX_ParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
                                 VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
                                 datastoreName, directoryName,
M
Matthias Bolte 已提交
1476
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1477
                goto cleanup;
1478 1479 1480 1481 1482 1483 1484 1485 1486
            }

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

    /* def:disks (floppy) */
1487 1488 1489
    for (unit = 0; unit < 2; ++unit) {
        if (esxVMX_ParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_FLOPPY,
                             VIR_DOMAIN_DISK_BUS_FDC, 0, unit,
M
Matthias Bolte 已提交
1490
                             datastoreName, directoryName,
1491
                             &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1492
            goto cleanup;
1493 1494 1495 1496 1497 1498 1499
        }

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

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
    /* def:controllers */
    if (virDomainDefAddImplicitControllers(def) < 0) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not add controllers"));
        goto cleanup;
    }

    for (controller = 0; controller < def->ncontrollers; ++controller) {
        if (def->controllers[controller]->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
            if (def->controllers[controller]->idx < 0 ||
                def->controllers[controller]->idx > 3) {
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                          _("SCSI controller index %d out of [0..3] range"),
                          def->controllers[controller]->idx);
                goto cleanup;
            }

            def->controllers[controller]->model =
              scsi_virtualDev[def->controllers[controller]->idx];
        }
    }

1521 1522 1523 1524 1525
    /* def:fss */
    /* FIXME */

    /* def:nets */
    if (VIR_ALLOC_N(def->nets, 4) < 0) {
1526
        virReportOOMError();
M
Matthias Bolte 已提交
1527
        goto cleanup;
1528 1529 1530 1531 1532
    }

    def->nnets = 0;

    for (controller = 0; controller < 4; ++controller) {
1533
        if (esxVMX_ParseEthernet(conf, controller,
1534
                                 &def->nets[def->nnets]) < 0) {
M
Matthias Bolte 已提交
1535
            goto cleanup;
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
        }

        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) {
1554
        virReportOOMError();
M
Matthias Bolte 已提交
1555
        goto cleanup;
1556 1557 1558 1559 1560
    }

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1561
        if (esxVMX_ParseSerial(ctx, conf, port, datastoreName, directoryName,
1562
                               &def->serials[def->nserials]) < 0) {
M
Matthias Bolte 已提交
1563
            goto cleanup;
1564 1565 1566 1567 1568 1569 1570 1571 1572
        }

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

    /* def:parallels */
    if (VIR_ALLOC_N(def->parallels, 3) < 0) {
1573
        virReportOOMError();
M
Matthias Bolte 已提交
1574
        goto cleanup;
1575 1576 1577 1578 1579
    }

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1580
        if (esxVMX_ParseParallel(ctx, conf, port, datastoreName, directoryName,
1581
                                 &def->parallels[def->nparallels]) < 0) {
M
Matthias Bolte 已提交
1582
            goto cleanup;
1583 1584 1585 1586 1587 1588 1589
        }

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

M
Matthias Bolte 已提交
1590 1591
    success = true;

M
Matthias Bolte 已提交
1592
  cleanup:
M
Matthias Bolte 已提交
1593 1594 1595 1596 1597
    if (! success) {
        virDomainDefFree(def);
        def = NULL;
    }

1598 1599
    virConfFree(conf);
    VIR_FREE(sched_cpu_affinity);
M
Matthias Bolte 已提交
1600
    VIR_FREE(guestOS);
1601 1602 1603 1604 1605 1606

    return def;
}



M
Matthias Bolte 已提交
1607
int
1608
esxVMX_ParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1609
{
1610
    bool enabled = false;
M
Matthias Bolte 已提交
1611 1612 1613
    long long port = 0;

    if (def == NULL || *def != NULL) {
1614
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1615 1616 1617
        return -1;
    }

1618
    if (esxUtil_GetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
1619
                                 false, true) < 0) {
M
Matthias Bolte 已提交
1620 1621 1622 1623 1624 1625 1626 1627
        return -1;
    }

    if (! enabled) {
        return 0;
    }

    if (VIR_ALLOC(*def) < 0) {
1628
        virReportOOMError();
M
Matthias Bolte 已提交
1629 1630 1631 1632 1633
        goto failure;
    }

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1634
    if (esxUtil_GetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
1635
                              true) < 0 ||
1636
        esxUtil_GetConfigString(conf, "RemoteDisplay.vnc.ip",
1637
                                &(*def)->data.vnc.listenAddr, true) < 0 ||
1638
        esxUtil_GetConfigString(conf, "RemoteDisplay.vnc.keymap",
1639
                                &(*def)->data.vnc.keymap, true) < 0 ||
1640
        esxUtil_GetConfigString(conf, "RemoteDisplay.vnc.password",
1641
                                &(*def)->data.vnc.passwd, true) < 0) {
M
Matthias Bolte 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
        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;
}



1671
int
1672
esxVMX_ParseSCSIController(virConfPtr conf, int controller, bool *present,
1673
                           int *virtualDev)
1674 1675 1676
{
    char present_name[32];
    char virtualDev_name[32];
1677 1678
    char *virtualDev_string = NULL;
    char *tmp;
1679

1680
    if (virtualDev == NULL || *virtualDev != -1) {
1681
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1682
        return -1;
1683 1684 1685
    }

    if (controller < 0 || controller > 3) {
1686
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1687
                  _("SCSI controller index %d out of [0..3] range"),
1688
                  controller);
1689
        return -1;
1690 1691
    }

1692 1693 1694
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1695

1696 1697
    if (esxUtil_GetConfigBoolean(conf, present_name, present, false,
                                 true) < 0) {
1698 1699 1700 1701 1702 1703 1704
        goto failure;
    }

    if (! *present) {
        return 0;
    }

1705
    if (esxUtil_GetConfigString(conf, virtualDev_name, &virtualDev_string,
1706
                                true) < 0) {
1707 1708 1709
        goto failure;
    }

1710 1711 1712 1713 1714 1715 1716
    if (virtualDev_string != NULL) {
        tmp = virtualDev_string;

        for (; *tmp != '\0'; ++tmp) {
            *tmp = c_tolower(*tmp);
        }

1717
        *virtualDev = esxVMX_SCSIControllerModelTypeFromString(virtualDev_string);
1718 1719 1720 1721

        if (*virtualDev == -1 ||
            (*virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_BUSLOGIC &&
             *virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_LSILOGIC &&
1722 1723
             *virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_LSISAS1068 &&
             *virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_VMPVSCSI)) {
1724 1725
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Expecting VMX entry '%s' to be 'buslogic' or 'lsilogic' "
1726 1727
                        "or 'lsisas1068' or 'pvscsi' but found '%s'"),
                       virtualDev_name, virtualDev_string);
1728 1729
            goto failure;
        }
1730 1731 1732 1733
    }

    return 0;

M
Matthias Bolte 已提交
1734
  failure:
1735
    VIR_FREE(virtualDev_string);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757

    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
1758 1759
esxVMX_ParseDisk(esxVI_Context *ctx, virCapsPtr caps, virConfPtr conf,
                 int device, int busType, int controllerOrBus, int unit,
1760 1761
                 const char *datastoreName, const char *directoryName,
                 virDomainDiskDefPtr *def)
1762 1763
{
    /*
1764 1765 1766 1767
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK, VIR_DOMAIN_DISK_DEVICE_CDROM}
     *         busType = VIR_DOMAIN_DISK_BUS_SCSI
     * controllerOrBus = [0..3] -> controller
     *            unit = [0..6,8..15]
1768
     *
1769 1770 1771 1772
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK, VIR_DOMAIN_DISK_DEVICE_CDROM}
     *         busType = VIR_DOMAIN_DISK_BUS_IDE
     * controllerOrBus = [0..1] -> bus
     *            unit = [0..1]
1773
     *
1774 1775 1776 1777
     *          device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *         busType = VIR_DOMAIN_DISK_BUS_FDC
     * controllerOrBus = [0]
     *            unit = [0..1]
1778 1779
     */

M
Matthias Bolte 已提交
1780
    int result = -1;
1781 1782 1783
    char *prefix = NULL;

    char present_name[32] = "";
1784
    bool present = false;
1785 1786

    char startConnected_name[32] = "";
1787
    bool startConnected = false;
1788 1789 1790 1791 1792

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

    char clientDevice_name[32] = "";
1793
    bool clientDevice = false;
1794 1795 1796 1797 1798 1799 1800 1801

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

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

    char writeThrough_name[32] = "";
1802
    bool writeThrough = false;
1803 1804

    if (def == NULL || *def != NULL) {
1805
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1806 1807 1808 1809
        return -1;
    }

    if (VIR_ALLOC(*def) < 0) {
1810
        virReportOOMError();
M
Matthias Bolte 已提交
1811
        return -1;
1812 1813 1814
    }

    (*def)->device = device;
1815
    (*def)->bus = busType;
1816 1817 1818 1819

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
1820 1821
        if (busType == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controllerOrBus < 0 || controllerOrBus > 3) {
1822
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1823
                          _("SCSI controller index %d out of [0..3] range"),
1824
                          controllerOrBus);
M
Matthias Bolte 已提交
1825
                goto cleanup;
1826 1827
            }

1828
            if (unit < 0 || unit > 15 || unit == 7) {
1829
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1830 1831
                          _("SCSI unit index %d out of [0..6,8..15] range"),
                          unit);
M
Matthias Bolte 已提交
1832
                goto cleanup;
1833 1834
            }

1835
            if (virAsprintf(&prefix, "scsi%d:%d", controllerOrBus, unit) < 0) {
1836
                virReportOOMError();
M
Matthias Bolte 已提交
1837
                goto cleanup;
1838 1839
            }

M
Matthias Bolte 已提交
1840
            (*def)->dst =
1841
               virIndexToDiskName
1842
                 (controllerOrBus * 15 + (unit < 7 ? unit : unit - 1), "sd");
1843 1844

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
1845
                goto cleanup;
1846
            }
1847 1848
        } else if (busType == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controllerOrBus < 0 || controllerOrBus > 1) {
1849
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1850 1851
                          _("IDE bus index %d out of [0..1] range"),
                          controllerOrBus);
M
Matthias Bolte 已提交
1852
                goto cleanup;
1853 1854
            }

1855
            if (unit < 0 || unit > 1) {
1856
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1857
                          _("IDE unit index %d out of [0..1] range"), unit);
M
Matthias Bolte 已提交
1858
                goto cleanup;
1859 1860
            }

1861
            if (virAsprintf(&prefix, "ide%d:%d", controllerOrBus, unit) < 0) {
1862
                virReportOOMError();
M
Matthias Bolte 已提交
1863
                goto cleanup;
1864 1865
            }

1866
            (*def)->dst = virIndexToDiskName(controllerOrBus * 2 + unit, "hd");
1867 1868

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
1869
                goto cleanup;
1870 1871
            }
        } else {
1872
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1873
                      _("Unsupported bus type '%s' for device type '%s'"),
1874
                      virDomainDiskBusTypeToString(busType),
M
Matthias Bolte 已提交
1875
                      virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
1876
            goto cleanup;
1877 1878
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
1879 1880 1881 1882 1883 1884 1885 1886 1887
        if (busType == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controllerOrBus != 0) {
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                          _("FDC controller index %d out of [0] range"),
                          controllerOrBus);
                goto cleanup;
            }

            if (unit < 0 || unit > 1) {
1888
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1889 1890
                          _("FDC unit index %d out of [0..1] range"),
                          unit);
M
Matthias Bolte 已提交
1891
                goto cleanup;
1892 1893
            }

1894
            if (virAsprintf(&prefix, "floppy%d", unit) < 0) {
1895
                virReportOOMError();
M
Matthias Bolte 已提交
1896
                goto cleanup;
1897 1898
            }

1899
            (*def)->dst = virIndexToDiskName(unit, "fd");
1900 1901

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
1902
                goto cleanup;
1903 1904
            }
        } else {
1905
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1906
                      _("Unsupported bus type '%s' for device type '%s'"),
1907
                      virDomainDiskBusTypeToString(busType),
M
Matthias Bolte 已提交
1908
                      virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
1909
            goto cleanup;
1910 1911
        }
    } else {
1912
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1913
                  _("Unsupported device type '%s'"),
M
Matthias Bolte 已提交
1914
                  virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
1915
        goto cleanup;
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
    }

    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 */
1927 1928
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, false,
                                 true) < 0) {
M
Matthias Bolte 已提交
1929
        goto cleanup;
1930 1931 1932
    }

    /* vmx:startConnected */
1933
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected,
1934
                                 true, true) < 0) {
M
Matthias Bolte 已提交
1935
        goto cleanup;
1936 1937 1938 1939 1940 1941 1942 1943
    }

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

    /* vmx:deviceType -> def:type */
1944
    if (esxUtil_GetConfigString(conf, deviceType_name, &deviceType, true) < 0) {
M
Matthias Bolte 已提交
1945
        goto cleanup;
1946 1947 1948
    }

    /* vmx:clientDevice */
1949 1950
    if (esxUtil_GetConfigBoolean(conf, clientDevice_name, &clientDevice, false,
                                 true) < 0) {
M
Matthias Bolte 已提交
1951
        goto cleanup;
1952 1953 1954 1955 1956
    }

    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
1957
         * handle them (e.g. assign an image) without the VI Client GUI.
1958 1959 1960 1961 1962
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
1963
    if (esxUtil_GetConfigString(conf, fileType_name, &fileType, true) < 0) {
M
Matthias Bolte 已提交
1964
        goto cleanup;
1965 1966 1967
    }

    /* vmx:fileName -> def:src, def:type */
1968
    if (esxUtil_GetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
1969
        goto cleanup;
1970 1971 1972
    }

    /* vmx:writeThrough -> def:cachemode */
1973 1974
    if (esxUtil_GetConfigBoolean(conf, writeThrough_name, &writeThrough, false,
                                 true) < 0) {
M
Matthias Bolte 已提交
1975
        goto cleanup;
1976 1977 1978 1979
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
1980
        if (virFileHasSuffix(fileName, ".vmdk")) {
1981
            if (deviceType != NULL) {
1982
                if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
1983 1984
                    STRCASENEQ(deviceType, "scsi-hardDisk") &&
                    STRCASENEQ(deviceType, "disk")) {
1985
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1986
                              _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
1987 1988
                                "or 'disk' but found '%s'"), deviceType_name,
                              deviceType);
M
Matthias Bolte 已提交
1989
                    goto cleanup;
1990
                } else if (busType == VIR_DOMAIN_DISK_BUS_IDE &&
1991 1992
                           STRCASENEQ(deviceType, "ata-hardDisk") &&
                           STRCASENEQ(deviceType, "disk")) {
1993
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
1994
                              _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
1995 1996
                                "or 'disk' but found '%s'"), deviceType_name,
                              deviceType);
M
Matthias Bolte 已提交
1997
                    goto cleanup;
1998 1999 2000 2001
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2002 2003
            (*def)->src = esxVMX_ParseFileName(ctx, fileName, datastoreName,
                                               directoryName);
2004 2005 2006
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;

M
Matthias Bolte 已提交
2007
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2008
                goto cleanup;
M
Matthias Bolte 已提交
2009
            }
2010
        } else if (virFileHasSuffix(fileName, ".iso") ||
2011 2012 2013 2014 2015 2016 2017 2018 2019
                   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 {
2020
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2021 2022
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2023
            goto cleanup;
2024 2025
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2026
        if (virFileHasSuffix(fileName, ".iso")) {
2027 2028
            if (deviceType != NULL) {
                if (STRCASENEQ(deviceType, "cdrom-image")) {
2029
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2030 2031
                              _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                "but found '%s'"), deviceType_name, deviceType);
M
Matthias Bolte 已提交
2032
                    goto cleanup;
2033 2034 2035 2036
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2037 2038
            (*def)->src = esxVMX_ParseFileName(ctx, fileName, datastoreName,
                                               directoryName);
2039

M
Matthias Bolte 已提交
2040
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2041
                goto cleanup;
M
Matthias Bolte 已提交
2042
            }
2043
        } else if (virFileHasSuffix(fileName, ".vmdk")) {
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
            /*
             * 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 {
2057
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2058 2059
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2060
            goto cleanup;
2061 2062
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2063
        if (virFileHasSuffix(fileName, ".flp")) {
2064 2065
            if (fileType != NULL) {
                if (STRCASENEQ(fileType, "file")) {
2066
                    ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2067 2068
                              _("Expecting VMX entry '%s' to be 'file' but "
                                "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2069
                    goto cleanup;
2070 2071 2072 2073
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2074 2075
            (*def)->src = esxVMX_ParseFileName(ctx, fileName, datastoreName,
                                               directoryName);
2076

M
Matthias Bolte 已提交
2077
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2078
                goto cleanup;
M
Matthias Bolte 已提交
2079
            }
2080 2081 2082 2083 2084 2085
        } else if (fileType != NULL && STREQ(fileType, "device")) {
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
2086
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2087 2088
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2089
            goto cleanup;
2090 2091
        }
    } else {
2092
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, _("Unsupported device type '%s'"),
M
Matthias Bolte 已提交
2093
                  virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2094
        goto cleanup;
2095 2096
    }

2097 2098 2099 2100 2101 2102
    if (virDomainDiskDefAssignAddress(caps, *def) < 0) {
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Could not assign address to disk '%s'"), (*def)->src);
        goto cleanup;
    }

M
Matthias Bolte 已提交
2103 2104
    result = 0;

M
Matthias Bolte 已提交
2105
  cleanup:
M
Matthias Bolte 已提交
2106 2107 2108 2109 2110
    if (result < 0) {
        virDomainDiskDefFree(*def);
        *def = NULL;
    }

2111 2112 2113 2114 2115 2116 2117
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

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

M
Matthias Bolte 已提交
2122 2123
    result = 0;

2124 2125 2126 2127 2128 2129
    goto cleanup;
}



int
2130
esxVMX_ParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
2131
{
M
Matthias Bolte 已提交
2132
    int result = -1;
2133 2134 2135
    char prefix[48] = "";

    char present_name[48] = "";
2136
    bool present = false;
2137 2138

    char startConnected_name[48] = "";
2139
    bool startConnected = false;
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155

    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;

2156 2157 2158
    char features_name[48] = "";
    long long features = 0;

2159 2160 2161
    char vnet_name[48] = "";
    char *vnet = NULL;

M
Matthias Bolte 已提交
2162 2163 2164
    char networkName_name[48] = "";
    char *networkName = NULL;

2165
    if (def == NULL || *def != NULL) {
2166
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2167 2168 2169 2170
        return -1;
    }

    if (controller < 0 || controller > 3) {
2171
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2172
                  _("Ethernet controller index %d out of [0..3] range"),
2173
                  controller);
2174
        return -1;
2175 2176 2177
    }

    if (VIR_ALLOC(*def) < 0) {
2178
        virReportOOMError();
M
Matthias Bolte 已提交
2179
        return -1;
2180 2181
    }

2182
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
2183 2184 2185 2186 2187 2188 2189 2190

    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);
2191
    ESX_BUILD_VMX_NAME(features);
M
Matthias Bolte 已提交
2192
    ESX_BUILD_VMX_NAME(networkName);
2193
    ESX_BUILD_VMX_NAME(vnet);
2194 2195

    /* vmx:present */
2196 2197
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, false,
                                 true) < 0) {
M
Matthias Bolte 已提交
2198
        goto cleanup;
2199 2200 2201
    }

    /* vmx:startConnected */
2202 2203
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected,
                                 true, true) < 0) {
M
Matthias Bolte 已提交
2204
        goto cleanup;
2205 2206 2207 2208 2209 2210 2211 2212
    }

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

    /* vmx:connectionType -> def:type */
2213
    if (esxUtil_GetConfigString(conf, connectionType_name, &connectionType,
2214
                                true) < 0) {
M
Matthias Bolte 已提交
2215
        goto cleanup;
2216 2217 2218
    }

    /* vmx:addressType, vmx:generatedAddress, vmx:address -> def:mac */
2219 2220
    if (esxUtil_GetConfigString(conf, addressType_name, &addressType,
                                true) < 0 ||
2221
        esxUtil_GetConfigString(conf, generatedAddress_name, &generatedAddress,
2222 2223
                                true) < 0 ||
        esxUtil_GetConfigString(conf, address_name, &address, true) < 0) {
M
Matthias Bolte 已提交
2224
        goto cleanup;
2225 2226
    }

2227 2228
    if (addressType == NULL || STRCASEEQ(addressType, "generated") ||
        STRCASEEQ(addressType, "vpx")) {
2229 2230
        if (generatedAddress != NULL) {
            if (virParseMacAddr(generatedAddress, (*def)->mac) < 0) {
2231
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2232 2233
                          _("Expecting VMX entry '%s' to be MAC address but "
                            "found '%s'"), generatedAddress_name,
2234
                          generatedAddress);
M
Matthias Bolte 已提交
2235
                goto cleanup;
2236 2237 2238 2239 2240
            }
        }
    } else if (STRCASEEQ(addressType, "static")) {
        if (address != NULL) {
            if (virParseMacAddr(address, (*def)->mac) < 0) {
2241
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2242 2243
                          _("Expecting VMX entry '%s' to be MAC address but "
                            "found '%s'"), address_name, address);
M
Matthias Bolte 已提交
2244
                goto cleanup;
2245 2246 2247
            }
        }
    } else {
2248
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2249 2250
                  _("Expecting VMX entry '%s' to be 'generated' or 'static' or "
                    "'vpx' but found '%s'"), addressType_name, addressType);
M
Matthias Bolte 已提交
2251
        goto cleanup;
2252 2253
    }

2254
    /* vmx:virtualDev, vmx:features -> def:model */
2255 2256
    if (esxUtil_GetConfigString(conf, virtualDev_name, &virtualDev, true) < 0 ||
        esxUtil_GetConfigLong(conf, features_name, &features, 0, true) < 0) {
M
Matthias Bolte 已提交
2257
        goto cleanup;
2258 2259
    }

2260 2261 2262 2263 2264 2265 2266 2267 2268
    if (virtualDev != NULL) {
        if (STRCASENEQ(virtualDev, "vlance") &&
            STRCASENEQ(virtualDev, "vmxnet") &&
            STRCASENEQ(virtualDev, "vmxnet3") &&
            STRCASENEQ(virtualDev, "e1000")) {
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Expecting VMX entry '%s' to be 'vlance' or 'vmxnet' or "
                        "'vmxnet3' or 'e1000' but found '%s'"), virtualDev_name,
                      virtualDev);
M
Matthias Bolte 已提交
2269
            goto cleanup;
2270 2271 2272 2273 2274 2275 2276 2277 2278
        }

        if (STRCASEEQ(virtualDev, "vmxnet") && features == 15) {
            VIR_FREE(virtualDev);

            virtualDev = strdup("vmxnet2");

            if (virtualDev == NULL) {
                virReportOOMError();
M
Matthias Bolte 已提交
2279
                goto cleanup;
2280 2281
            }
        }
2282 2283
    }

M
Matthias Bolte 已提交
2284 2285 2286 2287
    /* vmx:networkName -> def:data.bridge.brname */
    if ((connectionType == NULL ||
         STRCASEEQ(connectionType, "bridged") ||
         STRCASEEQ(connectionType, "custom")) &&
2288 2289
        esxUtil_GetConfigString(conf, networkName_name, &networkName,
                                false) < 0) {
M
Matthias Bolte 已提交
2290
        goto cleanup;
M
Matthias Bolte 已提交
2291 2292 2293
    }

    /* vmx:vnet -> def:data.ifname */
2294
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
2295
        esxUtil_GetConfigString(conf, vnet_name, &vnet, false) < 0) {
M
Matthias Bolte 已提交
2296
        goto cleanup;
2297 2298 2299 2300 2301 2302
    }

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

        virtualDev = NULL;
M
Matthias Bolte 已提交
2306
        networkName = NULL;
2307 2308
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
2309
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2310
                  _("No yet handled value '%s' for VMX entry '%s'"),
2311
                  connectionType, connectionType_name);
M
Matthias Bolte 已提交
2312
        goto cleanup;
2313 2314
    } else if (STRCASEEQ(connectionType, "nat")) {
        /* FIXME */
2315
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2316
                  _("No yet handled value '%s' for VMX entry '%s'"),
2317
                  connectionType, connectionType_name);
M
Matthias Bolte 已提交
2318
        goto cleanup;
2319 2320 2321
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
2322 2323
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
2324 2325

        virtualDev = NULL;
M
Matthias Bolte 已提交
2326
        networkName = NULL;
2327 2328
        vnet = NULL;
    } else {
2329
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2330
                  _("Invalid value '%s' for VMX entry '%s'"), connectionType,
2331
                  connectionType_name);
M
Matthias Bolte 已提交
2332
        goto cleanup;
2333 2334
    }

M
Matthias Bolte 已提交
2335 2336
    result = 0;

M
Matthias Bolte 已提交
2337
  cleanup:
M
Matthias Bolte 已提交
2338 2339 2340 2341 2342
    if (result < 0) {
        virDomainNetDefFree(*def);
        *def = NULL;
    }

2343 2344 2345 2346 2347 2348 2349 2350 2351
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;

M
Matthias Bolte 已提交
2352
  ignore:
2353 2354 2355
    virDomainNetDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2356 2357
    result = 0;

2358 2359 2360 2361 2362 2363
    goto cleanup;
}



int
2364 2365 2366
esxVMX_ParseSerial(esxVI_Context *ctx, virConfPtr conf, int port,
                   const char *datastoreName, const char *directoryName,
                   virDomainChrDefPtr *def)
2367
{
M
Matthias Bolte 已提交
2368
    int result = -1;
2369 2370 2371
    char prefix[48] = "";

    char present_name[48] = "";
2372
    bool present = false;
2373 2374

    char startConnected_name[48] = "";
2375
    bool startConnected = false;
2376 2377 2378 2379 2380 2381 2382 2383

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

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

    if (def == NULL || *def != NULL) {
2384
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2385 2386 2387 2388
        return -1;
    }

    if (port < 0 || port > 3) {
2389
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2390
                  _("Serial port index %d out of [0..3] range"), port);
2391
        return -1;
2392 2393 2394
    }

    if (VIR_ALLOC(*def) < 0) {
2395
        virReportOOMError();
M
Matthias Bolte 已提交
2396
        return -1;
2397 2398
    }

2399 2400
    (*def)->targetType = VIR_DOMAIN_CHR_TARGET_TYPE_SERIAL;

2401
    snprintf(prefix, sizeof(prefix), "serial%d", port);
2402 2403 2404 2405 2406 2407 2408

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

    /* vmx:present */
2409 2410
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, false,
                                 true) < 0) {
M
Matthias Bolte 已提交
2411
        goto cleanup;
2412 2413 2414
    }

    /* vmx:startConnected */
2415 2416
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected,
                                 true, true) < 0) {
M
Matthias Bolte 已提交
2417
        goto cleanup;
2418 2419 2420 2421 2422 2423 2424 2425
    }

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

    /* vmx:fileType -> def:type */
2426
    if (esxUtil_GetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2427
        goto cleanup;
2428 2429 2430
    }

    /* vmx:fileName -> def:data.file.path */
2431
    if (esxUtil_GetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2432
        goto cleanup;
2433 2434 2435 2436
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2437
        (*def)->target.port = port;
2438 2439 2440 2441 2442
        (*def)->type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->data.file.path = fileName;

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2443
        (*def)->target.port = port;
2444
        (*def)->type = VIR_DOMAIN_CHR_TYPE_FILE;
2445
        (*def)->data.file.path = esxVMX_ParseFileName(ctx, fileName,
M
Matthias Bolte 已提交
2446 2447
                                                      datastoreName,
                                                      directoryName);
2448

M
Matthias Bolte 已提交
2449
        if ((*def)->data.file.path == NULL) {
M
Matthias Bolte 已提交
2450
            goto cleanup;
M
Matthias Bolte 已提交
2451
        }
2452
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
2453 2454 2455 2456
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
2457
        (*def)->target.port = port;
M
Matthias Bolte 已提交
2458 2459 2460 2461
        (*def)->type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->data.file.path = fileName;

        fileName = NULL;
2462
    } else {
2463
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2464 2465
                  _("Expecting VMX entry '%s' to be 'device', 'file' or 'pipe' "
                    "but found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2466
        goto cleanup;
2467 2468
    }

M
Matthias Bolte 已提交
2469 2470
    result = 0;

M
Matthias Bolte 已提交
2471
  cleanup:
M
Matthias Bolte 已提交
2472 2473 2474 2475 2476
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2477 2478 2479 2480 2481
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2482
  ignore:
2483 2484 2485
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2486 2487
    result = 0;

2488 2489 2490 2491 2492 2493
    goto cleanup;
}



int
2494 2495 2496
esxVMX_ParseParallel(esxVI_Context *ctx, virConfPtr conf, int port,
                     const char *datastoreName, const char *directoryName,
                     virDomainChrDefPtr *def)
2497
{
M
Matthias Bolte 已提交
2498
    int result = -1;
2499 2500 2501
    char prefix[48] = "";

    char present_name[48] = "";
2502
    bool present = false;
2503 2504

    char startConnected_name[48] = "";
2505
    bool startConnected = false;
2506 2507 2508 2509 2510 2511 2512 2513

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

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

    if (def == NULL || *def != NULL) {
2514
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2515 2516 2517 2518
        return -1;
    }

    if (port < 0 || port > 2) {
2519
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2520
                  _("Parallel port index %d out of [0..2] range"), port);
2521
        return -1;
2522 2523 2524
    }

    if (VIR_ALLOC(*def) < 0) {
2525
        virReportOOMError();
M
Matthias Bolte 已提交
2526
        return -1;
2527 2528
    }

2529 2530
    (*def)->targetType = VIR_DOMAIN_CHR_TARGET_TYPE_PARALLEL;

2531
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2532 2533 2534 2535 2536 2537 2538

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

    /* vmx:present */
2539 2540
    if (esxUtil_GetConfigBoolean(conf, present_name, &present, false,
                                 true) < 0) {
M
Matthias Bolte 已提交
2541
        goto cleanup;
2542 2543 2544
    }

    /* vmx:startConnected */
2545 2546
    if (esxUtil_GetConfigBoolean(conf, startConnected_name, &startConnected,
                                 true, true) < 0) {
M
Matthias Bolte 已提交
2547
        goto cleanup;
2548 2549 2550 2551 2552 2553 2554 2555
    }

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

    /* vmx:fileType -> def:type */
2556
    if (esxUtil_GetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2557
        goto cleanup;
2558 2559 2560
    }

    /* vmx:fileName -> def:data.file.path */
2561
    if (esxUtil_GetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2562
        goto cleanup;
2563 2564 2565 2566
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2567
        (*def)->target.port = port;
2568 2569 2570 2571 2572
        (*def)->type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->data.file.path = fileName;

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2573
        (*def)->target.port = port;
2574
        (*def)->type = VIR_DOMAIN_CHR_TYPE_FILE;
2575
        (*def)->data.file.path = esxVMX_ParseFileName(ctx, fileName,
M
Matthias Bolte 已提交
2576 2577
                                                      datastoreName,
                                                      directoryName);
2578

M
Matthias Bolte 已提交
2579
        if ((*def)->data.file.path == NULL) {
M
Matthias Bolte 已提交
2580
            goto cleanup;
M
Matthias Bolte 已提交
2581
        }
2582
    } else {
2583
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2584 2585
                  _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                    "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2586
        goto cleanup;
2587 2588
    }

M
Matthias Bolte 已提交
2589 2590
    result = 0;

M
Matthias Bolte 已提交
2591
  cleanup:
M
Matthias Bolte 已提交
2592 2593 2594 2595 2596
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2597 2598 2599 2600 2601
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2602
  ignore:
2603 2604 2605
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2606 2607
    result = 0;

2608 2609
    goto cleanup;
}
M
Matthias Bolte 已提交
2610 2611 2612 2613 2614 2615 2616 2617



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

char *
2618
esxVMX_FormatFileName(esxVI_Context *ctx ATTRIBUTE_UNUSED, const char *src)
M
Matthias Bolte 已提交
2619
{
M
Matthias Bolte 已提交
2620
    bool success = false;
M
Matthias Bolte 已提交
2621 2622 2623 2624 2625 2626 2627
    char *datastoreName = NULL;
    char *directoryName = NULL;
    char *fileName = NULL;
    char *absolutePath = NULL;

    if (STRPREFIX(src, "[")) {
        /* Found potential datastore related path */
2628
        if (esxUtil_ParseDatastoreRelatedPath(src, &datastoreName,
M
Matthias Bolte 已提交
2629
                                              &directoryName, &fileName) < 0) {
M
Matthias Bolte 已提交
2630
            goto cleanup;
M
Matthias Bolte 已提交
2631 2632 2633 2634 2635
        }

        if (directoryName == NULL) {
            if (virAsprintf(&absolutePath, "/vmfs/volumes/%s/%s",
                            datastoreName, fileName) < 0) {
2636
                virReportOOMError();
M
Matthias Bolte 已提交
2637
                goto cleanup;
M
Matthias Bolte 已提交
2638 2639 2640 2641
            }
        } else {
            if (virAsprintf(&absolutePath, "/vmfs/volumes/%s/%s/%s",
                            datastoreName, directoryName, fileName) < 0) {
2642
                virReportOOMError();
M
Matthias Bolte 已提交
2643
                goto cleanup;
M
Matthias Bolte 已提交
2644 2645 2646 2647 2648 2649 2650
            }
        }
    } else if (STRPREFIX(src, "/")) {
        /* Found absolute path */
        absolutePath = strdup(src);

        if (absolutePath == NULL) {
2651
            virReportOOMError();
M
Matthias Bolte 已提交
2652
            goto cleanup;
M
Matthias Bolte 已提交
2653 2654 2655
        }
    } else {
        /* Found relative path, this is not supported */
2656
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2657 2658
                  _("Found relative path '%s' in domain XML, this is not "
                    "supported"), src);
M
Matthias Bolte 已提交
2659
        goto cleanup;
M
Matthias Bolte 已提交
2660 2661 2662 2663
    }

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

M
Matthias Bolte 已提交
2664 2665
    success = true;

M
Matthias Bolte 已提交
2666
  cleanup:
M
Matthias Bolte 已提交
2667 2668 2669 2670
    if (! success) {
        VIR_FREE(absolutePath);
    }

M
Matthias Bolte 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
    VIR_FREE(datastoreName);
    VIR_FREE(directoryName);
    VIR_FREE(fileName);

    return absolutePath;
}



char *
2681
esxVMX_FormatConfig(esxVI_Context *ctx, virCapsPtr caps, virDomainDefPtr def,
2682
                    esxVI_ProductVersion productVersion)
M
Matthias Bolte 已提交
2683 2684 2685 2686 2687
{
    int i;
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
2688 2689
    bool scsi_present[4] = { false, false, false, false };
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
M
Matthias Bolte 已提交
2690 2691 2692 2693

    memset(zero, 0, VIR_UUID_BUFLEN);

    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) { /* FIXME: maybe add VIR_DOMAIN_VIRT_ESX ? */
2694
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2695
                  _("Expecting virt type to be '%s' but found '%s'"),
M
Matthias Bolte 已提交
2696 2697 2698 2699 2700 2701 2702 2703 2704
                  virDomainVirtTypeToString(VIR_DOMAIN_VIRT_VMWARE),
                  virDomainVirtTypeToString(def->virtType));
        return NULL;
    }

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

    /* vmx:virtualHW.version */
2705 2706
    switch (productVersion) {
      case esxVI_ProductVersion_ESX35:
M
Matthias Bolte 已提交
2707 2708 2709
        virBufferAddLit(&buffer, "virtualHW.version = \"4\"\n");
        break;

2710 2711
      case esxVI_ProductVersion_GSX20:
      case esxVI_ProductVersion_ESX40:
M
Matthias Bolte 已提交
2712 2713
      case esxVI_ProductVersion_ESX41:
      case esxVI_ProductVersion_ESX4x:
M
Matthias Bolte 已提交
2714 2715 2716 2717
        virBufferAddLit(&buffer, "virtualHW.version = \"7\"\n");
        break;

      default:
2718
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
2719
                  _("Unexpected product version"));
M
Matthias Bolte 已提交
2720 2721 2722 2723 2724 2725 2726 2727 2728
        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 {
2729
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2730 2731
                  _("Expecting domain XML attribute 'arch' of entry 'os/type' "
                    "to be 'i686' or 'x86_64' but found '%s'"), def->os.arch);
M
Matthias Bolte 已提交
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
        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) {
2753
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2754 2755
                  _("Expecting domain XML entry 'memory' to be an unsigned "
                    "integer (multiple of 4096) but found %lld"),
M
Matthias Bolte 已提交
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
                  (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) {
2767
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2768 2769
                      _("Expecting domain XML entry 'currentMemory' to be an "
                        "unsigned integer (multiple of 1024) but found %lld"),
M
Matthias Bolte 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778
                      (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 已提交
2779
    /* def:vcpus -> vmx:numvcpus */
M
Matthias Bolte 已提交
2780
    if (def->vcpus <= 0 || (def->vcpus % 2 != 0 && def->vcpus != 1)) {
2781
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2782 2783
                  _("Expecting domain XML entry 'vcpu' to be an unsigned "
                    "integer (1 or a multiple of 2) but found %d"),
M
Matthias Bolte 已提交
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
                  (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) {
2803
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2804 2805
                      _("Expecting domain XML attribute 'cpuset' of entry "
                        "'vcpu' to contains at least %d CPU(s)"),
M
Matthias Bolte 已提交
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824
                      (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 已提交
2825 2826 2827 2828
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
2829
            if (esxVMX_FormatVNC(def->graphics[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2830 2831 2832 2833 2834 2835
                goto failure;
            }

            break;

          default:
2836
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2837
                      _("Unsupported graphics type '%s'"),
M
Matthias Bolte 已提交
2838 2839 2840 2841 2842
                      virDomainGraphicsTypeToString(def->graphics[i]->type));
            goto failure;
        }
    }

M
Matthias Bolte 已提交
2843
    /* def:disks */
2844 2845 2846 2847 2848 2849
    for (i = 0; i < def->ndisks; ++i) {
        if (esxVMX_VerifyDiskAddress(caps, def->disks[i]) < 0 ||
            esxVMX_HandleLegacySCSIDiskDriverName(def, def->disks[i]) < 0) {
            goto failure;
        }
    }
M
Matthias Bolte 已提交
2850

2851 2852
    if (esxVMX_GatherSCSIControllers(ctx, def, scsi_virtualDev,
                                     scsi_present) < 0) {
M
Matthias Bolte 已提交
2853 2854 2855 2856 2857 2858 2859
        goto failure;
    }

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

2860
            if (scsi_virtualDev[i] != -1) {
M
Matthias Bolte 已提交
2861
                virBufferVSprintf(&buffer, "scsi%d.virtualDev = \"%s\"\n", i,
2862
                                  esxVMX_SCSIControllerModelTypeToString
2863
                                    (scsi_virtualDev[i]));
M
Matthias Bolte 已提交
2864 2865 2866 2867 2868 2869 2870
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
2871
            if (esxVMX_FormatHardDisk(ctx, def->disks[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2872 2873 2874 2875 2876 2877
                goto failure;
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_CDROM:
2878
            if (esxVMX_FormatCDROM(ctx, def->disks[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2879 2880 2881 2882 2883 2884
                goto failure;
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
2885
            if (esxVMX_FormatFloppy(ctx, def->disks[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2886 2887 2888 2889 2890 2891
                goto failure;
            }

            break;

          default:
2892
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
2893
                      _("Unsupported disk device type '%s'"),
M
Matthias Bolte 已提交
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
                      virDomainDiskDeviceTypeToString(def->disks[i]->device));
            goto failure;
        }
    }

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

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
2904
        if (esxVMX_FormatEthernet(def->nets[i], i, &buffer) < 0) {
M
Matthias Bolte 已提交
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
            goto failure;
        }
    }

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

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

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

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
2920
        if (esxVMX_FormatSerial(ctx, def->serials[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2921 2922 2923 2924 2925 2926
            goto failure;
        }
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
2927
        if (esxVMX_FormatParallel(ctx, def->parallels[i], &buffer) < 0) {
M
Matthias Bolte 已提交
2928 2929 2930 2931 2932 2933
            goto failure;
        }
    }

    /* Get final VMX output */
    if (virBufferError(&buffer)) {
2934
        virReportOOMError();
M
Matthias Bolte 已提交
2935 2936 2937
        goto failure;
    }

2938
    return virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
2939 2940

  failure:
2941
    virBufferFreeAndReset(&buffer);
M
Matthias Bolte 已提交
2942 2943 2944 2945 2946 2947

    return NULL;
}



M
Matthias Bolte 已提交
2948
int
2949
esxVMX_FormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
2950 2951
{
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
2952
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
        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 已提交
2991
int
2992 2993
esxVMX_FormatHardDisk(esxVI_Context *ctx, virDomainDiskDefPtr def,
                      virBufferPtr buffer)
M
Matthias Bolte 已提交
2994
{
2995
    int controllerOrBus, unit;
M
Matthias Bolte 已提交
2996 2997 2998
    const char *busName = NULL;
    const char *entryPrefix = NULL;
    const char *deviceTypePrefix = NULL;
M
Matthias Bolte 已提交
2999
    char *fileName = NULL;
M
Matthias Bolte 已提交
3000 3001

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
3002
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3003 3004 3005 3006 3007 3008 3009 3010
        return -1;
    }

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

3011 3012
        if (esxVMX_SCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                   &unit) < 0) {
M
Matthias Bolte 已提交
3013 3014 3015 3016 3017 3018 3019
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";
        deviceTypePrefix = "ata";

3020 3021
        if (esxVMX_IDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
                                           &unit) < 0) {
M
Matthias Bolte 已提交
3022 3023 3024
            return -1;
        }
    } else {
3025
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3026
                  _("Unsupported bus type '%s' for harddisk"),
M
Matthias Bolte 已提交
3027 3028 3029 3030 3031
                  virDomainDiskBusTypeToString(def->bus));
        return -1;
    }

    if (def->type != VIR_DOMAIN_DISK_TYPE_FILE) {
3032
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3033
                  _("%s harddisk '%s' has unsupported type '%s', expecting '%s'"),
M
Matthias Bolte 已提交
3034 3035 3036 3037 3038 3039
                  busName, def->dst, virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE));
        return -1;
    }

    virBufferVSprintf(buffer, "%s%d:%d.present = \"true\"\n",
3040
                      entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3041
    virBufferVSprintf(buffer, "%s%d:%d.deviceType = \"%s-hardDisk\"\n",
3042
                      entryPrefix, controllerOrBus, unit, deviceTypePrefix);
M
Matthias Bolte 已提交
3043 3044

    if (def->src != NULL) {
3045
        if (! virFileHasSuffix(def->src, ".vmdk")) {
3046
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3047 3048
                      _("Image file for %s harddisk '%s' has unsupported suffix, "
                        "expecting '.vmdk'"), busName, def->dst);
M
Matthias Bolte 已提交
3049 3050 3051
            return -1;
        }

3052
        fileName = esxVMX_FormatFileName(ctx, def->src);
M
Matthias Bolte 已提交
3053 3054 3055 3056 3057

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

M
Matthias Bolte 已提交
3058
        virBufferVSprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3059
                          entryPrefix, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3060 3061

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3062 3063 3064 3065 3066
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
            virBufferVSprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
3067
                              entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3068
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
3069
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3070
                      _("%s harddisk '%s' has unsupported cache mode '%s'"),
M
Matthias Bolte 已提交
3071 3072
                      busName, def->dst,
                      virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
3073
            return -1;
M
Matthias Bolte 已提交
3074 3075 3076 3077 3078 3079 3080 3081 3082
        }
    }

    return 0;
}



int
3083 3084
esxVMX_FormatCDROM(esxVI_Context *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
3085
{
3086
    int controllerOrBus, unit;
M
Matthias Bolte 已提交
3087 3088
    const char *busName = NULL;
    const char *entryPrefix = NULL;
M
Matthias Bolte 已提交
3089
    char *fileName = NULL;
M
Matthias Bolte 已提交
3090 3091

    if (def->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
3092
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3093 3094 3095 3096 3097 3098 3099
        return -1;
    }

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

3100 3101
        if (esxVMX_SCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                   &unit) < 0) {
M
Matthias Bolte 已提交
3102 3103 3104 3105 3106 3107
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";

3108 3109
        if (esxVMX_IDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
                                           &unit) < 0) {
M
Matthias Bolte 已提交
3110 3111 3112
            return -1;
        }
    } else {
3113 3114
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Unsupported bus type '%s' for cdrom"),
M
Matthias Bolte 已提交
3115 3116 3117 3118 3119
                  virDomainDiskBusTypeToString(def->bus));
        return -1;
    }

    virBufferVSprintf(buffer, "%s%d:%d.present = \"true\"\n",
3120
                      entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3121 3122 3123

    if (def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
        virBufferVSprintf(buffer, "%s%d:%d.deviceType = \"cdrom-image\"\n",
3124
                          entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3125 3126

        if (def->src != NULL) {
3127
            if (! virFileHasSuffix(def->src, ".iso")) {
3128
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3129 3130
                          _("Image file for %s cdrom '%s' has unsupported "
                            "suffix, expecting '.iso'"), busName, def->dst);
M
Matthias Bolte 已提交
3131 3132 3133
                return -1;
            }

3134
            fileName = esxVMX_FormatFileName(ctx, def->src);
M
Matthias Bolte 已提交
3135 3136 3137 3138 3139 3140

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

            virBufferVSprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3141
                              entryPrefix, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3142 3143 3144

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
3145 3146
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
        virBufferVSprintf(buffer, "%s%d:%d.deviceType = \"atapi-cdrom\"\n",
3147
                          entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3148 3149 3150

        if (def->src != NULL) {
            virBufferVSprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3151
                              entryPrefix, controllerOrBus, unit, def->src);
M
Matthias Bolte 已提交
3152
        }
M
Matthias Bolte 已提交
3153
    } else {
3154
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3155 3156
                  _("%s cdrom '%s' has unsupported type '%s', expecting '%s' "
                    "or '%s'"), busName, def->dst,
M
Matthias Bolte 已提交
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
                  virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }

    return 0;
}



int
3169 3170
esxVMX_FormatFloppy(esxVI_Context *ctx, virDomainDiskDefPtr def,
                    virBufferPtr buffer)
M
Matthias Bolte 已提交
3171
{
3172
    int unit;
M
Matthias Bolte 已提交
3173
    char *fileName = NULL;
M
Matthias Bolte 已提交
3174 3175

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
3176
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3177 3178 3179
        return -1;
    }

3180
    if (esxVMX_FloppyDiskNameToUnit(def->dst, &unit) < 0) {
M
Matthias Bolte 已提交
3181 3182 3183
        return -1;
    }

3184
    virBufferVSprintf(buffer, "floppy%d.present = \"true\"\n", unit);
M
Matthias Bolte 已提交
3185 3186

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

        if (def->src != NULL) {
3190
            if (! virFileHasSuffix(def->src, ".flp")) {
3191
                ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3192 3193
                          _("Image file for floppy '%s' has unsupported "
                            "suffix, expecting '.flp'"), def->dst);
M
Matthias Bolte 已提交
3194 3195 3196
                return -1;
            }

3197
            fileName = esxVMX_FormatFileName(ctx, def->src);
M
Matthias Bolte 已提交
3198 3199 3200 3201 3202 3203

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

            virBufferVSprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3204
                              unit, fileName);
M
Matthias Bolte 已提交
3205 3206 3207

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
3208
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
3209
        virBufferVSprintf(buffer, "floppy%d.fileType = \"device\"\n", unit);
M
Matthias Bolte 已提交
3210 3211 3212

        if (def->src != NULL) {
            virBufferVSprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3213
                              unit, def->src);
M
Matthias Bolte 已提交
3214
        }
M
Matthias Bolte 已提交
3215
    } else {
3216
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3217 3218
                  _("Floppy '%s' has unsupported type '%s', expecting '%s' "
                    "or '%s'"), def->dst,
M
Matthias Bolte 已提交
3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
                  virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }

    return 0;
}



int
3231 3232
esxVMX_FormatEthernet(virDomainNetDefPtr def, int controller,
                      virBufferPtr buffer)
M
Matthias Bolte 已提交
3233 3234
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
3235
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
3236 3237

    if (controller < 0 || controller > 3) {
3238
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3239
                  _("Ethernet controller index %d out of [0..3] range"),
M
Matthias Bolte 已提交
3240 3241 3242 3243 3244 3245
                  controller);
        return -1;
    }

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

3246
    /* def:model -> vmx:virtualDev, vmx:features */
M
Matthias Bolte 已提交
3247 3248 3249
    if (def->model != NULL) {
        if (STRCASENEQ(def->model, "vlance") &&
            STRCASENEQ(def->model, "vmxnet") &&
3250
            STRCASENEQ(def->model, "vmxnet2") &&
3251
            STRCASENEQ(def->model, "vmxnet3") &&
M
Matthias Bolte 已提交
3252
            STRCASENEQ(def->model, "e1000")) {
3253
            ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3254
                      _("Expecting domain XML entry 'devices/interfase/model' "
3255 3256
                        "to be 'vlance' or 'vmxnet' or 'vmxnet2' or 'vmxnet3' "
                        "or 'e1000' but found '%s'"), def->model);
M
Matthias Bolte 已提交
3257 3258 3259
            return -1;
        }

3260 3261 3262 3263 3264 3265 3266 3267 3268
        if (STRCASEEQ(def->model, "vmxnet2")) {
            virBufferVSprintf(buffer, "ethernet%d.virtualDev = \"vmxnet\"\n",
                              controller);
            virBufferVSprintf(buffer, "ethernet%d.features = \"15\"\n",
                              controller);
        } else {
            virBufferVSprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
                              controller, def->model);
        }
M
Matthias Bolte 已提交
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
    }

    /* 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:
3290
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, _("Unsupported net type '%s'"),
M
Matthias Bolte 已提交
3291 3292 3293 3294
                  virDomainNetTypeToString(def->type));
        return -1;
    }

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

3298 3299 3300 3301
    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 已提交
3302 3303 3304 3305
        virBufferVSprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
                          controller);
        virBufferVSprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
                          controller, mac_string);
M
Matthias Bolte 已提交
3306 3307
        virBufferVSprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
                          controller);
3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
    } 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 已提交
3318
    } else {
3319 3320 3321 3322 3323 3324
        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 已提交
3325 3326 3327 3328 3329 3330 3331 3332
    }

    return 0;
}



int
3333 3334
esxVMX_FormatSerial(esxVI_Context *ctx, virDomainChrDefPtr def,
                    virBufferPtr buffer)
M
Matthias Bolte 已提交
3335
{
M
Matthias Bolte 已提交
3336 3337
    char *fileName = NULL;

3338
    if (def->target.port < 0 || def->target.port > 3) {
3339
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3340 3341
                  _("Serial port index %d out of [0..3] range"),
                  def->target.port);
M
Matthias Bolte 已提交
3342 3343 3344 3345
        return -1;
    }

    if (def->data.file.path == NULL) {
3346 3347 3348
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'path' of entry "
                    "'devices/serial/source' to be present"));
M
Matthias Bolte 已提交
3349 3350 3351
        return -1;
    }

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

M
Matthias Bolte 已提交
3354
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
M
Matthias Bolte 已提交
3355 3356 3357
    switch (def->type) {
      case VIR_DOMAIN_CHR_TYPE_DEV:
        virBufferVSprintf(buffer, "serial%d.fileType = \"device\"\n",
3358
                          def->target.port);
M
Matthias Bolte 已提交
3359
        virBufferVSprintf(buffer, "serial%d.fileName = \"%s\"\n",
3360
                          def->target.port, def->data.file.path);
M
Matthias Bolte 已提交
3361 3362 3363 3364
        break;

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

3367
        fileName = esxVMX_FormatFileName(ctx, def->data.file.path);
M
Matthias Bolte 已提交
3368 3369 3370 3371 3372 3373

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

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

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3377 3378 3379 3380
        break;

      case VIR_DOMAIN_CHR_TYPE_PIPE:
        virBufferVSprintf(buffer, "serial%d.fileType = \"pipe\"\n",
3381
                          def->target.port);
M
Matthias Bolte 已提交
3382 3383
        /* FIXME: Based on VI Client GUI default */
        virBufferVSprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
3384
                          def->target.port);
M
Matthias Bolte 已提交
3385 3386
        /* FIXME: Based on VI Client GUI default */
        virBufferVSprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
3387
                          def->target.port);
M
Matthias Bolte 已提交
3388
        virBufferVSprintf(buffer, "serial%d.fileName = \"%s\"\n",
3389
                          def->target.port, def->data.file.path);
M
Matthias Bolte 已提交
3390 3391 3392
        break;

      default:
3393
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3394
                  _("Unsupported character device type '%s'"),
M
Matthias Bolte 已提交
3395 3396 3397 3398 3399 3400 3401
                  virDomainChrTypeToString(def->type));
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
    virBufferVSprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
3402
                      def->target.port);
M
Matthias Bolte 已提交
3403 3404 3405 3406 3407 3408 3409

    return 0;
}



int
3410 3411
esxVMX_FormatParallel(esxVI_Context *ctx, virDomainChrDefPtr def,
                      virBufferPtr buffer)
M
Matthias Bolte 已提交
3412
{
M
Matthias Bolte 已提交
3413 3414
    char *fileName = NULL;

3415
    if (def->target.port < 0 || def->target.port > 2) {
3416
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3417
                  _("Parallel port index %d out of [0..2] range"),
3418
                  def->target.port);
M
Matthias Bolte 已提交
3419 3420 3421 3422
        return -1;
    }

    if (def->data.file.path == NULL) {
3423 3424 3425
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("Expecting domain XML attribute 'path' of entry "
                    "'devices/parallel/source' to be present"));
M
Matthias Bolte 已提交
3426 3427 3428
        return -1;
    }

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

M
Matthias Bolte 已提交
3432
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
M
Matthias Bolte 已提交
3433 3434 3435
    switch (def->type) {
      case VIR_DOMAIN_CHR_TYPE_DEV:
        virBufferVSprintf(buffer, "parallel%d.fileType = \"device\"\n",
3436
                          def->target.port);
M
Matthias Bolte 已提交
3437
        virBufferVSprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3438
                          def->target.port, def->data.file.path);
M
Matthias Bolte 已提交
3439 3440 3441 3442
        break;

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

3445
        fileName = esxVMX_FormatFileName(ctx, def->data.file.path);
M
Matthias Bolte 已提交
3446 3447 3448 3449 3450 3451

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

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

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3455 3456 3457
        break;

      default:
3458
        ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
3459
                  _("Unsupported character device type '%s'"),
M
Matthias Bolte 已提交
3460 3461 3462 3463 3464 3465
                  virDomainChrTypeToString(def->type));
        return -1;
    }

    return 0;
}