Note

The Funtoo Linux project has transitioned to "Hobby Mode" and this wiki is now read-only.

Difference between revisions of "Initramfs"

From Funtoo
Jump to navigation Jump to search
(Updated the <code> and <console> tags on the page to {{c}} and {{console}} templates. Replaced the old table of better initramfs arguments with a new one.)
Line 6: Line 6:


== Do I need an initramfs? ==
== Do I need an initramfs? ==
The reason to build a kernel with an initramfs is mostly for interoperability (e.g. live-cd's) and special features like an included busybox, ssh, etc. But mostly, and that's why we are doing this here now, to have a proper kernel up and running quick'n dirty in a reasonable time without fighting hours and days until a more or less exotic hardware is perfectly run by the kernel. After having a proper basic kernel running with the help of an initramfs, I really recommend you to go a step further and build a true kernel with all features includes without an initramfs. However, relying only on a kernel to boot a system can be quite time consuming, so we have provided several initramfs options for Funtoo. If you have decided to use an initramfs, not just a kernel, check out the options below and choose the one that you like the most.
The reason to build a kernel with an initramfs is mostly for interoperability (e.g. live-cd's) and special features like an included busybox, ssh, etc. But mostly, and that's why we are doing this here now, to have a proper kernel up and running quick'n dirty in a reasonable time without fighting hours and days until a more or less exotic hardware is perfectly run by the kernel. After having a proper basic kernel running with the help of an initramfs, I really recommend you to go a step further and build a true kernel with all features included without an initramfs. However, relying only on a kernel to boot a system can be quite time consuming, so we have provided several initramfs options for Funtoo. If you have decided to use an initramfs, not just a kernel, check out the options below and choose the one that you like the most.


== better-initramfs ==
== better-initramfs ==
Line 12: Line 12:


=== Installation from git ===
=== Installation from git ===
To install better-initramfs on your system, change to the <code>/opt</code> directory (or any other directory that you deem suitable) and clone the better-initramfs repository from bitbucket.
To install better-initramfs on your system, change to the {{c|/opt}} directory (or any other directory that you deem suitable for building packages in) and clone the better-initramfs repository from bitbucket:
<console>
{{console|body=
###i## cd /opt
###i## cd /opt
###i## git clone https://bitbucket.org/piotrkarbowski/better-initramfs.git
###i## git clone https://bitbucket.org/piotrkarbowski/better-initramfs.git
</console>
}}


=== Building <code>/initramfs.cpio.gz</code> ===
=== Building {{c|/initramfs.cpio.gz}} ===
Now that you have better-initramfs on your system, we can make the <code>initramfs.cpio.gz</code> image and move it to <code>/boot</code>.
Now that you have the better-initramfs sources on your system, we can build the {{c|initramfs.cpio.gz}} image and move it to {{c|/boot}}.
{{fancynote| do not run <code>make prepare</code> or <code>make image</code> until you have configured your kernel. If you have not installed / configured a kernel yet, check out: [[Building_a_Kernel_from_Source| Building a Kernel From Source]] and [[Funtoo_Linux_Kernels| Funtoo Linux Kernels]].}}
{{Note|Do not run {{c|make prepare}} or {{c|make image}} until you have configured your kernel. If you have not installed / configured a kernel yet, read: [[Building_a_Kernel_from_Source| Building a Kernel From Source]] and [[Funtoo_Linux_Kernels| Funtoo Linux Kernels]].}}
<console>
{{console|body=
###i## cd better-initramfs
###i## cd better-initramfs
###i## bootstrap/bootstrap-all
###i## bootstrap/bootstrap-all
Line 27: Line 27:
###i## make image
###i## make image
###i## cp output/initramfs.cpio.gz /boot
###i## cp output/initramfs.cpio.gz /boot
</console>
}}


=== Installation using portage ===
=== Installation using portage ===
If you don't want the version from git with the newest features and bugs, you can emerge <tt>better-initramfs-bin</tt>:
If you don't want the version from git with the newest features '''and''' bugs, you can emerge {{c|better-initramfs-bin}}:
<console>
{{console|body=
###i## emerge better-initramfs-bin
}}
==== The {{c|gzip}} use flag ====
The ebuild for {{c|better-initramfs-bin}} allows for the installation of the better-initramfs initramfs without gzip compression. Although gzip compression is enabled on the image by default, it can be disabled by emering {{c|better-initramfs-bin}} without the gzip USE flag. To disable the gzip use flag for {{c|better-initramfs-bin}}, do the following:
{{console|body=
###i## mkdir /etc/portage/package.use (if it doesn't exist on your system)
###i## echo "sys-kernel/better-initramfs-bin -gzip" > /etc/portage/package.use/better-initramfs-bin
}}
Now, when better initramfs is emerged, the initramfs file in {{c|/boot}} will be called {{c|/boot/initramfs.cpio}} instead of {{c|initramfs.cpio.gz}}. To emerge better initramfs, run the following:
{{console|body=
###i## emerge better-initramfs-bin
###i## emerge better-initramfs-bin
</console>
}}


=== Configuring <code>/etc/boot.conf</code> ===
=== Configuring {{c|/etc/boot.conf}} ===
Now that the <code>initramfs.cpio.gz</code> file is in <code>/boot</code>, we can configure what we want the initramfs to do for us when the system boots. Below is a table of options that better-initramfs supports (from [https://bitbucket.org/piotrkarbowski/better-initramfs better-initramfs Bitbucket page]):
Now that the {{c|initramfs.cpio.gz}} or {{c|initramfs.cpio}} file is in {{c|/boot}}, we can configure what we want the initramfs to do for us when the system boots. Below is a table of options that better-initramfs supports (from [https://bitbucket.org/piotrkarbowski/better-initramfs better-initramfs Bitbucket page]):
==== Options ====
==== Options ====
{| {{table}}
!Option
!Description
|-
|rescueshell
|Drop to the rescueshell before mounting rootfs to <code>/newroot</code>
|-
|sshd
|Run the sshd server when the initramfs loads. This allows you to ssh into your initramfs to fix any errors that may have occurred while booting.
|-
|sshd_wait=x
|Wait x number of seconds after starting sshd to continue booting the system.
|-
|sshd_port=x
|Change the port that sshd "listens" on. The default port for sshd is port 22.
|-
|binit_net_if=interface
|Choose which interface the network should be configured on. For example: wlan0, eth0, etc.
|-
|binit_net_addr=<ipaddr/cidr>
|Configure <code>ipaddr</code> with <code>cidr</code> netmask. For example: <code>11.11.11.2/24</code>. If you do not add a netmask, the netmask is assumed to be 32. Furthermore, you will have to configure <code>binit_net_gw</code>.
|-
|binit_net_gw=ipaddr
|The address of your gateway. Only needed if you want to connect to WAN.
|-
|rw
|Mount the rootfs as read-write. By default, the rootfs is mounted as read only.
|-
|mdev
|Use mdev instead of devtmpfs. This option can prove handy on older kernels.
|-
|softraid
|Detect and run RAID arrays.
|-
|init=
|Change the init system. For example, if you are using systemd, change this to <code>/usr/lib/systemd/systemd</code>. The default for this option (<code>/sbin/init</code>) will work fine with OpenRC.
|-
|tuxonice
|Try resuming with TuxOnIce.
|-
|uswsusp
|Try resuming the system with userspace software suspend. This depends on the <code>resume</code> option.
|-
|swsusp
|Try resuming the system with in-kernel suspend. This depends on the <code>resume</code> option.
|-
|resume=<device/path>
|Specify the device and path from which you want to resume.
|-
|lvm
|Scan for volume groups. If any are found, activate them.
|-
|luks
|Run <code>cryptsetup luksOpen</code> on the <code>enc_root</code> variable.
|-
|luks_trim
|Enable support for TRIM on your LUKS encrypted root device. This option is very helpful for those using SSDs.
|-
|enc_root=<device>
|The device that you wish to decrypt using <code>cryptsetup luksOpen</code>.
|-
|root=<device>
|The name of your root device.
|-
|rootfstype=<fstype>
|Specify the type of filesystem that your rootfs uses.
|-
|rootdelay=<time (as integer)>
|How long the initramfs should wait before attempting to mount devices.
|-
|rootflags=x
|Pass x flags to <code>mount</code> as it mounts your rootfs.
|}


{{TableStart}}
<tr class="info"><th>Option</th><th>Description</th></tr>
<tr><td>rescueshell</td><td>Drop to rescueshell just before mount rootfs to /newroot.</td></tr>
<tr><td>sshd</td><td>Run sshd server. Lets you ssh into initramfs on error, to input password for encrypted rootfs, or to fix something remotly.</td></tr>
<tr><td>sshd_wait=x</td><td>Wait X seconds after setting up sshd, useful when you want to login (and thus pause boot process) before booting real system.</td></tr>
<tr><td>sshd_port=x</td><td>Setup sshd to listen on X port. Default: 22.</td></tr>
<tr><td>binit_net_if=<id></td><td>Specify on which interface the network should be configured. Optionally a vlan can be specified separated by a dot. Example: eth0 or eth0.55</td></tr>
<tr><td>binit_net_addr=<addr/cidr></td><td>Configure <addr> with <cidr> netmask on binit_net_if. Usualy you want something like '1.2.3.4/24'. If you will not add /CIDR, the IP will be configured with /32 thus you will be not able to connect to it unless you specify binit_net_gw.</td></tr>
<tr><td>binit_net_gw=<addr></td><td>Optional gateway config, if you want to connect via WAN.</td></tr>
<tr><td>rw</td><td>Mount rootfs in read-write. Default: read-only.</td></tr>
<tr><td>mdev</td><td>Don't check if kernel support devtmpfs, use mdev instead. (Useful for really old kernels).</td></tr>
<tr><td>softraid</td><td>Get up raid arrays</td></tr>
<tr><td>init=x</td><td>Run X after switching to newroot, Default: /sbin/init.</td></tr>
<tr><td>tuxonice</td><td>Try resuming with TuxOnIce. Depends on resume= variable which points to the device with image, usualy swap partition.</td></tr>
<tr><td>uswsusp</td><td>Try resuming with userspace software suspend. Depends on resume= variable which points to the device with the system snapshot, usually swap partition.</td></tr>
<tr><td>swsusp</td><td>Try resuming with swusps (in-kernel suspend). Depends on resume= variable which points to the device with system snapshot, usually swap partition.</td></tr>
<tr><td>resume=<device/path></td><td>Specify device from which you want to resume (with tuxonice or uswsusp).</td></tr>
<tr><td>lvm</td><td>Scan all disks for volume groups and activate them.</td></tr>
<tr><td>luks</td><td>Do cryptsetup luksOpen on enc_root variable.</td></tr>
<tr><td>enc_root=<device></td><td>For example /dev/sda2 if sda2 is your encrypted rootfs. This variable is ignored if luks isn't enabled. You can specify multiple devices with colon as separator, like enc_root=/dev/sda2:/dev/sdb2:/dev/vda1.</td></tr>
<tr><td>root=<device></td><td>For example /dev/mapper/enc_root if you have LUKS-encrypted rootfs, /dev/mapper/vg-rootfs or similar if lvm or just /dev/sdXX if you haven't rootfs over lvm or encrypted.</td></tr>
<tr><td>rootfstype=<filesystem type></td><td>Set type of filesystem on your rootfs if you do not want to use 'auto',</td></tr>
<tr><td>rootdelay=<integer></td><td>Set how many seconds initramfs should wait [for devices]. Useful for rootfs on USB device.</td></tr>
<tr><td>rootflags=x</td><td>Pass X flag(s) to mount while mounting rootfs, you can use it to specify which btrfs subvolume you want to mount.</td></tr>
<tr><td>luks_no_discards</td><td>Disable discards support on LUKS level, use if you don't want to allow lvm layer (if used) to send discards on reduce/resize or filesystem layer on file deletions to underlaying storage thru dmcrypt luks layer. Disabling discards on SSD-type storage may noticable degradate performance over time.</td></tr>
<tr><td>bcache</td><td>Bring up bcache devices. This will get ready for use /dev/bcache* which means one can have rootfs on bcache as well as anything else.</td></tr>
{{TableEnd}}
==== Examples ====
==== Examples ====
Below are examples of different use cases for better-initramfs and how your <code>/etc/boot.conf</code> should be configured for them.  
Below are examples of different use cases for better-initramfs and how your {{c|/etc/boot.conf}} should be configured for them.  
{{fancynote| Realize that your <code>enc_root</code>, <code>root</code>, and <code>rootfstype</code> variables may be different from the examples provided below.}}
{{Note|Realize that your {{c|enc_root}}, {{c|root}}, and {{c|rootfstype}} variables may be different from the examples provided below.}}


==== Regular ext4 root ====
==== Regular ext4 root ====
Line 145: Line 109:
== Dracut ==
== Dracut ==
=== Installation ===
=== Installation ===
To install Dracut, simply emerge it:
To install Dracut, emerge it:
<console>
{{console|body=
###i## emerge dracut
###i## emerge dracut
</console>
}}
 
=== Build the initramfs ===
=== Build the initramfs ===
To build the initrd with dracut, we just execute:
To build the initrd with dracut, run:
<console>
{{console|body=
###i## dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img  3.2.6-pf
###i## dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img  3.2.6-pf
</console>
}}
 
Generally, this should be enough!
Generally, this should be enough!
If you experience booting problems like missing modules / drivers then just boot from your livecd and fix Dracut or consider changing to another initramfs. You can look into the man page to tweak the command a bit (e.g. --add-drivers "xz dm_crypt" etc...).
If you experience booting problems like missing modules / drivers then just boot from your livecd and fix Dracut or consider changing to another initramfs. You can look into the man page to tweak the command a bit (e.g. {{c|--add-drivers "xz dm_crypt"}} etc...).


=== Configuring <code>/etc/boot.conf</code> ===
=== Configuring {{c|/etc/boot.conf}} ===
==== Examples ====
==== Examples ====



Revision as of 16:13, June 30, 2015

What is initramfs?

Wikipedia defines initramfs as:

initramfs, abbreviated from "initial ram file system", is the successor of initrd. It is a cpio archive of the initial file system that gets loaded into memory during the Linux startup process. The Linux kernel mounts it as root file system and starts the init process from it. This will complete certain tasks before the real root file system is loaded; thus, initramfs needs to contain all of the device drivers and tools needed to mount the real root filesystem.

Do I need an initramfs?

The reason to build a kernel with an initramfs is mostly for interoperability (e.g. live-cd's) and special features like an included busybox, ssh, etc. But mostly, and that's why we are doing this here now, to have a proper kernel up and running quick'n dirty in a reasonable time without fighting hours and days until a more or less exotic hardware is perfectly run by the kernel. After having a proper basic kernel running with the help of an initramfs, I really recommend you to go a step further and build a true kernel with all features included without an initramfs. However, relying only on a kernel to boot a system can be quite time consuming, so we have provided several initramfs options for Funtoo. If you have decided to use an initramfs, not just a kernel, check out the options below and choose the one that you like the most.

better-initramfs

Piotr's better-initramfs is a popular approach among Funtoo'ers to building an initramfs. It is quick, nice, and shiny. The biggest plus is that, once built, it is kernel version independent.

Installation from git

To install better-initramfs on your system, change to the /opt directory (or any other directory that you deem suitable for building packages in) and clone the better-initramfs repository from bitbucket:

root # cd /opt
root # git clone https://bitbucket.org/piotrkarbowski/better-initramfs.git

Building /initramfs.cpio.gz

Now that you have the better-initramfs sources on your system, we can build the initramfs.cpio.gz image and move it to /boot.

   Note

Do not run make prepare or make image until you have configured your kernel. If you have not installed / configured a kernel yet, read: Building a Kernel From Source and Funtoo Linux Kernels.

root # cd better-initramfs
root # bootstrap/bootstrap-all
root # make prepare
root # make image
root # cp output/initramfs.cpio.gz /boot

Installation using portage

If you don't want the version from git with the newest features and bugs, you can emerge better-initramfs-bin:

root # emerge better-initramfs-bin

The gzip use flag

The ebuild for better-initramfs-bin allows for the installation of the better-initramfs initramfs without gzip compression. Although gzip compression is enabled on the image by default, it can be disabled by emering better-initramfs-bin without the gzip USE flag. To disable the gzip use flag for better-initramfs-bin, do the following:

root # mkdir /etc/portage/package.use (if it doesn't exist on your system)
root # echo "sys-kernel/better-initramfs-bin -gzip" > /etc/portage/package.use/better-initramfs-bin

Now, when better initramfs is emerged, the initramfs file in /boot will be called /boot/initramfs.cpio instead of initramfs.cpio.gz. To emerge better initramfs, run the following:

root # emerge better-initramfs-bin

Configuring /etc/boot.conf

Now that the initramfs.cpio.gz or initramfs.cpio file is in /boot, we can configure what we want the initramfs to do for us when the system boots. Below is a table of options that better-initramfs supports (from better-initramfs Bitbucket page):

Options

OptionDescription
rescueshellDrop to rescueshell just before mount rootfs to /newroot.
sshdRun sshd server. Lets you ssh into initramfs on error, to input password for encrypted rootfs, or to fix something remotly.
sshd_wait=xWait X seconds after setting up sshd, useful when you want to login (and thus pause boot process) before booting real system.
sshd_port=xSetup sshd to listen on X port. Default: 22.
binit_net_if=<id>Specify on which interface the network should be configured. Optionally a vlan can be specified separated by a dot. Example: eth0 or eth0.55
binit_net_addr=<addr/cidr>Configure <addr> with <cidr> netmask on binit_net_if. Usualy you want something like '1.2.3.4/24'. If you will not add /CIDR, the IP will be configured with /32 thus you will be not able to connect to it unless you specify binit_net_gw.
binit_net_gw=<addr>Optional gateway config, if you want to connect via WAN.
rwMount rootfs in read-write. Default: read-only.
mdevDon't check if kernel support devtmpfs, use mdev instead. (Useful for really old kernels).
softraidGet up raid arrays
init=xRun X after switching to newroot, Default: /sbin/init.
tuxoniceTry resuming with TuxOnIce. Depends on resume= variable which points to the device with image, usualy swap partition.
uswsuspTry resuming with userspace software suspend. Depends on resume= variable which points to the device with the system snapshot, usually swap partition.
swsuspTry resuming with swusps (in-kernel suspend). Depends on resume= variable which points to the device with system snapshot, usually swap partition.
resume=<device/path>Specify device from which you want to resume (with tuxonice or uswsusp).
lvmScan all disks for volume groups and activate them.
luksDo cryptsetup luksOpen on enc_root variable.
enc_root=<device>For example /dev/sda2 if sda2 is your encrypted rootfs. This variable is ignored if luks isn't enabled. You can specify multiple devices with colon as separator, like enc_root=/dev/sda2:/dev/sdb2:/dev/vda1.
root=<device>For example /dev/mapper/enc_root if you have LUKS-encrypted rootfs, /dev/mapper/vg-rootfs or similar if lvm or just /dev/sdXX if you haven't rootfs over lvm or encrypted.
rootfstype=<filesystem type>Set type of filesystem on your rootfs if you do not want to use 'auto',
rootdelay=<integer>Set how many seconds initramfs should wait [for devices]. Useful for rootfs on USB device.
rootflags=xPass X flag(s) to mount while mounting rootfs, you can use it to specify which btrfs subvolume you want to mount.
luks_no_discardsDisable discards support on LUKS level, use if you don't want to allow lvm layer (if used) to send discards on reduce/resize or filesystem layer on file deletions to underlaying storage thru dmcrypt luks layer. Disabling discards on SSD-type storage may noticable degradate performance over time.
bcacheBring up bcache devices. This will get ready for use /dev/bcache* which means one can have rootfs on bcache as well as anything else.

Examples

Below are examples of different use cases for better-initramfs and how your /etc/boot.conf should be configured for them.

   Note

Realize that your enc_root, root, and rootfstype variables may be different from the examples provided below.

Regular ext4 root

   /etc/boot.conf
"Funtoo Linux" {
	kernel	vmlinuz[-v]
	initrd	/initramfs.cpio.gz
	params	+= root=/dev/sdaX rootfstype=ext4
}

Encrypted root with lvm

   /etc/boot.conf
"Funtoo Linux" {
	kernel	vmlinuz[-v]
	initrd	/initramfs.cpio.gz
	params	+= enc_root=/dev/sdaX lvm luks luks_trim root=/dev/mapper/funtoo--vg-root rootfstype=ext4
}

Encrypted root with lvm and RAID

   /etc/boot.conf
"Funtoo Linux" {
	kernel	vmlinuz[-v]
	initrd	/initramfs.cpio.gz
	params	+= enc_root=/dev/md0 lvm luks luks_trim softraid root=/dev/mapper/funtoo--vg-root rootfstype=ext4
}

Dracut

Installation

To install Dracut, emerge it:

root # emerge dracut

Build the initramfs

To build the initrd with dracut, run:

root # dracut -f --fstab --xz /boot/initramfs-3.2.6-pf.img  3.2.6-pf

Generally, this should be enough! If you experience booting problems like missing modules / drivers then just boot from your livecd and fix Dracut or consider changing to another initramfs. You can look into the man page to tweak the command a bit (e.g. --add-drivers "xz dm_crypt" etc...).

Configuring /etc/boot.conf

Examples

Encrypted root with lvm

   /etc/boot.conf
boot {
        generate grub
        default "Funtoo Linux dracut"
        timeout 3
}

"Funtoo Linux dracut" {
        kernel vmlinuz[-v]
        initrd initramfs[-v].img
        params  = quiet rootfstype=ext4
        params += luks enc_root=/dev/sda3
        params += lvm root=/dev/mapper/vg-root
}