Os documentos do kernel explicam como empacotar uma imagem no próprio kernel. De kernel.org :
What is rootfs?
Rootfs
is a special instance of ramfs
(or tmpfs
, if that's enabled), which is
always present in 2.6 systems. You can't unmount rootfs
for approximately the
same reason you can't kill the init process; rather than having special code to check for and handle an empty list, it's smaller and simpler for the kernel
to just make sure certain lists can't become empty.
Most systems just mount another filesystem over rootfs
and ignore it. The amount of space an empty instance of ramfs takes up is tiny.
If CONFIG_TMPFS is enabled, rootfs
will use tmpfs
instead of ramfs
by default. To force ramfs
, add "rootfstype=ramfs"
to the kernel command line.
What is initramfs?
All 2.6 Linux kernels contain a gzipped "cpio"
format archive, which is extracted into rootfs
when the kernel boots up. After extracting, the kernel checks to see if rootfs
contains a file "init"
, and if so it executes it as PID 1. If found, this init
process is responsible for bringing the system the rest of the way up, including locating and mounting the real root device (if any). If rootfs
does not contain an init
program after the embedded cpio
archive is extracted into it, the kernel will fall through to the older code to locate and mount a root partition, then exec some variant of /sbin/init
out of that.
All this differs from the old initrd in several ways:
The old initrd was always a separate file, while the initramfs archive is linked into the linux kernel image. (The directory linux-*/usr is devoted to generating this archive during the build.)
The old initrd file was a gzipped filesystem image (in some file format, such as ext2, that needed a driver built into the kernel), while the new initramfs archive is a gzipped cpio archive (like tar only simpler, see cpio(1) and Documentation/early-userspace/buffer-format.txt). The kernel's cpio extraction code is not only extremely small, it's also __init text and data that can be discarded during the boot process.
The program run by the old initrd (which was called /initrd, not /init) did some setup and then returned to the kernel, while the init program from initramfs is not expected to return to the kernel. (If /init needs to hand off control it can overmount / with a new root device and exec another init program. See the switch_root utility, below.)
When switching another root device, initrd would pivot_root and then umount the ramdisk. But initramfs is rootfs: you can neither pivot_root rootfs, nor unmount it. Instead delete everything out of rootfs to free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs with the new root (cd /newmount; mount --move . /; chroot .), attach stdin/stdout/stderr to the new /dev/console, and exec the new init.
Since this is a remarkably persnickety process (and involves deleting commands before you can run them), the klibc package introduced a helper program (utils/run_init.c) to do all this for you. Most other packages (such as busybox) have named this command "switch_root".
Populating initramfs:
The 2.6 kernel build process always creates a gzipped cpio format initramfs archive and links it into the resulting kernel binary. By default, this archive is empty (consuming 134 bytes on x86).
The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig,
and living in usr/Kconfig
) can be used to specify a source for the
initramfs
archive, which will automatically be incorporated into the resulting binary. This option can point to an *existing gzipped cpio
*
archive, a directory containing files to be archived, or a text file specification such as the following example:
dir /dev 755 0 0
nod /dev/console 644 0 0 c 5 1
nod /dev/loop0 644 0 0 b 7 0
dir /bin 755 1000 1000
slink /bin/sh busybox 777 0 0
file /bin/busybox initramfs/busybox 755 0 0
dir /proc 755 0 0
dir /sys 755 0 0
dir /mnt 755 0 0
file /init initramfs/init.sh 755 0 0
Run "usr/gen_init_cpio
" (after the kernel build) to get a usage message documenting the above file format.
One advantage of the configuration file is that root
access is not required to set permissions or create device nodes in the new archive.
(Note that those two example "file" entries expect to find files named "init.sh
" and "busybox
" in a directory called "initramfs
", under the linux
-2.6.* directory. See Documentation/early-userspace/README for more details.)
The kernel does not depend on external cpio
tools. If you specify a directory instead of a configuration file, the kernel's build infrastructure
creates a configuration file from that directory (usr/Makefile
calls scripts/gen_initramfs_list.sh
), and proceeds to package up that directory
using the config file (by feeding it to usr/gen_init_cpio
, which is created
from usr/gen_init_cpio.c
). The kernel's build-time cpio
creation code is entirely self-contained, and the kernel's boot-time extractor is also
(obviously) self-contained.