Initramfs Is Too Big

What is it?

The initramfs, or initial ram file system, is a file system image that is loaded in memory that contains the necessary tools to mount the file systems on the operating system before init process is called. Ideally, this image contains only the drivers necessary to bootstrap the kernel. Despite this, it is most often the case that your initramfs is bloated with unnecessary options for things you don’t need; when compiling a custom kernel, the initramfs will become huge without module stripping and most Linux distributions choose a generic image to support more hardware out of the box.

For those with a separate /boot partition that is often only 200-300MB in size, these initramfs images quickly fill up the partition. While my stock 5.11.2 kernel is only 8.7MB in size, the stock initramfs for it is a whopping 60MB! Furthermore, a bloated initramfs can considerably slow down boot times, so making a custom one is almost always worthwhile after initial system setup.

Generating a Custom Image

The only option to make your initramfs smaller is by generating a custom image. There are two primary utilities used on Linux for creating and managing an initramfs:

  1. mkinitcpio
  2. dracut

To retrieve a list of modules being used in your current initramfs, run

lsinitrd | less

You’ll notice that your image almost certainly is loading far more modules than necessary.

For both utilities, you can choose to specify custom options for both in their respective configuration files or manually, but we’ll just be looking at doing it via configuration file, since it makes changes persistent.

Configuration File

When using mkinitpcio, you can create a custom configuration file at /etc/mkinitcpio-name.conf and create a custom initramfs if you use that configuration file as an argument. To do so, you’d run

mkinitcpio --config /etc/mkinitcpio-name.conf --generate /boot/initramfs-name.img

This will generate an image for the current running kernel. To specify for a different kernel, set it with the --kernel flag. The mkinitcpio configuration has five variables we can tweak:

  • MODULES
  • FILES
  • BINARIES
  • HOOKS
  • COMPRESSION

You can pass extra compression options, but its strongly discouraged. An example config might look like this:

# /etc/mkinitcpio-custom.conf

MODULES="ext4 ahci sd_mod ehci-pci hid_generic"
HOOKS="base udev autodetect pata scsi sata lvm2 encrypt filesystems"
BINARIES="fsck fsck.vfat fsck.ext4 e2fsck dosfsck"
COMPRESSION="gzip"

For dracut, you’ll want to make configuration files within the /etc/dracut.conf.d directory. The simplest way to make a targeted initramfs for your computer is making a file at /etc/dracut.conf.d/flags.conf and entering hostonly=yes.

This alone cut down my 60MB initramfs down to less than half that size at 28MB.

Dracut is by far the easier of the two, so I strongly encourage most people to use that with the hostonly flag activated. Arch Linux develops mkinitcpio and chooses it as their default. The man page is here for those who want to learn about all aspects of its configuration.