Unified kernel image: Difference between revisions

From JookWiki
(Add boot order)
(Finish first revision)
Line 2: Line 2:


== Overview ==
== Overview ==
The modern Linux boot process follows these steps:
Traditionally bootloaders provided the following features:


# UEFI starts up
* The ability to select something to boot
# UEFI loads a bootloader
* The ability to boot applications from a filesystem
# The bootloader shows a splash screen
# The bootloader loads microcode
# The bootloader loads an initramfs
# The bootloader sets up kernel arguments
# The bootloader loads the Linux kernel
# Linux configures itself using the kernel arguments
# Linux runs the init system in the initramfs
# The init system loads drivers
# The init system shows a splash screen
# The init system mounts partitions
# The init system switches to the actual root


- EFI stub
Before UEFI these features were useful as BIOS machines could only specify a drive to boot from, not individual kernels or applications.


If you tend to only boot one kernel automatically when using Linux, yo
On UEFI systems using a bootloader with these features is redundant and wastes time.


https://systemd.io/BOOT_LOADER_SPECIFICATION/#type-2-efi-unified-kernel-images
With the help of an stub you can package everything needed to boot your Linux system in to a single UEFI application. This is called a 'Unified Kernel Image', defined in [https://systemd.io/BOOT_LOADER_SPECIFICATION/ systemd's Boot Loader Specification].


systemd's efistub + sections + whatever
There are currently two main stubs you can use to boot Linux:


kernel efistub
* The kernel's [https://docs.kernel.org/admin-guide/efi-stub.html EFI Boot Stub] which is only useful for kernel developers
* [https://www.freedesktop.org/software/systemd/man/systemd-stub.html systemd-stub] which can be used to package pre-built components


https://www.freedesktop.org/software/systemd/man/systemd-stub.html
Because systemd-stub can be used with existing components provided by your Linux distro, it's possible to create a script that builds a Unified Kernel Image yourself.


== Ubuntu install guide ==
== Debian and Ubuntu install guide ==
apt install
chmod +x /etc/kernel/postinst.d/zz-unified


Step 1: Copy and paste this file to 'install.sh' somewhere on your computer.


#!/bin/bash
DISK=/dev/vda
apt install systemd-boot binutils efibootmgr
cat /proc/cmdline > /boot/cmdline.txt
mkdir -p /boot/efi/EFI/unified
cat <<EOF >/etc/kernel/postinst.d/zz-unified
#!/bin/bash
test -e /boot/splash.bmp && SPLASH='--add-section .splash=/boot/splash.bmp --change-section-vma .splash=0x40000'
exec objcopy \
  --add-section .osrel=/usr/lib/os-release --change-section-vma .osrel=0x20000 \
  --add-section .cmdline=/boot/cmdline.txt --change-section-vma .cmdline=0x30000 \
  \$SPLASH \
  --add-section .linux=/boot/vmlinuz --change-section-vma .linux=0x2000000 \
  --add-section .initrd=/boot/initrd.img --change-section-vma .initrd=0x3000000 \
  "/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "/boot/efi/EFI/unified/bootx64.efi"
EOF
chmod +x /etc/kernel/postinst.d/zz-unified
/etc/kernel/postinst.d/zz-unified
efibootmgr -c -d $DISK -L "Linux Unified" -l "\EFI\unified\bootx64.efi"


<nowiki>#</nowiki>!/bin/bash
Step 2: Change <code>DISK=/dev/vda</code> to the disk with your UEFI partition on it. This will probably be something like <code>DISK=/dev/sda</code>. Typing <code>lsblk</code> might help you find this.


echo "root=UUID=b1e14117-8e88-4db6-9a6f-6bea5a0605e9 ro quiet bgrt_disable vt.handoff=7" > /tmp/cmdline.txt
Step 3: Run <code>bash install.sh</code> as root in the directory containing the install.sh file.


objcopy \
Step 4: Reboot and enter UEFI setup. Run the new "Linux Unified" option and see if it works. If it does, set it to be the default option.


   --add-section .osrel="/usr/lib/os-release" --change-section-vma .osrel=0x20000 \
Step 5: Optionally, add a splash file in <code>/boot/splash.bmp</code> and run <code>/etc/kernel/postinst.d/zz-unified</code> . You may also want to add <code>bgrt_disable</code> to the kernel arguments to hide your computer's UEFI logo.
 
   --add-section .cmdline="/tmp/cmdline.txt" --change-section-vma .cmdline=0x30000 \
 
   --add-section .splash="/boot/splash.bmp" --change-section-vma .splash=0x40000 \
 
   --add-section .linux="/boot/vmlinuz" --change-section-vma .linux=0x2000000 \
 
   --add-section .initrd="/boot/initrd.img" --change-section-vma .initrd=0x3000000 \
 
   "/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "/boot/efi/EFI/unified/bootx64.efi"
 
 
- splash bmp can be anything
 
efibootmgr -c -d /dev/vda -p 2 -L "unified" -l "\efi\unified\bootx64.efi"
 
mkdir /boot/efi/EFI/unified
 
 
 
root@jookia-Standard-PC-Q35-ICH9-2009:/boot/efi/EFI/unified# efibootmgr -c -d /dev/vda -p 2 -L "unified" -l "\efi\unified\bootx64.efi" -^C
 
root@jookia-Standard-PC-Q35-ICH9-2009:/boot/efi/EFI/unified# efibootmgr -o 4
 
BootCurrent: 0004
 
Timeout: 3 seconds
 
BootOrder: 0004
 
Boot0000* UiApp
 
Boot0001* UEFI Misc Device
 
Boot0002* EFI Internal Shell
 
Boot0003* ubuntu
 
Boot0004* unified
 
Boot0005* UEFI PXEv4 (MAC:5254003512E6)

Revision as of 01:29, 24 October 2022

If you use UEFI and only boot a single Linux kernel, you don't need a bootloader like systemd-boot or GRUB. Instead you can create a Unified Kernel Image and boot that directly. This article documents how.

Overview

Traditionally bootloaders provided the following features:

  • The ability to select something to boot
  • The ability to boot applications from a filesystem

Before UEFI these features were useful as BIOS machines could only specify a drive to boot from, not individual kernels or applications.

On UEFI systems using a bootloader with these features is redundant and wastes time.

With the help of an stub you can package everything needed to boot your Linux system in to a single UEFI application. This is called a 'Unified Kernel Image', defined in systemd's Boot Loader Specification.

There are currently two main stubs you can use to boot Linux:

  • The kernel's EFI Boot Stub which is only useful for kernel developers
  • systemd-stub which can be used to package pre-built components

Because systemd-stub can be used with existing components provided by your Linux distro, it's possible to create a script that builds a Unified Kernel Image yourself.

Debian and Ubuntu install guide

Step 1: Copy and paste this file to 'install.sh' somewhere on your computer.

#!/bin/bash
DISK=/dev/vda
apt install systemd-boot binutils efibootmgr
cat /proc/cmdline > /boot/cmdline.txt
mkdir -p /boot/efi/EFI/unified
cat <<EOF >/etc/kernel/postinst.d/zz-unified
#!/bin/bash
test -e /boot/splash.bmp && SPLASH='--add-section .splash=/boot/splash.bmp --change-section-vma .splash=0x40000'
exec objcopy \
  --add-section .osrel=/usr/lib/os-release --change-section-vma .osrel=0x20000 \
  --add-section .cmdline=/boot/cmdline.txt --change-section-vma .cmdline=0x30000 \
  \$SPLASH \
  --add-section .linux=/boot/vmlinuz --change-section-vma .linux=0x2000000 \
  --add-section .initrd=/boot/initrd.img --change-section-vma .initrd=0x3000000 \
  "/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "/boot/efi/EFI/unified/bootx64.efi"
EOF
chmod +x /etc/kernel/postinst.d/zz-unified
/etc/kernel/postinst.d/zz-unified
efibootmgr -c -d $DISK -L "Linux Unified" -l "\EFI\unified\bootx64.efi"

Step 2: Change DISK=/dev/vda to the disk with your UEFI partition on it. This will probably be something like DISK=/dev/sda. Typing lsblk might help you find this.

Step 3: Run bash install.sh as root in the directory containing the install.sh file.

Step 4: Reboot and enter UEFI setup. Run the new "Linux Unified" option and see if it works. If it does, set it to be the default option.

Step 5: Optionally, add a splash file in /boot/splash.bmp and run /etc/kernel/postinst.d/zz-unified . You may also want to add bgrt_disable to the kernel arguments to hide your computer's UEFI logo.