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 <levinsasha...@gmail.com>
---
 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 6e6fbf5..183aa42 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 cb590d1..19ffe50 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 <sys/types.h>
 #include <dirent.h>
@@ -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 c3f5280..0705d79 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 <stdio.h>
 #include <stdlib.h>
@@ -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;
-- 
1.7.12

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to