LinuxCommandLibrary

mkinitfs

Create initial ramdisk (initrd) image

TLDR

Generate an initramfs with the features specified in /etc/mkinitfs/mkinitfs.conf

$ mkinitfs
copy

Use a different configuration file
$ mkinitfs -c [path/to/config]
copy

Compress the initramfs using the specified compression algorithm (default: gzip)
$ mkinitfs -C [gzip|xz|zstd|lz4|none]
copy

List files that will be included in the initramfs image
$ mkinitfs -l
copy

List all available features
$ mkinitfs -L
copy

SYNOPSIS

mkinitfs [ OPTIONS ] [ KERNEL_VERSION ] [ OUTFILE ]

PARAMETERS

-o OUTFILE
    Specify the path and filename for the output initramfs image. If not specified, a default location (e.g., /boot/initramfs-VERSION.img) is often used.

KERNEL_VERSION
    The specific kernel version for which to build the initramfs image. If omitted, the currently running kernel's version is typically used.

-c CONFIG_FILE
    Use an alternate configuration file for specifying modules, hooks, and other options.

-m MODULES
    Explicitly include a comma-separated list of kernel modules in the image. Useful for drivers not auto-detected.

-d DIRECTORY
    Add a specified directory and its contents to the initramfs image.

-H HOOK_SCRIPT
    Include a custom hook script to be executed during the initramfs boot process.

-r ROOT_DEVICE
    Specify the root device to be used by the initramfs, e.g., /dev/sda1 or UUID.

-v
    Enable verbose output, showing the steps and files being included in the image.

-q
    Operate quietly, suppressing non-essential output.

-f
    Force overwrite of an existing output file without prompting.

DESCRIPTION

The mkinitfs command is a utility used to create an initramfs (initial RAM filesystem) image. This image is a compressed CPIO archive that is loaded by the bootloader (like GRUB or LILO) into RAM before the actual root filesystem is mounted. Its primary purpose is to provide the necessary kernel modules, utilities, and scripts (especially an init script) to handle the early boot process, including tasks such as detecting hardware, loading crucial drivers (e.g., for disk controllers, NVMe, USB), configuring logical volume managers (LVM), software RAID, or decrypting encrypted root filesystems. Without a correctly configured initramfs, the Linux kernel might not be able to locate and mount the root filesystem, leading to a boot failure.

While mkinitfs is a generic name, specific distributions often provide their own wrappers or more advanced tools like update-initramfs (Debian/Ubuntu), dracut (Red Hat/Fedora), or mkinitcpio (Arch Linux) to manage the creation of these images, simplifying the process for users.

CAVEATS

The exact behavior and available options for mkinitfs can vary significantly between Linux distributions, as many provide their own specialized implementations or wrapper scripts. Incorrectly configured initramfs images can lead to an unbootable system. Always back up existing initramfs images before making changes, and test new images in a safe environment if possible. Advanced configurations often require a deep understanding of kernel modules, device drivers, and the Linux boot process.

PURPOSE IN BOOT PROCESS

The initramfs is crucial for bridging the gap between the kernel's initial loading and the mounting of the actual root filesystem. It contains the minimal set of binaries and libraries needed to perform early boot tasks that the kernel itself cannot handle natively, such as handling LVM volumes, encrypted disks (LUKS), software RAID arrays, or specific network boot configurations (like NFS root). After these tasks are completed, the initramfs pivots root to the real filesystem, and the normal boot process continues.

CUSTOMIZATION AND HOOKS

Advanced users can customize the initramfs by adding custom scripts (known as 'hooks'), additional binaries, or specific kernel modules. These hooks allow for complex pre-root mount operations, such as prompting for passphrases for encrypted volumes, configuring network interfaces for network-booted systems, or running diagnostic checks. The exact mechanism for customization depends on the specific initramfs generation tool being used (e.g., hooks in dracut or mkinitcpio).

HISTORY

The concept of an initial RAM disk (initrd) was introduced in Linux kernel 2.0. Initially, initrd images were block devices containing a small filesystem, which required a loop device to be mounted. This approach was largely superseded by the initramfs model with Linux kernel 2.6. initramfs is a cpio archive (often gzipped) that is directly unpacked into a tmpfs (RAM-based filesystem) by the kernel. This change simplified the architecture by eliminating the need for a separate filesystem and loopback mounting.

While mkinitrd was the common tool for creating initrd images, mkinitfs (or tools like mkinitramfs, dracut, update-initramfs) emerged to manage the more flexible and powerful initramfs images. These tools abstract away the complexities of selecting necessary modules, integrating hooks, and packaging the final archive, making the process more robust and user-friendly across diverse hardware configurations.

SEE ALSO

initramfs(7), mkinitrd(8), update-initramfs(8), dracut(8), mkinitcpio(8), cpio(1), gzip(1), boot(7)

Copied to clipboard