2 * qemu_command.c: QEMU command generation
4 * Copyright (C) 2006-2014 Red Hat, Inc.
5 * Copyright (C) 2006 Daniel P. Berrange
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <http://www.gnu.org/licenses/>.
21 * Author: Daniel P. Berrange <berrange@redhat.com>
26 #include "qemu_command.h"
27 #include "qemu_hostdev.h"
28 #include "qemu_capabilities.h"
37 #include "virnetdev.h"
38 #include "virstring.h"
42 #include "domain_nwfilter.h"
43 #include "domain_addr.h"
44 #include "domain_audit.h"
45 #include "domain_conf.h"
46 #include "snapshot_conf.h"
47 #include "storage_conf.h"
48 #include "network/bridge_driver.h"
49 #include "virnetdevtap.h"
51 #include "device_conf.h"
52 #include "virstoragefile.h"
55 #if defined(__linux__)
56 # include <linux/capability.h>
62 #define VIR_FROM_THIS VIR_FROM_QEMU
64 VIR_LOG_INIT("qemu.qemu_command");
66 #define VIO_ADDR_NET 0x1000ul
67 #define VIO_ADDR_SCSI 0x2000ul
68 #define VIO_ADDR_SERIAL 0x30000000ul
69 #define VIO_ADDR_NVRAM 0x3000ul
71 VIR_ENUM_DECL(virDomainDiskQEMUBus)
72 VIR_ENUM_IMPL(virDomainDiskQEMUBus, VIR_DOMAIN_DISK_BUS_LAST,
84 VIR_ENUM_DECL(qemuDiskCacheV1)
85 VIR_ENUM_DECL(qemuDiskCacheV2)
87 VIR_ENUM_IMPL(qemuDiskCacheV1, VIR_DOMAIN_DISK_CACHE_LAST,
90 "off", /* writethrough not supported, so for safety, disable */
91 "on", /* Old 'on' was equivalent to 'writeback' */
92 "off", /* directsync not supported, for safety, disable */
93 "off"); /* unsafe not supported, for safety, disable */
95 VIR_ENUM_IMPL(qemuDiskCacheV2, VIR_DOMAIN_DISK_CACHE_LAST,
103 VIR_ENUM_DECL(qemuVideo)
105 VIR_ENUM_IMPL(qemuVideo, VIR_DOMAIN_VIDEO_TYPE_LAST,
109 "", /* no arg needed for xen */
110 "", /* don't support vbox */
113 VIR_ENUM_DECL(qemuDeviceVideo)
115 VIR_ENUM_IMPL(qemuDeviceVideo, VIR_DOMAIN_VIDEO_TYPE_LAST,
119 "", /* no device for xen */
120 "", /* don't support vbox */
123 VIR_ENUM_DECL(qemuSoundCodec)
125 VIR_ENUM_IMPL(qemuSoundCodec, VIR_DOMAIN_SOUND_CODEC_TYPE_LAST,
129 VIR_ENUM_DECL(qemuControllerModelUSB)
131 VIR_ENUM_IMPL(qemuControllerModelUSB, VIR_DOMAIN_CONTROLLER_MODEL_USB_LAST,
139 "vt82c686b-usb-uhci",
144 VIR_ENUM_DECL(qemuDomainFSDriver)
145 VIR_ENUM_IMPL(qemuDomainFSDriver, VIR_DOMAIN_FS_DRIVER_TYPE_LAST,
154 * qemuPhysIfaceConnect:
155 * @def: the definition of the VM (needed by 802.1Qbh and audit)
156 * @driver: pointer to the driver instance
157 * @net: pointer to he VM's interface description with direct device type
158 * @qemuCaps: flags for qemu
159 * @vmop: VM operation type
161 * Returns a filedescriptor on success or -1 in case of error.
164 qemuPhysIfaceConnect(virDomainDefPtr def,
165 virQEMUDriverPtr driver,
166 virDomainNetDefPtr net,
167 virQEMUCapsPtr qemuCaps,
168 virNetDevVPortProfileOp vmop)
171 char *res_ifname = NULL;
173 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
175 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNET_HDR) &&
176 net->model && STREQ(net->model, "virtio"))
179 rc = virNetDevMacVLanCreateWithVPortProfile(
180 net->ifname, &net->mac,
181 virDomainNetGetActualDirectDev(net),
182 virDomainNetGetActualDirectMode(net),
183 true, vnet_hdr, def->uuid,
184 virDomainNetGetActualVirtPortProfile(net),
187 virDomainNetGetActualBandwidth(net));
189 if (virSecurityManagerSetTapFDLabel(driver->securityManager,
193 virDomainAuditNetDevice(def, net, res_ifname, true);
194 VIR_FREE(net->ifname);
195 net->ifname = res_ifname;
202 ignore_value(virNetDevMacVLanDeleteWithVPortProfile(
203 res_ifname, &net->mac,
204 virDomainNetGetActualDirectDev(net),
205 virDomainNetGetActualDirectMode(net),
206 virDomainNetGetActualVirtPortProfile(net),
208 VIR_FREE(res_ifname);
215 * qemuCreateInBridgePortWithHelper:
216 * @cfg: the configuration object in which the helper name is looked up
217 * @brname: the bridge name
218 * @ifname: the returned interface name
219 * @macaddr: the returned MAC address
220 * @tapfd: file descriptor return value for the new tap device
221 * @flags: OR of virNetDevTapCreateFlags:
223 * VIR_NETDEV_TAP_CREATE_VNET_HDR
224 * - Enable IFF_VNET_HDR on the tap device
226 * This function creates a new tap device on a bridge using an external
227 * helper. The final name for the bridge will be stored in @ifname.
229 * Returns 0 in case of success or -1 on failure
231 static int qemuCreateInBridgePortWithHelper(virQEMUDriverConfigPtr cfg,
238 int pair[2] = { -1, -1 };
240 if ((flags & ~VIR_NETDEV_TAP_CREATE_VNET_HDR) != VIR_NETDEV_TAP_CREATE_IFUP)
243 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
244 virReportSystemError(errno, "%s", _("failed to create socket"));
248 cmd = virCommandNew(cfg->bridgeHelperName);
249 if (flags & VIR_NETDEV_TAP_CREATE_VNET_HDR)
250 virCommandAddArgFormat(cmd, "--use-vnet");
251 virCommandAddArgFormat(cmd, "--br=%s", brname);
252 virCommandAddArgFormat(cmd, "--fd=%d", pair[1]);
253 virCommandPassFD(cmd, pair[1],
254 VIR_COMMAND_PASS_FD_CLOSE_PARENT);
255 virCommandClearCaps(cmd);
257 virCommandAllowCap(cmd, CAP_NET_ADMIN);
259 if (virCommandRunAsync(cmd, NULL) < 0) {
265 *tapfd = recvfd(pair[0], 0);
266 } while (*tapfd < 0 && errno == EINTR);
268 virReportSystemError(errno, "%s",
269 _("failed to retrieve file descriptor for interface"));
273 if (virNetDevTapGetName(*tapfd, ifname) < 0 ||
274 virCommandWait(cmd, NULL) < 0) {
275 VIR_FORCE_CLOSE(*tapfd);
281 VIR_FORCE_CLOSE(pair[0]);
282 return *tapfd < 0 ? -1 : 0;
286 qemuNetworkIfaceConnect(virDomainDefPtr def,
288 virQEMUDriverPtr driver,
289 virDomainNetDefPtr net,
290 virQEMUCapsPtr qemuCaps,
296 unsigned int tap_create_flags = VIR_NETDEV_TAP_CREATE_IFUP;
297 bool template_ifname = false;
298 int actualType = virDomainNetGetActualType(net);
299 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
301 if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
304 virNetworkPtr network = virNetworkLookupByName(conn,
305 net->data.network.name);
309 if (!(brname = virNetworkGetBridgeName(network)))
312 /* Make sure any above failure is preserved */
313 errobj = virSaveLastError();
314 virNetworkFree(network);
316 virFreeError(errobj);
321 } else if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
322 if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
325 virReportError(VIR_ERR_INTERNAL_ERROR,
326 _("Network type %d is not supported"),
327 virDomainNetGetActualType(net));
332 STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
333 strchr(net->ifname, '%')) {
334 VIR_FREE(net->ifname);
335 if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_PREFIX "%d") < 0)
337 /* avoid exposing vnet%d in getXMLDesc or error outputs */
338 template_ifname = true;
341 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNET_HDR) &&
342 net->model && STREQ(net->model, "virtio")) {
343 tap_create_flags |= VIR_NETDEV_TAP_CREATE_VNET_HDR;
346 if (cfg->privileged) {
347 if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
348 def->uuid, tapfd, *tapfdSize,
349 virDomainNetGetActualVirtPortProfile(net),
350 virDomainNetGetActualVlan(net),
351 tap_create_flags) < 0) {
352 virDomainAuditNetDevice(def, net, "/dev/net/tun", false);
356 if (qemuCreateInBridgePortWithHelper(cfg, brname,
358 tapfd, tap_create_flags) < 0) {
359 virDomainAuditNetDevice(def, net, "/dev/net/tun", false);
362 /* qemuCreateInBridgePortWithHelper can only create a single FD */
363 if (*tapfdSize > 1) {
364 VIR_WARN("Ignoring multiqueue network request");
369 virDomainAuditNetDevice(def, net, "/dev/net/tun", true);
371 if (cfg->macFilter &&
372 ebtablesAddForwardAllowIn(driver->ebtables,
377 if (virNetDevBandwidthSet(net->ifname,
378 virDomainNetGetActualBandwidth(net),
380 virReportError(VIR_ERR_INTERNAL_ERROR,
381 _("cannot set bandwidth limits on %s"),
386 if (net->filter && net->ifname &&
387 virDomainConfNWFilterInstantiate(conn, def->uuid, net) < 0) {
396 for (i = 0; i < *tapfdSize && tapfd[i] >= 0; i++)
397 VIR_FORCE_CLOSE(tapfd[i]);
399 VIR_FREE(net->ifname);
408 qemuDomainSupportsNicdev(virDomainDefPtr def,
409 virQEMUCapsPtr qemuCaps,
410 virDomainNetDefPtr net)
412 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
415 /* non-virtio ARM nics require legacy -net nic */
416 if (((def->os.arch == VIR_ARCH_ARMV7L) ||
417 (def->os.arch == VIR_ARCH_AARCH64)) &&
418 net->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO)
425 qemuDomainSupportsNetdev(virDomainDefPtr def,
426 virQEMUCapsPtr qemuCaps,
427 virDomainNetDefPtr net)
429 if (!qemuDomainSupportsNicdev(def, qemuCaps, net))
431 return virQEMUCapsGet(qemuCaps, QEMU_CAPS_NETDEV);
436 * @def: domain definition
437 * @net: network definition
438 * @qemuCaps: qemu binary capabilities
439 * @vhostfd: array of opened vhost-net device
440 * @vhostfdSize: number of file descriptors in @vhostfd array
442 * Open vhost-net, multiple times - if requested.
443 * In case, no vhost-net is needed, @vhostfdSize is set to 0
446 * Returns: 0 on success
450 qemuOpenVhostNet(virDomainDefPtr def,
451 virDomainNetDefPtr net,
452 virQEMUCapsPtr qemuCaps,
458 /* If running a plain QEMU guest, or
459 * if the config says explicitly to not use vhost, return now*/
460 if (def->virtType != VIR_DOMAIN_VIRT_KVM ||
461 net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_QEMU) {
466 /* If qemu doesn't support vhost-net mode (including the -netdev command
467 * option), don't try to open the device.
469 if (!(virQEMUCapsGet(qemuCaps, QEMU_CAPS_VHOST_NET) &&
470 qemuDomainSupportsNetdev(def, qemuCaps, net))) {
471 if (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_VHOST) {
472 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
473 "%s", _("vhost-net is not supported with "
474 "this QEMU binary"));
481 /* If the nic model isn't virtio, don't try to open. */
482 if (!(net->model && STREQ(net->model, "virtio"))) {
483 if (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_VHOST) {
484 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
485 "%s", _("vhost-net is only supported for "
486 "virtio network interfaces"));
493 for (i = 0; i < *vhostfdSize; i++) {
494 vhostfd[i] = open("/dev/vhost-net", O_RDWR);
496 /* If the config says explicitly to use vhost and we couldn't open it,
499 if (vhostfd[i] < 0) {
500 virDomainAuditNetDevice(def, net, "/dev/vhost-net", false);
501 if (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_VHOST) {
502 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
503 "%s", _("vhost-net was requested for an interface, "
504 "but is unavailable"));
507 VIR_WARN("Unable to open vhost-net. Opened so far %zu, requested %d",
513 virDomainAuditNetDevice(def, net, "/dev/vhost-net", *vhostfdSize);
518 VIR_FORCE_CLOSE(vhostfd[i]);
524 qemuNetworkPrepareDevices(virDomainDefPtr def)
529 for (i = 0; i < def->nnets; i++) {
530 virDomainNetDefPtr net = def->nets[i];
533 /* If appropriate, grab a physical device from the configured
534 * network's pool of devices, or resolve bridge device name
535 * to the one defined in the network definition.
537 if (networkAllocateActualDevice(def, net) < 0)
540 actualType = virDomainNetGetActualType(net);
541 if (actualType == VIR_DOMAIN_NET_TYPE_HOSTDEV &&
542 net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
543 /* Each type='hostdev' network device must also have a
544 * corresponding entry in the hostdevs array. For netdevs
545 * that are hardcoded as type='hostdev', this is already
546 * done by the parser, but for those allocated from a
547 * network / determined at runtime, we need to do it
550 virDomainHostdevDefPtr hostdev = virDomainNetGetActualHostdev(net);
552 if (virDomainHostdevFind(def, hostdev, NULL) >= 0) {
553 virReportError(VIR_ERR_INTERNAL_ERROR,
554 _("PCI device %04x:%02x:%02x.%x "
555 "allocated from network %s is already "
556 "in use by domain %s"),
557 hostdev->source.subsys.u.pci.addr.domain,
558 hostdev->source.subsys.u.pci.addr.bus,
559 hostdev->source.subsys.u.pci.addr.slot,
560 hostdev->source.subsys.u.pci.addr.function,
561 net->data.network.name,
565 if (virDomainHostdevInsert(def, hostdev) < 0)
574 static int qemuDomainDeviceAliasIndex(const virDomainDeviceInfo *info,
581 if (!STRPREFIX(info->alias, prefix))
584 if (virStrToLong_i(info->alias + strlen(prefix), NULL, 10, &idx) < 0)
591 int qemuDomainNetVLAN(virDomainNetDefPtr def)
593 return qemuDomainDeviceAliasIndex(&def->info, "net");
597 /* Names used before -drive existed */
598 static int qemuAssignDeviceDiskAliasLegacy(virDomainDiskDefPtr disk)
602 if (VIR_STRDUP(dev_name,
603 disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
604 STREQ(disk->dst, "hdc") ? "cdrom" : disk->dst) < 0)
606 disk->info.alias = dev_name;
611 char *qemuDeviceDriveHostAlias(virDomainDiskDefPtr disk,
612 virQEMUCapsPtr qemuCaps)
616 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
617 ignore_value(virAsprintf(&ret, "%s%s", QEMU_DRIVE_HOST_PREFIX,
620 ignore_value(VIR_STRDUP(ret, disk->info.alias));
626 /* Names used before -drive supported the id= option */
627 static int qemuAssignDeviceDiskAliasFixed(virDomainDiskDefPtr disk)
633 if (virDiskNameToBusDeviceIndex(disk, &busid, &devid) < 0) {
634 virReportError(VIR_ERR_INTERNAL_ERROR,
635 _("cannot convert disk '%s' to bus/device index"),
641 case VIR_DOMAIN_DISK_BUS_IDE:
642 if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
643 ret = virAsprintf(&dev_name, "ide%d-hd%d", busid, devid);
645 ret = virAsprintf(&dev_name, "ide%d-cd%d", busid, devid);
647 case VIR_DOMAIN_DISK_BUS_SCSI:
648 if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
649 ret = virAsprintf(&dev_name, "scsi%d-hd%d", busid, devid);
651 ret = virAsprintf(&dev_name, "scsi%d-cd%d", busid, devid);
653 case VIR_DOMAIN_DISK_BUS_FDC:
654 ret = virAsprintf(&dev_name, "floppy%d", devid);
656 case VIR_DOMAIN_DISK_BUS_VIRTIO:
657 ret = virAsprintf(&dev_name, "virtio%d", devid);
659 case VIR_DOMAIN_DISK_BUS_XEN:
660 ret = virAsprintf(&dev_name, "xenblk%d", devid);
662 case VIR_DOMAIN_DISK_BUS_SD:
663 ret = virAsprintf(&dev_name, "sd%d", devid);
666 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
667 _("Unsupported disk name mapping for bus '%s'"),
668 virDomainDiskBusTypeToString(disk->bus));
675 disk->info.alias = dev_name;
681 qemuSetSCSIControllerModel(virDomainDefPtr def,
682 virQEMUCapsPtr qemuCaps,
687 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC:
688 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_LSI)) {
689 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
690 _("This QEMU doesn't support "
691 "the LSI 53C895A SCSI controller"));
695 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VIRTIO_SCSI:
696 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_SCSI)) {
697 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
698 _("This QEMU doesn't support "
699 "virtio scsi controller"));
703 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_IBMVSCSI:
704 /*TODO: need checking work here if necessary */
706 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSISAS1078:
707 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_MEGASAS)) {
708 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
709 _("This QEMU doesn't support "
710 "the LSI SAS1078 controller"));
715 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
716 _("Unsupported controller model: %s"),
717 virDomainControllerModelSCSITypeToString(*model));
721 if ((def->os.arch == VIR_ARCH_PPC64) &&
722 STREQ(def->os.machine, "pseries")) {
723 *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_IBMVSCSI;
724 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_LSI)) {
725 *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC;
726 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_SCSI)) {
727 *model = VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VIRTIO_SCSI;
729 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
730 _("Unable to determine model for scsi controller"));
738 /* Our custom -drive naming scheme used with id= */
740 qemuAssignDeviceDiskAliasCustom(virDomainDefPtr def,
741 virDomainDiskDefPtr disk,
742 virQEMUCapsPtr qemuCaps)
744 const char *prefix = virDomainDiskBusTypeToString(disk->bus);
745 int controllerModel = -1;
747 if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
748 if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
750 virDomainDeviceFindControllerModel(def, &disk->info,
751 VIR_DOMAIN_CONTROLLER_TYPE_SCSI);
753 if ((qemuSetSCSIControllerModel(def, qemuCaps, &controllerModel)) < 0)
757 if (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI ||
758 controllerModel == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC) {
759 if (virAsprintf(&disk->info.alias, "%s%d-%d-%d", prefix,
760 disk->info.addr.drive.controller,
761 disk->info.addr.drive.bus,
762 disk->info.addr.drive.unit) < 0)
765 if (virAsprintf(&disk->info.alias, "%s%d-%d-%d-%d", prefix,
766 disk->info.addr.drive.controller,
767 disk->info.addr.drive.bus,
768 disk->info.addr.drive.target,
769 disk->info.addr.drive.unit) < 0)
773 int idx = virDiskNameToIndex(disk->dst);
774 if (virAsprintf(&disk->info.alias, "%s-disk%d", prefix, idx) < 0)
783 qemuAssignDeviceDiskAlias(virDomainDefPtr vmdef,
784 virDomainDiskDefPtr def,
785 virQEMUCapsPtr qemuCaps)
787 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE)) {
788 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
789 return qemuAssignDeviceDiskAliasCustom(vmdef, def, qemuCaps);
791 return qemuAssignDeviceDiskAliasFixed(def);
793 return qemuAssignDeviceDiskAliasLegacy(def);
799 qemuAssignDeviceNetAlias(virDomainDefPtr def, virDomainNetDefPtr net, int idx)
804 for (i = 0; i < def->nnets; i++) {
807 if (virDomainNetGetActualType(def->nets[i])
808 == VIR_DOMAIN_NET_TYPE_HOSTDEV) {
809 /* type='hostdev' interfaces have a hostdev%d alias */
812 if ((thisidx = qemuDomainDeviceAliasIndex(&def->nets[i]->info, "net")) < 0) {
813 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
814 _("Unable to determine device index for network device"));
822 if (virAsprintf(&net->info.alias, "net%d", idx) < 0)
829 qemuAssignDeviceHostdevAlias(virDomainDefPtr def, virDomainHostdevDefPtr hostdev, int idx)
834 for (i = 0; i < def->nhostdevs; i++) {
836 if ((thisidx = qemuDomainDeviceAliasIndex(def->hostdevs[i]->info, "hostdev")) < 0) {
837 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
838 _("Unable to determine device index for hostdev device"));
846 if (virAsprintf(&hostdev->info->alias, "hostdev%d", idx) < 0)
854 qemuAssignDeviceRedirdevAlias(virDomainDefPtr def, virDomainRedirdevDefPtr redirdev, int idx)
859 for (i = 0; i < def->nredirdevs; i++) {
861 if ((thisidx = qemuDomainDeviceAliasIndex(&def->redirdevs[i]->info, "redir")) < 0) {
862 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
863 _("Unable to determine device index for redirected device"));
871 if (virAsprintf(&redirdev->info.alias, "redir%d", idx) < 0)
878 qemuAssignDeviceControllerAlias(virDomainControllerDefPtr controller)
880 const char *prefix = virDomainControllerTypeToString(controller->type);
882 if (controller->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
883 /* only pcie-root uses a different naming convention
884 * ("pcie.0"), because it is hardcoded that way in qemu. All
885 * other buses use the consistent "pci.%u".
887 if (controller->model == VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT)
888 return virAsprintf(&controller->info.alias, "pcie.%d", controller->idx);
890 return virAsprintf(&controller->info.alias, "pci.%d", controller->idx);
893 return virAsprintf(&controller->info.alias, "%s%d", prefix, controller->idx);
897 qemuGetNextChrDevIndex(virDomainDefPtr def,
898 virDomainChrDefPtr chr,
901 const virDomainChrDef **arrPtr;
905 const char *prefix2 = NULL;
907 if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE)
910 virDomainChrGetDomainPtrs(def, chr->deviceType, &arrPtr, &cnt);
912 for (i = 0; i < cnt; i++) {
914 if (((thisidx = qemuDomainDeviceAliasIndex(&arrPtr[i]->info, prefix)) < 0) &&
916 (thisidx = qemuDomainDeviceAliasIndex(&arrPtr[i]->info, prefix2)) < 0)) {
917 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
918 _("Unable to determine device index for character device"));
930 qemuAssignDeviceChrAlias(virDomainDefPtr def,
931 virDomainChrDefPtr chr,
934 const char *prefix = NULL;
936 switch ((enum virDomainChrDeviceType) chr->deviceType) {
937 case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
941 case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
945 case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
949 case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
953 case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
957 if (idx == -1 && (idx = qemuGetNextChrDevIndex(def, chr, prefix)) < 0)
960 return virAsprintf(&chr->info.alias, "%s%zd", prefix, idx);
964 qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
968 for (i = 0; i < def->ndisks; i++) {
969 if (qemuAssignDeviceDiskAlias(def, def->disks[i], qemuCaps) < 0)
972 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NET_NAME) ||
973 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
974 for (i = 0; i < def->nnets; i++) {
975 /* type='hostdev' interfaces are also on the hostdevs list,
976 * and will have their alias assigned with other hostdevs.
978 if (virDomainNetGetActualType(def->nets[i])
979 != VIR_DOMAIN_NET_TYPE_HOSTDEV &&
980 qemuAssignDeviceNetAlias(def, def->nets[i], i) < 0) {
986 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
989 for (i = 0; i < def->nfss; i++) {
990 if (virAsprintf(&def->fss[i]->info.alias, "fs%zu", i) < 0)
993 for (i = 0; i < def->nsounds; i++) {
994 if (virAsprintf(&def->sounds[i]->info.alias, "sound%zu", i) < 0)
997 for (i = 0; i < def->nhostdevs; i++) {
998 if (qemuAssignDeviceHostdevAlias(def, def->hostdevs[i], i) < 0)
1001 for (i = 0; i < def->nredirdevs; i++) {
1002 if (qemuAssignDeviceRedirdevAlias(def, def->redirdevs[i], i) < 0)
1005 for (i = 0; i < def->nvideos; i++) {
1006 if (virAsprintf(&def->videos[i]->info.alias, "video%zu", i) < 0)
1009 for (i = 0; i < def->ncontrollers; i++) {
1010 if (qemuAssignDeviceControllerAlias(def->controllers[i]) < 0)
1013 for (i = 0; i < def->ninputs; i++) {
1014 if (virAsprintf(&def->inputs[i]->info.alias, "input%zu", i) < 0)
1017 for (i = 0; i < def->nparallels; i++) {
1018 if (qemuAssignDeviceChrAlias(def, def->parallels[i], i) < 0)
1021 for (i = 0; i < def->nserials; i++) {
1022 if (qemuAssignDeviceChrAlias(def, def->serials[i], i) < 0)
1025 for (i = 0; i < def->nchannels; i++) {
1026 if (qemuAssignDeviceChrAlias(def, def->channels[i], i) < 0)
1029 for (i = 0; i < def->nconsoles; i++) {
1030 if (qemuAssignDeviceChrAlias(def, def->consoles[i], i) < 0)
1033 for (i = 0; i < def->nhubs; i++) {
1034 if (virAsprintf(&def->hubs[i]->info.alias, "hub%zu", i) < 0)
1037 for (i = 0; i < def->nsmartcards; i++) {
1038 if (virAsprintf(&def->smartcards[i]->info.alias, "smartcard%zu", i) < 0)
1041 if (def->watchdog) {
1042 if (virAsprintf(&def->watchdog->info.alias, "watchdog%d", 0) < 0)
1045 if (def->memballoon) {
1046 if (virAsprintf(&def->memballoon->info.alias, "balloon%d", 0) < 0)
1050 if (virAsprintf(&def->rng->info.alias, "rng%d", 0) < 0)
1054 if (virAsprintf(&def->tpm->info.alias, "tpm%d", 0) < 0)
1061 /* S390 ccw bus support */
1063 struct _qemuDomainCCWAddressSet {
1064 virHashTablePtr defined;
1065 virDomainDeviceCCWAddress next;
1069 qemuCCWAddressAsString(virDomainDeviceCCWAddressPtr addr)
1071 char *addrstr = NULL;
1073 ignore_value(virAsprintf(&addrstr, "%x.%x.%04x",
1081 qemuCCWAdressIncrement(virDomainDeviceCCWAddressPtr addr)
1083 virDomainDeviceCCWAddress ccwaddr = *addr;
1085 /* We are not touching subchannel sets and channel subsystems */
1086 if (++ccwaddr.devno > VIR_DOMAIN_DEVICE_CCW_MAX_DEVNO)
1094 int qemuDomainCCWAddressAssign(virDomainDeviceInfoPtr dev,
1095 qemuDomainCCWAddressSetPtr addrs,
1101 if (dev->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW)
1104 if (!autoassign && dev->addr.ccw.assigned) {
1105 if (!(addr = qemuCCWAddressAsString(&dev->addr.ccw)))
1108 if (virHashLookup(addrs->defined, addr)) {
1109 virReportError(VIR_ERR_XML_ERROR,
1110 _("The CCW devno '%s' is in use already "),
1114 } else if (autoassign && !dev->addr.ccw.assigned) {
1115 if (!(addr = qemuCCWAddressAsString(&addrs->next)) < 0)
1118 while (virHashLookup(addrs->defined, addr)) {
1119 if (qemuCCWAdressIncrement(&addrs->next) < 0) {
1120 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1121 _("There are no more free CCW devnos."));
1125 addr = qemuCCWAddressAsString(&addrs->next);
1127 dev->addr.ccw = addrs->next;
1128 dev->addr.ccw.assigned = true;
1133 if (virHashAddEntry(addrs->defined, addr, addr) < 0)
1136 addr = NULL; /* memory will be freed by hash table */
1146 qemuDomainPrimeVirtioDeviceAddresses(virDomainDefPtr def,
1147 enum virDomainDeviceAddressType type)
1150 declare address-less virtio devices to be of address type 'type'
1151 disks, networks, consoles, controllers, memballoon and rng in this
1156 for (i = 0; i < def->ndisks; i++) {
1157 if (def->disks[i]->bus == VIR_DOMAIN_DISK_BUS_VIRTIO &&
1158 def->disks[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1159 def->disks[i]->info.type = type;
1162 for (i = 0; i < def->nnets; i++) {
1163 if (STREQ(def->nets[i]->model, "virtio") &&
1164 def->nets[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
1165 def->nets[i]->info.type = type;
1169 for (i = 0; i < def->ncontrollers; i++) {
1170 if ((def->controllers[i]->type ==
1171 VIR_DOMAIN_CONTROLLER_TYPE_VIRTIO_SERIAL ||
1172 def->controllers[i]->type ==
1173 VIR_DOMAIN_CONTROLLER_TYPE_SCSI) &&
1174 def->controllers[i]->info.type ==
1175 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1176 def->controllers[i]->info.type = type;
1179 if (def->memballoon &&
1180 def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO &&
1181 def->memballoon->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1182 def->memballoon->info.type = type;
1185 def->rng->model == VIR_DOMAIN_RNG_MODEL_VIRTIO &&
1186 def->rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1187 def->rng->info.type = type;
1191 qemuDomainCCWAddressAllocate(virDomainDefPtr def ATTRIBUTE_UNUSED,
1192 virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
1193 virDomainDeviceInfoPtr info,
1196 return qemuDomainCCWAddressAssign(info, data, true);
1200 qemuDomainCCWAddressValidate(virDomainDefPtr def ATTRIBUTE_UNUSED,
1201 virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
1202 virDomainDeviceInfoPtr info,
1205 return qemuDomainCCWAddressAssign(info, data, false);
1209 qemuDomainCCWAddressReleaseAddr(qemuDomainCCWAddressSetPtr addrs,
1210 virDomainDeviceInfoPtr dev)
1215 addr = qemuCCWAddressAsString(&(dev->addr.ccw));
1219 if ((ret = virHashRemoveEntry(addrs->defined, addr)) == 0 &&
1220 dev->addr.ccw.cssid == addrs->next.cssid &&
1221 dev->addr.ccw.ssid == addrs->next.ssid &&
1222 dev->addr.ccw.devno < addrs->next.devno) {
1223 addrs->next.devno = dev->addr.ccw.devno;
1224 addrs->next.assigned = false;
1232 void qemuDomainCCWAddressSetFree(qemuDomainCCWAddressSetPtr addrs)
1237 virHashFree(addrs->defined);
1241 static qemuDomainCCWAddressSetPtr
1242 qemuDomainCCWAddressSetCreate(void)
1244 qemuDomainCCWAddressSetPtr addrs = NULL;
1246 if (VIR_ALLOC(addrs) < 0)
1249 if (!(addrs->defined = virHashCreate(10, virHashValueFree)))
1252 /* must use cssid = 0xfe (254) for virtio-ccw devices */
1253 addrs->next.cssid = 254;
1254 addrs->next.ssid = 0;
1255 addrs->next.devno = 0;
1256 addrs->next.assigned = 0;
1260 qemuDomainCCWAddressSetFree(addrs);
1265 * Three steps populating CCW devnos
1266 * 1. Allocate empty address set
1267 * 2. Gather addresses with explicit devno
1268 * 3. Assign defaults to the rest
1271 qemuDomainAssignS390Addresses(virDomainDefPtr def,
1272 virQEMUCapsPtr qemuCaps,
1273 virDomainObjPtr obj)
1276 qemuDomainCCWAddressSetPtr addrs = NULL;
1277 qemuDomainObjPrivatePtr priv = NULL;
1279 if (STREQLEN(def->os.machine, "s390-ccw", 8) &&
1280 virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_CCW)) {
1281 qemuDomainPrimeVirtioDeviceAddresses(
1282 def, VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW);
1284 if (!(addrs = qemuDomainCCWAddressSetCreate()))
1287 if (virDomainDeviceInfoIterate(def, qemuDomainCCWAddressValidate,
1291 if (virDomainDeviceInfoIterate(def, qemuDomainCCWAddressAllocate,
1294 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_S390)) {
1295 /* deal with legacy virtio-s390 */
1296 qemuDomainPrimeVirtioDeviceAddresses(
1297 def, VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390);
1300 if (obj && obj->privateData) {
1301 priv = obj->privateData;
1303 /* if this is the live domain object, we persist the CCW addresses*/
1304 qemuDomainCCWAddressSetFree(priv->ccwaddrs);
1305 priv->persistentAddrs = 1;
1306 priv->ccwaddrs = addrs;
1309 priv->persistentAddrs = 0;
1315 qemuDomainCCWAddressSetFree(addrs);
1321 qemuDomainAssignARMVirtioMMIOAddresses(virDomainDefPtr def,
1322 virQEMUCapsPtr qemuCaps)
1324 if (((def->os.arch == VIR_ARCH_ARMV7L) ||
1325 (def->os.arch == VIR_ARCH_AARCH64)) &&
1326 (STRPREFIX(def->os.machine, "vexpress-") ||
1327 STREQ(def->os.machine, "virt")) &&
1328 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VIRTIO_MMIO)) {
1329 qemuDomainPrimeVirtioDeviceAddresses(
1330 def, VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO);
1336 qemuSpaprVIOFindByReg(virDomainDefPtr def ATTRIBUTE_UNUSED,
1337 virDomainDeviceDefPtr device ATTRIBUTE_UNUSED,
1338 virDomainDeviceInfoPtr info, void *opaque)
1340 virDomainDeviceInfoPtr target = opaque;
1342 if (info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO)
1345 /* Match a dev that has a reg, is not us, and has a matching reg */
1346 if (info->addr.spaprvio.has_reg && info != target &&
1347 info->addr.spaprvio.reg == target->addr.spaprvio.reg)
1348 /* Has to be < 0 so virDomainDeviceInfoIterate() will exit */
1355 qemuAssignSpaprVIOAddress(virDomainDefPtr def, virDomainDeviceInfoPtr info,
1356 unsigned long long default_reg)
1361 if (info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO)
1364 /* Check if the user has assigned the reg already, if so use it */
1365 user_reg = info->addr.spaprvio.has_reg;
1367 info->addr.spaprvio.reg = default_reg;
1368 info->addr.spaprvio.has_reg = true;
1371 ret = virDomainDeviceInfoIterate(def, qemuSpaprVIOFindByReg, info);
1374 virReportError(VIR_ERR_XML_ERROR,
1375 _("spapr-vio address %#llx already in use"),
1376 info->addr.spaprvio.reg);
1380 /* We assigned the reg, so try a new value */
1381 info->addr.spaprvio.reg += 0x1000;
1382 ret = virDomainDeviceInfoIterate(def, qemuSpaprVIOFindByReg, info);
1388 int qemuDomainAssignSpaprVIOAddresses(virDomainDefPtr def,
1389 virQEMUCapsPtr qemuCaps)
1395 /* Default values match QEMU. See spapr_(llan|vscsi|vty).c */
1397 for (i = 0; i < def->nnets; i++) {
1398 if (def->nets[i]->model &&
1399 STREQ(def->nets[i]->model, "spapr-vlan"))
1400 def->nets[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO;
1401 if (qemuAssignSpaprVIOAddress(def, &def->nets[i]->info,
1406 for (i = 0; i < def->ncontrollers; i++) {
1407 model = def->controllers[i]->model;
1408 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI) {
1409 if (qemuSetSCSIControllerModel(def, qemuCaps, &model) < 0)
1413 if (model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_IBMVSCSI &&
1414 def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI)
1415 def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO;
1416 if (qemuAssignSpaprVIOAddress(def, &def->controllers[i]->info,
1421 for (i = 0; i < def->nserials; i++) {
1422 if (def->serials[i]->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
1423 (def->os.arch == VIR_ARCH_PPC64) &&
1424 STREQ(def->os.machine, "pseries"))
1425 def->serials[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO;
1426 if (qemuAssignSpaprVIOAddress(def, &def->serials[i]->info,
1427 VIO_ADDR_SERIAL) < 0)
1432 if (def->os.arch == VIR_ARCH_PPC64 &&
1433 STREQ(def->os.machine, "pseries"))
1434 def->nvram->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO;
1435 if (qemuAssignSpaprVIOAddress(def, &def->nvram->info,
1436 VIO_ADDR_NVRAM) < 0)
1440 /* No other devices are currently supported on spapr-vio */
1450 qemuCollectPCIAddress(virDomainDefPtr def ATTRIBUTE_UNUSED,
1451 virDomainDeviceDefPtr device,
1452 virDomainDeviceInfoPtr info,
1455 virDomainPCIAddressSetPtr addrs = opaque;
1457 virDevicePCIAddressPtr addr = &info->addr.pci;
1459 /* flags may be changed from default below */
1460 virDomainPCIConnectFlags flags = (VIR_PCI_CONNECT_HOTPLUGGABLE |
1461 VIR_PCI_CONNECT_TYPE_PCI);
1463 if ((info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)
1464 || ((device->type == VIR_DOMAIN_DEVICE_HOSTDEV) &&
1465 (device->data.hostdev->parent.type != VIR_DOMAIN_DEVICE_NONE))) {
1466 /* If a hostdev has a parent, its info will be a part of the
1467 * parent, and will have its address collected during the scan
1468 * of the parent's device type.
1473 /* Change flags according to differing requirements of different
1476 switch (device->type) {
1477 case VIR_DOMAIN_DEVICE_CONTROLLER:
1478 switch (device->data.controller->type) {
1479 case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
1480 switch (device->data.controller->model) {
1481 case VIR_DOMAIN_CONTROLLER_MODEL_PCI_BRIDGE:
1482 /* pci-bridge needs a PCI slot, but it isn't
1483 * hot-pluggable, so it doesn't need a hot-pluggable slot.
1485 flags = VIR_PCI_CONNECT_TYPE_PCI;
1487 case VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE:
1488 /* pci-bridge needs a PCIe slot, but it isn't
1489 * hot-pluggable, so it doesn't need a hot-pluggable slot.
1491 flags = VIR_PCI_CONNECT_TYPE_PCIE;
1498 case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
1499 /* SATA controllers aren't hot-plugged, and can be put in
1500 * either a PCI or PCIe slot
1502 flags = VIR_PCI_CONNECT_TYPE_PCI | VIR_PCI_CONNECT_TYPE_PCIE;
1505 case VIR_DOMAIN_CONTROLLER_TYPE_USB:
1506 /* allow UHCI and EHCI controllers to be manually placed on
1507 * the PCIe bus (but don't put them there automatically)
1509 switch (device->data.controller->model) {
1510 case VIR_DOMAIN_CONTROLLER_MODEL_USB_EHCI:
1511 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_EHCI1:
1512 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI1:
1513 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI2:
1514 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI3:
1515 case VIR_DOMAIN_CONTROLLER_MODEL_USB_VT82C686B_UHCI:
1516 flags = (VIR_PCI_CONNECT_TYPE_PCI |
1517 VIR_PCI_CONNECT_TYPE_EITHER_IF_CONFIG);
1519 case VIR_DOMAIN_CONTROLLER_MODEL_USB_NEC_XHCI:
1520 /* should this be PCIE-only? Or do we need to allow PCI
1521 * for backward compatibility?
1523 flags = VIR_PCI_CONNECT_TYPE_PCI | VIR_PCI_CONNECT_TYPE_PCIE;
1525 case VIR_DOMAIN_CONTROLLER_MODEL_USB_PCI_OHCI:
1526 case VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI:
1527 case VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX4_UHCI:
1528 /* Allow these for PCI only */
1534 case VIR_DOMAIN_DEVICE_SOUND:
1535 switch (device->data.sound->model) {
1536 case VIR_DOMAIN_SOUND_MODEL_ICH6:
1537 case VIR_DOMAIN_SOUND_MODEL_ICH9:
1538 flags = (VIR_PCI_CONNECT_TYPE_PCI |
1539 VIR_PCI_CONNECT_TYPE_EITHER_IF_CONFIG);
1544 case VIR_DOMAIN_DEVICE_VIDEO:
1545 /* video cards aren't hot-plugged, and can be put in either a
1548 flags = VIR_PCI_CONNECT_TYPE_PCI | VIR_PCI_CONNECT_TYPE_PCIE;
1552 /* Ignore implicit controllers on slot 0:0:1.0:
1553 * implicit IDE controller on 0:0:1.1 (no qemu command line)
1554 * implicit USB controller on 0:0:1.2 (-usb)
1556 * If the machine does have a PCI bus, they will get reserved
1557 * in qemuAssignDevicePCISlots().
1560 /* These are the IDE and USB controllers in the PIIX3, hardcoded
1561 * to bus 0 slot 1. They cannot be attached to a PCIe slot, only
1564 if (device->type == VIR_DOMAIN_DEVICE_CONTROLLER && addr->domain == 0 &&
1565 addr->bus == 0 && addr->slot == 1) {
1566 virDomainControllerDefPtr cont = device->data.controller;
1568 if ((cont->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE && cont->idx == 0 &&
1569 addr->function == 1) ||
1570 (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_USB && cont->idx == 0 &&
1571 (cont->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI ||
1572 cont->model == -1) && addr->function == 2)) {
1573 /* Note the check for nbuses > 0 - if there are no PCI
1574 * buses, we skip this check. This is a quirk required for
1575 * some machinetypes such as s390, which pretend to have a
1576 * PCI bus for long enough to generate the "-usb" on the
1577 * commandline, but that don't really care if a PCI bus
1578 * actually exists. */
1579 if (addrs->nbuses > 0 &&
1580 !(addrs->buses[0].flags & VIR_PCI_CONNECT_TYPE_PCI)) {
1581 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1582 _("Bus 0 must be PCI for integrated PIIX3 "
1583 "USB or IDE controllers"));
1591 entireSlot = (addr->function == 0 &&
1592 addr->multi != VIR_DEVICE_ADDRESS_PCI_MULTI_ON);
1594 if (virDomainPCIAddressReserveAddr(addrs, addr, flags,
1595 entireSlot, true) < 0)
1604 qemuDomainSupportsPCI(virDomainDefPtr def)
1606 if ((def->os.arch != VIR_ARCH_ARMV7L) && (def->os.arch != VIR_ARCH_AARCH64))
1609 if (STREQ(def->os.machine, "versatilepb"))
1616 qemuDomainAssignPCIAddresses(virDomainDefPtr def,
1617 virQEMUCapsPtr qemuCaps,
1618 virDomainObjPtr obj)
1621 virDomainPCIAddressSetPtr addrs = NULL;
1622 qemuDomainObjPrivatePtr priv = NULL;
1624 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
1629 virDomainPCIConnectFlags flags = VIR_PCI_CONNECT_TYPE_PCI;
1631 for (i = 0; i < def->ncontrollers; i++) {
1632 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
1633 if ((int) def->controllers[i]->idx > max_idx)
1634 max_idx = def->controllers[i]->idx;
1638 nbuses = max_idx + 1;
1641 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_PCI_BRIDGE)) {
1642 virDomainDeviceInfo info;
1644 /* 1st pass to figure out how many PCI bridges we need */
1645 if (!(addrs = qemuDomainPCIAddressSetCreate(def, nbuses, true)))
1647 if (qemuAssignDevicePCISlots(def, qemuCaps, addrs) < 0)
1649 /* Reserve 1 extra slot for a (potential) bridge */
1650 if (virDomainPCIAddressReserveNextSlot(addrs, &info, flags) < 0)
1653 for (i = 1; i < addrs->nbuses; i++) {
1654 virDomainPCIAddressBusPtr bus = &addrs->buses[i];
1656 if ((rv = virDomainDefMaybeAddController(
1657 def, VIR_DOMAIN_CONTROLLER_TYPE_PCI,
1658 i, bus->model)) < 0)
1660 /* If we added a new bridge, we will need one more address */
1661 if (rv > 0 && virDomainPCIAddressReserveNextSlot(addrs, &info,
1665 nbuses = addrs->nbuses;
1666 virDomainPCIAddressSetFree(addrs);
1669 } else if (max_idx > 0) {
1670 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1671 _("PCI bridges are not supported "
1672 "by this QEMU binary"));
1676 if (!(addrs = qemuDomainPCIAddressSetCreate(def, nbuses, false)))
1679 if (qemuDomainSupportsPCI(def)) {
1680 if (qemuAssignDevicePCISlots(def, qemuCaps, addrs) < 0)
1685 if (obj && obj->privateData) {
1686 priv = obj->privateData;
1688 /* if this is the live domain object, we persist the PCI addresses*/
1689 virDomainPCIAddressSetFree(priv->pciaddrs);
1690 priv->persistentAddrs = 1;
1691 priv->pciaddrs = addrs;
1694 priv->persistentAddrs = 0;
1701 virDomainPCIAddressSetFree(addrs);
1706 int qemuDomainAssignAddresses(virDomainDefPtr def,
1707 virQEMUCapsPtr qemuCaps,
1708 virDomainObjPtr obj)
1712 rc = qemuDomainAssignSpaprVIOAddresses(def, qemuCaps);
1716 rc = qemuDomainAssignS390Addresses(def, qemuCaps, obj);
1720 rc = qemuDomainAssignARMVirtioMMIOAddresses(def, qemuCaps);
1724 return qemuDomainAssignPCIAddresses(def, qemuCaps, obj);
1728 virDomainPCIAddressSetPtr
1729 qemuDomainPCIAddressSetCreate(virDomainDefPtr def,
1730 unsigned int nbuses,
1733 virDomainPCIAddressSetPtr addrs;
1736 if ((addrs = virDomainPCIAddressSetAlloc(nbuses)) == NULL)
1739 addrs->nbuses = nbuses;
1740 addrs->dryRun = dryRun;
1742 /* As a safety measure, set default model='pci-root' for first pci
1743 * controller and 'pci-bridge' for all subsequent. After setting
1744 * those defaults, then scan the config and set the actual model
1745 * for all addrs[idx]->bus that already have a corresponding
1746 * controller in the config.
1750 virDomainPCIAddressBusSetModel(&addrs->buses[0],
1751 VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT);
1752 for (i = 1; i < nbuses; i++) {
1753 virDomainPCIAddressBusSetModel(&addrs->buses[i],
1754 VIR_DOMAIN_CONTROLLER_MODEL_PCI_BRIDGE);
1757 for (i = 0; i < def->ncontrollers; i++) {
1758 size_t idx = def->controllers[i]->idx;
1760 if (def->controllers[i]->type != VIR_DOMAIN_CONTROLLER_TYPE_PCI)
1763 if (idx >= addrs->nbuses) {
1764 virReportError(VIR_ERR_INTERNAL_ERROR,
1765 _("Inappropriate new pci controller index %zu "
1766 "not found in addrs"), idx);
1770 if (virDomainPCIAddressBusSetModel(&addrs->buses[idx],
1771 def->controllers[i]->model) < 0)
1775 if (virDomainDeviceInfoIterate(def, qemuCollectPCIAddress, addrs) < 0)
1781 virDomainPCIAddressSetFree(addrs);
1787 qemuDomainReleaseDeviceAddress(virDomainObjPtr vm,
1788 virDomainDeviceInfoPtr info,
1791 qemuDomainObjPrivatePtr priv = vm->privateData;
1794 devstr = info->alias;
1796 if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW &&
1797 STREQLEN(vm->def->os.machine, "s390-ccw", 8) &&
1798 virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_VIRTIO_CCW) &&
1799 qemuDomainCCWAddressReleaseAddr(priv->ccwaddrs, info) < 0)
1800 VIR_WARN("Unable to release CCW address on %s",
1802 else if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI &&
1803 virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) &&
1804 virDomainPCIAddressReleaseSlot(priv->pciaddrs,
1805 &info->addr.pci) < 0)
1806 VIR_WARN("Unable to release PCI address on %s",
1811 #define IS_USB2_CONTROLLER(ctrl) \
1812 (((ctrl)->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) && \
1813 ((ctrl)->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_EHCI1 || \
1814 (ctrl)->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI1 || \
1815 (ctrl)->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI2 || \
1816 (ctrl)->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI3))
1820 qemuValidateDevicePCISlotsPIIX3(virDomainDefPtr def,
1821 virQEMUCapsPtr qemuCaps,
1822 virDomainPCIAddressSetPtr addrs)
1826 virDevicePCIAddress tmp_addr;
1827 bool qemuDeviceVideoUsable = virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VIDEO_PRIMARY);
1828 char *addrStr = NULL;
1829 virDomainPCIConnectFlags flags = VIR_PCI_CONNECT_HOTPLUGGABLE | VIR_PCI_CONNECT_TYPE_PCI;
1831 /* Verify that first IDE and USB controllers (if any) is on the PIIX3, fn 1 */
1832 for (i = 0; i < def->ncontrollers; i++) {
1833 /* First IDE controller lives on the PIIX3 at slot=1, function=1 */
1834 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE &&
1835 def->controllers[i]->idx == 0) {
1836 if (def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1837 if (def->controllers[i]->info.addr.pci.domain != 0 ||
1838 def->controllers[i]->info.addr.pci.bus != 0 ||
1839 def->controllers[i]->info.addr.pci.slot != 1 ||
1840 def->controllers[i]->info.addr.pci.function != 1) {
1841 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1842 _("Primary IDE controller must have PCI address 0:0:1.1"));
1846 def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1847 def->controllers[i]->info.addr.pci.domain = 0;
1848 def->controllers[i]->info.addr.pci.bus = 0;
1849 def->controllers[i]->info.addr.pci.slot = 1;
1850 def->controllers[i]->info.addr.pci.function = 1;
1852 } else if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
1853 def->controllers[i]->idx == 0 &&
1854 (def->controllers[i]->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI ||
1855 def->controllers[i]->model == -1)) {
1856 if (def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1857 if (def->controllers[i]->info.addr.pci.domain != 0 ||
1858 def->controllers[i]->info.addr.pci.bus != 0 ||
1859 def->controllers[i]->info.addr.pci.slot != 1 ||
1860 def->controllers[i]->info.addr.pci.function != 2) {
1861 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1862 _("PIIX3 USB controller must have PCI address 0:0:1.2"));
1866 def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1867 def->controllers[i]->info.addr.pci.domain = 0;
1868 def->controllers[i]->info.addr.pci.bus = 0;
1869 def->controllers[i]->info.addr.pci.slot = 1;
1870 def->controllers[i]->info.addr.pci.function = 2;
1875 /* PIIX3 (ISA bridge, IDE controller, something else unknown, USB controller)
1876 * hardcoded slot=1, multifunction device
1878 if (addrs->nbuses) {
1879 memset(&tmp_addr, 0, sizeof(tmp_addr));
1881 if (virDomainPCIAddressReserveSlot(addrs, &tmp_addr, flags) < 0)
1885 if (def->nvideos > 0) {
1886 /* Because the PIIX3 integrated IDE/USB controllers are
1887 * already at slot 1, when qemu looks for the first free slot
1888 * to place the VGA controller (which is always the first
1889 * device added after integrated devices), it *always* ends up
1892 virDomainVideoDefPtr primaryVideo = def->videos[0];
1893 if (primaryVideo->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1894 memset(&tmp_addr, 0, sizeof(tmp_addr));
1897 if (!(addrStr = virDomainPCIAddressAsString(&tmp_addr)))
1899 if (!virDomainPCIAddressValidate(addrs, &tmp_addr,
1900 addrStr, flags, false))
1903 if (virDomainPCIAddressSlotInUse(addrs, &tmp_addr)) {
1904 if (qemuDeviceVideoUsable) {
1905 if (virDomainPCIAddressReserveNextSlot(addrs,
1906 &primaryVideo->info,
1910 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1911 _("PCI address 0:0:2.0 is in use, "
1912 "QEMU needs it for primary video"));
1916 if (virDomainPCIAddressReserveSlot(addrs, &tmp_addr, flags) < 0)
1918 primaryVideo->info.addr.pci = tmp_addr;
1919 primaryVideo->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1921 } else if (!qemuDeviceVideoUsable) {
1922 if (primaryVideo->info.addr.pci.domain != 0 ||
1923 primaryVideo->info.addr.pci.bus != 0 ||
1924 primaryVideo->info.addr.pci.slot != 2 ||
1925 primaryVideo->info.addr.pci.function != 0) {
1926 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1927 _("Primary video card must have PCI address 0:0:2.0"));
1930 /* If TYPE == PCI, then qemuCollectPCIAddress() function
1931 * has already reserved the address, so we must skip */
1933 } else if (addrs->nbuses && !qemuDeviceVideoUsable) {
1934 memset(&tmp_addr, 0, sizeof(tmp_addr));
1937 if (virDomainPCIAddressSlotInUse(addrs, &tmp_addr)) {
1938 VIR_DEBUG("PCI address 0:0:2.0 in use, future addition of a video"
1939 " device will not be possible without manual"
1941 virResetLastError();
1942 } else if (virDomainPCIAddressReserveSlot(addrs, &tmp_addr, flags) < 0) {
1954 qemuDomainMachineIsQ35(virDomainDefPtr def)
1956 return (STRPREFIX(def->os.machine, "pc-q35") ||
1957 STREQ(def->os.machine, "q35"));
1962 qemuDomainMachineIsI440FX(virDomainDefPtr def)
1964 return (STREQ(def->os.machine, "pc") ||
1965 STRPREFIX(def->os.machine, "pc-0.") ||
1966 STRPREFIX(def->os.machine, "pc-1.") ||
1967 STRPREFIX(def->os.machine, "pc-i440") ||
1968 STRPREFIX(def->os.machine, "rhel"));
1973 qemuDomainValidateDevicePCISlotsQ35(virDomainDefPtr def,
1974 virQEMUCapsPtr qemuCaps,
1975 virDomainPCIAddressSetPtr addrs)
1979 virDevicePCIAddress tmp_addr;
1980 bool qemuDeviceVideoUsable = virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VIDEO_PRIMARY);
1981 char *addrStr = NULL;
1982 virDomainPCIConnectFlags flags = VIR_PCI_CONNECT_TYPE_PCIE;
1984 for (i = 0; i < def->ncontrollers; i++) {
1985 switch (def->controllers[i]->type) {
1986 case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
1987 /* Verify that the first SATA controller is at 00:1F.2 the
1988 * q35 machine type *always* has a SATA controller at this
1991 if (def->controllers[i]->idx == 0) {
1992 if (def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1993 if (def->controllers[i]->info.addr.pci.domain != 0 ||
1994 def->controllers[i]->info.addr.pci.bus != 0 ||
1995 def->controllers[i]->info.addr.pci.slot != 0x1F ||
1996 def->controllers[i]->info.addr.pci.function != 2) {
1997 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1998 _("Primary SATA controller must have PCI address 0:0:1f.2"));
2002 def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
2003 def->controllers[i]->info.addr.pci.domain = 0;
2004 def->controllers[i]->info.addr.pci.bus = 0;
2005 def->controllers[i]->info.addr.pci.slot = 0x1F;
2006 def->controllers[i]->info.addr.pci.function = 2;
2011 case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
2012 if (def->controllers[i]->model == VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE &&
2013 def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
2014 /* Try to assign this bridge to 00:1E.0 (because that
2015 * is its standard location on real hardware) unless
2016 * it's already taken, but don't insist on it.
2018 memset(&tmp_addr, 0, sizeof(tmp_addr));
2019 tmp_addr.slot = 0x1E;
2020 if (!virDomainPCIAddressSlotInUse(addrs, &tmp_addr)) {
2021 if (virDomainPCIAddressReserveAddr(addrs, &tmp_addr,
2022 flags, true, false) < 0)
2024 def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
2025 def->controllers[i]->info.addr.pci.domain = 0;
2026 def->controllers[i]->info.addr.pci.bus = 0;
2027 def->controllers[i]->info.addr.pci.slot = 0x1E;
2028 def->controllers[i]->info.addr.pci.function = 0;
2035 /* Reserve slot 0x1F function 0 (ISA bridge, not in config model)
2036 * and function 3 (SMBus, also not (yet) in config model). As with
2037 * the SATA controller, these devices are always present in a q35
2038 * machine; there is no way to not have them.
2040 if (addrs->nbuses) {
2041 memset(&tmp_addr, 0, sizeof(tmp_addr));
2042 tmp_addr.slot = 0x1F;
2043 tmp_addr.function = 0;
2045 if (virDomainPCIAddressReserveAddr(addrs, &tmp_addr, flags,
2048 tmp_addr.function = 3;
2050 if (virDomainPCIAddressReserveAddr(addrs, &tmp_addr, flags,
2055 if (def->nvideos > 0) {
2056 /* NB: unlike the pc machinetypes, on q35 machinetypes the
2057 * integrated devices are at slot 0x1f, so when qemu looks for
2058 * the first free lot for the first VGA, it will always be at
2059 * slot 1 (which was used up by the integrated PIIX3 devices
2060 * on pc machinetypes).
2062 virDomainVideoDefPtr primaryVideo = def->videos[0];
2063 if (primaryVideo->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
2064 memset(&tmp_addr, 0, sizeof(tmp_addr));
2067 if (!(addrStr = virDomainPCIAddressAsString(&tmp_addr)))
2069 if (!virDomainPCIAddressValidate(addrs, &tmp_addr,
2070 addrStr, flags, false))
2073 if (virDomainPCIAddressSlotInUse(addrs, &tmp_addr)) {
2074 if (qemuDeviceVideoUsable) {
2075 if (virDomainPCIAddressReserveNextSlot(addrs,
2076 &primaryVideo->info,
2080 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2081 _("PCI address 0:0:1.0 is in use, "
2082 "QEMU needs it for primary video"));
2086 if (virDomainPCIAddressReserveSlot(addrs, &tmp_addr, flags) < 0)
2088 primaryVideo->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
2089 primaryVideo->info.addr.pci = tmp_addr;
2091 } else if (!qemuDeviceVideoUsable) {
2092 if (primaryVideo->info.addr.pci.domain != 0 ||
2093 primaryVideo->info.addr.pci.bus != 0 ||
2094 primaryVideo->info.addr.pci.slot != 1 ||
2095 primaryVideo->info.addr.pci.function != 0) {
2096 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2097 _("Primary video card must have PCI address 0:0:1.0"));
2100 /* If TYPE == PCI, then qemuCollectPCIAddress() function
2101 * has already reserved the address, so we must skip */
2103 } else if (addrs->nbuses && !qemuDeviceVideoUsable) {
2104 memset(&tmp_addr, 0, sizeof(tmp_addr));
2107 if (virDomainPCIAddressSlotInUse(addrs, &tmp_addr)) {
2108 VIR_DEBUG("PCI address 0:0:1.0 in use, future addition of a video"
2109 " device will not be possible without manual"
2111 virResetLastError();
2112 } else if (virDomainPCIAddressReserveSlot(addrs, &tmp_addr, flags) < 0) {
2124 * This assigns static PCI slots to all configured devices.
2125 * The ordering here is chosen to match the ordering used
2126 * with old QEMU < 0.12, so that if a user updates a QEMU
2127 * host from old QEMU to QEMU >= 0.12, their guests should
2128 * get PCI addresses in the same order as before.
2130 * NB, if they previously hotplugged devices then all bets
2131 * are off. Hotplug for old QEMU was unfixably broken wrt
2132 * to stable PCI addressing.
2136 * - Host bridge (slot 0)
2137 * - PIIX3 ISA bridge, IDE controller, something else unknown, USB controller (slot 1)
2140 * Incrementally assign slots from 3 onwards:
2144 * - SCSI controllers
2147 * - Host device passthrough
2148 * - Watchdog (not IB700)
2150 * Prior to this function being invoked, qemuCollectPCIAddress() will have
2151 * added all existing PCI addresses from the 'def' to 'addrs'. Thus this
2152 * function must only try to reserve addresses if info.type == NONE and
2153 * skip over info.type == PCI
2156 qemuAssignDevicePCISlots(virDomainDefPtr def,
2157 virQEMUCapsPtr qemuCaps,
2158 virDomainPCIAddressSetPtr addrs)
2161 virDomainPCIConnectFlags flags;
2162 virDevicePCIAddress tmp_addr;
2164 if ((STRPREFIX(def->os.machine, "pc-0.") ||
2165 STRPREFIX(def->os.machine, "pc-1.") ||
2166 STRPREFIX(def->os.machine, "pc-i440") ||
2167 STREQ(def->os.machine, "pc") ||
2168 STRPREFIX(def->os.machine, "rhel")) &&
2169 qemuValidateDevicePCISlotsPIIX3(def, qemuCaps, addrs) < 0) {
2173 if (qemuDomainMachineIsQ35(def) &&
2174 qemuDomainValidateDevicePCISlotsQ35(def, qemuCaps, addrs) < 0) {
2178 /* PCI controllers */
2179 for (i = 0; i < def->ncontrollers; i++) {
2180 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
2181 if (def->controllers[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
2183 switch (def->controllers[i]->model) {
2184 case VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT:
2185 case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT:
2186 /* pci-root and pcie-root are implicit in the machine,
2187 * and needs no address */
2189 case VIR_DOMAIN_CONTROLLER_MODEL_PCI_BRIDGE:
2190 /* pci-bridge doesn't require hot-plug
2191 * (although it does provide hot-plug in its slots)
2193 flags = VIR_PCI_CONNECT_TYPE_PCI;
2195 case VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE:
2196 /* dmi-to-pci-bridge requires a non-hotplug PCIe
2199 flags = VIR_PCI_CONNECT_TYPE_PCIE;
2202 flags = VIR_PCI_CONNECT_HOTPLUGGABLE | VIR_PCI_CONNECT_TYPE_PCI;
2205 if (virDomainPCIAddressReserveNextSlot(addrs,
2206 &def->controllers[i]->info,
2212 flags = VIR_PCI_CONNECT_HOTPLUGGABLE | VIR_PCI_CONNECT_TYPE_PCI;
2214 for (i = 0; i < def->nfss; i++) {
2215 if (def->fss[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
2218 /* Only support VirtIO-9p-pci so far. If that changes,
2219 * we might need to skip devices here */
2220 if (virDomainPCIAddressReserveNextSlot(addrs, &def->fss[i]->info,
2225 /* Network interfaces */
2226 for (i = 0; i < def->nnets; i++) {
2227 /* type='hostdev' network devices might be USB, and are also
2228 * in hostdevs list anyway, so handle them with other hostdevs
2231 if ((def->nets[i]->type == VIR_DOMAIN_NET_TYPE_HOSTDEV) ||
2232 (def->nets[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)) {
2235 if (virDomainPCIAddressReserveNextSlot(addrs, &def->nets[i]->info,
2241 for (i = 0; i < def->nsounds; i++) {
2242 if (def->sounds[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
2244 /* Skip ISA sound card, and PCSPK */
2245 if (def->sounds[i]->model == VIR_DOMAIN_SOUND_MODEL_SB16 ||
2246 def->sounds[i]->model == VIR_DOMAIN_SOUND_MODEL_PCSPK)
2249 if (virDomainPCIAddressReserveNextSlot(addrs, &def->sounds[i]->info,
2254 /* Device controllers (SCSI, USB, but not IDE, FDC or CCID) */
2255 for (i = 0; i < def->ncontrollers; i++) {
2256 /* PCI controllers have been dealt with earlier */
2257 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI)
2260 /* USB controller model 'none' doesn't need a PCI address */
2261 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
2262 def->controllers[i]->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_NONE)
2265 /* FDC lives behind the ISA bridge; CCID is a usb device */
2266 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_FDC ||
2267 def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_CCID)
2270 /* First IDE controller lives on the PIIX3 at slot=1, function=1,
2271 dealt with earlier on*/
2272 if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE &&
2273 def->controllers[i]->idx == 0)
2276 if (def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO)
2278 if (def->controllers[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
2281 /* USB2 needs special handling to put all companions in the same slot */
2282 if (IS_USB2_CONTROLLER(def->controllers[i])) {
2283 virDevicePCIAddress addr = { 0, 0, 0, 0, false };
2284 memset(&tmp_addr, 0, sizeof(tmp_addr));
2285 for (j = 0; j < i; j++) {
2286 if (IS_USB2_CONTROLLER(def->controllers[j]) &&
2287 def->controllers[j]->idx == def->controllers[i]->idx) {
2288 addr = def->controllers[j]->info.addr.pci;
2293 switch (def->controllers[i]->model) {
2294 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_EHCI1:
2297 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI1:
2299 addr.multi = VIR_DEVICE_ADDRESS_PCI_MULTI_ON;
2301 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI2:
2304 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI3:
2309 if (addr.slot == 0) {
2310 /* This is the first part of the controller, so need
2311 * to find a free slot & then reserve a function */
2312 if (virDomainPCIAddressGetNextSlot(addrs, &tmp_addr, flags) < 0)
2315 addr.bus = tmp_addr.bus;
2316 addr.slot = tmp_addr.slot;
2318 addrs->lastaddr = addr;
2319 addrs->lastaddr.function = 0;
2320 addrs->lastaddr.multi = 0;
2322 /* Finally we can reserve the slot+function */
2323 if (virDomainPCIAddressReserveAddr(addrs, &addr, flags,
2327 def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
2328 def->controllers[i]->info.addr.pci = addr;
2330 if (virDomainPCIAddressReserveNextSlot(addrs,
2331 &def->controllers[i]->info,
2337 /* Disks (VirtIO only for now) */
2338 for (i = 0; i < def->ndisks; i++) {
2339 /* Only VirtIO disks use PCI addrs */
2340 if (def->disks[i]->bus != VIR_DOMAIN_DISK_BUS_VIRTIO)
2343 /* don't touch s390 devices */
2344 if (def->disks[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI ||
2345 def->disks[i]->info.type ==
2346 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390 ||
2347 def->disks[i]->info.type ==
2348 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW)
2351 if (def->disks[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
2352 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2353 _("virtio only support device address type 'PCI'"));
2357 if (virDomainPCIAddressReserveNextSlot(addrs, &def->disks[i]->info,
2362 /* Host PCI devices */
2363 for (i = 0; i < def->nhostdevs; i++) {
2364 if (def->hostdevs[i]->info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
2366 if (def->hostdevs[i]->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
2367 def->hostdevs[i]->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
2370 if (virDomainPCIAddressReserveNextSlot(addrs,
2371 def->hostdevs[i]->info,
2376 /* VirtIO balloon */
2377 if (def->memballoon &&
2378 def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO &&
2379 def->memballoon->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
2380 if (virDomainPCIAddressReserveNextSlot(addrs,
2381 &def->memballoon->info,
2388 def->rng->model == VIR_DOMAIN_RNG_MODEL_VIRTIO &&
2389 def->rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
2390 if (virDomainPCIAddressReserveNextSlot(addrs,
2391 &def->rng->info, flags) < 0)
2395 /* A watchdog - skip IB700, it is not a PCI device */
2396 if (def->watchdog &&
2397 def->watchdog->model != VIR_DOMAIN_WATCHDOG_MODEL_IB700 &&
2398 def->watchdog->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
2399 if (virDomainPCIAddressReserveNextSlot(addrs, &def->watchdog->info,
2404 /* Assign a PCI slot to the primary video card if there is not an
2405 * assigned address. */
2406 if (def->nvideos > 0 &&
2407 def->videos[0]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
2408 if (virDomainPCIAddressReserveNextSlot(addrs, &def->videos[0]->info,
2412 /* Further non-primary video cards which have to be qxl type */
2413 for (i = 1; i < def->nvideos; i++) {
2414 if (def->videos[i]->type != VIR_DOMAIN_VIDEO_TYPE_QXL) {
2415 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2416 _("non-primary video device must be type of 'qxl'"));
2419 if (def->videos[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
2421 if (virDomainPCIAddressReserveNextSlot(addrs, &def->videos[i]->info,
2425 for (i = 0; i < def->ninputs; i++) {
2426 /* Nada - none are PCI based (yet) */
2428 for (i = 0; i < def->nparallels; i++) {
2429 /* Nada - none are PCI based (yet) */
2431 for (i = 0; i < def->nserials; i++) {
2432 /* Nada - none are PCI based (yet) */
2434 for (i = 0; i < def->nchannels; i++) {
2435 /* Nada - none are PCI based (yet) */
2437 for (i = 0; i < def->nhubs; i++) {
2438 /* Nada - none are PCI based (yet) */
2448 qemuUSBId(virBufferPtr buf, int idx)
2451 virBufferAddLit(buf, "usb");
2453 virBufferAsprintf(buf, "usb%d", idx);
2457 qemuBuildDeviceAddressStr(virBufferPtr buf,
2458 virDomainDefPtr domainDef,
2459 virDomainDeviceInfoPtr info,
2460 virQEMUCapsPtr qemuCaps)
2463 char *devStr = NULL;
2465 if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
2466 const char *contAlias = NULL;
2469 if (!(devStr = virDomainPCIAddressAsString(&info->addr.pci)))
2471 for (i = 0; i < domainDef->ncontrollers; i++) {
2472 virDomainControllerDefPtr cont = domainDef->controllers[i];
2474 if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI &&
2475 cont->idx == info->addr.pci.bus) {
2476 contAlias = cont->info.alias;
2478 virReportError(VIR_ERR_INTERNAL_ERROR,
2479 _("Device alias was not set for PCI "
2480 "controller with index %u required "
2481 "for device at address %s"),
2482 info->addr.pci.bus, devStr);
2489 virReportError(VIR_ERR_INTERNAL_ERROR,
2490 _("Could not find PCI "
2491 "controller with index %u required "
2492 "for device at address %s"),
2493 info->addr.pci.bus, devStr);
2497 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_MULTIFUNCTION)) {
2498 if (info->addr.pci.function != 0) {
2499 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2500 _("Only PCI device addresses with function=0 "
2501 "are supported with this QEMU binary"));
2504 if (info->addr.pci.multi == VIR_DEVICE_ADDRESS_PCI_MULTI_ON) {
2505 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2506 _("'multifunction=on' is not supported with "
2507 "this QEMU binary"));
2513 * PCI bridge support is required for multiple buses
2514 * 'pci.%u' is the ID of the bridge as specified in
2515 * qemuBuildControllerDevStr
2517 * PCI_MULTIBUS capability indicates that the implicit
2518 * PCI bus is named 'pci.0' instead of 'pci'.
2520 if (info->addr.pci.bus != 0) {
2521 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_PCI_BRIDGE)) {
2522 virBufferAsprintf(buf, ",bus=%s", contAlias);
2524 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2525 _("Multiple PCI buses are not supported "
2526 "with this QEMU binary"));
2530 if (virQEMUCapsHasPCIMultiBus(qemuCaps, domainDef)) {
2531 virBufferAsprintf(buf, ",bus=%s", contAlias);
2533 virBufferAddLit(buf, ",bus=pci");
2536 if (info->addr.pci.multi == VIR_DEVICE_ADDRESS_PCI_MULTI_ON)
2537 virBufferAddLit(buf, ",multifunction=on");
2538 else if (info->addr.pci.multi == VIR_DEVICE_ADDRESS_PCI_MULTI_OFF)
2539 virBufferAddLit(buf, ",multifunction=off");
2540 virBufferAsprintf(buf, ",addr=0x%x", info->addr.pci.slot);
2541 if (info->addr.pci.function != 0)
2542 virBufferAsprintf(buf, ".0x%x", info->addr.pci.function);
2543 } else if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB) {
2544 virBufferAddLit(buf, ",bus=");
2545 qemuUSBId(buf, info->addr.usb.bus);
2546 virBufferAsprintf(buf, ".0,port=%s", info->addr.usb.port);
2547 } else if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO) {
2548 if (info->addr.spaprvio.has_reg)
2549 virBufferAsprintf(buf, ",reg=0x%llx", info->addr.spaprvio.reg);
2550 } else if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
2551 if (info->addr.ccw.assigned)
2552 virBufferAsprintf(buf, ",devno=%x.%x.%04x",
2553 info->addr.ccw.cssid,
2554 info->addr.ccw.ssid,
2555 info->addr.ccw.devno);
2565 qemuBuildRomStr(virBufferPtr buf,
2566 virDomainDeviceInfoPtr info,
2567 virQEMUCapsPtr qemuCaps)
2569 if (info->rombar || info->romfile) {
2570 if (info->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
2571 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2572 "%s", _("rombar and romfile are supported only for PCI devices"));
2575 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_ROMBAR)) {
2576 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2577 "%s", _("rombar and romfile not supported in this QEMU binary"));
2581 switch (info->rombar) {
2582 case VIR_DOMAIN_PCI_ROMBAR_OFF:
2583 virBufferAddLit(buf, ",rombar=0");
2585 case VIR_DOMAIN_PCI_ROMBAR_ON:
2586 virBufferAddLit(buf, ",rombar=1");
2592 virBufferAsprintf(buf, ",romfile=%s", info->romfile);
2598 qemuBuildIoEventFdStr(virBufferPtr buf,
2599 enum virDomainIoEventFd use,
2600 virQEMUCapsPtr qemuCaps)
2602 if (use && virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_IOEVENTFD))
2603 virBufferAsprintf(buf, ",ioeventfd=%s",
2604 virDomainIoEventFdTypeToString(use));
2608 #define QEMU_SERIAL_PARAM_ACCEPTED_CHARS \
2609 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
2612 qemuSafeSerialParamValue(const char *value)
2614 if (strspn(value, QEMU_SERIAL_PARAM_ACCEPTED_CHARS) != strlen(value)) {
2615 virReportError(VIR_ERR_INTERNAL_ERROR,
2616 _("driver serial '%s' contains unsafe characters"),
2625 qemuGetSecretString(virConnectPtr conn,
2630 unsigned char *uuid, char *usage,
2631 virSecretUsageType secretUsageType)
2634 virSecretPtr sec = NULL;
2635 char *secret = NULL;
2636 char uuidStr[VIR_UUID_STRING_BUFLEN];
2638 /* look up secret */
2639 switch (diskSecretType) {
2640 case VIR_STORAGE_SECRET_TYPE_UUID:
2641 sec = virSecretLookupByUUID(conn, uuid);
2642 virUUIDFormat(uuid, uuidStr);
2644 case VIR_STORAGE_SECRET_TYPE_USAGE:
2645 sec = virSecretLookupByUsage(conn, secretUsageType, usage);
2650 if (diskSecretType == VIR_STORAGE_SECRET_TYPE_UUID) {
2651 virReportError(VIR_ERR_NO_SECRET,
2652 _("%s no secret matches uuid '%s'"),
2655 virReportError(VIR_ERR_NO_SECRET,
2656 _("%s no secret matches usage value '%s'"),
2662 secret = (char *)conn->secretDriver->secretGetValue(sec, &secret_size, 0,
2663 VIR_SECRET_GET_VALUE_INTERNAL_CALL);
2665 if (diskSecretType == VIR_STORAGE_SECRET_TYPE_UUID) {
2666 virReportError(VIR_ERR_INTERNAL_ERROR,
2667 _("could not get value of the secret for "
2668 "username '%s' using uuid '%s'"),
2671 virReportError(VIR_ERR_INTERNAL_ERROR,
2672 _("could not get value of the secret for "
2673 "username '%s' using usage value '%s'"),
2680 char *base64 = NULL;
2682 base64_encode_alloc(secret, secret_size, &base64);
2685 virReportOOMError();
2692 virObjectUnref(sec);
2697 static int qemuAddRBDHost(virDomainDiskDefPtr disk, char *hostport)
2703 if (VIR_EXPAND_N(disk->src.hosts, disk->src.nhosts, 1) < 0)
2706 if ((port = strchr(hostport, ']'))) {
2707 /* ipv6, strip brackets */
2711 port = strstr(hostport, "\\:");
2718 if (VIR_STRDUP(disk->src.hosts[disk->src.nhosts - 1].port, port) < 0)
2721 if (VIR_STRDUP(disk->src.hosts[disk->src.nhosts - 1].port, "6789") < 0)
2725 parts = virStringSplit(hostport, "\\:", 0);
2728 disk->src.hosts[disk->src.nhosts-1].name = virStringJoin((const char **)parts, ":");
2729 virStringFreeList(parts);
2730 if (!disk->src.hosts[disk->src.nhosts-1].name)
2733 disk->src.hosts[disk->src.nhosts-1].transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
2734 disk->src.hosts[disk->src.nhosts-1].socket = NULL;
2739 VIR_FREE(disk->src.hosts[disk->src.nhosts-1].port);
2740 VIR_FREE(disk->src.hosts[disk->src.nhosts-1].name);
2744 /* disk->src initially has everything after the rbd: prefix */
2745 static int qemuParseRBDString(virDomainDiskDefPtr disk)
2747 char *options = NULL;
2750 p = strchr(disk->src.path, ':');
2752 if (VIR_STRDUP(options, p + 1) < 0)
2759 return 0; /* all done */
2763 /* find : delimiter or end of string */
2764 for (e = p; *e && *e != ':'; ++e) {
2772 next = e; /* last kv pair */
2778 if (STRPREFIX(p, "id=") &&
2779 VIR_STRDUP(disk->src.auth.username, p + strlen("id=")) < 0)
2781 if (STRPREFIX(p, "mon_host=")) {
2784 h = p + strlen("mon_host=");
2786 for (sep = h; sep < e; ++sep) {
2787 if (*sep == '\\' && (sep[1] == ',' ||
2795 if (qemuAddRBDHost(disk, h) < 0)
2813 qemuParseDriveURIString(virDomainDiskDefPtr def, virURIPtr uri,
2817 char *transp = NULL;
2819 char *volimg = NULL;
2820 char *secret = NULL;
2822 if (VIR_ALLOC(def->src.hosts) < 0)
2825 transp = strchr(uri->scheme, '+');
2829 if (!STREQ(uri->scheme, scheme)) {
2830 virReportError(VIR_ERR_INTERNAL_ERROR,
2831 _("Invalid transport/scheme '%s'"), uri->scheme);
2836 def->src.hosts->transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
2838 def->src.hosts->transport = virStorageNetHostTransportTypeFromString(transp);
2839 if (def->src.hosts->transport < 0) {
2840 virReportError(VIR_ERR_INTERNAL_ERROR,
2841 _("Invalid %s transport type '%s'"), scheme, transp);
2845 def->src.nhosts = 0; /* set to 1 once everything succeeds */
2847 if (def->src.hosts->transport != VIR_STORAGE_NET_HOST_TRANS_UNIX) {
2848 if (VIR_STRDUP(def->src.hosts->name, uri->server) < 0)
2851 if (virAsprintf(&def->src.hosts->port, "%d", uri->port) < 0)
2854 def->src.hosts->name = NULL;
2855 def->src.hosts->port = 0;
2857 if (STRPREFIX(uri->query, "socket=")) {
2858 sock = strchr(uri->query, '=') + 1;
2859 if (VIR_STRDUP(def->src.hosts->socket, sock) < 0)
2862 virReportError(VIR_ERR_INTERNAL_ERROR,
2863 _("Invalid query parameter '%s'"), uri->query);
2869 volimg = uri->path + 1; /* skip the prefix slash */
2870 VIR_FREE(def->src.path);
2871 if (VIR_STRDUP(def->src.path, volimg) < 0)
2874 VIR_FREE(def->src.path);
2878 secret = strchr(uri->user, ':');
2882 if (VIR_STRDUP(def->src.auth.username, uri->user) < 0)
2886 def->src.nhosts = 1;
2895 virStorageNetHostDefClear(def->src.hosts);
2896 VIR_FREE(def->src.hosts);
2901 qemuParseGlusterString(virDomainDiskDefPtr def)
2903 virURIPtr uri = NULL;
2905 if (!(uri = virURIParse(def->src.path)))
2908 return qemuParseDriveURIString(def, uri, "gluster");
2912 qemuParseISCSIString(virDomainDiskDefPtr def)
2914 virURIPtr uri = NULL;
2918 if (!(uri = virURIParse(def->src.path)))
2922 (slash = strchr(uri->path + 1, '/')) != NULL) {
2924 if (slash[1] == '\0')
2926 else if (virStrToLong_ui(slash + 1, NULL, 10, &lun) == -1) {
2927 virReportError(VIR_ERR_INTERNAL_ERROR,
2928 _("invalid name '%s' for iSCSI disk"),
2934 return qemuParseDriveURIString(def, uri, "iscsi");
2938 qemuParseNBDString(virDomainDiskDefPtr disk)
2940 virStorageNetHostDefPtr h = NULL;
2944 virURIPtr uri = NULL;
2946 if (strstr(disk->src.path, "://")) {
2947 if (!(uri = virURIParse(disk->src.path)))
2949 return qemuParseDriveURIString(disk, uri, "nbd");
2952 if (VIR_ALLOC(h) < 0)
2955 host = disk->src.path + strlen("nbd:");
2956 if (STRPREFIX(host, "unix:/")) {
2957 src = strchr(host + strlen("unix:"), ':');
2961 h->transport = VIR_STORAGE_NET_HOST_TRANS_UNIX;
2962 if (VIR_STRDUP(h->socket, host + strlen("unix:")) < 0)
2965 port = strchr(host, ':');
2967 virReportError(VIR_ERR_INTERNAL_ERROR,
2968 _("cannot parse nbd filename '%s'"), disk->src.path);
2973 if (VIR_STRDUP(h->name, host) < 0)
2976 src = strchr(port, ':');
2980 if (VIR_STRDUP(h->port, port) < 0)
2984 if (src && STRPREFIX(src, "exportname=")) {
2985 if (VIR_STRDUP(src, strchr(src, '=') + 1) < 0)
2991 VIR_FREE(disk->src.path);
2992 disk->src.path = src;
2993 disk->src.nhosts = 1;
2994 disk->src.hosts = h;
2998 virStorageNetHostDefClear(h);
3005 qemuNetworkDriveGetPort(int protocol,
3011 if (virStrToLong_i(port, NULL, 10, &ret) < 0) {
3012 virReportError(VIR_ERR_INTERNAL_ERROR,
3013 _("failed to parse port number '%s'"),
3021 switch ((virStorageNetProtocol) protocol) {
3022 case VIR_STORAGE_NET_PROTOCOL_HTTP:
3025 case VIR_STORAGE_NET_PROTOCOL_HTTPS:
3028 case VIR_STORAGE_NET_PROTOCOL_FTP:
3031 case VIR_STORAGE_NET_PROTOCOL_FTPS:
3034 case VIR_STORAGE_NET_PROTOCOL_TFTP:
3037 case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
3040 case VIR_STORAGE_NET_PROTOCOL_NBD:
3043 case VIR_STORAGE_NET_PROTOCOL_ISCSI:
3044 case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
3045 /* no default port specified */
3048 case VIR_STORAGE_NET_PROTOCOL_RBD:
3049 case VIR_STORAGE_NET_PROTOCOL_LAST:
3050 case VIR_STORAGE_NET_PROTOCOL_NONE:
3051 /* not applicable */
3058 #define QEMU_DEFAULT_NBD_PORT "10809"
3061 qemuBuildNetworkDriveURI(int protocol,
3065 virStorageNetHostDefPtr hosts,
3066 const char *username,
3070 virBuffer buf = VIR_BUFFER_INITIALIZER;
3071 virURIPtr uri = NULL;
3074 switch ((virStorageNetProtocol) protocol) {
3075 case VIR_STORAGE_NET_PROTOCOL_NBD:
3077 virReportError(VIR_ERR_INTERNAL_ERROR,
3078 _("protocol '%s' accepts only one host"),
3079 virStorageNetProtocolTypeToString(protocol));
3083 if (!((hosts->name && strchr(hosts->name, ':')) ||
3084 (hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP &&
3086 (hosts->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX &&
3088 hosts->socket[0] != '/'))) {
3090 virBufferAddLit(&buf, "nbd:");
3092 switch (hosts->transport) {
3093 case VIR_STORAGE_NET_HOST_TRANS_TCP:
3094 virBufferStrcat(&buf, hosts->name, NULL);
3095 virBufferAsprintf(&buf, ":%s",
3096 hosts->port ? hosts->port :
3097 QEMU_DEFAULT_NBD_PORT);
3100 case VIR_STORAGE_NET_HOST_TRANS_UNIX:
3101 if (!hosts->socket) {
3102 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3103 _("socket attribute required for "
3108 virBufferAsprintf(&buf, "unix:%s", hosts->socket);
3112 virReportError(VIR_ERR_INTERNAL_ERROR,
3113 _("nbd does not support transport '%s'"),
3114 virStorageNetHostTransportTypeToString(hosts->transport));
3119 virBufferAsprintf(&buf, ":exportname=%s", src);
3121 if (virBufferError(&buf) < 0) {
3122 virReportOOMError();
3126 ret = virBufferContentAndReset(&buf);
3130 /* NBD code uses same formatting scheme as others in some cases */
3132 case VIR_STORAGE_NET_PROTOCOL_HTTP:
3133 case VIR_STORAGE_NET_PROTOCOL_HTTPS:
3134 case VIR_STORAGE_NET_PROTOCOL_FTP:
3135 case VIR_STORAGE_NET_PROTOCOL_FTPS:
3136 case VIR_STORAGE_NET_PROTOCOL_TFTP:
3137 case VIR_STORAGE_NET_PROTOCOL_ISCSI:
3138 case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
3140 virReportError(VIR_ERR_INTERNAL_ERROR,
3141 _("protocol '%s' accepts only one host"),
3142 virStorageNetProtocolTypeToString(protocol));
3146 if (VIR_ALLOC(uri) < 0)
3149 if (hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
3150 if (VIR_STRDUP(uri->scheme,
3151 virStorageNetProtocolTypeToString(protocol)) < 0)
3154 if (virAsprintf(&uri->scheme, "%s+%s",
3155 virStorageNetProtocolTypeToString(protocol),
3156 virStorageNetHostTransportTypeToString(hosts->transport)) < 0)
3160 if ((uri->port = qemuNetworkDriveGetPort(protocol, hosts->port)) < 0)
3165 if (virAsprintf(&uri->path, "/%s%s",
3169 if (virAsprintf(&uri->path, "%s%s",
3170 src[0] == '/' ? "" : "/",
3176 if (hosts->socket &&
3177 virAsprintf(&uri->query, "socket=%s", hosts->socket) < 0)
3182 if (virAsprintf(&uri->user, "%s:%s", username, secret) < 0)
3185 if (VIR_STRDUP(uri->user, username) < 0)
3190 if (VIR_STRDUP(uri->server, hosts->name) < 0)
3193 ret = virURIFormat(uri);
3197 case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
3199 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3200 _("missing disk source for 'sheepdog' protocol"));
3205 if (virAsprintf(&ret, "sheepdog:%s", src) < 0)
3207 } else if (nhosts == 1) {
3208 if (virAsprintf(&ret, "sheepdog:%s:%s:%s",
3210 hosts->port ? hosts->port : "7000",
3214 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3215 _("protocol 'sheepdog' accepts up to one host"));
3221 case VIR_STORAGE_NET_PROTOCOL_RBD:
3222 if (strchr(src, ':')) {
3223 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3224 _("':' not allowed in RBD source volume name '%s'"),
3229 virBufferStrcat(&buf, "rbd:", src, NULL);
3232 virBufferEscape(&buf, '\\', ":", ":id=%s", username);
3233 virBufferEscape(&buf, '\\', ":",
3234 ":key=%s:auth_supported=cephx\\;none",
3237 virBufferAddLit(&buf, ":auth_supported=none");
3241 virBufferAddLit(&buf, ":mon_host=");
3242 for (i = 0; i < nhosts; i++) {
3244 virBufferAddLit(&buf, "\\;");
3246 /* assume host containing : is ipv6 */
3247 if (strchr(hosts[i].name, ':'))
3248 virBufferEscape(&buf, '\\', ":", "[%s]", hosts[i].name);
3250 virBufferAsprintf(&buf, "%s", hosts[i].name);
3253 virBufferAsprintf(&buf, "\\:%s", hosts[i].port);
3257 if (virBufferError(&buf) < 0) {
3258 virReportOOMError();
3262 ret = virBufferContentAndReset(&buf);
3266 case VIR_STORAGE_NET_PROTOCOL_LAST:
3267 case VIR_STORAGE_NET_PROTOCOL_NONE:
3272 virBufferFreeAndReset(&buf);
3280 qemuGetDriveSourceString(virStorageSourcePtr src,
3284 int actualType = virStorageSourceGetActualType(src);
3285 char *secret = NULL;
3286 char *username = NULL;
3292 if (actualType == VIR_STORAGE_TYPE_NETWORK &&
3293 src->auth.username &&
3294 (src->protocol == VIR_STORAGE_NET_PROTOCOL_ISCSI ||
3295 src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD)) {
3296 bool encode = false;
3297 int secretType = VIR_SECRET_USAGE_TYPE_ISCSI;
3298 const char *protocol = virStorageNetProtocolTypeToString(src->protocol);
3300 username = src->auth.username;
3302 if (src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD) {
3303 /* qemu requires the secret to be encoded for RBD */
3305 secretType = VIR_SECRET_USAGE_TYPE_CEPH;
3308 if (!(secret = qemuGetSecretString(conn,
3311 src->auth.secretType,
3313 src->auth.secret.uuid,
3314 src->auth.secret.usage,
3320 switch ((virStorageType) actualType) {
3321 case VIR_STORAGE_TYPE_BLOCK:
3322 case VIR_STORAGE_TYPE_FILE:
3323 case VIR_STORAGE_TYPE_DIR:
3329 if (VIR_STRDUP(*source, src->path) < 0)
3334 case VIR_STORAGE_TYPE_NETWORK:
3335 if (!(*source = qemuBuildNetworkDriveURI(src->protocol,
3345 case VIR_STORAGE_TYPE_VOLUME:
3346 case VIR_STORAGE_TYPE_NONE:
3347 case VIR_STORAGE_TYPE_LAST:
3360 qemuBuildDriveStr(virConnectPtr conn,
3361 virDomainDiskDefPtr disk,
3363 virQEMUCapsPtr qemuCaps)
3365 virBuffer opt = VIR_BUFFER_INITIALIZER;
3366 const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
3368 virDomainDiskGeometryTransTypeToString(disk->geometry.trans);
3369 int idx = virDiskNameToIndex(disk->dst);
3370 int busid = -1, unitid = -1;
3371 char *source = NULL;
3372 int actualType = virStorageSourceGetActualType(&disk->src);
3375 virReportError(VIR_ERR_INTERNAL_ERROR,
3376 _("unsupported disk type '%s'"), disk->dst);
3380 switch (disk->bus) {
3381 case VIR_DOMAIN_DISK_BUS_SCSI:
3382 if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
3383 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3384 _("unexpected address type for scsi disk"));
3388 /* Setting bus= attr for SCSI drives, causes a controller
3389 * to be created. Yes this is slightly odd. It is not possible
3390 * to have > 1 bus on a SCSI controller (yet). */
3391 if (disk->info.addr.drive.bus != 0) {
3392 virReportError(VIR_ERR_INTERNAL_ERROR,
3393 "%s", _("SCSI controller only supports 1 bus"));
3396 busid = disk->info.addr.drive.controller;
3397 unitid = disk->info.addr.drive.unit;
3400 case VIR_DOMAIN_DISK_BUS_IDE:
3401 if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
3402 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3403 _("unexpected address type for ide disk"));
3406 /* We can only have 1 IDE controller (currently) */
3407 if (disk->info.addr.drive.controller != 0) {
3408 virReportError(VIR_ERR_INTERNAL_ERROR,
3409 _("Only 1 %s controller is supported"), bus);
3412 busid = disk->info.addr.drive.bus;
3413 unitid = disk->info.addr.drive.unit;
3416 case VIR_DOMAIN_DISK_BUS_FDC:
3417 if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
3418 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3419 _("unexpected address type for fdc disk"));
3422 /* We can only have 1 FDC controller (currently) */
3423 if (disk->info.addr.drive.controller != 0) {
3424 virReportError(VIR_ERR_INTERNAL_ERROR,
3425 _("Only 1 %s controller is supported"), bus);
3428 /* We can only have 1 FDC bus (currently) */
3429 if (disk->info.addr.drive.bus != 0) {
3430 virReportError(VIR_ERR_INTERNAL_ERROR,
3431 _("Only 1 %s bus is supported"), bus);
3434 if (disk->info.addr.drive.target != 0) {
3435 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3436 _("target must be 0 for controller fdc"));
3439 unitid = disk->info.addr.drive.unit;
3443 case VIR_DOMAIN_DISK_BUS_VIRTIO:
3447 case VIR_DOMAIN_DISK_BUS_XEN:
3448 case VIR_DOMAIN_DISK_BUS_SD:
3449 /* Xen and SD have no address type currently, so assign
3454 if (qemuGetDriveSourceString(&disk->src, conn, &source) < 0)
3458 !((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
3459 disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
3460 disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
3462 virBufferAddLit(&opt, "file=");
3464 switch (actualType) {
3465 case VIR_STORAGE_TYPE_DIR:
3466 /* QEMU only supports magic FAT format for now */
3467 if (disk->src.format > 0 &&
3468 disk->src.format != VIR_STORAGE_FILE_FAT) {
3469 virReportError(VIR_ERR_INTERNAL_ERROR,
3470 _("unsupported disk driver type for '%s'"),
3471 virStorageFileFormatTypeToString(disk->src.format));
3475 if (!disk->readonly) {
3476 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3477 _("cannot create virtual FAT disks in read-write mode"));
3481 virBufferAddLit(&opt, "fat:");
3483 if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
3484 virBufferAddLit(&opt, "floppy:");
3488 case VIR_STORAGE_TYPE_BLOCK:
3489 if (disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN) {
3490 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3491 disk->src.type == VIR_STORAGE_TYPE_VOLUME ?
3492 _("tray status 'open' is invalid for block type volume") :
3493 _("tray status 'open' is invalid for block type disk"));
3503 virBufferEscape(&opt, ',', ",", "%s,", source);
3507 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
3508 virBufferAddLit(&opt, "if=none");
3510 virBufferAsprintf(&opt, "if=%s", bus);
3512 if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
3513 if (disk->bus == VIR_DOMAIN_DISK_BUS_SCSI) {
3514 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_CD))
3515 virBufferAddLit(&opt, ",media=cdrom");
3516 } else if (disk->bus == VIR_DOMAIN_DISK_BUS_IDE) {
3517 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_IDE_CD))
3518 virBufferAddLit(&opt, ",media=cdrom");
3520 virBufferAddLit(&opt, ",media=cdrom");
3524 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
3525 virBufferAsprintf(&opt, ",id=%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias);
3527 if (busid == -1 && unitid == -1) {
3529 virBufferAsprintf(&opt, ",index=%d", idx);
3532 virBufferAsprintf(&opt, ",bus=%d", busid);
3534 virBufferAsprintf(&opt, ",unit=%d", unitid);
3538 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_BOOT) &&
3539 (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK ||
3540 disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) &&
3541 disk->bus != VIR_DOMAIN_DISK_BUS_IDE)
3542 virBufferAddLit(&opt, ",boot=on");
3543 if (disk->readonly &&
3544 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_READONLY))
3545 virBufferAddLit(&opt, ",readonly=on");
3546 if (disk->transient) {
3547 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3548 _("transient disks not supported yet"));
3551 if (disk->src.format > 0 &&
3552 disk->src.type != VIR_STORAGE_TYPE_DIR &&
3553 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_FORMAT))
3554 virBufferAsprintf(&opt, ",format=%s",
3555 virStorageFileFormatTypeToString(disk->src.format));
3557 /* generate geometry command string */
3558 if (disk->geometry.cylinders > 0 &&
3559 disk->geometry.heads > 0 &&
3560 disk->geometry.sectors > 0) {
3562 virBufferAsprintf(&opt, ",cyls=%u,heads=%u,secs=%u",
3563 disk->geometry.cylinders,
3564 disk->geometry.heads,
3565 disk->geometry.sectors);
3567 if (disk->geometry.trans != VIR_DOMAIN_DISK_TRANS_DEFAULT)
3568 virBufferEscapeString(&opt, ",trans=%s", trans);
3572 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_SERIAL)) {
3573 if (qemuSafeSerialParamValue(disk->serial) < 0)
3575 virBufferAsprintf(&opt, ",serial=%s", disk->serial);
3578 if (disk->cachemode) {
3579 const char *mode = NULL;
3581 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_CACHE_V2)) {
3582 mode = qemuDiskCacheV2TypeToString(disk->cachemode);
3584 if (disk->cachemode == VIR_DOMAIN_DISK_CACHE_DIRECTSYNC &&
3585 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_CACHE_DIRECTSYNC)) {
3586 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3587 _("disk cache mode 'directsync' is not "
3588 "supported by this QEMU"));
3590 } else if (disk->cachemode == VIR_DOMAIN_DISK_CACHE_UNSAFE &&
3591 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_CACHE_UNSAFE)) {
3592 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3593 _("disk cache mode 'unsafe' is not "
3594 "supported by this QEMU"));
3598 mode = qemuDiskCacheV1TypeToString(disk->cachemode);
3601 virBufferAsprintf(&opt, ",cache=%s", mode);
3602 } else if (disk->shared && !disk->readonly) {
3603 virBufferAddLit(&opt, ",cache=off");
3606 if (disk->copy_on_read) {
3607 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_COPY_ON_READ)) {
3608 virBufferAsprintf(&opt, ",copy-on-read=%s",
3609 virDomainDiskCopyOnReadTypeToString(disk->copy_on_read));
3611 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3612 _("copy_on_read is not supported by this QEMU binary"));
3617 if (disk->discard) {
3618 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_DISCARD)) {
3619 virBufferAsprintf(&opt, ",discard=%s",
3620 virDomainDiskDiscardTypeToString(disk->discard));
3622 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3623 _("discard is not supported by this QEMU binary"));
3628 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MONITOR_JSON)) {
3629 const char *wpolicy = NULL, *rpolicy = NULL;
3631 if (disk->error_policy)
3632 wpolicy = virDomainDiskErrorPolicyTypeToString(disk->error_policy);
3633 if (disk->rerror_policy)
3634 rpolicy = virDomainDiskErrorPolicyTypeToString(disk->rerror_policy);
3636 if (disk->error_policy == VIR_DOMAIN_DISK_ERROR_POLICY_ENOSPACE) {
3637 /* in the case of enospace, the option is spelled
3638 * differently in qemu, and it's only valid for werror,
3639 * not for rerror, so leave leave rerror NULL.
3642 } else if (!rpolicy) {
3643 /* for other policies, rpolicy can match wpolicy */
3648 virBufferAsprintf(&opt, ",werror=%s", wpolicy);
3650 virBufferAsprintf(&opt, ",rerror=%s", rpolicy);
3654 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_AIO)) {
3655 virBufferAsprintf(&opt, ",aio=%s",
3656 virDomainDiskIoTypeToString(disk->iomode));
3658 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3659 _("disk aio mode not supported with this "
3665 /* block I/O throttling */
3666 if ((disk->blkdeviotune.total_bytes_sec ||
3667 disk->blkdeviotune.read_bytes_sec ||
3668 disk->blkdeviotune.write_bytes_sec ||
3669 disk->blkdeviotune.total_iops_sec ||
3670 disk->blkdeviotune.read_iops_sec ||
3671 disk->blkdeviotune.write_iops_sec) &&
3672 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_IOTUNE)) {
3673 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3674 _("block I/O throttling not supported with this "
3679 if (disk->blkdeviotune.total_bytes_sec) {
3680 virBufferAsprintf(&opt, ",bps=%llu",
3681 disk->blkdeviotune.total_bytes_sec);
3684 if (disk->blkdeviotune.read_bytes_sec) {
3685 virBufferAsprintf(&opt, ",bps_rd=%llu",
3686 disk->blkdeviotune.read_bytes_sec);
3689 if (disk->blkdeviotune.write_bytes_sec) {
3690 virBufferAsprintf(&opt, ",bps_wr=%llu",
3691 disk->blkdeviotune.write_bytes_sec);
3694 if (disk->blkdeviotune.total_iops_sec) {
3695 virBufferAsprintf(&opt, ",iops=%llu",
3696 disk->blkdeviotune.total_iops_sec);
3699 if (disk->blkdeviotune.read_iops_sec) {
3700 virBufferAsprintf(&opt, ",iops_rd=%llu",
3701 disk->blkdeviotune.read_iops_sec);
3704 if (disk->blkdeviotune.write_iops_sec) {
3705 virBufferAsprintf(&opt, ",iops_wr=%llu",
3706 disk->blkdeviotune.write_iops_sec);
3709 if (virBufferError(&opt)) {
3710 virReportOOMError();
3714 return virBufferContentAndReset(&opt);
3718 virBufferFreeAndReset(&opt);
3723 qemuBuildDriveDevStr(virDomainDefPtr def,
3724 virDomainDiskDefPtr disk,
3726 virQEMUCapsPtr qemuCaps)
3728 virBuffer opt = VIR_BUFFER_INITIALIZER;
3729 const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
3730 int idx = virDiskNameToIndex(disk->dst);
3731 int controllerModel;
3734 virReportError(VIR_ERR_INTERNAL_ERROR,
3735 _("unsupported disk type '%s'"), disk->dst);
3740 if ((disk->bus != VIR_DOMAIN_DISK_BUS_IDE) &&
3741 (disk->bus != VIR_DOMAIN_DISK_BUS_SCSI)) {
3742 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3743 _("Only ide and scsi disk support wwn"));
3748 if ((disk->vendor || disk->product) &&
3749 disk->bus != VIR_DOMAIN_DISK_BUS_SCSI) {
3750 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3751 _("Only scsi disk supports vendor and product"));
3755 if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
3756 /* make sure that both the bus and the qemu binary support
3757 * type='lun' (SG_IO).
3759 if (disk->bus != VIR_DOMAIN_DISK_BUS_VIRTIO &&
3760 disk->bus != VIR_DOMAIN_DISK_BUS_SCSI) {
3761 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3762 _("disk device='lun' is not supported for bus='%s'"),
3766 if (disk->src.type == VIR_STORAGE_TYPE_NETWORK) {
3767 if (disk->src.protocol != VIR_STORAGE_NET_PROTOCOL_ISCSI) {
3768 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3769 _("disk device='lun' is not supported for protocol='%s'"),
3770 virStorageNetProtocolTypeToString(disk->src.protocol));
3773 } else if (!virDomainDiskSourceIsBlockType(disk)) {
3774 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3775 _("disk device='lun' is only valid for block type disk source"));
3778 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BLK_SG_IO)) {
3779 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3780 _("disk device='lun' is not supported by this QEMU"));
3784 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3785 _("Setting wwn is not supported for lun device"));
3788 if (disk->vendor || disk->product) {
3789 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3790 _("Setting vendor or product is not supported "
3796 switch (disk->bus) {
3797 case VIR_DOMAIN_DISK_BUS_IDE:
3798 if (disk->info.addr.drive.target != 0) {
3799 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3800 _("target must be 0 for ide controller"));
3805 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_IDE_DRIVE_WWN)) {
3806 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3807 _("Setting wwn for ide disk is not supported "
3812 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_IDE_CD)) {
3813 if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
3814 virBufferAddLit(&opt, "ide-cd");
3816 virBufferAddLit(&opt, "ide-hd");
3818 virBufferAddLit(&opt, "ide-drive");
3821 virBufferAsprintf(&opt, ",bus=ide.%d,unit=%d",
3822 disk->info.addr.drive.bus,
3823 disk->info.addr.drive.unit);
3825 case VIR_DOMAIN_DISK_BUS_SCSI:
3826 if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
3827 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_BLOCK)) {
3828 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3829 _("This QEMU doesn't support scsi-block for "
3830 "lun passthrough"));
3836 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_DISK_WWN)) {
3837 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3838 _("Setting wwn for scsi disk is not supported "
3843 /* Properties wwn, vendor and product were introduced in the
3844 * same QEMU release (1.2.0).
3846 if ((disk->vendor || disk->product) &&
3847 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_DISK_WWN)) {
3848 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3849 _("Setting vendor or product for scsi disk is not "
3850 "supported by this QEMU"));
3855 virDomainDeviceFindControllerModel(def, &disk->info,
3856 VIR_DOMAIN_CONTROLLER_TYPE_SCSI);
3857 if ((qemuSetSCSIControllerModel(def, qemuCaps, &controllerModel)) < 0)
3860 if (controllerModel == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC) {
3861 if (disk->info.addr.drive.target != 0) {
3862 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3863 _("target must be 0 for controller "
3864 "model 'lsilogic'"));
3868 if (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN) {
3869 virBufferAddLit(&opt, "scsi-block");
3871 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_CD)) {
3872 if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
3873 virBufferAddLit(&opt, "scsi-cd");
3875 virBufferAddLit(&opt, "scsi-hd");
3877 virBufferAddLit(&opt, "scsi-disk");
3881 virBufferAsprintf(&opt, ",bus=scsi%d.%d,scsi-id=%d",
3882 disk->info.addr.drive.controller,
3883 disk->info.addr.drive.bus,
3884 disk->info.addr.drive.unit);
3886 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_DISK_CHANNEL)) {
3887 if (disk->info.addr.drive.target > 7) {
3888 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3889 _("This QEMU doesn't support target "
3894 if ((disk->info.addr.drive.bus != disk->info.addr.drive.unit) &&
3895 (disk->info.addr.drive.bus != 0)) {
3896 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3897 _("This QEMU only supports both bus and "
3898 "unit equal to 0"));
3903 if (disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
3904 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCSI_CD)) {
3905 if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
3906 virBufferAddLit(&opt, "scsi-cd");
3908 virBufferAddLit(&opt, "scsi-hd");
3910 virBufferAddLit(&opt, "scsi-disk");
3913 virBufferAddLit(&opt, "scsi-block");
3916 virBufferAsprintf(&opt, ",bus=scsi%d.0,channel=%d,scsi-id=%d,lun=%d",
3917 disk->info.addr.drive.controller,
3918 disk->info.addr.drive.bus,
3919 disk->info.addr.drive.target,
3920 disk->info.addr.drive.unit);
3923 case VIR_DOMAIN_DISK_BUS_SATA:
3924 if (disk->info.addr.drive.bus != 0) {
3925 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3926 _("bus must be 0 for ide controller"));
3929 if (disk->info.addr.drive.target != 0) {
3930 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3931 _("target must be 0 for ide controller"));
3935 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_IDE_CD)) {
3936 if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
3937 virBufferAddLit(&opt, "ide-cd");
3939 virBufferAddLit(&opt, "ide-hd");
3941 virBufferAddLit(&opt, "ide-drive");
3944 if (qemuDomainMachineIsQ35(def) &&
3945 disk->info.addr.drive.controller == 0) {
3946 /* Q35 machines have an implicit ahci (sata) controller at
3947 * 00:1F.2 which for some reason is hardcoded with the id
3948 * "ide" instead of the seemingly more reasonable "ahci0"
3951 virBufferAsprintf(&opt, ",bus=ide.%d", disk->info.addr.drive.unit);
3953 /* All other ahci controllers have been created by
3954 * libvirt, and we gave them the id "ahci${n}" where ${n}
3955 * is the controller number. So we identify them that way.
3957 virBufferAsprintf(&opt, ",bus=ahci%d.%d",
3958 disk->info.addr.drive.controller,
3959 disk->info.addr.drive.unit);
3962 case VIR_DOMAIN_DISK_BUS_VIRTIO:
3963 if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
3964 virBufferAddLit(&opt, "virtio-blk-ccw");
3965 } else if (disk->info.type ==
3966 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390) {
3967 virBufferAddLit(&opt, "virtio-blk-s390");
3968 } else if (disk->info.type ==
3969 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO) {
3970 virBufferAddLit(&opt, "virtio-blk-device");
3972 virBufferAddLit(&opt, "virtio-blk-pci");
3974 qemuBuildIoEventFdStr(&opt, disk->ioeventfd, qemuCaps);
3975 if (disk->event_idx &&
3976 virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BLK_EVENT_IDX)) {
3977 virBufferAsprintf(&opt, ",event_idx=%s",
3978 virDomainVirtioEventIdxTypeToString(disk->event_idx));
3980 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BLK_SCSI)) {
3981 /* if sg_io is true but the scsi option isn't supported,
3982 * that means it's just always on in this version of qemu.
3984 virBufferAsprintf(&opt, ",scsi=%s",
3985 (disk->device == VIR_DOMAIN_DISK_DEVICE_LUN)
3988 if (qemuBuildDeviceAddressStr(&opt, def, &disk->info, qemuCaps) < 0)
3991 case VIR_DOMAIN_DISK_BUS_USB:
3992 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_USB_STORAGE)) {
3993 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3994 _("This QEMU doesn't support '-device "
3999 virBufferAddLit(&opt, "usb-storage");
4001 if (qemuBuildDeviceAddressStr(&opt, def, &disk->info, qemuCaps) < 0)
4005 virReportError(VIR_ERR_INTERNAL_ERROR,
4006 _("unsupported disk bus '%s' with device setup"), bus);
4009 virBufferAsprintf(&opt, ",drive=%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias);
4010 virBufferAsprintf(&opt, ",id=%s", disk->info.alias);
4011 if (bootindex && virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX))
4012 virBufferAsprintf(&opt, ",bootindex=%d", bootindex);
4013 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKIO)) {
4014 if (disk->blockio.logical_block_size > 0)
4015 virBufferAsprintf(&opt, ",logical_block_size=%u",
4016 disk->blockio.logical_block_size);
4017 if (disk->blockio.physical_block_size > 0)
4018 virBufferAsprintf(&opt, ",physical_block_size=%u",
4019 disk->blockio.physical_block_size);
4023 if (STRPREFIX(disk->wwn, "0x"))
4024 virBufferAsprintf(&opt, ",wwn=%s", disk->wwn);
4026 virBufferAsprintf(&opt, ",wwn=0x%s", disk->wwn);
4030 virBufferAsprintf(&opt, ",vendor=%s", disk->vendor);
4033 virBufferAsprintf(&opt, ",product=%s", disk->product);
4035 if (disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
4036 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_USB_STORAGE_REMOVABLE)) {
4037 if (disk->removable == VIR_DOMAIN_FEATURE_STATE_ON)
4038 virBufferAddLit(&opt, ",removable=on");
4040 virBufferAddLit(&opt, ",removable=off");
4042 if (disk->removable != VIR_DOMAIN_FEATURE_STATE_DEFAULT) {
4043 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4044 _("This QEMU doesn't support setting the "
4045 "removable flag of USB storage devices"));
4051 if (virBufferError(&opt)) {
4052 virReportOOMError();
4056 return virBufferContentAndReset(&opt);
4059 virBufferFreeAndReset(&opt);
4064 char *qemuBuildFSStr(virDomainFSDefPtr fs,
4065 virQEMUCapsPtr qemuCaps ATTRIBUTE_UNUSED)
4067 virBuffer opt = VIR_BUFFER_INITIALIZER;
4068 const char *driver = qemuDomainFSDriverTypeToString(fs->fsdriver);
4069 const char *wrpolicy = virDomainFSWrpolicyTypeToString(fs->wrpolicy);
4071 if (fs->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
4072 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4073 _("only supports mount filesystem type"));
4078 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4079 _("Filesystem driver type not supported"));
4082 virBufferAdd(&opt, driver, -1);
4084 if (fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_PATH ||
4085 fs->fsdriver == VIR_DOMAIN_FS_DRIVER_TYPE_DEFAULT) {
4086 if (fs->accessmode == VIR_DOMAIN_FS_ACCESSMODE_MAPPED) {
4087 virBufferAddLit(&opt, ",security_model=mapped");
4088 } else if (fs->accessmode == VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH) {
4089 virBufferAddLit(&opt, ",security_model=passthrough");
4090 } else if (fs->accessmode == VIR_DOMAIN_FS_ACCESSMODE_SQUASH) {
4091 virBufferAddLit(&opt, ",security_model=none");
4094 /* For other fs drivers, default(passthru) should always
4096 if (fs->accessmode != VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH) {
4097 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4098 _("only supports passthrough accessmode"));
4104 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_FSDEV_WRITEOUT)) {
4105 virBufferAsprintf(&opt, ",writeout=%s", wrpolicy);
4107 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4108 _("filesystem writeout not supported"));
4113 virBufferAsprintf(&opt, ",id=%s%s", QEMU_FSDEV_HOST_PREFIX, fs->info.alias);
4114 virBufferAsprintf(&opt, ",path=%s", fs->src);
4117 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_FSDEV_READONLY)) {
4118 virBufferAddLit(&opt, ",readonly");
4120 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4121 _("readonly filesystem is not supported by this "
4127 if (virBufferError(&opt)) {
4128 virReportOOMError();
4132 return virBufferContentAndReset(&opt);
4135 virBufferFreeAndReset(&opt);
4141 qemuBuildFSDevStr(virDomainDefPtr def,
4142 virDomainFSDefPtr fs,
4143 virQEMUCapsPtr qemuCaps)
4145 virBuffer opt = VIR_BUFFER_INITIALIZER;
4147 if (fs->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
4148 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4149 _("can only passthrough directories"));
4153 virBufferAddLit(&opt, "virtio-9p-pci");
4154 virBufferAsprintf(&opt, ",id=%s", fs->info.alias);
4155 virBufferAsprintf(&opt, ",fsdev=%s%s", QEMU_FSDEV_HOST_PREFIX, fs->info.alias);
4156 virBufferAsprintf(&opt, ",mount_tag=%s", fs->dst);
4158 if (qemuBuildDeviceAddressStr(&opt, def, &fs->info, qemuCaps) < 0)
4161 if (virBufferError(&opt)) {
4162 virReportOOMError();
4166 return virBufferContentAndReset(&opt);
4169 virBufferFreeAndReset(&opt);
4175 qemuControllerModelUSBToCaps(int model)
4178 case VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI:
4179 return QEMU_CAPS_PIIX3_USB_UHCI;
4180 case VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX4_UHCI:
4181 return QEMU_CAPS_PIIX4_USB_UHCI;
4182 case VIR_DOMAIN_CONTROLLER_MODEL_USB_EHCI:
4183 return QEMU_CAPS_USB_EHCI;
4184 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_EHCI1:
4185 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI1:
4186 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI2:
4187 case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI3:
4188 return QEMU_CAPS_ICH9_USB_EHCI1;
4189 case VIR_DOMAIN_CONTROLLER_MODEL_USB_VT82C686B_UHCI:
4190 return QEMU_CAPS_VT82C686B_USB_UHCI;
4191 case VIR_DOMAIN_CONTROLLER_MODEL_USB_PCI_OHCI:
4192 return QEMU_CAPS_PCI_OHCI;
4193 case VIR_DOMAIN_CONTROLLER_MODEL_USB_NEC_XHCI:
4194 return QEMU_CAPS_NEC_USB_XHCI;
4202 qemuBuildUSBControllerDevStr(virDomainDefPtr domainDef,
4203 virDomainControllerDefPtr def,
4204 virQEMUCapsPtr qemuCaps,
4213 if (domainDef->os.arch == VIR_ARCH_PPC64)
4214 model = VIR_DOMAIN_CONTROLLER_MODEL_USB_PCI_OHCI;
4216 model = VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI;
4219 smodel = qemuControllerModelUSBTypeToString(model);
4220 flags = qemuControllerModelUSBToCaps(model);
4222 if (flags == -1 || !virQEMUCapsGet(qemuCaps, flags)) {
4223 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4224 _("%s not supported in this QEMU binary"), smodel);
4228 virBufferAsprintf(buf, "%s", smodel);
4230 if (def->info.mastertype == VIR_DOMAIN_CONTROLLER_MASTER_USB) {
4231 virBufferAddLit(buf, ",masterbus=");
4232 qemuUSBId(buf, def->idx);
4233 virBufferAsprintf(buf, ".0,firstport=%d", def->info.master.usb.startport);
4235 virBufferAddLit(buf, ",id=");
4236 qemuUSBId(buf, def->idx);
4243 qemuBuildControllerDevStr(virDomainDefPtr domainDef,
4244 virDomainControllerDefPtr def,
4245 virQEMUCapsPtr qemuCaps,
4246 int *nusbcontroller)
4248 virBuffer buf = VIR_BUFFER_INITIALIZER;
4252 !(def->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI &&
4253 def->model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VIRTIO_SCSI)) {
4254 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4255 _("'queues' is only supported by virtio-scsi controller"));
4259 switch (def->type) {
4260 case VIR_DOMAIN_CONTROLLER_TYPE_SCSI:
4262 if ((qemuSetSCSIControllerModel(domainDef, qemuCaps, &model)) < 0)
4266 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VIRTIO_SCSI:
4267 if (def->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW)
4268 virBufferAddLit(&buf, "virtio-scsi-ccw");
4269 else if (def->info.type ==
4270 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390)
4271 virBufferAddLit(&buf, "virtio-scsi-s390");
4272 else if (def->info.type ==
4273 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO)
4274 virBufferAddLit(&buf, "virtio-scsi-device");
4276 virBufferAddLit(&buf, "virtio-scsi-pci");
4278 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC:
4279 virBufferAddLit(&buf, "lsi");
4281 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_IBMVSCSI:
4282 virBufferAddLit(&buf, "spapr-vscsi");
4284 case VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSISAS1078:
4285 virBufferAddLit(&buf, "megasas");
4288 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4289 _("Unsupported controller model: %s"),
4290 virDomainControllerModelSCSITypeToString(def->model));
4292 virBufferAsprintf(&buf, ",id=scsi%d", def->idx);
4295 case VIR_DOMAIN_CONTROLLER_TYPE_VIRTIO_SERIAL:
4296 if (def->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
4297 virBufferAddLit(&buf, "virtio-serial-pci");
4298 } else if (def->info.type ==
4299 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
4300 virBufferAddLit(&buf, "virtio-serial-ccw");
4301 } else if (def->info.type ==
4302 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390) {
4303 virBufferAddLit(&buf, "virtio-serial-s390");
4304 } else if (def->info.type ==
4305 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO) {
4306 virBufferAddLit(&buf, "virtio-serial-device");
4308 virBufferAddLit(&buf, "virtio-serial");
4310 virBufferAsprintf(&buf, ",id=" QEMU_VIRTIO_SERIAL_PREFIX "%d",
4312 if (def->opts.vioserial.ports != -1) {
4313 virBufferAsprintf(&buf, ",max_ports=%d",
4314 def->opts.vioserial.ports);
4316 if (def->opts.vioserial.vectors != -1) {
4317 virBufferAsprintf(&buf, ",vectors=%d",
4318 def->opts.vioserial.vectors);
4322 case VIR_DOMAIN_CONTROLLER_TYPE_CCID:
4323 virBufferAsprintf(&buf, "usb-ccid,id=ccid%d", def->idx);
4326 case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
4327 virBufferAsprintf(&buf, "ahci,id=ahci%d", def->idx);
4330 case VIR_DOMAIN_CONTROLLER_TYPE_USB:
4331 if (qemuBuildUSBControllerDevStr(domainDef, def, qemuCaps, &buf) == -1)
4335 *nusbcontroller += 1;
4339 case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
4340 switch (def->model) {
4341 case VIR_DOMAIN_CONTROLLER_MODEL_PCI_BRIDGE:
4342 if (def->idx == 0) {
4343 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4344 _("PCI bridge index should be > 0"));
4347 virBufferAsprintf(&buf, "pci-bridge,chassis_nr=%d,id=pci.%d",
4348 def->idx, def->idx);
4350 case VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE:
4351 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_DMI_TO_PCI_BRIDGE)) {
4352 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4353 _("The dmi-to-pci-bridge (i82801b11-bridge) "
4354 "controller is not supported in this QEMU binary"));
4357 if (def->idx == 0) {
4358 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4359 _("dmi-to-pci-bridge index should be > 0"));
4362 virBufferAsprintf(&buf, "i82801b11-bridge,id=pci.%d", def->idx);
4364 case VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT:
4365 case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT:
4366 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
4367 _("wrong function called for pci-root/pcie-root"));
4372 /* We always get an IDE controller, whether we want it or not. */
4373 case VIR_DOMAIN_CONTROLLER_TYPE_IDE:
4375 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4376 _("Unknown controller type: %s"),
4377 virDomainControllerTypeToString(def->type));
4382 virBufferAsprintf(&buf, ",num_queues=%u", def->queues);
4384 if (qemuBuildDeviceAddressStr(&buf, domainDef, &def->info, qemuCaps) < 0)
4387 if (virBufferError(&buf)) {
4388 virReportOOMError();
4392 return virBufferContentAndReset(&buf);
4395 virBufferFreeAndReset(&buf);
4401 qemuBuildNicStr(virDomainNetDefPtr net,
4406 char macaddr[VIR_MAC_STRING_BUFLEN];
4408 ignore_value(virAsprintf(&str,
4409 "%smacaddr=%s,vlan=%d%s%s%s%s",
4410 prefix ? prefix : "",
4411 virMacAddrFormat(&net->mac, macaddr),
4413 (net->model ? ",model=" : ""),
4414 (net->model ? net->model : ""),
4415 (net->info.alias ? ",name=" : ""),
4416 (net->info.alias ? net->info.alias : "")));
4422 qemuBuildNicDevStr(virDomainDefPtr def,
4423 virDomainNetDefPtr net,
4427 virQEMUCapsPtr qemuCaps)
4429 virBuffer buf = VIR_BUFFER_INITIALIZER;
4430 const char *nic = net->model;
4431 bool usingVirtio = false;
4432 char macaddr[VIR_MAC_STRING_BUFLEN];
4434 if (STREQ(net->model, "virtio")) {
4435 if (net->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW)
4436 nic = "virtio-net-ccw";
4437 else if (net->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390)
4438 nic = "virtio-net-s390";
4439 else if (net->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO)
4440 nic = "virtio-net-device";
4442 nic = "virtio-net-pci";
4447 virBufferAdd(&buf, nic, -1);
4448 if (usingVirtio && net->driver.virtio.txmode) {
4449 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_TX_ALG)) {
4450 virBufferAddLit(&buf, ",tx=");
4451 switch (net->driver.virtio.txmode) {
4452 case VIR_DOMAIN_NET_VIRTIO_TX_MODE_IOTHREAD:
4453 virBufferAddLit(&buf, "bh");
4456 case VIR_DOMAIN_NET_VIRTIO_TX_MODE_TIMER:
4457 virBufferAddLit(&buf, "timer");
4460 /* this should never happen, if it does, we need
4461 * to add another case to this switch.
4463 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
4464 _("unrecognized virtio-net-pci 'tx' option"));
4468 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4469 _("virtio-net-pci 'tx' option not supported in this QEMU binary"));
4474 qemuBuildIoEventFdStr(&buf, net->driver.virtio.ioeventfd, qemuCaps);
4475 if (net->driver.virtio.event_idx &&
4476 virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_EVENT_IDX)) {
4477 virBufferAsprintf(&buf, ",event_idx=%s",
4478 virDomainVirtioEventIdxTypeToString(net->driver.virtio.event_idx));
4481 if (usingVirtio && vhostfdSize > 1) {
4482 /* As advised at http://www.linux-kvm.org/page/Multiqueue
4483 * we should add vectors=2*N+2 where N is the vhostfdSize */
4484 virBufferAsprintf(&buf, ",mq=on,vectors=%d", 2 * vhostfdSize + 2);
4487 virBufferAsprintf(&buf, ",netdev=host%s", net->info.alias);
4489 virBufferAsprintf(&buf, ",vlan=%d", vlan);
4490 virBufferAsprintf(&buf, ",id=%s", net->info.alias);
4491 virBufferAsprintf(&buf, ",mac=%s",
4492 virMacAddrFormat(&net->mac, macaddr));
4493 if (qemuBuildDeviceAddressStr(&buf, def, &net->info, qemuCaps) < 0)
4495 if (qemuBuildRomStr(&buf, &net->info, qemuCaps) < 0)
4497 if (bootindex && virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX))
4498 virBufferAsprintf(&buf, ",bootindex=%d", bootindex);
4500 if (virBufferError(&buf)) {
4501 virReportOOMError();
4505 return virBufferContentAndReset(&buf);
4508 virBufferFreeAndReset(&buf);
4514 qemuBuildHostNetStr(virDomainNetDefPtr net,
4515 virQEMUDriverPtr driver,
4523 bool is_tap = false;
4524 virBuffer buf = VIR_BUFFER_INITIALIZER;
4525 enum virDomainNetType netType = virDomainNetGetActualType(net);
4526 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
4529 if (net->script && netType != VIR_DOMAIN_NET_TYPE_ETHERNET) {
4530 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4531 _("scripts are not supported on interfaces of type %s"),
4532 virDomainNetTypeToString(netType));
4533 virObjectUnref(cfg);
4539 * If type='bridge', and we're running as privileged user
4540 * or -netdev bridge is not supported then it will fall
4541 * through, -net tap,fd
4543 case VIR_DOMAIN_NET_TYPE_BRIDGE:
4544 case VIR_DOMAIN_NET_TYPE_NETWORK:
4545 case VIR_DOMAIN_NET_TYPE_DIRECT:
4546 virBufferAsprintf(&buf, "tap%c", type_sep);
4547 /* for one tapfd 'fd=' shall be used,
4548 * for more than one 'fds=' is the right choice */
4549 if (tapfdSize == 1) {
4550 virBufferAsprintf(&buf, "fd=%s", tapfd[0]);
4552 virBufferAddLit(&buf, "fds=");
4553 for (i = 0; i < tapfdSize; i++) {
4555 virBufferAddChar(&buf, ':');
4556 virBufferAdd(&buf, tapfd[i], -1);
4563 case VIR_DOMAIN_NET_TYPE_ETHERNET:
4564 virBufferAddLit(&buf, "tap");
4566 virBufferAsprintf(&buf, "%cifname=%s", type_sep, net->ifname);
4570 virBufferAsprintf(&buf, "%cscript=%s", type_sep,
4577 case VIR_DOMAIN_NET_TYPE_CLIENT:
4578 virBufferAsprintf(&buf, "socket%cconnect=%s:%d",
4580 net->data.socket.address,
4581 net->data.socket.port);
4585 case VIR_DOMAIN_NET_TYPE_SERVER:
4586 virBufferAsprintf(&buf, "socket%clisten=%s:%d",
4588 net->data.socket.address,
4589 net->data.socket.port);
4593 case VIR_DOMAIN_NET_TYPE_MCAST:
4594 virBufferAsprintf(&buf, "socket%cmcast=%s:%d",
4596 net->data.socket.address,
4597 net->data.socket.port);
4601 case VIR_DOMAIN_NET_TYPE_USER:
4603 virBufferAddLit(&buf, "user");
4608 virBufferAsprintf(&buf, "%cvlan=%d", type_sep, vlan);
4609 if (net->info.alias)
4610 virBufferAsprintf(&buf, ",name=host%s",
4613 virBufferAsprintf(&buf, "%cid=host%s",
4614 type_sep, net->info.alias);
4619 virBufferAddLit(&buf, ",vhost=on,");
4620 if (vhostfdSize == 1) {
4621 virBufferAsprintf(&buf, "vhostfd=%s", vhostfd[0]);
4623 virBufferAddLit(&buf, "vhostfds=");
4624 for (i = 0; i < vhostfdSize; i++) {
4626 virBufferAddChar(&buf, ':');
4627 virBufferAdd(&buf, vhostfd[i], -1);
4631 if (net->tune.sndbuf_specified)
4632 virBufferAsprintf(&buf, ",sndbuf=%lu", net->tune.sndbuf);
4635 virObjectUnref(cfg);
4637 if (virBufferError(&buf)) {
4638 virBufferFreeAndReset(&buf);
4639 virReportOOMError();
4643 return virBufferContentAndReset(&buf);
4648 qemuBuildWatchdogDevStr(virDomainDefPtr def,
4649 virDomainWatchdogDefPtr dev,
4650 virQEMUCapsPtr qemuCaps)
4652 virBuffer buf = VIR_BUFFER_INITIALIZER;
4654 const char *model = virDomainWatchdogModelTypeToString(dev->model);
4656 virReportError(VIR_ERR_INTERNAL_ERROR,
4657 "%s", _("missing watchdog model"));
4661 virBufferAsprintf(&buf, "%s,id=%s", model, dev->info.alias);
4662 if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
4665 if (virBufferError(&buf)) {
4666 virReportOOMError();
4670 return virBufferContentAndReset(&buf);
4673 virBufferFreeAndReset(&buf);
4679 qemuBuildMemballoonDevStr(virDomainDefPtr def,
4680 virDomainMemballoonDefPtr dev,
4681 virQEMUCapsPtr qemuCaps)
4683 virBuffer buf = VIR_BUFFER_INITIALIZER;
4685 switch (dev->info.type) {
4686 case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI:
4687 virBufferAddLit(&buf, "virtio-balloon-pci");
4689 case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW:
4690 virBufferAddLit(&buf, "virtio-balloon-ccw");
4692 case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO:
4693 virBufferAddLit(&buf, "virtio-balloon-device");
4696 virReportError(VIR_ERR_XML_ERROR,
4697 _("memballoon unsupported with address type '%s'"),
4698 virDomainDeviceAddressTypeToString(dev->info.type));
4702 virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
4703 if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
4706 if (virBufferError(&buf)) {
4707 virReportOOMError();
4711 return virBufferContentAndReset(&buf);
4714 virBufferFreeAndReset(&buf);
4719 qemuBuildNVRAMDevStr(virDomainNVRAMDefPtr dev)
4721 virBuffer buf = VIR_BUFFER_INITIALIZER;
4723 if (dev->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO &&
4724 dev->info.addr.spaprvio.has_reg) {
4725 virBufferAsprintf(&buf, "spapr-nvram.reg=0x%llx",
4726 dev->info.addr.spaprvio.reg);
4728 virReportError(VIR_ERR_XML_ERROR, "%s",
4729 _("nvram address type must be spaprvio"));
4733 if (virBufferError(&buf)) {
4734 virReportOOMError();
4738 return virBufferContentAndReset(&buf);
4741 virBufferFreeAndReset(&buf);
4746 qemuBuildUSBInputDevStr(virDomainDefPtr def,
4747 virDomainInputDefPtr dev,
4748 virQEMUCapsPtr qemuCaps)
4750 virBuffer buf = VIR_BUFFER_INITIALIZER;
4752 switch (dev->type) {
4753 case VIR_DOMAIN_INPUT_TYPE_MOUSE:
4754 virBufferAsprintf(&buf, "usb-mouse,id=%s", dev->info.alias);
4756 case VIR_DOMAIN_INPUT_TYPE_TABLET:
4757 virBufferAsprintf(&buf, "usb-tablet,id=%s", dev->info.alias);
4759 case VIR_DOMAIN_INPUT_TYPE_KBD:
4760 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_USB_KBD))
4762 virBufferAsprintf(&buf, "usb-kbd,id=%s", dev->info.alias);
4766 if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
4769 if (virBufferError(&buf)) {
4770 virReportOOMError();
4774 return virBufferContentAndReset(&buf);
4777 virBufferFreeAndReset(&buf);
4783 qemuBuildSoundDevStr(virDomainDefPtr def,
4784 virDomainSoundDefPtr sound,
4785 virQEMUCapsPtr qemuCaps)
4787 virBuffer buf = VIR_BUFFER_INITIALIZER;
4788 const char *model = virDomainSoundModelTypeToString(sound->model);
4791 virReportError(VIR_ERR_INTERNAL_ERROR,
4792 "%s", _("invalid sound model"));
4796 /* Hack for devices with different names in QEMU and libvirt */
4797 switch (sound->model) {
4798 case VIR_DOMAIN_SOUND_MODEL_ES1370:
4801 case VIR_DOMAIN_SOUND_MODEL_AC97:
4804 case VIR_DOMAIN_SOUND_MODEL_ICH6:
4805 model = "intel-hda";
4807 case VIR_DOMAIN_SOUND_MODEL_ICH9:
4808 model = "ich9-intel-hda";
4809 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_ICH9_INTEL_HDA)) {
4810 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4811 _("The ich9-intel-hda audio controller "
4812 "is not supported in this QEMU binary"));
4818 virBufferAsprintf(&buf, "%s,id=%s", model, sound->info.alias);
4819 if (qemuBuildDeviceAddressStr(&buf, def, &sound->info, qemuCaps) < 0)
4822 if (virBufferError(&buf)) {
4823 virReportOOMError();
4827 return virBufferContentAndReset(&buf);
4830 virBufferFreeAndReset(&buf);
4836 qemuSoundCodecTypeToCaps(int type)
4839 case VIR_DOMAIN_SOUND_CODEC_TYPE_DUPLEX:
4840 return QEMU_CAPS_HDA_DUPLEX;
4841 case VIR_DOMAIN_SOUND_CODEC_TYPE_MICRO:
4842 return QEMU_CAPS_HDA_MICRO;
4850 qemuBuildSoundCodecStr(virDomainSoundDefPtr sound,
4851 virDomainSoundCodecDefPtr codec,
4852 virQEMUCapsPtr qemuCaps)
4854 virBuffer buf = VIR_BUFFER_INITIALIZER;
4859 stype = qemuSoundCodecTypeToString(type);
4860 flags = qemuSoundCodecTypeToCaps(type);
4862 if (flags == -1 || !virQEMUCapsGet(qemuCaps, flags)) {
4863 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4864 _("%s not supported in this QEMU binary"), stype);
4868 virBufferAsprintf(&buf, "%s,id=%s-codec%d,bus=%s.0,cad=%d",
4869 stype, sound->info.alias, codec->cad, sound->info.alias, codec->cad);
4871 return virBufferContentAndReset(&buf);
4874 virBufferFreeAndReset(&buf);
4879 qemuBuildDeviceVideoStr(virDomainDefPtr def,
4880 virDomainVideoDefPtr video,
4881 virQEMUCapsPtr qemuCaps,
4884 virBuffer buf = VIR_BUFFER_INITIALIZER;
4888 model = qemuDeviceVideoTypeToString(video->type);
4889 if (!model || STREQ(model, "")) {
4890 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4891 _("video type %s is not supported with QEMU"),
4892 virDomainVideoTypeToString(video->type));
4896 if (video->type != VIR_DOMAIN_VIDEO_TYPE_QXL) {
4897 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4898 "%s", _("non-primary video device must be type of 'qxl'"));
4902 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_QXL)) {
4903 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4904 "%s", _("only one video card is currently supported"));
4911 virBufferAsprintf(&buf, "%s,id=%s", model, video->info.alias);
4913 if (video->type == VIR_DOMAIN_VIDEO_TYPE_QXL) {
4914 if (video->vram > (UINT_MAX / 1024)) {
4915 virReportError(VIR_ERR_OVERFLOW,
4916 _("value for 'vram' must be less than '%u'"),
4920 if (video->ram > (UINT_MAX / 1024)) {
4921 virReportError(VIR_ERR_OVERFLOW,
4922 _("value for 'ram' must be less than '%u'"),
4927 /* QEMU accepts bytes for ram_size. */
4928 virBufferAsprintf(&buf, ",ram_size=%u", video->ram * 1024);
4930 /* QEMU accepts bytes for vram_size. */
4931 virBufferAsprintf(&buf, ",vram_size=%u", video->vram * 1024);
4934 if (qemuBuildDeviceAddressStr(&buf, def, &video->info, qemuCaps) < 0)
4937 if (virBufferError(&buf)) {
4938 virReportOOMError();
4942 return virBufferContentAndReset(&buf);
4945 virBufferFreeAndReset(&buf);
4951 qemuOpenPCIConfig(virDomainHostdevDefPtr dev)
4956 if (virAsprintf(&path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/config",
4957 dev->source.subsys.u.pci.addr.domain,
4958 dev->source.subsys.u.pci.addr.bus,
4959 dev->source.subsys.u.pci.addr.slot,
4960 dev->source.subsys.u.pci.addr.function) < 0)
4963 configfd = open(path, O_RDWR, 0);
4966 virReportSystemError(errno, _("Failed opening %s"), path);
4974 qemuBuildPCIHostdevDevStr(virDomainDefPtr def,
4975 virDomainHostdevDefPtr dev,
4976 const char *configfd,
4977 virQEMUCapsPtr qemuCaps)
4979 virBuffer buf = VIR_BUFFER_INITIALIZER;
4980 int backend = dev->source.subsys.u.pci.backend;
4982 /* caller has to assign proper passthrough backend type */
4983 switch ((virDomainHostdevSubsysPCIBackendType) backend) {
4984 case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
4985 virBufferAddLit(&buf, "pci-assign");
4986 if (configfd && *configfd)
4987 virBufferAsprintf(&buf, ",configfd=%s", configfd);
4990 case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
4991 virBufferAddLit(&buf, "vfio-pci");
4995 virReportError(VIR_ERR_INTERNAL_ERROR,
4996 _("invalid PCI passthrough type '%s'"),
4997 virDomainHostdevSubsysPCIBackendTypeToString(backend));
5001 virBufferAddLit(&buf, ",host=");
5002 if (dev->source.subsys.u.pci.addr.domain) {
5003 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_HOST_PCI_MULTIDOMAIN)) {
5004 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5005 _("non-zero domain='%.4x' in host device PCI address "
5006 "not supported in this QEMU binary"),
5007 dev->source.subsys.u.pci.addr.domain);
5010 virBufferAsprintf(&buf, "%.4x:", dev->source.subsys.u.pci.addr.domain);
5012 virBufferAsprintf(&buf, "%.2x:%.2x.%.1x",
5013 dev->source.subsys.u.pci.addr.bus,
5014 dev->source.subsys.u.pci.addr.slot,
5015 dev->source.subsys.u.pci.addr.function);
5016 virBufferAsprintf(&buf, ",id=%s", dev->info->alias);
5017 if (dev->info->bootIndex)
5018 virBufferAsprintf(&buf, ",bootindex=%d", dev->info->bootIndex);
5019 if (qemuBuildDeviceAddressStr(&buf, def, dev->info, qemuCaps) < 0)
5021 if (qemuBuildRomStr(&buf, dev->info, qemuCaps) < 0)
5024 if (virBufferError(&buf)) {
5025 virReportOOMError();
5029 return virBufferContentAndReset(&buf);
5032 virBufferFreeAndReset(&buf);
5038 qemuBuildPCIHostdevPCIDevStr(virDomainHostdevDefPtr dev,
5039 virQEMUCapsPtr qemuCaps)
5043 if (dev->source.subsys.u.pci.addr.domain) {
5044 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_HOST_PCI_MULTIDOMAIN)) {
5045 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5046 _("non-zero domain='%.4x' in host device PCI address "
5047 "not supported in this QEMU binary"),
5048 dev->source.subsys.u.pci.addr.domain);
5051 ignore_value(virAsprintf(&ret, "host=%.4x:%.2x:%.2x.%.1x",
5052 dev->source.subsys.u.pci.addr.domain,
5053 dev->source.subsys.u.pci.addr.bus,
5054 dev->source.subsys.u.pci.addr.slot,
5055 dev->source.subsys.u.pci.addr.function));
5057 ignore_value(virAsprintf(&ret, "host=%.2x:%.2x.%.1x",
5058 dev->source.subsys.u.pci.addr.bus,
5059 dev->source.subsys.u.pci.addr.slot,
5060 dev->source.subsys.u.pci.addr.function));
5068 qemuBuildRedirdevDevStr(virDomainDefPtr def,
5069 virDomainRedirdevDefPtr dev,
5070 virQEMUCapsPtr qemuCaps)
5073 virBuffer buf = VIR_BUFFER_INITIALIZER;
5074 virDomainRedirFilterDefPtr redirfilter = def->redirfilter;
5076 if (dev->bus != VIR_DOMAIN_REDIRDEV_BUS_USB) {
5077 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5078 _("Redirection bus %s is not supported by QEMU"),
5079 virDomainRedirdevBusTypeToString(dev->bus));
5083 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_USB_REDIR)) {
5084 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5085 _("USB redirection is not supported "
5086 "by this version of QEMU"));
5090 virBufferAsprintf(&buf, "usb-redir,chardev=char%s,id=%s",
5094 if (redirfilter && redirfilter->nusbdevs) {
5095 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_USB_REDIR_FILTER)) {
5096 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5097 _("USB redirection filter is not "
5098 "supported by this version of QEMU"));
5102 virBufferAddLit(&buf, ",filter=");
5104 for (i = 0; i < redirfilter->nusbdevs; i++) {
5105 virDomainRedirFilterUSBDevDefPtr usbdev = redirfilter->usbdevs[i];
5106 if (usbdev->usbClass >= 0)
5107 virBufferAsprintf(&buf, "0x%02X:", usbdev->usbClass);
5109 virBufferAddLit(&buf, "-1:");
5111 if (usbdev->vendor >= 0)
5112 virBufferAsprintf(&buf, "0x%04X:", usbdev->vendor);
5114 virBufferAddLit(&buf, "-1:");
5116 if (usbdev->product >= 0)
5117 virBufferAsprintf(&buf, "0x%04X:", usbdev->product);
5119 virBufferAddLit(&buf, "-1:");
5121 if (usbdev->version >= 0)
5122 virBufferAsprintf(&buf, "0x%04X:", usbdev->version);
5124 virBufferAddLit(&buf, "-1:");
5126 virBufferAsprintf(&buf, "%u", usbdev->allow);
5127 if (i < redirfilter->nusbdevs -1)
5128 virBufferAddLit(&buf, "|");
5132 if (dev->info.bootIndex) {
5133 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_USB_REDIR_BOOTINDEX)) {
5134 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5135 _("USB redirection booting is not "
5136 "supported by this version of QEMU"));
5139 virBufferAsprintf(&buf, ",bootindex=%d", dev->info.bootIndex);
5142 if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
5145 if (virBufferError(&buf)) {
5146 virReportOOMError();
5150 return virBufferContentAndReset(&buf);
5153 virBufferFreeAndReset(&buf);
5158 qemuBuildUSBHostdevDevStr(virDomainDefPtr def,
5159 virDomainHostdevDefPtr dev,
5160 virQEMUCapsPtr qemuCaps)
5162 virBuffer buf = VIR_BUFFER_INITIALIZER;
5164 if (!dev->missing &&
5165 !dev->source.subsys.u.usb.bus &&
5166 !dev->source.subsys.u.usb.device) {
5167 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
5168 _("USB host device is missing bus/device information"));
5172 virBufferAddLit(&buf, "usb-host");
5173 if (!dev->missing) {
5174 virBufferAsprintf(&buf, ",hostbus=%d,hostaddr=%d",
5175 dev->source.subsys.u.usb.bus,
5176 dev->source.subsys.u.usb.device);
5178 virBufferAsprintf(&buf, ",id=%s", dev->info->alias);
5179 if (dev->info->bootIndex)
5180 virBufferAsprintf(&buf, ",bootindex=%d", dev->info->bootIndex);
5182 if (qemuBuildDeviceAddressStr(&buf, def, dev->info, qemuCaps) < 0)
5185 if (virBufferError(&buf)) {
5186 virReportOOMError();
5190 return virBufferContentAndReset(&buf);
5193 virBufferFreeAndReset(&buf);
5199 qemuBuildHubDevStr(virDomainDefPtr def,
5200 virDomainHubDefPtr dev,
5201 virQEMUCapsPtr qemuCaps)
5203 virBuffer buf = VIR_BUFFER_INITIALIZER;
5205 if (dev->type != VIR_DOMAIN_HUB_TYPE_USB) {
5206 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5207 _("hub type %s not supported"),
5208 virDomainHubTypeToString(dev->type));
5212 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_USB_HUB)) {
5213 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5214 _("usb-hub not supported by QEMU binary"));
5218 virBufferAddLit(&buf, "usb-hub");
5219 virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
5220 if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
5223 if (virBufferError(&buf)) {
5224 virReportOOMError();
5228 return virBufferContentAndReset(&buf);
5231 virBufferFreeAndReset(&buf);
5237 qemuBuildUSBHostdevUSBDevStr(virDomainHostdevDefPtr dev)
5242 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5243 _("This QEMU doesn't not support missing USB devices"));
5247 if (!dev->source.subsys.u.usb.bus &&
5248 !dev->source.subsys.u.usb.device) {
5249 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
5250 _("USB host device is missing bus/device information"));
5254 ignore_value(virAsprintf(&ret, "host:%d.%d",
5255 dev->source.subsys.u.usb.bus,
5256 dev->source.subsys.u.usb.device));
5261 qemuBuildSCSIHostdevDrvStr(virDomainHostdevDefPtr dev,
5262 virQEMUCapsPtr qemuCaps ATTRIBUTE_UNUSED,
5263 qemuBuildCommandLineCallbacksPtr callbacks)
5265 virBuffer buf = VIR_BUFFER_INITIALIZER;
5268 sg = (callbacks->qemuGetSCSIDeviceSgName)(NULL,
5269 dev->source.subsys.u.scsi.adapter,
5270 dev->source.subsys.u.scsi.bus,
5271 dev->source.subsys.u.scsi.target,
5272 dev->source.subsys.u.scsi.unit);
5276 virBufferAsprintf(&buf, "file=/dev/%s,if=none", sg);
5277 virBufferAsprintf(&buf, ",id=%s-%s",
5278 virDomainDeviceAddressTypeToString(dev->info->type),
5281 if (dev->readonly) {
5282 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_READONLY)) {
5283 virBufferAddLit(&buf, ",readonly=on");
5285 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5286 _("this qemu doesn't support 'readonly' "
5292 if (virBufferError(&buf)) {
5293 virReportOOMError();
5298 return virBufferContentAndReset(&buf);
5301 virBufferFreeAndReset(&buf);
5306 qemuBuildSCSIHostdevDevStr(virDomainDefPtr def,
5307 virDomainHostdevDefPtr dev,
5308 virQEMUCapsPtr qemuCaps)
5310 virBuffer buf = VIR_BUFFER_INITIALIZER;
5313 model = virDomainDeviceFindControllerModel(def, dev->info,
5314 VIR_DOMAIN_CONTROLLER_TYPE_SCSI);
5316 if (qemuSetSCSIControllerModel(def, qemuCaps, &model) < 0)
5319 if (model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC) {
5320 if (dev->info->addr.drive.target != 0) {
5321 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5322 _("target must be 0 for scsi host device "
5323 "if its controller model is 'lsilogic'"));
5327 if (dev->info->addr.drive.unit > 7) {
5328 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5329 _("unit must be not more than 7 for scsi host "
5330 "device if its controller model is 'lsilogic'"));
5335 virBufferAddLit(&buf, "scsi-generic");
5337 if (model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LSILOGIC) {
5338 virBufferAsprintf(&buf, ",bus=scsi%d.%d,scsi-id=%d",
5339 dev->info->addr.drive.controller,
5340 dev->info->addr.drive.bus,
5341 dev->info->addr.drive.unit);
5343 virBufferAsprintf(&buf, ",bus=scsi%d.0,channel=%d,scsi-id=%d,lun=%d",
5344 dev->info->addr.drive.controller,
5345 dev->info->addr.drive.bus,
5346 dev->info->addr.drive.target,
5347 dev->info->addr.drive.unit);
5350 virBufferAsprintf(&buf, ",drive=%s-%s,id=%s",
5351 virDomainDeviceAddressTypeToString(dev->info->type),
5352 dev->info->alias, dev->info->alias);
5354 if (dev->info->bootIndex)
5355 virBufferAsprintf(&buf, ",bootindex=%d", dev->info->bootIndex);
5357 if (virBufferError(&buf)) {
5358 virReportOOMError();
5362 return virBufferContentAndReset(&buf);
5364 virBufferFreeAndReset(&buf);
5368 /* This function outputs a -chardev command line option which describes only the
5369 * host side of the character device */
5371 qemuBuildChrChardevStr(virDomainChrSourceDefPtr dev, const char *alias,
5372 virQEMUCapsPtr qemuCaps)
5374 virBuffer buf = VIR_BUFFER_INITIALIZER;
5377 switch (dev->type) {
5378 case VIR_DOMAIN_CHR_TYPE_NULL:
5379 virBufferAsprintf(&buf, "null,id=char%s", alias);
5382 case VIR_DOMAIN_CHR_TYPE_VC:
5383 virBufferAsprintf(&buf, "vc,id=char%s", alias);
5386 case VIR_DOMAIN_CHR_TYPE_PTY:
5387 virBufferAsprintf(&buf, "pty,id=char%s", alias);
5390 case VIR_DOMAIN_CHR_TYPE_DEV:
5391 virBufferAsprintf(&buf, "%s,id=char%s,path=%s",
5392 STRPREFIX(alias, "parallel") ? "parport" : "tty",
5393 alias, dev->data.file.path);
5396 case VIR_DOMAIN_CHR_TYPE_FILE:
5397 virBufferAsprintf(&buf, "file,id=char%s,path=%s", alias,
5398 dev->data.file.path);
5401 case VIR_DOMAIN_CHR_TYPE_PIPE:
5402 virBufferAsprintf(&buf, "pipe,id=char%s,path=%s", alias,
5403 dev->data.file.path);
5406 case VIR_DOMAIN_CHR_TYPE_STDIO:
5407 virBufferAsprintf(&buf, "stdio,id=char%s", alias);
5410 case VIR_DOMAIN_CHR_TYPE_UDP: {
5411 const char *connectHost = dev->data.udp.connectHost;
5412 const char *bindHost = dev->data.udp.bindHost;
5413 const char *bindService = dev->data.udp.bindService;
5415 if (connectHost == NULL)
5417 if (bindHost == NULL)
5419 if (bindService == NULL)
5422 virBufferAsprintf(&buf,
5423 "udp,id=char%s,host=%s,port=%s,localaddr=%s,"
5427 dev->data.udp.connectService,
5428 bindHost, bindService);
5431 case VIR_DOMAIN_CHR_TYPE_TCP:
5432 telnet = dev->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
5433 virBufferAsprintf(&buf,
5434 "socket,id=char%s,host=%s,port=%s%s%s",
5437 dev->data.tcp.service,
5438 telnet ? ",telnet" : "",
5439 dev->data.tcp.listen ? ",server,nowait" : "");
5442 case VIR_DOMAIN_CHR_TYPE_UNIX:
5443 virBufferAsprintf(&buf,
5444 "socket,id=char%s,path=%s%s",
5447 dev->data.nix.listen ? ",server,nowait" : "");
5450 case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
5451 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV_SPICEVMC)) {
5452 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5453 _("spicevmc not supported in this QEMU binary"));
5456 virBufferAsprintf(&buf, "spicevmc,id=char%s,name=%s", alias,
5457 virDomainChrSpicevmcTypeToString(dev->data.spicevmc));
5460 case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
5461 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV_SPICEPORT)) {
5462 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5463 _("spiceport not supported in this QEMU binary"));
5466 virBufferAsprintf(&buf, "spiceport,id=char%s,name=%s", alias,
5467 dev->data.spiceport.channel);
5471 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5472 _("unsupported chardev '%s'"),
5473 virDomainChrTypeToString(dev->type));
5477 if (virBufferError(&buf)) {
5478 virReportOOMError();
5482 return virBufferContentAndReset(&buf);
5485 virBufferFreeAndReset(&buf);
5491 qemuBuildChrArgStr(virDomainChrSourceDefPtr dev, const char *prefix)
5493 virBuffer buf = VIR_BUFFER_INITIALIZER;
5496 virBufferAdd(&buf, prefix, strlen(prefix));
5498 switch ((enum virDomainChrType)dev->type) {
5499 case VIR_DOMAIN_CHR_TYPE_NULL:
5500 virBufferAddLit(&buf, "null");
5503 case VIR_DOMAIN_CHR_TYPE_VC:
5504 virBufferAddLit(&buf, "vc");
5507 case VIR_DOMAIN_CHR_TYPE_PTY:
5508 virBufferAddLit(&buf, "pty");
5511 case VIR_DOMAIN_CHR_TYPE_DEV:
5512 virBufferStrcat(&buf, dev->data.file.path, NULL);
5515 case VIR_DOMAIN_CHR_TYPE_FILE:
5516 virBufferAsprintf(&buf, "file:%s", dev->data.file.path);
5519 case VIR_DOMAIN_CHR_TYPE_PIPE:
5520 virBufferAsprintf(&buf, "pipe:%s", dev->data.file.path);
5523 case VIR_DOMAIN_CHR_TYPE_STDIO:
5524 virBufferAddLit(&buf, "stdio");
5527 case VIR_DOMAIN_CHR_TYPE_UDP: {
5528 const char *connectHost = dev->data.udp.connectHost;
5529 const char *bindHost = dev->data.udp.bindHost;
5530 const char *bindService = dev->data.udp.bindService;
5532 if (connectHost == NULL)
5534 if (bindHost == NULL)
5536 if (bindService == NULL)
5539 virBufferAsprintf(&buf, "udp:%s:%s@%s:%s",
5541 dev->data.udp.connectService,
5546 case VIR_DOMAIN_CHR_TYPE_TCP:
5547 if (dev->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET) {
5548 virBufferAsprintf(&buf, "telnet:%s:%s%s",
5550 dev->data.tcp.service,
5551 dev->data.tcp.listen ? ",server,nowait" : "");
5553 virBufferAsprintf(&buf, "tcp:%s:%s%s",
5555 dev->data.tcp.service,
5556 dev->data.tcp.listen ? ",server,nowait" : "");
5560 case VIR_DOMAIN_CHR_TYPE_UNIX:
5561 virBufferAsprintf(&buf, "unix:%s%s",
5563 dev->data.nix.listen ? ",server,nowait" : "");
5566 case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
5567 case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
5568 case VIR_DOMAIN_CHR_TYPE_NMDM:
5569 case VIR_DOMAIN_CHR_TYPE_LAST:
5573 if (virBufferError(&buf)) {
5574 virReportOOMError();
5578 return virBufferContentAndReset(&buf);
5581 virBufferFreeAndReset(&buf);
5587 qemuBuildVirtioSerialPortDevStr(virDomainChrDefPtr dev,
5588 virQEMUCapsPtr qemuCaps)
5590 virBuffer buf = VIR_BUFFER_INITIALIZER;
5591 switch (dev->deviceType) {
5592 case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
5593 virBufferAddLit(&buf, "virtconsole");
5595 case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
5596 /* Legacy syntax '-device spicevmc' */
5597 if (dev->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC &&
5598 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SPICEVMC)) {
5599 virBufferAddLit(&buf, "spicevmc");
5601 virBufferAddLit(&buf, "virtserialport");
5605 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5606 _("Cannot use virtio serial for parallel/serial devices"));
5610 if (dev->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE &&
5611 dev->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW &&
5612 dev->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390) {
5613 /* Check it's a virtio-serial address */
5614 if (dev->info.type !=
5615 VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_SERIAL)
5617 virReportError(VIR_ERR_INTERNAL_ERROR,
5618 "%s", _("virtio serial device has invalid address type"));
5622 virBufferAsprintf(&buf,
5623 ",bus=" QEMU_VIRTIO_SERIAL_PREFIX "%d.%d",
5624 dev->info.addr.vioserial.controller,
5625 dev->info.addr.vioserial.bus);
5626 virBufferAsprintf(&buf,
5628 dev->info.addr.vioserial.port);
5631 if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
5632 dev->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC &&
5634 STRNEQ(dev->target.name, "com.redhat.spice.0")) {
5635 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5636 _("Unsupported spicevmc target name '%s'"),
5641 if (!(dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
5642 dev->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC &&
5643 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SPICEVMC))) {
5644 virBufferAsprintf(&buf, ",chardev=char%s,id=%s",
5645 dev->info.alias, dev->info.alias);
5646 if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL) {
5647 virBufferAsprintf(&buf, ",name=%s", dev->target.name
5648 ? dev->target.name : "com.redhat.spice.0");
5651 virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
5653 if (virBufferError(&buf)) {
5654 virReportOOMError();
5658 return virBufferContentAndReset(&buf);
5661 virBufferFreeAndReset(&buf);
5666 qemuBuildSclpDevStr(virDomainChrDefPtr dev)
5668 virBuffer buf = VIR_BUFFER_INITIALIZER;
5669 if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE) {
5670 switch (dev->targetType) {
5671 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLP:
5672 virBufferAddLit(&buf, "sclpconsole");
5674 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLPLM:
5675 virBufferAddLit(&buf, "sclplmconsole");
5679 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5680 _("Cannot use slcp with devices other than console"));
5683 virBufferAsprintf(&buf, ",chardev=char%s,id=%s",
5684 dev->info.alias, dev->info.alias);
5685 if (virBufferError(&buf)) {
5686 virReportOOMError();
5690 return virBufferContentAndReset(&buf);
5693 virBufferFreeAndReset(&buf);
5699 qemuBuildRNGBackendArgs(virCommandPtr cmd,
5700 virDomainRNGDefPtr dev,
5701 virQEMUCapsPtr qemuCaps)
5703 virBuffer buf = VIR_BUFFER_INITIALIZER;
5704 char *backend = NULL;
5707 switch ((enum virDomainRNGBackend) dev->backend) {
5708 case VIR_DOMAIN_RNG_BACKEND_RANDOM:
5709 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_RANDOM)) {
5710 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5711 _("this qemu doesn't support the rng-random "
5716 virBufferAsprintf(&buf, "rng-random,id=%s", dev->info.alias);
5717 if (dev->source.file)
5718 virBufferAsprintf(&buf, ",filename=%s", dev->source.file);
5720 virCommandAddArg(cmd, "-object");
5721 virCommandAddArgBuffer(cmd, &buf);
5724 case VIR_DOMAIN_RNG_BACKEND_EGD:
5726 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_EGD)) {
5727 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5728 _("this qemu doesn't support the rng-egd "
5733 if (!(backend = qemuBuildChrChardevStr(dev->source.chardev,
5734 dev->info.alias, qemuCaps)))
5737 virCommandAddArgList(cmd, "-chardev", backend, NULL);
5739 virCommandAddArg(cmd, "-object");
5740 virCommandAddArgFormat(cmd, "rng-egd,chardev=char%s,id=%s",
5741 dev->info.alias, dev->info.alias);
5744 case VIR_DOMAIN_RNG_BACKEND_LAST:
5751 virBufferFreeAndReset(&buf);
5758 qemuBuildRNGDeviceArgs(virCommandPtr cmd,
5759 virDomainDefPtr def,
5760 virDomainRNGDefPtr dev,
5761 virQEMUCapsPtr qemuCaps)
5763 virBuffer buf = VIR_BUFFER_INITIALIZER;
5766 if (dev->model != VIR_DOMAIN_RNG_MODEL_VIRTIO ||
5767 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VIRTIO_RNG)) {
5768 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5769 _("this qemu doesn't support RNG device type '%s'"),
5770 virDomainRNGModelTypeToString(dev->model));
5774 if (dev->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW)
5775 virBufferAsprintf(&buf, "virtio-rng-ccw,rng=%s", dev->info.alias);
5776 else if (dev->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390)
5777 virBufferAsprintf(&buf, "virtio-rng-s390,rng=%s", dev->info.alias);
5778 else if (dev->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_MMIO)
5779 virBufferAsprintf(&buf, "virtio-rng-device,rng=%s", dev->info.alias);
5781 virBufferAsprintf(&buf, "virtio-rng-pci,rng=%s", dev->info.alias);
5783 if (dev->rate > 0) {
5784 virBufferAsprintf(&buf, ",max-bytes=%u", dev->rate);
5786 virBufferAsprintf(&buf, ",period=%u", dev->period);
5788 virBufferAddLit(&buf, ",period=1000");
5791 if (qemuBuildDeviceAddressStr(&buf, def, &dev->info, qemuCaps) < 0)
5794 virCommandAddArg(cmd, "-device");
5795 virCommandAddArgBuffer(cmd, &buf);
5800 virBufferFreeAndReset(&buf);
5805 static char *qemuBuildTPMBackendStr(const virDomainDef *def,
5806 virQEMUCapsPtr qemuCaps,
5807 const char *emulator)
5809 const virDomainTPMDef *tpm = def->tpm;
5810 virBuffer buf = VIR_BUFFER_INITIALIZER;
5811 const char *type = virDomainTPMBackendTypeToString(tpm->type);
5815 virBufferAsprintf(&buf, "%s,id=tpm-%s", type, tpm->info.alias);
5817 switch (tpm->type) {
5818 case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
5819 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_TPM_PASSTHROUGH))
5822 tpmdev = tpm->data.passthrough.source.data.file.path;
5823 if (!(cancel_path = virTPMCreateCancelPath(tpmdev)))
5826 virBufferAddLit(&buf, ",path=");
5827 virBufferEscape(&buf, ',', ",", "%s", tpmdev);
5829 virBufferAddLit(&buf, ",cancel-path=");
5830 virBufferEscape(&buf, ',', ",", "%s", cancel_path);
5831 VIR_FREE(cancel_path);
5834 case VIR_DOMAIN_TPM_TYPE_LAST:
5838 if (virBufferError(&buf)) {
5839 virReportOOMError();
5843 return virBufferContentAndReset(&buf);
5846 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5847 _("The QEMU executable %s does not support TPM "
5852 virBufferFreeAndReset(&buf);
5857 static char *qemuBuildTPMDevStr(const virDomainDef *def,
5858 virQEMUCapsPtr qemuCaps,
5859 const char *emulator)
5861 virBuffer buf = VIR_BUFFER_INITIALIZER;
5862 const virDomainTPMDef *tpm = def->tpm;
5863 const char *model = virDomainTPMModelTypeToString(tpm->model);
5865 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_TPM_TIS)) {
5866 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5867 _("The QEMU executable %s does not support TPM "
5873 virBufferAsprintf(&buf, "%s,tpmdev=tpm-%s,id=%s",
5874 model, tpm->info.alias, tpm->info.alias);
5876 if (virBufferError(&buf)) {
5877 virReportOOMError();
5881 return virBufferContentAndReset(&buf);
5884 virBufferFreeAndReset(&buf);
5889 static char *qemuBuildSmbiosBiosStr(virSysinfoDefPtr def)
5891 virBuffer buf = VIR_BUFFER_INITIALIZER;
5893 if ((def->bios_vendor == NULL) && (def->bios_version == NULL) &&
5894 (def->bios_date == NULL) && (def->bios_release == NULL))
5897 virBufferAddLit(&buf, "type=0");
5900 if (def->bios_vendor)
5901 virBufferAsprintf(&buf, ",vendor=%s", def->bios_vendor);
5902 /* 0:BIOS Version */
5903 if (def->bios_version)
5904 virBufferAsprintf(&buf, ",version=%s", def->bios_version);
5905 /* 0:BIOS Release Date */
5907 virBufferAsprintf(&buf, ",date=%s", def->bios_date);
5908 /* 0:System BIOS Major Release and 0:System BIOS Minor Release */
5909 if (def->bios_release)
5910 virBufferAsprintf(&buf, ",release=%s", def->bios_release);
5912 if (virBufferError(&buf)) {
5913 virReportOOMError();
5917 return virBufferContentAndReset(&buf);
5920 virBufferFreeAndReset(&buf);
5924 static char *qemuBuildSmbiosSystemStr(virSysinfoDefPtr def, bool skip_uuid)
5926 virBuffer buf = VIR_BUFFER_INITIALIZER;
5928 if ((def->system_manufacturer == NULL) && (def->system_sku == NULL) &&
5929 (def->system_product == NULL) && (def->system_version == NULL) &&
5930 (def->system_serial == NULL) && (def->system_family == NULL) &&
5931 (def->system_uuid == NULL || skip_uuid))
5934 virBufferAddLit(&buf, "type=1");
5936 /* 1:Manufacturer */
5937 if (def->system_manufacturer)
5938 virBufferAsprintf(&buf, ",manufacturer=%s",
5939 def->system_manufacturer);
5940 /* 1:Product Name */
5941 if (def->system_product)
5942 virBufferAsprintf(&buf, ",product=%s", def->system_product);
5944 if (def->system_version)
5945 virBufferAsprintf(&buf, ",version=%s", def->system_version);
5946 /* 1:Serial Number */
5947 if (def->system_serial)
5948 virBufferAsprintf(&buf, ",serial=%s", def->system_serial);
5950 if (def->system_uuid && !skip_uuid)
5951 virBufferAsprintf(&buf, ",uuid=%s", def->system_uuid);
5953 if (def->system_sku)
5954 virBufferAsprintf(&buf, ",sku=%s", def->system_sku);
5956 if (def->system_family)
5957 virBufferAsprintf(&buf, ",family=%s", def->system_family);
5959 if (virBufferError(&buf)) {
5960 virReportOOMError();
5964 return virBufferContentAndReset(&buf);
5967 virBufferFreeAndReset(&buf);
5972 qemuBuildClockArgStr(virDomainClockDefPtr def)
5974 virBuffer buf = VIR_BUFFER_INITIALIZER;
5976 switch (def->offset) {
5977 case VIR_DOMAIN_CLOCK_OFFSET_UTC:
5978 virBufferAddLit(&buf, "base=utc");
5981 case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
5982 case VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE:
5983 virBufferAddLit(&buf, "base=localtime");
5986 case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE: {
5987 time_t now = time(NULL);
5990 if (def->data.variable.basis == VIR_DOMAIN_CLOCK_BASIS_LOCALTIME) {
5993 /* in the case of basis='localtime', rather than trying to
5994 * keep that basis (and associated offset from UTC) in the
5995 * status and deal with adding in the difference each time
5996 * there is an RTC_CHANGE event, it is simpler and less
5997 * error prone to just convert the adjustment an offset
5998 * from UTC right now (and change the status to
5999 * "basis='utc' to reflect this). This eliminates
6000 * potential errors in both RTC_CHANGE events and in
6001 * migration (in the case that the status of DST, or the
6002 * timezone of the destination host, changed relative to
6005 if (virTimeLocalOffsetFromUTC(&localOffset) < 0)
6007 def->data.variable.adjustment += localOffset;
6008 def->data.variable.basis = VIR_DOMAIN_CLOCK_BASIS_UTC;
6011 now += def->data.variable.adjustment;
6012 gmtime_r(&now, &nowbits);
6014 /* when an RTC_CHANGE event is received from qemu, we need to
6015 * have the adjustment used at domain start time available to
6016 * compute the new offset from UTC. As this new value is
6017 * itself stored in def->data.variable.adjustment, we need to
6018 * save a copy of it now.
6020 def->data.variable.adjustment0 = def->data.variable.adjustment;
6022 virBufferAsprintf(&buf, "base=%d-%02d-%02dT%02d:%02d:%02d",
6023 nowbits.tm_year + 1900,
6032 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6033 _("unsupported clock offset '%s'"),
6034 virDomainClockOffsetTypeToString(def->offset));
6038 /* Look for an 'rtc' timer element, and add in appropriate clock= and driftfix= */
6040 for (i = 0; i < def->ntimers; i++) {
6041 if (def->timers[i]->name == VIR_DOMAIN_TIMER_NAME_RTC) {
6042 switch (def->timers[i]->track) {
6043 case -1: /* unspecified - use hypervisor default */
6045 case VIR_DOMAIN_TIMER_TRACK_BOOT:
6046 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6047 _("unsupported rtc timer track '%s'"),
6048 virDomainTimerTrackTypeToString(def->timers[i]->track));
6050 case VIR_DOMAIN_TIMER_TRACK_GUEST:
6051 virBufferAddLit(&buf, ",clock=vm");
6053 case VIR_DOMAIN_TIMER_TRACK_WALL:
6054 virBufferAddLit(&buf, ",clock=host");
6058 switch (def->timers[i]->tickpolicy) {
6060 case VIR_DOMAIN_TIMER_TICKPOLICY_DELAY:
6061 /* This is the default - missed ticks delivered when
6062 next scheduled, at normal rate */
6064 case VIR_DOMAIN_TIMER_TICKPOLICY_CATCHUP:
6065 /* deliver ticks at a faster rate until caught up */
6066 virBufferAddLit(&buf, ",driftfix=slew");
6068 case VIR_DOMAIN_TIMER_TICKPOLICY_MERGE:
6069 case VIR_DOMAIN_TIMER_TICKPOLICY_DISCARD:
6070 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6071 _("unsupported rtc timer tickpolicy '%s'"),
6072 virDomainTimerTickpolicyTypeToString(def->timers[i]->tickpolicy));
6075 break; /* no need to check other timers - there is only one rtc */
6079 if (virBufferError(&buf)) {
6080 virReportOOMError();
6084 return virBufferContentAndReset(&buf);
6087 virBufferFreeAndReset(&buf);
6093 qemuBuildCpuArgStr(virQEMUDriverPtr driver,
6094 const virDomainDef *def,
6095 const char *emulator,
6096 virQEMUCapsPtr qemuCaps,
6102 virCPUDefPtr host = NULL;
6103 virCPUDefPtr guest = NULL;
6104 virCPUDefPtr cpu = NULL;
6107 const char *default_model;
6108 virCPUDataPtr data = NULL;
6109 bool have_cpu = false;
6110 char *compare_msg = NULL;
6112 virBuffer buf = VIR_BUFFER_INITIALIZER;
6114 virCapsPtr caps = NULL;
6118 if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
6121 host = caps->host.cpu;
6123 if (def->os.arch == VIR_ARCH_I686)
6124 default_model = "qemu32";
6126 default_model = "qemu64";
6129 (def->cpu->mode != VIR_CPU_MODE_CUSTOM || def->cpu->model)) {
6130 virCPUCompareResult cmp;
6131 const char *preferred;
6135 (ncpus = virQEMUCapsGetCPUDefinitions(qemuCaps, &cpus)) == 0) {
6136 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6137 _("CPU specification not supported by hypervisor"));
6141 if (!(cpu = virCPUDefCopy(def->cpu)))
6144 if (cpu->mode != VIR_CPU_MODE_CUSTOM &&
6146 cpuUpdate(cpu, host) < 0)
6149 cmp = cpuGuestData(host, cpu, &data, &compare_msg);
6151 case VIR_CPU_COMPARE_INCOMPATIBLE:
6153 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6154 _("guest and host CPU are not compatible: %s"),
6157 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6158 _("guest CPU is not compatible with host CPU"));
6161 case VIR_CPU_COMPARE_ERROR:
6168 /* Only 'svm' requires --enable-nesting. The nested
6169 * 'vmx' patches now simply hook off the CPU features
6171 if (def->os.arch == VIR_ARCH_X86_64 ||
6172 def->os.arch == VIR_ARCH_I686) {
6173 int hasSVM = cpuHasFeature(data, "svm");
6176 *hasHwVirt = hasSVM > 0 ? true : false;
6179 if (cpu->mode == VIR_CPU_MODE_HOST_PASSTHROUGH) {
6180 const char *mode = virCPUModeTypeToString(cpu->mode);
6181 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CPU_HOST)) {
6182 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6183 _("CPU mode '%s' is not supported by QEMU"
6187 if (def->virtType != VIR_DOMAIN_VIRT_KVM) {
6188 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6189 _("CPU mode '%s' is only supported with kvm"),
6193 virBufferAddLit(&buf, "host");
6195 if (VIR_ALLOC(guest) < 0)
6197 if (VIR_STRDUP(guest->vendor_id, cpu->vendor_id) < 0)
6200 guest->arch = host->arch;
6201 if (cpu->match == VIR_CPU_MATCH_MINIMUM)
6202 preferred = host->model;
6204 preferred = cpu->model;
6206 guest->type = VIR_CPU_TYPE_GUEST;
6207 guest->fallback = cpu->fallback;
6208 if (cpuDecode(guest, data, (const char **)cpus, ncpus, preferred) < 0)
6211 virBufferAdd(&buf, guest->model, -1);
6212 if (guest->vendor_id)
6213 virBufferAsprintf(&buf, ",vendor=%s", guest->vendor_id);
6214 for (i = 0; i < guest->nfeatures; i++) {
6216 if (guest->features[i].policy == VIR_CPU_FEATURE_DISABLE)
6221 virBufferAsprintf(&buf, ",%c%s", sign, guest->features[i].name);
6227 * Need to force a 32-bit guest CPU type if
6229 * 1. guest OS is i686
6230 * 2. host OS is x86_64
6231 * 3. emulator is qemu-kvm or kvm
6235 * 1. guest OS is i686
6236 * 2. emulator is qemu-system-x86_64
6238 if (def->os.arch == VIR_ARCH_I686 &&
6239 ((hostarch == VIR_ARCH_X86_64 &&
6240 strstr(emulator, "kvm")) ||
6241 strstr(emulator, "x86_64"))) {
6242 virBufferAdd(&buf, default_model, -1);
6247 /* Handle paravirtual timers */
6248 for (i = 0; i < def->clock.ntimers; i++) {
6249 virDomainTimerDefPtr timer = def->clock.timers[i];
6251 if (timer->present == -1)
6254 if (timer->name == VIR_DOMAIN_TIMER_NAME_KVMCLOCK) {
6255 virBufferAsprintf(&buf, "%s,%ckvmclock",
6256 have_cpu ? "" : default_model,
6257 timer->present ? '+' : '-');
6259 } else if (timer->name == VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK &&
6261 virBufferAsprintf(&buf, "%s,hv_time",
6262 have_cpu ? "" : default_model);
6267 if (def->apic_eoi) {
6269 if (def->apic_eoi == VIR_DOMAIN_FEATURE_STATE_ON)
6274 virBufferAsprintf(&buf, "%s,%ckvm_pv_eoi",
6275 have_cpu ? "" : default_model,
6280 if (def->features[VIR_DOMAIN_FEATURE_PVSPINLOCK]) {
6282 if (def->features[VIR_DOMAIN_FEATURE_PVSPINLOCK] == VIR_DOMAIN_FEATURE_STATE_ON)
6287 virBufferAsprintf(&buf, "%s,%ckvm_pv_unhalt",
6288 have_cpu ? "" : default_model,
6293 if (def->features[VIR_DOMAIN_FEATURE_HYPERV] == VIR_DOMAIN_FEATURE_STATE_ON) {
6295 virBufferAdd(&buf, default_model, -1);
6299 for (i = 0; i < VIR_DOMAIN_HYPERV_LAST; i++) {
6300 switch ((enum virDomainHyperv) i) {
6301 case VIR_DOMAIN_HYPERV_RELAXED:
6302 case VIR_DOMAIN_HYPERV_VAPIC:
6303 if (def->hyperv_features[i] == VIR_DOMAIN_FEATURE_STATE_ON)
6304 virBufferAsprintf(&buf, ",hv_%s",
6305 virDomainHypervTypeToString(i));
6308 case VIR_DOMAIN_HYPERV_SPINLOCKS:
6309 if (def->hyperv_features[i] == VIR_DOMAIN_FEATURE_STATE_ON)
6310 virBufferAsprintf(&buf, ",hv_spinlocks=0x%x",
6311 def->hyperv_spinlocks);
6314 case VIR_DOMAIN_HYPERV_LAST:
6320 if (virBufferError(&buf)) {
6321 virReportOOMError();
6325 *opt = virBufferContentAndReset(&buf);
6330 VIR_FREE(compare_msg);
6332 virCPUDefFree(guest);
6334 virObjectUnref(caps);
6339 qemuBuildObsoleteAccelArg(virCommandPtr cmd,
6340 const virDomainDef *def,
6341 virQEMUCapsPtr qemuCaps)
6343 bool disableKQEMU = false;
6344 bool enableKQEMU = false;
6345 bool disableKVM = false;
6346 bool enableKVM = false;
6348 switch (def->virtType) {
6349 case VIR_DOMAIN_VIRT_QEMU:
6350 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KQEMU))
6351 disableKQEMU = true;
6352 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM))
6356 case VIR_DOMAIN_VIRT_KQEMU:
6357 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM))
6360 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KQEMU)) {
6362 } else if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_KQEMU)) {
6363 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6364 _("the QEMU binary does not support kqemu"));
6369 case VIR_DOMAIN_VIRT_KVM:
6370 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KQEMU))
6371 disableKQEMU = true;
6373 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KVM)) {
6375 } else if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM)) {
6376 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6377 _("the QEMU binary does not support kvm"));
6382 case VIR_DOMAIN_VIRT_XEN:
6383 /* XXX better check for xenner */
6387 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6388 _("the QEMU binary does not support %s"),
6389 virDomainVirtTypeToString(def->virtType));
6394 virCommandAddArg(cmd, "-no-kqemu");
6395 else if (enableKQEMU)
6396 virCommandAddArgList(cmd, "-enable-kqemu", "-kernel-kqemu", NULL);
6398 virCommandAddArg(cmd, "-no-kvm");
6400 virCommandAddArg(cmd, "-enable-kvm");
6406 qemuBuildMachineArgStr(virCommandPtr cmd,
6407 const virDomainDef *def,
6408 virQEMUCapsPtr qemuCaps)
6410 bool obsoleteAccel = false;
6412 /* This should *never* be NULL, since we always provide
6413 * a machine in the capabilities data for QEMU. So this
6414 * check is just here as a safety in case the unexpected
6416 if (!def->os.machine)
6419 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MACHINE_OPT)) {
6420 /* if no parameter to the machine type is needed, we still use
6421 * '-M' to keep the most of the compatibility with older versions.
6423 virCommandAddArgList(cmd, "-M", def->os.machine, NULL);
6424 if (def->mem.dump_core) {
6425 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6426 _("dump-guest-core is not available "
6427 "with this QEMU binary"));
6431 if (def->mem.nosharepages) {
6432 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6433 _("disable shared memory is not available "
6434 "with this QEMU binary"));
6438 obsoleteAccel = true;
6440 virBuffer buf = VIR_BUFFER_INITIALIZER;
6442 virCommandAddArg(cmd, "-machine");
6443 virBufferAdd(&buf, def->os.machine, -1);
6445 if (def->virtType == VIR_DOMAIN_VIRT_QEMU)
6446 virBufferAddLit(&buf, ",accel=tcg");
6447 else if (def->virtType == VIR_DOMAIN_VIRT_KVM)
6448 virBufferAddLit(&buf, ",accel=kvm");
6450 obsoleteAccel = true;
6452 /* To avoid the collision of creating USB controllers when calling
6453 * machine->init in QEMU, it needs to set usb=off
6455 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MACHINE_USB_OPT))
6456 virBufferAddLit(&buf, ",usb=off");
6458 if (def->mem.dump_core) {
6459 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DUMP_GUEST_CORE)) {
6460 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6461 _("dump-guest-core is not available "
6462 "with this QEMU binary"));
6463 virBufferFreeAndReset(&buf);
6467 virBufferAsprintf(&buf, ",dump-guest-core=%s",
6468 virDomainMemDumpTypeToString(def->mem.dump_core));
6471 if (def->mem.nosharepages) {
6472 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MEM_MERGE)) {
6473 virBufferAddLit(&buf, ",mem-merge=off");
6475 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6476 _("disable shared memory is not available "
6477 "with this QEMU binary"));
6478 virBufferFreeAndReset(&buf);
6483 virCommandAddArgBuffer(cmd, &buf);
6486 if (obsoleteAccel &&
6487 qemuBuildObsoleteAccelArg(cmd, def, qemuCaps) < 0)
6494 qemuBuildSmpArgStr(const virDomainDef *def,
6495 virQEMUCapsPtr qemuCaps)
6497 virBuffer buf = VIR_BUFFER_INITIALIZER;
6499 virBufferAsprintf(&buf, "%u", def->vcpus);
6501 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SMP_TOPOLOGY)) {
6502 if (def->vcpus != def->maxvcpus)
6503 virBufferAsprintf(&buf, ",maxcpus=%u", def->maxvcpus);
6504 /* sockets, cores, and threads are either all zero
6505 * or all non-zero, thus checking one of them is enough */
6506 if (def->cpu && def->cpu->sockets) {
6507 virBufferAsprintf(&buf, ",sockets=%u", def->cpu->sockets);
6508 virBufferAsprintf(&buf, ",cores=%u", def->cpu->cores);
6509 virBufferAsprintf(&buf, ",threads=%u", def->cpu->threads);
6512 virBufferAsprintf(&buf, ",sockets=%u", def->maxvcpus);
6513 virBufferAsprintf(&buf, ",cores=%u", 1);
6514 virBufferAsprintf(&buf, ",threads=%u", 1);
6516 } else if (def->vcpus != def->maxvcpus) {
6517 virBufferFreeAndReset(&buf);
6518 /* FIXME - consider hot-unplugging cpus after boot for older qemu */
6519 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6520 _("setting current vcpu count less than maximum is "
6521 "not supported with this QEMU binary"));
6525 if (virBufferError(&buf)) {
6526 virBufferFreeAndReset(&buf);
6527 virReportOOMError();
6531 return virBufferContentAndReset(&buf);
6535 qemuBuildNumaArgStr(const virDomainDef *def, virCommandPtr cmd)
6538 virBuffer buf = VIR_BUFFER_INITIALIZER;
6539 char *cpumask = NULL;
6542 for (i = 0; i < def->cpu->ncells; i++) {
6544 virCommandAddArg(cmd, "-numa");
6545 virBufferAsprintf(&buf, "node,nodeid=%d", def->cpu->cells[i].cellid);
6546 virBufferAddLit(&buf, ",cpus=");
6547 if (!(cpumask = virBitmapFormat(def->cpu->cells[i].cpumask)))
6550 /* Up through qemu 1.4, -numa does not accept a cpus
6551 * argument any more complex than start-stop.
6553 * XXX For qemu 1.5, the syntax has not yet been decided;
6554 * but when it is, we need a capability bit and
6555 * translation of our cpumask into the qemu syntax. */
6556 if (strchr(cpumask, ',')) {
6557 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6558 _("disjoint NUMA cpu ranges are not supported "
6562 virBufferAdd(&buf, cpumask, -1);
6563 def->cpu->cells[i].mem = VIR_DIV_UP(def->cpu->cells[i].mem,
6565 virBufferAsprintf(&buf, ",mem=%d", def->cpu->cells[i].mem / 1024);
6567 if (virBufferError(&buf)) {
6568 virReportOOMError();
6572 virCommandAddArgBuffer(cmd, &buf);
6578 virBufferFreeAndReset(&buf);
6584 qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg,
6586 virDomainDefPtr def,
6587 virQEMUCapsPtr qemuCaps,
6588 virDomainGraphicsDefPtr graphics)
6590 virBuffer opt = VIR_BUFFER_INITIALIZER;
6591 const char *listenNetwork;
6592 const char *listenAddr = NULL;
6593 char *netAddr = NULL;
6597 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNC)) {
6598 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6599 _("vnc graphics are not supported with this QEMU"));
6603 if (graphics->data.vnc.socket || cfg->vncAutoUnixSocket) {
6604 if (!graphics->data.vnc.socket &&
6605 virAsprintf(&graphics->data.vnc.socket,
6606 "%s/%s.vnc", cfg->libDir, def->name) == -1)
6609 virBufferAsprintf(&opt, "unix:%s", graphics->data.vnc.socket);
6611 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNC_COLON)) {
6612 switch (virDomainGraphicsListenGetType(graphics, 0)) {
6613 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS:
6614 listenAddr = virDomainGraphicsListenGetAddress(graphics, 0);
6617 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK:
6618 listenNetwork = virDomainGraphicsListenGetNetwork(graphics, 0);
6621 ret = networkGetNetworkAddress(listenNetwork, &netAddr);
6623 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6624 "%s", _("network-based listen not possible, "
6625 "network driver not present"));
6629 virReportError(VIR_ERR_XML_ERROR,
6630 _("listen network '%s' had no usable address"),
6634 listenAddr = netAddr;
6635 /* store the address we found in the <graphics> element so it will
6636 * show up in status. */
6637 if (virDomainGraphicsListenSetAddress(graphics, 0,
6638 listenAddr, -1, false) < 0)
6644 listenAddr = cfg->vncListen;
6646 escapeAddr = strchr(listenAddr, ':') != NULL;
6648 virBufferAsprintf(&opt, "[%s]", listenAddr);
6650 virBufferAdd(&opt, listenAddr, -1);
6651 virBufferAsprintf(&opt, ":%d",
6652 graphics->data.vnc.port - 5900);
6656 virBufferAsprintf(&opt, "%d",
6657 graphics->data.vnc.port - 5900);
6660 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNC_COLON)) {
6661 if (!graphics->data.vnc.socket &&
6662 graphics->data.vnc.websocket) {
6663 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNC_WEBSOCKET)) {
6664 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6665 _("VNC WebSockets are not supported "
6666 "with this QEMU binary"));
6669 virBufferAsprintf(&opt, ",websocket=%d", graphics->data.vnc.websocket);
6672 if (graphics->data.vnc.sharePolicy) {
6673 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VNC_SHARE_POLICY)) {
6674 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6675 _("vnc display sharing policy is not "
6676 "supported with this QEMU"));
6680 virBufferAsprintf(&opt, ",share=%s",
6681 virDomainGraphicsVNCSharePolicyTypeToString(
6682 graphics->data.vnc.sharePolicy));
6685 if (graphics->data.vnc.auth.passwd || cfg->vncPassword)
6686 virBufferAddLit(&opt, ",password");
6689 virBufferAddLit(&opt, ",tls");
6690 if (cfg->vncTLSx509verify)
6691 virBufferAsprintf(&opt, ",x509verify=%s", cfg->vncTLSx509certdir);
6693 virBufferAsprintf(&opt, ",x509=%s", cfg->vncTLSx509certdir);
6697 virBufferAddLit(&opt, ",sasl");
6699 if (cfg->vncSASLdir)
6700 virCommandAddEnvPair(cmd, "SASL_CONF_DIR", cfg->vncSASLdir);
6702 /* TODO: Support ACLs later */
6706 virCommandAddArg(cmd, "-vnc");
6707 virCommandAddArgBuffer(cmd, &opt);
6708 if (graphics->data.vnc.keymap)
6709 virCommandAddArgList(cmd, "-k", graphics->data.vnc.keymap, NULL);
6711 /* Unless user requested it, set the audio backend to none, to
6712 * prevent it opening the host OS audio devices, since that causes
6713 * security issues and might not work when using VNC.
6715 if (cfg->vncAllowHostAudio)
6716 virCommandAddEnvPassBlockSUID(cmd, "QEMU_AUDIO_DRV", NULL);
6718 virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=none");
6724 virBufferFreeAndReset(&opt);
6730 qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg,
6732 virQEMUCapsPtr qemuCaps,
6733 virDomainGraphicsDefPtr graphics)
6735 virBuffer opt = VIR_BUFFER_INITIALIZER;
6736 const char *listenNetwork;
6737 const char *listenAddr = NULL;
6738 char *netAddr = NULL;
6740 int defaultMode = graphics->data.spice.defaultMode;
6741 int port = graphics->data.spice.port;
6742 int tlsPort = graphics->data.spice.tlsPort;
6745 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SPICE)) {
6746 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6747 _("spice graphics are not supported with this QEMU"));
6751 if (port > 0 || tlsPort <= 0)
6752 virBufferAsprintf(&opt, "port=%u", port);
6755 if (!cfg->spiceTLS) {
6756 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6757 _("spice TLS port set in XML configuration,"
6758 " but TLS is disabled in qemu.conf"));
6762 virBufferAddChar(&opt, ',');
6763 virBufferAsprintf(&opt, "tls-port=%u", tlsPort);
6766 if (cfg->spiceSASL) {
6767 virBufferAddLit(&opt, ",sasl");
6769 if (cfg->spiceSASLdir)
6770 virCommandAddEnvPair(cmd, "SASL_CONF_PATH",
6773 /* TODO: Support ACLs later */
6776 switch (virDomainGraphicsListenGetType(graphics, 0)) {
6777 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS:
6778 listenAddr = virDomainGraphicsListenGetAddress(graphics, 0);
6781 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK:
6782 listenNetwork = virDomainGraphicsListenGetNetwork(graphics, 0);
6785 ret = networkGetNetworkAddress(listenNetwork, &netAddr);
6787 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6788 "%s", _("network-based listen not possible, "
6789 "network driver not present"));
6793 virReportError(VIR_ERR_XML_ERROR,
6794 _("listen network '%s' had no usable address"),
6798 listenAddr = netAddr;
6799 /* store the address we found in the <graphics> element so it will
6800 * show up in status. */
6801 if (virDomainGraphicsListenSetAddress(graphics, 0,
6802 listenAddr, -1, false) < 0)
6808 listenAddr = cfg->spiceListen;
6810 virBufferAsprintf(&opt, ",addr=%s", listenAddr);
6814 if (graphics->data.spice.mousemode) {
6815 switch (graphics->data.spice.mousemode) {
6816 case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_SERVER:
6817 virBufferAddLit(&opt, ",agent-mouse=off");
6819 case VIR_DOMAIN_GRAPHICS_SPICE_MOUSE_MODE_CLIENT:
6820 virBufferAddLit(&opt, ",agent-mouse=on");
6827 /* In the password case we set it via monitor command, to avoid
6828 * making it visible on CLI, so there's no use of password=XXX
6829 * in this bit of the code */
6830 if (!graphics->data.spice.auth.passwd &&
6831 !cfg->spicePassword)
6832 virBufferAddLit(&opt, ",disable-ticketing");
6835 virBufferAsprintf(&opt, ",x509-dir=%s", cfg->spiceTLSx509certdir);
6837 switch (defaultMode) {
6838 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE:
6839 virBufferAddLit(&opt, ",tls-channel=default");
6841 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
6842 virBufferAddLit(&opt, ",plaintext-channel=default");
6844 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
6849 for (i = 0; i < VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_LAST; i++) {
6850 switch (graphics->data.spice.channels[i]) {
6851 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE:
6853 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6854 _("spice secure channels set in XML configuration, "
6855 "but TLS port is not provided"));
6858 virBufferAsprintf(&opt, ",tls-channel=%s",
6859 virDomainGraphicsSpiceChannelNameTypeToString(i));
6862 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
6864 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6865 _("spice insecure channels set in XML "
6866 "configuration, but plain port is not provided"));
6869 virBufferAsprintf(&opt, ",plaintext-channel=%s",
6870 virDomainGraphicsSpiceChannelNameTypeToString(i));
6873 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
6874 switch (defaultMode) {
6875 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE:
6877 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6878 _("spice defaultMode secure requested in XML "
6879 "configuration but TLS port not provided"));
6884 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
6886 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6887 _("spice defaultMode insecure requested in XML "
6888 "configuration but plain port not provided"));
6893 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
6900 if (graphics->data.spice.image)
6901 virBufferAsprintf(&opt, ",image-compression=%s",
6902 virDomainGraphicsSpiceImageCompressionTypeToString(graphics->data.spice.image));
6903 if (graphics->data.spice.jpeg)
6904 virBufferAsprintf(&opt, ",jpeg-wan-compression=%s",
6905 virDomainGraphicsSpiceJpegCompressionTypeToString(graphics->data.spice.jpeg));
6906 if (graphics->data.spice.zlib)
6907 virBufferAsprintf(&opt, ",zlib-glz-wan-compression=%s",
6908 virDomainGraphicsSpiceZlibCompressionTypeToString(graphics->data.spice.zlib));
6909 if (graphics->data.spice.playback)
6910 virBufferAsprintf(&opt, ",playback-compression=%s",
6911 virDomainGraphicsSpicePlaybackCompressionTypeToString(graphics->data.spice.playback));
6912 if (graphics->data.spice.streaming)
6913 virBufferAsprintf(&opt, ",streaming-video=%s",
6914 virDomainGraphicsSpiceStreamingModeTypeToString(graphics->data.spice.streaming));
6915 if (graphics->data.spice.copypaste == VIR_DOMAIN_GRAPHICS_SPICE_CLIPBOARD_COPYPASTE_NO)
6916 virBufferAddLit(&opt, ",disable-copy-paste");
6917 if (graphics->data.spice.filetransfer == VIR_DOMAIN_GRAPHICS_SPICE_AGENT_FILE_TRANSFER_NO) {
6918 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SPICE_FILE_XFER_DISABLE)) {
6919 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
6920 _("This QEMU can't disable file transfers through spice"));
6923 virBufferAddLit(&opt, ",disable-agent-file-xfer");
6927 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SEAMLESS_MIGRATION)) {
6928 /* If qemu supports seamless migration turn it
6929 * unconditionally on. If migration destination
6930 * doesn't support it, it fallbacks to previous
6931 * migration algorithm silently. */
6932 virBufferAddLit(&opt, ",seamless-migration=on");
6935 virCommandAddArg(cmd, "-spice");
6936 virCommandAddArgBuffer(cmd, &opt);
6937 if (graphics->data.spice.keymap)
6938 virCommandAddArgList(cmd, "-k",
6939 graphics->data.spice.keymap, NULL);
6940 /* SPICE includes native support for tunnelling audio, so we
6941 * set the audio backend to point at SPICE's own driver
6943 virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=spice");
6949 virBufferFreeAndReset(&opt);
6954 qemuBuildGraphicsCommandLine(virQEMUDriverConfigPtr cfg,
6956 virDomainDefPtr def,
6957 virQEMUCapsPtr qemuCaps,
6958 virDomainGraphicsDefPtr graphics)
6960 switch ((enum virDomainGraphicsType) graphics->type) {
6961 case VIR_DOMAIN_GRAPHICS_TYPE_SDL:
6962 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_0_10) &&
6963 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_SDL)) {
6964 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6965 _("sdl not supported by '%s'"), def->emulator);
6969 if (graphics->data.sdl.xauth)
6970 virCommandAddEnvPair(cmd, "XAUTHORITY", graphics->data.sdl.xauth);
6971 if (graphics->data.sdl.display)
6972 virCommandAddEnvPair(cmd, "DISPLAY", graphics->data.sdl.display);
6973 if (graphics->data.sdl.fullscreen)
6974 virCommandAddArg(cmd, "-full-screen");
6976 /* If using SDL for video, then we should just let it
6977 * use QEMU's host audio drivers, possibly SDL too
6978 * User can set these two before starting libvirtd
6980 virCommandAddEnvPassBlockSUID(cmd, "QEMU_AUDIO_DRV", NULL);
6981 virCommandAddEnvPassBlockSUID(cmd, "SDL_AUDIODRIVER", NULL);
6983 /* New QEMU has this flag to let us explicitly ask for
6984 * SDL graphics. This is better than relying on the
6985 * default, since the default changes :-( */
6986 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SDL))
6987 virCommandAddArg(cmd, "-sdl");
6991 case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
6992 return qemuBuildGraphicsVNCCommandLine(cfg, cmd, def, qemuCaps, graphics);
6994 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
6995 return qemuBuildGraphicsSPICECommandLine(cfg, cmd, qemuCaps, graphics);
6997 case VIR_DOMAIN_GRAPHICS_TYPE_RDP:
6998 case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP:
6999 case VIR_DOMAIN_GRAPHICS_TYPE_LAST:
7000 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7001 _("unsupported graphics type '%s'"),
7002 virDomainGraphicsTypeToString(graphics->type));
7010 qemuBuildInterfaceCommandLine(virCommandPtr cmd,
7011 virQEMUDriverPtr driver,
7013 virDomainDefPtr def,
7014 virDomainNetDefPtr net,
7015 virQEMUCapsPtr qemuCaps,
7018 virNetDevVPortProfileOp vmop,
7022 char *nic = NULL, *host = NULL;
7025 int *vhostfd = NULL;
7026 int vhostfdSize = 0;
7027 char **tapfdName = NULL;
7028 char **vhostfdName = NULL;
7029 int actualType = virDomainNetGetActualType(net);
7032 if (actualType == VIR_DOMAIN_NET_TYPE_HOSTDEV) {
7033 /* NET_TYPE_HOSTDEV devices are really hostdev devices, so
7034 * their commandlines are constructed with other hostdevs.
7040 bootindex = net->info.bootIndex;
7042 /* Currently nothing besides TAP devices supports multiqueue. */
7043 if (net->driver.virtio.queues > 0 &&
7044 !(actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
7045 actualType == VIR_DOMAIN_NET_TYPE_BRIDGE)) {
7046 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7047 _("Multiqueue network is not supported for: %s"),
7048 virDomainNetTypeToString(actualType));
7052 if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
7053 actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
7054 tapfdSize = net->driver.virtio.queues;
7058 if (VIR_ALLOC_N(tapfd, tapfdSize) < 0 ||
7059 VIR_ALLOC_N(tapfdName, tapfdSize) < 0)
7062 memset(tapfd, -1, tapfdSize * sizeof(tapfd[0]));
7064 if (qemuNetworkIfaceConnect(def, conn, driver, net,
7065 qemuCaps, tapfd, &tapfdSize) < 0)
7067 } else if (actualType == VIR_DOMAIN_NET_TYPE_DIRECT) {
7068 if (VIR_ALLOC(tapfd) < 0 || VIR_ALLOC(tapfdName) < 0)
7071 tapfd[0] = qemuPhysIfaceConnect(def, driver, net,
7077 if ((actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
7078 actualType == VIR_DOMAIN_NET_TYPE_BRIDGE ||
7079 actualType == VIR_DOMAIN_NET_TYPE_ETHERNET ||
7080 actualType == VIR_DOMAIN_NET_TYPE_DIRECT) &&
7082 /* Attempt to use vhost-net mode for these types of
7084 vhostfdSize = net->driver.virtio.queues;
7088 if (VIR_ALLOC_N(vhostfd, vhostfdSize) < 0 ||
7089 VIR_ALLOC_N(vhostfdName, vhostfdSize))
7092 memset(vhostfd, -1, vhostfdSize * sizeof(vhostfd[0]));
7094 if (qemuOpenVhostNet(def, net, qemuCaps, vhostfd, &vhostfdSize) < 0)
7098 for (i = 0; i < tapfdSize; i++) {
7099 virCommandPassFD(cmd, tapfd[i],
7100 VIR_COMMAND_PASS_FD_CLOSE_PARENT);
7101 if (virAsprintf(&tapfdName[i], "%d", tapfd[i]) < 0)
7105 for (i = 0; i < vhostfdSize; i++) {
7106 virCommandPassFD(cmd, vhostfd[i],
7107 VIR_COMMAND_PASS_FD_CLOSE_PARENT);
7108 if (virAsprintf(&vhostfdName[i], "%d", vhostfd[i]) < 0)
7112 /* Possible combinations:
7114 * 1. Old way: -net nic,model=e1000,vlan=1 -net tap,vlan=1
7115 * 2. Semi-new: -device e1000,vlan=1 -net tap,vlan=1
7116 * 3. Best way: -netdev type=tap,id=netdev1 -device e1000,id=netdev1
7118 * NB, no support for -netdev without use of -device
7120 if (qemuDomainSupportsNetdev(def, qemuCaps, net)) {
7121 if (!(host = qemuBuildHostNetStr(net, driver,
7123 tapfdName, tapfdSize,
7124 vhostfdName, vhostfdSize)))
7126 virCommandAddArgList(cmd, "-netdev", host, NULL);
7128 if (qemuDomainSupportsNicdev(def, qemuCaps, net)) {
7129 if (!(nic = qemuBuildNicDevStr(def, net, vlan, bootindex,
7130 vhostfdSize, qemuCaps)))
7132 virCommandAddArgList(cmd, "-device", nic, NULL);
7134 if (!(nic = qemuBuildNicStr(net, "nic,", vlan)))
7136 virCommandAddArgList(cmd, "-net", nic, NULL);
7138 if (!qemuDomainSupportsNetdev(def, qemuCaps, net)) {
7139 if (!(host = qemuBuildHostNetStr(net, driver,
7141 tapfdName, tapfdSize,
7142 vhostfdName, vhostfdSize)))
7144 virCommandAddArgList(cmd, "-net", host, NULL);
7150 virErrorPtr saved_err = virSaveLastError();
7151 virDomainConfNWFilterTeardown(net);
7152 virSetError(saved_err);
7153 virFreeError(saved_err);
7155 for (i = 0; tapfd && i < tapfdSize && tapfd[i] >= 0; i++) {
7157 VIR_FORCE_CLOSE(tapfd[i]);
7159 VIR_FREE(tapfdName[i]);
7161 for (i = 0; vhostfd && i < vhostfdSize && vhostfd[i] >= 0; i++) {
7163 VIR_FORCE_CLOSE(vhostfd[i]);
7165 VIR_FREE(vhostfdName[i]);
7171 VIR_FREE(tapfdName);
7172 VIR_FREE(vhostfdName);
7177 qemuBuildChrDeviceCommandLine(virCommandPtr cmd,
7178 virDomainDefPtr def,
7179 virDomainChrDefPtr chr,
7180 virQEMUCapsPtr qemuCaps)
7182 char *devstr = NULL;
7184 if (qemuBuildChrDeviceStr(&devstr, def, chr, qemuCaps) < 0)
7187 virCommandAddArgList(cmd, "-device", devstr, NULL);
7192 qemuBuildCommandLineCallbacks buildCommandLineCallbacks = {
7193 .qemuGetSCSIDeviceSgName = virSCSIDeviceGetSgName,
7197 * Constructs a argv suitable for launching qemu with config defined
7198 * for a given virtual machine.
7200 * XXX 'conn' is only required to resolve network -> bridge name
7201 * figure out how to remove this requirement some day
7204 qemuBuildCommandLine(virConnectPtr conn,
7205 virQEMUDriverPtr driver,
7206 virDomainDefPtr def,
7207 virDomainChrSourceDefPtr monitor_chr,
7209 virQEMUCapsPtr qemuCaps,
7210 const char *migrateFrom,
7212 virDomainSnapshotObjPtr snapshot,
7213 virNetDevVPortProfileOp vmop,
7214 qemuBuildCommandLineCallbacksPtr callbacks,
7217 virErrorPtr originalError = NULL;
7219 const char *emulator;
7220 char uuid[VIR_UUID_STRING_BUFLEN];
7223 int last_good_net = -1;
7224 bool hasHwVirt = false;
7225 virCommandPtr cmd = NULL;
7226 bool allowReboot = true;
7227 bool emitBootindex = false;
7231 int usbcontroller = 0;
7232 int actualSerials = 0;
7233 bool usblegacy = false;
7236 /* We don't add an explicit IDE or FD controller because the
7237 * provided PIIX4 device already includes one. It isn't possible to
7240 * We don't add PCI root controller either, because it's implicit,
7241 * but we do add PCI bridges. */
7242 VIR_DOMAIN_CONTROLLER_TYPE_PCI,
7243 VIR_DOMAIN_CONTROLLER_TYPE_USB,
7244 VIR_DOMAIN_CONTROLLER_TYPE_SCSI,
7245 VIR_DOMAIN_CONTROLLER_TYPE_SATA,
7246 VIR_DOMAIN_CONTROLLER_TYPE_VIRTIO_SERIAL,
7247 VIR_DOMAIN_CONTROLLER_TYPE_CCID,
7249 virArch hostarch = virArchFromHost();
7250 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
7252 VIR_DEBUG("conn=%p driver=%p def=%p mon=%p json=%d "
7253 "qemuCaps=%p migrateFrom=%s migrateFD=%d "
7254 "snapshot=%p vmop=%d",
7255 conn, driver, def, monitor_chr, monitor_json,
7256 qemuCaps, migrateFrom, migrateFd, snapshot, vmop);
7258 virUUIDFormat(def->uuid, uuid);
7260 emulator = def->emulator;
7262 if (!cfg->privileged) {
7263 /* If we have no cgroups than we can have no tunings that
7266 if (def->mem.hard_limit || def->mem.soft_limit ||
7267 def->mem.min_guarantee || def->mem.swap_hard_limit) {
7268 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7269 _("Memory tuning is not available in session mode"));
7273 if (def->blkio.weight || def->blkio.ndevices) {
7274 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7275 _("Block I/O tuning is not available in session mode"));
7279 if (def->cputune.sharesSpecified || def->cputune.period ||
7280 def->cputune.quota || def->cputune.emulator_period ||
7281 def->cputune.emulator_quota) {
7282 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7283 _("CPU tuning is not available in session mode"));
7288 for (i = 0; i < def->ngraphics; ++i) {
7289 switch (def->graphics[i]->type) {
7290 case VIR_DOMAIN_GRAPHICS_TYPE_SDL:
7293 case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
7296 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
7303 * do not use boot=on for drives when not using KVM since this
7304 * is not supported at all in upstream QEmu.
7306 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) &&
7307 (def->virtType == VIR_DOMAIN_VIRT_QEMU))
7308 virQEMUCapsClear(qemuCaps, QEMU_CAPS_DRIVE_BOOT);
7310 cmd = virCommandNew(emulator);
7312 virCommandAddEnvPassCommon(cmd);
7314 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NAME)) {
7315 virCommandAddArg(cmd, "-name");
7316 if (cfg->setProcessName &&
7317 virQEMUCapsGet(qemuCaps, QEMU_CAPS_NAME_PROCESS)) {
7318 virCommandAddArgFormat(cmd, "%s,process=qemu:%s",
7319 def->name, def->name);
7321 virCommandAddArg(cmd, def->name);
7326 virCommandAddArg(cmd, "-S"); /* freeze CPU */
7328 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_FIPS))
7329 virCommandAddArg(cmd, "-enable-fips");
7331 if (qemuBuildMachineArgStr(cmd, def, qemuCaps) < 0)
7334 if (qemuBuildCpuArgStr(driver, def, emulator, qemuCaps,
7335 hostarch, &cpu, &hasHwVirt, !!migrateFrom) < 0)
7339 virCommandAddArgList(cmd, "-cpu", cpu, NULL);
7342 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NESTING) &&
7344 virCommandAddArg(cmd, "-enable-nesting");
7347 if (def->os.loader) {
7348 virCommandAddArg(cmd, "-bios");
7349 virCommandAddArg(cmd, def->os.loader);
7352 /* Set '-m MB' based on maxmem, because the lower 'memory' limit
7353 * is set post-startup using the balloon driver. If balloon driver
7354 * is not supported, then they're out of luck anyway. Update the
7355 * XML to reflect our rounding.
7357 virCommandAddArg(cmd, "-m");
7358 def->mem.max_balloon = VIR_DIV_UP(def->mem.max_balloon, 1024) * 1024;
7359 virCommandAddArgFormat(cmd, "%llu", def->mem.max_balloon / 1024);
7360 if (def->mem.hugepage_backed) {
7361 if (!cfg->hugetlbfsMount) {
7362 virReportError(VIR_ERR_INTERNAL_ERROR,
7363 "%s", _("hugetlbfs filesystem is not mounted"));
7366 if (!cfg->hugepagePath) {
7367 virReportError(VIR_ERR_INTERNAL_ERROR,
7368 "%s", _("hugepages are disabled by administrator config"));
7371 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MEM_PATH)) {
7372 virReportError(VIR_ERR_INTERNAL_ERROR,
7373 _("hugepage backing not supported by '%s'"),
7377 virCommandAddArgList(cmd, "-mem-prealloc", "-mem-path",
7378 cfg->hugepagePath, NULL);
7381 if (def->mem.locked && !virQEMUCapsGet(qemuCaps, QEMU_CAPS_MLOCK)) {
7382 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7383 _("memory locking not supported by QEMU binary"));
7386 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MLOCK)) {
7387 virCommandAddArg(cmd, "-realtime");
7388 virCommandAddArgFormat(cmd, "mlock=%s",
7389 def->mem.locked ? "on" : "off");
7391 mlock = def->mem.locked;
7393 virCommandAddArg(cmd, "-smp");
7394 if (!(smp = qemuBuildSmpArgStr(def, qemuCaps)))
7396 virCommandAddArg(cmd, smp);
7399 if (def->cpu && def->cpu->ncells)
7400 if (qemuBuildNumaArgStr(def, cmd) < 0)
7403 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_UUID))
7404 virCommandAddArgList(cmd, "-uuid", uuid, NULL);
7405 if (def->virtType == VIR_DOMAIN_VIRT_XEN ||
7406 STREQ(def->os.type, "xen") ||
7407 STREQ(def->os.type, "linux")) {
7408 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DOMID)) {
7409 virCommandAddArg(cmd, "-domid");
7410 virCommandAddArgFormat(cmd, "%d", def->id);
7411 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_XEN_DOMID)) {
7412 virCommandAddArg(cmd, "-xen-attach");
7413 virCommandAddArg(cmd, "-xen-domid");
7414 virCommandAddArgFormat(cmd, "%d", def->id);
7416 virReportError(VIR_ERR_INTERNAL_ERROR,
7417 _("qemu emulator '%s' does not support xen"),
7423 if ((def->os.smbios_mode != VIR_DOMAIN_SMBIOS_NONE) &&
7424 (def->os.smbios_mode != VIR_DOMAIN_SMBIOS_EMULATE)) {
7425 virSysinfoDefPtr source = NULL;
7426 bool skip_uuid = false;
7428 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SMBIOS_TYPE)) {
7429 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7430 _("the QEMU binary %s does not support smbios settings"),
7435 /* should we really error out or just warn in those cases ? */
7436 if (def->os.smbios_mode == VIR_DOMAIN_SMBIOS_HOST) {
7437 if (driver->hostsysinfo == NULL) {
7438 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7439 _("Host SMBIOS information is not available"));
7442 source = driver->hostsysinfo;
7443 /* Host and guest uuid must differ, by definition of UUID. */
7445 } else if (def->os.smbios_mode == VIR_DOMAIN_SMBIOS_SYSINFO) {
7446 if (def->sysinfo == NULL) {
7447 virReportError(VIR_ERR_XML_ERROR,
7448 _("Domain '%s' sysinfo are not available"),
7452 source = def->sysinfo;
7453 /* domain_conf guaranteed that system_uuid matches guest uuid. */
7455 if (source != NULL) {
7458 smbioscmd = qemuBuildSmbiosBiosStr(source);
7459 if (smbioscmd != NULL) {
7460 virCommandAddArgList(cmd, "-smbios", smbioscmd, NULL);
7461 VIR_FREE(smbioscmd);
7463 smbioscmd = qemuBuildSmbiosSystemStr(source, skip_uuid);
7464 if (smbioscmd != NULL) {
7465 virCommandAddArgList(cmd, "-smbios", smbioscmd, NULL);
7466 VIR_FREE(smbioscmd);
7472 * NB, -nographic *MUST* come before any serial, or monitor
7473 * or parallel port flags due to QEMU craziness, where it
7474 * decides to change the serial port & monitor to be on stdout
7475 * if you ask for nographic. So we have to make sure we override
7476 * these defaults ourselves...
7478 if (!def->graphics) {
7479 virCommandAddArg(cmd, "-nographic");
7481 if (cfg->nogfxAllowHostAudio)
7482 virCommandAddEnvPassBlockSUID(cmd, "QEMU_AUDIO_DRV", NULL);
7484 virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=none");
7487 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
7488 /* Disable global config files and default devices */
7489 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_USER_CONFIG))
7490 virCommandAddArg(cmd, "-no-user-config");
7491 else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NODEFCONFIG))
7492 virCommandAddArg(cmd, "-nodefconfig");
7493 virCommandAddArg(cmd, "-nodefaults");
7496 /* Serial graphics adapter */
7497 if (def->os.bios.useserial == VIR_DOMAIN_BIOS_USESERIAL_YES) {
7498 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
7499 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
7500 _("qemu does not support -device"));
7503 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SGA)) {
7504 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
7505 _("qemu does not support SGA"));
7508 if (!def->nserials) {
7509 virReportError(VIR_ERR_XML_ERROR, "%s",
7510 _("need at least one serial port to use SGA"));
7513 virCommandAddArgList(cmd, "-device", "sga", NULL);
7518 /* Use -chardev if it's available */
7519 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV)) {
7521 virCommandAddArg(cmd, "-chardev");
7522 if (!(chrdev = qemuBuildChrChardevStr(monitor_chr, "monitor",
7525 virCommandAddArg(cmd, chrdev);
7528 virCommandAddArg(cmd, "-mon");
7529 virCommandAddArgFormat(cmd,
7530 "chardev=charmonitor,id=monitor,mode=%s",
7531 monitor_json ? "control" : "readline");
7533 const char *prefix = NULL;
7535 prefix = "control,";
7537 virCommandAddArg(cmd, "-monitor");
7538 if (!(chrdev = qemuBuildChrArgStr(monitor_chr, prefix)))
7540 virCommandAddArg(cmd, chrdev);
7545 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_RTC)) {
7547 virCommandAddArg(cmd, "-rtc");
7548 if (!(rtcopt = qemuBuildClockArgStr(&def->clock)))
7550 virCommandAddArg(cmd, rtcopt);
7553 switch (def->clock.offset) {
7554 case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
7555 case VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE:
7556 virCommandAddArg(cmd, "-localtime");
7559 case VIR_DOMAIN_CLOCK_OFFSET_UTC:
7560 /* Nothing, its the default */
7564 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7565 _("unsupported clock offset '%s'"),
7566 virDomainClockOffsetTypeToString(def->clock.offset));
7570 if (def->clock.offset == VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE &&
7571 def->clock.data.timezone) {
7572 virCommandAddEnvPair(cmd, "TZ", def->clock.data.timezone);
7575 for (i = 0; i < def->clock.ntimers; i++) {
7576 switch ((enum virDomainTimerNameType) def->clock.timers[i]->name) {
7577 case VIR_DOMAIN_TIMER_NAME_PLATFORM:
7578 case VIR_DOMAIN_TIMER_NAME_TSC:
7579 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7580 _("unsupported timer type (name) '%s'"),
7581 virDomainTimerNameTypeToString(def->clock.timers[i]->name));
7584 case VIR_DOMAIN_TIMER_NAME_KVMCLOCK:
7585 case VIR_DOMAIN_TIMER_NAME_HYPERVCLOCK:
7586 /* Timers above are handled when building -cpu. */
7587 case VIR_DOMAIN_TIMER_NAME_LAST:
7590 case VIR_DOMAIN_TIMER_NAME_RTC:
7591 /* This has already been taken care of (in qemuBuildClockArgStr)
7592 if QEMU_CAPS_RTC is set (mutually exclusive with
7593 QEMUD_FLAG_RTC_TD_HACK) */
7594 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_RTC_TD_HACK)) {
7595 switch (def->clock.timers[i]->tickpolicy) {
7597 case VIR_DOMAIN_TIMER_TICKPOLICY_DELAY:
7598 /* the default - do nothing */
7600 case VIR_DOMAIN_TIMER_TICKPOLICY_CATCHUP:
7601 virCommandAddArg(cmd, "-rtc-td-hack");
7603 case VIR_DOMAIN_TIMER_TICKPOLICY_MERGE:
7604 case VIR_DOMAIN_TIMER_TICKPOLICY_DISCARD:
7605 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7606 _("unsupported rtc tickpolicy '%s'"),
7607 virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
7610 } else if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_RTC)
7611 && (def->clock.timers[i]->tickpolicy
7612 != VIR_DOMAIN_TIMER_TICKPOLICY_DELAY)
7613 && (def->clock.timers[i]->tickpolicy != -1)) {
7614 /* a non-default rtc policy was given, but there is no
7615 way to implement it in this version of qemu */
7616 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7617 _("unsupported rtc tickpolicy '%s'"),
7618 virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
7623 case VIR_DOMAIN_TIMER_NAME_PIT:
7624 switch (def->clock.timers[i]->tickpolicy) {
7626 case VIR_DOMAIN_TIMER_TICKPOLICY_DELAY:
7627 /* delay is the default if we don't have kernel
7628 (-no-kvm-pit), otherwise, the default is catchup. */
7629 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM_PIT_TICK_POLICY))
7630 virCommandAddArgList(cmd, "-global",
7631 "kvm-pit.lost_tick_policy=discard", NULL);
7632 else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_KVM_PIT))
7633 virCommandAddArg(cmd, "-no-kvm-pit-reinjection");
7635 case VIR_DOMAIN_TIMER_TICKPOLICY_CATCHUP:
7636 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_KVM_PIT) ||
7637 virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM_PIT_TICK_POLICY)) {
7638 /* do nothing - this is default for kvm-pit */
7639 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_TDF)) {
7640 /* -tdf switches to 'catchup' with userspace pit. */
7641 virCommandAddArg(cmd, "-tdf");
7643 /* can't catchup if we have neither pit mode */
7644 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7645 _("unsupported pit tickpolicy '%s'"),
7646 virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
7650 case VIR_DOMAIN_TIMER_TICKPOLICY_MERGE:
7651 case VIR_DOMAIN_TIMER_TICKPOLICY_DISCARD:
7652 /* no way to support these modes for pit in qemu */
7653 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7654 _("unsupported pit tickpolicy '%s'"),
7655 virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
7660 case VIR_DOMAIN_TIMER_NAME_HPET:
7661 /* the only meaningful attribute for hpet is "present". If
7662 * present is -1, that means it wasn't specified, and
7663 * should be left at the default for the
7664 * hypervisor. "default" when -no-hpet exists is "yes",
7665 * and when -no-hpet doesn't exist is "no". "confusing"?
7668 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_HPET)) {
7669 if (def->clock.timers[i]->present == 0)
7670 virCommandAddArg(cmd, "-no-hpet");
7672 /* no hpet timer available. The only possible action
7673 is to raise an error if present="yes" */
7674 if (def->clock.timers[i]->present == 1) {
7675 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7676 "%s", _("hpet timer is not supported"));
7684 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_REBOOT)) {
7685 /* Only add -no-reboot option if each event destroys domain */
7686 if (def->onReboot == VIR_DOMAIN_LIFECYCLE_DESTROY &&
7687 def->onPoweroff == VIR_DOMAIN_LIFECYCLE_DESTROY &&
7688 def->onCrash == VIR_DOMAIN_LIFECYCLE_DESTROY) {
7689 allowReboot = false;
7690 virCommandAddArg(cmd, "-no-reboot");
7694 /* If JSON monitor is enabled, we can receive an event
7695 * when QEMU stops. If we use no-shutdown, then we can
7696 * watch for this event and do a soft/warm reboot.
7698 if (monitor_json && allowReboot &&
7699 virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_SHUTDOWN)) {
7700 virCommandAddArg(cmd, "-no-shutdown");
7703 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_NO_ACPI)) {
7704 if (def->features[VIR_DOMAIN_FEATURE_ACPI] != VIR_DOMAIN_FEATURE_STATE_ON)
7705 virCommandAddArg(cmd, "-no-acpi");
7709 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DISABLE_S3)) {
7710 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7711 "%s", _("setting ACPI S3 not supported"));
7714 virCommandAddArg(cmd, "-global");
7715 virCommandAddArgFormat(cmd, "PIIX4_PM.disable_s3=%d",
7716 def->pm.s3 == VIR_DOMAIN_PM_STATE_DISABLED);
7720 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DISABLE_S4)) {
7721 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7722 "%s", _("setting ACPI S4 not supported"));
7725 virCommandAddArg(cmd, "-global");
7726 virCommandAddArgFormat(cmd, "PIIX4_PM.disable_s4=%d",
7727 def->pm.s4 == VIR_DOMAIN_PM_STATE_DISABLED);
7730 if (!def->os.bootloader) {
7731 int boot_nparams = 0;
7732 virBuffer boot_buf = VIR_BUFFER_INITIALIZER;
7734 * We prefer using explicit bootindex=N parameters for predictable
7735 * results even though domain XML doesn't use per device boot elements.
7736 * However, we can't use bootindex if boot menu was requested.
7738 if (!def->os.nBootDevs) {
7739 /* def->os.nBootDevs is guaranteed to be > 0 unless per-device boot
7740 * configuration is used
7742 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX)) {
7743 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7744 _("hypervisor lacks deviceboot feature"));
7747 emitBootindex = true;
7748 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX) &&
7749 (def->os.bootmenu != VIR_DOMAIN_BOOT_MENU_ENABLED ||
7750 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOT_MENU))) {
7751 emitBootindex = true;
7754 if (!emitBootindex) {
7755 char boot[VIR_DOMAIN_BOOT_LAST+1];
7757 for (i = 0; i < def->os.nBootDevs; i++) {
7758 switch (def->os.bootDevs[i]) {
7759 case VIR_DOMAIN_BOOT_CDROM:
7762 case VIR_DOMAIN_BOOT_FLOPPY:
7765 case VIR_DOMAIN_BOOT_DISK:
7768 case VIR_DOMAIN_BOOT_NET:
7776 boot[def->os.nBootDevs] = '\0';
7778 virBufferAsprintf(&boot_buf, "%s", boot);
7782 if (def->os.bootmenu) {
7783 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOT_MENU)) {
7785 virBufferAddChar(&boot_buf, ',');
7787 if (def->os.bootmenu == VIR_DOMAIN_BOOT_MENU_ENABLED)
7788 virBufferAddLit(&boot_buf, "menu=on");
7790 virBufferAddLit(&boot_buf, "menu=off");
7792 /* We cannot emit an error when bootmenu is enabled but
7793 * unsupported because of backward compatibility */
7794 VIR_WARN("bootmenu is enabled but not "
7795 "supported by this QEMU binary");
7799 if (def->os.bios.rt_set) {
7800 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_REBOOT_TIMEOUT)) {
7801 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7802 _("reboot timeout is not supported "
7803 "by this QEMU binary"));
7804 virBufferFreeAndReset(&boot_buf);
7809 virBufferAddChar(&boot_buf, ',');
7811 virBufferAsprintf(&boot_buf,
7812 "reboot-timeout=%d",
7813 def->os.bios.rt_delay);
7816 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOT_STRICT)) {
7818 virBufferAddChar(&boot_buf, ',');
7819 virBufferAddLit(&boot_buf, "strict=on");
7822 if (boot_nparams > 0) {
7823 virCommandAddArg(cmd, "-boot");
7825 if (virBufferError(&boot_buf)) {
7826 virReportOOMError();
7830 if (boot_nparams < 2 || emitBootindex) {
7831 virCommandAddArgBuffer(cmd, &boot_buf);
7832 virBufferFreeAndReset(&boot_buf);
7834 char *str = virBufferContentAndReset(&boot_buf);
7835 virCommandAddArgFormat(cmd,
7843 virCommandAddArgList(cmd, "-kernel", def->os.kernel, NULL);
7845 virCommandAddArgList(cmd, "-initrd", def->os.initrd, NULL);
7846 if (def->os.cmdline)
7847 virCommandAddArgList(cmd, "-append", def->os.cmdline, NULL);
7849 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DTB)) {
7850 virCommandAddArgList(cmd, "-dtb", def->os.dtb, NULL);
7852 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7853 _("dtb is not supported with this QEMU binary"));
7858 virCommandAddArgList(cmd, "-bootloader", def->os.bootloader, NULL);
7861 for (i = 0; i < def->ncontrollers; i++) {
7862 virDomainControllerDefPtr cont = def->controllers[i];
7863 if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI &&
7864 cont->opts.pciopts.pcihole64) {
7865 const char *hoststr = NULL;
7867 bool machine = false;
7869 switch (cont->model) {
7870 case VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT:
7871 hoststr = "i440FX-pcihost";
7872 cap = virQEMUCapsGet(qemuCaps, QEMU_CAPS_I440FX_PCI_HOLE64_SIZE);
7873 machine = qemuDomainMachineIsI440FX(def);
7876 case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT:
7877 hoststr = "q35-pcihost";
7878 cap = virQEMUCapsGet(qemuCaps, QEMU_CAPS_Q35_PCI_HOLE64_SIZE);
7879 machine = qemuDomainMachineIsQ35(def);
7883 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
7884 _("64-bit PCI hole setting is only for root"
7885 " PCI controllers"));
7890 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7891 _("Setting the 64-bit PCI hole size is not "
7892 "supported for machine '%s'"), def->os.machine);
7896 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7897 _("64-bit PCI hole size setting is not supported "
7898 "with this QEMU binary"));
7902 virCommandAddArg(cmd, "-global");
7903 virCommandAddArgFormat(cmd, "%s.pci-hole64-size=%luK", hoststr,
7904 cont->opts.pciopts.pcihole64size);
7908 for (i = 0; i < def->ndisks; i++) {
7909 virDomainDiskDefPtr disk = def->disks[i];
7911 if (disk->src.driverName != NULL &&
7912 !STREQ(disk->src.driverName, "qemu")) {
7913 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
7914 _("unsupported driver name '%s' for disk '%s'"),
7915 disk->src.driverName, disk->src.path);
7920 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
7921 for (j = 0; j < ARRAY_CARDINALITY(contOrder); j++) {
7922 for (i = 0; i < def->ncontrollers; i++) {
7923 virDomainControllerDefPtr cont = def->controllers[i];
7925 if (cont->type != contOrder[j])
7928 /* Also, skip USB controllers with type none.*/
7929 if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
7930 cont->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_NONE) {
7931 usbcontroller = -1; /* mark we don't want a controller */
7935 /* Skip pci-root/pcie-root */
7936 if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI &&
7937 (cont->model == VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT ||
7938 cont->model == VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT)) {
7942 /* Only recent QEMU implements a SATA (AHCI) controller */
7943 if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_SATA) {
7944 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_ICH9_AHCI)) {
7945 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7946 _("SATA is not supported with this "
7949 } else if (cont->idx == 0 && qemuDomainMachineIsQ35(def)) {
7950 /* first SATA controller on Q35 machines is implicit */
7955 virCommandAddArg(cmd, "-device");
7956 if (!(devstr = qemuBuildControllerDevStr(def, cont,
7960 virCommandAddArg(cmd, devstr);
7963 } else if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
7964 cont->model == -1 &&
7965 !qemuDomainMachineIsQ35(def) &&
7966 (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_PIIX3_USB_UHCI) ||
7967 (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_OHCI) &&
7968 def->os.arch == VIR_ARCH_PPC64))) {
7970 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
7971 _("Multiple legacy USB controllers are "
7977 virCommandAddArg(cmd, "-device");
7980 if (!(devstr = qemuBuildControllerDevStr(def, cont, qemuCaps,
7984 virCommandAddArg(cmd, devstr);
7991 if (usbcontroller == 0 && !qemuDomainMachineIsQ35(def))
7992 virCommandAddArg(cmd, "-usb");
7994 for (i = 0; i < def->nhubs; i++) {
7995 virDomainHubDefPtr hub = def->hubs[i];
7998 virCommandAddArg(cmd, "-device");
7999 if (!(optstr = qemuBuildHubDevStr(def, hub, qemuCaps)))
8001 virCommandAddArg(cmd, optstr);
8005 /* If QEMU supports -drive param instead of old -hda, -hdb, -cdrom .. */
8006 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE)) {
8007 int bootCD = 0, bootFloppy = 0, bootDisk = 0;
8009 if ((virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE_BOOT) || emitBootindex)) {
8010 /* bootDevs will get translated into either bootindex=N or boot=on
8011 * depending on what qemu supports */
8012 for (i = 0; i < def->os.nBootDevs; i++) {
8013 switch (def->os.bootDevs[i]) {
8014 case VIR_DOMAIN_BOOT_CDROM:
8017 case VIR_DOMAIN_BOOT_FLOPPY:
8020 case VIR_DOMAIN_BOOT_DISK:
8027 for (i = 0; i < def->ndisks; i++) {
8030 virDomainDiskDefPtr disk = def->disks[i];
8031 bool withDeviceArg = false;
8032 bool deviceFlagMasked = false;
8034 /* Unless we have -device, then USB disks need special
8036 if ((disk->bus == VIR_DOMAIN_DISK_BUS_USB) &&
8037 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8038 if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
8039 virCommandAddArg(cmd, "-usbdevice");
8040 virCommandAddArgFormat(cmd, "disk:%s", disk->src.path);
8042 virReportError(VIR_ERR_INTERNAL_ERROR,
8043 _("unsupported usb disk type for '%s'"),
8050 switch (disk->device) {
8051 case VIR_DOMAIN_DISK_DEVICE_CDROM:
8055 case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
8056 bootindex = bootFloppy;
8059 case VIR_DOMAIN_DISK_DEVICE_DISK:
8060 case VIR_DOMAIN_DISK_DEVICE_LUN:
8061 bootindex = bootDisk;
8066 virCommandAddArg(cmd, "-drive");
8068 /* Unfortunately it is not possible to use
8069 -device for floppies, xen PV, or SD
8070 devices. Fortunately, those don't need
8071 static PCI addresses, so we don't really
8072 care that we can't use -device */
8073 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8074 if (disk->bus != VIR_DOMAIN_DISK_BUS_XEN &&
8075 disk->bus != VIR_DOMAIN_DISK_BUS_SD) {
8076 withDeviceArg = true;
8078 virQEMUCapsClear(qemuCaps, QEMU_CAPS_DEVICE);
8079 deviceFlagMasked = true;
8082 optstr = qemuBuildDriveStr(conn, disk,
8083 emitBootindex ? false : !!bootindex,
8085 if (deviceFlagMasked)
8086 virQEMUCapsSet(qemuCaps, QEMU_CAPS_DEVICE);
8089 virCommandAddArg(cmd, optstr);
8094 else if (disk->info.bootIndex)
8095 bootindex = disk->info.bootIndex;
8097 if (withDeviceArg) {
8098 if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
8099 virCommandAddArg(cmd, "-global");
8100 virCommandAddArgFormat(cmd, "isa-fdc.drive%c=drive-%s",
8101 disk->info.addr.drive.unit
8106 virCommandAddArg(cmd, "-global");
8107 virCommandAddArgFormat(cmd, "isa-fdc.bootindex%c=%d",
8108 disk->info.addr.drive.unit
8113 virCommandAddArg(cmd, "-device");
8115 if (!(optstr = qemuBuildDriveDevStr(def, disk, bootindex,
8118 virCommandAddArg(cmd, optstr);
8124 for (i = 0; i < def->ndisks; i++) {
8128 virDomainDiskDefPtr disk = def->disks[i];
8130 if ((disk->src.type == VIR_STORAGE_TYPE_BLOCK) &&
8131 (disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
8132 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8133 _("tray status 'open' is invalid for "
8134 "block type disk"));
8138 if (disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
8139 if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
8140 virCommandAddArg(cmd, "-usbdevice");
8141 virCommandAddArgFormat(cmd, "disk:%s", disk->src.path);
8143 virReportError(VIR_ERR_INTERNAL_ERROR,
8144 _("unsupported usb disk type for '%s'"),
8151 if (STREQ(disk->dst, "hdc") &&
8152 disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
8153 if (disk->src.path) {
8154 snprintf(dev, NAME_MAX, "-%s", "cdrom");
8159 if (STRPREFIX(disk->dst, "hd") ||
8160 STRPREFIX(disk->dst, "fd")) {
8161 snprintf(dev, NAME_MAX, "-%s", disk->dst);
8163 virReportError(VIR_ERR_INTERNAL_ERROR,
8164 _("unsupported disk type '%s'"), disk->dst);
8169 if (disk->src.type == VIR_STORAGE_TYPE_DIR) {
8170 /* QEMU only supports magic FAT format for now */
8171 if (disk->src.format > 0 &&
8172 disk->src.format != VIR_STORAGE_FILE_FAT) {
8173 virReportError(VIR_ERR_INTERNAL_ERROR,
8174 _("unsupported disk driver type for '%s'"),
8175 virStorageFileFormatTypeToString(disk->src.format));
8178 if (!disk->readonly) {
8179 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
8180 _("cannot create virtual FAT disks in read-write mode"));
8183 if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
8184 fmt = "fat:floppy:%s";
8188 if (virAsprintf(&file, fmt, disk->src) < 0)
8190 } else if (disk->src.type == VIR_STORAGE_TYPE_NETWORK) {
8191 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
8192 _("network disks are only supported with -drive"));
8195 if (VIR_STRDUP(file, disk->src.path) < 0) {
8200 /* Don't start with source if the tray is open for
8201 * CDROM and Floppy device.
8203 if (!((disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY ||
8204 disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
8205 disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN))
8206 virCommandAddArgList(cmd, dev, file, NULL);
8211 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_FSDEV)) {
8212 for (i = 0; i < def->nfss; i++) {
8214 virDomainFSDefPtr fs = def->fss[i];
8216 virCommandAddArg(cmd, "-fsdev");
8217 if (!(optstr = qemuBuildFSStr(fs, qemuCaps)))
8219 virCommandAddArg(cmd, optstr);
8222 virCommandAddArg(cmd, "-device");
8223 if (!(optstr = qemuBuildFSDevStr(def, fs, qemuCaps)))
8225 virCommandAddArg(cmd, optstr);
8230 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8231 _("filesystem passthrough not supported by this QEMU"));
8237 /* If we have -device, then we set -nodefault already */
8238 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
8239 virCommandAddArgList(cmd, "-net", "none", NULL);
8243 if (emitBootindex) {
8244 /* convert <boot dev='network'/> to bootindex since we didn't emit
8247 for (i = 0; i < def->os.nBootDevs; i++) {
8248 if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_NET) {
8255 for (i = 0; i < def->nnets; i++) {
8256 virDomainNetDefPtr net = def->nets[i];
8259 /* VLANs are not used with -netdev, so don't record them */
8260 if (qemuDomainSupportsNetdev(def, qemuCaps, net))
8265 if (qemuBuildInterfaceCommandLine(cmd, driver, conn, def, net,
8266 qemuCaps, vlan, bootNet, vmop,
8274 if (def->nsmartcards) {
8275 /* -device usb-ccid was already emitted along with other
8276 * controllers. For now, qemu handles only one smartcard. */
8277 virDomainSmartcardDefPtr smartcard = def->smartcards[0];
8279 virBuffer opt = VIR_BUFFER_INITIALIZER;
8280 const char *database;
8282 if (def->nsmartcards > 1 ||
8283 smartcard->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCID ||
8284 smartcard->info.addr.ccid.controller != 0 ||
8285 smartcard->info.addr.ccid.slot != 0) {
8286 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8287 _("this QEMU binary lacks multiple smartcard "
8289 virBufferFreeAndReset(&opt);
8293 switch (smartcard->type) {
8294 case VIR_DOMAIN_SMARTCARD_TYPE_HOST:
8295 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
8296 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_CCID_EMULATED)) {
8297 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8298 _("this QEMU binary lacks smartcard host "
8303 virBufferAddLit(&opt, "ccid-card-emulated,backend=nss-emulated");
8306 case VIR_DOMAIN_SMARTCARD_TYPE_HOST_CERTIFICATES:
8307 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
8308 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_CCID_EMULATED)) {
8309 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8310 _("this QEMU binary lacks smartcard host "
8315 virBufferAddLit(&opt, "ccid-card-emulated,backend=certificates");
8316 for (j = 0; j < VIR_DOMAIN_SMARTCARD_NUM_CERTIFICATES; j++) {
8317 if (strchr(smartcard->data.cert.file[j], ',')) {
8318 virBufferFreeAndReset(&opt);
8319 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8320 _("invalid certificate name: %s"),
8321 smartcard->data.cert.file[j]);
8324 virBufferAsprintf(&opt, ",cert%zu=%s", j + 1,
8325 smartcard->data.cert.file[j]);
8327 if (smartcard->data.cert.database) {
8328 if (strchr(smartcard->data.cert.database, ',')) {
8329 virBufferFreeAndReset(&opt);
8330 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8331 _("invalid database name: %s"),
8332 smartcard->data.cert.database);
8335 database = smartcard->data.cert.database;
8337 database = VIR_DOMAIN_SMARTCARD_DEFAULT_DATABASE;
8339 virBufferAsprintf(&opt, ",db=%s", database);
8342 case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
8343 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
8344 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_CCID_PASSTHRU)) {
8345 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8346 _("this QEMU binary lacks smartcard "
8347 "passthrough mode support"));
8351 virCommandAddArg(cmd, "-chardev");
8352 if (!(devstr = qemuBuildChrChardevStr(&smartcard->data.passthru,
8353 smartcard->info.alias,
8355 virBufferFreeAndReset(&opt);
8358 virCommandAddArg(cmd, devstr);
8361 virBufferAsprintf(&opt, "ccid-card-passthru,chardev=char%s",
8362 smartcard->info.alias);
8366 virReportError(VIR_ERR_INTERNAL_ERROR,
8367 _("unexpected smartcard type %d"),
8369 virBufferFreeAndReset(&opt);
8372 virCommandAddArg(cmd, "-device");
8373 virBufferAsprintf(&opt, ",id=%s,bus=ccid0.0", smartcard->info.alias);
8374 virCommandAddArgBuffer(cmd, &opt);
8377 for (i = 0; i < def->nserials; i++) {
8378 virDomainChrDefPtr serial = def->serials[i];
8381 if (serial->source.type == VIR_DOMAIN_CHR_TYPE_SPICEPORT && !spice)
8384 /* Use -chardev with -device if they are available */
8385 if (virQEMUCapsSupportsChardev(def, qemuCaps, serial)) {
8386 virCommandAddArg(cmd, "-chardev");
8387 if (!(devstr = qemuBuildChrChardevStr(&serial->source,
8391 virCommandAddArg(cmd, devstr);
8394 if (qemuBuildChrDeviceCommandLine(cmd, def, serial, qemuCaps) < 0)
8397 virCommandAddArg(cmd, "-serial");
8398 if (!(devstr = qemuBuildChrArgStr(&serial->source, NULL)))
8400 virCommandAddArg(cmd, devstr);
8406 /* If we have -device, then we set -nodefault already */
8407 if (!actualSerials && !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
8408 virCommandAddArgList(cmd, "-serial", "none", NULL);
8410 if (!def->nparallels) {
8411 /* If we have -device, then we set -nodefault already */
8412 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
8413 virCommandAddArgList(cmd, "-parallel", "none", NULL);
8415 for (i = 0; i < def->nparallels; i++) {
8416 virDomainChrDefPtr parallel = def->parallels[i];
8419 /* Use -chardev with -device if they are available */
8420 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) &&
8421 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8422 virCommandAddArg(cmd, "-chardev");
8423 if (!(devstr = qemuBuildChrChardevStr(¶llel->source,
8424 parallel->info.alias,
8427 virCommandAddArg(cmd, devstr);
8430 if (qemuBuildChrDeviceCommandLine(cmd, def, parallel, qemuCaps) < 0)
8433 virCommandAddArg(cmd, "-parallel");
8434 if (!(devstr = qemuBuildChrArgStr(¶llel->source, NULL)))
8436 virCommandAddArg(cmd, devstr);
8442 for (i = 0; i < def->nchannels; i++) {
8443 virDomainChrDefPtr channel = def->channels[i];
8446 switch (channel->targetType) {
8447 case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_GUESTFWD:
8448 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
8449 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8450 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8451 "%s", _("guestfwd requires QEMU to support -chardev & -device"));
8455 virCommandAddArg(cmd, "-chardev");
8456 if (!(devstr = qemuBuildChrChardevStr(&channel->source,
8457 channel->info.alias,
8460 virCommandAddArg(cmd, devstr);
8463 if (qemuBuildChrDeviceStr(&devstr, def, channel, qemuCaps) < 0)
8465 virCommandAddArgList(cmd, "-netdev", devstr, NULL);
8469 case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO:
8470 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8471 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8472 _("virtio channel requires QEMU to support -device"));
8476 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SPICEVMC) &&
8477 channel->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC) {
8478 /* spicevmc was originally introduced via a -device
8479 * with a backend internal to qemu; although we prefer
8480 * the newer -chardev interface. */
8483 virCommandAddArg(cmd, "-chardev");
8484 if (!(devstr = qemuBuildChrChardevStr(&channel->source,
8485 channel->info.alias,
8488 virCommandAddArg(cmd, devstr);
8492 if (qemuBuildChrDeviceCommandLine(cmd, def, channel, qemuCaps) < 0)
8498 /* Explicit console devices */
8499 for (i = 0; i < def->nconsoles; i++) {
8500 virDomainChrDefPtr console = def->consoles[i];
8503 switch (console->targetType) {
8504 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLP:
8505 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLPLM:
8506 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8507 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8508 _("sclp console requires QEMU to support -device"));
8511 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SCLP_S390)) {
8512 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8513 _("sclp console requires QEMU to support s390-sclp"));
8517 virCommandAddArg(cmd, "-chardev");
8518 if (!(devstr = qemuBuildChrChardevStr(&console->source,
8519 console->info.alias,
8522 virCommandAddArg(cmd, devstr);
8525 if (qemuBuildChrDeviceCommandLine(cmd, def, console, qemuCaps) < 0)
8529 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_VIRTIO:
8530 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8531 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8532 _("virtio channel requires QEMU to support -device"));
8536 virCommandAddArg(cmd, "-chardev");
8537 if (!(devstr = qemuBuildChrChardevStr(&console->source,
8538 console->info.alias,
8541 virCommandAddArg(cmd, devstr);
8544 if (qemuBuildChrDeviceCommandLine(cmd, def, console, qemuCaps) < 0)
8548 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL:
8552 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8553 _("unsupported console target type %s"),
8554 NULLSTR(virDomainChrConsoleTargetTypeToString(console->targetType)));
8562 if (!(optstr = qemuBuildTPMBackendStr(def, qemuCaps, emulator)))
8565 virCommandAddArgList(cmd, "-tpmdev", optstr, NULL);
8568 if (!(optstr = qemuBuildTPMDevStr(def, qemuCaps, emulator)))
8571 virCommandAddArgList(cmd, "-device", optstr, NULL);
8575 for (i = 0; i < def->ninputs; i++) {
8576 virDomainInputDefPtr input = def->inputs[i];
8578 if (input->bus == VIR_DOMAIN_INPUT_BUS_USB) {
8579 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8581 virCommandAddArg(cmd, "-device");
8582 if (!(optstr = qemuBuildUSBInputDevStr(def, input, qemuCaps)))
8584 virCommandAddArg(cmd, optstr);
8587 switch (input->type) {
8588 case VIR_DOMAIN_INPUT_TYPE_MOUSE:
8589 virCommandAddArgList(cmd, "-usbdevice", "mouse", NULL);
8591 case VIR_DOMAIN_INPUT_TYPE_TABLET:
8592 virCommandAddArgList(cmd, "-usbdevice", "tablet", NULL);
8594 case VIR_DOMAIN_INPUT_TYPE_KBD:
8595 virCommandAddArgList(cmd, "-usbdevice", "keyboard", NULL);
8602 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_0_10) && sdl + vnc + spice > 1) {
8603 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8604 _("only 1 graphics device is supported"));
8607 if (sdl > 1 || vnc > 1 || spice > 1) {
8608 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8609 _("only 1 graphics device of each type "
8610 "(sdl, vnc, spice) is supported"));
8614 for (i = 0; i < def->ngraphics; ++i) {
8615 if (qemuBuildGraphicsCommandLine(cfg, cmd, def, qemuCaps,
8616 def->graphics[i]) < 0)
8620 if (def->nvideos > 0) {
8621 int primaryVideoType = def->videos[0]->type;
8622 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VIDEO_PRIMARY) &&
8623 ((primaryVideoType == VIR_DOMAIN_VIDEO_TYPE_VGA &&
8624 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VGA)) ||
8625 (primaryVideoType == VIR_DOMAIN_VIDEO_TYPE_CIRRUS &&
8626 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_CIRRUS_VGA)) ||
8627 (primaryVideoType == VIR_DOMAIN_VIDEO_TYPE_VMVGA &&
8628 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VMWARE_SVGA)) ||
8629 (primaryVideoType == VIR_DOMAIN_VIDEO_TYPE_QXL &&
8630 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_QXL_VGA)))
8632 for (i = 0; i < def->nvideos; i++) {
8634 virCommandAddArg(cmd, "-device");
8635 if (!(str = qemuBuildDeviceVideoStr(def, def->videos[i], qemuCaps, !i)))
8638 virCommandAddArg(cmd, str);
8641 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_VGA)) {
8642 if (primaryVideoType == VIR_DOMAIN_VIDEO_TYPE_XEN) {
8643 /* nothing - vga has no effect on Xen pvfb */
8645 if ((primaryVideoType == VIR_DOMAIN_VIDEO_TYPE_QXL) &&
8646 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_VGA_QXL)) {
8647 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8648 _("This QEMU does not support QXL graphics adapters"));
8652 const char *vgastr = qemuVideoTypeToString(primaryVideoType);
8653 if (!vgastr || STREQ(vgastr, "")) {
8654 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8655 _("video type %s is not supported with QEMU"),
8656 virDomainVideoTypeToString(primaryVideoType));
8660 virCommandAddArgList(cmd, "-vga", vgastr, NULL);
8662 if (def->videos[0]->type == VIR_DOMAIN_VIDEO_TYPE_QXL &&
8663 (def->videos[0]->vram || def->videos[0]->ram) &&
8664 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8665 const char *dev = (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_QXL_VGA)
8666 ? "qxl-vga" : "qxl");
8667 int ram = def->videos[0]->ram;
8668 int vram = def->videos[0]->vram;
8670 if (vram > (UINT_MAX / 1024)) {
8671 virReportError(VIR_ERR_OVERFLOW,
8672 _("value for 'vram' must be less than '%u'"),
8676 if (ram > (UINT_MAX / 1024)) {
8677 virReportError(VIR_ERR_OVERFLOW,
8678 _("value for 'ram' must be less than '%u'"),
8684 virCommandAddArg(cmd, "-global");
8685 virCommandAddArgFormat(cmd, "%s.ram_size=%u",
8689 virCommandAddArg(cmd, "-global");
8690 virCommandAddArgFormat(cmd, "%s.vram_size=%u",
8696 if (def->nvideos > 1) {
8697 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8698 for (i = 1; i < def->nvideos; i++) {
8700 if (def->videos[i]->type != VIR_DOMAIN_VIDEO_TYPE_QXL) {
8701 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8702 _("video type %s is only valid as primary video card"),
8703 virDomainVideoTypeToString(def->videos[0]->type));
8707 virCommandAddArg(cmd, "-device");
8709 if (!(str = qemuBuildDeviceVideoStr(def, def->videos[i], qemuCaps, false)))
8712 virCommandAddArg(cmd, str);
8716 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8717 "%s", _("only one video card is currently supported"));
8723 switch (def->videos[0]->type) {
8724 case VIR_DOMAIN_VIDEO_TYPE_VGA:
8725 virCommandAddArg(cmd, "-std-vga");
8728 case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
8729 virCommandAddArg(cmd, "-vmwarevga");
8732 case VIR_DOMAIN_VIDEO_TYPE_XEN:
8733 case VIR_DOMAIN_VIDEO_TYPE_CIRRUS:
8734 /* No special args - this is the default */
8738 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8739 _("video type %s is not supported with this QEMU"),
8740 virDomainVideoTypeToString(def->videos[0]->type));
8744 if (def->nvideos > 1) {
8745 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
8746 "%s", _("only one video card is currently supported"));
8752 /* If we have -device, then we set -nodefault already */
8753 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE) &&
8754 virQEMUCapsGet(qemuCaps, QEMU_CAPS_VGA) &&
8755 virQEMUCapsGet(qemuCaps, QEMU_CAPS_VGA_NONE))
8756 virCommandAddArgList(cmd, "-vga", "none", NULL);
8759 /* Add sound hardware */
8761 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8762 for (i = 0; i < def->nsounds; i++) {
8763 virDomainSoundDefPtr sound = def->sounds[i];
8766 /* Sadly pcspk device doesn't use -device syntax. Fortunately
8767 * we don't need to set any PCI address on it, so we don't
8769 if (sound->model == VIR_DOMAIN_SOUND_MODEL_PCSPK) {
8770 virCommandAddArgList(cmd, "-soundhw", "pcspk", NULL);
8772 virCommandAddArg(cmd, "-device");
8773 if (!(str = qemuBuildSoundDevStr(def, sound, qemuCaps)))
8776 virCommandAddArg(cmd, str);
8778 if (sound->model == VIR_DOMAIN_SOUND_MODEL_ICH6 ||
8779 sound->model == VIR_DOMAIN_SOUND_MODEL_ICH9) {
8780 char *codecstr = NULL;
8782 for (j = 0; j < sound->ncodecs; j++) {
8783 virCommandAddArg(cmd, "-device");
8784 if (!(codecstr = qemuBuildSoundCodecStr(sound, sound->codecs[j], qemuCaps))) {
8788 virCommandAddArg(cmd, codecstr);
8792 virDomainSoundCodecDef codec = {
8793 VIR_DOMAIN_SOUND_CODEC_TYPE_DUPLEX,
8796 virCommandAddArg(cmd, "-device");
8797 if (!(codecstr = qemuBuildSoundCodecStr(sound, &codec, qemuCaps))) {
8801 virCommandAddArg(cmd, codecstr);
8810 if (VIR_ALLOC_N(modstr, size+1) < 0)
8813 for (i = 0; i < def->nsounds && size > 0; i++) {
8814 virDomainSoundDefPtr sound = def->sounds[i];
8815 const char *model = virDomainSoundModelTypeToString(sound->model);
8818 virReportError(VIR_ERR_INTERNAL_ERROR,
8819 "%s", _("invalid sound model"));
8823 if (sound->model == VIR_DOMAIN_SOUND_MODEL_ICH6 ||
8824 sound->model == VIR_DOMAIN_SOUND_MODEL_ICH9) {
8826 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8827 _("this QEMU binary lacks hda support"));
8831 strncat(modstr, model, size);
8832 size -= strlen(model);
8833 if (i < (def->nsounds - 1))
8834 strncat(modstr, ",", size--);
8836 virCommandAddArgList(cmd, "-soundhw", modstr, NULL);
8841 /* Add watchdog hardware */
8842 if (def->watchdog) {
8843 virDomainWatchdogDefPtr watchdog = def->watchdog;
8846 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8847 virCommandAddArg(cmd, "-device");
8849 optstr = qemuBuildWatchdogDevStr(def, watchdog, qemuCaps);
8853 virCommandAddArg(cmd, "-watchdog");
8855 const char *model = virDomainWatchdogModelTypeToString(watchdog->model);
8857 virReportError(VIR_ERR_INTERNAL_ERROR,
8858 "%s", _("missing watchdog model"));
8862 if (VIR_STRDUP(optstr, model) < 0)
8865 virCommandAddArg(cmd, optstr);
8868 int act = watchdog->action;
8869 if (act == VIR_DOMAIN_WATCHDOG_ACTION_DUMP)
8870 act = VIR_DOMAIN_WATCHDOG_ACTION_PAUSE;
8871 const char *action = virDomainWatchdogActionTypeToString(act);
8873 virReportError(VIR_ERR_INTERNAL_ERROR,
8874 "%s", _("invalid watchdog action"));
8877 virCommandAddArgList(cmd, "-watchdog-action", action, NULL);
8880 /* Add redirected devices */
8881 for (i = 0; i < def->nredirdevs; i++) {
8882 virDomainRedirdevDefPtr redirdev = def->redirdevs[i];
8885 virCommandAddArg(cmd, "-chardev");
8886 if (!(devstr = qemuBuildChrChardevStr(&redirdev->source.chr,
8887 redirdev->info.alias,
8892 virCommandAddArg(cmd, devstr);
8895 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
8898 virCommandAddArg(cmd, "-device");
8899 if (!(devstr = qemuBuildRedirdevDevStr(def, redirdev, qemuCaps)))
8901 virCommandAddArg(cmd, devstr);
8905 /* Add host passthrough hardware */
8906 for (i = 0; i < def->nhostdevs; i++) {
8907 virDomainHostdevDefPtr hostdev = def->hostdevs[i];
8910 if (hostdev->info->bootIndex) {
8911 if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
8912 (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
8913 hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB &&
8914 hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI)) {
8915 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8916 _("booting from assigned devices is only "
8917 "supported for PCI, USB and SCSI devices"));
8920 if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
8921 if (hostdev->source.subsys.u.pci.backend
8922 == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
8923 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_VFIO_PCI_BOOTINDEX)) {
8924 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8925 _("booting from PCI devices assigned with VFIO "
8926 "is not supported with this version of qemu"));
8930 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_BOOTINDEX)) {
8931 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8932 _("booting from assigned PCI devices is not "
8933 "supported with this version of qemu"));
8938 if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB &&
8939 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_USB_HOST_BOOTINDEX)) {
8940 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8941 _("booting from assigned USB devices is not "
8942 "supported with this version of qemu"));
8945 if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI &&
8946 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SCSI_GENERIC_BOOTINDEX)) {
8947 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8948 _("booting from assigned SCSI devices is not"
8949 " supported with this version of qemu"));
8956 if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
8957 hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
8959 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8960 virCommandAddArg(cmd, "-device");
8961 if (!(devstr = qemuBuildUSBHostdevDevStr(def, hostdev, qemuCaps)))
8963 virCommandAddArg(cmd, devstr);
8966 virCommandAddArg(cmd, "-usbdevice");
8967 if (!(devstr = qemuBuildUSBHostdevUSBDevStr(hostdev)))
8969 virCommandAddArg(cmd, devstr);
8975 if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
8976 hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
8977 int backend = hostdev->source.subsys.u.pci.backend;
8979 if (backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
8980 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
8981 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
8982 _("VFIO PCI device assignment is not "
8983 "supported by this version of qemu"));
8986 /* VFIO requires all of the guest's memory to be locked
8991 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
8992 char *configfd_name = NULL;
8993 if ((backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) &&
8994 virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCI_CONFIGFD)) {
8995 int configfd = qemuOpenPCIConfig(hostdev);
8997 if (configfd >= 0) {
8998 if (virAsprintf(&configfd_name, "%d", configfd) < 0) {
8999 VIR_FORCE_CLOSE(configfd);
9003 virCommandPassFD(cmd, configfd,
9004 VIR_COMMAND_PASS_FD_CLOSE_PARENT);
9007 virCommandAddArg(cmd, "-device");
9008 devstr = qemuBuildPCIHostdevDevStr(def, hostdev, configfd_name, qemuCaps);
9009 VIR_FREE(configfd_name);
9012 virCommandAddArg(cmd, devstr);
9014 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCIDEVICE)) {
9015 virCommandAddArg(cmd, "-pcidevice");
9016 if (!(devstr = qemuBuildPCIHostdevPCIDevStr(hostdev, qemuCaps)))
9018 virCommandAddArg(cmd, devstr);
9021 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9022 _("PCI device assignment is not supported by this version of qemu"));
9028 if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
9029 hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI) {
9030 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DRIVE) &&
9031 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE) &&
9032 virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SCSI_GENERIC)) {
9035 virCommandAddArg(cmd, "-drive");
9036 if (!(drvstr = qemuBuildSCSIHostdevDrvStr(hostdev, qemuCaps, callbacks)))
9038 virCommandAddArg(cmd, drvstr);
9041 virCommandAddArg(cmd, "-device");
9042 if (!(devstr = qemuBuildSCSIHostdevDevStr(def, hostdev, qemuCaps)))
9044 virCommandAddArg(cmd, devstr);
9047 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9048 _("SCSI passthrough is not supported by this version of qemu"));
9054 /* Migration is very annoying due to wildly varying syntax &
9055 * capabilities over time of KVM / QEMU codebases.
9058 virCommandAddArg(cmd, "-incoming");
9059 if (STRPREFIX(migrateFrom, "tcp")) {
9060 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_QEMU_TCP)) {
9061 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9062 "%s", _("TCP migration is not supported with "
9063 "this QEMU binary"));
9066 virCommandAddArg(cmd, migrateFrom);
9067 } else if (STREQ(migrateFrom, "stdio")) {
9068 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_QEMU_FD)) {
9069 virCommandAddArgFormat(cmd, "fd:%d", migrateFd);
9070 virCommandPassFD(cmd, migrateFd, 0);
9071 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_QEMU_EXEC)) {
9072 virCommandAddArg(cmd, "exec:cat");
9073 virCommandSetInputFD(cmd, migrateFd);
9074 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_KVM_STDIO)) {
9075 virCommandAddArg(cmd, migrateFrom);
9076 virCommandSetInputFD(cmd, migrateFd);
9078 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9079 "%s", _("STDIO migration is not supported "
9080 "with this QEMU binary"));
9083 } else if (STRPREFIX(migrateFrom, "exec")) {
9084 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_QEMU_EXEC)) {
9085 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9086 "%s", _("EXEC migration is not supported "
9087 "with this QEMU binary"));
9090 virCommandAddArg(cmd, migrateFrom);
9091 } else if (STRPREFIX(migrateFrom, "fd")) {
9092 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_QEMU_FD)) {
9093 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9094 "%s", _("FD migration is not supported "
9095 "with this QEMU binary"));
9098 virCommandAddArg(cmd, migrateFrom);
9099 virCommandPassFD(cmd, migrateFd, 0);
9100 } else if (STRPREFIX(migrateFrom, "unix")) {
9101 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MIGRATE_QEMU_UNIX)) {
9102 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9103 "%s", _("UNIX migration is not supported "
9104 "with this QEMU binary"));
9107 virCommandAddArg(cmd, migrateFrom);
9109 virReportError(VIR_ERR_INTERNAL_ERROR,
9110 "%s", _("unknown migration protocol"));
9115 /* QEMU changed its default behavior to not include the virtio balloon
9116 * device. Explicitly request it to ensure it will be present.
9118 * NB: Earlier we declared that VirtIO balloon will always be in
9119 * slot 0x3 on bus 0x0
9121 if (STREQLEN(def->os.machine, "s390-virtio", 10) &&
9122 virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_S390) && def->memballoon)
9123 def->memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_NONE;
9125 if (def->memballoon &&
9126 def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_NONE) {
9127 if (def->memballoon->model != VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO) {
9128 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9129 _("Memory balloon device type '%s' is not supported by this version of qemu"),
9130 virDomainMemballoonModelTypeToString(def->memballoon->model));
9133 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
9135 virCommandAddArg(cmd, "-device");
9137 optstr = qemuBuildMemballoonDevStr(def, def->memballoon, qemuCaps);
9140 virCommandAddArg(cmd, optstr);
9142 } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BALLOON)) {
9143 virCommandAddArgList(cmd, "-balloon", "virtio", NULL);
9148 /* add the RNG source backend */
9149 if (qemuBuildRNGBackendArgs(cmd, def->rng, qemuCaps) < 0)
9152 /* add the device */
9153 if (qemuBuildRNGDeviceArgs(cmd, def, def->rng, qemuCaps) < 0)
9158 if (def->os.arch == VIR_ARCH_PPC64 &&
9159 STREQ(def->os.machine, "pseries")) {
9160 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_NVRAM)) {
9161 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9162 _("nvram device is not supported by "
9163 "this QEMU binary"));
9168 virCommandAddArg(cmd, "-global");
9169 optstr = qemuBuildNVRAMDevStr(def->nvram);
9173 virCommandAddArg(cmd, optstr);
9176 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9177 _("nvram device is only supported for PPC64"));
9182 virCommandAddArgList(cmd, "-loadvm", snapshot->def->name, NULL);
9184 if (def->namespaceData) {
9185 qemuDomainCmdlineDefPtr qemucmd;
9187 qemucmd = def->namespaceData;
9188 for (i = 0; i < qemucmd->num_args; i++)
9189 virCommandAddArg(cmd, qemucmd->args[i]);
9190 for (i = 0; i < qemucmd->num_env; i++)
9191 virCommandAddEnvPair(cmd, qemucmd->env_name[i],
9192 qemucmd->env_value[i]
9193 ? qemucmd->env_value[i] : "");
9196 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SECCOMP_SANDBOX)) {
9197 if (cfg->seccompSandbox == 0)
9198 virCommandAddArgList(cmd, "-sandbox", "off", NULL);
9199 else if (cfg->seccompSandbox > 0)
9200 virCommandAddArgList(cmd, "-sandbox", "on", NULL);
9201 } else if (cfg->seccompSandbox > 0) {
9202 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9203 _("QEMU does not support seccomp sandboxes"));
9208 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_PANIC)) {
9209 if (def->panic->info.addr.isa.iobase > 0) {
9210 virCommandAddArg(cmd, "-device");
9211 virCommandAddArgFormat(cmd, "pvpanic,ioport=%d",
9212 def->panic->info.addr.isa.iobase);
9214 virCommandAddArgList(cmd, "-device", "pvpanic", NULL);
9217 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9218 _("your QEMU is too old to support pvpanic"));
9224 unsigned long long memKB;
9226 /* VFIO requires all of the guest's memory to be
9227 * locked resident, plus some amount for IO
9228 * space. Alex Williamson suggested adding 1GiB for IO
9229 * space just to be safe (some finer tuning might be
9232 memKB = def->mem.hard_limit ?
9233 def->mem.hard_limit : def->mem.max_balloon + 1024 * 1024;
9234 virCommandSetMaxMemLock(cmd, memKB * 1024);
9237 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MSG_TIMESTAMP) &&
9239 virCommandAddArgList(cmd, "-msg", "timestamp=on", NULL);
9241 virObjectUnref(cfg);
9245 virObjectUnref(cfg);
9246 /* free up any resources in the network driver
9247 * but don't overwrite the original error */
9248 originalError = virSaveLastError();
9249 for (i = 0; last_good_net != -1 && i <= last_good_net; i++)
9250 virDomainConfNWFilterTeardown(def->nets[i]);
9251 virSetError(originalError);
9252 virFreeError(originalError);
9253 virCommandFree(cmd);
9257 /* This function generates the correct '-device' string for character
9258 * devices of each architecture.
9261 qemuBuildSerialChrDeviceStr(char **deviceStr,
9262 virDomainDefPtr def,
9263 virDomainChrDefPtr serial,
9264 virQEMUCapsPtr qemuCaps,
9268 virBuffer cmd = VIR_BUFFER_INITIALIZER;
9270 if ((arch == VIR_ARCH_PPC64) && STREQ(machine, "pseries")) {
9271 if (serial->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
9272 serial->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO) {
9273 virBufferAsprintf(&cmd, "spapr-vty,chardev=char%s",
9274 serial->info.alias);
9275 if (qemuBuildDeviceAddressStr(&cmd, def, &serial->info, qemuCaps) < 0)
9279 virBufferAsprintf(&cmd, "%s,chardev=char%s,id=%s",
9280 virDomainChrSerialTargetTypeToString(serial->targetType),
9281 serial->info.alias, serial->info.alias);
9283 if (serial->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB) {
9284 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_USB_SERIAL)) {
9285 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9286 _("usb-serial is not supported in this QEMU binary"));
9290 if (serial->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE &&
9291 serial->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB) {
9292 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
9293 _("usb-serial requires address of usb type"));
9297 if (qemuBuildDeviceAddressStr(&cmd, def, &serial->info, qemuCaps) < 0)
9302 if (virBufferError(&cmd)) {
9303 virReportOOMError();
9307 *deviceStr = virBufferContentAndReset(&cmd);
9311 virBufferFreeAndReset(&cmd);
9316 qemuBuildParallelChrDeviceStr(char **deviceStr,
9317 virDomainChrDefPtr chr)
9319 if (virAsprintf(deviceStr, "isa-parallel,chardev=char%s,id=%s",
9320 chr->info.alias, chr->info.alias) < 0) {
9321 virReportOOMError();
9328 qemuBuildChannelChrDeviceStr(char **deviceStr,
9329 virDomainChrDefPtr chr,
9330 virQEMUCapsPtr qemuCaps)
9336 switch ((enum virDomainChrChannelTargetType) chr->targetType) {
9337 case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_GUESTFWD:
9339 addr = virSocketAddrFormat(chr->target.addr);
9342 port = virSocketAddrGetPort(chr->target.addr);
9344 if (virAsprintf(deviceStr,
9345 "user,guestfwd=tcp:%s:%i,chardev=char%s,id=user-%s",
9346 addr, port, chr->info.alias, chr->info.alias) < 0) {
9347 virReportOOMError();
9352 case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO:
9353 if (!(*deviceStr = qemuBuildVirtioSerialPortDevStr(chr, qemuCaps)))
9357 case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_NONE:
9358 case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_LAST:
9369 qemuBuildConsoleChrDeviceStr(char **deviceStr,
9370 virDomainChrDefPtr chr,
9371 virQEMUCapsPtr qemuCaps)
9375 switch ((enum virDomainChrConsoleTargetType) chr->targetType) {
9376 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLP:
9377 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SCLPLM:
9378 if (!(*deviceStr = qemuBuildSclpDevStr(chr)))
9382 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_VIRTIO:
9383 if (!(*deviceStr = qemuBuildVirtioSerialPortDevStr(chr, qemuCaps)))
9387 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL:
9388 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE:
9389 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN:
9390 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_UML:
9391 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_LXC:
9392 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_OPENVZ:
9393 case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_LAST:
9403 qemuBuildChrDeviceStr(char **deviceStr,
9404 virDomainDefPtr vmdef,
9405 virDomainChrDefPtr chr,
9406 virQEMUCapsPtr qemuCaps)
9410 switch ((enum virDomainChrDeviceType) chr->deviceType) {
9411 case VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL:
9412 ret = qemuBuildSerialChrDeviceStr(deviceStr, vmdef, chr, qemuCaps,
9417 case VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL:
9418 ret = qemuBuildParallelChrDeviceStr(deviceStr, chr);
9421 case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
9422 ret = qemuBuildChannelChrDeviceStr(deviceStr, chr, qemuCaps);
9425 case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
9426 ret = qemuBuildConsoleChrDeviceStr(deviceStr, chr, qemuCaps);
9429 case VIR_DOMAIN_CHR_DEVICE_TYPE_LAST:
9438 * This method takes a string representing a QEMU command line ARGV set
9439 * optionally prefixed by a list of environment variables. It then tries
9440 * to split it up into a NULL terminated list of env & argv, splitting
9443 static int qemuStringToArgvEnv(const char *args,
9447 char **arglist = NULL;
9448 size_t argcount = 0;
9449 size_t argalloc = 0;
9452 const char *curr = args;
9454 char **progenv = NULL;
9455 char **progargv = NULL;
9457 /* Iterate over string, splitting on sequences of ' ' */
9458 while (curr && *curr != '\0') {
9463 /* accept a space in CEPH_ARGS */
9464 if (STRPREFIX(curr, "CEPH_ARGS=-m ")) {
9465 start += strlen("CEPH_ARGS=-m ");
9467 if (*start == '\'') {
9470 next = strchr(start + 1, '\'');
9471 } else if (*start == '"') {
9474 next = strchr(start + 1, '"');
9476 next = strchr(start, ' ');
9479 next = strchr(curr, '\n');
9481 if (VIR_STRNDUP(arg, curr, next ? next - curr : -1) < 0)
9484 if (next && (*next == '\'' || *next == '"'))
9487 if (VIR_RESIZE_N(arglist, argalloc, argcount, 2) < 0) {
9492 arglist[argcount++] = arg;
9493 arglist[argcount] = NULL;
9495 while (next && c_isspace(*next))
9501 /* Iterate over list of args, finding first arg not containing
9502 * the '=' character (eg, skip over env vars FOO=bar) */
9503 for (envend = 0; ((envend < argcount) &&
9504 (strchr(arglist[envend], '=') != NULL));
9508 /* Copy the list of env vars */
9510 if (VIR_REALLOC_N(progenv, envend+1) < 0)
9512 for (i = 0; i < envend; i++)
9513 progenv[i] = arglist[i];
9517 /* Copy the list of argv */
9518 if (VIR_REALLOC_N(progargv, argcount-envend + 1) < 0)
9520 for (i = envend; i < argcount; i++)
9521 progargv[i-envend] = arglist[i];
9522 progargv[i-envend] = NULL;
9527 *retargv = progargv;
9534 virStringFreeList(arglist);
9540 * Search for a named env variable, and return the value part
9542 static const char *qemuFindEnv(char **progenv,
9546 int len = strlen(name);
9548 for (i = 0; progenv && progenv[i]; i++) {
9549 if (STREQLEN(progenv[i], name, len) &&
9550 progenv[i][len] == '=')
9551 return progenv[i] + len + 1;
9557 * Takes a string containing a set of key=value,key=value,key...
9558 * parameters and splits them up, returning two arrays with
9559 * the individual keys and values. If allowEmptyValue is nonzero,
9560 * the "=value" part is optional and if a key with no value is found,
9561 * NULL is be placed into corresponding place in retvalues.
9564 qemuParseKeywords(const char *str,
9565 char ***retkeywords,
9568 int allowEmptyValue)
9570 int keywordCount = 0;
9571 int keywordAlloc = 0;
9572 char **keywords = NULL;
9573 char **values = NULL;
9574 const char *start = str;
9578 *retkeywords = NULL;
9581 end = start + strlen(str);
9584 const char *separator;
9585 const char *endmark;
9591 /* Qemu accepts ',,' as an escape for a literal comma;
9592 * skip past those here while searching for the end of the
9593 * value, then strip them down below */
9594 endmark = strchr(endmark, ',');
9595 } while (endmark && endmark[1] == ',' && (endmark += 2));
9598 if (!(separator = strchr(start, '=')))
9601 if (separator >= endmark) {
9602 if (!allowEmptyValue) {
9603 virReportError(VIR_ERR_INTERNAL_ERROR,
9604 _("malformed keyword arguments in '%s'"), str);
9607 separator = endmark;
9610 if (VIR_STRNDUP(keyword, start, separator - start) < 0)
9613 if (separator < endmark) {
9615 if (VIR_STRNDUP(value, separator, endmark - separator) < 0) {
9619 if (strchr(value, ',')) {
9620 char *p = strchr(value, ',') + 1;
9631 if (keywordAlloc == keywordCount) {
9632 if (VIR_REALLOC_N(keywords, keywordAlloc + 10) < 0 ||
9633 VIR_REALLOC_N(values, keywordAlloc + 10) < 0) {
9641 keywords[keywordCount] = keyword;
9642 values[keywordCount] = value;
9645 start = endmark < end ? endmark + 1 : NULL;
9648 *retkeywords = keywords;
9649 *retvalues = values;
9650 *retnkeywords = keywordCount;
9654 for (i = 0; i < keywordCount; i++) {
9655 VIR_FREE(keywords[i]);
9656 VIR_FREE(values[i]);
9664 * Tries to parse new style QEMU -drive args.
9666 * eg -drive file=/dev/HostVG/VirtData1,if=ide,index=1
9668 * Will fail if not using the 'index' keyword
9670 static virDomainDiskDefPtr
9671 qemuParseCommandLineDisk(virDomainXMLOptionPtr xmlopt,
9673 virDomainDefPtr dom,
9675 bool old_style_ceph_args)
9677 virDomainDiskDefPtr def = NULL;
9685 int trans = VIR_DOMAIN_DISK_TRANS_DEFAULT;
9687 if (qemuParseKeywords(val,
9694 if (VIR_ALLOC(def) < 0)
9697 if (((dom->os.arch == VIR_ARCH_PPC64) &&
9698 dom->os.machine && STREQ(dom->os.machine, "pseries")))
9699 def->bus = VIR_DOMAIN_DISK_BUS_SCSI;
9701 def->bus = VIR_DOMAIN_DISK_BUS_IDE;
9702 def->device = VIR_DOMAIN_DISK_DEVICE_DISK;
9703 def->src.type = VIR_STORAGE_TYPE_FILE;
9705 for (i = 0; i < nkeywords; i++) {
9706 if (STREQ(keywords[i], "file")) {
9707 if (values[i] && STRNEQ(values[i], "")) {
9708 def->src.path = values[i];
9710 if (STRPREFIX(def->src.path, "/dev/"))
9711 def->src.type = VIR_STORAGE_TYPE_BLOCK;
9712 else if (STRPREFIX(def->src.path, "nbd:") ||
9713 STRPREFIX(def->src.path, "nbd+")) {
9714 def->src.type = VIR_STORAGE_TYPE_NETWORK;
9715 def->src.protocol = VIR_STORAGE_NET_PROTOCOL_NBD;
9717 if (qemuParseNBDString(def) < 0)
9719 } else if (STRPREFIX(def->src.path, "rbd:")) {
9720 char *p = def->src.path;
9722 def->src.type = VIR_STORAGE_TYPE_NETWORK;
9723 def->src.protocol = VIR_STORAGE_NET_PROTOCOL_RBD;
9724 if (VIR_STRDUP(def->src.path, p + strlen("rbd:")) < 0)
9726 /* old-style CEPH_ARGS env variable is parsed later */
9727 if (!old_style_ceph_args && qemuParseRBDString(def) < 0) {
9733 } else if (STRPREFIX(def->src.path, "gluster:") ||
9734 STRPREFIX(def->src.path, "gluster+")) {
9735 def->src.type = VIR_STORAGE_TYPE_NETWORK;
9736 def->src.protocol = VIR_STORAGE_NET_PROTOCOL_GLUSTER;
9738 if (qemuParseGlusterString(def) < 0)
9740 } else if (STRPREFIX(def->src.path, "iscsi:")) {
9741 def->src.type = VIR_STORAGE_TYPE_NETWORK;
9742 def->src.protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI;
9744 if (qemuParseISCSIString(def) < 0)
9746 } else if (STRPREFIX(def->src.path, "sheepdog:")) {
9747 char *p = def->src.path;
9750 def->src.type = VIR_STORAGE_TYPE_NETWORK;
9751 def->src.protocol = VIR_STORAGE_NET_PROTOCOL_SHEEPDOG;
9752 if (VIR_STRDUP(def->src.path, p + strlen("sheepdog:")) < 0)
9756 /* def->src.path must be [vdiname] or [host]:[port]:[vdiname] */
9757 port = strchr(def->src.path, ':');
9760 vdi = strchr(port + 1, ':');
9763 virReportError(VIR_ERR_INTERNAL_ERROR,
9764 _("cannot parse sheepdog filename '%s'"),
9770 if (VIR_ALLOC(def->src.hosts) < 0)
9772 def->src.nhosts = 1;
9773 def->src.hosts->name = def->src.path;
9774 if (VIR_STRDUP(def->src.hosts->port, port) < 0)
9776 def->src.hosts->transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
9777 def->src.hosts->socket = NULL;
9778 if (VIR_STRDUP(def->src.path, vdi) < 0)
9782 def->src.type = VIR_STORAGE_TYPE_FILE;
9784 def->src.type = VIR_STORAGE_TYPE_FILE;
9786 } else if (STREQ(keywords[i], "if")) {
9787 if (STREQ(values[i], "ide")) {
9788 def->bus = VIR_DOMAIN_DISK_BUS_IDE;
9789 if (((dom->os.arch == VIR_ARCH_PPC64) &&
9790 dom->os.machine && STREQ(dom->os.machine, "pseries"))) {
9791 virReportError(VIR_ERR_INTERNAL_ERROR,
9792 _("pseries systems do not support ide devices '%s'"), val);
9795 } else if (STREQ(values[i], "scsi"))
9796 def->bus = VIR_DOMAIN_DISK_BUS_SCSI;
9797 else if (STREQ(values[i], "virtio"))
9798 def->bus = VIR_DOMAIN_DISK_BUS_VIRTIO;
9799 else if (STREQ(values[i], "xen"))
9800 def->bus = VIR_DOMAIN_DISK_BUS_XEN;
9801 else if (STREQ(values[i], "sd"))
9802 def->bus = VIR_DOMAIN_DISK_BUS_SD;
9803 } else if (STREQ(keywords[i], "media")) {
9804 if (STREQ(values[i], "cdrom")) {
9805 def->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
9806 def->readonly = true;
9807 } else if (STREQ(values[i], "floppy"))
9808 def->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
9809 } else if (STREQ(keywords[i], "format")) {
9810 if (VIR_STRDUP(def->src.driverName, "qemu") < 0)
9812 def->src.format = virStorageFileFormatTypeFromString(values[i]);
9813 } else if (STREQ(keywords[i], "cache")) {
9814 if (STREQ(values[i], "off") ||
9815 STREQ(values[i], "none"))
9816 def->cachemode = VIR_DOMAIN_DISK_CACHE_DISABLE;
9817 else if (STREQ(values[i], "writeback") ||
9818 STREQ(values[i], "on"))
9819 def->cachemode = VIR_DOMAIN_DISK_CACHE_WRITEBACK;
9820 else if (STREQ(values[i], "writethrough"))
9821 def->cachemode = VIR_DOMAIN_DISK_CACHE_WRITETHRU;
9822 else if (STREQ(values[i], "directsync"))
9823 def->cachemode = VIR_DOMAIN_DISK_CACHE_DIRECTSYNC;
9824 else if (STREQ(values[i], "unsafe"))
9825 def->cachemode = VIR_DOMAIN_DISK_CACHE_UNSAFE;
9826 } else if (STREQ(keywords[i], "werror")) {
9827 if (STREQ(values[i], "stop"))
9828 def->error_policy = VIR_DOMAIN_DISK_ERROR_POLICY_STOP;
9829 else if (STREQ(values[i], "report"))
9830 def->error_policy = VIR_DOMAIN_DISK_ERROR_POLICY_REPORT;
9831 else if (STREQ(values[i], "ignore"))
9832 def->error_policy = VIR_DOMAIN_DISK_ERROR_POLICY_IGNORE;
9833 else if (STREQ(values[i], "enospc"))
9834 def->error_policy = VIR_DOMAIN_DISK_ERROR_POLICY_ENOSPACE;
9835 } else if (STREQ(keywords[i], "rerror")) {
9836 if (STREQ(values[i], "stop"))
9837 def->rerror_policy = VIR_DOMAIN_DISK_ERROR_POLICY_STOP;
9838 else if (STREQ(values[i], "report"))
9839 def->rerror_policy = VIR_DOMAIN_DISK_ERROR_POLICY_REPORT;
9840 else if (STREQ(values[i], "ignore"))
9841 def->rerror_policy = VIR_DOMAIN_DISK_ERROR_POLICY_IGNORE;
9842 } else if (STREQ(keywords[i], "index")) {
9843 if (virStrToLong_i(values[i], NULL, 10, &idx) < 0) {
9844 virReportError(VIR_ERR_INTERNAL_ERROR,
9845 _("cannot parse drive index '%s'"), val);
9848 } else if (STREQ(keywords[i], "bus")) {
9849 if (virStrToLong_i(values[i], NULL, 10, &busid) < 0) {
9850 virReportError(VIR_ERR_INTERNAL_ERROR,
9851 _("cannot parse drive bus '%s'"), val);
9854 } else if (STREQ(keywords[i], "unit")) {
9855 if (virStrToLong_i(values[i], NULL, 10, &unitid) < 0) {
9856 virReportError(VIR_ERR_INTERNAL_ERROR,
9857 _("cannot parse drive unit '%s'"), val);
9860 } else if (STREQ(keywords[i], "readonly")) {
9861 if ((values[i] == NULL) || STREQ(values[i], "on"))
9862 def->readonly = true;
9863 } else if (STREQ(keywords[i], "aio")) {
9864 if ((def->iomode = virDomainDiskIoTypeFromString(values[i])) < 0) {
9865 virReportError(VIR_ERR_INTERNAL_ERROR,
9866 _("cannot parse io mode '%s'"), values[i]);
9869 } else if (STREQ(keywords[i], "cyls")) {
9870 if (virStrToLong_ui(values[i], NULL, 10,
9871 &(def->geometry.cylinders)) < 0) {
9872 virDomainDiskDefFree(def);
9874 virReportError(VIR_ERR_INTERNAL_ERROR,
9875 _("cannot parse cylinders value'%s'"),
9879 } else if (STREQ(keywords[i], "heads")) {
9880 if (virStrToLong_ui(values[i], NULL, 10,
9881 &(def->geometry.heads)) < 0) {
9882 virDomainDiskDefFree(def);
9884 virReportError(VIR_ERR_INTERNAL_ERROR,
9885 _("cannot parse heads value'%s'"),
9889 } else if (STREQ(keywords[i], "secs")) {
9890 if (virStrToLong_ui(values[i], NULL, 10,
9891 &(def->geometry.sectors)) < 0) {
9892 virDomainDiskDefFree(def);
9894 virReportError(VIR_ERR_INTERNAL_ERROR,
9895 _("cannot parse sectors value'%s'"),
9899 } else if (STREQ(keywords[i], "trans")) {
9900 def->geometry.trans =
9901 virDomainDiskGeometryTransTypeFromString(values[i]);
9902 if ((trans < VIR_DOMAIN_DISK_TRANS_DEFAULT) ||
9903 (trans >= VIR_DOMAIN_DISK_TRANS_LAST)) {
9904 virDomainDiskDefFree(def);
9906 virReportError(VIR_ERR_INTERNAL_ERROR,
9907 _("cannot parse translation value'%s'"),
9914 if (def->rerror_policy == def->error_policy)
9915 def->rerror_policy = 0;
9917 if (!def->src.path &&
9918 def->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
9919 def->src.type != VIR_STORAGE_TYPE_NETWORK) {
9920 virReportError(VIR_ERR_INTERNAL_ERROR,
9921 _("missing file parameter in drive '%s'"), val);
9925 def->bus == VIR_DOMAIN_DISK_BUS_VIRTIO)
9931 virReportError(VIR_ERR_INTERNAL_ERROR,
9932 _("missing index/unit/bus parameter in drive '%s'"),
9943 case VIR_DOMAIN_DISK_BUS_IDE:
9944 idx = (busid * 2) + unitid;
9946 case VIR_DOMAIN_DISK_BUS_SCSI:
9947 idx = (busid * 7) + unitid;
9955 if (def->bus == VIR_DOMAIN_DISK_BUS_IDE) {
9956 ignore_value(VIR_STRDUP(def->dst, "hda"));
9957 } else if (def->bus == VIR_DOMAIN_DISK_BUS_SCSI ||
9958 def->bus == VIR_DOMAIN_DISK_BUS_SD) {
9959 ignore_value(VIR_STRDUP(def->dst, "sda"));
9960 } else if (def->bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
9961 ignore_value(VIR_STRDUP(def->dst, "vda"));
9962 } else if (def->bus == VIR_DOMAIN_DISK_BUS_XEN) {
9963 ignore_value(VIR_STRDUP(def->dst, "xvda"));
9965 ignore_value(VIR_STRDUP(def->dst, "hda"));
9970 if (STREQ(def->dst, "xvda"))
9971 def->dst[3] = 'a' + idx;
9973 def->dst[2] = 'a' + idx;
9975 if (virDomainDiskDefAssignAddress(xmlopt, def) < 0) {
9976 virReportError(VIR_ERR_INTERNAL_ERROR,
9977 _("invalid device name '%s'"), def->dst);
9978 virDomainDiskDefFree(def);
9984 for (i = 0; i < nkeywords; i++) {
9985 VIR_FREE(keywords[i]);
9986 VIR_FREE(values[i]);
9993 virDomainDiskDefFree(def);
9999 * Tries to find a NIC definition matching a vlan we want
10001 static const char *
10002 qemuFindNICForVLAN(int nnics,
10007 for (i = 0; i < nnics; i++) {
10009 const char *tmp = strstr(nics[i], "vlan=");
10014 tmp += strlen("vlan=");
10016 if (virStrToLong_i(tmp, &end, 10, &gotvlan) < 0) {
10017 virReportError(VIR_ERR_INTERNAL_ERROR,
10018 _("cannot parse NIC vlan in '%s'"), nics[i]);
10022 if (gotvlan == wantvlan)
10026 if (wantvlan == 0 && nnics > 0)
10029 virReportError(VIR_ERR_INTERNAL_ERROR,
10030 _("cannot find NIC definition for vlan %d"), wantvlan);
10036 * Tries to parse a QEMU -net backend argument. Gets given
10037 * a list of all known -net frontend arguments to try and
10038 * match up against. Horribly complicated stuff
10040 static virDomainNetDefPtr
10041 qemuParseCommandLineNet(virDomainXMLOptionPtr xmlopt,
10046 virDomainNetDefPtr def = NULL;
10047 char **keywords = NULL;
10048 char **values = NULL;
10053 bool genmac = true;
10056 tmp = strchr(val, ',');
10059 if (qemuParseKeywords(tmp+1,
10069 if (VIR_ALLOC(def) < 0)
10072 /* 'tap' could turn into libvirt type=ethernet, type=bridge or
10073 * type=network, but we can't tell, so use the generic config */
10074 if (STRPREFIX(val, "tap,"))
10075 def->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
10076 else if (STRPREFIX(val, "socket"))
10077 def->type = VIR_DOMAIN_NET_TYPE_CLIENT;
10078 else if (STRPREFIX(val, "user"))
10079 def->type = VIR_DOMAIN_NET_TYPE_USER;
10081 def->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
10083 for (i = 0; i < nkeywords; i++) {
10084 if (STREQ(keywords[i], "vlan")) {
10085 if (virStrToLong_i(values[i], NULL, 10, &wantvlan) < 0) {
10086 virReportError(VIR_ERR_INTERNAL_ERROR,
10087 _("cannot parse vlan in '%s'"), val);
10088 virDomainNetDefFree(def);
10092 } else if (def->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
10093 STREQ(keywords[i], "script") && STRNEQ(values[i], "")) {
10094 def->script = values[i];
10096 } else if (def->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
10097 STREQ(keywords[i], "ifname")) {
10098 def->ifname = values[i];
10104 /* Done parsing the nic backend. Now to try and find corresponding
10105 * frontend, based off vlan number. NB this assumes a 1-1 mapping
10108 nic = qemuFindNICForVLAN(nnics, nics, wantvlan);
10110 virDomainNetDefFree(def);
10115 if (!STRPREFIX(nic, "nic")) {
10116 virReportError(VIR_ERR_INTERNAL_ERROR,
10117 _("cannot parse NIC definition '%s'"), nic);
10118 virDomainNetDefFree(def);
10123 for (i = 0; i < nkeywords; i++) {
10124 VIR_FREE(keywords[i]);
10125 VIR_FREE(values[i]);
10127 VIR_FREE(keywords);
10130 if (STRPREFIX(nic, "nic,")) {
10131 if (qemuParseKeywords(nic + strlen("nic,"),
10136 virDomainNetDefFree(def);
10144 for (i = 0; i < nkeywords; i++) {
10145 if (STREQ(keywords[i], "macaddr")) {
10147 if (virMacAddrParse(values[i], &def->mac) < 0) {
10148 virReportError(VIR_ERR_INTERNAL_ERROR,
10149 _("unable to parse mac address '%s'"),
10151 virDomainNetDefFree(def);
10155 } else if (STREQ(keywords[i], "model")) {
10156 def->model = values[i];
10158 } else if (STREQ(keywords[i], "vhost")) {
10159 if ((values[i] == NULL) || STREQ(values[i], "on")) {
10160 def->driver.virtio.name = VIR_DOMAIN_NET_BACKEND_TYPE_VHOST;
10161 } else if (STREQ(keywords[i], "off")) {
10162 def->driver.virtio.name = VIR_DOMAIN_NET_BACKEND_TYPE_QEMU;
10164 } else if (STREQ(keywords[i], "sndbuf") && values[i]) {
10165 if (virStrToLong_ul(values[i], NULL, 10, &def->tune.sndbuf) < 0) {
10166 virReportError(VIR_ERR_INTERNAL_ERROR,
10167 _("cannot parse sndbuf size in '%s'"), val);
10168 virDomainNetDefFree(def);
10172 def->tune.sndbuf_specified = true;
10177 virDomainNetGenerateMAC(xmlopt, &def->mac);
10180 for (i = 0; i < nkeywords; i++) {
10181 VIR_FREE(keywords[i]);
10182 VIR_FREE(values[i]);
10184 VIR_FREE(keywords);
10191 * Tries to parse a QEMU PCI device
10193 static virDomainHostdevDefPtr
10194 qemuParseCommandLinePCI(const char *val)
10196 int bus = 0, slot = 0, func = 0;
10199 virDomainHostdevDefPtr def = virDomainHostdevDefAlloc();
10204 if (!STRPREFIX(val, "host=")) {
10205 virReportError(VIR_ERR_INTERNAL_ERROR,
10206 _("unknown PCI device syntax '%s'"), val);
10210 start = val + strlen("host=");
10211 if (virStrToLong_i(start, &end, 16, &bus) < 0 || *end != ':') {
10212 virReportError(VIR_ERR_INTERNAL_ERROR,
10213 _("cannot extract PCI device bus '%s'"), val);
10217 if (virStrToLong_i(start, &end, 16, &slot) < 0 || *end != '.') {
10218 virReportError(VIR_ERR_INTERNAL_ERROR,
10219 _("cannot extract PCI device slot '%s'"), val);
10223 if (virStrToLong_i(start, NULL, 16, &func) < 0) {
10224 virReportError(VIR_ERR_INTERNAL_ERROR,
10225 _("cannot extract PCI device function '%s'"), val);
10229 def->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
10230 def->managed = true;
10231 def->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
10232 def->source.subsys.u.pci.addr.bus = bus;
10233 def->source.subsys.u.pci.addr.slot = slot;
10234 def->source.subsys.u.pci.addr.function = func;
10238 virDomainHostdevDefFree(def);
10244 * Tries to parse a QEMU USB device
10246 static virDomainHostdevDefPtr
10247 qemuParseCommandLineUSB(const char *val)
10249 virDomainHostdevDefPtr def = virDomainHostdevDefAlloc();
10250 int first = 0, second = 0;
10257 if (!STRPREFIX(val, "host:")) {
10258 virReportError(VIR_ERR_INTERNAL_ERROR,
10259 _("unknown USB device syntax '%s'"), val);
10263 start = val + strlen("host:");
10264 if (strchr(start, ':')) {
10265 if (virStrToLong_i(start, &end, 16, &first) < 0 || *end != ':') {
10266 virReportError(VIR_ERR_INTERNAL_ERROR,
10267 _("cannot extract USB device vendor '%s'"), val);
10271 if (virStrToLong_i(start, NULL, 16, &second) < 0) {
10272 virReportError(VIR_ERR_INTERNAL_ERROR,
10273 _("cannot extract USB device product '%s'"), val);
10277 if (virStrToLong_i(start, &end, 10, &first) < 0 || *end != '.') {
10278 virReportError(VIR_ERR_INTERNAL_ERROR,
10279 _("cannot extract USB device bus '%s'"), val);
10283 if (virStrToLong_i(start, NULL, 10, &second) < 0) {
10284 virReportError(VIR_ERR_INTERNAL_ERROR,
10285 _("cannot extract USB device address '%s'"), val);
10290 def->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
10291 def->managed = false;
10292 def->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB;
10294 def->source.subsys.u.usb.bus = first;
10295 def->source.subsys.u.usb.device = second;
10297 def->source.subsys.u.usb.vendor = first;
10298 def->source.subsys.u.usb.product = second;
10303 virDomainHostdevDefFree(def);
10309 * Tries to parse a QEMU serial/parallel device
10312 qemuParseCommandLineChr(virDomainChrSourceDefPtr source,
10315 if (STREQ(val, "null")) {
10316 source->type = VIR_DOMAIN_CHR_TYPE_NULL;
10317 } else if (STREQ(val, "vc")) {
10318 source->type = VIR_DOMAIN_CHR_TYPE_VC;
10319 } else if (STREQ(val, "pty")) {
10320 source->type = VIR_DOMAIN_CHR_TYPE_PTY;
10321 } else if (STRPREFIX(val, "file:")) {
10322 source->type = VIR_DOMAIN_CHR_TYPE_FILE;
10323 if (VIR_STRDUP(source->data.file.path, val + strlen("file:")) < 0)
10325 } else if (STRPREFIX(val, "pipe:")) {
10326 source->type = VIR_DOMAIN_CHR_TYPE_PIPE;
10327 if (VIR_STRDUP(source->data.file.path, val + strlen("pipe:")) < 0)
10329 } else if (STREQ(val, "stdio")) {
10330 source->type = VIR_DOMAIN_CHR_TYPE_STDIO;
10331 } else if (STRPREFIX(val, "udp:")) {
10332 const char *svc1, *host2, *svc2;
10333 source->type = VIR_DOMAIN_CHR_TYPE_UDP;
10334 val += strlen("udp:");
10335 svc1 = strchr(val, ':');
10336 host2 = svc1 ? strchr(svc1, '@') : NULL;
10337 svc2 = host2 ? strchr(host2, ':') : NULL;
10339 if (svc1 && svc1 != val &&
10340 VIR_STRNDUP(source->data.udp.connectHost, val, svc1 - val) < 0)
10345 if (VIR_STRNDUP(source->data.udp.connectService, svc1,
10346 host2 ? host2 - svc1 : strlen(svc1)) < 0)
10352 if (svc2 && svc2 != host2 &&
10353 VIR_STRNDUP(source->data.udp.bindHost, host2, svc2 - host2) < 0)
10359 if (STRNEQ(svc2, "0")) {
10360 if (VIR_STRDUP(source->data.udp.bindService, svc2) < 0)
10364 } else if (STRPREFIX(val, "tcp:") ||
10365 STRPREFIX(val, "telnet:")) {
10366 const char *opt, *svc;
10367 source->type = VIR_DOMAIN_CHR_TYPE_TCP;
10368 if (STRPREFIX(val, "tcp:")) {
10369 val += strlen("tcp:");
10371 val += strlen("telnet:");
10372 source->data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
10374 svc = strchr(val, ':');
10376 virReportError(VIR_ERR_INTERNAL_ERROR,
10377 _("cannot find port number in character device %s"), val);
10380 opt = strchr(svc, ',');
10381 if (opt && strstr(opt, "server"))
10382 source->data.tcp.listen = true;
10384 if (VIR_STRNDUP(source->data.tcp.host, val, svc - val) < 0)
10387 if (VIR_STRNDUP(source->data.tcp.service, svc, opt ? opt - svc : -1) < 0)
10389 } else if (STRPREFIX(val, "unix:")) {
10391 val += strlen("unix:");
10392 opt = strchr(val, ',');
10393 source->type = VIR_DOMAIN_CHR_TYPE_UNIX;
10394 if (VIR_STRNDUP(source->data.nix.path, val, opt ? opt - val : -1) < 0)
10397 } else if (STRPREFIX(val, "/dev")) {
10398 source->type = VIR_DOMAIN_CHR_TYPE_DEV;
10399 if (VIR_STRDUP(source->data.file.path, val) < 0)
10402 virReportError(VIR_ERR_INTERNAL_ERROR,
10403 _("unknown character device syntax %s"), val);
10414 static virCPUDefPtr
10415 qemuInitGuestCPU(virDomainDefPtr dom)
10420 if (VIR_ALLOC(cpu) < 0)
10423 cpu->type = VIR_CPU_TYPE_GUEST;
10424 cpu->match = VIR_CPU_MATCH_EXACT;
10433 qemuParseCommandLineCPU(virDomainDefPtr dom,
10436 virCPUDefPtr cpu = NULL;
10438 char **hv_tokens = NULL;
10439 char *model = NULL;
10443 if (!(tokens = virStringSplit(val, ",", 0)))
10446 if (tokens[0] == NULL)
10449 for (i = 0; tokens[i] != NULL; i++) {
10450 if (*tokens[i] == '\0')
10454 if (VIR_STRDUP(model, tokens[i]) < 0)
10457 if (!STREQ(model, "qemu32") && !STREQ(model, "qemu64")) {
10458 if (!(cpu = qemuInitGuestCPU(dom)))
10461 cpu->model = model;
10464 } else if (*tokens[i] == '+' || *tokens[i] == '-') {
10465 const char *feature = tokens[i] + 1; /* '+' or '-' */
10468 if (*tokens[i] == '+')
10469 policy = VIR_CPU_FEATURE_REQUIRE;
10471 policy = VIR_CPU_FEATURE_DISABLE;
10473 if (*feature == '\0')
10476 if (dom->os.arch != VIR_ARCH_X86_64 &&
10477 dom->os.arch != VIR_ARCH_I686) {
10478 virReportError(VIR_ERR_INTERNAL_ERROR,
10479 _("%s platform doesn't support CPU features'"),
10480 virArchToString(dom->os.arch));
10484 if (STREQ(feature, "kvmclock")) {
10485 bool present = (policy == VIR_CPU_FEATURE_REQUIRE);
10488 for (j = 0; j < dom->clock.ntimers; j++) {
10489 if (dom->clock.timers[j]->name == VIR_DOMAIN_TIMER_NAME_KVMCLOCK)
10493 if (j == dom->clock.ntimers) {
10494 virDomainTimerDefPtr timer;
10495 if (VIR_ALLOC(timer) < 0 ||
10496 VIR_APPEND_ELEMENT_COPY(dom->clock.timers,
10497 dom->clock.ntimers, timer) < 0) {
10501 timer->name = VIR_DOMAIN_TIMER_NAME_KVMCLOCK;
10502 timer->present = present;
10503 timer->tickpolicy = -1;
10506 } else if (dom->clock.timers[j]->present != -1 &&
10507 dom->clock.timers[j]->present != present) {
10508 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
10509 _("conflicting occurrences of kvmclock feature"));
10512 } else if (STREQ(feature, "kvm_pv_eoi")) {
10513 if (policy == VIR_CPU_FEATURE_REQUIRE)
10514 dom->apic_eoi = VIR_DOMAIN_FEATURE_STATE_ON;
10516 dom->apic_eoi = VIR_DOMAIN_FEATURE_STATE_OFF;
10519 if (!(cpu = qemuInitGuestCPU(dom)))
10522 cpu->model = model;
10526 if (virCPUDefAddFeature(cpu, feature, policy) < 0)
10529 } else if (STRPREFIX(tokens[i], "hv_")) {
10530 const char *token = tokens[i] + 3; /* "hv_" */
10531 const char *feature, *value;
10534 if (*token == '\0')
10537 if (!(hv_tokens = virStringSplit(token, "=", 2)))
10540 feature = hv_tokens[0];
10541 value = hv_tokens[1];
10543 if (*feature == '\0')
10546 dom->features[VIR_DOMAIN_FEATURE_HYPERV] = VIR_DOMAIN_FEATURE_STATE_ON;
10548 if ((f = virDomainHypervTypeFromString(feature)) < 0) {
10549 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
10550 _("unsupported HyperV Enlightenment feature "
10555 switch ((enum virDomainHyperv) f) {
10556 case VIR_DOMAIN_HYPERV_RELAXED:
10557 case VIR_DOMAIN_HYPERV_VAPIC:
10559 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
10560 _("HyperV feature '%s' should not "
10561 "have a value"), feature);
10564 dom->hyperv_features[f] = VIR_DOMAIN_FEATURE_STATE_ON;
10567 case VIR_DOMAIN_HYPERV_SPINLOCKS:
10568 dom->hyperv_features[f] = VIR_DOMAIN_FEATURE_STATE_ON;
10570 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
10571 _("missing HyperV spinlock retry count"));
10575 if (virStrToLong_ui(value, NULL, 0, &dom->hyperv_spinlocks) < 0) {
10576 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
10577 _("cannot parse HyperV spinlock retry count"));
10581 if (dom->hyperv_spinlocks < 0xFFF)
10582 dom->hyperv_spinlocks = 0xFFF;
10585 case VIR_DOMAIN_HYPERV_LAST:
10588 virStringFreeList(hv_tokens);
10593 if (dom->os.arch == VIR_ARCH_X86_64) {
10594 bool is_32bit = false;
10596 virCPUDataPtr cpuData = NULL;
10598 if (cpuEncode(VIR_ARCH_X86_64, cpu, NULL, &cpuData,
10599 NULL, NULL, NULL, NULL) < 0)
10602 is_32bit = (cpuHasFeature(cpuData, "lm") != 1);
10603 cpuDataFree(cpuData);
10604 } else if (model) {
10605 is_32bit = STREQ(model, "qemu32");
10609 dom->os.arch = VIR_ARCH_I686;
10616 virStringFreeList(tokens);
10617 virStringFreeList(hv_tokens);
10621 virReportError(VIR_ERR_INTERNAL_ERROR,
10622 _("unknown CPU syntax '%s'"), val);
10628 qemuParseCommandLineSmp(virDomainDefPtr dom,
10631 unsigned int sockets = 0;
10632 unsigned int cores = 0;
10633 unsigned int threads = 0;
10634 unsigned int maxcpus = 0;
10643 if (qemuParseKeywords(val, &kws, &vals, &nkws, 1) < 0)
10646 for (i = 0; i < nkws; i++) {
10647 if (vals[i] == NULL) {
10649 virStrToLong_i(kws[i], &end, 10, &n) < 0 || *end != '\0')
10653 if (virStrToLong_i(vals[i], &end, 10, &n) < 0 || *end != '\0')
10655 if (STREQ(kws[i], "sockets"))
10657 else if (STREQ(kws[i], "cores"))
10659 else if (STREQ(kws[i], "threads"))
10661 else if (STREQ(kws[i], "maxcpus"))
10668 dom->maxvcpus = maxcpus ? maxcpus : dom->vcpus;
10670 if (sockets && cores && threads) {
10673 if (!(cpu = qemuInitGuestCPU(dom)))
10675 cpu->sockets = sockets;
10676 cpu->cores = cores;
10677 cpu->threads = threads;
10678 } else if (sockets || cores || threads)
10684 for (i = 0; i < nkws; i++) {
10694 virReportError(VIR_ERR_INTERNAL_ERROR,
10695 _("cannot parse CPU topology '%s'"), val);
10703 qemuParseCommandLineBootDevs(virDomainDefPtr def, const char *str)
10707 for (n = 0; str[n] && b < VIR_DOMAIN_BOOT_LAST; n++) {
10709 def->os.bootDevs[b++] = VIR_DOMAIN_BOOT_FLOPPY;
10710 else if (str[n] == 'c')
10711 def->os.bootDevs[b++] = VIR_DOMAIN_BOOT_DISK;
10712 else if (str[n] == 'd')
10713 def->os.bootDevs[b++] = VIR_DOMAIN_BOOT_CDROM;
10714 else if (str[n] == 'n')
10715 def->os.bootDevs[b++] = VIR_DOMAIN_BOOT_NET;
10716 else if (str[n] == ',')
10719 def->os.nBootDevs = b;
10724 * Analyse the env and argv settings and reconstruct a
10725 * virDomainDefPtr representing these settings as closely
10726 * as is practical. This is not an exact science....
10728 static virDomainDefPtr
10729 qemuParseCommandLine(virCapsPtr qemuCaps,
10730 virDomainXMLOptionPtr xmlopt,
10734 virDomainChrSourceDefPtr *monConfig,
10737 virDomainDefPtr def;
10739 bool nographics = false;
10740 bool fullscreen = false;
10743 const char **nics = NULL;
10744 int video = VIR_DOMAIN_VIDEO_TYPE_CIRRUS;
10745 int nvirtiodisk = 0;
10746 qemuDomainCmdlineDefPtr cmd = NULL;
10747 virDomainDiskDefPtr disk = NULL;
10748 const char *ceph_args = qemuFindEnv(progenv, "CEPH_ARGS");
10757 if (!progargv[0]) {
10758 virReportError(VIR_ERR_INTERNAL_ERROR,
10759 "%s", _("no emulator path found"));
10763 if (VIR_ALLOC(def) < 0)
10766 /* allocate the cmdlinedef up-front; if it's unused, we'll free it later */
10767 if (VIR_ALLOC(cmd) < 0)
10770 if (virUUIDGenerate(def->uuid) < 0) {
10771 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
10772 _("failed to generate uuid"));
10777 def->mem.cur_balloon = def->mem.max_balloon = 64 * 1024;
10780 def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_UTC;
10782 def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
10783 def->onCrash = VIR_DOMAIN_LIFECYCLE_DESTROY;
10784 def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
10785 def->virtType = VIR_DOMAIN_VIRT_QEMU;
10786 if (VIR_STRDUP(def->emulator, progargv[0]) < 0)
10789 if (!(path = last_component(def->emulator)))
10792 if (strstr(path, "xenner")) {
10793 def->virtType = VIR_DOMAIN_VIRT_KVM;
10794 if (VIR_STRDUP(def->os.type, "xen") < 0)
10797 if (VIR_STRDUP(def->os.type, "hvm") < 0)
10799 if (strstr(path, "kvm")) {
10800 def->virtType = VIR_DOMAIN_VIRT_KVM;
10801 def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_DOMAIN_FEATURE_STATE_ON;
10805 if (def->virtType == VIR_DOMAIN_VIRT_KVM)
10806 def->os.arch = qemuCaps->host.arch;
10807 else if (STRPREFIX(path, "qemu-system-"))
10808 def->os.arch = virArchFromString(path + strlen("qemu-system-"));
10810 def->os.arch = VIR_ARCH_I686;
10812 if ((def->os.arch == VIR_ARCH_I686) ||
10813 (def->os.arch == VIR_ARCH_X86_64))
10814 def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_DOMAIN_FEATURE_STATE_ON;
10816 #define WANT_VALUE() \
10817 const char *val = progargv[++i]; \
10819 virReportError(VIR_ERR_INTERNAL_ERROR, \
10820 _("missing value for %s argument"), arg); \
10824 /* One initial loop to get list of NICs, so we
10825 * can correlate them later */
10826 for (i = 1; progargv[i]; i++) {
10827 const char *arg = progargv[i];
10828 /* Make sure we have a single - for all options to
10829 simplify next logic */
10830 if (STRPREFIX(arg, "--"))
10833 if (STREQ(arg, "-net")) {
10835 if (STRPREFIX(val, "nic") &&
10836 VIR_APPEND_ELEMENT(nics, nnics, val) < 0)
10841 /* Now the real processing loop */
10842 for (i = 1; progargv[i]; i++) {
10843 const char *arg = progargv[i];
10844 /* Make sure we have a single - for all options to
10845 simplify next logic */
10846 if (STRPREFIX(arg, "--"))
10849 if (STREQ(arg, "-vnc")) {
10850 virDomainGraphicsDefPtr vnc;
10853 if (VIR_ALLOC(vnc) < 0)
10855 vnc->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
10857 if (STRPREFIX(val, "unix:")) {
10858 /* -vnc unix:/some/big/path */
10859 if (VIR_STRDUP(vnc->data.vnc.socket, val + 5) < 0) {
10860 virDomainGraphicsDefFree(vnc);
10866 * -vnc [2001:1:2:3:4:5:1234:1234]:4
10867 * -vnc some.host.name:4
10871 const char *sep = ":";
10874 tmp = strstr(val, sep);
10876 virDomainGraphicsDefFree(vnc);
10877 virReportError(VIR_ERR_INTERNAL_ERROR,
10878 _("missing VNC port number in '%s'"), val);
10881 port = tmp + strlen(sep);
10882 if (virStrToLong_i(port, &opts, 10,
10883 &vnc->data.vnc.port) < 0) {
10884 virDomainGraphicsDefFree(vnc);
10885 virReportError(VIR_ERR_INTERNAL_ERROR,
10886 _("cannot parse VNC port '%s'"), port);
10890 virDomainGraphicsListenSetAddress(vnc, 0,
10891 val+1, tmp-(val+1), true);
10893 virDomainGraphicsListenSetAddress(vnc, 0,
10894 val, tmp-val, true);
10895 if (!virDomainGraphicsListenGetAddress(vnc, 0)) {
10896 virDomainGraphicsDefFree(vnc);
10900 if (*opts == ',') {
10903 if (VIR_STRDUP(orig_opts, opts + 1) < 0) {
10904 virDomainGraphicsDefFree(vnc);
10909 while (opts && *opts) {
10910 char *nextopt = strchr(opts, ',');
10912 *(nextopt++) = '\0';
10914 if (STRPREFIX(opts, "websocket")) {
10915 char *websocket = opts + strlen("websocket");
10916 if (*(websocket++) == '=' &&
10918 /* If the websocket continues with
10919 * '=<something>', we'll parse it */
10920 if (virStrToLong_i(websocket,
10922 &vnc->data.vnc.websocket) < 0) {
10923 virReportError(VIR_ERR_INTERNAL_ERROR,
10924 _("cannot parse VNC "
10925 "WebSocket port '%s'"),
10927 virDomainGraphicsDefFree(vnc);
10928 VIR_FREE(orig_opts);
10932 /* Otherwise, we'll compute the port the same
10933 * way QEMU does, by adding a 5700 to the
10934 * display value. */
10935 vnc->data.vnc.websocket =
10936 vnc->data.vnc.port + 5700;
10938 } else if (STRPREFIX(opts, "share=")) {
10939 char *sharePolicy = opts + strlen("share=");
10940 if (sharePolicy && *sharePolicy) {
10942 virDomainGraphicsVNCSharePolicyTypeFromString(sharePolicy);
10945 virReportError(VIR_ERR_INTERNAL_ERROR,
10946 _("unknown vnc display sharing policy '%s'"),
10948 virDomainGraphicsDefFree(vnc);
10949 VIR_FREE(orig_opts);
10952 vnc->data.vnc.sharePolicy = policy;
10955 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
10956 _("missing vnc sharing policy"));
10957 virDomainGraphicsDefFree(vnc);
10958 VIR_FREE(orig_opts);
10965 VIR_FREE(orig_opts);
10967 vnc->data.vnc.port += 5900;
10968 vnc->data.vnc.autoport = false;
10971 if (VIR_APPEND_ELEMENT(def->graphics, def->ngraphics, vnc) < 0) {
10972 virDomainGraphicsDefFree(vnc);
10975 } else if (STREQ(arg, "-m")) {
10978 if (virStrToLong_i(val, NULL, 10, &mem) < 0) {
10979 virReportError(VIR_ERR_INTERNAL_ERROR, \
10980 _("cannot parse memory level '%s'"), val);
10983 def->mem.cur_balloon = def->mem.max_balloon = mem * 1024;
10984 } else if (STREQ(arg, "-smp")) {
10986 if (qemuParseCommandLineSmp(def, val) < 0)
10988 } else if (STREQ(arg, "-uuid")) {
10990 if (virUUIDParse(val, def->uuid) < 0) {
10991 virReportError(VIR_ERR_INTERNAL_ERROR, \
10992 _("cannot parse UUID '%s'"), val);
10995 } else if (STRPREFIX(arg, "-hd") ||
10996 STRPREFIX(arg, "-sd") ||
10997 STRPREFIX(arg, "-fd") ||
10998 STREQ(arg, "-cdrom")) {
11000 if (VIR_ALLOC(disk) < 0)
11003 if (STRPREFIX(val, "/dev/"))
11004 disk->src.type = VIR_STORAGE_TYPE_BLOCK;
11005 else if (STRPREFIX(val, "nbd:")) {
11006 disk->src.type = VIR_STORAGE_TYPE_NETWORK;
11007 disk->src.protocol = VIR_STORAGE_NET_PROTOCOL_NBD;
11008 } else if (STRPREFIX(val, "rbd:")) {
11009 disk->src.type = VIR_STORAGE_TYPE_NETWORK;
11010 disk->src.protocol = VIR_STORAGE_NET_PROTOCOL_RBD;
11011 val += strlen("rbd:");
11012 } else if (STRPREFIX(val, "gluster")) {
11013 disk->src.type = VIR_STORAGE_TYPE_NETWORK;
11014 disk->src.protocol = VIR_STORAGE_NET_PROTOCOL_GLUSTER;
11015 } else if (STRPREFIX(val, "sheepdog:")) {
11016 disk->src.type = VIR_STORAGE_TYPE_NETWORK;
11017 disk->src.protocol = VIR_STORAGE_NET_PROTOCOL_SHEEPDOG;
11018 val += strlen("sheepdog:");
11020 disk->src.type = VIR_STORAGE_TYPE_FILE;
11021 if (STREQ(arg, "-cdrom")) {
11022 disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
11023 if (((def->os.arch == VIR_ARCH_PPC64) &&
11024 def->os.machine && STREQ(def->os.machine, "pseries")))
11025 disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
11026 if (VIR_STRDUP(disk->dst, "hdc") < 0)
11028 disk->readonly = true;
11030 if (STRPREFIX(arg, "-fd")) {
11031 disk->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
11032 disk->bus = VIR_DOMAIN_DISK_BUS_FDC;
11034 disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
11035 if (STRPREFIX(arg, "-hd"))
11036 disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
11038 disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
11039 if (((def->os.arch == VIR_ARCH_PPC64) &&
11040 def->os.machine && STREQ(def->os.machine, "pseries")))
11041 disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
11043 if (VIR_STRDUP(disk->dst, arg + 1) < 0)
11046 if (VIR_STRDUP(disk->src.path, val) < 0)
11049 if (disk->src.type == VIR_STORAGE_TYPE_NETWORK) {
11052 switch ((virStorageNetProtocol) disk->src.protocol) {
11053 case VIR_STORAGE_NET_PROTOCOL_NBD:
11054 if (qemuParseNBDString(disk) < 0)
11057 case VIR_STORAGE_NET_PROTOCOL_RBD:
11058 /* old-style CEPH_ARGS env variable is parsed later */
11059 if (!ceph_args && qemuParseRBDString(disk) < 0)
11062 case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
11063 /* disk->src must be [vdiname] or [host]:[port]:[vdiname] */
11064 port = strchr(disk->src.path, ':');
11069 vdi = strchr(port, ':');
11071 virReportError(VIR_ERR_INTERNAL_ERROR,
11072 _("cannot parse sheepdog filename '%s'"), val);
11076 if (VIR_ALLOC(disk->src.hosts) < 0)
11078 disk->src.nhosts = 1;
11079 disk->src.hosts->name = disk->src.path;
11080 if (VIR_STRDUP(disk->src.hosts->port, port) < 0)
11082 if (VIR_STRDUP(disk->src.path, vdi) < 0)
11086 case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
11087 if (qemuParseGlusterString(disk) < 0)
11091 case VIR_STORAGE_NET_PROTOCOL_ISCSI:
11092 if (qemuParseISCSIString(disk) < 0)
11096 case VIR_STORAGE_NET_PROTOCOL_HTTP:
11097 case VIR_STORAGE_NET_PROTOCOL_HTTPS:
11098 case VIR_STORAGE_NET_PROTOCOL_FTP:
11099 case VIR_STORAGE_NET_PROTOCOL_FTPS:
11100 case VIR_STORAGE_NET_PROTOCOL_TFTP:
11101 case VIR_STORAGE_NET_PROTOCOL_LAST:
11102 case VIR_STORAGE_NET_PROTOCOL_NONE:
11103 /* ignored for now */
11108 if (virDomainDiskDefAssignAddress(xmlopt, disk) < 0) {
11109 virReportError(VIR_ERR_INTERNAL_ERROR,
11110 _("Cannot assign address for device name '%s'"),
11115 if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
11117 } else if (STREQ(arg, "-no-acpi")) {
11118 def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_DOMAIN_FEATURE_STATE_DEFAULT;
11119 } else if (STREQ(arg, "-no-reboot")) {
11120 def->onReboot = VIR_DOMAIN_LIFECYCLE_DESTROY;
11121 } else if (STREQ(arg, "-no-kvm")) {
11122 def->virtType = VIR_DOMAIN_VIRT_QEMU;
11123 } else if (STREQ(arg, "-enable-kvm")) {
11124 def->virtType = VIR_DOMAIN_VIRT_KVM;
11125 } else if (STREQ(arg, "-nographic")) {
11127 } else if (STREQ(arg, "-full-screen")) {
11129 } else if (STREQ(arg, "-localtime")) {
11130 def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME;
11131 } else if (STREQ(arg, "-kernel")) {
11133 if (VIR_STRDUP(def->os.kernel, val) < 0)
11135 } else if (STREQ(arg, "-bios")) {
11137 if (VIR_STRDUP(def->os.loader, val) < 0)
11139 } else if (STREQ(arg, "-initrd")) {
11141 if (VIR_STRDUP(def->os.initrd, val) < 0)
11143 } else if (STREQ(arg, "-append")) {
11145 if (VIR_STRDUP(def->os.cmdline, val) < 0)
11147 } else if (STREQ(arg, "-dtb")) {
11149 if (VIR_STRDUP(def->os.dtb, val) < 0)
11151 } else if (STREQ(arg, "-boot")) {
11152 const char *token = NULL;
11155 if (!strchr(val, ','))
11156 qemuParseCommandLineBootDevs(def, val);
11159 while (token && *token) {
11160 if (STRPREFIX(token, "order=")) {
11161 token += strlen("order=");
11162 qemuParseCommandLineBootDevs(def, token);
11163 } else if (STRPREFIX(token, "menu=on")) {
11164 def->os.bootmenu = 1;
11165 } else if (STRPREFIX(token, "reboot-timeout=")) {
11168 if (virStrToLong_i(token + strlen("reboot-timeout="),
11169 &endptr, 10, &num) < 0 ||
11170 (*endptr != '\0' && endptr != strchr(token, ','))) {
11171 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
11172 _("cannot parse reboot-timeout value"));
11179 def->os.bios.rt_delay = num;
11180 def->os.bios.rt_set = true;
11182 token = strchr(token, ',');
11183 /* This incrementation has to be done here in order to make it
11184 * possible to pass the token pointer properly into the loop */
11189 } else if (STREQ(arg, "-name")) {
11192 process = strstr(val, ",process=");
11193 if (process == NULL) {
11194 if (VIR_STRDUP(def->name, val) < 0)
11197 if (VIR_STRNDUP(def->name, val, process - val) < 0)
11200 if (STREQ(def->name, ""))
11201 VIR_FREE(def->name);
11202 } else if (STREQ(arg, "-M") ||
11203 STREQ(arg, "-machine")) {
11208 /* -machine [type=]name[,prop[=value][,...]]
11209 * Set os.machine only if first parameter lacks '=' or
11210 * contains explicit type='...' */
11212 if (!(list = virStringSplit(val, ",", 0)))
11216 if (STRPREFIX(param, "type="))
11217 param += strlen("type=");
11218 if (!strchr(param, '=')) {
11219 if (VIR_STRDUP(def->os.machine, param) < 0) {
11220 virStringFreeList(list);
11226 /* handle all remaining "-machine" parameters */
11227 while ((param = list[j++])) {
11228 if (STRPREFIX(param, "dump-guest-core=")) {
11229 param += strlen("dump-guest-core=");
11230 def->mem.dump_core = virDomainMemDumpTypeFromString(param);
11231 if (def->mem.dump_core <= 0)
11232 def->mem.dump_core = VIR_DOMAIN_MEM_DUMP_DEFAULT;
11233 } else if (STRPREFIX(param, "mem-merge=off")) {
11234 def->mem.nosharepages = true;
11235 } else if (STRPREFIX(param, "accel=kvm")) {
11236 def->virtType = VIR_DOMAIN_VIRT_KVM;
11237 def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_DOMAIN_FEATURE_STATE_ON;
11240 virStringFreeList(list);
11241 } else if (STREQ(arg, "-serial")) {
11243 if (STRNEQ(val, "none")) {
11244 virDomainChrDefPtr chr;
11246 if (!(chr = virDomainChrDefNew()))
11249 if (qemuParseCommandLineChr(&chr->source, val) < 0) {
11250 virDomainChrDefFree(chr);
11253 chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
11254 chr->target.port = def->nserials;
11255 if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) {
11256 virDomainChrDefFree(chr);
11260 } else if (STREQ(arg, "-parallel")) {
11262 if (STRNEQ(val, "none")) {
11263 virDomainChrDefPtr chr;
11265 if (!(chr = virDomainChrDefNew()))
11268 if (qemuParseCommandLineChr(&chr->source, val) < 0) {
11269 virDomainChrDefFree(chr);
11272 chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
11273 chr->target.port = def->nparallels;
11274 if (VIR_APPEND_ELEMENT(def->parallels, def->nparallels, chr) < 0) {
11275 virDomainChrDefFree(chr);
11279 } else if (STREQ(arg, "-usbdevice")) {
11281 if (STREQ(val, "tablet") ||
11282 STREQ(val, "mouse") ||
11283 STREQ(val, "keyboard")) {
11284 virDomainInputDefPtr input;
11285 if (VIR_ALLOC(input) < 0)
11287 input->bus = VIR_DOMAIN_INPUT_BUS_USB;
11288 if (STREQ(val, "tablet"))
11289 input->type = VIR_DOMAIN_INPUT_TYPE_TABLET;
11290 else if (STREQ(val, "mouse"))
11291 input->type = VIR_DOMAIN_INPUT_TYPE_MOUSE;
11293 input->type = VIR_DOMAIN_INPUT_TYPE_KBD;
11295 if (VIR_APPEND_ELEMENT(def->inputs, def->ninputs, input) < 0) {
11296 virDomainInputDefFree(input);
11299 } else if (STRPREFIX(val, "disk:")) {
11300 if (VIR_ALLOC(disk) < 0)
11302 if (VIR_STRDUP(disk->src.path, val + strlen("disk:")) < 0)
11304 if (STRPREFIX(disk->src.path, "/dev/"))
11305 disk->src.type = VIR_STORAGE_TYPE_BLOCK;
11307 disk->src.type = VIR_STORAGE_TYPE_FILE;
11308 disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
11309 disk->bus = VIR_DOMAIN_DISK_BUS_USB;
11310 disk->removable = VIR_DOMAIN_FEATURE_STATE_DEFAULT;
11311 if (VIR_STRDUP(disk->dst, "sda") < 0)
11313 if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
11316 virDomainHostdevDefPtr hostdev;
11317 if (!(hostdev = qemuParseCommandLineUSB(val)))
11319 if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
11320 virDomainHostdevDefFree(hostdev);
11324 } else if (STREQ(arg, "-net")) {
11326 if (!STRPREFIX(val, "nic") && STRNEQ(val, "none")) {
11327 virDomainNetDefPtr net;
11328 if (!(net = qemuParseCommandLineNet(xmlopt, val, nnics, nics)))
11330 if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0) {
11331 virDomainNetDefFree(net);
11335 } else if (STREQ(arg, "-drive")) {
11337 if (!(disk = qemuParseCommandLineDisk(xmlopt, val, def,
11339 ceph_args != NULL)))
11341 if (disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO)
11343 if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
11345 } else if (STREQ(arg, "-pcidevice")) {
11346 virDomainHostdevDefPtr hostdev;
11348 if (!(hostdev = qemuParseCommandLinePCI(val)))
11350 if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
11351 virDomainHostdevDefFree(hostdev);
11354 } else if (STREQ(arg, "-soundhw")) {
11359 const char *tmp = strchr(start, ',');
11361 if (STRPREFIX(start, "pcspk")) {
11362 type = VIR_DOMAIN_SOUND_MODEL_PCSPK;
11363 } else if (STRPREFIX(start, "sb16")) {
11364 type = VIR_DOMAIN_SOUND_MODEL_SB16;
11365 } else if (STRPREFIX(start, "es1370")) {
11366 type = VIR_DOMAIN_SOUND_MODEL_ES1370;
11367 } else if (STRPREFIX(start, "ac97")) {
11368 type = VIR_DOMAIN_SOUND_MODEL_AC97;
11369 } else if (STRPREFIX(start, "hda")) {
11370 type = VIR_DOMAIN_SOUND_MODEL_ICH6;
11374 virDomainSoundDefPtr snd;
11375 if (VIR_ALLOC(snd) < 0)
11378 if (VIR_APPEND_ELEMENT(def->sounds, def->nsounds, snd) < 0) {
11384 start = tmp ? tmp + 1 : NULL;
11386 } else if (STREQ(arg, "-watchdog")) {
11388 int model = virDomainWatchdogModelTypeFromString(val);
11391 virDomainWatchdogDefPtr wd;
11392 if (VIR_ALLOC(wd) < 0)
11395 wd->action = VIR_DOMAIN_WATCHDOG_ACTION_RESET;
11396 def->watchdog = wd;
11398 } else if (STREQ(arg, "-watchdog-action") && def->watchdog) {
11400 int action = virDomainWatchdogActionTypeFromString(val);
11403 def->watchdog->action = action;
11404 } else if (STREQ(arg, "-bootloader")) {
11406 if (VIR_STRDUP(def->os.bootloader, val) < 0)
11408 } else if (STREQ(arg, "-vmwarevga")) {
11409 video = VIR_DOMAIN_VIDEO_TYPE_VMVGA;
11410 } else if (STREQ(arg, "-std-vga")) {
11411 video = VIR_DOMAIN_VIDEO_TYPE_VGA;
11412 } else if (STREQ(arg, "-vga")) {
11414 if (STRNEQ(val, "none")) {
11415 video = qemuVideoTypeFromString(val);
11417 virReportError(VIR_ERR_INTERNAL_ERROR,
11418 _("unknown video adapter type '%s'"), val);
11422 } else if (STREQ(arg, "-cpu")) {
11424 if (qemuParseCommandLineCPU(def, val) < 0)
11426 } else if (STREQ(arg, "-domid")) {
11428 /* ignore, generted on the fly */
11429 } else if (STREQ(arg, "-usb")) {
11430 virDomainControllerDefPtr ctldef;
11431 if (VIR_ALLOC(ctldef) < 0)
11433 ctldef->type = VIR_DOMAIN_CONTROLLER_TYPE_USB;
11435 ctldef->model = -1;
11436 if (virDomainControllerInsert(def, ctldef) < 0) {
11440 } else if (STREQ(arg, "-pidfile")) {
11443 if (VIR_STRDUP(*pidfile, val) < 0)
11445 } else if (STREQ(arg, "-incoming")) {
11447 /* ignore, used via restore/migrate APIs */
11448 } else if (STREQ(arg, "-monitor")) {
11451 virDomainChrSourceDefPtr chr;
11453 if (VIR_ALLOC(chr) < 0)
11456 if (qemuParseCommandLineChr(chr, val) < 0) {
11457 virDomainChrSourceDefFree(chr);
11463 } else if (STREQ(arg, "-global") &&
11464 STRPREFIX(progargv[i + 1], "PIIX4_PM.disable_s3=")) {
11465 /* We want to parse only the known "-global" parameters,
11466 * so the ones that we don't know are still added to the
11470 val += strlen("PIIX4_PM.disable_s3=");
11471 if (STREQ(val, "0"))
11472 def->pm.s3 = VIR_DOMAIN_PM_STATE_ENABLED;
11473 else if (STREQ(val, "1"))
11474 def->pm.s3 = VIR_DOMAIN_PM_STATE_DISABLED;
11476 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
11477 _("invalid value for disable_s3 parameter: "
11482 } else if (STREQ(arg, "-global") &&
11483 STRPREFIX(progargv[i + 1], "PIIX4_PM.disable_s4=")) {
11487 val += strlen("PIIX4_PM.disable_s4=");
11488 if (STREQ(val, "0"))
11489 def->pm.s4 = VIR_DOMAIN_PM_STATE_ENABLED;
11490 else if (STREQ(val, "1"))
11491 def->pm.s4 = VIR_DOMAIN_PM_STATE_DISABLED;
11493 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
11494 _("invalid value for disable_s4 parameter: "
11499 } else if (STREQ(arg, "-global") &&
11500 STRPREFIX(progargv[i + 1], "spapr-nvram.reg=")) {
11503 if (VIR_ALLOC(def->nvram) < 0)
11506 def->nvram->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_SPAPRVIO;
11507 def->nvram->info.addr.spaprvio.has_reg = true;
11509 val += strlen("spapr-nvram.reg=");
11510 if (virStrToLong_ull(val, NULL, 16,
11511 &def->nvram->info.addr.spaprvio.reg) < 0) {
11512 virReportError(VIR_ERR_INTERNAL_ERROR,
11513 _("cannot parse nvram's address '%s'"), val);
11516 } else if (STREQ(arg, "-S")) {
11517 /* ignore, always added by libvirt */
11520 /* something we can't yet parse. Add it to the qemu namespace
11521 * cmdline/environment advanced options and hope for the best
11523 VIR_WARN("unknown QEMU argument '%s', adding to the qemu namespace",
11525 if (VIR_STRDUP(tmp, arg) < 0 ||
11526 VIR_APPEND_ELEMENT(cmd->args, cmd->num_args, tmp) < 0) {
11534 if (def->ndisks > 0 && ceph_args) {
11535 char *hosts, *port, *saveptr = NULL, *token;
11536 virDomainDiskDefPtr first_rbd_disk = NULL;
11537 for (i = 0; i < def->ndisks; i++) {
11538 if (def->disks[i]->src.type == VIR_STORAGE_TYPE_NETWORK &&
11539 def->disks[i]->src.protocol == VIR_STORAGE_NET_PROTOCOL_RBD) {
11540 first_rbd_disk = def->disks[i];
11545 if (!first_rbd_disk) {
11546 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
11547 _("CEPH_ARGS was set without an rbd disk"));
11551 /* CEPH_ARGS should be: -m host1[:port1][,host2[:port2]]... */
11552 if (!STRPREFIX(ceph_args, "-m ")) {
11553 virReportError(VIR_ERR_INTERNAL_ERROR,
11554 _("could not parse CEPH_ARGS '%s'"), ceph_args);
11557 if (VIR_STRDUP(hosts, strchr(ceph_args, ' ') + 1) < 0)
11559 first_rbd_disk->src.nhosts = 0;
11560 token = strtok_r(hosts, ",", &saveptr);
11561 while (token != NULL) {
11562 if (VIR_REALLOC_N(first_rbd_disk->src.hosts,
11563 first_rbd_disk->src.nhosts + 1) < 0) {
11567 port = strchr(token, ':');
11570 if (VIR_STRDUP(port, port) < 0) {
11575 first_rbd_disk->src.hosts[first_rbd_disk->src.nhosts].port = port;
11576 if (VIR_STRDUP(first_rbd_disk->src.hosts[first_rbd_disk->src.nhosts].name,
11581 first_rbd_disk->src.hosts[first_rbd_disk->src.nhosts].transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
11582 first_rbd_disk->src.hosts[first_rbd_disk->src.nhosts].socket = NULL;
11584 first_rbd_disk->src.nhosts++;
11585 token = strtok_r(NULL, ",", &saveptr);
11589 if (first_rbd_disk->src.nhosts == 0) {
11590 virReportError(VIR_ERR_INTERNAL_ERROR,
11591 _("found no rbd hosts in CEPH_ARGS '%s'"), ceph_args);
11596 if (!def->os.machine) {
11597 const char *defaultMachine =
11598 virCapabilitiesDefaultGuestMachine(qemuCaps,
11601 virDomainVirtTypeToString(def->virtType));
11602 if (defaultMachine != NULL)
11603 if (VIR_STRDUP(def->os.machine, defaultMachine) < 0)
11607 if (!nographics && def->ngraphics == 0) {
11608 virDomainGraphicsDefPtr sdl;
11609 const char *display = qemuFindEnv(progenv, "DISPLAY");
11610 const char *xauth = qemuFindEnv(progenv, "XAUTHORITY");
11611 if (VIR_ALLOC(sdl) < 0)
11613 sdl->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
11614 sdl->data.sdl.fullscreen = fullscreen;
11615 if (VIR_STRDUP(sdl->data.sdl.display, display) < 0) {
11619 if (VIR_STRDUP(sdl->data.sdl.xauth, xauth) < 0) {
11624 if (VIR_APPEND_ELEMENT(def->graphics, def->ngraphics, sdl) < 0) {
11625 virDomainGraphicsDefFree(sdl);
11630 if (def->ngraphics) {
11631 virDomainVideoDefPtr vid;
11632 if (VIR_ALLOC(vid) < 0)
11634 if (def->virtType == VIR_DOMAIN_VIRT_XEN)
11635 vid->type = VIR_DOMAIN_VIDEO_TYPE_XEN;
11638 vid->vram = virDomainVideoDefaultRAM(def, vid->type);
11639 vid->ram = vid->type == VIR_DOMAIN_VIDEO_TYPE_QXL ?
11640 virDomainVideoDefaultRAM(def, vid->type) : 0;
11643 if (VIR_APPEND_ELEMENT(def->videos, def->nvideos, vid) < 0) {
11644 virDomainVideoDefFree(vid);
11650 * having a balloon is the default, define one with type="none" to avoid it
11652 if (!def->memballoon) {
11653 virDomainMemballoonDefPtr memballoon;
11654 if (VIR_ALLOC(memballoon) < 0)
11656 memballoon->model = VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO;
11658 def->memballoon = memballoon;
11663 if (virDomainDefAddImplicitControllers(def) < 0)
11666 if (virDomainDefPostParse(def, qemuCaps, xmlopt) < 0)
11669 if (cmd->num_args || cmd->num_env) {
11670 def->ns = *virDomainXMLOptionGetNamespace(xmlopt);
11671 def->namespaceData = cmd;
11674 qemuDomainCmdlineDefFree(cmd);
11679 virDomainDiskDefFree(disk);
11680 qemuDomainCmdlineDefFree(cmd);
11681 virDomainDefFree(def);
11684 virDomainChrSourceDefFree(*monConfig);
11688 VIR_FREE(*pidfile);
11693 virDomainDefPtr qemuParseCommandLineString(virCapsPtr qemuCaps,
11694 virDomainXMLOptionPtr xmlopt,
11697 virDomainChrSourceDefPtr *monConfig,
11700 char **progenv = NULL;
11701 char **progargv = NULL;
11702 virDomainDefPtr def = NULL;
11704 if (qemuStringToArgvEnv(args, &progenv, &progargv) < 0)
11707 def = qemuParseCommandLine(qemuCaps, xmlopt, progenv, progargv,
11708 pidfile, monConfig, monJSON);
11711 virStringFreeList(progargv);
11712 virStringFreeList(progenv);
11718 static int qemuParseProcFileStrings(int pid_value,
11730 if (virAsprintf(&path, "/proc/%d/%s", pid_value, name) < 0)
11733 if ((len = virFileReadAll(path, 1024*128, &data)) < 0)
11737 while (tmp < (data + len)) {
11738 if (VIR_EXPAND_N(str, nstr, 1) < 0)
11741 if (VIR_STRDUP(str[nstr-1], tmp) < 0)
11744 tmp += strlen(tmp);
11745 /* Skip \0 separator */
11749 if (VIR_EXPAND_N(str, nstr, 1) < 0)
11752 str[nstr-1] = NULL;
11759 virStringFreeList(str);
11765 virDomainDefPtr qemuParseCommandLinePid(virCapsPtr qemuCaps,
11766 virDomainXMLOptionPtr xmlopt,
11769 virDomainChrSourceDefPtr *monConfig,
11772 virDomainDefPtr def = NULL;
11773 char **progargv = NULL;
11774 char **progenv = NULL;
11775 char *exepath = NULL;
11778 /* The parser requires /proc/pid, which only exists on platforms
11779 * like Linux where pid_t fits in int. */
11780 if ((int) pid != pid ||
11781 qemuParseProcFileStrings(pid, "cmdline", &progargv) < 0 ||
11782 qemuParseProcFileStrings(pid, "environ", &progenv) < 0)
11785 if (!(def = qemuParseCommandLine(qemuCaps, xmlopt, progenv, progargv,
11786 pidfile, monConfig, monJSON)))
11789 if (virAsprintf(&exepath, "/proc/%d/exe", (int) pid) < 0)
11792 if (virFileResolveLink(exepath, &emulator) < 0) {
11793 virReportSystemError(errno,
11794 _("Unable to resolve %s for pid %u"),
11795 exepath, (int) pid);
11798 VIR_FREE(def->emulator);
11799 def->emulator = emulator;
11803 virStringFreeList(progargv);
11804 virStringFreeList(progenv);