vmx.c 135.6 KB
Newer Older
1
/*
2
 * vmx.c: VMware VMX parsing/formatting functions
3
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
M
Matthias Bolte 已提交
5
 * Copyright (C) 2009-2011, 2014-2015 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
VIR_LOG_INIT("vmx.vmx");

40 41
/*

42
 mapping:
43 44 45 46 47

domain-xml                        <=>   vmx


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


???                               <=>   guestOS = "<value>"                     # essential, FIXME: not representable
def->id = <value>                 <=>   ???                                     # not representable
def->uuid = <value>               <=>   uuid.bios = "<value>"
def->name = <value>               <=>   displayName = "<value>"
59 60
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
61
def->mem.min_guarantee = <value kilobyte>  <=>   sched.mem.minsize = "<value megabyte>"  # defaults to 0
P
Pino Toscano 已提交
62
def->maxvcpus = <value>           <=>   numvcpus = "<value>"                    # must be greater than 0, defaults to 1
63
def->cpumask = <uint list>        <=>   sched.cpu.affinity = "<uint list>"
64 65
def->cputune.shares = <value>     <=>   sched.cpu.shares = "<value>"            # with handling for special values
                                                                                # "high", "normal", "low"
66 67 68 69 70 71 72 73 74



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

def->os

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



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

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

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


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

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


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

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



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



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


                                        ethernet0.addressType = "generated"     # default to "generated"
->mac = <value>                   <=>   ethernet0.generatedAddress = "<value>"
309
                                        ethernet0.generatedAddressOffset = "0"  # ?
310 311 312 313 314


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

315 316 317 318

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

319 320 321 322 323

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

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


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

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


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

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


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

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



359 360 361 362 363 364 365 366 367 368
################################################################################
## video #######################################################################

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



369 370 371 372 373 374 375 376 377
################################################################################
## serials #####################################################################

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

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

def->serials[0]...
378
->target.port = <port>
379 380 381 382 383 384 385


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


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


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

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


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

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


427 428 429 430 431 432 433 434
## 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>

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

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

450
->data.tcp.listen = false         <=>   serial0.network.endPoint = "client"     # defaults to "server"
451 452 453 454 455 456

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


457 458 459 460 461 462 463 464 465 466

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

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

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

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


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


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

498 499
*/

500
#define VIR_FROM_THIS VIR_FROM_NONE
501

502
#define VMX_BUILD_NAME_EXTRA(_suffix, _extra) \
503 504
    snprintf(_suffix##_name, sizeof(_suffix##_name), "%s."_extra, prefix);

505
#define VMX_BUILD_NAME(_suffix) \
506
    VMX_BUILD_NAME_EXTRA(_suffix, #_suffix)
507

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


527 528 529
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Helpers
 */
530

531
static int
532
virVMXDomainDefPostParse(virDomainDefPtr def ATTRIBUTE_UNUSED,
533
                         virCapsPtr caps ATTRIBUTE_UNUSED,
534
                         unsigned int parseFlags ATTRIBUTE_UNUSED,
535 536
                         void *opaque ATTRIBUTE_UNUSED,
                         void *parseOpaque ATTRIBUTE_UNUSED)
537 538 539 540 541
{
    return 0;
}

static int
542 543 544
virVMXDomainDevicesDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
                                const virDomainDef *def ATTRIBUTE_UNUSED,
                                virCapsPtr caps ATTRIBUTE_UNUSED,
545
                                unsigned int parseFlags ATTRIBUTE_UNUSED,
546 547
                                void *opaque ATTRIBUTE_UNUSED,
                                void *parseOpaque ATTRIBUTE_UNUSED)
548 549 550
{
    return 0;
}
551

552
static virDomainDefParserConfig virVMXDomainDefParserConfig = {
553
    .macPrefix = {0x00, 0x0c, 0x29},
554 555
    .devicesPostParseCallback = virVMXDomainDevicesDefPostParse,
    .domainPostParseCallback = virVMXDomainDefPostParse,
556
    .features = (VIR_DOMAIN_DEF_FEATURE_WIDE_SCSI |
557 558
                 VIR_DOMAIN_DEF_FEATURE_NAME_SLASH |
                 VIR_DOMAIN_DEF_FEATURE_NO_BOOT_ORDER),
559 560
};

561 562 563 564 565
struct virVMXDomainDefNamespaceData {
    char *datacenterPath;
    char *moref;
};

566 567 568
static void
virVMXDomainDefNamespaceFree(void *nsdata)
{
569 570 571 572 573 574 575
    struct virVMXDomainDefNamespaceData *data = nsdata;

    if (data) {
        VIR_FREE(data->datacenterPath);
        VIR_FREE(data->moref);
    }
    VIR_FREE(data);
576 577 578 579 580
}

static int
virVMXDomainDefNamespaceFormatXML(virBufferPtr buf, void *nsdata)
{
581
    struct virVMXDomainDefNamespaceData *data = nsdata;
582

583
    if (!data)
584 585
        return 0;

586 587 588 589 590 591 592 593 594 595
    if (data->datacenterPath) {
        virBufferAddLit(buf, "<vmware:datacenterpath>");
        virBufferEscapeString(buf, "%s", data->datacenterPath);
        virBufferAddLit(buf, "</vmware:datacenterpath>\n");
    }
    if (data->moref) {
        virBufferAddLit(buf, "<vmware:moref>");
        virBufferEscapeString(buf, "%s", data->moref);
        virBufferAddLit(buf, "</vmware:moref>\n");
    }
596 597 598 599 600 601 602

    return 0;
}

static const char *
virVMXDomainDefNamespaceHref(void)
{
603
    return "http://libvirt.org/schemas/domain/vmware/1.0";
604 605
}

606
static virXMLNamespace virVMXDomainXMLNamespace = {
607 608 609
    .parse = NULL,
    .free = virVMXDomainDefNamespaceFree,
    .format = virVMXDomainDefNamespaceFormatXML,
610
    .prefix = "vmware",
611 612
    .href = virVMXDomainDefNamespaceHref,
};
613 614 615 616

virDomainXMLOptionPtr
virVMXDomainXMLConfInit(void)
{
617
    return virDomainXMLOptionNew(&virVMXDomainDefParserConfig, NULL,
618
                                 &virVMXDomainXMLNamespace, NULL, NULL);
619 620
}

621 622 623 624 625 626 627 628 629 630
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') {
631
        if (*tmp1 == escape || strspn(tmp1, special) > 0)
632 633 634 635 636 637
            length += 2;

        ++tmp1;
        ++length;
    }

638
    if (VIR_ALLOC_N(escaped, length) < 0)
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
        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 已提交
666
int
667 668 669 670 671 672 673 674
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) {
675
            if (!c_isxdigit(tmp1[1]) || !c_isxdigit(tmp1[2]))
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
                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) {
703 704
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxml2 doesn't handle %s encoding"), encoding);
705 706 707 708 709 710 711
        return NULL;
    }

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

    if (xmlCharEncInFunc(handler, utf8, input) < 0) {
712 713
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not convert from %s to UTF-8 encoding"), encoding);
714 715 716 717 718 719
        goto cleanup;
    }

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

720
 cleanup:
721 722 723 724 725 726 727 728 729 730
    xmlCharEncCloseFunc(handler);
    xmlBufferFree(input);
    xmlBufferFree(utf8);

    return result;
}



static int
731 732
virVMXGetConfigStringHelper(virConfPtr conf, const char *name, char **string,
                            bool optional)
733
{
734
    int rc;
735 736
    *string = NULL;

737 738 739
    rc = virConfGetValueString(conf, name, string);
    if (rc == 1 && *string != NULL)
        return 1;
740

741 742
    if (optional)
        return 0;
743

744 745 746 747
    virReportError(VIR_ERR_INTERNAL_ERROR,
                   _("Missing essential config entry '%s'"), name);
    return -1;
}
748 749


750 751 752 753 754 755 756 757

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

    if (virVMXGetConfigStringHelper(conf, name, string, optional) < 0)
758 759
        return -1;

760
    return 0;
761 762 763 764 765 766 767 768
}



static int
virVMXGetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid,
                    bool optional)
{
769 770 771
    char *string = NULL;
    int ret = -1;
    int rc;
772

773 774 775
    rc = virVMXGetConfigStringHelper(conf, name, &string, optional);
    if (rc <= 0)
        return rc;
776

777 778
    rc = virUUIDParse(string, uuid);
    if (rc < 0) {
779
        virReportError(VIR_ERR_INTERNAL_ERROR,
780 781
                       _("Could not parse UUID from string '%s'"), string);
        goto cleanup;
782 783
    }

784
    ret = 0;
785

786 787 788
 cleanup:
    VIR_FREE(string);
    return ret;
789 790 791 792 793 794 795 796
}



static int
virVMXGetConfigLong(virConfPtr conf, const char *name, long long *number,
                    long long default_, bool optional)
{
797 798 799
    char *string = NULL;
    int ret = -1;
    int rc;
800 801 802

    *number = default_;

803 804 805
    rc = virVMXGetConfigStringHelper(conf, name, &string, optional);
    if (rc <= 0)
        return rc;
806

807 808 809
    if (STRCASEEQ(string, "unlimited")) {
        *number = -1;
    } else if (virStrToLong_ll(string, NULL, 10, number) < 0) {
810
        virReportError(VIR_ERR_INTERNAL_ERROR,
811 812 813
                _("Config entry '%s' must represent an integer value"),
                name);
        goto cleanup;
814 815
    }

816 817 818 819 820
    ret = 0;

 cleanup:
    VIR_FREE(string);
    return ret;
821 822 823 824 825 826 827 828
}



static int
virVMXGetConfigBoolean(virConfPtr conf, const char *name, bool *boolean_,
                       bool default_, bool optional)
{
829 830 831
    char *string = NULL;
    int ret = -1;
    int rc;
832 833 834

    *boolean_ = default_;

835 836 837
    rc = virVMXGetConfigStringHelper(conf, name, &string, optional);
    if (rc <= 0)
        return rc;
838

839 840 841 842
    if (STRCASEEQ(string, "true")) {
        *boolean_ = 1;
    } else if (STRCASEEQ(string, "false")) {
        *boolean_ = 0;
843
    } else {
844
        virReportError(VIR_ERR_INTERNAL_ERROR,
845 846 847
                       _("Config entry '%s' must represent a boolean value "
                         "(true|false)"), name);
        goto cleanup;
848 849
    }

850 851 852 853 854
    ret = 0;

 cleanup:
    VIR_FREE(string);
    return ret;
855 856 857 858 859 860
}



static int
virVMXSCSIDiskNameToControllerAndUnit(const char *name, int *controller, int *unit)
M
Matthias Bolte 已提交
861 862 863 864
{
    int idx;

    if (! STRPREFIX(name, "sd")) {
865 866 867
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'sd'"));
M
Matthias Bolte 已提交
868 869 870 871 872 873
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
874 875
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
876 877 878
        return -1;
    }

879
    /* Each of the 4 SCSI controllers has 1 bus with 15 units each for devices */
M
Matthias Bolte 已提交
880
    if (idx >= (4 * 15)) {
881 882
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
883 884 885 886
        return -1;
    }

    *controller = idx / 15;
887
    *unit = idx % 15;
M
Matthias Bolte 已提交
888

889
    /* Skip the controller ifself at unit 7 */
890
    if (*unit >= 7)
891
        ++(*unit);
M
Matthias Bolte 已提交
892 893 894 895 896 897

    return 0;
}



898 899
static int
virVMXIDEDiskNameToBusAndUnit(const char *name, int *bus, int *unit)
M
Matthias Bolte 已提交
900 901 902 903
{
    int idx;

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

    idx = virDiskNameToIndex(name);

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

918
    /* The IDE controller has 2 buses with 2 units each for devices */
M
Matthias Bolte 已提交
919
    if (idx >= (2 * 2)) {
920 921
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("IDE disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
922 923 924
        return -1;
    }

925 926
    *bus = idx / 2;
    *unit = idx % 2;
M
Matthias Bolte 已提交
927 928 929 930 931 932

    return 0;
}



933 934
static int
virVMXFloppyDiskNameToUnit(const char *name, int *unit)
M
Matthias Bolte 已提交
935 936 937 938
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
939 940 941
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
942 943 944 945 946 947
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
948 949
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
950 951 952
        return -1;
    }

953
    /* The FDC controller has 1 bus with 2 units for devices */
M
Matthias Bolte 已提交
954
    if (idx >= 2) {
955 956
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Floppy disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
957 958 959
        return -1;
    }

960 961 962 963 964 965 966
    *unit = idx;

    return 0;
}



967
static int
968 969 970
virVMXVerifyDiskAddress(virDomainXMLOptionPtr xmlopt,
                        virDomainDiskDefPtr disk,
                        virDomainDefPtr vmdef)
971 972 973 974 975 976 977
{
    virDomainDiskDef def;
    virDomainDeviceDriveAddressPtr drive;

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

    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
978 979 980
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported disk address type '%s'"),
                       virDomainDeviceAddressTypeToString(disk->info.type));
981 982 983 984 985 986 987 988
        return -1;
    }

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

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

989
    if (virDomainDiskDefAssignAddress(xmlopt, &def, vmdef) < 0) {
990 991
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not verify disk address"));
992 993 994 995 996 997
        return -1;
    }

    if (def.info.addr.drive.controller != drive->controller ||
        def.info.addr.drive.bus != drive->bus ||
        def.info.addr.drive.unit != drive->unit) {
998 999 1000
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Disk address %d:%d:%d doesn't match target device '%s'"),
                       drive->controller, drive->bus, drive->unit, disk->dst);
1001 1002 1003 1004 1005 1006
        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) {
1007 1008 1009
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI controller index %d out of [0..3] range"),
                           drive->controller);
1010 1011 1012 1013
            return -1;
        }

        if (drive->bus != 0) {
1014 1015 1016
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI bus index %d out of [0] range"),
                           drive->bus);
1017 1018 1019 1020
            return -1;
        }

        if (drive->unit > 15 || drive->unit == 7) {
1021 1022 1023
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI unit index %d out of [0..6,8..15] range"),
                           drive->unit);
1024 1025 1026 1027
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_IDE) {
        if (drive->controller != 0) {
1028 1029 1030
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE controller index %d out of [0] range"),
                           drive->controller);
1031 1032 1033 1034
            return -1;
        }

        if (drive->bus > 1) {
1035 1036 1037
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE bus index %d out of [0..1] range"),
                           drive->bus);
1038 1039 1040 1041
            return -1;
        }

        if (drive->unit > 1) {
1042 1043 1044
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE unit index %d out of [0..1] range"),
                           drive->unit);
1045 1046 1047 1048
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
        if (drive->controller != 0) {
1049 1050 1051
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC controller index %d out of [0] range"),
                           drive->controller);
1052 1053 1054 1055
            return -1;
        }

        if (drive->bus != 0) {
1056 1057 1058
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC bus index %d out of [0] range"),
                           drive->bus);
1059 1060 1061 1062
            return -1;
        }

        if (drive->unit > 1) {
1063 1064 1065
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC unit index %d out of [0..1] range"),
                           drive->unit);
1066 1067 1068
            return -1;
        }
    } else {
1069 1070 1071
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported bus type '%s'"),
                       virDomainDiskBusTypeToString(disk->bus));
1072 1073 1074 1075 1076 1077 1078 1079
        return -1;
    }

    return 0;
}



1080 1081 1082
static int
virVMXHandleLegacySCSIDiskDriverName(virDomainDefPtr def,
                                     virDomainDiskDefPtr disk)
1083 1084
{
    char *tmp;
1085 1086
    int model;
    size_t i;
1087
    virDomainControllerDefPtr controller = NULL;
1088 1089
    const char *driver = virDomainDiskGetDriver(disk);
    char *copy;
1090

1091
    if (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI || !driver)
1092 1093
        return 0;

1094 1095 1096
    if (VIR_STRDUP(copy, driver) < 0)
        return -1;
    tmp = copy;
1097

1098
    for (; *tmp != '\0'; ++tmp)
1099 1100
        *tmp = c_tolower(*tmp);

1101 1102
    model = virDomainControllerModelSCSITypeFromString(copy);
    VIR_FREE(copy);
1103 1104

    if (model < 0) {
1105
        virReportError(VIR_ERR_INTERNAL_ERROR,
1106
                       _("Unknown driver name '%s'"), driver);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
        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) {
1118 1119 1120
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing SCSI controller for index %d"),
                       disk->info.addr.drive.controller);
1121 1122 1123
        return -1;
    }

1124
    if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_DEFAULT) {
1125 1126
        controller->model = model;
    } else if (controller->model != model) {
1127 1128
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Inconsistent SCSI controller model ('%s' is not '%s') "
1129
                         "for SCSI controller index %d"), driver,
1130 1131
                       virDomainControllerModelSCSITypeToString(controller->model),
                       controller->idx);
1132 1133
        return -1;
    }
M
Matthias Bolte 已提交
1134 1135 1136 1137 1138 1139

    return 0;
}



1140 1141 1142
static int
virVMXGatherSCSIControllers(virVMXContext *ctx, virDomainDefPtr def,
                            int virtualDev[4], bool present[4])
1143 1144
{
    int result = -1;
1145
    size_t i, k;
M
Matthias Bolte 已提交
1146
    virDomainDiskDefPtr disk;
1147 1148 1149 1150
    virDomainControllerDefPtr controller;
    bool controllerHasDisksAttached;
    int count = 0;
    int *autodetectedModels;
M
Matthias Bolte 已提交
1151

1152
    if (VIR_ALLOC_N(autodetectedModels, def->ndisks) < 0)
1153 1154 1155 1156
        return -1;

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

1158
        if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1159
            /* skip non-SCSI controllers */
M
Matthias Bolte 已提交
1160 1161 1162
            continue;
        }

1163
        controllerHasDisksAttached = false;
1164

1165 1166 1167 1168 1169 1170
        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;
1171 1172
                break;
            }
M
Matthias Bolte 已提交
1173 1174
        }

1175
        if (! controllerHasDisksAttached) {
1176
            /* skip SCSI controllers without attached disks */
1177 1178 1179
            continue;
        }

1180
        if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_AUTO &&
1181
            ctx->autodetectSCSIControllerModel != NULL) {
1182 1183
            count = 0;

1184 1185
            /* try to autodetect the SCSI controller model by collecting
             * SCSI controller model of all disks attached to this controller */
1186 1187 1188 1189 1190
            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) {
1191 1192 1193
                    if (ctx->autodetectSCSIControllerModel
                               (disk, &autodetectedModels[count],
                                ctx->opaque) < 0) {
1194 1195 1196 1197 1198 1199 1200
                        goto cleanup;
                    }

                    ++count;
                }
            }

1201 1202
            /* autodetection fails when the disks attached to one controller
             * have inconsistent SCSI controller models */
1203 1204
            for (k = 0; k < count; ++k) {
                if (autodetectedModels[k] != autodetectedModels[0]) {
1205 1206 1207 1208
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Disks on SCSI controller %d have inconsistent "
                                     "controller models, cannot autodetect model"),
                                   controller->idx);
1209 1210 1211 1212 1213
                    goto cleanup;
                }
            }

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

1216
        if (controller->model != -1 &&
1217 1218 1219 1220
            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) {
1221 1222 1223 1224 1225
            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));
1226
            goto cleanup;
M
Matthias Bolte 已提交
1227
        }
1228 1229 1230

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

1233 1234
    result = 0;

1235
 cleanup:
1236 1237 1238
    VIR_FREE(autodetectedModels);

    return result;
M
Matthias Bolte 已提交
1239 1240
}

P
Pino Toscano 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
struct virVMXConfigScanResults {
    int networks_max_index;
};

static int
virVMXConfigScanResultsCollector(const char* name,
                                 virConfValuePtr value ATTRIBUTE_UNUSED,
                                 void *opaque)
{
    struct virVMXConfigScanResults *results = opaque;

    if (STRCASEPREFIX(name, "ethernet")) {
        unsigned int idx;
        char *p;

        if (virStrToLong_uip(name + 8, &p, 10, &idx) < 0 ||
            *p != '.') {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("failed to parse the index of the VMX key '%s'"),
                           name);
            return -1;
        }

1264 1265
        if ((int)idx > results->networks_max_index)
            results->networks_max_index = (int)idx;
P
Pino Toscano 已提交
1266 1267 1268 1269 1270
    }

    return 0;
}

M
Matthias Bolte 已提交
1271 1272 1273 1274 1275 1276


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

1277
virDomainDefPtr
1278 1279
virVMXParseConfig(virVMXContext *ctx,
                  virDomainXMLOptionPtr xmlopt,
1280
                  virCapsPtr caps,
1281
                  const char *vmx)
1282
{
M
Matthias Bolte 已提交
1283
    bool success = false;
1284
    virConfPtr conf = NULL;
1285 1286
    char *encoding = NULL;
    char *utf8;
1287 1288 1289 1290
    virDomainDefPtr def = NULL;
    long long config_version = 0;
    long long virtualHW_version = 0;
    long long memsize = 0;
1291 1292
    long long sched_mem_max = 0;
    long long sched_mem_minsize = 0;
1293 1294
    long long numvcpus = 0;
    char *sched_cpu_affinity = NULL;
1295
    char *sched_cpu_shares = NULL;
M
Matthias Bolte 已提交
1296
    char *guestOS = NULL;
M
Matthias Bolte 已提交
1297
    bool smbios_reflecthost = false;
1298
    int controller;
1299
    int bus;
1300
    int port;
1301
    bool present;
1302 1303
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
    int unit;
1304 1305
    bool hgfs_disabled = true;
    long long sharedFolder_maxNum = 0;
P
Pino Toscano 已提交
1306
    struct virVMXConfigScanResults results = { -1 };
1307 1308
    long long coresPerSocket = 0;
    virCPUDefPtr cpu = NULL;
1309
    char *firmware = NULL;
1310

1311
    if (ctx->parseFileName == NULL) {
1312 1313
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no parseFileName function set"));
1314 1315 1316
        return NULL;
    }

J
Ján Tomko 已提交
1317
    conf = virConfReadString(vmx, VIR_CONF_FLAG_VMX_FORMAT);
1318

1319
    if (conf == NULL)
1320 1321
        return NULL;

1322
    /* vmx:.encoding */
1323
    if (virVMXGetConfigString(conf, ".encoding", &encoding, true) < 0)
1324 1325 1326 1327 1328 1329 1330 1331
        goto cleanup;

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

1332
        utf8 = virVMXConvertToUTF8(encoding, vmx);
1333

1334
        if (utf8 == NULL)
1335 1336
            goto cleanup;

J
Ján Tomko 已提交
1337
        conf = virConfReadString(utf8, VIR_CONF_FLAG_VMX_FORMAT);
1338 1339 1340

        VIR_FREE(utf8);

1341
        if (conf == NULL)
1342 1343 1344
            goto cleanup;
    }

P
Pino Toscano 已提交
1345 1346 1347
    if (virConfWalk(conf, virVMXConfigScanResultsCollector, &results) < 0)
        goto cleanup;

1348
    /* Allocate domain def */
1349
    if (!(def = virDomainDefNew()))
A
ajia@redhat.com 已提交
1350
        goto cleanup;
1351

1352
    def->virtType = VIR_DOMAIN_VIRT_VMWARE;
1353 1354
    def->id = -1;

M
Matthias Bolte 已提交
1355
    /* vmx:config.version */
1356 1357
    if (virVMXGetConfigLong(conf, "config.version", &config_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1358
        goto cleanup;
1359 1360 1361
    }

    if (config_version != 8) {
1362 1363 1364
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'config.version' to be 8 but found "
                         "%lld"), config_version);
M
Matthias Bolte 已提交
1365
        goto cleanup;
1366 1367
    }

M
Matthias Bolte 已提交
1368
    /* vmx:virtualHW.version */
1369 1370
    if (virVMXGetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1371
        goto cleanup;
1372 1373
    }

1374
    if (virtualHW_version < 4) {
1375
        virReportError(VIR_ERR_INTERNAL_ERROR,
1376
                       _("Expecting VMX entry 'virtualHW.version' to be "
1377
                         "4 or higher but found %lld"),
1378
                       virtualHW_version);
M
Matthias Bolte 已提交
1379
        goto cleanup;
1380 1381
    }

M
Matthias Bolte 已提交
1382
    /* vmx:uuid.bios -> def:uuid */
1383
    /* FIXME: Need to handle 'uuid.action = "create"' */
1384
    if (virVMXGetConfigUUID(conf, "uuid.bios", def->uuid, true) < 0)
M
Matthias Bolte 已提交
1385
        goto cleanup;
1386

M
Matthias Bolte 已提交
1387
    /* vmx:displayName -> def:name */
1388
    if (virVMXGetConfigString(conf, "displayName", &def->name, true) < 0)
M
Matthias Bolte 已提交
1389
        goto cleanup;
1390

1391
    if (def->name != NULL) {
1392 1393
        if (virVMXUnescapeHexPercent(def->name) < 0 ||
            virVMXUnescapeHexPipe(def->name) < 0) {
1394 1395
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'name' contains invalid escape sequence"));
1396 1397 1398 1399
            goto cleanup;
        }
    }

1400
    /* vmx:annotation -> def:description */
1401 1402
    if (virVMXGetConfigString(conf, "annotation", &def->description,
                              true) < 0) {
1403 1404 1405 1406
        goto cleanup;
    }

    if (def->description != NULL) {
1407
        if (virVMXUnescapeHexPipe(def->description) < 0) {
1408 1409 1410
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'annotation' contains invalid escape "
                             "sequence"));
1411
            goto cleanup;
1412 1413 1414
        }
    }

1415
    /* vmx:memsize -> def:mem.max_balloon */
1416
    if (virVMXGetConfigLong(conf, "memsize", &memsize, 32, true) < 0)
M
Matthias Bolte 已提交
1417
        goto cleanup;
1418 1419

    if (memsize <= 0 || memsize % 4 != 0) {
1420 1421 1422
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'memsize' to be an unsigned "
                         "integer (multiple of 4) but found %lld"), memsize);
M
Matthias Bolte 已提交
1423
        goto cleanup;
1424 1425
    }

1426
    virDomainDefSetMemoryTotal(def, memsize * 1024); /* Scale from megabytes to kilobytes */
1427

1428
    /* vmx:sched.mem.max -> def:mem.cur_balloon */
1429 1430
    if (virVMXGetConfigLong(conf, "sched.mem.max", &sched_mem_max, memsize,
                            true) < 0) {
M
Matthias Bolte 已提交
1431
        goto cleanup;
1432 1433
    }

1434
    if (sched_mem_max < 0)
1435
        sched_mem_max = memsize;
1436

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

1439 1440
    if (def->mem.cur_balloon > virDomainDefGetMemoryTotal(def))
        def->mem.cur_balloon = virDomainDefGetMemoryTotal(def);
1441

1442
    /* vmx:sched.mem.minsize -> def:mem.min_guarantee */
1443 1444
    if (virVMXGetConfigLong(conf, "sched.mem.minsize", &sched_mem_minsize, 0,
                            true) < 0) {
1445 1446 1447
        goto cleanup;
    }

1448
    if (sched_mem_minsize < 0)
1449 1450 1451 1452
        sched_mem_minsize = 0;

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

1453 1454
    if (def->mem.min_guarantee > virDomainDefGetMemoryTotal(def))
        def->mem.min_guarantee = virDomainDefGetMemoryTotal(def);
1455

M
Matthias Bolte 已提交
1456
    /* vmx:numvcpus -> def:vcpus */
1457
    if (virVMXGetConfigLong(conf, "numvcpus", &numvcpus, 1, true) < 0)
M
Matthias Bolte 已提交
1458
        goto cleanup;
1459

P
Pino Toscano 已提交
1460
    if (numvcpus <= 0) {
1461 1462
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'numvcpus' to be an unsigned "
P
Pino Toscano 已提交
1463
                         "integer greater than 0 but found %lld"), numvcpus);
M
Matthias Bolte 已提交
1464
        goto cleanup;
1465 1466
    }

1467
    if (virDomainDefSetVcpusMax(def, numvcpus, xmlopt) < 0)
1468 1469
        goto cleanup;

1470 1471
    if (virDomainDefSetVcpus(def, numvcpus) < 0)
        goto cleanup;
1472

1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    /* vmx:cpuid.coresPerSocket -> def:cpu */
    if (virVMXGetConfigLong(conf, "cpuid.coresPerSocket", &coresPerSocket, 1,
                            true) < 0)
        goto cleanup;

    if (coresPerSocket > 1) {
        if (VIR_ALLOC(cpu) < 0)
            goto cleanup;

        cpu->type = VIR_CPU_TYPE_GUEST;
        cpu->mode = VIR_CPU_MODE_CUSTOM;

        cpu->sockets = numvcpus / coresPerSocket;
        if (cpu->sockets <= 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'cpuid.coresPerSocket' smaller than "
                             "'numvcpus'"));
            goto cleanup;
        }
        cpu->cores = coresPerSocket;
        cpu->threads = 1;

        VIR_STEAL_PTR(def->cpu, cpu);
    }

M
Matthias Bolte 已提交
1498
    /* vmx:sched.cpu.affinity -> def:cpumask */
1499
    /* NOTE: maps to VirtualMachine:config.cpuAffinity.affinitySet */
1500 1501
    if (virVMXGetConfigString(conf, "sched.cpu.affinity", &sched_cpu_affinity,
                              true) < 0) {
M
Matthias Bolte 已提交
1502
        goto cleanup;
1503 1504
    }

1505
    if (sched_cpu_affinity != NULL && STRCASENEQ(sched_cpu_affinity, "all")) {
1506 1507 1508
        VIR_AUTOSTRINGLIST afflist = NULL;
        char **aff;
        size_t naffs;
1509

H
Hu Tao 已提交
1510
        def->cpumask = virBitmapNew(VIR_DOMAIN_CPUMASK_LEN);
1511
        if (!def->cpumask)
M
Matthias Bolte 已提交
1512
            goto cleanup;
1513

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
        if (!(afflist = virStringSplitCount(sched_cpu_affinity, ",", 0, &naffs)))
            goto cleanup;

        if (naffs < numvcpus) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry 'sched.cpu.affinity' to contain "
                             "at least as many values as 'numvcpus' (%lld) but "
                             "found only %zu value(s)"), numvcpus, naffs);
            goto cleanup;
        }
1524

1525 1526 1527 1528
        for (aff = afflist; *aff; aff++) {
            const char *current = *aff;
            unsigned int number;
            int rc;
1529

1530 1531 1532 1533 1534
            virSkipSpaces(&current);
            rc = virStrToLong_uip(current, (char **) &current, 10, &number);
            virSkipSpaces(&current);

            if (rc < 0 || *current != '\0') {
1535 1536 1537 1538
                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 已提交
1539
                goto cleanup;
1540 1541 1542
            }

            if (number >= VIR_DOMAIN_CPUMASK_LEN) {
1543 1544 1545
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("VMX entry 'sched.cpu.affinity' contains a %d, "
                                 "this value is too large"), number);
M
Matthias Bolte 已提交
1546
                goto cleanup;
1547 1548
            }

H
Hu Tao 已提交
1549
            ignore_value(virBitmapSetBit(def->cpumask, number));
1550 1551 1552
        }
    }

1553 1554 1555 1556 1557 1558 1559
    /* 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) {
1560
        unsigned int vcpus = virDomainDefGetVcpus(def);
1561 1562
        /* See http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.SharesInfo.Level.html */
        if (STRCASEEQ(sched_cpu_shares, "low")) {
1563
            def->cputune.shares = vcpus * 500;
1564
        } else if (STRCASEEQ(sched_cpu_shares, "normal")) {
1565
            def->cputune.shares = vcpus * 1000;
1566
        } else if (STRCASEEQ(sched_cpu_shares, "high")) {
1567
            def->cputune.shares = vcpus * 2000;
1568 1569
        } else if (virStrToLong_ull(sched_cpu_shares, NULL, 10,
                                    &def->cputune.shares) < 0) {
1570 1571 1572 1573
            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);
1574 1575
            goto cleanup;
        }
1576
        def->cputune.sharesSpecified = true;
1577 1578
    }

1579
    /* def:lifecycle */
1580 1581
    def->onReboot = VIR_DOMAIN_LIFECYCLE_ACTION_RESTART;
    def->onPoweroff = VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY;
1582
    def->onCrash = VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY;
1583 1584

    /* def:os */
1585
    def->os.type = VIR_DOMAIN_OSTYPE_HVM;
1586

M
Matthias Bolte 已提交
1587
    /* vmx:guestOS -> def:os.arch */
1588
    if (virVMXGetConfigString(conf, "guestOS", &guestOS, true) < 0)
M
Matthias Bolte 已提交
1589
        goto cleanup;
M
Matthias Bolte 已提交
1590

1591
    if (guestOS != NULL && virStringHasSuffix(guestOS, "-64")) {
1592
        def->os.arch = VIR_ARCH_X86_64;
M
Matthias Bolte 已提交
1593
    } else {
1594
        def->os.arch = VIR_ARCH_I686;
M
Matthias Bolte 已提交
1595 1596
    }

M
Matthias Bolte 已提交
1597
    /* vmx:smbios.reflecthost -> def:os.smbios_mode */
1598 1599
    if (virVMXGetConfigBoolean(conf, "smbios.reflecthost",
                               &smbios_reflecthost, false, true) < 0) {
M
Matthias Bolte 已提交
1600 1601 1602
        goto cleanup;
    }

1603
    if (smbios_reflecthost)
M
Matthias Bolte 已提交
1604 1605
        def->os.smbios_mode = VIR_DOMAIN_SMBIOS_HOST;

1606 1607
    /* def:features */
    /* FIXME */
1608

1609 1610
    /* def:clock */
    /* FIXME */
1611 1612

    /* def:graphics */
1613
    if (VIR_ALLOC_N(def->graphics, 1) < 0)
M
Matthias Bolte 已提交
1614
        goto cleanup;
M
Matthias Bolte 已提交
1615 1616 1617

    def->ngraphics = 0;

1618
    if (virVMXParseVNC(conf, &def->graphics[def->ngraphics]) < 0)
M
Matthias Bolte 已提交
1619
        goto cleanup;
M
Matthias Bolte 已提交
1620

1621
    if (def->graphics[def->ngraphics] != NULL)
M
Matthias Bolte 已提交
1622
        ++def->ngraphics;
1623

M
Matthias Bolte 已提交
1624
    /* def:disks: 4 * 15 scsi + 2 * 2 ide + 2 floppy = 66 */
1625
    if (VIR_ALLOC_N(def->disks, 66) < 0)
M
Matthias Bolte 已提交
1626
        goto cleanup;
1627 1628 1629 1630 1631

    def->ndisks = 0;

    /* def:disks (scsi) */
    for (controller = 0; controller < 4; ++controller) {
1632 1633
        if (virVMXParseSCSIController(conf, controller, &present,
                                      &scsi_virtualDev[controller]) < 0) {
M
Matthias Bolte 已提交
1634
            goto cleanup;
1635 1636
        }

1637
        if (! present)
1638 1639
            continue;

1640 1641
        for (unit = 0; unit < 16; ++unit) {
            if (unit == 7) {
1642
                /*
1643
                 * SCSI unit 7 is assigned to the SCSI controller and cannot be
1644 1645 1646 1647 1648
                 * used for disk devices.
                 */
                continue;
            }

1649
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
1650
                                VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
1651
                                &def->disks[def->ndisks], def) < 0) {
M
Matthias Bolte 已提交
1652
                goto cleanup;
1653 1654 1655 1656 1657 1658 1659
            }

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

1660
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1661
                                 VIR_DOMAIN_DISK_BUS_SCSI, controller, unit,
1662
                                 &def->disks[def->ndisks], def) < 0) {
M
Matthias Bolte 已提交
1663
                goto cleanup;
1664 1665
            }

1666
            if (def->disks[def->ndisks] != NULL)
1667 1668
                ++def->ndisks;
        }
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

    }

    /* add all the SCSI controllers we've seen, up until the last one that is
     * currently used by a disk */
    if (def->ndisks != 0) {
        virDomainDeviceInfoPtr info = &def->disks[def->ndisks - 1]->info;
        for (controller = 0; controller <= info->addr.drive.controller; controller++) {
            if (!virDomainDefAddController(def, VIR_DOMAIN_CONTROLLER_TYPE_SCSI,
                                           controller, scsi_virtualDev[controller]))
                goto cleanup;
        }
1681 1682 1683
    }

    /* def:disks (ide) */
1684 1685
    for (bus = 0; bus < 2; ++bus) {
        for (unit = 0; unit < 2; ++unit) {
1686
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_DISK,
1687
                                VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
1688
                                &def->disks[def->ndisks], def) < 0) {
M
Matthias Bolte 已提交
1689
                goto cleanup;
1690 1691 1692 1693 1694 1695 1696
            }

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

1697
            if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_CDROM,
1698
                                VIR_DOMAIN_DISK_BUS_IDE, bus, unit,
1699
                                &def->disks[def->ndisks], def) < 0) {
M
Matthias Bolte 已提交
1700
                goto cleanup;
1701 1702
            }

1703
            if (def->disks[def->ndisks] != NULL)
1704 1705 1706 1707 1708
                ++def->ndisks;
        }
    }

    /* def:disks (floppy) */
1709
    for (unit = 0; unit < 2; ++unit) {
1710
        if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_FLOPPY,
1711
                            VIR_DOMAIN_DISK_BUS_FDC, 0, unit,
1712
                            &def->disks[def->ndisks], def) < 0) {
M
Matthias Bolte 已提交
1713
            goto cleanup;
1714 1715
        }

1716
        if (def->disks[def->ndisks] != NULL)
1717 1718 1719 1720
            ++def->ndisks;
    }

    /* def:fss */
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    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;

1735
            if (VIR_ALLOC_N(def->fss, sharedFolder_maxNum) < 0)
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
                goto cleanup;

            def->nfss = 0;

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

1746
                if (def->fss[def->nfss] != NULL)
1747 1748 1749 1750
                    ++def->nfss;
            }
        }
    }
1751 1752

    /* def:nets */
P
Pino Toscano 已提交
1753
    for (controller = 0; controller <= results.networks_max_index; ++controller) {
1754 1755
        virDomainNetDefPtr net = NULL;
        if (virVMXParseEthernet(conf, controller, &net) < 0)
M
Matthias Bolte 已提交
1756
            goto cleanup;
1757

1758 1759 1760 1761 1762
        if (!net)
            continue;

        if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
            goto cleanup;
1763 1764 1765 1766 1767
    }

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

1768
    /* def:videos */
1769
    if (VIR_ALLOC_N(def->videos, 1) < 0)
1770 1771 1772 1773
        goto cleanup;

    def->nvideos = 0;

1774
    if (virVMXParseSVGA(conf, &def->videos[def->nvideos]) < 0)
1775 1776 1777 1778
        goto cleanup;

    def->nvideos = 1;

1779 1780 1781 1782 1783 1784 1785
    /* def:sounds */
    /* FIXME */

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

    /* def:serials */
1786
    if (VIR_ALLOC_N(def->serials, 4) < 0)
M
Matthias Bolte 已提交
1787
        goto cleanup;
1788 1789 1790 1791

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1792 1793
        if (virVMXParseSerial(ctx, conf, port,
                              &def->serials[def->nserials]) < 0) {
M
Matthias Bolte 已提交
1794
            goto cleanup;
1795 1796
        }

1797
        if (def->serials[def->nserials] != NULL)
1798 1799 1800 1801
            ++def->nserials;
    }

    /* def:parallels */
1802
    if (VIR_ALLOC_N(def->parallels, 3) < 0)
M
Matthias Bolte 已提交
1803
        goto cleanup;
1804 1805 1806 1807

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1808 1809
        if (virVMXParseParallel(ctx, conf, port,
                                &def->parallels[def->nparallels]) < 0) {
M
Matthias Bolte 已提交
1810
            goto cleanup;
1811 1812
        }

1813
        if (def->parallels[def->nparallels] != NULL)
1814 1815 1816
            ++def->nparallels;
    }

1817
    /* ctx:datacenterPath -> def:namespaceData */
1818 1819 1820 1821 1822 1823 1824
    if (ctx->datacenterPath || ctx->moref) {
        struct virVMXDomainDefNamespaceData *nsdata = NULL;

        if (VIR_ALLOC(nsdata) < 0 ||
            VIR_STRDUP(nsdata->datacenterPath, ctx->datacenterPath) < 0 ||
            VIR_STRDUP(nsdata->moref, ctx->moref) < 0) {
            virVMXDomainDefNamespaceFree(nsdata);
1825
            goto cleanup;
1826
        }
1827 1828

        def->ns = *virDomainXMLOptionGetNamespace(xmlopt);
1829
        def->namespaceData = nsdata;
1830 1831
    }

1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
    /* vmx:firmware */
    if (virVMXGetConfigString(conf, "firmware", &firmware, true) < 0)
        goto cleanup;

    if (firmware != NULL) {
        if (STREQ(firmware, "efi")) {
            def->os.firmware = VIR_DOMAIN_OS_DEF_FIRMWARE_EFI;
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry 'firmware' has unknown value '%s'"),
                           firmware);
            goto cleanup;
        }
    }

1847
    if (virDomainDefPostParse(def, caps, VIR_DOMAIN_DEF_PARSE_ABI_UPDATE,
1848
                              xmlopt, NULL) < 0)
1849 1850
        goto cleanup;

M
Matthias Bolte 已提交
1851 1852
    success = true;

1853
 cleanup:
M
Matthias Bolte 已提交
1854 1855 1856 1857 1858
    if (! success) {
        virDomainDefFree(def);
        def = NULL;
    }

1859
    virConfFree(conf);
1860
    VIR_FREE(encoding);
1861
    VIR_FREE(sched_cpu_affinity);
1862
    VIR_FREE(sched_cpu_shares);
M
Matthias Bolte 已提交
1863
    VIR_FREE(guestOS);
1864
    virCPUDefFree(cpu);
1865
    VIR_FREE(firmware);
1866 1867 1868 1869 1870 1871

    return def;
}



M
Matthias Bolte 已提交
1872
int
1873
virVMXParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1874
{
1875
    bool enabled = false;
M
Matthias Bolte 已提交
1876
    long long port = 0;
1877
    char *listenAddr = NULL;
M
Matthias Bolte 已提交
1878 1879

    if (def == NULL || *def != NULL) {
1880
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1881 1882 1883
        return -1;
    }

1884 1885
    if (virVMXGetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
                               false, true) < 0) {
M
Matthias Bolte 已提交
1886 1887 1888
        return -1;
    }

1889
    if (! enabled)
M
Matthias Bolte 已提交
1890 1891
        return 0;

1892
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
1893 1894 1895 1896
        goto failure;

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1897 1898 1899
    if (virVMXGetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
                            true) < 0 ||
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.ip",
1900
                              &listenAddr, true) < 0 ||
1901 1902 1903 1904
        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 已提交
1905 1906 1907
        goto failure;
    }

1908 1909 1910
    if (virDomainGraphicsListenAppendAddress(*def, listenAddr) < 0)
        goto failure;
    VIR_FREE(listenAddr);
1911

M
Matthias Bolte 已提交
1912
    if (port < 0) {
1913
        VIR_WARN("VNC is enabled but VMX entry 'RemoteDisplay.vnc.port' "
M
Matthias Bolte 已提交
1914 1915 1916
                  "is missing, the VNC port is unknown");

        (*def)->data.vnc.port = 0;
1917
        (*def)->data.vnc.autoport = true;
M
Matthias Bolte 已提交
1918
    } else {
1919
        if (port < 5900 || port > 5964)
M
Matthias Bolte 已提交
1920 1921 1922
            VIR_WARN("VNC port %lld it out of [5900..5964] range", port);

        (*def)->data.vnc.port = port;
1923
        (*def)->data.vnc.autoport = false;
M
Matthias Bolte 已提交
1924 1925 1926 1927
    }

    return 0;

1928
 failure:
1929
    VIR_FREE(listenAddr);
M
Matthias Bolte 已提交
1930 1931 1932 1933 1934 1935 1936 1937
    virDomainGraphicsDefFree(*def);
    *def = NULL;

    return -1;
}



1938
int
1939 1940
virVMXParseSCSIController(virConfPtr conf, int controller, bool *present,
                          int *virtualDev)
1941
{
1942
    int result = -1;
1943 1944
    char present_name[32];
    char virtualDev_name[32];
1945 1946
    char *virtualDev_string = NULL;
    char *tmp;
1947

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

    if (controller < 0 || controller > 3) {
1954 1955 1956
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI controller index %d out of [0..3] range"),
                       controller);
1957
        return -1;
1958 1959
    }

1960 1961 1962
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1963

1964
    if (virVMXGetConfigBoolean(conf, present_name, present, false, true) < 0)
1965
        goto cleanup;
1966 1967

    if (! *present) {
1968 1969
        result = 0;
        goto cleanup;
1970 1971
    }

1972 1973
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev_string,
                              true) < 0) {
1974
        goto cleanup;
1975 1976
    }

1977 1978 1979
    if (virtualDev_string != NULL) {
        tmp = virtualDev_string;

1980
        for (; *tmp != '\0'; ++tmp)
1981 1982
            *tmp = c_tolower(*tmp);

1983
        *virtualDev = virVMXControllerModelSCSITypeFromString(virtualDev_string);
1984 1985

        if (*virtualDev == -1 ||
1986 1987 1988 1989
            (*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)) {
1990 1991 1992 1993
            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);
1994
            goto cleanup;
1995
        }
1996 1997
    }

1998
    result = 0;
1999

2000
 cleanup:
2001
    VIR_FREE(virtualDev_string);
2002

2003
    return result;
2004 2005 2006 2007 2008
}



int
2009
virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virConfPtr conf,
2010
                int device, int busType, int controllerOrBus, int unit,
2011
                virDomainDiskDefPtr *def, virDomainDefPtr vmdef)
2012 2013
{
    /*
2014 2015 2016
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK,
     *                    VIR_DOMAIN_DISK_DEVICE_CDROM,
     *                    VIR_DOMAIN_DISK_DEVICE_LUN}
2017 2018 2019
     *         busType = VIR_DOMAIN_DISK_BUS_SCSI
     * controllerOrBus = [0..3] -> controller
     *            unit = [0..6,8..15]
2020
     *
2021 2022 2023
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK,
     *                    VIR_DOMAIN_DISK_DEVICE_CDROM,
     *                    VIR_DOMAIN_DISK_DEVICE_LUN}
2024 2025 2026
     *         busType = VIR_DOMAIN_DISK_BUS_IDE
     * controllerOrBus = [0..1] -> bus
     *            unit = [0..1]
2027
     *
2028 2029 2030 2031
     *          device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *         busType = VIR_DOMAIN_DISK_BUS_FDC
     * controllerOrBus = [0]
     *            unit = [0..1]
2032 2033
     */

M
Matthias Bolte 已提交
2034
    int result = -1;
2035 2036 2037
    char *prefix = NULL;

    char present_name[32] = "";
2038
    bool present = false;
2039 2040

    char startConnected_name[32] = "";
2041
    bool startConnected = false;
2042 2043 2044 2045 2046

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

    char clientDevice_name[32] = "";
2047
    bool clientDevice = false;
2048 2049 2050 2051 2052 2053 2054 2055

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

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

    char writeThrough_name[32] = "";
2056
    bool writeThrough = false;
2057

2058 2059 2060
    char mode_name[32] = "";
    char *mode = NULL;

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

2066
    if (!(*def = virDomainDiskDefNew(xmlopt)))
M
Matthias Bolte 已提交
2067
        return -1;
2068 2069

    (*def)->device = device;
2070
    (*def)->bus = busType;
2071 2072 2073 2074

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2075 2076
        if (busType == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controllerOrBus < 0 || controllerOrBus > 3) {
2077 2078 2079
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI controller index %d out of [0..3] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2080
                goto cleanup;
2081 2082
            }

2083
            if (unit < 0 || unit > 15 || unit == 7) {
2084 2085 2086
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI unit index %d out of [0..6,8..15] range"),
                               unit);
M
Matthias Bolte 已提交
2087
                goto cleanup;
2088 2089
            }

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

M
Matthias Bolte 已提交
2093
            (*def)->dst =
2094
               virIndexToDiskName
2095
                 (controllerOrBus * 15 + (unit < 7 ? unit : unit - 1), "sd");
2096

2097
            if ((*def)->dst == NULL)
M
Matthias Bolte 已提交
2098
                goto cleanup;
2099 2100
        } else if (busType == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controllerOrBus < 0 || controllerOrBus > 1) {
2101 2102 2103
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IDE bus index %d out of [0..1] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2104
                goto cleanup;
2105 2106
            }

2107
            if (unit < 0 || unit > 1) {
2108 2109
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IDE unit index %d out of [0..1] range"), unit);
M
Matthias Bolte 已提交
2110
                goto cleanup;
2111 2112
            }

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

2116
            (*def)->dst = virIndexToDiskName(controllerOrBus * 2 + unit, "hd");
2117

2118
            if ((*def)->dst == NULL)
M
Matthias Bolte 已提交
2119
                goto cleanup;
2120
        } else {
2121 2122 2123 2124
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2125
            goto cleanup;
2126 2127
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2128 2129
        if (busType == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controllerOrBus != 0) {
2130 2131 2132
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC controller index %d out of [0] range"),
                               controllerOrBus);
2133 2134 2135 2136
                goto cleanup;
            }

            if (unit < 0 || unit > 1) {
2137 2138 2139
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC unit index %d out of [0..1] range"),
                               unit);
M
Matthias Bolte 已提交
2140
                goto cleanup;
2141 2142
            }

2143
            if (virAsprintf(&prefix, "floppy%d", unit) < 0)
M
Matthias Bolte 已提交
2144
                goto cleanup;
2145

2146
            (*def)->dst = virIndexToDiskName(unit, "fd");
2147

2148
            if ((*def)->dst == NULL)
M
Matthias Bolte 已提交
2149
                goto cleanup;
2150
        } else {
2151 2152 2153 2154
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2155
            goto cleanup;
2156 2157
        }
    } else {
2158 2159 2160
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2161
        goto cleanup;
2162 2163
    }

2164 2165 2166 2167 2168 2169 2170
    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);
2171
    VMX_BUILD_NAME(mode);
2172 2173

    /* vmx:present */
2174
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
M
Matthias Bolte 已提交
2175
        goto cleanup;
2176 2177

    /* vmx:startConnected */
2178 2179
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2180
        goto cleanup;
2181 2182
    }

2183
    /* FIXME: Need to distinguish between active and inactive domains here */
2184
    if (! present/* && ! startConnected*/)
2185 2186 2187
        goto ignore;

    /* vmx:deviceType -> def:type */
2188
    if (virVMXGetConfigString(conf, deviceType_name, &deviceType, true) < 0)
M
Matthias Bolte 已提交
2189
        goto cleanup;
2190 2191

    /* vmx:clientDevice */
2192 2193
    if (virVMXGetConfigBoolean(conf, clientDevice_name, &clientDevice, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2194
        goto cleanup;
2195 2196
    }

2197
    /* vmx:mode -> def:transient */
2198
    if (virVMXGetConfigString(conf, mode_name, &mode, true) < 0)
2199 2200
        goto cleanup;

2201 2202 2203
    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
2204
         * handle them (e.g. assign an image) without the VI Client GUI.
2205 2206 2207 2208 2209
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
2210
    if (virVMXGetConfigString(conf, fileType_name, &fileType, true) < 0)
M
Matthias Bolte 已提交
2211
        goto cleanup;
2212 2213

    /* vmx:fileName -> def:src, def:type */
2214
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0)
M
Matthias Bolte 已提交
2215
        goto cleanup;
2216 2217

    /* vmx:writeThrough -> def:cachemode */
2218 2219
    if (virVMXGetConfigBoolean(conf, writeThrough_name, &writeThrough, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2220
        goto cleanup;
2221 2222 2223 2224
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
2225
        if (virStringHasCaseSuffix(fileName, ".vmdk")) {
2226 2227
            char *tmp;

2228
            if (deviceType != NULL) {
2229
                if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
2230 2231
                    STRCASENEQ(deviceType, "scsi-hardDisk") &&
                    STRCASENEQ(deviceType, "disk")) {
2232 2233 2234 2235
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2236
                    goto cleanup;
2237
                } else if (busType == VIR_DOMAIN_DISK_BUS_IDE &&
2238 2239
                           STRCASENEQ(deviceType, "ata-hardDisk") &&
                           STRCASENEQ(deviceType, "disk")) {
2240 2241 2242 2243
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2244
                    goto cleanup;
2245 2246 2247
                }
            }

E
Eric Blake 已提交
2248
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
2249 2250 2251 2252 2253 2254 2255
            if (!(tmp = ctx->parseFileName(fileName, ctx->opaque)))
                goto cleanup;
            if (virDomainDiskSetSource(*def, tmp) < 0) {
                VIR_FREE(tmp);
                goto cleanup;
            }
            VIR_FREE(tmp);
2256 2257
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;
2258 2259 2260
            if (mode)
                (*def)->transient = STRCASEEQ(mode,
                                              "independent-nonpersistent");
2261
        } else if (virStringHasCaseSuffix(fileName, ".iso") ||
2262
                   STREQ(fileName, "emptyBackingString") ||
2263 2264
                   (deviceType &&
                    (STRCASEEQ(deviceType, "atapi-cdrom") ||
2265 2266 2267
                     STRCASEEQ(deviceType, "cdrom-raw") ||
                     (STRCASEEQ(deviceType, "scsi-passthru") &&
                      STRPREFIX(fileName, "/vmfs/devices/cdrom/"))))) {
2268 2269
            /*
             * This function was called in order to parse a harddisk device,
2270 2271 2272
             * but .iso files, 'atapi-cdrom', 'cdrom-raw', and 'scsi-passthru'
             * CDROM devices are for CDROM devices only. Just ignore it, another
             * call to this function to parse a CDROM device may handle it.
2273 2274 2275
             */
            goto ignore;
        } else {
2276
            virReportError(VIR_ERR_INTERNAL_ERROR,
2277 2278
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
M
Matthias Bolte 已提交
2279 2280
                           fileName, fileName_name,
                           deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2281
            goto cleanup;
2282 2283
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2284
        if (virStringHasCaseSuffix(fileName, ".iso")) {
2285 2286
            char *tmp;

2287 2288 2289 2290 2291
            if (deviceType && STRCASENEQ(deviceType, "cdrom-image")) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                 "but found '%s'"), deviceType_name, deviceType);
                goto cleanup;
2292 2293
            }

E
Eric Blake 已提交
2294
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
2295 2296 2297 2298
            if (!(tmp = ctx->parseFileName(fileName, ctx->opaque)))
                goto cleanup;
            if (virDomainDiskSetSource(*def, tmp) < 0) {
                VIR_FREE(tmp);
M
Matthias Bolte 已提交
2299
                goto cleanup;
M
Matthias Bolte 已提交
2300
            }
2301
            VIR_FREE(tmp);
2302
        } else if (virStringHasCaseSuffix(fileName, ".vmdk")) {
2303 2304 2305 2306 2307 2308 2309
            /*
             * 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;
2310
        } else if (deviceType && STRCASEEQ(deviceType, "atapi-cdrom")) {
E
Eric Blake 已提交
2311
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);
2312 2313

            if (STRCASEEQ(fileName, "auto detect")) {
2314
                ignore_value(virDomainDiskSetSource(*def, NULL));
2315
                (*def)->startupPolicy = VIR_DOMAIN_STARTUP_POLICY_OPTIONAL;
2316 2317
            } else if (virDomainDiskSetSource(*def, fileName) < 0) {
                goto cleanup;
2318
            }
2319
        } else if (deviceType && STRCASEEQ(deviceType, "cdrom-raw")) {
2320 2321
            /* Raw access CD-ROMs actually are device='lun' */
            (*def)->device = VIR_DOMAIN_DISK_DEVICE_LUN;
E
Eric Blake 已提交
2322
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);
2323 2324

            if (STRCASEEQ(fileName, "auto detect")) {
2325
                ignore_value(virDomainDiskSetSource(*def, NULL));
2326
                (*def)->startupPolicy = VIR_DOMAIN_STARTUP_POLICY_OPTIONAL;
2327 2328
            } else if (virDomainDiskSetSource(*def, fileName) < 0) {
                goto cleanup;
2329
            }
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
        } else if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
                   deviceType && STRCASEEQ(deviceType, "scsi-passthru")) {
            if (STRPREFIX(fileName, "/vmfs/devices/cdrom/")) {
                /* SCSI-passthru CD-ROMs actually are device='lun' */
                (*def)->device = VIR_DOMAIN_DISK_DEVICE_LUN;
                virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);

                if (virDomainDiskSetSource(*def, fileName) < 0)
                    goto cleanup;
            } else {
                /*
                 * This function was called in order to parse a CDROM device,
                 * but the filename does not indicate a CDROM device. Just ignore
                 * it, another call to this function to parse a harddisk device
                 * may handle it.
                 */
                goto ignore;
            }
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
        } else if (STREQ(fileName, "emptyBackingString")) {
            if (deviceType && STRCASENEQ(deviceType, "cdrom-image")) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                 "but found '%s'"), deviceType_name, deviceType);
                goto cleanup;
            }

            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
            ignore_value(virDomainDiskSetSource(*def, NULL));
2358
        } else {
2359
            virReportError(VIR_ERR_INTERNAL_ERROR,
2360 2361
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
M
Matthias Bolte 已提交
2362 2363
                           fileName, fileName_name,
                           deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2364
            goto cleanup;
2365 2366
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2367
        if (fileType != NULL && STRCASEEQ(fileType, "device")) {
E
Eric Blake 已提交
2368
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);
2369 2370
            if (virDomainDiskSetSource(*def, fileName) < 0)
                goto cleanup;
2371
        } else if (fileType != NULL && STRCASEEQ(fileType, "file")) {
2372 2373
            char *tmp;

E
Eric Blake 已提交
2374
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
2375 2376 2377 2378 2379 2380 2381
            if (!(tmp = ctx->parseFileName(fileName, ctx->opaque)))
                goto cleanup;
            if (virDomainDiskSetSource(*def, tmp) < 0) {
                VIR_FREE(tmp);
                goto cleanup;
            }
            VIR_FREE(tmp);
2382
        } else {
2383
            virReportError(VIR_ERR_INTERNAL_ERROR,
2384 2385
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
M
Matthias Bolte 已提交
2386 2387
                           fileName, fileName_name,
                           deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2388
            goto cleanup;
2389 2390
        }
    } else {
2391 2392
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2393
        goto cleanup;
2394 2395
    }

2396
    if (virDomainDiskDefAssignAddress(xmlopt, *def, vmdef) < 0) {
2397
        virReportError(VIR_ERR_INTERNAL_ERROR,
2398 2399
                       _("Could not assign address to disk '%s'"),
                       virDomainDiskGetSource(*def));
2400 2401 2402
        goto cleanup;
    }

M
Matthias Bolte 已提交
2403 2404
    result = 0;

2405
 cleanup:
M
Matthias Bolte 已提交
2406 2407 2408 2409 2410
    if (result < 0) {
        virDomainDiskDefFree(*def);
        *def = NULL;
    }

2411 2412 2413 2414
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2415
    VIR_FREE(mode);
2416 2417 2418

    return result;

2419
 ignore:
2420 2421 2422
    virDomainDiskDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2423 2424
    result = 0;

2425 2426 2427 2428 2429
    goto cleanup;
}



2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
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;
    }

    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 */
2464
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
2465
        return -1;
2466 2467

    /* vmx:enabled */
2468
    if (virVMXGetConfigBoolean(conf, enabled_name, &enabled, false, true) < 0)
2469
        return -1;
2470

2471
    if (!(present && enabled))
2472 2473 2474 2475 2476 2477
        return 0;

    if (!(*def = virDomainFSDefNew()))
        return -1;

    (*def)->type = VIR_DOMAIN_FS_TYPE_MOUNT;
2478 2479

    /* vmx:hostPath */
2480
    if (virVMXGetConfigString(conf, hostPath_name, &hostPath, false) < 0)
2481 2482
        goto cleanup;

2483
    (*def)->src->path = hostPath;
2484 2485 2486
    hostPath = NULL;

    /* vmx:guestName */
2487
    if (virVMXGetConfigString(conf, guestName_name, &guestName, false) < 0)
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
        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;

2503
 cleanup:
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
    if (result < 0) {
        virDomainFSDefFree(*def);
        *def = NULL;
    }

    VIR_FREE(hostPath);
    VIR_FREE(guestName);

    return result;
}



2517
int
2518
virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
2519
{
M
Matthias Bolte 已提交
2520
    int result = -1;
2521 2522 2523
    char prefix[48] = "";

    char present_name[48] = "";
2524
    bool present = false;
2525 2526

    char startConnected_name[48] = "";
2527
    bool startConnected = false;
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543

    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;

2544 2545 2546
    char features_name[48] = "";
    long long features = 0;

2547 2548 2549
    char vnet_name[48] = "";
    char *vnet = NULL;

M
Matthias Bolte 已提交
2550 2551 2552
    char networkName_name[48] = "";
    char *networkName = NULL;

C
Cole Robinson 已提交
2553 2554
    int netmodel = VIR_DOMAIN_NET_MODEL_UNKNOWN;

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

2560
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
2561

2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
    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);
2572 2573

    /* vmx:present */
2574
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
2575
        return -1;
2576 2577

    /* vmx:startConnected */
2578 2579
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
2580
        return -1;
2581 2582
    }

2583
    /* FIXME: Need to distinguish between active and inactive domains here */
2584
    if (! present/* && ! startConnected*/)
2585 2586 2587 2588
        return 0;

    if (VIR_ALLOC(*def) < 0)
        return -1;
2589 2590

    /* vmx:connectionType -> def:type */
2591 2592
    if (virVMXGetConfigString(conf, connectionType_name, &connectionType,
                              true) < 0) {
M
Matthias Bolte 已提交
2593
        goto cleanup;
2594 2595 2596
    }

    /* vmx:addressType, vmx:generatedAddress, vmx:address -> def:mac */
2597 2598 2599 2600 2601
    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 已提交
2602
        goto cleanup;
2603 2604
    }

2605 2606
    if (addressType == NULL || STRCASEEQ(addressType, "generated") ||
        STRCASEEQ(addressType, "vpx")) {
2607
        if (generatedAddress != NULL) {
2608
            if (virMacAddrParse(generatedAddress, &(*def)->mac) < 0) {
2609 2610 2611 2612
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be MAC address but "
                                 "found '%s'"), generatedAddress_name,
                               generatedAddress);
M
Matthias Bolte 已提交
2613
                goto cleanup;
2614 2615 2616 2617
            }
        }
    } else if (STRCASEEQ(addressType, "static")) {
        if (address != NULL) {
2618
            if (virMacAddrParse(address, &(*def)->mac) < 0) {
2619 2620 2621
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be MAC address but "
                                 "found '%s'"), address_name, address);
M
Matthias Bolte 已提交
2622
                goto cleanup;
2623 2624 2625
            }
        }
    } else {
2626 2627 2628
        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 已提交
2629
        goto cleanup;
2630 2631
    }

2632
    /* vmx:virtualDev, vmx:features -> def:model */
2633 2634
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev, true) < 0 ||
        virVMXGetConfigLong(conf, features_name, &features, 0, true) < 0) {
M
Matthias Bolte 已提交
2635
        goto cleanup;
2636 2637
    }

2638
    if (virtualDev != NULL) {
C
Cole Robinson 已提交
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
        if (STRCASEEQ(virtualDev, "vlance")) {
            netmodel = VIR_DOMAIN_NET_MODEL_VLANCE;
        } else if (STRCASEEQ(virtualDev, "vmxnet")) {
            netmodel = VIR_DOMAIN_NET_MODEL_VMXNET;
        } else if (STRCASEEQ(virtualDev, "vmxnet3")) {
            netmodel = VIR_DOMAIN_NET_MODEL_VMXNET3;
        } else if (STRCASEEQ(virtualDev, "e1000")) {
            netmodel = VIR_DOMAIN_NET_MODEL_E1000;
        } else if (STRCASEEQ(virtualDev, "e1000e")) {
            netmodel = VIR_DOMAIN_NET_MODEL_E1000E;
        } else {
2650 2651
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'vlance' or 'vmxnet' or "
Y
Yuri Chornoivan 已提交
2652
                             "'vmxnet3' or 'e1000' or 'e1000e' but found '%s'"),
M
Matthias Bolte 已提交
2653
                           virtualDev_name, virtualDev);
M
Matthias Bolte 已提交
2654
            goto cleanup;
2655 2656
        }

C
Cole Robinson 已提交
2657 2658
        if (netmodel == VIR_DOMAIN_NET_MODEL_VMXNET && features == 15)
            netmodel = VIR_DOMAIN_NET_MODEL_VMXNET2;
2659 2660
    }

M
Matthias Bolte 已提交
2661
    /* vmx:networkName -> def:data.bridge.brname */
2662 2663 2664 2665 2666 2667 2668
    if (connectionType == NULL ||
        STRCASEEQ(connectionType, "bridged") ||
        STRCASEEQ(connectionType, "custom")) {
        if (virVMXGetConfigString(conf, networkName_name, &networkName,
                                  true) < 0)
            goto cleanup;

2669 2670
        if (!networkName && VIR_STRDUP(networkName, "") < 0)
            goto cleanup;
M
Matthias Bolte 已提交
2671 2672 2673
    }

    /* vmx:vnet -> def:data.ifname */
2674
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
2675
        virVMXGetConfigString(conf, vnet_name, &vnet, false) < 0) {
M
Matthias Bolte 已提交
2676
        goto cleanup;
2677 2678 2679 2680 2681
    }

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

M
Matthias Bolte 已提交
2684
        networkName = NULL;
2685 2686
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
2687 2688 2689
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No yet handled value '%s' for VMX entry '%s'"),
                       connectionType, connectionType_name);
M
Matthias Bolte 已提交
2690
        goto cleanup;
2691
    } else if (STRCASEEQ(connectionType, "nat")) {
2692 2693
        (*def)->type = VIR_DOMAIN_NET_TYPE_USER;

2694 2695
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
M
Matthias Bolte 已提交
2696 2697
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
2698

M
Matthias Bolte 已提交
2699
        networkName = NULL;
2700 2701
        vnet = NULL;
    } else {
2702 2703 2704
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid value '%s' for VMX entry '%s'"), connectionType,
                       connectionType_name);
M
Matthias Bolte 已提交
2705
        goto cleanup;
2706 2707
    }

C
Cole Robinson 已提交
2708
    (*def)->model = netmodel;
M
Matthias Bolte 已提交
2709 2710
    result = 0;

2711
 cleanup:
M
Matthias Bolte 已提交
2712 2713 2714 2715 2716
    if (result < 0) {
        virDomainNetDefFree(*def);
        *def = NULL;
    }

S
Stefan Berger 已提交
2717
    VIR_FREE(networkName);
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;
}



int
2731 2732
virVMXParseSerial(virVMXContext *ctx, virConfPtr conf, int port,
                  virDomainChrDefPtr *def)
2733
{
M
Matthias Bolte 已提交
2734
    int result = -1;
2735 2736 2737
    char prefix[48] = "";

    char present_name[48] = "";
2738
    bool present = false;
2739 2740

    char startConnected_name[48] = "";
2741
    bool startConnected = false;
2742 2743 2744 2745 2746 2747 2748

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

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

2749 2750 2751
    char network_endPoint_name[48] = "";
    char *network_endPoint = NULL;

M
Martin Kletzander 已提交
2752
    virURIPtr parsedUri = NULL;
2753

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

    if (port < 0 || port > 3) {
2760 2761
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Serial port index %d out of [0..3] range"), port);
2762
        return -1;
2763 2764
    }

2765
    snprintf(prefix, sizeof(prefix), "serial%d", port);
2766

2767 2768 2769 2770 2771
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
    VMX_BUILD_NAME_EXTRA(network_endPoint, "network.endPoint");
2772 2773

    /* vmx:present */
2774
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
2775
        return -1;
2776 2777

    /* vmx:startConnected */
2778 2779
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
2780
        return -1;
2781 2782
    }

2783
    /* FIXME: Need to distinguish between active and inactive domains here */
2784
    if (! present/* && ! startConnected*/)
2785 2786 2787 2788 2789 2790
        return 0;

    if (!(*def = virDomainChrDefNew(NULL)))
        return -1;

    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
2791 2792

    /* vmx:fileType -> def:type */
G
Geoff Hickey 已提交
2793
    if (virVMXGetConfigString(conf, fileType_name, &fileType, true) < 0)
M
Matthias Bolte 已提交
2794
        goto cleanup;
2795 2796

    /* vmx:fileName -> def:data.file.path */
2797
    if (virVMXGetConfigString(conf, fileName_name, &fileName, true) < 0)
2798
        goto cleanup;
2799

2800
    /* vmx:network.endPoint -> def:data.tcp.listen */
2801 2802
    if (virVMXGetConfigString(conf, network_endPoint_name, &network_endPoint,
                              true) < 0) {
2803 2804 2805
        goto cleanup;
    }

G
Geoff Hickey 已提交
2806 2807 2808 2809 2810 2811
    /*
     * Setup virDomainChrDef. The default fileType is "device", and vmware
     * will sometimes omit this tag when adding a new serial port of this
     * type.
     */
    if (!fileType || STRCASEEQ(fileType, "device")) {
2812
        (*def)->target.port = port;
2813 2814
        (*def)->source->type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source->data.file.path = fileName;
2815 2816 2817

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2818
        (*def)->target.port = port;
2819 2820 2821
        (*def)->source->type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source->data.file.path = ctx->parseFileName(fileName,
                                                            ctx->opaque);
2822

2823
        if ((*def)->source->data.file.path == NULL)
M
Matthias Bolte 已提交
2824
            goto cleanup;
2825
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
2826 2827 2828 2829
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
2830
        (*def)->target.port = port;
2831 2832
        (*def)->source->type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->source->data.file.path = fileName;
M
Matthias Bolte 已提交
2833 2834

        fileName = NULL;
2835 2836
    } else if (STRCASEEQ(fileType, "network")) {
        (*def)->target.port = port;
2837
        (*def)->source->type = VIR_DOMAIN_CHR_TYPE_TCP;
2838

2839
        if (!(parsedUri = virURIParse(fileName)))
2840 2841 2842
            goto cleanup;

        if (parsedUri->port == 0) {
2843 2844 2845
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' doesn't contain a port part"),
                           fileName_name);
2846 2847 2848
            goto cleanup;
        }

2849
        if (VIR_STRDUP((*def)->source->data.tcp.host, parsedUri->server) < 0)
2850 2851
            goto cleanup;

2852
        if (virAsprintf(&(*def)->source->data.tcp.service, "%d",
2853
                        parsedUri->port) < 0)
2854 2855 2856 2857 2858 2859 2860
            goto cleanup;

        /* See vSphere API documentation about VirtualSerialPortURIBackingInfo */
        if (parsedUri->scheme == NULL ||
            STRCASEEQ(parsedUri->scheme, "tcp") ||
            STRCASEEQ(parsedUri->scheme, "tcp4") ||
            STRCASEEQ(parsedUri->scheme, "tcp6")) {
2861
            (*def)->source->data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW;
2862
        } else if (STRCASEEQ(parsedUri->scheme, "telnet")) {
2863
            (*def)->source->data.tcp.protocol
2864
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
2865
        } else if (STRCASEEQ(parsedUri->scheme, "telnets")) {
2866
            (*def)->source->data.tcp.protocol
2867
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNETS;
2868 2869 2870 2871
        } else if (STRCASEEQ(parsedUri->scheme, "ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp4+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp6+ssl")) {
2872
            (*def)->source->data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TLS;
2873
        } else {
2874 2875 2876
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' contains unsupported scheme '%s'"),
                           fileName_name, parsedUri->scheme);
2877 2878 2879 2880
            goto cleanup;
        }

        if (network_endPoint == NULL || STRCASEEQ(network_endPoint, "server")) {
2881
            (*def)->source->data.tcp.listen = true;
2882
        } else if (STRCASEEQ(network_endPoint, "client")) {
2883
            (*def)->source->data.tcp.listen = false;
2884
        } else {
2885 2886 2887
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'server' or 'client' "
                             "but found '%s'"), network_endPoint_name, network_endPoint);
2888 2889
            goto cleanup;
        }
2890
    } else {
2891 2892 2893
        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 已提交
2894
        goto cleanup;
2895 2896
    }

M
Matthias Bolte 已提交
2897 2898
    result = 0;

2899
 cleanup:
M
Matthias Bolte 已提交
2900 2901 2902 2903 2904
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2905 2906
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2907
    VIR_FREE(network_endPoint);
2908
    virURIFree(parsedUri);
2909 2910 2911 2912 2913 2914 2915

    return result;
}



int
2916 2917
virVMXParseParallel(virVMXContext *ctx, virConfPtr conf, int port,
                    virDomainChrDefPtr *def)
2918
{
M
Matthias Bolte 已提交
2919
    int result = -1;
2920 2921 2922
    char prefix[48] = "";

    char present_name[48] = "";
2923
    bool present = false;
2924 2925

    char startConnected_name[48] = "";
2926
    bool startConnected = false;
2927 2928 2929 2930 2931 2932 2933 2934

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

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

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

    if (port < 0 || port > 2) {
2940 2941
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"), port);
2942
        return -1;
2943 2944
    }

2945
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2946

2947 2948 2949 2950
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
2951 2952

    /* vmx:present */
2953
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
2954
        return -1;
2955 2956

    /* vmx:startConnected */
2957 2958
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
2959
        return -1;
2960 2961
    }

2962
    /* FIXME: Need to distinguish between active and inactive domains here */
2963
    if (! present/* && ! startConnected*/)
2964 2965 2966 2967 2968 2969
        return 0;

    if (!(*def = virDomainChrDefNew(NULL)))
        return -1;

    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
2970 2971

    /* vmx:fileType -> def:type */
2972
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0)
M
Matthias Bolte 已提交
2973
        goto cleanup;
2974 2975

    /* vmx:fileName -> def:data.file.path */
2976
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0)
M
Matthias Bolte 已提交
2977
        goto cleanup;
2978 2979 2980

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2981
        (*def)->target.port = port;
2982 2983
        (*def)->source->type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source->data.file.path = fileName;
2984 2985 2986

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2987
        (*def)->target.port = port;
2988 2989 2990
        (*def)->source->type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source->data.file.path = ctx->parseFileName(fileName,
                                                            ctx->opaque);
2991

2992
        if ((*def)->source->data.file.path == NULL)
M
Matthias Bolte 已提交
2993
            goto cleanup;
2994
    } else {
2995 2996 2997
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                         "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2998
        goto cleanup;
2999 3000
    }

M
Matthias Bolte 已提交
3001 3002
    result = 0;

3003
 cleanup:
M
Matthias Bolte 已提交
3004 3005 3006 3007 3008
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

3009 3010 3011 3012 3013
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;
}
M
Matthias Bolte 已提交
3014 3015 3016



3017 3018 3019 3020 3021 3022 3023
int
virVMXParseSVGA(virConfPtr conf, virDomainVideoDefPtr *def)
{
    int result = -1;
    long long svga_vramSize = 0;

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

3028
    if (VIR_ALLOC(*def) < 0)
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
        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;
    }

3039
    (*def)->vram = VIR_DIV_UP(svga_vramSize, 1024); /* Scale from bytes to kilobytes */
3040 3041 3042

    result = 0;

3043
 cleanup:
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053
    if (result < 0) {
        virDomainVideoDefFree(*def);
        *def = NULL;
    }

    return result;
}



M
Matthias Bolte 已提交
3054 3055 3056 3057 3058
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Domain XML -> VMX
 */

char *
3059
virVMXFormatConfig(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virDomainDefPtr def,
3060
                   int virtualHW_version)
M
Matthias Bolte 已提交
3061
{
3062
    char *vmx = NULL;
3063
    size_t i;
M
Matthias Bolte 已提交
3064 3065 3066
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
3067 3068
    char *preliminaryDisplayName = NULL;
    char *displayName = NULL;
3069
    char *annotation = NULL;
3070
    unsigned long long max_balloon;
3071 3072
    bool scsi_present[4] = { false, false, false, false };
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
3073
    bool floppy_present[2] = { false, false };
3074
    unsigned int maxvcpus;
3075
    bool hasSCSI = false;
M
Matthias Bolte 已提交
3076

3077
    if (ctx->formatFileName == NULL) {
3078 3079
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no formatFileName function set"));
3080 3081 3082
        return NULL;
    }

M
Matthias Bolte 已提交
3083 3084
    memset(zero, 0, VIR_UUID_BUFLEN);

3085
    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) {
3086 3087 3088 3089
        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 已提交
3090 3091 3092
        return NULL;
    }

3093 3094 3095
    /* vmx:.encoding */
    virBufferAddLit(&buffer, ".encoding = \"UTF-8\"\n");

M
Matthias Bolte 已提交
3096 3097 3098 3099
    /* vmx:config.version */
    virBufferAddLit(&buffer, "config.version = \"8\"\n");

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

M
Matthias Bolte 已提交
3103
    /* def:os.arch -> vmx:guestOS */
3104
    if (def->os.arch == VIR_ARCH_I686) {
M
Matthias Bolte 已提交
3105
        virBufferAddLit(&buffer, "guestOS = \"other\"\n");
3106
    } else if (def->os.arch == VIR_ARCH_X86_64) {
M
Matthias Bolte 已提交
3107 3108
        virBufferAddLit(&buffer, "guestOS = \"other-64\"\n");
    } else {
3109 3110
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting domain XML attribute 'arch' of entry 'os/type' "
3111 3112
                         "to be 'i686' or 'x86_64' but found '%s'"),
                       virArchToString(def->os.arch));
3113
        goto cleanup;
M
Matthias Bolte 已提交
3114 3115
    }

M
Matthias Bolte 已提交
3116 3117 3118 3119 3120 3121 3122
    /* 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 {
3123 3124 3125
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported SMBIOS mode '%s'"),
                       virDomainSmbiosModeTypeToString(def->os.smbios_mode));
M
Matthias Bolte 已提交
3126 3127 3128
        goto cleanup;
    }

M
Matthias Bolte 已提交
3129 3130 3131 3132
    /* def:uuid -> vmx:uuid.action, vmx:uuid.bios */
    if (memcmp(def->uuid, zero, VIR_UUID_BUFLEN) == 0) {
        virBufferAddLit(&buffer, "uuid.action = \"create\"\n");
    } else {
3133
        virBufferAsprintf(&buffer, "uuid.bios = \"%02x %02x %02x %02x %02x %02x "
M
Matthias Bolte 已提交
3134 3135 3136 3137 3138 3139 3140 3141 3142
                          "%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 */
3143
    preliminaryDisplayName = virVMXEscapeHexPipe(def->name);
3144

3145
    if (preliminaryDisplayName == NULL)
3146
        goto cleanup;
3147

3148
    displayName = virVMXEscapeHexPercent(preliminaryDisplayName);
3149

3150
    if (displayName == NULL)
3151
        goto cleanup;
3152

3153
    virBufferAsprintf(&buffer, "displayName = \"%s\"\n", displayName);
3154

3155 3156
    /* def:description -> vmx:annotation */
    if (def->description != NULL) {
3157 3158
        if (!(annotation = virVMXEscapeHexPipe(def->description)))
            goto cleanup;
3159

3160
        virBufferAsprintf(&buffer, "annotation = \"%s\"\n", annotation);
3161 3162
    }

3163
    /* def:mem.max_balloon -> vmx:memsize */
3164
    /* max-memory must be a multiple of 4096 kilobyte */
3165
    max_balloon = VIR_DIV_UP(virDomainDefGetMemoryTotal(def), 4096) * 4096;
M
Matthias Bolte 已提交
3166

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

3170
    /* def:mem.cur_balloon -> vmx:sched.mem.max */
3171
    if (def->mem.cur_balloon < max_balloon) {
3172
        virBufferAsprintf(&buffer, "sched.mem.max = \"%llu\"\n",
3173 3174
                          VIR_DIV_UP(def->mem.cur_balloon,
                                     1024)); /* Scale from kilobytes to megabytes */
M
Matthias Bolte 已提交
3175 3176
    }

3177 3178
    /* def:mem.min_guarantee -> vmx:sched.mem.minsize */
    if (def->mem.min_guarantee > 0) {
3179
        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%llu\"\n",
3180 3181
                          VIR_DIV_UP(def->mem.min_guarantee,
                                     1024)); /* Scale from kilobytes to megabytes */
3182 3183
    }

E
Eric Blake 已提交
3184
    /* def:maxvcpus -> vmx:numvcpus */
3185
    if (virDomainDefHasVcpusOffline(def)) {
3186 3187 3188
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("No support for domain XML entry 'vcpu' attribute "
                         "'current'"));
E
Eric Blake 已提交
3189 3190
        goto cleanup;
    }
3191
    maxvcpus = virDomainDefGetVcpusMax(def);
P
Pino Toscano 已提交
3192 3193 3194 3195
    if (maxvcpus == 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML entry 'vcpu' to be greater "
                         "than 0"));
3196
        goto cleanup;
M
Matthias Bolte 已提交
3197 3198
    }

3199
    virBufferAsprintf(&buffer, "numvcpus = \"%d\"\n", maxvcpus);
M
Matthias Bolte 已提交
3200

3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229
    if (def->cpu) {
        unsigned int calculated_vcpus;

        if (def->cpu->mode != VIR_CPU_MODE_CUSTOM) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML CPU mode 'custom' but "
                             "found '%s'"),
                           virCPUModeTypeToString(def->cpu->mode));
            goto cleanup;
        }

        if (def->cpu->threads != 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Only 1 thread per core is supported"));
            goto cleanup;
        }

        calculated_vcpus = def->cpu->sockets * def->cpu->cores;
        if (calculated_vcpus != maxvcpus) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML CPU sockets per core as %d "
                             "but found %d"),
                           maxvcpus, calculated_vcpus);
            goto cleanup;
        }

        virBufferAsprintf(&buffer, "cpuid.coresPerSocket = \"%d\"\n", def->cpu->cores);
    }

M
Matthias Bolte 已提交
3230
    /* def:cpumask -> vmx:sched.cpu.affinity */
H
Hu Tao 已提交
3231
    if (def->cpumask && virBitmapSize(def->cpumask) > 0) {
3232
        int bit;
M
Matthias Bolte 已提交
3233 3234 3235 3236
        virBufferAddLit(&buffer, "sched.cpu.affinity = \"");

        sched_cpu_affinity_length = 0;

3237
        bit = -1;
3238
        while ((bit = virBitmapNextSetBit(def->cpumask, bit)) >= 0)
H
Hu Tao 已提交
3239
            ++sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3240

3241
        if (sched_cpu_affinity_length < maxvcpus) {
3242 3243 3244
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML attribute 'cpuset' of entry "
                             "'vcpu' to contain at least %d CPU(s)"),
3245
                           maxvcpus);
3246
            goto cleanup;
M
Matthias Bolte 已提交
3247 3248
        }

3249 3250 3251
        bit = -1;
        while ((bit = virBitmapNextSetBit(def->cpumask, bit)) >= 0) {
            virBufferAsprintf(&buffer, "%d", bit);
M
Matthias Bolte 已提交
3252

3253
            if (sched_cpu_affinity_length > 1)
H
Hu Tao 已提交
3254 3255 3256
                virBufferAddChar(&buffer, ',');

            --sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3257 3258 3259 3260 3261
        }

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

3262
    /* def:cputune.shares -> vmx:sched.cpu.shares */
3263
    if (def->cputune.sharesSpecified) {
3264
        unsigned int vcpus = virDomainDefGetVcpus(def);
3265
        /* See http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.SharesInfo.Level.html */
3266
        if (def->cputune.shares == vcpus * 500) {
3267
            virBufferAddLit(&buffer, "sched.cpu.shares = \"low\"\n");
3268
        } else if (def->cputune.shares == vcpus * 1000) {
3269
            virBufferAddLit(&buffer, "sched.cpu.shares = \"normal\"\n");
3270
        } else if (def->cputune.shares == vcpus * 2000) {
3271 3272
            virBufferAddLit(&buffer, "sched.cpu.shares = \"high\"\n");
        } else {
3273
            virBufferAsprintf(&buffer, "sched.cpu.shares = \"%llu\"\n",
3274 3275 3276 3277
                              def->cputune.shares);
        }
    }

M
Matthias Bolte 已提交
3278 3279 3280 3281
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
3282
            if (virVMXFormatVNC(def->graphics[i], &buffer) < 0)
3283
                goto cleanup;
M
Matthias Bolte 已提交
3284 3285 3286

            break;

3287 3288 3289 3290
          case VIR_DOMAIN_GRAPHICS_TYPE_SDL:
          case VIR_DOMAIN_GRAPHICS_TYPE_RDP:
          case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP:
          case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
3291
          case VIR_DOMAIN_GRAPHICS_TYPE_EGL_HEADLESS:
3292 3293 3294
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported graphics type '%s'"),
                           virDomainGraphicsTypeToString(def->graphics[i]->type));
3295
            goto cleanup;
3296 3297 3298 3299 3300

          case VIR_DOMAIN_GRAPHICS_TYPE_LAST:
          default:
              virReportEnumRangeError(virDomainGraphicsType, def->graphics[i]->type);
              goto cleanup;
M
Matthias Bolte 已提交
3301 3302 3303
        }
    }

M
Matthias Bolte 已提交
3304
    /* def:disks */
3305
    for (i = 0; i < def->ndisks; ++i) {
3306
        if (virVMXVerifyDiskAddress(xmlopt, def->disks[i], def) < 0 ||
3307
            virVMXHandleLegacySCSIDiskDriverName(def, def->disks[i]) < 0) {
3308
            goto cleanup;
3309 3310
        }
    }
M
Matthias Bolte 已提交
3311

3312 3313
    if (virVMXGatherSCSIControllers(ctx, def, scsi_virtualDev,
                                    scsi_present) < 0) {
3314
        goto cleanup;
M
Matthias Bolte 已提交
3315 3316 3317 3318
    }

    for (i = 0; i < 4; ++i) {
        if (scsi_present[i]) {
3319 3320
            hasSCSI = true;

3321
            virBufferAsprintf(&buffer, "scsi%zu.present = \"true\"\n", i);
M
Matthias Bolte 已提交
3322

3323
            if (scsi_virtualDev[i] != -1) {
3324
                virBufferAsprintf(&buffer, "scsi%zu.virtualDev = \"%s\"\n", i,
3325
                                  virVMXControllerModelSCSITypeToString
3326
                                    (scsi_virtualDev[i]));
M
Matthias Bolte 已提交
3327 3328 3329 3330 3331 3332 3333 3334
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
          case VIR_DOMAIN_DISK_DEVICE_CDROM:
3335
          case VIR_DOMAIN_DISK_DEVICE_LUN:
3336
            if (virVMXFormatDisk(ctx, def->disks[i], &buffer) < 0)
3337
                goto cleanup;
M
Matthias Bolte 已提交
3338 3339 3340 3341

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
3342 3343
            if (virVMXFormatFloppy(ctx, def->disks[i], &buffer,
                                   floppy_present) < 0) {
3344
                goto cleanup;
M
Matthias Bolte 已提交
3345 3346 3347 3348 3349
            }

            break;

          default:
3350 3351 3352
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported disk device type '%s'"),
                           virDomainDiskDeviceTypeToString(def->disks[i]->device));
3353
            goto cleanup;
M
Matthias Bolte 已提交
3354 3355 3356
        }
    }

3357 3358
    for (i = 0; i < 2; ++i) {
        /* floppy[0..1].present defaults to true, disable it explicitly */
3359
        if (! floppy_present[i])
3360
            virBufferAsprintf(&buffer, "floppy%zu.present = \"false\"\n", i);
3361 3362
    }

M
Matthias Bolte 已提交
3363
    /* def:fss */
3364 3365
    if (def->nfss > 0) {
        virBufferAddLit(&buffer, "isolation.tools.hgfs.disable = \"false\"\n");
3366
        virBufferAsprintf(&buffer, "sharedFolder.maxNum = \"%zu\"\n", def->nfss);
3367 3368 3369
    }

    for (i = 0; i < def->nfss; ++i) {
3370
        if (virVMXFormatFileSystem(def->fss[i], i, &buffer) < 0)
3371 3372
            goto cleanup;
    }
M
Matthias Bolte 已提交
3373 3374 3375

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
3376
        if (virVMXFormatEthernet(def->nets[i], i, &buffer) < 0)
3377
            goto cleanup;
M
Matthias Bolte 已提交
3378 3379 3380 3381 3382 3383 3384 3385
    }

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

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

3386 3387 3388
    /* def:videos */
    if (def->nvideos > 0) {
        if (def->nvideos > 1) {
3389 3390
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("No support for multiple video devices"));
3391 3392 3393
            goto cleanup;
        }

3394
        if (virVMXFormatSVGA(def->videos[0], &buffer) < 0)
3395 3396 3397
            goto cleanup;
    }

M
Matthias Bolte 已提交
3398 3399 3400 3401 3402
    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
3403
        if (virVMXFormatSerial(ctx, def->serials[i], &buffer) < 0)
3404
            goto cleanup;
M
Matthias Bolte 已提交
3405 3406 3407 3408
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
3409
        if (virVMXFormatParallel(ctx, def->parallels[i], &buffer) < 0)
3410
            goto cleanup;
M
Matthias Bolte 已提交
3411 3412
    }

3413 3414 3415 3416
    /* vmx:firmware */
    if (def->os.firmware == VIR_DOMAIN_OS_DEF_FIRMWARE_EFI)
        virBufferAddLit(&buffer, "firmware = \"efi\"\n");

3417 3418 3419
    if (virtualHW_version >= 7) {
        if (hasSCSI) {
            virBufferAddLit(&buffer, "pciBridge0.present = \"true\"\n");
3420

3421 3422 3423
            virBufferAddLit(&buffer, "pciBridge4.present = \"true\"\n");
            virBufferAddLit(&buffer, "pciBridge4.virtualDev = \"pcieRootPort\"\n");
            virBufferAddLit(&buffer, "pciBridge4.functions = \"8\"\n");
3424

3425 3426 3427
            virBufferAddLit(&buffer, "pciBridge5.present = \"true\"\n");
            virBufferAddLit(&buffer, "pciBridge5.virtualDev = \"pcieRootPort\"\n");
            virBufferAddLit(&buffer, "pciBridge5.functions = \"8\"\n");
3428

3429 3430 3431
            virBufferAddLit(&buffer, "pciBridge6.present = \"true\"\n");
            virBufferAddLit(&buffer, "pciBridge6.virtualDev = \"pcieRootPort\"\n");
            virBufferAddLit(&buffer, "pciBridge6.functions = \"8\"\n");
3432

3433 3434 3435 3436 3437 3438
            virBufferAddLit(&buffer, "pciBridge7.present = \"true\"\n");
            virBufferAddLit(&buffer, "pciBridge7.virtualDev = \"pcieRootPort\"\n");
            virBufferAddLit(&buffer, "pciBridge7.functions = \"8\"\n");
        }

        virBufferAddLit(&buffer, "vmci0.present = \"true\"\n");
3439 3440
    }

M
Matthias Bolte 已提交
3441
    /* Get final VMX output */
3442
    if (virBufferCheckError(&buffer) < 0)
3443
        goto cleanup;
M
Matthias Bolte 已提交
3444

3445
    vmx = virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
3446

3447
 cleanup:
3448
    if (vmx == NULL)
3449 3450
        virBufferFreeAndReset(&buffer);

3451 3452
    VIR_FREE(preliminaryDisplayName);
    VIR_FREE(displayName);
3453
    VIR_FREE(annotation);
M
Matthias Bolte 已提交
3454

3455
    return vmx;
M
Matthias Bolte 已提交
3456 3457 3458 3459
}



M
Matthias Bolte 已提交
3460
int
3461
virVMXFormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
3462
{
3463
    virDomainGraphicsListenDefPtr glisten;
3464

M
Matthias Bolte 已提交
3465
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
3466
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3467 3468 3469
        return -1;
    }

3470
    virBufferAddLit(buffer, "RemoteDisplay.vnc.enabled = \"true\"\n");
M
Matthias Bolte 已提交
3471 3472

    if (def->data.vnc.autoport) {
3473
        VIR_WARN("VNC autoport is enabled, but the automatically assigned "
M
Matthias Bolte 已提交
3474 3475 3476 3477 3478 3479 3480
                  "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);
        }

3481
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.port = \"%d\"\n",
M
Matthias Bolte 已提交
3482 3483 3484
                          def->data.vnc.port);
    }

3485 3486
    if ((glisten = virDomainGraphicsGetListen(def, 0)) &&
        glisten->address) {
3487
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.ip = \"%s\"\n",
3488
                          glisten->address);
M
Matthias Bolte 已提交
3489 3490 3491
    }

    if (def->data.vnc.keymap != NULL) {
3492
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.keymap = \"%s\"\n",
M
Matthias Bolte 已提交
3493 3494 3495
                          def->data.vnc.keymap);
    }

3496
    if (def->data.vnc.auth.passwd != NULL) {
3497
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.password = \"%s\"\n",
3498
                          def->data.vnc.auth.passwd);
M
Matthias Bolte 已提交
3499 3500 3501 3502 3503
    }

    return 0;
}

M
Matthias Bolte 已提交
3504
int
3505
virVMXFormatDisk(virVMXContext *ctx, virDomainDiskDefPtr def,
M
Matthias Bolte 已提交
3506
                 virBufferPtr buffer)
M
Matthias Bolte 已提交
3507
{
3508
    int controllerOrBus, unit;
3509
    const char *vmxDeviceType = NULL;
M
Matthias Bolte 已提交
3510
    char *fileName = NULL;
3511
    int type = virDomainDiskGetType(def);
M
Matthias Bolte 已提交
3512

3513 3514 3515
    /* Convert a handful of types to their string values */
    const char *busType = virDomainDiskBusTypeToString(def->bus);
    const char *deviceType = virDomainDeviceTypeToString(def->device);
3516
    const char *diskType = virDomainDeviceTypeToString(type);
3517 3518 3519 3520 3521

    /* 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) ?
M
Matthias Bolte 已提交
3522
                           ".vmdk" : ".iso";
3523 3524 3525

    /* Check that we got a valid device type */
    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK &&
3526 3527
        def->device != VIR_DOMAIN_DISK_DEVICE_CDROM &&
        def->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
3528 3529
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid device type supplied: %s"), deviceType);
M
Matthias Bolte 已提交
3530 3531 3532
        return -1;
    }

3533
    /* We only support type='file' and type='block' */
E
Eric Blake 已提交
3534 3535
    if (type != VIR_STORAGE_TYPE_FILE &&
        type != VIR_STORAGE_TYPE_BLOCK) {
3536 3537 3538 3539
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("%s %s '%s' has unsupported type '%s', expecting "
                         "'%s' or '%s'"), busType, deviceType, def->dst,
                       diskType,
E
Eric Blake 已提交
3540 3541
                       virStorageTypeToString(VIR_STORAGE_TYPE_FILE),
                       virStorageTypeToString(VIR_STORAGE_TYPE_BLOCK));
3542 3543
        return -1;
    }
M
Matthias Bolte 已提交
3544

3545
    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
3546 3547
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3548 3549 3550
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
3551
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
3552
                                          &unit) < 0) {
M
Matthias Bolte 已提交
3553 3554 3555
            return -1;
        }
    } else {
3556
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3557 3558
                       _("Unsupported bus type '%s' for %s"),
                       busType, deviceType);
M
Matthias Bolte 已提交
3559 3560 3561
        return -1;
    }

3562
    if (def->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
E
Eric Blake 已提交
3563
        type == VIR_STORAGE_TYPE_FILE) {
3564
        vmxDeviceType = (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) ?
M
Matthias Bolte 已提交
3565
                        "scsi-hardDisk" : "ata-hardDisk";
3566
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
E
Eric Blake 已提交
3567
        if (type == VIR_STORAGE_TYPE_FILE)
3568 3569 3570
            vmxDeviceType = "cdrom-image";
        else
            vmxDeviceType = "atapi-cdrom";
3571
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
3572 3573 3574 3575 3576 3577 3578
        const char *src = virDomainDiskGetSource(def);

        if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI &&
            src && STRPREFIX(src, "/vmfs/devices/cdrom/"))
            vmxDeviceType = "scsi-passthru";
        else
            vmxDeviceType = "cdrom-raw";
3579
    } else {
3580
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3581 3582
                       _("%s %s '%s' has an unsupported type '%s'"),
                       busType, deviceType, def->dst, diskType);
M
Matthias Bolte 已提交
3583 3584 3585
        return -1;
    }

3586
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3587 3588 3589
                      busType, controllerOrBus, unit);
    virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"%s\"\n",
                      busType, controllerOrBus, unit, vmxDeviceType);
M
Matthias Bolte 已提交
3590

E
Eric Blake 已提交
3591
    if (type == VIR_STORAGE_TYPE_FILE) {
3592 3593
        const char *src = virDomainDiskGetSource(def);

3594
        if (src) {
3595
            if (!virStringHasCaseSuffix(src, fileExt)) {
3596 3597 3598 3599
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Image file for %s %s '%s' has "
                                 "unsupported suffix, expecting '%s'"),
                               busType, deviceType, def->dst, fileExt);
3600
                return -1;
3601
            }
M
Matthias Bolte 已提交
3602

3603 3604 3605 3606
            fileName = ctx->formatFileName(src, ctx->opaque);
        } else if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
            ignore_value(VIR_STRDUP(fileName, "emptyBackingString"));
        }
M
Matthias Bolte 已提交
3607

3608
        if (fileName == NULL)
M
Matthias Bolte 已提交
3609 3610
            return -1;

3611
        virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3612
                          busType, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3613 3614

        VIR_FREE(fileName);
E
Eric Blake 已提交
3615
    } else if (type == VIR_STORAGE_TYPE_BLOCK) {
3616 3617 3618
        const char *src = virDomainDiskGetSource(def);

        if (!src &&
3619 3620 3621 3622 3623 3624
            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 {
3625
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3626
                              busType, controllerOrBus, unit, src);
3627
        }
M
Matthias Bolte 已提交
3628 3629 3630 3631
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
3632
            virBufferAsprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
3633
                              busType, controllerOrBus, unit);
M
Matthias Bolte 已提交
3634
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
3635 3636
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("%s harddisk '%s' has unsupported cache mode '%s'"),
3637
                           busType, def->dst,
3638
                           virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
3639
            return -1;
M
Matthias Bolte 已提交
3640 3641 3642
        }
    }

3643 3644 3645 3646
    if (def->transient)
        virBufferAsprintf(buffer,
                          "%s%d:%d.mode = \"independent-nonpersistent\"\n",
                          busType, controllerOrBus, unit);
M
Matthias Bolte 已提交
3647

M
Matthias Bolte 已提交
3648 3649 3650 3651
    return 0;
}

int
3652 3653
virVMXFormatFloppy(virVMXContext *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer, bool floppy_present[2])
M
Matthias Bolte 已提交
3654
{
3655
    int unit;
M
Matthias Bolte 已提交
3656
    char *fileName = NULL;
3657 3658
    int type = virDomainDiskGetType(def);
    const char *src = virDomainDiskGetSource(def);
M
Matthias Bolte 已提交
3659 3660

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
3661
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3662 3663 3664
        return -1;
    }

3665
    if (virVMXFloppyDiskNameToUnit(def->dst, &unit) < 0)
M
Matthias Bolte 已提交
3666 3667
        return -1;

3668 3669
    floppy_present[unit] = true;

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

E
Eric Blake 已提交
3672
    if (type == VIR_STORAGE_TYPE_FILE) {
3673
        virBufferAsprintf(buffer, "floppy%d.fileType = \"file\"\n", unit);
M
Matthias Bolte 已提交
3674

3675 3676
        if (src) {
            fileName = ctx->formatFileName(src, ctx->opaque);
M
Matthias Bolte 已提交
3677

3678
            if (fileName == NULL)
M
Matthias Bolte 已提交
3679 3680
                return -1;

3681
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3682
                              unit, fileName);
M
Matthias Bolte 已提交
3683 3684 3685

            VIR_FREE(fileName);
        }
E
Eric Blake 已提交
3686
    } else if (type == VIR_STORAGE_TYPE_BLOCK) {
3687
        virBufferAsprintf(buffer, "floppy%d.fileType = \"device\"\n", unit);
M
Matthias Bolte 已提交
3688

3689
        if (src) {
3690
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3691
                              unit, src);
M
Matthias Bolte 已提交
3692
        }
M
Matthias Bolte 已提交
3693
    } else {
3694 3695 3696
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Floppy '%s' has unsupported type '%s', expecting '%s' "
                         "or '%s'"), def->dst,
E
Eric Blake 已提交
3697 3698 3699
                       virStorageTypeToString(type),
                       virStorageTypeToString(VIR_STORAGE_TYPE_FILE),
                       virStorageTypeToString(VIR_STORAGE_TYPE_BLOCK));
M
Matthias Bolte 已提交
3700 3701 3702 3703 3704 3705 3706 3707
        return -1;
    }

    return 0;
}



3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723
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,
3724
                      def->src->path);
3725 3726 3727 3728 3729 3730 3731 3732
    virBufferAsprintf(buffer, "sharedFolder%d.guestName = \"%s\"\n", number,
                      def->dst);

    return 0;
}



M
Matthias Bolte 已提交
3733
int
3734 3735
virVMXFormatEthernet(virDomainNetDefPtr def, int controller,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3736 3737
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
3738
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
3739 3740

    if (controller < 0 || controller > 3) {
3741 3742 3743
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Ethernet controller index %d out of [0..3] range"),
                       controller);
M
Matthias Bolte 已提交
3744 3745 3746
        return -1;
    }

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

3749
    /* def:model -> vmx:virtualDev, vmx:features */
C
Cole Robinson 已提交
3750 3751 3752 3753 3754 3755 3756
    if (def->model) {
        if (def->model != VIR_DOMAIN_NET_MODEL_VLANCE &&
            def->model != VIR_DOMAIN_NET_MODEL_VMXNET &&
            def->model != VIR_DOMAIN_NET_MODEL_VMXNET2 &&
            def->model != VIR_DOMAIN_NET_MODEL_VMXNET3 &&
            def->model != VIR_DOMAIN_NET_MODEL_E1000 &&
            def->model != VIR_DOMAIN_NET_MODEL_E1000E) {
3757 3758 3759
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML entry 'devices/interface/model' "
                             "to be 'vlance' or 'vmxnet' or 'vmxnet2' or 'vmxnet3' "
3760
                             "or 'e1000' or 'e1000e' but found '%s'"),
C
Cole Robinson 已提交
3761
                            virDomainNetModelTypeToString(def->model));
M
Matthias Bolte 已提交
3762 3763 3764
            return -1;
        }

C
Cole Robinson 已提交
3765
        if (def->model == VIR_DOMAIN_NET_MODEL_VMXNET2) {
3766
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"vmxnet\"\n",
3767
                              controller);
3768
            virBufferAsprintf(buffer, "ethernet%d.features = \"15\"\n",
3769 3770
                              controller);
        } else {
3771
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
C
Cole Robinson 已提交
3772 3773
                              controller,
                              virDomainNetModelTypeToString(def->model));
3774
        }
M
Matthias Bolte 已提交
3775 3776 3777 3778 3779
    }

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

        if (def->ifname != NULL) {
3785
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"custom\"\n",
M
Matthias Bolte 已提交
3786
                              controller);
3787
            virBufferAsprintf(buffer, "ethernet%d.vnet = \"%s\"\n",
M
Matthias Bolte 已提交
3788 3789
                              controller, def->ifname);
        } else {
3790
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"bridged\"\n",
M
Matthias Bolte 已提交
3791 3792 3793 3794 3795
                              controller);
        }

        break;

3796 3797 3798 3799 3800
      case VIR_DOMAIN_NET_TYPE_USER:
        virBufferAsprintf(buffer, "ethernet%d.connectionType = \"nat\"\n",
                          controller);
        break;

3801 3802 3803 3804 3805 3806 3807 3808 3809 3810
      case VIR_DOMAIN_NET_TYPE_ETHERNET:
      case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
      case VIR_DOMAIN_NET_TYPE_SERVER:
      case VIR_DOMAIN_NET_TYPE_CLIENT:
      case VIR_DOMAIN_NET_TYPE_MCAST:
      case VIR_DOMAIN_NET_TYPE_NETWORK:
      case VIR_DOMAIN_NET_TYPE_INTERNAL:
      case VIR_DOMAIN_NET_TYPE_DIRECT:
      case VIR_DOMAIN_NET_TYPE_HOSTDEV:
      case VIR_DOMAIN_NET_TYPE_UDP:
3811 3812
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported net type '%s'"),
                       virDomainNetTypeToString(def->type));
M
Matthias Bolte 已提交
3813
        return -1;
3814 3815 3816 3817 3818

      case VIR_DOMAIN_NET_TYPE_LAST:
      default:
          virReportEnumRangeError(virDomainNetType, def->type);
          return -1;
M
Matthias Bolte 已提交
3819 3820
    }

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

3824 3825
    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];
3826 3827

    if (prefix == 0x000c29) {
3828
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
M
Matthias Bolte 已提交
3829
                          controller);
3830
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
M
Matthias Bolte 已提交
3831
                          controller, mac_string);
3832
        virBufferAsprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
M
Matthias Bolte 已提交
3833
                          controller);
3834
    } else if (prefix == 0x005056 && suffix <= 0x3fffff) {
3835
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3836
                          controller);
3837
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3838 3839
                          controller, mac_string);
    } else if (prefix == 0x005056 && suffix >= 0x800000 && suffix <= 0xbfffff) {
3840
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"vpx\"\n",
3841
                          controller);
3842
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
3843
                          controller, mac_string);
M
Matthias Bolte 已提交
3844
    } else {
3845
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3846
                          controller);
3847
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3848
                          controller, mac_string);
3849
        virBufferAsprintf(buffer, "ethernet%d.checkMACAddress = \"false\"\n",
3850
                          controller);
M
Matthias Bolte 已提交
3851 3852 3853 3854 3855 3856 3857 3858
    }

    return 0;
}



int
3859 3860
virVMXFormatSerial(virVMXContext *ctx, virDomainChrDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
3861
{
M
Matthias Bolte 已提交
3862
    char *fileName = NULL;
3863
    const char *protocol;
M
Matthias Bolte 已提交
3864

3865
    if (def->target.port < 0 || def->target.port > 3) {
3866 3867 3868
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Serial port index %d out of [0..3] range"),
                       def->target.port);
M
Matthias Bolte 已提交
3869 3870 3871
        return -1;
    }

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

M
Matthias Bolte 已提交
3874
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3875
    switch (def->source->type) {
M
Matthias Bolte 已提交
3876
      case VIR_DOMAIN_CHR_TYPE_DEV:
3877
        virBufferAsprintf(buffer, "serial%d.fileType = \"device\"\n",
3878
                          def->target.port);
3879
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3880
                          def->target.port, def->source->data.file.path);
M
Matthias Bolte 已提交
3881 3882 3883
        break;

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

3887
        fileName = ctx->formatFileName(def->source->data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3888

3889
        if (fileName == NULL)
M
Matthias Bolte 已提交
3890 3891
            return -1;

3892
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3893
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3894 3895

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3896 3897 3898
        break;

      case VIR_DOMAIN_CHR_TYPE_PIPE:
3899
        virBufferAsprintf(buffer, "serial%d.fileType = \"pipe\"\n",
3900
                          def->target.port);
M
Matthias Bolte 已提交
3901
        /* FIXME: Based on VI Client GUI default */
3902
        virBufferAsprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
3903
                          def->target.port);
M
Matthias Bolte 已提交
3904
        /* FIXME: Based on VI Client GUI default */
3905
        virBufferAsprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
3906
                          def->target.port);
3907
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3908
                          def->target.port, def->source->data.file.path);
M
Matthias Bolte 已提交
3909 3910
        break;

3911
      case VIR_DOMAIN_CHR_TYPE_TCP:
3912
        switch (def->source->data.tcp.protocol) {
3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
          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:
3930 3931 3932
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported character device TCP protocol '%s'"),
                           virDomainChrTcpProtocolTypeToString(
3933
                               def->source->data.tcp.protocol));
3934 3935 3936
            return -1;
        }

3937
        virBufferAsprintf(buffer, "serial%d.fileType = \"network\"\n",
3938
                          def->target.port);
3939
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s://%s:%s\"\n",
3940 3941
                          def->target.port, protocol, def->source->data.tcp.host,
                          def->source->data.tcp.service);
3942
        virBufferAsprintf(buffer, "serial%d.network.endPoint = \"%s\"\n",
3943
                          def->target.port,
3944
                          def->source->data.tcp.listen ? "server" : "client");
3945 3946
        break;

M
Matthias Bolte 已提交
3947
      default:
3948 3949
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
3950
                       virDomainChrTypeToString(def->source->type));
M
Matthias Bolte 已提交
3951 3952 3953 3954 3955
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
3956
    virBufferAsprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
3957
                      def->target.port);
M
Matthias Bolte 已提交
3958 3959 3960 3961 3962 3963 3964

    return 0;
}



int
3965 3966
virVMXFormatParallel(virVMXContext *ctx, virDomainChrDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3967
{
M
Matthias Bolte 已提交
3968 3969
    char *fileName = NULL;

3970
    if (def->target.port < 0 || def->target.port > 2) {
3971 3972 3973
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"),
                       def->target.port);
M
Matthias Bolte 已提交
3974 3975 3976
        return -1;
    }

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

M
Matthias Bolte 已提交
3980
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3981
    switch (def->source->type) {
M
Matthias Bolte 已提交
3982
      case VIR_DOMAIN_CHR_TYPE_DEV:
3983
        virBufferAsprintf(buffer, "parallel%d.fileType = \"device\"\n",
3984
                          def->target.port);
3985
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3986
                          def->target.port, def->source->data.file.path);
M
Matthias Bolte 已提交
3987 3988 3989
        break;

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

3993
        fileName = ctx->formatFileName(def->source->data.file.path, ctx->opaque);
M
Matthias Bolte 已提交
3994

3995
        if (fileName == NULL)
M
Matthias Bolte 已提交
3996 3997
            return -1;

3998
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3999
                          def->target.port, fileName);
M
Matthias Bolte 已提交
4000 4001

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
4002 4003 4004
        break;

      default:
4005 4006
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
4007
                       virDomainChrTypeToString(def->source->type));
M
Matthias Bolte 已提交
4008 4009 4010 4011 4012
        return -1;
    }

    return 0;
}
4013 4014 4015 4016 4017 4018



int
virVMXFormatSVGA(virDomainVideoDefPtr def, virBufferPtr buffer)
{
4019 4020
    unsigned long long vram;

4021
    if (def->type != VIR_DOMAIN_VIDEO_TYPE_VMVGA) {
4022 4023 4024
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported video device type '%s'"),
                       virDomainVideoTypeToString(def->type));
4025 4026 4027 4028 4029 4030 4031
        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
     */
4032
    vram = VIR_DIV_UP(def->vram, 64) * 64;
4033 4034

    if (def->heads > 1) {
4035 4036
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Multi-head video devices are unsupported"));
4037 4038 4039
        return -1;
    }

4040
    virBufferAsprintf(buffer, "svga.vramSize = \"%lld\"\n",
4041
                      vram * 1024); /* kilobyte to byte */
4042 4043 4044

    return 0;
}