Files
kvmtool/kvm-cmd.c
T
Pekka Enberg 282113fd51 kvm tools: Introduce 'kvm setup' command
This patch implements 'kvm setup' command that can be used to setup a guest
filesystem that shares system libraries and binaries from host filesystem in
read-only mode.

You can setup a new shared rootfs guest with:

  ./kvm setup -n default

and launch it with:

  ./kvm run --9p /,hostfs -p "init=virt/init" -d ~/.kvm-tools/default/

We want to teach 'kvm run' to be able to launch guest filesystems by name in
the future. Furthermore, 'kvm run' should setup a 'default' filesystem and use
it by default unless the user specifies otherwise.

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:47 +01:00

92 lines
2.3 KiB
C

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
/* user defined header files */
#include "kvm/builtin-debug.h"
#include "kvm/builtin-pause.h"
#include "kvm/builtin-resume.h"
#include "kvm/builtin-balloon.h"
#include "kvm/builtin-list.h"
#include "kvm/builtin-version.h"
#include "kvm/builtin-setup.h"
#include "kvm/builtin-stop.h"
#include "kvm/builtin-stat.h"
#include "kvm/builtin-help.h"
#include "kvm/kvm-cmd.h"
#include "kvm/builtin-run.h"
#include "kvm/util.h"
struct cmd_struct kvm_commands[] = {
{ "pause", kvm_cmd_pause, kvm_pause_help, 0 },
{ "resume", kvm_cmd_resume, kvm_resume_help, 0 },
{ "debug", kvm_cmd_debug, kvm_debug_help, 0 },
{ "balloon", kvm_cmd_balloon, kvm_balloon_help, 0 },
{ "list", kvm_cmd_list, kvm_list_help, 0 },
{ "version", kvm_cmd_version, NULL, 0 },
{ "--version", kvm_cmd_version, NULL, 0 },
{ "stop", kvm_cmd_stop, kvm_stop_help, 0 },
{ "stat", kvm_cmd_stat, kvm_stat_help, 0 },
{ "help", kvm_cmd_help, NULL, 0 },
{ "setup", kvm_cmd_setup, kvm_setup_help, 0 },
{ "run", kvm_cmd_run, kvm_run_help, 0 },
{ NULL, NULL, NULL, 0 },
};
/*
* kvm_get_command: Searches the command in an array of the commands and
* returns a pointer to cmd_struct if a match is found.
*
* Input parameters:
* command: Array of possible commands. The last entry in the array must be
* NULL.
* cmd: A string command to search in the array
*
* Return Value:
* NULL: If the cmd is not matched with any of the command in the command array
* p: Pointer to cmd_struct of the matching command
*/
struct cmd_struct *kvm_get_command(struct cmd_struct *command,
const char *cmd)
{
struct cmd_struct *p = command;
while (p->cmd) {
if (!strcmp(p->cmd, cmd))
return p;
p++;
}
return NULL;
}
int handle_command(struct cmd_struct *command, int argc, const char **argv)
{
struct cmd_struct *p;
const char *prefix = NULL;
int ret = 0;
if (!argv || !*argv) {
p = kvm_get_command(command, "help");
assert(p);
return p->fn(argc, argv, prefix);
}
p = kvm_get_command(command, argv[0]);
if (!p) {
p = kvm_get_command(command, "help");
assert(p);
p->fn(0, NULL, prefix);
return EINVAL;
}
ret = p->fn(argc - 1, &argv[1], prefix);
if (ret < 0) {
if (errno == EPERM)
die("Permission error - are you root?");
}
return ret;
}