Linux mkinitrd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mkinitrd command to create and customize an initial RAM disk (initramfs) image, which is a critical component of the Linux boot process. You will understand the purpose of the mkinitrd command, create a custom initramfs image, and troubleshoot kernel boot issues using this utility. The lab covers essential disk and file system management skills, providing practical examples and step-by-step guidance.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/uname("`System Information Displaying`") subgraph Lab Skills linux/mkdir -.-> lab-422811{{"`Linux mkinitrd Command with Practical Examples`"}} linux/cp -.-> lab-422811{{"`Linux mkinitrd Command with Practical Examples`"}} linux/nano -.-> lab-422811{{"`Linux mkinitrd Command with Practical Examples`"}} linux/uname -.-> lab-422811{{"`Linux mkinitrd Command with Practical Examples`"}} end

Understand the Purpose of mkinitrd Command

In this step, you will learn about the purpose of the mkinitrd command in Linux. The mkinitrd command is used to create an initial RAM disk (initramfs) image, which is a critical component of the Linux boot process.

The initramfs is a temporary file system that is loaded into memory during the early stages of the boot process. It contains the necessary drivers and modules required to mount the root file system, which may be located on a variety of storage devices, such as a hard disk, RAID array, or network-attached storage.

Without the initramfs, the kernel would not be able to access the root file system and complete the boot process. The mkinitrd command is responsible for generating this initramfs image, which can be customized to include specific drivers, modules, and utilities required for your system.

Let's explore the mkinitrd command and its usage:

sudo mkinitrd -v -f /boot/initramfs-$(uname -r).img $(uname -r)

Example output:

Creating initramfs image file '/boot/initramfs-5.15.0-58-generic.img'
Copying modules to initramfs image...
Preparing initramfs image...

The command above generates a new initramfs image file named initramfs-$(uname -r).img and stores it in the /boot directory. The -v option enables verbose output, and the -f option forces the creation of a new image file.

The mkinitrd command reads the kernel version from the uname -r command and uses it to generate the initramfs image for the corresponding kernel.

Create a Custom initramfs Image

In this step, you will learn how to create a custom initramfs image using the mkinitrd command. Customizing the initramfs can be useful in scenarios where you need to include additional drivers, modules, or utilities that are not part of the default initramfs.

Let's start by creating a directory to store the custom initramfs files:

mkdir ~/project/custom-initramfs
cd ~/project/custom-initramfs

Next, we'll create a custom configuration file for the initramfs. This file will specify the additional content we want to include in the image.

nano ~/project/custom-initramfs/initramfs.conf

Add the following content to the initramfs.conf file:

add_dracutmodules+="custom-module"

This configuration will include a custom module named custom-module in the initramfs image.

Now, let's generate the custom initramfs image:

sudo mkinitrd -v -f /boot/custom-initramfs.img --with-modules --with-firmware --with-usb --with-i18n --with-nfs --with-crypt --with-dm --with-lvm --with-raid --with-md --with-fips --with-selinux --with-plymouth --with-shutdown --with-network --with-multipath --with-kernel-modules=custom-module

The command above creates a custom initramfs image named custom-initramfs.img and stores it in the /boot directory. The --with-* options specify the additional modules, drivers, and utilities to be included in the initramfs.

Example output:

Creating initramfs image file '/boot/custom-initramfs.img'
Copying modules to initramfs image...
Preparing initramfs image...

Troubleshoot Kernel Boot Issues with mkinitrd

In this step, you will learn how to use the mkinitrd command to troubleshoot kernel boot issues. The initramfs image generated by mkinitrd is a critical component of the Linux boot process, and if it is not properly configured, it can lead to boot problems.

Let's simulate a boot issue by modifying the initramfs image and then using mkinitrd to troubleshoot the problem.

First, let's create a backup of the current initramfs image:

sudo cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak

Now, let's intentionally corrupt the initramfs image by removing a critical file:

sudo rm /boot/initramfs-$(uname -r).img

If you try to reboot the system now, you will likely encounter a kernel boot issue, as the initramfs image is missing.

To troubleshoot the issue, we can use the mkinitrd command to recreate the initramfs image:

sudo mkinitrd -v -f /boot/initramfs-$(uname -r).img $(uname -r)

Example output:

Creating initramfs image file '/boot/initramfs-5.15.0-58-generic.img'
Copying modules to initramfs image...
Preparing initramfs image...

After running the mkinitrd command, the initramfs image should be recreated, and the kernel should be able to boot successfully.

To verify that the issue has been resolved, you can reboot the system and check the boot logs for any errors related to the initramfs.

Summary

In this lab, you first learned about the purpose of the mkinitrd command in Linux, which is used to create an initial RAM disk (initramfs) image. The initramfs is a critical component of the Linux boot process, as it contains the necessary drivers and modules required to mount the root file system. You then explored how to create a custom initramfs image by including additional drivers, modules, or utilities that are not part of the default initramfs. Finally, you learned how to troubleshoot kernel boot issues using the mkinitrd command, which can be helpful in scenarios where the default initramfs is unable to mount the root file system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like