Skip to contentSkip to conversation

kvm tools: Add read-only support for block devices

PATCH In review Pekka Enberg 2 participants 2 messages Apr 14, 2011 – Apr 15, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kvm tools: Add read-only support for block devices

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

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Prasad Joshi <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 tools/kvm/disk-image.c |   39 +++++++++++++++++++++++++++++++--------
 1 files changed, 31 insertions(+), 8 deletions(-)
tools/kvm/disk-image.c +31 -8
index fff71b4..f5e11b9 100644
@@ -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;
 
Signature · 1 line
1.7.0.4
Tested-by on v2 1/1
#1 lore raw
Quoted 11 lines · Pekka Enberg

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

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Prasad Joshi <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 tools/kvm/disk-image.c |   39 +++++++++++++++++++++++++++++++--------
 1 files changed, 31 insertions(+), 8 deletions(-)

Nice patch - booting into the host's userspace works out of box now. The following disk-image-less command:

   ./kvm run ../../arch/x86/boot/bzImage --readonly --image=/dev/sda --params="root=/dev/vda1"

has booted all the way into the host's Fedora Rawhide userspace:

===================================
Welcome to Fedora release 16 (Rawhide)!

[...]

Fedora release 16 (Rawhide)
Kernel 2.6.39-rc3-tip+ on an x86_64 (ttyS0)

aldebaran login:
===================================

I ran this as unprivileged user (who had read access to the partitions in question).

This is a very useful feature for testing kernels - i can test any random Linux box's userspace via KVM, without having to copy an image there! :-)

Thanks,

Ingo