{
/* 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);
#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)
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;