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
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== What is initramfs? ==
== What is initramfs? ==
Wikipedia defines initramfs as:
[http://en.wikipedia.org/wiki/Initramfs Wikipedia] defines initramfs as:
<blockquote>
<blockquote>
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.
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.
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.
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 {{c|/opt}} directory (or any other directory that you deem suitable for building packages in) and clone the better-initramfs repository from bitbucket:
{{console|body=
###i## cd /opt
###i## git clone https://bitbucket.org/piotrkarbowski/better-initramfs.git
}}
=== Building {{c|/initramfs.cpio.gz}} ===
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}}.
{{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|body=
###i## cd better-initramfs
###i## bootstrap/bootstrap-all
###i## make prepare
###i## make image
###i## cp output/initramfs.cpio.gz /boot
}}
=== Configuring {{c|/etc/boot.conf}} ===
Now that the {{c|initramfs.cpio.gz}} 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 ====
{{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 ====
Below are examples of different use cases for better-initramfs and how your {{c|/etc/boot.conf}} should be configured for them.
{{Note|Realize that your {{c|enc_root}}, {{c|root}}, and {{c|rootfstype}} variables may be different from the examples provided below.}}
==== Regular ext4 root ====
{{file|name=/etc/boot.conf|desc= |body=
"Funtoo Linux" {
kernel vmlinuz[-v]
initrd /initramfs.cpio.gz
params += root=/dev/sdaX rootfstype=ext4
}
}}
==== Encrypted root with lvm ====
{{file|name=/etc/boot.conf|desc= |body=
"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 ====
{{file|name=/etc/boot.conf|desc= |body=
"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:
{{console|body=
###i## emerge dracut
}}
=== Build the initramfs ===
To build the initrd with dracut, run:
{{console|body=
###i## 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. {{c|--add-drivers "xz dm_crypt"}} etc...).
=== Configuring {{c|/etc/boot.conf}} ===
==== Examples ====
==== Encrypted root with lvm ====
{{file|name=/etc/boot.conf|desc= |body=
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
}
}}
[[Category:Kernel]]
[[Category:Kernel]]

Latest revision as of 11:45, March 23, 2016

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

Configuring /etc/boot.conf

Now that the initramfs.cpio.gz 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
}