]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm: Cleanup disk geometry setup code
authorPekka Enberg <penberg@kernel.org>
Thu, 6 Jan 2011 08:22:09 +0000 (10:22 +0200)
committerPekka Enberg <penberg@kernel.org>
Thu, 6 Jan 2011 08:36:39 +0000 (10:36 +0200)
Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/blk-virtio.c
tools/kvm/disk-image.c

index da295bc0d066a69656183fedb09d495465c33eec..737c3330bb695f5494320119c96704d43286b8a4 100644 (file)
@@ -234,10 +234,14 @@ void blk_virtio__init(struct kvm *self)
 {
        /* update disk geometry */
        if (self->disk_image) {
-               device.blk_config.capacity              = self->disk_image->size;
-               device.blk_config.geometry.cylinders    = self->disk_image->cylinders;
-               device.blk_config.geometry.heads        = self->disk_image->heads;
-               device.blk_config.geometry.sectors      = self->disk_image->sectors;
+               device.blk_config       = (struct virtio_blk_config) {
+                       .capacity               = self->disk_image->size,
+                       .geometry               = (struct virtio_blk_geometry) {
+                               .cylinders              = self->disk_image->cylinders,
+                               .heads                  = self->disk_image->heads,
+                               .sectors                = self->disk_image->sectors,
+                       },
+               };
        }
 
        pci__register(&blk_virtio_pci_device, 1);
index 7ac16e4a2ef931bb98af14bd1112330ec1879461..bc9189891de5875cd750352296c05eed1406d83b 100644 (file)
@@ -3,6 +3,7 @@
 #include "kvm/util.h"
 
 #include <sys/types.h>
+#include <inttypes.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <stddef.h>
 #define SECTOR_SHIFT           9
 #define SECTOR_SIZE            (1UL << SECTOR_SHIFT)
 
+static void setup_geometry(struct disk_image *self)
+{
+       int cylinders, heads, sectors;
+       uint64_t total_sects;
+
+       /*
+        * Set the standart disk geometry of the image.
+        *
+        * Real disk example:
+        *
+        *   Disk /dev/sda: 500.1 GB, 500107862016 bytes
+        *   255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
+        */
+       cylinders       = (self->size >> SECTOR_SHIFT) / 16383;
+       if (cylinders > 16383)
+               cylinders       = 16383;
+       else if (cylinders < 2)
+               cylinders       = 2;
+
+       heads           = (self->size >> SECTOR_SHIFT) / cylinders;
+       if (heads > 255)
+               heads           = 255;
+       else if (heads < 1)
+               heads           = 1;
+
+       sectors         = (self->size >> SECTOR_SHIFT) / cylinders / heads;
+       if (sectors > 255)
+               sectors         = 255;
+       else if (sectors < 1)
+               sectors         = 1;
+
+       self->sectors   = sectors;
+       self->heads     = heads;
+       self->cylinders = cylinders;
+
+       total_sects     = self->sectors * self->heads * self->cylinders;
+
+       if (total_sects != self->size >> SECTOR_SHIFT)
+               warning("Geometry information advertises %" PRIu64 " total sectors but raw image size has %" PRIu64 " sectors",
+                               total_sects, self->size >> SECTOR_SHIFT);
+}
+
 struct disk_image *disk_image__open(const char *filename)
 {
        struct disk_image *self;
        struct stat st;
-       int cylinders, heads, sectors;
 
        self            = malloc(sizeof *self);
        if (!self)
@@ -36,35 +78,10 @@ struct disk_image *disk_image__open(const char *filename)
        if (self->mmap == MAP_FAILED)
                goto failed_close_fd;
 
-       /*
-        * set the standart disk geometry of the image
-        *
-        * real disk example
-        * Disk /dev/sda: 500.1 GB, 500107862016 bytes
-        * 255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
-        */
-       cylinders = (self->size >> SECTOR_SHIFT) / 16383;
-       if (cylinders > 16383)
-               cylinders = 16383;
-       else if (cylinders < 2)
-               cylinders = 2;
-       heads = (self->size >> SECTOR_SHIFT) / cylinders;
-       if (heads > 255)
-               heads = 255;
-       else if (heads < 1)
-               heads = 1;
-       sectors = (self->size >> SECTOR_SHIFT) / cylinders / heads;
-       if (sectors > 255)
-               sectors = 255;
-       else if (sectors < 1)
-               sectors = 1;
-
-       self->sectors   = sectors;
-       self->heads     = heads;
-       self->cylinders = cylinders;
+       setup_geometry(self);
 
        info("block image geometry: sectors: %d heads: %d cylinders: %d",
-               sectors, heads, cylinders);
+               self->sectors, self->heads, self->cylinders);
 
        return self;