[PATCH] kvm tools: Use host kernel image by default

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If user doesn't specify a kernel image for 'kvm run' command, try to look up
host kernel kernel image from /boot directory and use it as guest kernel.

Cc: Asias He <asias.hejun@xxxxxxxxx>
Cc: Cyrill Gorcunov <gorcunov@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxx>
Signed-off-by: Pekka Enberg <penberg@xxxxxxxxxx>
---
 tools/kvm/kvm-run.c |   40 +++++++++++++++++++++++++++++++++-------
 1 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c
index ba02172..4986d6c 100644
--- a/tools/kvm/kvm-run.c
+++ b/tools/kvm/kvm-run.c
@@ -6,6 +6,9 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <termios.h>
+#include <sys/utsname.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 /* user defined header files */
 #include <linux/types.h>
@@ -68,7 +71,7 @@ extern int  active_console;
 static int nrcpus = 1;
 
 static const char * const run_usage[] = {
-	"kvm run [<options>] <kernel image>",
+	"kvm run [<options>] [<kernel image>]",
 	NULL
 };
 
@@ -122,6 +125,28 @@ panic_kvm:
 	return (void *) (intptr_t) 1;
 }
 
+static char host_kernel[PATH_MAX];
+
+static const char *find_host_kernel(void)
+{
+	struct utsname uts;
+	struct stat st;
+
+	if (uname(&uts) < 0)
+		return NULL;
+
+	if (snprintf(host_kernel, PATH_MAX, "/boot/vmlinuz-%s", uts.release) < 0)
+		return NULL;
+
+	if (stat(host_kernel, &st) < 0)
+		return NULL;
+
+	if (!S_ISREG(st.st_mode))
+		return NULL;
+
+	return host_kernel;
+}
+
 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 {
 	static char real_cmdline[2048];
@@ -132,12 +157,6 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	signal(SIGQUIT, handle_sigquit);
 	signal(SIGINT, handle_sigint);
 
-	if (!argv || !*argv) {
-		/* no argument specified */
-		usage_with_options(run_usage, options);
-		return EINVAL;
-	}
-
 	while (argc != 0) {
 		argc = parse_options(argc, argv, options, run_usage,
 				PARSE_OPT_STOP_AT_NON_OPTION);
@@ -158,6 +177,13 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	}
 
+	kernel_filename = find_host_kernel();
+
+	if (!kernel_filename) {
+		usage_with_options(run_usage, options);
+		return EINVAL;
+	}
+
 	if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
 		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
 
-- 
1.7.0.4

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


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux