backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / src / qemu / qemu_process.c
blobd66212781fc6b6b487931c51135bb069a10ae750
1 /*
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/>.
22 #include <config.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <sys/stat.h>
28 #if defined(__linux__)
29 # include <linux/capability.h>
30 #elif defined(__FreeBSD__)
31 # include <sys/param.h>
32 # include <sys/cpuset.h>
33 #endif
35 #include <sys/utsname.h>
37 #if WITH_CAPNG
38 # include <cap-ng.h>
39 #endif
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"
61 #include "cpu/cpu.h"
62 #include "cpu/cpu_x86.h"
63 #include "datatypes.h"
64 #include "virlog.h"
65 #include "virerror.h"
66 #include "viralloc.h"
67 #include "virhook.h"
68 #include "virfile.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"
74 #include "viruuid.h"
75 #include "virprocess.h"
76 #include "virtime.h"
77 #include "virnetdevtap.h"
78 #include "virnetdevopenvswitch.h"
79 #include "virnetdevmidonet.h"
80 #include "virbitmap.h"
81 #include "viratomic.h"
82 #include "virnuma.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"
90 #include "virvsock.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");
98 /**
99 * qemuProcessRemoveDomainStatus
101 * remove all state files of a domain from statedir
103 * Returns 0 on success
105 static int
106 qemuProcessRemoveDomainStatus(virQEMUDriverPtr driver,
107 virDomainObjPtr vm)
109 char ebuf[1024];
110 char *file = NULL;
111 qemuDomainObjPrivatePtr priv = vm->privateData;
112 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
113 int ret = -1;
115 if (virAsprintf(&file, "%s/%s.xml", cfg->stateDir, vm->def->name) < 0)
116 goto cleanup;
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)));
121 VIR_FREE(file);
123 if (priv->pidfile &&
124 unlink(priv->pidfile) < 0 &&
125 errno != ENOENT)
126 VIR_WARN("Failed to remove PID file for %s: %s",
127 vm->def->name, virStrerror(errno, ebuf, sizeof(ebuf)));
129 ret = 0;
130 cleanup:
131 virObjectUnref(cfg);
132 return ret;
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
140 * performed
142 static void
143 qemuProcessHandleAgentEOF(qemuAgentPtr agent,
144 virDomainObjPtr vm)
146 qemuDomainObjPrivatePtr priv;
148 VIR_DEBUG("Received EOF from agent on %p '%s'", vm, vm->def->name);
150 virObjectLock(vm);
152 priv = vm->privateData;
154 if (!priv->agent) {
155 VIR_DEBUG("Agent freed already");
156 goto unlock;
159 if (priv->beingDestroyed) {
160 VIR_DEBUG("Domain is being destroyed, agent EOF is expected");
161 goto unlock;
164 qemuAgentClose(agent);
165 priv->agent = NULL;
166 priv->agentError = false;
168 virObjectUnlock(vm);
169 return;
171 unlock:
172 virObjectUnlock(vm);
173 return;
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
181 * allowed
183 static void
184 qemuProcessHandleAgentError(qemuAgentPtr agent ATTRIBUTE_UNUSED,
185 virDomainObjPtr vm)
187 qemuDomainObjPrivatePtr priv;
189 VIR_DEBUG("Received error from agent on %p '%s'", vm, vm->def->name);
191 virObjectLock(vm);
193 priv = vm->privateData;
195 priv->agentError = true;
197 virObjectUnlock(vm);
200 static void qemuProcessHandleAgentDestroy(qemuAgentPtr agent,
201 virDomainObjPtr vm)
203 VIR_DEBUG("Received destroy agent=%p vm=%p", agent, vm);
205 virObjectUnref(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);
223 if (!config)
224 return 0;
226 if (priv->agent)
227 return 0;
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");
232 return 0;
235 if (qemuSecuritySetDaemonSocketLabel(driver->securityManager, vm->def) < 0) {
236 VIR_ERROR(_("Failed to set security context for agent for %s"),
237 vm->def->name);
238 goto cleanup;
241 /* Hold an extra reference because we can't allow 'vm' to be
242 * deleted while the agent is active */
243 virObjectRef(vm);
245 virObjectUnlock(vm);
247 agent = qemuAgentOpen(vm,
248 config->source,
249 &agentCallbacks);
251 virObjectLock(vm);
253 if (agent == NULL)
254 virObjectUnref(vm);
256 if (!virDomainObjIsActive(vm)) {
257 qemuAgentClose(agent);
258 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
259 _("guest crashed while connecting to the guest agent"));
260 return -1;
263 if (qemuSecurityClearSocketLabel(driver->securityManager, vm->def) < 0) {
264 VIR_ERROR(_("Failed to clear security context for agent for %s"),
265 vm->def->name);
266 qemuAgentClose(agent);
267 goto cleanup;
270 priv->agent = agent;
271 if (!priv->agent)
272 VIR_INFO("Failed to connect agent for %s", vm->def->name);
274 cleanup:
275 if (!priv->agent) {
276 VIR_WARN("Cannot connect to QEMU guest agent for %s", vm->def->name);
277 priv->agentError = true;
278 virResetLastError();
281 return 0;
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
289 * performed
291 static void
292 qemuProcessHandleMonitorEOF(qemuMonitorPtr mon,
293 virDomainObjPtr vm,
294 void *opaque)
296 virQEMUDriverPtr driver = opaque;
297 qemuDomainObjPrivatePtr priv;
298 struct qemuProcessEvent *processEvent;
300 virObjectLock(vm);
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");
307 goto cleanup;
310 if (VIR_ALLOC(processEvent) < 0)
311 goto cleanup;
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);
319 goto cleanup;
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);
331 cleanup:
332 virObjectUnlock(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
340 * allowed
342 static void
343 qemuProcessHandleMonitorError(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
344 virDomainObjPtr vm,
345 void *opaque)
347 virQEMUDriverPtr driver = opaque;
348 virObjectEventPtr event = NULL;
350 VIR_DEBUG("Received error on %p '%s'", vm, vm->def->name);
352 virObjectLock(vm);
354 ((qemuDomainObjPrivatePtr) vm->privateData)->monError = true;
355 event = virDomainEventControlErrorNewFromObj(vm);
356 virObjectEventStateQueue(driver->domainEventState, event);
358 virObjectUnlock(vm);
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
371 * in some cases.
373 * Returns a disk from @vm or NULL if it could not be found.
375 virDomainDiskDefPtr
376 qemuProcessFindDomainDiskByAliasOrQOM(virDomainObjPtr vm,
377 const char *alias,
378 const char *qomid)
380 size_t i;
382 if (alias && *alias == '\0')
383 alias = NULL;
385 if (alias)
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)))
394 return disk;
397 virReportError(VIR_ERR_INTERNAL_ERROR,
398 _("no disk found with alias '%s' or id '%s'"),
399 NULLSTR(alias), NULLSTR(qomid));
400 return NULL;
404 static int
405 qemuProcessHandleReset(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
406 virDomainObjPtr vm,
407 void *opaque)
409 virQEMUDriverPtr driver = opaque;
410 virObjectEventPtr event;
411 qemuDomainObjPrivatePtr priv;
412 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
413 int ret = -1;
415 virObjectLock(vm);
417 event = virDomainEventRebootNewFromObj(vm);
418 priv = vm->privateData;
419 if (priv->agent)
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)
429 goto cleanup;
431 if (!virDomainObjIsActive(vm)) {
432 VIR_DEBUG("Ignoring RESET event from inactive domain %s",
433 vm->def->name);
434 goto endjob;
437 qemuProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_DESTROYED,
438 QEMU_ASYNC_JOB_NONE, 0);
439 virDomainAuditStop(vm, "destroyed");
440 qemuDomainRemoveInactive(driver, vm);
441 endjob:
442 qemuDomainObjEndJob(driver, vm);
445 ret = 0;
446 cleanup:
447 virObjectUnlock(vm);
448 virObjectEventStateQueue(driver->domainEventState, event);
449 virObjectUnref(cfg);
450 return ret;
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
462 static void
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;
470 int ret = -1, rc;
472 VIR_DEBUG("vm=%p", vm);
473 virObjectLock(vm);
474 if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
475 goto cleanup;
477 if (!virDomainObjIsActive(vm)) {
478 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
479 _("guest unexpectedly quit"));
480 goto endjob;
483 qemuDomainObjEnterMonitor(driver, vm);
484 rc = qemuMonitorSystemReset(priv->mon);
486 if (qemuDomainObjExitMonitor(driver, vm) < 0)
487 goto endjob;
489 if (rc < 0)
490 goto endjob;
492 if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_CRASHED)
493 reason = VIR_DOMAIN_RUNNING_CRASHED;
495 if (qemuProcessStartCPUs(driver, vm,
496 reason,
497 QEMU_ASYNC_JOB_NONE) < 0) {
498 if (virGetLastErrorCode() == VIR_ERR_OK)
499 virReportError(VIR_ERR_INTERNAL_ERROR,
500 "%s", _("resume operation failed"));
501 goto endjob;
504 if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0) {
505 VIR_WARN("Unable to save status on vm %s after state change",
506 vm->def->name);
509 ret = 0;
511 endjob:
512 qemuDomainObjEndJob(driver, vm);
514 cleanup:
515 if (ret == -1)
516 ignore_value(qemuProcessKill(vm, VIR_QEMU_PROCESS_KILL_FORCE));
517 virDomainObjEndAPI(&vm);
518 virObjectUnref(cfg);
522 void
523 qemuProcessShutdownOrReboot(virQEMUDriverPtr driver,
524 virDomainObjPtr vm)
526 qemuDomainObjPrivatePtr priv = vm->privateData;
528 if (priv->fakeReboot) {
529 qemuDomainSetFakeReboot(driver, vm, false);
530 virObjectRef(vm);
531 virThread th;
532 if (virThreadCreate(&th,
533 false,
534 qemuProcessFakeReboot,
535 vm) < 0) {
536 VIR_ERROR(_("Failed to create reboot thread, killing domain"));
537 ignore_value(qemuProcessKill(vm, VIR_QEMU_PROCESS_KILL_NOWAIT));
538 virObjectUnref(vm);
540 } else {
541 ignore_value(qemuProcessKill(vm, VIR_QEMU_PROCESS_KILL_NOWAIT));
546 static int
547 qemuProcessHandleEvent(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
548 virDomainObjPtr vm,
549 const char *eventName,
550 long long seconds,
551 unsigned int micros,
552 const char *details,
553 void *opaque)
555 virQEMUDriverPtr driver = opaque;
556 virObjectEventPtr event = NULL;
558 VIR_DEBUG("vm=%p", vm);
560 virObjectLock(vm);
561 event = virDomainQemuMonitorEventNew(vm->def->id, vm->def->name,
562 vm->def->uuid, eventName,
563 seconds, micros, details);
565 virObjectUnlock(vm);
566 virObjectEventStateQueue(driver->domainEventState, event);
568 return 0;
572 static int
573 qemuProcessHandleShutdown(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
574 virDomainObjPtr vm,
575 virTristateBool guest_initiated,
576 void *opaque)
578 virQEMUDriverPtr driver = opaque;
579 qemuDomainObjPrivatePtr priv;
580 virObjectEventPtr event = NULL;
581 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
582 int detail = 0;
584 VIR_DEBUG("vm=%p", vm);
586 virObjectLock(vm);
588 priv = vm->privateData;
589 if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_SHUTDOWN) {
590 VIR_DEBUG("Ignoring repeated SHUTDOWN event from domain %s",
591 vm->def->name);
592 goto unlock;
593 } else if (!virDomainObjIsActive(vm)) {
594 VIR_DEBUG("Ignoring SHUTDOWN event from inactive domain %s",
595 vm->def->name);
596 goto unlock;
599 VIR_DEBUG("Transitioned guest %s to shutdown state",
600 vm->def->name);
601 virDomainObjSetState(vm,
602 VIR_DOMAIN_SHUTDOWN,
603 VIR_DOMAIN_SHUTDOWN_UNKNOWN);
605 switch (guest_initiated) {
606 case VIR_TRISTATE_BOOL_YES:
607 detail = VIR_DOMAIN_EVENT_SHUTDOWN_GUEST;
608 break;
610 case VIR_TRISTATE_BOOL_NO:
611 detail = VIR_DOMAIN_EVENT_SHUTDOWN_HOST;
612 break;
614 case VIR_TRISTATE_BOOL_ABSENT:
615 case VIR_TRISTATE_BOOL_LAST:
616 default:
617 detail = VIR_DOMAIN_EVENT_SHUTDOWN_FINISHED;
618 break;
621 event = virDomainEventLifecycleNewFromObj(vm,
622 VIR_DOMAIN_EVENT_SHUTDOWN,
623 detail);
625 if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0) {
626 VIR_WARN("Unable to save status on vm %s after state change",
627 vm->def->name);
630 if (priv->agent)
631 qemuAgentNotifyEvent(priv->agent, QEMU_AGENT_EVENT_SHUTDOWN);
633 qemuProcessShutdownOrReboot(driver, vm);
635 unlock:
636 virObjectUnlock(vm);
637 virObjectEventStateQueue(driver->domainEventState, event);
638 virObjectUnref(cfg);
640 return 0;
644 static int
645 qemuProcessHandleStop(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
646 virDomainObjPtr vm,
647 void *opaque)
649 virQEMUDriverPtr driver = opaque;
650 virObjectEventPtr event = NULL;
651 virDomainPausedReason reason;
652 virDomainEventSuspendedDetailType detail;
653 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
654 qemuDomainObjPrivatePtr priv = vm->privateData;
656 virObjectLock(vm);
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;
665 else
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),
673 detail);
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,
684 detail);
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",
693 vm->def->name);
697 virObjectUnlock(vm);
698 virObjectEventStateQueue(driver->domainEventState, event);
699 virObjectUnref(cfg);
701 return 0;
705 static int
706 qemuProcessHandleResume(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
707 virDomainObjPtr vm,
708 void *opaque)
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;
717 virObjectLock(vm);
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', "
728 "event detail %d",
729 vm->def->name, virDomainRunningReasonTypeToString(reason),
730 eventDetail);
732 virDomainObjSetState(vm, VIR_DOMAIN_RUNNING, reason);
733 event = virDomainEventLifecycleNewFromObj(vm,
734 VIR_DOMAIN_EVENT_RESUMED,
735 eventDetail);
737 if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0) {
738 VIR_WARN("Unable to save status on vm %s after state change",
739 vm->def->name);
743 virObjectUnlock(vm);
744 virObjectEventStateQueue(driver->domainEventState, event);
745 virObjectUnref(cfg);
746 return 0;
749 static int
750 qemuProcessHandleRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
751 virDomainObjPtr vm,
752 long long offset,
753 void *opaque)
755 virQEMUDriverPtr driver = opaque;
756 virObjectEventPtr event = NULL;
757 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
759 virObjectLock(vm);
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);
786 virObjectUnlock(vm);
788 virObjectEventStateQueue(driver->domainEventState, event);
789 virObjectUnref(cfg);
790 return 0;
794 static int
795 qemuProcessHandleWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
796 virDomainObjPtr vm,
797 int action,
798 void *opaque)
800 virQEMUDriverPtr driver = opaque;
801 virObjectEventPtr watchdogEvent = NULL;
802 virObjectEventPtr lifecycleEvent = NULL;
803 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
805 virObjectLock(vm);
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",
825 vm->def->name);
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))
840 vm = NULL;
841 qemuProcessEventFree(processEvent);
846 if (vm)
847 virObjectUnlock(vm);
848 virObjectEventStateQueue(driver->domainEventState, watchdogEvent);
849 virObjectEventStateQueue(driver->domainEventState, lifecycleEvent);
851 virObjectUnref(cfg);
852 return 0;
856 static int
857 qemuProcessHandleIOError(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
858 virDomainObjPtr vm,
859 const char *diskAlias,
860 const char *nodename,
861 int action,
862 const char *reason,
863 void *opaque)
865 virQEMUDriverPtr driver = opaque;
866 virObjectEventPtr ioErrorEvent = NULL;
867 virObjectEventPtr ioErrorEvent2 = NULL;
868 virObjectEventPtr lifecycleEvent = NULL;
869 const char *srcPath;
870 const char *devAlias;
871 virDomainDiskDefPtr disk;
872 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
874 virObjectLock(vm);
876 if (*diskAlias == '\0')
877 diskAlias = NULL;
879 if (diskAlias)
880 disk = qemuProcessFindDomainDiskByAliasOrQOM(vm, diskAlias, NULL);
881 else if (nodename)
882 disk = qemuDomainDiskLookupByNodename(vm->def, nodename, NULL, NULL);
883 else
884 disk = NULL;
886 if (disk) {
887 srcPath = virDomainDiskGetSource(disk);
888 devAlias = disk->info.alias;
889 } else {
890 srcPath = "";
891 devAlias = "";
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);
918 virObjectUnlock(vm);
920 virObjectEventStateQueue(driver->domainEventState, ioErrorEvent);
921 virObjectEventStateQueue(driver->domainEventState, ioErrorEvent2);
922 virObjectEventStateQueue(driver->domainEventState, lifecycleEvent);
923 virObjectUnref(cfg);
924 return 0;
927 static int
928 qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
929 virDomainObjPtr vm,
930 const char *diskAlias,
931 int type,
932 int status,
933 const char *error,
934 void *opaque)
936 qemuDomainObjPrivatePtr priv;
937 virQEMUDriverPtr driver = opaque;
938 struct qemuProcessEvent *processEvent = NULL;
939 virDomainDiskDefPtr disk;
940 VIR_AUTOUNREF(qemuBlockJobDataPtr) job = NULL;
941 char *data = NULL;
943 virObjectLock(vm);
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))
949 goto cleanup;
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 */
959 goto cleanup;
961 if (!(disk = qemuProcessFindDomainDiskByAliasOrQOM(vm, diskAlias, NULL)))
962 goto cleanup;
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);
972 } else {
973 /* there is no waiting SYNC API, dispatch the update to a thread */
974 if (VIR_ALLOC(processEvent) < 0)
975 goto cleanup;
977 processEvent->eventType = QEMU_PROCESS_EVENT_BLOCK_JOB;
978 if (VIR_STRDUP(data, diskAlias) < 0)
979 goto cleanup;
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));
987 goto cleanup;
990 processEvent = NULL;
993 cleanup:
994 qemuProcessEventFree(processEvent);
995 virObjectUnlock(vm);
996 return 0;
1000 static int
1001 qemuProcessHandleJobStatusChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1002 virDomainObjPtr vm,
1003 const char *jobname,
1004 int status,
1005 void *opaque)
1007 virQEMUDriverPtr driver = opaque;
1008 qemuDomainObjPrivatePtr priv;
1009 struct qemuProcessEvent *processEvent = NULL;
1010 qemuBlockJobDataPtr job = NULL;
1011 int jobnewstate;
1013 virObjectLock(vm);
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);
1022 goto cleanup;
1025 if ((jobnewstate = qemuBlockjobConvertMonitorStatus(status)) == QEMU_BLOCKJOB_STATE_LAST)
1026 goto cleanup;
1028 if (!(job = virHashLookup(priv->blockjobs, jobname))) {
1029 VIR_DEBUG("job '%s' not registered", jobname);
1030 goto cleanup;
1033 job->newstate = jobnewstate;
1035 if (job->synchronous) {
1036 VIR_DEBUG("job '%s' handled synchronously", jobname);
1037 virDomainObjBroadcast(vm);
1038 } else {
1039 VIR_DEBUG("job '%s' handled by event thread", jobname);
1040 if (VIR_ALLOC(processEvent) < 0)
1041 goto cleanup;
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));
1049 goto cleanup;
1052 processEvent = NULL;
1055 cleanup:
1056 qemuProcessEventFree(processEvent);
1057 virObjectUnlock(vm);
1058 return 0;
1062 static int
1063 qemuProcessHandleGraphics(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1064 virDomainObjPtr vm,
1065 int phase,
1066 int localFamily,
1067 const char *localNode,
1068 const char *localService,
1069 int remoteFamily,
1070 const char *remoteNode,
1071 const char *remoteService,
1072 const char *authScheme,
1073 const char *x509dname,
1074 const char *saslUsername,
1075 void *opaque)
1077 virQEMUDriverPtr driver = opaque;
1078 virObjectEventPtr event;
1079 virDomainEventGraphicsAddressPtr localAddr = NULL;
1080 virDomainEventGraphicsAddressPtr remoteAddr = NULL;
1081 virDomainEventGraphicsSubjectPtr subject = NULL;
1082 size_t i;
1084 if (VIR_ALLOC(localAddr) < 0)
1085 goto error;
1086 localAddr->family = localFamily;
1087 if (VIR_STRDUP(localAddr->service, localService) < 0 ||
1088 VIR_STRDUP(localAddr->node, localNode) < 0)
1089 goto error;
1091 if (VIR_ALLOC(remoteAddr) < 0)
1092 goto error;
1093 remoteAddr->family = remoteFamily;
1094 if (VIR_STRDUP(remoteAddr->service, remoteService) < 0 ||
1095 VIR_STRDUP(remoteAddr->node, remoteNode) < 0)
1096 goto error;
1098 if (VIR_ALLOC(subject) < 0)
1099 goto error;
1100 if (x509dname) {
1101 if (VIR_REALLOC_N(subject->identities, subject->nidentity+1) < 0)
1102 goto error;
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)
1106 goto error;
1108 if (saslUsername) {
1109 if (VIR_REALLOC_N(subject->identities, subject->nidentity+1) < 0)
1110 goto error;
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)
1114 goto error;
1117 virObjectLock(vm);
1118 event = virDomainEventGraphicsNewFromObj(vm, phase, localAddr, remoteAddr, authScheme, subject);
1119 virObjectUnlock(vm);
1121 virObjectEventStateQueue(driver->domainEventState, event);
1123 return 0;
1125 error:
1126 if (localAddr) {
1127 VIR_FREE(localAddr->service);
1128 VIR_FREE(localAddr->node);
1129 VIR_FREE(localAddr);
1131 if (remoteAddr) {
1132 VIR_FREE(remoteAddr->service);
1133 VIR_FREE(remoteAddr->node);
1134 VIR_FREE(remoteAddr);
1136 if (subject) {
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);
1142 VIR_FREE(subject);
1145 return -1;
1148 static int
1149 qemuProcessHandleTrayChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1150 virDomainObjPtr vm,
1151 const char *devAlias,
1152 const char *devid,
1153 int reason,
1154 void *opaque)
1156 virQEMUDriverPtr driver = opaque;
1157 virObjectEventPtr event = NULL;
1158 virDomainDiskDefPtr disk;
1159 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
1161 virObjectLock(vm);
1162 disk = qemuProcessFindDomainDiskByAliasOrQOM(vm, devAlias, devid);
1164 if (disk) {
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",
1174 vm->def->name);
1177 virDomainObjBroadcast(vm);
1180 virObjectUnlock(vm);
1181 virObjectEventStateQueue(driver->domainEventState, event);
1182 virObjectUnref(cfg);
1183 return 0;
1186 static int
1187 qemuProcessHandlePMWakeup(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1188 virDomainObjPtr vm,
1189 void *opaque)
1191 virQEMUDriverPtr driver = opaque;
1192 virObjectEventPtr event = NULL;
1193 virObjectEventPtr lifecycleEvent = NULL;
1194 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
1196 virObjectLock(vm);
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",
1214 vm->def->name);
1218 virObjectUnlock(vm);
1219 virObjectEventStateQueue(driver->domainEventState, event);
1220 virObjectEventStateQueue(driver->domainEventState, lifecycleEvent);
1221 virObjectUnref(cfg);
1222 return 0;
1225 static int
1226 qemuProcessHandlePMSuspend(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1227 virDomainObjPtr vm,
1228 void *opaque)
1230 virQEMUDriverPtr driver = opaque;
1231 virObjectEventPtr event = NULL;
1232 virObjectEventPtr lifecycleEvent = NULL;
1233 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
1235 virObjectLock(vm);
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);
1245 lifecycleEvent =
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",
1252 vm->def->name);
1255 if (priv->agent)
1256 qemuAgentNotifyEvent(priv->agent, QEMU_AGENT_EVENT_SUSPEND);
1259 virObjectUnlock(vm);
1261 virObjectEventStateQueue(driver->domainEventState, event);
1262 virObjectEventStateQueue(driver->domainEventState, lifecycleEvent);
1263 virObjectUnref(cfg);
1264 return 0;
1267 static int
1268 qemuProcessHandleBalloonChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1269 virDomainObjPtr vm,
1270 unsigned long long actual,
1271 void *opaque)
1273 virQEMUDriverPtr driver = opaque;
1274 virObjectEventPtr event = NULL;
1275 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
1277 virObjectLock(vm);
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);
1291 return 0;
1294 static int
1295 qemuProcessHandlePMSuspendDisk(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1296 virDomainObjPtr vm,
1297 void *opaque)
1299 virQEMUDriverPtr driver = opaque;
1300 virObjectEventPtr event = NULL;
1301 virObjectEventPtr lifecycleEvent = NULL;
1302 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
1304 virObjectLock(vm);
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);
1314 lifecycleEvent =
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",
1321 vm->def->name);
1324 if (priv->agent)
1325 qemuAgentNotifyEvent(priv->agent, QEMU_AGENT_EVENT_SUSPEND);
1328 virObjectUnlock(vm);
1330 virObjectEventStateQueue(driver->domainEventState, event);
1331 virObjectEventStateQueue(driver->domainEventState, lifecycleEvent);
1332 virObjectUnref(cfg);
1334 return 0;
1338 static int
1339 qemuProcessHandleGuestPanic(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1340 virDomainObjPtr vm,
1341 qemuMonitorEventPanicInfoPtr info,
1342 void *opaque)
1344 virQEMUDriverPtr driver = opaque;
1345 struct qemuProcessEvent *processEvent;
1347 virObjectLock(vm);
1348 if (VIR_ALLOC(processEvent) < 0)
1349 goto cleanup;
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))
1361 vm = NULL;
1362 qemuProcessEventFree(processEvent);
1365 cleanup:
1366 if (vm)
1367 virObjectUnlock(vm);
1369 return 0;
1374 qemuProcessHandleDeviceDeleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1375 virDomainObjPtr vm,
1376 const char *devAlias,
1377 void *opaque)
1379 virQEMUDriverPtr driver = opaque;
1380 struct qemuProcessEvent *processEvent = NULL;
1381 char *data;
1383 virObjectLock(vm);
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))
1390 goto cleanup;
1392 if (VIR_ALLOC(processEvent) < 0)
1393 goto error;
1395 processEvent->eventType = QEMU_PROCESS_EVENT_DEVICE_DELETED;
1396 if (VIR_STRDUP(data, devAlias) < 0)
1397 goto error;
1398 processEvent->data = data;
1399 processEvent->vm = virObjectRef(vm);
1401 if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
1402 ignore_value(virObjectUnref(vm));
1403 goto error;
1406 cleanup:
1407 virObjectUnlock(vm);
1408 return 0;
1409 error:
1410 qemuProcessEventFree(processEvent);
1411 goto cleanup;
1417 * Meaning of fields reported by the event according to the ACPI standard:
1418 * @source:
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
1425 * @status:
1426 * general values
1427 * 0x00: success
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
1444 * 0x82: Device Busy
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
1458 * Bit [1]: I/O
1459 * Bit [0]: Memory
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
1469 * devices.
1471 static int
1472 qemuProcessHandleAcpiOstInfo(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1473 virDomainObjPtr vm,
1474 const char *alias,
1475 const char *slotType,
1476 const char *slot,
1477 unsigned int source,
1478 unsigned int status,
1479 void *opaque)
1481 virQEMUDriverPtr driver = opaque;
1482 virObjectEventPtr event = NULL;
1484 virObjectLock(vm);
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);
1490 if (!alias)
1491 goto cleanup;
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);
1503 cleanup:
1504 virObjectUnlock(vm);
1505 virObjectEventStateQueue(driver->domainEventState, event);
1507 return 0;
1511 static int
1512 qemuProcessHandleBlockThreshold(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1513 virDomainObjPtr vm,
1514 const char *nodename,
1515 unsigned long long threshold,
1516 unsigned long long excess,
1517 void *opaque)
1519 virQEMUDriverPtr driver = opaque;
1520 virObjectEventPtr event = NULL;
1521 virDomainDiskDefPtr disk;
1522 virStorageSourcePtr src;
1523 unsigned int idx;
1524 char *dev = NULL;
1525 const char *path = NULL;
1527 virObjectLock(vm);
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))
1535 path = src->path;
1537 if ((dev = qemuDomainDiskBackingStoreGetName(disk, src, idx))) {
1538 event = virDomainEventBlockThresholdNewFromObj(vm, dev, path,
1539 threshold, excess);
1540 VIR_FREE(dev);
1544 virObjectUnlock(vm);
1545 virObjectEventStateQueue(driver->domainEventState, event);
1547 return 0;
1551 static int
1552 qemuProcessHandleNicRxFilterChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1553 virDomainObjPtr vm,
1554 const char *devAlias,
1555 void *opaque)
1557 virQEMUDriverPtr driver = opaque;
1558 struct qemuProcessEvent *processEvent = NULL;
1559 char *data;
1561 virObjectLock(vm);
1563 VIR_DEBUG("Device %s RX Filter changed in domain %p %s",
1564 devAlias, vm, vm->def->name);
1566 if (VIR_ALLOC(processEvent) < 0)
1567 goto error;
1569 processEvent->eventType = QEMU_PROCESS_EVENT_NIC_RX_FILTER_CHANGED;
1570 if (VIR_STRDUP(data, devAlias) < 0)
1571 goto error;
1572 processEvent->data = data;
1573 processEvent->vm = virObjectRef(vm);
1575 if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
1576 ignore_value(virObjectUnref(vm));
1577 goto error;
1580 cleanup:
1581 virObjectUnlock(vm);
1582 return 0;
1583 error:
1584 qemuProcessEventFree(processEvent);
1585 goto cleanup;
1589 static int
1590 qemuProcessHandleSerialChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1591 virDomainObjPtr vm,
1592 const char *devAlias,
1593 bool connected,
1594 void *opaque)
1596 virQEMUDriverPtr driver = opaque;
1597 struct qemuProcessEvent *processEvent = NULL;
1598 char *data;
1600 virObjectLock(vm);
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)
1606 goto error;
1608 processEvent->eventType = QEMU_PROCESS_EVENT_SERIAL_CHANGED;
1609 if (VIR_STRDUP(data, devAlias) < 0)
1610 goto error;
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));
1617 goto error;
1620 cleanup:
1621 virObjectUnlock(vm);
1622 return 0;
1623 error:
1624 qemuProcessEventFree(processEvent);
1625 goto cleanup;
1629 static int
1630 qemuProcessHandleSpiceMigrated(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1631 virDomainObjPtr vm,
1632 void *opaque ATTRIBUTE_UNUSED)
1634 qemuDomainObjPrivatePtr priv;
1636 virObjectLock(vm);
1638 VIR_DEBUG("Spice migration completed for domain %p %s",
1639 vm, vm->def->name);
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");
1644 goto cleanup;
1647 priv->job.spiceMigrated = true;
1648 virDomainObjBroadcast(vm);
1650 cleanup:
1651 virObjectUnlock(vm);
1652 return 0;
1656 static int
1657 qemuProcessHandleMigrationStatus(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1658 virDomainObjPtr vm,
1659 int status,
1660 void *opaque)
1662 qemuDomainObjPrivatePtr priv;
1663 virQEMUDriverPtr driver = opaque;
1664 virObjectEventPtr event = NULL;
1665 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
1666 int reason;
1668 virObjectLock(vm);
1670 VIR_DEBUG("Migration of domain %p %s changed state to %s",
1671 vm, vm->def->name,
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");
1677 goto cleanup;
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",
1687 vm->def->name,
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",
1697 vm->def->name);
1701 cleanup:
1702 virObjectUnlock(vm);
1703 virObjectEventStateQueue(driver->domainEventState, event);
1704 virObjectUnref(cfg);
1705 return 0;
1709 static int
1710 qemuProcessHandleMigrationPass(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1711 virDomainObjPtr vm,
1712 int pass,
1713 void *opaque)
1715 virQEMUDriverPtr driver = opaque;
1716 qemuDomainObjPrivatePtr priv;
1718 virObjectLock(vm);
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");
1726 goto cleanup;
1729 virObjectEventStateQueue(driver->domainEventState,
1730 virDomainEventMigrationIterationNewFromObj(vm, pass));
1732 cleanup:
1733 virObjectUnlock(vm);
1734 return 0;
1738 static int
1739 qemuProcessHandleDumpCompleted(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1740 virDomainObjPtr vm,
1741 int status,
1742 qemuMonitorDumpStatsPtr stats,
1743 const char *error,
1744 void *opaque ATTRIBUTE_UNUSED)
1746 qemuDomainObjPrivatePtr priv;
1748 virObjectLock(vm);
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");
1756 goto cleanup;
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);
1770 cleanup:
1771 virResetLastError();
1772 virObjectUnlock(vm);
1773 return 0;
1777 static int
1778 qemuProcessHandlePRManagerStatusChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1779 virDomainObjPtr vm,
1780 const char *prManager,
1781 bool connected,
1782 void *opaque)
1784 virQEMUDriverPtr driver = opaque;
1785 qemuDomainObjPrivatePtr priv;
1786 struct qemuProcessEvent *processEvent = NULL;
1787 const char *managedAlias = qemuDomainGetManagedPRAlias();
1788 int ret = -1;
1790 virObjectLock(vm);
1792 VIR_DEBUG("pr-manager %s status changed for domain %p %s connected=%d",
1793 prManager, vm, vm->def->name, connected);
1795 if (connected) {
1796 /* Connect events are boring. */
1797 ret = 0;
1798 goto cleanup;
1800 /* Disconnect events are more interesting. */
1802 if (STRNEQ(prManager, managedAlias)) {
1803 VIR_DEBUG("pr-manager %s not managed, ignoring event",
1804 prManager);
1805 ret = 0;
1806 goto cleanup;
1809 priv = vm->privateData;
1810 priv->prDaemonRunning = false;
1812 if (VIR_ALLOC(processEvent) < 0)
1813 goto cleanup;
1815 processEvent->eventType = QEMU_PROCESS_EVENT_PR_DISCONNECT;
1816 processEvent->vm = virObjectRef(vm);
1818 if (virThreadPoolSendJob(driver->workerPool, 0, processEvent) < 0) {
1819 qemuProcessEventFree(processEvent);
1820 virObjectUnref(vm);
1821 goto cleanup;
1824 ret = 0;
1825 cleanup:
1826 virObjectUnlock(vm);
1827 return ret;
1831 static int
1832 qemuProcessHandleRdmaGidStatusChanged(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
1833 virDomainObjPtr vm,
1834 const char *netdev,
1835 bool gid_status,
1836 unsigned long long subnet_prefix,
1837 unsigned long long interface_id,
1838 void *opaque)
1840 virQEMUDriverPtr driver = opaque;
1841 struct qemuProcessEvent *processEvent = NULL;
1842 qemuMonitorRdmaGidStatusPtr info = NULL;
1843 int ret = -1;
1845 virObjectLock(vm);
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)
1852 goto cleanup;
1854 info->gid_status = gid_status;
1855 info->subnet_prefix = subnet_prefix;
1856 info->interface_id = interface_id;
1858 if (VIR_ALLOC(processEvent) < 0)
1859 goto cleanup;
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);
1867 virObjectUnref(vm);
1868 goto cleanup;
1871 ret = 0;
1872 cleanup:
1873 qemuMonitorEventRdmaGidStatusFree(info);
1874 virObjectUnlock(vm);
1875 return ret;
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,
1912 static void
1913 qemuProcessMonitorReportLogError(qemuMonitorPtr mon,
1914 const char *msg,
1915 void *opaque);
1918 static void
1919 qemuProcessMonitorLogFree(void *opaque)
1921 qemuDomainLogContextPtr logCtxt = opaque;
1922 virObjectUnref(logCtxt);
1926 static int
1927 qemuProcessInitMonitor(virQEMUDriverPtr driver,
1928 virDomainObjPtr vm,
1929 qemuDomainAsyncJob asyncJob)
1931 int ret;
1933 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
1934 return -1;
1936 ret = qemuMonitorSetCapabilities(QEMU_DOMAIN_PRIVATE(vm)->mon);
1938 if (qemuDomainObjExitMonitor(driver, vm) < 0)
1939 ret = -1;
1941 return ret;
1945 static int
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"),
1956 vm->def->name);
1957 return -1;
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. */
1968 virObjectRef(vm);
1970 ignore_value(virTimeMillisNow(&priv->monStart));
1971 monConfig = virObjectRef(priv->monConfig);
1972 virObjectUnlock(vm);
1974 mon = qemuMonitorOpen(vm,
1975 monConfig,
1976 retry,
1977 timeout,
1978 &monitorCallbacks,
1979 driver);
1981 if (mon && logCtxt) {
1982 virObjectRef(logCtxt);
1983 qemuMonitorSetDomainLog(mon,
1984 qemuProcessMonitorReportLogError,
1985 logCtxt,
1986 qemuProcessMonitorLogFree);
1989 virObjectLock(vm);
1990 virObjectUnref(monConfig);
1991 virObjectUnref(vm);
1992 priv->monStart = 0;
1994 if (!virDomainObjIsActive(vm)) {
1995 qemuMonitorClose(mon);
1996 mon = NULL;
1998 priv->mon = mon;
2000 if (qemuSecurityClearSocketLabel(driver->securityManager, vm->def) < 0) {
2001 VIR_ERROR(_("Failed to clear security context for monitor for %s"),
2002 vm->def->name);
2003 return -1;
2006 if (priv->mon == NULL) {
2007 VIR_INFO("Failed to connect monitor for %s", vm->def->name);
2008 return -1;
2011 if (qemuProcessInitMonitor(driver, vm, asyncJob) < 0)
2012 return -1;
2014 if (qemuMigrationCapsCheck(driver, vm, asyncJob) < 0)
2015 return -1;
2017 return 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
2033 static int
2034 qemuProcessReadLog(qemuDomainLogContextPtr logCtxt,
2035 char **msg,
2036 size_t max)
2038 char *buf;
2039 ssize_t got;
2040 char *eol;
2041 char *filter_next;
2042 size_t skip;
2044 if ((got = qemuDomainLogContextRead(logCtxt, &buf)) < 0)
2045 return -1;
2047 /* Filter out debug messages from intermediate libvirt process */
2048 filter_next = buf;
2049 while ((eol = strchr(filter_next, '\n'))) {
2050 *eol = '\0';
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);
2055 got -= skip;
2056 } else {
2057 filter_next = eol + 1;
2058 *eol = '\n';
2061 filter_next = NULL; /* silence false coverity warning */
2063 if (got > 0 &&
2064 buf[got - 1] == '\n') {
2065 buf[got - 1] = '\0';
2066 got--;
2069 if (max > 0 && got > max) {
2070 skip = 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);
2078 got -= skip;
2081 ignore_value(VIR_REALLOC_N_QUIET(buf, got + 1));
2082 *msg = buf;
2083 return 0;
2087 static int
2088 qemuProcessReportLogError(qemuDomainLogContextPtr logCtxt,
2089 const char *msgprefix)
2091 char *logmsg = NULL;
2092 size_t max;
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)
2100 return -1;
2102 virResetLastError();
2103 if (virStringIsEmpty(logmsg))
2104 virReportError(VIR_ERR_INTERNAL_ERROR, "%s", msgprefix);
2105 else
2106 virReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"), msgprefix, logmsg);
2108 VIR_FREE(logmsg);
2109 return 0;
2113 static void
2114 qemuProcessMonitorReportLogError(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
2115 const char *msg,
2116 void *opaque)
2118 qemuDomainLogContextPtr logCtxt = opaque;
2119 qemuProcessReportLogError(logCtxt, msg);
2123 static int
2124 qemuProcessLookupPTYs(virDomainChrDefPtr *devices,
2125 int count,
2126 virHashTablePtr info)
2128 char *id = NULL;
2129 size_t i;
2130 int ret = -1;
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;
2137 VIR_FREE(id);
2138 if (virAsprintf(&id, "char%s", chr->info.alias) < 0)
2139 return -1;
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);
2149 goto cleanup;
2150 } else {
2151 /* 'info chardev' had no pty path for this chardev,
2152 * but the log output had, so we're fine
2154 continue;
2158 VIR_FREE(chr->source->data.file.path);
2159 if (VIR_STRDUP(chr->source->data.file.path, entry->ptyPath) < 0)
2160 goto cleanup;
2164 ret = 0;
2165 cleanup:
2166 VIR_FREE(id);
2167 return ret;
2170 static int
2171 qemuProcessFindCharDevicePTYsMonitor(virDomainObjPtr vm,
2172 virHashTablePtr info)
2174 size_t i = 0;
2176 if (qemuProcessLookupPTYs(vm->def->serials, vm->def->nserials, info) < 0)
2177 return -1;
2179 if (qemuProcessLookupPTYs(vm->def->parallels, vm->def->nparallels,
2180 info) < 0)
2181 return -1;
2183 if (qemuProcessLookupPTYs(vm->def->channels, vm->def->nchannels, info) < 0)
2184 return -1;
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] */
2194 i = 1;
2195 if (virDomainChrSourceDefCopy(chr->source,
2196 ((vm->def->serials[0])->source)) < 0)
2197 return -1;
2201 if (qemuProcessLookupPTYs(vm->def->consoles + i, vm->def->nconsoles - i,
2202 info) < 0)
2203 return -1;
2205 return 0;
2209 static int
2210 qemuProcessRefreshChannelVirtioState(virQEMUDriverPtr driver,
2211 virDomainObjPtr vm,
2212 virHashTablePtr info,
2213 int booted)
2215 size_t i;
2216 int agentReason = VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_CHANNEL;
2217 qemuMonitorChardevInfoPtr entry;
2218 virObjectEventPtr event = NULL;
2219 char *id = NULL;
2220 int ret = -1;
2222 if (booted)
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) {
2229 VIR_FREE(id);
2230 if (virAsprintf(&id, "char%s", chr->info.alias) < 0)
2231 goto cleanup;
2233 /* port state not reported */
2234 if (!(entry = virHashLookup(info, id)) ||
2235 !entry->state)
2236 continue;
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,
2241 agentReason)))
2242 virObjectEventStateQueue(driver->domainEventState, event);
2244 chr->state = entry->state;
2248 ret = 0;
2249 cleanup:
2250 VIR_FREE(id);
2251 return ret;
2256 qemuRefreshVirtioChannelState(virQEMUDriverPtr driver,
2257 virDomainObjPtr vm,
2258 qemuDomainAsyncJob asyncJob)
2260 qemuDomainObjPrivatePtr priv = vm->privateData;
2261 virHashTablePtr info = NULL;
2262 int ret = -1;
2264 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
2265 goto cleanup;
2267 ret = qemuMonitorGetChardevInfo(priv->mon, &info);
2268 if (qemuDomainObjExitMonitor(driver, vm) < 0)
2269 ret = -1;
2271 if (ret < 0)
2272 goto cleanup;
2274 ret = qemuProcessRefreshChannelVirtioState(driver, vm, info, false);
2276 cleanup:
2277 virHashFree(info);
2278 return ret;
2282 static int
2283 qemuProcessRefreshPRManagerState(virDomainObjPtr vm,
2284 virHashTablePtr info)
2286 qemuDomainObjPrivatePtr priv = vm->privateData;
2287 qemuMonitorPRManagerInfoPtr prManagerInfo;
2288 const char *managedAlias = qemuDomainGetManagedPRAlias();
2289 int ret = -1;
2291 if (!(prManagerInfo = virHashLookup(info, managedAlias))) {
2292 virReportError(VIR_ERR_OPERATION_FAILED,
2293 _("missing info on pr-manager %s"),
2294 managedAlias);
2295 goto cleanup;
2298 priv->prDaemonRunning = prManagerInfo->connected;
2300 if (!priv->prDaemonRunning &&
2301 qemuProcessStartManagedPRDaemon(vm) < 0)
2302 goto cleanup;
2304 ret = 0;
2305 cleanup:
2306 return ret;
2310 static int
2311 qemuRefreshPRManagerState(virQEMUDriverPtr driver,
2312 virDomainObjPtr vm)
2314 qemuDomainObjPrivatePtr priv = vm->privateData;
2315 virHashTablePtr info = NULL;
2316 int ret = -1;
2318 if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_PR_MANAGER_HELPER) ||
2319 !qemuDomainDefHasManagedPR(vm))
2320 return 0;
2322 qemuDomainObjEnterMonitor(driver, vm);
2323 ret = qemuMonitorGetPRManagerInfo(priv->mon, &info);
2324 if (qemuDomainObjExitMonitor(driver, vm) < 0)
2325 ret = -1;
2327 if (ret < 0)
2328 goto cleanup;
2330 ret = qemuProcessRefreshPRManagerState(vm, info);
2332 cleanup:
2333 virHashFree(info);
2334 return ret;
2338 static void
2339 qemuRefreshRTC(virQEMUDriverPtr driver,
2340 virDomainObjPtr vm)
2342 qemuDomainObjPrivatePtr priv = vm->privateData;
2343 time_t now, then;
2344 struct tm thenbits;
2345 long localOffset;
2346 int rv;
2348 if (vm->def->clock.offset != VIR_DOMAIN_CLOCK_OFFSET_VARIABLE)
2349 return;
2351 memset(&thenbits, 0, sizeof(thenbits));
2352 qemuDomainObjEnterMonitor(driver, vm);
2353 now = time(NULL);
2354 rv = qemuMonitorGetRTCTime(priv->mon, &thenbits);
2355 if (qemuDomainObjExitMonitor(driver, vm) < 0)
2356 rv = -1;
2358 if (rv < 0)
2359 return;
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"));
2365 return;
2368 /* Thing is, @now is in local TZ but @then in UTC. */
2369 if (virTimeLocalOffsetFromUTC(&localOffset) < 0)
2370 return;
2372 vm->def->clock.data.variable.adjustment = then - now + localOffset;
2376 qemuProcessRefreshBalloonState(virQEMUDriverPtr driver,
2377 virDomainObjPtr vm,
2378 int asyncJob)
2380 unsigned long long balloon;
2381 int rc;
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);
2387 return 0;
2390 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
2391 return -1;
2393 rc = qemuMonitorGetBalloonInfo(qemuDomainGetMonitor(vm), &balloon);
2394 if (qemuDomainObjExitMonitor(driver, vm) < 0 || rc < 0)
2395 return -1;
2397 vm->def->mem.cur_balloon = balloon;
2399 return 0;
2403 static int
2404 qemuProcessWaitForMonitor(virQEMUDriverPtr driver,
2405 virDomainObjPtr vm,
2406 int asyncJob,
2407 qemuDomainLogContextPtr logCtxt)
2409 int ret = -1;
2410 virHashTablePtr info = NULL;
2411 qemuDomainObjPrivatePtr priv = vm->privateData;
2412 bool retry = true;
2414 if (priv->qemuCaps &&
2415 virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_CHARDEV_FD_PASS))
2416 retry = false;
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)
2422 goto cleanup;
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)
2429 goto cleanup;
2430 ret = qemuMonitorGetChardevInfo(priv->mon, &info);
2431 VIR_DEBUG("qemuMonitorGetChardevInfo returned %i", ret);
2432 if (qemuDomainObjExitMonitor(driver, vm) < 0)
2433 ret = -1;
2435 if (ret == 0) {
2436 if ((ret = qemuProcessFindCharDevicePTYsMonitor(vm, info)) < 0)
2437 goto cleanup;
2439 if ((ret = qemuProcessRefreshChannelVirtioState(driver, vm, info,
2440 true)) < 0)
2441 goto cleanup;
2444 cleanup:
2445 virHashFree(info);
2447 if (logCtxt && kill(vm->pid, 0) == -1 && errno == ESRCH) {
2448 qemuProcessReportLogError(logCtxt,
2449 _("process exited while connecting to monitor"));
2450 ret = -1;
2453 return ret;
2457 static int
2458 qemuProcessDetectIOThreadPIDs(virQEMUDriverPtr driver,
2459 virDomainObjPtr vm,
2460 int asyncJob)
2462 qemuDomainObjPrivatePtr priv = vm->privateData;
2463 qemuMonitorIOThreadInfoPtr *iothreads = NULL;
2464 int niothreads = 0;
2465 int ret = -1;
2466 size_t i;
2468 if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_OBJECT_IOTHREAD)) {
2469 ret = 0;
2470 goto cleanup;
2473 /* Get the list of IOThreads from qemu */
2474 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
2475 goto cleanup;
2476 niothreads = qemuMonitorGetIOThreads(priv->mon, &iothreads);
2477 if (qemuDomainObjExitMonitor(driver, vm) < 0)
2478 goto cleanup;
2479 if (niothreads < 0)
2480 goto cleanup;
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);
2487 goto cleanup;
2490 /* Nothing to do */
2491 if (niothreads == 0) {
2492 ret = 0;
2493 goto cleanup;
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);
2504 goto cleanup;
2506 iothrid->thread_id = iothreads[i]->thread_id;
2509 ret = 0;
2511 cleanup:
2512 if (iothreads) {
2513 for (i = 0; i < niothreads; i++)
2514 VIR_FREE(iothreads[i]);
2515 VIR_FREE(iothreads);
2517 return ret;
2521 static int
2522 qemuProcessGetAllCpuAffinity(virBitmapPtr *cpumapRet)
2524 *cpumapRet = NULL;
2526 if (!virHostCPUHasBitmap())
2527 return 0;
2529 if (!(*cpumapRet = virHostCPUGetOnlineBitmap()))
2530 return -1;
2532 return 0;
2537 * To be run between fork/exec of QEMU only
2539 #if defined(HAVE_SCHED_GETAFFINITY) || defined(HAVE_BSD_CPU_AFFINITY)
2540 static int
2541 qemuProcessInitCpuAffinity(virDomainObjPtr vm)
2543 VIR_AUTOPTR(virBitmap) cpumapToSet = NULL;
2544 virDomainNumatuneMemMode mem_mode;
2545 qemuDomainObjPrivatePtr priv = vm->privateData;
2547 if (!vm->pid) {
2548 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
2549 _("Cannot setup CPU affinity until process is started"));
2550 return -1;
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,
2570 priv->autoNodeset,
2571 &nodeset,
2572 -1) < 0)
2573 return -1;
2575 if (virNumaNodesetToCPUset(nodeset, &cpumapToSet) < 0)
2576 return -1;
2577 } else if (vm->def->cputune.emulatorpin) {
2578 if (!(cpumapToSet = virBitmapNewCopy(vm->def->cputune.emulatorpin)))
2579 return -1;
2580 } else {
2581 if (qemuProcessGetAllCpuAffinity(&cpumapToSet) < 0)
2582 return -1;
2585 if (cpumapToSet &&
2586 virProcessSetAffinity(vm->pid, cpumapToSet) < 0) {
2587 return -1;
2590 return 0;
2592 #else /* !defined(HAVE_SCHED_GETAFFINITY) && !defined(HAVE_BSD_CPU_AFFINITY) */
2593 static int
2594 qemuProcessInitCpuAffinity(virDomainObjPtr vm ATTRIBUTE_UNUSED)
2596 return 0;
2598 #endif /* !defined(HAVE_SCHED_GETAFFINITY) && !defined(HAVE_BSD_CPU_AFFINITY) */
2600 /* set link states to down on interfaces at qemu start */
2601 static int
2602 qemuProcessSetLinkStates(virQEMUDriverPtr driver,
2603 virDomainObjPtr vm,
2604 qemuDomainAsyncJob asyncJob)
2606 qemuDomainObjPrivatePtr priv = vm->privateData;
2607 virDomainDefPtr def = vm->def;
2608 size_t i;
2609 int ret = -1;
2610 int rv;
2612 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
2613 return -1;
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"));
2620 goto cleanup;
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);
2628 if (rv < 0) {
2629 virReportError(VIR_ERR_OPERATION_FAILED,
2630 _("Couldn't set link state on interface: %s"),
2631 def->nets[i]->info.alias);
2632 goto cleanup;
2637 ret = 0;
2639 cleanup:
2640 if (qemuDomainObjExitMonitor(driver, vm) < 0)
2641 ret = -1;
2642 return ret;
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.
2656 static int
2657 qemuProcessSetupPid(virDomainObjPtr vm,
2658 pid_t pid,
2659 virCgroupThreadName nameval,
2660 int id,
2661 virBitmapPtr cpumask,
2662 unsigned long long period,
2663 long long quota,
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;
2672 int ret = -1;
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"));
2678 goto cleanup;
2681 /* Infer which cpumask shall be used. */
2682 if (cpumask) {
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;
2688 } else {
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)
2694 goto cleanup;
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,
2709 priv->autoNodeset,
2710 &mem_mask, -1) < 0)
2711 goto cleanup;
2713 if (virCgroupNewThread(priv->cgroup, nameval, id, true, &cgroup) < 0)
2714 goto cleanup;
2716 if (virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET)) {
2717 if (use_cpumask &&
2718 qemuSetupCgroupCpusetCpus(cgroup, use_cpumask) < 0)
2719 goto cleanup;
2721 if (mem_mask && virCgroupSetCpusetMems(cgroup, mem_mask) < 0)
2722 goto cleanup;
2726 if ((period || quota) &&
2727 qemuSetupCgroupVcpuBW(cgroup, period, quota) < 0)
2728 goto cleanup;
2730 /* Move the thread to the sub dir */
2731 if (virCgroupAddThread(cgroup, pid) < 0)
2732 goto cleanup;
2736 /* Setup legacy affinity. */
2737 if (use_cpumask && virProcessSetAffinity(pid, use_cpumask) < 0)
2738 goto cleanup;
2740 /* Set scheduler type and priority, but not for the main thread. */
2741 if (sched &&
2742 nameval != VIR_CGROUP_THREAD_EMULATOR &&
2743 virProcessSetScheduler(pid, sched->policy, sched->priority) < 0)
2744 goto cleanup;
2746 ret = 0;
2747 cleanup:
2748 VIR_FREE(mem_mask);
2749 if (cgroup) {
2750 if (ret < 0)
2751 virCgroupRemove(cgroup);
2752 virCgroupFree(&cgroup);
2755 return ret;
2759 static int
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);
2770 static int
2771 qemuProcessResctrlCreate(virQEMUDriverPtr driver,
2772 virDomainObjPtr vm)
2774 int ret = -1;
2775 size_t i = 0;
2776 virCapsPtr caps = NULL;
2777 qemuDomainObjPrivatePtr priv = vm->privateData;
2779 if (!vm->def->nresctrls)
2780 return 0;
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);
2785 if (!caps)
2786 return -1;
2788 for (i = 0; i < vm->def->nresctrls; i++) {
2789 size_t j = 0;
2790 if (virResctrlAllocCreate(caps->host.resctrl,
2791 vm->def->resctrls[i]->alloc,
2792 priv->machineName) < 0)
2793 goto cleanup;
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)
2801 goto cleanup;
2805 ret = 0;
2806 cleanup:
2807 virObjectUnref(caps);
2808 return ret;
2812 static char *
2813 qemuProcessBuildPRHelperPidfilePath(virDomainObjPtr vm)
2815 qemuDomainObjPrivatePtr priv = vm->privateData;
2816 const char *prdAlias = qemuDomainGetManagedPRAlias();
2818 return virPidFileBuildPath(priv->libDir, prdAlias);
2822 void
2823 qemuProcessKillManagedPRDaemon(virDomainObjPtr vm)
2825 qemuDomainObjPrivatePtr priv = vm->privateData;
2826 virErrorPtr orig_err;
2827 char *pidfile;
2829 if (!(pidfile = qemuProcessBuildPRHelperPidfilePath(vm))) {
2830 VIR_WARN("Unable to construct pr-helper pidfile path");
2831 return;
2834 virErrorPreserveLast(&orig_err);
2835 if (virPidFileForceCleanupPath(pidfile) < 0) {
2836 VIR_WARN("Unable to kill pr-helper process");
2837 } else {
2838 if (unlink(pidfile) < 0 &&
2839 errno != ENOENT) {
2840 virReportSystemError(errno,
2841 _("Unable to remove stale pidfile %s"),
2842 pidfile);
2843 } else {
2844 priv->prDaemonRunning = false;
2847 virErrorRestore(&orig_err);
2849 VIR_FREE(pidfile);
2853 static int
2854 qemuProcessStartPRDaemonHook(void *opaque)
2856 virDomainObjPtr vm = opaque;
2857 size_t i, nfds = 0;
2858 int *fds = NULL;
2859 int ret = -1;
2861 if (qemuDomainNamespaceEnabled(vm, QEMU_DOMAIN_NS_MOUNT)) {
2862 if (virProcessGetNamespaces(vm->pid, &nfds, &fds) < 0)
2863 return ret;
2865 if (nfds > 0 &&
2866 virProcessSetNamespaces(nfds, fds) < 0)
2867 goto cleanup;
2870 ret = 0;
2871 cleanup:
2872 for (i = 0; i < nfds; i++)
2873 VIR_FORCE_CLOSE(fds[i]);
2874 VIR_FREE(fds);
2875 return ret;
2880 qemuProcessStartManagedPRDaemon(virDomainObjPtr vm)
2882 qemuDomainObjPrivatePtr priv = vm->privateData;
2883 virQEMUDriverPtr driver = priv->driver;
2884 virQEMUDriverConfigPtr cfg;
2885 int errfd = -1;
2886 char *pidfile = NULL;
2887 int pidfd = -1;
2888 char *socketPath = NULL;
2889 pid_t cpid = -1;
2890 virCommandPtr cmd = NULL;
2891 virTimeBackOffVar timebackoff;
2892 const unsigned long long timeout = 500000; /* ms */
2893 int ret = -1;
2895 cfg = virQEMUDriverGetConfig(driver);
2897 if (!virFileIsExecutable(cfg->prHelperName)) {
2898 virReportSystemError(errno, _("'%s' is not a suitable pr helper"),
2899 cfg->prHelperName);
2900 goto cleanup;
2903 if (!(pidfile = qemuProcessBuildPRHelperPidfilePath(vm)))
2904 goto cleanup;
2906 /* Just try to acquire. Dummy pid will be replaced later */
2907 if ((pidfd = virPidFileAcquirePath(pidfile, false, -1)) < 0)
2908 goto cleanup;
2910 if (!(socketPath = qemuDomainGetManagedPRSocketPath(priv)))
2911 goto cleanup;
2913 /* Remove stale socket */
2914 if (unlink(socketPath) < 0 &&
2915 errno != ENOENT) {
2916 virReportSystemError(errno,
2917 _("Unable to remove stale socket path: %s"),
2918 socketPath);
2919 goto cleanup;
2922 if (!(cmd = virCommandNewArgList(cfg->prHelperName,
2923 "-k", socketPath,
2924 "-f", pidfile,
2925 NULL)))
2926 goto cleanup;
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)
2939 goto cleanup;
2941 if (virPidFileReadPath(pidfile, &cpid) < 0) {
2942 virReportError(VIR_ERR_INTERNAL_ERROR,
2943 _("pr helper %s didn't show up"),
2944 cfg->prHelperName);
2945 goto cleanup;
2948 if (virTimeBackOffStart(&timebackoff, 1, timeout) < 0)
2949 goto cleanup;
2950 while (virTimeBackOffWait(&timebackoff)) {
2951 char errbuf[1024] = { 0 };
2953 if (virFileExists(socketPath))
2954 break;
2956 if (virProcessKill(cpid, 0) == 0)
2957 continue;
2959 if (saferead(errfd, errbuf, sizeof(errbuf) - 1) < 0) {
2960 virReportSystemError(errno,
2961 _("pr helper %s died unexpectedly"),
2962 cfg->prHelperName);
2963 } else {
2964 virReportError(VIR_ERR_OPERATION_FAILED,
2965 _("pr helper died and reported: %s"), errbuf);
2967 goto cleanup;
2970 if (!virFileExists(socketPath)) {
2971 virReportError(VIR_ERR_OPERATION_TIMEOUT, "%s",
2972 _("pr helper socked did not show up"));
2973 goto cleanup;
2976 if (priv->cgroup &&
2977 virCgroupAddMachineProcess(priv->cgroup, cpid) < 0)
2978 goto cleanup;
2980 if (qemuSecurityDomainSetPathLabel(driver, vm, socketPath, true) < 0)
2981 goto cleanup;
2983 priv->prDaemonRunning = true;
2984 ret = 0;
2985 cleanup:
2986 if (ret < 0) {
2987 virCommandAbort(cmd);
2988 if (cpid >= 0)
2989 virProcessKillPainfully(cpid, true);
2990 if (pidfile)
2991 unlink(pidfile);
2993 virCommandFree(cmd);
2994 VIR_FREE(socketPath);
2995 VIR_FORCE_CLOSE(pidfd);
2996 VIR_FREE(pidfile);
2997 VIR_FORCE_CLOSE(errfd);
2998 virObjectUnref(cfg);
2999 return ret;
3003 static int
3004 qemuProcessInitPasswords(virQEMUDriverPtr driver,
3005 virDomainObjPtr vm,
3006 int asyncJob)
3008 int ret = 0;
3009 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
3010 size_t i;
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,
3018 cfg->vncPassword,
3019 asyncJob);
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,
3024 cfg->spicePassword,
3025 asyncJob);
3028 if (ret < 0)
3029 goto cleanup;
3032 cleanup:
3033 virObjectUnref(cfg);
3034 return ret;
3038 static int
3039 qemuProcessPrepareChardevDevice(virDomainDefPtr def ATTRIBUTE_UNUSED,
3040 virDomainChrDefPtr dev,
3041 void *opaque ATTRIBUTE_UNUSED)
3043 int fd;
3044 if (dev->source->type != VIR_DOMAIN_CHR_TYPE_FILE)
3045 return 0;
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);
3052 return -1;
3055 VIR_FORCE_CLOSE(fd);
3057 return 0;
3061 static int
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);
3071 return 0;
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
3083 * migration.
3085 static int
3086 qemuProcessUpdateVideoRamSize(virQEMUDriverPtr driver,
3087 virDomainObjPtr vm,
3088 int asyncJob)
3090 int ret = -1;
3091 ssize_t i;
3092 qemuDomainObjPrivatePtr priv = vm->privateData;
3093 virDomainVideoDefPtr video = NULL;
3094 virQEMUDriverConfigPtr cfg = NULL;
3096 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
3097 return -1;
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)
3106 goto error;
3108 break;
3109 case VIR_DOMAIN_VIDEO_TYPE_QXL:
3110 if (i == 0) {
3111 if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_QXL_VGAMEM) &&
3112 qemuMonitorUpdateVideoMemorySize(priv->mon, video,
3113 "qxl-vga") < 0)
3114 goto error;
3116 if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_QXL_VRAM64) &&
3117 qemuMonitorUpdateVideoVram64Size(priv->mon, video,
3118 "qxl-vga") < 0)
3119 goto error;
3120 } else {
3121 if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_QXL_VGAMEM) &&
3122 qemuMonitorUpdateVideoMemorySize(priv->mon, video,
3123 "qxl") < 0)
3124 goto error;
3126 if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_QXL_VRAM64) &&
3127 qemuMonitorUpdateVideoVram64Size(priv->mon, video,
3128 "qxl") < 0)
3129 goto error;
3131 break;
3132 case VIR_DOMAIN_VIDEO_TYPE_VMVGA:
3133 if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_VMWARE_SVGA_VGAMEM)) {
3134 if (qemuMonitorUpdateVideoMemorySize(priv->mon, video,
3135 "vmware-svga") < 0)
3136 goto error;
3138 break;
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:
3143 break;
3148 if (qemuDomainObjExitMonitor(driver, vm) < 0)
3149 return -1;
3151 cfg = virQEMUDriverGetConfig(driver);
3152 ret = virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps);
3153 virObjectUnref(cfg);
3155 return ret;
3157 error:
3158 ignore_value(qemuDomainObjExitMonitor(driver, vm));
3159 return -1;
3163 struct qemuProcessHookData {
3164 virDomainObjPtr vm;
3165 virQEMUDriverPtr driver;
3166 virQEMUDriverConfigPtr cfg;
3169 static int qemuProcessHook(void *data)
3171 struct qemuProcessHookData *h = data;
3172 qemuDomainObjPrivatePtr priv = h->vm->privateData;
3173 int ret = -1;
3174 int fd;
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)
3196 goto cleanup;
3197 if (virDomainLockProcessStart(h->driver->lockManager,
3198 h->cfg->uri,
3199 h->vm,
3200 /* QEMU is always paused initially */
3201 true,
3202 &fd) < 0)
3203 goto cleanup;
3204 if (qemuSecurityClearSocketLabel(h->driver->securityManager, h->vm->def) < 0)
3205 goto cleanup;
3207 if (qemuDomainBuildNamespace(h->cfg, h->driver->securityManager, h->vm) < 0)
3208 goto cleanup;
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");
3218 } else {
3219 nodeset = virDomainNumatuneGetNodeset(h->vm->def->numa,
3220 priv->autoNodeset, -1);
3222 if (virNumaSetupMemoryPolicy(mode, nodeset) < 0)
3223 goto cleanup;
3227 ret = 0;
3229 cleanup:
3230 virObjectUnref(h->cfg);
3231 VIR_DEBUG("Hook complete ret=%d", ret);
3232 return 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",
3243 domainDir) < 0)
3244 return -1;
3245 return 0;
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)
3258 int ret = -1;
3259 qemuDomainObjPrivatePtr priv = vm->privateData;
3260 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
3262 /* Bring up netdevs before starting CPUs */
3263 if (qemuInterfaceStartDevices(vm->def) < 0)
3264 goto cleanup;
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
3273 goto cleanup;
3275 VIR_FREE(priv->lockState);
3277 priv->runningReason = reason;
3279 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
3280 goto release;
3282 ret = qemuMonitorStartCPUs(priv->mon);
3283 if (qemuDomainObjExitMonitor(driver, vm) < 0)
3284 ret = -1;
3286 if (ret < 0)
3287 goto release;
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
3291 * lifecycle event.
3294 cleanup:
3295 virObjectUnref(cfg);
3296 return ret;
3298 release:
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));
3303 goto cleanup;
3307 int qemuProcessStopCPUs(virQEMUDriverPtr driver,
3308 virDomainObjPtr vm,
3309 virDomainPausedReason reason,
3310 qemuDomainAsyncJob asyncJob)
3312 int ret = -1;
3313 qemuDomainObjPrivatePtr priv = vm->privateData;
3315 VIR_FREE(priv->lockState);
3317 priv->pausedReason = reason;
3319 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
3320 goto cleanup;
3322 ret = qemuMonitorStopCPUs(priv->mon);
3323 if (qemuDomainObjExitMonitor(driver, vm) < 0)
3324 ret = -1;
3326 if (ret < 0)
3327 goto cleanup;
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
3337 * lifecycle event.
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));
3344 cleanup:
3345 if (ret < 0)
3346 priv->pausedReason = VIR_DOMAIN_PAUSED_UNKNOWN;
3348 return ret;
3353 static void
3354 qemuProcessNotifyNets(virDomainDefPtr def)
3356 size_t i;
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*
3364 * impolite.
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()))
3371 continue;
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) */
3386 static void
3387 qemuProcessFiltersInstantiate(virDomainDefPtr def)
3389 size_t i;
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,
3395 true) < 0) {
3396 VIR_WARN("filter '%s' instantiation for '%s' failed '%s'",
3397 net->filter, net->ifname, virGetLastErrorMessage());
3398 virResetLastError();
3404 static int
3405 qemuProcessUpdateState(virQEMUDriverPtr driver, virDomainObjPtr vm)
3407 qemuDomainObjPrivatePtr priv = vm->privateData;
3408 virDomainState state;
3409 virDomainPausedReason reason;
3410 virDomainState newState = VIR_DOMAIN_NOSTATE;
3411 int oldReason;
3412 int newReason;
3413 bool running;
3414 char *msg = NULL;
3415 int ret;
3417 qemuDomainObjEnterMonitor(driver, vm);
3418 ret = qemuMonitorGetStatus(priv->mon, &running, &reason);
3419 if (qemuDomainObjExitMonitor(driver, vm) < 0)
3420 return -1;
3422 if (ret < 0)
3423 return -1;
3425 state = virDomainObjGetState(vm, &oldReason);
3427 if (running &&
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"));
3447 } else {
3448 newState = VIR_DOMAIN_PAUSED;
3449 newReason = reason;
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)",
3458 vm->def->name,
3459 NULLSTR(msg),
3460 virDomainStateTypeToString(newState),
3461 virDomainStateReasonToString(newState, newReason));
3462 VIR_FREE(msg);
3463 virDomainObjSetState(vm, newState, newReason);
3466 return 0;
3469 static int
3470 qemuProcessRecoverMigrationIn(virQEMUDriverPtr driver,
3471 virDomainObjPtr vm,
3472 const qemuDomainJobObj *job,
3473 virDomainState state,
3474 int reason)
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 */
3491 break;
3493 case QEMU_MIGRATION_PHASE_PREPARE:
3494 VIR_DEBUG("Killing unfinished incoming migration for domain %s",
3495 vm->def->name);
3496 return -1;
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",
3502 vm->def->name);
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);
3508 break;
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 */
3514 if (postcopy) {
3515 qemuMigrationAnyPostcopyFailed(driver, vm);
3516 } else if (state != VIR_DOMAIN_RUNNING) {
3517 VIR_DEBUG("Killing migrated domain %s", vm->def->name);
3518 return -1;
3520 break;
3523 qemuMigrationParamsReset(driver, vm, QEMU_ASYNC_JOB_NONE,
3524 job->migParams, job->apiFlags);
3525 return 0;
3528 static int
3529 qemuProcessRecoverMigrationOut(virQEMUDriverPtr driver,
3530 virDomainObjPtr vm,
3531 const qemuDomainJobObj *job,
3532 virDomainState state,
3533 int reason,
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 */
3548 break;
3550 case QEMU_MIGRATION_PHASE_BEGIN3:
3551 /* nothing happened so far, just forget we were about to migrate the
3552 * domain */
3553 break;
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
3559 * post-copy mode
3561 if (postcopy) {
3562 qemuMigrationAnyPostcopyFailed(driver, vm);
3563 } else {
3564 VIR_DEBUG("Cancelling unfinished migration of domain %s",
3565 vm->def->name);
3566 if (qemuMigrationSrcCancel(driver, vm) < 0) {
3567 VIR_WARN("Could not cancel ongoing migration of domain %s",
3568 vm->def->name);
3570 resume = true;
3572 break;
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
3579 if (postcopy)
3580 qemuMigrationAnyPostcopyFailed(driver, vm);
3581 break;
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
3588 if (postcopy) {
3589 qemuMigrationAnyPostcopyFailed(driver, vm);
3590 } else {
3591 VIR_DEBUG("Resuming domain %s after failed migration",
3592 vm->def->name);
3593 resume = true;
3595 break;
3597 case QEMU_MIGRATION_PHASE_CONFIRM3:
3598 /* migration completed, we need to kill the domain here */
3599 *stopFlags |= VIR_QEMU_PROCESS_STOP_MIGRATED;
3600 return -1;
3603 if (resume) {
3604 /* resume the domain but only if it was paused as a result of
3605 * migration
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);
3620 return 0;
3623 static int
3624 qemuProcessRecoverJob(virQEMUDriverPtr driver,
3625 virDomainObjPtr vm,
3626 const qemuDomainJobObj *job,
3627 unsigned int *stopFlags)
3629 qemuDomainObjPrivatePtr priv = vm->privateData;
3630 virDomainState state;
3631 int reason;
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)
3639 return -1;
3640 break;
3642 case QEMU_ASYNC_JOB_MIGRATION_IN:
3643 if (qemuProcessRecoverMigrationIn(driver, vm, job,
3644 state, reason) < 0)
3645 return -1;
3646 break;
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)
3654 return -1;
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",
3672 vm->def->name);
3675 break;
3677 case QEMU_ASYNC_JOB_START:
3678 /* Already handled in VIR_DOMAIN_PAUSED_STARTING_UP check. */
3679 break;
3681 case QEMU_ASYNC_JOB_NONE:
3682 case QEMU_ASYNC_JOB_LAST:
3683 break;
3686 if (!virDomainObjIsActive(vm))
3687 return -1;
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:
3695 /* harmless */
3696 break;
3698 case QEMU_JOB_DESTROY:
3699 VIR_DEBUG("Domain %s should have already been destroyed",
3700 vm->def->name);
3701 return -1;
3703 case QEMU_JOB_SUSPEND:
3704 /* mostly harmless */
3705 break;
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
3711 break;
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 */
3718 case QEMU_JOB_NONE:
3719 case QEMU_JOB_LAST:
3720 break;
3723 return 0;
3726 static int
3727 qemuProcessUpdateDevices(virQEMUDriverPtr driver,
3728 virDomainObjPtr vm)
3730 qemuDomainObjPrivatePtr priv = vm->privateData;
3731 virDomainDeviceDef dev;
3732 const char **qemuDevices;
3733 char **old;
3734 char **tmp;
3735 int ret = -1;
3737 old = priv->qemuDevices;
3738 priv->qemuDevices = NULL;
3739 if (qemuDomainUpdateDeviceList(driver, vm, QEMU_ASYNC_JOB_NONE) < 0)
3740 goto cleanup;
3742 qemuDevices = (const char **)priv->qemuDevices;
3743 if ((tmp = old)) {
3744 while (*tmp) {
3745 if (!virStringListHasString(qemuDevices, *tmp) &&
3746 virDomainDefFindDevice(vm->def, *tmp, &dev, false) == 0 &&
3747 qemuDomainRemoveDevice(driver, vm, &dev) < 0) {
3748 goto cleanup;
3750 tmp++;
3753 ret = 0;
3755 cleanup:
3756 virStringListFree(old);
3757 return ret;
3760 static int
3761 qemuDomainPerfRestart(virDomainObjPtr vm)
3763 size_t i;
3764 virDomainDefPtr def = vm->def;
3765 qemuDomainObjPrivatePtr priv = vm->privateData;
3767 if (!(priv->perf = virPerfNew()))
3768 return -1;
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;
3780 return 0;
3784 static void
3785 qemuProcessReconnectCheckMemAliasOrderMismatch(virDomainObjPtr vm)
3787 size_t i;
3788 int aliasidx;
3789 virDomainDefPtr def = vm->def;
3790 qemuDomainObjPrivatePtr priv = vm->privateData;
3792 if (!virDomainDefHasMemoryHotplug(def) || def->nmems == 0)
3793 return;
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;
3800 break;
3806 static bool
3807 qemuProcessNeedHugepagesPath(virDomainDefPtr def,
3808 virDomainMemoryDefPtr mem)
3810 const long system_pagesize = virGetSystemPageSizeKB();
3811 size_t i;
3813 if (def->mem.source == VIR_DOMAIN_MEMORY_SOURCE_FILE)
3814 return true;
3816 for (i = 0; i < def->mem.nhugepages; i++) {
3817 if (def->mem.hugepages[i].size != system_pagesize)
3818 return true;
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)
3825 return true;
3828 if (mem &&
3829 mem->model == VIR_DOMAIN_MEMORY_MODEL_DIMM &&
3830 mem->pagesize &&
3831 mem->pagesize != system_pagesize)
3832 return true;
3834 return false;
3838 static bool
3839 qemuProcessNeedMemoryBackingPath(virDomainDefPtr def,
3840 virDomainMemoryDefPtr mem)
3842 size_t i;
3843 size_t numaNodes;
3845 if (def->mem.source == VIR_DOMAIN_MEMORY_SOURCE_FILE ||
3846 def->mem.access != VIR_DOMAIN_MEMORY_ACCESS_DEFAULT)
3847 return true;
3849 numaNodes = virDomainNumaGetNodeCount(def->numa);
3850 for (i = 0; i < numaNodes; i++) {
3851 if (virDomainNumaGetNodeMemoryAccessMode(def->numa, i)
3852 != VIR_DOMAIN_MEMORY_ACCESS_DEFAULT)
3853 return true;
3856 if (mem &&
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)))
3862 return true;
3864 return false;
3868 static int
3869 qemuProcessBuildDestroyMemoryPathsImpl(virQEMUDriverPtr driver,
3870 virDomainObjPtr vm,
3871 const char *path,
3872 bool build)
3874 if (build) {
3875 if (virFileExists(path))
3876 return 0;
3878 if (virFileMakePathWithMode(path, 0700) < 0) {
3879 virReportSystemError(errno,
3880 _("Unable to create %s"),
3881 path);
3882 return -1;
3885 if (qemuSecurityDomainSetPathLabel(driver, vm, path, true) < 0)
3886 return -1;
3887 } else {
3888 if (virFileDeleteTree(path) < 0)
3889 return -1;
3892 return 0;
3897 qemuProcessBuildDestroyMemoryPaths(virQEMUDriverPtr driver,
3898 virDomainObjPtr vm,
3899 virDomainMemoryDefPtr mem,
3900 bool build)
3902 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
3903 char *path = NULL;
3904 size_t i;
3905 bool shouldBuildHP = false;
3906 bool shouldBuildMB = false;
3907 int ret = -1;
3909 if (build) {
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]);
3918 if (!path)
3919 goto cleanup;
3921 if (qemuProcessBuildDestroyMemoryPathsImpl(driver, vm,
3922 path, build) < 0)
3923 goto cleanup;
3925 VIR_FREE(path);
3929 if (!build || shouldBuildMB) {
3930 if (qemuGetMemoryBackingDomainPath(vm->def, cfg, &path) < 0)
3931 goto cleanup;
3933 if (qemuProcessBuildDestroyMemoryPathsImpl(driver, vm,
3934 path, build) < 0)
3935 goto cleanup;
3937 VIR_FREE(path);
3940 ret = 0;
3941 cleanup:
3942 VIR_FREE(path);
3943 virObjectUnref(cfg);
3944 return ret;
3949 qemuProcessDestroyMemoryBackingPath(virQEMUDriverPtr driver,
3950 virDomainObjPtr vm,
3951 virDomainMemoryDefPtr mem)
3953 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
3954 char *path = NULL;
3955 int ret = -1;
3957 if (qemuGetMemoryBackingPath(vm->def, cfg, mem->info.alias, &path) < 0)
3958 goto cleanup;
3960 if (unlink(path) < 0 &&
3961 errno != ENOENT) {
3962 virReportSystemError(errno, _("Unable to remove %s"), path);
3963 goto cleanup;
3966 ret = 0;
3967 cleanup:
3968 VIR_FREE(path);
3969 virObjectUnref(cfg);
3970 return ret;
3974 static int
3975 qemuProcessVNCAllocatePorts(virQEMUDriverPtr driver,
3976 virDomainGraphicsDefPtr graphics,
3977 bool allocate)
3979 unsigned short port;
3981 if (!allocate) {
3982 if (graphics->data.vnc.autoport)
3983 graphics->data.vnc.port = 5900;
3985 return 0;
3988 if (graphics->data.vnc.autoport) {
3989 if (virPortAllocatorAcquire(driver->remotePorts, &port) < 0)
3990 return -1;
3991 graphics->data.vnc.port = port;
3994 if (graphics->data.vnc.websocket == -1) {
3995 if (virPortAllocatorAcquire(driver->webSocketPorts, &port) < 0)
3996 return -1;
3997 graphics->data.vnc.websocket = port;
3998 graphics->data.vnc.websocketGenerated = true;
4001 return 0;
4004 static int
4005 qemuProcessSPICEAllocatePorts(virQEMUDriverPtr driver,
4006 virDomainGraphicsDefPtr graphics,
4007 bool allocate)
4009 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
4010 unsigned short port = 0;
4011 unsigned short tlsPort;
4012 size_t i;
4013 int defaultMode = graphics->data.spice.defaultMode;
4014 int ret = -1;
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:
4024 needTLSPort = true;
4025 break;
4027 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
4028 needPort = true;
4029 break;
4031 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
4032 /* default mode will be used */
4033 break;
4036 switch (defaultMode) {
4037 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_SECURE:
4038 needTLSPort = true;
4039 break;
4041 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_INSECURE:
4042 needPort = true;
4043 break;
4045 case VIR_DOMAIN_GRAPHICS_SPICE_CHANNEL_MODE_ANY:
4046 if (cfg->spiceTLS)
4047 needTLSPort = true;
4048 needPort = true;
4049 break;
4053 if (!allocate) {
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;
4060 ret = 0;
4061 goto cleanup;
4064 if (needPort || graphics->data.spice.port == -1) {
4065 if (virPortAllocatorAcquire(driver->remotePorts, &port) < 0)
4066 goto cleanup;
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"));
4079 goto cleanup;
4082 if (virPortAllocatorAcquire(driver->remotePorts, &tlsPort) < 0)
4083 goto cleanup;
4085 graphics->data.spice.tlsPort = tlsPort;
4087 if (!graphics->data.spice.autoport)
4088 graphics->data.spice.tlsPortReserved = true;
4091 ret = 0;
4093 cleanup:
4094 virObjectUnref(cfg);
4095 return ret;
4099 static int
4100 qemuProcessVerifyHypervFeatures(virDomainDefPtr def,
4101 virCPUDataPtr cpu)
4103 char *cpuFeature;
4104 size_t i;
4105 int rc;
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)
4111 continue;
4113 if (def->hyperv_features[i] != VIR_TRISTATE_SWITCH_ON)
4114 continue;
4116 if (virAsprintf(&cpuFeature, "hv-%s",
4117 virDomainHypervTypeToString(i)) < 0)
4118 return -1;
4120 rc = virCPUDataCheckFeature(cpu, cpuFeature);
4121 VIR_FREE(cpuFeature);
4123 if (rc < 0) {
4124 return -1;
4125 } else if (rc == 1) {
4126 if (i == VIR_DOMAIN_HYPERV_STIMER) {
4127 if (def->hyperv_stimer_direct != VIR_TRISTATE_SWITCH_ON)
4128 continue;
4130 rc = virCPUDataCheckFeature(cpu, VIR_CPU_x86_HV_STIMER_DIRECT);
4131 if (rc < 0)
4132 return -1;
4133 else if (rc == 1)
4134 continue;
4136 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
4137 _("host doesn't support hyperv stimer '%s' feature"),
4138 "direct");
4139 return -1;
4141 continue;
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));
4149 break;
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));
4164 return -1;
4166 /* coverity[dead_error_begin] */
4167 case VIR_DOMAIN_HYPERV_SPINLOCKS:
4168 case VIR_DOMAIN_HYPERV_VENDOR_ID:
4169 case VIR_DOMAIN_HYPERV_LAST:
4170 break;
4174 return 0;
4178 static int
4179 qemuProcessVerifyKVMFeatures(virDomainDefPtr def,
4180 virCPUDataPtr cpu)
4182 int rc = 0;
4184 if (def->features[VIR_DOMAIN_FEATURE_PVSPINLOCK] != VIR_TRISTATE_SWITCH_ON)
4185 return 0;
4187 rc = virCPUDataCheckFeature(cpu, VIR_CPU_x86_KVM_PV_UNHALT);
4189 if (rc <= 0) {
4190 if (rc == 0)
4191 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4192 _("host doesn't support paravirtual spinlocks"));
4193 return -1;
4196 return 0;
4200 static int
4201 qemuProcessVerifyCPUFeatures(virDomainDefPtr def,
4202 virCPUDataPtr cpu)
4204 int rc;
4206 rc = virCPUCheckFeature(def->os.arch, def->cpu, "invtsc");
4208 if (rc < 0) {
4209 return -1;
4210 } else if (rc == 1) {
4211 rc = virCPUDataCheckFeature(cpu, "invtsc");
4212 if (rc <= 0) {
4213 if (rc == 0) {
4214 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4215 _("host doesn't support invariant TSC"));
4217 return -1;
4221 return 0;
4225 static const char *
4226 qemuProcessTranslateCPUFeatures(const char *name,
4227 void *opaque)
4229 virQEMUCapsPtr qemuCaps = opaque;
4231 return virQEMUCapsCPUFeatureFromQEMU(qemuCaps, name);
4235 static int
4236 qemuProcessFetchGuestCPU(virQEMUDriverPtr driver,
4237 virDomainObjPtr vm,
4238 qemuDomainAsyncJob asyncJob,
4239 virCPUDataPtr *enabled,
4240 virCPUDataPtr *disabled)
4242 qemuDomainObjPrivatePtr priv = vm->privateData;
4243 virCPUDataPtr dataEnabled = NULL;
4244 virCPUDataPtr dataDisabled = NULL;
4245 bool generic;
4246 int rc;
4248 *enabled = NULL;
4249 *disabled = NULL;
4251 generic = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_CPU_UNAVAILABLE_FEATURES);
4253 if (!generic && !ARCH_IS_X86(vm->def->os.arch))
4254 return 0;
4256 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
4257 goto error;
4259 if (generic) {
4260 rc = qemuMonitorGetGuestCPU(priv->mon,
4261 vm->def->os.arch,
4262 qemuProcessTranslateCPUFeatures, priv->qemuCaps,
4263 &dataEnabled, &dataDisabled);
4264 } else {
4265 rc = qemuMonitorGetGuestCPUx86(priv->mon, &dataEnabled, &dataDisabled);
4268 if (qemuDomainObjExitMonitor(driver, vm) < 0)
4269 goto error;
4271 if (rc == -1)
4272 goto error;
4274 *enabled = dataEnabled;
4275 *disabled = dataDisabled;
4276 return 0;
4278 error:
4279 virCPUDataFree(dataEnabled);
4280 virCPUDataFree(dataDisabled);
4281 return -1;
4285 static int
4286 qemuProcessVerifyCPU(virDomainObjPtr vm,
4287 virCPUDataPtr cpu)
4289 virDomainDefPtr def = vm->def;
4291 if (!cpu)
4292 return 0;
4294 if (qemuProcessVerifyKVMFeatures(def, cpu) < 0 ||
4295 qemuProcessVerifyHypervFeatures(def, cpu) < 0)
4296 return -1;
4298 if (!def->cpu ||
4299 (def->cpu->mode == VIR_CPU_MODE_CUSTOM &&
4300 !def->cpu->model))
4301 return 0;
4303 if (qemuProcessVerifyCPUFeatures(def, cpu) < 0)
4304 return -1;
4306 return 0;
4310 static int
4311 qemuProcessUpdateLiveGuestCPU(virDomainObjPtr vm,
4312 virCPUDataPtr enabled,
4313 virCPUDataPtr disabled)
4315 virDomainDefPtr def = vm->def;
4316 qemuDomainObjPrivatePtr priv = vm->privateData;
4317 virCPUDefPtr orig = NULL;
4318 int rc;
4319 int ret = -1;
4321 if (!enabled)
4322 return 0;
4324 if (!def->cpu ||
4325 (def->cpu->mode == VIR_CPU_MODE_CUSTOM &&
4326 !def->cpu->model))
4327 return 0;
4329 if (!(orig = virCPUDefCopy(def->cpu)))
4330 goto cleanup;
4332 if ((rc = virCPUUpdateLive(def->os.arch, def->cpu, enabled, disabled)) < 0) {
4333 goto cleanup;
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;
4344 ret = 0;
4346 cleanup:
4347 virCPUDefFree(orig);
4348 return ret;
4352 static int
4353 qemuProcessUpdateAndVerifyCPU(virQEMUDriverPtr driver,
4354 virDomainObjPtr vm,
4355 qemuDomainAsyncJob asyncJob)
4357 virCPUDataPtr cpu = NULL;
4358 virCPUDataPtr disabled = NULL;
4359 int ret = -1;
4361 if (qemuProcessFetchGuestCPU(driver, vm, asyncJob, &cpu, &disabled) < 0)
4362 goto cleanup;
4364 if (qemuProcessVerifyCPU(vm, cpu) < 0)
4365 goto cleanup;
4367 if (qemuProcessUpdateLiveGuestCPU(vm, cpu, disabled) < 0)
4368 goto cleanup;
4370 ret = 0;
4372 cleanup:
4373 virCPUDataFree(cpu);
4374 virCPUDataFree(disabled);
4375 return ret;
4379 static virDomainCapsCPUModelsPtr
4380 qemuProcessFetchCPUDefinitions(virQEMUDriverPtr driver,
4381 virDomainObjPtr vm,
4382 qemuDomainAsyncJob asyncJob)
4384 qemuDomainObjPrivatePtr priv = vm->privateData;
4385 virDomainCapsCPUModelsPtr models = NULL;
4387 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
4388 goto error;
4390 models = virQEMUCapsFetchCPUDefinitions(priv->mon);
4392 if (qemuDomainObjExitMonitor(driver, vm) < 0)
4393 goto error;
4395 return models;
4397 error:
4398 virObjectUnref(models);
4399 return NULL;
4403 static int
4404 qemuProcessUpdateCPU(virQEMUDriverPtr driver,
4405 virDomainObjPtr vm,
4406 qemuDomainAsyncJob asyncJob)
4408 virCPUDataPtr cpu = NULL;
4409 virCPUDataPtr disabled = NULL;
4410 virDomainCapsCPUModelsPtr models = NULL;
4411 int ret = -1;
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)
4419 goto cleanup;
4421 if (qemuProcessUpdateLiveGuestCPU(vm, cpu, disabled) < 0)
4422 goto cleanup;
4424 if (!(models = qemuProcessFetchCPUDefinitions(driver, vm, asyncJob)) ||
4425 virCPUTranslate(vm->def->os.arch, vm->def->cpu, models) < 0)
4426 goto cleanup;
4428 ret = 0;
4430 cleanup:
4431 virCPUDataFree(cpu);
4432 virCPUDataFree(disabled);
4433 virObjectUnref(models);
4434 return ret;
4438 static int
4439 qemuPrepareNVRAM(virQEMUDriverConfigPtr cfg,
4440 virDomainObjPtr vm)
4442 int ret = -1;
4443 int srcFD = -1;
4444 int dstFD = -1;
4445 virDomainLoaderDefPtr loader = vm->def->os.loader;
4446 bool created = false;
4447 const char *master_nvram_path;
4448 ssize_t r;
4450 if (!loader || !loader->nvram || virFileExists(loader->nvram))
4451 return 0;
4453 master_nvram_path = loader->templt;
4454 if (!loader->templt) {
4455 size_t i;
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;
4459 break;
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);
4468 goto cleanup;
4471 if ((srcFD = virFileOpenAs(master_nvram_path, O_RDONLY,
4472 0, -1, -1, 0)) < 0) {
4473 virReportSystemError(-srcFD,
4474 _("Failed to open file '%s'"),
4475 master_nvram_path);
4476 goto cleanup;
4478 if ((dstFD = virFileOpenAs(loader->nvram,
4479 O_WRONLY | O_CREAT | O_EXCL,
4480 S_IRUSR | S_IWUSR,
4481 cfg->user, cfg->group, 0)) < 0) {
4482 virReportSystemError(-dstFD,
4483 _("Failed to create file '%s'"),
4484 loader->nvram);
4485 goto cleanup;
4487 created = true;
4489 do {
4490 char buf[1024];
4492 if ((r = saferead(srcFD, buf, sizeof(buf))) < 0) {
4493 virReportSystemError(errno,
4494 _("Unable to read from file '%s'"),
4495 master_nvram_path);
4496 goto cleanup;
4499 if (safewrite(dstFD, buf, r) < 0) {
4500 virReportSystemError(errno,
4501 _("Unable to write to file '%s'"),
4502 loader->nvram);
4503 goto cleanup;
4505 } while (r);
4507 if (VIR_CLOSE(srcFD) < 0) {
4508 virReportSystemError(errno,
4509 _("Unable to close file '%s'"),
4510 master_nvram_path);
4511 goto cleanup;
4513 if (VIR_CLOSE(dstFD) < 0) {
4514 virReportSystemError(errno,
4515 _("Unable to close file '%s'"),
4516 loader->nvram);
4517 goto cleanup;
4520 ret = 0;
4521 cleanup:
4522 /* We successfully generated the nvram path, but failed to
4523 * copy the file content. Roll back. */
4524 if (ret < 0) {
4525 if (created)
4526 unlink(loader->nvram);
4529 VIR_FORCE_CLOSE(srcFD);
4530 VIR_FORCE_CLOSE(dstFD);
4531 return ret;
4535 static void
4536 qemuLogOperation(virDomainObjPtr vm,
4537 const char *msg,
4538 virCommandPtr cmd,
4539 qemuDomainLogContextPtr logCtxt)
4541 char *timestamp;
4542 qemuDomainObjPrivatePtr priv = vm->privateData;
4543 int qemuVersion = virQEMUCapsGetVersion(priv->qemuCaps);
4544 const char *package = virQEMUCapsGetPackage(priv->qemuCaps);
4545 char *hostname = virGetHostname();
4546 struct utsname uts;
4548 uname(&uts);
4550 if ((timestamp = virTimeStringNow()) == NULL)
4551 goto cleanup;
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,
4558 qemuVersion % 1000,
4559 NULLSTR_EMPTY(package),
4560 uts.release,
4561 NULLSTR_EMPTY(hostname)) < 0)
4562 goto cleanup;
4564 if (cmd) {
4565 char *args = virCommandToString(cmd, true);
4566 qemuDomainLogContextWrite(logCtxt, "%s\n", args);
4567 VIR_FREE(args);
4570 cleanup:
4571 VIR_FREE(hostname);
4572 VIR_FREE(timestamp);
4576 void
4577 qemuProcessIncomingDefFree(qemuProcessIncomingDefPtr inc)
4579 if (!inc)
4580 return;
4582 VIR_FREE(inc->address);
4583 VIR_FREE(inc->launchURI);
4584 VIR_FREE(inc->deferredURI);
4585 VIR_FREE(inc);
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,
4601 int fd,
4602 const char *path)
4604 qemuProcessIncomingDefPtr inc = NULL;
4606 if (qemuMigrationDstCheckProtocol(qemuCaps, migrateFrom) < 0)
4607 return NULL;
4609 if (VIR_ALLOC(inc) < 0)
4610 return NULL;
4612 if (VIR_STRDUP(inc->address, listenAddress) < 0)
4613 goto error;
4615 inc->launchURI = qemuMigrationDstGetURI(migrateFrom, fd);
4616 if (!inc->launchURI)
4617 goto error;
4619 if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_INCOMING_DEFER)) {
4620 inc->deferredURI = inc->launchURI;
4621 if (VIR_STRDUP(inc->launchURI, "defer") < 0)
4622 goto error;
4625 inc->fd = fd;
4626 inc->path = path;
4628 return inc;
4630 error:
4631 qemuProcessIncomingDefFree(inc);
4632 return NULL;
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,
4644 virDomainObjPtr vm,
4645 virDomainJobOperation operation,
4646 unsigned long apiFlags)
4648 if (qemuDomainObjBeginAsyncJob(driver, vm, QEMU_ASYNC_JOB_START,
4649 operation, apiFlags) < 0)
4650 return -1;
4652 qemuDomainObjSetAsyncJobMask(vm, QEMU_JOB_NONE);
4653 return 0;
4657 void
4658 qemuProcessEndJob(virQEMUDriverPtr driver,
4659 virDomainObjPtr vm)
4661 qemuDomainObjEndAsyncJob(driver, vm);
4665 static int
4666 qemuProcessStartHook(virQEMUDriverPtr driver,
4667 virDomainObjPtr vm,
4668 virHookQemuOpType op,
4669 virHookSubopType subop)
4671 qemuDomainObjPrivatePtr priv = vm->privateData;
4672 char *xml;
4673 int ret;
4675 if (!virHookPresent(VIR_HOOK_DRIVER_QEMU))
4676 return 0;
4678 if (!(xml = qemuDomainDefFormatXML(driver, priv->qemuCaps, vm->def, 0)))
4679 return -1;
4681 ret = virHookCall(VIR_HOOK_DRIVER_QEMU, vm->def->name, op, subop,
4682 NULL, xml, NULL);
4683 VIR_FREE(xml);
4685 return ret;
4689 static int
4690 qemuProcessGraphicsReservePorts(virDomainGraphicsDefPtr graphics,
4691 bool reconnect)
4693 virDomainGraphicsListenDefPtr glisten;
4695 if (graphics->nListens <= 0)
4696 return 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)
4702 return 0;
4704 switch (graphics->type) {
4705 case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
4706 if (!graphics->data.vnc.autoport ||
4707 reconnect) {
4708 if (virPortAllocatorSetUsed(graphics->data.vnc.port) < 0)
4709 return -1;
4710 graphics->data.vnc.portReserved = true;
4712 if (graphics->data.vnc.websocket > 0 &&
4713 virPortAllocatorSetUsed(graphics->data.vnc.websocket) < 0)
4714 return -1;
4715 break;
4717 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
4718 if (graphics->data.spice.autoport && !reconnect)
4719 return 0;
4721 if (graphics->data.spice.port > 0) {
4722 if (virPortAllocatorSetUsed(graphics->data.spice.port) < 0)
4723 return -1;
4724 graphics->data.spice.portReserved = true;
4727 if (graphics->data.spice.tlsPort > 0) {
4728 if (virPortAllocatorSetUsed(graphics->data.spice.tlsPort) < 0)
4729 return -1;
4730 graphics->data.spice.tlsPortReserved = true;
4732 break;
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:
4739 break;
4742 return 0;
4746 static int
4747 qemuProcessGraphicsAllocatePorts(virQEMUDriverPtr driver,
4748 virDomainGraphicsDefPtr graphics,
4749 bool allocate)
4751 virDomainGraphicsListenDefPtr glisten;
4753 if (graphics->nListens <= 0)
4754 return 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)
4760 return 0;
4762 switch (graphics->type) {
4763 case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
4764 if (qemuProcessVNCAllocatePorts(driver, graphics, allocate) < 0)
4765 return -1;
4766 break;
4768 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
4769 if (qemuProcessSPICEAllocatePorts(driver, graphics, allocate) < 0)
4770 return -1;
4771 break;
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:
4778 break;
4781 return 0;
4784 static int
4785 qemuProcessGetNetworkAddress(const char *netname,
4786 char **netaddr)
4788 virConnectPtr conn = NULL;
4789 int ret = -1;
4790 virNetworkPtr net;
4791 virNetworkDefPtr netdef = NULL;
4792 virNetworkIPDefPtr ipdef;
4793 virSocketAddr addr;
4794 virSocketAddrPtr addrptr = NULL;
4795 char *dev_name = NULL;
4796 char *xml = NULL;
4798 *netaddr = NULL;
4800 if (!(conn = virGetConnectNetwork()))
4801 return -1;
4803 net = virNetworkLookupByName(conn, netname);
4804 if (!net)
4805 goto cleanup;
4807 xml = virNetworkGetXMLDesc(net, 0);
4808 if (!xml)
4809 goto cleanup;
4811 netdef = virNetworkDefParseString(xml, NULL);
4812 if (!netdef)
4813 goto cleanup;
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);
4821 if (!ipdef) {
4822 virReportError(VIR_ERR_INTERNAL_ERROR,
4823 _("network '%s' doesn't have an IP address"),
4824 netdef->name);
4825 goto cleanup;
4827 addrptr = &ipdef->address;
4828 break;
4830 case VIR_NETWORK_FORWARD_BRIDGE:
4831 if ((dev_name = netdef->bridge))
4832 break;
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;
4845 if (!dev_name) {
4846 virReportError(VIR_ERR_INTERNAL_ERROR,
4847 _("network '%s' has no associated interface or bridge"),
4848 netdef->name);
4849 goto cleanup;
4851 break;
4853 case VIR_NETWORK_FORWARD_HOSTDEV:
4854 break;
4856 case VIR_NETWORK_FORWARD_LAST:
4857 default:
4858 virReportEnumRangeError(virNetworkForwardType, netdef->forward.type);
4859 goto cleanup;
4862 if (dev_name) {
4863 if (virNetDevIPAddrGet(dev_name, &addr) < 0)
4864 goto cleanup;
4865 addrptr = &addr;
4868 if (!(addrptr &&
4869 (*netaddr = virSocketAddrFormat(addrptr)))) {
4870 goto cleanup;
4873 ret = 0;
4874 cleanup:
4875 virNetworkDefFree(netdef);
4876 virObjectUnref(net);
4877 virObjectUnref(conn);
4878 VIR_FREE(xml);
4879 return ret;
4883 static int
4884 qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr glisten,
4885 const char *listenAddr)
4887 int rc;
4889 /* TODO: reject configuration without network specified for network listen */
4890 if (!glisten->network) {
4891 if (VIR_STRDUP(glisten->address, listenAddr) < 0)
4892 return -1;
4893 return 0;
4896 rc = qemuProcessGetNetworkAddress(glisten->network, &glisten->address);
4897 if (rc <= -2) {
4898 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4899 _("network-based listen isn't possible, "
4900 "network driver isn't present"));
4901 return -1;
4903 if (rc < 0)
4904 return -1;
4906 return 0;
4910 static int
4911 qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
4912 virDomainGraphicsDefPtr graphics,
4913 virDomainObjPtr vm)
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;
4920 size_t i;
4921 int ret = -1;
4923 switch (graphics->type) {
4924 case VIR_DOMAIN_GRAPHICS_TYPE_VNC:
4925 useSocket = cfg->vncAutoUnixSocket;
4926 listenAddr = cfg->vncListen;
4927 break;
4929 case VIR_DOMAIN_GRAPHICS_TYPE_SPICE:
4930 useSocket = cfg->spiceAutoUnixSocket;
4931 listenAddr = cfg->spiceListen;
4932 break;
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:
4939 break;
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. */
4951 if (useSocket) {
4952 memset(glisten, 0, sizeof(virDomainGraphicsListenDef));
4953 if (virAsprintf(&glisten->socket, "%s/%s.sock",
4954 priv->libDir, type) < 0)
4955 goto cleanup;
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)
4960 goto cleanup;
4961 glisten->fromConfig = true;
4964 break;
4966 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK:
4967 if (glisten->address || !listenAddr)
4968 continue;
4970 if (qemuProcessGraphicsSetupNetworkAddress(glisten,
4971 listenAddr) < 0)
4972 goto cleanup;
4973 break;
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)
4979 goto cleanup;
4980 glisten->autoGenerated = true;
4982 break;
4984 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NONE:
4985 case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_LAST:
4986 break;
4990 ret = 0;
4992 cleanup:
4993 virObjectUnref(cfg);
4994 return ret;
4998 static int
4999 qemuProcessGraphicsSetupRenderNode(virDomainGraphicsDefPtr graphics,
5000 virQEMUCapsPtr qemuCaps)
5002 char **rendernode = NULL;
5004 if (!virDomainGraphicsNeedsAutoRenderNode(graphics))
5005 return 0;
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))
5010 return 0;
5012 rendernode = &graphics->data.spice.rendernode;
5013 } else {
5014 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_EGL_HEADLESS_RENDERNODE))
5015 return 0;
5017 rendernode = &graphics->data.egl_headless.rendernode;
5020 if (!(*rendernode = virHostGetDRMRenderNode()))
5021 return -1;
5023 return 0;
5027 static int
5028 qemuProcessSetupGraphics(virQEMUDriverPtr driver,
5029 virDomainObjPtr vm,
5030 virQEMUCapsPtr qemuCaps,
5031 unsigned int flags)
5033 virDomainGraphicsDefPtr graphics;
5034 bool allocate = !(flags & VIR_QEMU_PROCESS_START_PRETEND);
5035 size_t i;
5036 int ret = -1;
5038 for (i = 0; i < vm->def->ngraphics; i++) {
5039 graphics = vm->def->graphics[i];
5041 if (qemuProcessGraphicsSetupRenderNode(graphics, qemuCaps) < 0)
5042 goto cleanup;
5044 if (qemuProcessGraphicsSetupListen(driver, graphics, vm) < 0)
5045 goto cleanup;
5048 if (allocate) {
5049 for (i = 0; i < vm->def->ngraphics; i++) {
5050 graphics = vm->def->graphics[i];
5052 if (qemuProcessGraphicsReservePorts(graphics, false) < 0)
5053 goto cleanup;
5057 for (i = 0; i < vm->def->ngraphics; ++i) {
5058 graphics = vm->def->graphics[i];
5060 if (qemuProcessGraphicsAllocatePorts(driver, graphics, allocate) < 0)
5061 goto cleanup;
5064 ret = 0;
5066 cleanup:
5067 return ret;
5071 static int
5072 qemuProcessSetupRawIO(virQEMUDriverPtr driver,
5073 virDomainObjPtr vm,
5074 virCommandPtr cmd ATTRIBUTE_UNUSED)
5076 bool rawio = false;
5077 size_t i;
5078 int ret = -1;
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) {
5086 rawio = true;
5087 #ifndef CAP_SYS_RAWIO
5088 break;
5089 #endif
5092 dev.type = VIR_DOMAIN_DEVICE_DISK;
5093 dev.data.disk = disk;
5094 if (qemuAddSharedDevice(driver, &dev, vm->def->name) < 0)
5095 goto cleanup;
5097 if (qemuSetUnprivSGIO(&dev) < 0)
5098 goto cleanup;
5101 /* If rawio not already set, check hostdevs as well */
5102 if (!rawio) {
5103 for (i = 0; i < vm->def->nhostdevs; i++) {
5104 if (!virHostdevIsSCSIDevice(vm->def->hostdevs[i]))
5105 continue;
5107 virDomainHostdevSubsysSCSIPtr scsisrc =
5108 &vm->def->hostdevs[i]->source.subsys.u.scsi;
5109 if (scsisrc->rawio == VIR_TRISTATE_BOOL_YES) {
5110 rawio = true;
5111 break;
5116 ret = 0;
5118 cleanup:
5119 if (rawio) {
5120 #ifdef CAP_SYS_RAWIO
5121 if (ret == 0)
5122 virCommandAllowCap(cmd, CAP_SYS_RAWIO);
5123 #else
5124 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
5125 _("Raw I/O is not supported on this platform"));
5126 ret = -1;
5127 #endif
5129 return ret;
5133 static int
5134 qemuProcessSetupBalloon(virQEMUDriverPtr driver,
5135 virDomainObjPtr vm,
5136 qemuDomainAsyncJob asyncJob)
5138 unsigned long long balloon = vm->def->mem.cur_balloon;
5139 qemuDomainObjPrivatePtr priv = vm->privateData;
5140 int ret = -1;
5142 if (!virDomainDefHasMemballoon(vm->def))
5143 return 0;
5145 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
5146 return -1;
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)
5152 goto cleanup;
5154 ret = 0;
5156 cleanup:
5157 if (qemuDomainObjExitMonitor(driver, vm) < 0)
5158 ret = -1;
5159 return ret;
5163 static int
5164 qemuProcessMakeDir(virQEMUDriverPtr driver,
5165 virDomainObjPtr vm,
5166 const char *path)
5168 int ret = -1;
5170 if (virFileMakePathWithMode(path, 0750) < 0) {
5171 virReportSystemError(errno, _("Cannot create directory '%s'"), path);
5172 goto cleanup;
5175 if (qemuSecurityDomainSetPathLabel(driver, vm, path, true) < 0)
5176 goto cleanup;
5178 ret = 0;
5180 cleanup:
5181 return ret;
5185 static void
5186 qemuProcessStartWarnShmem(virDomainObjPtr vm)
5188 size_t i;
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
5200 * aim to do so.
5202 for (i = 0; i < vm->def->nnets; i++) {
5203 if (virDomainNetGetActualType(vm->def->nets[i]) ==
5204 VIR_DOMAIN_NET_TYPE_VHOSTUSER) {
5205 check_shmem = true;
5206 break;
5210 if (!check_shmem)
5211 return;
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) {
5222 shmem = true;
5223 break;
5228 if (!shmem) {
5229 VIR_WARN("Detected vhost-user interface without any shared memory, "
5230 "the interface might not be operational");
5235 static int
5236 qemuProcessStartValidateGraphics(virDomainObjPtr vm)
5238 size_t i;
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."));
5250 return -1;
5252 break;
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:
5259 break;
5263 return 0;
5267 static int
5268 qemuProcessStartValidateVideo(virDomainObjPtr vm,
5269 virQEMUCapsPtr qemuCaps)
5271 size_t i;
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));
5293 return -1;
5296 if (video->accel) {
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));
5303 return -1;
5308 return 0;
5312 static int
5313 qemuProcessStartValidateIOThreads(virDomainObjPtr vm,
5314 virQEMUCapsPtr qemuCaps)
5316 size_t i;
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"));
5322 return -1;
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 "
5334 "this QEMU"));
5335 return -1;
5339 return 0;
5343 static int
5344 qemuProcessStartValidateShmem(virDomainObjPtr vm)
5346 size_t i;
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 '/'"),
5354 shmem->name);
5355 return -1;
5359 return 0;
5363 static int
5364 qemuProcessStartValidateDisks(virDomainObjPtr vm,
5365 virQEMUCapsPtr qemuCaps)
5367 size_t i;
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 "
5381 "QEMU binary"));
5382 return -1;
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"));
5390 return -1;
5394 return 0;
5398 static int
5399 qemuProcessStartValidateTSC(virDomainObjPtr vm,
5400 virCapsPtr caps)
5402 size_t i;
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;
5412 break;
5416 if (freq == 0)
5417 return 0;
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");
5423 return 0;
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)
5431 return 0;
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");
5436 return 0;
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 "
5442 "by the host CPU"),
5443 freq, tsc->frequency);
5444 return -1;
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.
5460 static int
5461 qemuProcessStartValidate(virQEMUDriverPtr driver,
5462 virDomainObjPtr vm,
5463 virQEMUCapsPtr qemuCaps,
5464 virCapsPtr caps,
5465 unsigned int flags)
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."));
5476 return -1;
5480 VIR_DEBUG("Checking domain and device security labels");
5481 if (qemuSecurityCheckAllLabel(driver->securityManager, vm->def) < 0)
5482 return -1;
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)
5491 return -1;
5493 if (qemuProcessStartValidateGraphics(vm) < 0)
5494 return -1;
5496 if (qemuProcessStartValidateVideo(vm, qemuCaps) < 0)
5497 return -1;
5499 if (qemuProcessStartValidateIOThreads(vm, qemuCaps) < 0)
5500 return -1;
5502 if (qemuProcessStartValidateShmem(vm) < 0)
5503 return -1;
5505 if (vm->def->cpu) {
5506 if (virCPUValidateFeatures(vm->def->os.arch, vm->def->cpu) < 0)
5507 return -1;
5509 if (ARCH_IS_X86(vm->def->os.arch) &&
5510 !virQEMUCapsGet(qemuCaps, QEMU_CAPS_CPU_UNAVAILABLE_FEATURES)) {
5511 VIR_AUTOSTRINGLIST features = NULL;
5512 int n;
5514 if ((n = virCPUDefCheckFeatures(vm->def->cpu,
5515 virCPUx86FeatureFilterSelectMSR,
5516 NULL,
5517 &features)) < 0)
5518 return -1;
5520 if (n > 0) {
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);
5527 return -1;
5532 if (qemuProcessStartValidateDisks(vm, qemuCaps) < 0)
5533 return -1;
5535 if (qemuProcessStartValidateTSC(vm, caps) < 0)
5536 return -1;
5538 VIR_DEBUG("Checking for any possible (non-fatal) issues");
5540 qemuProcessStartWarnShmem(vm);
5542 return 0;
5546 static int
5547 qemuProcessStartUpdateCustomCaps(virDomainObjPtr vm)
5549 qemuDomainObjPrivatePtr priv = vm->privateData;
5550 VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(priv->driver);
5551 qemuDomainXmlNsDefPtr nsdef = vm->def->namespaceData;
5552 char **next;
5553 int tmp;
5554 size_t i;
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'"),
5561 *next);
5562 return -1;
5565 virQEMUCapsClear(priv->qemuCaps, tmp);
5569 if (nsdef) {
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'"),
5574 nsdef->capsadd[i]);
5575 return -1;
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'"),
5585 nsdef->capsdel[i]);
5586 return -1;
5589 virQEMUCapsClear(priv->qemuCaps, tmp);
5593 return 0;
5598 * qemuProcessInit:
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,
5611 virDomainObjPtr vm,
5612 virCPUDefPtr updatedCPU,
5613 qemuDomainAsyncJob asyncJob,
5614 bool migration,
5615 unsigned int flags)
5617 virCapsPtr caps = NULL;
5618 qemuDomainObjPrivatePtr priv = vm->privateData;
5619 int stopFlags;
5620 virCPUDefPtr origCPU = NULL;
5621 int ret = -1;
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"));
5631 goto cleanup;
5634 if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
5635 goto cleanup;
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)
5643 goto cleanup;
5646 VIR_DEBUG("Determining emulator version");
5647 if (qemuDomainUpdateQEMUCaps(vm, driver->qemuCapsCache) < 0)
5648 goto cleanup;
5650 if (flags & VIR_QEMU_PROCESS_START_STANDALONE)
5651 virQEMUCapsClear(priv->qemuCaps, QEMU_CAPS_CHARDEV_FD_PASS);
5653 if (qemuDomainUpdateCPU(vm, updatedCPU, &origCPU) < 0)
5654 goto cleanup;
5656 if (qemuProcessStartValidate(driver, vm, priv->qemuCaps, caps, flags) < 0)
5657 goto cleanup;
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)
5665 goto cleanup;
5667 /* Update qemu capabilities according to lists passed in via namespace */
5668 if (qemuProcessStartUpdateCustomCaps(vm) < 0)
5669 goto cleanup;
5671 if (flags & VIR_QEMU_PROCESS_START_PRETEND) {
5672 if (qemuDomainSetPrivatePaths(driver, vm) < 0) {
5673 virDomainObjRemoveTransientDef(vm);
5674 goto cleanup;
5676 } else {
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)
5688 goto stop;
5690 if (qemuDomainSetPrivatePaths(driver, vm) < 0)
5691 goto stop;
5693 VIR_STEAL_PTR(priv->origCPU, origCPU);
5696 ret = 0;
5698 cleanup:
5699 virCPUDefFree(origCPU);
5700 virObjectUnref(caps);
5701 return ret;
5703 stop:
5704 stopFlags = VIR_QEMU_PROCESS_STOP_NO_RELABEL;
5705 if (migration)
5706 stopFlags |= VIR_QEMU_PROCESS_STOP_MIGRATED;
5707 qemuProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED, asyncJob, stopFlags);
5708 goto cleanup;
5713 * qemuProcessNetworkPrepareDevices
5715 static int
5716 qemuProcessNetworkPrepareDevices(virDomainDefPtr def)
5718 int ret = -1;
5719 size_t i;
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()))
5732 goto cleanup;
5733 if (virDomainNetAllocateActualDevice(conn, def, net) < 0)
5734 goto cleanup;
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
5745 * separately.
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);
5758 goto cleanup;
5760 if (virDomainHostdevInsert(def, hostdev) < 0)
5761 goto cleanup;
5764 ret = 0;
5765 cleanup:
5766 virObjectUnref(conn);
5767 return ret;
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;
5789 size_t i = 0;
5791 if (qemuProcessSetupPid(vm, vcpupid, VIR_CGROUP_THREAD_VCPU,
5792 vcpuid, vcpu->cpumask,
5793 vm->def->cputune.period,
5794 vm->def->cputune.quota,
5795 &vcpu->sched) < 0)
5796 return -1;
5798 for (i = 0; i < vm->def->nresctrls; i++) {
5799 size_t j = 0;
5800 virDomainResctrlDefPtr ct = vm->def->resctrls[i];
5802 if (virBitmapIsBitSet(ct->vcpus, vcpuid)) {
5803 if (virResctrlAllocAddPID(ct->alloc, vcpupid) < 0)
5804 return -1;
5806 for (j = 0; j < ct->nmonitors; j++) {
5807 mon = ct->monitors[j];
5809 if (virBitmapEqual(ct->vcpus, mon->vcpus) &&
5810 !virResctrlAllocIsEmpty(ct->alloc))
5811 continue;
5813 if (virBitmapIsBitSet(mon->vcpus, vcpuid)) {
5814 if (virResctrlMonitorAddPID(mon->instance, vcpupid) < 0)
5815 return -1;
5816 break;
5820 break;
5824 return 0;
5828 static int
5829 qemuProcessSetupVcpus(virDomainObjPtr vm)
5831 virDomainVcpuDefPtr vcpu;
5832 unsigned int maxvcpus = virDomainDefGetVcpusMax(vm->def);
5833 size_t i;
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"));
5840 return -1;
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);
5849 if (!vcpu->online)
5850 continue;
5852 if (vcpu->cpumask &&
5853 !virBitmapEqual(vm->def->cpumask, vcpu->cpumask)) {
5854 virReportError(VIR_ERR_OPERATION_INVALID, "%s",
5855 _("cpu affinity is not supported"));
5856 return -1;
5860 return 0;
5863 for (i = 0; i < maxvcpus; i++) {
5864 vcpu = virDomainDefGetVcpu(vm->def, i);
5866 if (!vcpu->online)
5867 continue;
5869 if (qemuProcessSetupVcpu(vm, i) < 0)
5870 return -1;
5873 return 0;
5878 qemuProcessSetupIOThread(virDomainObjPtr vm,
5879 virDomainIOThreadIDDefPtr iothread)
5881 return qemuProcessSetupPid(vm, iothread->thread_id,
5882 VIR_CGROUP_THREAD_IOTHREAD,
5883 iothread->iothread_id,
5884 iothread->cpumask,
5885 vm->def->cputune.iothread_period,
5886 vm->def->cputune.iothread_quota,
5887 &iothread->sched);
5891 static int
5892 qemuProcessSetupIOThreads(virDomainObjPtr vm)
5894 size_t i;
5896 for (i = 0; i < vm->def->niothreadids; i++) {
5897 virDomainIOThreadIDDefPtr info = vm->def->iothreadids[i];
5899 if (qemuProcessSetupIOThread(vm, info) < 0)
5900 return -1;
5903 return 0;
5907 static int
5908 qemuProcessValidateHotpluggableVcpus(virDomainDefPtr def)
5910 virDomainVcpuDefPtr vcpu;
5911 virDomainVcpuDefPtr subvcpu;
5912 qemuDomainVcpuPrivatePtr vcpupriv;
5913 unsigned int maxvcpus = virDomainDefGetVcpusMax(def);
5914 size_t i = 0;
5915 size_t j;
5916 virBitmapPtr ordermap = NULL;
5917 int ret = -1;
5919 if (!(ordermap = virBitmapNew(maxvcpus + 1)))
5920 goto cleanup;
5922 /* validate:
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)
5933 continue;
5935 if (vcpu->order != 0) {
5936 if (virBitmapIsBitSet(ordermap, vcpu->order)) {
5937 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5938 _("duplicate vcpu order '%u'"), vcpu->order);
5939 goto cleanup;
5942 if (virBitmapSetBit(ordermap, vcpu->order)) {
5943 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5944 _("vcpu order '%u' exceeds vcpu count"),
5945 vcpu->order);
5946 goto cleanup;
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);
5958 goto cleanup;
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) ||
5965 !vcpupriv->type) {
5966 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
5967 _("vcpu '%zu' is missing hotplug data"), i);
5968 goto cleanup;
5973 ret = 0;
5974 cleanup:
5975 virBitmapFree(ordermap);
5976 return ret;
5980 static int
5981 qemuDomainHasHotpluggableStartupVcpus(virDomainDefPtr def)
5983 size_t maxvcpus = virDomainDefGetVcpusMax(def);
5984 virDomainVcpuDefPtr vcpu;
5985 size_t i;
5987 for (i = 0; i < maxvcpus; i++) {
5988 vcpu = virDomainDefGetVcpu(def, i);
5990 if (vcpu->online && vcpu->hotpluggable == VIR_TRISTATE_BOOL_YES)
5991 return true;
5994 return false;
5998 static int
5999 qemuProcessVcpusSortOrder(const void *a,
6000 const void *b)
6002 virDomainVcpuDefPtr vcpua = *((virDomainVcpuDefPtr *)a);
6003 virDomainVcpuDefPtr vcpub = *((virDomainVcpuDefPtr *)b);
6005 return vcpua->order - vcpub->order;
6009 static int
6010 qemuProcessSetupHotpluggableVcpus(virQEMUDriverPtr driver,
6011 virDomainObjPtr vm,
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;
6020 size_t i;
6021 int ret = -1;
6022 int rc;
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)
6034 goto cleanup;
6036 if (VIR_APPEND_ELEMENT(bootHotplug, nbootHotplug, vcpu) < 0)
6037 goto cleanup;
6041 if (nbootHotplug == 0) {
6042 ret = 0;
6043 goto cleanup;
6046 qsort(bootHotplug, nbootHotplug, sizeof(*bootHotplug),
6047 qemuProcessVcpusSortOrder);
6049 if (qemuCgroupEmulatorAllNodesAllow(priv->cgroup, &emulatorCgroup) < 0)
6050 goto cleanup;
6052 for (i = 0; i < nbootHotplug; i++) {
6053 vcpu = bootHotplug[i];
6055 if (!(vcpuprops = qemuBuildHotpluggableCPUProps(vcpu)))
6056 goto cleanup;
6058 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
6059 goto cleanup;
6061 rc = qemuMonitorAddDeviceArgs(qemuDomainGetMonitor(vm), vcpuprops);
6062 vcpuprops = NULL;
6064 if (qemuDomainObjExitMonitor(driver, vm) < 0)
6065 goto cleanup;
6067 if (rc < 0)
6068 goto cleanup;
6070 virJSONValueFree(vcpuprops);
6073 ret = 0;
6075 cleanup:
6076 qemuCgroupEmulatorAllNodesRestore(emulatorCgroup);
6077 VIR_FREE(bootHotplug);
6078 virJSONValueFree(vcpuprops);
6079 return ret;
6083 static int
6084 qemuProcessUpdateGuestCPU(virDomainDefPtr def,
6085 virQEMUCapsPtr qemuCaps,
6086 virCapsPtr caps,
6087 unsigned int flags)
6089 if (!def->cpu)
6090 return 0;
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)
6094 return 0;
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 &&
6104 def->cpu->model) {
6105 def->cpu->mode = VIR_CPU_MODE_CUSTOM;
6108 if (!virQEMUCapsIsCPUModeSupported(qemuCaps, caps, def->virtType,
6109 def->cpu->mode)) {
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));
6117 return -1;
6120 if (virCPUConvertLegacy(caps->host.arch, def->cpu) < 0)
6121 return -1;
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)
6130 return -1;
6132 if (virCPUUpdate(def->os.arch, def->cpu,
6133 virQEMUCapsGetHostModel(qemuCaps, def->virtType,
6134 VIR_QEMU_CAPS_HOST_CPU_MIGRATABLE)) < 0)
6135 return -1;
6137 if (virCPUTranslate(def->os.arch, def->cpu,
6138 virQEMUCapsGetCPUDefinitions(qemuCaps, def->virtType)) < 0)
6139 return -1;
6141 def->cpu->fallback = VIR_CPU_FALLBACK_FORBID;
6144 if (virCPUDefFilterFeatures(def->cpu, virQEMUCapsCPUFilterFeatures,
6145 &def->os.arch) < 0)
6146 return -1;
6148 return 0;
6152 static int
6153 qemuProcessPrepareDomainNUMAPlacement(virDomainObjPtr vm,
6154 virCapsPtr caps)
6156 qemuDomainObjPrivatePtr priv = vm->privateData;
6157 char *nodeset = NULL;
6158 virBitmapPtr numadNodeset = NULL;
6159 virBitmapPtr hostMemoryNodeset = NULL;
6160 int ret = -1;
6162 /* Get the advisory nodeset from numad if 'placement' of
6163 * either <vcpu> or <numatune> is 'auto'.
6165 if (!virDomainDefNeedsPlacementAdvice(vm->def))
6166 return 0;
6168 nodeset = virNumaGetAutoPlacementAdvice(virDomainDefGetVcpus(vm->def),
6169 virDomainDefGetMemoryTotal(vm->def));
6171 if (!nodeset)
6172 goto cleanup;
6174 if (!(hostMemoryNodeset = virNumaGetHostMemoryNodeset()))
6175 goto cleanup;
6177 VIR_DEBUG("Nodeset returned from numad: %s", nodeset);
6179 if (virBitmapParse(nodeset, &numadNodeset, VIR_DOMAIN_CPUMASK_LEN) < 0)
6180 goto cleanup;
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)))
6186 goto cleanup;
6188 virBitmapIntersect(numadNodeset, hostMemoryNodeset);
6190 VIR_STEAL_PTR(priv->autoNodeset, numadNodeset);
6192 ret = 0;
6194 cleanup:
6195 VIR_FREE(nodeset);
6196 virBitmapFree(numadNodeset);
6197 virBitmapFree(hostMemoryNodeset);
6198 return ret;
6202 static int
6203 qemuProcessPrepareDomainStorage(virQEMUDriverPtr driver,
6204 virDomainObjPtr vm,
6205 qemuDomainObjPrivatePtr priv,
6206 virQEMUDriverConfigPtr cfg,
6207 unsigned int flags)
6209 size_t i;
6210 bool cold_boot = flags & VIR_QEMU_PROCESS_START_COLD;
6212 for (i = vm->def->ndisks; i > 0; i--) {
6213 size_t idx = i - 1;
6214 virDomainDiskDefPtr disk = vm->def->disks[idx];
6216 if (virDomainDiskTranslateSourcePool(disk) < 0) {
6217 if (qemuDomainCheckDiskStartupPolicy(driver, vm, idx, cold_boot) < 0)
6218 return -1;
6220 /* disk source was dropped */
6221 continue;
6224 if (qemuDomainPrepareDiskSource(disk, priv, cfg) < 0)
6225 return -1;
6228 return 0;
6232 static void
6233 qemuProcessPrepareAllowReboot(virDomainObjPtr vm)
6235 virDomainDefPtr def = vm->def;
6236 qemuDomainObjPrivatePtr priv = vm->privateData;
6238 if (priv->allowReboot != VIR_TRISTATE_BOOL_ABSENT)
6239 return;
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;
6246 } else {
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,
6269 virDomainObjPtr vm,
6270 unsigned int flags)
6272 int ret = -1;
6273 size_t i;
6274 qemuDomainObjPrivatePtr priv = vm->privateData;
6275 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
6276 virCapsPtr caps;
6278 if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
6279 goto cleanup;
6281 priv->machineName = qemuDomainGetMachineName(vm);
6282 if (!priv->machineName)
6283 goto cleanup;
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);
6291 goto cleanup;
6293 virDomainAuditSecurityLabel(vm, true);
6295 if (qemuProcessPrepareDomainNUMAPlacement(vm, caps) < 0)
6296 goto cleanup;
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);
6316 break;
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
6325 * use in hotplug
6327 VIR_DEBUG("Assigning domain PCI addresses");
6328 if ((qemuDomainAssignAddresses(vm->def, priv->qemuCaps, driver, vm,
6329 !!(flags & VIR_QEMU_PROCESS_START_NEW))) < 0) {
6330 goto cleanup;
6333 if (qemuAssignDeviceAliases(vm->def, priv->qemuCaps) < 0)
6334 goto cleanup;
6336 VIR_DEBUG("Setting graphics devices");
6337 if (qemuProcessSetupGraphics(driver, vm, priv->qemuCaps, flags) < 0)
6338 goto cleanup;
6340 VIR_DEBUG("Create domain masterKey");
6341 if (qemuDomainMasterKeyCreate(vm) < 0)
6342 goto cleanup;
6344 VIR_DEBUG("Setting up storage");
6345 if (qemuProcessPrepareDomainStorage(driver, vm, priv, cfg, flags) < 0)
6346 goto cleanup;
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)
6353 goto cleanup;
6355 VIR_DEBUG("Prepare bios/uefi paths");
6356 if (qemuFirmwareFillDomain(driver, vm, flags) < 0)
6357 goto cleanup;
6359 for (i = 0; i < vm->def->nchannels; i++) {
6360 if (qemuDomainPrepareChannel(vm->def->channels[i],
6361 priv->channelTargetDir) < 0)
6362 goto cleanup;
6365 if (!(priv->monConfig = virDomainChrSourceDefNew(driver->xmlopt)))
6366 goto cleanup;
6368 VIR_DEBUG("Preparing monitor state");
6369 if (qemuProcessPrepareMonitorChr(priv->monConfig, priv->libDir) < 0)
6370 goto cleanup;
6372 priv->monError = false;
6373 priv->monStart = 0;
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)
6379 goto cleanup;
6381 for (i = 0; i < vm->def->nshmems; i++) {
6382 if (qemuDomainPrepareShmemChardev(vm->def->shmems[i]) < 0)
6383 goto cleanup;
6386 ret = 0;
6387 cleanup:
6388 virObjectUnref(caps);
6389 virObjectUnref(cfg);
6390 return ret;
6394 static int
6395 qemuProcessSEVCreateFile(virDomainObjPtr vm,
6396 const char *name,
6397 const char *data)
6399 qemuDomainObjPrivatePtr priv = vm->privateData;
6400 virQEMUDriverPtr driver = priv->driver;
6401 char *configFile;
6402 int ret = -1;
6404 if (!(configFile = virFileBuildPath(priv->libDir, name, ".base64")))
6405 return -1;
6407 if (virFileRewriteStr(configFile, S_IRUSR | S_IWUSR, data) < 0) {
6408 virReportSystemError(errno, _("failed to write data to config '%s'"),
6409 configFile);
6410 goto cleanup;
6413 if (qemuSecurityDomainSetPathLabel(driver, vm, configFile, true) < 0)
6414 goto cleanup;
6416 ret = 0;
6417 cleanup:
6418 VIR_FREE(configFile);
6419 return ret;
6423 static int
6424 qemuProcessPrepareSEVGuestInput(virDomainObjPtr vm)
6426 qemuDomainObjPrivatePtr priv = vm->privateData;
6427 virDomainDefPtr def = vm->def;
6428 virQEMUCapsPtr qemuCaps = priv->qemuCaps;
6429 virDomainSEVDefPtr sev = def->sev;
6431 if (!sev)
6432 return 0;
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);
6440 return -1;
6443 if (sev->dh_cert) {
6444 if (qemuProcessSEVCreateFile(vm, "dh_cert", sev->dh_cert) < 0)
6445 return -1;
6448 if (sev->session) {
6449 if (qemuProcessSEVCreateFile(vm, "session", sev->session) < 0)
6450 return -1;
6453 return 0;
6457 static int
6458 qemuProcessPrepareHostStorage(virQEMUDriverPtr driver,
6459 virDomainObjPtr vm,
6460 unsigned int flags)
6462 qemuDomainObjPrivatePtr priv = vm->privateData;
6463 size_t i;
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--) {
6468 size_t idx = i - 1;
6469 virDomainDiskDefPtr disk = vm->def->disks[idx];
6471 if (virStorageSourceIsEmpty(disk->src))
6472 continue;
6474 /* backing chain needs to be redetected if we aren't using blockdev */
6475 if (!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)
6487 continue;
6489 if (qemuDomainCheckDiskStartupPolicy(driver, vm, idx, cold_boot) >= 0)
6490 continue;
6492 return -1;
6495 return 0;
6500 qemuProcessOpenVhostVsock(virDomainVsockDefPtr vsock)
6502 qemuDomainVsockPrivatePtr priv = (qemuDomainVsockPrivatePtr)vsock->privateData;
6503 const char *vsock_path = "/dev/vhost-vsock";
6504 int fd;
6506 if ((fd = open(vsock_path, O_RDWR)) < 0) {
6507 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
6508 "%s", _("unable to open vhost-vsock device"));
6509 return -1;
6512 if (vsock->auto_cid == VIR_TRISTATE_BOOL_YES) {
6513 if (virVsockAcquireGuestCid(fd, &vsock->guest_cid) < 0)
6514 goto error;
6515 } else {
6516 if (virVsockSetGuestCid(fd, vsock->guest_cid) < 0)
6517 goto error;
6520 priv->vhostfd = fd;
6521 return 0;
6523 error:
6524 VIR_FORCE_CLOSE(fd);
6525 return -1;
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,
6543 virDomainObjPtr vm,
6544 unsigned int flags)
6546 int ret = -1;
6547 unsigned int hostdev_flags = 0;
6548 qemuDomainObjPrivatePtr priv = vm->privateData;
6549 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
6551 if (qemuPrepareNVRAM(cfg, vm) < 0)
6552 goto cleanup;
6554 if (vm->def->vsock) {
6555 if (qemuProcessOpenVhostVsock(vm->def->vsock) < 0)
6556 goto cleanup;
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)
6564 goto cleanup;
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,
6573 hostdev_flags) < 0)
6574 goto cleanup;
6576 VIR_DEBUG("Preparing chr devices");
6577 if (virDomainChrDefForeach(vm->def,
6578 true,
6579 qemuProcessPrepareChardevDevice,
6580 NULL) < 0)
6581 goto cleanup;
6583 if (qemuProcessBuildDestroyMemoryPaths(driver, vm, NULL, true) < 0)
6584 goto cleanup;
6586 /* Ensure no historical cgroup for this VM is lying around bogus
6587 * settings */
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"),
6594 cfg->logDir);
6595 goto cleanup;
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."));
6602 goto cleanup;
6605 if (unlink(priv->pidfile) < 0 &&
6606 errno != ENOENT) {
6607 virReportSystemError(errno,
6608 _("Cannot remove stale PID file %s"),
6609 priv->pidfile);
6610 goto cleanup;
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)
6619 goto cleanup;
6621 VIR_DEBUG("Write domain masterKey");
6622 if (qemuDomainWriteMasterKeyFile(driver, vm) < 0)
6623 goto cleanup;
6625 VIR_DEBUG("Preparing disks (host)");
6626 if (qemuProcessPrepareHostStorage(driver, vm, flags) < 0)
6627 goto cleanup;
6629 VIR_DEBUG("Preparing external devices");
6630 if (qemuExtDevicesPrepareHost(driver, vm->def) < 0)
6631 goto cleanup;
6633 if (qemuProcessPrepareSEVGuestInput(vm) < 0)
6634 goto cleanup;
6636 ret = 0;
6637 cleanup:
6638 virObjectUnref(cfg);
6639 return ret;
6644 * qemuProcessGenID:
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.
6654 static int
6655 qemuProcessGenID(virDomainObjPtr vm,
6656 unsigned int flags)
6658 if (!vm->def->genidRequested)
6659 return 0;
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"));
6668 return -1;
6672 return 0;
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.
6683 static int
6684 qemuProcessSetupDiskThrottlingBlockdev(virQEMUDriverPtr driver,
6685 virDomainObjPtr vm,
6686 qemuDomainAsyncJob asyncJob)
6688 qemuDomainObjPrivatePtr priv = vm->privateData;
6689 size_t i;
6690 int ret = -1;
6692 if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV))
6693 return 0;
6695 VIR_DEBUG("Setting up disk throttling for -blockdev via block_set_io_throttle");
6697 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
6698 return -1;
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))
6705 continue;
6707 if (qemuMonitorSetBlockIoThrottle(qemuDomainGetMonitor(vm), NULL,
6708 diskPriv->qomName, &disk->blkdeviotune,
6709 true, true, true) < 0)
6710 goto cleanup;
6713 ret = 0;
6715 cleanup:
6716 if (qemuDomainObjExitMonitor(driver, vm) < 0)
6717 ret = -1;
6718 return ret;
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,
6738 virDomainObjPtr vm,
6739 qemuDomainAsyncJob asyncJob,
6740 qemuProcessIncomingDefPtr incoming,
6741 virDomainMomentObjPtr snapshot,
6742 virNetDevVPortProfileOp vmop,
6743 unsigned int flags)
6745 int ret = -1;
6746 int rv;
6747 int logfile = -1;
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;
6756 size_t i;
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"));
6782 return -1;
6785 hookData.vm = vm;
6786 hookData.driver = driver;
6787 /* We don't increase cfg's reference counter here. */
6788 hookData.cfg = cfg;
6790 if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
6791 goto cleanup;
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"));
6797 goto cleanup;
6799 logfile = qemuDomainLogContextGetWriteFD(logCtxt);
6801 if (qemuProcessGenID(vm, flags) < 0)
6802 goto cleanup;
6804 if (qemuExtDevicesStart(driver, vm, logCtxt, incoming != NULL) < 0)
6805 goto cleanup;
6807 VIR_DEBUG("Building emulator command line");
6808 if (!(cmd = qemuBuildCommandLine(driver,
6809 qemuDomainLogContextGetManager(logCtxt),
6810 driver->securityManager,
6812 incoming ? incoming->launchURI : NULL,
6813 snapshot, vmop,
6814 false,
6815 qemuCheckFips(),
6816 &nnicindexes, &nicindexes)))
6817 goto cleanup;
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)
6826 goto cleanup;
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)
6837 goto cleanup;
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)
6846 goto cleanup;
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,
6856 vm->def, cmd) < 0)
6857 goto cleanup;
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)
6867 goto cleanup;
6868 rv = virCommandRun(cmd, NULL);
6869 qemuSecurityPostFork(driver->securityManager);
6871 /* wait for qemu process to show up */
6872 if (rv == 0) {
6873 if ((rv = virPidFileReadPath(priv->pidfile, &vm->pid)) < 0) {
6874 virReportSystemError(-rv,
6875 _("Domain %s didn't show up"),
6876 vm->def->name);
6877 goto cleanup;
6879 VIR_DEBUG("QEMU vm=%p name=%s running with pid=%lld",
6880 vm, vm->def->name, (long long)vm->pid);
6881 } else {
6882 VIR_DEBUG("QEMU vm=%p name=%s failed to spawn",
6883 vm, vm->def->name);
6884 goto cleanup;
6887 VIR_DEBUG("Writing early domain status to disk");
6888 if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0)
6889 goto cleanup;
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"));
6896 goto cleanup;
6899 VIR_DEBUG("Setting up domain cgroup (if required)");
6900 if (qemuSetupCgroup(vm, nnicindexes, nicindexes) < 0)
6901 goto cleanup;
6903 if (!(priv->perf = virPerfNew()))
6904 goto cleanup;
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)
6909 goto cleanup;
6912 /* This must be done after cgroup placement to avoid resetting CPU
6913 * affinity */
6914 if (qemuProcessInitCpuAffinity(vm) < 0)
6915 goto cleanup;
6917 VIR_DEBUG("Setting emulator tuning/settings");
6918 if (qemuProcessSetupEmulator(vm) < 0)
6919 goto cleanup;
6921 VIR_DEBUG("Setting cgroup for external devices (if required)");
6922 if (qemuSetupCgroupForExtDevices(vm, driver) < 0)
6923 goto cleanup;
6925 VIR_DEBUG("Setting up resctrl");
6926 if (qemuProcessResctrlCreate(driver, vm) < 0)
6927 goto cleanup;
6929 VIR_DEBUG("Setting up managed PR daemon");
6930 if (virDomainDefHasManagedPR(vm->def) &&
6931 qemuProcessStartManagedPRDaemon(vm) < 0)
6932 goto cleanup;
6934 VIR_DEBUG("Setting domain security labels");
6935 if (qemuSecuritySetAllLabel(driver,
6937 incoming ? incoming->path : NULL) < 0)
6938 goto cleanup;
6940 /* Security manager labeled all devices, therefore
6941 * if any operation from now on fails, we need to ask the caller to
6942 * restore labels.
6944 ret = -2;
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);
6957 goto cleanup;
6959 if (S_ISFIFO(stdin_sb.st_mode) &&
6960 qemuSecuritySetImageFDLabel(driver->securityManager,
6961 vm->def, incoming->fd) < 0)
6962 goto cleanup;
6965 VIR_DEBUG("Labelling done, completing handshake to child");
6966 if (virCommandHandshakeNotify(cmd) < 0)
6967 goto cleanup;
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 */
6974 goto cleanup;
6976 VIR_DEBUG("Waiting for monitor to show up");
6977 if (qemuProcessWaitForMonitor(driver, vm, asyncJob, logCtxt) < 0)
6978 goto cleanup;
6980 if (qemuConnectAgent(driver, vm) < 0)
6981 goto cleanup;
6983 VIR_DEBUG("Verifying and updating provided guest CPU");
6984 if (qemuProcessUpdateAndVerifyCPU(driver, vm, asyncJob) < 0)
6985 goto cleanup;
6987 VIR_DEBUG("setting up hotpluggable cpus");
6988 if (qemuDomainHasHotpluggableStartupVcpus(vm->def)) {
6989 if (qemuDomainRefreshVcpuInfo(driver, vm, asyncJob, false) < 0)
6990 goto cleanup;
6992 if (qemuProcessValidateHotpluggableVcpus(vm->def) < 0)
6993 goto cleanup;
6995 if (qemuProcessSetupHotpluggableVcpus(driver, vm, asyncJob) < 0)
6996 goto cleanup;
6999 VIR_DEBUG("Refreshing VCPU info");
7000 if (qemuDomainRefreshVcpuInfo(driver, vm, asyncJob, false) < 0)
7001 goto cleanup;
7003 if (qemuDomainValidateVcpuInfo(vm) < 0)
7004 goto cleanup;
7006 qemuDomainVcpuPersistOrder(vm->def);
7008 VIR_DEBUG("Detecting IOThread PIDs");
7009 if (qemuProcessDetectIOThreadPIDs(driver, vm, asyncJob) < 0)
7010 goto cleanup;
7012 VIR_DEBUG("Setting global CPU cgroup (if required)");
7013 if (qemuSetupGlobalCpuCgroup(vm) < 0)
7014 goto cleanup;
7016 VIR_DEBUG("Setting vCPU tuning/settings");
7017 if (qemuProcessSetupVcpus(vm) < 0)
7018 goto cleanup;
7020 VIR_DEBUG("Setting IOThread tuning/settings");
7021 if (qemuProcessSetupIOThreads(vm) < 0)
7022 goto cleanup;
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)
7029 goto cleanup;
7031 VIR_DEBUG("Setting any required VM passwords");
7032 if (qemuProcessInitPasswords(driver, vm, asyncJob) < 0)
7033 goto cleanup;
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)
7040 goto cleanup;
7042 VIR_DEBUG("Setting initial memory amount");
7043 if (qemuProcessSetupBalloon(driver, vm, asyncJob) < 0)
7044 goto cleanup;
7046 if (qemuProcessSetupDiskThrottlingBlockdev(driver, vm, asyncJob) < 0)
7047 goto cleanup;
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)
7054 goto cleanup;
7056 if (flags & VIR_QEMU_PROCESS_START_AUTODESTROY &&
7057 qemuProcessAutoDestroyAdd(driver, vm, conn) < 0)
7058 goto cleanup;
7060 ret = 0;
7062 cleanup:
7063 if (ret < 0)
7064 qemuExtDevicesStop(driver, vm);
7065 qemuDomainSecretDestroy(vm);
7066 virCommandFree(cmd);
7067 virObjectUnref(logCtxt);
7068 virObjectUnref(cfg);
7069 virObjectUnref(caps);
7070 VIR_FREE(nicindexes);
7071 return ret;
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,
7087 virDomainObjPtr vm,
7088 qemuDomainAsyncJob asyncJob)
7090 qemuDomainObjPrivatePtr priv = vm->privateData;
7092 VIR_DEBUG("Fetching list of active devices");
7093 if (qemuDomainUpdateDeviceList(driver, vm, asyncJob) < 0)
7094 return -1;
7096 VIR_DEBUG("Updating info of memory devices");
7097 if (qemuDomainUpdateMemoryDeviceInfo(driver, vm, asyncJob) < 0)
7098 return -1;
7100 VIR_DEBUG("Detecting actual memory size for video device");
7101 if (qemuProcessUpdateVideoRamSize(driver, vm, asyncJob) < 0)
7102 return -1;
7104 VIR_DEBUG("Updating disk data");
7105 if (qemuProcessRefreshDisks(driver, vm, asyncJob) < 0)
7106 return -1;
7107 if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV) &&
7108 qemuBlockNodeNamesDetect(driver, vm, asyncJob) < 0)
7109 return -1;
7111 return 0;
7116 * qemuProcessFinishStartup:
7118 * Finish starting a new domain.
7121 qemuProcessFinishStartup(virQEMUDriverPtr driver,
7122 virDomainObjPtr vm,
7123 qemuDomainAsyncJob asyncJob,
7124 bool startCPUs,
7125 virDomainPausedReason pausedReason)
7127 virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
7128 int ret = -1;
7130 if (startCPUs) {
7131 VIR_DEBUG("Starting domain CPUs");
7132 if (qemuProcessStartCPUs(driver, vm,
7133 VIR_DOMAIN_RUNNING_BOOTED,
7134 asyncJob) < 0) {
7135 if (virGetLastErrorCode() == VIR_ERR_OK)
7136 virReportError(VIR_ERR_OPERATION_FAILED, "%s",
7137 _("resume operation failed"));
7138 goto cleanup;
7140 } else {
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)
7146 goto cleanup;
7148 if (qemuProcessStartHook(driver, vm,
7149 VIR_HOOK_QEMU_OP_STARTED,
7150 VIR_HOOK_SUBOP_BEGIN) < 0)
7151 goto cleanup;
7153 ret = 0;
7155 cleanup:
7156 virObjectUnref(cfg);
7157 return ret;
7162 qemuProcessStart(virConnectPtr conn,
7163 virQEMUDriverPtr driver,
7164 virDomainObjPtr vm,
7165 virCPUDefPtr updatedCPU,
7166 qemuDomainAsyncJob asyncJob,
7167 const char *migrateFrom,
7168 int migrateFd,
7169 const char *migratePath,
7170 virDomainMomentObjPtr snapshot,
7171 virNetDevVPortProfileOp vmop,
7172 unsigned int flags)
7174 qemuDomainObjPrivatePtr priv = vm->privateData;
7175 qemuProcessIncomingDefPtr incoming = NULL;
7176 unsigned int stopFlags;
7177 bool relabel = false;
7178 int ret = -1;
7179 int rv;
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)
7199 goto cleanup;
7201 if (migrateFrom) {
7202 incoming = qemuProcessIncomingDefNew(priv->qemuCaps, NULL, migrateFrom,
7203 migrateFd, migratePath);
7204 if (!incoming)
7205 goto stop;
7208 if (qemuProcessPrepareDomain(driver, vm, flags) < 0)
7209 goto stop;
7211 if (qemuProcessPrepareHost(driver, vm, flags) < 0)
7212 goto stop;
7214 if ((rv = qemuProcessLaunch(conn, driver, vm, asyncJob, incoming,
7215 snapshot, vmop, flags)) < 0) {
7216 if (rv == -2)
7217 relabel = true;
7218 goto stop;
7220 relabel = true;
7222 if (incoming) {
7223 if (incoming->deferredURI &&
7224 qemuMigrationDstRun(driver, vm, incoming->deferredURI, asyncJob) < 0)
7225 goto stop;
7226 } else {
7227 /* Refresh state of devices from QEMU. During migration this happens
7228 * in qemuMigrationDstFinish to ensure that state information is fully
7229 * transferred. */
7230 if (qemuProcessRefreshState(driver, vm, asyncJob) < 0)
7231 goto stop;
7234 if (qemuProcessFinishStartup(driver, vm, asyncJob,
7235 !(flags & VIR_QEMU_PROCESS_START_PAUSED),
7236 incoming ?
7237 VIR_DOMAIN_PAUSED_MIGRATION :
7238 VIR_DOMAIN_PAUSED_USER) < 0)
7239 goto stop;
7241 if (!incoming) {
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);
7247 ret = 0;
7249 cleanup:
7250 qemuProcessIncomingDefFree(incoming);
7251 return ret;
7253 stop:
7254 stopFlags = 0;
7255 if (!relabel)
7256 stopFlags |= VIR_QEMU_PROCESS_STOP_NO_RELABEL;
7257 if (migrateFrom)
7258 stopFlags |= VIR_QEMU_PROCESS_STOP_MIGRATED;
7259 if (priv->mon)
7260 qemuMonitorSetDomainLog(priv->mon, NULL, NULL, NULL);
7261 qemuProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED, asyncJob, stopFlags);
7262 goto cleanup;
7266 virCommandPtr
7267 qemuProcessCreatePretendCmd(virQEMUDriverPtr driver,
7268 virDomainObjPtr vm,
7269 const char *migrateURI,
7270 bool enableFips,
7271 bool standalone,
7272 unsigned int flags)
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;
7282 if (standalone)
7283 flags |= VIR_QEMU_PROCESS_START_STANDALONE;
7285 if (qemuProcessInit(driver, vm, NULL, QEMU_ASYNC_JOB_NONE,
7286 !!migrateURI, flags) < 0)
7287 goto cleanup;
7289 if (qemuProcessPrepareDomain(driver, vm, flags) < 0)
7290 goto cleanup;
7292 VIR_DEBUG("Building emulator command line");
7293 cmd = qemuBuildCommandLine(driver,
7294 NULL,
7295 driver->securityManager,
7297 migrateURI,
7298 NULL,
7299 VIR_NETDEV_VPORT_PROFILE_OP_NO_OP,
7300 standalone,
7301 enableFips,
7302 NULL,
7303 NULL);
7305 cleanup:
7306 return cmd;
7311 qemuProcessKill(virDomainObjPtr vm, unsigned int flags)
7313 int ret;
7315 VIR_DEBUG("vm=%p name=%s pid=%lld flags=0x%x",
7316 vm, vm->def->name,
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);
7322 return 0;
7326 if (flags & VIR_QEMU_PROCESS_KILL_NOWAIT) {
7327 virProcessKill(vm->pid,
7328 (flags & VIR_QEMU_PROCESS_KILL_FORCE) ?
7329 SIGKILL : SIGTERM);
7330 return 0;
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);
7339 return ret;
7344 * qemuProcessBeginStopJob:
7346 * Stop all current jobs by killing the domain and start a new one for
7347 * qemuProcessStop.
7350 qemuProcessBeginStopJob(virQEMUDriverPtr driver,
7351 virDomainObjPtr vm,
7352 qemuDomainJob job,
7353 bool forceKill)
7355 qemuDomainObjPrivatePtr priv = vm->privateData;
7356 unsigned int killFlags = forceKill ? VIR_QEMU_PROCESS_KILL_FORCE : 0;
7357 int ret = -1;
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)
7366 goto cleanup;
7368 /* Wake up anything waiting on domain condition */
7369 virDomainObjBroadcast(vm);
7371 if (qemuDomainObjBeginJob(driver, vm, job) < 0)
7372 goto cleanup;
7374 ret = 0;
7376 cleanup:
7377 priv->beingDestroyed = false;
7378 return ret;
7382 void qemuProcessStop(virQEMUDriverPtr driver,
7383 virDomainObjPtr vm,
7384 virDomainShutoffReason reason,
7385 qemuDomainAsyncJob asyncJob,
7386 unsigned int flags)
7388 int ret;
7389 int retries = 0;
7390 qemuDomainObjPrivatePtr priv = vm->privateData;
7391 virErrorPtr orig_err;
7392 virDomainDefPtr def;
7393 virNetDevVPortProfilePtr vport = NULL;
7394 size_t i;
7395 char *timestamp;
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,
7402 (long long)vm->pid,
7403 virDomainShutoffReasonTypeToString(reason),
7404 qemuDomainAsyncJobTypeToString(asyncJob),
7405 flags);
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)
7413 goto cleanup;
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);
7423 goto endjob;
7426 qemuProcessBuildDestroyMemoryPaths(driver, vm, NULL, false);
7428 vm->def->id = -1;
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",
7438 timestamp,
7439 virDomainShutoffReasonTypeToString(reason));
7440 VIR_FREE(timestamp);
7443 /* Clear network bandwidth */
7444 virDomainClearNetBandwidth(vm);
7446 virDomainConfVMNWFilterTeardown(vm);
7448 if (cfg->macFilter) {
7449 def = vm->def;
7450 for (i = 0; i < def->nnets; i++) {
7451 virDomainNetDefPtr net = def->nets[i];
7452 if (net->ifname == NULL)
7453 continue;
7454 ignore_value(ebtablesRemoveForwardAllowIn(driver->ebtables,
7455 net->ifname,
7456 &net->mac));
7460 virPortAllocatorRelease(priv->nbdPort);
7461 priv->nbdPort = 0;
7463 if (priv->agent) {
7464 qemuAgentClose(priv->agent);
7465 priv->agent = NULL;
7467 priv->agentError = false;
7469 if (priv->mon) {
7470 qemuMonitorClose(priv->mon);
7471 priv->mon = NULL;
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,
7491 false,
7492 qemuProcessCleanupChardevDevice,
7493 NULL));
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,
7515 NULL, xml, NULL));
7516 VIR_FREE(xml);
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);
7544 def = 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),
7556 cfg->stateDir));
7557 break;
7558 case VIR_DOMAIN_NET_TYPE_ETHERNET:
7559 if (net->ifname) {
7560 ignore_value(virNetDevTapDelete(net->ifname, net->backend.tap));
7561 VIR_FREE(net->ifname);
7563 break;
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));
7569 #endif
7570 break;
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. */
7581 break;
7583 /* release the physical device (or any other resources used by
7584 * this interface in the network driver
7586 if (vport) {
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),
7592 net->ifname));
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);
7601 else
7602 VIR_WARN("Unable to release network device '%s'", NULLSTR(net->ifname));
7606 retry:
7607 if ((ret = qemuRemoveCgroup(vm)) < 0) {
7608 if (ret == -EBUSY && (retries++ < 5)) {
7609 usleep(200*1000);
7610 goto retry;
7612 VIR_WARN("Failed to remove cgroup for %s",
7613 vm->def->name);
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++) {
7620 size_t j = 0;
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);
7658 } else {
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;
7672 vm->taint = 0;
7673 vm->pid = -1;
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,
7691 NULL, xml, NULL);
7692 VIR_FREE(xml);
7695 virDomainObjRemoveTransientDef(vm);
7697 endjob:
7698 if (asyncJob != QEMU_ASYNC_JOB_NONE)
7699 qemuDomainObjEndJob(driver, vm);
7701 cleanup:
7702 if (orig_err) {
7703 virSetError(orig_err);
7704 virFreeError(orig_err);
7706 virObjectUnref(conn);
7707 virObjectUnref(cfg);
7711 static void
7712 qemuProcessAutoDestroy(virDomainObjPtr dom,
7713 virConnectPtr conn,
7714 void *opaque)
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",
7728 dom->def->name);
7729 qemuDomainObjDiscardAsyncJob(driver, dom);
7732 VIR_DEBUG("Killing domain");
7734 if (qemuProcessBeginStopJob(driver, dom, QEMU_JOB_DESTROY, true) < 0)
7735 return;
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,
7753 virDomainObjPtr vm,
7754 virConnectPtr conn)
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,
7762 virDomainObjPtr vm)
7764 int ret;
7765 VIR_DEBUG("vm=%s", vm->def->name);
7766 ret = virCloseCallbacksUnset(driver->closeCallbacks, vm,
7767 qemuProcessAutoDestroy);
7768 return ret;
7771 bool qemuProcessAutoDestroyActive(virQEMUDriverPtr driver,
7772 virDomainObjPtr vm)
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,
7783 virDomainObjPtr vm,
7784 qemuDomainAsyncJob asyncJob)
7786 qemuDomainObjPrivatePtr priv = vm->privateData;
7787 bool blockdev = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV);
7788 virHashTablePtr table = NULL;
7789 int ret = -1;
7790 size_t i;
7792 if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) == 0) {
7793 table = qemuMonitorGetBlockInfo(priv->mon);
7794 if (qemuDomainObjExitMonitor(driver, vm) < 0)
7795 goto cleanup;
7798 if (!table)
7799 goto cleanup;
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;
7807 if (blockdev)
7808 entryname = diskpriv->qomName;
7810 if (!(info = virHashLookup(table, entryname)))
7811 continue;
7813 if (info->removable) {
7814 if (info->empty)
7815 virDomainDiskEmptySource(disk);
7817 if (info->tray) {
7818 if (info->tray_open)
7819 disk->tray_status = VIR_DOMAIN_DISK_TRAY_OPEN;
7820 else
7821 disk->tray_status = VIR_DOMAIN_DISK_TRAY_CLOSED;
7825 /* fill in additional data */
7826 diskpriv->removable = info->removable;
7827 diskpriv->tray = info->tray;
7830 ret = 0;
7832 cleanup:
7833 virHashFree(table);
7834 return ret;
7838 static int
7839 qemuProcessRefreshCPU(virQEMUDriverPtr driver,
7840 virDomainObjPtr vm)
7842 virCapsPtr caps = virQEMUDriverGetCapabilities(driver, false);
7843 qemuDomainObjPrivatePtr priv = vm->privateData;
7844 virCPUDefPtr host = NULL;
7845 virCPUDefPtr cpu = NULL;
7846 int ret = -1;
7848 if (!caps)
7849 return -1;
7851 if (!virQEMUCapsGuestIsNative(caps->host.arch, vm->def->os.arch) ||
7852 !caps->host.cpu ||
7853 !vm->def->cpu) {
7854 ret = 0;
7855 goto cleanup;
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
7861 * running domain.
7863 if (vm->def->cpu->mode == VIR_CPU_MODE_HOST_MODEL) {
7864 if (!(host = virCPUCopyMigratable(caps->host.cpu->arch, caps->host.cpu)))
7865 goto cleanup;
7867 if (!(cpu = virCPUDefCopyWithoutModel(host)) ||
7868 virCPUDefCopyModelFilter(cpu, host, false,
7869 virQEMUCapsCPUFilterFeatures,
7870 &caps->host.cpu->arch) < 0)
7871 goto cleanup;
7873 if (virCPUUpdate(vm->def->os.arch, vm->def->cpu, cpu) < 0)
7874 goto cleanup;
7876 if (qemuProcessUpdateCPU(driver, vm, QEMU_ASYNC_JOB_NONE) < 0)
7877 goto cleanup;
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)
7885 goto cleanup;
7888 ret = 0;
7890 cleanup:
7891 virCPUDefFree(cpu);
7892 virCPUDefFree(host);
7893 virObjectUnref(caps);
7894 return ret;
7898 static int
7899 qemuProcessRefreshLegacyBlockjob(void *payload,
7900 const void *name,
7901 void *opaque)
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);
7913 return 0;
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)))
7921 return -1;
7923 if (disk->mirror) {
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)
7939 goto cleanup;
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,
7946 true) < 0))
7947 goto cleanup;
7951 qemuBlockJobStarted(job, vm);
7953 cleanup:
7954 qemuBlockJobStartupFinalize(vm, job);
7956 return 0;
7960 static int
7961 qemuProcessRefreshLegacyBlockjobs(virQEMUDriverPtr driver,
7962 virDomainObjPtr vm)
7964 virHashTablePtr blockJobs = NULL;
7965 int ret = -1;
7967 qemuDomainObjEnterMonitor(driver, vm);
7968 blockJobs = qemuMonitorGetAllBlockJobInfo(qemuDomainGetMonitor(vm), true);
7969 if (qemuDomainObjExitMonitor(driver, vm) < 0 || !blockJobs)
7970 goto cleanup;
7972 if (virHashForEach(blockJobs, qemuProcessRefreshLegacyBlockjob, vm) < 0)
7973 goto cleanup;
7975 ret = 0;
7977 cleanup:
7978 virHashFree(blockJobs);
7979 return ret;
7983 static int
7984 qemuProcessRefreshBlockjobs(virQEMUDriverPtr driver,
7985 virDomainObjPtr vm)
7987 qemuDomainObjPrivatePtr priv = vm->privateData;
7989 if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV))
7990 return qemuBlockJobRefreshJobs(driver, vm);
7991 else
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:
8008 * 1. Enter job
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
8014 * 5. EndJob
8016 * We can't do normal MonitorEnter & MonitorExit because these two lock the
8017 * monitor lock, which does not exists in this early phase.
8019 static void
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;
8027 int state;
8028 int reason;
8029 virQEMUDriverConfigPtr cfg;
8030 size_t i;
8031 unsigned int stopFlags = 0;
8032 bool jobStarted = false;
8033 virCapsPtr caps = NULL;
8034 bool retry = true;
8035 bool tryMonReconn = false;
8037 virIdentitySetCurrent(data->identity);
8038 virObjectUnref(data->identity);
8039 VIR_FREE(data);
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)))
8049 goto error;
8051 if (qemuDomainObjBeginJob(driver, obj, QEMU_JOB_MODIFY) < 0)
8052 goto error;
8053 jobStarted = true;
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)))
8058 goto error;
8060 /* Restore the masterKey */
8061 if (qemuDomainMasterKeyReadFile(priv) < 0)
8062 goto error;
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)
8069 goto error;
8071 if (priv->qemuCaps &&
8072 virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_CHARDEV_FD_PASS))
8073 retry = false;
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)
8082 goto error;
8084 priv->machineName = qemuDomainGetMachineName(obj);
8085 if (!priv->machineName)
8086 goto error;
8088 if (qemuConnectCgroup(obj) < 0)
8089 goto error;
8091 if (qemuDomainPerfRestart(obj) < 0)
8092 goto error;
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)
8102 goto error;
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)
8112 goto error;
8113 } else {
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)
8120 goto error;
8123 for (i = 0; i < obj->def->ngraphics; i++) {
8124 if (qemuProcessGraphicsReservePorts(obj->def->graphics[i], true) < 0)
8125 goto error;
8128 if (qemuProcessUpdateState(driver, obj) < 0)
8129 goto error;
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",
8136 obj->def->name);
8137 goto error;
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))
8145 goto error;
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",
8155 obj->def->name);
8156 qemuProcessShutdownOrReboot(driver, obj);
8157 goto cleanup;
8160 if (qemuProcessBuildDestroyMemoryPaths(driver, obj, NULL, true) < 0)
8161 goto error;
8163 if ((qemuDomainAssignAddresses(obj->def, priv->qemuCaps,
8164 driver, obj, false)) < 0) {
8165 goto error;
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,
8172 obj->def));
8174 if (qemuProcessRefreshCPU(driver, obj) < 0)
8175 goto error;
8177 if (qemuDomainRefreshVcpuInfo(driver, obj, QEMU_ASYNC_JOB_NONE, true) < 0)
8178 goto error;
8180 qemuDomainVcpuPersistOrder(obj->def);
8182 if (qemuProcessDetectIOThreadPIDs(driver, obj, QEMU_ASYNC_JOB_NONE) < 0)
8183 goto error;
8185 if (qemuSecurityReserveLabel(driver->securityManager, obj->def, obj->pid) < 0)
8186 goto error;
8188 qemuProcessNotifyNets(obj->def);
8190 qemuProcessFiltersInstantiate(obj->def);
8192 if (qemuProcessRefreshDisks(driver, obj, QEMU_ASYNC_JOB_NONE) < 0)
8193 goto error;
8195 if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV) &&
8196 qemuBlockNodeNamesDetect(driver, obj, QEMU_ASYNC_JOB_NONE) < 0)
8197 goto error;
8199 if (qemuProcessRefreshBlockjobs(driver, obj) < 0)
8200 goto error;
8202 if (qemuRefreshVirtioChannelState(driver, obj, QEMU_ASYNC_JOB_NONE) < 0)
8203 goto error;
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)
8209 goto error;
8211 if (qemuProcessRecoverJob(driver, obj, &oldjob, &stopFlags) < 0)
8212 goto error;
8214 if (qemuProcessUpdateDevices(driver, obj) < 0)
8215 goto error;
8217 if (qemuRefreshPRManagerState(driver, obj) < 0)
8218 goto error;
8220 qemuProcessReconnectCheckMemAliasOrderMismatch(obj);
8222 if (qemuConnectAgent(driver, obj) < 0)
8223 goto error;
8225 for (i = 0; i < obj->def->nresctrls; i++) {
8226 size_t j = 0;
8228 if (virResctrlAllocDeterminePath(obj->def->resctrls[i]->alloc,
8229 priv->machineName) < 0)
8230 goto error;
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)
8238 goto error;
8242 /* update domain state XML with possibly updated state in virDomainObj */
8243 if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, obj, driver->caps) < 0)
8244 goto error;
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);
8249 int hookret;
8251 hookret = virHookCall(VIR_HOOK_DRIVER_QEMU, obj->def->name,
8252 VIR_HOOK_QEMU_OP_RECONNECT, VIR_HOOK_SUBOP_BEGIN,
8253 NULL, xml, NULL);
8254 VIR_FREE(xml);
8257 * If the script raised an error abort the launch
8259 if (hookret < 0)
8260 goto error;
8263 if (virAtomicIntInc(&driver->nactive) == 1 && driver->inhibitCallback)
8264 driver->inhibitCallback(true, driver->inhibitOpaque);
8266 cleanup:
8267 if (jobStarted) {
8268 if (!virDomainObjIsActive(obj))
8269 qemuDomainRemoveInactive(driver, obj);
8270 qemuDomainObjEndJob(driver, obj);
8271 } else {
8272 if (!virDomainObjIsActive(obj))
8273 qemuDomainRemoveInactiveJob(driver, obj);
8275 virDomainObjEndAPI(&obj);
8276 virObjectUnref(cfg);
8277 virObjectUnref(caps);
8278 virNWFilterUnlockFilterUpdates();
8279 virIdentitySetCurrent(NULL);
8280 return;
8282 error:
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;
8296 else if (priv->mon)
8297 state = VIR_DOMAIN_SHUTOFF_DAEMON;
8298 else
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);
8307 goto cleanup;
8310 static int
8311 qemuProcessReconnectHelper(virDomainObjPtr obj,
8312 void *opaque)
8314 virThread thread;
8315 struct qemuProcessReconnectData *src = opaque;
8316 struct qemuProcessReconnectData *data;
8318 /* If the VM was inactive, we don't need to reconnect */
8319 if (!obj->pid)
8320 return 0;
8322 if (VIR_ALLOC(data) < 0)
8323 return -1;
8325 memcpy(data, src, sizeof(*data));
8326 data->obj = obj;
8327 data->identity = virIdentityGetCurrent();
8329 virNWFilterReadLockFilterUpdates();
8331 /* this lock and reference will be eventually transferred to the thread
8332 * that handles the reconnect */
8333 virObjectLock(obj);
8334 virObjectRef(obj);
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
8343 * object.
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);
8352 VIR_FREE(data);
8353 return -1;
8356 return 0;
8360 * qemuProcessReconnectAll
8362 * Try to re-open the resources for live VMs that we care
8363 * about.
8365 void
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,
8385 static void
8386 qemuProcessQMPStop(qemuProcessQMPPtr proc)
8388 if (proc->mon) {
8389 virObjectUnlock(proc->mon);
8390 qemuMonitorClose(proc->mon);
8391 proc->mon = NULL;
8394 if (proc->cmd) {
8395 virCommandAbort(proc->cmd);
8396 virCommandFree(proc->cmd);
8397 proc->cmd = NULL;
8400 if (proc->monpath)
8401 unlink(proc->monpath);
8403 virDomainObjEndAPI(&proc->vm);
8405 if (proc->pid != 0) {
8406 char ebuf[1024];
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)));
8414 proc->pid = 0;
8417 if (proc->pidfile)
8418 unlink(proc->pidfile);
8420 if (proc->uniqDir)
8421 rmdir(proc->uniqDir);
8426 * qemuProcessQMPFree:
8427 * @proc: Stores process and connection state
8429 * Kill QEMU process and free process data structure.
8431 void
8432 qemuProcessQMPFree(qemuProcessQMPPtr proc)
8434 if (!proc)
8435 return;
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);
8445 VIR_FREE(proc);
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.
8460 qemuProcessQMPPtr
8461 qemuProcessQMPNew(const char *binary,
8462 const char *libDir,
8463 uid_t runUid,
8464 gid_t runGid,
8465 bool forceTCG)
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)
8474 goto cleanup;
8476 if (VIR_STRDUP(proc->binary, binary) < 0 ||
8477 VIR_STRDUP(proc->libDir, libDir) < 0)
8478 goto cleanup;
8480 proc->runUid = runUid;
8481 proc->runGid = runGid;
8482 proc->forceTCG = forceTCG;
8484 VIR_STEAL_PTR(ret, proc);
8486 cleanup:
8487 qemuProcessQMPFree(proc);
8488 return ret;
8492 static int
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"),
8499 proc->uniqDir);
8500 return -1;
8503 return 0;
8507 static int
8508 qemuProcessQMPInit(qemuProcessQMPPtr proc)
8510 char *template = NULL;
8511 int ret = -1;
8513 VIR_DEBUG("proc=%p, emulator=%s", proc, proc->binary);
8515 if (virAsprintf(&template, "%s/qmp-XXXXXX", proc->libDir) < 0)
8516 goto cleanup;
8518 if (!(proc->uniqDir = mkdtemp(template))) {
8519 virReportSystemError(errno,
8520 _("Failed to create unique directory with "
8521 "template '%s' for probing QEMU"),
8522 template);
8523 goto cleanup;
8526 if (qemuProcessQEMULabelUniqPath(proc) < 0)
8527 goto cleanup;
8529 if (virAsprintf(&proc->monpath, "%s/%s", proc->uniqDir,
8530 "qmp.monitor") < 0)
8531 goto cleanup;
8533 if (virAsprintf(&proc->monarg, "unix:%s,server,nowait", proc->monpath) < 0)
8534 goto cleanup;
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)
8542 goto cleanup;
8544 ret = 0;
8546 cleanup:
8547 return ret;
8551 static int
8552 qemuProcessQMPLaunch(qemuProcessQMPPtr proc)
8554 const char *machine;
8555 int status = 0;
8556 int ret = -1;
8557 int rc;
8559 if (proc->forceTCG)
8560 machine = "none,accel=tcg";
8561 else
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,
8575 "-S",
8576 "-no-user-config",
8577 "-nodefaults",
8578 "-nographic",
8579 "-machine", machine,
8580 "-qmp", proc->monarg,
8581 "-pidfile", proc->pidfile,
8582 "-daemonize",
8583 NULL);
8584 virCommandAddEnvPassCommon(proc->cmd);
8585 virCommandClearCaps(proc->cmd);
8587 #if WITH_CAPNG
8588 /* QEMU might run into permission issues, e.g. /dev/sev (0600), override
8589 * them just for the purpose of probing */
8590 if (geteuid() == 0)
8591 virCommandAllowCap(proc->cmd, CAP_DAC_OVERRIDE);
8592 #endif
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)
8600 goto cleanup;
8602 if (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"),
8606 proc->binary,
8607 proc->stderr ? proc->stderr : _("unknown error"));
8608 goto cleanup;
8611 if ((rc = virPidFileReadPath(proc->pidfile, &proc->pid)) < 0) {
8612 virReportSystemError(-rc, _("Failed to read pidfile %s"), proc->pidfile);
8613 goto cleanup;
8616 ret = 0;
8618 cleanup:
8619 return ret;
8624 qemuProcessQMPInitMonitor(qemuMonitorPtr mon)
8626 if (qemuMonitorSetCapabilities(mon) < 0) {
8627 VIR_DEBUG("Failed to set monitor capabilities %s",
8628 virGetLastErrorMessage());
8629 return -1;
8632 return 0;
8636 static int
8637 qemuProcessQMPConnectMonitor(qemuProcessQMPPtr proc)
8639 virDomainXMLOptionPtr xmlopt = NULL;
8640 virDomainChrSourceDef monConfig;
8641 int ret = -1;
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)))
8652 goto cleanup;
8654 proc->vm->pid = proc->pid;
8656 if (!(proc->mon = qemuMonitorOpen(proc->vm, &monConfig, true,
8657 0, &callbacks, NULL)))
8658 goto cleanup;
8660 virObjectLock(proc->mon);
8662 if (qemuProcessQMPInitMonitor(proc->mon) < 0)
8663 goto cleanup;
8665 ret = 0;
8667 cleanup:
8668 virObjectUnref(xmlopt);
8669 return ret;
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.
8679 * Usage:
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)
8691 int ret = -1;
8693 VIR_DEBUG("proc=%p, emulator=%s", proc, proc->binary);
8695 if (qemuProcessQMPInit(proc) < 0)
8696 goto cleanup;
8698 if (qemuProcessQMPLaunch(proc) < 0)
8699 goto cleanup;
8701 if (qemuProcessQMPConnectMonitor(proc) < 0)
8702 goto cleanup;
8704 ret = 0;
8706 cleanup:
8707 return ret;