Changes between Initial Version and Version 1 of linux/initramfs


Ignore:
Timestamp:
11/01/2017 03:27:05 PM (6 years ago)
Author:
Tim Harvey
Comment:

initial page

Legend:

Unmodified
Added
Removed
Modified
  • linux/initramfs

    v1 v1  
     1= initramfs =
     2The initramfs is a complete set of directories that you would find on a normal root filesystem embedded into the Linux kernel. It is bundled into a single cpio archive and compressed with one of several compression algorithms. At boot time, the boot loader loads the kernel and the initramfs image into memory and starts the kerne. If a kernel has been built to include an initramfs it mounts that instead and ignores the 'root=' kernel parameter.
     3
     4Using a small root filesystem via initramfs is a great way to do Linux kernel development or work with a system that does not yet have any via-able storage available. See [wiki:buildroot buildroot] for an example of using a small ~1.5MiB buildroot rootfs attached to a kernel.
     5
     6To build a kernel with an initramfs configure the following kernel items:
     7 * CONFIG_INITRAMFS_SOURCE="/path/to/rootfs.cpio"
     8 * CONFIG_INITRAMFS_COMPRESSION_GZIP=y (optional) - or some other compression algorithm
     9 * CONFIG_DEVTMPFS=y (optional) - to get devtmpfs support and provide a dynamic /dev
     10
     11This will build a kernel that runs {{{/init}}}, {{{/sbin/init}}}, or {{{/bin/sh}}} (searched in that order) as PID1 from the initramfs (if it exists). If you want to run a different executable as PID1 you can use the {{{init=}}} kernel parameter.
     12
     13== external initramfs ==
     14This is a {{{cpio.gz}}} that you pass to the kernel when booting. If doing this with U-Boot its the 2nd (middle) arg to bootm/booti/bootz. Note that {{{bootm}}} expects a uImage and uramdisk which have U-Boot wrappers (created with the {{{mkimage}}} tool from the {{{u-boot-tools}}} package) around the images.
     15
     16The benefit of using an external initramfs is that you do not have to rebuild the kernel to update the root filesystem.
     17
     18Many Linux based Operating Systems using an initramfs as a shim to load kernel modules that are required to mount the root filesystem allowing them to use kernels that are very small and modular. Android and Ubuntu are two very popular Linux based Operating Systems that use this technique.
     19
     20== internal initramfs ==
     21An internal initramfs is when you embed an initramfs into the kernel via {{{CONFIG_INITRAMFS_SOURCE}}} which can point to a directory path, an existing cpio.gz, or a text file with some directives.
     22
     23Note that your bootloader (ie U-Boot) may have a limit on the size of a kernel. For U-Boot this is defined by CONFIG_SYS_BOOTM_LEN which you may need to increase if either the static kernel or the initramfs gets too large.
     24
     25== Building an initramfs ==
     26Here is a simple example of building an initramfs with a stand-alone application and some basic dev nodes as the only components in the rootfs:
     27{{{#!bash
     28cat << EOF > hello.c
     29#include <stdio.h>
     30#include <unistd.h>
     31
     32int main(int argc, char *argv[])
     33{
     34    printf("Hello world!\n");
     35    sleep(999999999);
     36}
     37EOF
     38${CROSS_COMPILE}gcc -static hello.c -o hello
     39cat << EOF > initramfs
     40dir /dev 755 0 0
     41nod /dev/console 644 0 0 c 5 1
     42nod /dev/loop0 644 0 0 b 7 0
     43dir /bin 755 1000 1000
     44dir /proc 755 0 0
     45dir /sys 755 0 0
     46dir /mnt 755 0 0
     47file /init hello 755 0 0
     48EOF
     49}}}
     50
     51kernel args:
     52 * CONFIG_INITRAMFS_SOURCE="initramfs"
     53 * CONFIG_INITRAMFS_ROOT_UID=0
     54 * CONFIG_INITRAMFS_ROOT_GID=0
     55 * CONFIG_INITRAMFS_COMPRESSION_GZIP=y
     56 * CONFIG_INITRAMFS_COMPRESSION=".gz"
     57
     58For a more complete root filesystem take a look at [wiki:buildroot buildroot]