mirror of
https://github.com/clearlinux/kvmtool.git
synced 2026-09-04 20:51:28 +00:00
kvm tools: Improve 'kvm stop' parameters
kvm stop now uses the git option parser to parse parameters. Added option to stop instance by PID and stop all instances. Improved usage message. Signed-off-by: Sasha Levin <levinsasha928@gmail.com> Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
+30
-10
@@ -8,15 +8,33 @@
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
static bool all;
|
||||
static u64 instance_pid;
|
||||
static const char *instance_name;
|
||||
|
||||
static const char * const stop_usage[] = {
|
||||
"kvm stop <instance name>",
|
||||
"kvm stop [--all] [-n name] [-p pid]",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct option stop_options[] = {
|
||||
OPT_GROUP("General options:"),
|
||||
OPT_BOOLEAN('a', "all", &all, "Stop all instances"),
|
||||
OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
|
||||
OPT_U64('p', "pid", &instance_pid, "Instance pid"),
|
||||
OPT_END()
|
||||
};
|
||||
|
||||
static void parse_stop_options(int argc, const char **argv)
|
||||
{
|
||||
while (argc != 0) {
|
||||
argc = parse_options(argc, argv, stop_options, stop_usage,
|
||||
PARSE_OPT_STOP_AT_NON_OPTION);
|
||||
if (argc != 0)
|
||||
kvm_stop_help();
|
||||
}
|
||||
}
|
||||
|
||||
void kvm_stop_help(void)
|
||||
{
|
||||
usage_with_options(stop_usage, stop_options);
|
||||
@@ -29,18 +47,20 @@ static int do_stop(const char *name, int pid)
|
||||
|
||||
int kvm_cmd_stop(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
int pid;
|
||||
parse_stop_options(argc, argv);
|
||||
|
||||
if (argc != 1)
|
||||
if (all)
|
||||
return kvm__enumerate_instances(do_stop);
|
||||
|
||||
if (instance_name == NULL &&
|
||||
instance_pid == 0)
|
||||
kvm_stop_help();
|
||||
|
||||
if (strcmp(argv[0], "all") == 0) {
|
||||
return kvm__enumerate_instances(do_stop);
|
||||
}
|
||||
if (instance_name)
|
||||
instance_pid = kvm__get_pid_by_instance(argv[0]);
|
||||
|
||||
pid = kvm__get_pid_by_instance(argv[0]);
|
||||
if (pid < 0)
|
||||
die("Failed locating instance name");
|
||||
if (instance_pid <= 0)
|
||||
die("Failed locating instance");
|
||||
|
||||
return kill(pid, SIGKVMSTOP);
|
||||
return kill(instance_pid, SIGKVMSTOP);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user