🧱 Building better initramfs: A deep dive into dracut on Fedora & RHEL

Understanding how to use dracut is critical for kernel upgrades, troubleshooting boot issues, disk migration, encryption, and even kernel debugging.

πŸš€ Introduction: What is dracut?

dracut is a powerful tool used in Fedora, RHEL, and other distributions to create and manage initramfs imagesβ€”the initial RAM filesystem used during system boot. Unlike older tools like mkinitrd, dracut uses a modular approach, allowing you to build minimal or specialized initramfs tailored to your system.

πŸ“¦ Installing dracut (if not already available)

dracut comes pre-installed in Fedora and RHEL. If it is missing, install it with:

$ sudo dnf install dracut

Verify the version:

$ dracut --version

πŸ“‚ Basic usage

πŸ“Œ Regenerate the current initramfs

$ sudo dracut --force

This regenerates the initramfs for the currently running kernel.

πŸ“Œ Generate initramfs for a specific kernel

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

Or Manually!

$ sudo dracut --force
$ sudo dracut --force /boot/initramfs-5.14.0-327.el9.x86_64.img 5.14.0-327.el9.x86_64

🧠 Understanding key dracut options (with examples)

–force

Force regeneration even if the file already exists:

$ sudo dracut --force

–kver <kernel-version>

Generate initramfs for a specific kernel:

$ sudo dracut --force --kver 5.14.0-327.el9.x86_64

–add <module> / –omit <module>

Include or exclude specific modules (e.g., lvm, crypt, network).

Include LVM module only:

$ sudo dracut --force --add lvm

Omit network module:

$ sudo dracut --force --omit network

–no-hostonly

Build a generic initramfs that boots on any compatible machine:

$ sudo dracut --force --no-hostonly

–hostonly

Create a host-specific image for minimal size:

$ sudo dracut --force --hostonly

–print-cmdline

Show the kernel command line:

$ dracut --print-cmdline

–list-modules

List all available dracut modules:

$ dracut --list-modules

–add-drivers “driver1 driver2”

Include specific drivers:

$ sudo dracut --add-drivers "nvme ahci" --force

πŸ§ͺ Test cases and real-world scenarios

1. LVM root disk fails to boot after migration

$ sudo dracut --force --add lvm --hostonly

2. Initramfs too large

Shrink it by omitting unused modules:

$ sudo dracut --force --omit network --omit plymouth

3. Generic initramfs for provisioning

$ sudo dracut --force --no-hostonly --add network --add nfs

4. Rebuild initramfs for rollback kernel

$ sudo dracut --force /boot/initramfs-5.14.0-362.el9.x86_64.img 5.14.0-362.el9.x86_64

πŸͺ› Advanced use: Debugging and analysis

Enable verbose output:

$ sudo dracut -v --force

Enter the dracut shell if boot fails:

Use rd.break in the GRUB kernel line.

πŸ“– Where is dracut configuration stored?

There are two locations where configuration setting may occur.

The global settings location is at:

/etc/dracut.conf

and the drop-in location is at:

/etc/dracut.conf.d/*.conf

Example using the drop-in location:

$ cat /etc/dracut.conf.d/custom.conf

The contents might appear as follows for omitting and adding modules:

omit_dracutmodules+=" plymouth network "
add_dracutmodules+=" crypt lvm "

⚠️ Note: Always include a space at the beginning and end of the value when using += in these configuration files. These files are sourced as Bash scripts, so

add_dracutmodules+=" crypt lvm "
ensures proper spacing when multiple config files are concatenated. Without the spaces, the resulting string could concatenate improperly (e.g., mod2mod3) and cause module loading failures.

🧠 Deep dive: /usr/lib/dracut/modules.d/ – the heart of dracut

The directory /usr/lib/dracut/modules.d includes all module definitions. Each contains:

  • A module-setup.sh script
  • Supporting scripts and binaries
  • Udev rules, hooks, and configs

List the modules using the following command:

$ ls /usr/lib/dracut/modules.d/

Example output:

01fips/ 30crypt/ 45ifcfg/ 90lvm/ 95resume/
02systemd/ 40network/ 50drm/ 91crypt-gpg/ 98selinux/

Inspect specific module content (module-setup.sh, in this example) using this:

$ cat /usr/lib/dracut/modules.d/90lvm/module-setup.sh

You can also create custom modules at this location for specialized logic.

🏁 Final thoughts

dracut is more than a utilityβ€”it’s your boot-time engineer. From creating lightweight images to resolving boot failures, it offers unparalleled flexibility.

Explore man dracut, read through /usr/lib/dracut/modules.d/, and start customizing.

πŸ’‘ This article is dedicated to my wife, Rupali Suraj Patil, for her continuous support and encouragement.

Fedora Project community

3 Comments

  1. Oscar

    Hey, mkosi-initrd is meant to replace dracut, isn’t?
    https://fedoraproject.org/wiki/Changes/mkosi-initrd

  2. mogoh

    I am sorry and I might be wrong, but when I see headlines starting with “random” emojis, I think it was written by ChatGPT and its kind.

Leave a Reply


The interval between posting a comment and its appearance will be irregular so please DO NOT resend the same post repeatedly. All comments are moderated but this site is not monitored continuously so comments will not appear as soon as posted.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions