Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: January 7, 2024
Many older and more specialized Linux distributions have primitive installation mechanics that either have no front end at all or use a terminal user interface (TUI). Installing a current major distribution is usually a much more comfortable experience, involving not only a full-fledged graphical user interface (GUI) but also many conveniences in the form of automated steps. Still, some automation may cause issues in the case of edge cases.
In this tutorial, we talk about storage medium detection and the bootloader installation during the setup of some Linux distributions. First, we briefly refresh our knowledge about bootloaders. After that, we turn to bootloader installation in general. Finally, we explore a common error with one of the most widely used bootloaders for Linux.
We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.2.15. It should work in most POSIX-compliant environments unless otherwise specified.
After performing the installation or inserting the medium of any operating system (OS), the machine BIOS or UEFI should be aware of the path to its kernel and how to run it. This is the role of a bootloader:
Notably, most bootloaders support many operating systems. On the other hand, some operating systems only have a single native boot system. However, Linux supports and is supported by many bootloaders.
Still, perhaps the most widely used one is the GRand Unified Bootloader (GRUB). In fact, although systemd can also be used as the bootloader, GRUB is still the main or only option in most major Linux distribution installation media.
During most Linux installation procedures, there is a step for setting up a bootloader. It’s optional, as our system might already have one, but there is usually at least a single option just in case.
As already stated, due to its ubiquity, GRUB is part of most installers and already deployed Linux systems. In fact, many automatic actions may run during the installation of bootloaders:
Despite this, there are cases when the automated installer may not properly detect the correct drive, partition, or filesystem to install the bootloader on.
When installing a bootloader like GRUB, the system attempts to detect two main conditions:
Notably, while the second decision might look more important, the first one can be fatal.
Usually, the first sector of a storage medium is called a Master Boot Record (MBR) and is reserved for the partition table. Yet, we can also install a bootloader like GRUB in the MBR, thereby overwriting any other bootloader.
Still, if GRUB hasn’t properly detected all bootable parts of the system, we might end up with a new bootloader that doesn’t recognize any operating system before the one it was installed with.
Because of this, there are systems that leave the original bootloader intact, but add an entry to it for a new partition. Then, we ensure GRUB or a new bootloader is installed in the first sector of that partition. This way, we ensure that at least our original OS is bootable.
Since OS installation usually involves an external medium like a compact disk (CD) or a USB flash drive, that might be present on the system during bootloader installation.
This fact can present challenges with the proper medium detection. For example, let’s assume that, when installing Linux, we have three drives available:
At the end of the setup, the installer should decide where to install the bootloader. Often, the sane default action is to pick the first fixed drive. In this case, that would be /dev/sdb, which might be a mistake, since that storage medium probably already has a bootloader for the current OS there.
Critically, newer portable drives have controllers that are similar or equivalent to the ones for fixed storage, further compounding the problem. In that case, we might end up with two problems:
To avoid such issues, we usually set the local drive or partition that we install Linux on to hold the bootloader. If needed, we then chain the bootloaders, so the main one loads any secondary ones.
In this article, we talked about bootloaders, how they are installed, and three main things to watch out for when installing:
In conclusion, although many of the steps to install a Linux system are now automated, it’s still part of best practices to ensure each step has detected the proper context and actions before proceeding.