| 1 | = initramfs = |
| 2 | The 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 | |
| 4 | Using 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 | |
| 6 | To 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 | |
| 11 | This 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 == |
| 14 | This 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 | |
| 16 | The benefit of using an external initramfs is that you do not have to rebuild the kernel to update the root filesystem. |
| 17 | |
| 18 | Many 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 == |
| 21 | An 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 | |
| 23 | Note 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 == |
| 26 | Here 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 |
| 28 | cat << EOF > hello.c |
| 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | int main(int argc, char *argv[]) |
| 33 | { |
| 34 | printf("Hello world!\n"); |
| 35 | sleep(999999999); |
| 36 | } |
| 37 | EOF |
| 38 | ${CROSS_COMPILE}gcc -static hello.c -o hello |
| 39 | cat << EOF > initramfs |
| 40 | dir /dev 755 0 0 |
| 41 | nod /dev/console 644 0 0 c 5 1 |
| 42 | nod /dev/loop0 644 0 0 b 7 0 |
| 43 | dir /bin 755 1000 1000 |
| 44 | dir /proc 755 0 0 |
| 45 | dir /sys 755 0 0 |
| 46 | dir /mnt 755 0 0 |
| 47 | file /init hello 755 0 0 |
| 48 | EOF |
| 49 | }}} |
| 50 | |
| 51 | kernel 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 | |
| 58 | For a more complete root filesystem take a look at [wiki:buildroot buildroot] |