[PATCH] hw/i386: Use qemu_open_old for kernel/initrd

Sertonix posted 1 patch 1 day, 17 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/DHXDTYTOFYVL.3H531B0BJOQ22@posteo.net
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>
hw/i386/x86-common.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
[PATCH] hw/i386: Use qemu_open_old for kernel/initrd
Posted by Sertonix 1 day, 17 hours ago

Allows using "-kernel /dev/fdset/0" and "-initrd /dev/fdset/1" (if the
file descriptors are opened read-only).

For the sake of consistency it would be best to do the same change in
the other hw/* subdirectories as well but I lack the means to test all
of them.

---
 hw/i386/x86-common.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index fde05fa7d7..5d8be30f4d 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -651,6 +651,7 @@ void x86_load_linux(X86MachineState *x86ms,
     uint8_t header[8192], *setup, *kernel;
     hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
     FILE *f;
+    int fd;
     const char *vmode;
     MachineState *machine = MACHINE(x86ms);
     struct setup_data *setup_data;
@@ -664,8 +665,8 @@ void x86_load_linux(X86MachineState *x86ms,
     cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
 
     /* load the kernel header */
-    f = fopen(kernel_filename, "rb");
-    if (!f) {
+    fd = qemu_open_old(kernel_filename, O_RDONLY);
+    if (fd < 0 || !(f = fdopen(fd, "rb"))) {
         fprintf(stderr, "qemu: could not open kernel file '%s': %s\n",
                 kernel_filename, strerror(errno));
         exit(1);
@@ -719,12 +720,14 @@ void x86_load_linux(X86MachineState *x86ms,
 
             /* load initrd */
             if (initrd_filename) {
+                int mapped_file_fd;
                 GMappedFile *mapped_file;
                 gsize initrd_size;
                 gchar *initrd_data;
                 GError *gerr = NULL;
 
-                mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
+                mapped_file_fd = qemu_open_old(initrd_filename, O_RDONLY);
+                mapped_file = g_mapped_file_new_from_fd(mapped_file_fd, false, &gerr);
                 if (!mapped_file) {
                     fprintf(stderr, "qemu: error reading initrd %s: %s\n",
                             initrd_filename, gerr->message);
@@ -860,6 +863,7 @@ void x86_load_linux(X86MachineState *x86ms,
 
     /* load initrd */
     if (initrd_filename) {
+        int mapped_file_fd;
         GMappedFile *mapped_file;
         gsize initrd_size;
         gchar *initrd_data;
@@ -870,7 +874,8 @@ void x86_load_linux(X86MachineState *x86ms,
             exit(1);
         }
 
-        mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
+        mapped_file_fd = qemu_open_old(initrd_filename, O_RDONLY);
+        mapped_file = g_mapped_file_new_from_fd(mapped_file_fd, false, &gerr);
         if (!mapped_file) {
             fprintf(stderr, "qemu: error reading initrd %s: %s\n",
                     initrd_filename, gerr->message);
-- 
2.53.0
Re: [PATCH] hw/i386: Use qemu_open_old for kernel/initrd
Posted by Philippe Mathieu-Daudé 16 hours ago
Hi,

On 19/4/26 21:35, Sertonix wrote:
> 
> Allows using "-kernel /dev/fdset/0" and "-initrd /dev/fdset/1" (if the
> file descriptors are opened read-only).
> 
> For the sake of consistency it would be best to do the same change in
> the other hw/* subdirectories as well but I lack the means to test all
> of them.

Thanks for this patch. Note however there is a comment before
the qemu_open_old() declaration in "qemu/osdep.h":

   /*
    * Don't introduce new usage of this function, prefer the following
    * qemu_open/qemu_create that take an "Error **errp"
    */

Do you mind using the latter in your patch?

Thanks,

Phil.

> ---
>   hw/i386/x86-common.c | 13 +++++++++----
>   1 file changed, 9 insertions(+), 4 deletions(-)