vmx.c 127.5 KB
Newer Older
1
/*
2
 * vmx.c: VMware VMX parsing/formatting functions
3
 *
4
 * Copyright (C) 2010-2013 Red Hat, Inc.
5
 * Copyright (C) 2009-2010 Matthias Bolte <matthias.bolte@googlemail.com>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */

#include <config.h>

25 26
#include <c-ctype.h>

27
#include "internal.h"
28
#include "virerror.h"
29
#include "virfile.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
#include "virstring.h"
37 38 39 40 41 42 43 44 45

/*

mapping:

domain-xml                        <=>   vmx


                                        config.version = "8"                    # essential
46 47
                                        virtualHW.version = "4"                 # essential for ESX 3.5
                                        virtualHW.version = "7"                 # essential for ESX 4.0
P
Patrice LACHANCE 已提交
48
                                        virtualHW.version = "8"                 # essential for ESX 5.0
49
                                        virtualHW.version = "9"                 # essential for ESX 5.1
50 51 52 53 54 55


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



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

def->os

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



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

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

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


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

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


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

247
                                        floppy0.present = "true"                # defaults to "true"
248 249 250 251 252 253 254 255
                                        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"
256
->dst = fd[<unit> mapped to [a-z]+]
257 258 259 260 261 262 263 264 265
->driverName
->driverType
->cachemode
->readonly
->shared
->slotnum



266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
################################################################################
## 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



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


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


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

312 313 314 315

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

316 317 318 319 320

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

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


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

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


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

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


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

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



356 357 358 359 360 361 362 363 364 365
################################################################################
## video #######################################################################

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



366 367 368 369 370 371 372 373 374
################################################################################
## serials #####################################################################

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

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

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


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


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


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

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


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

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


424 425 426 427 428 429 430 431
## 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>

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

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

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

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


454 455 456 457 458 459 460 461 462 463

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

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

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

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


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


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

495 496
*/

497
#define VIR_FROM_THIS VIR_FROM_NONE
498

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

502 503
#define VMX_BUILD_NAME(_suffix)                                               \
    VMX_BUILD_NAME_EXTRA(_suffix, #_suffix)
504

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


520 521 522 523
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Helpers
 */

524 525
virDomainDefParserConfig virVMXDomainDefParserConfig = {
    .hasWideScsiBus = true,
526
    .macPrefix = {0x00, 0x0c, 0x29},
527 528 529 530 531 532 533 534 535 536
};


virDomainXMLOptionPtr
virVMXDomainXMLConfInit(void)
{
    return virDomainXMLOptionNew(&virVMXDomainDefParserConfig,
                               NULL, NULL);
}

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
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;
    }

555
    if (VIR_ALLOC_N(escaped, length) < 0)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
        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 已提交
583
int
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
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) {
621 622
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxml2 doesn't handle %s encoding"), encoding);
623 624 625 626 627 628 629
        return NULL;
    }

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

    if (xmlCharEncInFunc(handler, utf8, input) < 0) {
630 631
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not convert from %s to UTF-8 encoding"), encoding);
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
        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;
        }

662 663
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing essential config entry '%s'"), name);
664 665 666 667
        return -1;
    }

    if (value->type != VIR_CONF_STRING) {
668 669
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
670 671 672 673 674 675 676 677
        return -1;
    }

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

678 679
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing essential config entry '%s'"), name);
680 681 682
        return -1;
    }

683
    return VIR_STRDUP(*string, value->str);
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
}



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 {
700 701
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
702 703 704 705 706
            return -1;
        }
    }

    if (value->type != VIR_CONF_STRING) {
707 708
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
709 710 711 712 713 714 715
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        } else {
716 717
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
718 719 720 721 722
            return -1;
        }
    }

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

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
757 758
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Missing essential config entry '%s'"), name);
759 760 761 762
                return -1;
            }
        }

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

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
806 807
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Missing essential config entry '%s'"), name);
808 809 810 811 812 813 814 815 816
                return -1;
            }
        }

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

    return 0;
}



static int
virVMXSCSIDiskNameToControllerAndUnit(const char *name, int *controller, int *unit)
M
Matthias Bolte 已提交
835 836 837 838
{
    int idx;

    if (! STRPREFIX(name, "sd")) {
839 840 841
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'sd'"));
M
Matthias Bolte 已提交
842 843 844 845 846 847
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
848 849
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
850 851 852
        return -1;
    }

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

    *controller = idx / 15;
861
    *unit = idx % 15;
M
Matthias Bolte 已提交
862

863 864 865
    /* Skip the controller ifself at unit 7 */
    if (*unit >= 7) {
        ++(*unit);
M
Matthias Bolte 已提交
866 867 868 869 870 871 872
    }

    return 0;
}



873 874
static int
virVMXIDEDiskNameToBusAndUnit(const char *name, int *bus, int *unit)
M
Matthias Bolte 已提交
875 876 877 878
{
    int idx;

    if (! STRPREFIX(name, "hd")) {
879 880 881
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'hd'"));
M
Matthias Bolte 已提交
882 883 884 885 886 887
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
888 889
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
890 891 892
        return -1;
    }

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

900 901
    *bus = idx / 2;
    *unit = idx % 2;
M
Matthias Bolte 已提交
902 903 904 905 906 907

    return 0;
}



908 909
static int
virVMXFloppyDiskNameToUnit(const char *name, int *unit)
M
Matthias Bolte 已提交
910 911 912 913
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
914 915 916
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
917 918 919 920 921 922
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
923 924
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
925 926 927
        return -1;
    }

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

935 936 937 938 939 940 941
    *unit = idx;

    return 0;
}



942
static int
943
virVMXVerifyDiskAddress(virDomainXMLOptionPtr xmlopt, virDomainDiskDefPtr disk)
944 945 946 947 948 949 950
{
    virDomainDiskDef def;
    virDomainDeviceDriveAddressPtr drive;

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

    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
951 952 953
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported disk address type '%s'"),
                       virDomainDeviceAddressTypeToString(disk->info.type));
954 955 956 957 958 959 960 961
        return -1;
    }

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

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

962
    if (virDomainDiskDefAssignAddress(xmlopt, &def) < 0) {
963 964
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not verify disk address"));
965 966 967 968 969 970
        return -1;
    }

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

        if (drive->bus != 0) {
987 988 989
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI bus index %d out of [0] range"),
                           drive->bus);
990 991 992 993
            return -1;
        }

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

        if (drive->bus > 1) {
1008 1009 1010
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE bus index %d out of [0..1] range"),
                           drive->bus);
1011 1012 1013 1014
            return -1;
        }

        if (drive->unit > 1) {
1015 1016 1017
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE unit index %d out of [0..1] range"),
                           drive->unit);
1018 1019 1020 1021
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
        if (drive->controller != 0) {
1022 1023 1024
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC controller index %d out of [0] range"),
                           drive->controller);
1025 1026 1027 1028
            return -1;
        }

        if (drive->bus != 0) {
1029 1030 1031
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC bus index %d out of [0] range"),
                           drive->bus);
1032 1033 1034 1035
            return -1;
        }

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

    return 0;
}



1053 1054 1055
static int
virVMXHandleLegacySCSIDiskDriverName(virDomainDefPtr def,
                                     virDomainDiskDefPtr disk)
1056 1057
{
    char *tmp;
1058 1059
    int model;
    size_t i;
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    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);
    }

1072
    model = virDomainControllerModelSCSITypeFromString(disk->driverName);
1073 1074

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

    if (controller->model == -1) {
        controller->model = model;
    } else if (controller->model != model) {
1097 1098 1099 1100 1101
        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);
1102 1103
        return -1;
    }
M
Matthias Bolte 已提交
1104 1105 1106 1107 1108 1109

    return 0;
}



1110 1111 1112
static int
virVMXGatherSCSIControllers(virVMXContext *ctx, virDomainDefPtr def,
                            int virtualDev[4], bool present[4])
1113 1114
{
    int result = -1;
1115
    size_t i, k;
M
Matthias Bolte 已提交
1116
    virDomainDiskDefPtr disk;
1117 1118 1119 1120
    virDomainControllerDefPtr controller;
    bool controllerHasDisksAttached;
    int count = 0;
    int *autodetectedModels;
M
Matthias Bolte 已提交
1121

1122
    if (VIR_ALLOC_N(autodetectedModels, def->ndisks) < 0)
1123 1124 1125 1126
        return -1;

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

1128
        if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1129
            /* skip non-SCSI controllers */
M
Matthias Bolte 已提交
1130 1131 1132
            continue;
        }

1133
        controllerHasDisksAttached = false;
1134

1135 1136 1137 1138 1139 1140
        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;
1141 1142
                break;
            }
M
Matthias Bolte 已提交
1143 1144
        }

1145
        if (! controllerHasDisksAttached) {
1146
            /* skip SCSI controllers without attached disks */
1147 1148 1149
            continue;
        }

1150
        if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_AUTO &&
1151
            ctx->autodetectSCSIControllerModel != NULL) {
1152 1153
            count = 0;

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

                    ++count;
                }
            }

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

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

1186
        if (controller->model != -1 &&
1187 1188 1189 1190
            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) {
1191 1192 1193 1194 1195
            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));
1196
            goto cleanup;
M
Matthias Bolte 已提交
1197
        }
1198 1199 1200

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

1203 1204 1205 1206 1207 1208
    result = 0;

  cleanup:
    VIR_FREE(autodetectedModels);

    return result;
M
Matthias Bolte 已提交
1209 1210 1211 1212 1213 1214 1215 1216
}



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

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

1247
    if (ctx->parseFileName == NULL) {
1248 1249
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no parseFileName function set"));
1250 1251 1252
        return NULL;
    }

1253 1254 1255 1256 1257 1258
    conf = virConfReadMem(vmx, strlen(vmx), VIR_CONF_FLAG_VMX_FORMAT);

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

1259
    /* vmx:.encoding */
1260
    if (virVMXGetConfigString(conf, ".encoding", &encoding, true) < 0) {
1261 1262 1263 1264 1265 1266 1267 1268 1269
        goto cleanup;
    }

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

1270
        utf8 = virVMXConvertToUTF8(encoding, vmx);
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285

        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 */
1286
    if (VIR_ALLOC(def) < 0)
A
ajia@redhat.com 已提交
1287
        goto cleanup;
1288

1289
    def->virtType = VIR_DOMAIN_VIRT_VMWARE;
1290 1291
    def->id = -1;

M
Matthias Bolte 已提交
1292
    /* vmx:config.version */
1293 1294
    if (virVMXGetConfigLong(conf, "config.version", &config_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1295
        goto cleanup;
1296 1297 1298
    }

    if (config_version != 8) {
1299 1300 1301
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'config.version' to be 8 but found "
                         "%lld"), config_version);
M
Matthias Bolte 已提交
1302
        goto cleanup;
1303 1304
    }

M
Matthias Bolte 已提交
1305
    /* vmx:virtualHW.version */
1306 1307
    if (virVMXGetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1308
        goto cleanup;
1309 1310
    }

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

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

M
Matthias Bolte 已提交
1326
    /* vmx:displayName -> def:name */
1327
    if (virVMXGetConfigString(conf, "displayName", &def->name, true) < 0) {
M
Matthias Bolte 已提交
1328
        goto cleanup;
1329 1330
    }

1331
    if (def->name != NULL) {
1332 1333
        if (virVMXUnescapeHexPercent(def->name) < 0 ||
            virVMXUnescapeHexPipe(def->name) < 0) {
1334 1335
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'name' contains invalid escape sequence"));
1336 1337 1338 1339
            goto cleanup;
        }
    }

1340
    /* vmx:annotation -> def:description */
1341 1342
    if (virVMXGetConfigString(conf, "annotation", &def->description,
                              true) < 0) {
1343 1344 1345 1346
        goto cleanup;
    }

    if (def->description != NULL) {
1347
        if (virVMXUnescapeHexPipe(def->description) < 0) {
1348 1349 1350
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'annotation' contains invalid escape "
                             "sequence"));
1351
            goto cleanup;
1352 1353 1354
        }
    }

1355
    /* vmx:memsize -> def:mem.max_balloon */
1356
    if (virVMXGetConfigLong(conf, "memsize", &memsize, 32, true) < 0) {
M
Matthias Bolte 已提交
1357
        goto cleanup;
1358 1359 1360
    }

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

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

1369
    /* vmx:sched.mem.max -> def:mem.cur_balloon */
1370 1371
    if (virVMXGetConfigLong(conf, "sched.mem.max", &sched_mem_max, memsize,
                            true) < 0) {
M
Matthias Bolte 已提交
1372
        goto cleanup;
1373 1374
    }

1375 1376
    if (sched_mem_max < 0) {
        sched_mem_max = memsize;
1377 1378
    }

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

1381 1382
    if (def->mem.cur_balloon > def->mem.max_balloon) {
        def->mem.cur_balloon = def->mem.max_balloon;
1383 1384
    }

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

    if (numvcpus <= 0 || (numvcpus % 2 != 0 && numvcpus != 1)) {
1407 1408 1409
        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 已提交
1410
        goto cleanup;
1411 1412
    }

E
Eric Blake 已提交
1413
    def->maxvcpus = def->vcpus = numvcpus;
1414

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

1422
    if (sched_cpu_affinity != NULL && STRCASENEQ(sched_cpu_affinity, "all")) {
1423 1424 1425
        const char *current = sched_cpu_affinity;
        int number, count = 0;

H
Hu Tao 已提交
1426
        cpumasklen = 0;
1427

H
Hu Tao 已提交
1428
        def->cpumask = virBitmapNew(VIR_DOMAIN_CPUMASK_LEN);
1429
        if (!def->cpumask)
M
Matthias Bolte 已提交
1430
            goto cleanup;
1431 1432 1433 1434 1435 1436 1437

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

            number = virParseNumber(&current);

            if (number < 0) {
1438 1439 1440 1441
                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 已提交
1442
                goto cleanup;
1443 1444 1445
            }

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

H
Hu Tao 已提交
1452 1453
            if (number + 1 > cpumasklen) {
                cpumasklen = number + 1;
1454 1455
            }

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

            virSkipSpaces(&current);

            if (*current == ',') {
                ++current;
            } else if (*current == '\0') {
                break;
            } else {
1466 1467 1468 1469
                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 已提交
1470
                goto cleanup;
1471 1472 1473 1474 1475 1476
            }

            virSkipSpaces(&current);
        }

        if (count < numvcpus) {
1477 1478 1479 1480
            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 已提交
1481
            goto cleanup;
1482 1483 1484
        }
    }

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

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 */
1515
    if (VIR_STRDUP(def->os.type, "hvm") < 0)
M
Matthias Bolte 已提交
1516
        goto cleanup;
1517

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

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

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

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

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

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

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

    def->ngraphics = 0;

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

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

M
Matthias Bolte 已提交
1559
    /* def:disks: 4 * 15 scsi + 2 * 2 ide + 2 floppy = 66 */
1560
    if (VIR_ALLOC_N(def->disks, 66) < 0)
M
Matthias Bolte 已提交
1561
        goto cleanup;
1562 1563 1564 1565 1566

    def->ndisks = 0;

    /* def:disks (scsi) */
    for (controller = 0; controller < 4; ++controller) {
1567 1568
        if (virVMXParseSCSIController(conf, controller, &present,
                                      &scsi_virtualDev[controller]) < 0) {
M
Matthias Bolte 已提交
1569
            goto cleanup;
1570 1571 1572 1573 1574 1575
        }

        if (! present) {
            continue;
        }

1576 1577
        for (unit = 0; unit < 16; ++unit) {
            if (unit == 7) {
1578
                /*
1579
                 * SCSI unit 7 is assigned to the SCSI controller and cannot be
1580 1581 1582 1583 1584
                 * used for disk devices.
                 */
                continue;
            }

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

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

1596
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1597
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
1598
                                 &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1599
                goto cleanup;
1600 1601 1602 1603 1604 1605 1606 1607 1608
            }

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

    /* def:disks (ide) */
1609 1610
    for (bus = 0; bus < 2; ++bus) {
        for (unit = 0; unit < 2; ++unit) {
1611
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
1612 1613
                                VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
                                &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1614
                goto cleanup;
1615 1616 1617 1618 1619 1620 1621
            }

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

1622
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1623 1624
                                VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
                                &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1625
                goto cleanup;
1626 1627 1628 1629 1630 1631 1632 1633 1634
            }

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

    /* def:disks (floppy) */
1635
    for (unit = 0; unit < 2; ++unit) {
1636
        if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_FLOPPY,
1637 1638
                            VIR_DOMAIN_DISK_BUS_FDC, 0, unit,
                            &def->disks[def->ndisks]) < 0) {
M
Matthias Bolte 已提交
1639
            goto cleanup;
1640 1641 1642 1643 1644 1645 1646
        }

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

1647 1648
    /* def:controllers */
    if (virDomainDefAddImplicitControllers(def) < 0) {
1649
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Could not add controllers"));
1650 1651 1652 1653 1654
        goto cleanup;
    }

    for (controller = 0; controller < def->ncontrollers; ++controller) {
        if (def->controllers[controller]->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1655
            if (def->controllers[controller]->idx > 3) {
1656 1657 1658
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI controller index %d out of [0..3] range"),
                               def->controllers[controller]->idx);
1659 1660 1661 1662 1663 1664 1665 1666
                goto cleanup;
            }

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

1667
    /* def:fss */
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    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;

1682
            if (VIR_ALLOC_N(def->fss, sharedFolder_maxNum) < 0)
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
                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;
                }
            }
        }
    }
1699 1700

    /* def:nets */
1701
    if (VIR_ALLOC_N(def->nets, 4) < 0)
M
Matthias Bolte 已提交
1702
        goto cleanup;
1703 1704 1705 1706

    def->nnets = 0;

    for (controller = 0; controller < 4; ++controller) {
1707 1708
        if (virVMXParseEthernet(conf, controller,
                                &def->nets[def->nnets]) < 0) {
M
Matthias Bolte 已提交
1709
            goto cleanup;
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
        }

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

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

1720
    /* def:videos */
1721
    if (VIR_ALLOC_N(def->videos, 1) < 0)
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
        goto cleanup;

    def->nvideos = 0;

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

    def->nvideos = 1;

1732 1733 1734 1735 1736 1737 1738
    /* def:sounds */
    /* FIXME */

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

    /* def:serials */
1739
    if (VIR_ALLOC_N(def->serials, 4) < 0)
M
Matthias Bolte 已提交
1740
        goto cleanup;
1741 1742 1743 1744

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1745 1746
        if (virVMXParseSerial(ctx, conf, port,
                              &def->serials[def->nserials]) < 0) {
M
Matthias Bolte 已提交
1747
            goto cleanup;
1748 1749 1750 1751 1752 1753 1754 1755
        }

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

    /* def:parallels */
1756
    if (VIR_ALLOC_N(def->parallels, 3) < 0)
M
Matthias Bolte 已提交
1757
        goto cleanup;
1758 1759 1760 1761

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1762 1763
        if (virVMXParseParallel(ctx, conf, port,
                                &def->parallels[def->nparallels]) < 0) {
M
Matthias Bolte 已提交
1764
            goto cleanup;
1765 1766 1767 1768 1769 1770 1771
        }

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

M
Matthias Bolte 已提交
1772 1773
    success = true;

M
Matthias Bolte 已提交
1774
  cleanup:
M
Matthias Bolte 已提交
1775 1776 1777 1778 1779
    if (! success) {
        virDomainDefFree(def);
        def = NULL;
    }

1780
    virConfFree(conf);
1781
    VIR_FREE(encoding);
1782
    VIR_FREE(sched_cpu_affinity);
1783
    VIR_FREE(sched_cpu_shares);
M
Matthias Bolte 已提交
1784
    VIR_FREE(guestOS);
1785 1786 1787 1788 1789 1790

    return def;
}



M
Matthias Bolte 已提交
1791
int
1792
virVMXParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1793
{
1794
    bool enabled = false;
M
Matthias Bolte 已提交
1795
    long long port = 0;
1796
    char *listenAddr = NULL;
M
Matthias Bolte 已提交
1797 1798

    if (def == NULL || *def != NULL) {
1799
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1800 1801 1802
        return -1;
    }

1803 1804
    if (virVMXGetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
                               false, true) < 0) {
M
Matthias Bolte 已提交
1805 1806 1807 1808 1809 1810 1811
        return -1;
    }

    if (! enabled) {
        return 0;
    }

1812
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
1813 1814 1815 1816
        goto failure;

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1817 1818 1819
    if (virVMXGetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
                            true) < 0 ||
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.ip",
1820
                              &listenAddr, true) < 0 ||
1821 1822 1823 1824
        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 已提交
1825 1826 1827
        goto failure;
    }

1828 1829 1830 1831 1832 1833
    if (listenAddr) {
        if (virDomainGraphicsListenSetAddress(*def, 0, listenAddr, -1, true) < 0)
            goto failure;
        VIR_FREE(listenAddr);
    }

M
Matthias Bolte 已提交
1834
    if (port < 0) {
1835
        VIR_WARN("VNC is enabled but VMX entry 'RemoteDisplay.vnc.port' "
M
Matthias Bolte 已提交
1836 1837 1838
                  "is missing, the VNC port is unknown");

        (*def)->data.vnc.port = 0;
1839
        (*def)->data.vnc.autoport = true;
M
Matthias Bolte 已提交
1840 1841 1842 1843 1844 1845
    } else {
        if (port < 5900 || port > 5964) {
            VIR_WARN("VNC port %lld it out of [5900..5964] range", port);
        }

        (*def)->data.vnc.port = port;
1846
        (*def)->data.vnc.autoport = false;
M
Matthias Bolte 已提交
1847 1848 1849 1850 1851
    }

    return 0;

  failure:
1852
    VIR_FREE(listenAddr);
M
Matthias Bolte 已提交
1853 1854 1855 1856 1857 1858 1859 1860
    virDomainGraphicsDefFree(*def);
    *def = NULL;

    return -1;
}



1861
int
1862 1863
virVMXParseSCSIController(virConfPtr conf, int controller, bool *present,
                          int *virtualDev)
1864
{
1865
    int result = -1;
1866 1867
    char present_name[32];
    char virtualDev_name[32];
1868 1869
    char *virtualDev_string = NULL;
    char *tmp;
1870

1871
    if (virtualDev == NULL || *virtualDev != -1) {
1872
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1873
        return -1;
1874 1875 1876
    }

    if (controller < 0 || controller > 3) {
1877 1878 1879
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI controller index %d out of [0..3] range"),
                       controller);
1880
        return -1;
1881 1882
    }

1883 1884 1885
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1886

1887
    if (virVMXGetConfigBoolean(conf, present_name, present, false, true) < 0) {
1888
        goto cleanup;
1889 1890 1891
    }

    if (! *present) {
1892 1893
        result = 0;
        goto cleanup;
1894 1895
    }

1896 1897
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev_string,
                              true) < 0) {
1898
        goto cleanup;
1899 1900
    }

1901 1902 1903 1904 1905 1906 1907
    if (virtualDev_string != NULL) {
        tmp = virtualDev_string;

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

1908
        *virtualDev = virVMXControllerModelSCSITypeFromString(virtualDev_string);
1909 1910

        if (*virtualDev == -1 ||
1911 1912 1913 1914
            (*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)) {
1915 1916 1917 1918
            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);
1919
            goto cleanup;
1920
        }
1921 1922
    }

1923
    result = 0;
1924

1925
  cleanup:
1926
    VIR_FREE(virtualDev_string);
1927

1928
    return result;
1929 1930 1931 1932 1933
}



int
1934
virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virConfPtr conf,
1935 1936
                int device, int busType, int controllerOrBus, int unit,
                virDomainDiskDefPtr *def)
1937 1938
{
    /*
1939 1940 1941
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK,
     *                    VIR_DOMAIN_DISK_DEVICE_CDROM,
     *                    VIR_DOMAIN_DISK_DEVICE_LUN}
1942 1943 1944
     *         busType = VIR_DOMAIN_DISK_BUS_SCSI
     * controllerOrBus = [0..3] -> controller
     *            unit = [0..6,8..15]
1945
     *
1946 1947 1948
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK,
     *                    VIR_DOMAIN_DISK_DEVICE_CDROM,
     *                    VIR_DOMAIN_DISK_DEVICE_LUN}
1949 1950 1951
     *         busType = VIR_DOMAIN_DISK_BUS_IDE
     * controllerOrBus = [0..1] -> bus
     *            unit = [0..1]
1952
     *
1953 1954 1955 1956
     *          device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *         busType = VIR_DOMAIN_DISK_BUS_FDC
     * controllerOrBus = [0]
     *            unit = [0..1]
1957 1958
     */

M
Matthias Bolte 已提交
1959
    int result = -1;
1960 1961 1962
    char *prefix = NULL;

    char present_name[32] = "";
1963
    bool present = false;
1964 1965

    char startConnected_name[32] = "";
1966
    bool startConnected = false;
1967 1968 1969 1970 1971

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

    char clientDevice_name[32] = "";
1972
    bool clientDevice = false;
1973 1974 1975 1976 1977 1978 1979 1980

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

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

    char writeThrough_name[32] = "";
1981
    bool writeThrough = false;
1982 1983

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

1988
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
1989
        return -1;
1990 1991

    (*def)->device = device;
1992
    (*def)->bus = busType;
1993 1994 1995 1996

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
1997 1998
        if (busType == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controllerOrBus < 0 || controllerOrBus > 3) {
1999 2000 2001
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI controller index %d out of [0..3] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2002
                goto cleanup;
2003 2004
            }

2005
            if (unit < 0 || unit > 15 || unit == 7) {
2006 2007 2008
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI unit index %d out of [0..6,8..15] range"),
                               unit);
M
Matthias Bolte 已提交
2009
                goto cleanup;
2010 2011
            }

2012
            if (virAsprintf(&prefix, "scsi%d:%d", controllerOrBus, unit) < 0)
M
Matthias Bolte 已提交
2013
                goto cleanup;
2014

M
Matthias Bolte 已提交
2015
            (*def)->dst =
2016
               virIndexToDiskName
2017
                 (controllerOrBus * 15 + (unit < 7 ? unit : unit - 1), "sd");
2018 2019

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2020
                goto cleanup;
2021
            }
2022 2023
        } else if (busType == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controllerOrBus < 0 || controllerOrBus > 1) {
2024 2025 2026
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IDE bus index %d out of [0..1] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2027
                goto cleanup;
2028 2029
            }

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

2036
            if (virAsprintf(&prefix, "ide%d:%d", controllerOrBus, unit) < 0)
M
Matthias Bolte 已提交
2037
                goto cleanup;
2038

2039
            (*def)->dst = virIndexToDiskName(controllerOrBus * 2 + unit, "hd");
2040 2041

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2042
                goto cleanup;
2043 2044
            }
        } else {
2045 2046 2047 2048
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2049
            goto cleanup;
2050 2051
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2052 2053
        if (busType == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controllerOrBus != 0) {
2054 2055 2056
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC controller index %d out of [0] range"),
                               controllerOrBus);
2057 2058 2059 2060
                goto cleanup;
            }

            if (unit < 0 || unit > 1) {
2061 2062 2063
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC unit index %d out of [0..1] range"),
                               unit);
M
Matthias Bolte 已提交
2064
                goto cleanup;
2065 2066
            }

2067
            if (virAsprintf(&prefix, "floppy%d", unit) < 0)
M
Matthias Bolte 已提交
2068
                goto cleanup;
2069

2070
            (*def)->dst = virIndexToDiskName(unit, "fd");
2071 2072

            if ((*def)->dst == NULL) {
M
Matthias Bolte 已提交
2073
                goto cleanup;
2074 2075
            }
        } else {
2076 2077 2078 2079
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2080
            goto cleanup;
2081 2082
        }
    } else {
2083 2084 2085
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2086
        goto cleanup;
2087 2088
    }

2089 2090 2091 2092 2093 2094 2095
    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);
2096 2097

    /* vmx:present */
2098
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2099
        goto cleanup;
2100 2101 2102
    }

    /* vmx:startConnected */
2103 2104
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2105
        goto cleanup;
2106 2107 2108 2109 2110 2111 2112 2113
    }

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

    /* vmx:deviceType -> def:type */
2114
    if (virVMXGetConfigString(conf, deviceType_name, &deviceType, true) < 0) {
M
Matthias Bolte 已提交
2115
        goto cleanup;
2116 2117 2118
    }

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

    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
2127
         * handle them (e.g. assign an image) without the VI Client GUI.
2128 2129 2130 2131 2132
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
2133
    if (virVMXGetConfigString(conf, fileType_name, &fileType, true) < 0) {
M
Matthias Bolte 已提交
2134
        goto cleanup;
2135 2136 2137
    }

    /* vmx:fileName -> def:src, def:type */
2138
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2139
        goto cleanup;
2140 2141 2142
    }

    /* vmx:writeThrough -> def:cachemode */
2143 2144
    if (virVMXGetConfigBoolean(conf, writeThrough_name, &writeThrough, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2145
        goto cleanup;
2146 2147 2148 2149
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
2150
        if (virFileHasSuffix(fileName, ".vmdk")) {
2151
            if (deviceType != NULL) {
2152
                if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
2153 2154
                    STRCASENEQ(deviceType, "scsi-hardDisk") &&
                    STRCASENEQ(deviceType, "disk")) {
2155 2156 2157 2158
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2159
                    goto cleanup;
2160
                } else if (busType == VIR_DOMAIN_DISK_BUS_IDE &&
2161 2162
                           STRCASENEQ(deviceType, "ata-hardDisk") &&
                           STRCASENEQ(deviceType, "disk")) {
2163 2164 2165 2166
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2167
                    goto cleanup;
2168 2169 2170 2171
                }
            }

            (*def)->type = VIR_DOMAIN_DISK_TYPE_FILE;
2172
            (*def)->src = ctx->parseFileName(fileName, ctx->opaque);
2173 2174 2175
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;

M
Matthias Bolte 已提交
2176
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2177
                goto cleanup;
M
Matthias Bolte 已提交
2178
            }
2179
        } else if (virFileHasSuffix(fileName, ".iso") ||
2180 2181
                   STRCASEEQ(deviceType, "atapi-cdrom") ||
                   STRCASEEQ(deviceType, "cdrom-raw")) {
2182 2183
            /*
             * This function was called in order to parse a harddisk device,
2184 2185 2186
             * but .iso files, 'atapi-cdrom', and 'cdrom-raw' devices are for
             * CDROM devices only. Just ignore it, another call to this
             * function to parse a CDROM device may handle it.
2187 2188 2189
             */
            goto ignore;
        } else {
2190
            virReportError(VIR_ERR_INTERNAL_ERROR,
2191 2192 2193 2194
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
                             fileName, fileName_name,
                             deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2195
            goto cleanup;
2196 2197
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2198
        if (virFileHasSuffix(fileName, ".iso")) {
2199 2200
            if (deviceType != NULL) {
                if (STRCASENEQ(deviceType, "cdrom-image")) {
2201 2202 2203
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                     "but found '%s'"), deviceType_name, deviceType);
M
Matthias Bolte 已提交
2204
                    goto cleanup;
2205 2206 2207 2208
                }
            }

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

M
Matthias Bolte 已提交
2211
            if ((*def)->src == NULL) {
M
Matthias Bolte 已提交
2212
                goto cleanup;
M
Matthias Bolte 已提交
2213
            }
2214
        } else if (virFileHasSuffix(fileName, ".vmdk")) {
2215 2216 2217 2218 2219 2220 2221
            /*
             * 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;
2222
        } else if (STRCASEEQ(deviceType, "atapi-cdrom")) {
2223
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
2224 2225 2226 2227 2228 2229 2230 2231

            if (STRCASEEQ(fileName, "auto detect")) {
                (*def)->src = NULL;
                (*def)->startupPolicy = VIR_DOMAIN_STARTUP_POLICY_OPTIONAL;
            } else {
                (*def)->src = fileName;
                fileName = NULL;
            }
2232 2233 2234 2235
        } else if (STRCASEEQ(deviceType, "cdrom-raw")) {
            /* Raw access CD-ROMs actually are device='lun' */
            (*def)->device = VIR_DOMAIN_DISK_DEVICE_LUN;
            (*def)->type = VIR_DOMAIN_DISK_TYPE_BLOCK;
2236 2237 2238 2239 2240 2241 2242 2243

            if (STRCASEEQ(fileName, "auto detect")) {
                (*def)->src = NULL;
                (*def)->startupPolicy = VIR_DOMAIN_STARTUP_POLICY_OPTIONAL;
            } else {
                (*def)->src = fileName;
                fileName = NULL;
            }
2244
        } else {
2245
            virReportError(VIR_ERR_INTERNAL_ERROR,
2246 2247 2248 2249
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
                             fileName, fileName_name,
                             deviceType ? deviceType : "unknown");
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
            virReportError(VIR_ERR_INTERNAL_ERROR,
2276 2277 2278 2279
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
                             fileName, fileName_name,
                             deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2280
            goto cleanup;
2281 2282
        }
    } else {
2283 2284
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2285
        goto cleanup;
2286 2287
    }

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

M
Matthias Bolte 已提交
2294 2295
    result = 0;

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

2302 2303 2304 2305 2306 2307 2308
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

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

M
Matthias Bolte 已提交
2313 2314
    result = 0;

2315 2316 2317 2318 2319
    goto cleanup;
}



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

2345
    if (VIR_ALLOC(*def) < 0)
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
        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)
M
Matthias Bolte 已提交
2469
        return -1;
2470

2471
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
2472

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

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

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

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

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

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

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

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

2548 2549 2550 2551 2552
    if (virtualDev != NULL) {
        if (STRCASENEQ(virtualDev, "vlance") &&
            STRCASENEQ(virtualDev, "vmxnet") &&
            STRCASENEQ(virtualDev, "vmxnet3") &&
            STRCASENEQ(virtualDev, "e1000")) {
2553 2554 2555 2556
            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 已提交
2557
            goto cleanup;
2558 2559 2560 2561 2562
        }

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

2563
            if (VIR_STRDUP(virtualDev, "vmxnet2") < 0)
M
Matthias Bolte 已提交
2564
                goto cleanup;
2565
        }
2566 2567
    }

M
Matthias Bolte 已提交
2568
    /* vmx:networkName -> def:data.bridge.brname */
2569 2570 2571 2572 2573 2574 2575
    if (connectionType == NULL ||
        STRCASEEQ(connectionType, "bridged") ||
        STRCASEEQ(connectionType, "custom")) {
        if (virVMXGetConfigString(conf, networkName_name, &networkName,
                                  true) < 0)
            goto cleanup;

2576 2577
        if (!networkName && VIR_STRDUP(networkName, "") < 0)
            goto cleanup;
M
Matthias Bolte 已提交
2578 2579 2580
    }

    /* vmx:vnet -> def:data.ifname */
2581
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
2582
        virVMXGetConfigString(conf, vnet_name, &vnet, false) < 0) {
M
Matthias Bolte 已提交
2583
        goto cleanup;
2584 2585 2586 2587 2588 2589
    }

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

        virtualDev = NULL;
M
Matthias Bolte 已提交
2593
        networkName = NULL;
2594 2595
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
2596 2597 2598
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No yet handled value '%s' for VMX entry '%s'"),
                       connectionType, connectionType_name);
M
Matthias Bolte 已提交
2599
        goto cleanup;
2600
    } else if (STRCASEEQ(connectionType, "nat")) {
2601 2602 2603 2604
        (*def)->type = VIR_DOMAIN_NET_TYPE_USER;
        (*def)->model = virtualDev;

        virtualDev = NULL;
2605 2606 2607
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
2608 2609
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
2610 2611

        virtualDev = NULL;
M
Matthias Bolte 已提交
2612
        networkName = NULL;
2613 2614
        vnet = NULL;
    } else {
2615 2616 2617
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid value '%s' for VMX entry '%s'"), connectionType,
                       connectionType_name);
M
Matthias Bolte 已提交
2618
        goto cleanup;
2619 2620
    }

M
Matthias Bolte 已提交
2621 2622
    result = 0;

M
Matthias Bolte 已提交
2623
  cleanup:
M
Matthias Bolte 已提交
2624 2625 2626 2627 2628
    if (result < 0) {
        virDomainNetDefFree(*def);
        *def = NULL;
    }

S
Stefan Berger 已提交
2629
    VIR_FREE(networkName);
2630 2631 2632 2633 2634 2635 2636 2637 2638
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;

M
Matthias Bolte 已提交
2639
  ignore:
2640 2641 2642
    virDomainNetDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2643 2644
    result = 0;

2645 2646 2647 2648 2649 2650
    goto cleanup;
}



int
2651 2652
virVMXParseSerial(virVMXContext *ctx, virConfPtr conf, int port,
                  virDomainChrDefPtr *def)
2653
{
M
Matthias Bolte 已提交
2654
    int result = -1;
2655 2656 2657
    char prefix[48] = "";

    char present_name[48] = "";
2658
    bool present = false;
2659 2660

    char startConnected_name[48] = "";
2661
    bool startConnected = false;
2662 2663 2664 2665 2666 2667 2668

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

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

2669 2670 2671
    char network_endPoint_name[48] = "";
    char *network_endPoint = NULL;

M
Martin Kletzander 已提交
2672
    virURIPtr parsedUri = NULL;
2673

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

    if (port < 0 || port > 3) {
2680 2681
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Serial port index %d out of [0..3] range"), port);
2682
        return -1;
2683 2684
    }

2685
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
2686
        return -1;
2687

2688
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
2689

2690
    snprintf(prefix, sizeof(prefix), "serial%d", port);
2691

2692 2693 2694 2695 2696
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
    VMX_BUILD_NAME_EXTRA(network_endPoint, "network.endPoint");
2697 2698

    /* vmx:present */
2699
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2700
        goto cleanup;
2701 2702 2703
    }

    /* vmx:startConnected */
2704 2705
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2706
        goto cleanup;
2707 2708 2709 2710 2711 2712 2713 2714
    }

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

    /* vmx:fileType -> def:type */
2715
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2716
        goto cleanup;
2717 2718 2719
    }

    /* vmx:fileName -> def:data.file.path */
2720
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
2721
        goto ignore;
2722 2723
    }

2724
    /* vmx:network.endPoint -> def:data.tcp.listen */
2725 2726
    if (virVMXGetConfigString(conf, network_endPoint_name, &network_endPoint,
                              true) < 0) {
2727 2728 2729
        goto cleanup;
    }

2730 2731
    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2732
        (*def)->target.port = port;
2733 2734
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2735 2736 2737

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2738
        (*def)->target.port = port;
2739 2740 2741
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2742

2743
        if ((*def)->source.data.file.path == NULL) {
M
Matthias Bolte 已提交
2744
            goto cleanup;
M
Matthias Bolte 已提交
2745
        }
2746
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
2747 2748 2749 2750
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
2751
        (*def)->target.port = port;
2752 2753
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->source.data.file.path = fileName;
M
Matthias Bolte 已提交
2754 2755

        fileName = NULL;
2756 2757
    } else if (STRCASEEQ(fileType, "network")) {
        (*def)->target.port = port;
2758
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_TCP;
2759

2760
        if (!(parsedUri = virURIParse(fileName)))
2761 2762 2763
            goto cleanup;

        if (parsedUri->port == 0) {
2764 2765 2766
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' doesn't contain a port part"),
                           fileName_name);
2767 2768 2769
            goto cleanup;
        }

2770
        if (VIR_STRDUP((*def)->source.data.tcp.host, parsedUri->server) < 0)
2771 2772
            goto cleanup;

2773
        if (virAsprintf(&(*def)->source.data.tcp.service, "%d",
2774
                        parsedUri->port) < 0)
2775 2776 2777 2778 2779 2780 2781
            goto cleanup;

        /* See vSphere API documentation about VirtualSerialPortURIBackingInfo */
        if (parsedUri->scheme == NULL ||
            STRCASEEQ(parsedUri->scheme, "tcp") ||
            STRCASEEQ(parsedUri->scheme, "tcp4") ||
            STRCASEEQ(parsedUri->scheme, "tcp6")) {
2782
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW;
2783
        } else if (STRCASEEQ(parsedUri->scheme, "telnet")) {
2784 2785
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
2786
        } else if (STRCASEEQ(parsedUri->scheme, "telnets")) {
2787 2788
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNETS;
2789 2790 2791 2792
        } else if (STRCASEEQ(parsedUri->scheme, "ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp4+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp6+ssl")) {
2793
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TLS;
2794
        } else {
2795 2796 2797
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' contains unsupported scheme '%s'"),
                           fileName_name, parsedUri->scheme);
2798 2799 2800 2801
            goto cleanup;
        }

        if (network_endPoint == NULL || STRCASEEQ(network_endPoint, "server")) {
2802
            (*def)->source.data.tcp.listen = true;
2803
        } else if (STRCASEEQ(network_endPoint, "client")) {
2804
            (*def)->source.data.tcp.listen = false;
2805
        } else {
2806 2807 2808
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'server' or 'client' "
                             "but found '%s'"), network_endPoint_name, network_endPoint);
2809 2810
            goto cleanup;
        }
2811
    } else {
2812 2813 2814
        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 已提交
2815
        goto cleanup;
2816 2817
    }

M
Matthias Bolte 已提交
2818 2819
    result = 0;

M
Matthias Bolte 已提交
2820
  cleanup:
M
Matthias Bolte 已提交
2821 2822 2823 2824 2825
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2826 2827
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2828
    VIR_FREE(network_endPoint);
2829
    virURIFree(parsedUri);
2830 2831 2832

    return result;

M
Matthias Bolte 已提交
2833
  ignore:
2834 2835 2836
    virDomainChrDefFree(*def);
    *def = NULL;

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

2839 2840 2841 2842 2843 2844
    goto cleanup;
}



int
2845 2846
virVMXParseParallel(virVMXContext *ctx, virConfPtr conf, int port,
                    virDomainChrDefPtr *def)
2847
{
M
Matthias Bolte 已提交
2848
    int result = -1;
2849 2850 2851
    char prefix[48] = "";

    char present_name[48] = "";
2852
    bool present = false;
2853 2854

    char startConnected_name[48] = "";
2855
    bool startConnected = false;
2856 2857 2858 2859 2860 2861 2862 2863

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

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

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

    if (port < 0 || port > 2) {
2869 2870
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"), port);
2871
        return -1;
2872 2873
    }

2874
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
2875
        return -1;
2876

2877
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
2878

2879
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2880

2881 2882 2883 2884
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
2885 2886

    /* vmx:present */
2887
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0) {
M
Matthias Bolte 已提交
2888
        goto cleanup;
2889 2890 2891
    }

    /* vmx:startConnected */
2892 2893
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2894
        goto cleanup;
2895 2896 2897 2898 2899 2900 2901 2902
    }

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

    /* vmx:fileType -> def:type */
2903
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0) {
M
Matthias Bolte 已提交
2904
        goto cleanup;
2905 2906 2907
    }

    /* vmx:fileName -> def:data.file.path */
2908
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0) {
M
Matthias Bolte 已提交
2909
        goto cleanup;
2910 2911 2912 2913
    }

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2914
        (*def)->target.port = port;
2915 2916
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2917 2918 2919

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2920
        (*def)->target.port = port;
2921 2922 2923
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2924

2925
        if ((*def)->source.data.file.path == NULL) {
M
Matthias Bolte 已提交
2926
            goto cleanup;
M
Matthias Bolte 已提交
2927
        }
2928
    } else {
2929 2930 2931
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                         "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2932
        goto cleanup;
2933 2934
    }

M
Matthias Bolte 已提交
2935 2936
    result = 0;

M
Matthias Bolte 已提交
2937
  cleanup:
M
Matthias Bolte 已提交
2938 2939 2940 2941 2942
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2943 2944 2945 2946 2947
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

M
Matthias Bolte 已提交
2948
  ignore:
2949 2950 2951
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2952 2953
    result = 0;

2954 2955
    goto cleanup;
}
M
Matthias Bolte 已提交
2956 2957 2958



2959 2960 2961 2962 2963 2964 2965
int
virVMXParseSVGA(virConfPtr conf, virDomainVideoDefPtr *def)
{
    int result = -1;
    long long svga_vramSize = 0;

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

2970
    if (VIR_ALLOC(*def) < 0)
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
        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;
    }

2981
    (*def)->vram = VIR_DIV_UP(svga_vramSize, 1024); /* Scale from bytes to kilobytes */
2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995

    result = 0;

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

    return result;
}



M
Matthias Bolte 已提交
2996 2997 2998 2999 3000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Domain XML -> VMX
 */

char *
3001
virVMXFormatConfig(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virDomainDefPtr def,
3002
                   int virtualHW_version)
M
Matthias Bolte 已提交
3003
{
3004
    char *vmx = NULL;
3005
    size_t i;
M
Matthias Bolte 已提交
3006 3007 3008
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
3009 3010
    char *preliminaryDisplayName = NULL;
    char *displayName = NULL;
3011
    char *annotation = NULL;
3012
    unsigned long long max_balloon;
3013 3014
    bool scsi_present[4] = { false, false, false, false };
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
3015
    bool floppy_present[2] = { false, false };
M
Matthias Bolte 已提交
3016

3017
    if (ctx->formatFileName == NULL) {
3018 3019
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no formatFileName function set"));
3020 3021 3022
        return NULL;
    }

M
Matthias Bolte 已提交
3023 3024
    memset(zero, 0, VIR_UUID_BUFLEN);

3025
    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) {
3026 3027 3028 3029
        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 已提交
3030 3031 3032
        return NULL;
    }

3033 3034 3035
    /* vmx:.encoding */
    virBufferAddLit(&buffer, ".encoding = \"UTF-8\"\n");

M
Matthias Bolte 已提交
3036 3037 3038 3039
    /* vmx:config.version */
    virBufferAddLit(&buffer, "config.version = \"8\"\n");

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

M
Matthias Bolte 已提交
3043
    /* def:os.arch -> vmx:guestOS */
3044
    if (def->os.arch == VIR_ARCH_I686) {
M
Matthias Bolte 已提交
3045
        virBufferAddLit(&buffer, "guestOS = \"other\"\n");
3046
    } else if (def->os.arch == VIR_ARCH_X86_64) {
M
Matthias Bolte 已提交
3047 3048
        virBufferAddLit(&buffer, "guestOS = \"other-64\"\n");
    } else {
3049 3050
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting domain XML attribute 'arch' of entry 'os/type' "
3051 3052
                         "to be 'i686' or 'x86_64' but found '%s'"),
                       virArchToString(def->os.arch));
3053
        goto cleanup;
M
Matthias Bolte 已提交
3054 3055
    }

M
Matthias Bolte 已提交
3056 3057 3058 3059 3060 3061 3062
    /* 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 {
3063 3064 3065
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported SMBIOS mode '%s'"),
                       virDomainSmbiosModeTypeToString(def->os.smbios_mode));
M
Matthias Bolte 已提交
3066 3067 3068
        goto cleanup;
    }

M
Matthias Bolte 已提交
3069 3070 3071 3072
    /* def:uuid -> vmx:uuid.action, vmx:uuid.bios */
    if (memcmp(def->uuid, zero, VIR_UUID_BUFLEN) == 0) {
        virBufferAddLit(&buffer, "uuid.action = \"create\"\n");
    } else {
3073
        virBufferAsprintf(&buffer, "uuid.bios = \"%02x %02x %02x %02x %02x %02x "
M
Matthias Bolte 已提交
3074 3075 3076 3077 3078 3079 3080 3081 3082
                          "%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 */
3083
    preliminaryDisplayName = virVMXEscapeHexPipe(def->name);
3084

3085 3086 3087
    if (preliminaryDisplayName == NULL) {
        goto cleanup;
    }
3088

3089
    displayName = virVMXEscapeHexPercent(preliminaryDisplayName);
3090

3091 3092 3093
    if (displayName == NULL) {
        goto cleanup;
    }
3094

3095
    virBufferAsprintf(&buffer, "displayName = \"%s\"\n", displayName);
3096

3097 3098
    /* def:description -> vmx:annotation */
    if (def->description != NULL) {
3099
        annotation = virVMXEscapeHexPipe(def->description);
3100

3101
        virBufferAsprintf(&buffer, "annotation = \"%s\"\n", annotation);
3102 3103
    }

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

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

3111
    /* def:mem.cur_balloon -> vmx:sched.mem.max */
3112
    if (def->mem.cur_balloon < max_balloon) {
3113
        virBufferAsprintf(&buffer, "sched.mem.max = \"%llu\"\n",
3114 3115
                          VIR_DIV_UP(def->mem.cur_balloon,
                                     1024)); /* Scale from kilobytes to megabytes */
M
Matthias Bolte 已提交
3116 3117
    }

3118 3119
    /* def:mem.min_guarantee -> vmx:sched.mem.minsize */
    if (def->mem.min_guarantee > 0) {
3120
        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%llu\"\n",
3121 3122
                          VIR_DIV_UP(def->mem.min_guarantee,
                                     1024)); /* Scale from kilobytes to megabytes */
3123 3124
    }

E
Eric Blake 已提交
3125 3126
    /* def:maxvcpus -> vmx:numvcpus */
    if (def->vcpus != def->maxvcpus) {
3127 3128 3129
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("No support for domain XML entry 'vcpu' attribute "
                         "'current'"));
E
Eric Blake 已提交
3130 3131 3132
        goto cleanup;
    }
    if (def->maxvcpus <= 0 || (def->maxvcpus % 2 != 0 && def->maxvcpus != 1)) {
3133 3134 3135 3136
        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);
3137
        goto cleanup;
M
Matthias Bolte 已提交
3138 3139
    }

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

    /* def:cpumask -> vmx:sched.cpu.affinity */
H
Hu Tao 已提交
3143
    if (def->cpumask && virBitmapSize(def->cpumask) > 0) {
3144
        int bit;
M
Matthias Bolte 已提交
3145 3146 3147 3148
        virBufferAddLit(&buffer, "sched.cpu.affinity = \"");

        sched_cpu_affinity_length = 0;

3149 3150
        bit = -1;
        while ((bit = virBitmapNextSetBit(def->cpumask, bit)) >= 0) {
H
Hu Tao 已提交
3151
            ++sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3152 3153
        }

E
Eric Blake 已提交
3154
        if (sched_cpu_affinity_length < def->maxvcpus) {
3155 3156 3157 3158
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML attribute 'cpuset' of entry "
                             "'vcpu' to contain at least %d CPU(s)"),
                           def->maxvcpus);
3159
            goto cleanup;
M
Matthias Bolte 已提交
3160 3161
        }

3162 3163 3164
        bit = -1;
        while ((bit = virBitmapNextSetBit(def->cpumask, bit)) >= 0) {
            virBufferAsprintf(&buffer, "%d", bit);
M
Matthias Bolte 已提交
3165

H
Hu Tao 已提交
3166 3167
            if (sched_cpu_affinity_length > 1) {
                virBufferAddChar(&buffer, ',');
M
Matthias Bolte 已提交
3168
            }
H
Hu Tao 已提交
3169 3170

            --sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3171 3172 3173 3174 3175
        }

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

3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
    /* 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 {
3186
            virBufferAsprintf(&buffer, "sched.cpu.shares = \"%lu\"\n",
3187 3188 3189 3190
                              def->cputune.shares);
        }
    }

M
Matthias Bolte 已提交
3191 3192 3193 3194
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
3195
            if (virVMXFormatVNC(def->graphics[i], &buffer) < 0) {
3196
                goto cleanup;
M
Matthias Bolte 已提交
3197 3198 3199 3200 3201
            }

            break;

          default:
3202 3203 3204
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported graphics type '%s'"),
                           virDomainGraphicsTypeToString(def->graphics[i]->type));
3205
            goto cleanup;
M
Matthias Bolte 已提交
3206 3207 3208
        }
    }

M
Matthias Bolte 已提交
3209
    /* def:disks */
3210
    for (i = 0; i < def->ndisks; ++i) {
3211
        if (virVMXVerifyDiskAddress(xmlopt, def->disks[i]) < 0 ||
3212
            virVMXHandleLegacySCSIDiskDriverName(def, def->disks[i]) < 0) {
3213
            goto cleanup;
3214 3215
        }
    }
M
Matthias Bolte 已提交
3216

3217 3218
    if (virVMXGatherSCSIControllers(ctx, def, scsi_virtualDev,
                                    scsi_present) < 0) {
3219
        goto cleanup;
M
Matthias Bolte 已提交
3220 3221 3222 3223
    }

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

3226
            if (scsi_virtualDev[i] != -1) {
3227
                virBufferAsprintf(&buffer, "scsi%zu.virtualDev = \"%s\"\n", i,
3228
                                  virVMXControllerModelSCSITypeToString
3229
                                    (scsi_virtualDev[i]));
M
Matthias Bolte 已提交
3230 3231 3232 3233 3234 3235 3236 3237
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
          case VIR_DOMAIN_DISK_DEVICE_CDROM:
3238
          case VIR_DOMAIN_DISK_DEVICE_LUN:
3239
            if (virVMXFormatDisk(ctx, def->disks[i], &buffer) < 0) {
3240
                goto cleanup;
M
Matthias Bolte 已提交
3241 3242 3243 3244 3245
            }

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
3246 3247
            if (virVMXFormatFloppy(ctx, def->disks[i], &buffer,
                                   floppy_present) < 0) {
3248
                goto cleanup;
M
Matthias Bolte 已提交
3249 3250 3251 3252 3253
            }

            break;

          default:
3254 3255 3256
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported disk device type '%s'"),
                           virDomainDiskDeviceTypeToString(def->disks[i]->device));
3257
            goto cleanup;
M
Matthias Bolte 已提交
3258 3259 3260
        }
    }

3261 3262 3263
    for (i = 0; i < 2; ++i) {
        /* floppy[0..1].present defaults to true, disable it explicitly */
        if (! floppy_present[i]) {
3264
            virBufferAsprintf(&buffer, "floppy%zu.present = \"false\"\n", i);
3265 3266 3267
        }
    }

M
Matthias Bolte 已提交
3268
    /* def:fss */
3269 3270
    if (def->nfss > 0) {
        virBufferAddLit(&buffer, "isolation.tools.hgfs.disable = \"false\"\n");
3271
        virBufferAsprintf(&buffer, "sharedFolder.maxNum = \"%zu\"\n", def->nfss);
3272 3273 3274 3275 3276 3277 3278
    }

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

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
3282
        if (virVMXFormatEthernet(def->nets[i], i, &buffer) < 0) {
3283
            goto cleanup;
M
Matthias Bolte 已提交
3284 3285 3286 3287 3288 3289 3290 3291 3292
        }
    }

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

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

3293 3294 3295
    /* def:videos */
    if (def->nvideos > 0) {
        if (def->nvideos > 1) {
3296 3297
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("No support for multiple video devices"));
3298 3299 3300 3301 3302 3303 3304 3305
            goto cleanup;
        }

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

M
Matthias Bolte 已提交
3306 3307 3308 3309 3310
    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
3311
        if (virVMXFormatSerial(ctx, def->serials[i], &buffer) < 0) {
3312
            goto cleanup;
M
Matthias Bolte 已提交
3313 3314 3315 3316 3317
        }
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
3318
        if (virVMXFormatParallel(ctx, def->parallels[i], &buffer) < 0) {
3319
            goto cleanup;
M
Matthias Bolte 已提交
3320 3321 3322 3323 3324
        }
    }

    /* Get final VMX output */
    if (virBufferError(&buffer)) {
3325
        virReportOOMError();
3326
        goto cleanup;
M
Matthias Bolte 已提交
3327 3328
    }

3329
    vmx = virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
3330

3331 3332 3333 3334 3335
  cleanup:
    if (vmx == NULL) {
        virBufferFreeAndReset(&buffer);
    }

3336 3337
    VIR_FREE(preliminaryDisplayName);
    VIR_FREE(displayName);
3338
    VIR_FREE(annotation);
M
Matthias Bolte 已提交
3339

3340
    return vmx;
M
Matthias Bolte 已提交
3341 3342 3343 3344
}



M
Matthias Bolte 已提交
3345
int
3346
virVMXFormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
3347
{
3348 3349
    const char *listenAddr;

M
Matthias Bolte 已提交
3350
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
3351
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3352 3353 3354
        return -1;
    }

3355
    virBufferAddLit(buffer, "RemoteDisplay.vnc.enabled = \"true\"\n");
M
Matthias Bolte 已提交
3356 3357

    if (def->data.vnc.autoport) {
3358
        VIR_WARN("VNC autoport is enabled, but the automatically assigned "
M
Matthias Bolte 已提交
3359 3360 3361 3362 3363 3364 3365
                  "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);
        }

3366
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.port = \"%d\"\n",
M
Matthias Bolte 已提交
3367 3368 3369
                          def->data.vnc.port);
    }

3370
    if ((listenAddr = virDomainGraphicsListenGetAddress(def, 0))) {
3371
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.ip = \"%s\"\n",
3372
                          listenAddr);
M
Matthias Bolte 已提交
3373 3374 3375
    }

    if (def->data.vnc.keymap != NULL) {
3376
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.keymap = \"%s\"\n",
M
Matthias Bolte 已提交
3377 3378 3379
                          def->data.vnc.keymap);
    }

3380
    if (def->data.vnc.auth.passwd != NULL) {
3381
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.password = \"%s\"\n",
3382
                          def->data.vnc.auth.passwd);
M
Matthias Bolte 已提交
3383 3384 3385 3386 3387
    }

    return 0;
}

M
Matthias Bolte 已提交
3388
int
3389
virVMXFormatDisk(virVMXContext *ctx, virDomainDiskDefPtr def,
3390
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3391
{
3392
    int controllerOrBus, unit;
3393
    const char *vmxDeviceType = NULL;
M
Matthias Bolte 已提交
3394
    char *fileName = NULL;
M
Matthias Bolte 已提交
3395

3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
    /* Convert a handful of types to their string values */
    const char *busType = virDomainDiskBusTypeToString(def->bus);
    const char *deviceType = virDomainDeviceTypeToString(def->device);
    const char *diskType = virDomainDeviceTypeToString(def->type);

    /* If we are dealing with a disk its a .vmdk, otherwise it must be
     * an ISO.
     */
    const char *fileExt = (def->device == VIR_DOMAIN_DISK_DEVICE_DISK) ?
                            ".vmdk" : ".iso";

    /* Check that we got a valid device type */
    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK &&
3409 3410
        def->device != VIR_DOMAIN_DISK_DEVICE_CDROM &&
        def->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
3411 3412
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid device type supplied: %s"), deviceType);
M
Matthias Bolte 已提交
3413 3414 3415
        return -1;
    }

3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
    /* We only support type='file' and type='block' */
    if (def->type != VIR_DOMAIN_DISK_TYPE_FILE &&
        def->type != VIR_DOMAIN_DISK_TYPE_BLOCK) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("%s %s '%s' has unsupported type '%s', expecting "
                         "'%s' or '%s'"), busType, deviceType, def->dst,
                       diskType,
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_FILE),
                       virDomainDiskTypeToString(VIR_DOMAIN_DISK_TYPE_BLOCK));
        return -1;
    }
M
Matthias Bolte 已提交
3427

3428
    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
3429 3430
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3431 3432 3433
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
3434
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
3435
                                          &unit) < 0) {
M
Matthias Bolte 已提交
3436 3437 3438
            return -1;
        }
    } else {
3439
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3440 3441
                       _("Unsupported bus type '%s' for %s"),
                       busType, deviceType);
M
Matthias Bolte 已提交
3442 3443 3444
        return -1;
    }

3445 3446 3447 3448 3449 3450 3451 3452 3453
    if (def->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
        def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
        vmxDeviceType = (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) ?
            "scsi-hardDisk" : "ata-hardDisk";
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
        if (def->type == VIR_DOMAIN_DISK_TYPE_FILE)
            vmxDeviceType = "cdrom-image";
        else
            vmxDeviceType = "atapi-cdrom";
3454 3455
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
        vmxDeviceType = "cdrom-raw";
3456
    } else {
3457
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3458 3459
                       _("%s %s '%s' has an unsupported type '%s'"),
                       busType, deviceType, def->dst, diskType);
M
Matthias Bolte 已提交
3460 3461 3462
        return -1;
    }

3463
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3464 3465 3466
                      busType, controllerOrBus, unit);
    virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"%s\"\n",
                      busType, controllerOrBus, unit, vmxDeviceType);
M
Matthias Bolte 已提交
3467

3468 3469
    if (def->type == VIR_DOMAIN_DISK_TYPE_FILE) {
        if (def->src != NULL && ! virFileHasSuffix(def->src, fileExt)) {
3470
            virReportError(VIR_ERR_INTERNAL_ERROR,
3471 3472 3473 3474
                           _("Image file for %s %s '%s' has "
                             "unsupported suffix, expecting '%s'"),
                           busType, deviceType, def->dst, fileExt);
                return -1;
M
Matthias Bolte 已提交
3475 3476
        }

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

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

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

        VIR_FREE(fileName);
3487
    } else if (def->type == VIR_DOMAIN_DISK_TYPE_BLOCK) {
3488 3489 3490 3491 3492 3493 3494
        if (!def->src &&
            def->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_OPTIONAL) {
            virBufferAsprintf(buffer, "%s%d:%d.autodetect = \"true\"\n",
                              busType, controllerOrBus, unit);
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"auto detect\"\n",
                              busType, controllerOrBus, unit);
        } else {
3495 3496 3497
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
                              busType, controllerOrBus, unit, def->src);
        }
M
Matthias Bolte 已提交
3498 3499 3500 3501
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
3502
            virBufferAsprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
3503
                              busType, controllerOrBus, unit);
M
Matthias Bolte 已提交
3504
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
3505 3506
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("%s harddisk '%s' has unsupported cache mode '%s'"),
3507
                           busType, def->dst,
3508
                           virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
3509
            return -1;
M
Matthias Bolte 已提交
3510 3511 3512 3513 3514 3515 3516
        }
    }

    return 0;
}

int
3517 3518
virVMXFormatFloppy(virVMXContext *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer, bool floppy_present[2])
M
Matthias Bolte 已提交
3519
{
3520
    int unit;
M
Matthias Bolte 已提交
3521
    char *fileName = NULL;
M
Matthias Bolte 已提交
3522 3523

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
3524
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3525 3526 3527
        return -1;
    }

3528
    if (virVMXFloppyDiskNameToUnit(def->dst, &unit) < 0) {
M
Matthias Bolte 已提交
3529 3530 3531
        return -1;
    }

3532 3533
    floppy_present[unit] = true;

3534
    virBufferAsprintf(buffer, "floppy%d.present = \"true\"\n", unit);
M
Matthias Bolte 已提交
3535 3536

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

        if (def->src != NULL) {
3540
            if (! virFileHasSuffix(def->src, ".flp")) {
3541 3542 3543
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Image file for floppy '%s' has unsupported "
                                 "suffix, expecting '.flp'"), def->dst);
M
Matthias Bolte 已提交
3544 3545 3546
                return -1;
            }

3547
            fileName = ctx->formatFileName(def->src, ctx->opaque);
M
Matthias Bolte 已提交
3548 3549 3550 3551 3552

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

3553
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3554
                              unit, fileName);
M
Matthias Bolte 已提交
3555 3556 3557

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

        if (def->src != NULL) {
3562
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3563
                              unit, def->src);
M
Matthias Bolte 已提交
3564
        }
M
Matthias Bolte 已提交
3565
    } else {
3566 3567 3568 3569 3570 3571
        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 已提交
3572 3573 3574 3575 3576 3577 3578 3579
        return -1;
    }

    return 0;
}



3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604
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 已提交
3605
int
3606 3607
virVMXFormatEthernet(virDomainNetDefPtr def, int controller,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3608 3609
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
3610
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
3611 3612

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

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

3621
    /* def:model -> vmx:virtualDev, vmx:features */
M
Matthias Bolte 已提交
3622 3623 3624
    if (def->model != NULL) {
        if (STRCASENEQ(def->model, "vlance") &&
            STRCASENEQ(def->model, "vmxnet") &&
3625
            STRCASENEQ(def->model, "vmxnet2") &&
3626
            STRCASENEQ(def->model, "vmxnet3") &&
M
Matthias Bolte 已提交
3627
            STRCASENEQ(def->model, "e1000")) {
3628 3629 3630 3631
            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 已提交
3632 3633 3634
            return -1;
        }

3635
        if (STRCASEEQ(def->model, "vmxnet2")) {
3636
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"vmxnet\"\n",
3637
                              controller);
3638
            virBufferAsprintf(buffer, "ethernet%d.features = \"15\"\n",
3639 3640
                              controller);
        } else {
3641
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
3642 3643
                              controller, def->model);
        }
M
Matthias Bolte 已提交
3644 3645 3646 3647 3648
    }

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

        if (def->ifname != NULL) {
3654
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"custom\"\n",
M
Matthias Bolte 已提交
3655
                              controller);
3656
            virBufferAsprintf(buffer, "ethernet%d.vnet = \"%s\"\n",
M
Matthias Bolte 已提交
3657 3658
                              controller, def->ifname);
        } else {
3659
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"bridged\"\n",
M
Matthias Bolte 已提交
3660 3661 3662 3663 3664
                              controller);
        }

        break;

3665 3666 3667 3668 3669
      case VIR_DOMAIN_NET_TYPE_USER:
        virBufferAsprintf(buffer, "ethernet%d.connectionType = \"nat\"\n",
                          controller);
        break;

M
Matthias Bolte 已提交
3670
      default:
3671 3672
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported net type '%s'"),
                       virDomainNetTypeToString(def->type));
M
Matthias Bolte 已提交
3673 3674 3675
        return -1;
    }

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

3679 3680
    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];
3681 3682

    if (prefix == 0x000c29) {
3683
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
M
Matthias Bolte 已提交
3684
                          controller);
3685
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
M
Matthias Bolte 已提交
3686
                          controller, mac_string);
3687
        virBufferAsprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
M
Matthias Bolte 已提交
3688
                          controller);
3689
    } else if (prefix == 0x005056 && suffix <= 0x3fffff) {
3690
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3691
                          controller);
3692
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3693 3694
                          controller, mac_string);
    } else if (prefix == 0x005056 && suffix >= 0x800000 && suffix <= 0xbfffff) {
3695
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"vpx\"\n",
3696
                          controller);
3697
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
3698
                          controller, mac_string);
M
Matthias Bolte 已提交
3699
    } else {
3700
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3701
                          controller);
3702
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3703
                          controller, mac_string);
3704
        virBufferAsprintf(buffer, "ethernet%d.checkMACAddress = \"false\"\n",
3705
                          controller);
M
Matthias Bolte 已提交
3706 3707 3708 3709 3710 3711 3712 3713
    }

    return 0;
}



int
3714 3715
virVMXFormatSerial(virVMXContext *ctx, virDomainChrDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
3716
{
M
Matthias Bolte 已提交
3717
    char *fileName = NULL;
3718
    const char *protocol;
M
Matthias Bolte 已提交
3719

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

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

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

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

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

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

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

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

      case VIR_DOMAIN_CHR_TYPE_PIPE:
3755
        virBufferAsprintf(buffer, "serial%d.fileType = \"pipe\"\n",
3756
                          def->target.port);
M
Matthias Bolte 已提交
3757
        /* FIXME: Based on VI Client GUI default */
3758
        virBufferAsprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
3759
                          def->target.port);
M
Matthias Bolte 已提交
3760
        /* FIXME: Based on VI Client GUI default */
3761
        virBufferAsprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
3762
                          def->target.port);
3763
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3764
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3765 3766
        break;

3767
      case VIR_DOMAIN_CHR_TYPE_TCP:
3768
        switch (def->source.data.tcp.protocol) {
3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785
          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:
3786 3787 3788 3789
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported character device TCP protocol '%s'"),
                           virDomainChrTcpProtocolTypeToString(
                               def->source.data.tcp.protocol));
3790 3791 3792
            return -1;
        }

3793
        virBufferAsprintf(buffer, "serial%d.fileType = \"network\"\n",
3794
                          def->target.port);
3795
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s://%s:%s\"\n",
3796 3797
                          def->target.port, protocol, def->source.data.tcp.host,
                          def->source.data.tcp.service);
3798
        virBufferAsprintf(buffer, "serial%d.network.endPoint = \"%s\"\n",
3799
                          def->target.port,
3800
                          def->source.data.tcp.listen ? "server" : "client");
3801 3802
        break;

M
Matthias Bolte 已提交
3803
      default:
3804 3805 3806
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
                       virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3807 3808 3809 3810 3811
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
3812
    virBufferAsprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
3813
                      def->target.port);
M
Matthias Bolte 已提交
3814 3815 3816 3817 3818 3819 3820

    return 0;
}



int
3821 3822
virVMXFormatParallel(virVMXContext *ctx, virDomainChrDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3823
{
M
Matthias Bolte 已提交
3824 3825
    char *fileName = NULL;

3826
    if (def->target.port < 0 || def->target.port > 2) {
3827 3828 3829
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"),
                       def->target.port);
M
Matthias Bolte 已提交
3830 3831 3832
        return -1;
    }

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

M
Matthias Bolte 已提交
3836
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3837
    switch (def->source.type) {
M
Matthias Bolte 已提交
3838
      case VIR_DOMAIN_CHR_TYPE_DEV:
3839
        virBufferAsprintf(buffer, "parallel%d.fileType = \"device\"\n",
3840
                          def->target.port);
3841
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3842
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3843 3844 3845
        break;

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

3849
        fileName = ctx->formatFileName(def->source.data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3850 3851 3852 3853 3854

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

3855
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3856
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3857 3858

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3859 3860 3861
        break;

      default:
3862 3863 3864
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
                       virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3865 3866 3867 3868 3869
        return -1;
    }

    return 0;
}
3870 3871 3872 3873 3874 3875



int
virVMXFormatSVGA(virDomainVideoDefPtr def, virBufferPtr buffer)
{
3876 3877
    unsigned long long vram;

3878
    if (def->type != VIR_DOMAIN_VIDEO_TYPE_VMVGA) {
3879 3880 3881
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported video device type '%s'"),
                       virDomainVideoTypeToString(def->type));
3882 3883 3884 3885 3886 3887 3888
        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
     */
3889
    vram = VIR_DIV_UP(def->vram, 64) * 64;
3890 3891

    if (def->heads > 1) {
3892 3893
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Multi-head video devices are unsupported"));
3894 3895 3896
        return -1;
    }

3897
    virBufferAsprintf(buffer, "svga.vramSize = \"%lld\"\n",
3898
                      vram * 1024); /* kilobyte to byte */
3899 3900 3901

    return 0;
}