From: Sasha Levin Subject: [PATCH] kvm tools: Add 'kvm stop' Date: Fri, 5 Aug 2011 18:21:10 +0300 Message-ID: <1312557670-32709-1-git-send-email-levinsasha928@gmail.com> Cc: kvm@vger.kernel.org, aneesh.kumar@linux.vnet.ibm.com, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, Sasha Levin To: penberg@kernel.org Return-path: Received: from mail-wy0-f174.google.com ([74.125.82.174]:50539 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754377Ab1HEPWI (ORCPT ); Fri, 5 Aug 2011 11:22:08 -0400 Received: by wyf22 with SMTP id 22so1299474wyf.19 for ; Fri, 05 Aug 2011 08:22:07 -0700 (PDT) Sender: kvm-owner@vger.kernel.org List-ID: This command stops a running instance. Syntax: kvm stop [instance name] Signed-off-by: Sasha Levin --- tools/kvm/Documentation/kvm-stop.txt | 16 ++++++++++++++++ tools/kvm/Makefile | 1 + tools/kvm/builtin-run.c | 6 ++++++ tools/kvm/builtin-stop.c | 32 ++++++++++++++++++++++++++++++++ tools/kvm/command-list.txt | 1 + tools/kvm/include/kvm/builtin-stop.h | 6 ++++++ tools/kvm/include/kvm/kvm.h | 1 + tools/kvm/kvm-cmd.c | 2 ++ 8 files changed, 65 insertions(+), 0 deletions(-) create mode 100644 tools/kvm/Documentation/kvm-stop.txt create mode 100644 tools/kvm/builtin-stop.c create mode 100644 tools/kvm/include/kvm/builtin-stop.h diff --git a/tools/kvm/Documentation/kvm-stop.txt b/tools/kvm/Documentation/kvm-stop.txt new file mode 100644 index 0000000..5267081 --- /dev/null +++ b/tools/kvm/Documentation/kvm-stop.txt @@ -0,0 +1,16 @@ +kvm-stop(1) +================ + +NAME +---- +kvm-stop - Stop a running instance + +SYNOPSIS +-------- +[verse] +'kvm stop [instance]' + +DESCRIPTION +----------- +The command stops a running instance. +For a list of running instances see 'kvm list'. diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 21d6189..b52f503 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -26,6 +26,7 @@ OBJS += builtin-help.o OBJS += builtin-list.o OBJS += builtin-pause.o OBJS += builtin-run.o +OBJS += builtin-stop.o OBJS += builtin-version.o OBJS += cpuid.o OBJS += disk/core.o diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index 2e04265..7a76255 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -261,6 +261,11 @@ static void handle_sigalrm(int sig) virtio_console__inject_interrupt(kvm); } +static void handle_sigstop(int sig) +{ + kvm_cpu__reboot(); +} + static void *kvm_cpu_thread(void *arg) { current_kvm_cpu = arg; @@ -489,6 +494,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) signal(SIGQUIT, handle_sigquit); signal(SIGUSR1, handle_sigusr1); signal(SIGUSR2, handle_sigusr2); + signal(SIGKVMSTOP, handle_sigstop); nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); diff --git a/tools/kvm/builtin-stop.c b/tools/kvm/builtin-stop.c new file mode 100644 index 0000000..7dd2015 --- /dev/null +++ b/tools/kvm/builtin-stop.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#include +#include +#include + +static void do_stop(const char *name, int pid) +{ + kill(pid, SIGKVMSTOP); +} + +int kvm_cmd_stop(int argc, const char **argv, const char *prefix) +{ + int pid; + + if (argc != 1) + die("Usage: kvm stop [instance name]\n"); + + if (strcmp(argv[0], "all") == 0) { + kvm__enumerate_instances(do_stop); + return 0; + } + + pid = kvm__get_pid_by_instance(argv[0]); + if (pid < 0) + die("Failed locating instance name"); + + return kill(pid, SIGKVMSTOP); +} diff --git a/tools/kvm/command-list.txt b/tools/kvm/command-list.txt index 037a8ea..fd809a8 100644 --- a/tools/kvm/command-list.txt +++ b/tools/kvm/command-list.txt @@ -8,3 +8,4 @@ kvm-version common kvm-list common kvm-debug common kvm-balloon common +kvm-stop common diff --git a/tools/kvm/include/kvm/builtin-stop.h b/tools/kvm/include/kvm/builtin-stop.h new file mode 100644 index 0000000..317d34d --- /dev/null +++ b/tools/kvm/include/kvm/builtin-stop.h @@ -0,0 +1,6 @@ +#ifndef KVM__STOP_H +#define KVM__STOP_H + +int kvm_cmd_stop(int argc, const char **argv, const char *prefix); + +#endif diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index 5f3cbbf..722b24b 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -20,6 +20,7 @@ #define SIGKVMPAUSE (SIGRTMIN + 1) #define SIGKVMADDMEM (SIGRTMIN + 2) #define SIGKVMDELMEM (SIGRTMIN + 3) +#define SIGKVMSTOP (SIGRTMIN + 4) struct kvm { int sys_fd; /* For system ioctls(), i.e. /dev/kvm */ diff --git a/tools/kvm/kvm-cmd.c b/tools/kvm/kvm-cmd.c index a954a61..d85cb0a 100644 --- a/tools/kvm/kvm-cmd.c +++ b/tools/kvm/kvm-cmd.c @@ -10,6 +10,7 @@ #include "kvm/builtin-balloon.h" #include "kvm/builtin-list.h" #include "kvm/builtin-version.h" +#include "kvm/builtin-stop.h" #include "kvm/builtin-help.h" #include "kvm/kvm-cmd.h" #include "kvm/builtin-run.h" @@ -20,6 +21,7 @@ struct cmd_struct kvm_commands[] = { { "balloon", kvm_cmd_balloon, NULL, 0 }, { "list", kvm_cmd_list, NULL, 0 }, { "version", kvm_cmd_version, NULL, 0 }, + { "stop", kvm_cmd_stop, NULL, 0 }, { "help", kvm_cmd_help, NULL, 0 }, { "run", kvm_cmd_run, kvm_run_help, 0 }, { NULL, NULL, NULL, 0 }, -- 1.7.6