templates/linux_xen: Properly handle multiple initrd files
cover templates/linux_xen: Properly handle multiple initrd files
Adding the xen-devel list as requested in grub-devel [0]:
Quoted 3 lines · Daniel Kiper
In general patches LGTM. However, I would prefer to hear an opinion from Xen folks too. So, please repost the patch set and add [email protected] to the list of recipients.
[0] https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00115.html
Thanks,
Mauricio
...
The linux_xen template seems to be broken for multiple initrd files. Linux fails to boot when it needs a real initrd but early/microcode initrd(s) are found by grub-mkconfig.
Patch 1 allows initrd(s) other than the first early/microcode initrd to be loaded at all, fixing an implementation error for multiboot[2].
Patch 2 allows Linux to get the real initrd as initrd (vs. an early/ microcode-only initrd), thus being able to boot if it needs a initrd.
Synthetic tests:
---
# touch /boot/xen /boot/microcode.cpio
Original:
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot /boot/xen ...
module /boot/vmlinuz-5.4.0-122-generic ...
module --nounzip /boot/microcode.cpio /boot/initrd.img-5.4.0-122-genericPatch 1:
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot /boot/xen ...
module /boot/vmlinuz-5.4.0-122-generic ...
module --nounzip /boot/microcode.cpio
module --nounzip /boot/initrd.img-5.4.0-122-genericPatch 2:
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot /boot/xen ...
module /boot/vmlinuz-5.4.0-122-generic ...
module --nounzip /boot/initrd.img-5.4.0-122-generic
module --nounzip /boot/microcode.cpio With fake multiboot2 header:
# echo -en '\xd6\x50\x52\xe8\x00\x00\x00\x00' > /boot/xen
# echo -en '\x00\x00\x00\x00\x2a\xaf\xad\x17' >> /boot/xen
# echo -en '\x00\x00\x00\x00\x00\x00\x00\x00' >> /boot/xen
# echo -en '\x00\x00\x00\x00\x00\x00\x00\x00' >> /boot/xen
# grub-file --is-x86-multiboot2 /boot/xen; echo $?
0
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot2 /boot/xen ...
module2 /boot/vmlinuz-5.4.0-122-generic ...
module2 --nounzip /boot/initrd.img-5.4.0-122-generic
module2 --nounzip /boot/microcode.cpioRegular tests:
---
Debug patch:
__start_xen() @ xen.git:xen/arch/x86/setup.c
...
+for (i = 0; i < mbi->mods_count; i++)
+ printk("MODULE %d size %d string %s\n",
+ i, mod[i].mod_end - mod[i].mod_start, (char*)__va(mod[i].string));
+
bitmap_fill(module_map, mbi->mods_count);
__clear_bit(0, module_map); /* Dom0 kernel is always first */For /boot/microcode.cpio:
$ sudo apt install microcode-initrd # on Ubuntu
$ du --bytes /boot/...
13660416 /boot/vmlinuz-5.4.0-122-generic
33062542 /boot/initrd.img-5.4.0-122-generic
5045248 /boot/microcode.cpioOriginal:
(XEN) MODULE 0 size 13660416 string placeholder root=UUID=74dd579c-a377-487d-b8f7-bc7c6df13ba1 ro console=ttyS0
(XEN) MODULE 1 size 5045248 string /boot/initrd.img-5.4.0-122-generic
...
[ 2.505207] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)Patch 1:
(XEN) MODULE 0 size 13660416 string placeholder root=UUID=74dd579c-a377-487d-b8f7-bc7c6df13ba1 ro console=ttyS0
(XEN) MODULE 1 size 5045248 string
(XEN) MODULE 2 size 33062542 string
...
[ 1.890498] Freeing initrd memory: 4928K
...
[ 2.710948] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)Patch 2:
(XEN) MODULE 0 size 13660416 string placeholder root=UUID=74dd579c-a377-487d-b8f7-bc7c6df13ba1 ro console=ttyS0
(XEN) MODULE 1 size 33062542 string
(XEN) MODULE 2 size 5045248 string
...
[ 1.968578] Freeing initrd memory: 32288K
...
[ 2.844889] Run /init as init process
[ 2.916532] systemd-udevd[148]: Starting version 245.4-4ubuntu3.17Xen code:
---
Key code path for this patchset:
@ xen.git:xen/arch/x86/setup.c:
void __init noreturn __start_xen(unsigned long mbi_p)
{
...
unsigned int initrdidx, ...
multiboot_info_t *mbi;
module_t *mod;
unsigned long ..., module_map[1];
...
mbi = __va(mbi_p);
...
mod = __va(mbi->mods_addr);
...
bitmap_fill(module_map, mbi->mods_count);
__clear_bit(0, module_map); /* Dom0 kernel is always first */
...
microcode_grab_module(module_map, mbi);
# ucode=number can __test_and_clear_bit(number, module_map),
# ucode=scan can't.
...
initrdidx = find_first_bit(module_map, mbi->mods_count);
if ( bitmap_weight(module_map, mbi->mods_count) > 1 )
printk(XENLOG_WARNING
"Multiple initrd candidates, picking module #%u\n",
initrdidx);
...
/*
* ... The second module, if present, is an initrd ramdisk.
*/
dom0 = create_dom0(mod, modules_headroom,
initrdidx < mbi->mods_count ? mod + initrdidx : NULL,
kextra, loader);
...Mauricio Faria de Oliveira (2):
templates/linux_xen: Properly load multiple initrd files templates/linux_xen: Properly order the multiple initrd files
util/grub.d/20_linux_xen.in | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 templates/linux_xen: Properly load multiple initrd files
The linux_xen template can put multiple initrd files in the same multiboot[2] module[2] command, which is against specs.
This causes ONLY the _first_ initrd file to be loaded; other files just have filenames in a "cmdline" string of the first initrd file and are NOT loaded.
Fix this by inserting a module[2] command per initrd file.
Before:
# touch /boot/xen /boot/microcode.cpio
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot /boot/xen ...
module /boot/vmlinuz-5.4.0-122-generic ...
module --nounzip /boot/microcode.cpio /boot/initrd.img-5.4.0-122-genericAfter:
# touch /boot/xen /boot/microcode.cpio
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot /boot/xen ...
module /boot/vmlinuz-5.4.0-122-generic ...
module --nounzip /boot/microcode.cpio
module --nounzip /boot/initrd.img-5.4.0-122-genericCause:
The code was copied from the linux template, which is *apparently* equivalent.. but its initrd command grub_cmd_initrd() *supports* multiple files (see grub_initrd_init()), while module/module2 in grub_cmd_module() *does not* (see grub_multiboot[2]_add_module()).
See commit e86f6aafb8de ("grub-mkconfig/20_linux_xen: Support multiple early initrd images"):
'This is basically a copy of a698240d "grub-mkconfig/10_linux:
Support multiple early initrd images" ...'
Specs:
Both multiboot and multiboot2 specifications mention support for 'multiple boot modules' (struct/tag used for kernel/initrd files):
"Boot loaders don’t have to support multiple boot modules,
but they are strongly encouraged to" [1,2]
However, there is a 1:1 relationship between boot modules and files, more or less clearly; note the usage of singular/plural "module(s)". (Multiboot2, clearly: "One tag appears per module".)
Multiboot [1]:
"the ‘mods’ fields indicate ... what boot modules
were loaded ..., and where they can be found. ‘mods_count’ contains the number of modules loaded"
"The first two fields contain the start and end addresses
of the boot module itself."
Multiboot2 [2]:
"This tag indicates ... what boot module was loaded ...,
and where it can be found."
"The ‘mod_start’ and ‘mod_end’ contain the start and end
physical addresses of the boot module itself."
"One tag appears per module.
This tag type may appear multiple times."
And both clearly mention the 'string' field of a boot module, which is to be used by the operating system, not boot loader:
"The ‘string’ field provides an arbitrary string to be
associated with that particular boot module ... its exact use is specific to the operating system."
Links:
[1] https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
3.3 Boot information format
[2] https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html
3.6.6 Modules
--- util/grub.d/20_linux_xen.in | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
util/grub.d/20_linux_xen.in +3 -3
@@ -162,12 +162,12 @@ EOF message="$(gettext_printf "Loading initial ramdisk ...")" initrd_path= for i in ${initrd}; do - initrd_path="${initrd_path} ${rel_dirname}/${i}" - done - sed "s/^/$submenu_indentation/" << EOF + initrd_path="${rel_dirname}/${i}" + sed "s/^/$submenu_indentation/" << EOF echo '$(echo "$message" | grub_quote)' ${module_loader} --nounzip $(echo $initrd_path) EOF + done fi if test -n "${xenpolicy}" ; then message="$(gettext_printf "Loading XSM policy ...")"
Signature · 1 line
2.34.1
PATCH 2/2 templates/linux_xen: Properly order the multiple initrd files
In reply to Mauricio Faria de OliveiraThe linux_xen template orders the "early" initrd file(s) _first_ (i.e., before the "real" initrd files) and that seems reasonable, as microcode updates usually come first.
However, this usually breaks Linux boot with initrd under Xen because Xen assumes the real initrd is the first multiboot[2] module after the kernel, passing its address over to Linux in Xen's start_info struct.
So, if a microcode-only initrd (i.e., without init/userspace) is found by grub-mkconfig, it ends up considered as a normal initrd by the Linux kernel, which cannot do anything with it (as it has no other files) and panic()s unable to mount root if it depends on a initrd to do that (e.g., root=UUID=...).
...
Well, since Xen doesn't actually use the provided microcode by default / unless the 'ucode=<module number|scan>' option is enabled, this isn't used in the general case (and breaks).
Additionally, if an user enables the 'ucode=' option, that either specifies which module is to be used for microcode, or scans all modules (regardless of being first) for that.
Thus, for Xen:
- it is *not required* to have microcode first,
- but it is *required* to have real initrd first
So, fix it by ordering the real initrd before early initrd(s).
After:
# touch /boot/xen /boot/microcode.cpio
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)'
multiboot /boot/xen ...
module /boot/vmlinuz-5.4.0-122-generic ...
module --nounzip /boot/initrd.img-5.4.0-122-generic
module --nounzip /boot/microcode.cpio...
Corner case specific to Xen implementation details:
It is actually _possible_ to have a microcode initrd first, but that requires a non-default option (so can't rely on it), and it turns out to be inconsistent with its counterpart (really shouldn't rely on it, as it may get confusing; below).
'ucode=1' does manually specify the first module is microcode _AND_ clears its bit in the module bitmap. The next module is now the 'new first', and gets passed to Linux as initrd. Good.
'ucode=scan' checks all modules for microcode, but does _NOT_ clear a bit if it finds one (reasonable, as it can find that prepended in a "real" initrd anyway, which needs to be used). The first module still gets passed to Linux as initrd. Bad.
--- util/grub.d/20_linux_xen.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
util/grub.d/20_linux_xen.in +4 -1
@@ -307,7 +307,10 @@ for current_xen in ${reverse_sorted_xen_list}; do initrd= if test -n "${initrd_early}" || test -n "${initrd_real}"; then - initrd="${initrd_early} ${initrd_real}" + # Xen assumes the real initrd is the first module after the kernel. + # Additional (later) initrds can also be used for microcode update, + # with Xen option 'ucode=<scan|module number> (non-default anyway). + initrd="${initrd_real} ${initrd_early}" initrd_display= for i in ${initrd}; do
Signature · 1 line
2.34.1
Quoted 159 lines · Mauricio Faria de Oliveira
Adding the xen-devel list as requested in grub-devel [0]:
Quoted 3 lines · Daniel Kiper
In general patches LGTM. However, I would prefer to hear an opinion from Xen folks too. So, please repost the patch set and add [email protected] to the list of recipients.
[0] https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00115.html
Thanks,
Mauricio...
The
linux_xentemplate seems to be broken for multiple initrd files. Linux fails to boot when it needs a real initrd but early/microcode initrd(s) are found by grub-mkconfig.Patch 1 allows initrd(s) other than the first early/microcode initrd to be loaded at all, fixing an implementation error for multiboot[2].
Patch 2 allows Linux to get the real initrd as initrd (vs. an early/ microcode-only initrd), thus being able to boot if it needs a initrd.
Synthetic tests:
---# touch /boot/xen /boot/microcode.cpioOriginal:
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)' multiboot /boot/xen ... module /boot/vmlinuz-5.4.0-122-generic ... module --nounzip /boot/microcode.cpio /boot/initrd.img-5.4.0-122-genericPatch 1:
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)' multiboot /boot/xen ... module /boot/vmlinuz-5.4.0-122-generic ... module --nounzip /boot/microcode.cpio module --nounzip /boot/initrd.img-5.4.0-122-genericPatch 2:
# grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)' multiboot /boot/xen ... module /boot/vmlinuz-5.4.0-122-generic ... module --nounzip /boot/initrd.img-5.4.0-122-generic module --nounzip /boot/microcode.cpioWith fake multiboot2 header: # echo -en '\xd6\x50\x52\xe8\x00\x00\x00\x00' > /boot/xen # echo -en '\x00\x00\x00\x00\x2a\xaf\xad\x17' >> /boot/xen # echo -en '\x00\x00\x00\x00\x00\x00\x00\x00' >> /boot/xen # echo -en '\x00\x00\x00\x00\x00\x00\x00\x00' >> /boot/xen # grub-file --is-x86-multiboot2 /boot/xen; echo $? 0 # grub-mkconfig 2>/dev/null | grep -P '^\t(multiboot|module)' multiboot2 /boot/xen ... module2 /boot/vmlinuz-5.4.0-122-generic ... module2 --nounzip /boot/initrd.img-5.4.0-122-generic module2 --nounzip /boot/microcode.cpioRegular tests:
---Debug patch:
__start_xen() @ xen.git:xen/arch/x86/setup.c ... +for (i = 0; i < mbi->mods_count; i++) + printk("MODULE %d size %d string %s\n", + i, mod[i].mod_end - mod[i].mod_start, (char*)__va(mod[i].string)); + bitmap_fill(module_map, mbi->mods_count); __clear_bit(0, module_map); /* Dom0 kernel is always first */For
/boot/microcode.cpio:$ sudo apt install microcode-initrd # on Ubuntu$ du --bytes /boot/... 13660416 /boot/vmlinuz-5.4.0-122-generic 33062542 /boot/initrd.img-5.4.0-122-generic 5045248 /boot/microcode.cpioOriginal:
(XEN) MODULE 0 size 13660416 string placeholder root=UUID=74dd579c-a377-487d-b8f7-bc7c6df13ba1 ro console=ttyS0 (XEN) MODULE 1 size 5045248 string /boot/initrd.img-5.4.0-122-generic ... [ 2.505207] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)Patch 1:
(XEN) MODULE 0 size 13660416 string placeholder root=UUID=74dd579c-a377-487d-b8f7-bc7c6df13ba1 ro console=ttyS0 (XEN) MODULE 1 size 5045248 string (XEN) MODULE 2 size 33062542 string ... [ 1.890498] Freeing initrd memory: 4928K ... [ 2.710948] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)Patch 2:
(XEN) MODULE 0 size 13660416 string placeholder root=UUID=74dd579c-a377-487d-b8f7-bc7c6df13ba1 ro console=ttyS0 (XEN) MODULE 1 size 33062542 string (XEN) MODULE 2 size 5045248 string ... [ 1.968578] Freeing initrd memory: 32288K ... [ 2.844889] Run /init as init process [ 2.916532] systemd-udevd[148]: Starting version 245.4-4ubuntu3.17Xen code:
---Key code path for this patchset:
@ xen.git:xen/arch/x86/setup.c: void __init noreturn __start_xen(unsigned long mbi_p) { ... unsigned int initrdidx, ... multiboot_info_t *mbi; module_t *mod; unsigned long ..., module_map[1]; ... mbi = __va(mbi_p); ... mod = __va(mbi->mods_addr); ... bitmap_fill(module_map, mbi->mods_count); __clear_bit(0, module_map); /* Dom0 kernel is always first */ ... microcode_grab_module(module_map, mbi); # ucode=number can __test_and_clear_bit(number, module_map), # ucode=scan can't. ... initrdidx = find_first_bit(module_map, mbi->mods_count); if ( bitmap_weight(module_map, mbi->mods_count) > 1 ) printk(XENLOG_WARNING "Multiple initrd candidates, picking module #%u\n", initrdidx); ... /* * ... The second module, if present, is an initrd ramdisk. */ dom0 = create_dom0(mod, modules_headroom, initrdidx < mbi->mods_count ? mod + initrdidx : NULL, kextra, loader); ...
Quoted 7 lines
Mauricio Faria de Oliveira (2):
templates/linux_xen: Properly load multiple initrd files templates/linux_xen: Properly order the multiple initrd filesutil/grub.d/20_linux_xen.in | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
For both patches:
Juergen
[removing xen-devel and Juergen Gross]
Hi Daniel,
Quoted 10 lines · Juergen Gross
Quoted 8 lines · Mauricio Faria de Oliveira
Adding the xen-devel list as requested in grub-devel [0]:
Quoted 3 lines · Daniel Kiper
In general patches LGTM. However, I would prefer to hear an opinion from Xen folks too. So, please repost the patch set and add [email protected] to the list of recipients.
[0] https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00115.html
[snip]
Quoted 7 lines
For both patches:
Acked-by Juergen Gross <[email protected]>Juergen
Just following up on this patchset, now that it's been Acked-by in xen-devel, as you requested.
Thanks,
Signature · 1 line
Mauricio Faria de Oliveira
Quoted 19 lines · Mauricio Faria de Oliveira
[removing xen-devel and Juergen Gross]
Hi Daniel,
Quoted 10 lines · Juergen Gross
Quoted 8 lines · Mauricio Faria de Oliveira
Adding the xen-devel list as requested in grub-devel [0]:
Quoted 3 lines · Daniel Kiper
In general patches LGTM. However, I would prefer to hear an opinion from Xen folks too. So, please repost the patch set and add [email protected] to the list of recipients.
[0] https://lists.gnu.org/archive/html/grub-devel/2022-08/msg00115.html
[snip]
Quoted 3 lines
For both patches:
Quoted 7 lines
Quoted 4 lines
Acked-by Juergen Gross <[email protected]>Juergen
Just following up on this patchset, now that it's been Acked-by in xen-devel, as you requested.
Thank you for doing that!
For both patches Reviewed-by: Daniel Kiper <[email protected]>...
Daniel