vmx.c 128.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
 *
 * 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
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24 25
 *
 */

#include <config.h>

26 27
#include <c-ctype.h>

28
#include "internal.h"
29
#include "virerror.h"
30
#include "virconf.h"
31
#include "viralloc.h"
32
#include "virlog.h"
33
#include "viruuid.h"
34
#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
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum



264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
################################################################################
## filesystems #################################################################

                                        isolation.tools.hgfs.disable = "false"  # defaults to "true"

def->nfss = 1                     <=>   sharedFolder.maxNum = "1"               # must match the number of shared folders

                                        sharedFolder[0..n] -> <filesystem>

def->fss[0]...                    <=>   sharedFolder0.present = "true"          # defaults to "false"
                                        sharedFolder0.enabled = "true"          # defaults to "false"
                                        sharedFolder0.expiration = "never"      # defaults to "never"
                                        sharedFolder0.readAccess = "true"       # defaults to "false"
->type = _FS_TYPE_MOUNT
->fsdriver
->accessmode
->wrpolicy
->src = <value>                   <=>   sharedFolder0.hostPath = "<value>"      # defaults to ?
->dst = <value>                   <=>   sharedFolder0.guestName = "<value>"
->readonly = <readonly>           <=>   sharedFolder0.writeAccess = "<value>"   # "true" -> <readonly> = 0, "false" -> <readonly> = 1



287 288 289 290 291 292 293 294 295 296 297 298
################################################################################
## 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
299
                                        ethernet0.features = "15"               # if present and virtualDev is "vmxnet" => vmxnet2 (enhanced)
300 301 302 303


                                        ethernet0.addressType = "generated"     # default to "generated"
->mac = <value>                   <=>   ethernet0.generatedAddress = "<value>"
304
                                        ethernet0.generatedAddressOffset = "0"  # ?
305 306 307 308 309


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

310 311 312 313

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

314 315 316 317 318

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

M
Matthias Bolte 已提交
319
                                                                                # 00:0c:29 prefix for autogenerated mac's -> ethernet0.addressType = "generated"
320
                                                                                # 00:50:56 prefix for manual configured mac's
M
Matthias Bolte 已提交
321
                                                                                #          00:50:56:00:00:00 - 00:50:56:3f:ff:ff -> ethernet0.addressType = "static"
322
                                                                                #          00:50:56:80:00:00 - 00:50:56:bf:ff:ff -> ethernet0.addressType = "vpx"
323 324 325 326 327 328 329
                                                                                # 00:05:69 old prefix from esx 1.5


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

...
->type = _NET_TYPE_BRIDGE         <=>   ethernet0.connectionType = "bridged"    # defaults to "bridged"
M
Matthias Bolte 已提交
330
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
331 332 333 334


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

M
Matthias Bolte 已提交
335
...                                                                             # FIXME: Investigate if ESX supports this
336 337 338 339 340
->type = _NET_TYPE_NETWORK        <=>   ethernet0.connectionType = "hostonly"   # defaults to "bridged"


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

M
Matthias Bolte 已提交
341
...                                                                             # FIXME: Investigate if ESX supports this
342 343 344 345 346 347 348
->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 已提交
349 350
->data.bridge.brname = <value>    <=>   ethernet0.networkName = "<value>"
->ifname = <value>                <=>   ethernet0.vnet = "<value>"
351 352 353



354 355 356 357 358 359 360 361 362 363
################################################################################
## video #######################################################################

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



364 365 366 367 368 369 370 371 372
################################################################################
## serials #####################################################################

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

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

def->serials[0]...
373
->target.port = <port>
374 375 376 377 378 379 380


## 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 已提交
381
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
382 383 384 385 386 387 388


## 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 已提交
389
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
390 391 392 393 394 395


## 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 已提交
396
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
397
???                               <=>   serial0.tryNoRxLoss = "true"            # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
398
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
399 400 401

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
402
???                               <=>   serial0.pipe.endPoint = "server"        # defaults to ?, FIXME: not representable
403
???                               <=>   serial0.tryNoRxLoss = "true"            # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
404
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
405 406 407 408 409 410


## 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 已提交
411
???                               <=>   serial0.pipe.endPoint = "client"        # defaults to ?, FIXME: not representable
412
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
413
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
414 415 416

->type = _CHR_TYPE_PIPE           <=>   serial0.fileType = "pipe"
->data.file.path = <value>        <=>   serial0.fileName = "<value>"            # e.g. "serial0.pipe"
M
Matthias Bolte 已提交
417
???                               <=>   serial0.pipe.endPoint = "server"        # defaults to ?, FIXME: not representable
418
???                               <=>   serial0.tryNoRxLoss = "false"           # defaults to "false", FIXME: not representable
M
Matthias Bolte 已提交
419
???                               <=>   serial0.yieldOnMsrRead = "true"         # defaults to "false", FIXME: not representable
420 421


422 423 424 425 426 427 428 429
## 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>

430
->data.tcp.listen = true          <=>   serial0.network.endPoint = "server"     # defaults to "server"
431 432 433 434 435 436 437 438 439 440 441 442 443 444

???                               <=>   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>

445
->data.tcp.listen = false         <=>   serial0.network.endPoint = "client"     # defaults to "server"
446 447 448 449 450 451

???                               <=>   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


452 453 454 455 456 457 458 459 460 461

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

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

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

def->parallels[0]...
462
->target.port = <port>
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477


## 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 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492


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

493 494
*/

495
#define VIR_FROM_THIS VIR_FROM_NONE
496

497
#define VMX_BUILD_NAME_EXTRA(_suffix, _extra)                                 \
498 499
    snprintf(_suffix##_name, sizeof(_suffix##_name), "%s."_extra, prefix);

500 501
#define VMX_BUILD_NAME(_suffix)                                               \
    VMX_BUILD_NAME_EXTRA(_suffix, #_suffix)
502

503 504 505
/* 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 */
506 507
VIR_ENUM_DECL(virVMXControllerModelSCSI)
VIR_ENUM_IMPL(virVMXControllerModelSCSI, VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LAST,
508
              "auto", /* just to match virDomainControllerModel, will never be used */
509 510 511
              "buslogic",
              "lsilogic",
              "lsisas1068",
512
              "pvscsi",
513 514
              "UNUSED ibmvscsi",
              "UNUSED virtio-scsi");
515 516


517 518 519 520 521

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

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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
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 已提交
570
int
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
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) {
608 609
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxml2 doesn't handle %s encoding"), encoding);
610 611 612 613 614 615 616
        return NULL;
    }

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

    if (xmlCharEncInFunc(handler, utf8, input) < 0) {
617 618
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not convert from %s to UTF-8 encoding"), encoding);
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
        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;
        }

649 650
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing essential config entry '%s'"), name);
651 652 653 654
        return -1;
    }

    if (value->type != VIR_CONF_STRING) {
655 656
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
657 658 659 660 661 662 663 664
        return -1;
    }

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

665 666
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing essential config entry '%s'"), name);
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
        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 {
694 695
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
696 697 698 699 700
            return -1;
        }
    }

    if (value->type != VIR_CONF_STRING) {
701 702
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
703 704 705 706 707 708 709
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        } else {
710 711
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
712 713 714 715 716
            return -1;
        }
    }

    if (virUUIDParse(value->str, uuid) < 0) {
717 718
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse UUID from string '%s'"), value->str);
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
        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 {
740 741
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
742 743 744 745 746 747 748 749 750
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
751 752
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Missing essential config entry '%s'"), name);
753 754 755 756
                return -1;
            }
        }

757
        if (STRCASEEQ(value->str, "unlimited")) {
758 759
            *number = -1;
        } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) {
760 761 762
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Config entry '%s' must represent an integer value"),
                           name);
763 764 765
            return -1;
        }
    } else {
766 767
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
        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 {
789 790
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
791 792 793 794 795 796 797 798 799
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
800 801
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Missing essential config entry '%s'"), name);
802 803 804 805 806 807 808 809 810
                return -1;
            }
        }

        if (STRCASEEQ(value->str, "true")) {
            *boolean_ = 1;
        } else if (STRCASEEQ(value->str, "false")) {
            *boolean_ = 0;
        } else {
811 812 813
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Config entry '%s' must represent a boolean value "
                             "(true|false)"), name);
814 815 816
            return -1;
        }
    } else {
817 818
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
819 820 821 822 823 824 825 826 827 828
        return -1;
    }

    return 0;
}



static int
virVMXSCSIDiskNameToControllerAndUnit(const char *name, int *controller, int *unit)
M
Matthias Bolte 已提交
829 830 831 832
{
    int idx;

    if (! STRPREFIX(name, "sd")) {
833 834 835
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'sd'"));
M
Matthias Bolte 已提交
836 837 838 839 840 841
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
842 843
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
844 845 846
        return -1;
    }

847
    /* Each of the 4 SCSI controllers has 1 bus with 15 units each for devices */
M
Matthias Bolte 已提交
848
    if (idx >= (4 * 15)) {
849 850
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
851 852 853 854
        return -1;
    }

    *controller = idx / 15;
855
    *unit = idx % 15;
M
Matthias Bolte 已提交
856

857 858 859
    /* Skip the controller ifself at unit 7 */
    if (*unit >= 7) {
        ++(*unit);
M
Matthias Bolte 已提交
860 861 862 863 864 865 866
    }

    return 0;
}



867 868
static int
virVMXIDEDiskNameToBusAndUnit(const char *name, int *bus, int *unit)
M
Matthias Bolte 已提交
869 870 871 872
{
    int idx;

    if (! STRPREFIX(name, "hd")) {
873 874 875
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'hd'"));
M
Matthias Bolte 已提交
876 877 878 879 880 881
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
882 883
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
884 885 886
        return -1;
    }

887
    /* The IDE controller has 2 buses with 2 units each for devices */
M
Matthias Bolte 已提交
888
    if (idx >= (2 * 2)) {
889 890
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("IDE disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
891 892 893
        return -1;
    }

894 895
    *bus = idx / 2;
    *unit = idx % 2;
M
Matthias Bolte 已提交
896 897 898 899 900 901

    return 0;
}



902 903
static int
virVMXFloppyDiskNameToUnit(const char *name, int *unit)
M
Matthias Bolte 已提交
904 905 906 907
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
908 909 910
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
911 912 913 914 915 916
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
917 918
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
919 920 921
        return -1;
    }

922
    /* The FDC controller has 1 bus with 2 units for devices */
M
Matthias Bolte 已提交
923
    if (idx >= 2) {
924 925
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Floppy disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
926 927 928
        return -1;
    }

929 930 931 932 933 934 935
    *unit = idx;

    return 0;
}



936 937
static int
virVMXVerifyDiskAddress(virCapsPtr caps, virDomainDiskDefPtr disk)
938 939 940 941 942 943 944
{
    virDomainDiskDef def;
    virDomainDeviceDriveAddressPtr drive;

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

    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
945 946 947
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported disk address type '%s'"),
                       virDomainDeviceAddressTypeToString(disk->info.type));
948 949 950 951 952 953 954 955 956
        return -1;
    }

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

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

    if (virDomainDiskDefAssignAddress(caps, &def) < 0) {
957 958
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not verify disk address"));
959 960 961 962 963 964
        return -1;
    }

    if (def.info.addr.drive.controller != drive->controller ||
        def.info.addr.drive.bus != drive->bus ||
        def.info.addr.drive.unit != drive->unit) {
965 966 967
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Disk address %d:%d:%d doesn't match target device '%s'"),
                       drive->controller, drive->bus, drive->unit, disk->dst);
968 969 970 971 972 973
        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) {
974 975 976
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI controller index %d out of [0..3] range"),
                           drive->controller);
977 978 979 980
            return -1;
        }

        if (drive->bus != 0) {
981 982 983
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI bus index %d out of [0] range"),
                           drive->bus);
984 985 986 987
            return -1;
        }

        if (drive->unit > 15 || drive->unit == 7) {
988 989 990
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI unit index %d out of [0..6,8..15] range"),
                           drive->unit);
991 992 993 994
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        if (drive->controller != 0) {
995 996 997
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE controller index %d out of [0] range"),
                           drive->controller);
998 999 1000 1001
            return -1;
        }

        if (drive->bus > 1) {
1002 1003 1004
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE bus index %d out of [0..1] range"),
                           drive->bus);
1005 1006 1007 1008
            return -1;
        }

        if (drive->unit > 1) {
1009 1010 1011
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE unit index %d out of [0..1] range"),
                           drive->unit);
1012 1013 1014 1015
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
        if (drive->controller != 0) {
1016 1017 1018
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC controller index %d out of [0] range"),
                           drive->controller);
1019 1020 1021 1022
            return -1;
        }

        if (drive->bus != 0) {
1023 1024 1025
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC bus index %d out of [0] range"),
                           drive->bus);
1026 1027 1028 1029
            return -1;
        }

        if (drive->unit > 1) {
1030 1031 1032
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC unit index %d out of [0..1] range"),
                           drive->unit);
1033 1034 1035
            return -1;
        }
    } else {
1036 1037 1038
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported bus type '%s'"),
                       virDomainDiskBusTypeToString(disk->bus));
1039 1040 1041 1042 1043 1044 1045 1046
        return -1;
    }

    return 0;
}



1047 1048 1049
static int
virVMXHandleLegacySCSIDiskDriverName(virDomainDefPtr def,
                                     virDomainDiskDefPtr disk)
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
{
    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);
    }

1065
    model = virDomainControllerModelSCSITypeFromString(disk->driverName);
1066 1067

    if (model < 0) {
1068 1069
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown driver name '%s'"), disk->driverName);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
        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) {
1081 1082 1083
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing SCSI controller for index %d"),
                       disk->info.addr.drive.controller);
1084 1085 1086 1087 1088 1089
        return -1;
    }

    if (controller->model == -1) {
        controller->model = model;
    } else if (controller->model != model) {
1090 1091 1092 1093 1094
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Inconsistent SCSI controller model ('%s' is not '%s') "
                         "for SCSI controller index %d"), disk->driverName,
                       virDomainControllerModelSCSITypeToString(controller->model),
                       controller->idx);
1095 1096
        return -1;
    }
M
Matthias Bolte 已提交
1097 1098 1099 1100 1101 1102

    return 0;
}



1103 1104 1105
static int
virVMXGatherSCSIControllers(virVMXContext *ctx, virDomainDefPtr def,
                            int virtualDev[4], bool present[4])
1106 1107 1108
{
    int result = -1;
    int i, k;
M
Matthias Bolte 已提交
1109
    virDomainDiskDefPtr disk;
1110 1111 1112 1113
    virDomainControllerDefPtr controller;
    bool controllerHasDisksAttached;
    int count = 0;
    int *autodetectedModels;
M
Matthias Bolte 已提交
1114

1115 1116 1117 1118 1119 1120 1121
    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 已提交
1122

1123
        if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1124
            /* skip non-SCSI controllers */
M
Matthias Bolte 已提交
1125 1126 1127
            continue;
        }

1128
        controllerHasDisksAttached = false;
1129

1130 1131 1132 1133 1134 1135
        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;
1136 1137
                break;
            }
M
Matthias Bolte 已提交
1138 1139
        }

1140
        if (! controllerHasDisksAttached) {
1141
            /* skip SCSI controllers without attached disks */
1142 1143 1144
            continue;
        }

1145
        if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_AUTO &&
1146
            ctx->autodetectSCSIControllerModel != NULL) {
1147 1148
            count = 0;

1149 1150
            /* try to autodetect the SCSI controller model by collecting
             * SCSI controller model of all disks attached to this controller */
1151 1152 1153 1154 1155
            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) {
1156 1157 1158
                    if (ctx->autodetectSCSIControllerModel
                               (disk, &autodetectedModels[count],
                                ctx->opaque) < 0) {
1159 1160 1161 1162 1163 1164 1165
                        goto cleanup;
                    }

                    ++count;
                }
            }

1166 1167
            /* autodetection fails when the disks attached to one controller
             * have inconsistent SCSI controller models */
1168 1169
            for (k = 0; k < count; ++k) {
                if (autodetectedModels[k] != autodetectedModels[0]) {
1170 1171 1172 1173
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Disks on SCSI controller %d have inconsistent "
                                     "controller models, cannot autodetect model"),
                                   controller->idx);
1174 1175 1176 1177 1178
                    goto cleanup;
                }
            }

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

1181
        if (controller->model != -1 &&
1182 1183 1184 1185
            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) {
1186 1187 1188 1189 1190
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML attribute 'model' of entry "
                             "'controller' to be 'buslogic' or 'lsilogic' or "
                             "'lsisas1068' or 'vmpvscsi' but found '%s'"),
                           virDomainControllerModelSCSITypeToString(controller->model));
1191
            goto cleanup;
M
Matthias Bolte 已提交
1192
        }
1193 1194 1195

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

1198 1199 1200 1201 1202 1203
    result = 0;

  cleanup:
    VIR_FREE(autodetectedModels);

    return result;
M
Matthias Bolte 已提交
1204 1205 1206 1207 1208 1209 1210 1211
}



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

1212
virDomainDefPtr
1213
virVMXParseConfig(virVMXContext *ctx, virCapsPtr caps, const char *vmx)
1214
{
M
Matthias Bolte 已提交
1215
    bool success = false;
1216
    virConfPtr conf = NULL;
1217 1218
    char *encoding = NULL;
    char *utf8;
1219 1220 1221 1222
    virDomainDefPtr def = NULL;
    long long config_version = 0;
    long long virtualHW_version = 0;
    long long memsize = 0;
1223 1224
    long long sched_mem_max = 0;
    long long sched_mem_minsize = 0;
1225 1226
    long long numvcpus = 0;
    char *sched_cpu_affinity = NULL;
1227
    char *sched_cpu_shares = NULL;
M
Matthias Bolte 已提交
1228
    char *guestOS = NULL;
M
Matthias Bolte 已提交
1229
    bool smbios_reflecthost = false;
1230
    int controller;
1231
    int bus;
1232
    int port;
1233
    bool present;
1234 1235
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
    int unit;
1236 1237
    bool hgfs_disabled = true;
    long long sharedFolder_maxNum = 0;
H
Hu Tao 已提交
1238
    int cpumasklen;
1239

1240
    if (ctx->parseFileName == NULL) {
1241 1242
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no parseFileName function set"));
1243 1244 1245
        return NULL;
    }

1246 1247 1248 1249 1250 1251
    conf = virConfReadMem(vmx, strlen(vmx), VIR_CONF_FLAG_VMX_FORMAT);

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

1252
    /* vmx:.encoding */
1253
    if (virVMXGetConfigString(conf, ".encoding", &encoding, true) < 0) {
1254 1255 1256 1257 1258 1259 1260 1261 1262
        goto cleanup;
    }

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

1263
        utf8 = virVMXConvertToUTF8(encoding, vmx);
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

        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 */
1279
    if (VIR_ALLOC(def) < 0) {
1280
        virReportOOMError();
A
ajia@redhat.com 已提交
1281
        goto cleanup;
1282 1283
    }

1284
    def->virtType = VIR_DOMAIN_VIRT_VMWARE;
1285 1286
    def->id = -1;

M
Matthias Bolte 已提交
1287
    /* vmx:config.version */
1288 1289
    if (virVMXGetConfigLong(conf, "config.version", &config_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1290
        goto cleanup;
1291 1292 1293
    }

    if (config_version != 8) {
1294 1295 1296
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'config.version' to be 8 but found "
                         "%lld"), config_version);
M
Matthias Bolte 已提交
1297
        goto cleanup;
1298 1299
    }

M
Matthias Bolte 已提交
1300
    /* vmx:virtualHW.version */
1301 1302
    if (virVMXGetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1303
        goto cleanup;
1304 1305
    }

P
Patrice LACHANCE 已提交
1306 1307
    if (virtualHW_version != 4 && virtualHW_version != 7 &&
        virtualHW_version != 8) {
1308 1309 1310 1311
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'virtualHW.version' to be 4, 7 or 8 "
                         "but found %lld"),
                       virtualHW_version);
M
Matthias Bolte 已提交
1312
        goto cleanup;
1313 1314
    }

M
Matthias Bolte 已提交
1315
    /* vmx:uuid.bios -> def:uuid */
1316
    /* FIXME: Need to handle 'uuid.action = "create"' */
1317
    if (virVMXGetConfigUUID(conf, "uuid.bios", def->uuid, true) < 0) {
M
Matthias Bolte 已提交
1318
        goto cleanup;
1319 1320
    }

M
Matthias Bolte 已提交
1321
    /* vmx:displayName -> def:name */
1322
    if (virVMXGetConfigString(conf, "displayName", &def->name, true) < 0) {
M
Matthias Bolte 已提交
1323
        goto cleanup;
1324 1325
    }

1326
    if (def->name != NULL) {
1327 1328
        if (virVMXUnescapeHexPercent(def->name) < 0 ||
            virVMXUnescapeHexPipe(def->name) < 0) {
1329 1330
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'name' contains invalid escape sequence"));
1331 1332 1333 1334
            goto cleanup;
        }
    }

1335
    /* vmx:annotation -> def:description */
1336 1337
    if (virVMXGetConfigString(conf, "annotation", &def->description,
                              true) < 0) {
1338 1339 1340 1341
        goto cleanup;
    }

    if (def->description != NULL) {
1342
        if (virVMXUnescapeHexPipe(def->description) < 0) {
1343 1344 1345
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'annotation' contains invalid escape "
                             "sequence"));
1346
            goto cleanup;
1347 1348 1349
        }
    }

1350
    /* vmx:memsize -> def:mem.max_balloon */
1351
    if (virVMXGetConfigLong(conf, "memsize", &memsize, 32, true) < 0) {
M
Matthias Bolte 已提交
1352
        goto cleanup;
1353 1354 1355
    }

    if (memsize <= 0 || memsize % 4 != 0) {
1356 1357 1358
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'memsize' to be an unsigned "
                         "integer (multiple of 4) but found %lld"), memsize);
M
Matthias Bolte 已提交
1359
        goto cleanup;
1360 1361
    }

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

1364
    /* vmx:sched.mem.max -> def:mem.cur_balloon */
1365 1366
    if (virVMXGetConfigLong(conf, "sched.mem.max", &sched_mem_max, memsize,
                            true) < 0) {
M
Matthias Bolte 已提交
1367
        goto cleanup;
1368 1369
    }

1370 1371
    if (sched_mem_max < 0) {
        sched_mem_max = memsize;
1372 1373
    }

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

1376 1377
    if (def->mem.cur_balloon > def->mem.max_balloon) {
        def->mem.cur_balloon = def->mem.max_balloon;
1378 1379
    }

1380
    /* vmx:sched.mem.minsize -> def:mem.min_guarantee */
1381 1382
    if (virVMXGetConfigLong(conf, "sched.mem.minsize", &sched_mem_minsize, 0,
                            true) < 0) {
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
        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 已提交
1396
    /* vmx:numvcpus -> def:vcpus */
1397
    if (virVMXGetConfigLong(conf, "numvcpus", &numvcpus, 1, true) < 0) {
M
Matthias Bolte 已提交
1398
        goto cleanup;
1399 1400 1401
    }

    if (numvcpus <= 0 || (numvcpus % 2 != 0 && numvcpus != 1)) {
1402 1403 1404
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'numvcpus' to be an unsigned "
                         "integer (1 or a multiple of 2) but found %lld"), numvcpus);
M
Matthias Bolte 已提交
1405
        goto cleanup;
1406 1407
    }

E
Eric Blake 已提交
1408
    def->maxvcpus = def->vcpus = numvcpus;
1409

M
Matthias Bolte 已提交
1410
    /* vmx:sched.cpu.affinity -> def:cpumask */
1411
    /* NOTE: maps to VirtualMachine:config.cpuAffinity.affinitySet */
1412 1413
    if (virVMXGetConfigString(conf, "sched.cpu.affinity", &sched_cpu_affinity,
                              true) < 0) {
M
Matthias Bolte 已提交
1414
        goto cleanup;
1415 1416
    }

1417
    if (sched_cpu_affinity != NULL && STRCASENEQ(sched_cpu_affinity, "all")) {
1418 1419 1420
        const char *current = sched_cpu_affinity;
        int number, count = 0;

H
Hu Tao 已提交
1421
        cpumasklen = 0;
1422

H
Hu Tao 已提交
1423 1424
        def->cpumask = virBitmapNew(VIR_DOMAIN_CPUMASK_LEN);
        if (!def->cpumask) {
1425
            virReportOOMError();
M
Matthias Bolte 已提交
1426
            goto cleanup;
1427 1428 1429 1430 1431 1432 1433 1434
        }

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

            number = virParseNumber(&current);

            if (number < 0) {
1435 1436 1437 1438
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry 'sched.cpu.affinity' to be "
                                 "a comma separated list of unsigned integers but "
                                 "found '%s'"), sched_cpu_affinity);
M
Matthias Bolte 已提交
1439
                goto cleanup;
1440 1441 1442
            }

            if (number >= VIR_DOMAIN_CPUMASK_LEN) {
1443 1444 1445
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("VMX entry 'sched.cpu.affinity' contains a %d, "
                                 "this value is too large"), number);
M
Matthias Bolte 已提交
1446
                goto cleanup;
1447 1448
            }

H
Hu Tao 已提交
1449 1450
            if (number + 1 > cpumasklen) {
                cpumasklen = number + 1;
1451 1452
            }

H
Hu Tao 已提交
1453
            ignore_value(virBitmapSetBit(def->cpumask, number));
1454 1455 1456 1457 1458 1459 1460 1461 1462
            ++count;

            virSkipSpaces(&current);

            if (*current == ',') {
                ++current;
            } else if (*current == '\0') {
                break;
            } else {
1463 1464 1465 1466
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry 'sched.cpu.affinity' to be "
                                 "a comma separated list of unsigned integers but "
                                 "found '%s'"), sched_cpu_affinity);
M
Matthias Bolte 已提交
1467
                goto cleanup;
1468 1469 1470 1471 1472 1473
            }

            virSkipSpaces(&current);
        }

        if (count < numvcpus) {
1474 1475 1476 1477
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("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 已提交
1478
            goto cleanup;
1479 1480 1481
        }
    }

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    /* 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) {
1498 1499 1500 1501
            virReportError(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);
1502 1503 1504 1505
            goto cleanup;
        }
    }

1506 1507 1508 1509 1510 1511 1512 1513 1514
    /* 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) {
1515
        virReportOOMError();
M
Matthias Bolte 已提交
1516
        goto cleanup;
1517 1518
    }

M
Matthias Bolte 已提交
1519
    /* vmx:guestOS -> def:os.arch */
1520
    if (virVMXGetConfigString(conf, "guestOS", &guestOS, true) < 0) {
M
Matthias Bolte 已提交
1521
        goto cleanup;
M
Matthias Bolte 已提交
1522 1523
    }

1524
    if (guestOS != NULL && virFileHasSuffix(guestOS, "-64")) {
1525
        def->os.arch = VIR_ARCH_X86_64;
M
Matthias Bolte 已提交
1526
    } else {
1527
        def->os.arch = VIR_ARCH_I686;
M
Matthias Bolte 已提交
1528 1529
    }

M
Matthias Bolte 已提交
1530
    /* vmx:smbios.reflecthost -> def:os.smbios_mode */
1531 1532
    if (virVMXGetConfigBoolean(conf, "smbios.reflecthost",
                               &smbios_reflecthost, false, true) < 0) {
M
Matthias Bolte 已提交
1533 1534 1535 1536 1537 1538 1539
        goto cleanup;
    }

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

1540 1541
    /* def:features */
    /* FIXME */
1542

1543 1544
    /* def:clock */
    /* FIXME */
1545 1546

    /* def:graphics */
M
Matthias Bolte 已提交
1547
    if (VIR_ALLOC_N(def->graphics, 1) < 0) {
1548
        virReportOOMError();
M
Matthias Bolte 已提交
1549
        goto cleanup;
M
Matthias Bolte 已提交
1550 1551 1552 1553
    }

    def->ngraphics = 0;

1554
    if (virVMXParseVNC(conf, &def->graphics[def->ngraphics]) < 0) {
M
Matthias Bolte 已提交
1555
        goto cleanup;
M
Matthias Bolte 已提交
1556 1557 1558 1559 1560
    }

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

M
Matthias Bolte 已提交
1562 1563
    /* def:disks: 4 * 15 scsi + 2 * 2 ide + 2 floppy = 66 */
    if (VIR_ALLOC_N(def->disks, 66) < 0) {
1564
        virReportOOMError();
M
Matthias Bolte 已提交
1565
        goto cleanup;
1566 1567 1568 1569 1570 1571
    }

    def->ndisks = 0;

    /* def:disks (scsi) */
    for (controller = 0; controller < 4; ++controller) {
1572 1573
        if (virVMXParseSCSIController(conf, controller, &present,
                                      &scsi_virtualDev[controller]) < 0) {
M
Matthias Bolte 已提交
1574
            goto cleanup;
1575 1576 1577 1578 1579 1580
        }

        if (! present) {
            continue;
        }

1581 1582
        for (unit = 0; unit < 16; ++unit) {
            if (unit == 7) {
1583
                /*
1584
                 * SCSI unit 7 is assigned to the SCSI controller and cannot be
1585 1586 1587 1588 1589
                 * used for disk devices.
                 */
                continue;
            }

1590 1591 1592
            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 已提交
1593
                goto cleanup;
1594 1595 1596 1597 1598 1599 1600
            }

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

1601
            if (virVMXParseDisk(ctx, caps, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1602
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
1603
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1604
                goto cleanup;
1605 1606 1607 1608 1609 1610 1611 1612 1613
            }

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

    /* def:disks (ide) */
1614 1615
    for (bus = 0; bus < 2; ++bus) {
        for (unit = 0; unit < 2; ++unit) {
1616 1617 1618
            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 已提交
1619
                goto cleanup;
1620 1621 1622 1623 1624 1625 1626
            }

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

1627 1628 1629
            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 已提交
1630
                goto cleanup;
1631 1632 1633 1634 1635 1636 1637 1638 1639
            }

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

    /* def:disks (floppy) */
1640
    for (unit = 0; unit < 2; ++unit) {
1641 1642 1643
        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 已提交
1644
            goto cleanup;
1645 1646 1647 1648 1649 1650 1651
        }

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

1652 1653
    /* def:controllers */
    if (virDomainDefAddImplicitControllers(def) < 0) {
1654
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not add controllers"));
1655 1656 1657 1658 1659 1660 1661
        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) {
1662 1663 1664
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI controller index %d out of [0..3] range"),
                               def->controllers[controller]->idx);
1665 1666 1667 1668 1669 1670 1671 1672
                goto cleanup;
            }

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

1673
    /* def:fss */
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    if (virVMXGetConfigBoolean(conf, "isolation.tools.hgfs.disable",
                               &hgfs_disabled, true, true) < 0) {
        goto cleanup;
    }

    if (!hgfs_disabled) {
        if (virVMXGetConfigLong(conf, "sharedFolder.maxNum", &sharedFolder_maxNum,
                                0, true) < 0) {
            goto cleanup;
        }

        if (sharedFolder_maxNum > 0) {
            int number;

            if (VIR_ALLOC_N(def->fss, sharedFolder_maxNum) < 0) {
                virReportOOMError();
                goto cleanup;
            }

            def->nfss = 0;

            for (number = 0; number < sharedFolder_maxNum; ++number) {
                if (virVMXParseFileSystem(conf, number,
                                          &def->fss[def->nfss]) < 0) {
                    goto cleanup;
                }

                if (def->fss[def->nfss] != NULL) {
                    ++def->nfss;
                }
            }
        }
    }
1707 1708 1709

    /* def:nets */
    if (VIR_ALLOC_N(def->nets, 4) < 0) {
1710
        virReportOOMError();
M
Matthias Bolte 已提交
1711
        goto cleanup;
1712 1713 1714 1715 1716
    }

    def->nnets = 0;

    for (controller = 0; controller < 4; ++controller) {
1717 1718
        if (virVMXParseEthernet(conf, controller,
                                &def->nets[def->nnets]) < 0) {
M
Matthias Bolte 已提交
1719
            goto cleanup;
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
        }

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

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

1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    /* 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;

1744 1745 1746 1747 1748 1749 1750 1751
    /* def:sounds */
    /* FIXME */

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

    /* def:serials */
    if (VIR_ALLOC_N(def->serials, 4) < 0) {
1752
        virReportOOMError();
M
Matthias Bolte 已提交
1753
        goto cleanup;
1754 1755 1756 1757 1758
    }

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1759 1760
        if (virVMXParseSerial(ctx, conf, port,
                              &def->serials[def->nserials]) < 0) {
M
Matthias Bolte 已提交
1761
            goto cleanup;
1762 1763 1764 1765 1766 1767 1768 1769 1770
        }

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

    /* def:parallels */
    if (VIR_ALLOC_N(def->parallels, 3) < 0) {
1771
        virReportOOMError();
M
Matthias Bolte 已提交
1772
        goto cleanup;
1773 1774 1775 1776 1777
    }

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1778 1779
        if (virVMXParseParallel(ctx, conf, port,
                                &def->parallels[def->nparallels]) < 0) {
M
Matthias Bolte 已提交
1780
            goto cleanup;
1781 1782 1783 1784 1785 1786 1787
        }

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

M
Matthias Bolte 已提交
1788 1789
    success = true;

M
Matthias Bolte 已提交
1790
  cleanup:
M
Matthias Bolte 已提交
1791 1792 1793 1794 1795
    if (! success) {
        virDomainDefFree(def);
        def = NULL;
    }

1796
    virConfFree(conf);
1797
    VIR_FREE(encoding);
1798
    VIR_FREE(sched_cpu_affinity);
1799
    VIR_FREE(sched_cpu_shares);
M
Matthias Bolte 已提交
1800
    VIR_FREE(guestOS);
1801 1802 1803 1804 1805 1806

    return def;
}



M
Matthias Bolte 已提交
1807
int
1808
virVMXParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1809
{
1810
    bool enabled = false;
M
Matthias Bolte 已提交
1811
    long long port = 0;
1812
    char *listenAddr = NULL;
M
Matthias Bolte 已提交
1813 1814

    if (def == NULL || *def != NULL) {
1815
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1816 1817 1818
        return -1;
    }

1819 1820
    if (virVMXGetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
                               false, true) < 0) {
M
Matthias Bolte 已提交
1821 1822 1823 1824 1825 1826 1827 1828
        return -1;
    }

    if (! enabled) {
        return 0;
    }

    if (VIR_ALLOC(*def) < 0) {
1829
        virReportOOMError();
M
Matthias Bolte 已提交
1830 1831 1832 1833 1834
        goto failure;
    }

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1835 1836 1837
    if (virVMXGetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
                            true) < 0 ||
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.ip",
1838
                              &listenAddr, true) < 0 ||
1839 1840 1841 1842
        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 已提交
1843 1844 1845
        goto failure;
    }

1846 1847 1848 1849 1850 1851
    if (listenAddr) {
        if (virDomainGraphicsListenSetAddress(*def, 0, listenAddr, -1, true) < 0)
            goto failure;
        VIR_FREE(listenAddr);
    }

M
Matthias Bolte 已提交
1852
    if (port < 0) {
1853
        VIR_WARN("VNC is enabled but VMX entry 'RemoteDisplay.vnc.port' "
M
Matthias Bolte 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
                  "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:
1870
    VIR_FREE(listenAddr);
M
Matthias Bolte 已提交
1871 1872 1873 1874 1875 1876 1877 1878
    virDomainGraphicsDefFree(*def);
    *def = NULL;

    return -1;
}



1879
int
1880 1881
virVMXParseSCSIController(virConfPtr conf, int controller, bool *present,
                          int *virtualDev)
1882
{
1883
    int result = -1;
1884 1885
    char present_name[32];
    char virtualDev_name[32];
1886 1887
    char *virtualDev_string = NULL;
    char *tmp;
1888

1889
    if (virtualDev == NULL || *virtualDev != -1) {
1890
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1891
        return -1;
1892 1893 1894
    }

    if (controller < 0 || controller > 3) {
1895 1896 1897
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI controller index %d out of [0..3] range"),
                       controller);
1898
        return -1;
1899 1900
    }

1901 1902 1903
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1904

1905
    if (virVMXGetConfigBoolean(conf, present_name, present, false, true) < 0) {
1906
        goto cleanup;
1907 1908 1909
    }

    if (! *present) {
1910 1911
        result = 0;
        goto cleanup;
1912 1913
    }

1914 1915
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev_string,
                              true) < 0) {
1916
        goto cleanup;
1917 1918
    }

1919 1920 1921 1922 1923 1924 1925
    if (virtualDev_string != NULL) {
        tmp = virtualDev_string;

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

1926
        *virtualDev = virVMXControllerModelSCSITypeFromString(virtualDev_string);
1927 1928

        if (*virtualDev == -1 ||
1929 1930 1931 1932
            (*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)) {
1933 1934 1935 1936
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'buslogic' or 'lsilogic' "
                             "or 'lsisas1068' or 'pvscsi' but found '%s'"),
                           virtualDev_name, virtualDev_string);
1937
            goto cleanup;
1938
        }
1939 1940
    }

1941
    result = 0;
1942

1943
  cleanup:
1944
    VIR_FREE(virtualDev_string);
1945

1946
    return result;
1947 1948 1949 1950 1951
}



int
1952 1953 1954
virVMXParseDisk(virVMXContext *ctx, virCapsPtr caps, virConfPtr conf,
                int device, int busType, int controllerOrBus, int unit,
                virDomainDiskDefPtr *def)
1955 1956
{
    /*
1957 1958 1959 1960
     *          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]
1961
     *
1962 1963 1964 1965
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK, VIR_DOMAIN_DISK_DEVICE_CDROM}
     *         busType = VIR_DOMAIN_DISK_BUS_IDE
     * controllerOrBus = [0..1] -> bus
     *            unit = [0..1]
1966
     *
1967 1968 1969 1970
     *          device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *         busType = VIR_DOMAIN_DISK_BUS_FDC
     * controllerOrBus = [0]
     *            unit = [0..1]
1971 1972
     */

M
Matthias Bolte 已提交
1973
    int result = -1;
1974 1975 1976
    char *prefix = NULL;

    char present_name[32] = "";
1977
    bool present = false;
1978 1979

    char startConnected_name[32] = "";
1980
    bool startConnected = false;
1981 1982 1983 1984 1985

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

    char clientDevice_name[32] = "";
1986
    bool clientDevice = false;
1987 1988 1989 1990 1991 1992 1993 1994

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

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

    char writeThrough_name[32] = "";
1995
    bool writeThrough = false;
1996 1997

    if (def == NULL || *def != NULL) {
1998
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1999 2000 2001 2002
        return -1;
    }

    if (VIR_ALLOC(*def) < 0) {
2003
        virReportOOMError();
M
Matthias Bolte 已提交
2004
        return -1;
2005 2006 2007
    }

    (*def)->device = device;
2008
    (*def)->bus = busType;
2009 2010 2011 2012

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2013 2014
        if (busType == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controllerOrBus < 0 || controllerOrBus > 3) {
2015 2016 2017
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI controller index %d out of [0..3] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2018
                goto cleanup;
2019 2020
            }

2021
            if (unit < 0 || unit > 15 || unit == 7) {
2022 2023 2024
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI unit index %d out of [0..6,8..15] range"),
                               unit);
M
Matthias Bolte 已提交
2025
                goto cleanup;
2026 2027
            }

2028
            if (virAsprintf(&prefix, "scsi%d:%d", controllerOrBus, unit) < 0) {
2029
                virReportOOMError();
M
Matthias Bolte 已提交
2030
                goto cleanup;
2031 2032
            }

M
Matthias Bolte 已提交
2033
            (*def)->dst =
2034
               virIndexToDiskName
2035
                 (controllerOrBus * 15 + (unit < 7 ? unit : unit - 1), "sd");
2036 2037

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2038
                goto cleanup;
2039
            }
2040 2041
        } else if (busType == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controllerOrBus < 0 || controllerOrBus > 1) {
2042 2043 2044
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IDE bus index %d out of [0..1] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2045
                goto cleanup;
2046 2047
            }

2048
            if (unit < 0 || unit > 1) {
2049 2050
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IDE unit index %d out of [0..1] range"), unit);
M
Matthias Bolte 已提交
2051
                goto cleanup;
2052 2053
            }

2054
            if (virAsprintf(&prefix, "ide%d:%d", controllerOrBus, unit) < 0) {
2055
                virReportOOMError();
M
Matthias Bolte 已提交
2056
                goto cleanup;
2057 2058
            }

2059
            (*def)->dst = virIndexToDiskName(controllerOrBus * 2 + unit, "hd");
2060 2061

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2062
                goto cleanup;
2063 2064
            }
        } else {
2065 2066 2067 2068
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2069
            goto cleanup;
2070 2071
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2072 2073
        if (busType == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controllerOrBus != 0) {
2074 2075 2076
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC controller index %d out of [0] range"),
                               controllerOrBus);
2077 2078 2079 2080
                goto cleanup;
            }

            if (unit < 0 || unit > 1) {
2081 2082 2083
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC unit index %d out of [0..1] range"),
                               unit);
M
Matthias Bolte 已提交
2084
                goto cleanup;
2085 2086
            }

2087
            if (virAsprintf(&prefix, "floppy%d", unit) < 0) {
2088
                virReportOOMError();
M
Matthias Bolte 已提交
2089
                goto cleanup;
2090 2091
            }

2092
            (*def)->dst = virIndexToDiskName(unit, "fd");
2093 2094

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2095
                goto cleanup;
2096 2097
            }
        } else {
2098 2099 2100 2101
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2102
            goto cleanup;
2103 2104
        }
    } else {
2105 2106 2107
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2108
        goto cleanup;
2109 2110
    }

2111 2112 2113 2114 2115 2116 2117
    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);
2118 2119

    /* vmx:present */
2120
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2121
        goto cleanup;
2122 2123 2124
    }

    /* vmx:startConnected */
2125 2126
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2127
        goto cleanup;
2128 2129 2130 2131 2132 2133 2134 2135
    }

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

    /* vmx:deviceType -> def:type */
2136
    if (virVMXGetConfigString(conf, deviceType_name, &deviceType, true) < 0) {
M
Matthias Bolte 已提交
2137
        goto cleanup;
2138 2139 2140
    }

    /* vmx:clientDevice */
2141 2142
    if (virVMXGetConfigBoolean(conf, clientDevice_name, &clientDevice, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2143
        goto cleanup;
2144 2145 2146 2147 2148
    }

    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
2149
         * handle them (e.g. assign an image) without the VI Client GUI.
2150 2151 2152 2153 2154
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
2155
    if (virVMXGetConfigString(conf, fileType_name, &fileType, true) < 0) {
M
Matthias Bolte 已提交
2156
        goto cleanup;
2157 2158 2159
    }

    /* vmx:fileName -> def:src, def:type */
2160
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2161
        goto cleanup;
2162 2163 2164
    }

    /* vmx:writeThrough -> def:cachemode */
2165 2166
    if (virVMXGetConfigBoolean(conf, writeThrough_name, &writeThrough, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2167
        goto cleanup;
2168 2169 2170 2171
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
2172
        if (virFileHasSuffix(fileName, ".vmdk")) {
2173
            if (deviceType != NULL) {
2174
                if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
2175 2176
                    STRCASENEQ(deviceType, "scsi-hardDisk") &&
                    STRCASENEQ(deviceType, "disk")) {
2177 2178 2179 2180
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2181
                    goto cleanup;
2182
                } else if (busType == VIR_DOMAIN_DISK_BUS_IDE &&
2183 2184
                           STRCASENEQ(deviceType, "ata-hardDisk") &&
                           STRCASENEQ(deviceType, "disk")) {
2185 2186 2187 2188
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2189
                    goto cleanup;
2190 2191 2192 2193
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2194
            (*def)->src = ctx->parseFileName(fileName, ctx->opaque);
2195 2196 2197
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;

M
Matthias Bolte 已提交
2198
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2199
                goto cleanup;
M
Matthias Bolte 已提交
2200
            }
2201
        } else if (virFileHasSuffix(fileName, ".iso") ||
2202
                   STRCASEEQ(deviceType, "atapi-cdrom")) {
2203 2204 2205 2206 2207 2208 2209 2210
            /*
             * 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 {
2211 2212 2213
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid or not yet handled value '%s' for VMX entry "
                             "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2214
            goto cleanup;
2215 2216
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2217
        if (virFileHasSuffix(fileName, ".iso")) {
2218 2219
            if (deviceType != NULL) {
                if (STRCASENEQ(deviceType, "cdrom-image")) {
2220 2221 2222
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                     "but found '%s'"), deviceType_name, deviceType);
M
Matthias Bolte 已提交
2223
                    goto cleanup;
2224 2225 2226 2227
                }
            }

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

M
Matthias Bolte 已提交
2230
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2231
                goto cleanup;
M
Matthias Bolte 已提交
2232
            }
2233
        } else if (virFileHasSuffix(fileName, ".vmdk")) {
2234 2235 2236 2237 2238 2239 2240
            /*
             * 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;
2241
        } else if (STRCASEEQ(deviceType, "atapi-cdrom")) {
2242 2243 2244 2245 2246
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
2247 2248 2249
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid or not yet handled value '%s' for VMX entry "
                             "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2250
            goto cleanup;
2251 2252
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2253
        if (virFileHasSuffix(fileName, ".flp")) {
2254 2255
            if (fileType != NULL) {
                if (STRCASENEQ(fileType, "file")) {
2256 2257 2258
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'file' but "
                                     "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2259
                    goto cleanup;
2260 2261 2262 2263
                }
            }

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

M
Matthias Bolte 已提交
2266
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2267
                goto cleanup;
M
Matthias Bolte 已提交
2268
            }
2269
        } else if (fileType != NULL && STRCASEEQ(fileType, "device")) {
2270 2271 2272 2273 2274
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
            (*def)->src = fileName;

            fileName = NULL;
        } else {
2275 2276 2277
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid or not yet handled value '%s' for VMX entry "
                             "'%s'"), fileName, fileName_name);
M
Matthias Bolte 已提交
2278
            goto cleanup;
2279 2280
        }
    } else {
2281 2282
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2283
        goto cleanup;
2284 2285
    }

2286
    if (virDomainDiskDefAssignAddress(caps, *def) < 0) {
2287 2288
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not assign address to disk '%s'"), (*def)->src);
2289 2290 2291
        goto cleanup;
    }

M
Matthias Bolte 已提交
2292 2293
    result = 0;

M
Matthias Bolte 已提交
2294
  cleanup:
M
Matthias Bolte 已提交
2295 2296 2297 2298 2299
    if (result < 0) {
        virDomainDiskDefFree(*def);
        *def = NULL;
    }

2300 2301 2302 2303 2304 2305 2306
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2307
  ignore:
2308 2309 2310
    virDomainDiskDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2311 2312
    result = 0;

2313 2314 2315 2316 2317
    goto cleanup;
}



2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
int virVMXParseFileSystem(virConfPtr conf, int number, virDomainFSDefPtr *def)
{
    int result = -1;
    char prefix[48] = "";

    char present_name[48] = "";
    bool present = false;

    char enabled_name[48] = "";
    bool enabled = false;

    char hostPath_name[48] = "";
    char *hostPath = NULL;

    char guestName_name[48] = "";
    char *guestName = NULL;

    char writeAccess_name[48] = "";
    bool writeAccess = false;

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

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

    (*def)->type = VIR_DOMAIN_FS_TYPE_MOUNT;

    snprintf(prefix, sizeof(prefix), "sharedFolder%d", number);

    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(enabled);
    VMX_BUILD_NAME(hostPath);
    VMX_BUILD_NAME(guestName);
    VMX_BUILD_NAME(writeAccess);

    /* vmx:present */
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
        goto cleanup;
    }

    /* vmx:enabled */
    if (virVMXGetConfigBoolean(conf, enabled_name, &enabled, false, true) < 0) {
        goto cleanup;
    }

    if (!(present && enabled)) {
        goto ignore;
    }

    /* vmx:hostPath */
    if (virVMXGetConfigString(conf, hostPath_name, &hostPath, false) < 0) {
        goto cleanup;
    }

    (*def)->src = hostPath;
    hostPath = NULL;

    /* vmx:guestName */
    if (virVMXGetConfigString(conf, guestName_name, &guestName, false) < 0) {
        goto cleanup;
    }

    (*def)->dst = guestName;
    guestName = NULL;

    /* vmx:writeAccess */
    if (virVMXGetConfigBoolean(conf, writeAccess_name, &writeAccess, false,
                               true) < 0) {
        goto cleanup;
    }

    (*def)->readonly = !writeAccess;

    result = 0;

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

    VIR_FREE(hostPath);
    VIR_FREE(guestName);

    return result;

  ignore:
    virDomainFSDefFree(*def);
    *def = NULL;

    result = 0;

    goto cleanup;
}



2420
int
2421
virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
2422
{
M
Matthias Bolte 已提交
2423
    int result = -1;
2424 2425 2426
    char prefix[48] = "";

    char present_name[48] = "";
2427
    bool present = false;
2428 2429

    char startConnected_name[48] = "";
2430
    bool startConnected = false;
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446

    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;

2447 2448 2449
    char features_name[48] = "";
    long long features = 0;

2450 2451 2452
    char vnet_name[48] = "";
    char *vnet = NULL;

M
Matthias Bolte 已提交
2453 2454 2455
    char networkName_name[48] = "";
    char *networkName = NULL;

2456
    if (def == NULL || *def != NULL) {
2457
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2458 2459 2460 2461
        return -1;
    }

    if (controller < 0 || controller > 3) {
2462 2463 2464
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Ethernet controller index %d out of [0..3] range"),
                       controller);
2465
        return -1;
2466 2467 2468
    }

    if (VIR_ALLOC(*def) < 0) {
2469
        virReportOOMError();
M
Matthias Bolte 已提交
2470
        return -1;
2471 2472
    }

2473
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
2474

2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
    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);
2485 2486

    /* vmx:present */
2487
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2488
        goto cleanup;
2489 2490 2491
    }

    /* vmx:startConnected */
2492 2493
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2494
        goto cleanup;
2495 2496 2497 2498 2499 2500 2501 2502
    }

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

    /* vmx:connectionType -> def:type */
2503 2504
    if (virVMXGetConfigString(conf, connectionType_name, &connectionType,
                              true) < 0) {
M
Matthias Bolte 已提交
2505
        goto cleanup;
2506 2507 2508
    }

    /* vmx:addressType, vmx:generatedAddress, vmx:address -> def:mac */
2509 2510 2511 2512 2513
    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 已提交
2514
        goto cleanup;
2515 2516
    }

2517 2518
    if (addressType == NULL || STRCASEEQ(addressType, "generated") ||
        STRCASEEQ(addressType, "vpx")) {
2519
        if (generatedAddress != NULL) {
2520
            if (virMacAddrParse(generatedAddress, &(*def)->mac) < 0) {
2521 2522 2523 2524
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be MAC address but "
                                 "found '%s'"), generatedAddress_name,
                               generatedAddress);
M
Matthias Bolte 已提交
2525
                goto cleanup;
2526 2527 2528 2529
            }
        }
    } else if (STRCASEEQ(addressType, "static")) {
        if (address != NULL) {
2530
            if (virMacAddrParse(address, &(*def)->mac) < 0) {
2531 2532 2533
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be MAC address but "
                                 "found '%s'"), address_name, address);
M
Matthias Bolte 已提交
2534
                goto cleanup;
2535 2536 2537
            }
        }
    } else {
2538 2539 2540
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry '%s' to be 'generated' or 'static' or "
                         "'vpx' but found '%s'"), addressType_name, addressType);
M
Matthias Bolte 已提交
2541
        goto cleanup;
2542 2543
    }

2544
    /* vmx:virtualDev, vmx:features -> def:model */
2545 2546
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev, true) < 0 ||
        virVMXGetConfigLong(conf, features_name, &features, 0, true) < 0) {
M
Matthias Bolte 已提交
2547
        goto cleanup;
2548 2549
    }

2550 2551 2552 2553 2554
    if (virtualDev != NULL) {
        if (STRCASENEQ(virtualDev, "vlance") &&
            STRCASENEQ(virtualDev, "vmxnet") &&
            STRCASENEQ(virtualDev, "vmxnet3") &&
            STRCASENEQ(virtualDev, "e1000")) {
2555 2556 2557 2558
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'vlance' or 'vmxnet' or "
                             "'vmxnet3' or 'e1000' but found '%s'"), virtualDev_name,
                           virtualDev);
M
Matthias Bolte 已提交
2559
            goto cleanup;
2560 2561 2562 2563 2564 2565 2566 2567 2568
        }

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

            virtualDev = strdup("vmxnet2");

            if (virtualDev == NULL) {
                virReportOOMError();
M
Matthias Bolte 已提交
2569
                goto cleanup;
2570 2571
            }
        }
2572 2573
    }

M
Matthias Bolte 已提交
2574
    /* vmx:networkName -> def:data.bridge.brname */
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
    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 已提交
2589 2590 2591
    }

    /* vmx:vnet -> def:data.ifname */
2592
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
2593
        virVMXGetConfigString(conf, vnet_name, &vnet, false) < 0) {
M
Matthias Bolte 已提交
2594
        goto cleanup;
2595 2596 2597 2598 2599 2600
    }

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

        virtualDev = NULL;
M
Matthias Bolte 已提交
2604
        networkName = NULL;
2605 2606
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
2607 2608 2609
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No yet handled value '%s' for VMX entry '%s'"),
                       connectionType, connectionType_name);
M
Matthias Bolte 已提交
2610
        goto cleanup;
2611
    } else if (STRCASEEQ(connectionType, "nat")) {
2612 2613 2614 2615
        (*def)->type = VIR_DOMAIN_NET_TYPE_USER;
        (*def)->model = virtualDev;

        virtualDev = NULL;
2616 2617 2618
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
2619 2620
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
2621 2622

        virtualDev = NULL;
M
Matthias Bolte 已提交
2623
        networkName = NULL;
2624 2625
        vnet = NULL;
    } else {
2626 2627 2628
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid value '%s' for VMX entry '%s'"), connectionType,
                       connectionType_name);
M
Matthias Bolte 已提交
2629
        goto cleanup;
2630 2631
    }

M
Matthias Bolte 已提交
2632 2633
    result = 0;

M
Matthias Bolte 已提交
2634
  cleanup:
M
Matthias Bolte 已提交
2635 2636 2637 2638 2639
    if (result < 0) {
        virDomainNetDefFree(*def);
        *def = NULL;
    }

S
Stefan Berger 已提交
2640
    VIR_FREE(networkName);
2641 2642 2643 2644 2645 2646 2647 2648 2649
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;

M
Matthias Bolte 已提交
2650
  ignore:
2651 2652 2653
    virDomainNetDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2654 2655
    result = 0;

2656 2657 2658 2659 2660 2661
    goto cleanup;
}



int
2662 2663
virVMXParseSerial(virVMXContext *ctx, virConfPtr conf, int port,
                  virDomainChrDefPtr *def)
2664
{
M
Matthias Bolte 已提交
2665
    int result = -1;
2666 2667 2668
    char prefix[48] = "";

    char present_name[48] = "";
2669
    bool present = false;
2670 2671

    char startConnected_name[48] = "";
2672
    bool startConnected = false;
2673 2674 2675 2676 2677 2678 2679

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

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

2680 2681 2682
    char network_endPoint_name[48] = "";
    char *network_endPoint = NULL;

M
Martin Kletzander 已提交
2683
    virURIPtr parsedUri = NULL;
2684

2685
    if (def == NULL || *def != NULL) {
2686
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2687 2688 2689 2690
        return -1;
    }

    if (port < 0 || port > 3) {
2691 2692
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Serial port index %d out of [0..3] range"), port);
2693
        return -1;
2694 2695 2696
    }

    if (VIR_ALLOC(*def) < 0) {
2697
        virReportOOMError();
M
Matthias Bolte 已提交
2698
        return -1;
2699 2700
    }

2701
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
2702

2703
    snprintf(prefix, sizeof(prefix), "serial%d", port);
2704

2705 2706 2707 2708 2709
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
    VMX_BUILD_NAME_EXTRA(network_endPoint, "network.endPoint");
2710 2711

    /* vmx:present */
2712
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2713
        goto cleanup;
2714 2715 2716
    }

    /* vmx:startConnected */
2717 2718
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2719
        goto cleanup;
2720 2721 2722 2723 2724 2725 2726 2727
    }

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

    /* vmx:fileType -> def:type */
2728
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2729
        goto cleanup;
2730 2731 2732
    }

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

2737
    /* vmx:network.endPoint -> def:data.tcp.listen */
2738 2739
    if (virVMXGetConfigString(conf, network_endPoint_name, &network_endPoint,
                              true) < 0) {
2740 2741 2742
        goto cleanup;
    }

2743 2744
    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2745
        (*def)->target.port = port;
2746 2747
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2748 2749 2750

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2751
        (*def)->target.port = port;
2752 2753 2754
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2755

2756
        if ((*def)->source.data.file.path == NULL) {
M
Matthias Bolte 已提交
2757
            goto cleanup;
M
Matthias Bolte 已提交
2758
        }
2759
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
2760 2761 2762 2763
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
2764
        (*def)->target.port = port;
2765 2766
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->source.data.file.path = fileName;
M
Matthias Bolte 已提交
2767 2768

        fileName = NULL;
2769 2770
    } else if (STRCASEEQ(fileType, "network")) {
        (*def)->target.port = port;
2771
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_TCP;
2772

2773
        if (!(parsedUri = virURIParse(fileName)))
2774 2775 2776
            goto cleanup;

        if (parsedUri->port == 0) {
2777 2778 2779
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' doesn't contain a port part"),
                           fileName_name);
2780 2781 2782
            goto cleanup;
        }

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

2785
        if ((*def)->source.data.tcp.host == NULL) {
2786 2787 2788 2789
            virReportOOMError();
            goto cleanup;
        }

2790 2791
        if (virAsprintf(&(*def)->source.data.tcp.service, "%d",
                        parsedUri->port) < 0) {
2792 2793 2794 2795 2796 2797 2798 2799 2800
            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")) {
2801
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW;
2802
        } else if (STRCASEEQ(parsedUri->scheme, "telnet")) {
2803 2804
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
2805
        } else if (STRCASEEQ(parsedUri->scheme, "telnets")) {
2806 2807
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNETS;
2808 2809 2810 2811
        } else if (STRCASEEQ(parsedUri->scheme, "ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp4+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp6+ssl")) {
2812
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TLS;
2813
        } else {
2814 2815 2816
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' contains unsupported scheme '%s'"),
                           fileName_name, parsedUri->scheme);
2817 2818 2819 2820
            goto cleanup;
        }

        if (network_endPoint == NULL || STRCASEEQ(network_endPoint, "server")) {
2821
            (*def)->source.data.tcp.listen = true;
2822
        } else if (STRCASEEQ(network_endPoint, "client")) {
2823
            (*def)->source.data.tcp.listen = false;
2824
        } else {
2825 2826 2827
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'server' or 'client' "
                             "but found '%s'"), network_endPoint_name, network_endPoint);
2828 2829
            goto cleanup;
        }
2830
    } else {
2831 2832 2833
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry '%s' to be 'device', 'file' or 'pipe' "
                         "or 'network' but found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2834
        goto cleanup;
2835 2836
    }

M
Matthias Bolte 已提交
2837 2838
    result = 0;

M
Matthias Bolte 已提交
2839
  cleanup:
M
Matthias Bolte 已提交
2840 2841 2842 2843 2844
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2845 2846
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2847
    VIR_FREE(network_endPoint);
2848
    virURIFree(parsedUri);
2849 2850 2851

    return result;

M
Matthias Bolte 已提交
2852
  ignore:
2853 2854 2855
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2856 2857
    result = 0;

2858 2859 2860 2861 2862 2863
    goto cleanup;
}



int
2864 2865
virVMXParseParallel(virVMXContext *ctx, virConfPtr conf, int port,
                    virDomainChrDefPtr *def)
2866
{
M
Matthias Bolte 已提交
2867
    int result = -1;
2868 2869 2870
    char prefix[48] = "";

    char present_name[48] = "";
2871
    bool present = false;
2872 2873

    char startConnected_name[48] = "";
2874
    bool startConnected = false;
2875 2876 2877 2878 2879 2880 2881 2882

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

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

    if (def == NULL || *def != NULL) {
2883
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2884 2885 2886 2887
        return -1;
    }

    if (port < 0 || port > 2) {
2888 2889
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"), port);
2890
        return -1;
2891 2892 2893
    }

    if (VIR_ALLOC(*def) < 0) {
2894
        virReportOOMError();
M
Matthias Bolte 已提交
2895
        return -1;
2896 2897
    }

2898
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
2899

2900
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2901

2902 2903 2904 2905
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
2906 2907

    /* vmx:present */
2908
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2909
        goto cleanup;
2910 2911 2912
    }

    /* vmx:startConnected */
2913 2914
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2915
        goto cleanup;
2916 2917 2918 2919 2920 2921 2922 2923
    }

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

    /* vmx:fileType -> def:type */
2924
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2925
        goto cleanup;
2926 2927 2928
    }

    /* vmx:fileName -> def:data.file.path */
2929
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2930
        goto cleanup;
2931 2932 2933 2934
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2935
        (*def)->target.port = port;
2936 2937
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2938 2939 2940

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2941
        (*def)->target.port = port;
2942 2943 2944
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2945

2946
        if ((*def)->source.data.file.path == NULL) {
M
Matthias Bolte 已提交
2947
            goto cleanup;
M
Matthias Bolte 已提交
2948
        }
2949
    } else {
2950 2951 2952
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                         "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2953
        goto cleanup;
2954 2955
    }

M
Matthias Bolte 已提交
2956 2957
    result = 0;

M
Matthias Bolte 已提交
2958
  cleanup:
M
Matthias Bolte 已提交
2959 2960 2961 2962 2963
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2964 2965 2966 2967 2968
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2969
  ignore:
2970 2971 2972
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2973 2974
    result = 0;

2975 2976
    goto cleanup;
}
M
Matthias Bolte 已提交
2977 2978 2979



2980 2981 2982 2983 2984 2985 2986
int
virVMXParseSVGA(virConfPtr conf, virDomainVideoDefPtr *def)
{
    int result = -1;
    long long svga_vramSize = 0;

    if (def == NULL || *def != NULL) {
2987
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
        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;
    }

3004
    (*def)->vram = VIR_DIV_UP(svga_vramSize, 1024); /* Scale from bytes to kilobytes */
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018

    result = 0;

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

    return result;
}



M
Matthias Bolte 已提交
3019 3020 3021 3022 3023
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Domain XML -> VMX
 */

char *
3024 3025
virVMXFormatConfig(virVMXContext *ctx, virCapsPtr caps, virDomainDefPtr def,
                   int virtualHW_version)
M
Matthias Bolte 已提交
3026
{
3027
    char *vmx = NULL;
M
Matthias Bolte 已提交
3028 3029 3030 3031
    int i;
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
3032 3033
    char *preliminaryDisplayName = NULL;
    char *displayName = NULL;
3034
    char *annotation = NULL;
3035
    unsigned long long max_balloon;
3036 3037
    bool scsi_present[4] = { false, false, false, false };
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
3038
    bool floppy_present[2] = { false, false };
M
Matthias Bolte 已提交
3039

3040
    if (ctx->formatFileName == NULL) {
3041 3042
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no formatFileName function set"));
3043 3044 3045
        return NULL;
    }

M
Matthias Bolte 已提交
3046 3047
    memset(zero, 0, VIR_UUID_BUFLEN);

3048
    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) {
3049 3050 3051 3052
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting virt type to be '%s' but found '%s'"),
                       virDomainVirtTypeToString(VIR_DOMAIN_VIRT_VMWARE),
                       virDomainVirtTypeToString(def->virtType));
M
Matthias Bolte 已提交
3053 3054 3055
        return NULL;
    }

3056 3057 3058
    /* vmx:.encoding */
    virBufferAddLit(&buffer, ".encoding = \"UTF-8\"\n");

M
Matthias Bolte 已提交
3059 3060 3061 3062
    /* vmx:config.version */
    virBufferAddLit(&buffer, "config.version = \"8\"\n");

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

M
Matthias Bolte 已提交
3066
    /* def:os.arch -> vmx:guestOS */
3067
    if (def->os.arch == VIR_ARCH_I686) {
M
Matthias Bolte 已提交
3068
        virBufferAddLit(&buffer, "guestOS = \"other\"\n");
3069
    } else if (def->os.arch == VIR_ARCH_X86_64) {
M
Matthias Bolte 已提交
3070 3071
        virBufferAddLit(&buffer, "guestOS = \"other-64\"\n");
    } else {
3072 3073
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting domain XML attribute 'arch' of entry 'os/type' "
3074 3075
                         "to be 'i686' or 'x86_64' but found '%s'"),
                       virArchToString(def->os.arch));
3076
        goto cleanup;
M
Matthias Bolte 已提交
3077 3078
    }

M
Matthias Bolte 已提交
3079 3080 3081 3082 3083 3084 3085
    /* 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 {
3086 3087 3088
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported SMBIOS mode '%s'"),
                       virDomainSmbiosModeTypeToString(def->os.smbios_mode));
M
Matthias Bolte 已提交
3089 3090 3091
        goto cleanup;
    }

M
Matthias Bolte 已提交
3092 3093 3094 3095
    /* def:uuid -> vmx:uuid.action, vmx:uuid.bios */
    if (memcmp(def->uuid, zero, VIR_UUID_BUFLEN) == 0) {
        virBufferAddLit(&buffer, "uuid.action = \"create\"\n");
    } else {
3096
        virBufferAsprintf(&buffer, "uuid.bios = \"%02x %02x %02x %02x %02x %02x "
M
Matthias Bolte 已提交
3097 3098 3099 3100 3101 3102 3103 3104 3105
                          "%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 */
3106
    preliminaryDisplayName = virVMXEscapeHexPipe(def->name);
3107

3108 3109 3110
    if (preliminaryDisplayName == NULL) {
        goto cleanup;
    }
3111

3112
    displayName = virVMXEscapeHexPercent(preliminaryDisplayName);
3113

3114 3115 3116
    if (displayName == NULL) {
        goto cleanup;
    }
3117

3118
    virBufferAsprintf(&buffer, "displayName = \"%s\"\n", displayName);
3119

3120 3121
    /* def:description -> vmx:annotation */
    if (def->description != NULL) {
3122
        annotation = virVMXEscapeHexPipe(def->description);
3123

3124
        virBufferAsprintf(&buffer, "annotation = \"%s\"\n", annotation);
3125 3126
    }

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

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

3134
    /* def:mem.cur_balloon -> vmx:sched.mem.max */
3135
    if (def->mem.cur_balloon < max_balloon) {
3136
        virBufferAsprintf(&buffer, "sched.mem.max = \"%llu\"\n",
3137 3138
                          VIR_DIV_UP(def->mem.cur_balloon,
                                     1024)); /* Scale from kilobytes to megabytes */
M
Matthias Bolte 已提交
3139 3140
    }

3141 3142
    /* def:mem.min_guarantee -> vmx:sched.mem.minsize */
    if (def->mem.min_guarantee > 0) {
3143
        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%llu\"\n",
3144 3145
                          VIR_DIV_UP(def->mem.min_guarantee,
                                     1024)); /* Scale from kilobytes to megabytes */
3146 3147
    }

E
Eric Blake 已提交
3148 3149
    /* def:maxvcpus -> vmx:numvcpus */
    if (def->vcpus != def->maxvcpus) {
3150 3151 3152
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("No support for domain XML entry 'vcpu' attribute "
                         "'current'"));
E
Eric Blake 已提交
3153 3154 3155
        goto cleanup;
    }
    if (def->maxvcpus <= 0 || (def->maxvcpus % 2 != 0 && def->maxvcpus != 1)) {
3156 3157 3158 3159
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting domain XML entry 'vcpu' to be an unsigned "
                         "integer (1 or a multiple of 2) but found %d"),
                       def->maxvcpus);
3160
        goto cleanup;
M
Matthias Bolte 已提交
3161 3162
    }

3163
    virBufferAsprintf(&buffer, "numvcpus = \"%d\"\n", def->maxvcpus);
M
Matthias Bolte 已提交
3164 3165

    /* def:cpumask -> vmx:sched.cpu.affinity */
H
Hu Tao 已提交
3166
    if (def->cpumask && virBitmapSize(def->cpumask) > 0) {
M
Matthias Bolte 已提交
3167 3168 3169 3170
        virBufferAddLit(&buffer, "sched.cpu.affinity = \"");

        sched_cpu_affinity_length = 0;

H
Hu Tao 已提交
3171 3172 3173
        i = -1;
        while ((i = virBitmapNextSetBit(def->cpumask, i)) >= 0) {
            ++sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3174 3175
        }

E
Eric Blake 已提交
3176
        if (sched_cpu_affinity_length < def->maxvcpus) {
3177 3178 3179 3180
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML attribute 'cpuset' of entry "
                             "'vcpu' to contain at least %d CPU(s)"),
                           def->maxvcpus);
3181
            goto cleanup;
M
Matthias Bolte 已提交
3182 3183
        }

H
Hu Tao 已提交
3184 3185 3186
        i = -1;
        while ((i = virBitmapNextSetBit(def->cpumask, i)) >= 0) {
            virBufferAsprintf(&buffer, "%d", i);
M
Matthias Bolte 已提交
3187

H
Hu Tao 已提交
3188 3189
            if (sched_cpu_affinity_length > 1) {
                virBufferAddChar(&buffer, ',');
M
Matthias Bolte 已提交
3190
            }
H
Hu Tao 已提交
3191 3192

            --sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3193 3194 3195 3196 3197
        }

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

3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
    /* 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 {
3208
            virBufferAsprintf(&buffer, "sched.cpu.shares = \"%lu\"\n",
3209 3210 3211 3212
                              def->cputune.shares);
        }
    }

M
Matthias Bolte 已提交
3213 3214 3215 3216
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
3217
            if (virVMXFormatVNC(def->graphics[i], &buffer) < 0) {
3218
                goto cleanup;
M
Matthias Bolte 已提交
3219 3220 3221 3222 3223
            }

            break;

          default:
3224 3225 3226
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported graphics type '%s'"),
                           virDomainGraphicsTypeToString(def->graphics[i]->type));
3227
            goto cleanup;
M
Matthias Bolte 已提交
3228 3229 3230
        }
    }

M
Matthias Bolte 已提交
3231
    /* def:disks */
3232
    for (i = 0; i < def->ndisks; ++i) {
3233 3234
        if (virVMXVerifyDiskAddress(caps, def->disks[i]) < 0 ||
            virVMXHandleLegacySCSIDiskDriverName(def, def->disks[i]) < 0) {
3235
            goto cleanup;
3236 3237
        }
    }
M
Matthias Bolte 已提交
3238

3239 3240
    if (virVMXGatherSCSIControllers(ctx, def, scsi_virtualDev,
                                    scsi_present) < 0) {
3241
        goto cleanup;
M
Matthias Bolte 已提交
3242 3243 3244 3245
    }

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

3248
            if (scsi_virtualDev[i] != -1) {
3249
                virBufferAsprintf(&buffer, "scsi%d.virtualDev = \"%s\"\n", i,
3250
                                  virVMXControllerModelSCSITypeToString
3251
                                    (scsi_virtualDev[i]));
M
Matthias Bolte 已提交
3252 3253 3254 3255 3256 3257 3258
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
3259
            if (virVMXFormatHardDisk(ctx, def->disks[i], &buffer) < 0) {
3260
                goto cleanup;
M
Matthias Bolte 已提交
3261 3262 3263 3264 3265
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_CDROM:
3266
            if (virVMXFormatCDROM(ctx, def->disks[i], &buffer) < 0) {
3267
                goto cleanup;
M
Matthias Bolte 已提交
3268 3269 3270 3271 3272
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
3273 3274
            if (virVMXFormatFloppy(ctx, def->disks[i], &buffer,
                                   floppy_present) < 0) {
3275
                goto cleanup;
M
Matthias Bolte 已提交
3276 3277 3278 3279 3280
            }

            break;

          default:
3281 3282 3283
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported disk device type '%s'"),
                           virDomainDiskDeviceTypeToString(def->disks[i]->device));
3284
            goto cleanup;
M
Matthias Bolte 已提交
3285 3286 3287
        }
    }

3288 3289 3290
    for (i = 0; i < 2; ++i) {
        /* floppy[0..1].present defaults to true, disable it explicitly */
        if (! floppy_present[i]) {
3291
            virBufferAsprintf(&buffer, "floppy%d.present = \"false\"\n", i);
3292 3293 3294
        }
    }

M
Matthias Bolte 已提交
3295
    /* def:fss */
3296 3297
    if (def->nfss > 0) {
        virBufferAddLit(&buffer, "isolation.tools.hgfs.disable = \"false\"\n");
3298
        virBufferAsprintf(&buffer, "sharedFolder.maxNum = \"%zu\"\n", def->nfss);
3299 3300 3301 3302 3303 3304 3305
    }

    for (i = 0; i < def->nfss; ++i) {
        if (virVMXFormatFileSystem(def->fss[i], i, &buffer) < 0) {
            goto cleanup;
        }
    }
M
Matthias Bolte 已提交
3306 3307 3308

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
3309
        if (virVMXFormatEthernet(def->nets[i], i, &buffer) < 0) {
3310
            goto cleanup;
M
Matthias Bolte 已提交
3311 3312 3313 3314 3315 3316 3317 3318 3319
        }
    }

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

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

3320 3321 3322
    /* def:videos */
    if (def->nvideos > 0) {
        if (def->nvideos > 1) {
3323 3324
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("No support for multiple video devices"));
3325 3326 3327 3328 3329 3330 3331 3332
            goto cleanup;
        }

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

M
Matthias Bolte 已提交
3333 3334 3335 3336 3337
    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
3338
        if (virVMXFormatSerial(ctx, def->serials[i], &buffer) < 0) {
3339
            goto cleanup;
M
Matthias Bolte 已提交
3340 3341 3342 3343 3344
        }
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
3345
        if (virVMXFormatParallel(ctx, def->parallels[i], &buffer) < 0) {
3346
            goto cleanup;
M
Matthias Bolte 已提交
3347 3348 3349 3350 3351
        }
    }

    /* Get final VMX output */
    if (virBufferError(&buffer)) {
3352
        virReportOOMError();
3353
        goto cleanup;
M
Matthias Bolte 已提交
3354 3355
    }

3356
    vmx = virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
3357

3358 3359 3360 3361 3362
  cleanup:
    if (vmx == NULL) {
        virBufferFreeAndReset(&buffer);
    }

3363 3364
    VIR_FREE(preliminaryDisplayName);
    VIR_FREE(displayName);
3365
    VIR_FREE(annotation);
M
Matthias Bolte 已提交
3366

3367
    return vmx;
M
Matthias Bolte 已提交
3368 3369 3370 3371
}



M
Matthias Bolte 已提交
3372
int
3373
virVMXFormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
3374
{
3375 3376
    const char *listenAddr;

M
Matthias Bolte 已提交
3377
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
3378
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3379 3380 3381
        return -1;
    }

3382
    virBufferAsprintf(buffer, "RemoteDisplay.vnc.enabled = \"true\"\n");
M
Matthias Bolte 已提交
3383 3384

    if (def->data.vnc.autoport) {
3385
        VIR_WARN("VNC autoport is enabled, but the automatically assigned "
M
Matthias Bolte 已提交
3386 3387 3388 3389 3390 3391 3392
                  "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);
        }

3393
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.port = \"%d\"\n",
M
Matthias Bolte 已提交
3394 3395 3396
                          def->data.vnc.port);
    }

3397
    if ((listenAddr = virDomainGraphicsListenGetAddress(def, 0))) {
3398
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.ip = \"%s\"\n",
3399
                          listenAddr);
M
Matthias Bolte 已提交
3400 3401 3402
    }

    if (def->data.vnc.keymap != NULL) {
3403
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.keymap = \"%s\"\n",
M
Matthias Bolte 已提交
3404 3405 3406
                          def->data.vnc.keymap);
    }

3407
    if (def->data.vnc.auth.passwd != NULL) {
3408
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.password = \"%s\"\n",
3409
                          def->data.vnc.auth.passwd);
M
Matthias Bolte 已提交
3410 3411 3412 3413 3414 3415 3416
    }

    return 0;
}



M
Matthias Bolte 已提交
3417
int
3418 3419
virVMXFormatHardDisk(virVMXContext *ctx, virDomainDiskDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3420
{
3421
    int controllerOrBus, unit;
M
Matthias Bolte 已提交
3422 3423 3424
    const char *busName = NULL;
    const char *entryPrefix = NULL;
    const char *deviceTypePrefix = NULL;
M
Matthias Bolte 已提交
3425
    char *fileName = NULL;
M
Matthias Bolte 已提交
3426 3427

    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
3428
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3429 3430 3431 3432 3433 3434 3435 3436
        return -1;
    }

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

3437 3438
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3439 3440 3441 3442 3443 3444 3445
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";
        deviceTypePrefix = "ata";

3446
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
3447
                                           &unit) < 0) {
M
Matthias Bolte 已提交
3448 3449 3450
            return -1;
        }
    } else {
3451 3452 3453
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported bus type '%s' for harddisk"),
                       virDomainDiskBusTypeToString(def->bus));
M
Matthias Bolte 已提交
3454 3455 3456 3457
        return -1;
    }

    if (def->type != VIR_DOMAIN_DISK_TYPE_FILE) {
3458 3459 3460 3461
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("%s harddisk '%s' has unsupported type '%s', expecting '%s'"),
                       busName, def->dst, virDomainDiskTypeToString(def->type),
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE));
M
Matthias Bolte 已提交
3462 3463 3464
        return -1;
    }

3465
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3466
                      entryPrefix, controllerOrBus, unit);
3467
    virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"%s-hardDisk\"\n",
3468
                      entryPrefix, controllerOrBus, unit, deviceTypePrefix);
M
Matthias Bolte 已提交
3469 3470

    if (def->src != NULL) {
3471
        if (! virFileHasSuffix(def->src, ".vmdk")) {
3472 3473 3474
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Image file for %s harddisk '%s' has unsupported suffix, "
                             "expecting '.vmdk'"), busName, def->dst);
M
Matthias Bolte 已提交
3475 3476 3477
            return -1;
        }

3478
        fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3479 3480 3481 3482 3483

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

3484
        virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3485
                          entryPrefix, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3486 3487

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3488 3489 3490 3491
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
3492
            virBufferAsprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
3493
                              entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3494
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
3495 3496 3497 3498
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("%s harddisk '%s' has unsupported cache mode '%s'"),
                           busName, def->dst,
                           virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
3499
            return -1;
M
Matthias Bolte 已提交
3500 3501 3502 3503 3504 3505 3506 3507 3508
        }
    }

    return 0;
}



int
3509 3510
virVMXFormatCDROM(virVMXContext *ctx, virDomainDiskDefPtr def,
                  virBufferPtr buffer)
M
Matthias Bolte 已提交
3511
{
3512
    int controllerOrBus, unit;
M
Matthias Bolte 已提交
3513 3514
    const char *busName = NULL;
    const char *entryPrefix = NULL;
M
Matthias Bolte 已提交
3515
    char *fileName = NULL;
M
Matthias Bolte 已提交
3516 3517

    if (def->device != VIR_DOMAIN_DISK_DEVICE_CDROM) {
3518
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3519 3520 3521 3522 3523 3524 3525
        return -1;
    }

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

3526 3527
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3528 3529 3530 3531 3532 3533
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        busName = "IDE";
        entryPrefix = "ide";

3534 3535
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
                                          &unit) < 0) {
M
Matthias Bolte 已提交
3536 3537 3538
            return -1;
        }
    } else {
3539 3540 3541
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported bus type '%s' for cdrom"),
                       virDomainDiskBusTypeToString(def->bus));
M
Matthias Bolte 已提交
3542 3543 3544
        return -1;
    }

3545
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3546
                      entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3547 3548

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

        if (def->src != NULL) {
3553
            if (! virFileHasSuffix(def->src, ".iso")) {
3554 3555 3556
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Image file for %s cdrom '%s' has unsupported "
                                 "suffix, expecting '.iso'"), busName, def->dst);
M
Matthias Bolte 已提交
3557 3558 3559
                return -1;
            }

3560
            fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3561 3562 3563 3564 3565

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

3566
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3567
                              entryPrefix, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3568 3569 3570

            VIR_FREE(fileName);
        }
M
Matthias Bolte 已提交
3571
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
3572
        virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"atapi-cdrom\"\n",
3573
                          entryPrefix, controllerOrBus, unit);
M
Matthias Bolte 已提交
3574 3575

        if (def->src != NULL) {
3576
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3577
                              entryPrefix, controllerOrBus, unit, def->src);
M
Matthias Bolte 已提交
3578
        }
M
Matthias Bolte 已提交
3579
    } else {
3580 3581 3582 3583 3584 3585
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("%s cdrom '%s' has unsupported type '%s', expecting '%s' "
                         "or '%s'"), busName, def->dst,
                       virDomainDiskTypeToString(def->type),
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
M
Matthias Bolte 已提交
3586 3587 3588 3589 3590 3591 3592 3593 3594
        return -1;
    }

    return 0;
}



int
3595 3596
virVMXFormatFloppy(virVMXContext *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer, bool floppy_present[2])
M
Matthias Bolte 已提交
3597
{
3598
    int unit;
M
Matthias Bolte 已提交
3599
    char *fileName = NULL;
M
Matthias Bolte 已提交
3600 3601

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
3602
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3603 3604 3605
        return -1;
    }

3606
    if (virVMXFloppyDiskNameToUnit(def->dst, &unit) < 0) {
M
Matthias Bolte 已提交
3607 3608 3609
        return -1;
    }

3610 3611
    floppy_present[unit] = true;

3612
    virBufferAsprintf(buffer, "floppy%d.present = \"true\"\n", unit);
M
Matthias Bolte 已提交
3613 3614

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

        if (def->src != NULL) {
3618
            if (! virFileHasSuffix(def->src, ".flp")) {
3619 3620 3621
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Image file for floppy '%s' has unsupported "
                                 "suffix, expecting '.flp'"), def->dst);
M
Matthias Bolte 已提交
3622 3623 3624
                return -1;
            }

3625
            fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3626 3627 3628 3629 3630

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

3631
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3632
                              unit, fileName);
M
Matthias Bolte 已提交
3633 3634 3635

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

        if (def->src != NULL) {
3640
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3641
                              unit, def->src);
M
Matthias Bolte 已提交
3642
        }
M
Matthias Bolte 已提交
3643
    } else {
3644 3645 3646 3647 3648 3649
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Floppy '%s' has unsupported type '%s', expecting '%s' "
                         "or '%s'"), def->dst,
                       virDomainDiskTypeToString(def->type),
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
M
Matthias Bolte 已提交
3650 3651 3652 3653 3654 3655 3656 3657
        return -1;
    }

    return 0;
}



3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
int
virVMXFormatFileSystem(virDomainFSDefPtr def, int number, virBufferPtr buffer)
{
    if (def->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Only '%s' filesystem type is supported"),
                       virDomainFSTypeToString(VIR_DOMAIN_FS_TYPE_MOUNT));
        return -1;
    }

    virBufferAsprintf(buffer, "sharedFolder%d.present = \"true\"\n", number);
    virBufferAsprintf(buffer, "sharedFolder%d.enabled = \"true\"\n", number);
    virBufferAsprintf(buffer, "sharedFolder%d.readAccess = \"true\"\n", number);
    virBufferAsprintf(buffer, "sharedFolder%d.writeAccess = \"%s\"\n", number,
                      def->readonly ? "false" : "true");
    virBufferAsprintf(buffer, "sharedFolder%d.hostPath = \"%s\"\n", number,
                      def->src);
    virBufferAsprintf(buffer, "sharedFolder%d.guestName = \"%s\"\n", number,
                      def->dst);

    return 0;
}



M
Matthias Bolte 已提交
3683
int
3684 3685
virVMXFormatEthernet(virDomainNetDefPtr def, int controller,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3686 3687
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
3688
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
3689 3690

    if (controller < 0 || controller > 3) {
3691 3692 3693
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Ethernet controller index %d out of [0..3] range"),
                       controller);
M
Matthias Bolte 已提交
3694 3695 3696
        return -1;
    }

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

3699
    /* def:model -> vmx:virtualDev, vmx:features */
M
Matthias Bolte 已提交
3700 3701 3702
    if (def->model != NULL) {
        if (STRCASENEQ(def->model, "vlance") &&
            STRCASENEQ(def->model, "vmxnet") &&
3703
            STRCASENEQ(def->model, "vmxnet2") &&
3704
            STRCASENEQ(def->model, "vmxnet3") &&
M
Matthias Bolte 已提交
3705
            STRCASENEQ(def->model, "e1000")) {
3706 3707 3708 3709
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML entry 'devices/interface/model' "
                             "to be 'vlance' or 'vmxnet' or 'vmxnet2' or 'vmxnet3' "
                             "or 'e1000' but found '%s'"), def->model);
M
Matthias Bolte 已提交
3710 3711 3712
            return -1;
        }

3713
        if (STRCASEEQ(def->model, "vmxnet2")) {
3714
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"vmxnet\"\n",
3715
                              controller);
3716
            virBufferAsprintf(buffer, "ethernet%d.features = \"15\"\n",
3717 3718
                              controller);
        } else {
3719
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
3720 3721
                              controller, def->model);
        }
M
Matthias Bolte 已提交
3722 3723 3724 3725 3726
    }

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

        if (def->ifname != NULL) {
3732
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"custom\"\n",
M
Matthias Bolte 已提交
3733
                              controller);
3734
            virBufferAsprintf(buffer, "ethernet%d.vnet = \"%s\"\n",
M
Matthias Bolte 已提交
3735 3736
                              controller, def->ifname);
        } else {
3737
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"bridged\"\n",
M
Matthias Bolte 已提交
3738 3739 3740 3741 3742
                              controller);
        }

        break;

3743 3744 3745 3746 3747
      case VIR_DOMAIN_NET_TYPE_USER:
        virBufferAsprintf(buffer, "ethernet%d.connectionType = \"nat\"\n",
                          controller);
        break;

M
Matthias Bolte 已提交
3748
      default:
3749 3750
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported net type '%s'"),
                       virDomainNetTypeToString(def->type));
M
Matthias Bolte 已提交
3751 3752 3753
        return -1;
    }

3754
    /* def:mac -> vmx:addressType, vmx:(generated)Address, vmx:checkMACAddress */
3755
    virMacAddrFormat(&def->mac, mac_string);
M
Matthias Bolte 已提交
3756

3757 3758
    prefix = (def->mac.addr[0] << 16) | (def->mac.addr[1] << 8) | def->mac.addr[2];
    suffix = (def->mac.addr[3] << 16) | (def->mac.addr[4] << 8) | def->mac.addr[5];
3759 3760

    if (prefix == 0x000c29) {
3761
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
M
Matthias Bolte 已提交
3762
                          controller);
3763
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
M
Matthias Bolte 已提交
3764
                          controller, mac_string);
3765
        virBufferAsprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
M
Matthias Bolte 已提交
3766
                          controller);
3767
    } else if (prefix == 0x005056 && suffix <= 0x3fffff) {
3768
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3769
                          controller);
3770
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3771 3772
                          controller, mac_string);
    } else if (prefix == 0x005056 && suffix >= 0x800000 && suffix <= 0xbfffff) {
3773
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"vpx\"\n",
3774
                          controller);
3775
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
3776
                          controller, mac_string);
M
Matthias Bolte 已提交
3777
    } else {
3778
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3779
                          controller);
3780
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3781
                          controller, mac_string);
3782
        virBufferAsprintf(buffer, "ethernet%d.checkMACAddress = \"false\"\n",
3783
                          controller);
M
Matthias Bolte 已提交
3784 3785 3786 3787 3788 3789 3790 3791
    }

    return 0;
}



int
3792 3793
virVMXFormatSerial(virVMXContext *ctx, virDomainChrDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
3794
{
M
Matthias Bolte 已提交
3795
    char *fileName = NULL;
3796
    const char *protocol;
M
Matthias Bolte 已提交
3797

3798
    if (def->target.port < 0 || def->target.port > 3) {
3799 3800 3801
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Serial port index %d out of [0..3] range"),
                       def->target.port);
M
Matthias Bolte 已提交
3802 3803 3804
        return -1;
    }

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

M
Matthias Bolte 已提交
3807
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3808
    switch (def->source.type) {
M
Matthias Bolte 已提交
3809
      case VIR_DOMAIN_CHR_TYPE_DEV:
3810
        virBufferAsprintf(buffer, "serial%d.fileType = \"device\"\n",
3811
                          def->target.port);
3812
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3813
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3814 3815 3816
        break;

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

3820
        fileName = ctx->formatFileName(def->source.data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3821 3822 3823 3824 3825

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

3826
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3827
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3828 3829

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3830 3831 3832
        break;

      case VIR_DOMAIN_CHR_TYPE_PIPE:
3833
        virBufferAsprintf(buffer, "serial%d.fileType = \"pipe\"\n",
3834
                          def->target.port);
M
Matthias Bolte 已提交
3835
        /* FIXME: Based on VI Client GUI default */
3836
        virBufferAsprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
3837
                          def->target.port);
M
Matthias Bolte 已提交
3838
        /* FIXME: Based on VI Client GUI default */
3839
        virBufferAsprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
3840
                          def->target.port);
3841
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3842
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3843 3844
        break;

3845
      case VIR_DOMAIN_CHR_TYPE_TCP:
3846
        switch (def->source.data.tcp.protocol) {
3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863
          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:
3864 3865 3866 3867
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported character device TCP protocol '%s'"),
                           virDomainChrTcpProtocolTypeToString(
                               def->source.data.tcp.protocol));
3868 3869 3870
            return -1;
        }

3871
        virBufferAsprintf(buffer, "serial%d.fileType = \"network\"\n",
3872
                          def->target.port);
3873
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s://%s:%s\"\n",
3874 3875
                          def->target.port, protocol, def->source.data.tcp.host,
                          def->source.data.tcp.service);
3876
        virBufferAsprintf(buffer, "serial%d.network.endPoint = \"%s\"\n",
3877
                          def->target.port,
3878
                          def->source.data.tcp.listen ? "server" : "client");
3879 3880
        break;

M
Matthias Bolte 已提交
3881
      default:
3882 3883 3884
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
                       virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3885 3886 3887 3888 3889
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
3890
    virBufferAsprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
3891
                      def->target.port);
M
Matthias Bolte 已提交
3892 3893 3894 3895 3896 3897 3898

    return 0;
}



int
3899 3900
virVMXFormatParallel(virVMXContext *ctx, virDomainChrDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3901
{
M
Matthias Bolte 已提交
3902 3903
    char *fileName = NULL;

3904
    if (def->target.port < 0 || def->target.port > 2) {
3905 3906 3907
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"),
                       def->target.port);
M
Matthias Bolte 已提交
3908 3909 3910
        return -1;
    }

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

M
Matthias Bolte 已提交
3914
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3915
    switch (def->source.type) {
M
Matthias Bolte 已提交
3916
      case VIR_DOMAIN_CHR_TYPE_DEV:
3917
        virBufferAsprintf(buffer, "parallel%d.fileType = \"device\"\n",
3918
                          def->target.port);
3919
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3920
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3921 3922 3923
        break;

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

3927
        fileName = ctx->formatFileName(def->source.data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3928 3929 3930 3931 3932

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

3933
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3934
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3935 3936

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3937 3938 3939
        break;

      default:
3940 3941 3942
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
                       virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3943 3944 3945 3946 3947
        return -1;
    }

    return 0;
}
3948 3949 3950 3951 3952 3953



int
virVMXFormatSVGA(virDomainVideoDefPtr def, virBufferPtr buffer)
{
3954 3955
    unsigned long long vram;

3956
    if (def->type != VIR_DOMAIN_VIDEO_TYPE_VMVGA) {
3957 3958 3959
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported video device type '%s'"),
                       virDomainVideoTypeToString(def->type));
3960 3961 3962 3963 3964 3965 3966
        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
     */
3967
    vram = VIR_DIV_UP(def->vram, 64) * 64;
3968 3969

    if (def->heads > 1) {
3970 3971
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Multi-head video devices are unsupported"));
3972 3973 3974
        return -1;
    }

3975
    virBufferAsprintf(buffer, "svga.vramSize = \"%lld\"\n",
3976
                      vram * 1024); /* kilobyte to byte */
3977 3978 3979

    return 0;
}