u-root / u-root

A fully Go userland with Linux bootloaders! u-root can create a one-binary root file system (initramfs) containing a busybox-like set of tools written in Go.
https://u-root.org
BSD 3-Clause "New" or "Revised" License
2.6k stars 402 forks source link

Implement parsing of GRUB2 submenus #2051

Open JohnAZoidberg opened 3 years ago

JohnAZoidberg commented 3 years ago

Update: I initially thought there's a bug in u-root that duplicates the menu entries but it's actually the case that there are many submenus with the same entries but different grub parameters: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/installer/cd-dvd/iso-image.nix#L334 I'll try to understand how GRUB2 submenus work and how u-root might want to support them.

I attached the NixOS installation iso to QEMU and use the boot command to boot it. The generated boot menu shows some boot entries many times.

I have the entries:

LABEL boot
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4
INITRD /boot/initrd

# A variant to boot with 'nomodeset'
LABEL boot-nomodeset
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 nomodeset
INITRD /boot/initrd

# A variant to boot with 'copytoram'
LABEL boot-copytoram
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (copytoram)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 copytoram
INITRD /boot/initrd

# A variant to boot with verbose logging to the console
LABEL boot-debug
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (debug)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 loglevel=7
INITRD /boot/initrd

# A variant to boot with a serial console enabled
LABEL boot-serial
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (serial console=ttyS0,115200n8)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 console=ttyS0,115200n8
INITRD /boot/initrd

LABEL memtest
MENU LABEL Memtest86+
LINUX /boot/memtest.bin
APPEND

The following are added by u-root 10 times each:

NixOS 21.05pre289339.83d907fd760 Installer
NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)
NixOS 21.05pre289339.83d907fd760 Installer (copytoram)
NixOS 21.05pre289339.83d907fd760 Installer (debug)```

but those two are added only once:

NixOS 21.05pre289339.83d907fd760 Installer (serial console=ttyS0,115200n8)
Memtest86+
~/# boot
2021/06/17 07:48:24 Booting from the following block devices: [BlockDevice(name=vda, fs_uuid=455d7919-56a4-45b0-8c47-51955d53a599) BlockDevice(name=vdb, fs_uuid=) BlockDevice(name=vdb1, fs_uuid=fabe-1afd) BlockDevice(name=vdc, fs_uuid=) BlockDevice(name=vdc1, fs_uuid=) BlockDevice(name=vdc2, fs_uuid=1234-5678)]
ISOFS: Unable to identify CD-ROM format.
ISOFS: Unable to identify CD-ROM format.
ISOFS: Unable to identify CD-ROM format.
ISOFS: Unable to identify CD-ROM format.
ISOFS: Unable to identify CD-ROM format.
ISOFS: Unable to identify CD-ROM format.
2021/06/17 07:48:24 [grub] Got config file file:///tmp/u-root-mounts569132526/vdc/EFI/boot/grub.cfg
2021/06/17 07:48:24 Got config file file:///tmp/u-root-mounts569132526/vdc/isolinux/isolinux.cfg:
SERIAL 0 115200
TIMEOUT 100
UI vesamenu.c32
MENU TITLE NixOS
MENU BACKGROUND /isolinux/background.png
MENU RESOLUTION 800 600
MENU CLEAR
MENU ROWS 6
MENU CMDLINEROW -4
MENU TIMEOUTROW -3
MENU TABMSGROW  -2
MENU HELPMSGROW -1
MENU HELPMSGENDROW -1
MENU MARGIN 0

#                                FG:AARRGGBB  BG:AARRGGBB   shadow
MENU COLOR BORDER       30;44      #00000000    #00000000   none
MENU COLOR SCREEN       37;40      #FF000000    #00E2E8FF   none
MENU COLOR TABMSG       31;40      #80000000    #00000000   none
MENU COLOR TIMEOUT      1;37;40    #FF000000    #00000000   none
MENU COLOR TIMEOUT_MSG  37;40      #FF000000    #00000000   none
MENU COLOR CMDMARK      1;36;40    #FF000000    #00000000   none
MENU COLOR CMDLINE      37;40      #FF000000    #00000000   none
MENU COLOR TITLE        1;36;44    #00000000    #00000000   none
MENU COLOR UNSEL        37;44      #FF000000    #00000000   none
MENU COLOR SEL          7;37;40    #FFFFFFFF    #FF5277C3   std

DEFAULT boot

LABEL boot
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4
INITRD /boot/initrd

# A variant to boot with 'nomodeset'
LABEL boot-nomodeset
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 nomodeset
INITRD /boot/initrd

# A variant to boot with 'copytoram'
LABEL boot-copytoram
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (copytoram)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 copytoram
INITRD /boot/initrd

# A variant to boot with verbose logging to the console
LABEL boot-debug
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (debug)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 loglevel=7
INITRD /boot/initrd

# A variant to boot with a serial console enabled
LABEL boot-serial
MENU LABEL NixOS 21.05pre289339.83d907fd760 Installer (serial console=ttyS0,115200n8)
LINUX /boot/bzImage
APPEND init=/nix/store/mqsfdrqf3p7hqds3j4p7fhk3r74d9s9p-nixos-system-nixos-21.05pre289339.83d907fd760/init root=LABEL=nixos-minimal-21.05-x86_64 boot.shell_on_fail loglevel=4 console=ttyS0,115200n8
INITRD /boot/initrd

LABEL memtest
MENU LABEL Memtest86+
LINUX /boot/memtest.bin
APPEND

Welcome to LinuxBoot's Menu

Enter a number to boot a kernel:

01. NixOS 21.05pre289339.83d907fd760 Installer

02. NixOS 21.05pre289339.83d907fd760 Installer

03. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

04. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

05. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

06. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

07. NixOS 21.05pre289339.83d907fd760 Installer (debug)

08. NixOS 21.05pre289339.83d907fd760 Installer (debug)

09. NixOS 21.05pre289339.83d907fd760 Installer

10. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

11. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

12. NixOS 21.05pre289339.83d907fd760 Installer (debug)

13. NixOS 21.05pre289339.83d907fd760 Installer

14. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

15. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

16. NixOS 21.05pre289339.83d907fd760 Installer (debug)

17. NixOS 21.05pre289339.83d907fd760 Installer

18. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

19. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

20. NixOS 21.05pre289339.83d907fd760 Installer (debug)

21. NixOS 21.05pre289339.83d907fd760 Installer

22. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

23. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

24. NixOS 21.05pre289339.83d907fd760 Installer (debug)

25. NixOS 21.05pre289339.83d907fd760 Installer

26. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

27. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

28. NixOS 21.05pre289339.83d907fd760 Installer (debug)

29. NixOS 21.05pre289339.83d907fd760 Installer

30. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

31. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

32. NixOS 21.05pre289339.83d907fd760 Installer (debug)

33. NixOS 21.05pre289339.83d907fd760 Installer

34. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

35. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

36. NixOS 21.05pre289339.83d907fd760 Installer (debug)

37. NixOS 21.05pre289339.83d907fd760 Installer

38. NixOS 21.05pre289339.83d907fd760 Installer (nomodeset)

39. NixOS 21.05pre289339.83d907fd760 Installer (copytoram)

40. NixOS 21.05pre289339.83d907fd760 Installer (debug)

41. NixOS 21.05pre289339.83d907fd760 Installer (serial console=ttyS0,115200n8)

42. Memtest86+

43. Reboot

44. Enter a LinuxBoot shell

Enter an option ('01' is the default, 'e' to edit kernel cmdline):
 >

To Reproduce

qemu-system-x86_64 \
    -L . \
    -net none \
    -enable-kvm \
    -nographic \
    -m 1024M \
    -drive file=nixos-21.05pre289339.83d907fd760-x86_64-linux.iso,format=raw,if=virtio,media=cdrom,id=hd2 \
    -kernel bzImage \
    -initrd initramfs.linux_amd64.cpio

where

Expected behavior I expect each grub entry to show up in the u-root boot menu only once.

Additional context Add any other context about the problem here.

JohnAZoidberg commented 3 years ago

Apparently one other duplication of the entries is because there's grub and isolinux configuration. Perhaps u-root should highlight where the entries were extracted from.

JohnAZoidberg commented 3 years ago

Maybe also make it possible to view the config (kernel, initrd, cmdline) of each option before executing it, to verify it's the right one. Currently, it seems, you have to tell by the name.

walterchris commented 2 years ago

Does this relate to #2237 ?

johnnylinwiwynn commented 2 years ago

Does this relate to #2237 ?

https://github.com/u-root/u-root/pull/2237 only focuses on improving GRUB BLSCFG support, the duplicate boot menu entries is still there.