User:krathalan

From ArchWiki
Jump to navigation Jump to search

This is a WIP draft for the dracut wiki page.

Note: Read the mailing list announcement for a possible Mkinitcpio replacement with Dracut.

dracut creates an initial image used by the kernel for preloading the block device modules (such as IDE, SCSI or RAID) which are needed to access the root filesystem. Upon installing linux, you can choose between mkinitcpio and dracut. dracut is used by Fedora, RHEL, Gentoo, and Debian, among others.

You can read the full project documentation for dracut in the kernel documentation.

Installation

dracut can be installed with the dracut package.

Tip: If dracut works on your machine after you test it, you can remove mkinitcpio.

Usage

dracut is easy to use and typically does not require user configuration, even when using non-standard setups, like LVM on LUKS.

To generate an initramfs for the running kernel:

# dracut /boot/initramfs-linux.img

/boot/initramfs-linux.img refers to the output image file. If you are using the non-regular kernel, consider changing the file name. For example, for the linux-lts kernel, the output file should be named /boot/initramfs-linux-lts.img. However, you can name these files whatever you wish as long as your bootloader configuration uses the same file names.

Additional flags

The --hostonly flag creates an image that only contains the files needed to boot the local host system, instead of creating a generic image with more files. Using this flag reduces the size of the generated image, but you won't be able to use it on other computers or switch to a different root file system without generating a new image.

The --force flag overwrites the image file if it's already present.

More flags can be found with man dracut.

Configuration

If you wish to always execute dracut with a certain set of flags, you can save a specified configuration in a .conf file in /etc/dracut.conf.d/. For example:

/etc/dracut.conf.d/myflags.conf
hostonly="yes"
compress="lz4"
add_drivers+=" i915 "
omit_dracutmodules+=" network iscsi "

You can see more configuration options with man dracut.conf. Fuller descriptions of each option can be found with man dracut.

Tips and tricks

View information about generated image

You can view information about a generated initramfs image, which you may wish to view in a pager:

$ lsinitrd <path to initramfs image> | less

This command will list the arguments passed to dracut when the image was created, the list of included dracut modules, and the list of all included files.

Change compression program

To reduce the amount of time spent compressing the final image, you may change the compression program used.

Warning: Make sure your kernel has your chosen decompression support compiled in, otherwise you will not be able to boot. You must also have the chosen compression program package installed. Note that the kernels provided in the Arch repos do not support zstd compression.

Simply add any one of the following lines (not multiple) to your dracut configuration:

compress="gzip"
compress="bzip2"
compress="lzma"
compress="xz"
compress="lzo"
compress="lz4"

gzip is the default compression program used.

You can also use a non-officially-supported compression program:

compress="<program>"

Generate a new initramfs on kernel upgrade

It is possible to automatically generate new initramfs images upon each kernel upgrade. The instructions here are for the default linux kernel, but it should be easy to add extra hooks for other kernels.

As the command to figure out the kernel version is somewhat complex, it will not work by itself in a pacman hook. So create a script anywhere on your system. For this example it will be created in ~/.local/bin:

~/.local/bin/90-dracut-linux.script
#!/bin/bash

args=('-H' '--no-hostonly-cmdline')

while read -r line; do
    if [[ $line = usr/lib/modules/+([^/])/pkgbase ]]; then
        mapfile -O ${#pkgbase[@]} -t pkgbase < "/$line"
        kver=${line#"usr/lib/modules/"}
        kver=${kver%"/pkgbase"}
    fi
done

dracut "${args[@]}" -f /boot/initramfs-"${pkgbase[@]}".img --kver "${kver[@]}"
dracut -f /boot/initramfs-"${pkgbase[@]}"-fallback.img --kver "${kver[@]}"

You may need to make the script executable with chmod a+x ~/.local/bin/90-dracut-linux.script. If you wish to add or remove flags, you should add them to your dracut configuration.

The next step is creating a pacman hook:

/etc/pacman.d/hooks/90-dracut-linux.hook
[Trigger]
Type = File
Operation = Install
Operation = Upgrade
Target = usr/lib/modules/*/pkgbase

[Action]
Description = Updating linux initcpios (with dracut!)...
When = PostTransaction
Exec = /home/username/.local/bin/90-dracut-linux.script
NeedsTargets

You must replace username with your user name.

You can stop mkinitcpio from creating initramfs images as well, either by removing mkinitcpio or with the following command:

# ln -sf /dev/null /etc/pacman.d/hooks/90-mkinitcpio-install.hook

You will also have to copy the new vmlinuz kernel file from /usr/lib/modules/[kver]/vmlinuz to /boot/vmlinuz-linux every kernel upgrade, as the linux package is no longer responsible for that -- 90-mkinitcpio-install.hook is. From the arch-general mailing list:

The kernel does not install itself anymore to /boot, as you've noticed. But, the mkinitcpio
hook does that. For now, we are replicating the same behavior as before, but with a little
more flexibility.
I'm working on dracut hooks for doing a similar job, but the idea is that we eventually will
be more flexible with our booting, giving the user more options. Keep an eye on the Arch announce
mailing list, as well as the news on the Arch site.

See also