From patchwork Thu Nov 22 02:15:46 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomoki Sekiyama X-Patchwork-Id: 200924 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3C2A12C008A for ; Thu, 22 Nov 2012 13:16:08 +1100 (EST) Received: from localhost ([::1]:40446 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbMKk-0004dN-9A for incoming@patchwork.ozlabs.org; Wed, 21 Nov 2012 21:16:06 -0500 Received: from eggs.gnu.org ([208.118.235.92]:52953) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbMKW-0004Ud-LY for qemu-devel@nongnu.org; Wed, 21 Nov 2012 21:15:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TbMKV-00014q-AO for qemu-devel@nongnu.org; Wed, 21 Nov 2012 21:15:52 -0500 Received: from mail9.hitachi.co.jp ([133.145.228.44]:38296) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbMKU-00014Y-Jo for qemu-devel@nongnu.org; Wed, 21 Nov 2012 21:15:51 -0500 Received: from mlsv6.hitachi.co.jp (unknown [133.144.234.166]) by mail9.hitachi.co.jp (Postfix) with ESMTP id 3F3BE37C82; Thu, 22 Nov 2012 11:15:49 +0900 (JST) Received: from mfilter03.hitachi.co.jp by mlsv6.hitachi.co.jp (8.13.1/8.13.1) id qAM2FnwZ021664; Thu, 22 Nov 2012 11:15:49 +0900 Received: from vshuts01.hitachi.co.jp (vshuts01.hitachi.co.jp [10.201.6.83]) by mfilter03.hitachi.co.jp (Switch-3.3.4/Switch-3.3.4) with ESMTP id qAM2FmjS015141; Thu, 22 Nov 2012 11:15:48 +0900 Received: from vshuts2.hitachi.co.jp (unknown [10.201.6.71]) by vshuts01.hitachi.co.jp (Postfix) with ESMTP id 198332F00A3; Thu, 22 Nov 2012 11:15:48 +0900 (JST) X-AuditID: b753bd60-94fe2ba000004744-16-50ad8ad39c72 Received: from hsdlmain.sdl.hitachi.co.jp (unknown [133.144.14.194]) by vshuts2.hitachi.co.jp (Symantec Mail Security) with ESMTP id D9B4F8B036E; Thu, 22 Nov 2012 11:15:47 +0900 (JST) Received: from hsdlvgate2.sdl.hitachi.co.jp by hsdlmain.sdl.hitachi.co.jp (8.13.8/3.7W11021512) id qAM2FlaS027050; Thu, 22 Nov 2012 11:15:47 +0900 X-AuditID: b753bd60-94fe2ba000004744-16-50ad8ad39c72 Received: from sdl99w.sdl.hitachi.co.jp (sdl99w.sdl.hitachi.co.jp [133.144.14.250]) by hsdlvgate2.sdl.hitachi.co.jp (Symantec Mail Security) with ESMTP id 5B999236561; Thu, 22 Nov 2012 20:00:17 +0900 (JST) Received: from melchior2.sdl.hitachi.co.jp (unknown [10.232.28.238]) by sdl99w.sdl.hitachi.co.jp (Postfix) with ESMTP id 0C7E253C1F1; Thu, 22 Nov 2012 11:15:50 +0900 (JST) To: qemu-devel@nongnu.org From: Tomoki Sekiyama Date: Thu, 22 Nov 2012 11:15:46 +0900 Message-ID: <20121122021546.9852.30043.stgit@melchior2.sdl.hitachi.co.jp> In-Reply-To: <20121122021535.9852.40353.stgit@melchior2.sdl.hitachi.co.jp> References: <20121122021535.9852.40353.stgit@melchior2.sdl.hitachi.co.jp> User-Agent: StGit/0.16 MIME-Version: 1.0 X-Brightmail-Tracker: AAAAAA== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 133.145.228.44 Cc: mdroth@linux.vnet.ibm.com Subject: Re: [Qemu-devel] [PATCH v4 1/2] qemu-ga: execute hook to quiesce the guest on fsfreeze-freeze/thaw X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org To use the online disk snapshot for online-backup, application-level consistency of the snapshot image is required. However, currently the guest agent can provide only filesystem-level consistency, and the snapshot may contain dirty data, for example, incomplete transactions. This patch provides the opportunity to quiesce applications before snapshot is taken. When the qemu-ga receives fsfreeze-freeze command, the hook script specified in --fsfreeze-hook option is executed with "freeze" argument before the filesystem is frozen. For fsfreeze-thaw command, the hook script is executed with "thaw" argument after the filesystem is thawed. Signed-off-by: Tomoki Sekiyama --- qemu-ga.c | 42 ++++++++++++++++++++++++++++++- qga/commands-posix.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ qga/guest-agent-core.h | 1 + 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/qemu-ga.c b/qemu-ga.c index 9b59a52..240f6e2 100644 --- a/qemu-ga.c +++ b/qemu-ga.c @@ -34,6 +34,12 @@ #include "qga/service-win32.h" #include #endif +#ifdef __linux__ +#include +#ifdef FIFREEZE +#define CONFIG_FSFREEZE +#endif +#endif #ifndef _WIN32 #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" @@ -42,6 +48,9 @@ #endif #define QGA_STATEDIR_DEFAULT CONFIG_QEMU_LOCALSTATEDIR "/run" #define QGA_PIDFILE_DEFAULT QGA_STATEDIR_DEFAULT "/qemu-ga.pid" +#ifdef CONFIG_FSFREEZE +#define QGA_FSFREEZE_HOOK_DEFAULT CONFIG_QEMU_CONFDIR "/fsfreeze-hook" +#endif #define QGA_SENTINEL_BYTE 0xFF struct GAState { @@ -64,6 +73,9 @@ struct GAState { const char *log_filepath; const char *pid_filepath; } deferred_options; +#ifdef CONFIG_FSFREEZE + const char *fsfreeze_hook; +#endif }; struct GAState *ga_state; @@ -153,6 +165,10 @@ static void usage(const char *cmd) " %s)\n" " -l, --logfile set logfile path, logs to stderr by default\n" " -f, --pidfile specify pidfile (default is %s)\n" +#ifdef CONFIG_FSFREEZE +" -F, --fsfreeze-hook\n" +" specify fsfreeze hook (default is %s)\n" +#endif " -t, --statedir specify dir to store state information (absolute paths\n" " only, default is %s)\n" " -v, --verbose log extra debugging information\n" @@ -167,6 +183,9 @@ static void usage(const char *cmd) "\n" "Report bugs to \n" , cmd, QEMU_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT, +#ifdef CONFIG_FSFREEZE + QGA_FSFREEZE_HOOK_DEFAULT, +#endif QGA_STATEDIR_DEFAULT); } @@ -401,6 +420,13 @@ void ga_unset_frozen(GAState *s) } } +#ifdef CONFIG_FSFREEZE +const char *ga_fsfreeze_hook(GAState *s) +{ + return s->fsfreeze_hook; +} +#endif + static void become_daemon(const char *pidfile) { #ifndef _WIN32 @@ -678,10 +704,13 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[]) int main(int argc, char **argv) { - const char *sopt = "hVvdm:p:l:f:b:s:t:"; + const char *sopt = "hVvdm:p:l:f:F:b:s:t:"; const char *method = NULL, *path = NULL; const char *log_filepath = NULL; const char *pid_filepath = QGA_PIDFILE_DEFAULT; +#ifdef CONFIG_FSFREEZE + const char *fsfreeze_hook = QGA_FSFREEZE_HOOK_DEFAULT; +#endif const char *state_dir = QGA_STATEDIR_DEFAULT; #ifdef _WIN32 const char *service = NULL; @@ -691,6 +720,9 @@ int main(int argc, char **argv) { "version", 0, NULL, 'V' }, { "logfile", 1, NULL, 'l' }, { "pidfile", 1, NULL, 'f' }, +#ifdef CONFIG_FSFREEZE + { "fsfreeze-hook", 1, NULL, 'F' }, +#endif { "verbose", 0, NULL, 'v' }, { "method", 1, NULL, 'm' }, { "path", 1, NULL, 'p' }, @@ -723,6 +755,11 @@ int main(int argc, char **argv) case 'f': pid_filepath = optarg; break; +#ifdef CONFIG_FSFREEZE + case 'F': + fsfreeze_hook = optarg; + break; +#endif case 't': state_dir = optarg; break; @@ -786,6 +823,9 @@ int main(int argc, char **argv) s = g_malloc0(sizeof(GAState)); s->log_level = log_level; s->log_file = stderr; +#ifdef CONFIG_FSFREEZE + s->fsfreeze_hook = fsfreeze_hook; +#endif g_log_set_default_handler(ga_log, s); g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); ga_enable_logging(s); diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 726930a..9849c10 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -396,6 +396,62 @@ GuestFsfreezeStatus qmp_guest_fsfreeze_status(Error **err) return GUEST_FSFREEZE_STATUS_THAWED; } +typedef enum { + FSFREEZE_HOOK_THAW = 0, + FSFREEZE_HOOK_FREEZE, +} FsfreezeHookArg; + +const char *fsfreeze_hook_arg_string[] = { + "thaw", + "freeze", +}; + +/* + * Return -1 if hook is configured and exited abnormally. Otherwise return 0. + */ +static int execute_fsfreeze_hook(FsfreezeHookArg arg) +{ + int status; + pid_t pid, rpid; + const char *hook; + const char *arg_str = fsfreeze_hook_arg_string[arg]; + + hook = ga_fsfreeze_hook(ga_state); + if (!hook || access(hook, X_OK) != 0) { + return 0; + } + + slog("executing fsfreeze hook with arg `%s'", arg_str); + pid = fork(); + if (pid == 0) { + setsid(); + reopen_fd_to_null(0); + reopen_fd_to_null(1); + reopen_fd_to_null(2); + + execle(hook, hook, arg_str, NULL, environ); + _exit(EXIT_FAILURE); + } else if (pid < 0) { + slog("execution of fsfreeze hook failed: %s", strerror(errno)); + return -1; + } + + do { + rpid = waitpid(pid, &status, 0); + } while (rpid == -1 && errno == EINTR); + if (rpid == pid && WIFEXITED(status)) { + int st = WEXITSTATUS(status); + if (st) { + slog("fsfreeze hook failed with status %d", st); + return -1; + } + } else if (rpid == pid && WIFSIGNALED(status)) { + slog("fsfreeze hook killed by signal %d", WTERMSIG(status)); + return -1; + } + return 0; +} + /* * Walk list of mounted file systems in the guest, and freeze the ones which * are real local file systems. @@ -410,6 +466,13 @@ int64_t qmp_guest_fsfreeze_freeze(Error **err) slog("guest-fsfreeze called"); + ret = execute_fsfreeze_hook(FSFREEZE_HOOK_FREEZE); + if (ret < 0) { + sprintf(err_msg, "execution of fsfreeze hook failed"); + error_set(err, QERR_QGA_COMMAND_FAILED, err_msg); + return ret; + } + QTAILQ_INIT(&mounts); ret = build_fs_mount_list(&mounts); if (ret < 0) { @@ -513,6 +576,9 @@ int64_t qmp_guest_fsfreeze_thaw(Error **err) ga_unset_frozen(ga_state); free_fs_mount_list(&mounts); + + execute_fsfreeze_hook(FSFREEZE_HOOK_THAW); + return i; } diff --git a/qga/guest-agent-core.h b/qga/guest-agent-core.h index 49a7abe..c6e8de0 100644 --- a/qga/guest-agent-core.h +++ b/qga/guest-agent-core.h @@ -34,6 +34,7 @@ void ga_set_response_delimited(GAState *s); bool ga_is_frozen(GAState *s); void ga_set_frozen(GAState *s); void ga_unset_frozen(GAState *s); +const char *ga_fsfreeze_hook(GAState *s); #ifndef _WIN32 void reopen_fd_to_null(int fd);