Motivation
I have installed dual or multiple systems on my computer before, but I have always been confused about the boot entries of multiple systems, often encountering problems such as missing boot entries and duplicate boot entries. This time I plan to study the configuration of multi-system boot entries in depth, including the settings of boot loaders, EFI/Grub/rEFInd, and how to customize the theme of multi-system boot loaders.
Operating System Boot Process
The computer boot process is mainly divided into 4 steps: 1. Power on; 2. BIOS/UEFI boot; 3. Load boot entries; 4. Operating system boot. Steps 1 and 4 generally do not require user intervention, while steps 2 and 3 require user configuration. So here we mainly introduce the content of steps 2 and 3.
BIOS/UEFI Boot
BIOS (Basic Input/Output System) and UEFI (Unified Extensible Firmware Interface) are the firmware of the computer, responsible for booting the computer and loading the operating system. BIOS is the old boot method, and UEFI is the new boot method, UEFI has more functions and better performance than BIOS. Whether UEFI or BIOS, they are firmware on the motherboard.
BIOS Boot Process
BIOS is generally used on older computers, and its boot process is relatively fixed, with low user customization. It is mainly divided into the following steps:
-
BIOS POST (Power-On Self-Test) self-test
-
BIOS reads the boot program in the MBR (Master Boot Record)
-
The boot program reads the partition table and finds the active partition
-
The boot program reads the boot sector of the active partition and loads the operating system
The location of the MBR is fixed, generally in the first sector of the hard disk, with a size of 512 bytes. The MBR contains the partition table and the boot program, the partition table records the partition information of the hard disk, and the boot program is responsible for loading the operating system.
UEFI Boot Process
UEFI is also a kind of BIOS, generally used on newer computers. Its boot process is more flexible, with more functions and higher user customization. Although UEFI has more functions, its process is more unified, mainly divided into the following steps:
-
UEFI POST (Power-On Self-Test) self-test
-
UEFI reads the boot program in the EFI partition
-
The boot program reads the boot file in the EFI partition and loads the operating system
Compared with BIOS, UEFI uses the EFI partition, and the location of the EFI partition is not fixed, and there can even be multiple EFI partitions on one hard disk.
The boot program and boot file of UEFI are stored in the EFI partition, including the boot program, boot file, driver program, etc. As long as the boot file meets the UEFI specification, whether it is the boot program of Windows, Linux, or macOS, and no matter where the boot file is placed in the EFI partition, UEFI can find and load it.
Since UEFI is more flexible and updated, the following multi-system boot entry configuration is based on UEFI.
Concept Distinction: Boot Loader, Boot Entry, Boot File
Before discussing the configuration of multi-system boot entries, let’s first sort out the concepts of boot loader, boot entry, and boot file.
-
Boot Loader: Responsible for loading the operating system’s boot program, each boot program is a boot entry. Common boot loaders include Grub (Grub2), rEFInd, Clover, etc.
-
Boot Entry: Each operating system loaded by the boot loader is a boot entry, and each boot entry corresponds to an operating system. The boot entry contains the operating system’s boot file, kernel, driver program, etc.
-
Boot File: The boot file in the boot entry is the boot program of the operating system, responsible for loading the operating system. The boot file of Windows is
bootmgfw.efi
, and the boot file of Linux isvmlinuz
.
On this basis, let’s take a look at how UEFI starts the operating system.
-
Each boot loader corresponds to a
.efi
file, and this.efi
file is the boot file of the boot loader, responsible for loading the boot loader. After UEFI completes the self-test, it reads all.efi
files in the EFI partition. If you enter the BIOS settings at this time, you will see the boot entries in the EFI partition. These boot entries have a priority sequence, and their order can be adjusted in the BIOS settings.For example, the highest priority boot entry in the figure below is
rEFInd
, followed byWindows Boot Manager
, and finallyubuntu
. When the computer starts, it will first try to loadrEFInd
, ifrEFInd
does not exist, it will try to loadWindows Boot Manager
, and so on. -
If
rEFInd
is successfully loaded,rEFInd
will read therefind.conf
configuration file in the EFI partition, generate a boot menu according to the configuration file, and display it on the screen. Each menu item corresponds to a boot entry, and boot entries are generally divided into two types: operating system kernel images and boot loaders.- Operating System Kernel Image: Corresponds to the operating system’s kernel file, such as Linux’s
vmlinuz
file, Windows’sbootmgfw.efi
file. - Boot Loader: Corresponds to the
.efi
file of the boot loader, such as Grub2’sgrubx64.efi
file.
- Operating System Kernel Image: Corresponds to the operating system’s kernel file, such as Linux’s
-
After the user selects a menu item,
rEFInd
will load the corresponding boot entry. If the boot entry is an operating system kernel image,rEFInd
will directly load the operating system; if the boot entry is a boot loader,rEFInd
will load the boot loader, and the boot loader will load the operating system. -
If the boot loader wants to start a Linux system, it will first load the Linux kernel file
vmlinuz-xxx
, then load the Linux initial file system fileinitrd-xxx.img
orinitramfs-xxx.img
, and finally load the Linux system. These two files are in the/boot
directory of the Linux system.
Boot Entry Configuration During Multi-System Installation
As mentioned above, the boot files of multiple systems are stored in the same EFI partition, so when installing multiple systems, each system will create a boot file in the EFI partition, so there will be multiple boot files, each boot file corresponds to a system. In this way, you can start different systems by selecting different boot files.
When installing multiple operating systems, we can put the boot files of each system in the same EFI partition, or put the boot files of each system in different EFI partitions. Both methods have their advantages and disadvantages, and the specific choice depends on personal preferences.
-
Same EFI Partition
In this case, the hard disk partition structure is as follows:
Advantages: Easy to manage, only one EFI partition is needed, no need for multiple EFI partitions. Easier to configure boot loaders.
Disadvantages: All boot files are in one EFI partition. If the EFI partition is damaged, all systems will not be able to start.
The specific operation of installing multiple systems is as follows:
- Hard Disk Partition: Divide an EFI partition on the hard disk, with a size of at least 1GB, and format it as FAT32. Other partitions can be determined according to the requirements of each operating system, for example, Windows generally requires at least 52GB of space, and Linux generally requires at least 20GB of space.
- Install Windows: If you are installing multiple operating systems with both Windows and Linux, it is recommended to install Windows first. Because the boot program of Windows will overwrite the boot program of other previously installed operating systems, causing the previously installed Linux to fail to start. When installing Windows, select the partition for Windows, and Windows will automatically create a boot file in the EFI partition.
- Install Each Linux: For each Linux system, when installing, select custom partitioning, manually mount the EFI partition to
/boot/efi
, and mount the partition prepared for Linux to/
, and create a swap partition and/home
partition if necessary. After installation, Linux will create a boot file in the EFI partition. - Configure Boot Loader: After installing multiple operating systems, we need to configure the boot loader so that we can select the operating system to start. These contents will be detailed later.
-
Different EFI Partitions
In this case, the hard disk partition structure is as follows:
Advantages: Each system has its own EFI partition, which does not interfere with each other. If the EFI partition of one system is damaged, other systems are not affected.
Disadvantages: Multiple EFI partitions are needed, which are more cumbersome to manage. The boot files of each system are in different EFI partitions, making it difficult to configure boot loaders.
The specific operation of installing multiple systems is as follows:
- Hard Disk Partition: Divide a space for each system on the hard disk. Do not create an EFI partition first, the EFI partition will be created in each system’s partition when installing the system.
- Install Windows: When installing Windows, select the partition for Windows, and Windows will automatically create an EFI partition in that partition.
- Install Linux: For each Linux system, when installing, select the partition for this system, create an EFI partition of about 200MB in this partition, mount point is
/boot/efi
; mount the remaining space as a partition prepared for Linux, mount point is/
; create a swap partition and/home
partition if necessary. - Configure Boot Loader: After installing multiple operating systems, we need to configure the boot loader so that we can select the operating system to start. These contents will be detailed later.
Boot Directory Structure
Let’s take a look at the structure of the boot directory and what files are created when installing an operating system. Here, it is assumed that the boot files of multiple operating systems are placed in the same EFI partition. We have installed 3 operating systems: Windows, Ubuntu, and Arch Linux. Ubuntu uses Grub2 as the boot loader, and Arch Linux uses systemd-boot as the boot loader. We also installed rEFInd as the boot loader in the Ubuntu system.
-
EFI Partition
The EFI partition is a FAT32 formatted independent partition, generally with a size of about 1GB. The EFI partition mainly contains the boot files of various boot loaders.
The directory structure of the EFI partition is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
EFI ├── Boot │ └── bootx64.efi ├── Microsoft │ └── Boot │ ├── BCD │ ├── BCD.LOG │ ├── BCD.LOG1 │ ├── BCD.LOG2 │ ├── bootmgfw.efi │ ├── bootmgr.efi │ └── boot.stl ├── systemd │ └── systemd-bootx64.efi └── ubuntu ├── grub.cfg ├── grubx64.efi ├── mmx64.efi └── shimx64.efi
EFI/Boot/bootx64.efi
: The default boot file of UEFI, generally points to the boot file of WindowsEFI/Microsoft/Boot/bootmgfw.efi
.EFI/Microsoft/Boot/bootmgfw.efi
: The boot file of Windows, responsible for loading the Windows operating system.EFI/systemd/systemd-bootx64.efi
: The boot file of systemd-boot, responsible for loading the Linux operating system.EFI/ubuntu/grubx64.efi
: The boot file of Grub2, responsible for loading the Linux operating system.EFI/ubuntu/shimx64.efi
: The boot file of Secure Boot, used to load Grub2.EFI/ubuntu/mmx64.efi
: The boot file of Grub2, used to load Grub2.EFI/ubuntu/grub.cfg
: The configuration file of Grub2, containing the configuration of each boot entry.
-
Install Operating System
When installing Ubuntu, select custom partitioning, manually mount the EFI partition to
/boot/efi
, and mount the partition prepared for Linux to/
. After installation, Ubuntu will create theEFI/ubuntu
directory in the EFI partition, which contains the boot files and configuration files of Grub2. Ubuntu will create two files in the/boot
directory:vmlinuz
andinitrd.img
, these two files are the kernel file and initial file system file of Linux. The boot file of Grub2 will recognize these two files and create boot entries, select the corresponding boot entry at startup, and load the Ubuntu system.In the Ubuntu system, you can enter
/boot/efi/EFI
to view the directory structure of the EFI partition.1 2 3 4 5 6 7 8 9
/boot ├── efi │ └── EFI │ ├── Boot │ ├── Microsoft │ ├── systemd │ └── ubuntu ├── vmlinuz └── initrd.img
Note that although
/boot/efi/EFI
is under/boot
, the files in/boot/efi/EFI
are in the EFI partition, while the other files in/boot
are in the partition of the Ubuntu system. The filesvmlinuz
andinitrd.img
in/boot
cannot be accessed in other systems, but if other systems also mount the EFI partition, then other systems can also access the files in/boot/efi/EFI
.If you continue to install more operating systems, the operating system will generally ask you to choose a boot loader, you can choose to use Grub2, systemd-boot, or other boot loaders. The configuration of the boot loader is introduced below.
Boot Loader
The boot loader (Boot Loader) is responsible for loading the operating system’s boot program, and each boot program is a boot entry (Boot Entry). Common boot loaders include Grub (Grub2), rEFInd, Clover, etc. Here we mainly introduce Grub2 and rEFInd.
Grub2
Grub2 (GNU GRand Unified Bootloader 2) is a commonly used boot loader on Linux systems, powerful, and supports multi-system boot. Grub2’s configuration file is /boot/grub/grub.cfg
, generally generated by the grub-mkconfig
command.
Grub2’s configuration file is relatively complex but powerful, and can customize various boot entries. Grub2’s theme can also be customized, but it is relatively complex.
Grub2 Configuration
-
Configuration File
Grub2’s main configuration file is
/boot/grub/grub.cfg
, which can be edited to configure Grub2’s boot entries.1
sudo vim /boot/grub/grub.cfg
However, it is generally not recommended to edit the
grub.cfg
file directly because this file is generated by thegrub-mkconfig
command. If you edit thegrub.cfg
file directly, it will be overwritten when updating Grub2 next time. It is recommended to configure Grub2 through the/etc/default/grub
file and the configuration files in the/etc/grub.d/
directory. -
Boot Entry Configuration
Grub2’s boot entry configuration is relatively complex, for example:
1 2 3 4 5
GRUB_DEFAULT=0 GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian` GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" GRUB_CMDLINE_LINUX=""
GRUB_DEFAULT
: The number of the default boot entry, starting from 0.GRUB_TIMEOUT
: The time the boot menu is displayed, in seconds.GRUB_DISTRIBUTOR
: The system’s distributor.GRUB_CMDLINE_LINUX_DEFAULT
: The default boot parameters of the Linux kernel.GRUB_CMDLINE_LINUX
: The boot parameters of the Linux kernel.
-
Customize Theme
Grub2’s theme can be customized, you can put the desired theme files in the
/boot/grub/themes
directory. The theme directory contains theme files, font files, background images, etc., and a configuration file namedtheme.txt
that can configure various properties of the theme.You can specify the theme file in the configuration file
/etc/default/grub
:1
GRUB_THEME="/boot/grub/themes/theme-name/theme.txt"
More Grub2 configurations can be found at Grub2 Configuration.
rEFInd
rEFInd is a UEFI-based boot loader, powerful, and supports multi-system boot. rEFInd’s configuration file is /boot/efi/EFI/refind/refind.conf
, generally generated by the refind-install
command.
rEFInd’s configuration file is relatively simple but powerful, and can customize various boot entries. rEFInd’s theme can also be customized, relatively simple.
rEFInd Installation
rEFInd installation is relatively simple, you can directly download the rEFInd installation package, unzip it to the EFI partition. You can also install rEFInd using the refind-install
command.
|
|
After installation, rEFInd will create the EFI/refind
directory in the EFI partition, which contains rEFInd’s boot files and configuration files.
rEFInd Configuration
rEFInd’s configuration file is /boot/efi/EFI/refind/refind.conf
, you can edit this file to configure rEFInd’s boot entries.
|
|
-
Boot Entry Configuration
rEFInd’s boot entry configuration is relatively simple, for example:
1 2 3
timeout 5 hideui singleuser scanfor manual,external
timeout
: The time the boot menu is displayed, in seconds.hideui
: Hide the boot menu and only display single-user mode.scanfor
: The way to scan boot entries, you can scan manually configured boot entries, or scan boot entries on external devices.
-
Customize Theme
rEFInd’s theme can be customized, you can put the desired theme files in the
/boot/efi/EFI/refind/theme
directory. The theme file is a CSS file, you can modify the color, font, background, etc.You can specify the theme file in the configuration file
/boot/efi/EFI/refind/refind.conf
:1
include themes/theme-name/theme.conf
-
Hide Boot Entries
Sometimes rEFInd will scan many unnecessary boot entries, for example, if you choose to use Grub2 as the boot loader when installing the system (Deepin, Fedora, etc. systems will install Grub2 by default, and it seems that you can’t cancel it during installation), then rEFInd will scan Grub2’s boot entries. You can hide these boot entries by configuring the file
/boot/efi/EFI/refind/refind.conf
. rEFInd can choose to ignore certain files (dont_scan_files
), tools (dont_scan_tools
), directories (dont_scan_dirs
), and volumes (dont_scan_volumes
).1 2
dont_scan_files shim.efi,mmx64.efi dont_scan_dirs ESP:/EFI/ubuntu,EFI/deepin,EFI/fedora
Troubleshooting
-
Install System on SD Card or USB Drive
- If your computer has an SD card slot, you can choose to install the system on the SD card.
- Similar to the method of installing multiple operating systems mentioned above, there are two EFI partition methods for installing the system on the SD card, one is to create an EFI partition on the SD card, and the other is to mount the EFI partition on the hard disk to
/boot/efi
when installing the system. - For the
/boot
partition, you can choose to create it directly on the SD card, or you can create a 500M ext4 partition on the hard disk first, and then mount it to/boot
when installing the system. The/boot
partition mainly stores kernel files and boot files, generally about 200M of space is enough, but if you choose to back up multiple kernels, you will need more space. Fedora system will limit the/boot
partition to no less than 512M. - If you choose to put the
/boot
partition on the SD card, you may encounter some security restrictions, for example, Secure Boot may prevent booting from the SD card. You need to register the registration key or disable Secure Boot. - I generally choose to put the EFI partition and
/boot
partition on the hard disk.
I haven’t tried installing the system on a USB drive yet, but the principle should be the same as installing it on an SD card.
-
Fedora Fails to Boot
After installing Fedora on the SD card, I encountered a problem that the system could not boot. rEFInd can scan Fedora’s kernel files
vmlinuz-xxx
andinitramfs-xxx.img
, but it will hang halfway through the boot process, reporting:1 2 3 4 5 6 7
Failed to switch root: Specified switch root path /sysroot does not seem to be an OS tree. os-release file is missing. initrd-switch-root.service: Main process exited, code=exited, status=1/FAILURE initrd-switch-root.service: Failed with result 'exit-code'. Failed to start initrd-switch-root.service: Switch Root. Startup finished in 1.073s (kernel) + 1.000s (initrd) + 1.000s (userspace) = 3.073s. initrd-switch-root.service: Triggering OnFailure= dependencies. Started emergency.service - Emergency Shell.
After checking in the Emergency Shell, I found that the
/sysroot
directory was empty, with no files. Normally, the/sysroot
directory should contain the files of the operating system after loading the kernel and initializing the file system.So this problem is because the SD card is not correctly mounted when starting the kernel and initializing the file system. You can manually mount the SD card in the Emergency Shell, then
exit
to exit the Emergency Shell, and the system can start normally.1 2
mount /dev/sda1 /sysroot exit
However, this is obviously not a good solution because you need to manually mount the SD card every time you start it. So the correct solution is to let rEFInd correctly mount the SD card when starting the kernel, which requires adding a
root
parameter to the boot options. You can add a menu item with anoptions
parameter in the rEFInd configuration file/boot/efi/EFI/refind/refind.conf
.1 2 3 4 5 6
menuentry "Fedora" { volume "SD-boot" loader /vmlinuz-xxx initrd /initramfs-xxx.img options "root=/dev/sda1" }
However, this solution is not very good because
- After each kernel update, you need to manually modify the rEFInd configuration file.
- If you have multiple kernels, this menu item cannot generate submenus automatically, you need to manually add menu items for each kernel.
So a better solution is to let the entire
boot
partition use theroot
option. The correct way is to create arefind_linux.conf
file in the root directory of the/boot
partition, and then add aroot
parameter in the rEFInd configuration file/boot/efi/EFI/refind/refind.conf
.1
"Boot with standard options" "root=/dev/sda1"
The problem is finally solved!