kvm: Cleanup disk geometry setup code

Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Pekka Enberg
2015-06-01 16:39:41 +01:00
committed by Will Deacon
parent ca7c891bfe
commit 33d3390123
2 changed files with 53 additions and 32 deletions
+8 -4
View 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);
+45 -28
View 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>
@@ -13,11 +14,52 @@
#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;