PCsuggest

  • Quick tip
  • SECURITY
  • NETWORKING
  • OpenWrt
  • HARDWARE

Use dracut in Ubuntu to generate minimal initramfs

Last Updated on December 27, 2017 by Arnab Satapathi 4 Comments

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.

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.

dracut linux ubuntu

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.

  1. First find out which kernel modules you need, in my case I was in need of the fbcon and related modules.
  2. 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.

Filed Under: how to, Quick tip Tagged With: command line, dracut, dracut linux, initramfs

Comments

  1. CyrIng says

    May 6, 2017 at 4:54 pm

    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

    Reply
    • Arnab Satapathi says

      May 7, 2017 at 2:00 am

      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 🙁

      Reply
    • Arnab Satapathi says

      May 7, 2017 at 2:15 am

      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 ?

      Reply
      • CyrIng says

        May 7, 2017 at 8:37 am

        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.

        Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search this site

Fresh Posts

  • 3 ways to create Windows 10 bootable USB
  • GRUB rescue on legacy BIOS systems
  • HDD to SSD cloning on Linux without re-installing
  • Install Phant on Ubuntu server for IoT data logging
  • Install SSH on Ubuntu - Step by Step Guide
  • Install NVIDIA drivers on Ubuntu from the PPA
  • Install NodeJS on Ubuntu from NodeSource repository
  • Unzip Linux command Explained With Examples

Get more ideas

Thank you for subscribing.

Something went wrong.

We're committed to protect your privacy

Recent Comments

  • Arnab Satapathi on NVIDIA Optimus troubleshooting in Debain, Kali Linux
  • Nick on NVIDIA Optimus troubleshooting in Debain, Kali Linux
  • Arnab Satapathi on How to update CPU microcode in Linux
  • John Nisbet on How to update CPU microcode in Linux
  • user on How to use an ISO file as offline repository in Debian

Copyright © PCsuggest.com · All rights reserved.

  • Home Page
  • About PCsuggest
  • Contact Us
  • Privacy Policy
  • Sitemap