User:Pietinger/Tutorials/Initramfs Overview

Tutorial: Initramfs Overview
In my point of view we have the best descriptions of an initramfs in our Wiki here ... but it is all a little bit "mixed". This tutorial gathers all links and informations needed to understand initramfs a little bit better (hopefully). Only links to our Wiki will be used.

What is an initramfs ?
In very easy words: It is a small linux system your kernel can start before of your main linux system. If you use an initramfs, it MUST mount your root partition of your main system, because kernel will not do this job anymore. In most cases it will also start your as last step, so your usual system start will run afterwards (or - very seldom - initramfs do all steps by itself to boot up your system).

Why ? / When do I need an initramfs ?
Because you can do things at boot-time a kernel is not able to do. Most famous example is: Decrypting a root partition. But there are also other reasons to use it. I will give you later some links for these "Solutions".

Type of initramfs
You can have two types of an initramfs: Embedded into the kernel image or as an external CPIO archive. This leads to two questions:

1. What are the advantages and disadvantages ?

2. How to build them ?

My answer to the first question:

Advantage of an embedded: Very easy to build IF you have access to kernel configuratiion (= not possible if you use the bin-kernel).

Disadvantage: If you want to change something IN your initramfs you must compile the kernel again.

Advantage of an external CPIO archive: You can change/update to other kernel versions without needing to compile your kernel again (if you can use the initramfs for many different kernel versions). Used in binary distributions OR in Linux-Boot-CD.

Disadvantage: You must configure one additional kernel command line parameter (initrd=) so your kernel can find (and load) this CPIO archive.

What do I need to build an initramfs ?
You can use a tool to build it automatically. For this we have enough articles and it is not part of this description.

For building it manually we need at minimum two files: The "new" init = your personal script, which kernel starts now instead of, and a list of files which shall be included in your initramfs. Instead of this file-list you can also build a directory with all needed files inside. So, here you have also two options. Together with the type of an initramfs you will now have 4 options for building an initramfs ;-)

Initramfs and your main root partition
As said before, your initramfs must mount your root partition and therefore needs the same information as kernel has needed before: Where is my root partition ? Here you have some choices ... depending if you use a bootmanager or if you use a stub kernel which will be started from your UEFI directly.

Building an initramfs
I will begin with the easiest type ...

Embedded with a file-list
1. Copy both files into a directory you like. In most articles here in our wiki is used. Your init-file MUST have the name "init". Your file-list can can have every name you like, but in most articles here in our wiki is used.

2. Configure your kernel with:

3. Build your kernel with "make" and install it - as you always do.

An example for this you will find here: Early_Userspace_Mounting

Embedded with a directory
1. Copy your AND all files you need into a directory you like. In most articles here in our wiki is used.

2. Configure your kernel with:

( You see the difference in "Initramfs source file(s)", do you ? )

3. Build your kernel with "make" and install it - as you always do.

Now it is time to give you a link to a great Wiki article: Custom_Initramfs

External CPIO archive with a file-list
1. Copy both files into a directory you like. In most articles here in our wiki is used. Your init-file MUST have the name "init". Your file-list can can have every name you like, but in most articles here in our wiki is used.

2. Do these steps:

3. Copy this file either into your /boot directory OR into your efi-directory of your ESP (depending if you use a bootmanager or an UEFI boot).

4. You need this in your kernel configuration:

5. Configure your bootmanager or your UEFI that kernel will get the needed command line parameter: See next chapter.

An example for this you will find here: Early_Userspace_Mounting

External CPIO archive with a directory
1. Copy your AND all files you need into a directory you like. In most articles here in our wiki is used.

2. Do these steps:

3. Copy this file either into your /boot directory OR into your efi-directory of your ESP (depending if you use a bootmanager or an UEFI boot).

4. You need this in your kernel configuration:

5. Configure your bootmanager or your UEFI that kernel will get the needed command line parameter: See next chapter.

An example for this you will find here: Custom_Initramfs

Special Case: Building an embedded initramfs with a CPIO archive
If you have already an external CPIO archive but you want an embedded initramfs then you can build it with a "make" and these kernel settings:

or:

It is important to use suffix .cpio so "make" understand it is a CPIO file !

It is important to use suffix .cpio.gz so "make" understand it is an already gzipped CPIO file

(Both is explained in )

Telling the kernel where to find its external CPIO archive
This is done with the kernel command line parameter "initrd=..." and you have a description already in Early_Userspace_Mounting

Telling initramfs where to find the "real" root partition
This is a little bit more complicated because it depends how your evaluates a kernel command line parameter. In most cases a "root=/dev/sdXY" will always work. Because this can sometimes lead to troubles it is recommended to use the ID of your root. Here it could be we must change something.

In many cases you cannot use a "root=PARTUUID=..." anymore because is a busybox-shell-script and needs "initrd=UUID=..." instead. Just look into the description of your "Solution".

Why do I need an additional line when using an embedded initramfs ?
You have read Early_Userspace_Mounting and you want to know why this line is additionally necessary:

The answer is: The kernel needs ALWAYS /dev/console BEFORE starting an initramfs (even if your initramfs uses a "mount -t devtmpfs none /dev").

Next question is: But why I dont need it when building an external CPIO archive ?

Because our kernel will ALWAYS build an embedded initramfs - even if you dont want ;-) For this it takes the file and builds with this an (additionally) emebdded initramfs. Now look into this file ...

So, I have two initramfs ? Yes, your kernel merges this embedded initramfs with your external CPIO archive together.

Solutions
You need only one link: Custom_Initramfs ... ;-)