Skip to contentSkip to conversation

hw/i386: Use qemu_open_old for kernel/initrd

PATCH Superseded Sertonix 2 participants 2 messages Apr 19 – Apr 20

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sertonix

author
#0 lore raw

PATCH 1/1 hw/i386: Use qemu_open_old for kernel/initrd

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(-)
hw/i386/x86-common.c +9 -4
index fde05fa7d7..5d8be30f4d 100644
@@ -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);
Signature · 1 line
2.53.0
#1 lore raw

Hi,

Quoted 7 lines · Sertonix

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.

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