From patchwork Tue Apr 19 14:29:04 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Joshi X-Patchwork-Id: 718301 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3JETGWT030194 for ; Tue, 19 Apr 2011 14:29:16 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752438Ab1DSO3N (ORCPT ); Tue, 19 Apr 2011 10:29:13 -0400 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=ew+Cp0uIqnLDjZS8MXeR7/AHs4UgysGlE+IAs4zIH0M=; b=GT5nw2O8/YOfShbPl9PpNgg8610RmW+FwoMUjNyfKe1SpEEqoxtSVNwH26BdqwK6K6 HqGmOg9E7i3gth5d6fqLAesmpGmVEHNt37Fh8qaFQRPAlB9wcZsyfMrQrGBQZohvKjg9 ZaWheLRfJTvvuvDfOVQ42nsnCcjhi4Jxi6+wA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=m2fg4Hkqysq0Y7aSaSpb7XSPvhBvdTbE7O+CTUSMLEAnBS99Fsh4zvGtDz/MfCtWqY 7blJwAuEgV7KfBfoqrcVqkQYN1fNcx3bozFu2RhaS5hY6N6RSCBtxraV3EamlOOH+jGJ CmvhxpkVSoQ2cejGL7FzzVCLIVXy30TEe294o= Received: by 10.227.208.207 with SMTP id gd15mr6572400wbb.93.1303223351429; Tue, 19 Apr 2011 07:29:11 -0700 (PDT) Received: from prasad-kvm.localdomain (pineapple.rdg.ac.uk [134.225.206.123]) by mx.google.com with ESMTPS id e13sm3927637wbi.23.2011.04.19.07.29.10 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 19 Apr 2011 07:29:10 -0700 (PDT) Received: by prasad-kvm.localdomain (Postfix, from userid 1000) id 6291A26E006E; Tue, 19 Apr 2011 15:29:06 +0100 (BST) From: Prasad Joshi To: 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 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> X-Mailer: git-send-email 1.7.1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Tue, 19 Apr 2011 14:29:16 +0000 (UTC) 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 Tested-by: Ingo Molnar --- 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))