emu: handle BLS /boot weirdness

Signed-off-by: Robbie Harwood <rharwood@redhat.com>
This commit is contained in:
Robbie Harwood 2023-03-09 16:48:40 +00:00
parent 4db0050f31
commit 48cf39de05
4 changed files with 217 additions and 1 deletions

View File

@ -0,0 +1,93 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Robbie Harwood <rharwood@redhat.com>
Date: Thu, 9 Mar 2023 11:18:19 -0500
Subject: [PATCH] hostdisk: work around /proc not reporting size
fstat(2) of files in /proc will yield st_size == 0 regardless of file
contents. Use a negative value in grub_file_t's size to denote "ignore"
and plumb through.
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
grub-core/kern/file.c | 28 ++++++++++++++++------------
grub-core/lib/progress.c | 2 +-
grub-core/osdep/unix/hostdisk.c | 6 ++++++
3 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/grub-core/kern/file.c b/grub-core/kern/file.c
index 868ce3b63e..4ea6d1ce95 100644
--- a/grub-core/kern/file.c
+++ b/grub-core/kern/file.c
@@ -172,26 +172,30 @@ grub_file_read (grub_file_t file, void *buf, grub_size_t len)
grub_disk_read_hook_t read_hook;
void *read_hook_data;
- if (file->offset > file->size)
- {
- grub_error (GRUB_ERR_OUT_OF_RANGE,
- N_("attempt to read past the end of file"));
- return -1;
- }
-
if (len == 0)
return 0;
- if (len > file->size - file->offset)
- len = file->size - file->offset;
+#ifdef GRUB_MACHINE_EMU
+ if (file->size >= 0)
+ {
+#endif
+ if (file->offset > file->size)
+ {
+ grub_error (GRUB_ERR_OUT_OF_RANGE,
+ N_("attempt to read past the end of file"));
+ return -1;
+ }
+
+ if (len > file->size - file->offset)
+ len = file->size - file->offset;
+#ifdef GRUB_MACHINE_EMU
+ }
+#endif
/* Prevent an overflow. */
if ((grub_ssize_t) len < 0)
len >>= 1;
- if (len == 0)
- return 0;
-
read_hook = file->read_hook;
read_hook_data = file->read_hook_data;
if (!file->read_hook)
diff --git a/grub-core/lib/progress.c b/grub-core/lib/progress.c
index 4b7cbbca6d..f3226b6898 100644
--- a/grub-core/lib/progress.c
+++ b/grub-core/lib/progress.c
@@ -71,7 +71,7 @@ grub_file_progress_hook_real (grub_disk_addr_t sector __attribute__ ((unused)),
* 100ULL * 1000ULL,
now - file->last_progress_time, 0);
- if (file->size == 0)
+ if (file->size <= 0)
percent = 100;
else
percent = grub_divmod64 (100 * file->progress_offset,
diff --git a/grub-core/osdep/unix/hostdisk.c b/grub-core/osdep/unix/hostdisk.c
index 3a00d7451a..e5f4b4d5f9 100644
--- a/grub-core/osdep/unix/hostdisk.c
+++ b/grub-core/osdep/unix/hostdisk.c
@@ -71,6 +71,12 @@ grub_util_get_fd_size (grub_util_fd_t fd, const char *name, unsigned *log_secsiz
if (log_secsize)
*log_secsize = 9;
+#ifdef GRUB_MACHINE_EMU
+ /* /proc doesn't behave itself and gives 0 for file sizes to stat. */
+ if (st.st_size == 0 && !grub_strncmp ("/proc", name, 5))
+ return -1;
+#endif
+
return st.st_size;
}

View File

@ -0,0 +1,118 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Robbie Harwood <rharwood@redhat.com>
Date: Tue, 7 Mar 2023 18:59:40 -0500
Subject: [PATCH] blscfg: check for mounted /boot in emu
Irritatingly, BLS defines paths relatives to the mountpoint of the
filesystem which contains its snippets, not / or any other fixed
location. So grub2-emu needs to know whether /boot is a separate
filesysem from / and conditionally prepend a path.
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
---
grub-core/commands/blscfg.c | 54 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 49 insertions(+), 5 deletions(-)
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
index dbd0899acf..6e398fc175 100644
--- a/grub-core/commands/blscfg.c
+++ b/grub-core/commands/blscfg.c
@@ -40,8 +40,9 @@ GRUB_MOD_LICENSE ("GPLv3+");
#include "loadenv.h"
#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
+
#ifdef GRUB_MACHINE_EMU
-#define GRUB_BOOT_DEVICE ""
+#define GRUB_BOOT_DEVICE "/boot"
#else
#define GRUB_BOOT_DEVICE "($root)"
#endif
@@ -54,8 +55,50 @@ struct keyval
static struct bls_entry *entries = NULL;
+/* Cache probing in frob_boot_device(). Used for linux entry also.
+ * Always true in non-emu, meaning to prefix things with GRUB_BOOT_DEVICE. */
+static int separate_boot = -1;
+
#define FOR_BLS_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)
+/* BLS appears to make paths relative to the filesystem that snippets are
+ * on, not /. Attempt to cope. */
+static char *frob_boot_device(char *tmp)
+{
+#ifdef GRUB_MACHINE_EMU
+ grub_file_t f;
+ char *line = NULL;
+
+ if (separate_boot != -1)
+ goto probed;
+
+ separate_boot = 0;
+
+ f = grub_file_open ("/proc/mounts", GRUB_FILE_TYPE_CONFIG);
+ if (f == NULL)
+ goto probed;
+
+ while ((line = grub_file_getline (f)))
+ {
+ if (grub_strstr (line, " " GRUB_BOOT_DEVICE " "))
+ {
+ separate_boot = 1;
+ grub_free (line);
+ break;
+ }
+
+ grub_free(line);
+ }
+
+ grub_file_close (f);
+ probed:
+ if (!separate_boot)
+ return grub_stpcpy (tmp, " ");
+#endif
+
+ return grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
+}
+
static int bls_add_keyval(struct bls_entry *entry, char *key, char *val)
{
char *k, *v;
@@ -842,7 +885,7 @@ static void create_entry (struct bls_entry *entry)
for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++)
{
grub_dprintf ("blscfg", "adding early initrd %s\n", early_initrds[i]);
- tmp = grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
+ tmp = frob_boot_device (tmp);
tmp = grub_stpcpy (tmp, initrd_prefix);
tmp = grub_stpcpy (tmp, early_initrds[i]);
grub_free(early_initrds[i]);
@@ -851,7 +894,7 @@ static void create_entry (struct bls_entry *entry)
for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
{
grub_dprintf ("blscfg", "adding initrd %s\n", initrds[i]);
- tmp = grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
+ tmp = frob_boot_device (tmp);
tmp = grub_stpcpy (tmp, initrds[i]);
}
tmp = grub_stpcpy (tmp, "\n");
@@ -888,7 +931,7 @@ static void create_entry (struct bls_entry *entry)
}
char *tmp = dt;
tmp = grub_stpcpy (dt, "devicetree");
- tmp = grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
+ tmp = frob_boot_device (tmp);
if (add_dt_prefix)
tmp = grub_stpcpy (tmp, prefix);
tmp = grub_stpcpy (tmp, devicetree);
@@ -907,7 +950,8 @@ static void create_entry (struct bls_entry *entry)
"linux %s%s%s%s\n"
"%s%s",
savedefault ? "savedefault\n" : "",
- GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options : "",
+ separate_boot ? GRUB_BOOT_DEVICE : "",
+ clinux, options ? " " : "", options ? options : "",
initrd ? initrd : "", dt ? dt : "");
grub_normal_add_menu_entry (argc, argv, classes, id, users, hotkey, NULL, src, 0, &index, entry);

View File

@ -320,3 +320,5 @@ Patch0319: 0319-osdep-linux-hostdisk-Modify-sector-by-sysfs-as-disk-.patch
Patch0320: 0320-mm-Adjust-new-region-size-to-take-management-overhea.patch Patch0320: 0320-mm-Adjust-new-region-size-to-take-management-overhea.patch
Patch0321: 0321-mm-Preallocate-some-space-when-adding-new-regions.patch Patch0321: 0321-mm-Preallocate-some-space-when-adding-new-regions.patch
Patch0322: 0322-mm-Avoid-complex-heap-growth-math-in-hot-path.patch Patch0322: 0322-mm-Avoid-complex-heap-growth-math-in-hot-path.patch
Patch0323: 0323-hostdisk-work-around-proc-not-reporting-size.patch
Patch0324: 0324-blscfg-check-for-mounted-boot-in-emu.patch

View File

@ -17,7 +17,7 @@
Name: grub2 Name: grub2
Epoch: 1 Epoch: 1
Version: 2.06 Version: 2.06
Release: 88%{?dist} Release: 89%{?dist}
Summary: Bootloader with support for Linux, Multiboot and more Summary: Bootloader with support for Linux, Multiboot and more
License: GPLv3+ License: GPLv3+
URL: http://www.gnu.org/software/grub/ URL: http://www.gnu.org/software/grub/
@ -544,6 +544,9 @@ mv ${EFI_HOME}/grub.cfg.stb ${EFI_HOME}/grub.cfg
%endif %endif
%changelog %changelog
* Thu Mar 09 2023 Robbie Harwood <rharwood@redhat.com> - 2.06-89
- emu: handle BLS /boot weirdness
* Mon Feb 20 2023 Robbie Harwood <rharwood@redhat.com> - 2.06-88 * Mon Feb 20 2023 Robbie Harwood <rharwood@redhat.com> - 2.06-88
- Update mm fixes from upstream - Update mm fixes from upstream