2 * qemu_process.c: QEMU process management
4 * Copyright (C) 2006-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
28 #if defined(__linux__)
29 # include <linux/capability.h>
30 #elif defined(__FreeBSD__)
31 # include <sys/param.h>
32 # include <sys/cpuset.h>
35 #include <sys/utsname.h>
41 #include "qemu_process.h"
42 #define LIBVIRT_QEMU_PROCESSPRIV_H_ALLOW
43 #include "qemu_processpriv.h"
44 #include "qemu_alias.h"
45 #include "qemu_block.h"
46 #include "qemu_domain.h"
47 #include "qemu_domain_address.h"
48 #include "qemu_cgroup.h"
49 #include "qemu_capabilities.h"
50 #include "qemu_monitor.h"
51 #include "qemu_command.h"
52 #include "qemu_hostdev.h"
53 #include "qemu_hotplug.h"
54 #include "qemu_migration.h"
55 #include "qemu_migration_params.h"
56 #include "qemu_interface.h"
57 #include "qemu_security.h"
58 #include "qemu_extdevice.h"
59 #include "qemu_firmware.h"
62 #include "cpu/cpu_x86.h"
63 #include "datatypes.h"
69 #include "virpidfile.h"
70 #include "virhostcpu.h"
71 #include "domain_audit.h"
72 #include "domain_nwfilter.h"
73 #include "locking/domain_lock.h"
75 #include "virprocess.h"
77 #include "virnetdevtap.h"
78 #include "virnetdevopenvswitch.h"
79 #include "virnetdevmidonet.h"
80 #include "virbitmap.h"
81 #include "viratomic.h"
83 #include "virstring.h"
84 #include "virhostdev.h"
85 #include "secret_util.h"
86 #include "configmake.h"
87 #include "nwfilter_conf.h"
88 #include "netdev_bandwidth_conf.h"
89 #include "virresctrl.h"
91 #include "viridentity.h"
92 #include "backup_conf.h"
94 #define VIR_FROM_THIS VIR_FROM_QEMU
96 VIR_LOG_INIT("qemu.qemu_process");
99 * qemuProcessRemoveDomainStatus
101 * remove all state files of a domain from statedir
103 * Returns 0 on success
106 qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver
,
111 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
112 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
115 if (virAsprintf(&file
, "%s/%s.xml", cfg
->stateDir
, vm
->def
->name
) < 0)
118 if (unlink(file
) < 0 && errno
!= ENOENT
&& errno
!= ENOTDIR
)
119 VIR_WARN("Failed to remove domain XML for %s: %s",
120 vm
->def
->name
, virStrerror(errno
, ebuf
, sizeof(ebuf
)));
124 unlink(priv
->pidfile
) < 0 &&
126 VIR_WARN("Failed to remove PID file for %s: %s",
127 vm
->def
->name
, virStrerror(errno
, ebuf
, sizeof(ebuf
)));
137 * This is a callback registered with a qemuAgentPtr instance,
138 * and to be invoked when the agent console hits an end of file
139 * condition, or error, thus indicating VM shutdown should be
143 qemuProcessHandleAgentEOF(qemuAgentPtr agent
,
146 qemuDomainObjPrivatePtr priv
;
148 VIR_DEBUG("Received EOF from agent on %p '%s'", vm
, vm
->def
->name
);
152 priv
= vm
->privateData
;
155 VIR_DEBUG("Agent freed already");
159 if (priv
->beingDestroyed
) {
160 VIR_DEBUG("Domain is being destroyed, agent EOF is expected");
164 qemuAgentClose(agent
);
166 priv
->agentError
= false;
178 * This is invoked when there is some kind of error
179 * parsing data to/from the agent. The VM can continue
180 * to run, but no further agent commands will be
184 qemuProcessHandleAgentError(qemuAgentPtr agent ATTRIBUTE_UNUSED
,
187 qemuDomainObjPrivatePtr priv
;
189 VIR_DEBUG("Received error from agent on %p '%s'", vm
, vm
->def
->name
);
193 priv
= vm
->privateData
;
195 priv
->agentError
= true;
200 static void qemuProcessHandleAgentDestroy(qemuAgentPtr agent
,
203 VIR_DEBUG("Received destroy agent=%p vm=%p", agent
, vm
);
209 static qemuAgentCallbacks agentCallbacks
= {
210 .destroy
= qemuProcessHandleAgentDestroy
,
211 .eofNotify
= qemuProcessHandleAgentEOF
,
212 .errorNotify
= qemuProcessHandleAgentError
,
217 qemuConnectAgent(virQEMUDriverPtr driver
, virDomainObjPtr vm
)
219 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
220 qemuAgentPtr agent
= NULL
;
221 virDomainChrDefPtr config
= qemuFindAgentConfig(vm
->def
);
229 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_VSERPORT_CHANGE
) &&
230 config
->state
!= VIR_DOMAIN_CHR_DEVICE_STATE_CONNECTED
) {
231 VIR_DEBUG("Deferring connecting to guest agent");
235 if (qemuSecuritySetDaemonSocketLabel(driver
->securityManager
, vm
->def
) < 0) {
236 VIR_ERROR(_("Failed to set security context for agent for %s"),
241 /* Hold an extra reference because we can't allow 'vm' to be
242 * deleted while the agent is active */
247 agent
= qemuAgentOpen(vm
,
256 if (!virDomainObjIsActive(vm
)) {
257 qemuAgentClose(agent
);
258 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
259 _("guest crashed while connecting to the guest agent"));
263 if (qemuSecurityClearSocketLabel(driver
->securityManager
, vm
->def
) < 0) {
264 VIR_ERROR(_("Failed to clear security context for agent for %s"),
266 qemuAgentClose(agent
);
272 VIR_INFO("Failed to connect agent for %s", vm
->def
->name
);
276 VIR_WARN("Cannot connect to QEMU guest agent for %s", vm
->def
->name
);
277 priv
->agentError
= true;
286 * This is a callback registered with a qemuMonitorPtr instance,
287 * and to be invoked when the monitor console hits an end of file
288 * condition, or error, thus indicating VM shutdown should be
292 qemuProcessHandleMonitorEOF(qemuMonitorPtr mon
,
296 virQEMUDriverPtr driver
= opaque
;
297 qemuDomainObjPrivatePtr priv
;
298 struct qemuProcessEvent
*processEvent
;
302 VIR_DEBUG("Received EOF on %p '%s'", vm
, vm
->def
->name
);
304 priv
= vm
->privateData
;
305 if (priv
->beingDestroyed
) {
306 VIR_DEBUG("Domain is being destroyed, EOF is expected");
310 if (VIR_ALLOC(processEvent
) < 0)
313 processEvent
->eventType
= QEMU_PROCESS_EVENT_MONITOR_EOF
;
314 processEvent
->vm
= virObjectRef(vm
);
316 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
317 ignore_value(virObjectUnref(vm
));
318 qemuProcessEventFree(processEvent
);
322 /* We don't want this EOF handler to be called over and over while the
323 * thread is waiting for a job.
325 qemuMonitorUnregister(mon
);
327 /* We don't want any cleanup from EOF handler (or any other
328 * thread) to enter qemu namespace. */
329 qemuDomainDestroyNamespace(driver
, vm
);
337 * This is invoked when there is some kind of error
338 * parsing data to/from the monitor. The VM can continue
339 * to run, but no further monitor commands will be
343 qemuProcessHandleMonitorError(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
347 virQEMUDriverPtr driver
= opaque
;
348 virObjectEventPtr event
= NULL
;
350 VIR_DEBUG("Received error on %p '%s'", vm
, vm
->def
->name
);
354 ((qemuDomainObjPrivatePtr
) vm
->privateData
)->monError
= true;
355 event
= virDomainEventControlErrorNewFromObj(vm
);
356 virObjectEventStateQueue(driver
->domainEventState
, event
);
363 * qemuProcessFindDomainDiskByAliasOrQOM:
364 * @vm: domain object to search for the disk
365 * @alias: -drive or -device alias of the disk
366 * @qomid: QOM tree device name
368 * Looks up a disk in the domain definition of @vm which either matches the
369 * -drive or -device alias used for the backend and frontend respectively or the
370 * QOM name. If @alias is empty it's treated as NULL as it's a mandatory field
373 * Returns a disk from @vm or NULL if it could not be found.
376 qemuProcessFindDomainDiskByAliasOrQOM(virDomainObjPtr vm
,
382 if (alias
&& *alias
== '\0')
386 alias
= qemuAliasDiskDriveSkipPrefix(alias
);
388 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
389 virDomainDiskDefPtr disk
= vm
->def
->disks
[i
];
390 qemuDomainDiskPrivatePtr diskPriv
= QEMU_DOMAIN_DISK_PRIVATE(disk
);
392 if ((disk
->info
.alias
&& STREQ_NULLABLE(disk
->info
.alias
, alias
)) ||
393 (diskPriv
->qomName
&& STREQ_NULLABLE(diskPriv
->qomName
, qomid
)))
397 virReportError(VIR_ERR_INTERNAL_ERROR
,
398 _("no disk found with alias '%s' or id '%s'"),
399 NULLSTR(alias
), NULLSTR(qomid
));
405 qemuProcessHandleReset(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
409 virQEMUDriverPtr driver
= opaque
;
410 virObjectEventPtr event
;
411 qemuDomainObjPrivatePtr priv
;
412 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
417 event
= virDomainEventRebootNewFromObj(vm
);
418 priv
= vm
->privateData
;
420 qemuAgentNotifyEvent(priv
->agent
, QEMU_AGENT_EVENT_RESET
);
422 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0)
423 VIR_WARN("Failed to save status on vm %s", vm
->def
->name
);
425 if (vm
->def
->onReboot
== VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY
||
426 vm
->def
->onReboot
== VIR_DOMAIN_LIFECYCLE_ACTION_PRESERVE
) {
428 if (qemuDomainObjBeginJob(driver
, vm
, QEMU_JOB_MODIFY
) < 0)
431 if (!virDomainObjIsActive(vm
)) {
432 VIR_DEBUG("Ignoring RESET event from inactive domain %s",
437 qemuProcessStop(driver
, vm
, VIR_DOMAIN_SHUTOFF_DESTROYED
,
438 QEMU_ASYNC_JOB_NONE
, 0);
439 virDomainAuditStop(vm
, "destroyed");
440 qemuDomainRemoveInactive(driver
, vm
);
442 qemuDomainObjEndJob(driver
, vm
);
448 virObjectEventStateQueue(driver
->domainEventState
, event
);
455 * Since we have the '-no-shutdown' flag set, the
456 * QEMU process will currently have guest OS shutdown
457 * and the CPUS stopped. To fake the reboot, we thus
458 * want todo a reset of the virtual hardware, followed
459 * by restart of the CPUs. This should result in the
460 * guest OS booting up again
463 qemuProcessFakeReboot(void *opaque
)
465 virDomainObjPtr vm
= opaque
;
466 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
467 virQEMUDriverPtr driver
= priv
->driver
;
468 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
469 virDomainRunningReason reason
= VIR_DOMAIN_RUNNING_BOOTED
;
472 VIR_DEBUG("vm=%p", vm
);
474 if (qemuDomainObjBeginJob(driver
, vm
, QEMU_JOB_MODIFY
) < 0)
477 if (!virDomainObjIsActive(vm
)) {
478 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
479 _("guest unexpectedly quit"));
483 qemuDomainObjEnterMonitor(driver
, vm
);
484 rc
= qemuMonitorSystemReset(priv
->mon
);
486 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
492 if (virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_CRASHED
)
493 reason
= VIR_DOMAIN_RUNNING_CRASHED
;
495 if (qemuProcessStartCPUs(driver
, vm
,
497 QEMU_ASYNC_JOB_NONE
) < 0) {
498 if (virGetLastErrorCode() == VIR_ERR_OK
)
499 virReportError(VIR_ERR_INTERNAL_ERROR
,
500 "%s", _("resume operation failed"));
504 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
505 VIR_WARN("Unable to save status on vm %s after state change",
512 qemuDomainObjEndJob(driver
, vm
);
516 ignore_value(qemuProcessKill(vm
, VIR_QEMU_PROCESS_KILL_FORCE
));
517 virDomainObjEndAPI(&vm
);
523 qemuProcessShutdownOrReboot(virQEMUDriverPtr driver
,
526 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
528 if (priv
->fakeReboot
) {
529 qemuDomainSetFakeReboot(driver
, vm
, false);
532 if (virThreadCreate(&th
,
534 qemuProcessFakeReboot
,
536 VIR_ERROR(_("Failed to create reboot thread, killing domain"));
537 ignore_value(qemuProcessKill(vm
, VIR_QEMU_PROCESS_KILL_NOWAIT
));
541 ignore_value(qemuProcessKill(vm
, VIR_QEMU_PROCESS_KILL_NOWAIT
));
547 qemuProcessHandleEvent(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
549 const char *eventName
,
555 virQEMUDriverPtr driver
= opaque
;
556 virObjectEventPtr event
= NULL
;
558 VIR_DEBUG("vm=%p", vm
);
561 event
= virDomainQemuMonitorEventNew(vm
->def
->id
, vm
->def
->name
,
562 vm
->def
->uuid
, eventName
,
563 seconds
, micros
, details
);
566 virObjectEventStateQueue(driver
->domainEventState
, event
);
573 qemuProcessHandleShutdown(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
575 virTristateBool guest_initiated
,
578 virQEMUDriverPtr driver
= opaque
;
579 qemuDomainObjPrivatePtr priv
;
580 virObjectEventPtr event
= NULL
;
581 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
584 VIR_DEBUG("vm=%p", vm
);
588 priv
= vm
->privateData
;
589 if (virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_SHUTDOWN
) {
590 VIR_DEBUG("Ignoring repeated SHUTDOWN event from domain %s",
593 } else if (!virDomainObjIsActive(vm
)) {
594 VIR_DEBUG("Ignoring SHUTDOWN event from inactive domain %s",
599 VIR_DEBUG("Transitioned guest %s to shutdown state",
601 virDomainObjSetState(vm
,
603 VIR_DOMAIN_SHUTDOWN_UNKNOWN
);
605 switch (guest_initiated
) {
606 case VIR_TRISTATE_BOOL_YES
:
607 detail
= VIR_DOMAIN_EVENT_SHUTDOWN_GUEST
;
610 case VIR_TRISTATE_BOOL_NO
:
611 detail
= VIR_DOMAIN_EVENT_SHUTDOWN_HOST
;
614 case VIR_TRISTATE_BOOL_ABSENT
:
615 case VIR_TRISTATE_BOOL_LAST
:
617 detail
= VIR_DOMAIN_EVENT_SHUTDOWN_FINISHED
;
621 event
= virDomainEventLifecycleNewFromObj(vm
,
622 VIR_DOMAIN_EVENT_SHUTDOWN
,
625 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
626 VIR_WARN("Unable to save status on vm %s after state change",
631 qemuAgentNotifyEvent(priv
->agent
, QEMU_AGENT_EVENT_SHUTDOWN
);
633 qemuProcessShutdownOrReboot(driver
, vm
);
637 virObjectEventStateQueue(driver
->domainEventState
, event
);
645 qemuProcessHandleStop(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
649 virQEMUDriverPtr driver
= opaque
;
650 virObjectEventPtr event
= NULL
;
651 virDomainPausedReason reason
;
652 virDomainEventSuspendedDetailType detail
;
653 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
654 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
658 reason
= priv
->pausedReason
;
659 priv
->pausedReason
= VIR_DOMAIN_PAUSED_UNKNOWN
;
661 if (virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_RUNNING
) {
662 if (priv
->job
.asyncJob
== QEMU_ASYNC_JOB_MIGRATION_OUT
) {
663 if (priv
->job
.current
->status
== QEMU_DOMAIN_JOB_STATUS_POSTCOPY
)
664 reason
= VIR_DOMAIN_PAUSED_POSTCOPY
;
666 reason
= VIR_DOMAIN_PAUSED_MIGRATION
;
669 detail
= qemuDomainPausedReasonToSuspendedEvent(reason
);
670 VIR_DEBUG("Transitioned guest %s to paused state, "
671 "reason %s, event detail %d",
672 vm
->def
->name
, virDomainPausedReasonTypeToString(reason
),
675 if (priv
->job
.current
)
676 ignore_value(virTimeMillisNow(&priv
->job
.current
->stopped
));
678 if (priv
->signalStop
)
679 virDomainObjBroadcast(vm
);
681 virDomainObjSetState(vm
, VIR_DOMAIN_PAUSED
, reason
);
682 event
= virDomainEventLifecycleNewFromObj(vm
,
683 VIR_DOMAIN_EVENT_SUSPENDED
,
686 VIR_FREE(priv
->lockState
);
687 if (virDomainLockProcessPause(driver
->lockManager
, vm
, &priv
->lockState
) < 0)
688 VIR_WARN("Unable to release lease on %s", vm
->def
->name
);
689 VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv
->lockState
));
691 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
692 VIR_WARN("Unable to save status on vm %s after state change",
698 virObjectEventStateQueue(driver
->domainEventState
, event
);
706 qemuProcessHandleResume(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
710 virQEMUDriverPtr driver
= opaque
;
711 virObjectEventPtr event
= NULL
;
712 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
713 qemuDomainObjPrivatePtr priv
;
714 virDomainRunningReason reason
= VIR_DOMAIN_RUNNING_UNPAUSED
;
715 virDomainEventResumedDetailType eventDetail
;
719 priv
= vm
->privateData
;
720 if (priv
->runningReason
!= VIR_DOMAIN_RUNNING_UNKNOWN
) {
721 reason
= priv
->runningReason
;
722 priv
->runningReason
= VIR_DOMAIN_RUNNING_UNKNOWN
;
725 if (virDomainObjGetState(vm
, NULL
) != VIR_DOMAIN_RUNNING
) {
726 eventDetail
= qemuDomainRunningReasonToResumeEvent(reason
);
727 VIR_DEBUG("Transitioned guest %s into running state, reason '%s', "
729 vm
->def
->name
, virDomainRunningReasonTypeToString(reason
),
732 virDomainObjSetState(vm
, VIR_DOMAIN_RUNNING
, reason
);
733 event
= virDomainEventLifecycleNewFromObj(vm
,
734 VIR_DOMAIN_EVENT_RESUMED
,
737 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
738 VIR_WARN("Unable to save status on vm %s after state change",
744 virObjectEventStateQueue(driver
->domainEventState
, event
);
750 qemuProcessHandleRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
755 virQEMUDriverPtr driver
= opaque
;
756 virObjectEventPtr event
= NULL
;
757 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
761 if (vm
->def
->clock
.offset
== VIR_DOMAIN_CLOCK_OFFSET_VARIABLE
) {
762 /* when a basedate is manually given on the qemu commandline
763 * rather than simply "-rtc base=utc", the offset sent by qemu
764 * in this event is *not* the new offset from UTC, but is
765 * instead the new offset from the *original basedate* +
766 * uptime. For example, if the original offset was 3600 and
767 * the guest clock has been advanced by 10 seconds, qemu will
768 * send "10" in the event - this means that the new offset
769 * from UTC is 3610, *not* 10. If the guest clock is advanced
770 * by another 10 seconds, qemu will now send "20" - i.e. each
771 * event is the sum of the most recent change and all previous
772 * changes since the domain was started. Fortunately, we have
773 * saved the initial offset in "adjustment0", so to arrive at
774 * the proper new "adjustment", we just add the most recent
775 * offset to adjustment0.
777 offset
+= vm
->def
->clock
.data
.variable
.adjustment0
;
778 vm
->def
->clock
.data
.variable
.adjustment
= offset
;
780 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0)
781 VIR_WARN("unable to save domain status with RTC change");
784 event
= virDomainEventRTCChangeNewFromObj(vm
, offset
);
788 virObjectEventStateQueue(driver
->domainEventState
, event
);
795 qemuProcessHandleWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
800 virQEMUDriverPtr driver
= opaque
;
801 virObjectEventPtr watchdogEvent
= NULL
;
802 virObjectEventPtr lifecycleEvent
= NULL
;
803 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
806 watchdogEvent
= virDomainEventWatchdogNewFromObj(vm
, action
);
808 if (action
== VIR_DOMAIN_EVENT_WATCHDOG_PAUSE
&&
809 virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_RUNNING
) {
810 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
811 VIR_DEBUG("Transitioned guest %s to paused state due to watchdog", vm
->def
->name
);
813 virDomainObjSetState(vm
, VIR_DOMAIN_PAUSED
, VIR_DOMAIN_PAUSED_WATCHDOG
);
814 lifecycleEvent
= virDomainEventLifecycleNewFromObj(vm
,
815 VIR_DOMAIN_EVENT_SUSPENDED
,
816 VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG
);
818 VIR_FREE(priv
->lockState
);
819 if (virDomainLockProcessPause(driver
->lockManager
, vm
, &priv
->lockState
) < 0)
820 VIR_WARN("Unable to release lease on %s", vm
->def
->name
);
821 VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv
->lockState
));
823 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
824 VIR_WARN("Unable to save status on vm %s after watchdog event",
829 if (vm
->def
->watchdog
->action
== VIR_DOMAIN_WATCHDOG_ACTION_DUMP
) {
830 struct qemuProcessEvent
*processEvent
;
831 if (VIR_ALLOC(processEvent
) == 0) {
832 processEvent
->eventType
= QEMU_PROCESS_EVENT_WATCHDOG
;
833 processEvent
->action
= VIR_DOMAIN_WATCHDOG_ACTION_DUMP
;
834 /* Hold an extra reference because we can't allow 'vm' to be
835 * deleted before handling watchdog event is finished.
837 processEvent
->vm
= virObjectRef(vm
);
838 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
839 if (!virObjectUnref(vm
))
841 qemuProcessEventFree(processEvent
);
848 virObjectEventStateQueue(driver
->domainEventState
, watchdogEvent
);
849 virObjectEventStateQueue(driver
->domainEventState
, lifecycleEvent
);
857 qemuProcessHandleIOError(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
859 const char *diskAlias
,
860 const char *nodename
,
865 virQEMUDriverPtr driver
= opaque
;
866 virObjectEventPtr ioErrorEvent
= NULL
;
867 virObjectEventPtr ioErrorEvent2
= NULL
;
868 virObjectEventPtr lifecycleEvent
= NULL
;
870 const char *devAlias
;
871 virDomainDiskDefPtr disk
;
872 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
876 if (*diskAlias
== '\0')
880 disk
= qemuProcessFindDomainDiskByAliasOrQOM(vm
, diskAlias
, NULL
);
882 disk
= qemuDomainDiskLookupByNodename(vm
->def
, nodename
, NULL
, NULL
);
887 srcPath
= virDomainDiskGetSource(disk
);
888 devAlias
= disk
->info
.alias
;
894 ioErrorEvent
= virDomainEventIOErrorNewFromObj(vm
, srcPath
, devAlias
, action
);
895 ioErrorEvent2
= virDomainEventIOErrorReasonNewFromObj(vm
, srcPath
, devAlias
, action
, reason
);
897 if (action
== VIR_DOMAIN_EVENT_IO_ERROR_PAUSE
&&
898 virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_RUNNING
) {
899 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
900 VIR_DEBUG("Transitioned guest %s to paused state due to IO error", vm
->def
->name
);
902 if (priv
->signalIOError
)
903 virDomainObjBroadcast(vm
);
905 virDomainObjSetState(vm
, VIR_DOMAIN_PAUSED
, VIR_DOMAIN_PAUSED_IOERROR
);
906 lifecycleEvent
= virDomainEventLifecycleNewFromObj(vm
,
907 VIR_DOMAIN_EVENT_SUSPENDED
,
908 VIR_DOMAIN_EVENT_SUSPENDED_IOERROR
);
910 VIR_FREE(priv
->lockState
);
911 if (virDomainLockProcessPause(driver
->lockManager
, vm
, &priv
->lockState
) < 0)
912 VIR_WARN("Unable to release lease on %s", vm
->def
->name
);
913 VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv
->lockState
));
915 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0)
916 VIR_WARN("Unable to save status on vm %s after IO error", vm
->def
->name
);
920 virObjectEventStateQueue(driver
->domainEventState
, ioErrorEvent
);
921 virObjectEventStateQueue(driver
->domainEventState
, ioErrorEvent2
);
922 virObjectEventStateQueue(driver
->domainEventState
, lifecycleEvent
);
928 qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
930 const char *diskAlias
,
936 qemuDomainObjPrivatePtr priv
;
937 virQEMUDriverPtr driver
= opaque
;
938 struct qemuProcessEvent
*processEvent
= NULL
;
939 virDomainDiskDefPtr disk
;
940 VIR_AUTOUNREF(qemuBlockJobDataPtr
) job
= NULL
;
945 priv
= vm
->privateData
;
947 /* with QEMU_CAPS_BLOCKDEV we handle block job events via JOB_STATUS_CHANGE */
948 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
))
951 VIR_DEBUG("Block job for device %s (domain: %p,%s) type %d status %d",
952 diskAlias
, vm
, vm
->def
->name
, type
, status
);
954 priv
= vm
->privateData
;
955 if (type
== VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP
&&
956 (!priv
->backup
|| priv
->backup
->type
== VIR_DOMAIN_BACKUP_TYPE_PULL
)) {
957 /* Event for canceling a pull-mode backup is side-effect that
958 * should not be forwarded on to user */
961 if (!(disk
= qemuProcessFindDomainDiskByAliasOrQOM(vm
, diskAlias
, NULL
)))
964 job
= qemuBlockJobDiskGetJob(disk
);
966 if (job
&& job
->synchronous
) {
967 /* We have a SYNC API waiting for this event, dispatch it back */
968 job
->newstate
= status
;
969 VIR_FREE(job
->errmsg
);
970 ignore_value(VIR_STRDUP_QUIET(job
->errmsg
, error
));
971 virDomainObjBroadcast(vm
);
973 /* there is no waiting SYNC API, dispatch the update to a thread */
974 if (VIR_ALLOC(processEvent
) < 0)
977 processEvent
->eventType
= QEMU_PROCESS_EVENT_BLOCK_JOB
;
978 if (VIR_STRDUP(data
, diskAlias
) < 0)
980 processEvent
->data
= data
;
981 processEvent
->vm
= virObjectRef(vm
);
982 processEvent
->action
= type
;
983 processEvent
->status
= status
;
985 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
986 ignore_value(virObjectUnref(vm
));
994 qemuProcessEventFree(processEvent
);
1001 qemuProcessHandleJobStatusChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1003 const char *jobname
,
1007 virQEMUDriverPtr driver
= opaque
;
1008 qemuDomainObjPrivatePtr priv
;
1009 struct qemuProcessEvent
*processEvent
= NULL
;
1010 qemuBlockJobDataPtr job
= NULL
;
1014 priv
= vm
->privateData
;
1016 VIR_DEBUG("job '%s'(domain: %p,%s) state changed to '%s'(%d)",
1017 jobname
, vm
, vm
->def
->name
,
1018 qemuMonitorJobStatusTypeToString(status
), status
);
1020 if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
)) {
1021 VIR_DEBUG("job '%s' handled by old blockjob handler", jobname
);
1025 if ((jobnewstate
= qemuBlockjobConvertMonitorStatus(status
)) == QEMU_BLOCKJOB_STATE_LAST
)
1028 if (!(job
= virHashLookup(priv
->blockjobs
, jobname
))) {
1029 VIR_DEBUG("job '%s' not registered", jobname
);
1033 job
->newstate
= jobnewstate
;
1035 if (job
->synchronous
) {
1036 VIR_DEBUG("job '%s' handled synchronously", jobname
);
1037 virDomainObjBroadcast(vm
);
1039 VIR_DEBUG("job '%s' handled by event thread", jobname
);
1040 if (VIR_ALLOC(processEvent
) < 0)
1043 processEvent
->eventType
= QEMU_PROCESS_EVENT_JOB_STATUS_CHANGE
;
1044 processEvent
->vm
= virObjectRef(vm
);
1045 processEvent
->data
= virObjectRef(job
);
1047 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1048 ignore_value(virObjectUnref(vm
));
1052 processEvent
= NULL
;
1056 qemuProcessEventFree(processEvent
);
1057 virObjectUnlock(vm
);
1063 qemuProcessHandleGraphics(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1067 const char *localNode
,
1068 const char *localService
,
1070 const char *remoteNode
,
1071 const char *remoteService
,
1072 const char *authScheme
,
1073 const char *x509dname
,
1074 const char *saslUsername
,
1077 virQEMUDriverPtr driver
= opaque
;
1078 virObjectEventPtr event
;
1079 virDomainEventGraphicsAddressPtr localAddr
= NULL
;
1080 virDomainEventGraphicsAddressPtr remoteAddr
= NULL
;
1081 virDomainEventGraphicsSubjectPtr subject
= NULL
;
1084 if (VIR_ALLOC(localAddr
) < 0)
1086 localAddr
->family
= localFamily
;
1087 if (VIR_STRDUP(localAddr
->service
, localService
) < 0 ||
1088 VIR_STRDUP(localAddr
->node
, localNode
) < 0)
1091 if (VIR_ALLOC(remoteAddr
) < 0)
1093 remoteAddr
->family
= remoteFamily
;
1094 if (VIR_STRDUP(remoteAddr
->service
, remoteService
) < 0 ||
1095 VIR_STRDUP(remoteAddr
->node
, remoteNode
) < 0)
1098 if (VIR_ALLOC(subject
) < 0)
1101 if (VIR_REALLOC_N(subject
->identities
, subject
->nidentity
+1) < 0)
1103 subject
->nidentity
++;
1104 if (VIR_STRDUP(subject
->identities
[subject
->nidentity
-1].type
, "x509dname") < 0 ||
1105 VIR_STRDUP(subject
->identities
[subject
->nidentity
-1].name
, x509dname
) < 0)
1109 if (VIR_REALLOC_N(subject
->identities
, subject
->nidentity
+1) < 0)
1111 subject
->nidentity
++;
1112 if (VIR_STRDUP(subject
->identities
[subject
->nidentity
-1].type
, "saslUsername") < 0 ||
1113 VIR_STRDUP(subject
->identities
[subject
->nidentity
-1].name
, saslUsername
) < 0)
1118 event
= virDomainEventGraphicsNewFromObj(vm
, phase
, localAddr
, remoteAddr
, authScheme
, subject
);
1119 virObjectUnlock(vm
);
1121 virObjectEventStateQueue(driver
->domainEventState
, event
);
1127 VIR_FREE(localAddr
->service
);
1128 VIR_FREE(localAddr
->node
);
1129 VIR_FREE(localAddr
);
1132 VIR_FREE(remoteAddr
->service
);
1133 VIR_FREE(remoteAddr
->node
);
1134 VIR_FREE(remoteAddr
);
1137 for (i
= 0; i
< subject
->nidentity
; i
++) {
1138 VIR_FREE(subject
->identities
[i
].type
);
1139 VIR_FREE(subject
->identities
[i
].name
);
1141 VIR_FREE(subject
->identities
);
1149 qemuProcessHandleTrayChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1151 const char *devAlias
,
1156 virQEMUDriverPtr driver
= opaque
;
1157 virObjectEventPtr event
= NULL
;
1158 virDomainDiskDefPtr disk
;
1159 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
1162 disk
= qemuProcessFindDomainDiskByAliasOrQOM(vm
, devAlias
, devid
);
1165 event
= virDomainEventTrayChangeNewFromObj(vm
, disk
->info
.alias
, reason
);
1166 /* Update disk tray status */
1167 if (reason
== VIR_DOMAIN_EVENT_TRAY_CHANGE_OPEN
)
1168 disk
->tray_status
= VIR_DOMAIN_DISK_TRAY_OPEN
;
1169 else if (reason
== VIR_DOMAIN_EVENT_TRAY_CHANGE_CLOSE
)
1170 disk
->tray_status
= VIR_DOMAIN_DISK_TRAY_CLOSED
;
1172 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
1173 VIR_WARN("Unable to save status on vm %s after tray moved event",
1177 virDomainObjBroadcast(vm
);
1180 virObjectUnlock(vm
);
1181 virObjectEventStateQueue(driver
->domainEventState
, event
);
1182 virObjectUnref(cfg
);
1187 qemuProcessHandlePMWakeup(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1191 virQEMUDriverPtr driver
= opaque
;
1192 virObjectEventPtr event
= NULL
;
1193 virObjectEventPtr lifecycleEvent
= NULL
;
1194 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
1197 event
= virDomainEventPMWakeupNewFromObj(vm
);
1199 /* Don't set domain status back to running if it wasn't paused
1200 * from guest side, otherwise it can just cause confusion.
1202 if (virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_PMSUSPENDED
) {
1203 VIR_DEBUG("Transitioned guest %s from pmsuspended to running "
1204 "state due to QMP wakeup event", vm
->def
->name
);
1206 virDomainObjSetState(vm
, VIR_DOMAIN_RUNNING
,
1207 VIR_DOMAIN_RUNNING_WAKEUP
);
1208 lifecycleEvent
= virDomainEventLifecycleNewFromObj(vm
,
1209 VIR_DOMAIN_EVENT_STARTED
,
1210 VIR_DOMAIN_EVENT_STARTED_WAKEUP
);
1212 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
1213 VIR_WARN("Unable to save status on vm %s after wakeup event",
1218 virObjectUnlock(vm
);
1219 virObjectEventStateQueue(driver
->domainEventState
, event
);
1220 virObjectEventStateQueue(driver
->domainEventState
, lifecycleEvent
);
1221 virObjectUnref(cfg
);
1226 qemuProcessHandlePMSuspend(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1230 virQEMUDriverPtr driver
= opaque
;
1231 virObjectEventPtr event
= NULL
;
1232 virObjectEventPtr lifecycleEvent
= NULL
;
1233 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
1236 event
= virDomainEventPMSuspendNewFromObj(vm
);
1238 if (virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_RUNNING
) {
1239 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
1240 VIR_DEBUG("Transitioned guest %s to pmsuspended state due to "
1241 "QMP suspend event", vm
->def
->name
);
1243 virDomainObjSetState(vm
, VIR_DOMAIN_PMSUSPENDED
,
1244 VIR_DOMAIN_PMSUSPENDED_UNKNOWN
);
1246 virDomainEventLifecycleNewFromObj(vm
,
1247 VIR_DOMAIN_EVENT_PMSUSPENDED
,
1248 VIR_DOMAIN_EVENT_PMSUSPENDED_MEMORY
);
1250 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
1251 VIR_WARN("Unable to save status on vm %s after suspend event",
1256 qemuAgentNotifyEvent(priv
->agent
, QEMU_AGENT_EVENT_SUSPEND
);
1259 virObjectUnlock(vm
);
1261 virObjectEventStateQueue(driver
->domainEventState
, event
);
1262 virObjectEventStateQueue(driver
->domainEventState
, lifecycleEvent
);
1263 virObjectUnref(cfg
);
1268 qemuProcessHandleBalloonChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1270 unsigned long long actual
,
1273 virQEMUDriverPtr driver
= opaque
;
1274 virObjectEventPtr event
= NULL
;
1275 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
1278 event
= virDomainEventBalloonChangeNewFromObj(vm
, actual
);
1280 VIR_DEBUG("Updating balloon from %lld to %lld kb",
1281 vm
->def
->mem
.cur_balloon
, actual
);
1282 vm
->def
->mem
.cur_balloon
= actual
;
1284 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0)
1285 VIR_WARN("unable to save domain status with balloon change");
1287 virObjectUnlock(vm
);
1289 virObjectEventStateQueue(driver
->domainEventState
, event
);
1290 virObjectUnref(cfg
);
1295 qemuProcessHandlePMSuspendDisk(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1299 virQEMUDriverPtr driver
= opaque
;
1300 virObjectEventPtr event
= NULL
;
1301 virObjectEventPtr lifecycleEvent
= NULL
;
1302 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
1305 event
= virDomainEventPMSuspendDiskNewFromObj(vm
);
1307 if (virDomainObjGetState(vm
, NULL
) == VIR_DOMAIN_RUNNING
) {
1308 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
1309 VIR_DEBUG("Transitioned guest %s to pmsuspended state due to "
1310 "QMP suspend_disk event", vm
->def
->name
);
1312 virDomainObjSetState(vm
, VIR_DOMAIN_PMSUSPENDED
,
1313 VIR_DOMAIN_PMSUSPENDED_UNKNOWN
);
1315 virDomainEventLifecycleNewFromObj(vm
,
1316 VIR_DOMAIN_EVENT_PMSUSPENDED
,
1317 VIR_DOMAIN_EVENT_PMSUSPENDED_DISK
);
1319 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
1320 VIR_WARN("Unable to save status on vm %s after suspend event",
1325 qemuAgentNotifyEvent(priv
->agent
, QEMU_AGENT_EVENT_SUSPEND
);
1328 virObjectUnlock(vm
);
1330 virObjectEventStateQueue(driver
->domainEventState
, event
);
1331 virObjectEventStateQueue(driver
->domainEventState
, lifecycleEvent
);
1332 virObjectUnref(cfg
);
1339 qemuProcessHandleGuestPanic(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1341 qemuMonitorEventPanicInfoPtr info
,
1344 virQEMUDriverPtr driver
= opaque
;
1345 struct qemuProcessEvent
*processEvent
;
1348 if (VIR_ALLOC(processEvent
) < 0)
1351 processEvent
->eventType
= QEMU_PROCESS_EVENT_GUESTPANIC
;
1352 processEvent
->action
= vm
->def
->onCrash
;
1353 processEvent
->data
= info
;
1354 /* Hold an extra reference because we can't allow 'vm' to be
1355 * deleted before handling guest panic event is finished.
1357 processEvent
->vm
= virObjectRef(vm
);
1359 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1360 if (!virObjectUnref(vm
))
1362 qemuProcessEventFree(processEvent
);
1367 virObjectUnlock(vm
);
1374 qemuProcessHandleDeviceDeleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1376 const char *devAlias
,
1379 virQEMUDriverPtr driver
= opaque
;
1380 struct qemuProcessEvent
*processEvent
= NULL
;
1385 VIR_DEBUG("Device %s removed from domain %p %s",
1386 devAlias
, vm
, vm
->def
->name
);
1388 if (qemuDomainSignalDeviceRemoval(vm
, devAlias
,
1389 QEMU_DOMAIN_UNPLUGGING_DEVICE_STATUS_OK
))
1392 if (VIR_ALLOC(processEvent
) < 0)
1395 processEvent
->eventType
= QEMU_PROCESS_EVENT_DEVICE_DELETED
;
1396 if (VIR_STRDUP(data
, devAlias
) < 0)
1398 processEvent
->data
= data
;
1399 processEvent
->vm
= virObjectRef(vm
);
1401 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1402 ignore_value(virObjectUnref(vm
));
1407 virObjectUnlock(vm
);
1410 qemuProcessEventFree(processEvent
);
1417 * Meaning of fields reported by the event according to the ACPI standard:
1419 * 0x00 - 0xff: Notification values, as passed at the request time
1420 * 0x100: Operating System Shutdown Processing
1421 * 0x103: Ejection processing
1422 * 0x200: Insertion processing
1423 * other values are reserved
1428 * 0x01: non-specific failure
1429 * 0x02: unrecognized notify code
1430 * 0x03 - 0x7f: reserved
1431 * other values are specific to the notification type (see below)
1433 * for the 0x100 source the following additional codes are standardized:
1434 * 0x80: OS Shutdown request denied
1435 * 0x81: OS Shutdown in progress
1436 * 0x82: OS Shutdown completed
1437 * 0x83: OS Graceful shutdown not supported
1438 * other higher values are reserved
1440 * for the 0x003 (Ejection request) and 0x103 (Ejection processing) source
1441 * the following additional codes are standardized:
1442 * 0x80: Device ejection not supported by OSPM
1443 * 0x81: Device in use by application
1445 * 0x83: Ejection dependency is busy or not supported for ejection by OSPM
1446 * 0x84: Ejection is in progress (pending)
1447 * other higher values are reserved
1449 * for the 0x200 source the following additional codes are standardized:
1450 * 0x80: Device insertion in progress (pending)
1451 * 0x81: Device driver load failure
1452 * 0x82: Device insertion not supported by OSPM
1453 * 0x83-0x8F: Reserved
1454 * 0x90-0x9F: Insertion failure - Resources Unavailable as described by the
1455 * following bit encodings:
1456 * Bit [3]: Bus or Segment Numbers
1457 * Bit [2]: Interrupts
1460 * other higher values are reserved
1462 * Other fields and semantics are specific to the qemu handling of the event.
1463 * - @alias may be NULL for successful unplug operations
1464 * - @slotType describes the device type a bit more closely, currently the
1465 * only known value is 'DIMM'
1466 * - @slot describes the specific device
1468 * Note that qemu does not emit the event for all the documented sources or
1472 qemuProcessHandleAcpiOstInfo(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1475 const char *slotType
,
1477 unsigned int source
,
1478 unsigned int status
,
1481 virQEMUDriverPtr driver
= opaque
;
1482 virObjectEventPtr event
= NULL
;
1486 VIR_DEBUG("ACPI OST info for device %s domain %p %s. "
1487 "slotType='%s' slot='%s' source=%u status=%u",
1488 NULLSTR(alias
), vm
, vm
->def
->name
, slotType
, slot
, source
, status
);
1493 if (STREQ(slotType
, "DIMM")) {
1494 if ((source
== 0x003 || source
== 0x103) &&
1495 (status
== 0x01 || (status
>= 0x80 && status
<= 0x83))) {
1496 qemuDomainSignalDeviceRemoval(vm
, alias
,
1497 QEMU_DOMAIN_UNPLUGGING_DEVICE_STATUS_GUEST_REJECTED
);
1499 event
= virDomainEventDeviceRemovalFailedNewFromObj(vm
, alias
);
1504 virObjectUnlock(vm
);
1505 virObjectEventStateQueue(driver
->domainEventState
, event
);
1512 qemuProcessHandleBlockThreshold(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1514 const char *nodename
,
1515 unsigned long long threshold
,
1516 unsigned long long excess
,
1519 virQEMUDriverPtr driver
= opaque
;
1520 virObjectEventPtr event
= NULL
;
1521 virDomainDiskDefPtr disk
;
1522 virStorageSourcePtr src
;
1525 const char *path
= NULL
;
1529 VIR_DEBUG("BLOCK_WRITE_THRESHOLD event for block node '%s' in domain %p %s:"
1530 "threshold '%llu' exceeded by '%llu'",
1531 nodename
, vm
, vm
->def
->name
, threshold
, excess
);
1533 if ((disk
= qemuDomainDiskLookupByNodename(vm
->def
, nodename
, &src
, &idx
))) {
1534 if (virStorageSourceIsLocalStorage(src
))
1537 if ((dev
= qemuDomainDiskBackingStoreGetName(disk
, src
, idx
))) {
1538 event
= virDomainEventBlockThresholdNewFromObj(vm
, dev
, path
,
1544 virObjectUnlock(vm
);
1545 virObjectEventStateQueue(driver
->domainEventState
, event
);
1552 qemuProcessHandleNicRxFilterChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1554 const char *devAlias
,
1557 virQEMUDriverPtr driver
= opaque
;
1558 struct qemuProcessEvent
*processEvent
= NULL
;
1563 VIR_DEBUG("Device %s RX Filter changed in domain %p %s",
1564 devAlias
, vm
, vm
->def
->name
);
1566 if (VIR_ALLOC(processEvent
) < 0)
1569 processEvent
->eventType
= QEMU_PROCESS_EVENT_NIC_RX_FILTER_CHANGED
;
1570 if (VIR_STRDUP(data
, devAlias
) < 0)
1572 processEvent
->data
= data
;
1573 processEvent
->vm
= virObjectRef(vm
);
1575 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1576 ignore_value(virObjectUnref(vm
));
1581 virObjectUnlock(vm
);
1584 qemuProcessEventFree(processEvent
);
1590 qemuProcessHandleSerialChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1592 const char *devAlias
,
1596 virQEMUDriverPtr driver
= opaque
;
1597 struct qemuProcessEvent
*processEvent
= NULL
;
1602 VIR_DEBUG("Serial port %s state changed to '%d' in domain %p %s",
1603 devAlias
, connected
, vm
, vm
->def
->name
);
1605 if (VIR_ALLOC(processEvent
) < 0)
1608 processEvent
->eventType
= QEMU_PROCESS_EVENT_SERIAL_CHANGED
;
1609 if (VIR_STRDUP(data
, devAlias
) < 0)
1611 processEvent
->data
= data
;
1612 processEvent
->action
= connected
;
1613 processEvent
->vm
= virObjectRef(vm
);
1615 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1616 ignore_value(virObjectUnref(vm
));
1621 virObjectUnlock(vm
);
1624 qemuProcessEventFree(processEvent
);
1630 qemuProcessHandleSpiceMigrated(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1632 void *opaque ATTRIBUTE_UNUSED
)
1634 qemuDomainObjPrivatePtr priv
;
1638 VIR_DEBUG("Spice migration completed for domain %p %s",
1641 priv
= vm
->privateData
;
1642 if (priv
->job
.asyncJob
!= QEMU_ASYNC_JOB_MIGRATION_OUT
) {
1643 VIR_DEBUG("got SPICE_MIGRATE_COMPLETED event without a migration job");
1647 priv
->job
.spiceMigrated
= true;
1648 virDomainObjBroadcast(vm
);
1651 virObjectUnlock(vm
);
1657 qemuProcessHandleMigrationStatus(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1662 qemuDomainObjPrivatePtr priv
;
1663 virQEMUDriverPtr driver
= opaque
;
1664 virObjectEventPtr event
= NULL
;
1665 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
1670 VIR_DEBUG("Migration of domain %p %s changed state to %s",
1672 qemuMonitorMigrationStatusTypeToString(status
));
1674 priv
= vm
->privateData
;
1675 if (priv
->job
.asyncJob
== QEMU_ASYNC_JOB_NONE
) {
1676 VIR_DEBUG("got MIGRATION event without a migration job");
1680 priv
->job
.current
->stats
.mig
.status
= status
;
1681 virDomainObjBroadcast(vm
);
1683 if (status
== QEMU_MONITOR_MIGRATION_STATUS_POSTCOPY
&&
1684 virDomainObjGetState(vm
, &reason
) == VIR_DOMAIN_PAUSED
&&
1685 reason
== VIR_DOMAIN_PAUSED_MIGRATION
) {
1686 VIR_DEBUG("Correcting paused state reason for domain %s to %s",
1688 virDomainPausedReasonTypeToString(VIR_DOMAIN_PAUSED_POSTCOPY
));
1690 virDomainObjSetState(vm
, VIR_DOMAIN_PAUSED
, VIR_DOMAIN_PAUSED_POSTCOPY
);
1691 event
= virDomainEventLifecycleNewFromObj(vm
,
1692 VIR_DOMAIN_EVENT_SUSPENDED
,
1693 VIR_DOMAIN_EVENT_SUSPENDED_POSTCOPY
);
1695 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0) {
1696 VIR_WARN("Unable to save status on vm %s after state change",
1702 virObjectUnlock(vm
);
1703 virObjectEventStateQueue(driver
->domainEventState
, event
);
1704 virObjectUnref(cfg
);
1710 qemuProcessHandleMigrationPass(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1715 virQEMUDriverPtr driver
= opaque
;
1716 qemuDomainObjPrivatePtr priv
;
1720 VIR_DEBUG("Migrating domain %p %s, iteration %d",
1721 vm
, vm
->def
->name
, pass
);
1723 priv
= vm
->privateData
;
1724 if (priv
->job
.asyncJob
== QEMU_ASYNC_JOB_NONE
) {
1725 VIR_DEBUG("got MIGRATION_PASS event without a migration job");
1729 virObjectEventStateQueue(driver
->domainEventState
,
1730 virDomainEventMigrationIterationNewFromObj(vm
, pass
));
1733 virObjectUnlock(vm
);
1739 qemuProcessHandleDumpCompleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1742 qemuMonitorDumpStatsPtr stats
,
1744 void *opaque ATTRIBUTE_UNUSED
)
1746 qemuDomainObjPrivatePtr priv
;
1750 VIR_DEBUG("Dump completed for domain %p %s with stats=%p error='%s'",
1751 vm
, vm
->def
->name
, stats
, NULLSTR(error
));
1753 priv
= vm
->privateData
;
1754 if (priv
->job
.asyncJob
== QEMU_ASYNC_JOB_NONE
) {
1755 VIR_DEBUG("got DUMP_COMPLETED event without a dump_completed job");
1758 priv
->job
.dumpCompleted
= true;
1759 priv
->job
.current
->stats
.dump
= *stats
;
1760 ignore_value(VIR_STRDUP_QUIET(priv
->job
.error
, error
));
1762 /* Force error if extracting the DUMP_COMPLETED status failed */
1763 if (!error
&& status
< 0) {
1764 ignore_value(VIR_STRDUP_QUIET(priv
->job
.error
, virGetLastErrorMessage()));
1765 priv
->job
.current
->stats
.dump
.status
= QEMU_MONITOR_DUMP_STATUS_FAILED
;
1768 virDomainObjBroadcast(vm
);
1771 virResetLastError();
1772 virObjectUnlock(vm
);
1778 qemuProcessHandlePRManagerStatusChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1780 const char *prManager
,
1784 virQEMUDriverPtr driver
= opaque
;
1785 qemuDomainObjPrivatePtr priv
;
1786 struct qemuProcessEvent
*processEvent
= NULL
;
1787 const char *managedAlias
= qemuDomainGetManagedPRAlias();
1792 VIR_DEBUG("pr-manager %s status changed for domain %p %s connected=%d",
1793 prManager
, vm
, vm
->def
->name
, connected
);
1796 /* Connect events are boring. */
1800 /* Disconnect events are more interesting. */
1802 if (STRNEQ(prManager
, managedAlias
)) {
1803 VIR_DEBUG("pr-manager %s not managed, ignoring event",
1809 priv
= vm
->privateData
;
1810 priv
->prDaemonRunning
= false;
1812 if (VIR_ALLOC(processEvent
) < 0)
1815 processEvent
->eventType
= QEMU_PROCESS_EVENT_PR_DISCONNECT
;
1816 processEvent
->vm
= virObjectRef(vm
);
1818 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1819 qemuProcessEventFree(processEvent
);
1826 virObjectUnlock(vm
);
1832 qemuProcessHandleRdmaGidStatusChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
1836 unsigned long long subnet_prefix
,
1837 unsigned long long interface_id
,
1840 virQEMUDriverPtr driver
= opaque
;
1841 struct qemuProcessEvent
*processEvent
= NULL
;
1842 qemuMonitorRdmaGidStatusPtr info
= NULL
;
1847 VIR_DEBUG("netdev=%s,gid_status=%d,subnet_prefix=0x%llx,interface_id=0x%llx",
1848 netdev
, gid_status
, subnet_prefix
, interface_id
);
1850 if (VIR_ALLOC(info
) < 0 ||
1851 VIR_STRDUP(info
->netdev
, netdev
) < 0)
1854 info
->gid_status
= gid_status
;
1855 info
->subnet_prefix
= subnet_prefix
;
1856 info
->interface_id
= interface_id
;
1858 if (VIR_ALLOC(processEvent
) < 0)
1861 processEvent
->eventType
= QEMU_PROCESS_EVENT_RDMA_GID_STATUS_CHANGED
;
1862 processEvent
->vm
= virObjectRef(vm
);
1863 VIR_STEAL_PTR(processEvent
->data
, info
);
1865 if (virThreadPoolSendJob(driver
->workerPool
, 0, processEvent
) < 0) {
1866 qemuProcessEventFree(processEvent
);
1873 qemuMonitorEventRdmaGidStatusFree(info
);
1874 virObjectUnlock(vm
);
1879 static qemuMonitorCallbacks monitorCallbacks
= {
1880 .eofNotify
= qemuProcessHandleMonitorEOF
,
1881 .errorNotify
= qemuProcessHandleMonitorError
,
1882 .domainEvent
= qemuProcessHandleEvent
,
1883 .domainShutdown
= qemuProcessHandleShutdown
,
1884 .domainStop
= qemuProcessHandleStop
,
1885 .domainResume
= qemuProcessHandleResume
,
1886 .domainReset
= qemuProcessHandleReset
,
1887 .domainRTCChange
= qemuProcessHandleRTCChange
,
1888 .domainWatchdog
= qemuProcessHandleWatchdog
,
1889 .domainIOError
= qemuProcessHandleIOError
,
1890 .domainGraphics
= qemuProcessHandleGraphics
,
1891 .domainBlockJob
= qemuProcessHandleBlockJob
,
1892 .jobStatusChange
= qemuProcessHandleJobStatusChange
,
1893 .domainTrayChange
= qemuProcessHandleTrayChange
,
1894 .domainPMWakeup
= qemuProcessHandlePMWakeup
,
1895 .domainPMSuspend
= qemuProcessHandlePMSuspend
,
1896 .domainBalloonChange
= qemuProcessHandleBalloonChange
,
1897 .domainPMSuspendDisk
= qemuProcessHandlePMSuspendDisk
,
1898 .domainGuestPanic
= qemuProcessHandleGuestPanic
,
1899 .domainDeviceDeleted
= qemuProcessHandleDeviceDeleted
,
1900 .domainNicRxFilterChanged
= qemuProcessHandleNicRxFilterChanged
,
1901 .domainSerialChange
= qemuProcessHandleSerialChanged
,
1902 .domainSpiceMigrated
= qemuProcessHandleSpiceMigrated
,
1903 .domainMigrationStatus
= qemuProcessHandleMigrationStatus
,
1904 .domainMigrationPass
= qemuProcessHandleMigrationPass
,
1905 .domainAcpiOstInfo
= qemuProcessHandleAcpiOstInfo
,
1906 .domainBlockThreshold
= qemuProcessHandleBlockThreshold
,
1907 .domainDumpCompleted
= qemuProcessHandleDumpCompleted
,
1908 .domainPRManagerStatusChanged
= qemuProcessHandlePRManagerStatusChanged
,
1909 .domainRdmaGidStatusChanged
= qemuProcessHandleRdmaGidStatusChanged
,
1913 qemuProcessMonitorReportLogError(qemuMonitorPtr mon
,
1919 qemuProcessMonitorLogFree(void *opaque
)
1921 qemuDomainLogContextPtr logCtxt
= opaque
;
1922 virObjectUnref(logCtxt
);
1927 qemuProcessInitMonitor(virQEMUDriverPtr driver
,
1929 qemuDomainAsyncJob asyncJob
)
1933 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
1936 ret
= qemuMonitorSetCapabilities(QEMU_DOMAIN_PRIVATE(vm
)->mon
);
1938 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
1946 qemuConnectMonitor(virQEMUDriverPtr driver
, virDomainObjPtr vm
, int asyncJob
,
1947 bool retry
, qemuDomainLogContextPtr logCtxt
)
1949 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
1950 qemuMonitorPtr mon
= NULL
;
1951 unsigned long long timeout
= 0;
1952 virDomainChrSourceDefPtr monConfig
;
1954 if (qemuSecuritySetDaemonSocketLabel(driver
->securityManager
, vm
->def
) < 0) {
1955 VIR_ERROR(_("Failed to set security context for monitor for %s"),
1960 /* When using hugepages, kernel zeroes them out before
1961 * handing them over to qemu. This can be very time
1962 * consuming. Therefore, add a second to timeout for each
1963 * 1GiB of guest RAM. */
1964 timeout
= vm
->def
->mem
.total_memory
/ (1024 * 1024);
1966 /* Hold an extra reference because we can't allow 'vm' to be
1967 * deleted until the monitor gets its own reference. */
1970 ignore_value(virTimeMillisNow(&priv
->monStart
));
1971 monConfig
= virObjectRef(priv
->monConfig
);
1972 virObjectUnlock(vm
);
1974 mon
= qemuMonitorOpen(vm
,
1981 if (mon
&& logCtxt
) {
1982 virObjectRef(logCtxt
);
1983 qemuMonitorSetDomainLog(mon
,
1984 qemuProcessMonitorReportLogError
,
1986 qemuProcessMonitorLogFree
);
1990 virObjectUnref(monConfig
);
1994 if (!virDomainObjIsActive(vm
)) {
1995 qemuMonitorClose(mon
);
2000 if (qemuSecurityClearSocketLabel(driver
->securityManager
, vm
->def
) < 0) {
2001 VIR_ERROR(_("Failed to clear security context for monitor for %s"),
2006 if (priv
->mon
== NULL
) {
2007 VIR_INFO("Failed to connect monitor for %s", vm
->def
->name
);
2011 if (qemuProcessInitMonitor(driver
, vm
, asyncJob
) < 0)
2014 if (qemuMigrationCapsCheck(driver
, vm
, asyncJob
) < 0)
2022 * qemuProcessReadLog: Read log file of a qemu VM
2023 * @logCtxt: the domain log context
2024 * @msg: pointer to buffer to store the read messages in
2025 * @max: maximum length of the message returned in @msg
2027 * Reads log of a qemu VM. Skips messages not produced by qemu or irrelevant
2028 * messages. If @max is not zero, @msg will contain at most @max characters
2029 * from the end of the log and @msg will start after a new line if possible.
2031 * Returns 0 on success or -1 on error
2034 qemuProcessReadLog(qemuDomainLogContextPtr logCtxt
,
2044 if ((got
= qemuDomainLogContextRead(logCtxt
, &buf
)) < 0)
2047 /* Filter out debug messages from intermediate libvirt process */
2049 while ((eol
= strchr(filter_next
, '\n'))) {
2051 if (virLogProbablyLogMessage(filter_next
) ||
2052 strstr(filter_next
, "char device redirected to")) {
2053 skip
= (eol
+ 1) - filter_next
;
2054 memmove(filter_next
, eol
+ 1, buf
+ got
- eol
);
2057 filter_next
= eol
+ 1;
2061 filter_next
= NULL
; /* silence false coverity warning */
2064 buf
[got
- 1] == '\n') {
2065 buf
[got
- 1] = '\0';
2069 if (max
> 0 && got
> max
) {
2072 if (buf
[skip
- 1] != '\n' &&
2073 (eol
= strchr(buf
+ skip
, '\n')) &&
2074 !virStringIsEmpty(eol
+ 1))
2075 skip
= eol
+ 1 - buf
;
2077 memmove(buf
, buf
+ skip
, got
- skip
+ 1);
2081 ignore_value(VIR_REALLOC_N_QUIET(buf
, got
+ 1));
2088 qemuProcessReportLogError(qemuDomainLogContextPtr logCtxt
,
2089 const char *msgprefix
)
2091 char *logmsg
= NULL
;
2094 max
= VIR_ERROR_MAX_LENGTH
- 1;
2095 max
-= strlen(msgprefix
);
2096 /* The length of the formatting string minus two '%s' */
2097 max
-= strlen(_("%s: %s")) - 4;
2099 if (qemuProcessReadLog(logCtxt
, &logmsg
, max
) < 0)
2102 virResetLastError();
2103 if (virStringIsEmpty(logmsg
))
2104 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s", msgprefix
);
2106 virReportError(VIR_ERR_INTERNAL_ERROR
, _("%s: %s"), msgprefix
, logmsg
);
2114 qemuProcessMonitorReportLogError(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
2118 qemuDomainLogContextPtr logCtxt
= opaque
;
2119 qemuProcessReportLogError(logCtxt
, msg
);
2124 qemuProcessLookupPTYs(virDomainChrDefPtr
*devices
,
2126 virHashTablePtr info
)
2132 for (i
= 0; i
< count
; i
++) {
2133 virDomainChrDefPtr chr
= devices
[i
];
2134 if (chr
->source
->type
== VIR_DOMAIN_CHR_TYPE_PTY
) {
2135 qemuMonitorChardevInfoPtr entry
;
2138 if (virAsprintf(&id
, "char%s", chr
->info
.alias
) < 0)
2141 entry
= virHashLookup(info
, id
);
2142 if (!entry
|| !entry
->ptyPath
) {
2143 if (chr
->source
->data
.file
.path
== NULL
) {
2144 /* neither the log output nor 'info chardev' had a
2145 * pty path for this chardev, report an error
2147 virReportError(VIR_ERR_INTERNAL_ERROR
,
2148 _("no assigned pty for device %s"), id
);
2151 /* 'info chardev' had no pty path for this chardev,
2152 * but the log output had, so we're fine
2158 VIR_FREE(chr
->source
->data
.file
.path
);
2159 if (VIR_STRDUP(chr
->source
->data
.file
.path
, entry
->ptyPath
) < 0)
2171 qemuProcessFindCharDevicePTYsMonitor(virDomainObjPtr vm
,
2172 virHashTablePtr info
)
2176 if (qemuProcessLookupPTYs(vm
->def
->serials
, vm
->def
->nserials
, info
) < 0)
2179 if (qemuProcessLookupPTYs(vm
->def
->parallels
, vm
->def
->nparallels
,
2183 if (qemuProcessLookupPTYs(vm
->def
->channels
, vm
->def
->nchannels
, info
) < 0)
2185 /* For historical reasons, console[0] can be just an alias
2186 * for serial[0]. That's why we need to update it as well. */
2187 if (vm
->def
->nconsoles
) {
2188 virDomainChrDefPtr chr
= vm
->def
->consoles
[0];
2190 if (vm
->def
->nserials
&&
2191 chr
->deviceType
== VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE
&&
2192 chr
->targetType
== VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL
) {
2193 /* yes, the first console is just an alias for serials[0] */
2195 if (virDomainChrSourceDefCopy(chr
->source
,
2196 ((vm
->def
->serials
[0])->source
)) < 0)
2201 if (qemuProcessLookupPTYs(vm
->def
->consoles
+ i
, vm
->def
->nconsoles
- i
,
2210 qemuProcessRefreshChannelVirtioState(virQEMUDriverPtr driver
,
2212 virHashTablePtr info
,
2216 int agentReason
= VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_CHANNEL
;
2217 qemuMonitorChardevInfoPtr entry
;
2218 virObjectEventPtr event
= NULL
;
2223 agentReason
= VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_DOMAIN_STARTED
;
2225 for (i
= 0; i
< vm
->def
->nchannels
; i
++) {
2226 virDomainChrDefPtr chr
= vm
->def
->channels
[i
];
2227 if (chr
->targetType
== VIR_DOMAIN_CHR_CHANNEL_TARGET_TYPE_VIRTIO
) {
2230 if (virAsprintf(&id
, "char%s", chr
->info
.alias
) < 0)
2233 /* port state not reported */
2234 if (!(entry
= virHashLookup(info
, id
)) ||
2238 if (entry
->state
!= VIR_DOMAIN_CHR_DEVICE_STATE_DEFAULT
&&
2239 STREQ_NULLABLE(chr
->target
.name
, "org.qemu.guest_agent.0") &&
2240 (event
= virDomainEventAgentLifecycleNewFromObj(vm
, entry
->state
,
2242 virObjectEventStateQueue(driver
->domainEventState
, event
);
2244 chr
->state
= entry
->state
;
2256 qemuRefreshVirtioChannelState(virQEMUDriverPtr driver
,
2258 qemuDomainAsyncJob asyncJob
)
2260 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2261 virHashTablePtr info
= NULL
;
2264 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
2267 ret
= qemuMonitorGetChardevInfo(priv
->mon
, &info
);
2268 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
2274 ret
= qemuProcessRefreshChannelVirtioState(driver
, vm
, info
, false);
2283 qemuProcessRefreshPRManagerState(virDomainObjPtr vm
,
2284 virHashTablePtr info
)
2286 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2287 qemuMonitorPRManagerInfoPtr prManagerInfo
;
2288 const char *managedAlias
= qemuDomainGetManagedPRAlias();
2291 if (!(prManagerInfo
= virHashLookup(info
, managedAlias
))) {
2292 virReportError(VIR_ERR_OPERATION_FAILED
,
2293 _("missing info on pr-manager %s"),
2298 priv
->prDaemonRunning
= prManagerInfo
->connected
;
2300 if (!priv
->prDaemonRunning
&&
2301 qemuProcessStartManagedPRDaemon(vm
) < 0)
2311 qemuRefreshPRManagerState(virQEMUDriverPtr driver
,
2314 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2315 virHashTablePtr info
= NULL
;
2318 if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_PR_MANAGER_HELPER
) ||
2319 !qemuDomainDefHasManagedPR(vm
))
2322 qemuDomainObjEnterMonitor(driver
, vm
);
2323 ret
= qemuMonitorGetPRManagerInfo(priv
->mon
, &info
);
2324 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
2330 ret
= qemuProcessRefreshPRManagerState(vm
, info
);
2339 qemuRefreshRTC(virQEMUDriverPtr driver
,
2342 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2348 if (vm
->def
->clock
.offset
!= VIR_DOMAIN_CLOCK_OFFSET_VARIABLE
)
2351 memset(&thenbits
, 0, sizeof(thenbits
));
2352 qemuDomainObjEnterMonitor(driver
, vm
);
2354 rv
= qemuMonitorGetRTCTime(priv
->mon
, &thenbits
);
2355 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
2361 thenbits
.tm_isdst
= -1;
2362 if ((then
= mktime(&thenbits
)) == (time_t)-1) {
2363 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
2364 _("Unable to convert time"));
2368 /* Thing is, @now is in local TZ but @then in UTC. */
2369 if (virTimeLocalOffsetFromUTC(&localOffset
) < 0)
2372 vm
->def
->clock
.data
.variable
.adjustment
= then
- now
+ localOffset
;
2376 qemuProcessRefreshBalloonState(virQEMUDriverPtr driver
,
2380 unsigned long long balloon
;
2383 /* if no ballooning is available, the current size equals to the current
2384 * full memory size */
2385 if (!virDomainDefHasMemballoon(vm
->def
)) {
2386 vm
->def
->mem
.cur_balloon
= virDomainDefGetMemoryTotal(vm
->def
);
2390 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
2393 rc
= qemuMonitorGetBalloonInfo(qemuDomainGetMonitor(vm
), &balloon
);
2394 if (qemuDomainObjExitMonitor(driver
, vm
) < 0 || rc
< 0)
2397 vm
->def
->mem
.cur_balloon
= balloon
;
2404 qemuProcessWaitForMonitor(virQEMUDriverPtr driver
,
2407 qemuDomainLogContextPtr logCtxt
)
2410 virHashTablePtr info
= NULL
;
2411 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2414 if (priv
->qemuCaps
&&
2415 virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_CHARDEV_FD_PASS
))
2418 VIR_DEBUG("Connect monitor to vm=%p name='%s' retry=%d",
2419 vm
, vm
->def
->name
, retry
);
2421 if (qemuConnectMonitor(driver
, vm
, asyncJob
, retry
, logCtxt
) < 0)
2424 /* Try to get the pty path mappings again via the monitor. This is much more
2425 * reliable if it's available.
2426 * Note that the monitor itself can be on a pty, so we still need to try the
2427 * log output method. */
2428 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
2430 ret
= qemuMonitorGetChardevInfo(priv
->mon
, &info
);
2431 VIR_DEBUG("qemuMonitorGetChardevInfo returned %i", ret
);
2432 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
2436 if ((ret
= qemuProcessFindCharDevicePTYsMonitor(vm
, info
)) < 0)
2439 if ((ret
= qemuProcessRefreshChannelVirtioState(driver
, vm
, info
,
2447 if (logCtxt
&& kill(vm
->pid
, 0) == -1 && errno
== ESRCH
) {
2448 qemuProcessReportLogError(logCtxt
,
2449 _("process exited while connecting to monitor"));
2458 qemuProcessDetectIOThreadPIDs(virQEMUDriverPtr driver
,
2462 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2463 qemuMonitorIOThreadInfoPtr
*iothreads
= NULL
;
2468 if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_OBJECT_IOTHREAD
)) {
2473 /* Get the list of IOThreads from qemu */
2474 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
2476 niothreads
= qemuMonitorGetIOThreads(priv
->mon
, &iothreads
);
2477 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
2482 if (niothreads
!= vm
->def
->niothreadids
) {
2483 virReportError(VIR_ERR_INTERNAL_ERROR
,
2484 _("got wrong number of IOThread pids from QEMU monitor. "
2485 "got %d, wanted %zu"),
2486 niothreads
, vm
->def
->niothreadids
);
2491 if (niothreads
== 0) {
2496 for (i
= 0; i
< niothreads
; i
++) {
2497 virDomainIOThreadIDDefPtr iothrid
;
2499 if (!(iothrid
= virDomainIOThreadIDFind(vm
->def
,
2500 iothreads
[i
]->iothread_id
))) {
2501 virReportError(VIR_ERR_INTERNAL_ERROR
,
2502 _("iothread %d not found"),
2503 iothreads
[i
]->iothread_id
);
2506 iothrid
->thread_id
= iothreads
[i
]->thread_id
;
2513 for (i
= 0; i
< niothreads
; i
++)
2514 VIR_FREE(iothreads
[i
]);
2515 VIR_FREE(iothreads
);
2522 qemuProcessGetAllCpuAffinity(virBitmapPtr
*cpumapRet
)
2526 if (!virHostCPUHasBitmap())
2529 if (!(*cpumapRet
= virHostCPUGetOnlineBitmap()))
2537 * To be run between fork/exec of QEMU only
2539 #if defined(HAVE_SCHED_GETAFFINITY) || defined(HAVE_BSD_CPU_AFFINITY)
2541 qemuProcessInitCpuAffinity(virDomainObjPtr vm
)
2543 VIR_AUTOPTR(virBitmap
) cpumapToSet
= NULL
;
2544 virDomainNumatuneMemMode mem_mode
;
2545 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2548 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
2549 _("Cannot setup CPU affinity until process is started"));
2553 /* Here is the deal, we can't set cpuset.mems before qemu is
2554 * started as it clashes with KVM allocation. Therefore, we
2555 * used to let qemu allocate its memory anywhere as we would
2556 * then move the memory to desired NUMA node via CGroups.
2557 * However, that might not be always possible because qemu
2558 * might lock some parts of its memory (e.g. due to VFIO).
2559 * Even if it possible, memory has to be copied between NUMA
2560 * nodes which is suboptimal.
2561 * Solution is to set affinity that matches the best what we
2562 * would have set in CGroups and then fix it later, once qemu
2563 * is already running. */
2564 if (virDomainNumaGetNodeCount(vm
->def
->numa
) <= 1 &&
2565 virDomainNumatuneGetMode(vm
->def
->numa
, -1, &mem_mode
) == 0 &&
2566 mem_mode
== VIR_DOMAIN_NUMATUNE_MEM_STRICT
) {
2567 virBitmapPtr nodeset
= NULL
;
2569 if (virDomainNumatuneMaybeGetNodeset(vm
->def
->numa
,
2575 if (virNumaNodesetToCPUset(nodeset
, &cpumapToSet
) < 0)
2577 } else if (vm
->def
->cputune
.emulatorpin
) {
2578 if (!(cpumapToSet
= virBitmapNewCopy(vm
->def
->cputune
.emulatorpin
)))
2581 if (qemuProcessGetAllCpuAffinity(&cpumapToSet
) < 0)
2586 virProcessSetAffinity(vm
->pid
, cpumapToSet
) < 0) {
2592 #else /* !defined(HAVE_SCHED_GETAFFINITY) && !defined(HAVE_BSD_CPU_AFFINITY) */
2594 qemuProcessInitCpuAffinity(virDomainObjPtr vm ATTRIBUTE_UNUSED
)
2598 #endif /* !defined(HAVE_SCHED_GETAFFINITY) && !defined(HAVE_BSD_CPU_AFFINITY) */
2600 /* set link states to down on interfaces at qemu start */
2602 qemuProcessSetLinkStates(virQEMUDriverPtr driver
,
2604 qemuDomainAsyncJob asyncJob
)
2606 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2607 virDomainDefPtr def
= vm
->def
;
2612 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
2615 for (i
= 0; i
< def
->nnets
; i
++) {
2616 if (def
->nets
[i
]->linkstate
== VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN
) {
2617 if (!def
->nets
[i
]->info
.alias
) {
2618 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
2619 _("missing alias for network device"));
2623 VIR_DEBUG("Setting link state: %s", def
->nets
[i
]->info
.alias
);
2625 rv
= qemuMonitorSetLink(priv
->mon
,
2626 def
->nets
[i
]->info
.alias
,
2627 VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN
);
2629 virReportError(VIR_ERR_OPERATION_FAILED
,
2630 _("Couldn't set link state on interface: %s"),
2631 def
->nets
[i
]->info
.alias
);
2640 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
2647 * qemuProcessSetupPid:
2649 * This function sets resource properties (affinity, cgroups,
2650 * scheduler) for any PID associated with a domain. It should be used
2651 * to set up emulator PIDs as well as vCPU and I/O thread pids to
2652 * ensure they are all handled the same way.
2654 * Returns 0 on success, -1 on error.
2657 qemuProcessSetupPid(virDomainObjPtr vm
,
2659 virCgroupThreadName nameval
,
2661 virBitmapPtr cpumask
,
2662 unsigned long long period
,
2664 virDomainThreadSchedParamPtr sched
)
2666 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2667 virDomainNumatuneMemMode mem_mode
;
2668 virCgroupPtr cgroup
= NULL
;
2669 virBitmapPtr use_cpumask
= NULL
;
2670 VIR_AUTOPTR(virBitmap
) hostcpumap
= NULL
;
2671 char *mem_mask
= NULL
;
2674 if ((period
|| quota
) &&
2675 !virCgroupHasController(priv
->cgroup
, VIR_CGROUP_CONTROLLER_CPU
)) {
2676 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
2677 _("cgroup cpu is required for scheduler tuning"));
2681 /* Infer which cpumask shall be used. */
2683 use_cpumask
= cpumask
;
2684 } else if (vm
->def
->placement_mode
== VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO
) {
2685 use_cpumask
= priv
->autoCpuset
;
2686 } else if (vm
->def
->cpumask
) {
2687 use_cpumask
= vm
->def
->cpumask
;
2689 /* You may think this is redundant, but we can't assume libvirtd
2690 * itself is running on all pCPUs, so we need to explicitly set
2691 * the spawned QEMU instance to all pCPUs if no map is given in
2692 * its config file */
2693 if (qemuProcessGetAllCpuAffinity(&hostcpumap
) < 0)
2695 use_cpumask
= hostcpumap
;
2699 * If CPU cgroup controller is not initialized here, then we need
2700 * neither period nor quota settings. And if CPUSET controller is
2701 * not initialized either, then there's nothing to do anyway.
2703 if (virCgroupHasController(priv
->cgroup
, VIR_CGROUP_CONTROLLER_CPU
) ||
2704 virCgroupHasController(priv
->cgroup
, VIR_CGROUP_CONTROLLER_CPUSET
)) {
2706 if (virDomainNumatuneGetMode(vm
->def
->numa
, -1, &mem_mode
) == 0 &&
2707 mem_mode
== VIR_DOMAIN_NUMATUNE_MEM_STRICT
&&
2708 virDomainNumatuneMaybeFormatNodeset(vm
->def
->numa
,
2713 if (virCgroupNewThread(priv
->cgroup
, nameval
, id
, true, &cgroup
) < 0)
2716 if (virCgroupHasController(priv
->cgroup
, VIR_CGROUP_CONTROLLER_CPUSET
)) {
2718 qemuSetupCgroupCpusetCpus(cgroup
, use_cpumask
) < 0)
2721 if (mem_mask
&& virCgroupSetCpusetMems(cgroup
, mem_mask
) < 0)
2726 if ((period
|| quota
) &&
2727 qemuSetupCgroupVcpuBW(cgroup
, period
, quota
) < 0)
2730 /* Move the thread to the sub dir */
2731 if (virCgroupAddThread(cgroup
, pid
) < 0)
2736 /* Setup legacy affinity. */
2737 if (use_cpumask
&& virProcessSetAffinity(pid
, use_cpumask
) < 0)
2740 /* Set scheduler type and priority, but not for the main thread. */
2742 nameval
!= VIR_CGROUP_THREAD_EMULATOR
&&
2743 virProcessSetScheduler(pid
, sched
->policy
, sched
->priority
) < 0)
2751 virCgroupRemove(cgroup
);
2752 virCgroupFree(&cgroup
);
2760 qemuProcessSetupEmulator(virDomainObjPtr vm
)
2762 return qemuProcessSetupPid(vm
, vm
->pid
, VIR_CGROUP_THREAD_EMULATOR
,
2763 0, vm
->def
->cputune
.emulatorpin
,
2764 vm
->def
->cputune
.emulator_period
,
2765 vm
->def
->cputune
.emulator_quota
,
2766 vm
->def
->cputune
.emulatorsched
);
2771 qemuProcessResctrlCreate(virQEMUDriverPtr driver
,
2776 virCapsPtr caps
= NULL
;
2777 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2779 if (!vm
->def
->nresctrls
)
2782 /* Force capability refresh since resctrl info can change
2783 * XXX: move cache info into virresctrl so caps are not needed */
2784 caps
= virQEMUDriverGetCapabilities(driver
, true);
2788 for (i
= 0; i
< vm
->def
->nresctrls
; i
++) {
2790 if (virResctrlAllocCreate(caps
->host
.resctrl
,
2791 vm
->def
->resctrls
[i
]->alloc
,
2792 priv
->machineName
) < 0)
2795 for (j
= 0; j
< vm
->def
->resctrls
[i
]->nmonitors
; j
++) {
2796 virDomainResctrlMonDefPtr mon
= NULL
;
2798 mon
= vm
->def
->resctrls
[i
]->monitors
[j
];
2799 if (virResctrlMonitorCreate(mon
->instance
,
2800 priv
->machineName
) < 0)
2807 virObjectUnref(caps
);
2813 qemuProcessBuildPRHelperPidfilePath(virDomainObjPtr vm
)
2815 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2816 const char *prdAlias
= qemuDomainGetManagedPRAlias();
2818 return virPidFileBuildPath(priv
->libDir
, prdAlias
);
2823 qemuProcessKillManagedPRDaemon(virDomainObjPtr vm
)
2825 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2826 virErrorPtr orig_err
;
2829 if (!(pidfile
= qemuProcessBuildPRHelperPidfilePath(vm
))) {
2830 VIR_WARN("Unable to construct pr-helper pidfile path");
2834 virErrorPreserveLast(&orig_err
);
2835 if (virPidFileForceCleanupPath(pidfile
) < 0) {
2836 VIR_WARN("Unable to kill pr-helper process");
2838 if (unlink(pidfile
) < 0 &&
2840 virReportSystemError(errno
,
2841 _("Unable to remove stale pidfile %s"),
2844 priv
->prDaemonRunning
= false;
2847 virErrorRestore(&orig_err
);
2854 qemuProcessStartPRDaemonHook(void *opaque
)
2856 virDomainObjPtr vm
= opaque
;
2861 if (qemuDomainNamespaceEnabled(vm
, QEMU_DOMAIN_NS_MOUNT
)) {
2862 if (virProcessGetNamespaces(vm
->pid
, &nfds
, &fds
) < 0)
2866 virProcessSetNamespaces(nfds
, fds
) < 0)
2872 for (i
= 0; i
< nfds
; i
++)
2873 VIR_FORCE_CLOSE(fds
[i
]);
2880 qemuProcessStartManagedPRDaemon(virDomainObjPtr vm
)
2882 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
2883 virQEMUDriverPtr driver
= priv
->driver
;
2884 virQEMUDriverConfigPtr cfg
;
2886 char *pidfile
= NULL
;
2888 char *socketPath
= NULL
;
2890 virCommandPtr cmd
= NULL
;
2891 virTimeBackOffVar timebackoff
;
2892 const unsigned long long timeout
= 500000; /* ms */
2895 cfg
= virQEMUDriverGetConfig(driver
);
2897 if (!virFileIsExecutable(cfg
->prHelperName
)) {
2898 virReportSystemError(errno
, _("'%s' is not a suitable pr helper"),
2903 if (!(pidfile
= qemuProcessBuildPRHelperPidfilePath(vm
)))
2906 /* Just try to acquire. Dummy pid will be replaced later */
2907 if ((pidfd
= virPidFileAcquirePath(pidfile
, false, -1)) < 0)
2910 if (!(socketPath
= qemuDomainGetManagedPRSocketPath(priv
)))
2913 /* Remove stale socket */
2914 if (unlink(socketPath
) < 0 &&
2916 virReportSystemError(errno
,
2917 _("Unable to remove stale socket path: %s"),
2922 if (!(cmd
= virCommandNewArgList(cfg
->prHelperName
,
2928 virCommandDaemonize(cmd
);
2929 /* We want our virCommand to write child PID into the pidfile
2930 * so that we can read it even before exec(). */
2931 virCommandSetPidFile(cmd
, pidfile
);
2932 virCommandSetErrorFD(cmd
, &errfd
);
2934 /* Place the process into the same namespace and cgroup as
2935 * qemu (so that it shares the same view of the system). */
2936 virCommandSetPreExecHook(cmd
, qemuProcessStartPRDaemonHook
, vm
);
2938 if (virCommandRun(cmd
, NULL
) < 0)
2941 if (virPidFileReadPath(pidfile
, &cpid
) < 0) {
2942 virReportError(VIR_ERR_INTERNAL_ERROR
,
2943 _("pr helper %s didn't show up"),
2948 if (virTimeBackOffStart(&timebackoff
, 1, timeout
) < 0)
2950 while (virTimeBackOffWait(&timebackoff
)) {
2951 char errbuf
[1024] = { 0 };
2953 if (virFileExists(socketPath
))
2956 if (virProcessKill(cpid
, 0) == 0)
2959 if (saferead(errfd
, errbuf
, sizeof(errbuf
) - 1) < 0) {
2960 virReportSystemError(errno
,
2961 _("pr helper %s died unexpectedly"),
2964 virReportError(VIR_ERR_OPERATION_FAILED
,
2965 _("pr helper died and reported: %s"), errbuf
);
2970 if (!virFileExists(socketPath
)) {
2971 virReportError(VIR_ERR_OPERATION_TIMEOUT
, "%s",
2972 _("pr helper socked did not show up"));
2977 virCgroupAddMachineProcess(priv
->cgroup
, cpid
) < 0)
2980 if (qemuSecurityDomainSetPathLabel(driver
, vm
, socketPath
, true) < 0)
2983 priv
->prDaemonRunning
= true;
2987 virCommandAbort(cmd
);
2989 virProcessKillPainfully(cpid
, true);
2993 virCommandFree(cmd
);
2994 VIR_FREE(socketPath
);
2995 VIR_FORCE_CLOSE(pidfd
);
2997 VIR_FORCE_CLOSE(errfd
);
2998 virObjectUnref(cfg
);
3004 qemuProcessInitPasswords(virQEMUDriverPtr driver
,
3009 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
3012 for (i
= 0; i
< vm
->def
->ngraphics
; ++i
) {
3013 virDomainGraphicsDefPtr graphics
= vm
->def
->graphics
[i
];
3014 if (graphics
->type
== VIR_DOMAIN_GRAPHICS_TYPE_VNC
) {
3015 ret
= qemuDomainChangeGraphicsPasswords(driver
, vm
,
3016 VIR_DOMAIN_GRAPHICS_TYPE_VNC
,
3017 &graphics
->data
.vnc
.auth
,
3020 } else if (graphics
->type
== VIR_DOMAIN_GRAPHICS_TYPE_SPICE
) {
3021 ret
= qemuDomainChangeGraphicsPasswords(driver
, vm
,
3022 VIR_DOMAIN_GRAPHICS_TYPE_SPICE
,
3023 &graphics
->data
.spice
.auth
,
3033 virObjectUnref(cfg
);
3039 qemuProcessPrepareChardevDevice(virDomainDefPtr def ATTRIBUTE_UNUSED
,
3040 virDomainChrDefPtr dev
,
3041 void *opaque ATTRIBUTE_UNUSED
)
3044 if (dev
->source
->type
!= VIR_DOMAIN_CHR_TYPE_FILE
)
3047 if ((fd
= open(dev
->source
->data
.file
.path
,
3048 O_CREAT
| O_APPEND
, S_IRUSR
|S_IWUSR
)) < 0) {
3049 virReportSystemError(errno
,
3050 _("Unable to pre-create chardev file '%s'"),
3051 dev
->source
->data
.file
.path
);
3055 VIR_FORCE_CLOSE(fd
);
3062 qemuProcessCleanupChardevDevice(virDomainDefPtr def ATTRIBUTE_UNUSED
,
3063 virDomainChrDefPtr dev
,
3064 void *opaque ATTRIBUTE_UNUSED
)
3066 if (dev
->source
->type
== VIR_DOMAIN_CHR_TYPE_UNIX
&&
3067 dev
->source
->data
.nix
.listen
&&
3068 dev
->source
->data
.nix
.path
)
3069 unlink(dev
->source
->data
.nix
.path
);
3076 * Loads and update video memory size for video devices according to QEMU
3077 * process as the QEMU will silently update the values that we pass to QEMU
3078 * through command line. We need to load these updated values and store them
3079 * into the status XML.
3081 * We will fail if for some reason the values cannot be loaded from QEMU because
3082 * its mandatory to get the correct video memory size to status XML to not break
3086 qemuProcessUpdateVideoRamSize(virQEMUDriverPtr driver
,
3092 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3093 virDomainVideoDefPtr video
= NULL
;
3094 virQEMUDriverConfigPtr cfg
= NULL
;
3096 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
3099 for (i
= 0; i
< vm
->def
->nvideos
; i
++) {
3100 video
= vm
->def
->videos
[i
];
3102 switch (video
->type
) {
3103 case VIR_DOMAIN_VIDEO_TYPE_VGA
:
3104 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_VGA_VGAMEM
)) {
3105 if (qemuMonitorUpdateVideoMemorySize(priv
->mon
, video
, "VGA") < 0)
3109 case VIR_DOMAIN_VIDEO_TYPE_QXL
:
3111 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_QXL_VGAMEM
) &&
3112 qemuMonitorUpdateVideoMemorySize(priv
->mon
, video
,
3116 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_QXL_VRAM64
) &&
3117 qemuMonitorUpdateVideoVram64Size(priv
->mon
, video
,
3121 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_QXL_VGAMEM
) &&
3122 qemuMonitorUpdateVideoMemorySize(priv
->mon
, video
,
3126 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_QXL_VRAM64
) &&
3127 qemuMonitorUpdateVideoVram64Size(priv
->mon
, video
,
3132 case VIR_DOMAIN_VIDEO_TYPE_VMVGA
:
3133 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_VMWARE_SVGA_VGAMEM
)) {
3134 if (qemuMonitorUpdateVideoMemorySize(priv
->mon
, video
,
3139 case VIR_DOMAIN_VIDEO_TYPE_CIRRUS
:
3140 case VIR_DOMAIN_VIDEO_TYPE_XEN
:
3141 case VIR_DOMAIN_VIDEO_TYPE_VBOX
:
3142 case VIR_DOMAIN_VIDEO_TYPE_LAST
:
3148 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
3151 cfg
= virQEMUDriverGetConfig(driver
);
3152 ret
= virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
);
3153 virObjectUnref(cfg
);
3158 ignore_value(qemuDomainObjExitMonitor(driver
, vm
));
3163 struct qemuProcessHookData
{
3165 virQEMUDriverPtr driver
;
3166 virQEMUDriverConfigPtr cfg
;
3169 static int qemuProcessHook(void *data
)
3171 struct qemuProcessHookData
*h
= data
;
3172 qemuDomainObjPrivatePtr priv
= h
->vm
->privateData
;
3175 virBitmapPtr nodeset
= NULL
;
3176 virDomainNumatuneMemMode mode
;
3178 /* This method cannot use any mutexes, which are not
3179 * protected across fork()
3182 qemuSecurityPostFork(h
->driver
->securityManager
);
3184 /* Some later calls want pid present */
3185 h
->vm
->pid
= getpid();
3187 VIR_DEBUG("Obtaining domain lock");
3189 * Since we're going to leak the returned FD to QEMU,
3190 * we need to make sure it gets a sensible label.
3191 * This mildly sucks, because there could be other
3192 * sockets the lock driver opens that we don't want
3193 * labelled. So far we're ok though.
3195 if (qemuSecuritySetSocketLabel(h
->driver
->securityManager
, h
->vm
->def
) < 0)
3197 if (virDomainLockProcessStart(h
->driver
->lockManager
,
3200 /* QEMU is always paused initially */
3204 if (qemuSecurityClearSocketLabel(h
->driver
->securityManager
, h
->vm
->def
) < 0)
3207 if (qemuDomainBuildNamespace(h
->cfg
, h
->driver
->securityManager
, h
->vm
) < 0)
3210 if (virDomainNumatuneGetMode(h
->vm
->def
->numa
, -1, &mode
) == 0) {
3211 if (mode
== VIR_DOMAIN_NUMATUNE_MEM_STRICT
&&
3212 h
->cfg
->cgroupControllers
& (1 << VIR_CGROUP_CONTROLLER_CPUSET
) &&
3213 virCgroupControllerAvailable(VIR_CGROUP_CONTROLLER_CPUSET
)) {
3214 /* Use virNuma* API iff necessary. Once set and child is exec()-ed,
3215 * there's no way for us to change it. Rely on cgroups (if available
3216 * and enabled in the config) rather than virNuma*. */
3217 VIR_DEBUG("Relying on CGroups for memory binding");
3219 nodeset
= virDomainNumatuneGetNodeset(h
->vm
->def
->numa
,
3220 priv
->autoNodeset
, -1);
3222 if (virNumaSetupMemoryPolicy(mode
, nodeset
) < 0)
3230 virObjectUnref(h
->cfg
);
3231 VIR_DEBUG("Hook complete ret=%d", ret
);
3236 qemuProcessPrepareMonitorChr(virDomainChrSourceDefPtr monConfig
,
3237 const char *domainDir
)
3239 monConfig
->type
= VIR_DOMAIN_CHR_TYPE_UNIX
;
3240 monConfig
->data
.nix
.listen
= true;
3242 if (virAsprintf(&monConfig
->data
.nix
.path
, "%s/monitor.sock",
3250 * Precondition: vm must be locked, and a job must be active.
3251 * This method will call {Enter,Exit}Monitor
3254 qemuProcessStartCPUs(virQEMUDriverPtr driver
, virDomainObjPtr vm
,
3255 virDomainRunningReason reason
,
3256 qemuDomainAsyncJob asyncJob
)
3259 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3260 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
3262 /* Bring up netdevs before starting CPUs */
3263 if (qemuInterfaceStartDevices(vm
->def
) < 0)
3266 VIR_DEBUG("Using lock state '%s'", NULLSTR(priv
->lockState
));
3267 if (virDomainLockProcessResume(driver
->lockManager
, cfg
->uri
,
3268 vm
, priv
->lockState
) < 0) {
3269 /* Don't free priv->lockState on error, because we need
3270 * to make sure we have state still present if the user
3271 * tries to resume again
3275 VIR_FREE(priv
->lockState
);
3277 priv
->runningReason
= reason
;
3279 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
3282 ret
= qemuMonitorStartCPUs(priv
->mon
);
3283 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
3289 /* The RESUME event handler will change the domain state with the reason
3290 * saved in priv->runningReason and it will also emit corresponding domain
3295 virObjectUnref(cfg
);
3299 priv
->runningReason
= VIR_DOMAIN_RUNNING_UNKNOWN
;
3300 if (virDomainLockProcessPause(driver
->lockManager
, vm
, &priv
->lockState
) < 0)
3301 VIR_WARN("Unable to release lease on %s", vm
->def
->name
);
3302 VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv
->lockState
));
3307 int qemuProcessStopCPUs(virQEMUDriverPtr driver
,
3309 virDomainPausedReason reason
,
3310 qemuDomainAsyncJob asyncJob
)
3313 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3315 VIR_FREE(priv
->lockState
);
3317 priv
->pausedReason
= reason
;
3319 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
3322 ret
= qemuMonitorStopCPUs(priv
->mon
);
3323 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
3329 /* de-activate netdevs after stopping CPUs */
3330 ignore_value(qemuInterfaceStopDevices(vm
->def
));
3332 if (priv
->job
.current
)
3333 ignore_value(virTimeMillisNow(&priv
->job
.current
->stopped
));
3335 /* The STOP event handler will change the domain state with the reason
3336 * saved in priv->pausedReason and it will also emit corresponding domain
3340 if (virDomainLockProcessPause(driver
->lockManager
, vm
, &priv
->lockState
) < 0)
3341 VIR_WARN("Unable to release lease on %s", vm
->def
->name
);
3342 VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv
->lockState
));
3346 priv
->pausedReason
= VIR_DOMAIN_PAUSED_UNKNOWN
;
3354 qemuProcessNotifyNets(virDomainDefPtr def
)
3357 virConnectPtr conn
= NULL
;
3359 for (i
= 0; i
< def
->nnets
; i
++) {
3360 virDomainNetDefPtr net
= def
->nets
[i
];
3361 /* keep others from trying to use the macvtap device name, but
3362 * don't return error if this happens, since that causes the
3363 * domain to be unceremoniously killed, which would be *very*
3366 if (virDomainNetGetActualType(net
) == VIR_DOMAIN_NET_TYPE_DIRECT
)
3367 ignore_value(virNetDevMacVLanReserveName(net
->ifname
, false));
3369 if (net
->type
== VIR_DOMAIN_NET_TYPE_NETWORK
) {
3370 if (!conn
&& !(conn
= virGetConnectNetwork()))
3372 virDomainNetNotifyActualDevice(conn
, def
, net
);
3376 virObjectUnref(conn
);
3379 /* Attempt to instantiate the filters. Ignore failures because it's
3380 * possible that someone deleted a filter binding and the associated
3381 * filter while the guest was running and we don't want that action
3382 * to cause failure to keep the guest running during the reconnection
3383 * processing. Nor do we necessarily want other failures to do the
3384 * same. We'll just log the error conditions other than of course
3385 * ignoreExists possibility (e.g. the true flag) */
3387 qemuProcessFiltersInstantiate(virDomainDefPtr def
)
3391 for (i
= 0; i
< def
->nnets
; i
++) {
3392 virDomainNetDefPtr net
= def
->nets
[i
];
3393 if ((net
->filter
) && (net
->ifname
)) {
3394 if (virDomainConfNWFilterInstantiate(def
->name
, def
->uuid
, net
,
3396 VIR_WARN("filter '%s' instantiation for '%s' failed '%s'",
3397 net
->filter
, net
->ifname
, virGetLastErrorMessage());
3398 virResetLastError();
3405 qemuProcessUpdateState(virQEMUDriverPtr driver
, virDomainObjPtr vm
)
3407 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3408 virDomainState state
;
3409 virDomainPausedReason reason
;
3410 virDomainState newState
= VIR_DOMAIN_NOSTATE
;
3417 qemuDomainObjEnterMonitor(driver
, vm
);
3418 ret
= qemuMonitorGetStatus(priv
->mon
, &running
, &reason
);
3419 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
3425 state
= virDomainObjGetState(vm
, &oldReason
);
3428 (state
== VIR_DOMAIN_SHUTOFF
||
3429 (state
== VIR_DOMAIN_PAUSED
&&
3430 oldReason
== VIR_DOMAIN_PAUSED_STARTING_UP
))) {
3431 newState
= VIR_DOMAIN_RUNNING
;
3432 newReason
= VIR_DOMAIN_RUNNING_BOOTED
;
3433 ignore_value(VIR_STRDUP_QUIET(msg
, "finished booting"));
3434 } else if (state
== VIR_DOMAIN_PAUSED
&& running
) {
3435 newState
= VIR_DOMAIN_RUNNING
;
3436 newReason
= VIR_DOMAIN_RUNNING_UNPAUSED
;
3437 ignore_value(VIR_STRDUP_QUIET(msg
, "was unpaused"));
3438 } else if (state
== VIR_DOMAIN_RUNNING
&& !running
) {
3439 if (reason
== VIR_DOMAIN_PAUSED_SHUTTING_DOWN
) {
3440 newState
= VIR_DOMAIN_SHUTDOWN
;
3441 newReason
= VIR_DOMAIN_SHUTDOWN_UNKNOWN
;
3442 ignore_value(VIR_STRDUP_QUIET(msg
, "shutdown"));
3443 } else if (reason
== VIR_DOMAIN_PAUSED_CRASHED
) {
3444 newState
= VIR_DOMAIN_CRASHED
;
3445 newReason
= VIR_DOMAIN_CRASHED_PANICKED
;
3446 ignore_value(VIR_STRDUP_QUIET(msg
, "crashed"));
3448 newState
= VIR_DOMAIN_PAUSED
;
3450 ignore_value(virAsprintf(&msg
, "was paused (%s)",
3451 virDomainPausedReasonTypeToString(reason
)));
3455 if (newState
!= VIR_DOMAIN_NOSTATE
) {
3456 VIR_DEBUG("Domain %s %s while its monitor was disconnected;"
3457 " changing state to %s (%s)",
3460 virDomainStateTypeToString(newState
),
3461 virDomainStateReasonToString(newState
, newReason
));
3463 virDomainObjSetState(vm
, newState
, newReason
);
3470 qemuProcessRecoverMigrationIn(virQEMUDriverPtr driver
,
3472 const qemuDomainJobObj
*job
,
3473 virDomainState state
,
3476 bool postcopy
= (state
== VIR_DOMAIN_PAUSED
&&
3477 reason
== VIR_DOMAIN_PAUSED_POSTCOPY_FAILED
) ||
3478 (state
== VIR_DOMAIN_RUNNING
&&
3479 reason
== VIR_DOMAIN_RUNNING_POSTCOPY
);
3481 switch ((qemuMigrationJobPhase
) job
->phase
) {
3482 case QEMU_MIGRATION_PHASE_NONE
:
3483 case QEMU_MIGRATION_PHASE_PERFORM2
:
3484 case QEMU_MIGRATION_PHASE_BEGIN3
:
3485 case QEMU_MIGRATION_PHASE_PERFORM3
:
3486 case QEMU_MIGRATION_PHASE_PERFORM3_DONE
:
3487 case QEMU_MIGRATION_PHASE_CONFIRM3_CANCELLED
:
3488 case QEMU_MIGRATION_PHASE_CONFIRM3
:
3489 case QEMU_MIGRATION_PHASE_LAST
:
3490 /* N/A for incoming migration */
3493 case QEMU_MIGRATION_PHASE_PREPARE
:
3494 VIR_DEBUG("Killing unfinished incoming migration for domain %s",
3498 case QEMU_MIGRATION_PHASE_FINISH2
:
3499 /* source domain is already killed so let's just resume the domain
3500 * and hope we are all set */
3501 VIR_DEBUG("Incoming migration finished, resuming domain %s",
3503 if (qemuProcessStartCPUs(driver
, vm
,
3504 VIR_DOMAIN_RUNNING_MIGRATED
,
3505 QEMU_ASYNC_JOB_NONE
) < 0) {
3506 VIR_WARN("Could not resume domain %s", vm
->def
->name
);
3510 case QEMU_MIGRATION_PHASE_FINISH3
:
3511 /* migration finished, we started resuming the domain but didn't
3512 * confirm success or failure yet; killing it seems safest unless
3513 * we already started guest CPUs or we were in post-copy mode */
3515 qemuMigrationAnyPostcopyFailed(driver
, vm
);
3516 } else if (state
!= VIR_DOMAIN_RUNNING
) {
3517 VIR_DEBUG("Killing migrated domain %s", vm
->def
->name
);
3523 qemuMigrationParamsReset(driver
, vm
, QEMU_ASYNC_JOB_NONE
,
3524 job
->migParams
, job
->apiFlags
);
3529 qemuProcessRecoverMigrationOut(virQEMUDriverPtr driver
,
3531 const qemuDomainJobObj
*job
,
3532 virDomainState state
,
3534 unsigned int *stopFlags
)
3536 bool postcopy
= state
== VIR_DOMAIN_PAUSED
&&
3537 (reason
== VIR_DOMAIN_PAUSED_POSTCOPY
||
3538 reason
== VIR_DOMAIN_PAUSED_POSTCOPY_FAILED
);
3539 bool resume
= false;
3541 switch ((qemuMigrationJobPhase
) job
->phase
) {
3542 case QEMU_MIGRATION_PHASE_NONE
:
3543 case QEMU_MIGRATION_PHASE_PREPARE
:
3544 case QEMU_MIGRATION_PHASE_FINISH2
:
3545 case QEMU_MIGRATION_PHASE_FINISH3
:
3546 case QEMU_MIGRATION_PHASE_LAST
:
3547 /* N/A for outgoing migration */
3550 case QEMU_MIGRATION_PHASE_BEGIN3
:
3551 /* nothing happened so far, just forget we were about to migrate the
3555 case QEMU_MIGRATION_PHASE_PERFORM2
:
3556 case QEMU_MIGRATION_PHASE_PERFORM3
:
3557 /* migration is still in progress, let's cancel it and resume the
3558 * domain; however we can only do that before migration enters
3562 qemuMigrationAnyPostcopyFailed(driver
, vm
);
3564 VIR_DEBUG("Cancelling unfinished migration of domain %s",
3566 if (qemuMigrationSrcCancel(driver
, vm
) < 0) {
3567 VIR_WARN("Could not cancel ongoing migration of domain %s",
3574 case QEMU_MIGRATION_PHASE_PERFORM3_DONE
:
3575 /* migration finished but we didn't have a chance to get the result
3576 * of Finish3 step; third party needs to check what to do next; in
3577 * post-copy mode we can use PAUSED_POSTCOPY_FAILED state for this
3580 qemuMigrationAnyPostcopyFailed(driver
, vm
);
3583 case QEMU_MIGRATION_PHASE_CONFIRM3_CANCELLED
:
3584 /* Finish3 failed, we need to resume the domain, but once we enter
3585 * post-copy mode there's no way back, so let's just mark the domain
3586 * as broken in that case
3589 qemuMigrationAnyPostcopyFailed(driver
, vm
);
3591 VIR_DEBUG("Resuming domain %s after failed migration",
3597 case QEMU_MIGRATION_PHASE_CONFIRM3
:
3598 /* migration completed, we need to kill the domain here */
3599 *stopFlags
|= VIR_QEMU_PROCESS_STOP_MIGRATED
;
3604 /* resume the domain but only if it was paused as a result of
3607 if (state
== VIR_DOMAIN_PAUSED
&&
3608 (reason
== VIR_DOMAIN_PAUSED_MIGRATION
||
3609 reason
== VIR_DOMAIN_PAUSED_UNKNOWN
)) {
3610 if (qemuProcessStartCPUs(driver
, vm
,
3611 VIR_DOMAIN_RUNNING_MIGRATION_CANCELED
,
3612 QEMU_ASYNC_JOB_NONE
) < 0) {
3613 VIR_WARN("Could not resume domain %s", vm
->def
->name
);
3618 qemuMigrationParamsReset(driver
, vm
, QEMU_ASYNC_JOB_NONE
,
3619 job
->migParams
, job
->apiFlags
);
3624 qemuProcessRecoverJob(virQEMUDriverPtr driver
,
3626 const qemuDomainJobObj
*job
,
3627 unsigned int *stopFlags
)
3629 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3630 virDomainState state
;
3633 state
= virDomainObjGetState(vm
, &reason
);
3635 switch (job
->asyncJob
) {
3636 case QEMU_ASYNC_JOB_MIGRATION_OUT
:
3637 if (qemuProcessRecoverMigrationOut(driver
, vm
, job
,
3638 state
, reason
, stopFlags
) < 0)
3642 case QEMU_ASYNC_JOB_MIGRATION_IN
:
3643 if (qemuProcessRecoverMigrationIn(driver
, vm
, job
,
3648 case QEMU_ASYNC_JOB_SAVE
:
3649 case QEMU_ASYNC_JOB_DUMP
:
3650 case QEMU_ASYNC_JOB_SNAPSHOT
:
3651 qemuDomainObjEnterMonitor(driver
, vm
);
3652 ignore_value(qemuMonitorMigrateCancel(priv
->mon
));
3653 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
3655 /* resume the domain but only if it was paused as a result of
3656 * running a migration-to-file operation. Although we are
3657 * recovering an async job, this function is run at startup
3658 * and must resume things using sync monitor connections. */
3659 if (state
== VIR_DOMAIN_PAUSED
&&
3660 ((job
->asyncJob
== QEMU_ASYNC_JOB_DUMP
&&
3661 reason
== VIR_DOMAIN_PAUSED_DUMP
) ||
3662 (job
->asyncJob
== QEMU_ASYNC_JOB_SAVE
&&
3663 reason
== VIR_DOMAIN_PAUSED_SAVE
) ||
3664 (job
->asyncJob
== QEMU_ASYNC_JOB_SNAPSHOT
&&
3665 (reason
== VIR_DOMAIN_PAUSED_SNAPSHOT
||
3666 reason
== VIR_DOMAIN_PAUSED_MIGRATION
)) ||
3667 reason
== VIR_DOMAIN_PAUSED_UNKNOWN
)) {
3668 if (qemuProcessStartCPUs(driver
, vm
,
3669 VIR_DOMAIN_RUNNING_SAVE_CANCELED
,
3670 QEMU_ASYNC_JOB_NONE
) < 0) {
3671 VIR_WARN("Could not resume domain '%s' after migration to file",
3677 case QEMU_ASYNC_JOB_START
:
3678 /* Already handled in VIR_DOMAIN_PAUSED_STARTING_UP check. */
3681 case QEMU_ASYNC_JOB_NONE
:
3682 case QEMU_ASYNC_JOB_LAST
:
3686 if (!virDomainObjIsActive(vm
))
3689 /* In case any special handling is added for job type that has been ignored
3690 * before, QEMU_DOMAIN_TRACK_JOBS (from qemu_domain.h) needs to be updated
3691 * for the job to be properly tracked in domain state XML.
3693 switch (job
->active
) {
3694 case QEMU_JOB_QUERY
:
3698 case QEMU_JOB_DESTROY
:
3699 VIR_DEBUG("Domain %s should have already been destroyed",
3703 case QEMU_JOB_SUSPEND
:
3704 /* mostly harmless */
3707 case QEMU_JOB_MODIFY
:
3708 /* XXX depending on the command we may be in an inconsistent state and
3709 * we should probably fall back to "monitor error" state and refuse to
3713 case QEMU_JOB_MIGRATION_OP
:
3714 case QEMU_JOB_ABORT
:
3715 case QEMU_JOB_ASYNC
:
3716 case QEMU_JOB_ASYNC_NESTED
:
3717 /* async job was already handled above */
3727 qemuProcessUpdateDevices(virQEMUDriverPtr driver
,
3730 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3731 virDomainDeviceDef dev
;
3732 const char **qemuDevices
;
3737 old
= priv
->qemuDevices
;
3738 priv
->qemuDevices
= NULL
;
3739 if (qemuDomainUpdateDeviceList(driver
, vm
, QEMU_ASYNC_JOB_NONE
) < 0)
3742 qemuDevices
= (const char **)priv
->qemuDevices
;
3745 if (!virStringListHasString(qemuDevices
, *tmp
) &&
3746 virDomainDefFindDevice(vm
->def
, *tmp
, &dev
, false) == 0 &&
3747 qemuDomainRemoveDevice(driver
, vm
, &dev
) < 0) {
3756 virStringListFree(old
);
3761 qemuDomainPerfRestart(virDomainObjPtr vm
)
3764 virDomainDefPtr def
= vm
->def
;
3765 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3767 if (!(priv
->perf
= virPerfNew()))
3770 for (i
= 0; i
< VIR_PERF_EVENT_LAST
; i
++) {
3771 if (def
->perf
.events
[i
] &&
3772 def
->perf
.events
[i
] == VIR_TRISTATE_BOOL_YES
) {
3774 /* Failure to re-enable the perf event should not be fatal */
3775 if (virPerfEventEnable(priv
->perf
, i
, vm
->pid
) < 0)
3776 def
->perf
.events
[i
] = VIR_TRISTATE_BOOL_NO
;
3785 qemuProcessReconnectCheckMemAliasOrderMismatch(virDomainObjPtr vm
)
3789 virDomainDefPtr def
= vm
->def
;
3790 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
3792 if (!virDomainDefHasMemoryHotplug(def
) || def
->nmems
== 0)
3795 for (i
= 0; i
< def
->nmems
; i
++) {
3796 aliasidx
= qemuDomainDeviceAliasIndex(&def
->mems
[i
]->info
, "dimm");
3798 if (def
->mems
[i
]->info
.addr
.dimm
.slot
!= aliasidx
) {
3799 priv
->memAliasOrderMismatch
= true;
3807 qemuProcessNeedHugepagesPath(virDomainDefPtr def
,
3808 virDomainMemoryDefPtr mem
)
3810 const long system_pagesize
= virGetSystemPageSizeKB();
3813 if (def
->mem
.source
== VIR_DOMAIN_MEMORY_SOURCE_FILE
)
3816 for (i
= 0; i
< def
->mem
.nhugepages
; i
++) {
3817 if (def
->mem
.hugepages
[i
].size
!= system_pagesize
)
3821 for (i
= 0; i
< def
->nmems
; i
++) {
3822 if (def
->mems
[i
]->model
== VIR_DOMAIN_MEMORY_MODEL_DIMM
&&
3823 def
->mems
[i
]->pagesize
&&
3824 def
->mems
[i
]->pagesize
!= system_pagesize
)
3829 mem
->model
== VIR_DOMAIN_MEMORY_MODEL_DIMM
&&
3831 mem
->pagesize
!= system_pagesize
)
3839 qemuProcessNeedMemoryBackingPath(virDomainDefPtr def
,
3840 virDomainMemoryDefPtr mem
)
3845 if (def
->mem
.source
== VIR_DOMAIN_MEMORY_SOURCE_FILE
||
3846 def
->mem
.access
!= VIR_DOMAIN_MEMORY_ACCESS_DEFAULT
)
3849 numaNodes
= virDomainNumaGetNodeCount(def
->numa
);
3850 for (i
= 0; i
< numaNodes
; i
++) {
3851 if (virDomainNumaGetNodeMemoryAccessMode(def
->numa
, i
)
3852 != VIR_DOMAIN_MEMORY_ACCESS_DEFAULT
)
3857 mem
->model
== VIR_DOMAIN_MEMORY_MODEL_DIMM
&&
3858 (mem
->access
!= VIR_DOMAIN_MEMORY_ACCESS_DEFAULT
||
3859 (mem
->targetNode
>= 0 &&
3860 virDomainNumaGetNodeMemoryAccessMode(def
->numa
, mem
->targetNode
)
3861 != VIR_DOMAIN_MEMORY_ACCESS_DEFAULT
)))
3869 qemuProcessBuildDestroyMemoryPathsImpl(virQEMUDriverPtr driver
,
3875 if (virFileExists(path
))
3878 if (virFileMakePathWithMode(path
, 0700) < 0) {
3879 virReportSystemError(errno
,
3880 _("Unable to create %s"),
3885 if (qemuSecurityDomainSetPathLabel(driver
, vm
, path
, true) < 0)
3888 if (virFileDeleteTree(path
) < 0)
3897 qemuProcessBuildDestroyMemoryPaths(virQEMUDriverPtr driver
,
3899 virDomainMemoryDefPtr mem
,
3902 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
3905 bool shouldBuildHP
= false;
3906 bool shouldBuildMB
= false;
3910 shouldBuildHP
= qemuProcessNeedHugepagesPath(vm
->def
, mem
);
3911 shouldBuildMB
= qemuProcessNeedMemoryBackingPath(vm
->def
, mem
);
3914 if (!build
|| shouldBuildHP
) {
3915 for (i
= 0; i
< cfg
->nhugetlbfs
; i
++) {
3916 path
= qemuGetDomainHugepagePath(vm
->def
, &cfg
->hugetlbfs
[i
]);
3921 if (qemuProcessBuildDestroyMemoryPathsImpl(driver
, vm
,
3929 if (!build
|| shouldBuildMB
) {
3930 if (qemuGetMemoryBackingDomainPath(vm
->def
, cfg
, &path
) < 0)
3933 if (qemuProcessBuildDestroyMemoryPathsImpl(driver
, vm
,
3943 virObjectUnref(cfg
);
3949 qemuProcessDestroyMemoryBackingPath(virQEMUDriverPtr driver
,
3951 virDomainMemoryDefPtr mem
)
3953 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
3957 if (qemuGetMemoryBackingPath(vm
->def
, cfg
, mem
->info
.alias
, &path
) < 0)
3960 if (unlink(path
) < 0 &&
3962 virReportSystemError(errno
, _("Unable to remove %s"), path
);
3969 virObjectUnref(cfg
);
3975 qemuProcessVNCAllocatePorts(virQEMUDriverPtr driver
,
3976 virDomainGraphicsDefPtr graphics
,
3979 unsigned short port
;
3982 if (graphics
->data
.vnc
.autoport
)
3983 graphics
->data
.vnc
.port
= 5900;
3988 if (graphics
->data
.vnc
.autoport
) {
3989 if (virPortAllocatorAcquire(driver
->remotePorts
, &port
) < 0)
3991 graphics
->data
.vnc
.port
= port
;
3994 if (graphics
->data
.vnc
.websocket
== -1) {
3995 if (virPortAllocatorAcquire(driver
->webSocketPorts
, &port
) < 0)
3997 graphics
->data
.vnc
.websocket
= port
;
3998 graphics
->data
.vnc
.websocketGenerated
= true;
4005 qemuProcessSPICEAllocatePorts(virQEMUDriverPtr driver
,
4006 virDomainGraphicsDefPtr graphics
,
4009 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
4010 unsigned short port
= 0;
4011 unsigned short tlsPort
;
4013 int defaultMode
= graphics
->data
.spice
.defaultMode
;
4016 bool needTLSPort
= false;
4017 bool needPort
= false;
4019 if (graphics
->data
.spice
.autoport
) {
4020 /* check if tlsPort or port need allocation */
4021 for (i
= 0; i
< VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_LAST
; i
++) {
4022 switch (graphics
->data
.spice
.channels
[i
]) {
4023 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE
:
4027 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE
:
4031 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY
:
4032 /* default mode will be used */
4036 switch (defaultMode
) {
4037 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE
:
4041 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE
:
4045 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY
:
4054 if (needPort
|| graphics
->data
.spice
.port
== -1)
4055 graphics
->data
.spice
.port
= 5901;
4057 if (needTLSPort
|| graphics
->data
.spice
.tlsPort
== -1)
4058 graphics
->data
.spice
.tlsPort
= 5902;
4064 if (needPort
|| graphics
->data
.spice
.port
== -1) {
4065 if (virPortAllocatorAcquire(driver
->remotePorts
, &port
) < 0)
4068 graphics
->data
.spice
.port
= port
;
4070 if (!graphics
->data
.spice
.autoport
)
4071 graphics
->data
.spice
.portReserved
= true;
4074 if (needTLSPort
|| graphics
->data
.spice
.tlsPort
== -1) {
4075 if (!cfg
->spiceTLS
) {
4076 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
4077 _("Auto allocation of spice TLS port requested "
4078 "but spice TLS is disabled in qemu.conf"));
4082 if (virPortAllocatorAcquire(driver
->remotePorts
, &tlsPort
) < 0)
4085 graphics
->data
.spice
.tlsPort
= tlsPort
;
4087 if (!graphics
->data
.spice
.autoport
)
4088 graphics
->data
.spice
.tlsPortReserved
= true;
4094 virObjectUnref(cfg
);
4100 qemuProcessVerifyHypervFeatures(virDomainDefPtr def
,
4107 for (i
= 0; i
< VIR_DOMAIN_HYPERV_LAST
; i
++) {
4108 /* always supported string property */
4109 if (i
== VIR_DOMAIN_HYPERV_VENDOR_ID
||
4110 i
== VIR_DOMAIN_HYPERV_SPINLOCKS
)
4113 if (def
->hyperv_features
[i
] != VIR_TRISTATE_SWITCH_ON
)
4116 if (virAsprintf(&cpuFeature
, "hv-%s",
4117 virDomainHypervTypeToString(i
)) < 0)
4120 rc
= virCPUDataCheckFeature(cpu
, cpuFeature
);
4121 VIR_FREE(cpuFeature
);
4125 } else if (rc
== 1) {
4126 if (i
== VIR_DOMAIN_HYPERV_STIMER
) {
4127 if (def
->hyperv_stimer_direct
!= VIR_TRISTATE_SWITCH_ON
)
4130 rc
= virCPUDataCheckFeature(cpu
, VIR_CPU_x86_HV_STIMER_DIRECT
);
4136 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
4137 _("host doesn't support hyperv stimer '%s' feature"),
4144 switch ((virDomainHyperv
) i
) {
4145 case VIR_DOMAIN_HYPERV_RELAXED
:
4146 case VIR_DOMAIN_HYPERV_VAPIC
:
4147 VIR_WARN("host doesn't support hyperv '%s' feature",
4148 virDomainHypervTypeToString(i
));
4151 case VIR_DOMAIN_HYPERV_VPINDEX
:
4152 case VIR_DOMAIN_HYPERV_RUNTIME
:
4153 case VIR_DOMAIN_HYPERV_SYNIC
:
4154 case VIR_DOMAIN_HYPERV_STIMER
:
4155 case VIR_DOMAIN_HYPERV_RESET
:
4156 case VIR_DOMAIN_HYPERV_FREQUENCIES
:
4157 case VIR_DOMAIN_HYPERV_REENLIGHTENMENT
:
4158 case VIR_DOMAIN_HYPERV_TLBFLUSH
:
4159 case VIR_DOMAIN_HYPERV_IPI
:
4160 case VIR_DOMAIN_HYPERV_EVMCS
:
4161 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
4162 _("host doesn't support hyperv '%s' feature"),
4163 virDomainHypervTypeToString(i
));
4166 /* coverity[dead_error_begin] */
4167 case VIR_DOMAIN_HYPERV_SPINLOCKS
:
4168 case VIR_DOMAIN_HYPERV_VENDOR_ID
:
4169 case VIR_DOMAIN_HYPERV_LAST
:
4179 qemuProcessVerifyKVMFeatures(virDomainDefPtr def
,
4184 if (def
->features
[VIR_DOMAIN_FEATURE_PVSPINLOCK
] != VIR_TRISTATE_SWITCH_ON
)
4187 rc
= virCPUDataCheckFeature(cpu
, VIR_CPU_x86_KVM_PV_UNHALT
);
4191 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
4192 _("host doesn't support paravirtual spinlocks"));
4201 qemuProcessVerifyCPUFeatures(virDomainDefPtr def
,
4206 rc
= virCPUCheckFeature(def
->os
.arch
, def
->cpu
, "invtsc");
4210 } else if (rc
== 1) {
4211 rc
= virCPUDataCheckFeature(cpu
, "invtsc");
4214 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
4215 _("host doesn't support invariant TSC"));
4226 qemuProcessTranslateCPUFeatures(const char *name
,
4229 virQEMUCapsPtr qemuCaps
= opaque
;
4231 return virQEMUCapsCPUFeatureFromQEMU(qemuCaps
, name
);
4236 qemuProcessFetchGuestCPU(virQEMUDriverPtr driver
,
4238 qemuDomainAsyncJob asyncJob
,
4239 virCPUDataPtr
*enabled
,
4240 virCPUDataPtr
*disabled
)
4242 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
4243 virCPUDataPtr dataEnabled
= NULL
;
4244 virCPUDataPtr dataDisabled
= NULL
;
4251 generic
= virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_CPU_UNAVAILABLE_FEATURES
);
4253 if (!generic
&& !ARCH_IS_X86(vm
->def
->os
.arch
))
4256 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
4260 rc
= qemuMonitorGetGuestCPU(priv
->mon
,
4262 qemuProcessTranslateCPUFeatures
, priv
->qemuCaps
,
4263 &dataEnabled
, &dataDisabled
);
4265 rc
= qemuMonitorGetGuestCPUx86(priv
->mon
, &dataEnabled
, &dataDisabled
);
4268 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
4274 *enabled
= dataEnabled
;
4275 *disabled
= dataDisabled
;
4279 virCPUDataFree(dataEnabled
);
4280 virCPUDataFree(dataDisabled
);
4286 qemuProcessVerifyCPU(virDomainObjPtr vm
,
4289 virDomainDefPtr def
= vm
->def
;
4294 if (qemuProcessVerifyKVMFeatures(def
, cpu
) < 0 ||
4295 qemuProcessVerifyHypervFeatures(def
, cpu
) < 0)
4299 (def
->cpu
->mode
== VIR_CPU_MODE_CUSTOM
&&
4303 if (qemuProcessVerifyCPUFeatures(def
, cpu
) < 0)
4311 qemuProcessUpdateLiveGuestCPU(virDomainObjPtr vm
,
4312 virCPUDataPtr enabled
,
4313 virCPUDataPtr disabled
)
4315 virDomainDefPtr def
= vm
->def
;
4316 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
4317 virCPUDefPtr orig
= NULL
;
4325 (def
->cpu
->mode
== VIR_CPU_MODE_CUSTOM
&&
4329 if (!(orig
= virCPUDefCopy(def
->cpu
)))
4332 if ((rc
= virCPUUpdateLive(def
->os
.arch
, def
->cpu
, enabled
, disabled
)) < 0) {
4334 } else if (rc
== 0) {
4335 /* Store the original CPU in priv if QEMU changed it and we didn't
4336 * get the original CPU via migration, restore, or snapshot revert.
4338 if (!priv
->origCPU
&& !virCPUDefIsEqual(def
->cpu
, orig
, false))
4339 VIR_STEAL_PTR(priv
->origCPU
, orig
);
4341 def
->cpu
->check
= VIR_CPU_CHECK_FULL
;
4347 virCPUDefFree(orig
);
4353 qemuProcessUpdateAndVerifyCPU(virQEMUDriverPtr driver
,
4355 qemuDomainAsyncJob asyncJob
)
4357 virCPUDataPtr cpu
= NULL
;
4358 virCPUDataPtr disabled
= NULL
;
4361 if (qemuProcessFetchGuestCPU(driver
, vm
, asyncJob
, &cpu
, &disabled
) < 0)
4364 if (qemuProcessVerifyCPU(vm
, cpu
) < 0)
4367 if (qemuProcessUpdateLiveGuestCPU(vm
, cpu
, disabled
) < 0)
4373 virCPUDataFree(cpu
);
4374 virCPUDataFree(disabled
);
4379 static virDomainCapsCPUModelsPtr
4380 qemuProcessFetchCPUDefinitions(virQEMUDriverPtr driver
,
4382 qemuDomainAsyncJob asyncJob
)
4384 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
4385 virDomainCapsCPUModelsPtr models
= NULL
;
4387 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
4390 models
= virQEMUCapsFetchCPUDefinitions(priv
->mon
);
4392 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
4398 virObjectUnref(models
);
4404 qemuProcessUpdateCPU(virQEMUDriverPtr driver
,
4406 qemuDomainAsyncJob asyncJob
)
4408 virCPUDataPtr cpu
= NULL
;
4409 virCPUDataPtr disabled
= NULL
;
4410 virDomainCapsCPUModelsPtr models
= NULL
;
4413 /* The host CPU model comes from host caps rather than QEMU caps so
4414 * fallback must be allowed no matter what the user specified in the XML.
4416 vm
->def
->cpu
->fallback
= VIR_CPU_FALLBACK_ALLOW
;
4418 if (qemuProcessFetchGuestCPU(driver
, vm
, asyncJob
, &cpu
, &disabled
) < 0)
4421 if (qemuProcessUpdateLiveGuestCPU(vm
, cpu
, disabled
) < 0)
4424 if (!(models
= qemuProcessFetchCPUDefinitions(driver
, vm
, asyncJob
)) ||
4425 virCPUTranslate(vm
->def
->os
.arch
, vm
->def
->cpu
, models
) < 0)
4431 virCPUDataFree(cpu
);
4432 virCPUDataFree(disabled
);
4433 virObjectUnref(models
);
4439 qemuPrepareNVRAM(virQEMUDriverConfigPtr cfg
,
4445 virDomainLoaderDefPtr loader
= vm
->def
->os
.loader
;
4446 bool created
= false;
4447 const char *master_nvram_path
;
4450 if (!loader
|| !loader
->nvram
|| virFileExists(loader
->nvram
))
4453 master_nvram_path
= loader
->templt
;
4454 if (!loader
->templt
) {
4456 for (i
= 0; i
< cfg
->nfirmwares
; i
++) {
4457 if (STREQ(cfg
->firmwares
[i
]->name
, loader
->path
)) {
4458 master_nvram_path
= cfg
->firmwares
[i
]->nvram
;
4464 if (!master_nvram_path
) {
4465 virReportError(VIR_ERR_OPERATION_FAILED
,
4466 _("unable to find any master var store for "
4467 "loader: %s"), loader
->path
);
4471 if ((srcFD
= virFileOpenAs(master_nvram_path
, O_RDONLY
,
4472 0, -1, -1, 0)) < 0) {
4473 virReportSystemError(-srcFD
,
4474 _("Failed to open file '%s'"),
4478 if ((dstFD
= virFileOpenAs(loader
->nvram
,
4479 O_WRONLY
| O_CREAT
| O_EXCL
,
4481 cfg
->user
, cfg
->group
, 0)) < 0) {
4482 virReportSystemError(-dstFD
,
4483 _("Failed to create file '%s'"),
4492 if ((r
= saferead(srcFD
, buf
, sizeof(buf
))) < 0) {
4493 virReportSystemError(errno
,
4494 _("Unable to read from file '%s'"),
4499 if (safewrite(dstFD
, buf
, r
) < 0) {
4500 virReportSystemError(errno
,
4501 _("Unable to write to file '%s'"),
4507 if (VIR_CLOSE(srcFD
) < 0) {
4508 virReportSystemError(errno
,
4509 _("Unable to close file '%s'"),
4513 if (VIR_CLOSE(dstFD
) < 0) {
4514 virReportSystemError(errno
,
4515 _("Unable to close file '%s'"),
4522 /* We successfully generated the nvram path, but failed to
4523 * copy the file content. Roll back. */
4526 unlink(loader
->nvram
);
4529 VIR_FORCE_CLOSE(srcFD
);
4530 VIR_FORCE_CLOSE(dstFD
);
4536 qemuLogOperation(virDomainObjPtr vm
,
4539 qemuDomainLogContextPtr logCtxt
)
4542 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
4543 int qemuVersion
= virQEMUCapsGetVersion(priv
->qemuCaps
);
4544 const char *package
= virQEMUCapsGetPackage(priv
->qemuCaps
);
4545 char *hostname
= virGetHostname();
4550 if ((timestamp
= virTimeStringNow()) == NULL
)
4553 if (qemuDomainLogContextWrite(logCtxt
,
4554 "%s: %s %s, qemu version: %d.%d.%d%s, kernel: %s, hostname: %s\n",
4555 timestamp
, msg
, VIR_LOG_VERSION_STRING
,
4556 (qemuVersion
/ 1000000) % 1000,
4557 (qemuVersion
/ 1000) % 1000,
4559 NULLSTR_EMPTY(package
),
4561 NULLSTR_EMPTY(hostname
)) < 0)
4565 char *args
= virCommandToString(cmd
, true);
4566 qemuDomainLogContextWrite(logCtxt
, "%s\n", args
);
4572 VIR_FREE(timestamp
);
4577 qemuProcessIncomingDefFree(qemuProcessIncomingDefPtr inc
)
4582 VIR_FREE(inc
->address
);
4583 VIR_FREE(inc
->launchURI
);
4584 VIR_FREE(inc
->deferredURI
);
4590 * This function does not copy @path, the caller is responsible for keeping
4591 * the @path pointer valid during the lifetime of the allocated
4592 * qemuProcessIncomingDef structure.
4594 * The caller is responsible for closing @fd, calling
4595 * qemuProcessIncomingDefFree will NOT close it.
4597 qemuProcessIncomingDefPtr
4598 qemuProcessIncomingDefNew(virQEMUCapsPtr qemuCaps
,
4599 const char *listenAddress
,
4600 const char *migrateFrom
,
4604 qemuProcessIncomingDefPtr inc
= NULL
;
4606 if (qemuMigrationDstCheckProtocol(qemuCaps
, migrateFrom
) < 0)
4609 if (VIR_ALLOC(inc
) < 0)
4612 if (VIR_STRDUP(inc
->address
, listenAddress
) < 0)
4615 inc
->launchURI
= qemuMigrationDstGetURI(migrateFrom
, fd
);
4616 if (!inc
->launchURI
)
4619 if (virQEMUCapsGet(qemuCaps
, QEMU_CAPS_INCOMING_DEFER
)) {
4620 inc
->deferredURI
= inc
->launchURI
;
4621 if (VIR_STRDUP(inc
->launchURI
, "defer") < 0)
4631 qemuProcessIncomingDefFree(inc
);
4637 * This function starts a new QEMU_ASYNC_JOB_START async job. The user is
4638 * responsible for calling qemuProcessEndJob to stop this job and for passing
4639 * QEMU_ASYNC_JOB_START as @asyncJob argument to any function requiring this
4640 * parameter between qemuProcessBeginJob and qemuProcessEndJob.
4643 qemuProcessBeginJob(virQEMUDriverPtr driver
,
4645 virDomainJobOperation operation
,
4646 unsigned long apiFlags
)
4648 if (qemuDomainObjBeginAsyncJob(driver
, vm
, QEMU_ASYNC_JOB_START
,
4649 operation
, apiFlags
) < 0)
4652 qemuDomainObjSetAsyncJobMask(vm
, QEMU_JOB_NONE
);
4658 qemuProcessEndJob(virQEMUDriverPtr driver
,
4661 qemuDomainObjEndAsyncJob(driver
, vm
);
4666 qemuProcessStartHook(virQEMUDriverPtr driver
,
4668 virHookQemuOpType op
,
4669 virHookSubopType subop
)
4671 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
4675 if (!virHookPresent(VIR_HOOK_DRIVER_QEMU
))
4678 if (!(xml
= qemuDomainDefFormatXML(driver
, priv
->qemuCaps
, vm
->def
, 0)))
4681 ret
= virHookCall(VIR_HOOK_DRIVER_QEMU
, vm
->def
->name
, op
, subop
,
4690 qemuProcessGraphicsReservePorts(virDomainGraphicsDefPtr graphics
,
4693 virDomainGraphicsListenDefPtr glisten
;
4695 if (graphics
->nListens
<= 0)
4698 glisten
= &graphics
->listens
[0];
4700 if (glisten
->type
!= VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS
&&
4701 glisten
->type
!= VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK
)
4704 switch (graphics
->type
) {
4705 case VIR_DOMAIN_GRAPHICS_TYPE_VNC
:
4706 if (!graphics
->data
.vnc
.autoport
||
4708 if (virPortAllocatorSetUsed(graphics
->data
.vnc
.port
) < 0)
4710 graphics
->data
.vnc
.portReserved
= true;
4712 if (graphics
->data
.vnc
.websocket
> 0 &&
4713 virPortAllocatorSetUsed(graphics
->data
.vnc
.websocket
) < 0)
4717 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE
:
4718 if (graphics
->data
.spice
.autoport
&& !reconnect
)
4721 if (graphics
->data
.spice
.port
> 0) {
4722 if (virPortAllocatorSetUsed(graphics
->data
.spice
.port
) < 0)
4724 graphics
->data
.spice
.portReserved
= true;
4727 if (graphics
->data
.spice
.tlsPort
> 0) {
4728 if (virPortAllocatorSetUsed(graphics
->data
.spice
.tlsPort
) < 0)
4730 graphics
->data
.spice
.tlsPortReserved
= true;
4734 case VIR_DOMAIN_GRAPHICS_TYPE_SDL
:
4735 case VIR_DOMAIN_GRAPHICS_TYPE_RDP
:
4736 case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP
:
4737 case VIR_DOMAIN_GRAPHICS_TYPE_EGL_HEADLESS
:
4738 case VIR_DOMAIN_GRAPHICS_TYPE_LAST
:
4747 qemuProcessGraphicsAllocatePorts(virQEMUDriverPtr driver
,
4748 virDomainGraphicsDefPtr graphics
,
4751 virDomainGraphicsListenDefPtr glisten
;
4753 if (graphics
->nListens
<= 0)
4756 glisten
= &graphics
->listens
[0];
4758 if (glisten
->type
!= VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS
&&
4759 glisten
->type
!= VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK
)
4762 switch (graphics
->type
) {
4763 case VIR_DOMAIN_GRAPHICS_TYPE_VNC
:
4764 if (qemuProcessVNCAllocatePorts(driver
, graphics
, allocate
) < 0)
4768 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE
:
4769 if (qemuProcessSPICEAllocatePorts(driver
, graphics
, allocate
) < 0)
4773 case VIR_DOMAIN_GRAPHICS_TYPE_SDL
:
4774 case VIR_DOMAIN_GRAPHICS_TYPE_RDP
:
4775 case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP
:
4776 case VIR_DOMAIN_GRAPHICS_TYPE_EGL_HEADLESS
:
4777 case VIR_DOMAIN_GRAPHICS_TYPE_LAST
:
4785 qemuProcessGetNetworkAddress(const char *netname
,
4788 virConnectPtr conn
= NULL
;
4791 virNetworkDefPtr netdef
= NULL
;
4792 virNetworkIPDefPtr ipdef
;
4794 virSocketAddrPtr addrptr
= NULL
;
4795 char *dev_name
= NULL
;
4800 if (!(conn
= virGetConnectNetwork()))
4803 net
= virNetworkLookupByName(conn
, netname
);
4807 xml
= virNetworkGetXMLDesc(net
, 0);
4811 netdef
= virNetworkDefParseString(xml
, NULL
);
4815 switch ((virNetworkForwardType
) netdef
->forward
.type
) {
4816 case VIR_NETWORK_FORWARD_NONE
:
4817 case VIR_NETWORK_FORWARD_NAT
:
4818 case VIR_NETWORK_FORWARD_ROUTE
:
4819 case VIR_NETWORK_FORWARD_OPEN
:
4820 ipdef
= virNetworkDefGetIPByIndex(netdef
, AF_UNSPEC
, 0);
4822 virReportError(VIR_ERR_INTERNAL_ERROR
,
4823 _("network '%s' doesn't have an IP address"),
4827 addrptr
= &ipdef
->address
;
4830 case VIR_NETWORK_FORWARD_BRIDGE
:
4831 if ((dev_name
= netdef
->bridge
))
4834 * fall through if netdef->bridge wasn't set, since that is
4835 * macvtap bridge mode network.
4837 ATTRIBUTE_FALLTHROUGH
;
4839 case VIR_NETWORK_FORWARD_PRIVATE
:
4840 case VIR_NETWORK_FORWARD_VEPA
:
4841 case VIR_NETWORK_FORWARD_PASSTHROUGH
:
4842 if ((netdef
->forward
.nifs
> 0) && netdef
->forward
.ifs
)
4843 dev_name
= netdef
->forward
.ifs
[0].device
.dev
;
4846 virReportError(VIR_ERR_INTERNAL_ERROR
,
4847 _("network '%s' has no associated interface or bridge"),
4853 case VIR_NETWORK_FORWARD_HOSTDEV
:
4856 case VIR_NETWORK_FORWARD_LAST
:
4858 virReportEnumRangeError(virNetworkForwardType
, netdef
->forward
.type
);
4863 if (virNetDevIPAddrGet(dev_name
, &addr
) < 0)
4869 (*netaddr
= virSocketAddrFormat(addrptr
)))) {
4875 virNetworkDefFree(netdef
);
4876 virObjectUnref(net
);
4877 virObjectUnref(conn
);
4884 qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr glisten
,
4885 const char *listenAddr
)
4889 /* TODO: reject configuration without network specified for network listen */
4890 if (!glisten
->network
) {
4891 if (VIR_STRDUP(glisten
->address
, listenAddr
) < 0)
4896 rc
= qemuProcessGetNetworkAddress(glisten
->network
, &glisten
->address
);
4898 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
4899 _("network-based listen isn't possible, "
4900 "network driver isn't present"));
4911 qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver
,
4912 virDomainGraphicsDefPtr graphics
,
4915 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
4916 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
4917 const char *type
= virDomainGraphicsTypeToString(graphics
->type
);
4918 char *listenAddr
= NULL
;
4919 bool useSocket
= false;
4923 switch (graphics
->type
) {
4924 case VIR_DOMAIN_GRAPHICS_TYPE_VNC
:
4925 useSocket
= cfg
->vncAutoUnixSocket
;
4926 listenAddr
= cfg
->vncListen
;
4929 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE
:
4930 useSocket
= cfg
->spiceAutoUnixSocket
;
4931 listenAddr
= cfg
->spiceListen
;
4934 case VIR_DOMAIN_GRAPHICS_TYPE_SDL
:
4935 case VIR_DOMAIN_GRAPHICS_TYPE_RDP
:
4936 case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP
:
4937 case VIR_DOMAIN_GRAPHICS_TYPE_EGL_HEADLESS
:
4938 case VIR_DOMAIN_GRAPHICS_TYPE_LAST
:
4942 for (i
= 0; i
< graphics
->nListens
; i
++) {
4943 virDomainGraphicsListenDefPtr glisten
= &graphics
->listens
[i
];
4945 switch (glisten
->type
) {
4946 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS
:
4947 if (!glisten
->address
) {
4948 /* If there is no address specified and qemu.conf has
4949 * *_auto_unix_socket set we should use unix socket as
4950 * default instead of tcp listen. */
4952 memset(glisten
, 0, sizeof(virDomainGraphicsListenDef
));
4953 if (virAsprintf(&glisten
->socket
, "%s/%s.sock",
4954 priv
->libDir
, type
) < 0)
4956 glisten
->fromConfig
= true;
4957 glisten
->type
= VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_SOCKET
;
4958 } else if (listenAddr
) {
4959 if (VIR_STRDUP(glisten
->address
, listenAddr
) < 0)
4961 glisten
->fromConfig
= true;
4966 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK
:
4967 if (glisten
->address
|| !listenAddr
)
4970 if (qemuProcessGraphicsSetupNetworkAddress(glisten
,
4975 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_SOCKET
:
4976 if (!glisten
->socket
) {
4977 if (virAsprintf(&glisten
->socket
, "%s/%s.sock",
4978 priv
->libDir
, type
) < 0)
4980 glisten
->autoGenerated
= true;
4984 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NONE
:
4985 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_LAST
:
4993 virObjectUnref(cfg
);
4999 qemuProcessGraphicsSetupRenderNode(virDomainGraphicsDefPtr graphics
,
5000 virQEMUCapsPtr qemuCaps
)
5002 char **rendernode
= NULL
;
5004 if (!virDomainGraphicsNeedsAutoRenderNode(graphics
))
5007 /* Don't bother picking a DRM node if QEMU doesn't support it. */
5008 if (graphics
->type
== VIR_DOMAIN_GRAPHICS_TYPE_SPICE
) {
5009 if (!virQEMUCapsGet(qemuCaps
, QEMU_CAPS_SPICE_RENDERNODE
))
5012 rendernode
= &graphics
->data
.spice
.rendernode
;
5014 if (!virQEMUCapsGet(qemuCaps
, QEMU_CAPS_EGL_HEADLESS_RENDERNODE
))
5017 rendernode
= &graphics
->data
.egl_headless
.rendernode
;
5020 if (!(*rendernode
= virHostGetDRMRenderNode()))
5028 qemuProcessSetupGraphics(virQEMUDriverPtr driver
,
5030 virQEMUCapsPtr qemuCaps
,
5033 virDomainGraphicsDefPtr graphics
;
5034 bool allocate
= !(flags
& VIR_QEMU_PROCESS_START_PRETEND
);
5038 for (i
= 0; i
< vm
->def
->ngraphics
; i
++) {
5039 graphics
= vm
->def
->graphics
[i
];
5041 if (qemuProcessGraphicsSetupRenderNode(graphics
, qemuCaps
) < 0)
5044 if (qemuProcessGraphicsSetupListen(driver
, graphics
, vm
) < 0)
5049 for (i
= 0; i
< vm
->def
->ngraphics
; i
++) {
5050 graphics
= vm
->def
->graphics
[i
];
5052 if (qemuProcessGraphicsReservePorts(graphics
, false) < 0)
5057 for (i
= 0; i
< vm
->def
->ngraphics
; ++i
) {
5058 graphics
= vm
->def
->graphics
[i
];
5060 if (qemuProcessGraphicsAllocatePorts(driver
, graphics
, allocate
) < 0)
5072 qemuProcessSetupRawIO(virQEMUDriverPtr driver
,
5074 virCommandPtr cmd ATTRIBUTE_UNUSED
)
5080 /* in case a certain disk is desirous of CAP_SYS_RAWIO, add this */
5081 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
5082 virDomainDeviceDef dev
;
5083 virDomainDiskDefPtr disk
= vm
->def
->disks
[i
];
5085 if (disk
->rawio
== VIR_TRISTATE_BOOL_YES
) {
5087 #ifndef CAP_SYS_RAWIO
5092 dev
.type
= VIR_DOMAIN_DEVICE_DISK
;
5093 dev
.data
.disk
= disk
;
5094 if (qemuAddSharedDevice(driver
, &dev
, vm
->def
->name
) < 0)
5097 if (qemuSetUnprivSGIO(&dev
) < 0)
5101 /* If rawio not already set, check hostdevs as well */
5103 for (i
= 0; i
< vm
->def
->nhostdevs
; i
++) {
5104 if (!virHostdevIsSCSIDevice(vm
->def
->hostdevs
[i
]))
5107 virDomainHostdevSubsysSCSIPtr scsisrc
=
5108 &vm
->def
->hostdevs
[i
]->source
.subsys
.u
.scsi
;
5109 if (scsisrc
->rawio
== VIR_TRISTATE_BOOL_YES
) {
5120 #ifdef CAP_SYS_RAWIO
5122 virCommandAllowCap(cmd
, CAP_SYS_RAWIO
);
5124 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5125 _("Raw I/O is not supported on this platform"));
5134 qemuProcessSetupBalloon(virQEMUDriverPtr driver
,
5136 qemuDomainAsyncJob asyncJob
)
5138 unsigned long long balloon
= vm
->def
->mem
.cur_balloon
;
5139 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
5142 if (!virDomainDefHasMemballoon(vm
->def
))
5145 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
5148 if (vm
->def
->memballoon
->period
)
5149 qemuMonitorSetMemoryStatsPeriod(priv
->mon
, vm
->def
->memballoon
,
5150 vm
->def
->memballoon
->period
);
5151 if (qemuMonitorSetBalloon(priv
->mon
, balloon
) < 0)
5157 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
5164 qemuProcessMakeDir(virQEMUDriverPtr driver
,
5170 if (virFileMakePathWithMode(path
, 0750) < 0) {
5171 virReportSystemError(errno
, _("Cannot create directory '%s'"), path
);
5175 if (qemuSecurityDomainSetPathLabel(driver
, vm
, path
, true) < 0)
5186 qemuProcessStartWarnShmem(virDomainObjPtr vm
)
5189 bool check_shmem
= false;
5190 bool shmem
= vm
->def
->nshmems
;
5193 * For vhost-user to work, the domain has to have some type of
5194 * shared memory configured. We're not the proper ones to judge
5195 * whether shared hugepages or shm are enough and will be in the
5196 * future, so we'll just warn in case neither is configured.
5197 * Moreover failing would give the false illusion that libvirt is
5198 * really checking that everything works before running the domain
5199 * and not only we are unable to do that, but it's also not our
5202 for (i
= 0; i
< vm
->def
->nnets
; i
++) {
5203 if (virDomainNetGetActualType(vm
->def
->nets
[i
]) ==
5204 VIR_DOMAIN_NET_TYPE_VHOSTUSER
) {
5214 * This check is by no means complete. We merely check
5215 * whether there are *some* hugepages enabled and *some* NUMA
5216 * nodes with shared memory access.
5218 if (!shmem
&& vm
->def
->mem
.nhugepages
) {
5219 for (i
= 0; i
< virDomainNumaGetNodeCount(vm
->def
->numa
); i
++) {
5220 if (virDomainNumaGetNodeMemoryAccessMode(vm
->def
->numa
, i
) ==
5221 VIR_DOMAIN_MEMORY_ACCESS_SHARED
) {
5229 VIR_WARN("Detected vhost-user interface without any shared memory, "
5230 "the interface might not be operational");
5236 qemuProcessStartValidateGraphics(virDomainObjPtr vm
)
5240 for (i
= 0; i
< vm
->def
->ngraphics
; i
++) {
5241 virDomainGraphicsDefPtr graphics
= vm
->def
->graphics
[i
];
5243 switch (graphics
->type
) {
5244 case VIR_DOMAIN_GRAPHICS_TYPE_VNC
:
5245 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE
:
5246 if (graphics
->nListens
> 1) {
5247 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5248 _("QEMU does not support multiple listens for "
5249 "one graphics device."));
5254 case VIR_DOMAIN_GRAPHICS_TYPE_SDL
:
5255 case VIR_DOMAIN_GRAPHICS_TYPE_RDP
:
5256 case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP
:
5257 case VIR_DOMAIN_GRAPHICS_TYPE_EGL_HEADLESS
:
5258 case VIR_DOMAIN_GRAPHICS_TYPE_LAST
:
5268 qemuProcessStartValidateVideo(virDomainObjPtr vm
,
5269 virQEMUCapsPtr qemuCaps
)
5272 virDomainVideoDefPtr video
;
5274 for (i
= 0; i
< vm
->def
->nvideos
; i
++) {
5275 video
= vm
->def
->videos
[i
];
5277 if ((video
->type
== VIR_DOMAIN_VIDEO_TYPE_VGA
&&
5278 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_DEVICE_VGA
)) ||
5279 (video
->type
== VIR_DOMAIN_VIDEO_TYPE_CIRRUS
&&
5280 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_DEVICE_CIRRUS_VGA
)) ||
5281 (video
->type
== VIR_DOMAIN_VIDEO_TYPE_VMVGA
&&
5282 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_DEVICE_VMWARE_SVGA
)) ||
5283 (video
->type
== VIR_DOMAIN_VIDEO_TYPE_QXL
&&
5284 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_DEVICE_QXL
)) ||
5285 (video
->type
== VIR_DOMAIN_VIDEO_TYPE_VIRTIO
&&
5286 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_DEVICE_VIRTIO_GPU
)) ||
5287 (video
->type
== VIR_DOMAIN_VIDEO_TYPE_VIRTIO
&&
5288 video
->info
.type
== VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW
&&
5289 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_DEVICE_VIRTIO_GPU_CCW
))) {
5290 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5291 _("this QEMU does not support '%s' video device"),
5292 virDomainVideoTypeToString(video
->type
));
5297 if (video
->accel
->accel3d
== VIR_TRISTATE_SWITCH_ON
&&
5298 (video
->type
!= VIR_DOMAIN_VIDEO_TYPE_VIRTIO
||
5299 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_VIRTIO_GPU_VIRGL
))) {
5300 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5301 _("%s 3d acceleration is not supported"),
5302 virDomainVideoTypeToString(video
->type
));
5313 qemuProcessStartValidateIOThreads(virDomainObjPtr vm
,
5314 virQEMUCapsPtr qemuCaps
)
5318 if (vm
->def
->niothreadids
> 0 &&
5319 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_OBJECT_IOTHREAD
)) {
5320 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5321 _("IOThreads not supported for this QEMU"));
5325 for (i
= 0; i
< vm
->def
->ncontrollers
; i
++) {
5326 virDomainControllerDefPtr cont
= vm
->def
->controllers
[i
];
5328 if (cont
->type
== VIR_DOMAIN_CONTROLLER_TYPE_SCSI
&&
5329 cont
->model
== VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VIRTIO_SCSI
&&
5330 cont
->iothread
> 0 &&
5331 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_VIRTIO_SCSI_IOTHREAD
)) {
5332 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5333 _("IOThreads for virtio-scsi not supported for "
5344 qemuProcessStartValidateShmem(virDomainObjPtr vm
)
5348 for (i
= 0; i
< vm
->def
->nshmems
; i
++) {
5349 virDomainShmemDefPtr shmem
= vm
->def
->shmems
[i
];
5351 if (strchr(shmem
->name
, '/')) {
5352 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5353 _("shmem name '%s' must not contain '/'"),
5364 qemuProcessStartValidateDisks(virDomainObjPtr vm
,
5365 virQEMUCapsPtr qemuCaps
)
5369 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
5370 virDomainDiskDefPtr disk
= vm
->def
->disks
[i
];
5371 virStorageSourcePtr src
= disk
->src
;
5373 /* This is a best effort check as we can only check if the command
5374 * option exists, but we cannot determine whether the running QEMU
5375 * was build with '--enable-vxhs'. */
5376 if (src
->type
== VIR_STORAGE_TYPE_NETWORK
&&
5377 src
->protocol
== VIR_STORAGE_NET_PROTOCOL_VXHS
&&
5378 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_VXHS
)) {
5379 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5380 _("VxHS protocol is not supported with this "
5385 /* PowerPC pseries based VMs do not support floppy device */
5386 if (disk
->device
== VIR_DOMAIN_DISK_DEVICE_FLOPPY
&&
5387 qemuDomainIsPSeries(vm
->def
)) {
5388 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5389 _("PowerPC pseries machines do not support floppy device"));
5399 qemuProcessStartValidateTSC(virDomainObjPtr vm
,
5403 unsigned long long freq
= 0;
5404 virHostCPUTscInfoPtr tsc
;
5406 for (i
= 0; i
< vm
->def
->clock
.ntimers
; i
++) {
5407 virDomainTimerDefPtr timer
= vm
->def
->clock
.timers
[i
];
5409 if (timer
->name
== VIR_DOMAIN_TIMER_NAME_TSC
&&
5410 timer
->frequency
> 0) {
5411 freq
= timer
->frequency
;
5419 VIR_DEBUG("Requested TSC frequency %llu Hz", freq
);
5421 if (!caps
->host
.cpu
|| !caps
->host
.cpu
->tsc
) {
5422 VIR_DEBUG("Host TSC frequency could not be probed");
5426 tsc
= caps
->host
.cpu
->tsc
;
5427 VIR_DEBUG("Host TSC frequency %llu Hz, scaling %s",
5428 tsc
->frequency
, virTristateBoolTypeToString(tsc
->scaling
));
5430 if (freq
== tsc
->frequency
|| tsc
->scaling
== VIR_TRISTATE_BOOL_YES
)
5433 if (tsc
->scaling
== VIR_TRISTATE_BOOL_ABSENT
) {
5434 VIR_DEBUG("TSC frequencies do not match and scaling support is "
5435 "unknown, QEMU will try and possibly fail later");
5439 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5440 _("Requested TSC frequency %llu Hz does not match "
5441 "host (%llu Hz) and TSC scaling is not supported "
5443 freq
, tsc
->frequency
);
5449 * qemuProcessStartValidate:
5450 * @vm: domain object
5451 * @qemuCaps: emulator capabilities
5452 * @migration: restoration of existing state
5454 * This function aggregates checks done prior to start of a VM.
5456 * Flag VIR_QEMU_PROCESS_START_PRETEND tells, that we don't want to actually
5457 * start the domain but create a valid qemu command. If some code shouldn't be
5458 * executed in this case, make sure to check this flag.
5461 qemuProcessStartValidate(virQEMUDriverPtr driver
,
5463 virQEMUCapsPtr qemuCaps
,
5467 if (!(flags
& VIR_QEMU_PROCESS_START_PRETEND
)) {
5468 if (vm
->def
->virtType
== VIR_DOMAIN_VIRT_KVM
) {
5469 VIR_DEBUG("Checking for KVM availability");
5470 if (!virFileExists("/dev/kvm")) {
5471 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5472 _("Domain requires KVM, but it is not available. "
5473 "Check that virtualization is enabled in the "
5474 "host BIOS, and host configuration is setup to "
5475 "load the kvm modules."));
5480 VIR_DEBUG("Checking domain and device security labels");
5481 if (qemuSecurityCheckAllLabel(driver
->securityManager
, vm
->def
) < 0)
5486 /* Checks below should not be executed when starting a qemu process for a
5487 * VM that was running before (migration, snapshots, save). It's more
5488 * important to start such VM than keep the configuration clean */
5489 if ((flags
& VIR_QEMU_PROCESS_START_NEW
) &&
5490 virDomainDefValidate(vm
->def
, caps
, 0, driver
->xmlopt
) < 0)
5493 if (qemuProcessStartValidateGraphics(vm
) < 0)
5496 if (qemuProcessStartValidateVideo(vm
, qemuCaps
) < 0)
5499 if (qemuProcessStartValidateIOThreads(vm
, qemuCaps
) < 0)
5502 if (qemuProcessStartValidateShmem(vm
) < 0)
5506 if (virCPUValidateFeatures(vm
->def
->os
.arch
, vm
->def
->cpu
) < 0)
5509 if (ARCH_IS_X86(vm
->def
->os
.arch
) &&
5510 !virQEMUCapsGet(qemuCaps
, QEMU_CAPS_CPU_UNAVAILABLE_FEATURES
)) {
5511 VIR_AUTOSTRINGLIST features
= NULL
;
5514 if ((n
= virCPUDefCheckFeatures(vm
->def
->cpu
,
5515 virCPUx86FeatureFilterSelectMSR
,
5521 VIR_AUTOFREE(char *) str
= NULL
;
5523 str
= virStringListJoin((const char **)features
, ", ");
5524 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5525 _("Some features cannot be reliably used "
5526 "with this QEMU: %s"), str
);
5532 if (qemuProcessStartValidateDisks(vm
, qemuCaps
) < 0)
5535 if (qemuProcessStartValidateTSC(vm
, caps
) < 0)
5538 VIR_DEBUG("Checking for any possible (non-fatal) issues");
5540 qemuProcessStartWarnShmem(vm
);
5547 qemuProcessStartUpdateCustomCaps(virDomainObjPtr vm
)
5549 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
5550 VIR_AUTOUNREF(virQEMUDriverConfigPtr
) cfg
= virQEMUDriverGetConfig(priv
->driver
);
5551 qemuDomainXmlNsDefPtr nsdef
= vm
->def
->namespaceData
;
5556 if (cfg
->capabilityfilters
) {
5557 for (next
= cfg
->capabilityfilters
; *next
; next
++) {
5558 if ((tmp
= virQEMUCapsTypeFromString(*next
)) < 0) {
5559 virReportError(VIR_ERR_INTERNAL_ERROR
,
5560 _("invalid capability_filters capability '%s'"),
5565 virQEMUCapsClear(priv
->qemuCaps
, tmp
);
5570 for (i
= 0; i
< nsdef
->ncapsadd
; i
++) {
5571 if ((tmp
= virQEMUCapsTypeFromString(nsdef
->capsadd
[i
])) < 0) {
5572 virReportError(VIR_ERR_INTERNAL_ERROR
,
5573 _("invalid qemu namespace capability '%s'"),
5578 virQEMUCapsSet(priv
->qemuCaps
, tmp
);
5581 for (i
= 0; i
< nsdef
->ncapsdel
; i
++) {
5582 if ((tmp
= virQEMUCapsTypeFromString(nsdef
->capsdel
[i
])) < 0) {
5583 virReportError(VIR_ERR_INTERNAL_ERROR
,
5584 _("invalid qemu namespace capability '%s'"),
5589 virQEMUCapsClear(priv
->qemuCaps
, tmp
);
5600 * Prepares the domain up to the point when priv->qemuCaps is initialized. The
5601 * function calls qemuProcessStop when needed.
5603 * Flag VIR_QEMU_PROCESS_START_PRETEND tells, that we don't want to actually
5604 * start the domain but create a valid qemu command. If some code shouldn't be
5605 * executed in this case, make sure to check this flag.
5607 * Returns 0 on success, -1 on error.
5610 qemuProcessInit(virQEMUDriverPtr driver
,
5612 virCPUDefPtr updatedCPU
,
5613 qemuDomainAsyncJob asyncJob
,
5617 virCapsPtr caps
= NULL
;
5618 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
5620 virCPUDefPtr origCPU
= NULL
;
5623 VIR_DEBUG("vm=%p name=%s id=%d migration=%d",
5624 vm
, vm
->def
->name
, vm
->def
->id
, migration
);
5626 VIR_DEBUG("Beginning VM startup process");
5628 if (virDomainObjIsActive(vm
)) {
5629 virReportError(VIR_ERR_OPERATION_INVALID
, "%s",
5630 _("VM is already active"));
5634 if (!(caps
= virQEMUDriverGetCapabilities(driver
, false)))
5637 /* in case when the post parse callback failed we need to re-run it on the
5638 * old config prior we start the VM */
5639 if (vm
->def
->postParseFailed
) {
5640 VIR_DEBUG("re-running the post parse callback");
5642 if (virDomainDefPostParse(vm
->def
, caps
, 0, driver
->xmlopt
, priv
->qemuCaps
) < 0)
5646 VIR_DEBUG("Determining emulator version");
5647 if (qemuDomainUpdateQEMUCaps(vm
, driver
->qemuCapsCache
) < 0)
5650 if (flags
& VIR_QEMU_PROCESS_START_STANDALONE
)
5651 virQEMUCapsClear(priv
->qemuCaps
, QEMU_CAPS_CHARDEV_FD_PASS
);
5653 if (qemuDomainUpdateCPU(vm
, updatedCPU
, &origCPU
) < 0)
5656 if (qemuProcessStartValidate(driver
, vm
, priv
->qemuCaps
, caps
, flags
) < 0)
5659 /* Do this upfront, so any part of the startup process can add
5660 * runtime state to vm->def that won't be persisted. This let's us
5661 * report implicit runtime defaults in the XML, like vnc listen/socket
5663 VIR_DEBUG("Setting current domain def as transient");
5664 if (virDomainObjSetDefTransient(caps
, driver
->xmlopt
, vm
, priv
->qemuCaps
) < 0)
5667 /* Update qemu capabilities according to lists passed in via namespace */
5668 if (qemuProcessStartUpdateCustomCaps(vm
) < 0)
5671 if (flags
& VIR_QEMU_PROCESS_START_PRETEND
) {
5672 if (qemuDomainSetPrivatePaths(driver
, vm
) < 0) {
5673 virDomainObjRemoveTransientDef(vm
);
5677 vm
->def
->id
= qemuDriverAllocateID(driver
);
5678 qemuDomainSetFakeReboot(driver
, vm
, false);
5679 virDomainObjSetState(vm
, VIR_DOMAIN_PAUSED
, VIR_DOMAIN_PAUSED_STARTING_UP
);
5681 if (virAtomicIntInc(&driver
->nactive
) == 1 && driver
->inhibitCallback
)
5682 driver
->inhibitCallback(true, driver
->inhibitOpaque
);
5684 /* Run an early hook to set-up missing devices */
5685 if (qemuProcessStartHook(driver
, vm
,
5686 VIR_HOOK_QEMU_OP_PREPARE
,
5687 VIR_HOOK_SUBOP_BEGIN
) < 0)
5690 if (qemuDomainSetPrivatePaths(driver
, vm
) < 0)
5693 VIR_STEAL_PTR(priv
->origCPU
, origCPU
);
5699 virCPUDefFree(origCPU
);
5700 virObjectUnref(caps
);
5704 stopFlags
= VIR_QEMU_PROCESS_STOP_NO_RELABEL
;
5706 stopFlags
|= VIR_QEMU_PROCESS_STOP_MIGRATED
;
5707 qemuProcessStop(driver
, vm
, VIR_DOMAIN_SHUTOFF_FAILED
, asyncJob
, stopFlags
);
5713 * qemuProcessNetworkPrepareDevices
5716 qemuProcessNetworkPrepareDevices(virDomainDefPtr def
)
5720 virConnectPtr conn
= NULL
;
5722 for (i
= 0; i
< def
->nnets
; i
++) {
5723 virDomainNetDefPtr net
= def
->nets
[i
];
5724 virDomainNetType actualType
;
5726 /* If appropriate, grab a physical device from the configured
5727 * network's pool of devices, or resolve bridge device name
5728 * to the one defined in the network definition.
5730 if (net
->type
== VIR_DOMAIN_NET_TYPE_NETWORK
) {
5731 if (!conn
&& !(conn
= virGetConnectNetwork()))
5733 if (virDomainNetAllocateActualDevice(conn
, def
, net
) < 0)
5737 actualType
= virDomainNetGetActualType(net
);
5738 if (actualType
== VIR_DOMAIN_NET_TYPE_HOSTDEV
&&
5739 net
->type
== VIR_DOMAIN_NET_TYPE_NETWORK
) {
5740 /* Each type='hostdev' network device must also have a
5741 * corresponding entry in the hostdevs array. For netdevs
5742 * that are hardcoded as type='hostdev', this is already
5743 * done by the parser, but for those allocated from a
5744 * network / determined at runtime, we need to do it
5747 virDomainHostdevDefPtr hostdev
= virDomainNetGetActualHostdev(net
);
5748 virDomainHostdevSubsysPCIPtr pcisrc
= &hostdev
->source
.subsys
.u
.pci
;
5750 if (virDomainHostdevFind(def
, hostdev
, NULL
) >= 0) {
5751 virReportError(VIR_ERR_INTERNAL_ERROR
,
5752 _("PCI device %04x:%02x:%02x.%x "
5753 "allocated from network %s is already "
5754 "in use by domain %s"),
5755 pcisrc
->addr
.domain
, pcisrc
->addr
.bus
,
5756 pcisrc
->addr
.slot
, pcisrc
->addr
.function
,
5757 net
->data
.network
.name
, def
->name
);
5760 if (virDomainHostdevInsert(def
, hostdev
) < 0)
5766 virObjectUnref(conn
);
5772 * qemuProcessSetupVcpu:
5773 * @vm: domain object
5774 * @vcpuid: id of VCPU to set defaults
5776 * This function sets resource properties (cgroups, affinity, scheduler) for a
5777 * vCPU. This function expects that the vCPU is online and the vCPU pids were
5778 * correctly detected at the point when it's called.
5780 * Returns 0 on success, -1 on error.
5783 qemuProcessSetupVcpu(virDomainObjPtr vm
,
5784 unsigned int vcpuid
)
5786 pid_t vcpupid
= qemuDomainGetVcpuPid(vm
, vcpuid
);
5787 virDomainVcpuDefPtr vcpu
= virDomainDefGetVcpu(vm
->def
, vcpuid
);
5788 virDomainResctrlMonDefPtr mon
= NULL
;
5791 if (qemuProcessSetupPid(vm
, vcpupid
, VIR_CGROUP_THREAD_VCPU
,
5792 vcpuid
, vcpu
->cpumask
,
5793 vm
->def
->cputune
.period
,
5794 vm
->def
->cputune
.quota
,
5798 for (i
= 0; i
< vm
->def
->nresctrls
; i
++) {
5800 virDomainResctrlDefPtr ct
= vm
->def
->resctrls
[i
];
5802 if (virBitmapIsBitSet(ct
->vcpus
, vcpuid
)) {
5803 if (virResctrlAllocAddPID(ct
->alloc
, vcpupid
) < 0)
5806 for (j
= 0; j
< ct
->nmonitors
; j
++) {
5807 mon
= ct
->monitors
[j
];
5809 if (virBitmapEqual(ct
->vcpus
, mon
->vcpus
) &&
5810 !virResctrlAllocIsEmpty(ct
->alloc
))
5813 if (virBitmapIsBitSet(mon
->vcpus
, vcpuid
)) {
5814 if (virResctrlMonitorAddPID(mon
->instance
, vcpupid
) < 0)
5829 qemuProcessSetupVcpus(virDomainObjPtr vm
)
5831 virDomainVcpuDefPtr vcpu
;
5832 unsigned int maxvcpus
= virDomainDefGetVcpusMax(vm
->def
);
5835 if ((vm
->def
->cputune
.period
|| vm
->def
->cputune
.quota
) &&
5836 !virCgroupHasController(((qemuDomainObjPrivatePtr
) vm
->privateData
)->cgroup
,
5837 VIR_CGROUP_CONTROLLER_CPU
)) {
5838 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
, "%s",
5839 _("cgroup cpu is required for scheduler tuning"));
5843 if (!qemuDomainHasVcpuPids(vm
)) {
5844 /* If any CPU has custom affinity that differs from the
5845 * VM default affinity, we must reject it */
5846 for (i
= 0; i
< maxvcpus
; i
++) {
5847 vcpu
= virDomainDefGetVcpu(vm
->def
, i
);
5852 if (vcpu
->cpumask
&&
5853 !virBitmapEqual(vm
->def
->cpumask
, vcpu
->cpumask
)) {
5854 virReportError(VIR_ERR_OPERATION_INVALID
, "%s",
5855 _("cpu affinity is not supported"));
5863 for (i
= 0; i
< maxvcpus
; i
++) {
5864 vcpu
= virDomainDefGetVcpu(vm
->def
, i
);
5869 if (qemuProcessSetupVcpu(vm
, i
) < 0)
5878 qemuProcessSetupIOThread(virDomainObjPtr vm
,
5879 virDomainIOThreadIDDefPtr iothread
)
5881 return qemuProcessSetupPid(vm
, iothread
->thread_id
,
5882 VIR_CGROUP_THREAD_IOTHREAD
,
5883 iothread
->iothread_id
,
5885 vm
->def
->cputune
.iothread_period
,
5886 vm
->def
->cputune
.iothread_quota
,
5892 qemuProcessSetupIOThreads(virDomainObjPtr vm
)
5896 for (i
= 0; i
< vm
->def
->niothreadids
; i
++) {
5897 virDomainIOThreadIDDefPtr info
= vm
->def
->iothreadids
[i
];
5899 if (qemuProcessSetupIOThread(vm
, info
) < 0)
5908 qemuProcessValidateHotpluggableVcpus(virDomainDefPtr def
)
5910 virDomainVcpuDefPtr vcpu
;
5911 virDomainVcpuDefPtr subvcpu
;
5912 qemuDomainVcpuPrivatePtr vcpupriv
;
5913 unsigned int maxvcpus
= virDomainDefGetVcpusMax(def
);
5916 virBitmapPtr ordermap
= NULL
;
5919 if (!(ordermap
= virBitmapNew(maxvcpus
+ 1)))
5923 * - all hotpluggable entities to be hotplugged have the correct data
5924 * - vcpus belonging to a hotpluggable entity share configuration
5925 * - order of the hotpluggable entities is unique
5927 for (i
= 0; i
< maxvcpus
; i
++) {
5928 vcpu
= virDomainDefGetVcpu(def
, i
);
5929 vcpupriv
= QEMU_DOMAIN_VCPU_PRIVATE(vcpu
);
5931 /* skip over hotpluggable entities */
5932 if (vcpupriv
->vcpus
== 0)
5935 if (vcpu
->order
!= 0) {
5936 if (virBitmapIsBitSet(ordermap
, vcpu
->order
)) {
5937 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5938 _("duplicate vcpu order '%u'"), vcpu
->order
);
5942 if (virBitmapSetBit(ordermap
, vcpu
->order
)) {
5943 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5944 _("vcpu order '%u' exceeds vcpu count"),
5950 for (j
= i
+ 1; j
< (i
+ vcpupriv
->vcpus
); j
++) {
5951 subvcpu
= virDomainDefGetVcpu(def
, j
);
5952 if (subvcpu
->hotpluggable
!= vcpu
->hotpluggable
||
5953 subvcpu
->online
!= vcpu
->online
||
5954 subvcpu
->order
!= vcpu
->order
) {
5955 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5956 _("vcpus '%zu' and '%zu' are in the same hotplug "
5957 "group but differ in configuration"), i
, j
);
5962 if (vcpu
->online
&& vcpu
->hotpluggable
== VIR_TRISTATE_BOOL_YES
) {
5963 if ((vcpupriv
->socket_id
== -1 && vcpupriv
->core_id
== -1 &&
5964 vcpupriv
->thread_id
== -1 && vcpupriv
->node_id
== -1) ||
5966 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
5967 _("vcpu '%zu' is missing hotplug data"), i
);
5975 virBitmapFree(ordermap
);
5981 qemuDomainHasHotpluggableStartupVcpus(virDomainDefPtr def
)
5983 size_t maxvcpus
= virDomainDefGetVcpusMax(def
);
5984 virDomainVcpuDefPtr vcpu
;
5987 for (i
= 0; i
< maxvcpus
; i
++) {
5988 vcpu
= virDomainDefGetVcpu(def
, i
);
5990 if (vcpu
->online
&& vcpu
->hotpluggable
== VIR_TRISTATE_BOOL_YES
)
5999 qemuProcessVcpusSortOrder(const void *a
,
6002 virDomainVcpuDefPtr vcpua
= *((virDomainVcpuDefPtr
*)a
);
6003 virDomainVcpuDefPtr vcpub
= *((virDomainVcpuDefPtr
*)b
);
6005 return vcpua
->order
- vcpub
->order
;
6010 qemuProcessSetupHotpluggableVcpus(virQEMUDriverPtr driver
,
6012 qemuDomainAsyncJob asyncJob
)
6014 unsigned int maxvcpus
= virDomainDefGetVcpusMax(vm
->def
);
6015 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6016 qemuCgroupEmulatorAllNodesDataPtr emulatorCgroup
= NULL
;
6017 virDomainVcpuDefPtr vcpu
;
6018 qemuDomainVcpuPrivatePtr vcpupriv
;
6019 virJSONValuePtr vcpuprops
= NULL
;
6024 virDomainVcpuDefPtr
*bootHotplug
= NULL
;
6025 size_t nbootHotplug
= 0;
6027 for (i
= 0; i
< maxvcpus
; i
++) {
6028 vcpu
= virDomainDefGetVcpu(vm
->def
, i
);
6029 vcpupriv
= QEMU_DOMAIN_VCPU_PRIVATE(vcpu
);
6031 if (vcpu
->hotpluggable
== VIR_TRISTATE_BOOL_YES
&& vcpu
->online
&&
6032 vcpupriv
->vcpus
!= 0) {
6033 if (virAsprintf(&vcpupriv
->alias
, "vcpu%zu", i
) < 0)
6036 if (VIR_APPEND_ELEMENT(bootHotplug
, nbootHotplug
, vcpu
) < 0)
6041 if (nbootHotplug
== 0) {
6046 qsort(bootHotplug
, nbootHotplug
, sizeof(*bootHotplug
),
6047 qemuProcessVcpusSortOrder
);
6049 if (qemuCgroupEmulatorAllNodesAllow(priv
->cgroup
, &emulatorCgroup
) < 0)
6052 for (i
= 0; i
< nbootHotplug
; i
++) {
6053 vcpu
= bootHotplug
[i
];
6055 if (!(vcpuprops
= qemuBuildHotpluggableCPUProps(vcpu
)))
6058 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
6061 rc
= qemuMonitorAddDeviceArgs(qemuDomainGetMonitor(vm
), vcpuprops
);
6064 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
6070 virJSONValueFree(vcpuprops
);
6076 qemuCgroupEmulatorAllNodesRestore(emulatorCgroup
);
6077 VIR_FREE(bootHotplug
);
6078 virJSONValueFree(vcpuprops
);
6084 qemuProcessUpdateGuestCPU(virDomainDefPtr def
,
6085 virQEMUCapsPtr qemuCaps
,
6092 /* nothing to do if only topology part of CPU def is used */
6093 if (def
->cpu
->mode
== VIR_CPU_MODE_CUSTOM
&& !def
->cpu
->model
)
6096 /* Old libvirt added host CPU model to host-model CPUs for migrations,
6097 * while new libvirt just turns host-model into custom mode. We need
6098 * to fix the mode to maintain backward compatibility and to avoid
6099 * the CPU model to be replaced in virCPUUpdate.
6101 if (!(flags
& VIR_QEMU_PROCESS_START_NEW
) &&
6102 ARCH_IS_X86(def
->os
.arch
) &&
6103 def
->cpu
->mode
== VIR_CPU_MODE_HOST_MODEL
&&
6105 def
->cpu
->mode
= VIR_CPU_MODE_CUSTOM
;
6108 if (!virQEMUCapsIsCPUModeSupported(qemuCaps
, caps
, def
->virtType
,
6110 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
6111 _("CPU mode '%s' for %s %s domain on %s host is not "
6112 "supported by hypervisor"),
6113 virCPUModeTypeToString(def
->cpu
->mode
),
6114 virArchToString(def
->os
.arch
),
6115 virDomainVirtTypeToString(def
->virtType
),
6116 virArchToString(caps
->host
.arch
));
6120 if (virCPUConvertLegacy(caps
->host
.arch
, def
->cpu
) < 0)
6123 /* nothing to update for host-passthrough */
6124 if (def
->cpu
->mode
!= VIR_CPU_MODE_HOST_PASSTHROUGH
) {
6125 if (def
->cpu
->check
== VIR_CPU_CHECK_PARTIAL
&&
6126 virCPUCompare(caps
->host
.arch
,
6127 virQEMUCapsGetHostModel(qemuCaps
, def
->virtType
,
6128 VIR_QEMU_CAPS_HOST_CPU_FULL
),
6129 def
->cpu
, true) < 0)
6132 if (virCPUUpdate(def
->os
.arch
, def
->cpu
,
6133 virQEMUCapsGetHostModel(qemuCaps
, def
->virtType
,
6134 VIR_QEMU_CAPS_HOST_CPU_MIGRATABLE
)) < 0)
6137 if (virCPUTranslate(def
->os
.arch
, def
->cpu
,
6138 virQEMUCapsGetCPUDefinitions(qemuCaps
, def
->virtType
)) < 0)
6141 def
->cpu
->fallback
= VIR_CPU_FALLBACK_FORBID
;
6144 if (virCPUDefFilterFeatures(def
->cpu
, virQEMUCapsCPUFilterFeatures
,
6153 qemuProcessPrepareDomainNUMAPlacement(virDomainObjPtr vm
,
6156 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6157 char *nodeset
= NULL
;
6158 virBitmapPtr numadNodeset
= NULL
;
6159 virBitmapPtr hostMemoryNodeset
= NULL
;
6162 /* Get the advisory nodeset from numad if 'placement' of
6163 * either <vcpu> or <numatune> is 'auto'.
6165 if (!virDomainDefNeedsPlacementAdvice(vm
->def
))
6168 nodeset
= virNumaGetAutoPlacementAdvice(virDomainDefGetVcpus(vm
->def
),
6169 virDomainDefGetMemoryTotal(vm
->def
));
6174 if (!(hostMemoryNodeset
= virNumaGetHostMemoryNodeset()))
6177 VIR_DEBUG("Nodeset returned from numad: %s", nodeset
);
6179 if (virBitmapParse(nodeset
, &numadNodeset
, VIR_DOMAIN_CPUMASK_LEN
) < 0)
6182 /* numad may return a nodeset that only contains cpus but cgroups don't play
6183 * well with that. Set the autoCpuset from all cpus from that nodeset, but
6184 * assign autoNodeset only with nodes containing memory. */
6185 if (!(priv
->autoCpuset
= virCapabilitiesGetCpusForNodemask(caps
, numadNodeset
)))
6188 virBitmapIntersect(numadNodeset
, hostMemoryNodeset
);
6190 VIR_STEAL_PTR(priv
->autoNodeset
, numadNodeset
);
6196 virBitmapFree(numadNodeset
);
6197 virBitmapFree(hostMemoryNodeset
);
6203 qemuProcessPrepareDomainStorage(virQEMUDriverPtr driver
,
6205 qemuDomainObjPrivatePtr priv
,
6206 virQEMUDriverConfigPtr cfg
,
6210 bool cold_boot
= flags
& VIR_QEMU_PROCESS_START_COLD
;
6212 for (i
= vm
->def
->ndisks
; i
> 0; i
--) {
6214 virDomainDiskDefPtr disk
= vm
->def
->disks
[idx
];
6216 if (virDomainDiskTranslateSourcePool(disk
) < 0) {
6217 if (qemuDomainCheckDiskStartupPolicy(driver
, vm
, idx
, cold_boot
) < 0)
6220 /* disk source was dropped */
6224 if (qemuDomainPrepareDiskSource(disk
, priv
, cfg
) < 0)
6233 qemuProcessPrepareAllowReboot(virDomainObjPtr vm
)
6235 virDomainDefPtr def
= vm
->def
;
6236 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6238 if (priv
->allowReboot
!= VIR_TRISTATE_BOOL_ABSENT
)
6241 if (def
->onReboot
== VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY
&&
6242 def
->onPoweroff
== VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY
&&
6243 (def
->onCrash
== VIR_DOMAIN_LIFECYCLE_ACTION_DESTROY
||
6244 def
->onCrash
== VIR_DOMAIN_LIFECYCLE_ACTION_COREDUMP_DESTROY
)) {
6245 priv
->allowReboot
= VIR_TRISTATE_BOOL_NO
;
6247 priv
->allowReboot
= VIR_TRISTATE_BOOL_YES
;
6253 * qemuProcessPrepareDomain:
6254 * @driver: qemu driver
6255 * @vm: domain object
6256 * @flags: qemuProcessStartFlags
6258 * This function groups all code that modifies only live XML of a domain which
6259 * is about to start and it's the only place to do those modifications.
6261 * Flag VIR_QEMU_PROCESS_START_PRETEND tells, that we don't want to actually
6262 * start the domain but create a valid qemu command. If some code shouldn't be
6263 * executed in this case, make sure to check this flag.
6265 * TODO: move all XML modification from qemuBuildCommandLine into this function
6268 qemuProcessPrepareDomain(virQEMUDriverPtr driver
,
6274 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6275 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
6278 if (!(caps
= virQEMUDriverGetCapabilities(driver
, false)))
6281 priv
->machineName
= qemuDomainGetMachineName(vm
);
6282 if (!priv
->machineName
)
6285 if (!(flags
& VIR_QEMU_PROCESS_START_PRETEND
)) {
6286 /* If you are using a SecurityDriver with dynamic labelling,
6287 then generate a security label for isolation */
6288 VIR_DEBUG("Generating domain security label (if required)");
6289 if (qemuSecurityGenLabel(driver
->securityManager
, vm
->def
) < 0) {
6290 virDomainAuditSecurityLabel(vm
, false);
6293 virDomainAuditSecurityLabel(vm
, true);
6295 if (qemuProcessPrepareDomainNUMAPlacement(vm
, caps
) < 0)
6299 /* Whether we should use virtlogd as stdio handler for character
6300 * devices source backend. */
6301 if (cfg
->stdioLogD
&&
6302 virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_CHARDEV_FILE_APPEND
)) {
6303 priv
->chardevStdioLogd
= true;
6306 /* Track if this domain remembers original owner */
6307 priv
->rememberOwner
= cfg
->rememberOwner
;
6309 qemuProcessPrepareAllowReboot(vm
);
6311 /* clear the 'blockdev' capability for VMs which have disks that need
6312 * -drive or which have floppies where we can't reliably get the QOM path */
6313 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
6314 if (qemuDiskBusNeedsDriveArg(vm
->def
->disks
[i
]->bus
)) {
6315 virQEMUCapsClear(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
);
6321 * Normally PCI addresses are assigned in the virDomainCreate
6322 * or virDomainDefine methods. We might still need to assign
6323 * some here to cope with the question of upgrades. Regardless
6324 * we also need to populate the PCI address set cache for later
6327 VIR_DEBUG("Assigning domain PCI addresses");
6328 if ((qemuDomainAssignAddresses(vm
->def
, priv
->qemuCaps
, driver
, vm
,
6329 !!(flags
& VIR_QEMU_PROCESS_START_NEW
))) < 0) {
6333 if (qemuAssignDeviceAliases(vm
->def
, priv
->qemuCaps
) < 0)
6336 VIR_DEBUG("Setting graphics devices");
6337 if (qemuProcessSetupGraphics(driver
, vm
, priv
->qemuCaps
, flags
) < 0)
6340 VIR_DEBUG("Create domain masterKey");
6341 if (qemuDomainMasterKeyCreate(vm
) < 0)
6344 VIR_DEBUG("Setting up storage");
6345 if (qemuProcessPrepareDomainStorage(driver
, vm
, priv
, cfg
, flags
) < 0)
6348 VIR_DEBUG("Prepare chardev source backends for TLS");
6349 qemuDomainPrepareChardevSource(vm
->def
, cfg
);
6351 VIR_DEBUG("Prepare device secrets");
6352 if (qemuDomainSecretPrepare(driver
, vm
) < 0)
6355 VIR_DEBUG("Prepare bios/uefi paths");
6356 if (qemuFirmwareFillDomain(driver
, vm
, flags
) < 0)
6359 for (i
= 0; i
< vm
->def
->nchannels
; i
++) {
6360 if (qemuDomainPrepareChannel(vm
->def
->channels
[i
],
6361 priv
->channelTargetDir
) < 0)
6365 if (!(priv
->monConfig
= virDomainChrSourceDefNew(driver
->xmlopt
)))
6368 VIR_DEBUG("Preparing monitor state");
6369 if (qemuProcessPrepareMonitorChr(priv
->monConfig
, priv
->libDir
) < 0)
6372 priv
->monError
= false;
6374 priv
->runningReason
= VIR_DOMAIN_RUNNING_UNKNOWN
;
6375 priv
->pausedReason
= VIR_DOMAIN_PAUSED_UNKNOWN
;
6377 VIR_DEBUG("Updating guest CPU definition");
6378 if (qemuProcessUpdateGuestCPU(vm
->def
, priv
->qemuCaps
, caps
, flags
) < 0)
6381 for (i
= 0; i
< vm
->def
->nshmems
; i
++) {
6382 if (qemuDomainPrepareShmemChardev(vm
->def
->shmems
[i
]) < 0)
6388 virObjectUnref(caps
);
6389 virObjectUnref(cfg
);
6395 qemuProcessSEVCreateFile(virDomainObjPtr vm
,
6399 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6400 virQEMUDriverPtr driver
= priv
->driver
;
6404 if (!(configFile
= virFileBuildPath(priv
->libDir
, name
, ".base64")))
6407 if (virFileRewriteStr(configFile
, S_IRUSR
| S_IWUSR
, data
) < 0) {
6408 virReportSystemError(errno
, _("failed to write data to config '%s'"),
6413 if (qemuSecurityDomainSetPathLabel(driver
, vm
, configFile
, true) < 0)
6418 VIR_FREE(configFile
);
6424 qemuProcessPrepareSEVGuestInput(virDomainObjPtr vm
)
6426 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6427 virDomainDefPtr def
= vm
->def
;
6428 virQEMUCapsPtr qemuCaps
= priv
->qemuCaps
;
6429 virDomainSEVDefPtr sev
= def
->sev
;
6434 VIR_DEBUG("Preparing SEV guest");
6436 if (!virQEMUCapsGet(qemuCaps
, QEMU_CAPS_SEV_GUEST
)) {
6437 virReportError(VIR_ERR_INTERNAL_ERROR
,
6438 _("Domain %s asked for 'sev' launch but this "
6439 "QEMU does not support SEV feature"), vm
->def
->name
);
6444 if (qemuProcessSEVCreateFile(vm
, "dh_cert", sev
->dh_cert
) < 0)
6449 if (qemuProcessSEVCreateFile(vm
, "session", sev
->session
) < 0)
6458 qemuProcessPrepareHostStorage(virQEMUDriverPtr driver
,
6462 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6464 bool cold_boot
= flags
& VIR_QEMU_PROCESS_START_COLD
;
6465 bool blockdev
= virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
);
6467 for (i
= vm
->def
->ndisks
; i
> 0; i
--) {
6469 virDomainDiskDefPtr disk
= vm
->def
->disks
[idx
];
6471 if (virStorageSourceIsEmpty(disk
->src
))
6474 /* backing chain needs to be redetected if we aren't using blockdev */
6476 virStorageSourceBackingStoreClear(disk
->src
);
6479 * Go to applying startup policy for optional disk with nonexistent
6480 * source file immediately as determining chain will surely fail
6481 * and we don't want noisy error notice in logs for this case.
6483 if (qemuDomainDiskIsMissingLocalOptional(disk
) && cold_boot
)
6484 VIR_INFO("optional disk '%s' source file is missing, "
6485 "skip checking disk chain", disk
->dst
);
6486 else if (qemuDomainDetermineDiskChain(driver
, vm
, disk
, NULL
, true) >= 0)
6489 if (qemuDomainCheckDiskStartupPolicy(driver
, vm
, idx
, cold_boot
) >= 0)
6500 qemuProcessOpenVhostVsock(virDomainVsockDefPtr vsock
)
6502 qemuDomainVsockPrivatePtr priv
= (qemuDomainVsockPrivatePtr
)vsock
->privateData
;
6503 const char *vsock_path
= "/dev/vhost-vsock";
6506 if ((fd
= open(vsock_path
, O_RDWR
)) < 0) {
6507 virReportError(VIR_ERR_CONFIG_UNSUPPORTED
,
6508 "%s", _("unable to open vhost-vsock device"));
6512 if (vsock
->auto_cid
== VIR_TRISTATE_BOOL_YES
) {
6513 if (virVsockAcquireGuestCid(fd
, &vsock
->guest_cid
) < 0)
6516 if (virVsockSetGuestCid(fd
, vsock
->guest_cid
) < 0)
6524 VIR_FORCE_CLOSE(fd
);
6530 * qemuProcessPrepareHost:
6531 * @driver: qemu driver
6532 * @vm: domain object
6533 * @flags: qemuProcessStartFlags
6535 * This function groups all code that modifies host system (which also may
6536 * update live XML) to prepare environment for a domain which is about to start
6537 * and it's the only place to do those modifications.
6539 * TODO: move all host modification from qemuBuildCommandLine into this function
6542 qemuProcessPrepareHost(virQEMUDriverPtr driver
,
6547 unsigned int hostdev_flags
= 0;
6548 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6549 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
6551 if (qemuPrepareNVRAM(cfg
, vm
) < 0)
6554 if (vm
->def
->vsock
) {
6555 if (qemuProcessOpenVhostVsock(vm
->def
->vsock
) < 0)
6558 /* network devices must be "prepared" before hostdevs, because
6559 * setting up a network device might create a new hostdev that
6560 * will need to be setup.
6562 VIR_DEBUG("Preparing network devices");
6563 if (qemuProcessNetworkPrepareDevices(vm
->def
) < 0)
6566 /* Must be run before security labelling */
6567 VIR_DEBUG("Preparing host devices");
6568 if (!cfg
->relaxedACS
)
6569 hostdev_flags
|= VIR_HOSTDEV_STRICT_ACS_CHECK
;
6570 if (flags
& VIR_QEMU_PROCESS_START_NEW
)
6571 hostdev_flags
|= VIR_HOSTDEV_COLD_BOOT
;
6572 if (qemuHostdevPrepareDomainDevices(driver
, vm
->def
, priv
->qemuCaps
,
6576 VIR_DEBUG("Preparing chr devices");
6577 if (virDomainChrDefForeach(vm
->def
,
6579 qemuProcessPrepareChardevDevice
,
6583 if (qemuProcessBuildDestroyMemoryPaths(driver
, vm
, NULL
, true) < 0)
6586 /* Ensure no historical cgroup for this VM is lying around bogus
6588 VIR_DEBUG("Ensuring no historical cgroup is lying around");
6589 qemuRemoveCgroup(vm
);
6591 if (virFileMakePath(cfg
->logDir
) < 0) {
6592 virReportSystemError(errno
,
6593 _("cannot create log directory %s"),
6598 VIR_FREE(priv
->pidfile
);
6599 if (!(priv
->pidfile
= virPidFileBuildPath(cfg
->stateDir
, vm
->def
->name
))) {
6600 virReportSystemError(errno
,
6601 "%s", _("Failed to build pidfile path."));
6605 if (unlink(priv
->pidfile
) < 0 &&
6607 virReportSystemError(errno
,
6608 _("Cannot remove stale PID file %s"),
6614 * Create all per-domain directories in order to make sure domain
6615 * with any possible seclabels can access it.
6617 if (qemuProcessMakeDir(driver
, vm
, priv
->libDir
) < 0 ||
6618 qemuProcessMakeDir(driver
, vm
, priv
->channelTargetDir
) < 0)
6621 VIR_DEBUG("Write domain masterKey");
6622 if (qemuDomainWriteMasterKeyFile(driver
, vm
) < 0)
6625 VIR_DEBUG("Preparing disks (host)");
6626 if (qemuProcessPrepareHostStorage(driver
, vm
, flags
) < 0)
6629 VIR_DEBUG("Preparing external devices");
6630 if (qemuExtDevicesPrepareHost(driver
, vm
->def
) < 0)
6633 if (qemuProcessPrepareSEVGuestInput(vm
) < 0)
6638 virObjectUnref(cfg
);
6645 * @vm: Pointer to domain object
6646 * @flags: qemuProcessStartFlags
6648 * If this domain is requesting to use genid, then update the GUID
6649 * value if the VIR_QEMU_PROCESS_START_GEN_VMID flag is set. This
6650 * flag is set on specific paths during domain start processing when
6651 * there is the possibility that the VM is potentially re-executing
6652 * something that has already been executed before.
6655 qemuProcessGenID(virDomainObjPtr vm
,
6658 if (!vm
->def
->genidRequested
)
6661 /* If we are coming from a path where we must provide a new gen id
6662 * value regardless of whether it was previously generated or provided,
6663 * then generate a new GUID value before we build the command line. */
6664 if (flags
& VIR_QEMU_PROCESS_START_GEN_VMID
) {
6665 if (virUUIDGenerate(vm
->def
->genid
) < 0) {
6666 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
6667 _("failed to regenerate genid"));
6677 * qemuProcessSetupDiskThrottlingBlockdev:
6679 * Sets up disk trottling for -blockdev via block_set_io_throttle monitor
6680 * command. This hack should be replaced by proper use of the 'throttle'
6681 * blockdev driver in qemu once it will support changing of the throttle group.
6684 qemuProcessSetupDiskThrottlingBlockdev(virQEMUDriverPtr driver
,
6686 qemuDomainAsyncJob asyncJob
)
6688 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6692 if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
))
6695 VIR_DEBUG("Setting up disk throttling for -blockdev via block_set_io_throttle");
6697 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) < 0)
6700 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
6701 virDomainDiskDefPtr disk
= vm
->def
->disks
[i
];
6702 qemuDomainDiskPrivatePtr diskPriv
= QEMU_DOMAIN_DISK_PRIVATE(disk
);
6704 if (!qemuDiskConfigBlkdeviotuneEnabled(disk
))
6707 if (qemuMonitorSetBlockIoThrottle(qemuDomainGetMonitor(vm
), NULL
,
6708 diskPriv
->qomName
, &disk
->blkdeviotune
,
6709 true, true, true) < 0)
6716 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
6723 * qemuProcessLaunch:
6725 * Launch a new QEMU process with stopped virtual CPUs.
6727 * The caller is supposed to call qemuProcessStop with appropriate
6728 * flags in case of failure.
6730 * Returns 0 on success,
6731 * -1 on error which happened before devices were labeled and thus
6732 * there is no need to restore them,
6733 * -2 on error requesting security labels to be restored.
6736 qemuProcessLaunch(virConnectPtr conn
,
6737 virQEMUDriverPtr driver
,
6739 qemuDomainAsyncJob asyncJob
,
6740 qemuProcessIncomingDefPtr incoming
,
6741 virDomainMomentObjPtr snapshot
,
6742 virNetDevVPortProfileOp vmop
,
6748 qemuDomainLogContextPtr logCtxt
= NULL
;
6749 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
6750 virCommandPtr cmd
= NULL
;
6751 struct qemuProcessHookData hookData
;
6752 virQEMUDriverConfigPtr cfg
;
6753 virCapsPtr caps
= NULL
;
6754 size_t nnicindexes
= 0;
6755 int *nicindexes
= NULL
;
6758 VIR_DEBUG("conn=%p driver=%p vm=%p name=%s if=%d asyncJob=%d "
6759 "incoming.launchURI=%s incoming.deferredURI=%s "
6760 "incoming.fd=%d incoming.path=%s "
6761 "snapshot=%p vmop=%d flags=0x%x",
6762 conn
, driver
, vm
, vm
->def
->name
, vm
->def
->id
, asyncJob
,
6763 NULLSTR(incoming
? incoming
->launchURI
: NULL
),
6764 NULLSTR(incoming
? incoming
->deferredURI
: NULL
),
6765 incoming
? incoming
->fd
: -1,
6766 NULLSTR(incoming
? incoming
->path
: NULL
),
6767 snapshot
, vmop
, flags
);
6769 /* Okay, these are just internal flags,
6770 * but doesn't hurt to check */
6771 virCheckFlags(VIR_QEMU_PROCESS_START_COLD
|
6772 VIR_QEMU_PROCESS_START_PAUSED
|
6773 VIR_QEMU_PROCESS_START_AUTODESTROY
|
6774 VIR_QEMU_PROCESS_START_NEW
|
6775 VIR_QEMU_PROCESS_START_GEN_VMID
, -1);
6777 cfg
= virQEMUDriverGetConfig(driver
);
6779 if ((flags
& VIR_QEMU_PROCESS_START_AUTODESTROY
) && !conn
) {
6780 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
6781 _("Domain autodestroy requires a connection handle"));
6786 hookData
.driver
= driver
;
6787 /* We don't increase cfg's reference counter here. */
6790 if (!(caps
= virQEMUDriverGetCapabilities(driver
, false)))
6793 VIR_DEBUG("Creating domain log file");
6794 if (!(logCtxt
= qemuDomainLogContextNew(driver
, vm
,
6795 QEMU_DOMAIN_LOG_CONTEXT_MODE_START
))) {
6796 virLastErrorPrefixMessage("%s", _("can't connect to virtlogd"));
6799 logfile
= qemuDomainLogContextGetWriteFD(logCtxt
);
6801 if (qemuProcessGenID(vm
, flags
) < 0)
6804 if (qemuExtDevicesStart(driver
, vm
, logCtxt
, incoming
!= NULL
) < 0)
6807 VIR_DEBUG("Building emulator command line");
6808 if (!(cmd
= qemuBuildCommandLine(driver
,
6809 qemuDomainLogContextGetManager(logCtxt
),
6810 driver
->securityManager
,
6812 incoming
? incoming
->launchURI
: NULL
,
6816 &nnicindexes
, &nicindexes
)))
6819 if (incoming
&& incoming
->fd
!= -1)
6820 virCommandPassFD(cmd
, incoming
->fd
, 0);
6822 /* now that we know it is about to start call the hook if present */
6823 if (qemuProcessStartHook(driver
, vm
,
6824 VIR_HOOK_QEMU_OP_START
,
6825 VIR_HOOK_SUBOP_BEGIN
) < 0)
6828 qemuLogOperation(vm
, "starting up", cmd
, logCtxt
);
6830 qemuDomainObjCheckTaint(driver
, vm
, logCtxt
);
6832 qemuDomainLogContextMarkPosition(logCtxt
);
6834 VIR_DEBUG("Building mount namespace");
6836 if (qemuDomainCreateNamespace(driver
, vm
) < 0)
6839 VIR_DEBUG("Clear emulator capabilities: %d",
6840 cfg
->clearEmulatorCapabilities
);
6841 if (cfg
->clearEmulatorCapabilities
)
6842 virCommandClearCaps(cmd
);
6844 VIR_DEBUG("Setting up raw IO");
6845 if (qemuProcessSetupRawIO(driver
, vm
, cmd
) < 0)
6848 virCommandSetPreExecHook(cmd
, qemuProcessHook
, &hookData
);
6849 virCommandSetMaxProcesses(cmd
, cfg
->maxProcesses
);
6850 virCommandSetMaxFiles(cmd
, cfg
->maxFiles
);
6851 virCommandSetMaxCoreSize(cmd
, cfg
->maxCore
);
6852 virCommandSetUmask(cmd
, 0x002);
6854 VIR_DEBUG("Setting up security labelling");
6855 if (qemuSecuritySetChildProcessLabel(driver
->securityManager
,
6859 virCommandSetOutputFD(cmd
, &logfile
);
6860 virCommandSetErrorFD(cmd
, &logfile
);
6861 virCommandNonblockingFDs(cmd
);
6862 virCommandSetPidFile(cmd
, priv
->pidfile
);
6863 virCommandDaemonize(cmd
);
6864 virCommandRequireHandshake(cmd
);
6866 if (qemuSecurityPreFork(driver
->securityManager
) < 0)
6868 rv
= virCommandRun(cmd
, NULL
);
6869 qemuSecurityPostFork(driver
->securityManager
);
6871 /* wait for qemu process to show up */
6873 if ((rv
= virPidFileReadPath(priv
->pidfile
, &vm
->pid
)) < 0) {
6874 virReportSystemError(-rv
,
6875 _("Domain %s didn't show up"),
6879 VIR_DEBUG("QEMU vm=%p name=%s running with pid=%lld",
6880 vm
, vm
->def
->name
, (long long)vm
->pid
);
6882 VIR_DEBUG("QEMU vm=%p name=%s failed to spawn",
6887 VIR_DEBUG("Writing early domain status to disk");
6888 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0)
6891 VIR_DEBUG("Waiting for handshake from child");
6892 if (virCommandHandshakeWait(cmd
) < 0) {
6893 /* Read errors from child that occurred between fork and exec. */
6894 qemuProcessReportLogError(logCtxt
,
6895 _("Process exited prior to exec"));
6899 VIR_DEBUG("Setting up domain cgroup (if required)");
6900 if (qemuSetupCgroup(vm
, nnicindexes
, nicindexes
) < 0)
6903 if (!(priv
->perf
= virPerfNew()))
6906 for (i
= 0; i
< VIR_PERF_EVENT_LAST
; i
++) {
6907 if (vm
->def
->perf
.events
[i
] == VIR_TRISTATE_BOOL_YES
&&
6908 virPerfEventEnable(priv
->perf
, i
, vm
->pid
) < 0)
6912 /* This must be done after cgroup placement to avoid resetting CPU
6914 if (qemuProcessInitCpuAffinity(vm
) < 0)
6917 VIR_DEBUG("Setting emulator tuning/settings");
6918 if (qemuProcessSetupEmulator(vm
) < 0)
6921 VIR_DEBUG("Setting cgroup for external devices (if required)");
6922 if (qemuSetupCgroupForExtDevices(vm
, driver
) < 0)
6925 VIR_DEBUG("Setting up resctrl");
6926 if (qemuProcessResctrlCreate(driver
, vm
) < 0)
6929 VIR_DEBUG("Setting up managed PR daemon");
6930 if (virDomainDefHasManagedPR(vm
->def
) &&
6931 qemuProcessStartManagedPRDaemon(vm
) < 0)
6934 VIR_DEBUG("Setting domain security labels");
6935 if (qemuSecuritySetAllLabel(driver
,
6937 incoming
? incoming
->path
: NULL
) < 0)
6940 /* Security manager labeled all devices, therefore
6941 * if any operation from now on fails, we need to ask the caller to
6946 if (incoming
&& incoming
->fd
!= -1) {
6947 /* if there's an fd to migrate from, and it's a pipe, put the
6948 * proper security label on it
6950 struct stat stdin_sb
;
6952 VIR_DEBUG("setting security label on pipe used for migration");
6954 if (fstat(incoming
->fd
, &stdin_sb
) < 0) {
6955 virReportSystemError(errno
,
6956 _("cannot stat fd %d"), incoming
->fd
);
6959 if (S_ISFIFO(stdin_sb
.st_mode
) &&
6960 qemuSecuritySetImageFDLabel(driver
->securityManager
,
6961 vm
->def
, incoming
->fd
) < 0)
6965 VIR_DEBUG("Labelling done, completing handshake to child");
6966 if (virCommandHandshakeNotify(cmd
) < 0)
6968 VIR_DEBUG("Handshake complete, child running");
6970 if (rv
== -1) /* The VM failed to start; tear filters before taps */
6971 virDomainConfVMNWFilterTeardown(vm
);
6973 if (rv
== -1) /* The VM failed to start */
6976 VIR_DEBUG("Waiting for monitor to show up");
6977 if (qemuProcessWaitForMonitor(driver
, vm
, asyncJob
, logCtxt
) < 0)
6980 if (qemuConnectAgent(driver
, vm
) < 0)
6983 VIR_DEBUG("Verifying and updating provided guest CPU");
6984 if (qemuProcessUpdateAndVerifyCPU(driver
, vm
, asyncJob
) < 0)
6987 VIR_DEBUG("setting up hotpluggable cpus");
6988 if (qemuDomainHasHotpluggableStartupVcpus(vm
->def
)) {
6989 if (qemuDomainRefreshVcpuInfo(driver
, vm
, asyncJob
, false) < 0)
6992 if (qemuProcessValidateHotpluggableVcpus(vm
->def
) < 0)
6995 if (qemuProcessSetupHotpluggableVcpus(driver
, vm
, asyncJob
) < 0)
6999 VIR_DEBUG("Refreshing VCPU info");
7000 if (qemuDomainRefreshVcpuInfo(driver
, vm
, asyncJob
, false) < 0)
7003 if (qemuDomainValidateVcpuInfo(vm
) < 0)
7006 qemuDomainVcpuPersistOrder(vm
->def
);
7008 VIR_DEBUG("Detecting IOThread PIDs");
7009 if (qemuProcessDetectIOThreadPIDs(driver
, vm
, asyncJob
) < 0)
7012 VIR_DEBUG("Setting global CPU cgroup (if required)");
7013 if (qemuSetupGlobalCpuCgroup(vm
) < 0)
7016 VIR_DEBUG("Setting vCPU tuning/settings");
7017 if (qemuProcessSetupVcpus(vm
) < 0)
7020 VIR_DEBUG("Setting IOThread tuning/settings");
7021 if (qemuProcessSetupIOThreads(vm
) < 0)
7024 VIR_DEBUG("Setting emulator scheduler");
7025 if (vm
->def
->cputune
.emulatorsched
&&
7026 virProcessSetScheduler(vm
->pid
,
7027 vm
->def
->cputune
.emulatorsched
->policy
,
7028 vm
->def
->cputune
.emulatorsched
->priority
) < 0)
7031 VIR_DEBUG("Setting any required VM passwords");
7032 if (qemuProcessInitPasswords(driver
, vm
, asyncJob
) < 0)
7035 /* set default link states */
7036 /* qemu doesn't support setting this on the command line, so
7037 * enter the monitor */
7038 VIR_DEBUG("Setting network link states");
7039 if (qemuProcessSetLinkStates(driver
, vm
, asyncJob
) < 0)
7042 VIR_DEBUG("Setting initial memory amount");
7043 if (qemuProcessSetupBalloon(driver
, vm
, asyncJob
) < 0)
7046 if (qemuProcessSetupDiskThrottlingBlockdev(driver
, vm
, asyncJob
) < 0)
7049 /* Since CPUs were not started yet, the balloon could not return the memory
7050 * to the host and thus cur_balloon needs to be updated so that GetXMLdesc
7051 * and friends return the correct size in case they can't grab the job */
7052 if (!incoming
&& !snapshot
&&
7053 qemuProcessRefreshBalloonState(driver
, vm
, asyncJob
) < 0)
7056 if (flags
& VIR_QEMU_PROCESS_START_AUTODESTROY
&&
7057 qemuProcessAutoDestroyAdd(driver
, vm
, conn
) < 0)
7064 qemuExtDevicesStop(driver
, vm
);
7065 qemuDomainSecretDestroy(vm
);
7066 virCommandFree(cmd
);
7067 virObjectUnref(logCtxt
);
7068 virObjectUnref(cfg
);
7069 virObjectUnref(caps
);
7070 VIR_FREE(nicindexes
);
7076 * qemuProcessRefreshState:
7077 * @driver: qemu driver data
7078 * @vm: domain to refresh
7079 * @asyncJob: async job type
7081 * This function gathers calls to refresh qemu state after startup. This
7082 * function is called after a deferred migration finishes so that we can update
7083 * state influenced by the migration stream.
7086 qemuProcessRefreshState(virQEMUDriverPtr driver
,
7088 qemuDomainAsyncJob asyncJob
)
7090 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7092 VIR_DEBUG("Fetching list of active devices");
7093 if (qemuDomainUpdateDeviceList(driver
, vm
, asyncJob
) < 0)
7096 VIR_DEBUG("Updating info of memory devices");
7097 if (qemuDomainUpdateMemoryDeviceInfo(driver
, vm
, asyncJob
) < 0)
7100 VIR_DEBUG("Detecting actual memory size for video device");
7101 if (qemuProcessUpdateVideoRamSize(driver
, vm
, asyncJob
) < 0)
7104 VIR_DEBUG("Updating disk data");
7105 if (qemuProcessRefreshDisks(driver
, vm
, asyncJob
) < 0)
7107 if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
) &&
7108 qemuBlockNodeNamesDetect(driver
, vm
, asyncJob
) < 0)
7116 * qemuProcessFinishStartup:
7118 * Finish starting a new domain.
7121 qemuProcessFinishStartup(virQEMUDriverPtr driver
,
7123 qemuDomainAsyncJob asyncJob
,
7125 virDomainPausedReason pausedReason
)
7127 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
7131 VIR_DEBUG("Starting domain CPUs");
7132 if (qemuProcessStartCPUs(driver
, vm
,
7133 VIR_DOMAIN_RUNNING_BOOTED
,
7135 if (virGetLastErrorCode() == VIR_ERR_OK
)
7136 virReportError(VIR_ERR_OPERATION_FAILED
, "%s",
7137 _("resume operation failed"));
7141 virDomainObjSetState(vm
, VIR_DOMAIN_PAUSED
, pausedReason
);
7144 VIR_DEBUG("Writing domain status to disk");
7145 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, vm
, driver
->caps
) < 0)
7148 if (qemuProcessStartHook(driver
, vm
,
7149 VIR_HOOK_QEMU_OP_STARTED
,
7150 VIR_HOOK_SUBOP_BEGIN
) < 0)
7156 virObjectUnref(cfg
);
7162 qemuProcessStart(virConnectPtr conn
,
7163 virQEMUDriverPtr driver
,
7165 virCPUDefPtr updatedCPU
,
7166 qemuDomainAsyncJob asyncJob
,
7167 const char *migrateFrom
,
7169 const char *migratePath
,
7170 virDomainMomentObjPtr snapshot
,
7171 virNetDevVPortProfileOp vmop
,
7174 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7175 qemuProcessIncomingDefPtr incoming
= NULL
;
7176 unsigned int stopFlags
;
7177 bool relabel
= false;
7181 VIR_DEBUG("conn=%p driver=%p vm=%p name=%s id=%d asyncJob=%s "
7182 "migrateFrom=%s migrateFd=%d migratePath=%s "
7183 "snapshot=%p vmop=%d flags=0x%x",
7184 conn
, driver
, vm
, vm
->def
->name
, vm
->def
->id
,
7185 qemuDomainAsyncJobTypeToString(asyncJob
),
7186 NULLSTR(migrateFrom
), migrateFd
, NULLSTR(migratePath
),
7187 snapshot
, vmop
, flags
);
7189 virCheckFlagsGoto(VIR_QEMU_PROCESS_START_COLD
|
7190 VIR_QEMU_PROCESS_START_PAUSED
|
7191 VIR_QEMU_PROCESS_START_AUTODESTROY
|
7192 VIR_QEMU_PROCESS_START_GEN_VMID
, cleanup
);
7194 if (!migrateFrom
&& !snapshot
)
7195 flags
|= VIR_QEMU_PROCESS_START_NEW
;
7197 if (qemuProcessInit(driver
, vm
, updatedCPU
,
7198 asyncJob
, !!migrateFrom
, flags
) < 0)
7202 incoming
= qemuProcessIncomingDefNew(priv
->qemuCaps
, NULL
, migrateFrom
,
7203 migrateFd
, migratePath
);
7208 if (qemuProcessPrepareDomain(driver
, vm
, flags
) < 0)
7211 if (qemuProcessPrepareHost(driver
, vm
, flags
) < 0)
7214 if ((rv
= qemuProcessLaunch(conn
, driver
, vm
, asyncJob
, incoming
,
7215 snapshot
, vmop
, flags
)) < 0) {
7223 if (incoming
->deferredURI
&&
7224 qemuMigrationDstRun(driver
, vm
, incoming
->deferredURI
, asyncJob
) < 0)
7227 /* Refresh state of devices from QEMU. During migration this happens
7228 * in qemuMigrationDstFinish to ensure that state information is fully
7230 if (qemuProcessRefreshState(driver
, vm
, asyncJob
) < 0)
7234 if (qemuProcessFinishStartup(driver
, vm
, asyncJob
,
7235 !(flags
& VIR_QEMU_PROCESS_START_PAUSED
),
7237 VIR_DOMAIN_PAUSED_MIGRATION
:
7238 VIR_DOMAIN_PAUSED_USER
) < 0)
7242 /* Keep watching qemu log for errors during incoming migration, otherwise
7243 * unset reporting errors from qemu log. */
7244 qemuMonitorSetDomainLog(priv
->mon
, NULL
, NULL
, NULL
);
7250 qemuProcessIncomingDefFree(incoming
);
7256 stopFlags
|= VIR_QEMU_PROCESS_STOP_NO_RELABEL
;
7258 stopFlags
|= VIR_QEMU_PROCESS_STOP_MIGRATED
;
7260 qemuMonitorSetDomainLog(priv
->mon
, NULL
, NULL
, NULL
);
7261 qemuProcessStop(driver
, vm
, VIR_DOMAIN_SHUTOFF_FAILED
, asyncJob
, stopFlags
);
7267 qemuProcessCreatePretendCmd(virQEMUDriverPtr driver
,
7269 const char *migrateURI
,
7274 virCommandPtr cmd
= NULL
;
7276 virCheckFlagsGoto(VIR_QEMU_PROCESS_START_COLD
|
7277 VIR_QEMU_PROCESS_START_PAUSED
|
7278 VIR_QEMU_PROCESS_START_AUTODESTROY
, cleanup
);
7280 flags
|= VIR_QEMU_PROCESS_START_PRETEND
;
7281 flags
|= VIR_QEMU_PROCESS_START_NEW
;
7283 flags
|= VIR_QEMU_PROCESS_START_STANDALONE
;
7285 if (qemuProcessInit(driver
, vm
, NULL
, QEMU_ASYNC_JOB_NONE
,
7286 !!migrateURI
, flags
) < 0)
7289 if (qemuProcessPrepareDomain(driver
, vm
, flags
) < 0)
7292 VIR_DEBUG("Building emulator command line");
7293 cmd
= qemuBuildCommandLine(driver
,
7295 driver
->securityManager
,
7299 VIR_NETDEV_VPORT_PROFILE_OP_NO_OP
,
7311 qemuProcessKill(virDomainObjPtr vm
, unsigned int flags
)
7315 VIR_DEBUG("vm=%p name=%s pid=%lld flags=0x%x",
7317 (long long)vm
->pid
, flags
);
7319 if (!(flags
& VIR_QEMU_PROCESS_KILL_NOCHECK
)) {
7320 if (!virDomainObjIsActive(vm
)) {
7321 VIR_DEBUG("VM '%s' not active", vm
->def
->name
);
7326 if (flags
& VIR_QEMU_PROCESS_KILL_NOWAIT
) {
7327 virProcessKill(vm
->pid
,
7328 (flags
& VIR_QEMU_PROCESS_KILL_FORCE
) ?
7333 /* Request an extra delay of two seconds per current nhostdevs
7334 * to be safe against stalls by the kernel freeing up the resources */
7335 ret
= virProcessKillPainfullyDelay(vm
->pid
,
7336 !!(flags
& VIR_QEMU_PROCESS_KILL_FORCE
),
7337 vm
->def
->nhostdevs
* 2);
7344 * qemuProcessBeginStopJob:
7346 * Stop all current jobs by killing the domain and start a new one for
7350 qemuProcessBeginStopJob(virQEMUDriverPtr driver
,
7355 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7356 unsigned int killFlags
= forceKill
? VIR_QEMU_PROCESS_KILL_FORCE
: 0;
7359 /* We need to prevent monitor EOF callback from doing our work (and
7360 * sending misleading events) while the vm is unlocked inside
7361 * BeginJob/ProcessKill API
7363 priv
->beingDestroyed
= true;
7365 if (qemuProcessKill(vm
, killFlags
) < 0)
7368 /* Wake up anything waiting on domain condition */
7369 virDomainObjBroadcast(vm
);
7371 if (qemuDomainObjBeginJob(driver
, vm
, job
) < 0)
7377 priv
->beingDestroyed
= false;
7382 void qemuProcessStop(virQEMUDriverPtr driver
,
7384 virDomainShutoffReason reason
,
7385 qemuDomainAsyncJob asyncJob
,
7390 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7391 virErrorPtr orig_err
;
7392 virDomainDefPtr def
;
7393 virNetDevVPortProfilePtr vport
= NULL
;
7396 virQEMUDriverConfigPtr cfg
= virQEMUDriverGetConfig(driver
);
7397 virConnectPtr conn
= NULL
;
7399 VIR_DEBUG("Shutting down vm=%p name=%s id=%d pid=%lld, "
7400 "reason=%s, asyncJob=%s, flags=0x%x",
7401 vm
, vm
->def
->name
, vm
->def
->id
,
7403 virDomainShutoffReasonTypeToString(reason
),
7404 qemuDomainAsyncJobTypeToString(asyncJob
),
7407 /* This method is routinely used in clean up paths. Disable error
7408 * reporting so we don't squash a legit error. */
7409 orig_err
= virSaveLastError();
7411 if (asyncJob
!= QEMU_ASYNC_JOB_NONE
) {
7412 if (qemuDomainObjBeginNestedJob(driver
, vm
, asyncJob
) < 0)
7414 } else if (priv
->job
.asyncJob
!= QEMU_ASYNC_JOB_NONE
&&
7415 priv
->job
.asyncOwner
== virThreadSelfID() &&
7416 priv
->job
.active
!= QEMU_JOB_ASYNC_NESTED
) {
7417 VIR_WARN("qemuProcessStop called without a nested job (async=%s)",
7418 qemuDomainAsyncJobTypeToString(asyncJob
));
7421 if (!virDomainObjIsActive(vm
)) {
7422 VIR_DEBUG("VM '%s' not active", vm
->def
->name
);
7426 qemuProcessBuildDestroyMemoryPaths(driver
, vm
, NULL
, false);
7430 if (virAtomicIntDecAndTest(&driver
->nactive
) && driver
->inhibitCallback
)
7431 driver
->inhibitCallback(false, driver
->inhibitOpaque
);
7433 /* Wake up anything waiting on domain condition */
7434 virDomainObjBroadcast(vm
);
7436 if ((timestamp
= virTimeStringNow()) != NULL
) {
7437 qemuDomainLogAppendMessage(driver
, vm
, "%s: shutting down, reason=%s\n",
7439 virDomainShutoffReasonTypeToString(reason
));
7440 VIR_FREE(timestamp
);
7443 /* Clear network bandwidth */
7444 virDomainClearNetBandwidth(vm
);
7446 virDomainConfVMNWFilterTeardown(vm
);
7448 if (cfg
->macFilter
) {
7450 for (i
= 0; i
< def
->nnets
; i
++) {
7451 virDomainNetDefPtr net
= def
->nets
[i
];
7452 if (net
->ifname
== NULL
)
7454 ignore_value(ebtablesRemoveForwardAllowIn(driver
->ebtables
,
7460 virPortAllocatorRelease(priv
->nbdPort
);
7464 qemuAgentClose(priv
->agent
);
7467 priv
->agentError
= false;
7470 qemuMonitorClose(priv
->mon
);
7474 if (priv
->monConfig
) {
7475 if (priv
->monConfig
->type
== VIR_DOMAIN_CHR_TYPE_UNIX
)
7476 unlink(priv
->monConfig
->data
.nix
.path
);
7477 virObjectUnref(priv
->monConfig
);
7478 priv
->monConfig
= NULL
;
7481 /* Remove the master key */
7482 qemuDomainMasterKeyRemove(priv
);
7484 /* Do this before we delete the tree and remove pidfile. */
7485 qemuProcessKillManagedPRDaemon(vm
);
7487 virFileDeleteTree(priv
->libDir
);
7488 virFileDeleteTree(priv
->channelTargetDir
);
7490 ignore_value(virDomainChrDefForeach(vm
->def
,
7492 qemuProcessCleanupChardevDevice
,
7496 /* shut it off for sure */
7497 ignore_value(qemuProcessKill(vm
,
7498 VIR_QEMU_PROCESS_KILL_FORCE
|
7499 VIR_QEMU_PROCESS_KILL_NOCHECK
));
7501 qemuDomainCleanupRun(driver
, vm
);
7503 qemuExtDevicesStop(driver
, vm
);
7505 /* Stop autodestroy in case guest is restarted */
7506 qemuProcessAutoDestroyRemove(driver
, vm
);
7508 /* now that we know it's stopped call the hook if present */
7509 if (virHookPresent(VIR_HOOK_DRIVER_QEMU
)) {
7510 char *xml
= qemuDomainDefFormatXML(driver
, NULL
, vm
->def
, 0);
7512 /* we can't stop the operation even if the script raised an error */
7513 ignore_value(virHookCall(VIR_HOOK_DRIVER_QEMU
, vm
->def
->name
,
7514 VIR_HOOK_QEMU_OP_STOPPED
, VIR_HOOK_SUBOP_END
,
7519 /* Reset Security Labels unless caller don't want us to */
7520 if (!(flags
& VIR_QEMU_PROCESS_STOP_NO_RELABEL
))
7521 qemuSecurityRestoreAllLabel(driver
, vm
,
7522 !!(flags
& VIR_QEMU_PROCESS_STOP_MIGRATED
));
7524 qemuSecurityReleaseLabel(driver
->securityManager
, vm
->def
);
7526 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
7527 virDomainDeviceDef dev
;
7528 virDomainDiskDefPtr disk
= vm
->def
->disks
[i
];
7530 dev
.type
= VIR_DOMAIN_DEVICE_DISK
;
7531 dev
.data
.disk
= disk
;
7532 ignore_value(qemuRemoveSharedDevice(driver
, &dev
, vm
->def
->name
));
7535 /* Clear out dynamically assigned labels */
7536 for (i
= 0; i
< vm
->def
->nseclabels
; i
++) {
7537 if (vm
->def
->seclabels
[i
]->type
== VIR_DOMAIN_SECLABEL_DYNAMIC
)
7538 VIR_FREE(vm
->def
->seclabels
[i
]->label
);
7539 VIR_FREE(vm
->def
->seclabels
[i
]->imagelabel
);
7542 qemuHostdevReAttachDomainDevices(driver
, vm
->def
);
7545 for (i
= 0; i
< def
->nnets
; i
++) {
7546 virDomainNetDefPtr net
= def
->nets
[i
];
7547 vport
= virDomainNetGetActualVirtPortProfile(net
);
7549 switch (virDomainNetGetActualType(net
)) {
7550 case VIR_DOMAIN_NET_TYPE_DIRECT
:
7551 ignore_value(virNetDevMacVLanDeleteWithVPortProfile(
7552 net
->ifname
, &net
->mac
,
7553 virDomainNetGetActualDirectDev(net
),
7554 virDomainNetGetActualDirectMode(net
),
7555 virDomainNetGetActualVirtPortProfile(net
),
7558 case VIR_DOMAIN_NET_TYPE_ETHERNET
:
7560 ignore_value(virNetDevTapDelete(net
->ifname
, net
->backend
.tap
));
7561 VIR_FREE(net
->ifname
);
7564 case VIR_DOMAIN_NET_TYPE_BRIDGE
:
7565 case VIR_DOMAIN_NET_TYPE_NETWORK
:
7566 #ifdef VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP
7567 if (!(vport
&& vport
->virtPortType
== VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH
))
7568 ignore_value(virNetDevTapDelete(net
->ifname
, net
->backend
.tap
));
7571 case VIR_DOMAIN_NET_TYPE_USER
:
7572 case VIR_DOMAIN_NET_TYPE_VHOSTUSER
:
7573 case VIR_DOMAIN_NET_TYPE_SERVER
:
7574 case VIR_DOMAIN_NET_TYPE_CLIENT
:
7575 case VIR_DOMAIN_NET_TYPE_MCAST
:
7576 case VIR_DOMAIN_NET_TYPE_INTERNAL
:
7577 case VIR_DOMAIN_NET_TYPE_HOSTDEV
:
7578 case VIR_DOMAIN_NET_TYPE_UDP
:
7579 case VIR_DOMAIN_NET_TYPE_LAST
:
7580 /* No special cleanup procedure for these types. */
7583 /* release the physical device (or any other resources used by
7584 * this interface in the network driver
7587 if (vport
->virtPortType
== VIR_NETDEV_VPORT_PROFILE_MIDONET
) {
7588 ignore_value(virNetDevMidonetUnbindPort(vport
));
7589 } else if (vport
->virtPortType
== VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH
) {
7590 ignore_value(virNetDevOpenvswitchRemovePort(
7591 virDomainNetGetActualBridgeName(net
),
7596 /* kick the device out of the hostdev list too */
7597 virDomainNetRemoveHostdev(def
, net
);
7598 if (net
->type
== VIR_DOMAIN_NET_TYPE_NETWORK
) {
7599 if (conn
|| (conn
= virGetConnectNetwork()))
7600 virDomainNetReleaseActualDevice(conn
, vm
->def
, net
);
7602 VIR_WARN("Unable to release network device '%s'", NULLSTR(net
->ifname
));
7607 if ((ret
= qemuRemoveCgroup(vm
)) < 0) {
7608 if (ret
== -EBUSY
&& (retries
++ < 5)) {
7612 VIR_WARN("Failed to remove cgroup for %s",
7616 /* Remove resctrl allocation after cgroups are cleaned up which makes it
7617 * kind of safer (although removing the allocation should work even with
7618 * pids in tasks file */
7619 for (i
= 0; i
< vm
->def
->nresctrls
; i
++) {
7622 for (j
= 0; j
< vm
->def
->resctrls
[i
]->nmonitors
; j
++) {
7623 virDomainResctrlMonDefPtr mon
= NULL
;
7625 mon
= vm
->def
->resctrls
[i
]->monitors
[j
];
7626 virResctrlMonitorRemove(mon
->instance
);
7629 virResctrlAllocRemove(vm
->def
->resctrls
[i
]->alloc
);
7632 qemuProcessRemoveDomainStatus(driver
, vm
);
7634 /* Remove VNC and Spice ports from port reservation bitmap, but only if
7635 they were reserved by the driver (autoport=yes)
7637 for (i
= 0; i
< vm
->def
->ngraphics
; ++i
) {
7638 virDomainGraphicsDefPtr graphics
= vm
->def
->graphics
[i
];
7639 if (graphics
->type
== VIR_DOMAIN_GRAPHICS_TYPE_VNC
) {
7640 if (graphics
->data
.vnc
.autoport
) {
7641 virPortAllocatorRelease(graphics
->data
.vnc
.port
);
7642 } else if (graphics
->data
.vnc
.portReserved
) {
7643 virPortAllocatorRelease(graphics
->data
.vnc
.port
);
7644 graphics
->data
.vnc
.portReserved
= false;
7646 if (graphics
->data
.vnc
.websocketGenerated
) {
7647 virPortAllocatorRelease(graphics
->data
.vnc
.websocket
);
7648 graphics
->data
.vnc
.websocketGenerated
= false;
7649 graphics
->data
.vnc
.websocket
= -1;
7650 } else if (graphics
->data
.vnc
.websocket
) {
7651 virPortAllocatorRelease(graphics
->data
.vnc
.websocket
);
7654 if (graphics
->type
== VIR_DOMAIN_GRAPHICS_TYPE_SPICE
) {
7655 if (graphics
->data
.spice
.autoport
) {
7656 virPortAllocatorRelease(graphics
->data
.spice
.port
);
7657 virPortAllocatorRelease(graphics
->data
.spice
.tlsPort
);
7659 if (graphics
->data
.spice
.portReserved
) {
7660 virPortAllocatorRelease(graphics
->data
.spice
.port
);
7661 graphics
->data
.spice
.portReserved
= false;
7664 if (graphics
->data
.spice
.tlsPortReserved
) {
7665 virPortAllocatorRelease(graphics
->data
.spice
.tlsPort
);
7666 graphics
->data
.spice
.tlsPortReserved
= false;
7674 virDomainObjSetState(vm
, VIR_DOMAIN_SHUTOFF
, reason
);
7675 for (i
= 0; i
< vm
->def
->niothreadids
; i
++)
7676 vm
->def
->iothreadids
[i
]->thread_id
= 0;
7678 /* clear all private data entries which are no longer needed */
7679 qemuDomainObjPrivateDataClear(priv
);
7681 /* reset node name allocator */
7682 qemuDomainStorageIdReset(priv
);
7684 /* The "release" hook cleans up additional resources */
7685 if (virHookPresent(VIR_HOOK_DRIVER_QEMU
)) {
7686 char *xml
= qemuDomainDefFormatXML(driver
, NULL
, vm
->def
, 0);
7688 /* we can't stop the operation even if the script raised an error */
7689 virHookCall(VIR_HOOK_DRIVER_QEMU
, vm
->def
->name
,
7690 VIR_HOOK_QEMU_OP_RELEASE
, VIR_HOOK_SUBOP_END
,
7695 virDomainObjRemoveTransientDef(vm
);
7698 if (asyncJob
!= QEMU_ASYNC_JOB_NONE
)
7699 qemuDomainObjEndJob(driver
, vm
);
7703 virSetError(orig_err
);
7704 virFreeError(orig_err
);
7706 virObjectUnref(conn
);
7707 virObjectUnref(cfg
);
7712 qemuProcessAutoDestroy(virDomainObjPtr dom
,
7716 virQEMUDriverPtr driver
= opaque
;
7717 qemuDomainObjPrivatePtr priv
= dom
->privateData
;
7718 virObjectEventPtr event
= NULL
;
7719 unsigned int stopFlags
= 0;
7721 VIR_DEBUG("vm=%s, conn=%p", dom
->def
->name
, conn
);
7723 if (priv
->job
.asyncJob
== QEMU_ASYNC_JOB_MIGRATION_IN
)
7724 stopFlags
|= VIR_QEMU_PROCESS_STOP_MIGRATED
;
7726 if (priv
->job
.asyncJob
) {
7727 VIR_DEBUG("vm=%s has long-term job active, cancelling",
7729 qemuDomainObjDiscardAsyncJob(driver
, dom
);
7732 VIR_DEBUG("Killing domain");
7734 if (qemuProcessBeginStopJob(driver
, dom
, QEMU_JOB_DESTROY
, true) < 0)
7737 qemuProcessStop(driver
, dom
, VIR_DOMAIN_SHUTOFF_DESTROYED
,
7738 QEMU_ASYNC_JOB_NONE
, stopFlags
);
7740 virDomainAuditStop(dom
, "destroyed");
7741 event
= virDomainEventLifecycleNewFromObj(dom
,
7742 VIR_DOMAIN_EVENT_STOPPED
,
7743 VIR_DOMAIN_EVENT_STOPPED_DESTROYED
);
7745 qemuDomainRemoveInactive(driver
, dom
);
7747 qemuDomainObjEndJob(driver
, dom
);
7749 virObjectEventStateQueue(driver
->domainEventState
, event
);
7752 int qemuProcessAutoDestroyAdd(virQEMUDriverPtr driver
,
7756 VIR_DEBUG("vm=%s, conn=%p", vm
->def
->name
, conn
);
7757 return virCloseCallbacksSet(driver
->closeCallbacks
, vm
, conn
,
7758 qemuProcessAutoDestroy
);
7761 int qemuProcessAutoDestroyRemove(virQEMUDriverPtr driver
,
7765 VIR_DEBUG("vm=%s", vm
->def
->name
);
7766 ret
= virCloseCallbacksUnset(driver
->closeCallbacks
, vm
,
7767 qemuProcessAutoDestroy
);
7771 bool qemuProcessAutoDestroyActive(virQEMUDriverPtr driver
,
7774 virCloseCallback cb
;
7775 VIR_DEBUG("vm=%s", vm
->def
->name
);
7776 cb
= virCloseCallbacksGet(driver
->closeCallbacks
, vm
, NULL
);
7777 return cb
== qemuProcessAutoDestroy
;
7782 qemuProcessRefreshDisks(virQEMUDriverPtr driver
,
7784 qemuDomainAsyncJob asyncJob
)
7786 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7787 bool blockdev
= virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
);
7788 virHashTablePtr table
= NULL
;
7792 if (qemuDomainObjEnterMonitorAsync(driver
, vm
, asyncJob
) == 0) {
7793 table
= qemuMonitorGetBlockInfo(priv
->mon
);
7794 if (qemuDomainObjExitMonitor(driver
, vm
) < 0)
7801 for (i
= 0; i
< vm
->def
->ndisks
; i
++) {
7802 virDomainDiskDefPtr disk
= vm
->def
->disks
[i
];
7803 qemuDomainDiskPrivatePtr diskpriv
= QEMU_DOMAIN_DISK_PRIVATE(disk
);
7804 struct qemuDomainDiskInfo
*info
;
7805 const char *entryname
= disk
->info
.alias
;
7808 entryname
= diskpriv
->qomName
;
7810 if (!(info
= virHashLookup(table
, entryname
)))
7813 if (info
->removable
) {
7815 virDomainDiskEmptySource(disk
);
7818 if (info
->tray_open
)
7819 disk
->tray_status
= VIR_DOMAIN_DISK_TRAY_OPEN
;
7821 disk
->tray_status
= VIR_DOMAIN_DISK_TRAY_CLOSED
;
7825 /* fill in additional data */
7826 diskpriv
->removable
= info
->removable
;
7827 diskpriv
->tray
= info
->tray
;
7839 qemuProcessRefreshCPU(virQEMUDriverPtr driver
,
7842 virCapsPtr caps
= virQEMUDriverGetCapabilities(driver
, false);
7843 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7844 virCPUDefPtr host
= NULL
;
7845 virCPUDefPtr cpu
= NULL
;
7851 if (!virQEMUCapsGuestIsNative(caps
->host
.arch
, vm
->def
->os
.arch
) ||
7858 /* If the domain with a host-model CPU was started by an old libvirt
7859 * (< 2.3) which didn't replace the CPU with a custom one, let's do it now
7860 * since the rest of our code does not really expect a host-model CPU in a
7863 if (vm
->def
->cpu
->mode
== VIR_CPU_MODE_HOST_MODEL
) {
7864 if (!(host
= virCPUCopyMigratable(caps
->host
.cpu
->arch
, caps
->host
.cpu
)))
7867 if (!(cpu
= virCPUDefCopyWithoutModel(host
)) ||
7868 virCPUDefCopyModelFilter(cpu
, host
, false,
7869 virQEMUCapsCPUFilterFeatures
,
7870 &caps
->host
.cpu
->arch
) < 0)
7873 if (virCPUUpdate(vm
->def
->os
.arch
, vm
->def
->cpu
, cpu
) < 0)
7876 if (qemuProcessUpdateCPU(driver
, vm
, QEMU_ASYNC_JOB_NONE
) < 0)
7878 } else if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_QUERY_CPU_MODEL_EXPANSION
)) {
7879 /* We only try to fix CPUs when the libvirt/QEMU combo used to start
7880 * the domain did not know about query-cpu-model-expansion in which
7881 * case the host-model is known to not contain features which QEMU
7882 * doesn't know about.
7884 if (qemuDomainFixupCPUs(vm
, &priv
->origCPU
) < 0)
7892 virCPUDefFree(host
);
7893 virObjectUnref(caps
);
7899 qemuProcessRefreshLegacyBlockjob(void *payload
,
7903 const char *jobname
= name
;
7904 virDomainObjPtr vm
= opaque
;
7905 qemuMonitorBlockJobInfoPtr info
= payload
;
7906 virDomainDiskDefPtr disk
;
7907 qemuBlockJobDataPtr job
;
7908 qemuBlockJobType jobtype
= info
->type
;
7909 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7911 if (!(disk
= qemuProcessFindDomainDiskByAliasOrQOM(vm
, jobname
, jobname
))) {
7912 VIR_DEBUG("could not find disk for block job '%s'", jobname
);
7916 if (jobtype
== QEMU_BLOCKJOB_TYPE_COMMIT
&&
7917 disk
->mirrorJob
== VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT
)
7918 jobtype
= disk
->mirrorJob
;
7920 if (!(job
= qemuBlockJobDiskNew(vm
, disk
, jobtype
, jobname
)))
7924 if (info
->ready
== 1 ||
7925 (info
->ready
== -1 && info
->end
== info
->cur
)) {
7926 disk
->mirrorState
= VIR_DOMAIN_DISK_MIRROR_STATE_READY
;
7927 job
->state
= VIR_DOMAIN_BLOCK_JOB_READY
;
7930 /* Pre-blockdev block copy labelled the chain of the mirrored device
7931 * just before pivoting. At that point it was no longer known whether
7932 * it's even necessary (e.g. disk is being reused). This code fixes
7933 * the labelling in case the job was started in a libvirt version
7934 * which did not label the chain when the block copy is being started.
7935 * Note that we can't do much on failure. */
7936 if (disk
->mirrorJob
== VIR_DOMAIN_BLOCK_JOB_TYPE_COPY
) {
7937 if (qemuDomainDetermineDiskChain(priv
->driver
, vm
, disk
,
7938 disk
->mirror
, true) < 0)
7941 if (disk
->mirror
->format
&&
7942 disk
->mirror
->format
!= VIR_STORAGE_FILE_RAW
&&
7943 (qemuDomainNamespaceSetupDisk(vm
, disk
->mirror
) < 0 ||
7944 qemuSetupImageChainCgroup(vm
, disk
->mirror
) < 0 ||
7945 qemuSecuritySetImageLabel(priv
->driver
, vm
, disk
->mirror
,
7951 qemuBlockJobStarted(job
, vm
);
7954 qemuBlockJobStartupFinalize(vm
, job
);
7961 qemuProcessRefreshLegacyBlockjobs(virQEMUDriverPtr driver
,
7964 virHashTablePtr blockJobs
= NULL
;
7967 qemuDomainObjEnterMonitor(driver
, vm
);
7968 blockJobs
= qemuMonitorGetAllBlockJobInfo(qemuDomainGetMonitor(vm
), true);
7969 if (qemuDomainObjExitMonitor(driver
, vm
) < 0 || !blockJobs
)
7972 if (virHashForEach(blockJobs
, qemuProcessRefreshLegacyBlockjob
, vm
) < 0)
7978 virHashFree(blockJobs
);
7984 qemuProcessRefreshBlockjobs(virQEMUDriverPtr driver
,
7987 qemuDomainObjPrivatePtr priv
= vm
->privateData
;
7989 if (virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
))
7990 return qemuBlockJobRefreshJobs(driver
, vm
);
7992 return qemuProcessRefreshLegacyBlockjobs(driver
, vm
);
7996 struct qemuProcessReconnectData
{
7997 virQEMUDriverPtr driver
;
7998 virDomainObjPtr obj
;
7999 virIdentityPtr identity
;
8002 * Open an existing VM's monitor, re-detect VCPU threads
8003 * and re-reserve the security labels in use
8005 * This function also inherits a locked and ref'd domain object.
8007 * This function needs to:
8009 * 1. just before monitor reconnect do lightweight MonitorEnter
8010 * (increase VM refcount and unlock VM)
8011 * 2. reconnect to monitor
8012 * 3. do lightweight MonitorExit (lock VM)
8013 * 4. continue reconnect process
8016 * We can't do normal MonitorEnter & MonitorExit because these two lock the
8017 * monitor lock, which does not exists in this early phase.
8020 qemuProcessReconnect(void *opaque
)
8022 struct qemuProcessReconnectData
*data
= opaque
;
8023 virQEMUDriverPtr driver
= data
->driver
;
8024 virDomainObjPtr obj
= data
->obj
;
8025 qemuDomainObjPrivatePtr priv
;
8026 qemuDomainJobObj oldjob
;
8029 virQEMUDriverConfigPtr cfg
;
8031 unsigned int stopFlags
= 0;
8032 bool jobStarted
= false;
8033 virCapsPtr caps
= NULL
;
8035 bool tryMonReconn
= false;
8037 virIdentitySetCurrent(data
->identity
);
8038 virObjectUnref(data
->identity
);
8041 qemuDomainObjRestoreJob(obj
, &oldjob
);
8042 if (oldjob
.asyncJob
== QEMU_ASYNC_JOB_MIGRATION_IN
)
8043 stopFlags
|= VIR_QEMU_PROCESS_STOP_MIGRATED
;
8045 cfg
= virQEMUDriverGetConfig(driver
);
8046 priv
= obj
->privateData
;
8048 if (!(caps
= virQEMUDriverGetCapabilities(driver
, false)))
8051 if (qemuDomainObjBeginJob(driver
, obj
, QEMU_JOB_MODIFY
) < 0)
8055 /* XXX If we ever gonna change pid file pattern, come up with
8056 * some intelligence here to deal with old paths. */
8057 if (!(priv
->pidfile
= virPidFileBuildPath(cfg
->stateDir
, obj
->def
->name
)))
8060 /* Restore the masterKey */
8061 if (qemuDomainMasterKeyReadFile(priv
) < 0)
8064 /* If we are connecting to a guest started by old libvirt there is no
8065 * allowReboot in status XML and we need to initialize it. */
8066 qemuProcessPrepareAllowReboot(obj
);
8068 if (qemuHostdevUpdateActiveDomainDevices(driver
, obj
->def
) < 0)
8071 if (priv
->qemuCaps
&&
8072 virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_CHARDEV_FD_PASS
))
8075 VIR_DEBUG("Reconnect monitor to def=%p name='%s' retry=%d",
8076 obj
, obj
->def
->name
, retry
);
8078 tryMonReconn
= true;
8080 /* XXX check PID liveliness & EXE path */
8081 if (qemuConnectMonitor(driver
, obj
, QEMU_ASYNC_JOB_NONE
, retry
, NULL
) < 0)
8084 priv
->machineName
= qemuDomainGetMachineName(obj
);
8085 if (!priv
->machineName
)
8088 if (qemuConnectCgroup(obj
) < 0)
8091 if (qemuDomainPerfRestart(obj
) < 0)
8094 /* XXX: Need to change as long as lock is introduced for
8095 * qemu_driver->sharedDevices.
8097 for (i
= 0; i
< obj
->def
->ndisks
; i
++) {
8098 virDomainDiskDefPtr disk
= obj
->def
->disks
[i
];
8099 virDomainDeviceDef dev
;
8101 if (virDomainDiskTranslateSourcePool(disk
) < 0)
8104 /* backing chains need to be refreshed only if they could change */
8105 if (priv
->reconnectBlockjobs
!= VIR_TRISTATE_BOOL_NO
&&
8106 !virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
)) {
8107 /* This should be the only place that calls
8108 * qemuDomainDetermineDiskChain with @report_broken == false
8109 * to guarantee best-effort domain reconnect */
8110 virStorageSourceBackingStoreClear(disk
->src
);
8111 if (qemuDomainDetermineDiskChain(driver
, obj
, disk
, NULL
, false) < 0)
8114 VIR_DEBUG("skipping backing chain detection for '%s'", disk
->dst
);
8117 dev
.type
= VIR_DOMAIN_DEVICE_DISK
;
8118 dev
.data
.disk
= disk
;
8119 if (qemuAddSharedDevice(driver
, &dev
, obj
->def
->name
) < 0)
8123 for (i
= 0; i
< obj
->def
->ngraphics
; i
++) {
8124 if (qemuProcessGraphicsReservePorts(obj
->def
->graphics
[i
], true) < 0)
8128 if (qemuProcessUpdateState(driver
, obj
) < 0)
8131 state
= virDomainObjGetState(obj
, &reason
);
8132 if (state
== VIR_DOMAIN_SHUTOFF
||
8133 (state
== VIR_DOMAIN_PAUSED
&&
8134 reason
== VIR_DOMAIN_PAUSED_STARTING_UP
)) {
8135 VIR_DEBUG("Domain '%s' wasn't fully started yet, killing it",
8140 /* If upgrading from old libvirtd we won't have found any
8141 * caps in the domain status, so re-query them
8143 if (!priv
->qemuCaps
&&
8144 (qemuDomainUpdateQEMUCaps(obj
, driver
->qemuCapsCache
) < 0))
8147 /* In case the domain shutdown while we were not running,
8148 * we need to finish the shutdown process. And we need to do it after
8149 * we have virQEMUCaps filled in.
8151 if (state
== VIR_DOMAIN_SHUTDOWN
||
8152 (state
== VIR_DOMAIN_PAUSED
&&
8153 reason
== VIR_DOMAIN_PAUSED_SHUTTING_DOWN
)) {
8154 VIR_DEBUG("Finishing shutdown sequence for domain %s",
8156 qemuProcessShutdownOrReboot(driver
, obj
);
8160 if (qemuProcessBuildDestroyMemoryPaths(driver
, obj
, NULL
, true) < 0)
8163 if ((qemuDomainAssignAddresses(obj
->def
, priv
->qemuCaps
,
8164 driver
, obj
, false)) < 0) {
8168 /* if domain requests security driver we haven't loaded, report error, but
8169 * do not kill the domain
8171 ignore_value(qemuSecurityCheckAllLabel(driver
->securityManager
,
8174 if (qemuProcessRefreshCPU(driver
, obj
) < 0)
8177 if (qemuDomainRefreshVcpuInfo(driver
, obj
, QEMU_ASYNC_JOB_NONE
, true) < 0)
8180 qemuDomainVcpuPersistOrder(obj
->def
);
8182 if (qemuProcessDetectIOThreadPIDs(driver
, obj
, QEMU_ASYNC_JOB_NONE
) < 0)
8185 if (qemuSecurityReserveLabel(driver
->securityManager
, obj
->def
, obj
->pid
) < 0)
8188 qemuProcessNotifyNets(obj
->def
);
8190 qemuProcessFiltersInstantiate(obj
->def
);
8192 if (qemuProcessRefreshDisks(driver
, obj
, QEMU_ASYNC_JOB_NONE
) < 0)
8195 if (!virQEMUCapsGet(priv
->qemuCaps
, QEMU_CAPS_BLOCKDEV
) &&
8196 qemuBlockNodeNamesDetect(driver
, obj
, QEMU_ASYNC_JOB_NONE
) < 0)
8199 if (qemuProcessRefreshBlockjobs(driver
, obj
) < 0)
8202 if (qemuRefreshVirtioChannelState(driver
, obj
, QEMU_ASYNC_JOB_NONE
) < 0)
8205 /* If querying of guest's RTC failed, report error, but do not kill the domain. */
8206 qemuRefreshRTC(driver
, obj
);
8208 if (qemuProcessRefreshBalloonState(driver
, obj
, QEMU_ASYNC_JOB_NONE
) < 0)
8211 if (qemuProcessRecoverJob(driver
, obj
, &oldjob
, &stopFlags
) < 0)
8214 if (qemuProcessUpdateDevices(driver
, obj
) < 0)
8217 if (qemuRefreshPRManagerState(driver
, obj
) < 0)
8220 qemuProcessReconnectCheckMemAliasOrderMismatch(obj
);
8222 if (qemuConnectAgent(driver
, obj
) < 0)
8225 for (i
= 0; i
< obj
->def
->nresctrls
; i
++) {
8228 if (virResctrlAllocDeterminePath(obj
->def
->resctrls
[i
]->alloc
,
8229 priv
->machineName
) < 0)
8232 for (j
= 0; j
< obj
->def
->resctrls
[i
]->nmonitors
; j
++) {
8233 virDomainResctrlMonDefPtr mon
= NULL
;
8235 mon
= obj
->def
->resctrls
[i
]->monitors
[j
];
8236 if (virResctrlMonitorDeterminePath(mon
->instance
,
8237 priv
->machineName
) < 0)
8242 /* update domain state XML with possibly updated state in virDomainObj */
8243 if (virDomainSaveStatus(driver
->xmlopt
, cfg
->stateDir
, obj
, driver
->caps
) < 0)
8246 /* Run an hook to allow admins to do some magic */
8247 if (virHookPresent(VIR_HOOK_DRIVER_QEMU
)) {
8248 char *xml
= qemuDomainDefFormatXML(driver
, priv
->qemuCaps
, obj
->def
, 0);
8251 hookret
= virHookCall(VIR_HOOK_DRIVER_QEMU
, obj
->def
->name
,
8252 VIR_HOOK_QEMU_OP_RECONNECT
, VIR_HOOK_SUBOP_BEGIN
,
8257 * If the script raised an error abort the launch
8263 if (virAtomicIntInc(&driver
->nactive
) == 1 && driver
->inhibitCallback
)
8264 driver
->inhibitCallback(true, driver
->inhibitOpaque
);
8268 if (!virDomainObjIsActive(obj
))
8269 qemuDomainRemoveInactive(driver
, obj
);
8270 qemuDomainObjEndJob(driver
, obj
);
8272 if (!virDomainObjIsActive(obj
))
8273 qemuDomainRemoveInactiveJob(driver
, obj
);
8275 virDomainObjEndAPI(&obj
);
8276 virObjectUnref(cfg
);
8277 virObjectUnref(caps
);
8278 virNWFilterUnlockFilterUpdates();
8279 virIdentitySetCurrent(NULL
);
8283 if (virDomainObjIsActive(obj
)) {
8284 /* We can't get the monitor back, so must kill the VM
8285 * to remove danger of it ending up running twice if
8286 * user tries to start it again later.
8288 * If we cannot get to the monitor when the QEMU command
8289 * line used -no-shutdown, then we can safely say that the
8290 * domain crashed; otherwise, if the monitor was started,
8291 * then we can blame ourselves, else we failed before the
8292 * monitor started so we don't really know. */
8293 if (!priv
->mon
&& tryMonReconn
&&
8294 qemuDomainIsUsingNoShutdown(priv
))
8295 state
= VIR_DOMAIN_SHUTOFF_CRASHED
;
8297 state
= VIR_DOMAIN_SHUTOFF_DAEMON
;
8299 state
= VIR_DOMAIN_SHUTOFF_UNKNOWN
;
8301 /* If BeginJob failed, we jumped here without a job, let's hope another
8302 * thread didn't have a chance to start playing with the domain yet
8303 * (it's all we can do anyway).
8305 qemuProcessStop(driver
, obj
, state
, QEMU_ASYNC_JOB_NONE
, stopFlags
);
8311 qemuProcessReconnectHelper(virDomainObjPtr obj
,
8315 struct qemuProcessReconnectData
*src
= opaque
;
8316 struct qemuProcessReconnectData
*data
;
8318 /* If the VM was inactive, we don't need to reconnect */
8322 if (VIR_ALLOC(data
) < 0)
8325 memcpy(data
, src
, sizeof(*data
));
8327 data
->identity
= virIdentityGetCurrent();
8329 virNWFilterReadLockFilterUpdates();
8331 /* this lock and reference will be eventually transferred to the thread
8332 * that handles the reconnect */
8336 if (virThreadCreate(&thread
, false, qemuProcessReconnect
, data
) < 0) {
8337 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
8338 _("Could not create thread. QEMU initialization "
8339 "might be incomplete"));
8340 /* We can't spawn a thread and thus connect to monitor. Kill qemu.
8341 * It's safe to call qemuProcessStop without a job here since there
8342 * is no thread that could be doing anything else with the same domain
8345 qemuProcessStop(src
->driver
, obj
, VIR_DOMAIN_SHUTOFF_FAILED
,
8346 QEMU_ASYNC_JOB_NONE
, 0);
8347 qemuDomainRemoveInactiveJobLocked(src
->driver
, obj
);
8349 virDomainObjEndAPI(&obj
);
8350 virNWFilterUnlockFilterUpdates();
8351 virObjectUnref(data
->identity
);
8360 * qemuProcessReconnectAll
8362 * Try to re-open the resources for live VMs that we care
8366 qemuProcessReconnectAll(virQEMUDriverPtr driver
)
8368 struct qemuProcessReconnectData data
= {.driver
= driver
};
8369 virDomainObjListForEach(driver
->domains
, qemuProcessReconnectHelper
, &data
);
8373 static void virQEMUCapsMonitorNotify(qemuMonitorPtr mon ATTRIBUTE_UNUSED
,
8374 virDomainObjPtr vm ATTRIBUTE_UNUSED
,
8375 void *opaque ATTRIBUTE_UNUSED
)
8379 static qemuMonitorCallbacks callbacks
= {
8380 .eofNotify
= virQEMUCapsMonitorNotify
,
8381 .errorNotify
= virQEMUCapsMonitorNotify
,
8386 qemuProcessQMPStop(qemuProcessQMPPtr proc
)
8389 virObjectUnlock(proc
->mon
);
8390 qemuMonitorClose(proc
->mon
);
8395 virCommandAbort(proc
->cmd
);
8396 virCommandFree(proc
->cmd
);
8401 unlink(proc
->monpath
);
8403 virDomainObjEndAPI(&proc
->vm
);
8405 if (proc
->pid
!= 0) {
8408 VIR_DEBUG("Killing QMP caps process %lld", (long long)proc
->pid
);
8409 if (virProcessKill(proc
->pid
, SIGKILL
) < 0 && errno
!= ESRCH
)
8410 VIR_ERROR(_("Failed to kill process %lld: %s"),
8411 (long long)proc
->pid
,
8412 virStrerror(errno
, ebuf
, sizeof(ebuf
)));
8418 unlink(proc
->pidfile
);
8421 rmdir(proc
->uniqDir
);
8426 * qemuProcessQMPFree:
8427 * @proc: Stores process and connection state
8429 * Kill QEMU process and free process data structure.
8432 qemuProcessQMPFree(qemuProcessQMPPtr proc
)
8437 qemuProcessQMPStop(proc
);
8438 VIR_FREE(proc
->binary
);
8439 VIR_FREE(proc
->libDir
);
8440 VIR_FREE(proc
->uniqDir
);
8441 VIR_FREE(proc
->monpath
);
8442 VIR_FREE(proc
->monarg
);
8443 VIR_FREE(proc
->pidfile
);
8444 VIR_FREE(proc
->stderr
);
8450 * qemuProcessQMPNew:
8451 * @binary: QEMU binary
8452 * @libDir: Directory for process and connection artifacts
8453 * @runUid: UserId for QEMU process
8454 * @runGid: GroupId for QEMU process
8455 * @forceTCG: Force TCG mode if true
8457 * Allocate and initialize domain structure encapsulating QEMU process state
8458 * and monitor connection for completing QMP queries.
8461 qemuProcessQMPNew(const char *binary
,
8467 qemuProcessQMPPtr ret
= NULL
;
8468 qemuProcessQMPPtr proc
= NULL
;
8470 VIR_DEBUG("exec=%s, libDir=%s, runUid=%u, runGid=%u, forceTCG=%d",
8471 binary
, libDir
, runUid
, runGid
, forceTCG
);
8473 if (VIR_ALLOC(proc
) < 0)
8476 if (VIR_STRDUP(proc
->binary
, binary
) < 0 ||
8477 VIR_STRDUP(proc
->libDir
, libDir
) < 0)
8480 proc
->runUid
= runUid
;
8481 proc
->runGid
= runGid
;
8482 proc
->forceTCG
= forceTCG
;
8484 VIR_STEAL_PTR(ret
, proc
);
8487 qemuProcessQMPFree(proc
);
8493 qemuProcessQEMULabelUniqPath(qemuProcessQMPPtr proc
)
8495 /* We cannot use the security driver here, but we should not need to. */
8496 if (chown(proc
->uniqDir
, proc
->runUid
, -1) < 0) {
8497 virReportSystemError(errno
,
8498 _("Cannot chown uniq path: %s"),
8508 qemuProcessQMPInit(qemuProcessQMPPtr proc
)
8510 char *template = NULL
;
8513 VIR_DEBUG("proc=%p, emulator=%s", proc
, proc
->binary
);
8515 if (virAsprintf(&template, "%s/qmp-XXXXXX", proc
->libDir
) < 0)
8518 if (!(proc
->uniqDir
= mkdtemp(template))) {
8519 virReportSystemError(errno
,
8520 _("Failed to create unique directory with "
8521 "template '%s' for probing QEMU"),
8526 if (qemuProcessQEMULabelUniqPath(proc
) < 0)
8529 if (virAsprintf(&proc
->monpath
, "%s/%s", proc
->uniqDir
,
8533 if (virAsprintf(&proc
->monarg
, "unix:%s,server,nowait", proc
->monpath
) < 0)
8537 * Normally we'd use runDir for pid files, but because we're using
8538 * -daemonize we need QEMU to be allowed to create them, rather
8539 * than libvirtd. So we're using libDir which QEMU can write to
8541 if (virAsprintf(&proc
->pidfile
, "%s/%s", proc
->uniqDir
, "qmp.pid") < 0)
8552 qemuProcessQMPLaunch(qemuProcessQMPPtr proc
)
8554 const char *machine
;
8560 machine
= "none,accel=tcg";
8562 machine
= "none,accel=kvm:tcg";
8564 VIR_DEBUG("Try to probe capabilities of '%s' via QMP, machine %s",
8565 proc
->binary
, machine
);
8568 * We explicitly need to use -daemonize here, rather than
8569 * virCommandDaemonize, because we need to synchronize
8570 * with QEMU creating its monitor socket API. Using
8571 * daemonize guarantees control won't return to libvirt
8572 * until the socket is present.
8574 proc
->cmd
= virCommandNewArgList(proc
->binary
,
8579 "-machine", machine
,
8580 "-qmp", proc
->monarg
,
8581 "-pidfile", proc
->pidfile
,
8584 virCommandAddEnvPassCommon(proc
->cmd
);
8585 virCommandClearCaps(proc
->cmd
);
8588 /* QEMU might run into permission issues, e.g. /dev/sev (0600), override
8589 * them just for the purpose of probing */
8591 virCommandAllowCap(proc
->cmd
, CAP_DAC_OVERRIDE
);
8594 virCommandSetGID(proc
->cmd
, proc
->runGid
);
8595 virCommandSetUID(proc
->cmd
, proc
->runUid
);
8597 virCommandSetErrorBuffer(proc
->cmd
, &(proc
->stderr
));
8599 if (virCommandRun(proc
->cmd
, &status
) < 0)
8603 VIR_DEBUG("QEMU %s exited with status %d", proc
->binary
, status
);
8604 virReportError(VIR_ERR_INTERNAL_ERROR
,
8605 _("Failed to start QEMU binary %s for probing: %s"),
8607 proc
->stderr
? proc
->stderr
: _("unknown error"));
8611 if ((rc
= virPidFileReadPath(proc
->pidfile
, &proc
->pid
)) < 0) {
8612 virReportSystemError(-rc
, _("Failed to read pidfile %s"), proc
->pidfile
);
8624 qemuProcessQMPInitMonitor(qemuMonitorPtr mon
)
8626 if (qemuMonitorSetCapabilities(mon
) < 0) {
8627 VIR_DEBUG("Failed to set monitor capabilities %s",
8628 virGetLastErrorMessage());
8637 qemuProcessQMPConnectMonitor(qemuProcessQMPPtr proc
)
8639 virDomainXMLOptionPtr xmlopt
= NULL
;
8640 virDomainChrSourceDef monConfig
;
8643 VIR_DEBUG("proc=%p, emulator=%s, proc->pid=%lld",
8644 proc
, proc
->binary
, (long long)proc
->pid
);
8646 monConfig
.type
= VIR_DOMAIN_CHR_TYPE_UNIX
;
8647 monConfig
.data
.nix
.path
= proc
->monpath
;
8648 monConfig
.data
.nix
.listen
= false;
8650 if (!(xmlopt
= virDomainXMLOptionNew(NULL
, NULL
, NULL
, NULL
, NULL
)) ||
8651 !(proc
->vm
= virDomainObjNew(xmlopt
)))
8654 proc
->vm
->pid
= proc
->pid
;
8656 if (!(proc
->mon
= qemuMonitorOpen(proc
->vm
, &monConfig
, true,
8657 0, &callbacks
, NULL
)))
8660 virObjectLock(proc
->mon
);
8662 if (qemuProcessQMPInitMonitor(proc
->mon
) < 0)
8668 virObjectUnref(xmlopt
);
8674 * qemuProcessQMPStart:
8675 * @proc: QEMU process and connection state created by qemuProcessQMPNew()
8677 * Start and connect to QEMU binary so QMP queries can be made.
8680 * proc = qemuProcessQMPNew(binary, libDir, runUid, runGid, forceTCG);
8681 * qemuProcessQMPStart(proc);
8682 * ** Send QMP Queries to QEMU using monitor (proc->mon) **
8683 * qemuProcessQMPFree(proc);
8685 * Process error output (proc->stderr) remains available in qemuProcessQMP
8686 * struct until qemuProcessQMPFree is called.
8689 qemuProcessQMPStart(qemuProcessQMPPtr proc
)
8693 VIR_DEBUG("proc=%p, emulator=%s", proc
, proc
->binary
);
8695 if (qemuProcessQMPInit(proc
) < 0)
8698 if (qemuProcessQMPLaunch(proc
) < 0)
8701 if (qemuProcessQMPConnectMonitor(proc
) < 0)