Skip to contentSkip to conversation

kvm tools: Use host kernel image by default

PATCH In review Pekka Enberg 1 participant 2 messages Apr 12, 2011 1 message not in the archive KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm tools: Use host kernel image by default

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 <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 tools/kvm/kvm-run.c |   40 +++++++++++++++++++++++++++++++++-------
 1 files changed, 33 insertions(+), 7 deletions(-)
tools/kvm/kvm-run.c +33 -7
index ba02172..4986d6c 100644
@@ -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);
 
Signature · 1 line
1.7.0.4
#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Prasad Joshi
(quoted diff) +0 -0
@@ -158,6 +177,13 @@ int kvm_cmd_run(int argc, const char **argv,
 const char *prefix)
 
               }
 
        +       kernel_filename = find_host_kernel();
 

Will this not unset the explicitly set kernel file name?

Fixed, thanks!