# π§± Building better initramfs: A deep dive into *dracut* on Fedora & RHEL
**By Suraj Rajendra Patil (Fedora ID: suraj522)**
---
## π 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.
Understanding how to use *dracut* is critical for kernel upgrades, troubleshooting boot issues, disk migration, encryption, and even kernel debugging.
---
## π¦ 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 /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 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 dracut shell if boot fails:
Use `rd.break` in the GRUB kernel line.
---
## π Where is dracut config stored?
* `/etc/dracut.conf` β global config
* `/etc/dracut.conf.d/*.conf` β drop-in configs
Example:
```
$ cat /etc/dracut.conf.d/custom.conf
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
This directory includes all module definitions. Each contains:
* A `module-setup.sh` script
* Supporting scripts and binaries
* Udev rules, hooks, and configs
List modules:
```
$ ls /usr/lib/dracut/modules.d/
```
Example output:
```
01fips/ 30crypt/ 45ifcfg/ 90lvm/ 95resume/
02systemd/ 40network/ 50drm/ 91crypt-gpg/ 98selinux/
```
Inspect a module:
```
$ cat /usr/lib/dracut/modules.d/90lvm/module-setup.sh
```
You can also create custom modules here 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.*