Skip to contentSkip to conversation

qemu-ga: add support for Windows

PATCH Superseded Michael Roth 4 participants 21 messages Feb 2, 2012 – Feb 8, 2012 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover qemu-ga: add support for Windows

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v2

Luiz/Gal, I decided not to roll the suspend/hibernate stuff into this series since the s3 situation isn't fully sorted out yet. The file structure is a little different now, posix/linux-specific stuff goes in qga/commands-posix.c, win32-specific stuff in qga/commands-win32.c, but other than that it should be a straightforward rebase if this gets merged first.

CHANGES SINCE V1:

- Dropped guest-set-support-level patch dependency
- Rebased on master and re-tested
- Spelling/grammar fixes in commits/comments

OVERVIEW:

These patches add support for Windows to the QEMU guest agent. With these patches the following guest agent commands are supported on Windows:

guest-ping
guest-info
guest-sync
guest-shutdown

The guest-file* commands can essentially be enabled for Windows as-is, but since mingw does not honor the O_NONBLOCK flag, they'll need to be reworked if we're to retain the current non-blocking behavior.

The rest of the commands are currently stubbed out for Windows (qemu-ga will return an "unsupported" error), but it should be easy to implement these going forward with basic Windows support/infrastructure in place.

The build was tested using Fedora15 with a MinGW cross-build target via:

configure --enable-guest-agent --cross-prefix=i686-pc-mingw32- make qemu-ga.exe

The executable was tested using Windows XP SP3, and partially tested using Windows Server 2008 and Windows 7 (no I/O for the latter 2, having issues with virtio-win drivers). GLib 2.28+ for Windows is required. You can install qemu-ga as a load-on-boot service by running:

./qemu-ga --service install

And start/stop manually via:

net start qemu-ga
net stop qemu-ga

Many thanks to Gal Hammer for contributing the service integration and shutdown code.

 Makefile                   |    2 +-
 Makefile.objs              |    8 +-
 configure                  |    2 +-
 qapi-schema-guest.json     |  118 ++++++++--
 qemu-ga.c                  |  413 ++++++++++++++-----------------
 qga/channel-posix.c        |  246 +++++++++++++++++++
 qga/channel-win32.c        |  337 +++++++++++++++++++++++++
 qga/channel.h              |   33 +++
 qga/commands-posix.c       |  528 +++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c       |  130 ++++++++++
 qga/commands.c             |   73 ++++++
 qga/guest-agent-commands.c |  585 --------------------------------------------
 qga/guest-agent-core.h     |    3 +-
 qga/service-win32.c        |  114 +++++++++
 qga/service-win32.h        |   30 +++
 15 files changed, 1782 insertions(+), 840 deletions(-)
#2 lore raw

PATCH v2 2/8 qemu-ga: move channel/transport functionality into wrapper class

This is mostly in preparation for the win32 port, which won't use GIO channels for reasons that will be made clearer later. Here the GAChannel class is just a loose wrapper around GIOChannel calls/callbacks, but we also roll in the logic/configuration for various channel types and managing unix socket connections, which makes the abstraction much more complete and further aids in the win32 port since isa-serial/unix-listen will not be supported initially.

There's also a bit of refactoring in the main logic to consolidate the exit paths so we can do common cleanup for things like pid files, which weren't always cleaned up previously.

---
 Makefile.objs          |    1 +
 qemu-ga.c              |  306 ++++++++++++------------------------------------
 qga/channel-posix.c    |  246 ++++++++++++++++++++++++++++++++++++++
 qga/channel.h          |   33 +++++
 qga/guest-agent-core.h |    2 +-
 5 files changed, 355 insertions(+), 233 deletions(-)
 create mode 100644 qga/channel-posix.c
 create mode 100644 qga/channel.h
Makefile.objs +1 -0
index b942625..27ff919 100644
@@ -425,6 +425,7 @@ common-obj-y += qmp.o hmp.o
 # guest agent
 
 qga-nested-y = guest-agent-commands.o guest-agent-command-state.o
+qga-nested-y += channel-posix.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
qemu-ga.c +74 -232

444 lines not on this page · raw message

qga/channel-posix.c new file +246 -0

246 lines not on this page · raw message

qga/channel.h new file +33 -0
new file mode 100644
index 0000000..3704ea9
@@ -0,0 +1,33 @@
+/*
+ * QEMU Guest Agent channel declarations
+ *
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Michael Roth      <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QGA_CHANNEL_H
+#define QGA_CHANNEL_H
+
+#include <glib.h>
+
+typedef struct GAChannel GAChannel;
+
+typedef enum {
+    GA_CHANNEL_VIRTIO_SERIAL,
+    GA_CHANNEL_ISA_SERIAL,
+    GA_CHANNEL_UNIX_LISTEN,
+} GAChannelMethod;
+
+typedef gboolean (*GAChannelCallback)(GIOCondition condition, gpointer opaque);
+
+GAChannel *ga_channel_new(GAChannelMethod method, const gchar *path,
+                          GAChannelCallback cb, gpointer opaque);
+void ga_channel_free(GAChannel *c);
+GIOStatus ga_channel_read(GAChannel *c, gchar *buf, gsize size, gsize *count);
+GIOStatus ga_channel_write_all(GAChannel *c, const gchar *buf, gsize size);
+
+#endif
qga/guest-agent-core.h +1 -1
index e42b91d..6148d10 100644
@@ -14,7 +14,7 @@
 #include "qemu-common.h"
 
 #define QGA_VERSION "1.0"
-#define QGA_READ_COUNT_DEFAULT 4 << 10
+#define QGA_READ_COUNT_DEFAULT 4096
 
 typedef struct GAState GAState;
 typedef struct GACommandState GACommandState;
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v2 1/8 qemu-ga: Add schema documentation for types

In reply to Michael Roth

Document guest agent schema types in similar fashion as qmp schema types.

---
 qapi-schema-guest.json |  118 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 97 insertions(+), 21 deletions(-)
qapi-schema-guest.json +97 -21
index 5f8a18d..706925d 100644
@@ -37,17 +37,42 @@
 { 'command': 'guest-ping' }
 
 ##
-# @guest-info:
+# @GuestAgentCommandInfo:
 #
-# Get some information about the guest agent.
+# Information about guest agent commands.
 #
-# Since: 0.15.0
+# @name: name of the command
+#
+# @enabled: whether command is currently enabled by guest admin
+#
+# Since 1.1.0
 ##
 { 'type': 'GuestAgentCommandInfo',
   'data': { 'name': 'str', 'enabled': 'bool' } }
+
+##
+# @GuestAgentInfo
+#
+# Information about guest agent.
+#
+# @version: guest agent version
+#
+# @supported_commands: Information about guest agent commands
+#
+# Since 0.15.0
+##
 { 'type': 'GuestAgentInfo',
   'data': { 'version': 'str',
             'supported_commands': ['GuestAgentCommandInfo'] } }
+##
+# @guest-info:
+#
+# Get some information about the guest agent.
+#
+# Returns: @GuestAgentInfo
+#
+# Since: 0.15.0
+##
 { 'command': 'guest-info',
   'returns': 'GuestAgentInfo' }
 
@@ -98,6 +123,23 @@
   'data': { 'handle': 'int' } }
 
 ##
+# @GuestFileRead
+#
+# Result of guest agent file-read operation
+#
+# @count: number of bytes read (note: count is *before*
+#         base64-encoding is applied)
+#
+# @buf-b64: base64-encoded bytes read
+#
+# @eof: whether EOF was encountered during read operation.
+#
+# Since: 0.15.0
+##
+{ 'type': 'GuestFileRead',
+  'data': { 'count': 'int', 'buf-b64': 'str', 'eof': 'bool' } }
+
+##
 # @guest-file-read:
 #
 # Read from an open file in the guest. Data will be base64-encoded
@@ -106,19 +148,30 @@
 #
 # @count: #optional maximum number of bytes to read (default is 4KB)
 #
-# Returns: GuestFileRead on success. Note: count is number of bytes read
-#          *before* base64 encoding bytes read.
+# Returns: @GuestFileRead on success.
 #
 # Since: 0.15.0
 ##
-{ 'type': 'GuestFileRead',
-  'data': { 'count': 'int', 'buf-b64': 'str', 'eof': 'bool' } }
-
 { 'command': 'guest-file-read',
   'data':    { 'handle': 'int', '*count': 'int' },
   'returns': 'GuestFileRead' }
 
 ##
+# @GuestFileWrite
+#
+# Result of guest agent file-write operation
+#
+# @count: number of bytes written (note: count is actual bytes
+#         written, after base64-decoding of provided buffer)
+#
+# @eof: whether EOF was encountered during write operation.
+#
+# Since: 0.15.0
+##
+{ 'type': 'GuestFileWrite',
+  'data': { 'count': 'int', 'eof': 'bool' } }
+
+##
 # @guest-file-write:
 #
 # Write to an open file in the guest.
@@ -130,17 +183,29 @@
 # @count: #optional bytes to write (actual bytes, after base64-decode),
 #         default is all content in buf-b64 buffer after base64 decoding
 #
-# Returns: GuestFileWrite on success. Note: count is the number of bytes
-#          base64-decoded bytes written
+# Returns: @GuestFileWrite on success.
 #
 # Since: 0.15.0
 ##
-{ 'type': 'GuestFileWrite',
-  'data': { 'count': 'int', 'eof': 'bool' } }
 { 'command': 'guest-file-write',
   'data':    { 'handle': 'int', 'buf-b64': 'str', '*count': 'int' },
   'returns': 'GuestFileWrite' }
 
+
+##
+# @GuestFileSeek
+#
+# Result of guest agent file-seek operation
+#
+# @position: current file position
+#
+# @eof: whether EOF was encountered during file seek
+#
+# Since: 0.15.0
+##
+{ 'type': 'GuestFileSeek',
+  'data': { 'position': 'int', 'eof': 'bool' } }
+
 ##
 # @guest-file-seek:
 #
@@ -154,13 +219,10 @@
 #
 # @whence: SEEK_SET, SEEK_CUR, or SEEK_END, as with fseek()
 #
-# Returns: GuestFileSeek on success.
+# Returns: @GuestFileSeek on success.
 #
 # Since: 0.15.0
 ##
-{ 'type': 'GuestFileSeek',
-  'data': { 'position': 'int', 'eof': 'bool' } }
-
 { 'command': 'guest-file-seek',
   'data':    { 'handle': 'int', 'offset': 'int', 'whence': 'int' },
   'returns': 'GuestFileSeek' }
@@ -180,18 +242,32 @@
   'data': { 'handle': 'int' } }
 
 ##
-# @guest-fsfreeze-status:
+# @GuestFsFreezeStatus
 #
-# Get guest fsfreeze state. error state indicates failure to thaw 1 or more
-# previously frozen filesystems, or failure to open a previously cached
-# filesytem (filesystem unmounted/directory changes, etc).
+# An enumation of filesystem freeze states
 #
-# Returns: GuestFsfreezeStatus ("thawed", "frozen", etc., as defined below)
+# @thawed: filesystems thawed/unfrozen
+#
+# @frozen: all non-network guest filesystems frozen
+#
+# @error: failure to thaw 1 or more
+#         previously frozen filesystems, or failure to open a previously
+#         cached filesytem (filesystem unmounted/directory changes, etc).
 #
 # Since: 0.15.0
 ##
 { 'enum': 'GuestFsfreezeStatus',
   'data': [ 'thawed', 'frozen', 'error' ] }
+
+##
+# @guest-fsfreeze-status:
+#
+# Get guest fsfreeze state. error state indicates
+#
+# Returns: GuestFsfreezeStatus ("thawed", "frozen", etc., as defined below)
+#
+# Since: 0.15.0
+##
 { 'command': 'guest-fsfreeze-status',
   'returns': 'GuestFsfreezeStatus' }
 
Signature · 1 line
1.7.4.1
#3 lore raw

PATCH v2 3/8 qemu-ga: separate out common commands from posix-specific ones

In reply to Michael Roth

Many of the current RPC implementations are very much POSIX-specific and require complete re-writes for Windows. There are however a small set of core guest agent commands that are common to both, and other commands such as guest-file-* which *may* be portable. So we introduce commands.c for the latter, and will rename guest-agent-commands.c to commands-posix.c in a future commit. Windows implementations will go in commands-win32.c, eventually.

---
 Makefile.objs              |    2 +-
 qga/commands.c             |   73 ++++++++++++++++++++++++++++++++++++++++++++
 qga/guest-agent-commands.c |   59 +-----------------------------------
 qga/guest-agent-core.h     |    1 +
 4 files changed, 76 insertions(+), 59 deletions(-)
 create mode 100644 qga/commands.c
Makefile.objs +1 -1
index 27ff919..d70cebe 100644
@@ -424,7 +424,7 @@ common-obj-y += qmp.o hmp.o
 ######################################################################
 # guest agent
 
-qga-nested-y = guest-agent-commands.o guest-agent-command-state.o
+qga-nested-y = commands.o guest-agent-commands.o guest-agent-command-state.o
 qga-nested-y += channel-posix.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
qga/commands.c new file +73 -0

73 lines not on this page · raw message

qga/guest-agent-commands.c +1 -58

69 lines not on this page · raw message

qga/guest-agent-core.h +1 -0
index 6148d10..b5dfa5b 100644
@@ -29,3 +29,4 @@ GACommandState *ga_command_state_new(void);
 bool ga_logging_enabled(GAState *s);
 void ga_disable_logging(GAState *s);
 void ga_enable_logging(GAState *s);
+void slog(const gchar *fmt, ...);
Signature · 1 line
1.7.4.1
#6 lore raw

PATCH v2 6/8 qemu-ga: add initial win32 support

In reply to Michael Roth

This adds a win32 channel implementation that makes qemu-ga functional on Windows using virtio-serial (unix-listen/isa-serial not currently implemented). Unlike with the posix implementation, we do not use GIOChannel for the following reasons:

- glib calls stat() on an fd to check whether S_IFCHR is set, which is the case for virtio-serial on win32. Because of that, a one-time check to determine whether the channel is readable is done by making a call to PeekConsoleInput(), which reports the underlying handle is not a valid console handle, and thus we can never read from the channel.

- if one goes as far as to "trick" glib into thinking it is a normal file descripter, the buffering is done in such a way that data written to the output stream will subsequently result in that same data being read back as if it were input, causing an error loop. furthermore, a forced flush of the channel only moves the data into a secondary buffer managed by glib, so there's no way to prevent output from getting read back as input.

The implementation here ties into the glib main loop by implementing a custom GSource that continually submits asynchronous/overlapped I/O to fill an GAChannel-managed read buffer, and tells glib to poll the corresponding event handle for a completion whenever there is no data/RPC in the read buffer to notify the main application about.

---
 Makefile.objs       |    2 +-
 qemu-ga.c           |    4 +
 qga/channel-win32.c |  337 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 342 insertions(+), 1 deletions(-)
 create mode 100644 qga/channel-win32.c
Makefile.objs +1 -1
index 18e79ce..e1cb54a 100644
@@ -426,7 +426,7 @@ common-obj-y += qmp.o hmp.o
 
 qga-nested-y = commands.o guest-agent-command-state.o
 qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
-qga-nested-$(CONFIG_WIN32) += commands-win32.o
+qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o module.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
qemu-ga.c +4 -0
index 93ebc3e..8e517b5 100644
@@ -30,7 +30,11 @@
 #include "qapi/qmp-core.h"
 #include "qga/channel.h"
 
+#ifndef _WIN32
 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
+#else
+#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
+#endif
 #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
 
 struct GAState {
qga/channel-win32.c new file +337 -0

337 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#4 lore raw

PATCH v2 4/8 qemu-ga: rename guest-agent-commands.c -> commands-posix.c

In reply to Michael Roth
---
 Makefile.objs              |    2 +-
 qga/commands-posix.c       |  528 ++++++++++++++++++++++++++++++++++++++++++++
 qga/guest-agent-commands.c |  528 --------------------------------------------
 3 files changed, 529 insertions(+), 529 deletions(-)
 create mode 100644 qga/commands-posix.c
 delete mode 100644 qga/guest-agent-commands.c
Makefile.objs +1 -1
index d70cebe..2e2efb4 100644
@@ -424,7 +424,7 @@ common-obj-y += qmp.o hmp.o
 ######################################################################
 # guest agent
 
-qga-nested-y = commands.o guest-agent-commands.o guest-agent-command-state.o
+qga-nested-y = commands.o commands-posix.o guest-agent-command-state.o
 qga-nested-y += channel-posix.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
qga/commands-posix.c new file +528 -0

528 lines not on this page · raw message

qga/guest-agent-commands.c deleted +0 -528

528 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#5 lore raw

PATCH v2 5/8 qemu-ga: fixes for win32 build of qemu-ga

In reply to Michael Roth

Various stubs and #ifdefs to compile for Windows using mingw cross-build. Still has 1 linker error due to a dependency on the forthcoming win32 versions of the GAChannel/transport class.

---
 Makefile             |    2 +-
 Makefile.objs        |    9 +++--
 configure            |    2 +-
 qemu-ga.c            |   16 +++++++++
 qga/commands-win32.c |   91 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 114 insertions(+), 6 deletions(-)
 create mode 100644 qga/commands-win32.c
Makefile +1 -1
index 2560b59..9baa532 100644
@@ -199,7 +199,7 @@ QGALIB_GEN=$(addprefix $(qapi-dir)/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-c
 $(QGALIB_OBJ): $(QGALIB_GEN) $(GENERATED_HEADERS)
 $(qga-obj-y) qemu-ga.o: $(QGALIB_GEN) $(GENERATED_HEADERS)
 
-qemu-ga$(EXESUF): qemu-ga.o $(qga-obj-y) $(qapi-obj-y) $(tools-obj-y) $(qobject-obj-y) $(version-obj-y) $(QGALIB_OBJ)
+qemu-ga$(EXESUF): qemu-ga.o $(qga-obj-y) $(tools-obj-y) $(qapi-obj-y) $(qobject-obj-y) $(version-obj-y) $(QGALIB_OBJ)
 
 QEMULIBS=libhw32 libhw64 libuser libdis libdis-user
 
Makefile.objs +5 -4
index 2e2efb4..18e79ce 100644
@@ -424,12 +424,13 @@ common-obj-y += qmp.o hmp.o
 ######################################################################
 # guest agent
 
-qga-nested-y = commands.o commands-posix.o guest-agent-command-state.o
-qga-nested-y += channel-posix.o
+qga-nested-y = commands.o guest-agent-command-state.o
+qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
+qga-nested-$(CONFIG_WIN32) += commands-win32.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
-qga-obj-y += qemu-ga.o qemu-sockets.o module.o qemu-option.o
+qga-obj-y += qemu-ga.o module.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
-qga-obj-$(CONFIG_POSIX) += oslib-posix.o
+qga-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-sockets.o qemu-option.o
 
 vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
 
configure +1 -1
index 3b0b300..aaf8171 100755
@@ -508,7 +508,7 @@ if test "$mingw32" = "yes" ; then
   bindir="\${prefix}"
   sysconfdir="\${prefix}"
   confsuffix=""
-  guest_agent="no"
+  libs_qga="-lws2_32 -lwinmm $lib_qga"
 fi
 
 werror=""
qemu-ga.c +16 -0
index 2e8af02..93ebc3e 100644
@@ -15,7 +15,9 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <getopt.h>
+#ifndef _WIN32
 #include <syslog.h>
+#endif
 #include "json-streamer.h"
 #include "json-parser.h"
 #include "qint.h"
@@ -44,6 +46,7 @@ struct GAState {
 
 static struct GAState *ga_state;
 
+#ifndef _WIN32
 static void quit_handler(int sig)
 {
     g_debug("received signal num %d, quitting", sig);
@@ -73,6 +76,7 @@ static gboolean register_signal_handlers(void)
     }
     return true;
 }
+#endif
 
 static void usage(const char *cmd)
 {
@@ -87,7 +91,9 @@ static void usage(const char *cmd)
 "  -f, --pidfile     specify pidfile (default is %s)\n"
 "  -v, --verbose     log extra debugging information\n"
 "  -V, --version     print version information and exit\n"
+#ifndef _WIN32
 "  -d, --daemonize   become a daemon\n"
+#endif
 "  -b, --blacklist   comma-separated list of RPCs to disable (no spaces, \"?\""
 "                    to list available RPCs)\n"
 "  -h, --help        display this help and exit\n"
@@ -143,9 +149,13 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
     }
 
     level &= G_LOG_LEVEL_MASK;
+#ifndef _WIN32
     if (domain && strcmp(domain, "syslog") == 0) {
         syslog(LOG_INFO, "%s: %s", level_str, msg);
     } else if (level & s->log_level) {
+#else
+    if (level & s->log_level) {
+#endif
         g_get_current_time(&time);
         fprintf(s->log_file,
                 "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg);
@@ -153,6 +163,7 @@ static void ga_log(const gchar *domain, GLogLevelFlags level,
     }
 }
 
+#ifndef _WIN32
 static void become_daemon(const char *pidfile)
 {
     pid_t pid, sid;
@@ -203,6 +214,7 @@ fail:
     g_critical("failed to daemonize");
     exit(EXIT_FAILURE);
 }
+#endif
 
 static int send_response(GAState *s, QObject *payload)
 {
@@ -466,10 +478,12 @@ int main(int argc, char **argv)
         }
     }
 
+#ifndef _WIN32
     if (daemonize) {
         g_debug("starting daemon");
         become_daemon(pidfile);
     }
+#endif
 
     s = g_malloc0(sizeof(GAState));
     s->log_file = log_file;
@@ -482,10 +496,12 @@ int main(int argc, char **argv)
     ga_command_state_init_all(s->command_state);
     json_message_parser_init(&s->parser, process_event);
     ga_state = s;
+#ifndef _WIN32
     if (!register_signal_handlers()) {
         g_critical("failed to register signal handlers");
         goto out_bad;
     }
+#endif
 
     s->main_loop = g_main_loop_new(NULL, false);
     if (!channel_init(ga_state, method, path)) {
qga/commands-win32.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#7 lore raw

PATCH v2 7/8 qemu-ga: add Windows service integration

In reply to Michael Roth

This allows qemu-ga to function as a Windows service:

- to install the service (will auto-start on boot):

     qemu-ga --service install
 - to start the service:
     net start qemu-ga
 - to stop the service:
     net stop qemu-ga
 - to uninstall service:
     qemu-ga --service uninstall

Original patch by Gal Hammer <[email protected]>

---
 Makefile.objs       |    2 +-
 qemu-ga.c           |  103 ++++++++++++++++++++++++++++++++++++++++++++--
 qga/service-win32.c |  114 +++++++++++++++++++++++++++++++++++++++++++++++++++
 qga/service-win32.h |   30 +++++++++++++
 4 files changed, 244 insertions(+), 5 deletions(-)
 create mode 100644 qga/service-win32.c
 create mode 100644 qga/service-win32.h
Makefile.objs +1 -1
index e1cb54a..3b08e70 100644
@@ -426,7 +426,7 @@ common-obj-y += qmp.o hmp.o
 
 qga-nested-y = commands.o guest-agent-command-state.o
 qga-nested-$(CONFIG_POSIX) += commands-posix.o channel-posix.o
-qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o
+qga-nested-$(CONFIG_WIN32) += commands-win32.o channel-win32.o service-win32.o
 qga-obj-y = $(addprefix qga/, $(qga-nested-y))
 qga-obj-y += qemu-ga.o module.o
 qga-obj-$(CONFIG_WIN32) += oslib-win32.o
qemu-ga.c +99 -4

166 lines not on this page · raw message

qga/service-win32.c new file +114 -0

114 lines not on this page · raw message

qga/service-win32.h new file +30 -0
new file mode 100644
index 0000000..99dfc53
@@ -0,0 +1,30 @@
+/*
+ * QEMU Guest Agent helpers for win32 service management
+ *
+ * Copyright IBM Corp. 2012
+ *
+ * Authors:
+ *  Gal Hammer        <[email protected]>
+ *  Michael Roth      <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QGA_SERVICE_H
+#define QGA_SERVICE_H
+
+#include <windows.h>
+
+#define QGA_SERVICE_DISPLAY_NAME "QEMU Guest Agent"
+#define QGA_SERVICE_NAME         "qemu-ga"
+#define QGA_SERVICE_DESCRIPTION  "Enables integration with QEMU machine emulator and virtualizer."
+
+typedef struct GAService {
+    SERVICE_STATUS status;
+    SERVICE_STATUS_HANDLE status_handle;
+} GAService;
+
+int ga_install_service(const char *path, const char *logfile);
+int ga_uninstall_service(void);
+
+#endif
Signature · 1 line
1.7.4.1
#8 lore raw

PATCH v2 8/8 qemu-ga: add win32 guest-shutdown command

In reply to Michael Roth

Implement guest-shutdown RPC for Windows. Functionally this should be equivalent to the posix implementation.

Original patch by Gal Hammer <[email protected]>

---
 qga/commands-win32.c |   41 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 40 insertions(+), 1 deletions(-)
qga/commands-win32.c +40 -1
index d96f1ad..4aa0f0d 100644
@@ -15,9 +15,48 @@
 #include "qga-qmp-commands.h"
 #include "qerror.h"
 
+#ifndef SHTDN_REASON_FLAG_PLANNED
+#define SHTDN_REASON_FLAG_PLANNED 0x80000000
+#endif
+
 void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
 {
-    error_set(err, QERR_UNSUPPORTED);
+    HANDLE token;
+    TOKEN_PRIVILEGES priv;
+    UINT shutdown_flag = EWX_FORCE;
+
+    slog("guest-shutdown called, mode: %s", mode);
+
+    if (!has_mode || strcmp(mode, "powerdown") == 0) {
+        shutdown_flag |= EWX_POWEROFF;
+    } else if (strcmp(mode, "halt") == 0) {
+        shutdown_flag |= EWX_SHUTDOWN;
+    } else if (strcmp(mode, "reboot") == 0) {
+        shutdown_flag |= EWX_REBOOT;
+    } else {
+        error_set(err, QERR_INVALID_PARAMETER_VALUE, "mode",
+                  "halt|powerdown|reboot");
+        return;
+    }
+
+    /* Request a shutdown privilege, but try to shut down the system
+       anyway. */
+    if (OpenProcessToken(GetCurrentProcess(),
+        TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &token))
+    {
+        LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
+            &priv.Privileges[0].Luid);
+
+        priv.PrivilegeCount = 1;
+        priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+
+        AdjustTokenPrivileges(token, FALSE, &priv, 0, NULL, 0);
+    }
+
+    if (!ExitWindowsEx(shutdown_flag, SHTDN_REASON_FLAG_PLANNED)) {
+        slog("guest-shutdown failed: %d", GetLastError());
+        error_set(err, QERR_UNDEFINED_ERROR);
+    }
 }
 
 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
Signature · 1 line
1.7.4.1
#9 lore raw
In reply to Michael Roth
Quoted 8 lines · On Thu, 2 Feb 2012 13:58:52 -0600 Michael Roth <[email protected]>…

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v2

Luiz/Gal, I decided not to roll the suspend/hibernate stuff into this series since the s3 situation isn't fully sorted out yet. The file structure is a little different now, posix/linux-specific stuff goes in qga/commands-posix.c, win32-specific stuff in qga/commands-win32.c, but other than that it should be a straightforward rebase if this gets merged first.

I think I'll have to rebase my series on top of this one, when do you plan to merge this?

#11 lore raw
In reply to Michael Roth
Quoted 17 lines · On Fri, 03 Feb 2012 10:37:25 -0600 Michael Roth <[email protected]>…
Quoted 6 lines · Luiz Capitulino
Quoted 3 lines · On Thu, 2 Feb 2012 13:58:52 -0600 Michael Roth<[email protected]>…

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v2

Quoted 9 lines
Quoted 5 lines

Luiz/Gal, I decided not to roll the suspend/hibernate stuff into this series since the s3 situation isn't fully sorted out yet. The file structure is a little different now, posix/linux-specific stuff goes in qga/commands-posix.c, win32-specific stuff in qga/commands-win32.c, but other than that it should be a straightforward rebase if this gets merged first.

I think I'll have to rebase my series on top of this one, when do you plan to merge this?

Quoted 6 lines

Hopefully soon, was planning on waiting for the suspend/hibernate bits but we seem to be blocked on the s3 issues and I have other patches accumulating on top of win32 (hesitant to base those on master since this patchset does a lot of refactoring that might affect them), so I figured I'd push this for merge since it doesn't have any dependencies outside master.

The S3 issues seem sorted to me, but I don't oppose having this series in first.

#10 lore raw
In reply to Luiz Capitulino
Quoted 6 lines · Luiz Capitulino
Quoted 3 lines · On Thu, 2 Feb 2012 13:58:52 -0600 Michael Roth<[email protected]>…

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v2

Quoted 9 lines
Quoted 5 lines

Luiz/Gal, I decided not to roll the suspend/hibernate stuff into this series since the s3 situation isn't fully sorted out yet. The file structure is a little different now, posix/linux-specific stuff goes in qga/commands-posix.c, win32-specific stuff in qga/commands-win32.c, but other than that it should be a straightforward rebase if this gets merged first.

I think I'll have to rebase my series on top of this one, when do you plan to merge this?

Hopefully soon, was planning on waiting for the suspend/hibernate bits but we seem to be blocked on the s3 issues and I have other patches accumulating on top of win32 (hesitant to base those on master since this patchset does a lot of refactoring that might affect them), so I figured I'd push this for merge since it doesn't have any dependencies outside master.

#12 lore raw
In reply to Luiz Capitulino
Quoted 20 lines · Luiz Capitulino
Quoted 17 lines · On Fri, 03 Feb 2012 10:37:25 -0600 Michael Roth<[email protected]>…
Quoted 15 lines · Luiz Capitulino
Quoted 8 lines · On Thu, 2 Feb 2012 13:58:52 -0600 Michael Roth<[email protected]>…

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v2

Luiz/Gal, I decided not to roll the suspend/hibernate stuff into this series since the s3 situation isn't fully sorted out yet. The file structure is a little different now, posix/linux-specific stuff goes in qga/commands-posix.c, win32-specific stuff in qga/commands-win32.c, but other than that it should be a straightforward rebase if this gets merged first.

I think I'll have to rebase my series on top of this one, when do you plan to merge this?

Quoted 9 lines
Quoted 6 lines

Hopefully soon, was planning on waiting for the suspend/hibernate bits but we seem to be blocked on the s3 issues and I have other patches accumulating on top of win32 (hesitant to base those on master since this patchset does a lot of refactoring that might affect them), so I figured I'd push this for merge since it doesn't have any dependencies outside master.

The S3 issues seem sorted to me, but I don't oppose having this series in first.

Thanks, in retrospect I probably should've just gotten these out of the way weeks ago since they'd immediately clobber git blame.

I'd been tracking Gerd's QMP wakeup series as the s3 resolution we need for guest-suspend, is that still the case? I guess those are coming through your QMP queue?

#13 lore raw
Quoted 28 lines · On Fri, 03 Feb 2012 11:23:05 -0600 Michael Roth <[email protected]>…
Quoted 27 lines · Luiz Capitulino
Quoted 23 lines · On Fri, 03 Feb 2012 10:37:25 -0600 Michael Roth<[email protected]>…
Quoted 6 lines · Luiz Capitulino
Quoted 3 lines · On Thu, 2 Feb 2012 13:58:52 -0600 Michael Roth<[email protected]>…

These patches apply on top of qemu.git master, and can also be obtained from:
git://github.com/mdroth/qemu.git qga-win32-v2

Quoted 9 lines
Quoted 5 lines

Luiz/Gal, I decided not to roll the suspend/hibernate stuff into this series since the s3 situation isn't fully sorted out yet. The file structure is a little different now, posix/linux-specific stuff goes in qga/commands-posix.c, win32-specific stuff in qga/commands-win32.c, but other than that it should be a straightforward rebase if this gets merged first.

I think I'll have to rebase my series on top of this one, when do you plan to merge this?

Hopefully soon, was planning on waiting for the suspend/hibernate bits but we seem to be blocked on the s3 issues and I have other patches accumulating on top of win32 (hesitant to base those on master since this patchset does a lot of refactoring that might affect them), so I figured I'd push this for merge since it doesn't have any dependencies outside master.

Quoted 8 lines
Quoted 2 lines

The S3 issues seem sorted to me, but I don't oppose having this series in first.

Thanks, in retrospect I probably should've just gotten these out of the way weeks ago since they'd immediately clobber git blame.

I'd been tracking Gerd's QMP wakeup series as the s3 resolution we need for guest-suspend, is that still the case?

Yes. But now I remembered about a seabios bug with S3... Need to check if it were already addressed.

Quoted 2 lines

I guess those are coming
through your QMP queue?

Oh, as the QMP part is trivial I thought someone else would pick them up, but I can do that.

#14 lore raw
Quoted 7 lines · Luiz Capitulino
Quoted 2 lines · On Fri, 03 Feb 2012 11:23:05 -0600 Michael Roth <[email protected]>…

I'd been tracking Gerd's QMP wakeup series as the s3 resolution we need for guest-suspend, is that still the case?

Yes. But now I remembered about a seabios bug with S3... Need to check if it were already addressed.

Hi Luiz,

I'm not aware of any recent S3 defects in SeaBIOS. If there is a defect, please let me know.

(I am aware of recent discussions on SeaBIOS and it running the vgabios on s3-resume, but I would not classify that issue as a defect.)

-Kevin

#15 lore raw
Quoted 9 lines · On Sat, 4 Feb 2012 10:34:32 -0500 "Kevin O'Connor" <[email protected]> wrote:
Quoted 7 lines · Luiz Capitulino
Quoted 2 lines · On Fri, 03 Feb 2012 11:23:05 -0600 Michael Roth <[email protected]>…

I'd been tracking Gerd's QMP wakeup series as the s3 resolution we need for guest-suspend, is that still the case?

Yes. But now I remembered about a seabios bug with S3... Need to check if it were already addressed.

Quoted 8 lines

Hi Luiz,

I'm not aware of any recent S3 defects in SeaBIOS. If there is a defect, please let me know.

(I am aware of recent discussions on SeaBIOS and it running the vgabios on s3-resume, but I would not classify that issue as a defect.)

The problem is that, the screen goes black after resuming from S3. Gleb debugged it a bit and he said that it was caused by a change in seabios.

Please, take a look at the last three comments in this bz:

https://bugzilla.redhat.com/show_bug.cgi?id=772614

#16 lore raw
Quoted 17 lines · Luiz Capitulino
Quoted 9 lines · Kevin O'Connor
Quoted 7 lines · Luiz Capitulino
Quoted 2 lines · On Fri, 03 Feb 2012 11:23:05 -0600 Michael Roth <[email protected]>…

I'd been tracking Gerd's QMP wakeup series as the s3 resolution we need for guest-suspend, is that still the case?

Yes. But now I remembered about a seabios bug with S3... Need to check if it were already addressed.

Quoted 6 lines

I'm not aware of any recent S3 defects in SeaBIOS. If there is a defect, please let me know.

(I am aware of recent discussions on SeaBIOS and it running the vgabios on s3-resume, but I would not classify that issue as a defect.)

Quoted 6 lines

The problem is that, the screen goes black after resuming from S3. Gleb debugged it a bit and he said that it was caused by a change in seabios.

Please, take a look at the last three comments in this bz:

https://bugzilla.redhat.com/show_bug.cgi?id=772614

Perhaps a semantic distinction, but I don't consider that to be a seabios defect.

In any case, I don't think this was addressed. Gerd published a patch that can address this in qemu:
http://www.seabios.org/pipermail/seabios/2012-January/002944.html

-Kevin

#17 lore raw
Quoted 21 lines · Kevin O'Connor
Quoted 20 lines · Luiz Capitulino
Quoted 9 lines · Kevin O'Connor
Quoted 7 lines · Luiz Capitulino
Quoted 2 lines · On Fri, 03 Feb 2012 11:23:05 -0600 Michael Roth <[email protected]>…

I'd been tracking Gerd's QMP wakeup series as the s3 resolution we need for guest-suspend, is that still the case?

Yes. But now I remembered about a seabios bug with S3... Need to check if it were already addressed.

Quoted 6 lines

I'm not aware of any recent S3 defects in SeaBIOS. If there is a defect, please let me know.

(I am aware of recent discussions on SeaBIOS and it running the vgabios on s3-resume, but I would not classify that issue as a defect.)

The problem is that, the screen goes black after resuming from S3. Gleb debugged it a bit and he said that it was caused by a change in seabios.

Quoted 7 lines
Quoted 3 lines

Please, take a look at the last three comments in this bz:

https://bugzilla.redhat.com/show_bug.cgi?id=772614

Perhaps a semantic distinction, but I don't consider that to be a seabios defect.

Non optimal default. The default didn't change BTW, but it was config parameter before and we changed it for RHEL. Now config parameter is gone.

Quoted 4 lines

In any case, I don't think this was addressed. Gerd published a patch that can address this in qemu:
http://www.seabios.org/pipermail/seabios/2012-January/002944.html

Strictly speaking the patch is incorrect since it introduces the file for all architectures, but I do not think qemu is the right place to tune SeaBIOS defaults. I propose this patch instead:
---

Run vgabios during S3 resume by default on QEMU. QEMU still able to modify SeaBIOS behavior if it wishes so by providing etc/s3-resume-vga-init file.

Gleb Natapov <[email protected]>

src/optionroms.c +1 -1
index 27cfffd..06db1c1 100644
@@ -423,7 +423,7 @@ vga_setup(void)
 
     // Load some config settings that impact VGA.
     EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
-    S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", 0);
+    S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", !CONFIG_COREBOOT);
     ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
 
     if (CONFIG_OPTIONROMS_DEPLOYED) {

--
Gleb.

#18 lore raw
Quoted 7 lines · Gleb Natapov
Quoted 3 lines · Kevin O'Connor

Perhaps a semantic distinction, but I don't consider that to be a seabios defect.

Non optimal default. The default didn't change BTW, but it was config parameter before and we changed it for RHEL. Now config parameter is gone.

Config parameter moved from compile time to runtime. But I agree it is unfortunate that knowledge of that didn't get to the right people.

Quoted 7 lines
Quoted 4 lines

In any case, I don't think this was addressed. Gerd published a patch that can address this in qemu:
http://www.seabios.org/pipermail/seabios/2012-January/002944.html

Strictly speaking the patch is incorrect since it introduces the file for all architectures, but I do not think qemu is the right place to tune SeaBIOS defaults. I propose this patch instead:

[...]

src/optionroms.c vga_setup(void) · line 427

Quoted 5 lines
     // Load some config settings that impact VGA.
     EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
src/optionroms.c +1 -1
-    S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", 0);
+    S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", !CONFIG_COREBOOT);
     ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);

I'm concerned about the VGA passthrough case. (I know that's not common and has other issues, but I also know several people have been working with it.) As near as I can tell, running the VGA rom on S3 resume has as much chance of breaking things as helping things. It's fine for the cirrus/bochsvga vgaroms that are totally under our control, but it'd be an open guess for any third-party code. (Again, if someone has documentation to the contrary please let me know.)

So, compiling this into SeaBIOS doesn't seems like the right choice to me.

-Kevin

#19 lore raw
Quoted 14 lines · Kevin O'Connor
Quoted 7 lines
Quoted 4 lines

In any case, I don't think this was addressed. Gerd published a patch that can address this in qemu:
http://www.seabios.org/pipermail/seabios/2012-January/002944.html

Strictly speaking the patch is incorrect since it introduces the file for all architectures, but I do not think qemu is the right place to tune SeaBIOS defaults. I propose this patch instead:

[...]

src/optionroms.c vga_setup(void) · line 427

Quoted 5 lines
     // Load some config settings that impact VGA.
     EnforceChecksum = romfile_loadint("etc/optionroms-checksum", 1);
src/optionroms.c +1 -1
-    S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", 0);
+    S3ResumeVgaInit = romfile_loadint("etc/s3-resume-vga-init", !CONFIG_COREBOOT);
     ScreenAndDebug = romfile_loadint("etc/screen-and-debug", 1);
Quoted 8 lines

I'm concerned about the VGA passthrough case. (I know that's not common and has other issues, but I also know several people have been working with it.) As near as I can tell, running the VGA rom on S3 resume has as much chance of breaking things as helping things. It's fine for the cirrus/bochsvga vgaroms that are totally under our control, but it'd be an open guess for any third-party code. (Again, if someone has documentation to the contrary please let me know.)

VGA passthrough does not work with QEMU without code changes. Whoever works on it will have to provide etc/s3-resume-vga-init file with appropriate value. My patch above does not remove run time selection, it only changes the default.

Quoted 3 lines

So, compiling this into SeaBIOS doesn't seems like the right choice to me.

It is still run time selectable. I think it is best of both worlds.

--
Gleb.

#20 lore raw
Quoted 9 lines · Gleb Natapov
Quoted 8 lines · Kevin O'Connor

I'm concerned about the VGA passthrough case. (I know that's not common and has other issues, but I also know several people have been working with it.) As near as I can tell, running the VGA rom on S3 resume has as much chance of breaking things as helping things. It's fine for the cirrus/bochsvga vgaroms that are totally under our control, but it'd be an open guess for any third-party code. (Again, if someone has documentation to the contrary please let me know.)

Quoted 4 lines

VGA passthrough does not work with QEMU without code changes. Whoever works on it will have to provide etc/s3-resume-vga-init file with appropriate value. My patch above does not remove run time selection, it only changes the default.

True.

I view running the vgabios on s3 a hack and think an explicit "please apply hack" flag is nicer than the inverse.

However, it's clear this hack helps the majority of qemu/kvm users. So, I'm okay with changing the default. It is a change of default though (upstream kvm/qemu has never run the vgabios on s3 resume before). So, we need to make sure there's proper notice of the change and assuming no objection I'll go forward with it.

-Kevin