kvm tools: Add a wrapper function to open disk images

The patch was suggested by Ingo to move the disk image subsystem code
from the kvm-run.c file. The code to open all of the specified disk
images is now moved to a wrapper function in disk/core.c.

Signed-off-by: Prasad Joshi <prasadjoshi124@gmail.com>
Suggested-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Prasad Joshi
2015-06-01 16:39:44 +01:00
committed by Will Deacon
parent 71fe3129b7
commit c1ed214e17
4 changed files with 41 additions and 8 deletions
+31
View File
@@ -59,6 +59,37 @@ struct disk_image *disk_image__open(const char *filename, bool readonly)
return NULL;
}
struct disk_image **disk_image__open_all(const char **filenames, bool *readonly, int count)
{
struct disk_image **disks;
int i;
if (!count || count > MAX_DISK_IMAGES)
return NULL;
disks = calloc(count, sizeof(*disks));
if (!disks)
return NULL;
for (i = 0; i < count; i++) {
if (!filenames[i])
continue;
disks[i] = disk_image__open(filenames[i], readonly[i]);
if (!disks[i]) {
pr_error("Loading disk image '%s' failed", filenames[i]);
goto error;
}
}
return disks;
error:
for (i = 0; i < count; i++)
disk_image__close(disks[i]);
free(disks);
return NULL;
}
int disk_image__flush(struct disk_image *disk)
{
if (disk->ops->flush)
+2
View File
@@ -23,6 +23,7 @@
#define DISK_IMAGE_MMAP 0
#define DISK_IMAGE_NOMMAP 1
#define MAX_DISK_IMAGES 4
struct disk_image;
@@ -51,6 +52,7 @@ struct disk_image {
};
struct disk_image *disk_image__open(const char *filename, bool readonly);
struct disk_image **disk_image__open_all(const char **filenames, bool *readonly, int count);
struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int mmap);
int disk_image__close(struct disk_image *disk);
int disk_image__flush(struct disk_image *disk);
+1
View File
@@ -32,6 +32,7 @@ struct kvm {
struct interrupt_table interrupt_table;
const char *vmlinux;
struct disk_image **disks;
};
struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size);
+7 -8
View File
@@ -44,7 +44,6 @@
#define MB_SHIFT (20)
#define MIN_RAM_SIZE_MB (64ULL)
#define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT)
#define MAX_DISK_IMAGES 4
static struct kvm *kvm;
static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
@@ -530,15 +529,15 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
if (!strstr(real_cmdline, "root="))
strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
for (i = 0; i < image_count; i++) {
if (image_filename[i]) {
struct disk_image *disk = disk_image__open(image_filename[i], readonly_image[i]);
if (!disk)
die("unable to load disk image %s", image_filename[i]);
if (image_count) {
kvm->disks = disk_image__open_all(image_filename, readonly_image, image_count);
if (!kvm->disks)
die("Unable to load all disk images.");
virtio_blk__init(kvm, disk);
}
for (i = 0; i < image_count; i++)
virtio_blk__init(kvm, kvm->disks[i]);
}
free(hi);
printf(" # kvm run -k %s -m %Lu -c %d\n", kernel_filename, ram_size / 1024 / 1024, nrcpus);