From e36e90124cd7de6911218b15ae090574e1c0c6e0 Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Wed, 5 Sep 2012 10:32:03 +0200 Subject: [PATCH] kvm tools: virtio-9p cleanup Sort out init/exit calls, move parser into the 9p code and make sure rootfs config is initialized before virtio-9p (or any other init func) is called. Signed-off-by: Sasha Levin Signed-off-by: Pekka Enberg --- tools/kvm/builtin-run.c | 67 ++++-------------------------- tools/kvm/include/kvm/virtio-9p.h | 3 ++ tools/kvm/virtio/9p.c | 68 +++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 58 deletions(-) diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index 8209cf19df89..5ddffaad57e2 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -77,42 +77,12 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset) { char path[PATH_MAX]; struct stat st; - struct kvm *kvm = opt->ptr; - - if (stat(arg, &st) == 0 && - S_ISDIR(st.st_mode)) { - char tmp[PATH_MAX]; - - if (kvm->cfg.using_rootfs) - die("Please use only one rootfs directory atmost"); - - if (realpath(arg, tmp) == 0 || - virtio_9p__register(kvm, tmp, "/dev/root") < 0) - die("Unable to initialize virtio 9p"); - kvm->cfg.using_rootfs = 1; - return 0; - } snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); - if (stat(path, &st) == 0 && - S_ISDIR(st.st_mode)) { - char tmp[PATH_MAX]; - - if (kvm->cfg.using_rootfs) - die("Please use only one rootfs directory atmost"); - - if (realpath(path, tmp) == 0 || - virtio_9p__register(kvm, tmp, "/dev/root") < 0) - die("Unable to initialize virtio 9p"); - if (virtio_9p__register(kvm, "/", "hostfs") < 0) - die("Unable to initialize virtio 9p"); - kvm_setup_resolv(arg); - kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; - kvm->cfg.custom_rootfs_name = arg; - return 0; - } - + if ((stat(arg, &st) == 0 && S_ISDIR(st.st_mode)) || + (stat(path, &st) == 0 && S_ISDIR(st.st_mode))) + return virtio_9p_img_name_parser(opt, arg, unset); return disk_img_name_parser(opt, arg, unset); } @@ -121,29 +91,6 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper = KVM_RUN_SANDBOX; } -static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) -{ - char *tag_name; - char tmp[PATH_MAX]; - - /* - * 9p dir can be of the form dirname,tag_name or - * just dirname. In the later case we use the - * default tag name - */ - tag_name = strstr(arg, ","); - if (tag_name) { - *tag_name = '\0'; - tag_name++; - } - if (realpath(arg, tmp)) { - if (virtio_9p__register(kvm, tmp, tag_name) < 0) - die("Unable to initialize virtio 9p"); - } else - die("Failed resolving 9p path"); - return 0; -} - #define BUILD_OPTIONS(name, cfg, kvm) \ struct option name[] = { \ OPT_GROUP("Basic options:"), \ @@ -167,7 +114,7 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, i Number Generator"), \ OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", \ "Enable virtio 9p to share files between host and \ - guest", virtio_9p_rootdir_parser, NULL), \ + guest", virtio_9p_rootdir_parser, kvm), \ OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or \ hv", "Console to use"), \ OPT_STRING('\0', "dev", &(cfg)->dev, "device_file", \ @@ -792,7 +739,11 @@ static int kvm_cmd_run_init(int argc, const char **argv) goto fail; } - virtio_9p__init(kvm); + r = virtio_9p__init(kvm); + if (r < 0) { + pr_err("virtio_9p__init() failed with error %d\n", r); + goto fail; + } r = virtio_net__init(kvm); if (r < 0) { diff --git a/tools/kvm/include/kvm/virtio-9p.h b/tools/kvm/include/kvm/virtio-9p.h index cb590d185db3..19ffe505a74c 100644 --- a/tools/kvm/include/kvm/virtio-9p.h +++ b/tools/kvm/include/kvm/virtio-9p.h @@ -3,6 +3,7 @@ #include "kvm/virtio.h" #include "kvm/pci.h" #include "kvm/threadpool.h" +#include "kvm/parse-options.h" #include #include @@ -65,6 +66,8 @@ struct p9_pdu { struct kvm; +int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset); +int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset); int virtio_9p__register(struct kvm *kvm, const char *root, const char *tag_name); int virtio_9p__init(struct kvm *kvm); int virtio_p9_pdu_readf(struct p9_pdu *pdu, const char *fmt, ...); diff --git a/tools/kvm/virtio/9p.c b/tools/kvm/virtio/9p.c index c3f5280305c4..0705d79f32d7 100644 --- a/tools/kvm/virtio/9p.c +++ b/tools/kvm/virtio/9p.c @@ -5,6 +5,7 @@ #include "kvm/irq.h" #include "kvm/virtio-9p.h" #include "kvm/guest_compat.h" +#include "kvm/builtin-setup.h" #include #include @@ -1325,6 +1326,73 @@ struct virtio_ops p9_dev_virtio_ops = (struct virtio_ops) { .get_size_vq = get_size_vq, }; +int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) +{ + char *tag_name; + char tmp[PATH_MAX]; + struct kvm *kvm = opt->ptr; + + /* + * 9p dir can be of the form dirname,tag_name or + * just dirname. In the later case we use the + * default tag name + */ + tag_name = strstr(arg, ","); + if (tag_name) { + *tag_name = '\0'; + tag_name++; + } + if (realpath(arg, tmp)) { + if (virtio_9p__register(kvm, tmp, tag_name) < 0) + die("Unable to initialize virtio 9p"); + } else + die("Failed resolving 9p path"); + return 0; +} + +int virtio_9p_img_name_parser(const struct option *opt, const char *arg, int unset) +{ + char path[PATH_MAX]; + struct stat st; + struct kvm *kvm = opt->ptr; + + if (stat(arg, &st) == 0 && + S_ISDIR(st.st_mode)) { + char tmp[PATH_MAX]; + + if (kvm->cfg.using_rootfs) + die("Please use only one rootfs directory atmost"); + + if (realpath(arg, tmp) == 0 || + virtio_9p__register(kvm, tmp, "/dev/root") < 0) + die("Unable to initialize virtio 9p"); + kvm->cfg.using_rootfs = 1; + return 0; + } + + snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); + + if (stat(path, &st) == 0 && + S_ISDIR(st.st_mode)) { + char tmp[PATH_MAX]; + + if (kvm->cfg.using_rootfs) + die("Please use only one rootfs directory atmost"); + + if (realpath(path, tmp) == 0 || + virtio_9p__register(kvm, tmp, "/dev/root") < 0) + die("Unable to initialize virtio 9p"); + if (virtio_9p__register(kvm, "/", "hostfs") < 0) + die("Unable to initialize virtio 9p"); + kvm_setup_resolv(arg); + kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; + kvm->cfg.custom_rootfs_name = arg; + return 0; + } + + return -1; +} + int virtio_9p__init(struct kvm *kvm) { struct p9_dev *p9dev; -- 2.39.5