2 * QEMU Management Protocol commands
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "qemu-version.h"
19 #include "qemu/cutils.h"
20 #include "qemu/option.h"
21 #include "monitor/monitor.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/uuid.h"
25 #include "chardev/char.h"
26 #include "ui/qemu-spice.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/runstate.h"
30 #include "sysemu/arch_init.h"
31 #include "sysemu/blockdev.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-block-core.h"
35 #include "qapi/qapi-commands-machine.h"
36 #include "qapi/qapi-commands-misc.h"
37 #include "qapi/qapi-commands-ui.h"
38 #include "qapi/qmp/qerror.h"
39 #include "hw/mem/memory-device.h"
40 #include "hw/acpi/acpi_dev_interface.h"
42 NameInfo
*qmp_query_name(Error
**errp
)
44 NameInfo
*info
= g_malloc0(sizeof(*info
));
47 info
->has_name
= true;
48 info
->name
= g_strdup(qemu_name
);
54 VersionInfo
*qmp_query_version(Error
**errp
)
56 VersionInfo
*info
= g_new0(VersionInfo
, 1);
58 info
->qemu
= g_new0(VersionTriple
, 1);
59 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
60 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
61 info
->qemu
->micro
= QEMU_VERSION_MICRO
;
62 info
->package
= g_strdup(QEMU_PKGVERSION
);
67 KvmInfo
*qmp_query_kvm(Error
**errp
)
69 KvmInfo
*info
= g_malloc0(sizeof(*info
));
71 info
->enabled
= kvm_enabled();
72 info
->present
= kvm_available();
77 UuidInfo
*qmp_query_uuid(Error
**errp
)
79 UuidInfo
*info
= g_malloc0(sizeof(*info
));
81 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
85 void qmp_quit(Error
**errp
)
88 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT
);
91 void qmp_stop(Error
**errp
)
93 /* if there is a dump in background, we should wait until the dump
95 if (dump_in_progress()) {
96 error_setg(errp
, "There is a dump in process, please wait.");
100 if (runstate_check(RUN_STATE_INMIGRATE
)) {
103 vm_stop(RUN_STATE_PAUSED
);
107 void qmp_system_reset(Error
**errp
)
109 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
112 void qmp_system_powerdown(Error
**erp
)
114 qemu_system_powerdown_request();
117 void qmp_x_exit_preconfig(Error
**errp
)
119 if (!runstate_check(RUN_STATE_PRECONFIG
)) {
120 error_setg(errp
, "The command is permitted only in '%s' state",
121 RunState_str(RUN_STATE_PRECONFIG
));
124 qemu_exit_preconfig_request();
127 void qmp_cont(Error
**errp
)
131 Error
*local_err
= NULL
;
133 /* if there is a dump in background, we should wait until the dump
135 if (dump_in_progress()) {
136 error_setg(errp
, "There is a dump in process, please wait.");
140 if (runstate_needs_reset()) {
141 error_setg(errp
, "Resetting the Virtual Machine is required");
143 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
145 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
146 error_setg(errp
, "Migration is not finalized yet");
150 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
151 blk_iostatus_reset(blk
);
154 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
155 block_job_iostatus_reset(job
);
158 /* Continuing after completed migration. Images have been inactivated to
159 * allow the destination to take control. Need to get control back now.
161 * If there are no inactive block nodes (e.g. because the VM was just
162 * paused rather than completing a migration), bdrv_inactivate_all() simply
163 * doesn't do anything. */
164 bdrv_invalidate_cache_all(&local_err
);
166 error_propagate(errp
, local_err
);
170 if (runstate_check(RUN_STATE_INMIGRATE
)) {
177 void qmp_system_wakeup(Error
**errp
)
179 if (!qemu_wakeup_suspend_enabled()) {
181 "wake-up from suspend is not supported by this guest");
185 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
188 void qmp_set_password(const char *protocol
, const char *password
,
189 bool has_connected
, const char *connected
, Error
**errp
)
191 int disconnect_if_connected
= 0;
192 int fail_if_connected
= 0;
196 if (strcmp(connected
, "fail") == 0) {
197 fail_if_connected
= 1;
198 } else if (strcmp(connected
, "disconnect") == 0) {
199 disconnect_if_connected
= 1;
200 } else if (strcmp(connected
, "keep") == 0) {
203 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
208 if (strcmp(protocol
, "spice") == 0) {
209 if (!qemu_using_spice(errp
)) {
212 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
213 disconnect_if_connected
);
215 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
220 if (strcmp(protocol
, "vnc") == 0) {
221 if (fail_if_connected
|| disconnect_if_connected
) {
222 /* vnc supports "connected=keep" only */
223 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
226 /* Note that setting an empty password will not disable login through
228 rc
= vnc_display_password(NULL
, password
);
230 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
235 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
238 void qmp_expire_password(const char *protocol
, const char *whenstr
,
244 if (strcmp(whenstr
, "now") == 0) {
246 } else if (strcmp(whenstr
, "never") == 0) {
248 } else if (whenstr
[0] == '+') {
249 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
251 when
= strtoull(whenstr
, NULL
, 10);
254 if (strcmp(protocol
, "spice") == 0) {
255 if (!qemu_using_spice(errp
)) {
258 rc
= qemu_spice_set_pw_expire(when
);
260 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
265 if (strcmp(protocol
, "vnc") == 0) {
266 rc
= vnc_display_pw_expire(NULL
, when
);
268 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
273 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
277 void qmp_change_vnc_password(const char *password
, Error
**errp
)
279 if (vnc_display_password(NULL
, password
) < 0) {
280 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
284 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
286 QemuOptsList
*olist
= qemu_find_opts("vnc");
289 if (strstr(target
, "id=")) {
290 error_setg(errp
, "id not supported");
294 opts
= qemu_opts_find(olist
, "default");
298 opts
= vnc_parse(target
, errp
);
303 vnc_display_open("default", errp
);
306 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
309 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
311 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
313 qmp_change_vnc_password(arg
, errp
);
316 qmp_change_vnc_listen(target
, errp
);
319 #endif /* !CONFIG_VNC */
321 void qmp_change(const char *device
, const char *target
,
322 bool has_arg
, const char *arg
, Error
**errp
)
324 if (strcmp(device
, "vnc") == 0) {
326 qmp_change_vnc(target
, has_arg
, arg
, errp
);
328 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
331 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
332 has_arg
, arg
, false, 0, errp
);
336 void qmp_add_client(const char *protocol
, const char *fdname
,
337 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
343 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
348 if (strcmp(protocol
, "spice") == 0) {
349 if (!qemu_using_spice(errp
)) {
353 skipauth
= has_skipauth
? skipauth
: false;
354 tls
= has_tls
? tls
: false;
355 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
356 error_setg(errp
, "spice failed to add client");
361 } else if (strcmp(protocol
, "vnc") == 0) {
362 skipauth
= has_skipauth
? skipauth
: false;
363 vnc_display_add_client(NULL
, fd
, skipauth
);
366 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
367 if (qemu_chr_add_client(s
, fd
) < 0) {
368 error_setg(errp
, "failed to add client");
375 error_setg(errp
, "protocol '%s' is invalid", protocol
);
380 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
382 return qmp_memory_device_list();
385 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
388 ACPIOSTInfoList
*head
= NULL
;
389 ACPIOSTInfoList
**prev
= &head
;
390 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
393 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
394 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
396 adevc
->ospm_status(adev
, &prev
);
398 error_setg(errp
, "command is not supported, missing ACPI device");
404 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
406 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
408 mem_info
->base_memory
= ram_size
;
410 mem_info
->plugged_memory
= get_plugged_memory_size();
411 mem_info
->has_plugged_memory
=
412 mem_info
->plugged_memory
!= (uint64_t)-1;