How to Generate an Initramfs Boot Image Using mkinitcpio in Linux

When a Linux system first powers on, the bootloader (like GRUB) loads the core kernel into memory. However, the kernel is small and does not contain all the massive storage drivers required to read your encrypted hard drives or complex RAID arrays. To bridge this gap, the bootloader also loads an “initial ramdisk”—a tiny, temporary root filesystem containing just enough drivers and scripts to find, unlock, and mount your real hard drive. On Arch Linux and its derivatives, you use the mkinitcpio command to build and customize this critical boot environment.

Understanding the Configuration File

Before you run the command to generate the image, you must tell mkinitcpio exactly which drivers (modules) and scripts (hooks) to include. This is controlled by the primary configuration file located at /etc/mkinitcpio.conf.

  1. Open the configuration file using a terminal text editor with root privileges:
    sudo nano /etc/mkinitcpio.conf
  2. Locate the MODULES=() line. If you have a specific storage controller (like an obscure NVMe RAID driver or a specific graphics driver like nouveau that needs to load early for high-resolution boot screens), you type the module name between the parentheses.
  3. Locate the HOOKS=() line. This is the most critical section. Hooks are scripts executed in sequence during boot. For example, if you encrypt your hard drive, you must include the encrypt hook before the filesystems hook, otherwise the kernel will fail to decrypt the drive and the boot process will crash into a kernel panic.

Once you have edited the configuration, save the file (Ctrl+O) and exit the editor (Ctrl+X).

Generating the Initramfs Image

Editing the text file does not change the boot process. You must run the mkinitcpio command to compile your settings into a compressed image file that GRUB can actually read.

To rebuild the default linux kernel image based on a preset, run:

sudo mkinitcpio -p linux

The -p (preset) flag tells the command to look inside /etc/mkinitcpio.d/ for the “linux” preset file. This preset file instructs the builder to create two specific images: a standard image and a massive “fallback” image. The fallback image includes almost every storage driver available, guaranteeing that your system will boot even if you severely misconfigured your custom MODULES array.

Advanced Image Generation

If you are building a custom kernel or testing a highly experimental boot configuration, you can bypass the presets entirely and instruct the command to generate a specific file.

sudo mkinitcpio -g /boot/custom_initramfs.img

The -g (generate) flag takes your current configuration and compiles it directly into the specified .img file. You can then edit your GRUB menu to boot from this custom image, allowing you to test new boot hooks safely without destroying your primary, working kernel environment.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.