All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pekka Enberg <penberg@kernel.org>
To: kvm@vger.kernel.org
Cc: Pekka Enberg <penberg@kernel.org>,
	Asias He <asias.hejun@gmail.com>,
	Cyrill Gorcunov <gorcunov@gmail.com>,
	Prasad Joshi <prasadjoshi124@gmail.com>,
	Sasha Levin <levinsasha928@gmail.com>,
	Ingo Molnar <mingo@elte.hu>
Subject: [RFT/PATCH v2] kvm tools: Add read-only support for block devices
Date: Thu, 14 Apr 2011 21:02:05 +0300	[thread overview]
Message-ID: <1302804125-14281-1-git-send-email-penberg@kernel.org> (raw)

Add support for booting guests to host block devices in read-only mode.

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: Sasha Levin <levinsasha928@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
---
 tools/kvm/disk-image.c |   39 +++++++++++++++++++++++++++++++--------
 1 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/tools/kvm/disk-image.c b/tools/kvm/disk-image.c
index fff71b4..f5e11b9 100644
--- a/tools/kvm/disk-image.c
+++ b/tools/kvm/disk-image.c
@@ -4,6 +4,9 @@
 #include "kvm/qcow.h"
 #include "kvm/util.h"
 
+#include <linux/fs.h>	/* for BLKGETSIZE64 */
+
+#include <sys/ioctl.h>
 #include <sys/types.h>
 #include <inttypes.h>
 #include <sys/mman.h>
@@ -110,24 +113,44 @@ static struct disk_image_operations raw_image_ro_mmap_ops = {
 	.close			= raw_image__close_sector_ro_mmap,
 };
 
-static struct disk_image *raw_image__probe(int fd, bool readonly)
+static struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
 {
-	struct stat st;
+	if (readonly)
+		return disk_image__new_readonly(fd, st->st_size, &raw_image_ro_mmap_ops);
+	else
+		return disk_image__new(fd, st->st_size, &raw_image_ops);
+}
 
-	if (fstat(fd, &st) < 0)
+static struct disk_image *blkdev__probe(const char *filename, struct stat *st)
+{
+	uint64_t size;
+	int fd;
+
+	if (!S_ISBLK(st->st_mode))
 		return NULL;
 
-	if (readonly)
-		return disk_image__new_readonly(fd, st.st_size, &raw_image_ro_mmap_ops);
-	else
-		return disk_image__new(fd, st.st_size, &raw_image_ops);
+	fd		= open(filename, O_RDONLY);
+	if (fd < 0)
+		return NULL;
+
+	if (ioctl(fd, BLKGETSIZE64, &size) < 0)
+		return NULL;
+
+	return disk_image__new_readonly(fd, size, &raw_image_ro_mmap_ops);
 }
 
 struct disk_image *disk_image__open(const char *filename, bool readonly)
 {
 	struct disk_image *self;
+	struct stat st;
 	int fd;
 
+	if (stat(filename, &st) < 0)
+		return NULL;
+
+	if (S_ISBLK(st.st_mode))
+		return blkdev__probe(filename, &st);
+
 	fd		= open(filename, readonly ? O_RDONLY : O_RDWR);
 	if (fd < 0)
 		return NULL;
@@ -136,7 +159,7 @@ struct disk_image *disk_image__open(const char *filename, bool readonly)
 	if (self)
 		return self;
 
-	self = raw_image__probe(fd, readonly);
+	self = raw_image__probe(fd, &st, readonly);
 	if (self)
 		return self;
 
-- 
1.7.0.4


             reply	other threads:[~2011-04-14 18:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-14 18:02 Pekka Enberg [this message]
2011-04-15 10:31 ` [RFT/PATCH v2] kvm tools: Add read-only support for block devices Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1302804125-14281-1-git-send-email-penberg@kernel.org \
    --to=penberg@kernel.org \
    --cc=asias.hejun@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=levinsasha928@gmail.com \
    --cc=mingo@elte.hu \
    --cc=prasadjoshi124@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.