Early Userspace Mounting

This article will detail Article description::how to build a custom minimal [[initramfs that checks the filesystem and pre-mounts .]] This has become necessary for affected configurations because of various changes in udev (see ).

In this article we'll be working with the following:


 * Busybox
 * An initramfs content list
 * The and  utilities, provided by the kernel itself.

The initramfs also contains the required libraries and binaries to run an ext4 fsck. Most of the code to run the fsck is coming from the script.

When using any other filesystem than ext4, add the required binaries / libraries into the initramfs list.

Basically, the init script is doing following actions:


 * 1) Mounts the root partition on  as read-only.
 * 2) Symlinks the /etc/fstab from the root partition to the initramfs environment.
 * 3) Checks the filesystem of our  device using the embedded  binary.
 * 4) Mounts, then moves it to  using the   mount parameter.
 * 5) Switches to real root and executes.

The article also assumes we are working in, so for the sake of ease, begin with creating this directory.

Requirements
The most important package here is as it provides utilities suitable for an initramfs. It is also critical that to emerge it with  USE flag enabled:

Make sure that the running kernel is built with the devtmpfs option enabled. It is required by the init script below and udev:

Next up is the file which will tell  how to construct the initramfs:

Copy and save the contents of the above to after adjusting for the current architecutre.

Last up is the actual file which will execute the initramfs:

Copy and save the contents of the above to.

System preparation
In fstab, we must set the sixth field for the  entry to , this will prevent the OpenRC fsck init script to try to check the filesystem for the already mounted :

Building as an embedded Initramfs
It is not necessary to compile or make it executable because these steps will be handled when building the kernel with. Both files, and  must be copied into. For an embedded initramfs one line is missing and must be added to

This kernel configuration will do all steps to include all needed files in an embedded initramfs.

For embedding the initramfs directly into the kernel image, the must be coded in Initramfs source file(s) ( CONFIG_INITRAMFS_SOURCE ) in the kernel (directly under the Initial RAM filesystem and RAM disk (initramfs/initrd) support ( CONFIG_BLK_DEV_INITRD ) option):

Building as an external CPIO archive
The kernel sources provide the and  utilities. The utility does not come prepackaged and needs to be built:

Make sure that these two are executable:

Run the script with the   argument pointing to where we want the initramfs image to be placed followed by the path to our  file:

After that compress the file via :

This will create the archive.

Bootloader configuration
To use the external initramfs, the bootloader needs to be configured as shown below for GRUB and LILO as examples. For an embedded Initramfs this is not necessary !

Configuring GRUB
Add the  line to :

Configuring LILO
Add the  and   line to :

Using a Stub Kernel
If no bootmanager is used (UEFI boots a stub kernel directly) the UUID of the root partition must be configured into the built-in kernel command line or as parameter in the UEFI boot entry (see next paragraph):

When using an external Initramfs must be copied to the EFI System Partition and initrd= must use the correct path. Set the parameter initrd= only in an UEFI boot entry. It does not work when setting it in the built-in kernel command line. In this case it is recommended to set both parameter in this UEFI boot entry. Example:

See more here: Efibootmgr

Result
When booting, the output looks like this:

External resources

 * http://jootamam.net/howto-initramfs-image.htm
 * http://whitehathouston.com/documentation/gentoo/initramfs_howto.htm Link seems dead
 * http://www.landley.net/writing/rootfs-howto.html
 * https://forums.gentoo.org/viewtopic-p-7038132.html