Skip to contentSkip to conversation

kvm tools: Move disk_image into virtio-blk

PATCH New Sasha Levin 1 participant 3 messages May 5, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 kvm tools: Move disk_image into virtio-blk

There may be multiple disk images on a running guest, each associated with a virtio-blk.

Move disk_image into virtio-blk in preperation for multiple disk images.

---
 tools/kvm/include/kvm/kvm.h        |    1 -
 tools/kvm/include/kvm/virtio-blk.h |    4 +++-
 tools/kvm/kvm-run.c                |    9 ++++-----
 tools/kvm/virtio-blk.c             |   13 ++++++++-----
 4 files changed, 15 insertions(+), 12 deletions(-)
tools/kvm/include/kvm/kvm.h +0 -1
index 327a1ab..d239f49 100644
@@ -16,7 +16,6 @@ struct kvm {
 
 	int			nrcpus;		/* Number of cpus to run */
 
-	struct disk_image	*disk_image;
 	uint64_t		ram_size;
 	void			*ram_start;
 
tools/kvm/include/kvm/virtio-blk.h +3 -1
index f82bbc7..f21a0e4 100644
@@ -1,8 +1,10 @@
 #ifndef KVM__BLK_VIRTIO_H
 #define KVM__BLK_VIRTIO_H
 
+#include "kvm/disk-image.h"
+
 struct kvm;
 
-void virtio_blk__init(struct kvm *self);
+void virtio_blk__init(struct kvm *self, struct disk_image *disk);
 
 #endif /* KVM__BLK_VIRTIO_H */
tools/kvm/kvm-run.c +4 -5
index 6fd46ed..b92232d 100644
@@ -409,9 +409,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		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)
+		struct disk_image *disk = disk_image__open(image_filename, readonly_image);
+		if (!disk)
 			die("unable to load disk image %s", image_filename);
+
+		virtio_blk__init(kvm, disk);
 	}
 	free(hi);
 
@@ -429,8 +431,6 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	pci__init();
 
-	virtio_blk__init(kvm);
-
 	virtio_console__init(kvm);
 
 	if (virtio_rng)
@@ -487,7 +487,6 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 			exit_code	= 1;
 	}
 
-	disk_image__close(kvm->disk_image);
 	kvm__delete(kvm);
 
 	if (!exit_code)
tools/kvm/virtio-blk.c +8 -5
index 9034abd..d7bda5f 100644
@@ -28,6 +28,7 @@ struct blk_device {
 	pthread_mutex_t			mutex;
 
 	struct virtio_blk_config	blk_config;
+	struct disk_image			*disk;
 	uint32_t			host_features;
 	uint32_t			guest_features;
 	uint16_t			config_vector;
@@ -130,11 +131,11 @@ static bool virtio_blk_do_io_request(struct kvm *self, struct virt_queue *queue)
 
 	switch (req->type) {
 	case VIRTIO_BLK_T_IN:
-		block_cnt = disk_image__read_sector_iov(self->disk_image, req->sector, iov + 1, in + out - 2);
+		block_cnt = disk_image__read_sector_iov(blk_device.disk, req->sector, iov + 1, in + out - 2);
 
 		break;
 	case VIRTIO_BLK_T_OUT:
-		block_cnt = disk_image__write_sector_iov(self->disk_image, req->sector, iov + 1, in + out - 2);
+		block_cnt = disk_image__write_sector_iov(blk_device.disk, req->sector, iov + 1, in + out - 2);
 
 		break;
 
@@ -243,12 +244,14 @@ static struct pci_device_header virtio_blk_pci_device = {
 
 #define PCI_VIRTIO_BLK_DEVNUM 1
 
-void virtio_blk__init(struct kvm *self)
+void virtio_blk__init(struct kvm *self, struct disk_image *disk)
 {
-	if (!self->disk_image)
+	if (!disk)
 		return;
 
-	blk_device.blk_config.capacity = self->disk_image->size / SECTOR_SIZE;
+	blk_device.disk = disk;
+
+	blk_device.blk_config.capacity = disk->size / SECTOR_SIZE;
 
 	pci__register(&virtio_blk_pci_device, PCI_VIRTIO_BLK_DEVNUM);
 
Signature · 1 line
1.7.5.rc3
#1 lore raw

PATCH v3 2/3 kvm tools: Add support for multiple virtio-blk

Add support for multiple blk_devices by un-globalizing the current blk_device and allow multiple blk_devices.

---
 tools/kvm/include/kvm/ioport.h |    2 +-
 tools/kvm/mptable.c            |   56 ++++++------
 tools/kvm/virtio-blk.c         |  193 +++++++++++++++++++++++++---------------
 3 files changed, 154 insertions(+), 97 deletions(-)
tools/kvm/include/kvm/ioport.h +1 -1
index 6020124..98a880f 100644
@@ -7,7 +7,7 @@
 /* some ports we reserve for own use */
 #define IOPORT_DBG			0xe0
 #define IOPORT_VIRTIO_BLK		0xc200	/* Virtio block device */
-#define IOPORT_VIRTIO_BLK_SIZE		256
+#define IOPORT_VIRTIO_BLK_SIZE		0x200
 #define IOPORT_VIRTIO_CONSOLE		0xd200	/* Virtio console device */
 #define IOPORT_VIRTIO_CONSOLE_SIZE	256
 #define IOPORT_VIRTIO_NET		0xe200	/* Virtio network device */
tools/kvm/mptable.c +30 -26

80 lines not on this page · raw message

tools/kvm/virtio-blk.c +123 -70

331 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3
#2 lore raw

PATCH v3 3/3 kvm tools: Add cmdline options for loading multiple images

In reply to Sasha Levin

Introduced new syntax for loading disk images.
Example:

./kvm run --image image1.img,ro --image image2.img

Will load image1.img with read only, and image2.img as read/write.

---
 tools/kvm/include/kvm/parse-options.h |   35 ++++++++++++++++++++++++
 tools/kvm/kvm-run.c                   |   48 ++++++++++++++++++++++++---------
 2 files changed, 70 insertions(+), 13 deletions(-)
tools/kvm/include/kvm/parse-options.h +35 -0
index 5d8c618..2d5c99e 100644
@@ -138,6 +138,41 @@ intptr_t defval;
 	.help = (h)                         \
 }
 
+#define OPT_CALLBACK(s, l, v, a, h, f)      \
+{					    \
+	.type = OPTION_CALLBACK,	    \
+	.short_name = (s),		    \
+	.long_name = (l),		    \
+	.value = (v),			    \
+	(a),				    \
+	.help = (h),			    \
+	.callback = (f)			    \
+}
+
+#define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
+{					    \
+	.type = OPTION_CALLBACK,	    \
+	.short_name = (s),		    \
+	.long_name = (l),		    \
+	.value = (v),			    \
+	(a),				    \
+	.help = (h),			    \
+	.callback = (f),		    \
+	.flags = PARSE_OPT_NOARG	    \
+}
+
+#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
+{					    \
+	.type = OPTION_CALLBACK,	    \
+	.short_name = (s),		    \
+	.long_name = (l),		    \
+	.value = (v), (a),		    \
+	.help = (h),			    \
+	.callback = (f),		    \
+	.defval = (intptr_t)d,		    \
+	.flags = PARSE_OPT_LASTARG_DEFAULT  \
+}
+
 #define OPT_END() { .type = OPTION_END }
 
 enum {
tools/kvm/kvm-run.c +35 -13

94 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3