[PATCH 16/28] kvm tools: Allow load_flat_binary() to load an initrd

Matt Evans
2011-12-06T03:41:07Z

This patch passes the initrd fd and commandline to load_flat_binary(), which may
be used to load both the kernel & an initrd (stashing or inserting the
commandline as appropriate) in the same way that load_bzimage() does. This is
especially useful when load_bzimage() is unused for a particular
architecture. :-)

Signed-off-by: Matt Evans <matt@ozlabs.org>
---
tools/kvm/include/kvm/kvm.h | 2 +-
tools/kvm/kvm.c | 10 ++++++----
tools/kvm/x86/kvm.c | 12 +++++++++---
3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index fae2ba9..5fe6e75 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -59,7 +59,7 @@ void kvm__arch_setup_firmware(struct kvm *kvm);
bool kvm__arch_cpu_supports_vm(void);
void kvm__arch_periodic_poll(struct kvm *kvm);

-int load_flat_binary(struct kvm *kvm, int fd);
+int load_flat_binary(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline);
bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline, u16 vidmode);

/*
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 457de1a..6f33e1a 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -354,23 +354,25 @@ bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,

ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, vidmode);

- if (initrd_filename)
- close(fd_initrd);
-
if (ret)
goto found_kernel;

pr_warning("%s is not a bzImage. Trying to load it as a flat binary...", kernel_filename);

- ret = load_flat_binary(kvm, fd_kernel);
+ ret = load_flat_binary(kvm, fd_kernel, fd_initrd, kernel_cmdline);
+
if (ret)
goto found_kernel;

+ if (initrd_filename)
+ close(fd_initrd);
close(fd_kernel);

die("%s is not a valid bzImage or flat binary", kernel_filename);

found_kernel:
+ if (initrd_filename)
+ close(fd_initrd);
close(fd_kernel);

return ret;
diff --git a/tools/kvm/x86/kvm.c b/tools/kvm/x86/kvm.c
index 7071dc6..4ac21c0 100644
--- a/tools/kvm/x86/kvm.c
+++ b/tools/kvm/x86/kvm.c
@@ -227,17 +227,23 @@ void kvm__irq_trigger(struct kvm *kvm, int irq)
#define BOOT_PROTOCOL_REQUIRED 0x206
#define LOAD_HIGH 0x01

-int load_flat_binary(struct kvm *kvm, int fd)
+int load_flat_binary(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline)
{
void *p;
int nr;

- if (lseek(fd, 0, SEEK_SET) < 0)
+ /* Some architectures may support loading an initrd alongside the flat kernel,
+ * but we do not.
+ */
+ if (fd_initrd != -1)
+ pr_warning("Loading initrd with flat binary not supported.");
+
+ if (lseek(fd_kernel, 0, SEEK_SET) < 0)
die_perror("lseek");

p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);

- while ((nr = read(fd, p, 65536)) > 0)
+ while ((nr = read(fd_kernel, p, 65536)) > 0)
p += nr;

kvm->boot_selector = BOOT_LOADER_SELECTOR;