vmx.c 121.0 KB
Newer Older
1 2

/*
3
 * vmx.c: VMware VMX parsing/formatting functions
4
 *
5
 * Copyright (C) 2010-2012 Red Hat, Inc.
6
 * Copyright (C) 2009-2010 Matthias Bolte <matthias.bolte@googlemail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
#include "internal.h"
#include "virterror_internal.h"
30
#include "conf.h"
31 32
#include "memory.h"
#include "logging.h"
33 34
#include "uuid.h"
#include "vmx.h"
M
Martin Kletzander 已提交
35
#include "viruri.h"
36 37 38 39 40 41 42 43 44

/*

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
P
Patrice LACHANCE 已提交
47
                                        virtualHW.version = "8"                 # essential for ESX 5.0
48 49 50 51 52 53


???                               <=>   guestOS = "<value>"                     # essential, FIXME: not representable
def->id = <value>                 <=>   ???                                     # not representable
def->uuid = <value>               <=>   uuid.bios = "<value>"
def->name = <value>               <=>   displayName = "<value>"
54 55
def->mem.max_balloon = <value kilobyte>    <=>   memsize = "<value megabyte>"            # must be a multiple of 4, defaults to 32
def->mem.cur_balloon = <value kilobyte>    <=>   sched.mem.max = "<value megabyte>"      # defaults to "unlimited" -> def->mem.cur_balloon = def->mem.max_balloon
56
def->mem.min_guarantee = <value kilobyte>  <=>   sched.mem.minsize = "<value megabyte>"  # defaults to 0
E
Eric Blake 已提交
57
def->maxvcpus = <value>           <=>   numvcpus = "<value>"                    # must be 1 or a multiple of 2, defaults to 1
58
def->cpumask = <uint list>        <=>   sched.cpu.affinity = "<uint list>"
59 60
def->cputune.shares = <value>     <=>   sched.cpu.shares = "<value>"            # with handling for special values
                                                                                # "high", "normal", "low"
61 62 63 64 65 66 67 68 69



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

def->os

->type = "hvm"
M
Matthias Bolte 已提交
70
->arch = <arch>                   <=>   guestOS = "<value>"                     # if <value> ends with -64 than <arch> is x86_64, otherwise <arch> is i686
71 72 73 74 75 76 77 78 79 80 81
->machine
->nBootDevs
->bootDevs
->init
->kernel
->initrd
->cmdline
->root
->loader
->bootloader
->bootloaderArgs
M
Matthias Bolte 已提交
82
->smbios_mode                     <=>   smbios.reflecthost = "<value>"          # <value> == true means SMBIOS_HOST, otherwise it's SMBIOS_EMULATE, defaults to "false"
83 84 85 86 87 88



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

89 90 91
                                        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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

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"
111
->dst = sd[<controller> * 15 + <unit> mapped to [a-z]+]
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
->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"
135
->dst = hd[<bus> * 2 + <unit> mapped to [a-z]+]
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
->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"
155
->dst = sd[<controller> * 15 + <unit> mapped to [a-z]+]
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
->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"
174
->dst = hd[<bus> * 2 + <unit> mapped to [a-z]+]
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
->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" ?
194
->dst = sd[<controller> * 15 + <unit> mapped to [a-z]+]
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
->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"
214
->dst = hd[<bus> * 2 + <unit> mapped to [a-z]+]
215 216 217 218 219 220 221 222 223 224
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum


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

225
                                        floppy0.present = "true"                # defaults to "true"
226 227 228 229 230 231 232 233
                                        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"
234
->dst = fd[<unit> mapped to [a-z]+]
235 236 237 238 239 240 241 242 243 244
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum


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

245
                                        floppy0.present = "true"                # defaults to "true"
246 247 248 249 250 251 252 253
                                        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"
254
->dst = fd[<unit> mapped to [a-z]+]
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
->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
276
                                        ethernet0.features = "15"               # if present and virtualDev is "vmxnet" => vmxnet2 (enhanced)
277 278 279 280


                                        ethernet0.addressType = "generated"     # default to "generated"
->mac = <value>                   <=>   ethernet0.generatedAddress = "<value>"
281
                                        ethernet0.generatedAddressOffset = "0"  # ?
282 283 284 285 286


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

287 288 289 290

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

291 292 293 294 295

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

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


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

...
->type = _NET_TYPE_BRIDGE         <=>   ethernet0.connectionType = "bridged"    # defaults to "bridged"
M
Matthias Bolte 已提交
307
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
308 309 310 311


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

M
Matthias Bolte 已提交
312
...                                                                             # FIXME: Investigate if ESX supports this
313 314 315 316 317
->type = _NET_TYPE_NETWORK        <=>   ethernet0.connectionType = "hostonly"   # defaults to "bridged"


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

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



331 332 333 334 335 336 337 338 339 340
################################################################################
## video #######################################################################

def->videos[0]...
->type = _VIDEO_TYPE_VMVGA
->vram = <value kilobyte>         <=>   svga.vramSize = "<value byte>"
->heads = 1



341 342 343 344 345 346 347 348 349
################################################################################
## serials #####################################################################

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

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

def->serials[0]...
350
->target.port = <port>
351 352 353 354 355 356 357


## 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 已提交
358
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
359 360 361 362 363 364 365


## 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 已提交
366
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
367 368 369 370 371 372


## 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 已提交
373
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
374
???                               <=>   serial0.tryNoRxLoss = "true"            # 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 = "true"            # 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


## 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 已提交
388
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
389
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
390
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
391 392 393

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
394
???                               <=>   serial0.pipe.endPoint = "server"        # defaults to ?, FIXME: not representable
395
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
396
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
397 398


399 400 401 402 403 404 405 406
## serials: network, server (since vSphere 4.1) ################################

->type = _CHR_TYPE_TCP            <=>   serial0.fileType = "network"

->data.tcp.host = <host>          <=>   serial0.fileName = "<protocol>://<host>:<service>"
->data.tcp.service = <service>                                                  # e.g. "telnet://0.0.0.0:42001"
->data.tcp.protocol = <protocol>

407
->data.tcp.listen = true          <=>   serial0.network.endPoint = "server"     # defaults to "server"
408 409 410 411 412 413 414 415 416 417 418 419 420 421

???                               <=>   serial0.vspc = "foobar"                 # defaults to <not present>, FIXME: not representable
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable


## serials: network, client (since vSphere 4.1) ################################

->type = _CHR_TYPE_TCP            <=>   serial0.fileType = "network"

->data.tcp.host = <host>          <=>   serial0.fileName = "<protocol>://<host>:<service>"
->data.tcp.service = <service>                                                  # e.g. "telnet://192.168.0.17:42001"
->data.tcp.protocol = <protocol>

422
->data.tcp.listen = false         <=>   serial0.network.endPoint = "client"     # defaults to "server"
423 424 425 426 427 428

???                               <=>   serial0.vspc = "foobar"                 # defaults to <not present>, FIXME: not representable
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable


429 430 431 432 433 434 435 436 437 438

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

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

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

def->parallels[0]...
439
->target.port = <port>
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454


## 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 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469


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

470 471
*/

472
#define VIR_FROM_THIS VIR_FROM_NONE
473

474
#define VMX_ERROR(code, ...)                                                  \
475
    virReportErrorHelper(VIR_FROM_NONE, code, __FILE__, __FUNCTION__,         \
476 477 478
                         __LINE__, __VA_ARGS__)

#define VMX_BUILD_NAME_EXTRA(_suffix, _extra)                                 \
479 480
    snprintf(_suffix##_name, sizeof(_suffix##_name), "%s."_extra, prefix);

481 482
#define VMX_BUILD_NAME(_suffix)                                               \
    VMX_BUILD_NAME_EXTRA(_suffix, #_suffix)
483

484 485 486
/* directly map the virDomainControllerModel to virVMXSCSIControllerModel,
 * this is good enough for now because all virDomainControllerModel values
 * are actually SCSI controller models in the ESX case */
487 488
VIR_ENUM_DECL(virVMXControllerModelSCSI)
VIR_ENUM_IMPL(virVMXControllerModelSCSI, VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LAST,
489
              "auto", /* just to match virDomainControllerModel, will never be used */
490 491 492
              "buslogic",
              "lsilogic",
              "lsisas1068",
493
              "pvscsi",
494 495
              "UNUSED ibmvscsi",
              "UNUSED virtio-scsi");
496 497


498 499 500 501 502

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Helpers
 */

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
char *
virVMXEscapeHex(const char *string, char escape, const char *special)
{
    char *escaped = NULL;
    size_t length = 1; /* 1 byte for termination */
    const char *tmp1 = string;
    char *tmp2;

    /* Calculate length of escaped string */
    while (*tmp1 != '\0') {
        if (*tmp1 == escape || strspn(tmp1, special) > 0) {
            length += 2;
        }

        ++tmp1;
        ++length;
    }

    if (VIR_ALLOC_N(escaped, length) < 0) {
        virReportOOMError();
        return NULL;
    }

    tmp1 = string; /* reading from this one */
    tmp2 = escaped; /* writing to this one */

    /* Escape to 'cXX' where c is the escape char and X is a hex digit */
    while (*tmp1 != '\0') {
        if (*tmp1 == escape || strspn(tmp1, special) > 0) {
            *tmp2++ = escape;

            snprintf(tmp2, 3, "%02x", (unsigned int)*tmp1);

            tmp2 += 2;
        } else {
            *tmp2++ = *tmp1;
        }

        ++tmp1;
    }

    *tmp2 = '\0';

    return escaped;
}



M
Matthias Bolte 已提交
551
int
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 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
virVMXUnescapeHex(char *string, char escape)
{
    char *tmp1 = string; /* reading from this one */
    char *tmp2 = string; /* writing to this one */

    /* Unescape from 'cXX' where c is the escape char and X is a hex digit */
    while (*tmp1 != '\0') {
        if (*tmp1 == escape) {
            if (!c_isxdigit(tmp1[1]) || !c_isxdigit(tmp1[2])) {
                return -1;
            }

            *tmp2++ = virHexToBin(tmp1[1]) * 16 + virHexToBin(tmp1[2]);
            tmp1 += 3;
        } else {
            *tmp2++ = *tmp1++;
        }
    }

    *tmp2 = '\0';

    return 0;
}



char *
virVMXConvertToUTF8(const char *encoding, const char *string)
{
    char *result = NULL;
    xmlCharEncodingHandlerPtr handler;
    xmlBufferPtr input;
    xmlBufferPtr utf8;

    handler = xmlFindCharEncodingHandler(encoding);

    if (handler == NULL) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("libxml2 doesn't handle %s encoding"), encoding);
        return NULL;
    }

    input = xmlBufferCreateStatic((char *)string, strlen(string));
    utf8 = xmlBufferCreate();

    if (xmlCharEncInFunc(handler, utf8, input) < 0) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Could not convert from %s to UTF-8 encoding"), encoding);
        goto cleanup;
    }

    result = (char *)utf8->content;
    utf8->content = NULL;

  cleanup:
    xmlCharEncCloseFunc(handler);
    xmlBufferFree(input);
    xmlBufferFree(utf8);

    return result;
}



static int
virVMXGetConfigString(virConfPtr conf, const char *name, char **string,
                      bool optional)
{
    virConfValuePtr value;

    *string = NULL;
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        }

        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Missing essential config entry '%s'"), name);
        return -1;
    }

    if (value->type != VIR_CONF_STRING) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Config entry '%s' must be a string"), name);
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        }

        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Missing essential config entry '%s'"), name);
        return -1;
    }

    *string = strdup(value->str);

    if (*string == NULL) {
        virReportOOMError();
        return -1;
    }

    return 0;
}



static int
virVMXGetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid,
                    bool optional)
{
    virConfValuePtr value;

    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Missing essential config entry '%s'"), name);
            return -1;
        }
    }

    if (value->type != VIR_CONF_STRING) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Config entry '%s' must be a string"), name);
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        } else {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Missing essential config entry '%s'"), name);
            return -1;
        }
    }

    if (virUUIDParse(value->str, uuid) < 0) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Could not parse UUID from string '%s'"), value->str);
        return -1;
    }

    return 0;
}



static int
virVMXGetConfigLong(virConfPtr conf, const char *name, long long *number,
                    long long default_, bool optional)
{
    virConfValuePtr value;

    *number = default_;
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Missing essential config entry '%s'"), name);
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                          _("Missing essential config entry '%s'"), name);
                return -1;
            }
        }

738
        if (STRCASEEQ(value->str, "unlimited")) {
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
            *number = -1;
        } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Config entry '%s' must represent an integer value"),
                      name);
            return -1;
        }
    } else {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Config entry '%s' must be a string"), name);
        return -1;
    }

    return 0;
}



static int
virVMXGetConfigBoolean(virConfPtr conf, const char *name, bool *boolean_,
                       bool default_, bool optional)
{
    virConfValuePtr value;

    *boolean_ = default_;
    value = virConfGetValue(conf, name);

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Missing essential config entry '%s'"), name);
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                          _("Missing essential config entry '%s'"), name);
                return -1;
            }
        }

        if (STRCASEEQ(value->str, "true")) {
            *boolean_ = 1;
        } else if (STRCASEEQ(value->str, "false")) {
            *boolean_ = 0;
        } else {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Config entry '%s' must represent a boolean value "
                        "(true|false)"), name);
            return -1;
        }
    } else {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                  _("Config entry '%s' must be a string"), name);
        return -1;
    }

    return 0;
}



static int
virVMXSCSIDiskNameToControllerAndUnit(const char *name, int *controller, int *unit)
M
Matthias Bolte 已提交
810 811 812 813
{
    int idx;

    if (! STRPREFIX(name, "sd")) {
814
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
815 816
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'sd'"));
M
Matthias Bolte 已提交
817 818 819 820 821 822
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
823
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
824
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
825 826 827
        return -1;
    }

828
    /* Each of the 4 SCSI controllers has 1 bus with 15 units each for devices */
M
Matthias Bolte 已提交
829
    if (idx >= (4 * 15)) {
830
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
831
                  _("SCSI disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
832 833 834 835
        return -1;
    }

    *controller = idx / 15;
836
    *unit = idx % 15;
M
Matthias Bolte 已提交
837

838 839 840
    /* Skip the controller ifself at unit 7 */
    if (*unit >= 7) {
        ++(*unit);
M
Matthias Bolte 已提交
841 842 843 844 845 846 847
    }

    return 0;
}



848 849
static int
virVMXIDEDiskNameToBusAndUnit(const char *name, int *bus, int *unit)
M
Matthias Bolte 已提交
850 851 852 853
{
    int idx;

    if (! STRPREFIX(name, "hd")) {
854
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
855 856
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'hd'"));
M
Matthias Bolte 已提交
857 858 859 860 861 862
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
863
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
864
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
865 866 867
        return -1;
    }

868
    /* The IDE controller has 2 buses with 2 units each for devices */
M
Matthias Bolte 已提交
869
    if (idx >= (2 * 2)) {
870
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
871
                  _("IDE disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
872 873 874
        return -1;
    }

875 876
    *bus = idx / 2;
    *unit = idx % 2;
M
Matthias Bolte 已提交
877 878 879 880 881 882

    return 0;
}



883 884
static int
virVMXFloppyDiskNameToUnit(const char *name, int *unit)
M
Matthias Bolte 已提交
885 886 887 888
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
889
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
890 891
                  _("Expecting domain XML attribute 'dev' of entry "
                    "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
892 893 894 895 896 897
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
898
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
899
                  _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
900 901 902
        return -1;
    }

903
    /* The FDC controller has 1 bus with 2 units for devices */
M
Matthias Bolte 已提交
904
    if (idx >= 2) {
905
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
906
                  _("Floppy disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
907 908 909
        return -1;
    }

910 911 912 913 914 915 916
    *unit = idx;

    return 0;
}



917 918
static int
virVMXVerifyDiskAddress(virCapsPtr caps, virDomainDiskDefPtr disk)
919 920 921 922 923 924 925
{
    virDomainDiskDef def;
    virDomainDeviceDriveAddressPtr drive;

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

    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
926
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
927 928 929 930 931 932 933 934 935 936 937
                  _("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) {
938
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
939 940 941 942 943 944 945
                  _("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) {
946
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
947 948 949 950 951 952 953 954
                  _("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) {
955
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
956 957 958 959 960 961
                      _("SCSI controller index %d out of [0..3] range"),
                      drive->controller);
            return -1;
        }

        if (drive->bus != 0) {
962
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
963 964 965 966 967 968
                      _("SCSI bus index %d out of [0] range"),
                      drive->bus);
            return -1;
        }

        if (drive->unit > 15 || drive->unit == 7) {
969
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
970 971 972 973 974 975
                      _("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) {
976
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
977 978 979 980 981 982
                      _("IDE controller index %d out of [0] range"),
                      drive->controller);
            return -1;
        }

        if (drive->bus > 1) {
983
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
984 985 986 987 988 989
                      _("IDE bus index %d out of [0..1] range"),
                      drive->bus);
            return -1;
        }

        if (drive->unit > 1) {
990
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
991 992 993 994 995 996
                      _("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) {
997
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
998 999 1000 1001 1002 1003
                      _("FDC controller index %d out of [0] range"),
                      drive->controller);
            return -1;
        }

        if (drive->bus != 0) {
1004
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1005 1006 1007 1008 1009 1010
                      _("FDC bus index %d out of [0] range"),
                      drive->bus);
            return -1;
        }

        if (drive->unit > 1) {
1011
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1012 1013 1014 1015 1016
                      _("FDC unit index %d out of [0..1] range"),
                      drive->unit);
            return -1;
        }
    } else {
1017
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                  _("Unsupported bus type '%s'"),
                  virDomainDiskBusTypeToString(disk->bus));
        return -1;
    }

    return 0;
}



1028 1029 1030
static int
virVMXHandleLegacySCSIDiskDriverName(virDomainDefPtr def,
                                     virDomainDiskDefPtr disk)
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
{
    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);
    }

1046
    model = virDomainControllerModelSCSITypeFromString(disk->driverName);
1047 1048

    if (model < 0) {
1049
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
                  _("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) {
1062
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1063 1064 1065 1066 1067 1068 1069 1070
                  _("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) {
1071
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1072 1073
                  _("Inconsistent SCSI controller model ('%s' is not '%s') "
                    "for SCSI controller index %d"), disk->driverName,
1074
                  virDomainControllerModelSCSITypeToString(controller->model),
1075 1076 1077
                  controller->idx);
        return -1;
    }
M
Matthias Bolte 已提交
1078 1079 1080 1081 1082 1083

    return 0;
}



1084 1085 1086
static int
virVMXGatherSCSIControllers(virVMXContext *ctx, virDomainDefPtr def,
                            int virtualDev[4], bool present[4])
1087 1088 1089
{
    int result = -1;
    int i, k;
M
Matthias Bolte 已提交
1090
    virDomainDiskDefPtr disk;
1091 1092 1093 1094
    virDomainControllerDefPtr controller;
    bool controllerHasDisksAttached;
    int count = 0;
    int *autodetectedModels;
M
Matthias Bolte 已提交
1095

1096 1097 1098 1099 1100 1101 1102
    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 已提交
1103

1104
        if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1105
            /* skip non-SCSI controllers */
M
Matthias Bolte 已提交
1106 1107 1108
            continue;
        }

1109
        controllerHasDisksAttached = false;
1110

1111 1112 1113 1114 1115 1116
        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;
1117 1118
                break;
            }
M
Matthias Bolte 已提交
1119 1120
        }

1121
        if (! controllerHasDisksAttached) {
1122
            /* skip SCSI controllers without attached disks */
1123 1124 1125
            continue;
        }

1126
        if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_AUTO &&
1127
            ctx->autodetectSCSIControllerModel != NULL) {
1128 1129
            count = 0;

1130 1131
            /* try to autodetect the SCSI controller model by collecting
             * SCSI controller model of all disks attached to this controller */
1132 1133 1134 1135 1136
            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) {
1137 1138 1139
                    if (ctx->autodetectSCSIControllerModel
                               (disk, &autodetectedModels[count],
                                ctx->opaque) < 0) {
1140 1141 1142 1143 1144 1145 1146
                        goto cleanup;
                    }

                    ++count;
                }
            }

1147 1148
            /* autodetection fails when the disks attached to one controller
             * have inconsistent SCSI controller models */
1149 1150
            for (k = 0; k < count; ++k) {
                if (autodetectedModels[k] != autodetectedModels[0]) {
1151
                    VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1152 1153 1154 1155 1156 1157 1158 1159
                              _("Disks on SCSI controller %d have inconsistent "
                                "controller models, cannot autodetect model"),
                              controller->idx);
                    goto cleanup;
                }
            }

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

1162
        if (controller->model != -1 &&
1163 1164 1165 1166
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_BUSLOGIC &&
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC &&
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSISAS1068 &&
            controller->model != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VMPVSCSI) {
1167
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1168 1169
                      _("Expecting domain XML attribute 'model' of entry "
                        "'controller' to be 'buslogic' or 'lsilogic' or "
1170
                        "'lsisas1068' or 'vmpvscsi' but found '%s'"),
1171
                      virDomainControllerModelSCSITypeToString(controller->model));
1172
            goto cleanup;
M
Matthias Bolte 已提交
1173
        }
1174 1175 1176

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

1179 1180 1181 1182 1183 1184
    result = 0;

  cleanup:
    VIR_FREE(autodetectedModels);

    return result;
M
Matthias Bolte 已提交
1185 1186 1187 1188 1189 1190 1191 1192
}



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

1193
virDomainDefPtr
1194
virVMXParseConfig(virVMXContext *ctx, virCapsPtr caps, const char *vmx)
1195
{
M
Matthias Bolte 已提交
1196
    bool success = false;
1197
    virConfPtr conf = NULL;
1198 1199
    char *encoding = NULL;
    char *utf8;
1200 1201 1202 1203
    virDomainDefPtr def = NULL;
    long long config_version = 0;
    long long virtualHW_version = 0;
    long long memsize = 0;
1204 1205
    long long sched_mem_max = 0;
    long long sched_mem_minsize = 0;
1206 1207
    long long numvcpus = 0;
    char *sched_cpu_affinity = NULL;
1208
    char *sched_cpu_shares = NULL;
M
Matthias Bolte 已提交
1209
    char *guestOS = NULL;
M
Matthias Bolte 已提交
1210
    bool smbios_reflecthost = false;
1211
    int controller;
1212
    int bus;
1213
    int port;
1214
    bool present;
1215 1216
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
    int unit;
1217

1218
    if (ctx->parseFileName == NULL) {
1219 1220
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("virVMXContext has no parseFileName function set"));
1221 1222 1223
        return NULL;
    }

1224 1225 1226 1227 1228 1229
    conf = virConfReadMem(vmx, strlen(vmx), VIR_CONF_FLAG_VMX_FORMAT);

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

1230
    /* vmx:.encoding */
1231
    if (virVMXGetConfigString(conf, ".encoding", &encoding, true) < 0) {
1232 1233 1234 1235 1236 1237 1238 1239 1240
        goto cleanup;
    }

    if (encoding == NULL || STRCASEEQ(encoding, "UTF-8")) {
        /* nothing */
    } else {
        virConfFree(conf);
        conf = NULL;

1241
        utf8 = virVMXConvertToUTF8(encoding, vmx);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

        if (utf8 == NULL) {
            goto cleanup;
        }

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

        VIR_FREE(utf8);

        if (conf == NULL) {
            goto cleanup;
        }
    }

    /* Allocate domain def */
1257
    if (VIR_ALLOC(def) < 0) {
1258
        virReportOOMError();
A
ajia@redhat.com 已提交
1259
        goto cleanup;
1260 1261
    }

1262
    def->virtType = VIR_DOMAIN_VIRT_VMWARE;
1263 1264
    def->id = -1;

M
Matthias Bolte 已提交
1265
    /* vmx:config.version */
1266 1267
    if (virVMXGetConfigLong(conf, "config.version", &config_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1268
        goto cleanup;
1269 1270 1271
    }

    if (config_version != 8) {
1272
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1273 1274
                  _("Expecting VMX entry 'config.version' to be 8 but found "
                    "%lld"), config_version);
M
Matthias Bolte 已提交
1275
        goto cleanup;
1276 1277
    }

M
Matthias Bolte 已提交
1278
    /* vmx:virtualHW.version */
1279 1280
    if (virVMXGetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1281
        goto cleanup;
1282 1283
    }

P
Patrice LACHANCE 已提交
1284 1285
    if (virtualHW_version != 4 && virtualHW_version != 7 &&
        virtualHW_version != 8) {
1286
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
P
Patrice LACHANCE 已提交
1287
                  _("Expecting VMX entry 'virtualHW.version' to be 4, 7 or 8 "
1288 1289
                    "but found %lld"),
                  virtualHW_version);
M
Matthias Bolte 已提交
1290
        goto cleanup;
1291 1292
    }

M
Matthias Bolte 已提交
1293
    /* vmx:uuid.bios -> def:uuid */
1294
    /* FIXME: Need to handle 'uuid.action = "create"' */
1295
    if (virVMXGetConfigUUID(conf, "uuid.bios", def->uuid, true) < 0) {
M
Matthias Bolte 已提交
1296
        goto cleanup;
1297 1298
    }

M
Matthias Bolte 已提交
1299
    /* vmx:displayName -> def:name */
1300
    if (virVMXGetConfigString(conf, "displayName", &def->name, true) < 0) {
M
Matthias Bolte 已提交
1301
        goto cleanup;
1302 1303
    }

1304
    if (def->name != NULL) {
1305 1306 1307
        if (virVMXUnescapeHexPercent(def->name) < 0 ||
            virVMXUnescapeHexPipe(def->name) < 0) {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
1308 1309 1310 1311 1312
                      _("VMX entry 'name' contains invalid escape sequence"));
            goto cleanup;
        }
    }

1313
    /* vmx:annotation -> def:description */
1314 1315
    if (virVMXGetConfigString(conf, "annotation", &def->description,
                              true) < 0) {
1316 1317 1318 1319
        goto cleanup;
    }

    if (def->description != NULL) {
1320 1321
        if (virVMXUnescapeHexPipe(def->description) < 0) {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
1322 1323 1324
                      _("VMX entry 'annotation' contains invalid escape "
                        "sequence"));
            goto cleanup;
1325 1326 1327
        }
    }

1328
    /* vmx:memsize -> def:mem.max_balloon */
1329
    if (virVMXGetConfigLong(conf, "memsize", &memsize, 32, true) < 0) {
M
Matthias Bolte 已提交
1330
        goto cleanup;
1331 1332 1333
    }

    if (memsize <= 0 || memsize % 4 != 0) {
1334
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1335 1336
                  _("Expecting VMX entry 'memsize' to be an unsigned "
                    "integer (multiple of 4) but found %lld"), memsize);
M
Matthias Bolte 已提交
1337
        goto cleanup;
1338 1339
    }

1340
    def->mem.max_balloon = memsize * 1024; /* Scale from megabytes to kilobytes */
1341

1342
    /* vmx:sched.mem.max -> def:mem.cur_balloon */
1343 1344
    if (virVMXGetConfigLong(conf, "sched.mem.max", &sched_mem_max, memsize,
                            true) < 0) {
M
Matthias Bolte 已提交
1345
        goto cleanup;
1346 1347
    }

1348 1349
    if (sched_mem_max < 0) {
        sched_mem_max = memsize;
1350 1351
    }

1352
    def->mem.cur_balloon = sched_mem_max * 1024; /* Scale from megabytes to kilobytes */
1353

1354 1355
    if (def->mem.cur_balloon > def->mem.max_balloon) {
        def->mem.cur_balloon = def->mem.max_balloon;
1356 1357
    }

1358
    /* vmx:sched.mem.minsize -> def:mem.min_guarantee */
1359 1360
    if (virVMXGetConfigLong(conf, "sched.mem.minsize", &sched_mem_minsize, 0,
                            true) < 0) {
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        goto cleanup;
    }

    if (sched_mem_minsize < 0) {
        sched_mem_minsize = 0;
    }

    def->mem.min_guarantee = sched_mem_minsize * 1024; /* Scale from megabytes to kilobytes */

    if (def->mem.min_guarantee > def->mem.max_balloon) {
        def->mem.min_guarantee = def->mem.max_balloon;
    }

M
Matthias Bolte 已提交
1374
    /* vmx:numvcpus -> def:vcpus */
1375
    if (virVMXGetConfigLong(conf, "numvcpus", &numvcpus, 1, true) < 0) {
M
Matthias Bolte 已提交
1376
        goto cleanup;
1377 1378 1379
    }

    if (numvcpus <= 0 || (numvcpus % 2 != 0 && numvcpus != 1)) {
1380
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1381 1382
                  _("Expecting VMX entry 'numvcpus' to be an unsigned "
                    "integer (1 or a multiple of 2) but found %lld"), numvcpus);
M
Matthias Bolte 已提交
1383
        goto cleanup;
1384 1385
    }

E
Eric Blake 已提交
1386
    def->maxvcpus = def->vcpus = numvcpus;
1387

M
Matthias Bolte 已提交
1388
    /* vmx:sched.cpu.affinity -> def:cpumask */
1389
    /* NOTE: maps to VirtualMachine:config.cpuAffinity.affinitySet */
1390 1391
    if (virVMXGetConfigString(conf, "sched.cpu.affinity", &sched_cpu_affinity,
                              true) < 0) {
M
Matthias Bolte 已提交
1392
        goto cleanup;
1393 1394
    }

1395
    if (sched_cpu_affinity != NULL && STRCASENEQ(sched_cpu_affinity, "all")) {
1396 1397 1398 1399 1400 1401
        const char *current = sched_cpu_affinity;
        int number, count = 0;

        def->cpumasklen = 0;

        if (VIR_ALLOC_N(def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0) {
1402
            virReportOOMError();
M
Matthias Bolte 已提交
1403
            goto cleanup;
1404 1405 1406 1407 1408 1409 1410 1411
        }

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

            number = virParseNumber(&current);

            if (number < 0) {
1412
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1413 1414 1415
                          _("Expecting VMX entry 'sched.cpu.affinity' to be "
                            "a comma separated list of unsigned integers but "
                            "found '%s'"), sched_cpu_affinity);
M
Matthias Bolte 已提交
1416
                goto cleanup;
1417 1418 1419
            }

            if (number >= VIR_DOMAIN_CPUMASK_LEN) {
1420
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1421 1422
                          _("VMX entry 'sched.cpu.affinity' contains a %d, "
                            "this value is too large"), number);
M
Matthias Bolte 已提交
1423
                goto cleanup;
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
            }

            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 {
1440
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1441 1442 1443
                          _("Expecting VMX entry 'sched.cpu.affinity' to be "
                            "a comma separated list of unsigned integers but "
                            "found '%s'"), sched_cpu_affinity);
M
Matthias Bolte 已提交
1444
                goto cleanup;
1445 1446 1447 1448 1449 1450
            }

            virSkipSpaces(&current);
        }

        if (count < numvcpus) {
1451
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1452 1453 1454
                      _("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 已提交
1455
            goto cleanup;
1456 1457 1458
        }
    }

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
    /* vmx:sched.cpu.shares -> def:cputune.shares */
    if (virVMXGetConfigString(conf, "sched.cpu.shares", &sched_cpu_shares,
                              true) < 0) {
        goto cleanup;
    }

    if (sched_cpu_shares != NULL) {
        /* See http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.SharesInfo.Level.html */
        if (STRCASEEQ(sched_cpu_shares, "low")) {
            def->cputune.shares = def->vcpus * 500;
        } else if (STRCASEEQ(sched_cpu_shares, "normal")) {
            def->cputune.shares = def->vcpus * 1000;
        } else if (STRCASEEQ(sched_cpu_shares, "high")) {
            def->cputune.shares = def->vcpus * 2000;
        } else if (virStrToLong_ul(sched_cpu_shares, NULL, 10,
                                   &def->cputune.shares) < 0) {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
                      _("Expecting VMX entry 'sched.cpu.shares' to be an "
                        "unsigned integer or 'low', 'normal' or 'high' but "
                        "found '%s'"), sched_cpu_shares);
            goto cleanup;
        }
    }

1483 1484 1485 1486 1487 1488 1489 1490 1491
    /* 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) {
1492
        virReportOOMError();
M
Matthias Bolte 已提交
1493
        goto cleanup;
1494 1495
    }

M
Matthias Bolte 已提交
1496
    /* vmx:guestOS -> def:os.arch */
1497
    if (virVMXGetConfigString(conf, "guestOS", &guestOS, true) < 0) {
M
Matthias Bolte 已提交
1498
        goto cleanup;
M
Matthias Bolte 已提交
1499 1500
    }

1501
    if (guestOS != NULL && virFileHasSuffix(guestOS, "-64")) {
M
Matthias Bolte 已提交
1502 1503 1504 1505 1506 1507
        def->os.arch = strdup("x86_64");
    } else {
        def->os.arch = strdup("i686");
    }

    if (def->os.arch == NULL) {
1508
        virReportOOMError();
M
Matthias Bolte 已提交
1509
        goto cleanup;
M
Matthias Bolte 已提交
1510 1511
    }

M
Matthias Bolte 已提交
1512
    /* vmx:smbios.reflecthost -> def:os.smbios_mode */
1513 1514
    if (virVMXGetConfigBoolean(conf, "smbios.reflecthost",
                               &smbios_reflecthost, false, true) < 0) {
M
Matthias Bolte 已提交
1515 1516 1517 1518 1519 1520 1521
        goto cleanup;
    }

    if (smbios_reflecthost) {
        def->os.smbios_mode = VIR_DOMAIN_SMBIOS_HOST;
    }

1522 1523
    /* def:features */
    /* FIXME */
1524

1525 1526
    /* def:clock */
    /* FIXME */
1527 1528

    /* def:graphics */
M
Matthias Bolte 已提交
1529
    if (VIR_ALLOC_N(def->graphics, 1) < 0) {
1530
        virReportOOMError();
M
Matthias Bolte 已提交
1531
        goto cleanup;
M
Matthias Bolte 已提交
1532 1533 1534 1535
    }

    def->ngraphics = 0;

1536
    if (virVMXParseVNC(conf, &def->graphics[def->ngraphics]) < 0) {
M
Matthias Bolte 已提交
1537
        goto cleanup;
M
Matthias Bolte 已提交
1538 1539 1540 1541 1542
    }

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

M
Matthias Bolte 已提交
1544 1545
    /* def:disks: 4 * 15 scsi + 2 * 2 ide + 2 floppy = 66 */
    if (VIR_ALLOC_N(def->disks, 66) < 0) {
1546
        virReportOOMError();
M
Matthias Bolte 已提交
1547
        goto cleanup;
1548 1549 1550 1551 1552 1553
    }

    def->ndisks = 0;

    /* def:disks (scsi) */
    for (controller = 0; controller < 4; ++controller) {
1554 1555
        if (virVMXParseSCSIController(conf, controller, &present,
                                      &scsi_virtualDev[controller]) < 0) {
M
Matthias Bolte 已提交
1556
            goto cleanup;
1557 1558 1559 1560 1561 1562
        }

        if (! present) {
            continue;
        }

1563 1564
        for (unit = 0; unit < 16; ++unit) {
            if (unit == 7) {
1565
                /*
1566
                 * SCSI unit 7 is assigned to the SCSI controller and cannot be
1567 1568 1569 1570 1571
                 * used for disk devices.
                 */
                continue;
            }

1572 1573 1574
            if (virVMXParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
                                VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
                                &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1575
                goto cleanup;
1576 1577 1578 1579 1580 1581 1582
            }

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

1583
            if (virVMXParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1584
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
1585
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1586
                goto cleanup;
1587 1588 1589 1590 1591 1592 1593 1594 1595
            }

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

    /* def:disks (ide) */
1596 1597
    for (bus = 0; bus < 2; ++bus) {
        for (unit = 0; unit < 2; ++unit) {
1598 1599 1600
            if (virVMXParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
                                VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
                                &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1601
                goto cleanup;
1602 1603 1604 1605 1606 1607 1608
            }

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

1609 1610 1611
            if (virVMXParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
                                VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
                                &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1612
                goto cleanup;
1613 1614 1615 1616 1617 1618 1619 1620 1621
            }

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

    /* def:disks (floppy) */
1622
    for (unit = 0; unit < 2; ++unit) {
1623 1624 1625
        if (virVMXParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_FLOPPY,
                            VIR_DOMAIN_DISK_BUS_FDC, 0, unit,
                            &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1626
            goto cleanup;
1627 1628 1629 1630 1631 1632 1633
        }

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

1634 1635
    /* def:controllers */
    if (virDomainDefAddImplicitControllers(def) < 0) {
1636
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not add controllers"));
1637 1638 1639 1640 1641 1642 1643
        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) {
1644
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
                          _("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];
        }
    }

1655 1656 1657 1658 1659
    /* def:fss */
    /* FIXME */

    /* def:nets */
    if (VIR_ALLOC_N(def->nets, 4) < 0) {
1660
        virReportOOMError();
M
Matthias Bolte 已提交
1661
        goto cleanup;
1662 1663 1664 1665 1666
    }

    def->nnets = 0;

    for (controller = 0; controller < 4; ++controller) {
1667 1668
        if (virVMXParseEthernet(conf, controller,
                                &def->nets[def->nnets]) < 0) {
M
Matthias Bolte 已提交
1669
            goto cleanup;
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
        }

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

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

1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    /* def:videos */
    if (VIR_ALLOC_N(def->videos, 1) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    def->nvideos = 0;

    if (virVMXParseSVGA(conf, &def->videos[def->nvideos]) < 0) {
        goto cleanup;
    }

    def->nvideos = 1;

1694 1695 1696 1697 1698 1699 1700 1701
    /* def:sounds */
    /* FIXME */

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

    /* def:serials */
    if (VIR_ALLOC_N(def->serials, 4) < 0) {
1702
        virReportOOMError();
M
Matthias Bolte 已提交
1703
        goto cleanup;
1704 1705 1706 1707 1708
    }

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1709 1710
        if (virVMXParseSerial(ctx, conf, port,
                              &def->serials[def->nserials]) < 0) {
M
Matthias Bolte 已提交
1711
            goto cleanup;
1712 1713 1714 1715 1716 1717 1718 1719 1720
        }

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

    /* def:parallels */
    if (VIR_ALLOC_N(def->parallels, 3) < 0) {
1721
        virReportOOMError();
M
Matthias Bolte 已提交
1722
        goto cleanup;
1723 1724 1725 1726 1727
    }

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1728 1729
        if (virVMXParseParallel(ctx, conf, port,
                                &def->parallels[def->nparallels]) < 0) {
M
Matthias Bolte 已提交
1730
            goto cleanup;
1731 1732 1733 1734 1735 1736 1737
        }

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

M
Matthias Bolte 已提交
1738 1739
    success = true;

M
Matthias Bolte 已提交
1740
  cleanup:
M
Matthias Bolte 已提交
1741 1742 1743 1744 1745
    if (! success) {
        virDomainDefFree(def);
        def = NULL;
    }

1746
    virConfFree(conf);
1747
    VIR_FREE(encoding);
1748
    VIR_FREE(sched_cpu_affinity);
1749
    VIR_FREE(sched_cpu_shares);
M
Matthias Bolte 已提交
1750
    VIR_FREE(guestOS);
1751 1752 1753 1754 1755 1756

    return def;
}



M
Matthias Bolte 已提交
1757
int
1758
virVMXParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1759
{
1760
    bool enabled = false;
M
Matthias Bolte 已提交
1761
    long long port = 0;
1762
    char *listenAddr = NULL;
M
Matthias Bolte 已提交
1763 1764

    if (def == NULL || *def != NULL) {
1765
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1766 1767 1768
        return -1;
    }

1769 1770
    if (virVMXGetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
                               false, true) < 0) {
M
Matthias Bolte 已提交
1771 1772 1773 1774 1775 1776 1777 1778
        return -1;
    }

    if (! enabled) {
        return 0;
    }

    if (VIR_ALLOC(*def) < 0) {
1779
        virReportOOMError();
M
Matthias Bolte 已提交
1780 1781 1782 1783 1784
        goto failure;
    }

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1785 1786 1787
    if (virVMXGetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
                            true) < 0 ||
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.ip",
1788
                              &listenAddr, true) < 0 ||
1789 1790 1791 1792
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.keymap",
                              &(*def)->data.vnc.keymap, true) < 0 ||
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.password",
                              &(*def)->data.vnc.auth.passwd, true) < 0) {
M
Matthias Bolte 已提交
1793 1794 1795
        goto failure;
    }

1796 1797 1798 1799 1800 1801
    if (listenAddr) {
        if (virDomainGraphicsListenSetAddress(*def, 0, listenAddr, -1, true) < 0)
            goto failure;
        VIR_FREE(listenAddr);
    }

M
Matthias Bolte 已提交
1802
    if (port < 0) {
1803
        VIR_WARN("VNC is enabled but VMX entry 'RemoteDisplay.vnc.port' "
M
Matthias Bolte 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
                  "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:
1820
    VIR_FREE(listenAddr);
M
Matthias Bolte 已提交
1821 1822 1823 1824 1825 1826 1827 1828
    virDomainGraphicsDefFree(*def);
    *def = NULL;

    return -1;
}



1829
int
1830 1831
virVMXParseSCSIController(virConfPtr conf, int controller, bool *present,
                          int *virtualDev)
1832
{
1833
    int result = -1;
1834 1835
    char present_name[32];
    char virtualDev_name[32];
1836 1837
    char *virtualDev_string = NULL;
    char *tmp;
1838

1839
    if (virtualDev == NULL || *virtualDev != -1) {
1840
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1841
        return -1;
1842 1843 1844
    }

    if (controller < 0 || controller > 3) {
1845
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1846
                  _("SCSI controller index %d out of [0..3] range"),
1847
                  controller);
1848
        return -1;
1849 1850
    }

1851 1852 1853
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1854

1855
    if (virVMXGetConfigBoolean(conf, present_name, present, false, true) < 0) {
1856
        goto cleanup;
1857 1858 1859
    }

    if (! *present) {
1860 1861
        result = 0;
        goto cleanup;
1862 1863
    }

1864 1865
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev_string,
                              true) < 0) {
1866
        goto cleanup;
1867 1868
    }

1869 1870 1871 1872 1873 1874 1875
    if (virtualDev_string != NULL) {
        tmp = virtualDev_string;

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

1876
        *virtualDev = virVMXControllerModelSCSITypeFromString(virtualDev_string);
1877 1878

        if (*virtualDev == -1 ||
1879 1880 1881 1882
            (*virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_BUSLOGIC &&
             *virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC &&
             *virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSISAS1068 &&
             *virtualDev != VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VMPVSCSI)) {
1883
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1884
                      _("Expecting VMX entry '%s' to be 'buslogic' or 'lsilogic' "
1885 1886
                        "or 'lsisas1068' or 'pvscsi' but found '%s'"),
                       virtualDev_name, virtualDev_string);
1887
            goto cleanup;
1888
        }
1889 1890
    }

1891
    result = 0;
1892

1893
  cleanup:
1894
    VIR_FREE(virtualDev_string);
1895

1896
    return result;
1897 1898 1899 1900 1901
}



int
1902 1903 1904
virVMXParseDisk(virVMXContext *ctx, virCapsPtr caps, virConfPtr conf,
                int device, int busType, int controllerOrBus, int unit,
                virDomainDiskDefPtr *def)
1905 1906
{
    /*
1907 1908 1909 1910
     *          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]
1911
     *
1912 1913 1914 1915
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK, VIR_DOMAIN_DISK_DEVICE_CDROM}
     *         busType = VIR_DOMAIN_DISK_BUS_IDE
     * controllerOrBus = [0..1] -> bus
     *            unit = [0..1]
1916
     *
1917 1918 1919 1920
     *          device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *         busType = VIR_DOMAIN_DISK_BUS_FDC
     * controllerOrBus = [0]
     *            unit = [0..1]
1921 1922
     */

M
Matthias Bolte 已提交
1923
    int result = -1;
1924 1925 1926
    char *prefix = NULL;

    char present_name[32] = "";
1927
    bool present = false;
1928 1929

    char startConnected_name[32] = "";
1930
    bool startConnected = false;
1931 1932 1933 1934 1935

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

    char clientDevice_name[32] = "";
1936
    bool clientDevice = false;
1937 1938 1939 1940 1941 1942 1943 1944

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

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

    char writeThrough_name[32] = "";
1945
    bool writeThrough = false;
1946 1947

    if (def == NULL || *def != NULL) {
1948
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1949 1950 1951 1952
        return -1;
    }

    if (VIR_ALLOC(*def) < 0) {
1953
        virReportOOMError();
M
Matthias Bolte 已提交
1954
        return -1;
1955 1956 1957
    }

    (*def)->device = device;
1958
    (*def)->bus = busType;
1959 1960 1961 1962

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
1963 1964
        if (busType == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controllerOrBus < 0 || controllerOrBus > 3) {
1965
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1966
                          _("SCSI controller index %d out of [0..3] range"),
1967
                          controllerOrBus);
M
Matthias Bolte 已提交
1968
                goto cleanup;
1969 1970
            }

1971
            if (unit < 0 || unit > 15 || unit == 7) {
1972
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1973 1974
                          _("SCSI unit index %d out of [0..6,8..15] range"),
                          unit);
M
Matthias Bolte 已提交
1975
                goto cleanup;
1976 1977
            }

1978
            if (virAsprintf(&prefix, "scsi%d:%d", controllerOrBus, unit) < 0) {
1979
                virReportOOMError();
M
Matthias Bolte 已提交
1980
                goto cleanup;
1981 1982
            }

M
Matthias Bolte 已提交
1983
            (*def)->dst =
1984
               virIndexToDiskName
1985
                 (controllerOrBus * 15 + (unit < 7 ? unit : unit - 1), "sd");
1986 1987

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
1988
                goto cleanup;
1989
            }
1990 1991
        } else if (busType == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controllerOrBus < 0 || controllerOrBus > 1) {
1992
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
1993 1994
                          _("IDE bus index %d out of [0..1] range"),
                          controllerOrBus);
M
Matthias Bolte 已提交
1995
                goto cleanup;
1996 1997
            }

1998
            if (unit < 0 || unit > 1) {
1999
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2000
                          _("IDE unit index %d out of [0..1] range"), unit);
M
Matthias Bolte 已提交
2001
                goto cleanup;
2002 2003
            }

2004
            if (virAsprintf(&prefix, "ide%d:%d", controllerOrBus, unit) < 0) {
2005
                virReportOOMError();
M
Matthias Bolte 已提交
2006
                goto cleanup;
2007 2008
            }

2009
            (*def)->dst = virIndexToDiskName(controllerOrBus * 2 + unit, "hd");
2010 2011

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2012
                goto cleanup;
2013 2014
            }
        } else {
2015
            VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
2016
                      _("Unsupported bus type '%s' for device type '%s'"),
2017
                      virDomainDiskBusTypeToString(busType),
M
Matthias Bolte 已提交
2018
                      virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2019
            goto cleanup;
2020 2021
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2022 2023
        if (busType == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controllerOrBus != 0) {
2024
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2025 2026 2027 2028 2029 2030
                          _("FDC controller index %d out of [0] range"),
                          controllerOrBus);
                goto cleanup;
            }

            if (unit < 0 || unit > 1) {
2031
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2032 2033
                          _("FDC unit index %d out of [0..1] range"),
                          unit);
M
Matthias Bolte 已提交
2034
                goto cleanup;
2035 2036
            }

2037
            if (virAsprintf(&prefix, "floppy%d", unit) < 0) {
2038
                virReportOOMError();
M
Matthias Bolte 已提交
2039
                goto cleanup;
2040 2041
            }

2042
            (*def)->dst = virIndexToDiskName(unit, "fd");
2043 2044

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2045
                goto cleanup;
2046 2047
            }
        } else {
2048
            VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
2049
                      _("Unsupported bus type '%s' for device type '%s'"),
2050
                      virDomainDiskBusTypeToString(busType),
M
Matthias Bolte 已提交
2051
                      virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2052
            goto cleanup;
2053 2054
        }
    } else {
2055
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
2056
                  _("Unsupported device type '%s'"),
M
Matthias Bolte 已提交
2057
                  virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2058
        goto cleanup;
2059 2060
    }

2061 2062 2063 2064 2065 2066 2067
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(deviceType);
    VMX_BUILD_NAME(clientDevice);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
    VMX_BUILD_NAME(writeThrough);
2068 2069

    /* vmx:present */
2070
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2071
        goto cleanup;
2072 2073 2074
    }

    /* vmx:startConnected */
2075 2076
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2077
        goto cleanup;
2078 2079 2080 2081 2082 2083 2084 2085
    }

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

    /* vmx:deviceType -> def:type */
2086
    if (virVMXGetConfigString(conf, deviceType_name, &deviceType, true) < 0) {
M
Matthias Bolte 已提交
2087
        goto cleanup;
2088 2089 2090
    }

    /* vmx:clientDevice */
2091 2092
    if (virVMXGetConfigBoolean(conf, clientDevice_name, &clientDevice, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2093
        goto cleanup;
2094 2095 2096 2097 2098
    }

    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
2099
         * handle them (e.g. assign an image) without the VI Client GUI.
2100 2101 2102 2103 2104
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
2105
    if (virVMXGetConfigString(conf, fileType_name, &fileType, true) < 0) {
M
Matthias Bolte 已提交
2106
        goto cleanup;
2107 2108 2109
    }

    /* vmx:fileName -> def:src, def:type */
2110
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2111
        goto cleanup;
2112 2113 2114
    }

    /* vmx:writeThrough -> def:cachemode */
2115 2116
    if (virVMXGetConfigBoolean(conf, writeThrough_name, &writeThrough, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2117
        goto cleanup;
2118 2119 2120 2121
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
2122
        if (virFileHasSuffix(fileName, ".vmdk")) {
2123
            if (deviceType != NULL) {
2124
                if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
2125 2126
                    STRCASENEQ(deviceType, "scsi-hardDisk") &&
                    STRCASENEQ(deviceType, "disk")) {
2127
                    VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2128
                              _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
2129 2130
                                "or 'disk' but found '%s'"), deviceType_name,
                              deviceType);
M
Matthias Bolte 已提交
2131
                    goto cleanup;
2132
                } else if (busType == VIR_DOMAIN_DISK_BUS_IDE &&
2133 2134
                           STRCASENEQ(deviceType, "ata-hardDisk") &&
                           STRCASENEQ(deviceType, "disk")) {
2135
                    VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2136
                              _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
2137 2138
                                "or 'disk' but found '%s'"), deviceType_name,
                              deviceType);
M
Matthias Bolte 已提交
2139
                    goto cleanup;
2140 2141 2142 2143
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2144
            (*def)->src = ctx->parseFileName(fileName, ctx->opaque);
2145 2146 2147
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;

M
Matthias Bolte 已提交
2148
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2149
                goto cleanup;
M
Matthias Bolte 已提交
2150
            }
2151
        } else if (virFileHasSuffix(fileName, ".iso") ||
2152
                   STRCASEEQ(deviceType, "atapi-cdrom")) {
2153 2154 2155 2156 2157 2158 2159 2160
            /*
             * 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 {
2161
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2162 2163
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2164
            goto cleanup;
2165 2166
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2167
        if (virFileHasSuffix(fileName, ".iso")) {
2168 2169
            if (deviceType != NULL) {
                if (STRCASENEQ(deviceType, "cdrom-image")) {
2170
                    VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2171 2172
                              _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                "but found '%s'"), deviceType_name, deviceType);
M
Matthias Bolte 已提交
2173
                    goto cleanup;
2174 2175 2176 2177
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2178
            (*def)->src = ctx->parseFileName(fileName, ctx->opaque);
2179

M
Matthias Bolte 已提交
2180
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2181
                goto cleanup;
M
Matthias Bolte 已提交
2182
            }
2183
        } else if (virFileHasSuffix(fileName, ".vmdk")) {
2184 2185 2186 2187 2188 2189 2190
            /*
             * 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;
2191
        } else if (STRCASEEQ(deviceType, "atapi-cdrom")) {
2192 2193 2194 2195 2196
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
2197
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2198 2199
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2200
            goto cleanup;
2201 2202
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2203
        if (virFileHasSuffix(fileName, ".flp")) {
2204 2205
            if (fileType != NULL) {
                if (STRCASENEQ(fileType, "file")) {
2206
                    VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2207 2208
                              _("Expecting VMX entry '%s' to be 'file' but "
                                "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2209
                    goto cleanup;
2210 2211 2212 2213
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2214
            (*def)->src = ctx->parseFileName(fileName, ctx->opaque);
2215

M
Matthias Bolte 已提交
2216
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2217
                goto cleanup;
M
Matthias Bolte 已提交
2218
            }
2219
        } else if (fileType != NULL && STRCASEEQ(fileType, "device")) {
2220 2221 2222 2223 2224
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
2225
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2226 2227
                      _("Invalid or not yet handled value '%s' for VMX entry "
                        "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2228
            goto cleanup;
2229 2230
        }
    } else {
2231
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported device type '%s'"),
M
Matthias Bolte 已提交
2232
                  virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2233
        goto cleanup;
2234 2235
    }

2236
    if (virDomainDiskDefAssignAddress(caps, *def) < 0) {
2237
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2238 2239 2240 2241
                  _("Could not assign address to disk '%s'"), (*def)->src);
        goto cleanup;
    }

M
Matthias Bolte 已提交
2242 2243
    result = 0;

M
Matthias Bolte 已提交
2244
  cleanup:
M
Matthias Bolte 已提交
2245 2246 2247 2248 2249
    if (result < 0) {
        virDomainDiskDefFree(*def);
        *def = NULL;
    }

2250 2251 2252 2253 2254 2255 2256
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2257
  ignore:
2258 2259 2260
    virDomainDiskDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2261 2262
    result = 0;

2263 2264 2265 2266 2267 2268
    goto cleanup;
}



int
2269
virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
2270
{
M
Matthias Bolte 已提交
2271
    int result = -1;
2272 2273 2274
    char prefix[48] = "";

    char present_name[48] = "";
2275
    bool present = false;
2276 2277

    char startConnected_name[48] = "";
2278
    bool startConnected = false;
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294

    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;

2295 2296 2297
    char features_name[48] = "";
    long long features = 0;

2298 2299 2300
    char vnet_name[48] = "";
    char *vnet = NULL;

M
Matthias Bolte 已提交
2301 2302 2303
    char networkName_name[48] = "";
    char *networkName = NULL;

2304
    if (def == NULL || *def != NULL) {
2305
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2306 2307 2308 2309
        return -1;
    }

    if (controller < 0 || controller > 3) {
2310
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2311
                  _("Ethernet controller index %d out of [0..3] range"),
2312
                  controller);
2313
        return -1;
2314 2315 2316
    }

    if (VIR_ALLOC(*def) < 0) {
2317
        virReportOOMError();
M
Matthias Bolte 已提交
2318
        return -1;
2319 2320
    }

2321
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
2322

2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(connectionType);
    VMX_BUILD_NAME(addressType);
    VMX_BUILD_NAME(generatedAddress);
    VMX_BUILD_NAME(address);
    VMX_BUILD_NAME(virtualDev);
    VMX_BUILD_NAME(features);
    VMX_BUILD_NAME(networkName);
    VMX_BUILD_NAME(vnet);
2333 2334

    /* vmx:present */
2335
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2336
        goto cleanup;
2337 2338 2339
    }

    /* vmx:startConnected */
2340 2341
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2342
        goto cleanup;
2343 2344 2345 2346 2347 2348 2349 2350
    }

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

    /* vmx:connectionType -> def:type */
2351 2352
    if (virVMXGetConfigString(conf, connectionType_name, &connectionType,
                              true) < 0) {
M
Matthias Bolte 已提交
2353
        goto cleanup;
2354 2355 2356
    }

    /* vmx:addressType, vmx:generatedAddress, vmx:address -> def:mac */
2357 2358 2359 2360 2361
    if (virVMXGetConfigString(conf, addressType_name, &addressType,
                              true) < 0 ||
        virVMXGetConfigString(conf, generatedAddress_name, &generatedAddress,
                              true) < 0 ||
        virVMXGetConfigString(conf, address_name, &address, true) < 0) {
M
Matthias Bolte 已提交
2362
        goto cleanup;
2363 2364
    }

2365 2366
    if (addressType == NULL || STRCASEEQ(addressType, "generated") ||
        STRCASEEQ(addressType, "vpx")) {
2367
        if (generatedAddress != NULL) {
2368
            if (virMacAddrParse(generatedAddress, (*def)->mac) < 0) {
2369
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2370 2371
                          _("Expecting VMX entry '%s' to be MAC address but "
                            "found '%s'"), generatedAddress_name,
2372
                          generatedAddress);
M
Matthias Bolte 已提交
2373
                goto cleanup;
2374 2375 2376 2377
            }
        }
    } else if (STRCASEEQ(addressType, "static")) {
        if (address != NULL) {
2378
            if (virMacAddrParse(address, (*def)->mac) < 0) {
2379
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2380 2381
                          _("Expecting VMX entry '%s' to be MAC address but "
                            "found '%s'"), address_name, address);
M
Matthias Bolte 已提交
2382
                goto cleanup;
2383 2384 2385
            }
        }
    } else {
2386
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2387 2388
                  _("Expecting VMX entry '%s' to be 'generated' or 'static' or "
                    "'vpx' but found '%s'"), addressType_name, addressType);
M
Matthias Bolte 已提交
2389
        goto cleanup;
2390 2391
    }

2392
    /* vmx:virtualDev, vmx:features -> def:model */
2393 2394
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev, true) < 0 ||
        virVMXGetConfigLong(conf, features_name, &features, 0, true) < 0) {
M
Matthias Bolte 已提交
2395
        goto cleanup;
2396 2397
    }

2398 2399 2400 2401 2402
    if (virtualDev != NULL) {
        if (STRCASENEQ(virtualDev, "vlance") &&
            STRCASENEQ(virtualDev, "vmxnet") &&
            STRCASENEQ(virtualDev, "vmxnet3") &&
            STRCASENEQ(virtualDev, "e1000")) {
2403
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2404 2405 2406
                      _("Expecting VMX entry '%s' to be 'vlance' or 'vmxnet' or "
                        "'vmxnet3' or 'e1000' but found '%s'"), virtualDev_name,
                      virtualDev);
M
Matthias Bolte 已提交
2407
            goto cleanup;
2408 2409 2410 2411 2412 2413 2414 2415 2416
        }

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

            virtualDev = strdup("vmxnet2");

            if (virtualDev == NULL) {
                virReportOOMError();
M
Matthias Bolte 已提交
2417
                goto cleanup;
2418 2419
            }
        }
2420 2421
    }

M
Matthias Bolte 已提交
2422
    /* vmx:networkName -> def:data.bridge.brname */
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
    if (connectionType == NULL ||
        STRCASEEQ(connectionType, "bridged") ||
        STRCASEEQ(connectionType, "custom")) {
        if (virVMXGetConfigString(conf, networkName_name, &networkName,
                                  true) < 0)
            goto cleanup;

        if (networkName == NULL) {
            networkName = strdup("");
            if (networkName == NULL) {
                virReportOOMError();
                goto cleanup;
            }
        }
M
Matthias Bolte 已提交
2437 2438 2439
    }

    /* vmx:vnet -> def:data.ifname */
2440
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
2441
        virVMXGetConfigString(conf, vnet_name, &vnet, false) < 0) {
M
Matthias Bolte 已提交
2442
        goto cleanup;
2443 2444 2445 2446 2447 2448
    }

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

        virtualDev = NULL;
M
Matthias Bolte 已提交
2452
        networkName = NULL;
2453 2454
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
2455
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2456
                  _("No yet handled value '%s' for VMX entry '%s'"),
2457
                  connectionType, connectionType_name);
M
Matthias Bolte 已提交
2458
        goto cleanup;
2459
    } else if (STRCASEEQ(connectionType, "nat")) {
2460 2461 2462 2463
        (*def)->type = VIR_DOMAIN_NET_TYPE_USER;
        (*def)->model = virtualDev;

        virtualDev = NULL;
2464 2465 2466
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
2467 2468
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
2469 2470

        virtualDev = NULL;
M
Matthias Bolte 已提交
2471
        networkName = NULL;
2472 2473
        vnet = NULL;
    } else {
2474
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2475
                  _("Invalid value '%s' for VMX entry '%s'"), connectionType,
2476
                  connectionType_name);
M
Matthias Bolte 已提交
2477
        goto cleanup;
2478 2479
    }

M
Matthias Bolte 已提交
2480 2481
    result = 0;

M
Matthias Bolte 已提交
2482
  cleanup:
M
Matthias Bolte 已提交
2483 2484 2485 2486 2487
    if (result < 0) {
        virDomainNetDefFree(*def);
        *def = NULL;
    }

2488 2489 2490 2491 2492 2493 2494 2495 2496
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;

M
Matthias Bolte 已提交
2497
  ignore:
2498 2499 2500
    virDomainNetDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2501 2502
    result = 0;

2503 2504 2505 2506 2507 2508
    goto cleanup;
}



int
2509 2510
virVMXParseSerial(virVMXContext *ctx, virConfPtr conf, int port,
                  virDomainChrDefPtr *def)
2511
{
M
Matthias Bolte 已提交
2512
    int result = -1;
2513 2514 2515
    char prefix[48] = "";

    char present_name[48] = "";
2516
    bool present = false;
2517 2518

    char startConnected_name[48] = "";
2519
    bool startConnected = false;
2520 2521 2522 2523 2524 2525 2526

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

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

2527 2528 2529
    char network_endPoint_name[48] = "";
    char *network_endPoint = NULL;

M
Martin Kletzander 已提交
2530
    virURIPtr parsedUri = NULL;
2531

2532
    if (def == NULL || *def != NULL) {
2533
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2534 2535 2536 2537
        return -1;
    }

    if (port < 0 || port > 3) {
2538
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2539
                  _("Serial port index %d out of [0..3] range"), port);
2540
        return -1;
2541 2542 2543
    }

    if (VIR_ALLOC(*def) < 0) {
2544
        virReportOOMError();
M
Matthias Bolte 已提交
2545
        return -1;
2546 2547
    }

2548
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
2549

2550
    snprintf(prefix, sizeof(prefix), "serial%d", port);
2551

2552 2553 2554 2555 2556
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
    VMX_BUILD_NAME_EXTRA(network_endPoint, "network.endPoint");
2557 2558

    /* vmx:present */
2559
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2560
        goto cleanup;
2561 2562 2563
    }

    /* vmx:startConnected */
2564 2565
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2566
        goto cleanup;
2567 2568 2569 2570 2571 2572 2573 2574
    }

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

    /* vmx:fileType -> def:type */
2575
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2576
        goto cleanup;
2577 2578 2579
    }

    /* vmx:fileName -> def:data.file.path */
2580
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2581
        goto cleanup;
2582 2583
    }

2584
    /* vmx:network.endPoint -> def:data.tcp.listen */
2585 2586
    if (virVMXGetConfigString(conf, network_endPoint_name, &network_endPoint,
                              true) < 0) {
2587 2588 2589
        goto cleanup;
    }

2590 2591
    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2592
        (*def)->target.port = port;
2593 2594
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2595 2596 2597

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2598
        (*def)->target.port = port;
2599 2600 2601
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2602

2603
        if ((*def)->source.data.file.path == NULL) {
M
Matthias Bolte 已提交
2604
            goto cleanup;
M
Matthias Bolte 已提交
2605
        }
2606
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
2607 2608 2609 2610
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
2611
        (*def)->target.port = port;
2612 2613
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->source.data.file.path = fileName;
M
Matthias Bolte 已提交
2614 2615

        fileName = NULL;
2616 2617
    } else if (STRCASEEQ(fileType, "network")) {
        (*def)->target.port = port;
2618
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_TCP;
2619

2620
        if (!(parsedUri = virURIParse(fileName)))
2621 2622 2623
            goto cleanup;

        if (parsedUri->port == 0) {
2624
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2625 2626 2627 2628 2629
                      _("VMX entry '%s' doesn't contain a port part"),
                      fileName_name);
            goto cleanup;
        }

2630
        (*def)->source.data.tcp.host = strdup(parsedUri->server);
2631

2632
        if ((*def)->source.data.tcp.host == NULL) {
2633 2634 2635 2636
            virReportOOMError();
            goto cleanup;
        }

2637 2638
        if (virAsprintf(&(*def)->source.data.tcp.service, "%d",
                        parsedUri->port) < 0) {
2639 2640 2641 2642 2643 2644 2645 2646 2647
            virReportOOMError();
            goto cleanup;
        }

        /* See vSphere API documentation about VirtualSerialPortURIBackingInfo */
        if (parsedUri->scheme == NULL ||
            STRCASEEQ(parsedUri->scheme, "tcp") ||
            STRCASEEQ(parsedUri->scheme, "tcp4") ||
            STRCASEEQ(parsedUri->scheme, "tcp6")) {
2648
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW;
2649
        } else if (STRCASEEQ(parsedUri->scheme, "telnet")) {
2650 2651
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
2652
        } else if (STRCASEEQ(parsedUri->scheme, "telnets")) {
2653 2654
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNETS;
2655 2656 2657 2658
        } else if (STRCASEEQ(parsedUri->scheme, "ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp4+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp6+ssl")) {
2659
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TLS;
2660
        } else {
2661
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2662 2663 2664 2665 2666 2667
                      _("VMX entry '%s' contains unsupported scheme '%s'"),
                      fileName_name, parsedUri->scheme);
            goto cleanup;
        }

        if (network_endPoint == NULL || STRCASEEQ(network_endPoint, "server")) {
2668
            (*def)->source.data.tcp.listen = true;
2669
        } else if (STRCASEEQ(network_endPoint, "client")) {
2670
            (*def)->source.data.tcp.listen = false;
2671
        } else {
2672
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2673 2674 2675 2676
                      _("Expecting VMX entry '%s' to be 'server' or 'client' "
                        "but found '%s'"), network_endPoint_name, network_endPoint);
            goto cleanup;
        }
2677
    } else {
2678
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2679
                  _("Expecting VMX entry '%s' to be 'device', 'file' or 'pipe' "
2680
                    "or 'network' but found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2681
        goto cleanup;
2682 2683
    }

M
Matthias Bolte 已提交
2684 2685
    result = 0;

M
Matthias Bolte 已提交
2686
  cleanup:
M
Matthias Bolte 已提交
2687 2688 2689 2690 2691
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2692 2693
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2694
    VIR_FREE(network_endPoint);
2695
    virURIFree(parsedUri);
2696 2697 2698

    return result;

M
Matthias Bolte 已提交
2699
  ignore:
2700 2701 2702
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2703 2704
    result = 0;

2705 2706 2707 2708 2709 2710
    goto cleanup;
}



int
2711 2712
virVMXParseParallel(virVMXContext *ctx, virConfPtr conf, int port,
                    virDomainChrDefPtr *def)
2713
{
M
Matthias Bolte 已提交
2714
    int result = -1;
2715 2716 2717
    char prefix[48] = "";

    char present_name[48] = "";
2718
    bool present = false;
2719 2720

    char startConnected_name[48] = "";
2721
    bool startConnected = false;
2722 2723 2724 2725 2726 2727 2728 2729

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

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

    if (def == NULL || *def != NULL) {
2730
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2731 2732 2733 2734
        return -1;
    }

    if (port < 0 || port > 2) {
2735
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2736
                  _("Parallel port index %d out of [0..2] range"), port);
2737
        return -1;
2738 2739 2740
    }

    if (VIR_ALLOC(*def) < 0) {
2741
        virReportOOMError();
M
Matthias Bolte 已提交
2742
        return -1;
2743 2744
    }

2745
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
2746

2747
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2748

2749 2750 2751 2752
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
2753 2754

    /* vmx:present */
2755
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2756
        goto cleanup;
2757 2758 2759
    }

    /* vmx:startConnected */
2760 2761
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2762
        goto cleanup;
2763 2764 2765 2766 2767 2768 2769 2770
    }

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

    /* vmx:fileType -> def:type */
2771
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2772
        goto cleanup;
2773 2774 2775
    }

    /* vmx:fileName -> def:data.file.path */
2776
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2777
        goto cleanup;
2778 2779 2780 2781
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2782
        (*def)->target.port = port;
2783 2784
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2785 2786 2787

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2788
        (*def)->target.port = port;
2789 2790 2791
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2792

2793
        if ((*def)->source.data.file.path == NULL) {
M
Matthias Bolte 已提交
2794
            goto cleanup;
M
Matthias Bolte 已提交
2795
        }
2796
    } else {
2797
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2798 2799
                  _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                    "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2800
        goto cleanup;
2801 2802
    }

M
Matthias Bolte 已提交
2803 2804
    result = 0;

M
Matthias Bolte 已提交
2805
  cleanup:
M
Matthias Bolte 已提交
2806 2807 2808 2809 2810
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2811 2812 2813 2814 2815
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2816
  ignore:
2817 2818 2819
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2820 2821
    result = 0;

2822 2823
    goto cleanup;
}
M
Matthias Bolte 已提交
2824 2825 2826



2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
int
virVMXParseSVGA(virConfPtr conf, virDomainVideoDefPtr *def)
{
    int result = -1;
    long long svga_vramSize = 0;

    if (def == NULL || *def != NULL) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
        return -1;
    }

    if (VIR_ALLOC(*def) < 0) {
        virReportOOMError();
        return -1;
    }

    (*def)->type = VIR_DOMAIN_VIDEO_TYPE_VMVGA;

    /* vmx:vramSize */
    if (virVMXGetConfigLong(conf, "svga.vramSize", &svga_vramSize,
                            4 * 1024 * 1024, true) < 0) {
        goto cleanup;
    }

2851
    (*def)->vram = VIR_DIV_UP(svga_vramSize, 1024); /* Scale from bytes to kilobytes */
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865

    result = 0;

  cleanup:
    if (result < 0) {
        virDomainVideoDefFree(*def);
        *def = NULL;
    }

    return result;
}



M
Matthias Bolte 已提交
2866 2867 2868 2869 2870
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Domain XML -> VMX
 */

char *
2871 2872
virVMXFormatConfig(virVMXContext *ctx, virCapsPtr caps, virDomainDefPtr def,
                   int virtualHW_version)
M
Matthias Bolte 已提交
2873
{
2874
    char *vmx = NULL;
M
Matthias Bolte 已提交
2875 2876 2877 2878
    int i;
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
2879 2880
    char *preliminaryDisplayName = NULL;
    char *displayName = NULL;
2881
    char *annotation = NULL;
2882
    unsigned long long max_balloon;
2883 2884
    bool scsi_present[4] = { false, false, false, false };
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
2885
    bool floppy_present[2] = { false, false };
M
Matthias Bolte 已提交
2886

2887
    if (ctx->formatFileName == NULL) {
2888 2889
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                  _("virVMXContext has no formatFileName function set"));
2890 2891 2892
        return NULL;
    }

M
Matthias Bolte 已提交
2893 2894
    memset(zero, 0, VIR_UUID_BUFLEN);

2895 2896
    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) {
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2897
                  _("Expecting virt type to be '%s' but found '%s'"),
M
Matthias Bolte 已提交
2898 2899 2900 2901 2902
                  virDomainVirtTypeToString(VIR_DOMAIN_VIRT_VMWARE),
                  virDomainVirtTypeToString(def->virtType));
        return NULL;
    }

2903 2904 2905
    /* vmx:.encoding */
    virBufferAddLit(&buffer, ".encoding = \"UTF-8\"\n");

M
Matthias Bolte 已提交
2906 2907 2908 2909
    /* vmx:config.version */
    virBufferAddLit(&buffer, "config.version = \"8\"\n");

    /* vmx:virtualHW.version */
2910
    virBufferAsprintf(&buffer, "virtualHW.version = \"%d\"\n",
2911
                      virtualHW_version);
M
Matthias Bolte 已提交
2912

M
Matthias Bolte 已提交
2913
    /* def:os.arch -> vmx:guestOS */
M
Matthias Bolte 已提交
2914 2915 2916 2917 2918
    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 {
2919
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
2920 2921
                  _("Expecting domain XML attribute 'arch' of entry 'os/type' "
                    "to be 'i686' or 'x86_64' but found '%s'"), def->os.arch);
2922
        goto cleanup;
M
Matthias Bolte 已提交
2923 2924
    }

M
Matthias Bolte 已提交
2925 2926 2927 2928 2929 2930 2931
    /* def:os.smbios_mode -> vmx:smbios.reflecthost */
    if (def->os.smbios_mode == VIR_DOMAIN_SMBIOS_NONE ||
        def->os.smbios_mode == VIR_DOMAIN_SMBIOS_EMULATE) {
        /* nothing */
    } else if (def->os.smbios_mode == VIR_DOMAIN_SMBIOS_HOST) {
        virBufferAddLit(&buffer, "smbios.reflecthost = \"true\"\n");
    } else {
2932
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
M
Matthias Bolte 已提交
2933 2934 2935 2936 2937
                  _("Unsupported SMBIOS mode '%s'"),
                  virDomainSmbiosModeTypeToString(def->os.smbios_mode));
        goto cleanup;
    }

M
Matthias Bolte 已提交
2938 2939 2940 2941
    /* def:uuid -> vmx:uuid.action, vmx:uuid.bios */
    if (memcmp(def->uuid, zero, VIR_UUID_BUFLEN) == 0) {
        virBufferAddLit(&buffer, "uuid.action = \"create\"\n");
    } else {
2942
        virBufferAsprintf(&buffer, "uuid.bios = \"%02x %02x %02x %02x %02x %02x "
M
Matthias Bolte 已提交
2943 2944 2945 2946 2947 2948 2949 2950 2951
                          "%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 */
2952
    preliminaryDisplayName = virVMXEscapeHexPipe(def->name);
2953

2954 2955 2956
    if (preliminaryDisplayName == NULL) {
        goto cleanup;
    }
2957

2958
    displayName = virVMXEscapeHexPercent(preliminaryDisplayName);
2959

2960 2961 2962
    if (displayName == NULL) {
        goto cleanup;
    }
2963

2964
    virBufferAsprintf(&buffer, "displayName = \"%s\"\n", displayName);
2965

2966 2967
    /* def:description -> vmx:annotation */
    if (def->description != NULL) {
2968
        annotation = virVMXEscapeHexPipe(def->description);
2969

2970
        virBufferAsprintf(&buffer, "annotation = \"%s\"\n", annotation);
2971 2972
    }

2973
    /* def:mem.max_balloon -> vmx:memsize */
2974 2975
    /* max-memory must be a multiple of 4096 kilobyte */
    max_balloon = VIR_DIV_UP(def->mem.max_balloon, 4096) * 4096;
M
Matthias Bolte 已提交
2976

2977
    virBufferAsprintf(&buffer, "memsize = \"%llu\"\n",
2978
                      max_balloon / 1024); /* Scale from kilobytes to megabytes */
M
Matthias Bolte 已提交
2979

2980
    /* def:mem.cur_balloon -> vmx:sched.mem.max */
2981
    if (def->mem.cur_balloon < max_balloon) {
2982
        virBufferAsprintf(&buffer, "sched.mem.max = \"%llu\"\n",
2983 2984
                          VIR_DIV_UP(def->mem.cur_balloon,
                                     1024)); /* Scale from kilobytes to megabytes */
M
Matthias Bolte 已提交
2985 2986
    }

2987 2988
    /* def:mem.min_guarantee -> vmx:sched.mem.minsize */
    if (def->mem.min_guarantee > 0) {
2989
        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%llu\"\n",
2990 2991
                          VIR_DIV_UP(def->mem.min_guarantee,
                                     1024)); /* Scale from kilobytes to megabytes */
2992 2993
    }

E
Eric Blake 已提交
2994 2995
    /* def:maxvcpus -> vmx:numvcpus */
    if (def->vcpus != def->maxvcpus) {
2996
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
E
Eric Blake 已提交
2997 2998 2999 3000 3001
                  _("No support for domain XML entry 'vcpu' attribute "
                    "'current'"));
        goto cleanup;
    }
    if (def->maxvcpus <= 0 || (def->maxvcpus % 2 != 0 && def->maxvcpus != 1)) {
3002
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3003 3004
                  _("Expecting domain XML entry 'vcpu' to be an unsigned "
                    "integer (1 or a multiple of 2) but found %d"),
E
Eric Blake 已提交
3005
                  def->maxvcpus);
3006
        goto cleanup;
M
Matthias Bolte 已提交
3007 3008
    }

3009
    virBufferAsprintf(&buffer, "numvcpus = \"%d\"\n", def->maxvcpus);
M
Matthias Bolte 已提交
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022

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

E
Eric Blake 已提交
3023
        if (sched_cpu_affinity_length < def->maxvcpus) {
3024
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3025
                      _("Expecting domain XML attribute 'cpuset' of entry "
E
Eric Blake 已提交
3026 3027
                        "'vcpu' to contain at least %d CPU(s)"),
                      def->maxvcpus);
3028
            goto cleanup;
M
Matthias Bolte 已提交
3029 3030 3031 3032
        }

        for (i = 0; i < def->cpumasklen; ++i) {
            if (def->cpumask[i]) {
3033
                virBufferAsprintf(&buffer, "%d", i);
M
Matthias Bolte 已提交
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045

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

                --sched_cpu_affinity_length;
            }
        }

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

3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
    /* def:cputune.shares -> vmx:sched.cpu.shares */
    if (def->cputune.shares > 0) {
        /* See http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.SharesInfo.Level.html */
        if (def->cputune.shares == def->vcpus * 500) {
            virBufferAddLit(&buffer, "sched.cpu.shares = \"low\"\n");
        } else if (def->cputune.shares == def->vcpus * 1000) {
            virBufferAddLit(&buffer, "sched.cpu.shares = \"normal\"\n");
        } else if (def->cputune.shares == def->vcpus * 2000) {
            virBufferAddLit(&buffer, "sched.cpu.shares = \"high\"\n");
        } else {
3056
            virBufferAsprintf(&buffer, "sched.cpu.shares = \"%lu\"\n",
3057 3058 3059 3060
                              def->cputune.shares);
        }
    }

M
Matthias Bolte 已提交
3061 3062 3063 3064
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
3065
            if (virVMXFormatVNC(def->graphics[i], &buffer) < 0) {
3066
                goto cleanup;
M
Matthias Bolte 已提交
3067 3068 3069 3070 3071
            }

            break;

          default:
3072
            VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3073
                      _("Unsupported graphics type '%s'"),
M
Matthias Bolte 已提交
3074
                      virDomainGraphicsTypeToString(def->graphics[i]->type));
3075
            goto cleanup;
M
Matthias Bolte 已提交
3076 3077 3078
        }
    }

M
Matthias Bolte 已提交
3079
    /* def:disks */
3080
    for (i = 0; i < def->ndisks; ++i) {
3081 3082
        if (virVMXVerifyDiskAddress(caps, def->disks[i]) < 0 ||
            virVMXHandleLegacySCSIDiskDriverName(def, def->disks[i]) < 0) {
3083
            goto cleanup;
3084 3085
        }
    }
M
Matthias Bolte 已提交
3086

3087 3088
    if (virVMXGatherSCSIControllers(ctx, def, scsi_virtualDev,
                                    scsi_present) < 0) {
3089
        goto cleanup;
M
Matthias Bolte 已提交
3090 3091 3092 3093
    }

    for (i = 0; i < 4; ++i) {
        if (scsi_present[i]) {
3094
            virBufferAsprintf(&buffer, "scsi%d.present = \"true\"\n", i);
M
Matthias Bolte 已提交
3095

3096
            if (scsi_virtualDev[i] != -1) {
3097
                virBufferAsprintf(&buffer, "scsi%d.virtualDev = \"%s\"\n", i,
3098
                                  virVMXControllerModelSCSITypeToString
3099
                                    (scsi_virtualDev[i]));
M
Matthias Bolte 已提交
3100 3101 3102 3103 3104 3105 3106
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
3107
            if (virVMXFormatHardDisk(ctx, def->disks[i], &buffer) < 0) {
3108
                goto cleanup;
M
Matthias Bolte 已提交
3109 3110 3111 3112 3113
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_CDROM:
3114
            if (virVMXFormatCDROM(ctx, def->disks[i], &buffer) < 0) {
3115
                goto cleanup;
M
Matthias Bolte 已提交
3116 3117 3118 3119 3120
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
3121 3122
            if (virVMXFormatFloppy(ctx, def->disks[i], &buffer,
                                   floppy_present) < 0) {
3123
                goto cleanup;
M
Matthias Bolte 已提交
3124 3125 3126 3127 3128
            }

            break;

          default:
3129
            VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3130
                      _("Unsupported disk device type '%s'"),
M
Matthias Bolte 已提交
3131
                      virDomainDiskDeviceTypeToString(def->disks[i]->device));
3132
            goto cleanup;
M
Matthias Bolte 已提交
3133 3134 3135
        }
    }

3136 3137 3138
    for (i = 0; i < 2; ++i) {
        /* floppy[0..1].present defaults to true, disable it explicitly */
        if (! floppy_present[i]) {
3139
            virBufferAsprintf(&buffer, "floppy%d.present = \"false\"\n", i);
3140 3141 3142
        }
    }

M
Matthias Bolte 已提交
3143 3144 3145 3146 3147
    /* def:fss */
    /* FIXME */

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
3148
        if (virVMXFormatEthernet(def->nets[i], i, &buffer) < 0) {
3149
            goto cleanup;
M
Matthias Bolte 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158
        }
    }

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

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

3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171
    /* def:videos */
    if (def->nvideos > 0) {
        if (def->nvideos > 1) {
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s",
                      _("No support for multiple video devices"));
            goto cleanup;
        }

        if (virVMXFormatSVGA(def->videos[0], &buffer) < 0) {
            goto cleanup;
        }
    }

M
Matthias Bolte 已提交
3172 3173 3174 3175 3176
    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
3177
        if (virVMXFormatSerial(ctx, def->serials[i], &buffer) < 0) {
3178
            goto cleanup;
M
Matthias Bolte 已提交
3179 3180 3181 3182 3183
        }
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
3184
        if (virVMXFormatParallel(ctx, def->parallels[i], &buffer) < 0) {
3185
            goto cleanup;
M
Matthias Bolte 已提交
3186 3187 3188 3189 3190
        }
    }

    /* Get final VMX output */
    if (virBufferError(&buffer)) {
3191
        virReportOOMError();
3192
        goto cleanup;
M
Matthias Bolte 已提交
3193 3194
    }

3195
    vmx = virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
3196

3197 3198 3199 3200 3201
  cleanup:
    if (vmx == NULL) {
        virBufferFreeAndReset(&buffer);
    }

3202 3203
    VIR_FREE(preliminaryDisplayName);
    VIR_FREE(displayName);
3204
    VIR_FREE(annotation);
M
Matthias Bolte 已提交
3205

3206
    return vmx;
M
Matthias Bolte 已提交
3207 3208 3209 3210
}



M
Matthias Bolte 已提交
3211
int
3212
virVMXFormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
3213
{
3214 3215
    const char *listenAddr;

M
Matthias Bolte 已提交
3216
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
3217
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3218 3219 3220
        return -1;
    }

3221
    virBufferAsprintf(buffer, "RemoteDisplay.vnc.enabled = \"true\"\n");
M
Matthias Bolte 已提交
3222 3223

    if (def->data.vnc.autoport) {
3224
        VIR_WARN("VNC autoport is enabled, but the automatically assigned "
M
Matthias Bolte 已提交
3225 3226 3227 3228 3229 3230 3231
                  "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);
        }

3232
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.port = \"%d\"\n",
M
Matthias Bolte 已提交
3233 3234 3235
                          def->data.vnc.port);
    }

3236
    if ((listenAddr = virDomainGraphicsListenGetAddress(def, 0))) {
3237
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.ip = \"%s\"\n",
3238
                          listenAddr);
M
Matthias Bolte 已提交
3239 3240 3241
    }

    if (def->data.vnc.keymap != NULL) {
3242
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.keymap = \"%s\"\n",
M
Matthias Bolte 已提交
3243 3244 3245
                          def->data.vnc.keymap);
    }

3246
    if (def->data.vnc.auth.passwd != NULL) {
3247
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.password = \"%s\"\n",
3248
                          def->data.vnc.auth.passwd);
M
Matthias Bolte 已提交
3249 3250 3251 3252 3253 3254 3255
    }

    return 0;
}



M
Matthias Bolte 已提交
3256
int
3257 3258
virVMXFormatHardDisk(virVMXContext *ctx, virDomainDiskDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3259
{
3260
    int controllerOrBus, unit;
M
Matthias Bolte 已提交
3261 3262 3263
    const char *busName = NULL;
    const char *entryPrefix = NULL;
    const char *deviceTypePrefix = NULL;
M
Matthias Bolte 已提交
3264
    char *fileName = NULL;
M
Matthias Bolte 已提交
3265 3266

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
3267
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3268 3269 3270 3271 3272 3273 3274 3275
        return -1;
    }

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

3276 3277
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3278 3279 3280 3281 3282 3283 3284
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";
        deviceTypePrefix = "ata";

3285
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
3286
                                           &unit) < 0) {
M
Matthias Bolte 已提交
3287 3288 3289
            return -1;
        }
    } else {
3290
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3291
                  _("Unsupported bus type '%s' for harddisk"),
M
Matthias Bolte 已提交
3292 3293 3294 3295 3296
                  virDomainDiskBusTypeToString(def->bus));
        return -1;
    }

    if (def->type != VIR_DOMAIN_DISK_TYPE_FILE) {
3297
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3298
                  _("%s harddisk '%s' has unsupported type '%s', expecting '%s'"),
M
Matthias Bolte 已提交
3299 3300 3301 3302 3303
                  busName, def->dst, virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE));
        return -1;
    }

3304
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3305
                      entryPrefix, controllerOrBus, unit);
3306
    virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"%s-hardDisk\"\n",
3307
                      entryPrefix, controllerOrBus, unit, deviceTypePrefix);
M
Matthias Bolte 已提交
3308 3309

    if (def->src != NULL) {
3310
        if (! virFileHasSuffix(def->src, ".vmdk")) {
3311
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3312 3313
                      _("Image file for %s harddisk '%s' has unsupported suffix, "
                        "expecting '.vmdk'"), busName, def->dst);
M
Matthias Bolte 已提交
3314 3315 3316
            return -1;
        }

3317
        fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3318 3319 3320 3321 3322

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

3323
        virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3324
                          entryPrefix, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3325 3326

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3327 3328 3329 3330
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
3331
            virBufferAsprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
3332
                              entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3333
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
3334
            VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3335
                      _("%s harddisk '%s' has unsupported cache mode '%s'"),
M
Matthias Bolte 已提交
3336 3337
                      busName, def->dst,
                      virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
3338
            return -1;
M
Matthias Bolte 已提交
3339 3340 3341 3342 3343 3344 3345 3346 3347
        }
    }

    return 0;
}



int
3348 3349
virVMXFormatCDROM(virVMXContext *ctx, virDomainDiskDefPtr def,
                  virBufferPtr buffer)
M
Matthias Bolte 已提交
3350
{
3351
    int controllerOrBus, unit;
M
Matthias Bolte 已提交
3352 3353
    const char *busName = NULL;
    const char *entryPrefix = NULL;
M
Matthias Bolte 已提交
3354
    char *fileName = NULL;
M
Matthias Bolte 已提交
3355 3356

    if (def->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
3357
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3358 3359 3360 3361 3362 3363 3364
        return -1;
    }

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

3365 3366
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3367 3368 3369 3370 3371 3372
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";

3373 3374
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
                                          &unit) < 0) {
M
Matthias Bolte 已提交
3375 3376 3377
            return -1;
        }
    } else {
3378
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3379
                  _("Unsupported bus type '%s' for cdrom"),
M
Matthias Bolte 已提交
3380 3381 3382 3383
                  virDomainDiskBusTypeToString(def->bus));
        return -1;
    }

3384
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3385
                      entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3386 3387

    if (def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
3388
        virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"cdrom-image\"\n",
3389
                          entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3390 3391

        if (def->src != NULL) {
3392
            if (! virFileHasSuffix(def->src, ".iso")) {
3393
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3394 3395
                          _("Image file for %s cdrom '%s' has unsupported "
                            "suffix, expecting '.iso'"), busName, def->dst);
M
Matthias Bolte 已提交
3396 3397 3398
                return -1;
            }

3399
            fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3400 3401 3402 3403 3404

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

3405
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3406
                              entryPrefix, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3407 3408 3409

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
3410
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
3411
        virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"atapi-cdrom\"\n",
3412
                          entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3413 3414

        if (def->src != NULL) {
3415
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3416
                              entryPrefix, controllerOrBus, unit, def->src);
M
Matthias Bolte 已提交
3417
        }
M
Matthias Bolte 已提交
3418
    } else {
3419
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3420 3421
                  _("%s cdrom '%s' has unsupported type '%s', expecting '%s' "
                    "or '%s'"), busName, def->dst,
M
Matthias Bolte 已提交
3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
                  virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }

    return 0;
}



int
3434 3435
virVMXFormatFloppy(virVMXContext *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer, bool floppy_present[2])
M
Matthias Bolte 已提交
3436
{
3437
    int unit;
M
Matthias Bolte 已提交
3438
    char *fileName = NULL;
M
Matthias Bolte 已提交
3439 3440

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
3441
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3442 3443 3444
        return -1;
    }

3445
    if (virVMXFloppyDiskNameToUnit(def->dst, &unit) < 0) {
M
Matthias Bolte 已提交
3446 3447 3448
        return -1;
    }

3449 3450
    floppy_present[unit] = true;

3451
    virBufferAsprintf(buffer, "floppy%d.present = \"true\"\n", unit);
M
Matthias Bolte 已提交
3452 3453

    if (def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
3454
        virBufferAsprintf(buffer, "floppy%d.fileType = \"file\"\n", unit);
M
Matthias Bolte 已提交
3455 3456

        if (def->src != NULL) {
3457
            if (! virFileHasSuffix(def->src, ".flp")) {
3458
                VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3459 3460
                          _("Image file for floppy '%s' has unsupported "
                            "suffix, expecting '.flp'"), def->dst);
M
Matthias Bolte 已提交
3461 3462 3463
                return -1;
            }

3464
            fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3465 3466 3467 3468 3469

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

3470
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3471
                              unit, fileName);
M
Matthias Bolte 已提交
3472 3473 3474

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
3475
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
3476
        virBufferAsprintf(buffer, "floppy%d.fileType = \"device\"\n", unit);
M
Matthias Bolte 已提交
3477 3478

        if (def->src != NULL) {
3479
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3480
                              unit, def->src);
M
Matthias Bolte 已提交
3481
        }
M
Matthias Bolte 已提交
3482
    } else {
3483
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3484 3485
                  _("Floppy '%s' has unsupported type '%s', expecting '%s' "
                    "or '%s'"), def->dst,
M
Matthias Bolte 已提交
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
                  virDomainDiskTypeToString(def->type),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                  virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }

    return 0;
}



int
3498 3499
virVMXFormatEthernet(virDomainNetDefPtr def, int controller,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3500 3501
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
3502
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
3503 3504

    if (controller < 0 || controller > 3) {
3505
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3506
                  _("Ethernet controller index %d out of [0..3] range"),
M
Matthias Bolte 已提交
3507 3508 3509 3510
                  controller);
        return -1;
    }

3511
    virBufferAsprintf(buffer, "ethernet%d.present = \"true\"\n", controller);
M
Matthias Bolte 已提交
3512

3513
    /* def:model -> vmx:virtualDev, vmx:features */
M
Matthias Bolte 已提交
3514 3515 3516
    if (def->model != NULL) {
        if (STRCASENEQ(def->model, "vlance") &&
            STRCASENEQ(def->model, "vmxnet") &&
3517
            STRCASENEQ(def->model, "vmxnet2") &&
3518
            STRCASENEQ(def->model, "vmxnet3") &&
M
Matthias Bolte 已提交
3519
            STRCASENEQ(def->model, "e1000")) {
3520
            VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
3521
                      _("Expecting domain XML entry 'devices/interface/model' "
3522 3523
                        "to be 'vlance' or 'vmxnet' or 'vmxnet2' or 'vmxnet3' "
                        "or 'e1000' but found '%s'"), def->model);
M
Matthias Bolte 已提交
3524 3525 3526
            return -1;
        }

3527
        if (STRCASEEQ(def->model, "vmxnet2")) {
3528
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"vmxnet\"\n",
3529
                              controller);
3530
            virBufferAsprintf(buffer, "ethernet%d.features = \"15\"\n",
3531 3532
                              controller);
        } else {
3533
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
3534 3535
                              controller, def->model);
        }
M
Matthias Bolte 已提交
3536 3537 3538 3539 3540
    }

    /* def:type, def:ifname -> vmx:connectionType */
    switch (def->type) {
      case VIR_DOMAIN_NET_TYPE_BRIDGE:
3541 3542 3543
        if (STRNEQ(def->data.bridge.brname, ""))
            virBufferAsprintf(buffer, "ethernet%d.networkName = \"%s\"\n",
                              controller, def->data.bridge.brname);
M
Matthias Bolte 已提交
3544 3545

        if (def->ifname != NULL) {
3546
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"custom\"\n",
M
Matthias Bolte 已提交
3547
                              controller);
3548
            virBufferAsprintf(buffer, "ethernet%d.vnet = \"%s\"\n",
M
Matthias Bolte 已提交
3549 3550
                              controller, def->ifname);
        } else {
3551
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"bridged\"\n",
M
Matthias Bolte 已提交
3552 3553 3554 3555 3556
                              controller);
        }

        break;

3557 3558 3559 3560 3561
      case VIR_DOMAIN_NET_TYPE_USER:
        virBufferAsprintf(buffer, "ethernet%d.connectionType = \"nat\"\n",
                          controller);
        break;

M
Matthias Bolte 已提交
3562
      default:
3563
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported net type '%s'"),
M
Matthias Bolte 已提交
3564 3565 3566 3567
                  virDomainNetTypeToString(def->type));
        return -1;
    }

3568
    /* def:mac -> vmx:addressType, vmx:(generated)Address, vmx:checkMACAddress */
3569
    virMacAddrFormat(def->mac, mac_string);
M
Matthias Bolte 已提交
3570

3571 3572 3573 3574
    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) {
3575
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
M
Matthias Bolte 已提交
3576
                          controller);
3577
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
M
Matthias Bolte 已提交
3578
                          controller, mac_string);
3579
        virBufferAsprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
M
Matthias Bolte 已提交
3580
                          controller);
3581
    } else if (prefix == 0x005056 && suffix <= 0x3fffff) {
3582
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3583
                          controller);
3584
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3585 3586
                          controller, mac_string);
    } else if (prefix == 0x005056 && suffix >= 0x800000 && suffix <= 0xbfffff) {
3587
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"vpx\"\n",
3588
                          controller);
3589
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
3590
                          controller, mac_string);
M
Matthias Bolte 已提交
3591
    } else {
3592
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3593
                          controller);
3594
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3595
                          controller, mac_string);
3596
        virBufferAsprintf(buffer, "ethernet%d.checkMACAddress = \"false\"\n",
3597
                          controller);
M
Matthias Bolte 已提交
3598 3599 3600 3601 3602 3603 3604 3605
    }

    return 0;
}



int
3606 3607
virVMXFormatSerial(virVMXContext *ctx, virDomainChrDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
3608
{
M
Matthias Bolte 已提交
3609
    char *fileName = NULL;
3610
    const char *protocol;
M
Matthias Bolte 已提交
3611

3612
    if (def->target.port < 0 || def->target.port > 3) {
3613
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3614 3615
                  _("Serial port index %d out of [0..3] range"),
                  def->target.port);
M
Matthias Bolte 已提交
3616 3617 3618
        return -1;
    }

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

M
Matthias Bolte 已提交
3621
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3622
    switch (def->source.type) {
M
Matthias Bolte 已提交
3623
      case VIR_DOMAIN_CHR_TYPE_DEV:
3624
        virBufferAsprintf(buffer, "serial%d.fileType = \"device\"\n",
3625
                          def->target.port);
3626
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3627
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3628 3629 3630
        break;

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

3634
        fileName = ctx->formatFileName(def->source.data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3635 3636 3637 3638 3639

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

3640
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3641
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3642 3643

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3644 3645 3646
        break;

      case VIR_DOMAIN_CHR_TYPE_PIPE:
3647
        virBufferAsprintf(buffer, "serial%d.fileType = \"pipe\"\n",
3648
                          def->target.port);
M
Matthias Bolte 已提交
3649
        /* FIXME: Based on VI Client GUI default */
3650
        virBufferAsprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
3651
                          def->target.port);
M
Matthias Bolte 已提交
3652
        /* FIXME: Based on VI Client GUI default */
3653
        virBufferAsprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
3654
                          def->target.port);
3655
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3656
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3657 3658
        break;

3659
      case VIR_DOMAIN_CHR_TYPE_TCP:
3660
        switch (def->source.data.tcp.protocol) {
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
          case VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW:
            protocol = "tcp";
            break;

          case VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET:
            protocol = "telnet";
            break;

          case VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNETS:
            protocol = "telnets";
            break;

          case VIR_DOMAIN_CHR_TCP_PROTOCOL_TLS:
            protocol = "ssl";
            break;

          default:
3678
            VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3679
                      _("Unsupported character device TCP protocol '%s'"),
3680 3681
                      virDomainChrTcpProtocolTypeToString(
                          def->source.data.tcp.protocol));
3682 3683 3684
            return -1;
        }

3685
        virBufferAsprintf(buffer, "serial%d.fileType = \"network\"\n",
3686
                          def->target.port);
3687
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s://%s:%s\"\n",
3688 3689
                          def->target.port, protocol, def->source.data.tcp.host,
                          def->source.data.tcp.service);
3690
        virBufferAsprintf(buffer, "serial%d.network.endPoint = \"%s\"\n",
3691
                          def->target.port,
3692
                          def->source.data.tcp.listen ? "server" : "client");
3693 3694
        break;

M
Matthias Bolte 已提交
3695
      default:
3696
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3697
                  _("Unsupported character device type '%s'"),
3698
                  virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3699 3700 3701 3702 3703
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
3704
    virBufferAsprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
3705
                      def->target.port);
M
Matthias Bolte 已提交
3706 3707 3708 3709 3710 3711 3712

    return 0;
}



int
3713 3714
virVMXFormatParallel(virVMXContext *ctx, virDomainChrDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3715
{
M
Matthias Bolte 已提交
3716 3717
    char *fileName = NULL;

3718
    if (def->target.port < 0 || def->target.port > 2) {
3719
        VMX_ERROR(VIR_ERR_INTERNAL_ERROR,
3720
                  _("Parallel port index %d out of [0..2] range"),
3721
                  def->target.port);
M
Matthias Bolte 已提交
3722 3723 3724
        return -1;
    }

3725
    virBufferAsprintf(buffer, "parallel%d.present = \"true\"\n",
3726
                      def->target.port);
M
Matthias Bolte 已提交
3727

M
Matthias Bolte 已提交
3728
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3729
    switch (def->source.type) {
M
Matthias Bolte 已提交
3730
      case VIR_DOMAIN_CHR_TYPE_DEV:
3731
        virBufferAsprintf(buffer, "parallel%d.fileType = \"device\"\n",
3732
                          def->target.port);
3733
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3734
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3735 3736 3737
        break;

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

3741
        fileName = ctx->formatFileName(def->source.data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3742 3743 3744 3745 3746

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

3747
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3748
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3749 3750

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3751 3752 3753
        break;

      default:
3754
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3755
                  _("Unsupported character device type '%s'"),
3756
                  virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3757 3758 3759 3760 3761
        return -1;
    }

    return 0;
}
3762 3763 3764 3765 3766 3767



int
virVMXFormatSVGA(virDomainVideoDefPtr def, virBufferPtr buffer)
{
3768 3769
    unsigned long long vram;

3770
    if (def->type != VIR_DOMAIN_VIDEO_TYPE_VMVGA) {
3771
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED,
3772 3773 3774 3775 3776 3777 3778 3779 3780
                  _("Unsupported video device type '%s'"),
                  virDomainVideoTypeToString(def->type));
        return -1;
    }

    /*
     * For Windows guests the VRAM size should be a multiple of 64 kilobyte.
     * See http://kb.vmware.com/kb/1003 and http://kb.vmware.com/kb/1001558
     */
3781
    vram = VIR_DIV_UP(def->vram, 64) * 64;
3782 3783

    if (def->heads > 1) {
3784
        VMX_ERROR(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3785 3786 3787 3788
                  _("Multi-head video devices are unsupported"));
        return -1;
    }

3789
    virBufferAsprintf(buffer, "svga.vramSize = \"%lld\"\n",
3790
                      vram * 1024); /* kilobyte to byte */
3791 3792 3793

    return 0;
}