Ubuntu

Merge lp:~qrilka/ubuntu/precise/libvirt/libvirt-fix-1092826 into lp:ubuntu/precise/libvirt

Proposed by Kirill Zaborsky on 2012-12-22
Status: Work in progress
Proposed branch: lp:~qrilka/ubuntu/precise/libvirt/libvirt-fix-1092826
Merge into: lp:ubuntu/precise/libvirt
Diff against target: 20791 lines (+20549/-6) 28 files modified
To merge this branch: bzr merge lp:~qrilka/ubuntu/precise/libvirt/libvirt-fix-1092826
Reviewer Review Type Date Requested Status
Jamie Strandboge Needs Fixing on 2013-01-22
Ubuntu branches 2012-12-22 Pending
Review via email: mp+141163@code.launchpad.net

Description of the Change

Fix for LP: #1092826

To post a comment you must log in.
Jamie Strandboge (jdstrand) wrote :

Thanks for your patch! Unfortunately it cannot be incorporated into Ubuntu in its current state:
 * debian/patches/fix-poll.patch does not include DEP-3 comments
 * debian/changelog should reference precise-proposed, not precise
 * the patch does not apply cleanly to 0.9.8-2ubuntu17.5, the version that is currently in precise

Please feel free to make these changes and resubmit. Thanks!

review: Needs Fixing
Jamie Strandboge (jdstrand) wrote :

Also:
 * the version '0.9.8-2ubuntu17.4ppa1' is incorrect. It should be 0.9.8-2ubuntu17.6 since 0.9.8-2ubuntu17.5 is currently in precise-updates.

Unmerged revisions

211. By Kirill Zaborsky on 2012-12-22

adds proper handling for EINTR signal (LP: #1092826)

210. By Serge Hallyn on 2012-08-13

* storage-fix-a-potential-crash-when-creating-vol-object: avoid a crash
  on bad free() on error lvm storage. (LP: #1035320)
* add pm-utils as suggests to avoid log spamming with messages about
  pm-is-supported not being found. (LP: #994476)

209. By Serge Hallyn on 2012-07-25

debian/apparmor/libvirt-qemu: add ceph.conf (LP: #1026404)

208. By Serge Hallyn on 2012-06-11

* debian/libvirt-bin.install, debian/rules: name the apport file
  source_libvirt.py, not source_libvirt-bin.py. (LP: #1007405)
* install /etc/dnsmasq.d/libvirt to configure system wide dnsmasq to not
  listen on the libvirt bridge. (Following Stéphane's lxc example)
  (LP: #928524) (LP: #231060)
  - postinst: restart dnsmasq; postrm: remove dnsmasq.d/libvirt file and
    restart dnsmasq; rules, libvirt-bin.dirs and libvirt-bin.install:
    install new debian/libvirt-bin.dnsmasq file.
* Warn user about bad pc-0.12 machine type, and help user transition.
  (LP: #1001625)
  - qemu-warn-on-pc-0.12.patch: When defining or starting a VM which uses the
    pc-0.12 machine type, warn in libvirtd.log.
  - debian/libvirt-migrate-qemu-machinetype: automatically migrate QEMU VMs
    to newest machine type. This is not done automatically as there will
    be some users who have good reason to stay with pc-0.12.

207. By Serge Hallyn on 2012-05-02

debian/apparmor/usr.sbin.libvirtd: allow execution of /lib/udev/scsi_id
(LP: #992378)

Preview Diff

1=== added file '.pc/.quilt_patches'
2--- .pc/.quilt_patches 1970-01-01 00:00:00 +0000
3+++ .pc/.quilt_patches 2012-12-22 20:09:19 +0000
4@@ -0,0 +1,1 @@
5+debian/patches
6
7=== added file '.pc/.quilt_series'
8--- .pc/.quilt_series 1970-01-01 00:00:00 +0000
9+++ .pc/.quilt_series 2012-12-22 20:09:19 +0000
10@@ -0,0 +1,1 @@
11+series
12
13=== modified file '.pc/applied-patches'
14--- .pc/applied-patches 2012-04-05 11:43:15 +0000
15+++ .pc/applied-patches 2012-12-22 20:09:19 +0000
16@@ -34,3 +34,5 @@
17 ubuntu/9024-qemu-allow-snapshotting-of-sheepdog-and-rbd-disks.patch
18 ubuntu/9025-qemu-change-rbd-auth_supported-separation-character-.patch
19 ubuntu/xen-config-no-vfb-for-hvm.patch
20+ubuntu/qemu-warn-on-pc-0.12.patch
21+ubuntu/storage-fix-a-potential-crash-when-creating-vol-object
22
23=== added directory '.pc/ubuntu/qemu-warn-on-pc-0.12.patch'
24=== added directory '.pc/ubuntu/qemu-warn-on-pc-0.12.patch/src'
25=== added directory '.pc/ubuntu/qemu-warn-on-pc-0.12.patch/src/qemu'
26=== added file '.pc/ubuntu/qemu-warn-on-pc-0.12.patch/src/qemu/qemu_command.c'
27--- .pc/ubuntu/qemu-warn-on-pc-0.12.patch/src/qemu/qemu_command.c 1970-01-01 00:00:00 +0000
28+++ .pc/ubuntu/qemu-warn-on-pc-0.12.patch/src/qemu/qemu_command.c 2012-12-22 20:09:19 +0000
29@@ -0,0 +1,7527 @@
30+/*
31+ * qemu_command.c: QEMU command generation
32+ *
33+ * Copyright (C) 2006-2011 Red Hat, Inc.
34+ * Copyright (C) 2006 Daniel P. Berrange
35+ *
36+ * This library is free software; you can redistribute it and/or
37+ * modify it under the terms of the GNU Lesser General Public
38+ * License as published by the Free Software Foundation; either
39+ * version 2.1 of the License, or (at your option) any later version.
40+ *
41+ * This library is distributed in the hope that it will be useful,
42+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
43+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44+ * Lesser General Public License for more details.
45+ *
46+ * You should have received a copy of the GNU Lesser General Public
47+ * License along with this library; if not, write to the Free Software
48+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49+ *
50+ * Author: Daniel P. Berrange <berrange@redhat.com>
51+ */
52+
53+#include <config.h>
54+
55+#include "qemu_command.h"
56+#include "qemu_capabilities.h"
57+#include "qemu_bridge_filter.h"
58+#include "cpu/cpu.h"
59+#include "memory.h"
60+#include "logging.h"
61+#include "virterror_internal.h"
62+#include "util.h"
63+#include "virfile.h"
64+#include "uuid.h"
65+#include "c-ctype.h"
66+#include "domain_nwfilter.h"
67+#include "domain_audit.h"
68+#include "domain_conf.h"
69+#include "network/bridge_driver.h"
70+#include "virnetdevtap.h"
71+#include "base64.h"
72+
73+#include <sys/utsname.h>
74+#include <sys/stat.h>
75+#include <fcntl.h>
76+
77+#define VIR_FROM_THIS VIR_FROM_QEMU
78+
79+
80+VIR_ENUM_DECL(virDomainDiskQEMUBus)
81+VIR_ENUM_IMPL(virDomainDiskQEMUBus, VIR_DOMAIN_DISK_BUS_LAST,
82+ "ide",
83+ "floppy",
84+ "scsi",
85+ "virtio",
86+ "xen",
87+ "usb",
88+ "uml",
89+ "sata")
90+
91+
92+VIR_ENUM_DECL(qemuDiskCacheV1)
93+VIR_ENUM_DECL(qemuDiskCacheV2)
94+
95+VIR_ENUM_IMPL(qemuDiskCacheV1, VIR_DOMAIN_DISK_CACHE_LAST,
96+ "default",
97+ "off",
98+ "off", /* writethrough not supported, so for safety, disable */
99+ "on", /* Old 'on' was equivalent to 'writeback' */
100+ "off", /* directsync not supported, for safety, disable */
101+ "off"); /* unsafe not supported, for safety, disable */
102+
103+VIR_ENUM_IMPL(qemuDiskCacheV2, VIR_DOMAIN_DISK_CACHE_LAST,
104+ "default",
105+ "none",
106+ "writethrough",
107+ "writeback",
108+ "directsync",
109+ "unsafe");
110+
111+VIR_ENUM_DECL(qemuVideo)
112+
113+VIR_ENUM_IMPL(qemuVideo, VIR_DOMAIN_VIDEO_TYPE_LAST,
114+ "std",
115+ "cirrus",
116+ "vmware",
117+ "", /* no arg needed for xen */
118+ "", /* don't support vbox */
119+ "qxl");
120+
121+VIR_ENUM_DECL(qemuControllerModelUSB)
122+
123+VIR_ENUM_IMPL(qemuControllerModelUSB, VIR_DOMAIN_CONTROLLER_MODEL_USB_LAST,
124+ "piix3-usb-uhci",
125+ "piix4-usb-uhci",
126+ "usb-ehci",
127+ "ich9-usb-ehci1",
128+ "ich9-usb-uhci1",
129+ "ich9-usb-uhci2",
130+ "ich9-usb-uhci3",
131+ "vt82c686b-usb-uhci",
132+ "pci-ohci");
133+
134+VIR_ENUM_DECL(qemuDomainFSDriver)
135+VIR_ENUM_IMPL(qemuDomainFSDriver, VIR_DOMAIN_FS_DRIVER_TYPE_LAST,
136+ "local",
137+ "local",
138+ "handle");
139+
140+
141+static void
142+uname_normalize (struct utsname *ut)
143+{
144+ uname(ut);
145+
146+ /* Map i386, i486, i586 to i686. */
147+ if (ut->machine[0] == 'i' &&
148+ ut->machine[1] != '\0' &&
149+ ut->machine[2] == '8' &&
150+ ut->machine[3] == '6' &&
151+ ut->machine[4] == '\0')
152+ ut->machine[1] = '6';
153+}
154+
155+
156+/**
157+ * qemuPhysIfaceConnect:
158+ * @def: the definition of the VM (needed by 802.1Qbh and audit)
159+ * @driver: pointer to the qemud_driver
160+ * @net: pointer to he VM's interface description with direct device type
161+ * @qemuCaps: flags for qemu
162+ * @vmop: VM operation type
163+ *
164+ * Returns a filedescriptor on success or -1 in case of error.
165+ */
166+int
167+qemuPhysIfaceConnect(virDomainDefPtr def,
168+ struct qemud_driver *driver,
169+ virDomainNetDefPtr net,
170+ virBitmapPtr qemuCaps,
171+ enum virNetDevVPortProfileOp vmop)
172+{
173+ int rc;
174+ char *res_ifname = NULL;
175+ int vnet_hdr = 0;
176+
177+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_VNET_HDR) &&
178+ net->model && STREQ(net->model, "virtio"))
179+ vnet_hdr = 1;
180+
181+ rc = virNetDevMacVLanCreateWithVPortProfile(
182+ net->ifname, net->mac,
183+ virDomainNetGetActualDirectDev(net),
184+ virDomainNetGetActualDirectMode(net),
185+ true, vnet_hdr, def->uuid,
186+ virDomainNetGetActualDirectVirtPortProfile(net),
187+ &res_ifname,
188+ vmop, driver->stateDir,
189+ virDomainNetGetActualBandwidth(net));
190+ if (rc >= 0) {
191+ virDomainAuditNetDevice(def, net, res_ifname, true);
192+ VIR_FREE(net->ifname);
193+ net->ifname = res_ifname;
194+ }
195+
196+ return rc;
197+}
198+
199+
200+int
201+qemuNetworkIfaceConnect(virDomainDefPtr def,
202+ virConnectPtr conn,
203+ struct qemud_driver *driver,
204+ virDomainNetDefPtr net,
205+ virBitmapPtr qemuCaps)
206+{
207+ char *brname = NULL;
208+ int err;
209+ int tapfd = -1;
210+ int vnet_hdr = 0;
211+ bool template_ifname = false;
212+ unsigned char tapmac[VIR_MAC_BUFLEN];
213+ int actualType = virDomainNetGetActualType(net);
214+
215+ if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
216+ int active, fail = 0;
217+ virErrorPtr errobj;
218+ virNetworkPtr network = virNetworkLookupByName(conn,
219+ net->data.network.name);
220+ if (!network)
221+ return -1;
222+
223+ active = virNetworkIsActive(network);
224+ if (active != 1) {
225+ fail = 1;
226+
227+ if (active == 0)
228+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
229+ _("Network '%s' is not active."),
230+ net->data.network.name);
231+ }
232+
233+ if (!fail) {
234+ brname = virNetworkGetBridgeName(network);
235+ if (brname == NULL)
236+ fail = 1;
237+ }
238+
239+ /* Make sure any above failure is preserved */
240+ errobj = virSaveLastError();
241+ virNetworkFree(network);
242+ virSetError(errobj);
243+ virFreeError(errobj);
244+
245+ if (fail)
246+ return -1;
247+
248+ } else if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
249+ if (!(brname = strdup(virDomainNetGetActualBridgeName(net)))) {
250+ virReportOOMError();
251+ return -1;
252+ }
253+ } else {
254+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
255+ _("Network type %d is not supported"),
256+ virDomainNetGetActualType(net));
257+ return -1;
258+ }
259+
260+ if (!net->ifname ||
261+ STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
262+ strchr(net->ifname, '%')) {
263+ VIR_FREE(net->ifname);
264+ if (!(net->ifname = strdup(VIR_NET_GENERATED_PREFIX "%d"))) {
265+ virReportOOMError();
266+ goto cleanup;
267+ }
268+ /* avoid exposing vnet%d in getXMLDesc or error outputs */
269+ template_ifname = true;
270+ }
271+
272+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_VNET_HDR) &&
273+ net->model && STREQ(net->model, "virtio"))
274+ vnet_hdr = 1;
275+
276+ memcpy(tapmac, net->mac, VIR_MAC_BUFLEN);
277+ tapmac[0] = 0xFE; /* Discourage bridge from using TAP dev MAC */
278+ err = virNetDevTapCreateInBridgePort(brname, &net->ifname, tapmac,
279+ vnet_hdr, true, &tapfd);
280+ virDomainAuditNetDevice(def, net, "/dev/net/tun", tapfd >= 0);
281+ if (err < 0) {
282+ if (template_ifname)
283+ VIR_FREE(net->ifname);
284+ tapfd = -1;
285+ }
286+
287+ if (driver->macFilter) {
288+ if ((err = networkAllowMacOnPort(driver, net->ifname, net->mac))) {
289+ virReportSystemError(err,
290+ _("failed to add ebtables rule to allow MAC address on '%s'"),
291+ net->ifname);
292+ }
293+ }
294+
295+ if (tapfd >= 0 &&
296+ virNetDevBandwidthSet(net->ifname,
297+ virDomainNetGetActualBandwidth(net)) < 0) {
298+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
299+ _("cannot set bandwidth limits on %s"),
300+ net->ifname);
301+ VIR_FORCE_CLOSE(tapfd);
302+ goto cleanup;
303+ }
304+
305+ if (tapfd >= 0) {
306+ if ((net->filter) && (net->ifname)) {
307+ err = virDomainConfNWFilterInstantiate(conn, net);
308+ if (err)
309+ VIR_FORCE_CLOSE(tapfd);
310+ }
311+ }
312+
313+cleanup:
314+ VIR_FREE(brname);
315+
316+ return tapfd;
317+}
318+
319+
320+int
321+qemuOpenVhostNet(virDomainDefPtr def,
322+ virDomainNetDefPtr net,
323+ virBitmapPtr qemuCaps,
324+ int *vhostfd)
325+{
326+ *vhostfd = -1; /* assume we won't use vhost */
327+
328+ /* If the config says explicitly to not use vhost, return now */
329+ if (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_QEMU) {
330+ return 0;
331+ }
332+
333+ /* If qemu doesn't support vhost-net mode (including the -netdev command
334+ * option), don't try to open the device.
335+ */
336+ if (!(qemuCapsGet(qemuCaps, QEMU_CAPS_VHOST_NET) &&
337+ qemuCapsGet(qemuCaps, QEMU_CAPS_NETDEV) &&
338+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))) {
339+ if (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_VHOST) {
340+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
341+ "%s", _("vhost-net is not supported with "
342+ "this QEMU binary"));
343+ return -1;
344+ }
345+ return 0;
346+ }
347+
348+ /* If the nic model isn't virtio, don't try to open. */
349+ if (!(net->model && STREQ(net->model, "virtio"))) {
350+ if (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_VHOST) {
351+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
352+ "%s", _("vhost-net is only supported for "
353+ "virtio network interfaces"));
354+ return -1;
355+ }
356+ return 0;
357+ }
358+
359+ *vhostfd = open("/dev/vhost-net", O_RDWR);
360+ virDomainAuditNetDevice(def, net, "/dev/vhost-net", *vhostfd >= 0);
361+
362+ /* If the config says explicitly to use vhost and we couldn't open it,
363+ * report an error.
364+ */
365+ if ((*vhostfd < 0) &&
366+ (net->driver.virtio.name == VIR_DOMAIN_NET_BACKEND_TYPE_VHOST)) {
367+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
368+ "%s", _("vhost-net was requested for an interface, "
369+ "but is unavailable"));
370+ return -1;
371+ }
372+ return 0;
373+}
374+
375+
376+static int qemuDomainDeviceAliasIndex(virDomainDeviceInfoPtr info,
377+ const char *prefix)
378+{
379+ int idx;
380+
381+ if (!info->alias)
382+ return -1;
383+ if (!STRPREFIX(info->alias, prefix))
384+ return -1;
385+
386+ if (virStrToLong_i(info->alias + strlen(prefix), NULL, 10, &idx) < 0)
387+ return -1;
388+
389+ return idx;
390+}
391+
392+
393+int qemuDomainNetVLAN(virDomainNetDefPtr def)
394+{
395+ return qemuDomainDeviceAliasIndex(&def->info, "net");
396+}
397+
398+
399+/* Names used before -drive existed */
400+static int qemuAssignDeviceDiskAliasLegacy(virDomainDiskDefPtr disk)
401+{
402+ char *dev_name;
403+
404+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
405+ STREQ(disk->dst, "hdc"))
406+ dev_name = strdup("cdrom");
407+ else
408+ dev_name = strdup(disk->dst);
409+
410+ if (!dev_name) {
411+ virReportOOMError();
412+ return -1;
413+ }
414+
415+ disk->info.alias = dev_name;
416+ return 0;
417+}
418+
419+
420+char *qemuDeviceDriveHostAlias(virDomainDiskDefPtr disk,
421+ virBitmapPtr qemuCaps)
422+{
423+ char *ret;
424+
425+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
426+ if (virAsprintf(&ret, "%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias) < 0) {
427+ virReportOOMError();
428+ return NULL;
429+ }
430+ } else {
431+ if (!(ret = strdup(disk->info.alias))) {
432+ virReportOOMError();
433+ return NULL;
434+ }
435+ }
436+ return ret;
437+}
438+
439+
440+/* Names used before -drive supported the id= option */
441+static int qemuAssignDeviceDiskAliasFixed(virDomainDiskDefPtr disk)
442+{
443+ int busid, devid;
444+ int ret;
445+ char *dev_name;
446+
447+ if (virDiskNameToBusDeviceIndex(disk, &busid, &devid) < 0) {
448+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
449+ _("cannot convert disk '%s' to bus/device index"),
450+ disk->dst);
451+ return -1;
452+ }
453+
454+ switch (disk->bus) {
455+ case VIR_DOMAIN_DISK_BUS_IDE:
456+ if (disk->device== VIR_DOMAIN_DISK_DEVICE_DISK)
457+ ret = virAsprintf(&dev_name, "ide%d-hd%d", busid, devid);
458+ else
459+ ret = virAsprintf(&dev_name, "ide%d-cd%d", busid, devid);
460+ break;
461+ case VIR_DOMAIN_DISK_BUS_SCSI:
462+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
463+ ret = virAsprintf(&dev_name, "scsi%d-hd%d", busid, devid);
464+ else
465+ ret = virAsprintf(&dev_name, "scsi%d-cd%d", busid, devid);
466+ break;
467+ case VIR_DOMAIN_DISK_BUS_FDC:
468+ ret = virAsprintf(&dev_name, "floppy%d", devid);
469+ break;
470+ case VIR_DOMAIN_DISK_BUS_VIRTIO:
471+ ret = virAsprintf(&dev_name, "virtio%d", devid);
472+ break;
473+ case VIR_DOMAIN_DISK_BUS_XEN:
474+ ret = virAsprintf(&dev_name, "xenblk%d", devid);
475+ break;
476+ default:
477+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
478+ _("Unsupported disk name mapping for bus '%s'"),
479+ virDomainDiskBusTypeToString(disk->bus));
480+ return -1;
481+ }
482+
483+ if (ret == -1) {
484+ virReportOOMError();
485+ return -1;
486+ }
487+
488+ disk->info.alias = dev_name;
489+
490+ return 0;
491+}
492+
493+
494+/* Our custom -drive naming scheme used with id= */
495+static int qemuAssignDeviceDiskAliasCustom(virDomainDiskDefPtr disk)
496+{
497+ const char *prefix = virDomainDiskBusTypeToString(disk->bus);
498+ if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
499+ if (virAsprintf(&disk->info.alias, "%s%d-%d-%d", prefix,
500+ disk->info.addr.drive.controller,
501+ disk->info.addr.drive.bus,
502+ disk->info.addr.drive.unit) < 0)
503+ goto no_memory;
504+ } else {
505+ int idx = virDiskNameToIndex(disk->dst);
506+ if (virAsprintf(&disk->info.alias, "%s-disk%d", prefix, idx) < 0)
507+ goto no_memory;
508+ }
509+
510+ return 0;
511+
512+no_memory:
513+ virReportOOMError();
514+ return -1;
515+}
516+
517+
518+int
519+qemuAssignDeviceDiskAlias(virDomainDiskDefPtr def, virBitmapPtr qemuCaps)
520+{
521+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE)) {
522+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
523+ return qemuAssignDeviceDiskAliasCustom(def);
524+ else
525+ return qemuAssignDeviceDiskAliasFixed(def);
526+ } else {
527+ return qemuAssignDeviceDiskAliasLegacy(def);
528+ }
529+}
530+
531+
532+int
533+qemuAssignDeviceNetAlias(virDomainDefPtr def, virDomainNetDefPtr net, int idx)
534+{
535+ if (idx == -1) {
536+ int i;
537+ idx = 0;
538+ for (i = 0 ; i < def->nnets ; i++) {
539+ int thisidx;
540+ if ((thisidx = qemuDomainDeviceAliasIndex(&def->nets[i]->info, "net")) < 0) {
541+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
542+ _("Unable to determine device index for network device"));
543+ return -1;
544+ }
545+ if (thisidx >= idx)
546+ idx = thisidx + 1;
547+ }
548+ }
549+
550+ if (virAsprintf(&net->info.alias, "net%d", idx) < 0) {
551+ virReportOOMError();
552+ return -1;
553+ }
554+
555+ return 0;
556+}
557+
558+
559+int
560+qemuAssignDeviceHostdevAlias(virDomainDefPtr def, virDomainHostdevDefPtr hostdev, int idx)
561+{
562+ if (idx == -1) {
563+ int i;
564+ idx = 0;
565+ for (i = 0 ; i < def->nhostdevs ; i++) {
566+ int thisidx;
567+ if ((thisidx = qemuDomainDeviceAliasIndex(&def->hostdevs[i]->info, "hostdev")) < 0) {
568+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
569+ _("Unable to determine device index for hostdev device"));
570+ return -1;
571+ }
572+ if (thisidx >= idx)
573+ idx = thisidx + 1;
574+ }
575+ }
576+
577+ if (virAsprintf(&hostdev->info.alias, "hostdev%d", idx) < 0) {
578+ virReportOOMError();
579+ return -1;
580+ }
581+
582+ return 0;
583+}
584+
585+
586+int
587+qemuAssignDeviceRedirdevAlias(virDomainDefPtr def, virDomainRedirdevDefPtr redirdev, int idx)
588+{
589+ if (idx == -1) {
590+ int i;
591+ idx = 0;
592+ for (i = 0 ; i < def->nredirdevs ; i++) {
593+ int thisidx;
594+ if ((thisidx = qemuDomainDeviceAliasIndex(&def->redirdevs[i]->info, "redir")) < 0) {
595+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
596+ _("Unable to determine device index for redirected device"));
597+ return -1;
598+ }
599+ if (thisidx >= idx)
600+ idx = thisidx + 1;
601+ }
602+ }
603+
604+ if (virAsprintf(&redirdev->info.alias, "redir%d", idx) < 0) {
605+ virReportOOMError();
606+ return -1;
607+ }
608+
609+ return 0;
610+}
611+
612+
613+int
614+qemuAssignDeviceControllerAlias(virDomainControllerDefPtr controller)
615+{
616+ const char *prefix = virDomainControllerTypeToString(controller->type);
617+
618+ if (virAsprintf(&controller->info.alias, "%s%d", prefix,
619+ controller->idx) < 0) {
620+ virReportOOMError();
621+ return -1;
622+ }
623+
624+ return 0;
625+}
626+
627+
628+int
629+qemuAssignDeviceAliases(virDomainDefPtr def, virBitmapPtr qemuCaps)
630+{
631+ int i;
632+
633+ for (i = 0; i < def->ndisks ; i++) {
634+ if (qemuAssignDeviceDiskAlias(def->disks[i], qemuCaps) < 0)
635+ return -1;
636+ }
637+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NET_NAME) ||
638+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
639+ for (i = 0; i < def->nnets ; i++) {
640+ if (qemuAssignDeviceNetAlias(def, def->nets[i], i) < 0)
641+ return -1;
642+ }
643+ }
644+
645+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
646+ return 0;
647+
648+ for (i = 0; i < def->nfss ; i++) {
649+ if (virAsprintf(&def->fss[i]->info.alias, "fs%d", i) < 0)
650+ goto no_memory;
651+ }
652+ for (i = 0; i < def->nsounds ; i++) {
653+ if (virAsprintf(&def->sounds[i]->info.alias, "sound%d", i) < 0)
654+ goto no_memory;
655+ }
656+ for (i = 0; i < def->nhostdevs ; i++) {
657+ if (qemuAssignDeviceHostdevAlias(def, def->hostdevs[i], i) < 0)
658+ return -1;
659+ }
660+ for (i = 0; i < def->nredirdevs ; i++) {
661+ if (qemuAssignDeviceRedirdevAlias(def, def->redirdevs[i], i) < 0)
662+ return -1;
663+ }
664+ for (i = 0; i < def->nvideos ; i++) {
665+ if (virAsprintf(&def->videos[i]->info.alias, "video%d", i) < 0)
666+ goto no_memory;
667+ }
668+ for (i = 0; i < def->ncontrollers ; i++) {
669+ if (qemuAssignDeviceControllerAlias(def->controllers[i]) < 0)
670+ return -1;
671+ }
672+ for (i = 0; i < def->ninputs ; i++) {
673+ if (virAsprintf(&def->inputs[i]->info.alias, "input%d", i) < 0)
674+ goto no_memory;
675+ }
676+ for (i = 0; i < def->nparallels ; i++) {
677+ if (virAsprintf(&def->parallels[i]->info.alias, "parallel%d", i) < 0)
678+ goto no_memory;
679+ }
680+ for (i = 0; i < def->nserials ; i++) {
681+ if (virAsprintf(&def->serials[i]->info.alias, "serial%d", i) < 0)
682+ goto no_memory;
683+ }
684+ for (i = 0; i < def->nchannels ; i++) {
685+ if (virAsprintf(&def->channels[i]->info.alias, "channel%d", i) < 0)
686+ goto no_memory;
687+ }
688+ for (i = 0; i < def->nconsoles ; i++) {
689+ if (virAsprintf(&def->consoles[i]->info.alias, "console%d", i) < 0)
690+ goto no_memory;
691+ }
692+ for (i = 0; i < def->nhubs ; i++) {
693+ if (virAsprintf(&def->hubs[i]->info.alias, "hub%d", i) < 0)
694+ goto no_memory;
695+ }
696+ for (i = 0; i < def->nsmartcards ; i++) {
697+ if (virAsprintf(&def->smartcards[i]->info.alias, "smartcard%d", i) < 0)
698+ goto no_memory;
699+ }
700+ if (def->watchdog) {
701+ if (virAsprintf(&def->watchdog->info.alias, "watchdog%d", 0) < 0)
702+ goto no_memory;
703+ }
704+ if (def->memballoon) {
705+ if (virAsprintf(&def->memballoon->info.alias, "balloon%d", 0) < 0)
706+ goto no_memory;
707+ }
708+
709+ return 0;
710+
711+ no_memory:
712+ virReportOOMError();
713+ return -1;
714+}
715+
716+
717+#define QEMU_PCI_ADDRESS_LAST_SLOT 31
718+#define QEMU_PCI_ADDRESS_LAST_FUNCTION 8
719+struct _qemuDomainPCIAddressSet {
720+ virHashTablePtr used;
721+ int nextslot;
722+};
723+
724+
725+static char *qemuPCIAddressAsString(virDomainDeviceInfoPtr dev)
726+{
727+ char *addr;
728+
729+ if (dev->addr.pci.domain != 0 ||
730+ dev->addr.pci.bus != 0) {
731+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
732+ _("Only PCI domain 0 and bus 0 are available"));
733+ return NULL;
734+ }
735+
736+ if (virAsprintf(&addr, "%d:%d:%d.%d",
737+ dev->addr.pci.domain,
738+ dev->addr.pci.bus,
739+ dev->addr.pci.slot,
740+ dev->addr.pci.function) < 0) {
741+ virReportOOMError();
742+ return NULL;
743+ }
744+ return addr;
745+}
746+
747+
748+static int qemuCollectPCIAddress(virDomainDefPtr def ATTRIBUTE_UNUSED,
749+ virDomainDeviceInfoPtr dev,
750+ void *opaque)
751+{
752+ int ret = -1;
753+ char *addr = NULL;
754+ qemuDomainPCIAddressSetPtr addrs = opaque;
755+
756+ if (dev->type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)
757+ return 0;
758+
759+ addr = qemuPCIAddressAsString(dev);
760+ if (!addr)
761+ goto cleanup;
762+
763+ if (virHashLookup(addrs->used, addr)) {
764+ if (dev->addr.pci.function != 0) {
765+ qemuReportError(VIR_ERR_XML_ERROR,
766+ _("Attempted double use of PCI Address '%s' "
767+ "(may need \"multifunction='on'\" for device on function 0"),
768+ addr);
769+ } else {
770+ qemuReportError(VIR_ERR_XML_ERROR,
771+ _("Attempted double use of PCI Address '%s'"), addr);
772+ }
773+ goto cleanup;
774+ }
775+
776+ VIR_DEBUG("Remembering PCI addr %s", addr);
777+ if (virHashAddEntry(addrs->used, addr, addr) < 0)
778+ goto cleanup;
779+ addr = NULL;
780+
781+ if ((dev->addr.pci.function == 0) &&
782+ (dev->addr.pci.multi != VIR_DOMAIN_DEVICE_ADDRESS_PCI_MULTI_ON)) {
783+ /* a function 0 w/o multifunction=on must reserve the entire slot */
784+ int function;
785+ virDomainDeviceInfo temp_dev = *dev;
786+
787+ for (function = 1; function < QEMU_PCI_ADDRESS_LAST_FUNCTION; function++) {
788+ temp_dev.addr.pci.function = function;
789+ addr = qemuPCIAddressAsString(&temp_dev);
790+ if (!addr)
791+ goto cleanup;
792+
793+ if (virHashLookup(addrs->used, addr)) {
794+ qemuReportError(VIR_ERR_XML_ERROR,
795+ _("Attempted double use of PCI Address '%s'"
796+ "(need \"multifunction='off'\" for device on function 0)"),
797+ addr);
798+ goto cleanup;
799+ }
800+
801+ VIR_DEBUG("Remembering PCI addr %s (multifunction=off for function 0)", addr);
802+ if (virHashAddEntry(addrs->used, addr, addr))
803+ goto cleanup;
804+ addr = NULL;
805+ }
806+ }
807+ ret = 0;
808+cleanup:
809+ VIR_FREE(addr);
810+ return ret;
811+}
812+
813+
814+int
815+qemuDomainAssignPCIAddresses(virDomainDefPtr def)
816+{
817+ int ret = -1;
818+ virBitmapPtr qemuCaps = NULL;
819+ qemuDomainPCIAddressSetPtr addrs = NULL;
820+
821+ if (qemuCapsExtractVersionInfo(def->emulator, def->os.arch,
822+ NULL,
823+ &qemuCaps) < 0)
824+ goto cleanup;
825+
826+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
827+ if (!(addrs = qemuDomainPCIAddressSetCreate(def)))
828+ goto cleanup;
829+
830+ if (qemuAssignDevicePCISlots(def, addrs) < 0)
831+ goto cleanup;
832+ }
833+
834+ ret = 0;
835+
836+cleanup:
837+ qemuCapsFree(qemuCaps);
838+ qemuDomainPCIAddressSetFree(addrs);
839+
840+ return ret;
841+}
842+
843+
844+static void
845+qemuDomainPCIAddressSetFreeEntry(void *payload,
846+ const void *name ATTRIBUTE_UNUSED)
847+{
848+ VIR_FREE(payload);
849+}
850+
851+qemuDomainPCIAddressSetPtr qemuDomainPCIAddressSetCreate(virDomainDefPtr def)
852+{
853+ qemuDomainPCIAddressSetPtr addrs;
854+
855+ if (VIR_ALLOC(addrs) < 0)
856+ goto no_memory;
857+
858+ if (!(addrs->used = virHashCreate(10, qemuDomainPCIAddressSetFreeEntry)))
859+ goto error;
860+
861+ if (virDomainDeviceInfoIterate(def, qemuCollectPCIAddress, addrs) < 0)
862+ goto error;
863+
864+ return addrs;
865+
866+no_memory:
867+ virReportOOMError();
868+error:
869+ qemuDomainPCIAddressSetFree(addrs);
870+ return NULL;
871+}
872+
873+/* check whether the slot is used by the other device
874+ * Return 0 if the slot is not used by the other device, or -1 if the slot
875+ * is used by the other device.
876+ */
877+static int qemuDomainPCIAddressCheckSlot(qemuDomainPCIAddressSetPtr addrs,
878+ virDomainDeviceInfoPtr dev)
879+{
880+ char *addr;
881+ virDomainDeviceInfo temp_dev;
882+ int function;
883+
884+ temp_dev = *dev;
885+ for (function = 0; function < QEMU_PCI_ADDRESS_LAST_FUNCTION; function++) {
886+ temp_dev.addr.pci.function = function;
887+ addr = qemuPCIAddressAsString(&temp_dev);
888+ if (!addr)
889+ return -1;
890+
891+ if (virHashLookup(addrs->used, addr)) {
892+ VIR_FREE(addr);
893+ return -1;
894+ }
895+
896+ VIR_FREE(addr);
897+ }
898+
899+ return 0;
900+}
901+
902+int qemuDomainPCIAddressReserveAddr(qemuDomainPCIAddressSetPtr addrs,
903+ virDomainDeviceInfoPtr dev)
904+{
905+ char *addr;
906+
907+ addr = qemuPCIAddressAsString(dev);
908+ if (!addr)
909+ return -1;
910+
911+ VIR_DEBUG("Reserving PCI addr %s", addr);
912+
913+ if (virHashLookup(addrs->used, addr)) {
914+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
915+ _("unable to reserve PCI address %s"), addr);
916+ VIR_FREE(addr);
917+ return -1;
918+ }
919+
920+ if (virHashAddEntry(addrs->used, addr, addr)) {
921+ VIR_FREE(addr);
922+ return -1;
923+ }
924+
925+ if (dev->addr.pci.slot > addrs->nextslot) {
926+ addrs->nextslot = dev->addr.pci.slot + 1;
927+ if (QEMU_PCI_ADDRESS_LAST_SLOT < addrs->nextslot)
928+ addrs->nextslot = 0;
929+ }
930+
931+ return 0;
932+}
933+
934+int qemuDomainPCIAddressReserveFunction(qemuDomainPCIAddressSetPtr addrs,
935+ int slot, int function)
936+{
937+ virDomainDeviceInfo dev;
938+
939+ dev.addr.pci.domain = 0;
940+ dev.addr.pci.bus = 0;
941+ dev.addr.pci.slot = slot;
942+ dev.addr.pci.function = function;
943+
944+ return qemuDomainPCIAddressReserveAddr(addrs, &dev);
945+}
946+
947+int qemuDomainPCIAddressReserveSlot(qemuDomainPCIAddressSetPtr addrs,
948+ int slot)
949+{
950+ int function;
951+
952+ for (function = 0; function < QEMU_PCI_ADDRESS_LAST_FUNCTION; function++) {
953+ if (qemuDomainPCIAddressReserveFunction(addrs, slot, function) < 0)
954+ goto cleanup;
955+ }
956+
957+ return 0;
958+
959+cleanup:
960+ for (function--; function >= 0; function--) {
961+ qemuDomainPCIAddressReleaseFunction(addrs, slot, function);
962+ }
963+ return -1;
964+}
965+
966+int qemuDomainPCIAddressEnsureAddr(qemuDomainPCIAddressSetPtr addrs,
967+ virDomainDeviceInfoPtr dev)
968+{
969+ int ret = 0;
970+ if (dev->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
971+ /* We do not support hotplug multi-function PCI device now, so we should
972+ * reserve the whole slot. The function of the PCI device must be 0.
973+ */
974+ if (dev->addr.pci.function != 0) {
975+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
976+ _("Only PCI device addresses with function=0"
977+ " are supported"));
978+ return -1;
979+ }
980+
981+ ret = qemuDomainPCIAddressReserveSlot(addrs, dev->addr.pci.slot);
982+ } else {
983+ ret = qemuDomainPCIAddressSetNextAddr(addrs, dev);
984+ }
985+ return ret;
986+}
987+
988+
989+int qemuDomainPCIAddressReleaseAddr(qemuDomainPCIAddressSetPtr addrs,
990+ virDomainDeviceInfoPtr dev)
991+{
992+ char *addr;
993+ int ret;
994+
995+ addr = qemuPCIAddressAsString(dev);
996+ if (!addr)
997+ return -1;
998+
999+ ret = virHashRemoveEntry(addrs->used, addr);
1000+
1001+ VIR_FREE(addr);
1002+
1003+ return ret;
1004+}
1005+
1006+int qemuDomainPCIAddressReleaseFunction(qemuDomainPCIAddressSetPtr addrs,
1007+ int slot, int function)
1008+{
1009+ virDomainDeviceInfo dev;
1010+
1011+ dev.addr.pci.domain = 0;
1012+ dev.addr.pci.bus = 0;
1013+ dev.addr.pci.slot = slot;
1014+ dev.addr.pci.function = function;
1015+
1016+ return qemuDomainPCIAddressReleaseAddr(addrs, &dev);
1017+}
1018+
1019+int qemuDomainPCIAddressReleaseSlot(qemuDomainPCIAddressSetPtr addrs, int slot)
1020+{
1021+ virDomainDeviceInfo dev;
1022+ char *addr;
1023+ int ret = 0;
1024+ unsigned int *function = &dev.addr.pci.function;
1025+
1026+ dev.addr.pci.domain = 0;
1027+ dev.addr.pci.bus = 0;
1028+ dev.addr.pci.slot = slot;
1029+
1030+ for (*function = 0; *function < QEMU_PCI_ADDRESS_LAST_FUNCTION; (*function)++) {
1031+ addr = qemuPCIAddressAsString(&dev);
1032+ if (!addr)
1033+ return -1;
1034+
1035+ if (!virHashLookup(addrs->used, addr)) {
1036+ VIR_FREE(addr);
1037+ continue;
1038+ }
1039+
1040+ VIR_FREE(addr);
1041+
1042+ if (qemuDomainPCIAddressReleaseFunction(addrs, slot, *function) < 0)
1043+ ret = -1;
1044+ }
1045+
1046+ return ret;
1047+}
1048+
1049+void qemuDomainPCIAddressSetFree(qemuDomainPCIAddressSetPtr addrs)
1050+{
1051+ if (!addrs)
1052+ return;
1053+
1054+ virHashFree(addrs->used);
1055+ VIR_FREE(addrs);
1056+}
1057+
1058+
1059+int qemuDomainPCIAddressSetNextAddr(qemuDomainPCIAddressSetPtr addrs,
1060+ virDomainDeviceInfoPtr dev)
1061+{
1062+ int i;
1063+ int iteration;
1064+
1065+ for (i = addrs->nextslot, iteration = 0;
1066+ iteration <= QEMU_PCI_ADDRESS_LAST_SLOT; i++, iteration++) {
1067+ virDomainDeviceInfo maybe;
1068+ char *addr;
1069+
1070+ if (QEMU_PCI_ADDRESS_LAST_SLOT < i)
1071+ i = 0;
1072+ memset(&maybe, 0, sizeof(maybe));
1073+ maybe.addr.pci.domain = 0;
1074+ maybe.addr.pci.bus = 0;
1075+ maybe.addr.pci.slot = i;
1076+ maybe.addr.pci.function = 0;
1077+
1078+ if (!(addr = qemuPCIAddressAsString(&maybe)))
1079+ return -1;
1080+
1081+ if (qemuDomainPCIAddressCheckSlot(addrs, &maybe) < 0) {
1082+ VIR_DEBUG("PCI addr %s already in use", addr);
1083+ VIR_FREE(addr);
1084+ continue;
1085+ }
1086+
1087+ VIR_DEBUG("Allocating PCI addr %s", addr);
1088+ VIR_FREE(addr);
1089+
1090+ if (qemuDomainPCIAddressReserveSlot(addrs, i) < 0)
1091+ return -1;
1092+
1093+ dev->type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1094+ dev->addr.pci = maybe.addr.pci;
1095+
1096+ addrs->nextslot = i + 1;
1097+ if (QEMU_PCI_ADDRESS_LAST_SLOT < addrs->nextslot)
1098+ addrs->nextslot = 0;
1099+
1100+ return 0;
1101+ }
1102+
1103+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1104+ "%s", _("No more available PCI addresses"));
1105+ return -1;
1106+}
1107+
1108+/*
1109+ * This assigns static PCI slots to all configured devices.
1110+ * The ordering here is chosen to match the ordering used
1111+ * with old QEMU < 0.12, so that if a user updates a QEMU
1112+ * host from old QEMU to QEMU >= 0.12, their guests should
1113+ * get PCI addresses in the same order as before.
1114+ *
1115+ * NB, if they previously hotplugged devices then all bets
1116+ * are off. Hotplug for old QEMU was unfixably broken wrt
1117+ * to stable PCI addressing.
1118+ *
1119+ * Order is:
1120+ *
1121+ * - Host bridge (slot 0)
1122+ * - PIIX3 ISA bridge, IDE controller, something else unknown, USB controller (slot 1)
1123+ * - Video (slot 2)
1124+ *
1125+ * Incrementally assign slots from 3 onwards:
1126+ *
1127+ * - Net
1128+ * - Sound
1129+ * - SCSI controllers
1130+ * - VirtIO block
1131+ * - VirtIO balloon
1132+ * - Host device passthrough
1133+ * - Watchdog (not IB700)
1134+ *
1135+ * Prior to this function being invoked, qemuCollectPCIAddress() will have
1136+ * added all existing PCI addresses from the 'def' to 'addrs'. Thus this
1137+ * function must only try to reserve addresses if info.type == NONE and
1138+ * skip over info.type == PCI
1139+ */
1140+int
1141+qemuAssignDevicePCISlots(virDomainDefPtr def, qemuDomainPCIAddressSetPtr addrs)
1142+{
1143+ int i;
1144+ bool reservedIDE = false;
1145+ bool reservedUSB = false;
1146+ bool reservedVGA = false;
1147+ int function;
1148+
1149+ /* Host bridge */
1150+ if (qemuDomainPCIAddressReserveSlot(addrs, 0) < 0)
1151+ goto error;
1152+
1153+ /* Verify that first IDE and USB controllers (if any) is on the PIIX3, fn 1 */
1154+ for (i = 0; i < def->ncontrollers ; i++) {
1155+ /* First IDE controller lives on the PIIX3 at slot=1, function=1 */
1156+ if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE &&
1157+ def->controllers[i]->idx == 0) {
1158+ if (def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1159+ if (def->controllers[i]->info.addr.pci.domain != 0 ||
1160+ def->controllers[i]->info.addr.pci.bus != 0 ||
1161+ def->controllers[i]->info.addr.pci.slot != 1 ||
1162+ def->controllers[i]->info.addr.pci.function != 1) {
1163+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1164+ _("Primary IDE controller must have PCI address 0:0:1.1"));
1165+ goto error;
1166+ }
1167+ /* If TYPE==PCI, then qemuCollectPCIAddress() function
1168+ * has already reserved the address, so we must skip */
1169+ reservedIDE = true;
1170+ } else {
1171+ def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1172+ def->controllers[i]->info.addr.pci.domain = 0;
1173+ def->controllers[i]->info.addr.pci.bus = 0;
1174+ def->controllers[i]->info.addr.pci.slot = 1;
1175+ def->controllers[i]->info.addr.pci.function = 1;
1176+ }
1177+ } else if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
1178+ def->controllers[i]->idx == 0 &&
1179+ (def->controllers[i]->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI ||
1180+ def->controllers[i]->model == -1)) {
1181+ if (def->controllers[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1182+ if (def->controllers[i]->info.addr.pci.domain != 0 ||
1183+ def->controllers[i]->info.addr.pci.bus != 0 ||
1184+ def->controllers[i]->info.addr.pci.slot != 1 ||
1185+ def->controllers[i]->info.addr.pci.function != 2) {
1186+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1187+ _("PIIX3 USB controller must have PCI address 0:0:1.2"));
1188+ goto error;
1189+ }
1190+ reservedUSB = true;
1191+ } else {
1192+ def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1193+ def->controllers[i]->info.addr.pci.domain = 0;
1194+ def->controllers[i]->info.addr.pci.bus = 0;
1195+ def->controllers[i]->info.addr.pci.slot = 1;
1196+ def->controllers[i]->info.addr.pci.function = 2;
1197+ }
1198+ }
1199+ }
1200+
1201+ /* PIIX3 (ISA bridge, IDE controller, something else unknown, USB controller)
1202+ * hardcoded slot=1, multifunction device
1203+ */
1204+ for (function = 0; function < QEMU_PCI_ADDRESS_LAST_FUNCTION; function++) {
1205+ if ((function == 1 && reservedIDE) ||
1206+ (function == 2 && reservedUSB))
1207+ /* we have reserved this pci address */
1208+ continue;
1209+
1210+ if (qemuDomainPCIAddressReserveFunction(addrs, 1, function) < 0)
1211+ goto error;
1212+ }
1213+
1214+ /* First VGA is hardcoded slot=2 */
1215+ if (def->nvideos > 0) {
1216+ if (def->videos[0]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1217+ if (def->videos[0]->info.addr.pci.domain != 0 ||
1218+ def->videos[0]->info.addr.pci.bus != 0 ||
1219+ def->videos[0]->info.addr.pci.slot != 2 ||
1220+ def->videos[0]->info.addr.pci.function != 0) {
1221+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1222+ _("Primary video card must have PCI address 0:0:2.0"));
1223+ goto error;
1224+ }
1225+ /* If TYPE==PCI, then qemuCollectPCIAddress() function
1226+ * has already reserved the address, so we must skip */
1227+ reservedVGA = true;
1228+ } else {
1229+ def->videos[0]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
1230+ def->videos[0]->info.addr.pci.domain = 0;
1231+ def->videos[0]->info.addr.pci.bus = 0;
1232+ def->videos[0]->info.addr.pci.slot = 2;
1233+ def->videos[0]->info.addr.pci.function = 0;
1234+ }
1235+ }
1236+
1237+ if (!reservedVGA
1238+ && qemuDomainPCIAddressReserveSlot(addrs, 2) < 0)
1239+ goto error;
1240+
1241+ for (i = 0; i < def->nfss ; i++) {
1242+ if (def->fss[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1243+ continue;
1244+
1245+ /* Only support VirtIO-9p-pci so far. If that changes,
1246+ * we might need to skip devices here */
1247+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->fss[i]->info) < 0)
1248+ goto error;
1249+ }
1250+
1251+ /* Network interfaces */
1252+ for (i = 0; i < def->nnets ; i++) {
1253+ if (def->nets[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1254+ continue;
1255+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->nets[i]->info) < 0)
1256+ goto error;
1257+ }
1258+
1259+ /* Sound cards */
1260+ for (i = 0; i < def->nsounds ; i++) {
1261+ if (def->sounds[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1262+ continue;
1263+ /* Skip ISA sound card, and PCSPK */
1264+ if (def->sounds[i]->model == VIR_DOMAIN_SOUND_MODEL_SB16 ||
1265+ def->sounds[i]->model == VIR_DOMAIN_SOUND_MODEL_PCSPK)
1266+ continue;
1267+
1268+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->sounds[i]->info) < 0)
1269+ goto error;
1270+ }
1271+
1272+ /* Disk controllers (SCSI only for now) */
1273+ for (i = 0; i < def->ncontrollers ; i++) {
1274+ /* FDC lives behind the ISA bridge; CCID is a usb device */
1275+ if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_FDC ||
1276+ def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_CCID)
1277+ continue;
1278+
1279+ /* First IDE controller lives on the PIIX3 at slot=1, function=1,
1280+ dealt with earlier on*/
1281+ if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE &&
1282+ def->controllers[i]->idx == 0)
1283+ continue;
1284+
1285+ if (def->controllers[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1286+ continue;
1287+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->controllers[i]->info) < 0)
1288+ goto error;
1289+ }
1290+
1291+ /* Disks (VirtIO only for now) */
1292+ for (i = 0; i < def->ndisks ; i++) {
1293+ /* Only VirtIO disks use PCI addrs */
1294+ if (def->disks[i]->bus != VIR_DOMAIN_DISK_BUS_VIRTIO)
1295+ continue;
1296+
1297+ if (def->disks[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)
1298+ continue;
1299+
1300+ if (def->disks[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
1301+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
1302+ _("virtio only support device address type 'PCI'"));
1303+ goto error;
1304+ }
1305+
1306+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->disks[i]->info) < 0)
1307+ goto error;
1308+ }
1309+
1310+ /* Host PCI devices */
1311+ for (i = 0; i < def->nhostdevs ; i++) {
1312+ if (def->hostdevs[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1313+ continue;
1314+ if (def->hostdevs[i]->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
1315+ def->hostdevs[i]->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
1316+ continue;
1317+
1318+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->hostdevs[i]->info) < 0)
1319+ goto error;
1320+ }
1321+
1322+ /* VirtIO balloon */
1323+ if (def->memballoon &&
1324+ def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO &&
1325+ def->memballoon->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
1326+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->memballoon->info) < 0)
1327+ goto error;
1328+ }
1329+
1330+ /* A watchdog - skip IB700, it is not a PCI device */
1331+ if (def->watchdog &&
1332+ def->watchdog->model != VIR_DOMAIN_WATCHDOG_MODEL_IB700 &&
1333+ def->watchdog->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
1334+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->watchdog->info) < 0)
1335+ goto error;
1336+ }
1337+
1338+ /* Further non-primary video cards */
1339+ for (i = 1; i < def->nvideos ; i++) {
1340+ if (def->videos[i]->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE)
1341+ continue;
1342+ if (qemuDomainPCIAddressSetNextAddr(addrs, &def->videos[i]->info) < 0)
1343+ goto error;
1344+ }
1345+ for (i = 0; i < def->ninputs ; i++) {
1346+ /* Nada - none are PCI based (yet) */
1347+ }
1348+ for (i = 0; i < def->nparallels ; i++) {
1349+ /* Nada - none are PCI based (yet) */
1350+ }
1351+ for (i = 0; i < def->nserials ; i++) {
1352+ /* Nada - none are PCI based (yet) */
1353+ }
1354+ for (i = 0; i < def->nchannels ; i++) {
1355+ /* Nada - none are PCI based (yet) */
1356+ }
1357+ for (i = 0; i < def->nhubs ; i++) {
1358+ /* Nada - none are PCI based (yet) */
1359+ }
1360+
1361+ return 0;
1362+
1363+error:
1364+ return -1;
1365+}
1366+
1367+static void
1368+qemuUsbId(virBufferPtr buf, int idx)
1369+{
1370+ if (idx == 0)
1371+ virBufferAsprintf(buf, "usb");
1372+ else
1373+ virBufferAsprintf(buf, "usb%d", idx);
1374+}
1375+
1376+static int
1377+qemuBuildDeviceAddressStr(virBufferPtr buf,
1378+ virDomainDeviceInfoPtr info,
1379+ virBitmapPtr qemuCaps)
1380+{
1381+ if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
1382+ if (info->addr.pci.domain != 0) {
1383+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1384+ _("Only PCI device addresses with domain=0 are supported"));
1385+ return -1;
1386+ }
1387+ if (info->addr.pci.bus != 0) {
1388+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1389+ _("Only PCI device addresses with bus=0 are supported"));
1390+ return -1;
1391+ }
1392+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_PCI_MULTIFUNCTION)) {
1393+ if (info->addr.pci.function > 7) {
1394+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1395+ _("The function of PCI device addresses must "
1396+ "less than 8"));
1397+ return -1;
1398+ }
1399+ } else {
1400+ if (info->addr.pci.function != 0) {
1401+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1402+ _("Only PCI device addresses with function=0 "
1403+ "are supported with this QEMU binary"));
1404+ return -1;
1405+ }
1406+ if (info->addr.pci.multi == VIR_DOMAIN_DEVICE_ADDRESS_PCI_MULTI_ON) {
1407+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1408+ _("'multifunction=on' is not supported with "
1409+ "this QEMU binary"));
1410+ return -1;
1411+ }
1412+ }
1413+
1414+ /* XXX
1415+ * When QEMU grows support for > 1 PCI bus, then pci.0 changes
1416+ * to pci.1, pci.2, etc
1417+ * When QEMU grows support for > 1 PCI domain, then pci.0 change
1418+ * to pciNN.0 where NN is the domain number
1419+ */
1420+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_PCI_MULTIBUS))
1421+ virBufferAsprintf(buf, ",bus=pci.0");
1422+ else
1423+ virBufferAsprintf(buf, ",bus=pci");
1424+ if (info->addr.pci.multi == VIR_DOMAIN_DEVICE_ADDRESS_PCI_MULTI_ON)
1425+ virBufferAddLit(buf, ",multifunction=on");
1426+ else if (info->addr.pci.multi == VIR_DOMAIN_DEVICE_ADDRESS_PCI_MULTI_OFF)
1427+ virBufferAddLit(buf, ",multifunction=off");
1428+ virBufferAsprintf(buf, ",addr=0x%x", info->addr.pci.slot);
1429+ if (info->addr.pci.function != 0)
1430+ virBufferAsprintf(buf, ".0x%x", info->addr.pci.function);
1431+ } else if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_USB) {
1432+ virBufferAsprintf(buf, ",bus=");
1433+ qemuUsbId(buf, info->addr.usb.bus);
1434+ virBufferAsprintf(buf, ".0,port=%s", info->addr.usb.port);
1435+ }
1436+
1437+ return 0;
1438+}
1439+
1440+static int
1441+qemuBuildIoEventFdStr(virBufferPtr buf,
1442+ enum virDomainIoEventFd use,
1443+ virBitmapPtr qemuCaps)
1444+{
1445+ if (use && qemuCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_IOEVENTFD))
1446+ virBufferAsprintf(buf, ",ioeventfd=%s",
1447+ virDomainIoEventFdTypeToString(use));
1448+ return 0;
1449+}
1450+
1451+#define QEMU_SERIAL_PARAM_ACCEPTED_CHARS \
1452+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
1453+
1454+static int
1455+qemuSafeSerialParamValue(const char *value)
1456+{
1457+ if (strspn(value, QEMU_SERIAL_PARAM_ACCEPTED_CHARS) != strlen (value)) {
1458+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1459+ _("driver serial '%s' contains unsafe characters"),
1460+ value);
1461+ return -1;
1462+ }
1463+
1464+ return 0;
1465+}
1466+
1467+static int
1468+qemuBuildRBDString(virConnectPtr conn,
1469+ virDomainDiskDefPtr disk,
1470+ virBufferPtr opt)
1471+{
1472+ int i, ret = 0;
1473+ virSecretPtr sec = NULL;
1474+ char *secret = NULL;
1475+ size_t secret_size;
1476+
1477+ virBufferAsprintf(opt, "rbd:%s", disk->src);
1478+ if (disk->auth.username) {
1479+ virBufferEscape(opt, ":", ":id=%s", disk->auth.username);
1480+ /* look up secret */
1481+ switch (disk->auth.secretType) {
1482+ case VIR_DOMAIN_DISK_SECRET_TYPE_UUID:
1483+ sec = virSecretLookupByUUID(conn,
1484+ disk->auth.secret.uuid);
1485+ break;
1486+ case VIR_DOMAIN_DISK_SECRET_TYPE_USAGE:
1487+ sec = virSecretLookupByUsage(conn,
1488+ VIR_SECRET_USAGE_TYPE_CEPH,
1489+ disk->auth.secret.usage);
1490+ break;
1491+ }
1492+
1493+ if (sec) {
1494+ char *base64 = NULL;
1495+
1496+ secret = (char *)conn->secretDriver->getValue(sec, &secret_size, 0,
1497+ VIR_SECRET_GET_VALUE_INTERNAL_CALL);
1498+ if (secret == NULL) {
1499+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1500+ _("could not get the value of the secret for username %s"),
1501+ disk->auth.username);
1502+ goto error;
1503+ }
1504+ /* qemu/librbd wants it base64 encoded */
1505+ base64_encode_alloc(secret, secret_size, &base64);
1506+ if (!base64) {
1507+ virReportOOMError();
1508+ goto error;
1509+ }
1510+ virBufferEscape(opt, ":;", ":key=%s:auth_supported=cephx\\;none",
1511+ base64);
1512+ VIR_FREE(base64);
1513+ } else {
1514+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1515+ _("rbd username '%s' specified but secret not found"),
1516+ disk->auth.username);
1517+ goto error;
1518+ }
1519+ }
1520+
1521+ if (disk->nhosts > 0) {
1522+ virBufferAddLit(opt, ":mon_host=");
1523+ for (i = 0; i < disk->nhosts; ++i) {
1524+ if (i) {
1525+ virBufferAddLit(opt, "\\;");
1526+ }
1527+ if (disk->hosts[i].port) {
1528+ virBufferAsprintf(opt, "%s\\:%s",
1529+ disk->hosts[i].name,
1530+ disk->hosts[i].port);
1531+ } else {
1532+ virBufferAsprintf(opt, "%s", disk->hosts[i].name);
1533+ }
1534+ }
1535+ }
1536+
1537+cleanup:
1538+ VIR_FREE(secret);
1539+ if (sec)
1540+ virUnrefSecret(sec);
1541+
1542+ return ret;
1543+
1544+error:
1545+ ret = -1;
1546+ goto cleanup;
1547+}
1548+
1549+static int qemuAddRBDHost(virDomainDiskDefPtr disk, char *hostport)
1550+{
1551+ char *port;
1552+
1553+ disk->nhosts++;
1554+ if (VIR_REALLOC_N(disk->hosts, disk->nhosts) < 0)
1555+ goto no_memory;
1556+
1557+ port = strstr(hostport, "\\:");
1558+ if (port) {
1559+ *port = '\0';
1560+ port += 2;
1561+ disk->hosts[disk->nhosts-1].port = strdup(port);
1562+ if (!disk->hosts[disk->nhosts-1].port)
1563+ goto no_memory;
1564+ } else {
1565+ disk->hosts[disk->nhosts-1].port = strdup("6789");
1566+ if (!disk->hosts[disk->nhosts-1].port)
1567+ goto no_memory;
1568+ }
1569+ disk->hosts[disk->nhosts-1].name = strdup(hostport);
1570+ if (!disk->hosts[disk->nhosts-1].name)
1571+ goto no_memory;
1572+ return 0;
1573+
1574+no_memory:
1575+ virReportOOMError();
1576+ VIR_FREE(disk->hosts[disk->nhosts-1].port);
1577+ VIR_FREE(disk->hosts[disk->nhosts-1].name);
1578+ return -1;
1579+}
1580+
1581+/* disk->src initially has everything after the rbd: prefix */
1582+static int qemuParseRBDString(virDomainDiskDefPtr disk)
1583+{
1584+ char *options = NULL;
1585+ char *p, *e, *next;
1586+
1587+ p = strchr(disk->src, ':');
1588+ if (p) {
1589+ options = strdup(p + 1);
1590+ if (!options)
1591+ goto no_memory;
1592+ *p = '\0';
1593+ }
1594+
1595+ /* options */
1596+ if (!options)
1597+ return 0; /* all done */
1598+
1599+ p = options;
1600+ while (*p) {
1601+ /* find : delimiter or end of string */
1602+ for (e = p; *e && *e != ':'; ++e) {
1603+ if (*e == '\\') {
1604+ e++;
1605+ if (*e == '\0')
1606+ break;
1607+ }
1608+ }
1609+ if (*e == '\0') {
1610+ next = e; /* last kv pair */
1611+ } else {
1612+ next = e + 1;
1613+ *e = '\0';
1614+ }
1615+
1616+ if (STRPREFIX(p, "id=")) {
1617+ disk->auth.username = strdup(p + strlen("id="));
1618+ if (!disk->auth.username)
1619+ goto no_memory;
1620+ }
1621+ if (STRPREFIX(p, "mon_host=")) {
1622+ char *h, *sep;
1623+
1624+ h = p + strlen("mon_host=");
1625+ while (h < e) {
1626+ for (sep = h; sep < e; ++sep) {
1627+ if (*sep == '\\' && (sep[1] == ',' ||
1628+ sep[1] == ';' ||
1629+ sep[1] == ' ')) {
1630+ *sep = '\0';
1631+ sep += 2;
1632+ break;
1633+ }
1634+ }
1635+ if (qemuAddRBDHost(disk, h) < 0) {
1636+ return -1;
1637+ }
1638+ h = sep;
1639+ }
1640+ }
1641+
1642+ p = next;
1643+ }
1644+ return 0;
1645+
1646+no_memory:
1647+ virReportOOMError();
1648+ return -1;
1649+}
1650+
1651+char *
1652+qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,
1653+ virDomainDiskDefPtr disk,
1654+ bool bootable,
1655+ virBitmapPtr qemuCaps)
1656+{
1657+ virBuffer opt = VIR_BUFFER_INITIALIZER;
1658+ const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
1659+ int idx = virDiskNameToIndex(disk->dst);
1660+ int busid = -1, unitid = -1;
1661+
1662+ if (idx < 0) {
1663+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1664+ _("unsupported disk type '%s'"), disk->dst);
1665+ goto error;
1666+ }
1667+
1668+ switch (disk->bus) {
1669+ case VIR_DOMAIN_DISK_BUS_SCSI:
1670+ if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
1671+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1672+ _("unexpected address type for scsi disk"));
1673+ goto error;
1674+ }
1675+
1676+ /* Setting bus= attr for SCSI drives, causes a controller
1677+ * to be created. Yes this is slightly odd. It is not possible
1678+ * to have > 1 bus on a SCSI controller (yet). */
1679+ if (disk->info.addr.drive.bus != 0) {
1680+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1681+ "%s", _("SCSI controller only supports 1 bus"));
1682+ goto error;
1683+ }
1684+ busid = disk->info.addr.drive.controller;
1685+ unitid = disk->info.addr.drive.unit;
1686+ break;
1687+
1688+ case VIR_DOMAIN_DISK_BUS_IDE:
1689+ if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
1690+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1691+ _("unexpected address type for ide disk"));
1692+ goto error;
1693+ }
1694+ /* We can only have 1 IDE controller (currently) */
1695+ if (disk->info.addr.drive.controller != 0) {
1696+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1697+ _("Only 1 %s controller is supported"), bus);
1698+ goto error;
1699+ }
1700+ busid = disk->info.addr.drive.bus;
1701+ unitid = disk->info.addr.drive.unit;
1702+ break;
1703+
1704+ case VIR_DOMAIN_DISK_BUS_FDC:
1705+ if (disk->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE) {
1706+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1707+ _("unexpected address type for fdc disk"));
1708+ goto error;
1709+ }
1710+ /* We can only have 1 FDC controller (currently) */
1711+ if (disk->info.addr.drive.controller != 0) {
1712+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1713+ _("Only 1 %s controller is supported"), bus);
1714+ goto error;
1715+ }
1716+ /* We can only have 1 FDC bus (currently) */
1717+ if (disk->info.addr.drive.bus != 0) {
1718+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1719+ _("Only 1 %s bus is supported"), bus);
1720+ goto error;
1721+ }
1722+ unitid = disk->info.addr.drive.unit;
1723+
1724+ break;
1725+
1726+ case VIR_DOMAIN_DISK_BUS_VIRTIO:
1727+ /* Each virtio drive is a separate PCI device, no unit/busid or index */
1728+ idx = -1;
1729+ break;
1730+
1731+ case VIR_DOMAIN_DISK_BUS_XEN:
1732+ /* Xen has no address type currently, so assign based on index */
1733+ break;
1734+ }
1735+
1736+ /* disk->src is NULL when we use nbd disks */
1737+ if (disk->src || (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK &&
1738+ disk->protocol == VIR_DOMAIN_DISK_PROTOCOL_NBD)) {
1739+ if (disk->type == VIR_DOMAIN_DISK_TYPE_DIR) {
1740+ /* QEMU only supports magic FAT format for now */
1741+ if (disk->driverType &&
1742+ STRNEQ(disk->driverType, "fat")) {
1743+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1744+ _("unsupported disk driver type for '%s'"),
1745+ disk->driverType);
1746+ goto error;
1747+ }
1748+ if (!disk->readonly) {
1749+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1750+ _("cannot create virtual FAT disks in read-write mode"));
1751+ goto error;
1752+ }
1753+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
1754+ virBufferAsprintf(&opt, "file=fat:floppy:%s,", disk->src);
1755+ else
1756+ virBufferAsprintf(&opt, "file=fat:%s,", disk->src);
1757+ } else if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK) {
1758+ switch (disk->protocol) {
1759+ case VIR_DOMAIN_DISK_PROTOCOL_NBD:
1760+ if (disk->nhosts != 1) {
1761+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1762+ _("NBD accepts only one host"));
1763+ goto error;
1764+ }
1765+ virBufferAsprintf(&opt, "file=nbd:%s:%s,",
1766+ disk->hosts->name, disk->hosts->port);
1767+ break;
1768+ case VIR_DOMAIN_DISK_PROTOCOL_RBD:
1769+ virBufferAddLit(&opt, "file=");
1770+ if (qemuBuildRBDString(conn, disk, &opt) < 0)
1771+ goto error;
1772+ virBufferAddChar(&opt, ',');
1773+ break;
1774+ case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG:
1775+ if (disk->nhosts == 0)
1776+ virBufferAsprintf(&opt, "file=sheepdog:%s,", disk->src);
1777+ else
1778+ /* only one host is supported now */
1779+ virBufferAsprintf(&opt, "file=sheepdog:%s:%s:%s,",
1780+ disk->hosts->name, disk->hosts->port,
1781+ disk->src);
1782+ break;
1783+ }
1784+ } else {
1785+ virBufferAsprintf(&opt, "file=%s,", disk->src);
1786+ }
1787+ }
1788+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
1789+ virBufferAddLit(&opt, "if=none");
1790+ else
1791+ virBufferAsprintf(&opt, "if=%s", bus);
1792+
1793+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
1794+ virBufferAddLit(&opt, ",media=cdrom");
1795+
1796+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
1797+ virBufferAsprintf(&opt, ",id=%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias);
1798+ } else {
1799+ if (busid == -1 && unitid == -1) {
1800+ if (idx != -1)
1801+ virBufferAsprintf(&opt, ",index=%d", idx);
1802+ } else {
1803+ if (busid != -1)
1804+ virBufferAsprintf(&opt, ",bus=%d", busid);
1805+ if (unitid != -1)
1806+ virBufferAsprintf(&opt, ",unit=%d", unitid);
1807+ }
1808+ }
1809+ if (bootable &&
1810+ qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_BOOT) &&
1811+ disk->device == VIR_DOMAIN_DISK_DEVICE_DISK &&
1812+ disk->bus != VIR_DOMAIN_DISK_BUS_IDE)
1813+ virBufferAddLit(&opt, ",boot=on");
1814+ if (disk->readonly &&
1815+ qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_READONLY))
1816+ virBufferAddLit(&opt, ",readonly=on");
1817+ if (disk->transient) {
1818+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1819+ _("transient disks not supported yet"));
1820+ goto error;
1821+ }
1822+ if (disk->driverType && *disk->driverType != '\0' &&
1823+ disk->type != VIR_DOMAIN_DISK_TYPE_DIR &&
1824+ qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_FORMAT))
1825+ virBufferAsprintf(&opt, ",format=%s", disk->driverType);
1826+ if (disk->serial &&
1827+ qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_SERIAL)) {
1828+ if (qemuSafeSerialParamValue(disk->serial) < 0)
1829+ goto error;
1830+ virBufferAsprintf(&opt, ",serial=%s", disk->serial);
1831+ }
1832+
1833+ if (disk->cachemode) {
1834+ const char *mode = NULL;
1835+
1836+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_CACHE_V2)) {
1837+ mode = qemuDiskCacheV2TypeToString(disk->cachemode);
1838+
1839+ if (disk->cachemode == VIR_DOMAIN_DISK_CACHE_DIRECTSYNC &&
1840+ !qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_CACHE_DIRECTSYNC)) {
1841+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1842+ _("disk cache mode 'directsync' is not "
1843+ "supported by this QEMU"));
1844+ goto error;
1845+ } else if (disk->cachemode == VIR_DOMAIN_DISK_CACHE_UNSAFE &&
1846+ !qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_CACHE_UNSAFE)) {
1847+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1848+ _("disk cache mode 'unsafe' is not "
1849+ "supported by this QEMU"));
1850+ goto error;
1851+ }
1852+ } else {
1853+ mode = qemuDiskCacheV1TypeToString(disk->cachemode);
1854+ }
1855+
1856+ virBufferAsprintf(&opt, ",cache=%s", mode);
1857+ } else if (disk->shared && !disk->readonly) {
1858+ virBufferAddLit(&opt, ",cache=off");
1859+ }
1860+
1861+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_MONITOR_JSON)) {
1862+ const char *wpolicy = NULL, *rpolicy = NULL;
1863+
1864+ if (disk->error_policy)
1865+ wpolicy = virDomainDiskErrorPolicyTypeToString(disk->error_policy);
1866+ if (disk->rerror_policy)
1867+ rpolicy = virDomainDiskErrorPolicyTypeToString(disk->rerror_policy);
1868+
1869+ if (disk->error_policy == VIR_DOMAIN_DISK_ERROR_POLICY_ENOSPACE) {
1870+ /* in the case of enospace, the option is spelled
1871+ * differently in qemu, and it's only valid for werror,
1872+ * not for rerror, so leave leave rerror NULL.
1873+ */
1874+ wpolicy = "enospc";
1875+ } else if (!rpolicy) {
1876+ /* for other policies, rpolicy can match wpolicy */
1877+ rpolicy = wpolicy;
1878+ }
1879+
1880+ if (wpolicy)
1881+ virBufferAsprintf(&opt, ",werror=%s", wpolicy);
1882+ if (rpolicy)
1883+ virBufferAsprintf(&opt, ",rerror=%s", rpolicy);
1884+ }
1885+
1886+ if (disk->iomode) {
1887+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_AIO)) {
1888+ virBufferAsprintf(&opt, ",aio=%s",
1889+ virDomainDiskIoTypeToString(disk->iomode));
1890+ } else {
1891+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1892+ _("disk aio mode not supported with this "
1893+ "QEMU binary"));
1894+ goto error;
1895+ }
1896+ }
1897+
1898+ /* block I/O throttling */
1899+ if (disk->blkdeviotune.total_bytes_sec) {
1900+ virBufferAsprintf(&opt, ",bps=%llu",
1901+ disk->blkdeviotune.total_bytes_sec);
1902+ }
1903+
1904+ if (disk->blkdeviotune.read_bytes_sec) {
1905+ virBufferAsprintf(&opt, ",bps_rd=%llu",
1906+ disk->blkdeviotune.read_bytes_sec);
1907+ }
1908+
1909+ if (disk->blkdeviotune.write_bytes_sec) {
1910+ virBufferAsprintf(&opt, ",bps_wr=%llu",
1911+ disk->blkdeviotune.write_bytes_sec);
1912+ }
1913+
1914+ if (disk->blkdeviotune.total_iops_sec) {
1915+ virBufferAsprintf(&opt, ",iops=%llu",
1916+ disk->blkdeviotune.total_iops_sec);
1917+ }
1918+
1919+ if (disk->blkdeviotune.read_iops_sec) {
1920+ virBufferAsprintf(&opt, ",iops_rd=%llu",
1921+ disk->blkdeviotune.read_iops_sec);
1922+ }
1923+
1924+ if (disk->blkdeviotune.write_iops_sec) {
1925+ virBufferAsprintf(&opt, ",iops_wr=%llu",
1926+ disk->blkdeviotune.write_iops_sec);
1927+ }
1928+
1929+ if (virBufferError(&opt)) {
1930+ virReportOOMError();
1931+ goto error;
1932+ }
1933+
1934+ return virBufferContentAndReset(&opt);
1935+
1936+error:
1937+ virBufferFreeAndReset(&opt);
1938+ return NULL;
1939+}
1940+
1941+
1942+char *
1943+qemuBuildDriveDevStr(virDomainDiskDefPtr disk,
1944+ int bootindex,
1945+ virBitmapPtr qemuCaps)
1946+{
1947+ virBuffer opt = VIR_BUFFER_INITIALIZER;
1948+ const char *bus = virDomainDiskQEMUBusTypeToString(disk->bus);
1949+ int idx = virDiskNameToIndex(disk->dst);
1950+
1951+ if (idx < 0) {
1952+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1953+ _("unsupported disk type '%s'"), disk->dst);
1954+ goto error;
1955+ }
1956+
1957+ switch (disk->bus) {
1958+ case VIR_DOMAIN_DISK_BUS_IDE:
1959+ virBufferAddLit(&opt, "ide-drive");
1960+ virBufferAsprintf(&opt, ",bus=ide.%d,unit=%d",
1961+ disk->info.addr.drive.bus,
1962+ disk->info.addr.drive.unit);
1963+ break;
1964+ case VIR_DOMAIN_DISK_BUS_SCSI:
1965+ virBufferAddLit(&opt, "scsi-disk");
1966+ virBufferAsprintf(&opt, ",bus=scsi%d.%d,scsi-id=%d",
1967+ disk->info.addr.drive.controller,
1968+ disk->info.addr.drive.bus,
1969+ disk->info.addr.drive.unit);
1970+ break;
1971+ case VIR_DOMAIN_DISK_BUS_SATA:
1972+ virBufferAddLit(&opt, "ide-drive");
1973+ virBufferAsprintf(&opt, ",bus=ahci%d.%d",
1974+ disk->info.addr.drive.controller,
1975+ disk->info.addr.drive.unit);
1976+ break;
1977+ case VIR_DOMAIN_DISK_BUS_VIRTIO:
1978+ virBufferAddLit(&opt, "virtio-blk-pci");
1979+ qemuBuildIoEventFdStr(&opt, disk->ioeventfd, qemuCaps);
1980+ if (disk->event_idx &&
1981+ qemuCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BLK_EVENT_IDX)) {
1982+ virBufferAsprintf(&opt, ",event_idx=%s",
1983+ virDomainVirtioEventIdxTypeToString(disk->event_idx));
1984+ }
1985+ if (qemuBuildDeviceAddressStr(&opt, &disk->info, qemuCaps) < 0)
1986+ goto error;
1987+ break;
1988+ case VIR_DOMAIN_DISK_BUS_USB:
1989+ virBufferAddLit(&opt, "usb-storage");
1990+ break;
1991+ default:
1992+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
1993+ _("unsupported disk bus '%s' with device setup"), bus);
1994+ goto error;
1995+ }
1996+ virBufferAsprintf(&opt, ",drive=%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias);
1997+ virBufferAsprintf(&opt, ",id=%s", disk->info.alias);
1998+ if (bootindex && qemuCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX))
1999+ virBufferAsprintf(&opt, ",bootindex=%d", bootindex);
2000+
2001+ if (virBufferError(&opt)) {
2002+ virReportOOMError();
2003+ goto error;
2004+ }
2005+
2006+ return virBufferContentAndReset(&opt);
2007+
2008+error:
2009+ virBufferFreeAndReset(&opt);
2010+ return NULL;
2011+}
2012+
2013+
2014+char *qemuBuildFSStr(virDomainFSDefPtr fs,
2015+ virBitmapPtr qemuCaps ATTRIBUTE_UNUSED)
2016+{
2017+ virBuffer opt = VIR_BUFFER_INITIALIZER;
2018+ const char *driver = qemuDomainFSDriverTypeToString(fs->fsdriver);
2019+
2020+ if (fs->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
2021+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2022+ _("only supports mount filesystem type"));
2023+ goto error;
2024+ }
2025+
2026+ if (!driver) {
2027+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2028+ _("Filesystem driver type not supported"));
2029+ goto error;
2030+ }
2031+ virBufferAdd(&opt, driver, -1);
2032+
2033+ if (fs->accessmode == VIR_DOMAIN_FS_ACCESSMODE_MAPPED) {
2034+ virBufferAddLit(&opt, ",security_model=mapped");
2035+ } else if(fs->accessmode == VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH) {
2036+ virBufferAddLit(&opt, ",security_model=passthrough");
2037+ } else if(fs->accessmode == VIR_DOMAIN_FS_ACCESSMODE_SQUASH) {
2038+ virBufferAddLit(&opt, ",security_model=none");
2039+ }
2040+ virBufferAsprintf(&opt, ",id=%s%s", QEMU_FSDEV_HOST_PREFIX, fs->info.alias);
2041+ virBufferAsprintf(&opt, ",path=%s", fs->src);
2042+
2043+ if (virBufferError(&opt)) {
2044+ virReportOOMError();
2045+ goto error;
2046+ }
2047+
2048+ return virBufferContentAndReset(&opt);
2049+
2050+error:
2051+ virBufferFreeAndReset(&opt);
2052+ return NULL;
2053+}
2054+
2055+
2056+char *
2057+qemuBuildFSDevStr(virDomainFSDefPtr fs,
2058+ virBitmapPtr qemuCaps)
2059+{
2060+ virBuffer opt = VIR_BUFFER_INITIALIZER;
2061+
2062+ if (fs->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
2063+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2064+ _("can only passthrough directories"));
2065+ goto error;
2066+ }
2067+
2068+ virBufferAddLit(&opt, "virtio-9p-pci");
2069+ virBufferAsprintf(&opt, ",id=%s", fs->info.alias);
2070+ virBufferAsprintf(&opt, ",fsdev=%s%s", QEMU_FSDEV_HOST_PREFIX, fs->info.alias);
2071+ virBufferAsprintf(&opt, ",mount_tag=%s", fs->dst);
2072+
2073+ if (qemuBuildDeviceAddressStr(&opt, &fs->info, qemuCaps) < 0)
2074+ goto error;
2075+
2076+ if (virBufferError(&opt)) {
2077+ virReportOOMError();
2078+ goto error;
2079+ }
2080+
2081+ return virBufferContentAndReset(&opt);
2082+
2083+error:
2084+ virBufferFreeAndReset(&opt);
2085+ return NULL;
2086+}
2087+
2088+
2089+static int
2090+qemuControllerModelUSBToCaps(int model)
2091+{
2092+ switch (model) {
2093+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI:
2094+ return QEMU_CAPS_PIIX3_USB_UHCI;
2095+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX4_UHCI:
2096+ return QEMU_CAPS_PIIX4_USB_UHCI;
2097+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_EHCI:
2098+ return QEMU_CAPS_USB_EHCI;
2099+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_EHCI1:
2100+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI1:
2101+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI2:
2102+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_ICH9_UHCI3:
2103+ return QEMU_CAPS_ICH9_USB_EHCI1;
2104+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_VT82C686B_UHCI:
2105+ return QEMU_CAPS_VT82C686B_USB_UHCI;
2106+ case VIR_DOMAIN_CONTROLLER_MODEL_USB_PCI_OHCI:
2107+ return QEMU_CAPS_PCI_OHCI;
2108+ default:
2109+ return -1;
2110+ }
2111+}
2112+
2113+
2114+static int
2115+qemuBuildUSBControllerDevStr(virDomainControllerDefPtr def,
2116+ virBitmapPtr qemuCaps,
2117+ virBuffer *buf)
2118+{
2119+ const char *smodel;
2120+ int model, caps;
2121+
2122+ model = def->model;
2123+ if (model == -1)
2124+ model = VIR_DOMAIN_CONTROLLER_MODEL_USB_PIIX3_UHCI;
2125+
2126+ smodel = qemuControllerModelUSBTypeToString(model);
2127+ caps = qemuControllerModelUSBToCaps(model);
2128+
2129+ if (caps == -1 || !qemuCapsGet(qemuCaps, caps)) {
2130+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2131+ _("%s not supported in this QEMU binary"), smodel);
2132+ return -1;
2133+ }
2134+
2135+ virBufferAsprintf(buf, "%s", smodel);
2136+
2137+ if (def->info.mastertype == VIR_DOMAIN_CONTROLLER_MASTER_USB) {
2138+ virBufferAsprintf(buf, ",masterbus=");
2139+ qemuUsbId(buf, def->idx);
2140+ virBufferAsprintf(buf, ".0,firstport=%d", def->info.master.usb.startport);
2141+ } else {
2142+ virBufferAsprintf(buf, ",id=");
2143+ qemuUsbId(buf, def->idx);
2144+ }
2145+
2146+ return 0;
2147+}
2148+
2149+char *
2150+qemuBuildControllerDevStr(virDomainControllerDefPtr def,
2151+ virBitmapPtr qemuCaps,
2152+ int *nusbcontroller)
2153+{
2154+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2155+
2156+ switch (def->type) {
2157+ case VIR_DOMAIN_CONTROLLER_TYPE_SCSI:
2158+ virBufferAddLit(&buf, "lsi");
2159+ virBufferAsprintf(&buf, ",id=scsi%d", def->idx);
2160+ break;
2161+
2162+ case VIR_DOMAIN_CONTROLLER_TYPE_VIRTIO_SERIAL:
2163+ if (def->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
2164+ virBufferAddLit(&buf, "virtio-serial-pci");
2165+ } else {
2166+ virBufferAddLit(&buf, "virtio-serial");
2167+ }
2168+ virBufferAsprintf(&buf, ",id=" QEMU_VIRTIO_SERIAL_PREFIX "%d",
2169+ def->idx);
2170+ if (def->opts.vioserial.ports != -1) {
2171+ virBufferAsprintf(&buf, ",max_ports=%d",
2172+ def->opts.vioserial.ports);
2173+ }
2174+ if (def->opts.vioserial.vectors != -1) {
2175+ virBufferAsprintf(&buf, ",vectors=%d",
2176+ def->opts.vioserial.vectors);
2177+ }
2178+ break;
2179+
2180+ case VIR_DOMAIN_CONTROLLER_TYPE_CCID:
2181+ virBufferAsprintf(&buf, "usb-ccid,id=ccid%d", def->idx);
2182+ break;
2183+
2184+ case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
2185+ virBufferAsprintf(&buf, "ahci,id=ahci%d", def->idx);
2186+ break;
2187+
2188+ case VIR_DOMAIN_CONTROLLER_TYPE_USB:
2189+ if (qemuBuildUSBControllerDevStr(def, qemuCaps, &buf) == -1)
2190+ goto error;
2191+
2192+ if (nusbcontroller)
2193+ *nusbcontroller += 1;
2194+
2195+ break;
2196+
2197+ /* We always get an IDE controller, whether we want it or not. */
2198+ case VIR_DOMAIN_CONTROLLER_TYPE_IDE:
2199+ default:
2200+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2201+ _("Unknown controller type: %s"),
2202+ virDomainControllerTypeToString(def->type));
2203+ goto error;
2204+ }
2205+
2206+ if (qemuBuildDeviceAddressStr(&buf, &def->info, qemuCaps) < 0)
2207+ goto error;
2208+
2209+ if (virBufferError(&buf)) {
2210+ virReportOOMError();
2211+ goto error;
2212+ }
2213+
2214+ return virBufferContentAndReset(&buf);
2215+
2216+error:
2217+ virBufferFreeAndReset(&buf);
2218+ return NULL;
2219+}
2220+
2221+
2222+char *
2223+qemuBuildNicStr(virDomainNetDefPtr net,
2224+ const char *prefix,
2225+ int vlan)
2226+{
2227+ char *str;
2228+ if (virAsprintf(&str,
2229+ "%smacaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s%s%s",
2230+ prefix ? prefix : "",
2231+ net->mac[0], net->mac[1],
2232+ net->mac[2], net->mac[3],
2233+ net->mac[4], net->mac[5],
2234+ vlan,
2235+ (net->model ? ",model=" : ""),
2236+ (net->model ? net->model : ""),
2237+ (net->info.alias ? ",name=" : ""),
2238+ (net->info.alias ? net->info.alias : "")) < 0) {
2239+ virReportOOMError();
2240+ return NULL;
2241+ }
2242+
2243+ return str;
2244+}
2245+
2246+
2247+char *
2248+qemuBuildNicDevStr(virDomainNetDefPtr net,
2249+ int vlan,
2250+ int bootindex,
2251+ virBitmapPtr qemuCaps)
2252+{
2253+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2254+ const char *nic;
2255+ bool usingVirtio = false;
2256+
2257+ if (!net->model) {
2258+ nic = "rtl8139";
2259+ } else if (STREQ(net->model, "virtio")) {
2260+ nic = "virtio-net-pci";
2261+ usingVirtio = true;
2262+ } else {
2263+ nic = net->model;
2264+ }
2265+
2266+ virBufferAdd(&buf, nic, strlen(nic));
2267+ if (usingVirtio && net->driver.virtio.txmode) {
2268+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_TX_ALG)) {
2269+ virBufferAddLit(&buf, ",tx=");
2270+ switch (net->driver.virtio.txmode) {
2271+ case VIR_DOMAIN_NET_VIRTIO_TX_MODE_IOTHREAD:
2272+ virBufferAddLit(&buf, "bh");
2273+ break;
2274+
2275+ case VIR_DOMAIN_NET_VIRTIO_TX_MODE_TIMER:
2276+ virBufferAddLit(&buf, "timer");
2277+ break;
2278+ default:
2279+ /* this should never happen, if it does, we need
2280+ * to add another case to this switch.
2281+ */
2282+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2283+ _("unrecognized virtio-net-pci 'tx' option"));
2284+ goto error;
2285+ }
2286+ } else {
2287+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2288+ _("virtio-net-pci 'tx' option not supported in this QEMU binary"));
2289+ goto error;
2290+ }
2291+ }
2292+ if (usingVirtio) {
2293+ qemuBuildIoEventFdStr(&buf, net->driver.virtio.ioeventfd, qemuCaps);
2294+ if (net->driver.virtio.event_idx &&
2295+ qemuCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_NET_EVENT_IDX)) {
2296+ virBufferAsprintf(&buf, ",event_idx=%s",
2297+ virDomainVirtioEventIdxTypeToString(net->driver.virtio.event_idx));
2298+ }
2299+ }
2300+ if (vlan == -1)
2301+ virBufferAsprintf(&buf, ",netdev=host%s", net->info.alias);
2302+ else
2303+ virBufferAsprintf(&buf, ",vlan=%d", vlan);
2304+ virBufferAsprintf(&buf, ",id=%s", net->info.alias);
2305+ virBufferAsprintf(&buf, ",mac=%02x:%02x:%02x:%02x:%02x:%02x",
2306+ net->mac[0], net->mac[1],
2307+ net->mac[2], net->mac[3],
2308+ net->mac[4], net->mac[5]);
2309+ if (qemuBuildDeviceAddressStr(&buf, &net->info, qemuCaps) < 0)
2310+ goto error;
2311+ if (bootindex && qemuCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX))
2312+ virBufferAsprintf(&buf, ",bootindex=%d", bootindex);
2313+
2314+ if (virBufferError(&buf)) {
2315+ virReportOOMError();
2316+ goto error;
2317+ }
2318+
2319+ return virBufferContentAndReset(&buf);
2320+
2321+error:
2322+ virBufferFreeAndReset(&buf);
2323+ return NULL;
2324+}
2325+
2326+
2327+char *
2328+qemuBuildHostNetStr(virDomainNetDefPtr net,
2329+ char type_sep,
2330+ int vlan,
2331+ const char *tapfd,
2332+ const char *vhostfd)
2333+{
2334+ bool is_tap = false;
2335+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2336+
2337+ switch (virDomainNetGetActualType(net)) {
2338+ case VIR_DOMAIN_NET_TYPE_NETWORK:
2339+ case VIR_DOMAIN_NET_TYPE_BRIDGE:
2340+ case VIR_DOMAIN_NET_TYPE_DIRECT:
2341+ virBufferAddLit(&buf, "tap");
2342+ virBufferAsprintf(&buf, "%cfd=%s", type_sep, tapfd);
2343+ type_sep = ',';
2344+ is_tap = true;
2345+ break;
2346+
2347+ case VIR_DOMAIN_NET_TYPE_ETHERNET:
2348+ virBufferAddLit(&buf, "tap");
2349+ if (net->ifname) {
2350+ virBufferAsprintf(&buf, "%cifname=%s", type_sep, net->ifname);
2351+ type_sep = ',';
2352+ }
2353+ if (net->data.ethernet.script) {
2354+ virBufferAsprintf(&buf, "%cscript=%s", type_sep,
2355+ net->data.ethernet.script);
2356+ type_sep = ',';
2357+ }
2358+ is_tap = true;
2359+ break;
2360+
2361+ case VIR_DOMAIN_NET_TYPE_CLIENT:
2362+ case VIR_DOMAIN_NET_TYPE_SERVER:
2363+ case VIR_DOMAIN_NET_TYPE_MCAST:
2364+ virBufferAddLit(&buf, "socket");
2365+ switch (virDomainNetGetActualType(net)) {
2366+ case VIR_DOMAIN_NET_TYPE_CLIENT:
2367+ virBufferAsprintf(&buf, "%cconnect=%s:%d",
2368+ type_sep,
2369+ net->data.socket.address,
2370+ net->data.socket.port);
2371+ break;
2372+ case VIR_DOMAIN_NET_TYPE_SERVER:
2373+ virBufferAsprintf(&buf, "%clisten=%s:%d",
2374+ type_sep,
2375+ net->data.socket.address,
2376+ net->data.socket.port);
2377+ break;
2378+ case VIR_DOMAIN_NET_TYPE_MCAST:
2379+ virBufferAsprintf(&buf, "%cmcast=%s:%d",
2380+ type_sep,
2381+ net->data.socket.address,
2382+ net->data.socket.port);
2383+ break;
2384+ case VIR_DOMAIN_NET_TYPE_USER:
2385+ case VIR_DOMAIN_NET_TYPE_ETHERNET:
2386+ case VIR_DOMAIN_NET_TYPE_NETWORK:
2387+ case VIR_DOMAIN_NET_TYPE_BRIDGE:
2388+ case VIR_DOMAIN_NET_TYPE_INTERNAL:
2389+ case VIR_DOMAIN_NET_TYPE_DIRECT:
2390+ case VIR_DOMAIN_NET_TYPE_LAST:
2391+ break;
2392+ }
2393+ type_sep = ',';
2394+ break;
2395+
2396+ case VIR_DOMAIN_NET_TYPE_USER:
2397+ default:
2398+ virBufferAddLit(&buf, "user");
2399+ break;
2400+ }
2401+
2402+ if (vlan >= 0) {
2403+ virBufferAsprintf(&buf, "%cvlan=%d", type_sep, vlan);
2404+ if (net->info.alias)
2405+ virBufferAsprintf(&buf, ",name=host%s",
2406+ net->info.alias);
2407+ } else {
2408+ virBufferAsprintf(&buf, "%cid=host%s",
2409+ type_sep, net->info.alias);
2410+ }
2411+
2412+ if (is_tap) {
2413+ if (vhostfd && *vhostfd)
2414+ virBufferAsprintf(&buf, ",vhost=on,vhostfd=%s", vhostfd);
2415+ if (net->tune.sndbuf_specified)
2416+ virBufferAsprintf(&buf, ",sndbuf=%lu", net->tune.sndbuf);
2417+ }
2418+
2419+ if (virBufferError(&buf)) {
2420+ virBufferFreeAndReset(&buf);
2421+ virReportOOMError();
2422+ return NULL;
2423+ }
2424+
2425+ return virBufferContentAndReset(&buf);
2426+}
2427+
2428+
2429+char *
2430+qemuBuildWatchdogDevStr(virDomainWatchdogDefPtr dev,
2431+ virBitmapPtr qemuCaps)
2432+{
2433+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2434+
2435+ const char *model = virDomainWatchdogModelTypeToString(dev->model);
2436+ if (!model) {
2437+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
2438+ "%s", _("missing watchdog model"));
2439+ goto error;
2440+ }
2441+
2442+ virBufferAsprintf(&buf, "%s,id=%s", model, dev->info.alias);
2443+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2444+ goto error;
2445+
2446+ if (virBufferError(&buf)) {
2447+ virReportOOMError();
2448+ goto error;
2449+ }
2450+
2451+ return virBufferContentAndReset(&buf);
2452+
2453+error:
2454+ virBufferFreeAndReset(&buf);
2455+ return NULL;
2456+}
2457+
2458+
2459+char *
2460+qemuBuildMemballoonDevStr(virDomainMemballoonDefPtr dev,
2461+ virBitmapPtr qemuCaps)
2462+{
2463+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2464+
2465+ virBufferAddLit(&buf, "virtio-balloon-pci");
2466+ virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
2467+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2468+ goto error;
2469+
2470+ if (virBufferError(&buf)) {
2471+ virReportOOMError();
2472+ goto error;
2473+ }
2474+
2475+ return virBufferContentAndReset(&buf);
2476+
2477+error:
2478+ virBufferFreeAndReset(&buf);
2479+ return NULL;
2480+}
2481+
2482+
2483+char *
2484+qemuBuildUSBInputDevStr(virDomainInputDefPtr dev,
2485+ virBitmapPtr qemuCaps)
2486+{
2487+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2488+
2489+ virBufferAsprintf(&buf, "%s,id=%s",
2490+ dev->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ?
2491+ "usb-mouse" : "usb-tablet", dev->info.alias);
2492+
2493+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2494+ goto error;
2495+
2496+ if (virBufferError(&buf)) {
2497+ virReportOOMError();
2498+ goto error;
2499+ }
2500+
2501+ return virBufferContentAndReset(&buf);
2502+
2503+error:
2504+ virBufferFreeAndReset(&buf);
2505+ return NULL;
2506+}
2507+
2508+
2509+char *
2510+qemuBuildSoundDevStr(virDomainSoundDefPtr sound,
2511+ virBitmapPtr qemuCaps)
2512+{
2513+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2514+ const char *model = virDomainSoundModelTypeToString(sound->model);
2515+
2516+ if (!model) {
2517+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
2518+ "%s", _("invalid sound model"));
2519+ goto error;
2520+ }
2521+
2522+ /* Hack for weirdly unusual devices name in QEMU */
2523+ if (STREQ(model, "es1370"))
2524+ model = "ES1370";
2525+ else if (STREQ(model, "ac97"))
2526+ model = "AC97";
2527+ else if (STREQ(model, "ich6"))
2528+ model = "intel-hda";
2529+
2530+ virBufferAsprintf(&buf, "%s,id=%s", model, sound->info.alias);
2531+ if (qemuBuildDeviceAddressStr(&buf, &sound->info, qemuCaps) < 0)
2532+ goto error;
2533+
2534+ if (virBufferError(&buf)) {
2535+ virReportOOMError();
2536+ goto error;
2537+ }
2538+
2539+ return virBufferContentAndReset(&buf);
2540+
2541+error:
2542+ virBufferFreeAndReset(&buf);
2543+ return NULL;
2544+}
2545+
2546+static char *
2547+qemuBuildSoundCodecStr(virDomainSoundDefPtr sound,
2548+ const char *codec)
2549+{
2550+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2551+ int cad = 0;
2552+
2553+ virBufferAsprintf(&buf, "%s,id=%s-codec%d,bus=%s.0,cad=%d",
2554+ codec, sound->info.alias, cad, sound->info.alias, cad);
2555+
2556+ if (virBufferError(&buf)) {
2557+ virReportOOMError();
2558+ goto error;
2559+ }
2560+
2561+ return virBufferContentAndReset(&buf);
2562+
2563+error:
2564+ virBufferFreeAndReset(&buf);
2565+ return NULL;
2566+}
2567+
2568+static char *
2569+qemuBuildVideoDevStr(virDomainVideoDefPtr video,
2570+ virBitmapPtr qemuCaps)
2571+{
2572+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2573+ const char *model = qemuVideoTypeToString(video->type);
2574+
2575+ if (!model) {
2576+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
2577+ "%s", _("invalid video model"));
2578+ goto error;
2579+ }
2580+
2581+ virBufferAsprintf(&buf, "%s,id=%s", model, video->info.alias);
2582+
2583+ if (video->type == VIR_DOMAIN_VIDEO_TYPE_QXL) {
2584+ if (video->vram > (UINT_MAX / 1024)) {
2585+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2586+ _("value for 'vram' must be less than '%u'"),
2587+ UINT_MAX / 1024);
2588+ goto error;
2589+ }
2590+
2591+ /* QEMU accepts bytes for vram_size. */
2592+ virBufferAsprintf(&buf, ",vram_size=%u", video->vram * 1024);
2593+ }
2594+
2595+ if (qemuBuildDeviceAddressStr(&buf, &video->info, qemuCaps) < 0)
2596+ goto error;
2597+
2598+ if (virBufferError(&buf)) {
2599+ virReportOOMError();
2600+ goto error;
2601+ }
2602+
2603+ return virBufferContentAndReset(&buf);
2604+
2605+error:
2606+ virBufferFreeAndReset(&buf);
2607+ return NULL;
2608+}
2609+
2610+
2611+int
2612+qemuOpenPCIConfig(virDomainHostdevDefPtr dev)
2613+{
2614+ char *path = NULL;
2615+ int configfd = -1;
2616+
2617+ if (virAsprintf(&path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/config",
2618+ dev->source.subsys.u.pci.domain,
2619+ dev->source.subsys.u.pci.bus,
2620+ dev->source.subsys.u.pci.slot,
2621+ dev->source.subsys.u.pci.function) < 0) {
2622+ virReportOOMError();
2623+ return -1;
2624+ }
2625+
2626+ configfd = open(path, O_RDWR, 0);
2627+
2628+ if (configfd < 0)
2629+ virReportSystemError(errno, _("Failed opening %s"), path);
2630+
2631+ VIR_FREE(path);
2632+
2633+ return configfd;
2634+}
2635+
2636+char *
2637+qemuBuildPCIHostdevDevStr(virDomainHostdevDefPtr dev, const char *configfd,
2638+ virBitmapPtr qemuCaps)
2639+{
2640+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2641+
2642+ virBufferAddLit(&buf, "pci-assign");
2643+ virBufferAsprintf(&buf, ",host=%.2x:%.2x.%.1x",
2644+ dev->source.subsys.u.pci.bus,
2645+ dev->source.subsys.u.pci.slot,
2646+ dev->source.subsys.u.pci.function);
2647+ virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
2648+ if (configfd && *configfd)
2649+ virBufferAsprintf(&buf, ",configfd=%s", configfd);
2650+ if (dev->bootIndex)
2651+ virBufferAsprintf(&buf, ",bootindex=%d", dev->bootIndex);
2652+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2653+ goto error;
2654+
2655+ if (dev->rombar) {
2656+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_PCI_ROMBAR)) {
2657+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2658+ "%s", _("rombar not supported in this QEMU binary"));
2659+ goto error;
2660+ }
2661+
2662+ switch (dev->rombar) {
2663+ case VIR_DOMAIN_PCI_ROMBAR_OFF:
2664+ virBufferAddLit(&buf, ",rombar=0");
2665+ break;
2666+ case VIR_DOMAIN_PCI_ROMBAR_ON:
2667+ virBufferAddLit(&buf, ",rombar=1");
2668+ break;
2669+ default:
2670+ break;
2671+ }
2672+ }
2673+
2674+ if (virBufferError(&buf)) {
2675+ virReportOOMError();
2676+ goto error;
2677+ }
2678+
2679+ return virBufferContentAndReset(&buf);
2680+
2681+error:
2682+ virBufferFreeAndReset(&buf);
2683+ return NULL;
2684+}
2685+
2686+
2687+char *
2688+qemuBuildPCIHostdevPCIDevStr(virDomainHostdevDefPtr dev)
2689+{
2690+ char *ret = NULL;
2691+
2692+ if (virAsprintf(&ret, "host=%.2x:%.2x.%.1x",
2693+ dev->source.subsys.u.pci.bus,
2694+ dev->source.subsys.u.pci.slot,
2695+ dev->source.subsys.u.pci.function) < 0)
2696+ virReportOOMError();
2697+
2698+ return ret;
2699+}
2700+
2701+
2702+char *
2703+qemuBuildRedirdevDevStr(virDomainRedirdevDefPtr dev,
2704+ virBitmapPtr qemuCaps)
2705+{
2706+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2707+
2708+ if (dev->bus != VIR_DOMAIN_REDIRDEV_BUS_USB) {
2709+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2710+ _("Redirection bus %s is not supported by QEMU"),
2711+ virDomainRedirdevBusTypeToString(dev->bus));
2712+ goto error;
2713+ }
2714+
2715+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_USB_REDIR)) {
2716+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2717+ _("USB redirection is not supported "
2718+ "by this version of QEMU"));
2719+ goto error;
2720+ }
2721+
2722+ virBufferAsprintf(&buf, "usb-redir,chardev=char%s,id=%s",
2723+ dev->info.alias,
2724+ dev->info.alias);
2725+
2726+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2727+ goto error;
2728+
2729+ if (virBufferError(&buf)) {
2730+ virReportOOMError();
2731+ goto error;
2732+ }
2733+
2734+ return virBufferContentAndReset(&buf);
2735+
2736+error:
2737+ virBufferFreeAndReset(&buf);
2738+ return NULL;
2739+}
2740+
2741+char *
2742+qemuBuildUSBHostdevDevStr(virDomainHostdevDefPtr dev,
2743+ virBitmapPtr qemuCaps)
2744+{
2745+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2746+
2747+ if (!dev->source.subsys.u.usb.bus &&
2748+ !dev->source.subsys.u.usb.device) {
2749+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2750+ _("USB host device is missing bus/device information"));
2751+ return NULL;
2752+ }
2753+
2754+ virBufferAsprintf(&buf, "usb-host,hostbus=%d,hostaddr=%d,id=%s",
2755+ dev->source.subsys.u.usb.bus,
2756+ dev->source.subsys.u.usb.device,
2757+ dev->info.alias);
2758+
2759+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2760+ goto error;
2761+
2762+ if (virBufferError(&buf)) {
2763+ virReportOOMError();
2764+ goto error;
2765+ }
2766+
2767+ return virBufferContentAndReset(&buf);
2768+
2769+error:
2770+ virBufferFreeAndReset(&buf);
2771+ return NULL;
2772+}
2773+
2774+
2775+char *
2776+qemuBuildHubDevStr(virDomainHubDefPtr dev,
2777+ virBitmapPtr qemuCaps)
2778+{
2779+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2780+
2781+ if (dev->type != VIR_DOMAIN_HUB_TYPE_USB) {
2782+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2783+ _("hub type %s not supported"),
2784+ virDomainHubTypeToString(dev->type));
2785+ goto error;
2786+ }
2787+
2788+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_USB_HUB)) {
2789+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2790+ _("usb-hub not supported by QEMU binary"));
2791+ goto error;
2792+ }
2793+
2794+ virBufferAddLit(&buf, "usb-hub");
2795+ virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
2796+ if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
2797+ goto error;
2798+
2799+ if (virBufferError(&buf)) {
2800+ virReportOOMError();
2801+ goto error;
2802+ }
2803+
2804+ return virBufferContentAndReset(&buf);
2805+
2806+error:
2807+ virBufferFreeAndReset(&buf);
2808+ return NULL;
2809+}
2810+
2811+
2812+char *
2813+qemuBuildUSBHostdevUsbDevStr(virDomainHostdevDefPtr dev)
2814+{
2815+ char *ret = NULL;
2816+
2817+ if (!dev->source.subsys.u.usb.bus &&
2818+ !dev->source.subsys.u.usb.device) {
2819+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2820+ _("USB host device is missing bus/device information"));
2821+ return NULL;
2822+ }
2823+
2824+ if (virAsprintf(&ret, "host:%d.%d",
2825+ dev->source.subsys.u.usb.bus,
2826+ dev->source.subsys.u.usb.device) < 0)
2827+ virReportOOMError();
2828+
2829+ return ret;
2830+}
2831+
2832+
2833+
2834+/* This function outputs a -chardev command line option which describes only the
2835+ * host side of the character device */
2836+static char *
2837+qemuBuildChrChardevStr(virDomainChrSourceDefPtr dev, const char *alias,
2838+ virBitmapPtr qemuCaps)
2839+{
2840+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2841+ bool telnet;
2842+
2843+ switch(dev->type) {
2844+ case VIR_DOMAIN_CHR_TYPE_NULL:
2845+ virBufferAsprintf(&buf, "null,id=char%s", alias);
2846+ break;
2847+
2848+ case VIR_DOMAIN_CHR_TYPE_VC:
2849+ virBufferAsprintf(&buf, "vc,id=char%s", alias);
2850+ break;
2851+
2852+ case VIR_DOMAIN_CHR_TYPE_PTY:
2853+ virBufferAsprintf(&buf, "pty,id=char%s", alias);
2854+ break;
2855+
2856+ case VIR_DOMAIN_CHR_TYPE_DEV:
2857+ virBufferAsprintf(&buf, "tty,id=char%s,path=%s", alias,
2858+ dev->data.file.path);
2859+ break;
2860+
2861+ case VIR_DOMAIN_CHR_TYPE_FILE:
2862+ virBufferAsprintf(&buf, "file,id=char%s,path=%s", alias,
2863+ dev->data.file.path);
2864+ break;
2865+
2866+ case VIR_DOMAIN_CHR_TYPE_PIPE:
2867+ virBufferAsprintf(&buf, "pipe,id=char%s,path=%s", alias,
2868+ dev->data.file.path);
2869+ break;
2870+
2871+ case VIR_DOMAIN_CHR_TYPE_STDIO:
2872+ virBufferAsprintf(&buf, "stdio,id=char%s", alias);
2873+ break;
2874+
2875+ case VIR_DOMAIN_CHR_TYPE_UDP: {
2876+ const char *connectHost = dev->data.udp.connectHost;
2877+ const char *bindHost = dev->data.udp.bindHost;
2878+ const char *bindService = dev->data.udp.bindService;
2879+
2880+ if (connectHost == NULL)
2881+ connectHost = "";
2882+ if (bindHost == NULL)
2883+ bindHost = "";
2884+ if (bindService == NULL)
2885+ bindService = "0";
2886+
2887+ virBufferAsprintf(&buf,
2888+ "udp,id=char%s,host=%s,port=%s,localaddr=%s,"
2889+ "localport=%s",
2890+ alias,
2891+ connectHost,
2892+ dev->data.udp.connectService,
2893+ bindHost, bindService);
2894+ break;
2895+ }
2896+ case VIR_DOMAIN_CHR_TYPE_TCP:
2897+ telnet = dev->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
2898+ virBufferAsprintf(&buf,
2899+ "socket,id=char%s,host=%s,port=%s%s%s",
2900+ alias,
2901+ dev->data.tcp.host,
2902+ dev->data.tcp.service,
2903+ telnet ? ",telnet" : "",
2904+ dev->data.tcp.listen ? ",server,nowait" : "");
2905+ break;
2906+
2907+ case VIR_DOMAIN_CHR_TYPE_UNIX:
2908+ virBufferAsprintf(&buf,
2909+ "socket,id=char%s,path=%s%s",
2910+ alias,
2911+ dev->data.nix.path,
2912+ dev->data.nix.listen ? ",server,nowait" : "");
2913+ break;
2914+
2915+ case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
2916+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV_SPICEVMC)) {
2917+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2918+ _("spicevmc not supported in this QEMU binary"));
2919+ goto error;
2920+ }
2921+ virBufferAsprintf(&buf, "spicevmc,id=char%s,name=%s", alias,
2922+ virDomainChrSpicevmcTypeToString(dev->data.spicevmc));
2923+ break;
2924+
2925+ default:
2926+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
2927+ _("unsupported chardev '%s'"),
2928+ virDomainChrTypeToString(dev->type));
2929+ goto error;
2930+ }
2931+
2932+ if (virBufferError(&buf)) {
2933+ virReportOOMError();
2934+ goto error;
2935+ }
2936+
2937+ return virBufferContentAndReset(&buf);
2938+
2939+error:
2940+ virBufferFreeAndReset(&buf);
2941+ return NULL;
2942+}
2943+
2944+
2945+static char *
2946+qemuBuildChrArgStr(virDomainChrSourceDefPtr dev, const char *prefix)
2947+{
2948+ virBuffer buf = VIR_BUFFER_INITIALIZER;
2949+
2950+ if (prefix)
2951+ virBufferAdd(&buf, prefix, strlen(prefix));
2952+
2953+ switch (dev->type) {
2954+ case VIR_DOMAIN_CHR_TYPE_NULL:
2955+ virBufferAddLit(&buf, "null");
2956+ break;
2957+
2958+ case VIR_DOMAIN_CHR_TYPE_VC:
2959+ virBufferAddLit(&buf, "vc");
2960+ break;
2961+
2962+ case VIR_DOMAIN_CHR_TYPE_PTY:
2963+ virBufferAddLit(&buf, "pty");
2964+ break;
2965+
2966+ case VIR_DOMAIN_CHR_TYPE_DEV:
2967+ virBufferStrcat(&buf, dev->data.file.path, NULL);
2968+ break;
2969+
2970+ case VIR_DOMAIN_CHR_TYPE_FILE:
2971+ virBufferAsprintf(&buf, "file:%s", dev->data.file.path);
2972+ break;
2973+
2974+ case VIR_DOMAIN_CHR_TYPE_PIPE:
2975+ virBufferAsprintf(&buf, "pipe:%s", dev->data.file.path);
2976+ break;
2977+
2978+ case VIR_DOMAIN_CHR_TYPE_STDIO:
2979+ virBufferAddLit(&buf, "stdio");
2980+ break;
2981+
2982+ case VIR_DOMAIN_CHR_TYPE_UDP: {
2983+ const char *connectHost = dev->data.udp.connectHost;
2984+ const char *bindHost = dev->data.udp.bindHost;
2985+ const char *bindService = dev->data.udp.bindService;
2986+
2987+ if (connectHost == NULL)
2988+ connectHost = "";
2989+ if (bindHost == NULL)
2990+ bindHost = "";
2991+ if (bindService == NULL)
2992+ bindService = "0";
2993+
2994+ virBufferAsprintf(&buf, "udp:%s:%s@%s:%s",
2995+ connectHost,
2996+ dev->data.udp.connectService,
2997+ bindHost,
2998+ bindService);
2999+ break;
3000+ }
3001+ case VIR_DOMAIN_CHR_TYPE_TCP:
3002+ if (dev->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET) {
3003+ virBufferAsprintf(&buf, "telnet:%s:%s%s",
3004+ dev->data.tcp.host,
3005+ dev->data.tcp.service,
3006+ dev->data.tcp.listen ? ",server,nowait" : "");
3007+ } else {
3008+ virBufferAsprintf(&buf, "tcp:%s:%s%s",
3009+ dev->data.tcp.host,
3010+ dev->data.tcp.service,
3011+ dev->data.tcp.listen ? ",server,nowait" : "");
3012+ }
3013+ break;
3014+
3015+ case VIR_DOMAIN_CHR_TYPE_UNIX:
3016+ virBufferAsprintf(&buf, "unix:%s%s",
3017+ dev->data.nix.path,
3018+ dev->data.nix.listen ? ",server,nowait" : "");
3019+ break;
3020+ }
3021+
3022+ if (virBufferError(&buf)) {
3023+ virReportOOMError();
3024+ goto error;
3025+ }
3026+
3027+ return virBufferContentAndReset(&buf);
3028+
3029+error:
3030+ virBufferFreeAndReset(&buf);
3031+ return NULL;
3032+}
3033+
3034+
3035+static char *
3036+qemuBuildVirtioSerialPortDevStr(virDomainChrDefPtr dev,
3037+ virBitmapPtr qemuCaps)
3038+{
3039+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3040+ switch (dev->deviceType) {
3041+ case VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE:
3042+ virBufferAddLit(&buf, "virtconsole");
3043+ break;
3044+ case VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL:
3045+ /* Legacy syntax '-device spicevmc' */
3046+ if (dev->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC &&
3047+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SPICEVMC)) {
3048+ virBufferAddLit(&buf, "spicevmc");
3049+ } else {
3050+ virBufferAddLit(&buf, "virtserialport");
3051+ }
3052+ break;
3053+ default:
3054+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3055+ _("Cannot use virtio serial for parallel/serial devices"));
3056+ return NULL;
3057+ }
3058+
3059+ if (dev->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) {
3060+ /* Check it's a virtio-serial address */
3061+ if (dev->info.type !=
3062+ VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_SERIAL)
3063+ {
3064+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
3065+ "%s", _("virtio serial device has invalid address type"));
3066+ goto error;
3067+ }
3068+
3069+ virBufferAsprintf(&buf,
3070+ ",bus=" QEMU_VIRTIO_SERIAL_PREFIX "%d.%d",
3071+ dev->info.addr.vioserial.controller,
3072+ dev->info.addr.vioserial.bus);
3073+ virBufferAsprintf(&buf,
3074+ ",nr=%d",
3075+ dev->info.addr.vioserial.port);
3076+ }
3077+
3078+ if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
3079+ dev->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC &&
3080+ dev->target.name &&
3081+ STRNEQ(dev->target.name, "com.redhat.spice.0")) {
3082+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3083+ _("Unsupported spicevmc target name '%s'"),
3084+ dev->target.name);
3085+ goto error;
3086+ }
3087+
3088+ if (!(dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
3089+ dev->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC &&
3090+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SPICEVMC))) {
3091+ virBufferAsprintf(&buf, ",chardev=char%s,id=%s",
3092+ dev->info.alias, dev->info.alias);
3093+ if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CHANNEL &&
3094+ dev->target.name) {
3095+ virBufferAsprintf(&buf, ",name=%s", dev->target.name);
3096+ }
3097+ } else {
3098+ virBufferAsprintf(&buf, ",id=%s", dev->info.alias);
3099+ }
3100+ if (virBufferError(&buf)) {
3101+ virReportOOMError();
3102+ goto error;
3103+ }
3104+
3105+ return virBufferContentAndReset(&buf);
3106+
3107+error:
3108+ virBufferFreeAndReset(&buf);
3109+ return NULL;
3110+}
3111+
3112+static char *qemuBuildSmbiosBiosStr(virSysinfoDefPtr def)
3113+{
3114+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3115+
3116+ if ((def->bios_vendor == NULL) && (def->bios_version == NULL) &&
3117+ (def->bios_date == NULL) && (def->bios_release == NULL))
3118+ return(NULL);
3119+
3120+ virBufferAddLit(&buf, "type=0");
3121+
3122+ /* 0:Vendor */
3123+ if (def->bios_vendor)
3124+ virBufferAsprintf(&buf, ",vendor=%s", def->bios_vendor);
3125+ /* 0:BIOS Version */
3126+ if (def->bios_version)
3127+ virBufferAsprintf(&buf, ",version=%s", def->bios_version);
3128+ /* 0:BIOS Release Date */
3129+ if (def->bios_date)
3130+ virBufferAsprintf(&buf, ",date=%s", def->bios_date);
3131+ /* 0:System BIOS Major Release and 0:System BIOS Minor Release */
3132+ if (def->bios_release)
3133+ virBufferAsprintf(&buf, ",release=%s", def->bios_release);
3134+
3135+ if (virBufferError(&buf)) {
3136+ virReportOOMError();
3137+ goto error;
3138+ }
3139+
3140+ return virBufferContentAndReset(&buf);
3141+
3142+error:
3143+ virBufferFreeAndReset(&buf);
3144+ return(NULL);
3145+}
3146+
3147+static char *qemuBuildSmbiosSystemStr(virSysinfoDefPtr def, bool skip_uuid)
3148+{
3149+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3150+
3151+ if ((def->system_manufacturer == NULL) && (def->system_sku == NULL) &&
3152+ (def->system_product == NULL) && (def->system_version == NULL) &&
3153+ (def->system_serial == NULL) && (def->system_family == NULL) &&
3154+ (def->system_uuid == NULL || skip_uuid))
3155+ return NULL;
3156+
3157+ virBufferAddLit(&buf, "type=1");
3158+
3159+ /* 1:Manufacturer */
3160+ if (def->system_manufacturer)
3161+ virBufferAsprintf(&buf, ",manufacturer=%s",
3162+ def->system_manufacturer);
3163+ /* 1:Product Name */
3164+ if (def->system_product)
3165+ virBufferAsprintf(&buf, ",product=%s", def->system_product);
3166+ /* 1:Version */
3167+ if (def->system_version)
3168+ virBufferAsprintf(&buf, ",version=%s", def->system_version);
3169+ /* 1:Serial Number */
3170+ if (def->system_serial)
3171+ virBufferAsprintf(&buf, ",serial=%s", def->system_serial);
3172+ /* 1:UUID */
3173+ if (def->system_uuid && !skip_uuid)
3174+ virBufferAsprintf(&buf, ",uuid=%s", def->system_uuid);
3175+ /* 1:SKU Number */
3176+ if (def->system_sku)
3177+ virBufferAsprintf(&buf, ",sku=%s", def->system_sku);
3178+ /* 1:Family */
3179+ if (def->system_family)
3180+ virBufferAsprintf(&buf, ",family=%s", def->system_family);
3181+
3182+ if (virBufferError(&buf)) {
3183+ virReportOOMError();
3184+ goto error;
3185+ }
3186+
3187+ return virBufferContentAndReset(&buf);
3188+
3189+error:
3190+ virBufferFreeAndReset(&buf);
3191+ return(NULL);
3192+}
3193+
3194+static char *
3195+qemuBuildClockArgStr(virDomainClockDefPtr def)
3196+{
3197+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3198+
3199+ switch (def->offset) {
3200+ case VIR_DOMAIN_CLOCK_OFFSET_UTC:
3201+ virBufferAddLit(&buf, "base=utc");
3202+ break;
3203+
3204+ case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
3205+ case VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE:
3206+ virBufferAddLit(&buf, "base=localtime");
3207+ break;
3208+
3209+ case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE: {
3210+ time_t now = time(NULL);
3211+ struct tm nowbits;
3212+
3213+ now += def->data.adjustment;
3214+ gmtime_r(&now, &nowbits);
3215+
3216+ virBufferAsprintf(&buf, "base=%d-%02d-%02dT%02d:%02d:%02d",
3217+ nowbits.tm_year + 1900,
3218+ nowbits.tm_mon + 1,
3219+ nowbits.tm_mday,
3220+ nowbits.tm_hour,
3221+ nowbits.tm_min,
3222+ nowbits.tm_sec);
3223+ } break;
3224+
3225+ default:
3226+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3227+ _("unsupported clock offset '%s'"),
3228+ virDomainClockOffsetTypeToString(def->offset));
3229+ goto error;
3230+ }
3231+
3232+ /* Look for an 'rtc' timer element, and add in appropriate clock= and driftfix= */
3233+ int i;
3234+ for (i = 0; i < def->ntimers; i++) {
3235+ if (def->timers[i]->name == VIR_DOMAIN_TIMER_NAME_RTC) {
3236+ switch (def->timers[i]->track) {
3237+ case -1: /* unspecified - use hypervisor default */
3238+ break;
3239+ case VIR_DOMAIN_TIMER_TRACK_BOOT:
3240+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3241+ _("unsupported rtc timer track '%s'"),
3242+ virDomainTimerTrackTypeToString(def->timers[i]->track));
3243+ goto error;
3244+ case VIR_DOMAIN_TIMER_TRACK_GUEST:
3245+ virBufferAddLit(&buf, ",clock=vm");
3246+ break;
3247+ case VIR_DOMAIN_TIMER_TRACK_WALL:
3248+ virBufferAddLit(&buf, ",clock=host");
3249+ break;
3250+ }
3251+
3252+ switch (def->timers[i]->tickpolicy) {
3253+ case -1:
3254+ case VIR_DOMAIN_TIMER_TICKPOLICY_DELAY:
3255+ /* This is the default - missed ticks delivered when
3256+ next scheduled, at normal rate */
3257+ break;
3258+ case VIR_DOMAIN_TIMER_TICKPOLICY_CATCHUP:
3259+ /* deliver ticks at a faster rate until caught up */
3260+ virBufferAddLit(&buf, ",driftfix=slew");
3261+ break;
3262+ case VIR_DOMAIN_TIMER_TICKPOLICY_MERGE:
3263+ case VIR_DOMAIN_TIMER_TICKPOLICY_DISCARD:
3264+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3265+ _("unsupported rtc timer tickpolicy '%s'"),
3266+ virDomainTimerTickpolicyTypeToString(def->timers[i]->tickpolicy));
3267+ goto error;
3268+ }
3269+ break; /* no need to check other timers - there is only one rtc */
3270+ }
3271+ }
3272+
3273+ if (virBufferError(&buf)) {
3274+ virReportOOMError();
3275+ goto error;
3276+ }
3277+
3278+ return virBufferContentAndReset(&buf);
3279+
3280+error:
3281+ virBufferFreeAndReset(&buf);
3282+ return NULL;
3283+}
3284+
3285+
3286+static int
3287+qemuBuildCpuArgStr(const struct qemud_driver *driver,
3288+ const virDomainDefPtr def,
3289+ const char *emulator,
3290+ virBitmapPtr qemuCaps,
3291+ const struct utsname *ut,
3292+ char **opt,
3293+ bool *hasHwVirt)
3294+{
3295+ const virCPUDefPtr host = driver->caps->host.cpu;
3296+ virCPUDefPtr guest = NULL;
3297+ unsigned int ncpus = 0;
3298+ const char **cpus = NULL;
3299+ union cpuData *data = NULL;
3300+ int ret = -1;
3301+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3302+ int i;
3303+
3304+ *hasHwVirt = false;
3305+
3306+ if (def->cpu && def->cpu->model) {
3307+ if (host &&
3308+ qemuCapsProbeCPUModels(emulator, qemuCaps, host->arch,
3309+ &ncpus, &cpus) < 0)
3310+ goto cleanup;
3311+
3312+ if (!ncpus || !host) {
3313+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3314+ _("CPU specification not supported by hypervisor"));
3315+ goto cleanup;
3316+ }
3317+ }
3318+
3319+ if (ncpus > 0 && host) {
3320+ virCPUCompareResult cmp;
3321+ const char *preferred;
3322+ int hasSVM;
3323+
3324+ cmp = cpuGuestData(host, def->cpu, &data);
3325+ switch (cmp) {
3326+ case VIR_CPU_COMPARE_INCOMPATIBLE:
3327+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
3328+ "%s", _("guest CPU is not compatible with host CPU"));
3329+ /* fall through */
3330+ case VIR_CPU_COMPARE_ERROR:
3331+ goto cleanup;
3332+
3333+ default:
3334+ break;
3335+ }
3336+
3337+ if (VIR_ALLOC(guest) < 0 || !(guest->arch = strdup(host->arch)))
3338+ goto no_memory;
3339+
3340+ if (def->cpu->match == VIR_CPU_MATCH_MINIMUM)
3341+ preferred = host->model;
3342+ else
3343+ preferred = def->cpu->model;
3344+
3345+ guest->type = VIR_CPU_TYPE_GUEST;
3346+ if (cpuDecode(guest, data, cpus, ncpus, preferred) < 0)
3347+ goto cleanup;
3348+
3349+ /* Only 'svm' requires --enable-nesting. The nested
3350+ * 'vmx' patches now simply hook off the CPU features
3351+ */
3352+ hasSVM = cpuHasFeature(guest->arch, data, "svm");
3353+ if (hasSVM < 0)
3354+ goto cleanup;
3355+ *hasHwVirt = hasSVM > 0 ? true : false;
3356+
3357+ virBufferAdd(&buf, guest->model, -1);
3358+ for (i = 0; i < guest->nfeatures; i++) {
3359+ char sign;
3360+ if (guest->features[i].policy == VIR_CPU_FEATURE_DISABLE)
3361+ sign = '-';
3362+ else
3363+ sign = '+';
3364+
3365+ virBufferAsprintf(&buf, ",%c%s", sign, guest->features[i].name);
3366+ }
3367+ }
3368+ else {
3369+ /*
3370+ * Need to force a 32-bit guest CPU type if
3371+ *
3372+ * 1. guest OS is i686
3373+ * 2. host OS is x86_64
3374+ * 3. emulator is qemu-kvm or kvm
3375+ *
3376+ * Or
3377+ *
3378+ * 1. guest OS is i686
3379+ * 2. emulator is qemu-system-x86_64
3380+ */
3381+ if (STREQ(def->os.arch, "i686") &&
3382+ ((STREQ(ut->machine, "x86_64") &&
3383+ strstr(emulator, "kvm")) ||
3384+ strstr(emulator, "x86_64")))
3385+ virBufferAddLit(&buf, "qemu32");
3386+ }
3387+
3388+ if (virBufferError(&buf))
3389+ goto no_memory;
3390+
3391+ *opt = virBufferContentAndReset(&buf);
3392+
3393+ ret = 0;
3394+
3395+cleanup:
3396+ if (guest)
3397+ cpuDataFree(guest->arch, data);
3398+ virCPUDefFree(guest);
3399+
3400+ if (cpus) {
3401+ for (i = 0; i < ncpus; i++)
3402+ VIR_FREE(cpus[i]);
3403+ VIR_FREE(cpus);
3404+ }
3405+
3406+ return ret;
3407+
3408+no_memory:
3409+ virReportOOMError();
3410+ goto cleanup;
3411+}
3412+
3413+static char *
3414+qemuBuildSmpArgStr(const virDomainDefPtr def,
3415+ virBitmapPtr qemuCaps)
3416+{
3417+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3418+
3419+ virBufferAsprintf(&buf, "%u", def->vcpus);
3420+
3421+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_SMP_TOPOLOGY)) {
3422+ if (def->vcpus != def->maxvcpus)
3423+ virBufferAsprintf(&buf, ",maxcpus=%u", def->maxvcpus);
3424+ /* sockets, cores, and threads are either all zero
3425+ * or all non-zero, thus checking one of them is enough */
3426+ if (def->cpu && def->cpu->sockets) {
3427+ virBufferAsprintf(&buf, ",sockets=%u", def->cpu->sockets);
3428+ virBufferAsprintf(&buf, ",cores=%u", def->cpu->cores);
3429+ virBufferAsprintf(&buf, ",threads=%u", def->cpu->threads);
3430+ }
3431+ else {
3432+ virBufferAsprintf(&buf, ",sockets=%u", def->maxvcpus);
3433+ virBufferAsprintf(&buf, ",cores=%u", 1);
3434+ virBufferAsprintf(&buf, ",threads=%u", 1);
3435+ }
3436+ } else if (def->vcpus != def->maxvcpus) {
3437+ virBufferFreeAndReset(&buf);
3438+ /* FIXME - consider hot-unplugging cpus after boot for older qemu */
3439+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3440+ _("setting current vcpu count less than maximum is "
3441+ "not supported with this QEMU binary"));
3442+ return NULL;
3443+ }
3444+
3445+ if (virBufferError(&buf)) {
3446+ virBufferFreeAndReset(&buf);
3447+ virReportOOMError();
3448+ return NULL;
3449+ }
3450+
3451+ return virBufferContentAndReset(&buf);
3452+}
3453+
3454+static void
3455+qemuBuildNumaCPUArgStr(char *cpumask, virBufferPtr buf)
3456+{
3457+ int i, first, last;
3458+ int cpuSet = 0;
3459+
3460+ first = last = 0;
3461+ for (i = 0; i < VIR_DOMAIN_CPUMASK_LEN; i++) {
3462+ if (cpumask[i]) {
3463+ if (cpuSet) {
3464+ last = i;
3465+ } else {
3466+ first = last = i;
3467+ cpuSet = 1;
3468+ }
3469+ } else {
3470+ if (!cpuSet)
3471+ continue;
3472+ if (first == last)
3473+ virBufferAsprintf(buf, "%d,", first);
3474+ else
3475+ virBufferAsprintf(buf, "%d-%d,", first, last);
3476+ cpuSet = 0;
3477+ }
3478+ }
3479+
3480+ if (cpuSet) {
3481+ if (first == last)
3482+ virBufferAsprintf(buf, "%d,", first);
3483+ else
3484+ virBufferAsprintf(buf, "%d-%d,", first, last);
3485+ }
3486+}
3487+
3488+static int
3489+qemuBuildNumaArgStr(const virDomainDefPtr def, virCommandPtr cmd)
3490+{
3491+ int i;
3492+ virBuffer buf = VIR_BUFFER_INITIALIZER;
3493+
3494+ for (i = 0; i < def->cpu->ncells; i++) {
3495+ virCommandAddArg(cmd, "-numa");
3496+ virBufferAsprintf(&buf, "node,nodeid=%d", def->cpu->cells[i].cellid);
3497+ virBufferAddLit(&buf, ",cpus=");
3498+ qemuBuildNumaCPUArgStr(def->cpu->cells[i].cpumask, &buf);
3499+ virBufferAsprintf(&buf, "mem=%d",
3500+ VIR_DIV_UP(def->cpu->cells[i].mem, 1024));
3501+
3502+ if (virBufferError(&buf))
3503+ goto error;
3504+
3505+ virCommandAddArgBuffer(cmd, &buf);
3506+ }
3507+ return 0;
3508+
3509+error:
3510+ virBufferFreeAndReset(&buf);
3511+ virReportOOMError();
3512+ return -1;
3513+}
3514+
3515+/*
3516+ * Constructs a argv suitable for launching qemu with config defined
3517+ * for a given virtual machine.
3518+ *
3519+ * XXX 'conn' is only required to resolve network -> bridge name
3520+ * figure out how to remove this requirement some day
3521+ */
3522+virCommandPtr
3523+qemuBuildCommandLine(virConnectPtr conn,
3524+ struct qemud_driver *driver,
3525+ virDomainDefPtr def,
3526+ virDomainChrSourceDefPtr monitor_chr,
3527+ bool monitor_json,
3528+ virBitmapPtr qemuCaps,
3529+ const char *migrateFrom,
3530+ int migrateFd,
3531+ virDomainSnapshotObjPtr snapshot,
3532+ enum virNetDevVPortProfileOp vmop)
3533+{
3534+ int i;
3535+ struct utsname ut;
3536+ int disableKQEMU = 0;
3537+ int enableKQEMU = 0;
3538+ int disableKVM = 0;
3539+ int enableKVM = 0;
3540+ const char *emulator;
3541+ char uuid[VIR_UUID_STRING_BUFLEN];
3542+ char *cpu;
3543+ char *smp;
3544+ int last_good_net = -1;
3545+ bool hasHwVirt = false;
3546+ virCommandPtr cmd;
3547+ bool emitBootindex = false;
3548+ int usbcontroller = 0;
3549+ bool usblegacy = false;
3550+ uname_normalize(&ut);
3551+
3552+ virUUIDFormat(def->uuid, uuid);
3553+
3554+ emulator = def->emulator;
3555+
3556+ /*
3557+ * do not use boot=on for drives when not using KVM since this
3558+ * is not supported at all in upstream QEmu.
3559+ */
3560+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_KVM) &&
3561+ (def->virtType == VIR_DOMAIN_VIRT_QEMU))
3562+ qemuCapsClear(qemuCaps, QEMU_CAPS_DRIVE_BOOT);
3563+
3564+ switch (def->virtType) {
3565+ case VIR_DOMAIN_VIRT_QEMU:
3566+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_KQEMU))
3567+ disableKQEMU = 1;
3568+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_KVM))
3569+ disableKVM = 1;
3570+ break;
3571+
3572+ case VIR_DOMAIN_VIRT_KQEMU:
3573+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_KVM))
3574+ disableKVM = 1;
3575+
3576+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KQEMU)) {
3577+ enableKQEMU = 1;
3578+ } else if (!qemuCapsGet(qemuCaps, QEMU_CAPS_KQEMU)) {
3579+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3580+ _("the QEMU binary %s does not support kqemu"),
3581+ emulator);
3582+ }
3583+ break;
3584+
3585+ case VIR_DOMAIN_VIRT_KVM:
3586+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_KQEMU))
3587+ disableKQEMU = 1;
3588+
3589+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KVM)) {
3590+ enableKVM = 1;
3591+ } else if (!qemuCapsGet(qemuCaps, QEMU_CAPS_KVM)) {
3592+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3593+ _("the QEMU binary %s does not support kvm"),
3594+ emulator);
3595+ }
3596+ break;
3597+
3598+ case VIR_DOMAIN_VIRT_XEN:
3599+ /* XXX better check for xenner */
3600+ break;
3601+
3602+ default:
3603+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3604+ _("the QEMU binary %s does not support %s"),
3605+ emulator, virDomainVirtTypeToString(def->virtType));
3606+ break;
3607+ }
3608+
3609+ cmd = virCommandNewArgList(emulator, "-S", NULL);
3610+
3611+ virCommandAddEnvPassCommon(cmd);
3612+
3613+ /* This should *never* be NULL, since we always provide
3614+ * a machine in the capabilities data for QEMU. So this
3615+ * check is just here as a safety in case the unexpected
3616+ * happens */
3617+ if (def->os.machine)
3618+ virCommandAddArgList(cmd, "-M", def->os.machine, NULL);
3619+
3620+ if (qemuBuildCpuArgStr(driver, def, emulator, qemuCaps,
3621+ &ut, &cpu, &hasHwVirt) < 0)
3622+ goto error;
3623+
3624+ if (cpu) {
3625+ virCommandAddArgList(cmd, "-cpu", cpu, NULL);
3626+ VIR_FREE(cpu);
3627+
3628+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NESTING) &&
3629+ hasHwVirt)
3630+ virCommandAddArg(cmd, "-enable-nesting");
3631+ }
3632+
3633+ if (disableKQEMU)
3634+ virCommandAddArg(cmd, "-no-kqemu");
3635+ else if (enableKQEMU)
3636+ virCommandAddArgList(cmd, "-enable-kqemu", "-kernel-kqemu", NULL);
3637+ if (disableKVM)
3638+ virCommandAddArg(cmd, "-no-kvm");
3639+ if (enableKVM)
3640+ virCommandAddArg(cmd, "-enable-kvm");
3641+
3642+ /* Set '-m MB' based on maxmem, because the lower 'memory' limit
3643+ * is set post-startup using the balloon driver. If balloon driver
3644+ * is not supported, then they're out of luck anyway
3645+ */
3646+ virCommandAddArg(cmd, "-m");
3647+ virCommandAddArgFormat(cmd, "%lu", VIR_DIV_UP(def->mem.max_balloon, 1024));
3648+ if (def->mem.hugepage_backed) {
3649+ if (!driver->hugetlbfs_mount) {
3650+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
3651+ "%s", _("hugetlbfs filesystem is not mounted"));
3652+ goto error;
3653+ }
3654+ if (!driver->hugepage_path) {
3655+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
3656+ "%s", _("hugepages are disabled by administrator config"));
3657+ goto error;
3658+ }
3659+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_MEM_PATH)) {
3660+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
3661+ _("hugepage backing not supported by '%s'"),
3662+ def->emulator);
3663+ goto error;
3664+ }
3665+ virCommandAddArgList(cmd, "-mem-prealloc", "-mem-path",
3666+ driver->hugepage_path, NULL);
3667+ }
3668+
3669+ virCommandAddArg(cmd, "-smp");
3670+ if (!(smp = qemuBuildSmpArgStr(def, qemuCaps)))
3671+ goto error;
3672+ virCommandAddArg(cmd, smp);
3673+ VIR_FREE(smp);
3674+
3675+ if (def->cpu && def->cpu->ncells)
3676+ if (qemuBuildNumaArgStr(def, cmd) < 0)
3677+ goto error;
3678+
3679+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NAME)) {
3680+ virCommandAddArg(cmd, "-name");
3681+ if (driver->setProcessName &&
3682+ qemuCapsGet(qemuCaps, QEMU_CAPS_NAME_PROCESS)) {
3683+ virCommandAddArgFormat(cmd, "%s,process=qemu:%s",
3684+ def->name, def->name);
3685+ } else {
3686+ virCommandAddArg(cmd, def->name);
3687+ }
3688+ }
3689+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_UUID))
3690+ virCommandAddArgList(cmd, "-uuid", uuid, NULL);
3691+ if (def->virtType == VIR_DOMAIN_VIRT_XEN ||
3692+ STREQ(def->os.type, "xen") ||
3693+ STREQ(def->os.type, "linux")) {
3694+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DOMID)) {
3695+ virCommandAddArg(cmd, "-domid");
3696+ virCommandAddArgFormat(cmd, "%d", def->id);
3697+ } else if (qemuCapsGet(qemuCaps, QEMU_CAPS_XEN_DOMID)) {
3698+ virCommandAddArg(cmd, "-xen-attach");
3699+ virCommandAddArg(cmd, "-xen-domid");
3700+ virCommandAddArgFormat(cmd, "%d", def->id);
3701+ } else {
3702+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
3703+ _("qemu emulator '%s' does not support xen"),
3704+ def->emulator);
3705+ goto error;
3706+ }
3707+ }
3708+
3709+ if ((def->os.smbios_mode != VIR_DOMAIN_SMBIOS_NONE) &&
3710+ (def->os.smbios_mode != VIR_DOMAIN_SMBIOS_EMULATE)) {
3711+ virSysinfoDefPtr source = NULL;
3712+ bool skip_uuid = false;
3713+
3714+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_SMBIOS_TYPE)) {
3715+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3716+ _("the QEMU binary %s does not support smbios settings"),
3717+ emulator);
3718+ goto error;
3719+ }
3720+
3721+ /* should we really error out or just warn in those cases ? */
3722+ if (def->os.smbios_mode == VIR_DOMAIN_SMBIOS_HOST) {
3723+ if (driver->hostsysinfo == NULL) {
3724+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3725+ _("Host SMBIOS information is not available"));
3726+ goto error;
3727+ }
3728+ source = driver->hostsysinfo;
3729+ /* Host and guest uuid must differ, by definition of UUID. */
3730+ skip_uuid = true;
3731+ } else if (def->os.smbios_mode == VIR_DOMAIN_SMBIOS_SYSINFO) {
3732+ if (def->sysinfo == NULL) {
3733+ qemuReportError(VIR_ERR_XML_ERROR,
3734+ _("Domain '%s' sysinfo are not available"),
3735+ def->name);
3736+ goto error;
3737+ }
3738+ source = def->sysinfo;
3739+ /* domain_conf guaranteed that system_uuid matches guest uuid. */
3740+ }
3741+ if (source != NULL) {
3742+ char *smbioscmd;
3743+
3744+ smbioscmd = qemuBuildSmbiosBiosStr(source);
3745+ if (smbioscmd != NULL) {
3746+ virCommandAddArgList(cmd, "-smbios", smbioscmd, NULL);
3747+ VIR_FREE(smbioscmd);
3748+ }
3749+ smbioscmd = qemuBuildSmbiosSystemStr(source, skip_uuid);
3750+ if (smbioscmd != NULL) {
3751+ virCommandAddArgList(cmd, "-smbios", smbioscmd, NULL);
3752+ VIR_FREE(smbioscmd);
3753+ }
3754+ }
3755+ }
3756+
3757+ /*
3758+ * NB, -nographic *MUST* come before any serial, or monitor
3759+ * or parallel port flags due to QEMU craziness, where it
3760+ * decides to change the serial port & monitor to be on stdout
3761+ * if you ask for nographic. So we have to make sure we override
3762+ * these defaults ourselves...
3763+ */
3764+ if (!def->graphics)
3765+ virCommandAddArg(cmd, "-nographic");
3766+
3767+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
3768+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NODEFCONFIG))
3769+ virCommandAddArg(cmd,
3770+ "-nodefconfig"); /* Disable global config files */
3771+ virCommandAddArg(cmd,
3772+ "-nodefaults"); /* Disable default guest devices */
3773+ }
3774+
3775+ /* Serial graphics adapter */
3776+ if (def->os.bios.useserial == VIR_DOMAIN_BIOS_USESERIAL_YES) {
3777+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
3778+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3779+ _("qemu does not support -device"));
3780+ goto error;
3781+ }
3782+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_SGA)) {
3783+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
3784+ _("qemu does not support SGA"));
3785+ goto error;
3786+ }
3787+ if (!def->nserials) {
3788+ qemuReportError(VIR_ERR_XML_ERROR, "%s",
3789+ _("need at least one serial port to use SGA"));
3790+ goto error;
3791+ }
3792+ virCommandAddArgList(cmd, "-device", "sga", NULL);
3793+ }
3794+
3795+ if (monitor_chr) {
3796+ char *chrdev;
3797+ /* Use -chardev if it's available */
3798+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV)) {
3799+
3800+ virCommandAddArg(cmd, "-chardev");
3801+ if (!(chrdev = qemuBuildChrChardevStr(monitor_chr, "monitor",
3802+ qemuCaps)))
3803+ goto error;
3804+ virCommandAddArg(cmd, chrdev);
3805+ VIR_FREE(chrdev);
3806+
3807+ virCommandAddArg(cmd, "-mon");
3808+ virCommandAddArgFormat(cmd,
3809+ "chardev=charmonitor,id=monitor,mode=%s",
3810+ monitor_json ? "control" : "readline");
3811+ } else {
3812+ const char *prefix = NULL;
3813+ if (monitor_json)
3814+ prefix = "control,";
3815+
3816+ virCommandAddArg(cmd, "-monitor");
3817+ if (!(chrdev = qemuBuildChrArgStr(monitor_chr, prefix)))
3818+ goto error;
3819+ virCommandAddArg(cmd, chrdev);
3820+ VIR_FREE(chrdev);
3821+ }
3822+ }
3823+
3824+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_RTC)) {
3825+ const char *rtcopt;
3826+ virCommandAddArg(cmd, "-rtc");
3827+ if (!(rtcopt = qemuBuildClockArgStr(&def->clock)))
3828+ goto error;
3829+ virCommandAddArg(cmd, rtcopt);
3830+ VIR_FREE(rtcopt);
3831+ } else {
3832+ switch (def->clock.offset) {
3833+ case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
3834+ case VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE:
3835+ virCommandAddArg(cmd, "-localtime");
3836+ break;
3837+
3838+ case VIR_DOMAIN_CLOCK_OFFSET_UTC:
3839+ /* Nothing, its the default */
3840+ break;
3841+
3842+ default:
3843+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3844+ _("unsupported clock offset '%s'"),
3845+ virDomainClockOffsetTypeToString(def->clock.offset));
3846+ goto error;
3847+ }
3848+ }
3849+ if (def->clock.offset == VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE &&
3850+ def->clock.data.timezone) {
3851+ virCommandAddEnvPair(cmd, "TZ", def->clock.data.timezone);
3852+ }
3853+
3854+ for (i = 0; i < def->clock.ntimers; i++) {
3855+ switch (def->clock.timers[i]->name) {
3856+ default:
3857+ case VIR_DOMAIN_TIMER_NAME_PLATFORM:
3858+ case VIR_DOMAIN_TIMER_NAME_TSC:
3859+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3860+ _("unsupported timer type (name) '%s'"),
3861+ virDomainTimerNameTypeToString(def->clock.timers[i]->name));
3862+ goto error;
3863+
3864+ case VIR_DOMAIN_TIMER_NAME_RTC:
3865+ /* This has already been taken care of (in qemuBuildClockArgStr)
3866+ if QEMU_CAPS_RTC is set (mutually exclusive with
3867+ QEMUD_FLAG_RTC_TD_HACK) */
3868+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_RTC_TD_HACK)) {
3869+ switch (def->clock.timers[i]->tickpolicy) {
3870+ case -1:
3871+ case VIR_DOMAIN_TIMER_TICKPOLICY_DELAY:
3872+ /* the default - do nothing */
3873+ break;
3874+ case VIR_DOMAIN_TIMER_TICKPOLICY_CATCHUP:
3875+ virCommandAddArg(cmd, "-rtc-td-hack");
3876+ break;
3877+ case VIR_DOMAIN_TIMER_TICKPOLICY_MERGE:
3878+ case VIR_DOMAIN_TIMER_TICKPOLICY_DISCARD:
3879+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3880+ _("unsupported rtc tickpolicy '%s'"),
3881+ virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
3882+ goto error;
3883+ }
3884+ } else if (!qemuCapsGet(qemuCaps, QEMU_CAPS_RTC)
3885+ && (def->clock.timers[i]->tickpolicy
3886+ != VIR_DOMAIN_TIMER_TICKPOLICY_DELAY)
3887+ && (def->clock.timers[i]->tickpolicy != -1)) {
3888+ /* a non-default rtc policy was given, but there is no
3889+ way to implement it in this version of qemu */
3890+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3891+ _("unsupported rtc tickpolicy '%s'"),
3892+ virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
3893+ goto error;
3894+ }
3895+ break;
3896+
3897+ case VIR_DOMAIN_TIMER_NAME_PIT:
3898+ switch (def->clock.timers[i]->tickpolicy) {
3899+ case -1:
3900+ case VIR_DOMAIN_TIMER_TICKPOLICY_DELAY:
3901+ /* delay is the default if we don't have kernel
3902+ (-no-kvm-pit), otherwise, the default is catchup. */
3903+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NO_KVM_PIT))
3904+ virCommandAddArg(cmd, "-no-kvm-pit-reinjection");
3905+ break;
3906+ case VIR_DOMAIN_TIMER_TICKPOLICY_CATCHUP:
3907+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NO_KVM_PIT)) {
3908+ /* do nothing - this is default for kvm-pit */
3909+ } else if (qemuCapsGet(qemuCaps, QEMU_CAPS_TDF)) {
3910+ /* -tdf switches to 'catchup' with userspace pit. */
3911+ virCommandAddArg(cmd, "-tdf");
3912+ } else {
3913+ /* can't catchup if we have neither pit mode */
3914+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3915+ _("unsupported pit tickpolicy '%s'"),
3916+ virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
3917+ goto error;
3918+ }
3919+ break;
3920+ case VIR_DOMAIN_TIMER_TICKPOLICY_MERGE:
3921+ case VIR_DOMAIN_TIMER_TICKPOLICY_DISCARD:
3922+ /* no way to support these modes for pit in qemu */
3923+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3924+ _("unsupported pit tickpolicy '%s'"),
3925+ virDomainTimerTickpolicyTypeToString(def->clock.timers[i]->tickpolicy));
3926+ goto error;
3927+ }
3928+ break;
3929+
3930+ case VIR_DOMAIN_TIMER_NAME_HPET:
3931+ /* the only meaningful attribute for hpet is "present". If
3932+ * present is -1, that means it wasn't specified, and
3933+ * should be left at the default for the
3934+ * hypervisor. "default" when -no-hpet exists is "yes",
3935+ * and when -no-hpet doesn't exist is "no". "confusing"?
3936+ * "yes"! */
3937+
3938+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NO_HPET)) {
3939+ if (def->clock.timers[i]->present == 0)
3940+ virCommandAddArg(cmd, "-no-hpet");
3941+ } else {
3942+ /* no hpet timer available. The only possible action
3943+ is to raise an error if present="yes" */
3944+ if (def->clock.timers[i]->present == 1) {
3945+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
3946+ "%s", _("pit timer is not supported"));
3947+ }
3948+ }
3949+ break;
3950+ }
3951+ }
3952+
3953+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NO_REBOOT) &&
3954+ def->onReboot != VIR_DOMAIN_LIFECYCLE_RESTART)
3955+ virCommandAddArg(cmd, "-no-reboot");
3956+
3957+ /* If JSON monitor is enabled, we can receive an event
3958+ * when QEMU stops. If we use no-shutdown, then we can
3959+ * watch for this event and do a soft/warm reboot.
3960+ */
3961+ if (monitor_json && qemuCapsGet(qemuCaps, QEMU_CAPS_NO_SHUTDOWN))
3962+ virCommandAddArg(cmd, "-no-shutdown");
3963+
3964+ if (!(def->features & (1 << VIR_DOMAIN_FEATURE_ACPI)))
3965+ virCommandAddArg(cmd, "-no-acpi");
3966+
3967+ if (!def->os.bootloader) {
3968+ /*
3969+ * We prefer using explicit bootindex=N parameters for predictable
3970+ * results even though domain XML doesn't use per device boot elements.
3971+ * However, we can't use bootindex if boot menu was requested.
3972+ */
3973+ if (!def->os.nBootDevs) {
3974+ /* def->os.nBootDevs is guaranteed to be > 0 unless per-device boot
3975+ * configuration is used
3976+ */
3977+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX)) {
3978+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
3979+ _("hypervisor lacks deviceboot feature"));
3980+ goto error;
3981+ }
3982+ emitBootindex = true;
3983+ } else if (qemuCapsGet(qemuCaps, QEMU_CAPS_BOOTINDEX) &&
3984+ (def->os.bootmenu != VIR_DOMAIN_BOOT_MENU_ENABLED ||
3985+ !qemuCapsGet(qemuCaps, QEMU_CAPS_BOOT_MENU))) {
3986+ emitBootindex = true;
3987+ }
3988+
3989+ if (!emitBootindex) {
3990+ virBuffer boot_buf = VIR_BUFFER_INITIALIZER;
3991+ char boot[VIR_DOMAIN_BOOT_LAST+1];
3992+
3993+ for (i = 0 ; i < def->os.nBootDevs ; i++) {
3994+ switch (def->os.bootDevs[i]) {
3995+ case VIR_DOMAIN_BOOT_CDROM:
3996+ boot[i] = 'd';
3997+ break;
3998+ case VIR_DOMAIN_BOOT_FLOPPY:
3999+ boot[i] = 'a';
4000+ break;
4001+ case VIR_DOMAIN_BOOT_DISK:
4002+ boot[i] = 'c';
4003+ break;
4004+ case VIR_DOMAIN_BOOT_NET:
4005+ boot[i] = 'n';
4006+ break;
4007+ default:
4008+ boot[i] = 'c';
4009+ break;
4010+ }
4011+ }
4012+ boot[def->os.nBootDevs] = '\0';
4013+
4014+ virCommandAddArg(cmd, "-boot");
4015+
4016+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_BOOT_MENU) &&
4017+ def->os.bootmenu != VIR_DOMAIN_BOOT_MENU_DEFAULT) {
4018+ if (def->os.bootmenu == VIR_DOMAIN_BOOT_MENU_ENABLED)
4019+ virBufferAsprintf(&boot_buf, "order=%s,menu=on", boot);
4020+ else if (def->os.bootmenu == VIR_DOMAIN_BOOT_MENU_DISABLED)
4021+ virBufferAsprintf(&boot_buf, "order=%s,menu=off", boot);
4022+ } else {
4023+ virBufferAdd(&boot_buf, boot, -1);
4024+ }
4025+
4026+ virCommandAddArgBuffer(cmd, &boot_buf);
4027+ }
4028+
4029+ if (def->os.kernel)
4030+ virCommandAddArgList(cmd, "-kernel", def->os.kernel, NULL);
4031+ if (def->os.initrd)
4032+ virCommandAddArgList(cmd, "-initrd", def->os.initrd, NULL);
4033+ if (def->os.cmdline)
4034+ virCommandAddArgList(cmd, "-append", def->os.cmdline, NULL);
4035+ } else {
4036+ virCommandAddArgList(cmd, "-bootloader", def->os.bootloader, NULL);
4037+ }
4038+
4039+ for (i = 0 ; i < def->ndisks ; i++) {
4040+ virDomainDiskDefPtr disk = def->disks[i];
4041+
4042+ if (disk->driverName != NULL &&
4043+ !STREQ(disk->driverName, "qemu")) {
4044+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4045+ _("unsupported driver name '%s' for disk '%s'"),
4046+ disk->driverName, disk->src);
4047+ goto error;
4048+ }
4049+ }
4050+
4051+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4052+ for (i = 0 ; i < def->ncontrollers ; i++) {
4053+ virDomainControllerDefPtr cont = def->controllers[i];
4054+
4055+ /* We don't add an explicit IDE or FD controller because the
4056+ * provided PIIX4 device already includes one. It isn't possible to
4057+ * remove the PIIX4. */
4058+ if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_IDE ||
4059+ cont->type == VIR_DOMAIN_CONTROLLER_TYPE_FDC)
4060+ continue;
4061+
4062+ /* Only recent QEMU implements a SATA (AHCI) controller */
4063+ if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_SATA) {
4064+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_ICH9_AHCI)) {
4065+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4066+ "%s", _("SATA is not supported with this QEMU binary"));
4067+ goto error;
4068+ } else {
4069+ char *devstr;
4070+
4071+ virCommandAddArg(cmd, "-device");
4072+ if (!(devstr = qemuBuildControllerDevStr(cont, qemuCaps, NULL)))
4073+ goto error;
4074+
4075+ virCommandAddArg(cmd, devstr);
4076+ VIR_FREE(devstr);
4077+ }
4078+ } else if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
4079+ def->controllers[i]->model == -1 &&
4080+ !qemuCapsGet(qemuCaps, QEMU_CAPS_PIIX3_USB_UHCI)) {
4081+ if (usblegacy) {
4082+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4083+ _("Multiple legacy USB controller not supported"));
4084+ goto error;
4085+ }
4086+ usblegacy = true;
4087+ } else {
4088+ virCommandAddArg(cmd, "-device");
4089+
4090+ char *devstr;
4091+ if (!(devstr = qemuBuildControllerDevStr(def->controllers[i], qemuCaps,
4092+ &usbcontroller)))
4093+ goto error;
4094+
4095+ virCommandAddArg(cmd, devstr);
4096+ VIR_FREE(devstr);
4097+ }
4098+ }
4099+ }
4100+
4101+ /* If QEMU supports -drive param instead of old -hda, -hdb, -cdrom .. */
4102+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE)) {
4103+ int bootCD = 0, bootFloppy = 0, bootDisk = 0;
4104+
4105+ if ((qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_BOOT) || emitBootindex) &&
4106+ !def->os.kernel) {
4107+ /* bootDevs will get translated into either bootindex=N or boot=on
4108+ * depending on what qemu supports */
4109+ for (i = 0 ; i < def->os.nBootDevs ; i++) {
4110+ switch (def->os.bootDevs[i]) {
4111+ case VIR_DOMAIN_BOOT_CDROM:
4112+ bootCD = i + 1;
4113+ break;
4114+ case VIR_DOMAIN_BOOT_FLOPPY:
4115+ bootFloppy = i + 1;
4116+ break;
4117+ case VIR_DOMAIN_BOOT_DISK:
4118+ bootDisk = i + 1;
4119+ break;
4120+ }
4121+ }
4122+ }
4123+
4124+ for (i = 0 ; i < def->ndisks ; i++) {
4125+ char *optstr;
4126+ int bootindex = 0;
4127+ virDomainDiskDefPtr disk = def->disks[i];
4128+ int withDeviceArg = 0;
4129+ bool deviceFlagMasked = false;
4130+
4131+ /* Unless we have -device, then USB disks need special
4132+ handling */
4133+ if ((disk->bus == VIR_DOMAIN_DISK_BUS_USB) &&
4134+ !qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4135+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
4136+ virCommandAddArg(cmd, "-usbdevice");
4137+ virCommandAddArgFormat(cmd, "disk:%s", disk->src);
4138+ } else {
4139+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
4140+ _("unsupported usb disk type for '%s'"),
4141+ disk->src);
4142+ goto error;
4143+ }
4144+ continue;
4145+ }
4146+
4147+ switch (disk->device) {
4148+ case VIR_DOMAIN_DISK_DEVICE_CDROM:
4149+ bootindex = bootCD;
4150+ bootCD = 0;
4151+ break;
4152+ case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
4153+ bootindex = bootFloppy;
4154+ bootFloppy = 0;
4155+ break;
4156+ case VIR_DOMAIN_DISK_DEVICE_DISK:
4157+ bootindex = bootDisk;
4158+ bootDisk = 0;
4159+ break;
4160+ }
4161+
4162+ virCommandAddArg(cmd, "-drive");
4163+
4164+ /* Unfortunately it is not possible to use
4165+ -device for floppies, or Xen paravirt
4166+ devices. Fortunately, those don't need
4167+ static PCI addresses, so we don't really
4168+ care that we can't use -device */
4169+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4170+ if (disk->bus != VIR_DOMAIN_DISK_BUS_XEN) {
4171+ withDeviceArg = 1;
4172+ } else {
4173+ qemuCapsClear(qemuCaps, QEMU_CAPS_DEVICE);
4174+ deviceFlagMasked = true;
4175+ }
4176+ }
4177+ optstr = qemuBuildDriveStr(conn, disk,
4178+ emitBootindex ? false : !!bootindex,
4179+ qemuCaps);
4180+ if (deviceFlagMasked)
4181+ qemuCapsSet(qemuCaps, QEMU_CAPS_DEVICE);
4182+ if (!optstr)
4183+ goto error;
4184+ virCommandAddArg(cmd, optstr);
4185+ VIR_FREE(optstr);
4186+
4187+ if (!emitBootindex)
4188+ bootindex = 0;
4189+ else if (disk->bootIndex)
4190+ bootindex = disk->bootIndex;
4191+
4192+ if (withDeviceArg) {
4193+ if (disk->bus == VIR_DOMAIN_DISK_BUS_FDC) {
4194+ virCommandAddArg(cmd, "-global");
4195+ virCommandAddArgFormat(cmd, "isa-fdc.drive%c=drive-%s",
4196+ disk->info.addr.drive.unit
4197+ ? 'B' : 'A',
4198+ disk->info.alias);
4199+
4200+ if (bootindex) {
4201+ virCommandAddArg(cmd, "-global");
4202+ virCommandAddArgFormat(cmd, "isa-fdc.bootindex%c=%d",
4203+ disk->info.addr.drive.unit
4204+ ? 'B' : 'A',
4205+ bootindex);
4206+ }
4207+ } else {
4208+ virCommandAddArg(cmd, "-device");
4209+
4210+ if (!(optstr = qemuBuildDriveDevStr(disk, bootindex,
4211+ qemuCaps)))
4212+ goto error;
4213+ virCommandAddArg(cmd, optstr);
4214+ VIR_FREE(optstr);
4215+ }
4216+ }
4217+ }
4218+ } else {
4219+ for (i = 0 ; i < def->ndisks ; i++) {
4220+ char dev[NAME_MAX];
4221+ char *file;
4222+ const char *fmt;
4223+ virDomainDiskDefPtr disk = def->disks[i];
4224+
4225+ if (disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
4226+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
4227+ virCommandAddArg(cmd, "-usbdevice");
4228+ virCommandAddArgFormat(cmd, "disk:%s", disk->src);
4229+ } else {
4230+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
4231+ _("unsupported usb disk type for '%s'"),
4232+ disk->src);
4233+ goto error;
4234+ }
4235+ continue;
4236+ }
4237+
4238+ if (STREQ(disk->dst, "hdc") &&
4239+ disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
4240+ if (disk->src) {
4241+ snprintf(dev, NAME_MAX, "-%s", "cdrom");
4242+ } else {
4243+ continue;
4244+ }
4245+ } else {
4246+ if (STRPREFIX(disk->dst, "hd") ||
4247+ STRPREFIX(disk->dst, "fd")) {
4248+ snprintf(dev, NAME_MAX, "-%s", disk->dst);
4249+ } else {
4250+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
4251+ _("unsupported disk type '%s'"), disk->dst);
4252+ goto error;
4253+ }
4254+ }
4255+
4256+ if (disk->type == VIR_DOMAIN_DISK_TYPE_DIR) {
4257+ /* QEMU only supports magic FAT format for now */
4258+ if (disk->driverType &&
4259+ STRNEQ(disk->driverType, "fat")) {
4260+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
4261+ _("unsupported disk driver type for '%s'"),
4262+ disk->driverType);
4263+ goto error;
4264+ }
4265+ if (!disk->readonly) {
4266+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
4267+ _("cannot create virtual FAT disks in read-write mode"));
4268+ goto error;
4269+ }
4270+ if (disk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
4271+ fmt = "fat:floppy:%s";
4272+ else
4273+ fmt = "fat:%s";
4274+
4275+ if (virAsprintf(&file, fmt, disk->src) < 0) {
4276+ goto no_memory;
4277+ }
4278+ } else if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK) {
4279+ switch (disk->protocol) {
4280+ case VIR_DOMAIN_DISK_PROTOCOL_NBD:
4281+ if (disk->nhosts != 1) {
4282+ qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
4283+ _("NBD accepts only one host"));
4284+ goto error;
4285+ }
4286+ if (virAsprintf(&file, "nbd:%s:%s,", disk->hosts->name,
4287+ disk->hosts->port) < 0) {
4288+ goto no_memory;
4289+ }
4290+ break;
4291+ case VIR_DOMAIN_DISK_PROTOCOL_RBD:
4292+ {
4293+ virBuffer opt = VIR_BUFFER_INITIALIZER;
4294+ if (qemuBuildRBDString(conn, disk, &opt) < 0)
4295+ goto error;
4296+ if (virBufferError(&opt)) {
4297+ virReportOOMError();
4298+ goto error;
4299+ }
4300+ file = virBufferContentAndReset(&opt);
4301+ }
4302+ break;
4303+ case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG:
4304+ if (disk->nhosts == 0) {
4305+ if (virAsprintf(&file, "sheepdog:%s,", disk->src) < 0) {
4306+ goto no_memory;
4307+ }
4308+ } else {
4309+ /* only one host is supported now */
4310+ if (virAsprintf(&file, "sheepdog:%s:%s:%s,",
4311+ disk->hosts->name, disk->hosts->port,
4312+ disk->src) < 0) {
4313+ goto no_memory;
4314+ }
4315+ }
4316+ break;
4317+ }
4318+ } else {
4319+ if (!(file = strdup(disk->src))) {
4320+ goto no_memory;
4321+ }
4322+ }
4323+
4324+ virCommandAddArgList(cmd, dev, file, NULL);
4325+ VIR_FREE(file);
4326+ }
4327+ }
4328+
4329+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_FSDEV)) {
4330+ for (i = 0 ; i < def->nfss ; i++) {
4331+ char *optstr;
4332+ virDomainFSDefPtr fs = def->fss[i];
4333+
4334+ virCommandAddArg(cmd, "-fsdev");
4335+ if (!(optstr = qemuBuildFSStr(fs, qemuCaps)))
4336+ goto error;
4337+ virCommandAddArg(cmd, optstr);
4338+ VIR_FREE(optstr);
4339+
4340+ virCommandAddArg(cmd, "-device");
4341+ if (!(optstr = qemuBuildFSDevStr(fs, qemuCaps)))
4342+ goto error;
4343+ virCommandAddArg(cmd, optstr);
4344+ VIR_FREE(optstr);
4345+ }
4346+ } else {
4347+ if (def->nfss) {
4348+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4349+ _("filesystem passthrough not supported by this QEMU"));
4350+ goto error;
4351+ }
4352+ }
4353+
4354+ if (!def->nnets) {
4355+ /* If we have -device, then we set -nodefault already */
4356+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
4357+ virCommandAddArgList(cmd, "-net", "none", NULL);
4358+ } else {
4359+ int bootNet = 0;
4360+
4361+ if (emitBootindex) {
4362+ /* convert <boot dev='network'/> to bootindex since we didn't emit
4363+ * -boot n
4364+ */
4365+ for (i = 0 ; i < def->os.nBootDevs ; i++) {
4366+ if (def->os.bootDevs[i] == VIR_DOMAIN_BOOT_NET) {
4367+ bootNet = i + 1;
4368+ break;
4369+ }
4370+ }
4371+ }
4372+
4373+ for (i = 0 ; i < def->nnets ; i++) {
4374+ virDomainNetDefPtr net = def->nets[i];
4375+ char *nic, *host;
4376+ char tapfd_name[50];
4377+ char vhostfd_name[50] = "";
4378+ int vlan;
4379+ int bootindex = bootNet;
4380+ int actualType;
4381+
4382+ bootNet = 0;
4383+ if (!bootindex)
4384+ bootindex = net->bootIndex;
4385+
4386+ /* VLANs are not used with -netdev, so don't record them */
4387+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NETDEV) &&
4388+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
4389+ vlan = -1;
4390+ else
4391+ vlan = i;
4392+
4393+ /* If appropriate, grab a physical device from the configured
4394+ * network's pool of devices, or resolve bridge device name
4395+ * to the one defined in the network definition.
4396+ */
4397+ if (networkAllocateActualDevice(net) < 0)
4398+ goto error;
4399+
4400+ actualType = virDomainNetGetActualType(net);
4401+ if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
4402+ actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
4403+ int tapfd = qemuNetworkIfaceConnect(def, conn, driver, net,
4404+ qemuCaps);
4405+ if (tapfd < 0)
4406+ goto error;
4407+
4408+ last_good_net = i;
4409+ virCommandTransferFD(cmd, tapfd);
4410+
4411+ if (snprintf(tapfd_name, sizeof(tapfd_name), "%d",
4412+ tapfd) >= sizeof(tapfd_name))
4413+ goto no_memory;
4414+ } else if (actualType == VIR_DOMAIN_NET_TYPE_DIRECT) {
4415+ int tapfd = qemuPhysIfaceConnect(def, driver, net,
4416+ qemuCaps, vmop);
4417+ if (tapfd < 0)
4418+ goto error;
4419+
4420+ last_good_net = i;
4421+ virCommandTransferFD(cmd, tapfd);
4422+
4423+ if (snprintf(tapfd_name, sizeof(tapfd_name), "%d",
4424+ tapfd) >= sizeof(tapfd_name))
4425+ goto no_memory;
4426+ }
4427+
4428+ if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK ||
4429+ actualType == VIR_DOMAIN_NET_TYPE_BRIDGE ||
4430+ actualType == VIR_DOMAIN_NET_TYPE_DIRECT) {
4431+ /* Attempt to use vhost-net mode for these types of
4432+ network device */
4433+ int vhostfd;
4434+
4435+ if (qemuOpenVhostNet(def, net, qemuCaps, &vhostfd) < 0)
4436+ goto error;
4437+ if (vhostfd >= 0) {
4438+ virCommandTransferFD(cmd, vhostfd);
4439+
4440+ if (snprintf(vhostfd_name, sizeof(vhostfd_name), "%d",
4441+ vhostfd) >= sizeof(vhostfd_name))
4442+ goto no_memory;
4443+ }
4444+ }
4445+ /* Possible combinations:
4446+ *
4447+ * 1. Old way: -net nic,model=e1000,vlan=1 -net tap,vlan=1
4448+ * 2. Semi-new: -device e1000,vlan=1 -net tap,vlan=1
4449+ * 3. Best way: -netdev type=tap,id=netdev1 -device e1000,id=netdev1
4450+ *
4451+ * NB, no support for -netdev without use of -device
4452+ */
4453+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_NETDEV) &&
4454+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4455+ virCommandAddArg(cmd, "-netdev");
4456+ if (!(host = qemuBuildHostNetStr(net, ',', vlan,
4457+ tapfd_name, vhostfd_name)))
4458+ goto error;
4459+ virCommandAddArg(cmd, host);
4460+ VIR_FREE(host);
4461+ }
4462+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4463+ virCommandAddArg(cmd, "-device");
4464+ nic = qemuBuildNicDevStr(net, vlan, bootindex, qemuCaps);
4465+ if (!nic)
4466+ goto error;
4467+ virCommandAddArg(cmd, nic);
4468+ VIR_FREE(nic);
4469+ } else {
4470+ virCommandAddArg(cmd, "-net");
4471+ if (!(nic = qemuBuildNicStr(net, "nic,", vlan)))
4472+ goto error;
4473+ virCommandAddArg(cmd, nic);
4474+ VIR_FREE(nic);
4475+ }
4476+ if (!(qemuCapsGet(qemuCaps, QEMU_CAPS_NETDEV) &&
4477+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))) {
4478+ virCommandAddArg(cmd, "-net");
4479+ if (!(host = qemuBuildHostNetStr(net, ',', vlan,
4480+ tapfd_name, vhostfd_name)))
4481+ goto error;
4482+ virCommandAddArg(cmd, host);
4483+ VIR_FREE(host);
4484+ }
4485+ }
4486+ }
4487+
4488+ if (def->nsmartcards) {
4489+ /* -device usb-ccid was already emitted along with other
4490+ * controllers. For now, qemu handles only one smartcard. */
4491+ virDomainSmartcardDefPtr smartcard = def->smartcards[0];
4492+ char *devstr;
4493+ virBuffer opt = VIR_BUFFER_INITIALIZER;
4494+ int j;
4495+ const char *database;
4496+
4497+ if (def->nsmartcards > 1 ||
4498+ smartcard->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCID ||
4499+ smartcard->info.addr.ccid.controller != 0 ||
4500+ smartcard->info.addr.ccid.slot != 0) {
4501+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4502+ _("this QEMU binary lacks multiple smartcard "
4503+ "support"));
4504+ virBufferFreeAndReset(&opt);
4505+ goto error;
4506+ }
4507+
4508+ switch (smartcard->type) {
4509+ case VIR_DOMAIN_SMARTCARD_TYPE_HOST:
4510+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
4511+ !qemuCapsGet(qemuCaps, QEMU_CAPS_CCID_EMULATED)) {
4512+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4513+ _("this QEMU binary lacks smartcard host "
4514+ "mode support"));
4515+ goto error;
4516+ }
4517+
4518+ virBufferAddLit(&opt, "ccid-card-emulated,backend=nss-emulated");
4519+ break;
4520+
4521+ case VIR_DOMAIN_SMARTCARD_TYPE_HOST_CERTIFICATES:
4522+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
4523+ !qemuCapsGet(qemuCaps, QEMU_CAPS_CCID_EMULATED)) {
4524+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4525+ _("this QEMU binary lacks smartcard host "
4526+ "mode support"));
4527+ goto error;
4528+ }
4529+
4530+ virBufferAddLit(&opt, "ccid-card-emulated,backend=certificates");
4531+ for (j = 0; j < VIR_DOMAIN_SMARTCARD_NUM_CERTIFICATES; j++) {
4532+ if (strchr(smartcard->data.cert.file[j], ',')) {
4533+ virBufferFreeAndReset(&opt);
4534+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4535+ _("invalid certificate name: %s"),
4536+ smartcard->data.cert.file[j]);
4537+ goto error;
4538+ }
4539+ virBufferAsprintf(&opt, ",cert%d=%s", j + 1,
4540+ smartcard->data.cert.file[j]);
4541+ }
4542+ if (smartcard->data.cert.database) {
4543+ if (strchr(smartcard->data.cert.database, ',')) {
4544+ virBufferFreeAndReset(&opt);
4545+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4546+ _("invalid database name: %s"),
4547+ smartcard->data.cert.database);
4548+ goto error;
4549+ }
4550+ database = smartcard->data.cert.database;
4551+ } else {
4552+ database = VIR_DOMAIN_SMARTCARD_DEFAULT_DATABASE;
4553+ }
4554+ virBufferAsprintf(&opt, ",database=%s", database);
4555+ break;
4556+
4557+ case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
4558+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
4559+ !qemuCapsGet(qemuCaps, QEMU_CAPS_CCID_PASSTHRU)) {
4560+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4561+ _("this QEMU binary lacks smartcard "
4562+ "passthrough mode support"));
4563+ goto error;
4564+ }
4565+
4566+ virCommandAddArg(cmd, "-chardev");
4567+ if (!(devstr = qemuBuildChrChardevStr(&smartcard->data.passthru,
4568+ smartcard->info.alias,
4569+ qemuCaps))) {
4570+ virBufferFreeAndReset(&opt);
4571+ goto error;
4572+ }
4573+ virCommandAddArg(cmd, devstr);
4574+ VIR_FREE(devstr);
4575+
4576+ virBufferAsprintf(&opt, "ccid-card-passthru,chardev=char%s",
4577+ smartcard->info.alias);
4578+ break;
4579+
4580+ default:
4581+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
4582+ _("unexpected smartcard type %d"),
4583+ smartcard->type);
4584+ virBufferFreeAndReset(&opt);
4585+ goto error;
4586+ }
4587+ virCommandAddArg(cmd, "-device");
4588+ virBufferAsprintf(&opt, ",id=%s,bus=ccid0.0", smartcard->info.alias);
4589+ virCommandAddArgBuffer(cmd, &opt);
4590+ }
4591+
4592+ if (!def->nserials) {
4593+ /* If we have -device, then we set -nodefault already */
4594+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
4595+ virCommandAddArgList(cmd, "-serial", "none", NULL);
4596+ } else {
4597+ for (i = 0 ; i < def->nserials ; i++) {
4598+ virDomainChrDefPtr serial = def->serials[i];
4599+ char *devstr;
4600+
4601+ /* Use -chardev with -device if they are available */
4602+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) &&
4603+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4604+ virCommandAddArg(cmd, "-chardev");
4605+ if (!(devstr = qemuBuildChrChardevStr(&serial->source,
4606+ serial->info.alias,
4607+ qemuCaps)))
4608+ goto error;
4609+ virCommandAddArg(cmd, devstr);
4610+ VIR_FREE(devstr);
4611+
4612+ virCommandAddArg(cmd, "-device");
4613+ virCommandAddArgFormat(cmd, "isa-serial,chardev=char%s,id=%s",
4614+ serial->info.alias, serial->info.alias);
4615+ } else {
4616+ virCommandAddArg(cmd, "-serial");
4617+ if (!(devstr = qemuBuildChrArgStr(&serial->source, NULL)))
4618+ goto error;
4619+ virCommandAddArg(cmd, devstr);
4620+ VIR_FREE(devstr);
4621+ }
4622+ }
4623+ }
4624+
4625+ if (!def->nparallels) {
4626+ /* If we have -device, then we set -nodefault already */
4627+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE))
4628+ virCommandAddArgList(cmd, "-parallel", "none", NULL);
4629+ } else {
4630+ for (i = 0 ; i < def->nparallels ; i++) {
4631+ virDomainChrDefPtr parallel = def->parallels[i];
4632+ char *devstr;
4633+
4634+ /* Use -chardev with -device if they are available */
4635+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) &&
4636+ qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4637+ virCommandAddArg(cmd, "-chardev");
4638+ if (!(devstr = qemuBuildChrChardevStr(&parallel->source,
4639+ parallel->info.alias,
4640+ qemuCaps)))
4641+ goto error;
4642+ virCommandAddArg(cmd, devstr);
4643+ VIR_FREE(devstr);
4644+
4645+ virCommandAddArg(cmd, "-device");
4646+ virCommandAddArgFormat(cmd, "isa-parallel,chardev=char%s,id=%s",
4647+ parallel->info.alias,
4648+ parallel->info.alias);
4649+ } else {
4650+ virCommandAddArg(cmd, "-parallel");
4651+ if (!(devstr = qemuBuildChrArgStr(&parallel->source, NULL)))
4652+ goto error;
4653+ virCommandAddArg(cmd, devstr);
4654+ VIR_FREE(devstr);
4655+ }
4656+ }
4657+ }
4658+
4659+ for (i = 0 ; i < def->nchannels ; i++) {
4660+ virDomainChrDefPtr channel = def->channels[i];
4661+ char *devstr;
4662+ char *addr;
4663+ int port;
4664+
4665+ switch(channel->targetType) {
4666+ case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_GUESTFWD:
4667+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV) ||
4668+ !qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4669+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4670+ "%s", _("guestfwd requires QEMU to support -chardev & -device"));
4671+ goto error;
4672+ }
4673+
4674+ virCommandAddArg(cmd, "-chardev");
4675+ if (!(devstr = qemuBuildChrChardevStr(&channel->source,
4676+ channel->info.alias,
4677+ qemuCaps)))
4678+ goto error;
4679+ virCommandAddArg(cmd, devstr);
4680+ VIR_FREE(devstr);
4681+
4682+ addr = virSocketAddrFormat(channel->target.addr);
4683+ if (!addr)
4684+ goto error;
4685+ port = virSocketAddrGetPort(channel->target.addr);
4686+
4687+ virCommandAddArg(cmd, "-netdev");
4688+ virCommandAddArgFormat(cmd,
4689+ "user,guestfwd=tcp:%s:%i,chardev=char%s,id=user-%s",
4690+ addr, port, channel->info.alias,
4691+ channel->info.alias);
4692+ VIR_FREE(addr);
4693+ break;
4694+
4695+ case VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO:
4696+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4697+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4698+ _("virtio channel requires QEMU to support -device"));
4699+ goto error;
4700+ }
4701+
4702+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE_SPICEVMC) &&
4703+ channel->source.type == VIR_DOMAIN_CHR_TYPE_SPICEVMC) {
4704+ /* spicevmc was originally introduced via a -device
4705+ * with a backend internal to qemu; although we prefer
4706+ * the newer -chardev interface. */
4707+ ;
4708+ } else {
4709+ virCommandAddArg(cmd, "-chardev");
4710+ if (!(devstr = qemuBuildChrChardevStr(&channel->source,
4711+ channel->info.alias,
4712+ qemuCaps)))
4713+ goto error;
4714+ virCommandAddArg(cmd, devstr);
4715+ VIR_FREE(devstr);
4716+ }
4717+
4718+ virCommandAddArg(cmd, "-device");
4719+ if (!(devstr = qemuBuildVirtioSerialPortDevStr(channel,
4720+ qemuCaps)))
4721+ goto error;
4722+ virCommandAddArg(cmd, devstr);
4723+ VIR_FREE(devstr);
4724+ break;
4725+ }
4726+ }
4727+
4728+ /* Explicit console devices */
4729+ for (i = 0 ; i < def->nconsoles ; i++) {
4730+ virDomainChrDefPtr console = def->consoles[i];
4731+ char *devstr;
4732+
4733+ switch(console->targetType) {
4734+ case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_VIRTIO:
4735+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4736+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4737+ _("virtio channel requires QEMU to support -device"));
4738+ goto error;
4739+ }
4740+
4741+ virCommandAddArg(cmd, "-chardev");
4742+ if (!(devstr = qemuBuildChrChardevStr(&console->source,
4743+ console->info.alias,
4744+ qemuCaps)))
4745+ goto error;
4746+ virCommandAddArg(cmd, devstr);
4747+ VIR_FREE(devstr);
4748+
4749+ virCommandAddArg(cmd, "-device");
4750+ if (!(devstr = qemuBuildVirtioSerialPortDevStr(console,
4751+ qemuCaps)))
4752+ goto error;
4753+ virCommandAddArg(cmd, devstr);
4754+ VIR_FREE(devstr);
4755+ break;
4756+
4757+ case VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL:
4758+ break;
4759+
4760+ default:
4761+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4762+ _("unsupported console target type %s"),
4763+ NULLSTR(virDomainChrConsoleTargetTypeToString(console->targetType)));
4764+ goto error;
4765+ }
4766+ }
4767+
4768+ if (usbcontroller == 0)
4769+ virCommandAddArg(cmd, "-usb");
4770+
4771+ for (i = 0 ; i < def->nhubs ; i++) {
4772+ virDomainHubDefPtr hub = def->hubs[i];
4773+ char *optstr;
4774+
4775+ virCommandAddArg(cmd, "-device");
4776+ if (!(optstr = qemuBuildHubDevStr(hub, qemuCaps)))
4777+ goto error;
4778+ virCommandAddArg(cmd, optstr);
4779+ VIR_FREE(optstr);
4780+ }
4781+
4782+ for (i = 0 ; i < def->ninputs ; i++) {
4783+ virDomainInputDefPtr input = def->inputs[i];
4784+
4785+ if (input->bus == VIR_DOMAIN_INPUT_BUS_USB) {
4786+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
4787+ char *optstr;
4788+ virCommandAddArg(cmd, "-device");
4789+ if (!(optstr = qemuBuildUSBInputDevStr(input, qemuCaps)))
4790+ goto error;
4791+ virCommandAddArg(cmd, optstr);
4792+ VIR_FREE(optstr);
4793+ } else {
4794+ virCommandAddArgList(cmd, "-usbdevice",
4795+ input->type == VIR_DOMAIN_INPUT_TYPE_MOUSE
4796+ ? "mouse" : "tablet", NULL);
4797+ }
4798+ }
4799+ }
4800+
4801+ if (def->ngraphics > 1) {
4802+ qemuReportError(VIR_ERR_INTERNAL_ERROR,
4803+ "%s", _("only 1 graphics device is supported"));
4804+ goto error;
4805+ }
4806+
4807+ if ((def->ngraphics == 1) &&
4808+ def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
4809+ virBuffer opt = VIR_BUFFER_INITIALIZER;
4810+
4811+ if (def->graphics[0]->data.vnc.socket ||
4812+ driver->vncAutoUnixSocket) {
4813+
4814+ if (!def->graphics[0]->data.vnc.socket &&
4815+ virAsprintf(&def->graphics[0]->data.vnc.socket,
4816+ "%s/%s.vnc", driver->libDir, def->name) == -1) {
4817+ goto no_memory;
4818+ }
4819+
4820+ virBufferAsprintf(&opt, "unix:%s",
4821+ def->graphics[0]->data.vnc.socket);
4822+
4823+ } else if (qemuCapsGet(qemuCaps, QEMU_CAPS_VNC_COLON)) {
4824+ const char *listenNetwork;
4825+ const char *listenAddr = NULL;
4826+ char *netAddr = NULL;
4827+ bool escapeAddr;
4828+ int ret;
4829+
4830+ switch (virDomainGraphicsListenGetType(def->graphics[0], 0)) {
4831+ case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS:
4832+ listenAddr = virDomainGraphicsListenGetAddress(def->graphics[0], 0);
4833+ break;
4834+
4835+ case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK:
4836+ listenNetwork = virDomainGraphicsListenGetNetwork(def->graphics[0], 0);
4837+ if (!listenNetwork)
4838+ break;
4839+ ret = networkGetNetworkAddress(listenNetwork, &netAddr);
4840+ if (ret <= -2) {
4841+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4842+ "%s", _("network-based listen not possible, "
4843+ "network driver not present"));
4844+ goto error;
4845+ }
4846+ if (ret < 0) {
4847+ qemuReportError(VIR_ERR_XML_ERROR,
4848+ _("listen network '%s' had no usable address"),
4849+ listenNetwork);
4850+ goto error;
4851+ }
4852+ listenAddr = netAddr;
4853+ /* store the address we found in the <graphics> element so it will
4854+ * show up in status. */
4855+ if (virDomainGraphicsListenSetAddress(def->graphics[0], 0,
4856+ listenAddr, -1, false) < 0)
4857+ goto error;
4858+ break;
4859+ }
4860+
4861+ if (!listenAddr)
4862+ listenAddr = driver->vncListen;
4863+
4864+ escapeAddr = strchr(listenAddr, ':') != NULL;
4865+ if (escapeAddr)
4866+ virBufferAsprintf(&opt, "[%s]", listenAddr);
4867+ else
4868+ virBufferAdd(&opt, listenAddr, -1);
4869+ virBufferAsprintf(&opt, ":%d",
4870+ def->graphics[0]->data.vnc.port - 5900);
4871+
4872+ VIR_FREE(netAddr);
4873+ } else {
4874+ virBufferAsprintf(&opt, "%d",
4875+ def->graphics[0]->data.vnc.port - 5900);
4876+ }
4877+
4878+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_VNC_COLON)) {
4879+ if (def->graphics[0]->data.vnc.auth.passwd ||
4880+ driver->vncPassword)
4881+ virBufferAddLit(&opt, ",password");
4882+
4883+ if (driver->vncTLS) {
4884+ virBufferAddLit(&opt, ",tls");
4885+ if (driver->vncTLSx509verify) {
4886+ virBufferAsprintf(&opt, ",x509verify=%s",
4887+ driver->vncTLSx509certdir);
4888+ } else {
4889+ virBufferAsprintf(&opt, ",x509=%s",
4890+ driver->vncTLSx509certdir);
4891+ }
4892+ }
4893+
4894+ if (driver->vncSASL) {
4895+ virBufferAddLit(&opt, ",sasl");
4896+
4897+ if (driver->vncSASLdir)
4898+ virCommandAddEnvPair(cmd, "SASL_CONF_DIR",
4899+ driver->vncSASLdir);
4900+
4901+ /* TODO: Support ACLs later */
4902+ }
4903+ }
4904+
4905+ virCommandAddArg(cmd, "-vnc");
4906+ virCommandAddArgBuffer(cmd, &opt);
4907+ if (def->graphics[0]->data.vnc.keymap) {
4908+ virCommandAddArgList(cmd, "-k", def->graphics[0]->data.vnc.keymap,
4909+ NULL);
4910+ }
4911+
4912+ /* Unless user requested it, set the audio backend to none, to
4913+ * prevent it opening the host OS audio devices, since that causes
4914+ * security issues and might not work when using VNC.
4915+ */
4916+ if (driver->vncAllowHostAudio) {
4917+ virCommandAddEnvPass(cmd, "QEMU_AUDIO_DRV");
4918+ } else {
4919+ virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=none");
4920+ }
4921+ } else if ((def->ngraphics == 1) &&
4922+ def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
4923+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_0_10) &&
4924+ !qemuCapsGet(qemuCaps, QEMU_CAPS_SDL)) {
4925+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4926+ _("sdl not supported by '%s'"),
4927+ def->emulator);
4928+ goto error;
4929+ }
4930+
4931+ if (def->graphics[0]->data.sdl.xauth)
4932+ virCommandAddEnvPair(cmd, "XAUTHORITY",
4933+ def->graphics[0]->data.sdl.xauth);
4934+ if (def->graphics[0]->data.sdl.display)
4935+ virCommandAddEnvPair(cmd, "DISPLAY",
4936+ def->graphics[0]->data.sdl.display);
4937+ if (def->graphics[0]->data.sdl.fullscreen)
4938+ virCommandAddArg(cmd, "-full-screen");
4939+
4940+ /* If using SDL for video, then we should just let it
4941+ * use QEMU's host audio drivers, possibly SDL too
4942+ * User can set these two before starting libvirtd
4943+ */
4944+ virCommandAddEnvPass(cmd, "QEMU_AUDIO_DRV");
4945+ virCommandAddEnvPass(cmd, "SDL_AUDIODRIVER");
4946+
4947+ /* New QEMU has this flag to let us explicitly ask for
4948+ * SDL graphics. This is better than relying on the
4949+ * default, since the default changes :-( */
4950+ if (qemuCapsGet(qemuCaps, QEMU_CAPS_SDL))
4951+ virCommandAddArg(cmd, "-sdl");
4952+
4953+ } else if ((def->ngraphics == 1) &&
4954+ def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE) {
4955+ virBuffer opt = VIR_BUFFER_INITIALIZER;
4956+ const char *listenNetwork;
4957+ const char *listenAddr = NULL;
4958+ char *netAddr = NULL;
4959+ int ret;
4960+
4961+ if (!qemuCapsGet(qemuCaps, QEMU_CAPS_SPICE)) {
4962+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4963+ _("spice graphics are not supported with this QEMU"));
4964+ goto error;
4965+ }
4966+
4967+ virBufferAsprintf(&opt, "port=%u", def->graphics[0]->data.spice.port);
4968+
4969+ if (driver->spiceTLS && def->graphics[0]->data.spice.tlsPort != -1)
4970+ virBufferAsprintf(&opt, ",tls-port=%u", def->graphics[0]->data.spice.tlsPort);
4971+
4972+ switch (virDomainGraphicsListenGetType(def->graphics[0], 0)) {
4973+ case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS:
4974+ listenAddr = virDomainGraphicsListenGetAddress(def->graphics[0], 0);
4975+ break;
4976+
4977+ case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK:
4978+ listenNetwork = virDomainGraphicsListenGetNetwork(def->graphics[0], 0);
4979+ if (!listenNetwork)
4980+ break;
4981+ ret = networkGetNetworkAddress(listenNetwork, &netAddr);
4982+ if (ret <= -2) {
4983+ qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4984+ "%s", _("network-based listen not possible, "
4985+ "network driver not present"));
4986+ goto error;
4987+ }
4988+ if (ret < 0) {
4989+ qemuReportError(VIR_ERR_XML_ERROR,
4990+ _("listen network '%s' had no usable address"),
4991+ listenNetwork);
4992+ goto error;
4993+ }
4994+ listenAddr = netAddr;
4995+ /* store the address we found in the <graphics> element so it will
4996+ * show up in status. */
4997+ if (virDomainGraphicsListenSetAddress(def->graphics[0], 0,
4998+ listenAddr, -1, false) < 0)
4999+ goto error;
5000+ break;
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches