Red Hat Enterprise Linux 9 Boot Process and Systemd Targets¶
Red Hat Enterprise Linux 9 Boot Process¶
Read more: https://www.freecodecamp.org/news/linux-boot-process-in-rhel/
-
Power On and POST
- System firmware (UEFI/BIOS) runs a Power On Self Test (POST) and initializes hardware.
-
System Firmware Configuration
- Accessed by pressing a key like F2 early in the boot process.
-
Boot Loader
- GRUB2 is used as the boot loader.
- Installed using
grub2-install
for BIOS systems. - UEFI systems use a prebuilt
grubx64.efi
file in/boot/efi/EFI/redhat/
.
-
Kernel and Initramfs
- GRUB2 loads the kernel and initramfs.
- Initramfs contains kernel modules, initialization scripts, and a bootable root filesystem.
-
Systemd Initialization
- Kernel hands control to systemd, which looks for a default target to start the system.
Power Off and Reboot¶
- Use
systemctl poweroff
to power off the system. - Use
systemctl reboot
to reboot the system. systemctl halt
stops the system but does not power it off.
What is Systemd Targets?¶
Watch more here: https://www.youtube.com/watch?v=4xO-_cQaEfA
Systemd Targets are a feature of the systemd init system used in many Linux distributions. They provide a way to group units and manage dependencies between them, essentially defining states or modes of system operation. A target is conceptually similar to runlevels in the traditional System V init system but offers more flexibility and functionality.
Key Characteristics of Systemd Targets¶
-
Purpose:
- Targets define a specific state or mode of the system, such as multi-user mode, graphical mode, rescue mode, etc.
- They group together related services (units) that should be started or stopped together to achieve a particular system state.
-
Units:
- A target is a type of unit file in systemd with the extension
.target
. - Unit files describe services, sockets, devices, mounts, and other system resources that systemd manages.
- A target is a type of unit file in systemd with the extension
-
Dependency Management:
- Targets manage dependencies between units. When a target is activated, systemd ensures that all dependent units are also activated.
- Dependencies are specified using directives like
Wants=
,Requires=
,After=
, andBefore=
within the target unit file.
-
Common Systemd Targets:
- default.target: The default target that the system should boot into. Typically a symlink to one of the other targets.
- graphical.target: Starts the system with a graphical user interface (GUI).
- multi-user.target: Starts the system with multiple user access and networking, but without a GUI.
- rescue.target: Starts the system in a single-user mode for rescue and maintenance, with minimal services.
- emergency.target: Starts the system in an emergency shell with the bare minimum services, often used for troubleshooting critical issues.
- shutdown.target: Shuts down the system.
- reboot.target: Reboots the system.
- poweroff.target: Powers off the system.
-
Customization:
- Users can create custom targets to define specific states tailored to their needs. Custom targets can be useful for setting up specialized environments or workflows.
Example of a Target File¶
Here is an example of a simple custom target unit file:
# /etc/systemd/system/custom.target
[Unit]
Description=Custom Target
Requires=multi-user.target
After=multi-user.target
[Install]
WantedBy=default.target
In this example:
- The [Unit]
section describes the target and specifies its dependencies.
- Requires=multi-user.target
ensures that the multi-user.target
is activated whenever custom.target
is activated.
- After=multi-user.target
specifies that custom.target
should be activated after multi-user.target
.
- The [Install]
section specifies that this target should be activated when default.target
is activated, making custom.target
the default state if the symlink default.target
points to it.
Managing Targets¶
-
List Available Targets: You can list all available targets with the command:
-
Change Default Target: You can change the default target by creating a symlink for
default.target
: -
Isolate a Target: To switch to a specific target and stop all units not part of it:
-
Start/Stop a Target: You can manually start or stop a target with:
In summary, systemd targets are a powerful and flexible way to manage system states and dependencies between units in a Linux system, providing a modern replacement for traditional init runlevels.
BIOS-based Systems vs UEFI-based Systems¶
BIOS-based Systems¶
- Legacy System: BIOS is an older standard, originating in the 1980s.
- Interface: It has a simple, text-based interface.
- Boot Mode: It uses the Master Boot Record (MBR) method for storing data about partitions.
- Limitations: MBR supports a maximum of 4 primary partitions and a disk size of up to 2 TB.
- Initialization: BIOS initializes system hardware using a POST (Power-On Self-Test) process.
- Security: Limited security features; supports only basic password protection.
- Hardware Compatibility: BIOS operates in 16-bit mode, limiting the hardware initialization capabilities.
UEFI-based Systems¶
- Modern System: UEFI is a newer standard, designed to replace BIOS.
- Interface: It supports a graphical user interface (GUI) and mouse support.
- Boot Mode: Uses the GUID Partition Table (GPT) method for storing data about partitions.
- Capabilities: GPT supports a virtually unlimited number of partitions and disk sizes greater than 2 TB.
- Initialization: UEFI can initialize hardware faster than BIOS, supporting faster boot times.
- Security: Enhanced security features, including Secure Boot, which helps prevent unauthorized operating systems from loading.
- Hardware Compatibility: Operates in 32-bit or 64-bit mode, allowing more efficient hardware initialization.
Examples¶
BIOS-based Systems:
- Older desktop and laptop computers, particularly those manufactured before 2010.
- Legacy servers and industrial computers designed for compatibility with older operating systems and hardware.
UEFI-based Systems:
- Modern desktops, laptops, and servers manufactured after 2010.
- Devices running modern operating systems like Windows 10/11, macOS, and many Linux distributions.
- Many tablets and some smartphones also use UEFI.
In summary, UEFI is a more modern and feature-rich firmware interface compared to BIOS, providing better performance, security, and hardware compatibility.
Master Boot Record (MBR) vs GUID Partition Table (GPT)¶
Master Boot Record (MBR)¶
Partitioning Limitations:
- Maximum Partitions: MBR supports up to 4 primary partitions. To create more partitions, one of these primary partitions can be converted into an extended partition, which can then contain multiple logical partitions.
- Disk Size Limit: MBR can only address disks up to 2 TB in size. This is because MBR uses 32 bits for addressing sectors, and with 512-byte sectors, this limits the addressable space to 2 TB.
Boot Data:
- Boot Sector: MBR stores the bootloader in the first 512 bytes of the disk. This area is known as the master boot record, hence the name.
- Partition Table: MBR contains a partition table that stores the details of the partitions.
Compatibility:
- Older Systems: MBR is compatible with older BIOS firmware and operating systems.
- Cross-Platform: Supported by most operating systems, making it versatile for different platforms.
GUID Partition Table (GPT)¶
Partitioning Capabilities:
- Maximum Partitions: GPT allows for virtually unlimited partitions. In practice, the number is often limited by the operating system, but it usually supports up to 128 partitions without needing an extended partition.
- Disk Size Limit: GPT can handle disks larger than 2 TB, theoretically up to 9.4 zettabytes (ZB), making it suitable for modern large-capacity drives.
Boot Data:
- Boot Sector: GPT doesn't use the first sector for boot code. Instead, it stores protective MBR in the first sector to prevent tools from misrecognizing the disk.
- Redundancy: GPT stores multiple copies of the partition table across the disk (at the beginning and end), which increases reliability and recoverability in case of corruption.
- CRC Protection: GPT includes cyclic redundancy check (CRC) values for better error detection.
Compatibility:
- Modern Systems: GPT is used by modern UEFI-based systems.
- Cross-Platform: Supported by all modern operating systems, including Windows, macOS, and Linux.
Comparison¶
MBR:
- Limited to 4 primary partitions (or 3 primary and 1 extended with multiple logical).
- Supports disks up to 2 TB.
- Single point of failure with the boot loader stored in the first 512 bytes.
- Compatible with older BIOS systems and older operating systems.
GPT:
- Supports a much higher number of partitions (up to 128 in most OSes).
- Supports disks larger than 2 TB.
- Stores multiple copies of the partition table for redundancy.
- Includes error detection with CRC protection.
- Requires UEFI firmware and is used by modern systems.
In conclusion, GPT is the preferred choice for new installations and large drives due to its enhanced features and reliability, while MBR remains useful for compatibility with older systems.
Initramfs (initial RAM filesystem) and the kernel are both critical components of the Linux operating system, but they serve different purposes and operate at different stages of the boot process. Here’s a detailed comparison of the two:
Kernel¶
Definition:
- The kernel is the core component of the operating system. It manages hardware, executes processes, and handles system calls.
Function:
- The kernel provides essential services such as process management, memory management, device management, and system calls.
- It acts as an intermediary between user applications and the hardware.
Location:
- The kernel is usually stored on the hard drive and is loaded into memory during the boot process by the bootloader.
Execution:
- Once loaded into memory, the kernel initializes hardware components and system processes, then mounts the root filesystem and starts the init process (or systemd in modern distributions).
Components:
- The kernel includes the scheduler, memory manager, device drivers, network stack, and various system libraries.
Initramfs¶
Definition:
- Initramfs (initial RAM filesystem) is a temporary root filesystem that is loaded into memory early in the boot process.
Function:
- Initramfs contains essential drivers and scripts needed to mount the actual root filesystem.
- It provides a minimal environment to execute the necessary tasks to prepare the system for the transition to the real root filesystem.
Location:
- Initramfs is stored as a compressed archive (usually cpio format) on the hard drive and is loaded into memory by the bootloader before the kernel.
Execution:
- The bootloader loads both the kernel and initramfs into memory. The kernel then mounts initramfs as its root filesystem.
- Initramfs executes its init script to load necessary modules, set up device nodes, and mount the actual root filesystem.
- After these tasks are completed, control is transferred from initramfs to the real root filesystem.
Components:
- Initramfs contains essential drivers, binaries, libraries, and scripts needed for early system initialization.
- It can include modules for disk controllers, filesystems, and other hardware necessary to mount the real root filesystem.
Comparison¶
Purpose:
- Kernel: Manages hardware resources and system processes, acting as the core of the operating system.
- Initramfs: Provides a temporary environment to initialize the system and mount the actual root filesystem.
Role in Boot Process:
- Kernel: Loaded by the bootloader to take control of the system and manage overall operations.
- Initramfs: Loaded by the bootloader to assist the kernel in mounting the real root filesystem and handling early initialization tasks.
Content:
- Kernel: Contains the core functionalities of the operating system, including device drivers, system calls, and process management.
- Initramfs: Contains essential drivers and scripts required to set up the system and prepare for the kernel's full operation.
In summary, the kernel is the central part of the operating system responsible for managing hardware and system operations, while initramfs is a temporary filesystem used during the early stages of the boot process to ensure that the system can mount the actual root filesystem and continue booting.
Questions and Answers¶
Q: What happens during the Power On Self Test (POST)?¶
A: The system firmware (BIOS/UEFI) checks hardware components and initializes them.
Q: How do I access the BIOS or UEFI settings?¶
A: Press a specific key like F2 during the early boot process.
Q: What is GRUB2 and what does it do?¶
A: GRUB2 is the boot loader that loads the kernel and initramfs into memory and starts the boot process.
Q: How do I install GRUB2 on a BIOS system?¶
A: Use the grub2-install
command.
Q: How can I power off or reboot the system from the command line?¶
A: Use systemctl poweroff
to power off and systemctl reboot
to reboot the system.
Q: What are the different systemd targets and their purposes?¶
A:
- graphical.target
: Supports graphical and text-based logins.
- multi-user.target
: Supports text-based logins only.
- rescue.target
: Single-user mode for repairing the system.
- emergency.target
: Minimal system for advanced troubleshooting.
Q: How do I change the default target that the system boots into?¶
A: Use systemctl set-default <target>
to set the default target.
Q: How do I boot into a different target temporarily?¶
A: Interrupt the boot process, edit the kernel command line, and add systemd.unit=<target>.target
.
Q: What is the difference between systemctl halt
and systemctl poweroff
?¶
A: systemctl halt
stops the system but does not power it off. systemctl poweroff
stops all services and powers down the system.
Q: What is the purpose of the initramfs image?¶
A: The initramfs image contains kernel modules for required hardware, initialization scripts, and a bootable root filesystem.
Q: How is the GRUB2 configuration file generated?¶
A: The GRUB2 configuration file is generated using the grub2-mkconfig
command, which creates /boot/grub2/grub.cfg
for BIOS systems and /boot/efi/EFI/redhat/grub.cfg
for UEFI systems.
Q: What is the difference between BIOS and UEFI?¶
A: BIOS is an older firmware interface for booting computers, while UEFI is a modern interface that includes more features like secure boot and support for larger hard drives.
Q: How can I view the dependencies of a systemd target?¶
A: Use the command systemctl list-dependencies <target> | grep target
.
Q: How do I list all available systemd targets?¶
A: Use the command systemctl list-units --type=target --all
.
Q: What does the systemctl isolate
command do?¶
A: The systemctl isolate <target>
command switches the system to the specified target, stopping services not required by the target and starting any that are required.
Q: What is the significance of AllowIsolate=yes
in a systemd unit file?¶
A: This setting allows the target to be isolated using the systemctl isolate
command. Not all targets have this setting enabled.
Q: How do I restore the correct grubx64.efi
file for UEFI systems?¶
A: Restore the file from the grub2-efi
package if the grubx64.efi
file is overwritten or missing.
Q: What happens if the rescue.target
fails to start?¶
A: If rescue.target
fails to start, you can use emergency.target
to start the most minimal system for advanced troubleshooting.
Q: How can I view the current default systemd target?¶
A: Use the command systemctl get-default
.
Q: What should I do if I need to troubleshoot the boot process?¶
A: Boot the system with a different target like rescue.target
by modifying the kernel command line to include systemd.unit=rescue.target
.
Q: How is the kernel command line modified at boot time?¶
A: Interrupt the boot loader menu, press e
to edit the entry, modify the line starting with linux
, append the desired systemd.unit=<target>.target
option, and press Ctrl+x
to boot.
Q: What does the systemctl cat
command do?¶
A: The systemctl cat <unit>
command displays the contents of the specified systemd unit file, including any drop-ins.
Q: How is the default.target
configured?¶
A: The default.target
is a symbolic link in the /etc/systemd/system/
directory, typically pointing to either graphical.target
or multi-user.target
. It can be set using systemctl set-default <target>
.
Q: What is the purpose of the dracut
command?¶
A: The dracut
command is used to create and modify the initramfs image.
Q: How do I inspect the contents of the initramfs file?¶
A: Use the lsinitrd
command to inspect the contents of the initramfs file.
Certainly! Here are additional questions and answers to further deepen understanding:
Q: What does the grub2-mkconfig
command do?¶
A: The grub2-mkconfig
command generates a new GRUB2 configuration file based on the scripts in /etc/grub.d/
and settings in /etc/default/grub
.
Q: How do I make changes to the GRUB2 configuration?¶
A: Edit the /etc/default/grub
file or add scripts to /etc/grub.d/
, then run grub2-mkconfig
to regenerate the GRUB2 configuration file.
Q: What is the role of the systemd.unit
kernel parameter?¶
A: The systemd.unit
kernel parameter specifies which systemd target to boot into, overriding the default target for that boot.
Q: How can I temporarily change the boot target without modifying configuration files?¶
A: Interrupt the boot loader menu, edit the kernel command line, and append systemd.unit=<target>.target
to temporarily boot into a different target.
Q: What is the poweroff
command a symbolic link to?¶
A: The poweroff
command is a symbolic link to the systemctl poweroff
command.
Q: What happens if I run systemctl reboot
?¶
A: The systemctl reboot
command stops all running services, unmounts all filesystems, and then reboots the system.
Q: How do I change the boot target for a single boot session?¶
A: Edit the kernel command line during the boot process to include systemd.unit=<target>.target
.
Q: What are the steps to edit the kernel command line from the boot loader?¶
A:
1. Interrupt the boot loader menu countdown.
2. Select the kernel entry.
3. Press e
to edit.
4. Modify the linux
line.
5. Press Ctrl+x
to boot with the changes.
Q: How can I check if a target can be isolated?¶
A: Use systemctl cat <target>
and look for AllowIsolate=yes
in the unit file.
Q: What is the grub2-install
command used for?¶
A: The grub2-install
command installs the GRUB2 boot loader on BIOS systems.
Q: Why should I not use grub2-install
on UEFI systems?¶
A: Using grub2-install
on UEFI systems can generate a new grubx64.efi
file without the required secure boot signatures. Instead, use the prebuilt grubx64.efi
provided by RHEL.
Q: How do I list all units of a specific type in systemd?¶
A: Use systemctl list-units --type=<unit_type> --all
, for example, systemctl list-units --type=target --all
.
Q: How do I create a symbolic link for the default target?¶
A: The systemctl set-default <target>
command creates a symbolic link for the default target.
Q: What is the difference between rescue.target
and emergency.target
?¶
A: rescue.target
provides a single-user mode with minimal services for repair, while emergency.target
starts the most minimal system for advanced troubleshooting when rescue.target
fails.
Q: How do I restore the original UEFI boot loader file if it's missing?¶
A: Restore the grubx64.efi
file from the grub2-efi
package.
Q: What is the purpose of the /etc/fstab
file?¶
A: The /etc/fstab
file is used to configure the mounting of file systems at boot time.
Q: How does systemd solve dependencies between units?¶
A: Systemd starts and stops units as needed to satisfy the requirements and dependencies defined in the unit files, ensuring that all necessary services are running for the selected target.
Q: What command is used to generate an initramfs image?¶
A: The dracut
command is used to generate and manage initramfs images.
Q: How do I check the current run level in a systemd-based system?¶
A: Use systemctl get-default
to check the current default target, which corresponds to the run level.
Q: How can I safely power off a system manually after stopping it?¶
A: Use the systemctl halt
command to bring the system down to a safe state where it can be powered off manually.
Q: How do I modify the GRUB2 boot menu entries?¶
A: Edit the scripts in /etc/grub.d/
and the configuration in /etc/default/grub
, then run grub2-mkconfig
to update the boot menu.
Q: What command can be used to inspect the initramfs image?¶
A: Use the lsinitrd
command to inspect the contents of the initramfs image.
Sure! Here are even more questions and answers to cover additional details and scenarios:
Q: What is the purpose of the /etc/systemd/system/default.target
file?¶
A: This file is a symbolic link to the target that systemd uses as the default target during the boot process. It typically points to graphical.target
or multi-user.target
.
Q: How do I set the system to boot into text mode by default?¶
A: Run systemctl set-default multi-user.target
to set the system to boot into text mode.
Q: How can I change the timeout for the GRUB2 menu?¶
A: Edit the GRUB_TIMEOUT
variable in the /etc/default/grub
file and then run grub2-mkconfig
to apply the changes.
Q: What command shows the current system status including the boot target?¶
A: Use systemctl status
to show the current system status, including the active target.
Q: How can I prevent a specific service from starting automatically at boot?¶
A: Use systemctl disable <service>
to prevent the service from starting automatically at boot.
Q: What is the function of the dracut.conf.d
directory?¶
A: The /etc/dracut.conf.d/
directory contains configuration files for the dracut
command, which is used to generate the initramfs image.
Q: How can I view the kernel command line parameters used at boot?¶
A: Use the cat /proc/cmdline
command to view the kernel command line parameters.
Q: How do I regenerate the initramfs image if I've made changes to hardware configuration?¶
A: Run the dracut -f
command to regenerate the initramfs image.
Q: What does the lsinitrd
command output include?¶
A: The lsinitrd
command output includes a list of files and directories contained in the initramfs image.
Q: How can I manually edit the GRUB2 configuration file?¶
A: While it is generally not recommended, you can manually edit the /boot/grub2/grub.cfg
file for BIOS systems or /boot/efi/EFI/redhat/grub.cfg
for UEFI systems. However, it's better to edit /etc/default/grub
and use grub2-mkconfig
.
Q: How do I troubleshoot a system that fails to boot properly?¶
A: Boot into rescue.target
or emergency.target
to troubleshoot and repair the system with minimal services running.
Q: How can I configure GRUB2 to display the menu without a timeout?¶
A: Set GRUB_TIMEOUT=-1
in /etc/default/grub
and run grub2-mkconfig
.
Q: What is the difference between grub.cfg
and grub.conf
?¶
A: grub.cfg
is used by GRUB2, while grub.conf
was used by legacy GRUB (GRUB version 1). They are configuration files for different versions of the boot loader.
Q: How can I list all the systemd services and their statuses?¶
A: Use the command systemctl list-units --type=service --all
to list all services and their statuses.
Q: What is a "unit" in systemd terminology?¶
A: A unit in systemd terminology is a resource that systemd knows how to manage. Units can be services, targets, devices, mounts, sockets, etc.
Q: How do I enable a service to start at boot?¶
A: Use systemctl enable <service>
to enable a service to start at boot.
Q: How can I view the logs for the boot process?¶
A: Use journalctl -b
to view the logs for the current boot process.
Q: What is the role of the sysinit.target
?¶
A: The sysinit.target
is used to bring the system to a state where basic system initialization is complete. It includes mounting local file systems, setting up encrypted volumes, etc.
Q: How can I manually trigger a systemd service to start?¶
A: Use systemctl start <service>
to manually start a service.
Q: How do I check the status of a specific service?¶
A: Use systemctl status <service>
to check the status of a specific service.
Q: How can I reboot into a specific target for one-time use?¶
A: Modify the kernel command line at boot to include systemd.unit=<target>.target
for a one-time boot into that target.
Q: What is the systemctl daemon-reload
command used for?¶
A: The systemctl daemon-reload
command reloads the systemd manager configuration, usually needed after modifying unit files.
Q: How can I stop a running service?¶
A: Use systemctl stop <service>
to stop a running service.
Q: What does the systemctl mask
command do?¶
A: The systemctl mask <unit>
command links a unit to /dev/null
, effectively disabling it and preventing it from being started.
Q: How do I unmask a masked service?¶
A: Use systemctl unmask <service>
to remove the mask and allow the service to be started again.
Q: What does the systemctl reload
command do?¶
A: The systemctl reload <service>
command reloads the configuration of a running service without interrupting its operation, if the service supports reloading.