Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - InitRAMFS, Dracut, and the Dracut Emergency Shell

#1
InitRAMFS, Dracut, and the Dracut Emergency Shell

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/initramfs-dracut-and-the-dracut-emergency-shell.jpg" width="720" height="540" title="" alt="" /></div><div><p>The <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://en.wikipedia.org/wiki/Linux_startup_process" target="_blank">Linux startup process</a> goes through several stages before reaching the final <a href="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-targets" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">graphical or multi-user target</a>. The initramfs stage occurs just before the root file system is mounted. Dracut is a tool that is used to manage the initramfs. The dracut emergency shell is an interactive mode that can be initiated while the initramfs is loaded.</p>
<p>This article will show how to use the dracut command to modify the initramfs. Some basic troubleshooting commands that can be run from the dracut emergency shell will also be demonstrated.</p>
<h2>The InitRAMFS</h2>
<p><a href="https://en.wikipedia.org/wiki/Initial_ramdisk" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Initramfs</a> stands for <u>Init</u>ial <u>R</u>andom-<u>A</u>ccess <u>M</u>emory <u>F</u>ile <u>S</u>ystem. On modern Linux systems, it is typically stored in a file under the /boot directory. The kernel version for which it was built will be included in the file name. A new initramfs is generated every time a new kernel is installed.</p>
<figure class="wp-block-image"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/initramfs-dracut-and-the-dracut-emergency-shell.jpg" alt="" class="wp-image-27101" /><figcaption>A Linux Boot Directory</figcaption></figure>
<p>By default, Fedora keeps the previous two versions of the kernel and its associated initramfs. This default can be changed by modifying the value of the <em>installonly_limit</em> setting the /etc/dnf/dnf.conf file.</p>
<p>You can use the <em>lsinitrd</em> command to list the contents of your initramfs archive:</p>
<figure class="wp-block-image"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/initramfs-dracut-and-the-dracut-emergency-shell-1.jpg" alt="" class="wp-image-27105" /><figcaption>The LsInitRD Command</figcaption></figure>
<p>The above screenshot shows that my initramfs archive contains the <em>nouveau</em> GPU driver. The <em>modinfo</em> command tells me that the nouveau driver supports several models of NVIDIA video cards. The <em>lspci</em> command shows that there is an NVIDIA GeForce video card in my computer’s PCI slot. There are also several basic Unix commands included in the archive such as <em>cat</em> and <em>cp</em>.</p>
<p>By default, the initramfs archive only includes the drivers that are needed for your specific computer. This allows the archive to be smaller and decreases the time that it takes for your computer to boot.</p>
<h2>The Dracut Command</h2>
<p>The <em>dracut</em> command can be used to modify the contents of your initramfs. For example, if you are going to move your hard drive to a new computer, you might want to temporarily include all drivers in the initramfs to be sure that the operating system can load on the new computer. To do so, you would run the following command:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># dracut –force –no-hostonly</div>
</div>
<p>The <em>force</em> parameter tells dracut that it is OK to overwrite the existing initramfs archive. The <em>no-hostonly</em> parameter overrides the default behavior of including only drivers that are germane to the currently-running computer and causes dracut to instead include all drivers in the initramfs.</p>
<p>By default dracut operates on the initramfs for the currently-running kernel. You can use the <em>uname</em> command to display which version of the Linux kernel you are currently running:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ uname -r<br />
5.0.5-200.fc29.x86_64</div>
</div>
<p>Once you have your hard drive installed and running in your new computer, you can re-run the dracut command to regenerate the initramfs with only the drivers that are needed for the new computer:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># dracut –force</div>
</div>
<p>There are also parameters to add arbitrary drivers, dracut modules, and files to the initramfs archive. You can also create configuration files for dracut and save them under the /etc/dracut.conf.d directory so that your customizations will be automatically applied to all new initramfs archives that are generated when new kernels are installed. As always, check the man page for the details that are specific to the version of dracut you have installed on your computer:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ man dracut</div>
</div>
<h2>The Dracut Emergency Shell</h2>
<figure class="wp-block-image"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/initramfs-dracut-and-the-dracut-emergency-shell-2.jpg" alt="" class="wp-image-27178" /><figcaption>The Dracut Emergency Shell</figcaption></figure>
<p>Sometimes something goes wrong during the initramfs stage of your computer’s boot process. When this happens, you will see “Entering emergency mode” printed to the screen followed by a shell prompt. This gives you a chance to try and fix things up manually and continue the boot process.</p>
<p>As a somewhat contrived example, let’s suppose that I accidentally deleted an important kernel parameter in my boot loader configuration:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sed -i ‘s/ rd.lvm.lv=fedora\/root / /’ /boot/grub2/grub.cfg</div>
</div>
<p>The next time I reboot my computer, it will seem to hang for several minutes while it is trying to find the root partition and eventually give up and drop to an emergency shell.</p>
<p>From the emergency shell, I can enter <em>journalctl</em> and then use the <strong>Space</strong> key to page down though the startup logs. Near the end of the log I see a warning that reads “/dev/mapper/fedora-root does not exist”. I can then use the <em>ls</em> command to find out what does exist:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># ls /dev/mapper<br />
control &nbsp;fedora-swap</div>
</div>
<p>Hmm, the fedora-root LVM volume appears to be missing. Let’s see what I can find with the lvm command:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># lvm lvscan<br />
&nbsp; ACTIVE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;‘/dev/fedora/swap’ [3.85 GiB] inherit<br />
&nbsp; inactive &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;‘/dev/fedora/home’ [22.85 GiB] inherit<br />
&nbsp; inactive &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;‘/dev/fedora/root’ [46.80 GiB] inherit</div>
</div>
<p>Ah ha! There’s my root partition. It’s just inactive. All I need to do is activate it and exit the emergency shell to continue the boot process:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># lvm lvchange -a y fedora/root<br />
# exit</div>
</div>
<figure class="wp-block-image"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/initramfs-dracut-and-the-dracut-emergency-shell-3.jpg" alt="" class="wp-image-27121" /><figcaption>The Fedora Login Screen</figcaption></figure>
<p>The above example only demonstrates the basic concept. You can check the <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="http://www.kernel.org/pub/linux/utils/boot/dracut/dracut.html#_troubleshooting" target="_blank">troubleshooting section</a> of the <a href="http://www.kernel.org/pub/linux/utils/boot/dracut/dracut.html" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">dracut guide</a> for a few more examples.</p>
<p>It is possible to access the dracut emergency shell manually by adding the <em>rd.break</em> parameter to your kernel command line. This can be useful if you need to access your files before any system services have been started.</p>
<p>Check the <em>dracut.kernel</em> man page for details about what kernel options your version of dracut supports:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ man dracut.kernel</div>
</div>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016