All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Sertonix" <sertonix@posteo.net>
To: <qemu-devel@nongnu.org>
Subject: [PATCH v2] hw/i386: Use qemu_open for kernel/initrd
Date: Tue, 21 Apr 2026 10:22:11 +0000	[thread overview]
Message-ID: <DHYRB7QIPFB3.3VG2UUUIXCBZP@posteo.net> (raw)


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.
---
v2: Use qemu_open instead of qemu_open_old

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

diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c
index fde05fa7d7..2e4fee3e87 100644
--- a/hw/i386/x86-common.c
+++ b/hw/i386/x86-common.c
@@ -650,7 +650,9 @@ void x86_load_linux(X86MachineState *x86ms,
     uint32_t initrd_max;
     uint8_t header[8192], *setup, *kernel;
     hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
+    Error *err = NULL;
     FILE *f;
+    int fd;
     const char *vmode;
     MachineState *machine = MACHINE(x86ms);
     struct setup_data *setup_data;
@@ -664,8 +666,13 @@ 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(kernel_filename, O_RDONLY, &err);
+    if (fd < 0) {
+        error_report_err(err);
+        exit(1);
+    }
+    f = fdopen(fd, "rb");
+    if (f) {
         fprintf(stderr, "qemu: could not open kernel file '%s': %s\n",
                 kernel_filename, strerror(errno));
         exit(1);
@@ -719,12 +726,18 @@ 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(initrd_filename, O_RDONLY, &err);
+                if (fd < 0) {
+                    error_report_err(err);
+                    exit(1);
+                }
+                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 +873,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 +884,12 @@ void x86_load_linux(X86MachineState *x86ms,
             exit(1);
         }
 
-        mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
+        mapped_file_fd = qemu_open(initrd_filename, O_RDONLY, &err);
+        if (mapped_file_fd < 0) {
+            error_report_err(err);
+            exit(1);
+        }
+        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



                 reply	other threads:[~2026-04-21 10:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DHYRB7QIPFB3.3VG2UUUIXCBZP@posteo.net \
    --to=sertonix@posteo.net \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.