Wondering how to use dracut command and automate processes on Linux? Here’s everything you need to do.
Dracut is a program that contains a set of tools used to automate the Linux boot process. If you don’t know yet, booting is the process of starting an electronic device with the help of buttons.
This program can be used as a command in the Linux Terminal, providing different features like easy automated booting. This command creates a boot image of Linux, and combines that with the dracut framework found in /usr/lib/dracut/modules.d
.
The dracut command is used to change contents in your initramfs. So, to use the current hard drive to a new computer, you need to include all drivers temporarily.
How to use dracut on Linux OS
Prerequisite
- Linux OS
- Sudo privilege
- dracut package
Installing dracut
First, check if your pc has dracut installed or not. You can use any of the dracut-related commands to check.
Just type dracut in the Terminal, and if you get a result like this, then you have to install dracut with the following command:
$ sudo apt install dracut
Or
$ sudo apt install dracut-core
Now run dracut in the Terminal again to check if it’s installed or not.
Input:
Here, you will get something like this.
Listing All Modules
To see all the listed modules located in /ur/lib/dracut/modules.d
directory, you can run the following command:
$ dracut --list-modules
Or
$ dracut --list-modules --no-kernel
Overwrite existing image
Dracut contains default modules, which can be overridden by CLI or configuration files. These modules use scripts to perform various tasks.
To create an initramfs image, we use the dracut command. This command creates initramfs images, and these images have all workable functionality.
To overwrite an existing image, use this command.
Input:
$ sudo dracut -force
Or if you try using
$ sudo dracut -force --no-kernel
You will see something like this:
This command will create an image file, replacing the already existing one, then you can use that image with the following syntax.
Syntax:
$ dracut [filename.img]
Booting parameters
You can boot from a modified partition without recreating new initrafms images and start the right kernel like this:
$ sudo dracut --print-cmdline
Output:
Creating initramfs for the running kernel
Similarly, you also create an image file for a running kernel using the following command:
$ sudo dracut --hostonly --no-hostonly-cmdline /boot/initramfs-linux.img
If this command doesn’t work, try adding --no-kernel
flag at the end like this:
$ sudo dracut --hostonly --no-hostonly-cmdline /boot/initramfs-linux.img --no-kernel
You can hit enter a few times if it feels like it’s stuck.
In the above command, initramfs-linux.img refers to the image file going to be generated.
Apparently, you can build an initramfs image for a specific kernel version. The easiest way is to use --kver
flag, invoking dracut, and then providing the kernel version as an argument.
Syntax:
The command above will build the image file in /boot directory. If you want to create an image in the directory you currently use, you can use the following command.
Syntax:
initramfs image for all existing kernels
Dracut also provides the feature to build and rebuild all the existing kernels on our OS. To achieve this, you can use the ‘--regenerate-all
’ flag. After a while, if you already have an image, you may use ‘--force flag
’ to force this process and overwrite existing files.
Input:
$ sudo dracut --regenerate-all --force
Moving hard drive to another PC
While moving your current hard drive to another PC, you may have to include all drivers in initramfs temporarily. This ensures that you can load your current OS on your new computer. You can achieve this using the following command:
$ dracut --foce --no-hostonly
Depending on the OS, you might have to give sudo privileges. And you might also need to give the ‘--no-kernel
’ flag. So the revised command may look like this:
$ sudo dracut --foce --no-hostonly --no-kernel
Output may look different depending upon your PC, OS, kernel, etc.
Dracut Emergency shell for recovery
Suppose your PC hangs for a few minutes to find the root partition from the bootloader. Then it goes to the emergency shell.
In the emergency shell, you can enter journalctl
and get the startup logs using the space key. There you can see an error log that is unable to find the root partition.
You can use ls
command to see what is missing in the /dev/mapper
directory.
Input:
You can see, fedora-root LVM is missing. Now list all partition with the following command:
# lvm lvscan
Output may look like this:
ACTIVE ‘/dev/fedora/swap’ [3.85GiB]
inactive ‘/dev/fedora/root’ [46.80GiB]
Here you can see the root partition is still there but just inactive. Now to get your system back, you just need to activate that partition and exit the emergency shell.
Syntax:
$ lvm lvchange -a y fedora/root
$ exit
Useful dracut command flags
Syntax:
$ dracut [flags] [image_file] [kernel]
As discussed so far, dracut creates an image file that the kernel can use to preload the device block modules. These modules are needed to access the root file system:
-f or --force
: Overwrites the existing image (initramfs) file[-m or --modules] LIST
: To provide modules located in /usr/share/dracut/modules.d[-o or --omit] LIST
: To omit the list of modules that are space separated[-a or --add] LIST
: Adds a list of modules (space separated).--filesystems LIST
: Just like other flags, lists of space separated kernel file systems.[-H or --hostonly]
: This command installs drivers and packages required to boot the local host.
And just like that, you’ve figured out what the dracut command can do, and how to use it the right way. If you have any questions on how to use the dracut command on Linux OS, don’t forget to leave a comment down below.
If this guide helped you, please share it.