qemu-ga patch queue
PATCH 1/7 qemu-ga: add guest-suspend-disk
In reply to Michael RothFrom: Luiz Capitulino <[email protected]>
As the command name implies, this command suspends the guest to disk.
The suspend operation is implemented by two functions: bios_supports_mode() and guest_suspend(). Both functions are generic enough to be used by other suspend modes (introduced by next commits).
Both functions will try to use the scripts provided by the pm-utils package if it's available. If it's not available, a manual method, which consists of directly writing to '/sys/power/state', will be used.
To reap terminated children, a new signal handler is installed in the parent to catch SIGCHLD signals and a non-blocking call to waitpid() is done to collect their exit statuses. The statuses, however, are discarded.
The approach used to query the guest for suspend support deserves some explanation. It's implemented by bios_supports_mode() and shown below:
qemu-ga
|
create pipe
| fork()
-----------------
| |
| |
| fork()
| --------------------------
| | |
| | |
| | exec('pm-is-supported')
| |
| wait()
| write exit status to pipe
| exit
|
read pipe
This might look complex, but the resulting code is quite simple. The purpose of that approach is to allow qemu-ga to reap its children (semi-)automatically from its SIGCHLD handler.
Implementing this the obvious way, that's, doing the exec() call from the first child process, would force us to introduce a more complex way to reap qemu-ga's children. Like registering PIDs to be reaped and having a way to wait for them when returning their exit status to qemu-ga is necessary. The approach explained above avoids that complexity.
--- qapi-schema-guest.json | 24 ++++++ qemu-ga.c | 19 +++++- qga/commands-posix.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ qga/commands-win32.c | 5 ++ 4 files changed, 235 insertions(+), 1 deletions(-)
qapi-schema-guest.json +24 -0
@@ -295,3 +295,27 @@ ## { 'command': 'guest-fsfreeze-thaw', 'returns': 'int' } + +## +# @guest-suspend-disk +# +# Suspend guest to disk. +# +# This command tries to execute the scripts provided by the pm-utils package. +# If it's not available, the suspend operation will be performed by manually +# writing to a sysfs file. +# +# For the best results it's strongly recommended to have the pm-utils +# package installed in the guest. +# +# Returns: nothing on success +# If suspend to disk is not supported, Unsupported +# +# Notes: o This is an asynchronous request. There's no guarantee a response +# will be sent +# o It's strongly recommended to issue the guest-sync command before +# sending commands when the guest resumes +# +# Since: 1.1 +## +{ 'command': 'guest-suspend-disk' }
qemu-ga.c +18 -1
@@ -17,6 +17,7 @@#include <getopt.h> #ifndef _WIN32#include <syslog.h> +#include <sys/wait.h> #endif #include "json-streamer.h" #include "json-parser.h"
@@ -73,9 +74,16 @@ static void quit_handler(int sig) }#ifndef _WIN32 +/* reap _all_ terminated children */ +static void child_handler (int sig) +{ +int status; +while (waitpid (-1 , &status,WNOHANG ) >0 )/* NOTHING */ ; +} +static gboolean register_signal_handlers (void ) { -struct sigaction sigact; +struct sigaction sigact, sigact_chld;int ret;memset (&sigact,0 ,sizeof (struct sigaction ));
@@ -91,6 +99,15 @@ static gboolean register_signal_handlers(void)g_error ("error configuring signal handler: %s" ,strerror (errno)); return false; } + +memset (&sigact_chld,0 ,sizeof (struct sigaction )); + sigact_chld.sa_handler = child_handler; + sigact_chld.sa_flags =SA_NOCLDSTOP ; + ret =sigaction (SIGCHLD , &sigact_chld,NULL ); +if (ret ==-1 ) { +g_error ("error configuring signal handler: %s" ,strerror (errno)); + } +return true; }#endif
qga/commands-posix.c +188 -0
206 lines not on this page · raw message
qga/commands-win32.c +5 -0
@@ -124,6 +124,11 @@ int64_t qmp_guest_fsfreeze_thaw(Error **err)return 0 ; } +void qmp_guest_suspend_disk (Error **err) +{ +error_set (err,QERR_UNSUPPORTED ); +} +/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) {
Signature · 1 line
1.7.4.1
PATCH 5/7 qemu-ga: add win32 guest-suspend-ram command
In reply to Michael RothS3 sleep implementation for windows.
--- qga/commands-win32.c | 34 +++++++++++++++++++++++++--------- 1 files changed, 25 insertions(+), 9 deletions(-)
qga/commands-win32.c +25 -9
@@ -174,7 +174,8 @@ int64_t qmp_guest_fsfreeze_thaw(Error **err) }typedef enum { -GUEST_SUSPEND_MODE_DISK +GUEST_SUSPEND_MODE_DISK , +GUEST_SUSPEND_MODE_RAM }GuestSuspendMode ;static void check_suspend_mode (GuestSuspendMode mode,Error **err)
@@ -192,18 +193,24 @@ static void check_suspend_mode(GuestSuspendMode mode, Error **err) goto out; } -if (mode ==GUEST_SUSPEND_MODE_DISK ) { -if (sys_pwr_caps.SystemS4) { -return ; +switch (mode) { +case GUEST_SUSPEND_MODE_DISK : +if (!sys_pwr_caps.SystemS4) { +error_set (&local_err,QERR_QGA_COMMAND_FAILED , +"suspend-to-disk not supported by OS" ); } - }else { +break ; +case GUEST_SUSPEND_MODE_RAM : +if (!sys_pwr_caps.SystemS3) { +error_set (&local_err,QERR_QGA_COMMAND_FAILED , +"suspend-to-ram not supported by OS" ); + } +break ; +default :error_set (&local_err,QERR_INVALID_PARAMETER_VALUE ,"mode" ,"GuestSuspendMode" ); - goto out; } -error_set (&local_err,QERR_QGA_COMMAND_FAILED , -"suspend mode not supported by OS" ); out:if (local_err) {error_propagate (err, local_err);
@@ -239,7 +246,16 @@ void qmp_guest_suspend_disk(Error **err)void qmp_guest_suspend_ram (Error **err) { -error_set (err,QERR_UNSUPPORTED ); +GuestSuspendMode *mode =g_malloc (sizeof (GuestSuspendMode)); + + *mode =GUEST_SUSPEND_MODE_RAM ; +check_suspend_mode (*mode, err); +acquire_privilege (SE_SHUTDOWN_NAME , err); +execute_async (do_suspend, mode, err); + +if (error_is_set (err)) { +g_free (mode); + } }void qmp_guest_suspend_hybrid (Error **err)
Signature · 1 line
1.7.4.1
The following changes since commit a348f108842fb928563865c9918642900cd0d477:
Add missing const attributes for MemoryRegionOps (2012-03-11 11:40:15 +0000)
are available in the git repository at:
git://github.com/mdroth/qemu.git qga-pull-3-12-2012
Gal Hammer (1):
qemu-ga: add win32 guest-suspend-disk command.
Luiz Capitulino (3):
qemu-ga: add guest-suspend-disk
qemu-ga: add guest-suspend-ram
qemu-ga: add guest-suspend-hybrid
Michael Roth (2):
qemu-ga: add win32 guest-suspend-ram command
qemu-ga: add guest-sync-delimited
Michal Privoznik (1):
qemu-ga: add guest-network-get-interfaces command
configure | 2 +- qapi-schema-guest.json | 182 ++++++++++++++++++++++- qemu-ga.c | 46 +++++- qga/commands-posix.c | 390 +++++++++++++++++++++++++++++++++++++++++++++++- qga/commands-win32.c | 167 +++++++++++++++++++-- qga/commands.c | 6 + qga/guest-agent-core.h | 2 + 7 files changed, 774 insertions(+), 21 deletions(-)
PATCH 2/7 qemu-ga: add guest-suspend-ram
From: Luiz Capitulino <[email protected]>
--- qapi-schema-guest.json | 28 ++++++++++++++++++++++++++++ qga/commands-posix.c | 10 ++++++++++ qga/commands-win32.c | 5 +++++ 3 files changed, 43 insertions(+), 0 deletions(-)
qapi-schema-guest.json +28 -0
@@ -319,3 +319,31 @@ # Since: 1.1 ## { 'command': 'guest-suspend-disk' } + +## +# @guest-suspend-ram +# +# Suspend guest to ram. +# +# This command tries to execute the scripts provided by the pm-utils package. +# If it's not available, the suspend operation will be performed by manually +# writing to a sysfs file. +# +# For the best results it's strongly recommended to have the pm-utils +# package installed in the guest. +# +# IMPORTANT: guest-suspend-ram requires QEMU to support the 'system_wakeup' +# command. Thus, it's *required* to query QEMU for the presence of the +# 'system_wakeup' command before issuing guest-suspend-ram. +# +# Returns: nothing on success +# If suspend to ram is not supported, Unsupported +# +# Notes: o This is an asynchronous request. There's no guarantee a response +# will be sent +# o It's strongly recommended to issue the guest-sync command before +# sending commands when the guest resumes +# +# Since: 1.1 +## +{ 'command': 'guest-suspend-ram' }
qga/commands-posix.c +10 -0
@@ -705,6 +705,16 @@ void qmp_guest_suspend_disk(Error **err)guest_suspend ("pm-hibernate" ,"disk" , err); } +void qmp_guest_suspend_ram (Error **err) +{ +bios_supports_mode ("pm-is-supported" ,"--suspend" ,"mem" , err); +if (error_is_set (err)) { +return ; + } + +guest_suspend ("pm-suspend" ,"mem" , err); +} +/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) {
qga/commands-win32.c +5 -0
@@ -129,6 +129,11 @@ void qmp_guest_suspend_disk(Error **err)error_set (err,QERR_UNSUPPORTED ); } +void qmp_guest_suspend_ram (Error **err) +{ +error_set (err,QERR_UNSUPPORTED ); +} +/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) {
Signature · 1 line
1.7.4.1
PATCH 3/7 qemu-ga: add guest-suspend-hybrid
In reply to Michael RothFrom: Luiz Capitulino <[email protected]>
--- qapi-schema-guest.json | 23 +++++++++++++++++++++++ qga/commands-posix.c | 10 ++++++++++ qga/commands-win32.c | 5 +++++ 3 files changed, 38 insertions(+), 0 deletions(-)
qapi-schema-guest.json +23 -0
@@ -347,3 +347,26 @@ # Since: 1.1 ## { 'command': 'guest-suspend-ram' } + +## +# @guest-suspend-hybrid +# +# Save guest state to disk and suspend to ram. +# +# This command requires the pm-utils package to be installed in the guest. +# +# IMPORTANT: guest-suspend-hybrid requires QEMU to support the 'system_wakeup' +# command. Thus, it's *required* to query QEMU for the presence of the +# 'system_wakeup' command before issuing guest-suspend-hybrid. +# +# Returns: nothing on success +# If hybrid suspend is not supported, Unsupported +# +# Notes: o This is an asynchronous request. There's no guarantee a response +# will be sent +# o It's strongly recommended to issue the guest-sync command before +# sending commands when the guest resumes +# +# Since: 1.1 +## +{ 'command': 'guest-suspend-hybrid' }
qga/commands-posix.c +10 -0
@@ -715,6 +715,16 @@ void qmp_guest_suspend_ram(Error **err)guest_suspend ("pm-suspend" ,"mem" , err); } +void qmp_guest_suspend_hybrid (Error **err) +{ +bios_supports_mode ("pm-is-supported" ,"--suspend-hybrid" ,NULL , err); +if (error_is_set (err)) { +return ; + } + +guest_suspend ("pm-suspend-hybrid" ,NULL , err); +} +/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) {
qga/commands-win32.c +5 -0
@@ -134,6 +134,11 @@ void qmp_guest_suspend_ram(Error **err)error_set (err,QERR_UNSUPPORTED ); } +void qmp_guest_suspend_hybrid (Error **err) +{ +error_set (err,QERR_UNSUPPORTED ); +} +/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) {
Signature · 1 line
1.7.4.1
PATCH 6/7 qemu-ga: add guest-network-get-interfaces command
In reply to Michael RothFrom: Michal Privoznik <[email protected]>
This command returns an array of:
[ifname, hwaddr, [ipaddr, ipaddr_family, prefix] ]
for each interface in the system.
Currently, only IPv4 and IPv6 are supported.
--- qapi-schema-guest.json | 59 ++++++++++++++++ qga/commands-posix.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ qga/commands-win32.c | 6 ++ 3 files changed, 246 insertions(+), 0 deletions(-)
qapi-schema-guest.json +59 -0
@@ -370,3 +370,62 @@ # Since: 1.1 ## { 'command': 'guest-suspend-hybrid' } + +## +# @GuestIpAddressType: +# +# An enumeration of supported IP address types +# +# @ipv4: IP version 4 +# +# @ipv6: IP version 6 +# +# Since: 1.1 +## +{ 'enum': 'GuestIpAddressType', + 'data': [ 'ipv4', 'ipv6' ] } + +## +# @GuestIpAddress: +# +# @ip-address: IP address +# +# @ip-address-type: Type of @ip-address (e.g. ipv4, ipv6) +# +# @prefix: Network prefix length of @ip-address +# +# Since: 1.1 +## +{ 'type': 'GuestIpAddress', + 'data': {'ip-address': 'str', + 'ip-address-type': 'GuestIpAddressType', + 'prefix': 'int'} } + +## +# @GuestNetworkInterface: +# +# @name: The name of interface for which info are being delivered +# +# @hardware-address: Hardware address of @name +# +# @ip-addresses: List of addresses assigned to @name +# +# Since: 1.1 +## +{ 'type': 'GuestNetworkInterface', + 'data': {'name': 'str', + '*hardware-address': 'str', + '*ip-addresses': ['GuestIpAddress'] } } + +## +# @guest-network-get-interfaces: +# +# Get list of guest IP addresses, MAC addresses +# and netmasks. +# +# Returns: List of GuestNetworkInfo on success. +# +# Since: 1.1 +## +{ 'command': 'guest-network-get-interfaces', + 'returns': ['GuestNetworkInterface'] }
qga/commands-posix.c +181 -0
204 lines not on this page · raw message
qga/commands-win32.c +6 -0
@@ -263,6 +263,12 @@ void qmp_guest_suspend_hybrid(Error **err)error_set (err,QERR_UNSUPPORTED ); } +GuestNetworkInterfaceList *qmp_guest_network_get_interfaces (Error **err) +{ +error_set (err,QERR_UNSUPPORTED ); +return NULL ; +} +/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) {
Signature · 1 line
1.7.4.1
PATCH 4/7 qemu-ga: add win32 guest-suspend-disk command.
In reply to Michael RothFrom: Gal Hammer <[email protected]>
Implement guest-suspend-disk RPC for Windows. Functionally this should be equivalent to the posix implementation.
--- configure | 2 +- qga/commands-win32.c | 132 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 121 insertions(+), 13 deletions(-)
configure +1 -1
@@ -525,7 +525,7 @@ EOF bindir="\${prefix}" sysconfdir="\${prefix}" confsuffix="" - libs_qga="-lws2_32 -lwinmm $lib_qga" + libs_qga="-lws2_32 -lwinmm -lpowrprof $lib_qga" fi werror=""
qga/commands-win32.c +120 -12
167 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH 7/7 qemu-ga: add guest-sync-delimited
In reply to Michael Rothguest-sync leaves it as an exercise to the user as to how to reliably obtain the response to guest-sync if the client had previously read in a partial response (due qemu-ga previously being restarted mid-"sentence" due to reboot, forced restart, etc).
qemu-ga handles this situation on its end by having a client precede their guest-sync request with a 0xFF byte (invalid UTF-8), which qemu-ga/QEMU JSON parsers will treat as a flush event. Thus we can reliably flush the qemu-ga parser state in preparation for receiving the guest-sync request.
guest-sync-delimited provides the same functionality for a client: when a guest-sync-delimited is issued, qemu-ga will precede it's response with a 0xFF byte that the client can use as an indicator to flush its buffer/parser state in preparation for reliably receiving the guest-sync-delimited response.
It is also useful as an optimization for clients, since, after issuing a guest-sync-delimited, clients can safely discard all stale data read from the channel until the 0xFF is found.
More information available on the wiki:
http://wiki.qemu.org/Features/QAPI/GuestAgent#QEMU_Guest_Agent_Protocol
--- qapi-schema-guest.json | 48 +++++++++++++++++++++++++++++++++++++++++++++++- qemu-ga.c | 27 ++++++++++++++++++++++----- qga/commands-posix.c | 3 --- qga/commands.c | 6 ++++++ qga/guest-agent-core.h | 2 ++ 5 files changed, 77 insertions(+), 9 deletions(-)
qapi-schema-guest.json +47 -1
@@ -1,6 +1,41 @@ # *-*- Mode: Python -*-* ## +# +# Echo back a unique integer value, and prepend to response a +# leading sentinel byte (0xFF) the client can check scan for. +# +# This is used by clients talking to the guest agent over the +# wire to ensure the stream is in sync and doesn't contain stale +# data from previous client. It must be issued upon initial +# connection, and after any client-side timeouts (including +# timeouts on receiving a response to this command). +# +# After issuing this request, all guest agent responses should be +# ignored until the response containing the unique integer value +# the client passed in is returned. Receival of the 0xFF sentinel +# byte must be handled as an indication that the client's +# lexer/tokenizer/parser state should be flushed/reset in +# preparation for reliably receiving the subsequent response. As +# an optimization, clients may opt to ignore all data until a +# sentinel value is receiving to avoid unecessary processing of +# stale data. +# +# Similarly, clients should also precede this *request* +# with a 0xFF byte to make sure the guest agent flushes any +# partially read JSON data from a previous client connection. +# +# @id: randomly generated 64-bit integer +# +# Returns: The unique integer id passed in by the client +# +# Since: 1.1 +# ## +{ 'command': 'guest-sync-delimited' + 'data': { 'id': 'int' }, + 'returns': 'int' } + +## # @guest-sync: # # Echo back a unique integer value
@@ -13,8 +48,19 @@ # partially-delivered JSON text in such a way that this response # can be obtained. # +# In cases where a partial stale response was previously +# received by the client, this cannot always be done reliably. +# One particular scenario being if qemu-ga responses are fed +# character-by-character into a JSON parser. In these situations, +# using guest-sync-delimited may be optimal. +# +# For clients that fetch responses line by line and convert them +# to JSON objects, guest-sync should be sufficient, but note that +# in cases where the channel is dirty some attempts at parsing the +# response may result in a parser error. +# # Such clients should also precede this command -# with a 0xFF byte to make such the guest agent flushes any +# with a 0xFF byte to make sure the guest agent flushes any # partially read JSON data from a previous session. # # @id: randomly generated 64-bit integer
qemu-ga.c +22 -5
@@ -41,6 +41,7 @@#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"#endif #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid" +#define QGA_SENTINEL_BYTE 0xFFstruct GAState {JSONMessageParser parser;
@@ -54,9 +55,10 @@ struct GAState {#ifdef _WIN32GAService service;#endif +bool delimit_response; }; -static struct GAState *ga_state; +struct GAState *ga_state;#ifdef _WIN32 DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
@@ -198,6 +200,11 @@ static void ga_log(const gchar *domain, GLogLevelFlags level, } } +void ga_set_response_delimited (GAState *s) +{ + s->delimit_response = true; +} +#ifndef _WIN32static void become_daemon (const char *pidfile) {
@@ -254,7 +261,7 @@ fail:static int send_response (GAState *s,QObject *payload) {const char *buf; -QString *payload_qstr; +QString *payload_qstr, *response_qstr;GIOStatus status;g_assert (payload && s->channel);
@@ -264,10 +271,20 @@ static int send_response(GAState *s, QObject *payload)return -EINVAL ; } -qstring_append_chr (payload_qstr,'\n' ); - buf =qstring_get_str (payload_qstr); +if (s->delimit_response) { + s->delimit_response = false; + response_qstr =qstring_new (); +qstring_append_chr (response_qstr,QGA_SENTINEL_BYTE ); +qstring_append (response_qstr,qstring_get_str (payload_qstr)); +QDECREF (payload_qstr); + }else { + response_qstr = payload_qstr; + } + +qstring_append_chr (response_qstr,'\n' ); + buf =qstring_get_str (response_qstr); status =ga_channel_write_all (s->channel, buf,strlen (buf)); -QDECREF (payload_qstr); +QDECREF (response_qstr);if (status !=G_IO_STATUS_NORMAL ) {return -EIO ; }
qga/commands-posix.c +0 -3
@@ -35,8 +35,6 @@#include "qemu-queue.h" #include "host-utils.h" -static GAState *ga_state; -static void reopen_fd_to_null (int fd) {int nullfd;
@@ -909,7 +907,6 @@ error:/* register init/cleanup routines for stateful command groups */ void ga_command_state_init (GAState *s,GACommandState *cs) { - ga_state = s;#if defined(CONFIG_FSFREEZE )ga_command_state_add (cs, guest_fsfreeze_init, guest_fsfreeze_cleanup);#endif
qga/commands.c +6 -0
@@ -29,6 +29,12 @@ void slog(const gchar *fmt, ...)va_end (ap); } +int64_t qmp_guest_sync_delimited (int64_t id,Error **errp) +{ +ga_set_response_delimited (ga_state); +return id; +} +int64_t qmp_guest_sync (int64_t id,Error **errp) {return id;
qga/guest-agent-core.h +2 -0
@@ -18,6 +18,7 @@typedef struct GAState GAState ;typedef struct GACommandState GACommandState ; +extern GAState *ga_state;void ga_command_state_init (GAState *s,GACommandState *cs); void ga_command_state_add(GACommandState *cs,
@@ -30,3 +31,4 @@ bool ga_logging_enabled(GAState *s);void ga_disable_logging (GAState *s);void ga_enable_logging (GAState *s);void slog (const gchar *fmt, ...); +void ga_set_response_delimited (GAState *s);
Signature · 1 line
1.7.4.1
Quoted 6 lines · Michael Roth
The following changes since commit
a348f108842fb928563865c9918642900cd0d477:Add missing const attributes for MemoryRegionOps (2012-03-11 11:40:15 +0000)
are available in the git repository at:
git://github.com/mdroth/qemu.git qga-pull-3-12-2012
Pulled. Thanks.
Regards,
Anthony Liguori
Quoted 27 lines
Gal Hammer (1):
qemu-ga: add win32 guest-suspend-disk command.Luiz Capitulino (3):
qemu-ga: add guest-suspend-disk
qemu-ga: add guest-suspend-ram
qemu-ga: add guest-suspend-hybridMichael Roth (2):
qemu-ga: add win32 guest-suspend-ram command
qemu-ga: add guest-sync-delimitedMichal Privoznik (1):
qemu-ga: add guest-network-get-interfaces commandconfigure | 2 +- qapi-schema-guest.json | 182 ++++++++++++++++++++++- qemu-ga.c | 46 +++++- qga/commands-posix.c | 390 +++++++++++++++++++++++++++++++++++++++++++++++- qga/commands-win32.c | 167 +++++++++++++++++++-- qga/commands.c | 6 + qga/guest-agent-core.h | 2 + 7 files changed, 774 insertions(+), 21 deletions(-)