So, if you're planning to modify the initramfs and play with it, dracut is here. It's an advanced tool to generate initramfs images.
Dracut is a complete initramfs infrastructure with many capibilities. Dracut is already used by Fedora, Redhat and CentOS and openSUSE, but not in Debian or Ubuntu.
The initramfs image generated by dracut is considerably smaller in size compared to the default in Ubuntu, so I'm going to use dracut in Ubuntu and this tutorial is Ubuntu specific.
So why not give dracut a try? Though it's a quite interesting, but this tutorial is only for experienced users. Attempt this only if you're sure that you can fix the system anyhow.
Contents
Install dracut in Ubuntu
This step is quite straight forward, dracut is already in Ubuntu repo.
sudo apt-get --no-install-recommends install dracut
The above command will install dracut and remove initramfs-tools as well.
The initramfs file will be created automatically, the GRUB willbe updated automatically, but to be sure, update grub once more. sudo update-grub
You can check the size of the initramfs file with the du command. It's around 7.9 MB in my case, generated for custom linux kernel 4.10.10 with some extra modules.
Reboot the machine to test the new initramfs, check it with systemd-analyze command.
If you use the systemd-analyze
command frequently, then you can see it's a little different than the Ubuntu default.
Generate initramfs for a specific kernel version
If you've installed multiple kernels, then you may need to generate or update an initramfs manually.
A generalised command should be like below,
sudo dracut -f /path/to/output/initrd.img kernel-version
A real example, generating initramfs with dracut, kernel verion 4.10.10 .
sudo dracut -f initrd-4.10.10.img 4.10.10
The above command will create the initramfs file in present directory, you need to copy it to the /boot
folder or modify the command to overrite the existing initramfs.
sudo dracut -f /boot/initrd.img-4.10.10 4.10.10
Adding extra kernel modules in dracut
If you need some extra kernel modules in your initramfs to boot the system properly, you can use the instruction below.
- First find out which kernel modules you need, in my case I was in need of the
fbcon
and related modules. - Now update the initramfs file, or generate a new one, keep both and use what you you need.
sudo dracut --add-drivers module_name /boot/initrd.img-kernel.version
The above command is just for representation, here's a real example.
sudo dracut --add-drivers fbcon /boot/initrd.img-4.10.10
Dracut is intelligent enough to check the extra module dependencies and also includes them.
Conclusion
So that's all about dracut linux command and how to use it in Ubuntu, hope you enjoyed it. Share your thoughts and opinions below.
Antonio Petricca says
Does the installation scripts manage initramfs-tools custom scripts?
Arnab Satapathi says
No, if dracut is installed, initramfs-tools will be uninstalled.
So, there's no scope of using those specific scripts with dracut.
However you can also add custom scripts to dracut, located under the
/usr/lib/dracut/modules.d/
directory.SUDESH KUMAR says
Hey Guys!!
I updated my grub in my ubuntu 20.04 after building my new kernel which is version 5.8.18. After this process, I checked my /boot directory the initrd.img-5.8.18 was also present. But once I boot into this kernel. I get "Loading Initial RamDisk ..." and my boot freezes. Anybody can help me out please.. I have been held up here for more than a week. I tried building around 5 kernels. Every time I build it, boot freezes and same issue pertains. Thanks in advance :). My mail id is sudesh.skofficial@gmail.com
CyrIng says
Nice howto, thank you Arnab.
I'm wondering how I could build the tiniest ramfs for demonstration purpose : I mean a ramfs which includes selected drivers; start a user-space software with the libc dependencies, and at least, a fallback shell.
Everything packed into a bootable ISO image for less than 10MB
ArchLinux provides mkinitcpio in place of dracut.
https://wiki.archlinux.org/index.php/Mkinitcpio
Arnab Satapathi says
Thanks for the feedback !
I did something similar about a year back, a minimal cpio.gz archive as initrd, unfortunately I forgot what i did exactly.
I first created a chroot dirctory, copied buybox static binary there and then created few more directories, /bin, /etc, /dev, /proc, /sys , /lib and etc. etc.
Used the default Debian kernel, and recursively copied kernel modules.
The whole thing boots from a FAT32 USB drive, GRUB as bootloader.
The main problem was the init script, and busybox mdev failed to detect devices, even it can't detect HDDs.
It's booting to busybox ash shell, but totally impractical if it can't find basic hardwares.
So I finally gave up 🙁
Arnab Satapathi says
I'll try Arch Linux again, but I think it's not as customizable as promoted.
Debian Sid is almost as cutting edge as Arch and much more customizable, installation ISO is around 250 MB, or 40 MB netboot.
Debian packages are more fragmented than Arch.
What do you think ?
CyrIng says
Exactly. ISO I've built so far takes too much space.
Using the Arch installer, my last ISO built including Xorg + browser and developer tools was more than 200MB
I've estimated that without Xorg but still Dev libs would between 50 - 100MB
It is still far above my requirements: for example, I want to be able to send ISO as an email attachment. Thus 5-10 MB
As soon as I get spare time, I will give a chance a customized mkinitcpio; but I believe I have to hook post-installation scripts.