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.

Signed-off-by: Sasha Levin <[email protected]>
---
 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(-)

diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index 327a1ab..d239f49 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -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;
 
diff --git a/tools/kvm/include/kvm/virtio-blk.h 
b/tools/kvm/include/kvm/virtio-blk.h
index f82bbc7..f21a0e4 100644
--- a/tools/kvm/include/kvm/virtio-blk.h
+++ b/tools/kvm/include/kvm/virtio-blk.h
@@ -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 */
diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c
index 6fd46ed..b92232d 100644
--- a/tools/kvm/kvm-run.c
+++ b/tools/kvm/kvm-run.c
@@ -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)
diff --git a/tools/kvm/virtio-blk.c b/tools/kvm/virtio-blk.c
index 9034abd..d7bda5f 100644
--- a/tools/kvm/virtio-blk.c
+++ b/tools/kvm/virtio-blk.c
@@ -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);
 
-- 
1.7.5.rc3

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to