vmx.c 128.3 KB
Newer Older
1
/*
2
 * vmx.c: VMware VMX parsing/formatting functions
3
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
5
 * Copyright (C) 2009-2010 Matthias Bolte <matthias.bolte@googlemail.com>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */

#include <config.h>

25 26
#include <c-ctype.h>

27
#include "internal.h"
28
#include "virerror.h"
29
#include "virfile.h"
30
#include "virconf.h"
31
#include "viralloc.h"
32
#include "virlog.h"
33
#include "viruuid.h"
34
#include "vmx.h"
M
Martin Kletzander 已提交
35
#include "viruri.h"
36
#include "virstring.h"
37

38 39
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
E
Eric Blake 已提交
62
def->maxvcpus = <value>           <=>   numvcpus = "<value>"                    # must be 1 or a multiple of 2, 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 506
#define VMX_BUILD_NAME(_suffix)                                               \
    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 512
VIR_ENUM_DECL(virVMXControllerModelSCSI)
VIR_ENUM_IMPL(virVMXControllerModelSCSI, VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LAST,
513
              "auto", /* just to match virDomainControllerModel, will never be used */
514 515 516
              "buslogic",
              "lsilogic",
              "lsisas1068",
517
              "pvscsi",
518
              "UNUSED ibmvscsi",
519 520
              "UNUSED virtio-scsi",
              "UNUSED lsisas1078");
521 522


523 524 525
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Helpers
 */
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
static int
vmxDomainDefPostParse(virDomainDefPtr def ATTRIBUTE_UNUSED,
                      virCapsPtr caps ATTRIBUTE_UNUSED,
                      void *opaque ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
vmxDomainDeviceDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
                            const virDomainDef *def ATTRIBUTE_UNUSED,
                            virCapsPtr caps ATTRIBUTE_UNUSED,
                            void *opaque ATTRIBUTE_UNUSED)
{
    return 0;
}
542

543
virDomainDefParserConfig virVMXDomainDefParserConfig = {
544
    .hasWideSCSIBus = true,
545
    .macPrefix = {0x00, 0x0c, 0x29},
546 547
    .devicesPostParseCallback = vmxDomainDeviceDefPostParse,
    .domainPostParseCallback = vmxDomainDefPostParse,
548 549 550 551 552 553 554 555 556 557
};


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

558 559 560 561 562 563 564 565 566 567
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') {
568
        if (*tmp1 == escape || strspn(tmp1, special) > 0)
569 570 571 572 573 574
            length += 2;

        ++tmp1;
        ++length;
    }

575
    if (VIR_ALLOC_N(escaped, length) < 0)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
        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 已提交
603
int
604 605 606 607 608 609 610 611
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) {
612
            if (!c_isxdigit(tmp1[1]) || !c_isxdigit(tmp1[2]))
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
                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) {
640 641
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("libxml2 doesn't handle %s encoding"), encoding);
642 643 644 645 646 647 648
        return NULL;
    }

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

    if (xmlCharEncInFunc(handler, utf8, input) < 0) {
649 650
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not convert from %s to UTF-8 encoding"), encoding);
651 652 653 654 655 656
        goto cleanup;
    }

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

657
 cleanup:
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    xmlCharEncCloseFunc(handler);
    xmlBufferFree(input);
    xmlBufferFree(utf8);

    return result;
}



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

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

    if (value == NULL) {
677
        if (optional)
678 679
            return 0;

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

    if (value->type != VIR_CONF_STRING) {
686 687
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
688 689 690 691
        return -1;
    }

    if (value->str == NULL) {
692
        if (optional)
693 694
            return 0;

695 696
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing essential config entry '%s'"), name);
697 698 699
        return -1;
    }

700
    return VIR_STRDUP(*string, value->str);
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
}



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

    value = virConfGetValue(conf, name);

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

    if (value->type != VIR_CONF_STRING) {
724 725
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
726 727 728 729 730 731 732
        return -1;
    }

    if (value->str == NULL) {
        if (optional) {
            return 0;
        } else {
733 734
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
735 736 737 738 739
            return -1;
        }
    }

    if (virUUIDParse(value->str, uuid) < 0) {
740 741
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse UUID from string '%s'"), value->str);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
        return -1;
    }

    return 0;
}



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

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

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
763 764
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
765 766 767 768 769 770 771 772 773
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
774 775
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Missing essential config entry '%s'"), name);
776 777 778 779
                return -1;
            }
        }

780
        if (STRCASEEQ(value->str, "unlimited")) {
781 782
            *number = -1;
        } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) {
783 784 785
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Config entry '%s' must represent an integer value"),
                           name);
786 787 788
            return -1;
        }
    } else {
789 790
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
        return -1;
    }

    return 0;
}



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

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

    if (value == NULL) {
        if (optional) {
            return 0;
        } else {
812 813
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Missing essential config entry '%s'"), name);
814 815 816 817 818 819 820 821 822
            return -1;
        }
    }

    if (value->type == VIR_CONF_STRING) {
        if (value->str == NULL) {
            if (optional) {
                return 0;
            } else {
823 824
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Missing essential config entry '%s'"), name);
825 826 827 828 829 830 831 832 833
                return -1;
            }
        }

        if (STRCASEEQ(value->str, "true")) {
            *boolean_ = 1;
        } else if (STRCASEEQ(value->str, "false")) {
            *boolean_ = 0;
        } else {
834 835 836
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Config entry '%s' must represent a boolean value "
                             "(true|false)"), name);
837 838 839
            return -1;
        }
    } else {
840 841
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Config entry '%s' must be a string"), name);
842 843 844 845 846 847 848 849 850 851
        return -1;
    }

    return 0;
}



static int
virVMXSCSIDiskNameToControllerAndUnit(const char *name, int *controller, int *unit)
M
Matthias Bolte 已提交
852 853 854 855
{
    int idx;

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

    idx = virDiskNameToIndex(name);

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

870
    /* Each of the 4 SCSI controllers has 1 bus with 15 units each for devices */
M
Matthias Bolte 已提交
871
    if (idx >= (4 * 15)) {
872 873
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
874 875 876 877
        return -1;
    }

    *controller = idx / 15;
878
    *unit = idx % 15;
M
Matthias Bolte 已提交
879

880
    /* Skip the controller ifself at unit 7 */
881
    if (*unit >= 7)
882
        ++(*unit);
M
Matthias Bolte 已提交
883 884 885 886 887 888

    return 0;
}



889 890
static int
virVMXIDEDiskNameToBusAndUnit(const char *name, int *bus, int *unit)
M
Matthias Bolte 已提交
891 892 893 894
{
    int idx;

    if (! STRPREFIX(name, "hd")) {
895 896 897
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'hd'"));
M
Matthias Bolte 已提交
898 899 900 901 902 903
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
904 905
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
906 907 908
        return -1;
    }

909
    /* The IDE controller has 2 buses with 2 units each for devices */
M
Matthias Bolte 已提交
910
    if (idx >= (2 * 2)) {
911 912
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("IDE disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
913 914 915
        return -1;
    }

916 917
    *bus = idx / 2;
    *unit = idx % 2;
M
Matthias Bolte 已提交
918 919 920 921 922 923

    return 0;
}



924 925
static int
virVMXFloppyDiskNameToUnit(const char *name, int *unit)
M
Matthias Bolte 已提交
926 927 928 929
{
    int idx;

    if (! STRPREFIX(name, "fd")) {
930 931 932
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Expecting domain XML attribute 'dev' of entry "
                         "'devices/disk/target' to start with 'fd'"));
M
Matthias Bolte 已提交
933 934 935 936 937 938
        return -1;
    }

    idx = virDiskNameToIndex(name);

    if (idx < 0) {
939 940
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Could not parse valid disk index from '%s'"), name);
M
Matthias Bolte 已提交
941 942 943
        return -1;
    }

944
    /* The FDC controller has 1 bus with 2 units for devices */
M
Matthias Bolte 已提交
945
    if (idx >= 2) {
946 947
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Floppy disk index (parsed from '%s') is too large"), name);
M
Matthias Bolte 已提交
948 949 950
        return -1;
    }

951 952 953 954 955 956 957
    *unit = idx;

    return 0;
}



958
static int
959
virVMXVerifyDiskAddress(virDomainXMLOptionPtr xmlopt, virDomainDiskDefPtr disk)
960 961 962 963 964 965 966
{
    virDomainDiskDef def;
    virDomainDeviceDriveAddressPtr drive;

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

    if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
967 968 969
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported disk address type '%s'"),
                       virDomainDeviceAddressTypeToString(disk->info.type));
970 971 972 973 974 975 976 977
        return -1;
    }

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

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

978
    if (virDomainDiskDefAssignAddress(xmlopt, &def) < 0) {
979 980
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Could not verify disk address"));
981 982 983 984 985 986
        return -1;
    }

    if (def.info.addr.drive.controller != drive->controller ||
        def.info.addr.drive.bus != drive->bus ||
        def.info.addr.drive.unit != drive->unit) {
987 988 989
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Disk address %d:%d:%d doesn't match target device '%s'"),
                       drive->controller, drive->bus, drive->unit, disk->dst);
990 991 992 993 994 995
        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) {
996 997 998
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("SCSI controller index %d out of [0..3] range"),
                           drive->controller);
999 1000 1001 1002
            return -1;
        }

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

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

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

        if (drive->unit > 1) {
1031 1032 1033
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("IDE unit index %d out of [0..1] range"),
                           drive->unit);
1034 1035 1036 1037
            return -1;
        }
    } else if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
        if (drive->controller != 0) {
1038 1039 1040
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC controller index %d out of [0] range"),
                           drive->controller);
1041 1042 1043 1044
            return -1;
        }

        if (drive->bus != 0) {
1045 1046 1047
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC bus index %d out of [0] range"),
                           drive->bus);
1048 1049 1050 1051
            return -1;
        }

        if (drive->unit > 1) {
1052 1053 1054
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("FDC unit index %d out of [0..1] range"),
                           drive->unit);
1055 1056 1057
            return -1;
        }
    } else {
1058 1059 1060
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported bus type '%s'"),
                       virDomainDiskBusTypeToString(disk->bus));
1061 1062 1063 1064 1065 1066 1067 1068
        return -1;
    }

    return 0;
}



1069 1070 1071
static int
virVMXHandleLegacySCSIDiskDriverName(virDomainDefPtr def,
                                     virDomainDiskDefPtr disk)
1072 1073
{
    char *tmp;
1074 1075
    int model;
    size_t i;
1076
    virDomainControllerDefPtr controller = NULL;
1077 1078
    const char *driver = virDomainDiskGetDriver(disk);
    char *copy;
1079

1080
    if (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI || !driver)
1081 1082
        return 0;

1083 1084 1085
    if (VIR_STRDUP(copy, driver) < 0)
        return -1;
    tmp = copy;
1086

1087
    for (; *tmp != '\0'; ++tmp)
1088 1089
        *tmp = c_tolower(*tmp);

1090 1091
    model = virDomainControllerModelSCSITypeFromString(copy);
    VIR_FREE(copy);
1092 1093

    if (model < 0) {
1094
        virReportError(VIR_ERR_INTERNAL_ERROR,
1095
                       _("Unknown driver name '%s'"), driver);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
        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) {
1107 1108 1109
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Missing SCSI controller for index %d"),
                       disk->info.addr.drive.controller);
1110 1111 1112 1113 1114 1115
        return -1;
    }

    if (controller->model == -1) {
        controller->model = model;
    } else if (controller->model != model) {
1116 1117
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Inconsistent SCSI controller model ('%s' is not '%s') "
1118
                         "for SCSI controller index %d"), driver,
1119 1120
                       virDomainControllerModelSCSITypeToString(controller->model),
                       controller->idx);
1121 1122
        return -1;
    }
M
Matthias Bolte 已提交
1123 1124 1125 1126 1127 1128

    return 0;
}



1129 1130 1131
static int
virVMXGatherSCSIControllers(virVMXContext *ctx, virDomainDefPtr def,
                            int virtualDev[4], bool present[4])
1132 1133
{
    int result = -1;
1134
    size_t i, k;
M
Matthias Bolte 已提交
1135
    virDomainDiskDefPtr disk;
1136 1137 1138 1139
    virDomainControllerDefPtr controller;
    bool controllerHasDisksAttached;
    int count = 0;
    int *autodetectedModels;
M
Matthias Bolte 已提交
1140

1141
    if (VIR_ALLOC_N(autodetectedModels, def->ndisks) < 0)
1142 1143 1144 1145
        return -1;

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

1147
        if (controller->type != VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1148
            /* skip non-SCSI controllers */
M
Matthias Bolte 已提交
1149 1150 1151
            continue;
        }

1152
        controllerHasDisksAttached = false;
1153

1154 1155 1156 1157 1158 1159
        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;
1160 1161
                break;
            }
M
Matthias Bolte 已提交
1162 1163
        }

1164
        if (! controllerHasDisksAttached) {
1165
            /* skip SCSI controllers without attached disks */
1166 1167 1168
            continue;
        }

1169
        if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_AUTO &&
1170
            ctx->autodetectSCSIControllerModel != NULL) {
1171 1172
            count = 0;

1173 1174
            /* try to autodetect the SCSI controller model by collecting
             * SCSI controller model of all disks attached to this controller */
1175 1176 1177 1178 1179
            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) {
1180 1181 1182
                    if (ctx->autodetectSCSIControllerModel
                               (disk, &autodetectedModels[count],
                                ctx->opaque) < 0) {
1183 1184 1185 1186 1187 1188 1189
                        goto cleanup;
                    }

                    ++count;
                }
            }

1190 1191
            /* autodetection fails when the disks attached to one controller
             * have inconsistent SCSI controller models */
1192 1193
            for (k = 0; k < count; ++k) {
                if (autodetectedModels[k] != autodetectedModels[0]) {
1194 1195 1196 1197
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Disks on SCSI controller %d have inconsistent "
                                     "controller models, cannot autodetect model"),
                                   controller->idx);
1198 1199 1200 1201 1202
                    goto cleanup;
                }
            }

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

1205
        if (controller->model != -1 &&
1206 1207 1208 1209
            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) {
1210 1211 1212 1213 1214
            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));
1215
            goto cleanup;
M
Matthias Bolte 已提交
1216
        }
1217 1218 1219

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

1222 1223
    result = 0;

1224
 cleanup:
1225 1226 1227
    VIR_FREE(autodetectedModels);

    return result;
M
Matthias Bolte 已提交
1228 1229 1230 1231 1232 1233 1234 1235
}



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

1236
virDomainDefPtr
1237 1238 1239
virVMXParseConfig(virVMXContext *ctx,
                  virDomainXMLOptionPtr xmlopt,
                  const char *vmx)
1240
{
M
Matthias Bolte 已提交
1241
    bool success = false;
1242
    virConfPtr conf = NULL;
1243 1244
    char *encoding = NULL;
    char *utf8;
1245 1246 1247 1248
    virDomainDefPtr def = NULL;
    long long config_version = 0;
    long long virtualHW_version = 0;
    long long memsize = 0;
1249 1250
    long long sched_mem_max = 0;
    long long sched_mem_minsize = 0;
1251 1252
    long long numvcpus = 0;
    char *sched_cpu_affinity = NULL;
1253
    char *sched_cpu_shares = NULL;
M
Matthias Bolte 已提交
1254
    char *guestOS = NULL;
M
Matthias Bolte 已提交
1255
    bool smbios_reflecthost = false;
1256
    int controller;
1257
    int bus;
1258
    int port;
1259
    bool present;
1260 1261
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
    int unit;
1262 1263
    bool hgfs_disabled = true;
    long long sharedFolder_maxNum = 0;
H
Hu Tao 已提交
1264
    int cpumasklen;
1265

1266
    if (ctx->parseFileName == NULL) {
1267 1268
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no parseFileName function set"));
1269 1270 1271
        return NULL;
    }

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

1274
    if (conf == NULL)
1275 1276
        return NULL;

1277
    /* vmx:.encoding */
1278
    if (virVMXGetConfigString(conf, ".encoding", &encoding, true) < 0)
1279 1280 1281 1282 1283 1284 1285 1286
        goto cleanup;

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

1287
        utf8 = virVMXConvertToUTF8(encoding, vmx);
1288

1289
        if (utf8 == NULL)
1290 1291 1292 1293 1294 1295
            goto cleanup;

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

        VIR_FREE(utf8);

1296
        if (conf == NULL)
1297 1298 1299 1300
            goto cleanup;
    }

    /* Allocate domain def */
1301
    if (!(def = virDomainDefNew()))
A
ajia@redhat.com 已提交
1302
        goto cleanup;
1303

1304
    def->virtType = VIR_DOMAIN_VIRT_VMWARE;
1305 1306
    def->id = -1;

M
Matthias Bolte 已提交
1307
    /* vmx:config.version */
1308 1309
    if (virVMXGetConfigLong(conf, "config.version", &config_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1310
        goto cleanup;
1311 1312 1313
    }

    if (config_version != 8) {
1314 1315 1316
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'config.version' to be 8 but found "
                         "%lld"), config_version);
M
Matthias Bolte 已提交
1317
        goto cleanup;
1318 1319
    }

M
Matthias Bolte 已提交
1320
    /* vmx:virtualHW.version */
1321 1322
    if (virVMXGetConfigLong(conf, "virtualHW.version", &virtualHW_version, 0,
                            false) < 0) {
M
Matthias Bolte 已提交
1323
        goto cleanup;
1324 1325
    }

1326
    if (virtualHW_version < 4) {
1327
        virReportError(VIR_ERR_INTERNAL_ERROR,
1328
                       _("Expecting VMX entry 'virtualHW.version' to be "
1329
                         "4 or higher but found %lld"),
1330
                       virtualHW_version);
M
Matthias Bolte 已提交
1331
        goto cleanup;
1332 1333
    }

M
Matthias Bolte 已提交
1334
    /* vmx:uuid.bios -> def:uuid */
1335
    /* FIXME: Need to handle 'uuid.action = "create"' */
1336
    if (virVMXGetConfigUUID(conf, "uuid.bios", def->uuid, true) < 0)
M
Matthias Bolte 已提交
1337
        goto cleanup;
1338

M
Matthias Bolte 已提交
1339
    /* vmx:displayName -> def:name */
1340
    if (virVMXGetConfigString(conf, "displayName", &def->name, true) < 0)
M
Matthias Bolte 已提交
1341
        goto cleanup;
1342

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

1352
    /* vmx:annotation -> def:description */
1353 1354
    if (virVMXGetConfigString(conf, "annotation", &def->description,
                              true) < 0) {
1355 1356 1357 1358
        goto cleanup;
    }

    if (def->description != NULL) {
1359
        if (virVMXUnescapeHexPipe(def->description) < 0) {
1360 1361 1362
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("VMX entry 'annotation' contains invalid escape "
                             "sequence"));
1363
            goto cleanup;
1364 1365 1366
        }
    }

1367
    /* vmx:memsize -> def:mem.max_balloon */
1368
    if (virVMXGetConfigLong(conf, "memsize", &memsize, 32, true) < 0)
M
Matthias Bolte 已提交
1369
        goto cleanup;
1370 1371

    if (memsize <= 0 || memsize % 4 != 0) {
1372 1373 1374
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'memsize' to be an unsigned "
                         "integer (multiple of 4) but found %lld"), memsize);
M
Matthias Bolte 已提交
1375
        goto cleanup;
1376 1377
    }

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

1380
    /* vmx:sched.mem.max -> def:mem.cur_balloon */
1381 1382
    if (virVMXGetConfigLong(conf, "sched.mem.max", &sched_mem_max, memsize,
                            true) < 0) {
M
Matthias Bolte 已提交
1383
        goto cleanup;
1384 1385
    }

1386
    if (sched_mem_max < 0)
1387
        sched_mem_max = memsize;
1388

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

1391
    if (def->mem.cur_balloon > def->mem.max_balloon)
1392
        def->mem.cur_balloon = def->mem.max_balloon;
1393

1394
    /* vmx:sched.mem.minsize -> def:mem.min_guarantee */
1395 1396
    if (virVMXGetConfigLong(conf, "sched.mem.minsize", &sched_mem_minsize, 0,
                            true) < 0) {
1397 1398 1399
        goto cleanup;
    }

1400
    if (sched_mem_minsize < 0)
1401 1402 1403 1404
        sched_mem_minsize = 0;

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

1405
    if (def->mem.min_guarantee > def->mem.max_balloon)
1406 1407
        def->mem.min_guarantee = def->mem.max_balloon;

M
Matthias Bolte 已提交
1408
    /* vmx:numvcpus -> def:vcpus */
1409
    if (virVMXGetConfigLong(conf, "numvcpus", &numvcpus, 1, true) < 0)
M
Matthias Bolte 已提交
1410
        goto cleanup;
1411 1412

    if (numvcpus <= 0 || (numvcpus % 2 != 0 && numvcpus != 1)) {
1413 1414 1415
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry 'numvcpus' to be an unsigned "
                         "integer (1 or a multiple of 2) but found %lld"), numvcpus);
M
Matthias Bolte 已提交
1416
        goto cleanup;
1417 1418
    }

E
Eric Blake 已提交
1419
    def->maxvcpus = def->vcpus = numvcpus;
1420

M
Matthias Bolte 已提交
1421
    /* vmx:sched.cpu.affinity -> def:cpumask */
1422
    /* NOTE: maps to VirtualMachine:config.cpuAffinity.affinitySet */
1423 1424
    if (virVMXGetConfigString(conf, "sched.cpu.affinity", &sched_cpu_affinity,
                              true) < 0) {
M
Matthias Bolte 已提交
1425
        goto cleanup;
1426 1427
    }

1428
    if (sched_cpu_affinity != NULL && STRCASENEQ(sched_cpu_affinity, "all")) {
1429 1430 1431
        const char *current = sched_cpu_affinity;
        int number, count = 0;

H
Hu Tao 已提交
1432
        cpumasklen = 0;
1433

H
Hu Tao 已提交
1434
        def->cpumask = virBitmapNew(VIR_DOMAIN_CPUMASK_LEN);
1435
        if (!def->cpumask)
M
Matthias Bolte 已提交
1436
            goto cleanup;
1437 1438 1439 1440 1441 1442 1443

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

            number = virParseNumber(&current);

            if (number < 0) {
1444 1445 1446 1447
                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 已提交
1448
                goto cleanup;
1449 1450 1451
            }

            if (number >= VIR_DOMAIN_CPUMASK_LEN) {
1452 1453 1454
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("VMX entry 'sched.cpu.affinity' contains a %d, "
                                 "this value is too large"), number);
M
Matthias Bolte 已提交
1455
                goto cleanup;
1456 1457
            }

1458
            if (number + 1 > cpumasklen)
H
Hu Tao 已提交
1459
                cpumasklen = number + 1;
1460

H
Hu Tao 已提交
1461
            ignore_value(virBitmapSetBit(def->cpumask, number));
1462 1463 1464 1465 1466 1467 1468 1469 1470
            ++count;

            virSkipSpaces(&current);

            if (*current == ',') {
                ++current;
            } else if (*current == '\0') {
                break;
            } else {
1471 1472 1473 1474
                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 已提交
1475
                goto cleanup;
1476 1477 1478 1479 1480 1481
            }

            virSkipSpaces(&current);
        }

        if (count < numvcpus) {
1482 1483 1484 1485
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry 'sched.cpu.affinity' to contain "
                             "at least as many values as 'numvcpus' (%lld) but "
                             "found only %d value(s)"), numvcpus, count);
M
Matthias Bolte 已提交
1486
            goto cleanup;
1487 1488 1489
        }
    }

1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
    /* vmx:sched.cpu.shares -> def:cputune.shares */
    if (virVMXGetConfigString(conf, "sched.cpu.shares", &sched_cpu_shares,
                              true) < 0) {
        goto cleanup;
    }

    if (sched_cpu_shares != NULL) {
        /* See http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.SharesInfo.Level.html */
        if (STRCASEEQ(sched_cpu_shares, "low")) {
            def->cputune.shares = def->vcpus * 500;
        } else if (STRCASEEQ(sched_cpu_shares, "normal")) {
            def->cputune.shares = def->vcpus * 1000;
        } else if (STRCASEEQ(sched_cpu_shares, "high")) {
            def->cputune.shares = def->vcpus * 2000;
        } else if (virStrToLong_ul(sched_cpu_shares, NULL, 10,
                                   &def->cputune.shares) < 0) {
1506 1507 1508 1509
            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);
1510 1511
            goto cleanup;
        }
1512
        def->cputune.sharesSpecified = true;
1513 1514
    }

1515 1516 1517 1518 1519 1520
    /* def:lifecycle */
    def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
    def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
    def->onCrash = VIR_DOMAIN_LIFECYCLE_DESTROY;

    /* def:os */
1521
    if (VIR_STRDUP(def->os.type, "hvm") < 0)
M
Matthias Bolte 已提交
1522
        goto cleanup;
1523

M
Matthias Bolte 已提交
1524
    /* vmx:guestOS -> def:os.arch */
1525
    if (virVMXGetConfigString(conf, "guestOS", &guestOS, true) < 0)
M
Matthias Bolte 已提交
1526
        goto cleanup;
M
Matthias Bolte 已提交
1527

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

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

1540
    if (smbios_reflecthost)
M
Matthias Bolte 已提交
1541 1542
        def->os.smbios_mode = VIR_DOMAIN_SMBIOS_HOST;

1543 1544
    /* def:features */
    /* FIXME */
1545

1546 1547
    /* def:clock */
    /* FIXME */
1548 1549

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

    def->ngraphics = 0;

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

1558
    if (def->graphics[def->ngraphics] != NULL)
M
Matthias Bolte 已提交
1559
        ++def->ngraphics;
1560

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

    def->ndisks = 0;

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

1574
        if (! present)
1575 1576
            continue;

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

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

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

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

1603
            if (def->disks[def->ndisks] != NULL)
1604 1605 1606 1607 1608
                ++def->ndisks;
        }
    }

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

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

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

1628
            if (def->disks[def->ndisks] != NULL)
1629 1630 1631 1632 1633
                ++def->ndisks;
        }
    }

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

1641
        if (def->disks[def->ndisks] != NULL)
1642 1643 1644
            ++def->ndisks;
    }

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

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

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

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

1680
            if (VIR_ALLOC_N(def->fss, sharedFolder_maxNum) < 0)
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
                goto cleanup;

            def->nfss = 0;

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

1691
                if (def->fss[def->nfss] != NULL)
1692 1693 1694 1695
                    ++def->nfss;
            }
        }
    }
1696 1697

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

    def->nnets = 0;

    for (controller = 0; controller < 4; ++controller) {
1704 1705
        if (virVMXParseEthernet(conf, controller,
                                &def->nets[def->nnets]) < 0) {
M
Matthias Bolte 已提交
1706
            goto cleanup;
1707 1708
        }

1709
        if (def->nets[def->nnets] != NULL)
1710 1711 1712 1713 1714 1715
            ++def->nnets;
    }

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

1716
    /* def:videos */
1717
    if (VIR_ALLOC_N(def->videos, 1) < 0)
1718 1719 1720 1721
        goto cleanup;

    def->nvideos = 0;

1722
    if (virVMXParseSVGA(conf, &def->videos[def->nvideos]) < 0)
1723 1724 1725 1726
        goto cleanup;

    def->nvideos = 1;

1727 1728 1729 1730 1731 1732 1733
    /* def:sounds */
    /* FIXME */

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

    /* def:serials */
1734
    if (VIR_ALLOC_N(def->serials, 4) < 0)
M
Matthias Bolte 已提交
1735
        goto cleanup;
1736 1737 1738 1739

    def->nserials = 0;

    for (port = 0; port < 4; ++port) {
1740 1741
        if (virVMXParseSerial(ctx, conf, port,
                              &def->serials[def->nserials]) < 0) {
M
Matthias Bolte 已提交
1742
            goto cleanup;
1743 1744
        }

1745
        if (def->serials[def->nserials] != NULL)
1746 1747 1748 1749
            ++def->nserials;
    }

    /* def:parallels */
1750
    if (VIR_ALLOC_N(def->parallels, 3) < 0)
M
Matthias Bolte 已提交
1751
        goto cleanup;
1752 1753 1754 1755

    def->nparallels = 0;

    for (port = 0; port < 3; ++port) {
1756 1757
        if (virVMXParseParallel(ctx, conf, port,
                                &def->parallels[def->nparallels]) < 0) {
M
Matthias Bolte 已提交
1758
            goto cleanup;
1759 1760
        }

1761
        if (def->parallels[def->nparallels] != NULL)
1762 1763 1764
            ++def->nparallels;
    }

M
Matthias Bolte 已提交
1765 1766
    success = true;

1767
 cleanup:
M
Matthias Bolte 已提交
1768 1769 1770 1771 1772
    if (! success) {
        virDomainDefFree(def);
        def = NULL;
    }

1773
    virConfFree(conf);
1774
    VIR_FREE(encoding);
1775
    VIR_FREE(sched_cpu_affinity);
1776
    VIR_FREE(sched_cpu_shares);
M
Matthias Bolte 已提交
1777
    VIR_FREE(guestOS);
1778 1779 1780 1781 1782 1783

    return def;
}



M
Matthias Bolte 已提交
1784
int
1785
virVMXParseVNC(virConfPtr conf, virDomainGraphicsDefPtr *def)
M
Matthias Bolte 已提交
1786
{
1787
    bool enabled = false;
M
Matthias Bolte 已提交
1788
    long long port = 0;
1789
    char *listenAddr = NULL;
M
Matthias Bolte 已提交
1790 1791

    if (def == NULL || *def != NULL) {
1792
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
1793 1794 1795
        return -1;
    }

1796 1797
    if (virVMXGetConfigBoolean(conf, "RemoteDisplay.vnc.enabled", &enabled,
                               false, true) < 0) {
M
Matthias Bolte 已提交
1798 1799 1800
        return -1;
    }

1801
    if (! enabled)
M
Matthias Bolte 已提交
1802 1803
        return 0;

1804
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
1805 1806 1807 1808
        goto failure;

    (*def)->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

1809 1810 1811
    if (virVMXGetConfigLong(conf, "RemoteDisplay.vnc.port", &port, -1,
                            true) < 0 ||
        virVMXGetConfigString(conf, "RemoteDisplay.vnc.ip",
1812
                              &listenAddr, true) < 0 ||
1813 1814 1815 1816
        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 已提交
1817 1818 1819
        goto failure;
    }

1820 1821 1822 1823 1824 1825
    if (listenAddr) {
        if (virDomainGraphicsListenSetAddress(*def, 0, listenAddr, -1, true) < 0)
            goto failure;
        VIR_FREE(listenAddr);
    }

M
Matthias Bolte 已提交
1826
    if (port < 0) {
1827
        VIR_WARN("VNC is enabled but VMX entry 'RemoteDisplay.vnc.port' "
M
Matthias Bolte 已提交
1828 1829 1830
                  "is missing, the VNC port is unknown");

        (*def)->data.vnc.port = 0;
1831
        (*def)->data.vnc.autoport = true;
M
Matthias Bolte 已提交
1832
    } else {
1833
        if (port < 5900 || port > 5964)
M
Matthias Bolte 已提交
1834 1835 1836
            VIR_WARN("VNC port %lld it out of [5900..5964] range", port);

        (*def)->data.vnc.port = port;
1837
        (*def)->data.vnc.autoport = false;
M
Matthias Bolte 已提交
1838 1839 1840 1841
    }

    return 0;

1842
 failure:
1843
    VIR_FREE(listenAddr);
M
Matthias Bolte 已提交
1844 1845 1846 1847 1848 1849 1850 1851
    virDomainGraphicsDefFree(*def);
    *def = NULL;

    return -1;
}



1852
int
1853 1854
virVMXParseSCSIController(virConfPtr conf, int controller, bool *present,
                          int *virtualDev)
1855
{
1856
    int result = -1;
1857 1858
    char present_name[32];
    char virtualDev_name[32];
1859 1860
    char *virtualDev_string = NULL;
    char *tmp;
1861

1862
    if (virtualDev == NULL || *virtualDev != -1) {
1863
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
1864
        return -1;
1865 1866 1867
    }

    if (controller < 0 || controller > 3) {
1868 1869 1870
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("SCSI controller index %d out of [0..3] range"),
                       controller);
1871
        return -1;
1872 1873
    }

1874 1875 1876
    snprintf(present_name, sizeof(present_name), "scsi%d.present", controller);
    snprintf(virtualDev_name, sizeof(virtualDev_name), "scsi%d.virtualDev",
             controller);
1877

1878
    if (virVMXGetConfigBoolean(conf, present_name, present, false, true) < 0)
1879
        goto cleanup;
1880 1881

    if (! *present) {
1882 1883
        result = 0;
        goto cleanup;
1884 1885
    }

1886 1887
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev_string,
                              true) < 0) {
1888
        goto cleanup;
1889 1890
    }

1891 1892 1893
    if (virtualDev_string != NULL) {
        tmp = virtualDev_string;

1894
        for (; *tmp != '\0'; ++tmp)
1895 1896
            *tmp = c_tolower(*tmp);

1897
        *virtualDev = virVMXControllerModelSCSITypeFromString(virtualDev_string);
1898 1899

        if (*virtualDev == -1 ||
1900 1901 1902 1903
            (*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)) {
1904 1905 1906 1907
            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);
1908
            goto cleanup;
1909
        }
1910 1911
    }

1912
    result = 0;
1913

1914
 cleanup:
1915
    VIR_FREE(virtualDev_string);
1916

1917
    return result;
1918 1919 1920 1921 1922
}



int
1923
virVMXParseDisk(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virConfPtr conf,
1924 1925
                int device, int busType, int controllerOrBus, int unit,
                virDomainDiskDefPtr *def)
1926 1927
{
    /*
1928 1929 1930
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK,
     *                    VIR_DOMAIN_DISK_DEVICE_CDROM,
     *                    VIR_DOMAIN_DISK_DEVICE_LUN}
1931 1932 1933
     *         busType = VIR_DOMAIN_DISK_BUS_SCSI
     * controllerOrBus = [0..3] -> controller
     *            unit = [0..6,8..15]
1934
     *
1935 1936 1937
     *          device = {VIR_DOMAIN_DISK_DEVICE_DISK,
     *                    VIR_DOMAIN_DISK_DEVICE_CDROM,
     *                    VIR_DOMAIN_DISK_DEVICE_LUN}
1938 1939 1940
     *         busType = VIR_DOMAIN_DISK_BUS_IDE
     * controllerOrBus = [0..1] -> bus
     *            unit = [0..1]
1941
     *
1942 1943 1944 1945
     *          device = VIR_DOMAIN_DISK_DEVICE_FLOPPY
     *         busType = VIR_DOMAIN_DISK_BUS_FDC
     * controllerOrBus = [0]
     *            unit = [0..1]
1946 1947
     */

M
Matthias Bolte 已提交
1948
    int result = -1;
1949 1950 1951
    char *prefix = NULL;

    char present_name[32] = "";
1952
    bool present = false;
1953 1954

    char startConnected_name[32] = "";
1955
    bool startConnected = false;
1956 1957 1958 1959 1960

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

    char clientDevice_name[32] = "";
1961
    bool clientDevice = false;
1962 1963 1964 1965 1966 1967 1968 1969

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

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

    char writeThrough_name[32] = "";
1970
    bool writeThrough = false;
1971

1972 1973 1974
    char mode_name[32] = "";
    char *mode = NULL;

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

1980
    if (!(*def = virDomainDiskDefNew()))
M
Matthias Bolte 已提交
1981
        return -1;
1982 1983

    (*def)->device = device;
1984
    (*def)->bus = busType;
1985 1986 1987 1988

    /* def:dst, def:driverName */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK ||
        device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
1989 1990
        if (busType == VIR_DOMAIN_DISK_BUS_SCSI) {
            if (controllerOrBus < 0 || controllerOrBus > 3) {
1991 1992 1993
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI controller index %d out of [0..3] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
1994
                goto cleanup;
1995 1996
            }

1997
            if (unit < 0 || unit > 15 || unit == 7) {
1998 1999 2000
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("SCSI unit index %d out of [0..6,8..15] range"),
                               unit);
M
Matthias Bolte 已提交
2001
                goto cleanup;
2002 2003
            }

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

M
Matthias Bolte 已提交
2007
            (*def)->dst =
2008
               virIndexToDiskName
2009
                 (controllerOrBus * 15 + (unit < 7 ? unit : unit - 1), "sd");
2010

2011
            if ((*def)->dst == NULL)
M
Matthias Bolte 已提交
2012
                goto cleanup;
2013 2014
        } else if (busType == VIR_DOMAIN_DISK_BUS_IDE) {
            if (controllerOrBus < 0 || controllerOrBus > 1) {
2015 2016 2017
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("IDE bus index %d out of [0..1] range"),
                               controllerOrBus);
M
Matthias Bolte 已提交
2018
                goto cleanup;
2019 2020
            }

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

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

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

2032
            if ((*def)->dst == NULL)
M
Matthias Bolte 已提交
2033
                goto cleanup;
2034
        } else {
2035 2036 2037 2038
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2039
            goto cleanup;
2040 2041
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2042 2043
        if (busType == VIR_DOMAIN_DISK_BUS_FDC) {
            if (controllerOrBus != 0) {
2044 2045 2046
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("FDC controller index %d out of [0] range"),
                               controllerOrBus);
2047 2048 2049 2050
                goto cleanup;
            }

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

2057
            if (virAsprintf(&prefix, "floppy%d", unit) < 0)
M
Matthias Bolte 已提交
2058
                goto cleanup;
2059

2060
            (*def)->dst = virIndexToDiskName(unit, "fd");
2061

2062
            if ((*def)->dst == NULL)
M
Matthias Bolte 已提交
2063
                goto cleanup;
2064
        } else {
2065 2066 2067 2068
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported bus type '%s' for device type '%s'"),
                           virDomainDiskBusTypeToString(busType),
                           virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2069
            goto cleanup;
2070 2071
        }
    } else {
2072 2073 2074
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2075
        goto cleanup;
2076 2077
    }

2078 2079 2080 2081 2082 2083 2084
    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);
2085
    VMX_BUILD_NAME(mode);
2086 2087

    /* vmx:present */
2088
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
M
Matthias Bolte 已提交
2089
        goto cleanup;
2090 2091

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

    /* FIXME: Need to distiguish between active and inactive domains here */
2098
    if (! present/* && ! startConnected*/)
2099 2100 2101
        goto ignore;

    /* vmx:deviceType -> def:type */
2102
    if (virVMXGetConfigString(conf, deviceType_name, &deviceType, true) < 0)
M
Matthias Bolte 已提交
2103
        goto cleanup;
2104 2105

    /* vmx:clientDevice */
2106 2107
    if (virVMXGetConfigBoolean(conf, clientDevice_name, &clientDevice, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2108
        goto cleanup;
2109 2110
    }

2111
    /* vmx:mode -> def:transient */
2112
    if (virVMXGetConfigString(conf, mode_name, &mode, true) < 0)
2113 2114
        goto cleanup;

2115 2116 2117
    if (clientDevice) {
        /*
         * Just ignore devices in client mode, because I have no clue how to
M
Matthias Bolte 已提交
2118
         * handle them (e.g. assign an image) without the VI Client GUI.
2119 2120 2121 2122 2123
         */
        goto ignore;
    }

    /* vmx:fileType -> def:type */
2124
    if (virVMXGetConfigString(conf, fileType_name, &fileType, true) < 0)
M
Matthias Bolte 已提交
2125
        goto cleanup;
2126 2127

    /* vmx:fileName -> def:src, def:type */
2128
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0)
M
Matthias Bolte 已提交
2129
        goto cleanup;
2130 2131

    /* vmx:writeThrough -> def:cachemode */
2132 2133
    if (virVMXGetConfigBoolean(conf, writeThrough_name, &writeThrough, false,
                               true) < 0) {
M
Matthias Bolte 已提交
2134
        goto cleanup;
2135 2136 2137 2138
    }

    /* Setup virDomainDiskDef */
    if (device == VIR_DOMAIN_DISK_DEVICE_DISK) {
2139
        if (virFileHasSuffix(fileName, ".vmdk")) {
2140 2141
            char *tmp;

2142
            if (deviceType != NULL) {
2143
                if (busType == VIR_DOMAIN_DISK_BUS_SCSI &&
2144 2145
                    STRCASENEQ(deviceType, "scsi-hardDisk") &&
                    STRCASENEQ(deviceType, "disk")) {
2146 2147 2148 2149
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'scsi-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2150
                    goto cleanup;
2151
                } else if (busType == VIR_DOMAIN_DISK_BUS_IDE &&
2152 2153
                           STRCASENEQ(deviceType, "ata-hardDisk") &&
                           STRCASENEQ(deviceType, "disk")) {
2154 2155 2156 2157
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'ata-hardDisk' "
                                     "or 'disk' but found '%s'"), deviceType_name,
                                   deviceType);
M
Matthias Bolte 已提交
2158
                    goto cleanup;
2159 2160 2161
                }
            }

E
Eric Blake 已提交
2162
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
2163 2164 2165 2166 2167 2168 2169
            if (!(tmp = ctx->parseFileName(fileName, ctx->opaque)))
                goto cleanup;
            if (virDomainDiskSetSource(*def, tmp) < 0) {
                VIR_FREE(tmp);
                goto cleanup;
            }
            VIR_FREE(tmp);
2170 2171
            (*def)->cachemode = writeThrough ? VIR_DOMAIN_DISK_CACHE_WRITETHRU
                                             : VIR_DOMAIN_DISK_CACHE_DEFAULT;
2172 2173 2174
            if (mode)
                (*def)->transient = STRCASEEQ(mode,
                                              "independent-nonpersistent");
2175
        } else if (virFileHasSuffix(fileName, ".iso") ||
2176 2177
                   STRCASEEQ(deviceType, "atapi-cdrom") ||
                   STRCASEEQ(deviceType, "cdrom-raw")) {
2178 2179
            /*
             * This function was called in order to parse a harddisk device,
2180 2181 2182
             * but .iso files, 'atapi-cdrom', and 'cdrom-raw' devices are for
             * CDROM devices only. Just ignore it, another call to this
             * function to parse a CDROM device may handle it.
2183 2184 2185
             */
            goto ignore;
        } else {
2186
            virReportError(VIR_ERR_INTERNAL_ERROR,
2187 2188 2189 2190
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
                             fileName, fileName_name,
                             deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2191
            goto cleanup;
2192 2193
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
2194
        if (virFileHasSuffix(fileName, ".iso")) {
2195 2196
            char *tmp;

2197 2198
            if (deviceType != NULL) {
                if (STRCASENEQ(deviceType, "cdrom-image")) {
2199 2200 2201
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Expecting VMX entry '%s' to be 'cdrom-image' "
                                     "but found '%s'"), deviceType_name, deviceType);
M
Matthias Bolte 已提交
2202
                    goto cleanup;
2203 2204 2205
                }
            }

E
Eric Blake 已提交
2206
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
2207 2208 2209 2210
            if (!(tmp = ctx->parseFileName(fileName, ctx->opaque)))
                goto cleanup;
            if (virDomainDiskSetSource(*def, tmp) < 0) {
                VIR_FREE(tmp);
M
Matthias Bolte 已提交
2211
                goto cleanup;
M
Matthias Bolte 已提交
2212
            }
2213
            VIR_FREE(tmp);
2214
        } else if (virFileHasSuffix(fileName, ".vmdk")) {
2215 2216 2217 2218 2219 2220 2221
            /*
             * This function was called in order to parse a CDROM device, but
             * .vmdk files are for harddisk devices only. Just ignore it,
             * another call to this function to parse a harddisk device may
             * handle it.
             */
            goto ignore;
2222
        } else if (STRCASEEQ(deviceType, "atapi-cdrom")) {
E
Eric Blake 已提交
2223
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);
2224 2225

            if (STRCASEEQ(fileName, "auto detect")) {
2226
                ignore_value(virDomainDiskSetSource(*def, NULL));
2227
                (*def)->startupPolicy = VIR_DOMAIN_STARTUP_POLICY_OPTIONAL;
2228 2229
            } else if (virDomainDiskSetSource(*def, fileName) < 0) {
                goto cleanup;
2230
            }
2231 2232 2233
        } else if (STRCASEEQ(deviceType, "cdrom-raw")) {
            /* Raw access CD-ROMs actually are device='lun' */
            (*def)->device = VIR_DOMAIN_DISK_DEVICE_LUN;
E
Eric Blake 已提交
2234
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);
2235 2236

            if (STRCASEEQ(fileName, "auto detect")) {
2237
                ignore_value(virDomainDiskSetSource(*def, NULL));
2238
                (*def)->startupPolicy = VIR_DOMAIN_STARTUP_POLICY_OPTIONAL;
2239 2240
            } else if (virDomainDiskSetSource(*def, fileName) < 0) {
                goto cleanup;
2241
            }
2242
        } else {
2243
            virReportError(VIR_ERR_INTERNAL_ERROR,
2244 2245 2246 2247
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
                             fileName, fileName_name,
                             deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2248
            goto cleanup;
2249 2250
        }
    } else if (device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
2251
        if (fileType != NULL && STRCASEEQ(fileType, "device")) {
E
Eric Blake 已提交
2252
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_BLOCK);
2253 2254
            if (virDomainDiskSetSource(*def, fileName) < 0)
                goto cleanup;
2255
        } else if (fileType != NULL && STRCASEEQ(fileType, "file")) {
2256 2257
            char *tmp;

E
Eric Blake 已提交
2258
            virDomainDiskSetType(*def, VIR_STORAGE_TYPE_FILE);
2259 2260 2261 2262 2263 2264 2265
            if (!(tmp = ctx->parseFileName(fileName, ctx->opaque)))
                goto cleanup;
            if (virDomainDiskSetSource(*def, tmp) < 0) {
                VIR_FREE(tmp);
                goto cleanup;
            }
            VIR_FREE(tmp);
2266
        } else {
2267
            virReportError(VIR_ERR_INTERNAL_ERROR,
2268 2269 2270 2271
                           _("Invalid or not yet handled value '%s' "
                             "for VMX entry '%s' for device type '%s'"),
                             fileName, fileName_name,
                             deviceType ? deviceType : "unknown");
M
Matthias Bolte 已提交
2272
            goto cleanup;
2273 2274
        }
    } else {
2275 2276
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported device type '%s'"),
                       virDomainDiskDeviceTypeToString(device));
M
Matthias Bolte 已提交
2277
        goto cleanup;
2278 2279
    }

2280
    if (virDomainDiskDefAssignAddress(xmlopt, *def) < 0) {
2281
        virReportError(VIR_ERR_INTERNAL_ERROR,
2282 2283
                       _("Could not assign address to disk '%s'"),
                       virDomainDiskGetSource(*def));
2284 2285 2286
        goto cleanup;
    }

M
Matthias Bolte 已提交
2287 2288
    result = 0;

2289
 cleanup:
M
Matthias Bolte 已提交
2290 2291 2292 2293 2294
    if (result < 0) {
        virDomainDiskDefFree(*def);
        *def = NULL;
    }

2295 2296 2297 2298
    VIR_FREE(prefix);
    VIR_FREE(deviceType);
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2299
    VIR_FREE(mode);
2300 2301 2302

    return result;

2303
 ignore:
2304 2305 2306
    virDomainDiskDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2307 2308
    result = 0;

2309 2310 2311 2312 2313
    goto cleanup;
}



2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
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;
    }

2339
    if (VIR_ALLOC(*def) < 0)
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
        return -1;

    (*def)->type = VIR_DOMAIN_FS_TYPE_MOUNT;

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

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

    /* vmx:present */
2353
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
2354 2355 2356
        goto cleanup;

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

2360
    if (!(present && enabled))
2361 2362 2363
        goto ignore;

    /* vmx:hostPath */
2364
    if (virVMXGetConfigString(conf, hostPath_name, &hostPath, false) < 0)
2365 2366 2367 2368 2369 2370
        goto cleanup;

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

    /* vmx:guestName */
2371
    if (virVMXGetConfigString(conf, guestName_name, &guestName, false) < 0)
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
        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;

2387
 cleanup:
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
    if (result < 0) {
        virDomainFSDefFree(*def);
        *def = NULL;
    }

    VIR_FREE(hostPath);
    VIR_FREE(guestName);

    return result;

2398
 ignore:
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
    virDomainFSDefFree(*def);
    *def = NULL;

    result = 0;

    goto cleanup;
}



2409
int
2410
virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
2411
{
M
Matthias Bolte 已提交
2412
    int result = -1;
2413 2414 2415
    char prefix[48] = "";

    char present_name[48] = "";
2416
    bool present = false;
2417 2418

    char startConnected_name[48] = "";
2419
    bool startConnected = false;
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435

    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;

2436 2437 2438
    char features_name[48] = "";
    long long features = 0;

2439 2440 2441
    char vnet_name[48] = "";
    char *vnet = NULL;

M
Matthias Bolte 已提交
2442 2443 2444
    char networkName_name[48] = "";
    char *networkName = NULL;

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

    if (controller < 0 || controller > 3) {
2451 2452 2453
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Ethernet controller index %d out of [0..3] range"),
                       controller);
2454
        return -1;
2455 2456
    }

2457
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
2458
        return -1;
2459

2460
    snprintf(prefix, sizeof(prefix), "ethernet%d", controller);
2461

2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
    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);
2472 2473

    /* vmx:present */
2474
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
M
Matthias Bolte 已提交
2475
        goto cleanup;
2476 2477

    /* vmx:startConnected */
2478 2479
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2480
        goto cleanup;
2481 2482 2483
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
2484
    if (! present/* && ! startConnected*/)
2485 2486 2487
        goto ignore;

    /* vmx:connectionType -> def:type */
2488 2489
    if (virVMXGetConfigString(conf, connectionType_name, &connectionType,
                              true) < 0) {
M
Matthias Bolte 已提交
2490
        goto cleanup;
2491 2492 2493
    }

    /* vmx:addressType, vmx:generatedAddress, vmx:address -> def:mac */
2494 2495 2496 2497 2498
    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 已提交
2499
        goto cleanup;
2500 2501
    }

2502 2503
    if (addressType == NULL || STRCASEEQ(addressType, "generated") ||
        STRCASEEQ(addressType, "vpx")) {
2504
        if (generatedAddress != NULL) {
2505
            if (virMacAddrParse(generatedAddress, &(*def)->mac) < 0) {
2506 2507 2508 2509
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be MAC address but "
                                 "found '%s'"), generatedAddress_name,
                               generatedAddress);
M
Matthias Bolte 已提交
2510
                goto cleanup;
2511 2512 2513 2514
            }
        }
    } else if (STRCASEEQ(addressType, "static")) {
        if (address != NULL) {
2515
            if (virMacAddrParse(address, &(*def)->mac) < 0) {
2516 2517 2518
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Expecting VMX entry '%s' to be MAC address but "
                                 "found '%s'"), address_name, address);
M
Matthias Bolte 已提交
2519
                goto cleanup;
2520 2521 2522
            }
        }
    } else {
2523 2524 2525
        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 已提交
2526
        goto cleanup;
2527 2528
    }

2529
    /* vmx:virtualDev, vmx:features -> def:model */
2530 2531
    if (virVMXGetConfigString(conf, virtualDev_name, &virtualDev, true) < 0 ||
        virVMXGetConfigLong(conf, features_name, &features, 0, true) < 0) {
M
Matthias Bolte 已提交
2532
        goto cleanup;
2533 2534
    }

2535 2536 2537 2538
    if (virtualDev != NULL) {
        if (STRCASENEQ(virtualDev, "vlance") &&
            STRCASENEQ(virtualDev, "vmxnet") &&
            STRCASENEQ(virtualDev, "vmxnet3") &&
2539 2540
            STRCASENEQ(virtualDev, "e1000") &&
            STRCASENEQ(virtualDev, "e1000e")) {
2541 2542
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'vlance' or 'vmxnet' or "
2543
                             "'vmxnet3' or 'e1000e' or 'e1000e' but found '%s'"), virtualDev_name,
2544
                           virtualDev);
M
Matthias Bolte 已提交
2545
            goto cleanup;
2546 2547 2548 2549 2550
        }

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

2551
            if (VIR_STRDUP(virtualDev, "vmxnet2") < 0)
M
Matthias Bolte 已提交
2552
                goto cleanup;
2553
        }
2554 2555
    }

M
Matthias Bolte 已提交
2556
    /* vmx:networkName -> def:data.bridge.brname */
2557 2558 2559 2560 2561 2562 2563
    if (connectionType == NULL ||
        STRCASEEQ(connectionType, "bridged") ||
        STRCASEEQ(connectionType, "custom")) {
        if (virVMXGetConfigString(conf, networkName_name, &networkName,
                                  true) < 0)
            goto cleanup;

2564 2565
        if (!networkName && VIR_STRDUP(networkName, "") < 0)
            goto cleanup;
M
Matthias Bolte 已提交
2566 2567 2568
    }

    /* vmx:vnet -> def:data.ifname */
2569
    if (connectionType != NULL && STRCASEEQ(connectionType, "custom") &&
2570
        virVMXGetConfigString(conf, vnet_name, &vnet, false) < 0) {
M
Matthias Bolte 已提交
2571
        goto cleanup;
2572 2573 2574 2575 2576 2577
    }

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

        virtualDev = NULL;
M
Matthias Bolte 已提交
2581
        networkName = NULL;
2582 2583
    } else if (STRCASEEQ(connectionType, "hostonly")) {
        /* FIXME */
2584 2585 2586
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("No yet handled value '%s' for VMX entry '%s'"),
                       connectionType, connectionType_name);
M
Matthias Bolte 已提交
2587
        goto cleanup;
2588
    } else if (STRCASEEQ(connectionType, "nat")) {
2589 2590 2591 2592
        (*def)->type = VIR_DOMAIN_NET_TYPE_USER;
        (*def)->model = virtualDev;

        virtualDev = NULL;
2593 2594 2595
    } else if (STRCASEEQ(connectionType, "custom")) {
        (*def)->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
        (*def)->model = virtualDev;
M
Matthias Bolte 已提交
2596 2597
        (*def)->data.bridge.brname = networkName;
        (*def)->ifname = vnet;
2598 2599

        virtualDev = NULL;
M
Matthias Bolte 已提交
2600
        networkName = NULL;
2601 2602
        vnet = NULL;
    } else {
2603 2604 2605
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid value '%s' for VMX entry '%s'"), connectionType,
                       connectionType_name);
M
Matthias Bolte 已提交
2606
        goto cleanup;
2607 2608
    }

M
Matthias Bolte 已提交
2609 2610
    result = 0;

2611
 cleanup:
M
Matthias Bolte 已提交
2612 2613 2614 2615 2616
    if (result < 0) {
        virDomainNetDefFree(*def);
        *def = NULL;
    }

S
Stefan Berger 已提交
2617
    VIR_FREE(networkName);
2618 2619 2620 2621 2622 2623 2624 2625 2626
    VIR_FREE(connectionType);
    VIR_FREE(addressType);
    VIR_FREE(generatedAddress);
    VIR_FREE(address);
    VIR_FREE(virtualDev);
    VIR_FREE(vnet);

    return result;

2627
 ignore:
2628 2629 2630
    virDomainNetDefFree(*def);
    *def = NULL;

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

2633 2634 2635 2636 2637 2638
    goto cleanup;
}



int
2639 2640
virVMXParseSerial(virVMXContext *ctx, virConfPtr conf, int port,
                  virDomainChrDefPtr *def)
2641
{
M
Matthias Bolte 已提交
2642
    int result = -1;
2643 2644 2645
    char prefix[48] = "";

    char present_name[48] = "";
2646
    bool present = false;
2647 2648

    char startConnected_name[48] = "";
2649
    bool startConnected = false;
2650 2651 2652 2653 2654 2655 2656

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

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

2657 2658 2659
    char network_endPoint_name[48] = "";
    char *network_endPoint = NULL;

M
Martin Kletzander 已提交
2660
    virURIPtr parsedUri = NULL;
2661

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

    if (port < 0 || port > 3) {
2668 2669
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Serial port index %d out of [0..3] range"), port);
2670
        return -1;
2671 2672
    }

2673
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
2674
        return -1;
2675

2676
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
2677

2678
    snprintf(prefix, sizeof(prefix), "serial%d", port);
2679

2680 2681 2682 2683 2684
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
    VMX_BUILD_NAME_EXTRA(network_endPoint, "network.endPoint");
2685 2686

    /* vmx:present */
2687
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
M
Matthias Bolte 已提交
2688
        goto cleanup;
2689 2690

    /* vmx:startConnected */
2691 2692
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2693
        goto cleanup;
2694 2695 2696
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
2697
    if (! present/* && ! startConnected*/)
2698 2699 2700
        goto ignore;

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

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

2708
    /* vmx:network.endPoint -> def:data.tcp.listen */
2709 2710
    if (virVMXGetConfigString(conf, network_endPoint_name, &network_endPoint,
                              true) < 0) {
2711 2712 2713
        goto cleanup;
    }

G
Geoff Hickey 已提交
2714 2715 2716 2717 2718 2719
    /*
     * 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")) {
2720
        (*def)->target.port = port;
2721 2722
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2723 2724 2725

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2726
        (*def)->target.port = port;
2727 2728 2729
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2730

2731
        if ((*def)->source.data.file.path == NULL)
M
Matthias Bolte 已提交
2732
            goto cleanup;
2733
    } else if (STRCASEEQ(fileType, "pipe")) {
M
Matthias Bolte 已提交
2734 2735 2736 2737
        /*
         * FIXME: Differences between client/server and VM/application pipes
         *        not representable in domain XML form
         */
2738
        (*def)->target.port = port;
2739 2740
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_PIPE;
        (*def)->source.data.file.path = fileName;
M
Matthias Bolte 已提交
2741 2742

        fileName = NULL;
2743 2744
    } else if (STRCASEEQ(fileType, "network")) {
        (*def)->target.port = port;
2745
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_TCP;
2746

2747
        if (!(parsedUri = virURIParse(fileName)))
2748 2749 2750
            goto cleanup;

        if (parsedUri->port == 0) {
2751 2752 2753
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' doesn't contain a port part"),
                           fileName_name);
2754 2755 2756
            goto cleanup;
        }

2757
        if (VIR_STRDUP((*def)->source.data.tcp.host, parsedUri->server) < 0)
2758 2759
            goto cleanup;

2760
        if (virAsprintf(&(*def)->source.data.tcp.service, "%d",
2761
                        parsedUri->port) < 0)
2762 2763 2764 2765 2766 2767 2768
            goto cleanup;

        /* See vSphere API documentation about VirtualSerialPortURIBackingInfo */
        if (parsedUri->scheme == NULL ||
            STRCASEEQ(parsedUri->scheme, "tcp") ||
            STRCASEEQ(parsedUri->scheme, "tcp4") ||
            STRCASEEQ(parsedUri->scheme, "tcp6")) {
2769
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW;
2770
        } else if (STRCASEEQ(parsedUri->scheme, "telnet")) {
2771 2772
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
2773
        } else if (STRCASEEQ(parsedUri->scheme, "telnets")) {
2774 2775
            (*def)->source.data.tcp.protocol
                = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNETS;
2776 2777 2778 2779
        } else if (STRCASEEQ(parsedUri->scheme, "ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp4+ssl") ||
                   STRCASEEQ(parsedUri->scheme, "tcp6+ssl")) {
2780
            (*def)->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TLS;
2781
        } else {
2782 2783 2784
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("VMX entry '%s' contains unsupported scheme '%s'"),
                           fileName_name, parsedUri->scheme);
2785 2786 2787 2788
            goto cleanup;
        }

        if (network_endPoint == NULL || STRCASEEQ(network_endPoint, "server")) {
2789
            (*def)->source.data.tcp.listen = true;
2790
        } else if (STRCASEEQ(network_endPoint, "client")) {
2791
            (*def)->source.data.tcp.listen = false;
2792
        } else {
2793 2794 2795
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting VMX entry '%s' to be 'server' or 'client' "
                             "but found '%s'"), network_endPoint_name, network_endPoint);
2796 2797
            goto cleanup;
        }
2798
    } else {
2799 2800 2801
        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 已提交
2802
        goto cleanup;
2803 2804
    }

M
Matthias Bolte 已提交
2805 2806
    result = 0;

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

2813 2814
    VIR_FREE(fileType);
    VIR_FREE(fileName);
2815
    VIR_FREE(network_endPoint);
2816
    virURIFree(parsedUri);
2817 2818 2819

    return result;

2820
 ignore:
2821 2822 2823
    virDomainChrDefFree(*def);
    *def = NULL;

M
Matthias Bolte 已提交
2824 2825
    result = 0;

2826 2827 2828 2829 2830 2831
    goto cleanup;
}



int
2832 2833
virVMXParseParallel(virVMXContext *ctx, virConfPtr conf, int port,
                    virDomainChrDefPtr *def)
2834
{
M
Matthias Bolte 已提交
2835
    int result = -1;
2836 2837 2838
    char prefix[48] = "";

    char present_name[48] = "";
2839
    bool present = false;
2840 2841

    char startConnected_name[48] = "";
2842
    bool startConnected = false;
2843 2844 2845 2846 2847 2848 2849 2850

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

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

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

    if (port < 0 || port > 2) {
2856 2857
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Parallel port index %d out of [0..2] range"), port);
2858
        return -1;
2859 2860
    }

2861
    if (VIR_ALLOC(*def) < 0)
M
Matthias Bolte 已提交
2862
        return -1;
2863

2864
    (*def)->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
2865

2866
    snprintf(prefix, sizeof(prefix), "parallel%d", port);
2867

2868 2869 2870 2871
    VMX_BUILD_NAME(present);
    VMX_BUILD_NAME(startConnected);
    VMX_BUILD_NAME(fileType);
    VMX_BUILD_NAME(fileName);
2872 2873

    /* vmx:present */
2874
    if (virVMXGetConfigBoolean(conf, present_name, &present, false, true) < 0)
M
Matthias Bolte 已提交
2875
        goto cleanup;
2876 2877

    /* vmx:startConnected */
2878 2879
    if (virVMXGetConfigBoolean(conf, startConnected_name, &startConnected,
                               true, true) < 0) {
M
Matthias Bolte 已提交
2880
        goto cleanup;
2881 2882 2883
    }

    /* FIXME: Need to distiguish between active and inactive domains here */
2884
    if (! present/* && ! startConnected*/)
2885 2886 2887
        goto ignore;

    /* vmx:fileType -> def:type */
2888
    if (virVMXGetConfigString(conf, fileType_name, &fileType, false) < 0)
M
Matthias Bolte 已提交
2889
        goto cleanup;
2890 2891

    /* vmx:fileName -> def:data.file.path */
2892
    if (virVMXGetConfigString(conf, fileName_name, &fileName, false) < 0)
M
Matthias Bolte 已提交
2893
        goto cleanup;
2894 2895 2896

    /* Setup virDomainChrDef */
    if (STRCASEEQ(fileType, "device")) {
2897
        (*def)->target.port = port;
2898 2899
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
        (*def)->source.data.file.path = fileName;
2900 2901 2902

        fileName = NULL;
    } else if (STRCASEEQ(fileType, "file")) {
2903
        (*def)->target.port = port;
2904 2905 2906
        (*def)->source.type = VIR_DOMAIN_CHR_TYPE_FILE;
        (*def)->source.data.file.path = ctx->parseFileName(fileName,
                                                           ctx->opaque);
2907

2908
        if ((*def)->source.data.file.path == NULL)
M
Matthias Bolte 已提交
2909
            goto cleanup;
2910
    } else {
2911 2912 2913
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting VMX entry '%s' to be 'device' or 'file' but "
                         "found '%s'"), fileType_name, fileType);
M
Matthias Bolte 已提交
2914
        goto cleanup;
2915 2916
    }

M
Matthias Bolte 已提交
2917 2918
    result = 0;

2919
 cleanup:
M
Matthias Bolte 已提交
2920 2921 2922 2923 2924
    if (result < 0) {
        virDomainChrDefFree(*def);
        *def = NULL;
    }

2925 2926 2927 2928 2929
    VIR_FREE(fileType);
    VIR_FREE(fileName);

    return result;

2930
 ignore:
2931 2932 2933
    virDomainChrDefFree(*def);
    *def = NULL;

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

2936 2937
    goto cleanup;
}
M
Matthias Bolte 已提交
2938 2939 2940



2941 2942 2943 2944 2945 2946 2947
int
virVMXParseSVGA(virConfPtr conf, virDomainVideoDefPtr *def)
{
    int result = -1;
    long long svga_vramSize = 0;

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

2952
    if (VIR_ALLOC(*def) < 0)
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962
        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;
    }

2963
    (*def)->vram = VIR_DIV_UP(svga_vramSize, 1024); /* Scale from bytes to kilobytes */
2964 2965 2966

    result = 0;

2967
 cleanup:
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977
    if (result < 0) {
        virDomainVideoDefFree(*def);
        *def = NULL;
    }

    return result;
}



M
Matthias Bolte 已提交
2978 2979 2980 2981 2982
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Domain XML -> VMX
 */

char *
2983
virVMXFormatConfig(virVMXContext *ctx, virDomainXMLOptionPtr xmlopt, virDomainDefPtr def,
2984
                   int virtualHW_version)
M
Matthias Bolte 已提交
2985
{
2986
    char *vmx = NULL;
2987
    size_t i;
M
Matthias Bolte 已提交
2988 2989 2990
    int sched_cpu_affinity_length;
    unsigned char zero[VIR_UUID_BUFLEN];
    virBuffer buffer = VIR_BUFFER_INITIALIZER;
2991 2992
    char *preliminaryDisplayName = NULL;
    char *displayName = NULL;
2993
    char *annotation = NULL;
2994
    unsigned long long max_balloon;
2995 2996
    bool scsi_present[4] = { false, false, false, false };
    int scsi_virtualDev[4] = { -1, -1, -1, -1 };
2997
    bool floppy_present[2] = { false, false };
M
Matthias Bolte 已提交
2998

2999
    if (ctx->formatFileName == NULL) {
3000 3001
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("virVMXContext has no formatFileName function set"));
3002 3003 3004
        return NULL;
    }

M
Matthias Bolte 已提交
3005 3006
    memset(zero, 0, VIR_UUID_BUFLEN);

3007
    if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) {
3008 3009 3010 3011
        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 已提交
3012 3013 3014
        return NULL;
    }

3015 3016 3017
    /* vmx:.encoding */
    virBufferAddLit(&buffer, ".encoding = \"UTF-8\"\n");

M
Matthias Bolte 已提交
3018 3019 3020 3021
    /* vmx:config.version */
    virBufferAddLit(&buffer, "config.version = \"8\"\n");

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

M
Matthias Bolte 已提交
3025
    /* def:os.arch -> vmx:guestOS */
3026
    if (def->os.arch == VIR_ARCH_I686) {
M
Matthias Bolte 已提交
3027
        virBufferAddLit(&buffer, "guestOS = \"other\"\n");
3028
    } else if (def->os.arch == VIR_ARCH_X86_64) {
M
Matthias Bolte 已提交
3029 3030
        virBufferAddLit(&buffer, "guestOS = \"other-64\"\n");
    } else {
3031 3032
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting domain XML attribute 'arch' of entry 'os/type' "
3033 3034
                         "to be 'i686' or 'x86_64' but found '%s'"),
                       virArchToString(def->os.arch));
3035
        goto cleanup;
M
Matthias Bolte 已提交
3036 3037
    }

M
Matthias Bolte 已提交
3038 3039 3040 3041 3042 3043 3044
    /* 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 {
3045 3046 3047
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported SMBIOS mode '%s'"),
                       virDomainSmbiosModeTypeToString(def->os.smbios_mode));
M
Matthias Bolte 已提交
3048 3049 3050
        goto cleanup;
    }

M
Matthias Bolte 已提交
3051 3052 3053 3054
    /* def:uuid -> vmx:uuid.action, vmx:uuid.bios */
    if (memcmp(def->uuid, zero, VIR_UUID_BUFLEN) == 0) {
        virBufferAddLit(&buffer, "uuid.action = \"create\"\n");
    } else {
3055
        virBufferAsprintf(&buffer, "uuid.bios = \"%02x %02x %02x %02x %02x %02x "
M
Matthias Bolte 已提交
3056 3057 3058 3059 3060 3061 3062 3063 3064
                          "%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 */
3065
    preliminaryDisplayName = virVMXEscapeHexPipe(def->name);
3066

3067
    if (preliminaryDisplayName == NULL)
3068
        goto cleanup;
3069

3070
    displayName = virVMXEscapeHexPercent(preliminaryDisplayName);
3071

3072
    if (displayName == NULL)
3073
        goto cleanup;
3074

3075
    virBufferAsprintf(&buffer, "displayName = \"%s\"\n", displayName);
3076

3077 3078
    /* def:description -> vmx:annotation */
    if (def->description != NULL) {
3079 3080
        if (!(annotation = virVMXEscapeHexPipe(def->description)))
            goto cleanup;
3081

3082
        virBufferAsprintf(&buffer, "annotation = \"%s\"\n", annotation);
3083 3084
    }

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

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

3092
    /* def:mem.cur_balloon -> vmx:sched.mem.max */
3093
    if (def->mem.cur_balloon < max_balloon) {
3094
        virBufferAsprintf(&buffer, "sched.mem.max = \"%llu\"\n",
3095 3096
                          VIR_DIV_UP(def->mem.cur_balloon,
                                     1024)); /* Scale from kilobytes to megabytes */
M
Matthias Bolte 已提交
3097 3098
    }

3099 3100
    /* def:mem.min_guarantee -> vmx:sched.mem.minsize */
    if (def->mem.min_guarantee > 0) {
3101
        virBufferAsprintf(&buffer, "sched.mem.minsize = \"%llu\"\n",
3102 3103
                          VIR_DIV_UP(def->mem.min_guarantee,
                                     1024)); /* Scale from kilobytes to megabytes */
3104 3105
    }

E
Eric Blake 已提交
3106 3107
    /* def:maxvcpus -> vmx:numvcpus */
    if (def->vcpus != def->maxvcpus) {
3108 3109 3110
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("No support for domain XML entry 'vcpu' attribute "
                         "'current'"));
E
Eric Blake 已提交
3111 3112 3113
        goto cleanup;
    }
    if (def->maxvcpus <= 0 || (def->maxvcpus % 2 != 0 && def->maxvcpus != 1)) {
3114 3115 3116 3117
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Expecting domain XML entry 'vcpu' to be an unsigned "
                         "integer (1 or a multiple of 2) but found %d"),
                       def->maxvcpus);
3118
        goto cleanup;
M
Matthias Bolte 已提交
3119 3120
    }

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

    /* def:cpumask -> vmx:sched.cpu.affinity */
H
Hu Tao 已提交
3124
    if (def->cpumask && virBitmapSize(def->cpumask) > 0) {
3125
        int bit;
M
Matthias Bolte 已提交
3126 3127 3128 3129
        virBufferAddLit(&buffer, "sched.cpu.affinity = \"");

        sched_cpu_affinity_length = 0;

3130
        bit = -1;
3131
        while ((bit = virBitmapNextSetBit(def->cpumask, bit)) >= 0)
H
Hu Tao 已提交
3132
            ++sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3133

E
Eric Blake 已提交
3134
        if (sched_cpu_affinity_length < def->maxvcpus) {
3135 3136 3137 3138
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML attribute 'cpuset' of entry "
                             "'vcpu' to contain at least %d CPU(s)"),
                           def->maxvcpus);
3139
            goto cleanup;
M
Matthias Bolte 已提交
3140 3141
        }

3142 3143 3144
        bit = -1;
        while ((bit = virBitmapNextSetBit(def->cpumask, bit)) >= 0) {
            virBufferAsprintf(&buffer, "%d", bit);
M
Matthias Bolte 已提交
3145

3146
            if (sched_cpu_affinity_length > 1)
H
Hu Tao 已提交
3147 3148 3149
                virBufferAddChar(&buffer, ',');

            --sched_cpu_affinity_length;
M
Matthias Bolte 已提交
3150 3151 3152 3153 3154
        }

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

3155
    /* def:cputune.shares -> vmx:sched.cpu.shares */
3156
    if (def->cputune.sharesSpecified) {
3157 3158 3159 3160 3161 3162 3163 3164
        /* See http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.SharesInfo.Level.html */
        if (def->cputune.shares == def->vcpus * 500) {
            virBufferAddLit(&buffer, "sched.cpu.shares = \"low\"\n");
        } else if (def->cputune.shares == def->vcpus * 1000) {
            virBufferAddLit(&buffer, "sched.cpu.shares = \"normal\"\n");
        } else if (def->cputune.shares == def->vcpus * 2000) {
            virBufferAddLit(&buffer, "sched.cpu.shares = \"high\"\n");
        } else {
3165
            virBufferAsprintf(&buffer, "sched.cpu.shares = \"%lu\"\n",
3166 3167 3168 3169
                              def->cputune.shares);
        }
    }

M
Matthias Bolte 已提交
3170 3171 3172 3173
    /* def:graphics */
    for (i = 0; i < def->ngraphics; ++i) {
        switch (def->graphics[i]->type) {
          case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
3174
            if (virVMXFormatVNC(def->graphics[i], &buffer) < 0)
3175
                goto cleanup;
M
Matthias Bolte 已提交
3176 3177 3178 3179

            break;

          default:
3180 3181 3182
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported graphics type '%s'"),
                           virDomainGraphicsTypeToString(def->graphics[i]->type));
3183
            goto cleanup;
M
Matthias Bolte 已提交
3184 3185 3186
        }
    }

M
Matthias Bolte 已提交
3187
    /* def:disks */
3188
    for (i = 0; i < def->ndisks; ++i) {
3189
        if (virVMXVerifyDiskAddress(xmlopt, def->disks[i]) < 0 ||
3190
            virVMXHandleLegacySCSIDiskDriverName(def, def->disks[i]) < 0) {
3191
            goto cleanup;
3192 3193
        }
    }
M
Matthias Bolte 已提交
3194

3195 3196
    if (virVMXGatherSCSIControllers(ctx, def, scsi_virtualDev,
                                    scsi_present) < 0) {
3197
        goto cleanup;
M
Matthias Bolte 已提交
3198 3199 3200 3201
    }

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

3204
            if (scsi_virtualDev[i] != -1) {
3205
                virBufferAsprintf(&buffer, "scsi%zu.virtualDev = \"%s\"\n", i,
3206
                                  virVMXControllerModelSCSITypeToString
3207
                                    (scsi_virtualDev[i]));
M
Matthias Bolte 已提交
3208 3209 3210 3211 3212 3213 3214 3215
            }
        }
    }

    for (i = 0; i < def->ndisks; ++i) {
        switch (def->disks[i]->device) {
          case VIR_DOMAIN_DISK_DEVICE_DISK:
          case VIR_DOMAIN_DISK_DEVICE_CDROM:
3216
          case VIR_DOMAIN_DISK_DEVICE_LUN:
3217
            if (virVMXFormatDisk(ctx, def->disks[i], &buffer) < 0)
3218
                goto cleanup;
M
Matthias Bolte 已提交
3219 3220 3221 3222

            break;

          case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
3223 3224
            if (virVMXFormatFloppy(ctx, def->disks[i], &buffer,
                                   floppy_present) < 0) {
3225
                goto cleanup;
M
Matthias Bolte 已提交
3226 3227 3228 3229 3230
            }

            break;

          default:
3231 3232 3233
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported disk device type '%s'"),
                           virDomainDiskDeviceTypeToString(def->disks[i]->device));
3234
            goto cleanup;
M
Matthias Bolte 已提交
3235 3236 3237
        }
    }

3238 3239
    for (i = 0; i < 2; ++i) {
        /* floppy[0..1].present defaults to true, disable it explicitly */
3240
        if (! floppy_present[i])
3241
            virBufferAsprintf(&buffer, "floppy%zu.present = \"false\"\n", i);
3242 3243
    }

M
Matthias Bolte 已提交
3244
    /* def:fss */
3245 3246
    if (def->nfss > 0) {
        virBufferAddLit(&buffer, "isolation.tools.hgfs.disable = \"false\"\n");
3247
        virBufferAsprintf(&buffer, "sharedFolder.maxNum = \"%zu\"\n", def->nfss);
3248 3249 3250
    }

    for (i = 0; i < def->nfss; ++i) {
3251
        if (virVMXFormatFileSystem(def->fss[i], i, &buffer) < 0)
3252 3253
            goto cleanup;
    }
M
Matthias Bolte 已提交
3254 3255 3256

    /* def:nets */
    for (i = 0; i < def->nnets; ++i) {
3257
        if (virVMXFormatEthernet(def->nets[i], i, &buffer) < 0)
3258
            goto cleanup;
M
Matthias Bolte 已提交
3259 3260 3261 3262 3263 3264 3265 3266
    }

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

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

3267 3268 3269
    /* def:videos */
    if (def->nvideos > 0) {
        if (def->nvideos > 1) {
3270 3271
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("No support for multiple video devices"));
3272 3273 3274
            goto cleanup;
        }

3275
        if (virVMXFormatSVGA(def->videos[0], &buffer) < 0)
3276 3277 3278
            goto cleanup;
    }

M
Matthias Bolte 已提交
3279 3280 3281 3282 3283
    /* def:hostdevs */
    /* FIXME */

    /* def:serials */
    for (i = 0; i < def->nserials; ++i) {
3284
        if (virVMXFormatSerial(ctx, def->serials[i], &buffer) < 0)
3285
            goto cleanup;
M
Matthias Bolte 已提交
3286 3287 3288 3289
    }

    /* def:parallels */
    for (i = 0; i < def->nparallels; ++i) {
3290
        if (virVMXFormatParallel(ctx, def->parallels[i], &buffer) < 0)
3291
            goto cleanup;
M
Matthias Bolte 已提交
3292 3293 3294
    }

    /* Get final VMX output */
3295
    if (virBufferCheckError(&buffer) < 0)
3296
        goto cleanup;
M
Matthias Bolte 已提交
3297

3298
    vmx = virBufferContentAndReset(&buffer);
M
Matthias Bolte 已提交
3299

3300
 cleanup:
3301
    if (vmx == NULL)
3302 3303
        virBufferFreeAndReset(&buffer);

3304 3305
    VIR_FREE(preliminaryDisplayName);
    VIR_FREE(displayName);
3306
    VIR_FREE(annotation);
M
Matthias Bolte 已提交
3307

3308
    return vmx;
M
Matthias Bolte 已提交
3309 3310 3311 3312
}



M
Matthias Bolte 已提交
3313
int
3314
virVMXFormatVNC(virDomainGraphicsDefPtr def, virBufferPtr buffer)
M
Matthias Bolte 已提交
3315
{
3316 3317
    const char *listenAddr;

M
Matthias Bolte 已提交
3318
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
3319
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3320 3321 3322
        return -1;
    }

3323
    virBufferAddLit(buffer, "RemoteDisplay.vnc.enabled = \"true\"\n");
M
Matthias Bolte 已提交
3324 3325

    if (def->data.vnc.autoport) {
3326
        VIR_WARN("VNC autoport is enabled, but the automatically assigned "
M
Matthias Bolte 已提交
3327 3328 3329 3330 3331 3332 3333
                  "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);
        }

3334
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.port = \"%d\"\n",
M
Matthias Bolte 已提交
3335 3336 3337
                          def->data.vnc.port);
    }

3338
    if ((listenAddr = virDomainGraphicsListenGetAddress(def, 0))) {
3339
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.ip = \"%s\"\n",
3340
                          listenAddr);
M
Matthias Bolte 已提交
3341 3342 3343
    }

    if (def->data.vnc.keymap != NULL) {
3344
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.keymap = \"%s\"\n",
M
Matthias Bolte 已提交
3345 3346 3347
                          def->data.vnc.keymap);
    }

3348
    if (def->data.vnc.auth.passwd != NULL) {
3349
        virBufferAsprintf(buffer, "RemoteDisplay.vnc.password = \"%s\"\n",
3350
                          def->data.vnc.auth.passwd);
M
Matthias Bolte 已提交
3351 3352 3353 3354 3355
    }

    return 0;
}

M
Matthias Bolte 已提交
3356
int
3357
virVMXFormatDisk(virVMXContext *ctx, virDomainDiskDefPtr def,
3358
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3359
{
3360
    int controllerOrBus, unit;
3361
    const char *vmxDeviceType = NULL;
M
Matthias Bolte 已提交
3362
    char *fileName = NULL;
3363
    int type = virDomainDiskGetType(def);
M
Matthias Bolte 已提交
3364

3365 3366 3367
    /* Convert a handful of types to their string values */
    const char *busType = virDomainDiskBusTypeToString(def->bus);
    const char *deviceType = virDomainDeviceTypeToString(def->device);
3368
    const char *diskType = virDomainDeviceTypeToString(type);
3369 3370 3371 3372 3373 3374 3375 3376 3377

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

    /* Check that we got a valid device type */
    if (def->device != VIR_DOMAIN_DISK_DEVICE_DISK &&
3378 3379
        def->device != VIR_DOMAIN_DISK_DEVICE_CDROM &&
        def->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
3380 3381
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid device type supplied: %s"), deviceType);
M
Matthias Bolte 已提交
3382 3383 3384
        return -1;
    }

3385
    /* We only support type='file' and type='block' */
E
Eric Blake 已提交
3386 3387
    if (type != VIR_STORAGE_TYPE_FILE &&
        type != VIR_STORAGE_TYPE_BLOCK) {
3388 3389 3390 3391
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("%s %s '%s' has unsupported type '%s', expecting "
                         "'%s' or '%s'"), busType, deviceType, def->dst,
                       diskType,
E
Eric Blake 已提交
3392 3393
                       virStorageTypeToString(VIR_STORAGE_TYPE_FILE),
                       virStorageTypeToString(VIR_STORAGE_TYPE_BLOCK));
3394 3395
        return -1;
    }
M
Matthias Bolte 已提交
3396

3397
    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
3398 3399
        if (virVMXSCSIDiskNameToControllerAndUnit(def->dst, &controllerOrBus,
                                                  &unit) < 0) {
M
Matthias Bolte 已提交
3400 3401 3402
            return -1;
        }
    } else if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
3403
        if (virVMXIDEDiskNameToBusAndUnit(def->dst, &controllerOrBus,
3404
                                          &unit) < 0) {
M
Matthias Bolte 已提交
3405 3406 3407
            return -1;
        }
    } else {
3408
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3409 3410
                       _("Unsupported bus type '%s' for %s"),
                       busType, deviceType);
M
Matthias Bolte 已提交
3411 3412 3413
        return -1;
    }

3414
    if (def->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
E
Eric Blake 已提交
3415
        type == VIR_STORAGE_TYPE_FILE) {
3416 3417 3418
        vmxDeviceType = (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) ?
            "scsi-hardDisk" : "ata-hardDisk";
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
E
Eric Blake 已提交
3419
        if (type == VIR_STORAGE_TYPE_FILE)
3420 3421 3422
            vmxDeviceType = "cdrom-image";
        else
            vmxDeviceType = "atapi-cdrom";
3423 3424
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
        vmxDeviceType = "cdrom-raw";
3425
    } else {
3426
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3427 3428
                       _("%s %s '%s' has an unsupported type '%s'"),
                       busType, deviceType, def->dst, diskType);
M
Matthias Bolte 已提交
3429 3430 3431
        return -1;
    }

3432
    virBufferAsprintf(buffer, "%s%d:%d.present = \"true\"\n",
3433 3434 3435
                      busType, controllerOrBus, unit);
    virBufferAsprintf(buffer, "%s%d:%d.deviceType = \"%s\"\n",
                      busType, controllerOrBus, unit, vmxDeviceType);
M
Matthias Bolte 已提交
3436

E
Eric Blake 已提交
3437
    if (type == VIR_STORAGE_TYPE_FILE) {
3438 3439 3440
        const char *src = virDomainDiskGetSource(def);

        if (src && ! virFileHasSuffix(src, fileExt)) {
3441
            virReportError(VIR_ERR_INTERNAL_ERROR,
3442 3443 3444 3445
                           _("Image file for %s %s '%s' has "
                             "unsupported suffix, expecting '%s'"),
                           busType, deviceType, def->dst, fileExt);
                return -1;
M
Matthias Bolte 已提交
3446 3447
        }

3448
        fileName = ctx->formatFileName(src, ctx->opaque);
M
Matthias Bolte 已提交
3449

3450
        if (fileName == NULL)
M
Matthias Bolte 已提交
3451 3452
            return -1;

3453
        virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3454
                          busType, controllerOrBus, unit, fileName);
M
Matthias Bolte 已提交
3455 3456

        VIR_FREE(fileName);
E
Eric Blake 已提交
3457
    } else if (type == VIR_STORAGE_TYPE_BLOCK) {
3458 3459 3460
        const char *src = virDomainDiskGetSource(def);

        if (!src &&
3461 3462 3463 3464 3465 3466
            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 {
3467
            virBufferAsprintf(buffer, "%s%d:%d.fileName = \"%s\"\n",
3468
                              busType, controllerOrBus, unit, src);
3469
        }
M
Matthias Bolte 已提交
3470 3471 3472 3473
    }

    if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
        if (def->cachemode == VIR_DOMAIN_DISK_CACHE_WRITETHRU) {
3474
            virBufferAsprintf(buffer, "%s%d:%d.writeThrough = \"true\"\n",
3475
                              busType, controllerOrBus, unit);
M
Matthias Bolte 已提交
3476
        } else if (def->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
3477 3478
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("%s harddisk '%s' has unsupported cache mode '%s'"),
3479
                           busType, def->dst,
3480
                           virDomainDiskCacheTypeToString(def->cachemode));
M
Matthias Bolte 已提交
3481
            return -1;
M
Matthias Bolte 已提交
3482 3483 3484
        }
    }

3485 3486 3487 3488
    if (def->transient)
        virBufferAsprintf(buffer,
                          "%s%d:%d.mode = \"independent-nonpersistent\"\n",
                          busType, controllerOrBus, unit);
M
Matthias Bolte 已提交
3489 3490 3491 3492
    return 0;
}

int
3493 3494
virVMXFormatFloppy(virVMXContext *ctx, virDomainDiskDefPtr def,
                   virBufferPtr buffer, bool floppy_present[2])
M
Matthias Bolte 已提交
3495
{
3496
    int unit;
M
Matthias Bolte 已提交
3497
    char *fileName = NULL;
3498 3499
    int type = virDomainDiskGetType(def);
    const char *src = virDomainDiskGetSource(def);
M
Matthias Bolte 已提交
3500 3501

    if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
3502
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
M
Matthias Bolte 已提交
3503 3504 3505
        return -1;
    }

3506
    if (virVMXFloppyDiskNameToUnit(def->dst, &unit) < 0)
M
Matthias Bolte 已提交
3507 3508
        return -1;

3509 3510
    floppy_present[unit] = true;

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

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

3516 3517
        if (src) {
            fileName = ctx->formatFileName(src, ctx->opaque);
M
Matthias Bolte 已提交
3518

3519
            if (fileName == NULL)
M
Matthias Bolte 已提交
3520 3521
                return -1;

3522
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3523
                              unit, fileName);
M
Matthias Bolte 已提交
3524 3525 3526

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

3530
        if (src) {
3531
            virBufferAsprintf(buffer, "floppy%d.fileName = \"%s\"\n",
3532
                              unit, src);
M
Matthias Bolte 已提交
3533
        }
M
Matthias Bolte 已提交
3534
    } else {
3535 3536 3537
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Floppy '%s' has unsupported type '%s', expecting '%s' "
                         "or '%s'"), def->dst,
E
Eric Blake 已提交
3538 3539 3540
                       virStorageTypeToString(type),
                       virStorageTypeToString(VIR_STORAGE_TYPE_FILE),
                       virStorageTypeToString(VIR_STORAGE_TYPE_BLOCK));
M
Matthias Bolte 已提交
3541 3542 3543 3544 3545 3546 3547 3548
        return -1;
    }

    return 0;
}



3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
int
virVMXFormatFileSystem(virDomainFSDefPtr def, int number, virBufferPtr buffer)
{
    if (def->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Only '%s' filesystem type is supported"),
                       virDomainFSTypeToString(VIR_DOMAIN_FS_TYPE_MOUNT));
        return -1;
    }

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

    return 0;
}



M
Matthias Bolte 已提交
3574
int
3575 3576
virVMXFormatEthernet(virDomainNetDefPtr def, int controller,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3577 3578
{
    char mac_string[VIR_MAC_STRING_BUFLEN];
3579
    unsigned int prefix, suffix;
M
Matthias Bolte 已提交
3580 3581

    if (controller < 0 || controller > 3) {
3582 3583 3584
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Ethernet controller index %d out of [0..3] range"),
                       controller);
M
Matthias Bolte 已提交
3585 3586 3587
        return -1;
    }

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

3590
    /* def:model -> vmx:virtualDev, vmx:features */
M
Matthias Bolte 已提交
3591 3592 3593
    if (def->model != NULL) {
        if (STRCASENEQ(def->model, "vlance") &&
            STRCASENEQ(def->model, "vmxnet") &&
3594
            STRCASENEQ(def->model, "vmxnet2") &&
3595
            STRCASENEQ(def->model, "vmxnet3") &&
3596 3597
            STRCASENEQ(def->model, "e1000") &&
            STRCASENEQ(def->model, "e1000e")) {
3598 3599 3600
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Expecting domain XML entry 'devices/interface/model' "
                             "to be 'vlance' or 'vmxnet' or 'vmxnet2' or 'vmxnet3' "
3601
                             "or 'e1000' or 'e1000e' but found '%s'"), def->model);
M
Matthias Bolte 已提交
3602 3603 3604
            return -1;
        }

3605
        if (STRCASEEQ(def->model, "vmxnet2")) {
3606
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"vmxnet\"\n",
3607
                              controller);
3608
            virBufferAsprintf(buffer, "ethernet%d.features = \"15\"\n",
3609 3610
                              controller);
        } else {
3611
            virBufferAsprintf(buffer, "ethernet%d.virtualDev = \"%s\"\n",
3612 3613
                              controller, def->model);
        }
M
Matthias Bolte 已提交
3614 3615 3616 3617 3618
    }

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

        if (def->ifname != NULL) {
3624
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"custom\"\n",
M
Matthias Bolte 已提交
3625
                              controller);
3626
            virBufferAsprintf(buffer, "ethernet%d.vnet = \"%s\"\n",
M
Matthias Bolte 已提交
3627 3628
                              controller, def->ifname);
        } else {
3629
            virBufferAsprintf(buffer, "ethernet%d.connectionType = \"bridged\"\n",
M
Matthias Bolte 已提交
3630 3631 3632 3633 3634
                              controller);
        }

        break;

3635 3636 3637 3638 3639
      case VIR_DOMAIN_NET_TYPE_USER:
        virBufferAsprintf(buffer, "ethernet%d.connectionType = \"nat\"\n",
                          controller);
        break;

M
Matthias Bolte 已提交
3640
      default:
3641 3642
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported net type '%s'"),
                       virDomainNetTypeToString(def->type));
M
Matthias Bolte 已提交
3643 3644 3645
        return -1;
    }

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

3649 3650
    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];
3651 3652

    if (prefix == 0x000c29) {
3653
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"generated\"\n",
M
Matthias Bolte 已提交
3654
                          controller);
3655
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
M
Matthias Bolte 已提交
3656
                          controller, mac_string);
3657
        virBufferAsprintf(buffer, "ethernet%d.generatedAddressOffset = \"0\"\n",
M
Matthias Bolte 已提交
3658
                          controller);
3659
    } else if (prefix == 0x005056 && suffix <= 0x3fffff) {
3660
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3661
                          controller);
3662
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3663 3664
                          controller, mac_string);
    } else if (prefix == 0x005056 && suffix >= 0x800000 && suffix <= 0xbfffff) {
3665
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"vpx\"\n",
3666
                          controller);
3667
        virBufferAsprintf(buffer, "ethernet%d.generatedAddress = \"%s\"\n",
3668
                          controller, mac_string);
M
Matthias Bolte 已提交
3669
    } else {
3670
        virBufferAsprintf(buffer, "ethernet%d.addressType = \"static\"\n",
3671
                          controller);
3672
        virBufferAsprintf(buffer, "ethernet%d.address = \"%s\"\n",
3673
                          controller, mac_string);
3674
        virBufferAsprintf(buffer, "ethernet%d.checkMACAddress = \"false\"\n",
3675
                          controller);
M
Matthias Bolte 已提交
3676 3677 3678 3679 3680 3681 3682 3683
    }

    return 0;
}



int
3684 3685
virVMXFormatSerial(virVMXContext *ctx, virDomainChrDefPtr def,
                   virBufferPtr buffer)
M
Matthias Bolte 已提交
3686
{
M
Matthias Bolte 已提交
3687
    char *fileName = NULL;
3688
    const char *protocol;
M
Matthias Bolte 已提交
3689

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

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

M
Matthias Bolte 已提交
3699
    /* def:type -> vmx:fileType and def:data.file.path -> vmx:fileName */
3700
    switch (def->source.type) {
M
Matthias Bolte 已提交
3701
      case VIR_DOMAIN_CHR_TYPE_DEV:
3702
        virBufferAsprintf(buffer, "serial%d.fileType = \"device\"\n",
3703
                          def->target.port);
3704
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3705
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3706 3707 3708
        break;

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

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

3714
        if (fileName == NULL)
M
Matthias Bolte 已提交
3715 3716
            return -1;

3717
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3718
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3719 3720

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3721 3722 3723
        break;

      case VIR_DOMAIN_CHR_TYPE_PIPE:
3724
        virBufferAsprintf(buffer, "serial%d.fileType = \"pipe\"\n",
3725
                          def->target.port);
M
Matthias Bolte 已提交
3726
        /* FIXME: Based on VI Client GUI default */
3727
        virBufferAsprintf(buffer, "serial%d.pipe.endPoint = \"client\"\n",
3728
                          def->target.port);
M
Matthias Bolte 已提交
3729
        /* FIXME: Based on VI Client GUI default */
3730
        virBufferAsprintf(buffer, "serial%d.tryNoRxLoss = \"false\"\n",
3731
                          def->target.port);
3732
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s\"\n",
3733
                          def->target.port, def->source.data.file.path);
M
Matthias Bolte 已提交
3734 3735
        break;

3736
      case VIR_DOMAIN_CHR_TYPE_TCP:
3737
        switch (def->source.data.tcp.protocol) {
3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754
          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:
3755 3756 3757 3758
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Unsupported character device TCP protocol '%s'"),
                           virDomainChrTcpProtocolTypeToString(
                               def->source.data.tcp.protocol));
3759 3760 3761
            return -1;
        }

3762
        virBufferAsprintf(buffer, "serial%d.fileType = \"network\"\n",
3763
                          def->target.port);
3764
        virBufferAsprintf(buffer, "serial%d.fileName = \"%s://%s:%s\"\n",
3765 3766
                          def->target.port, protocol, def->source.data.tcp.host,
                          def->source.data.tcp.service);
3767
        virBufferAsprintf(buffer, "serial%d.network.endPoint = \"%s\"\n",
3768
                          def->target.port,
3769
                          def->source.data.tcp.listen ? "server" : "client");
3770 3771
        break;

M
Matthias Bolte 已提交
3772
      default:
3773 3774 3775
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
                       virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3776 3777 3778 3779 3780
        return -1;
    }

    /* vmx:yieldOnMsrRead */
    /* FIXME: Based on VI Client GUI default */
3781
    virBufferAsprintf(buffer, "serial%d.yieldOnMsrRead = \"true\"\n",
3782
                      def->target.port);
M
Matthias Bolte 已提交
3783 3784 3785 3786 3787 3788 3789

    return 0;
}



int
3790 3791
virVMXFormatParallel(virVMXContext *ctx, virDomainChrDefPtr def,
                     virBufferPtr buffer)
M
Matthias Bolte 已提交
3792
{
M
Matthias Bolte 已提交
3793 3794
    char *fileName = NULL;

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

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

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

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

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

3820
        if (fileName == NULL)
M
Matthias Bolte 已提交
3821 3822
            return -1;

3823
        virBufferAsprintf(buffer, "parallel%d.fileName = \"%s\"\n",
3824
                          def->target.port, fileName);
M
Matthias Bolte 已提交
3825 3826

        VIR_FREE(fileName);
M
Matthias Bolte 已提交
3827 3828 3829
        break;

      default:
3830 3831 3832
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported character device type '%s'"),
                       virDomainChrTypeToString(def->source.type));
M
Matthias Bolte 已提交
3833 3834 3835 3836 3837
        return -1;
    }

    return 0;
}
3838 3839 3840 3841 3842 3843



int
virVMXFormatSVGA(virDomainVideoDefPtr def, virBufferPtr buffer)
{
3844 3845
    unsigned long long vram;

3846
    if (def->type != VIR_DOMAIN_VIDEO_TYPE_VMVGA) {
3847 3848 3849
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Unsupported video device type '%s'"),
                       virDomainVideoTypeToString(def->type));
3850 3851 3852 3853 3854 3855 3856
        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
     */
3857
    vram = VIR_DIV_UP(def->vram, 64) * 64;
3858 3859

    if (def->heads > 1) {
3860 3861
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Multi-head video devices are unsupported"));
3862 3863 3864
        return -1;
    }

3865
    virBufferAsprintf(buffer, "svga.vramSize = \"%lld\"\n",
3866
                      vram * 1024); /* kilobyte to byte */
3867 3868 3869

    return 0;
}