Skip to contentSkip to conversation

templates/linux_xen: Properly handle multiple initrd files

PATCH In review Mauricio Faria de Oliveira 2 participants 5 messages Aug 8, 2022 – Aug 11, 2022

lore
Share & tools
lore
#0 lore raw

cover templates/linux_xen: Properly handle multiple initrd files

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-generic

Patch 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-generic

Patch 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.cpio

Regular 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.cpio

Original:

    (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.17

Xen 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
#1 lore raw

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-generic

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/microcode.cpio
            module  --nounzip   /boot/initrd.img-5.4.0-122-generic

Cause:

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
Fixes e86f6aafb8de ("grub-mkconfig/20_linux_xen: Support multiple early initrd images")
---
 util/grub.d/20_linux_xen.in | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
util/grub.d/20_linux_xen.in +3 -3
index 4382303c17f0..50c62562b730 100644
@@ -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
#2 lore raw

PATCH 2/2 templates/linux_xen: Properly order the multiple initrd files

In reply to Mauricio Faria de Oliveira

The 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.

Fixes e86f6aafb8de ("grub-mkconfig/20_linux_xen: Support multiple early initrd images")
---
 util/grub.d/20_linux_xen.in | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
util/grub.d/20_linux_xen.in +4 -1
index 50c62562b730..626aed40cbfd 100644
@@ -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
#3 lore raw
In reply to Mauricio Faria de Oliveira
Quoted 9 lines · Mauricio Faria de Oliveira

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.

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.

Daniel

#4 lore raw
Quoted 9 lines · Daniel Kiper
Quoted 7 lines · Mauricio Faria de Oliveira

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].

Quoted 6 lines
Quoted 2 lines

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.

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.

Sure, I just reposted it with the xen-devel list added. Thanks for the initial review.

Quoted 2 lines

Daniel

Signature · 1 line
Mauricio Faria de Oliveira