From: Prasad Joshi Subject: [PATCH V4] kvm tool: Use the root partition of the host to boot the guest machine Date: Tue, 19 Apr 2011 15:29:04 +0100 Message-ID: <1303223344-3515-1-git-send-email-prasadjoshi124@gmail.com> Cc: mingo@elte.hu, kvm@vger.kernel.org, penberg@kernel.org, asias.hejun@gmail.com, gorcunov@gmail.com, levinsasha928@gmail.com, chaitanyakulkarni15@gmail.com To: prasadjoshi124@gmail.com Return-path: Received: from mail-wy0-f174.google.com ([74.125.82.174]:54826 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752079Ab1DSO3M (ORCPT ); Tue, 19 Apr 2011 10:29:12 -0400 Received: by wya21 with SMTP id 21so4858150wya.19 for ; Tue, 19 Apr 2011 07:29:11 -0700 (PDT) Sender: kvm-owner@vger.kernel.org List-ID: The kvm run command should automatically pickup the image file to boot if one is not explicitly specified. Changes since V1: - Deallocate variable 'line' when fopen(/proc/mounts) fails Changes since V2: - Instead of searching from /proc/mounts, use the device file /dev/root to detect the device file for root file system - Added a helper function get_host_image() to return the image to be used to boot the virtual machine. Changes since V3: - The link '/dev/root' might be missing on some of the systems. Switched back to using /proc/mounts file. - Some simple reformatting Signed-off-by: Prasad Joshi --- tools/kvm/kvm-run.c | 109 +++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 98 insertions(+), 11 deletions(-) diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index 17fef20..c3da21f 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -9,6 +9,7 @@ #include #include #include +#include /* user defined header files */ #include @@ -214,12 +215,91 @@ static const char *find_kernel(void) return NULL; } +static int root_device(char *dev, long *part) +{ + FILE *fp; + char *line; + int tmp; + size_t nr_read; + char device[PATH_MAX]; + char mnt_pt[PATH_MAX]; + char resolved_path[PATH_MAX]; + char *p; + struct stat st; + + fp = fopen("/proc/mounts", "r"); + if (!fp) + return -1; + + line = NULL; + tmp = 0; + while (!feof(fp)) { + if (getline(&line, &nr_read, fp) < 0) + break; + sscanf(line, "%s %s", device, mnt_pt); + if (!strncmp(device, "/dev", 4) && !strcmp(mnt_pt, "/")) { + tmp = 1; + break; + } + } + fclose(fp); + free(line); + + if (!tmp) + return -1; + + /* get the absolute path */ + if (!realpath(device, resolved_path)) + return -1; + + /* find the partition number */ + p = resolved_path; + while (*p) { + if (isdigit(*p)) { + strncpy(dev, resolved_path, p - resolved_path); + *part = atol(p); + break; + } + p++; + } + + /* verify the device path */ + if (stat(dev, &st) < 0) + return -1; + return 0; +} + +static char *host_image(char *cmd_line, size_t size) +{ + char *t; + char device[PATH_MAX]; + long part = 0; + + t = malloc(PATH_MAX); + if (!t) + return NULL; + + /* check for the root file system */ + if (root_device(device, &part) < 0) { + free(t); + return NULL; + } + strncpy(t, device, PATH_MAX); + if (!strstr(cmd_line, "root=")) { + char tmp[PATH_MAX]; + snprintf(tmp, sizeof(tmp), "root=/dev/vda%ld rw ", part); + strlcat(cmd_line, tmp, size); + } + return t; +} + int kvm_cmd_run(int argc, const char **argv, const char *prefix) { static char real_cmdline[2048]; int exit_code = 0; int i; struct virtio_net_parameters net_params; + char *hi; signal(SIGALRM, handle_sigalrm); signal(SIGQUIT, handle_sigquit); @@ -288,22 +368,29 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) kvm = kvm__init(kvm_dev, ram_size); + memset(real_cmdline, 0, sizeof(real_cmdline)); + strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); + if (kernel_cmdline) + strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); + + hi = NULL; + if (!image_filename) { + hi = host_image(real_cmdline, sizeof(real_cmdline)); + if (hi) { + image_filename = hi; + readonly_image = true; + } + } + + if (!strstr(real_cmdline, "root=")) + strlcat(real_cmdline, "root=/dev/vda rw ", sizeof(real_cmdline)); + if (image_filename) { kvm->disk_image = disk_image__open(image_filename, readonly_image); if (!kvm->disk_image) die("unable to load disk image %s", image_filename); } - - strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); - if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { - strlcat(real_cmdline, "root=/dev/vda rw ", - sizeof(real_cmdline)); - } - - if (kernel_cmdline) { - strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); - real_cmdline[sizeof(real_cmdline)-1] = '\0'; - } + free(hi); if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, real_cmdline)) -- 1.7.1