Jump to content

Bootloader: Difference between revisions

imported from old wiki
imported>Vater
(imported from old wiki)
 
(10 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{expansion|Explain what the bootloader does and which bootloader exists. Also explain Legacy Boot vs. UEFI and bootloaders for ARM platforms (with links)}}
== FAQ ==
 
=== What is a bootloader? ===
 
A bootloader is a type of software that manages the loading of the operating system (OS) on the computer at startup. It is responsible for preparing the system before passing control to the OS. Here are the types of bootloaders commonly used in NixOS:
* '''GRUB (Grand Unified Bootloader)''': Widely used, supports multiple operating systems, and provides a boot menu to select how the system boots.
* '''systemd-boot''': Simple UEFI-only bootloader, formerly known as gummiboot.
* '''EFISTUB''': Utilizes the UEFI firmware itself to boot the kernel, without the need for a traditional bootloader.
 
=== Legacy Boot vs UEFI ===


== FAQ ==
Legacy Boot and UEFI are two different methods of starting a computer. Legacy Boot, also known as BIOS, uses the Master Boot Record (MBR) to start the system, while UEFI (Unified Extensible Firmware Interface) uses the GUID Partition Table (GPT) and is the modern standard. UEFI supports larger hard drives, faster boot times, and more security features compared to Legacy Boot. Additionally, UEFI provides a better user interface and a more robust pre-boot environment.
 
=== Bootloaders for ARM platforms ===
 
On ARM platforms, the bootloader options can vary significantly based on the architecture and specific hardware. Common bootloaders include:
* '''U-Boot''': Universal Boot Loader, widely used in ARM devices.
* '''EFI ARM bootloaders''': Some ARM devices support UEFI, allowing the use of standard UEFI bootloaders like GRUB or systemd-boot.
 
For more detailed information on NixOS and ARM architecture, refer to the [https://nixos.wiki/wiki/NixOS_on_ARM NixOS on ARM] docs.


=== Am I booted in Legacy or UEFI? ===
=== Am I booted in Legacy or UEFI? ===
Line 24: Line 41:
</syntaxhighlight>
</syntaxhighlight>


=== Limiting amount of entries with grub ===
=== Limiting amount of entries with grub or systemd-boot ===


The grub module has an option to limit the number of configurations made available in the boot partition, {{nixos:option|boot.loader.grub.configurationLimit}}. Setting this to a lower amount than the default may help reduce the occasions where too many different kernels and initrds are added to the <tt>/boot</tt> partition or <tt>ESP</tt>.
The grub and systemd-boot modules has an option to limit the number of configurations made available in the boot partition, {{nixos:option|boot.loader.grub.configurationLimit}} or {{nixos:option|boot.loader.systemd-boot.configurationLimit}}. Setting this to a lower amount than the default may help reduce the occasions where too many different kernels and initrds are added to the <tt>/boot</tt> partition or <tt>ESP</tt>.
 
=== Why I still boot into systemd-boot after switching to grub in my configuration? ===
 
Make sure to check if in your boot order, '''NixOS-bootloader''' is the default / first option.


=== Keeping kernels/initrd on the main partition ===
=== Keeping kernels/initrd on the main partition ===
{{warning|1=This solution is broken starting with GRUB 2.04. It is unknown whether its a GRUB issue or NixOS issue. Refer to [https://github.com/NixOS/nixpkgs/issues/61718]}}


For these instructions, GRUB will be required; systemd-boot cannot reference boot files from the root partition. This will differ from the default expectation of NixOS on EFI, where <tt>/boot/</tt> is the <tt>ESP</tt>.
For these instructions, GRUB will be required; systemd-boot cannot reference boot files from the root partition. This will differ from the default expectation of NixOS on EFI, where <tt>/boot/</tt> is the <tt>ESP</tt>.
Line 53: Line 72:
== Troubleshooting ==
== Troubleshooting ==


=== Re-installing the bootloader ===
=== Kernel initramfs parameters for troubleshooting boot issues ===
 
* Something happened, and the bootloader doesn't work as expected.
* The ESP was re-made and nothing was kept
* The ESP wasn't mounted and I want to re-generate the menu entries
 
==== From a running system ====


<syntaxhighlight lang="bash">
You can add these flags to systemd-boot and grub boot entries via using the <code>e</code> command to edit the currently selected entry, then use arrow keys to move curser and type your additions in the entry area.  Type return or <code>F10</code> (Grub) to boot the entry.
sudo nixos-rebuild --install-bootloader switch
</syntaxhighlight>


kernel command line options to help debugging (reached via e on the grub boot menu)
* <code>boot.trace</code> uses <code>set -x</code> to trace the shell scripts.
 
* <code>boot.shell_on_fail</code> allows for root shell if failure to boot. ''Requires root password.''
* <code>boot.trace</code> uses set -x to trace the shell scripts.
* <code>boot.shell_on_fail</code> allows for a shell if failure to boot.
* <code>boot.debug1</code> allows shell and stops right away on failure.
* <code>boot.debug1</code> allows shell and stops right away on failure.
* <code>boot.debug1devices</code> stop after loading modules and creating device nodes.
* <code>boot.debug1devices</code> stop after loading modules and creating device nodes.
* <code>boot.debug1mounts</code> stop after mounting file systems.
* <code>boot.debug1mounts</code> stop after mounting file systems.
* <code>boot.panic_on_fail</code> panics on failure.
* <code>boot.panic_on_fail</code> panics on failure.
* <code>console=</code> set the console to something
* <code>console=</code> set the console to something.
* <code>root=</code> If a root device is specified on the kernel command line, make it available through the symlink /dev/root.
* <code>root=</code> If a root device is specified on the kernel command line, make it available through the symlink /dev/root.
* <code>findiso=</code> if an iso name is supplied, try to find the device where the iso resides on.
* <code>findiso=</code> if an iso name is supplied, try to find the device where the iso resides on.
* <code>copytoram</code> Skip mounting the ISO and copy its content to a tmpfs.
* <code>copytoram</code> Skip mounting the ISO and copy its content to a tmpfs.
=== Re-installing the bootloader ===
* Something happened, and the bootloader doesn't work as expected.
* The ESP was re-made and nothing was kept.
* The ESP wasn't mounted and I want to re-generate the menu entries.
==== From a running system ====
<syntaxhighlight lang="bash">
sudo nixos-rebuild --install-bootloader boot
</syntaxhighlight>


==== From an installation media ====
==== From an installation media ====


Booting from the installation media, mount the root partition, and the boot partition under <code>/mnt</code>. Next, bind system virtual file systems under <code>/mnt</code>. Finally, run the [https://github.com/NixOS/nixpkgs/blob/e140d71d6330786c40b4bd9c0d59af7ad1a5e86a/nixos/modules/installer/tools/nixos-install.sh#L191-L192 command that the installer would run]. This will re-install the bootloader.
Booting from the installation media, mount the root partition under <code>/mnt</code> and the boot partition under <code>/mnt/boot</code>. Next, enter the installed system with <code>nixos-enter</code>, or by manually binding the virtual filesystems and then calling <code>chroot</code>. Finally, run the [https://github.com/NixOS/nixpkgs/blob/e140d71d6330786c40b4bd9c0d59af7ad1a5e86a/nixos/modules/installer/tools/nixos-install.sh#L191-L192 command that the installer would run]. This will re-install the bootloader.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
mount /dev/[root partition] /mnt
mount /dev/[root partition] /mnt
mount /dev/[boot partition] /mnt/boot
</syntaxhighlight>
</syntaxhighlight>
With <code>nixos-enter</code>:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
mount /dev/[boot partition] /mnt/boot
nixos-enter
NIXOS_INSTALL_BOOTLOADER=1 /nix/var/nix/profiles/system/bin/switch-to-configuration boot
</syntaxhighlight>
</syntaxhighlight>


Create a file called foo.sh and paste these lines into it:
Or manually:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 97: Line 123:
</syntaxhighlight>
</syntaxhighlight>


Make it executable with chmod and then run it.
Tip: Be patient, it may take some times to re-install the bootloader for you.


Tip: Be patient, it may take some times to re-install the bootloader for you.
Tip: If the installation command fails with "<code>systemd-boot not installed in ESP</code>", try running <code>bootctl install</code>.
 
Tip: If you've replaced your drives, or have otherwise re-created the <code>/mnt/boot</code> partition, you may want to keep using the same UUID for it.  You can use <code>mlabel</code> from the <code>mtools</code> package for this.


=== New generations are not in the boot menu ===
=== New generations are not in the boot menu ===
Line 163: Line 191:
</nowiki>}}
</nowiki>}}
to the grub section of your configuration.
to the grub section of your configuration.
=== Manually adding EFI boot entry ===
If you somehow lost all EFI boot entries (e.g. by resetting your BIOS), then you can manually add it again.
Firstly, find out which disk and partition <tt>/EFI/NixOS-boot/grubx64.efi</tt> is located (can be x86 or something else), which in the example will be <tt>/dev/sda1</tt>.
Then use efibootmgr to add the entry again, where the disk device is specified, with the partition number followed by the index and finally the path to the grub boot loader.
<syntaxhighlight lang="bash">
efibootmgr -c -d /dev/sda -p 1 -L NixOS-boot -l '\EFI\NixOS-boot\grubx64.efi'
</syntaxhighlight>
Remember to specify the location using single quotes, otherwise it might try to escape them instead.


If you have some problems with installation, report them on [https://matrix.to/#/#nix:nixos.org #nix:nixos.org on Matrix].
If you have some problems with installation, report them on [https://matrix.to/#/#nix:nixos.org #nix:nixos.org on Matrix].
[[Category:Booting]]