blob: b176928e69631f5efe3cb37506b3eb375cc3e23a [file] [log] [blame]
Rob Landley7f46a242005-11-07 01:01:09 -08001ramfs, rootfs and initramfs
2October 17, 2005
3Rob Landley <rob@landley.net>
4=============================
5
6What is ramfs?
7--------------
8
9Ramfs is a very simple filesystem that exports Linux's disk caching
10mechanisms (the page cache and dentry cache) as a dynamically resizable
Randy Dunlap18107322007-10-16 23:29:29 -070011RAM-based filesystem.
Rob Landley7f46a242005-11-07 01:01:09 -080012
13Normally all files are cached in memory by Linux. Pages of data read from
14backing store (usually the block device the filesystem is mounted on) are kept
15around in case it's needed again, but marked as clean (freeable) in case the
16Virtual Memory system needs the memory for something else. Similarly, data
17written to files is marked clean as soon as it has been written to backing
18store, but kept around for caching purposes until the VM reallocates the
19memory. A similar mechanism (the dentry cache) greatly speeds up access to
20directories.
21
22With ramfs, there is no backing store. Files written into ramfs allocate
23dentries and page cache as usual, but there's nowhere to write them to.
24This means the pages are never marked clean, so they can't be freed by the
25VM when it's looking to recycle memory.
26
27The amount of code required to implement ramfs is tiny, because all the
28work is done by the existing Linux caching infrastructure. Basically,
29you're mounting the disk cache as a filesystem. Because of this, ramfs is not
30an optional component removable via menuconfig, since there would be negligible
31space savings.
32
33ramfs and ramdisk:
34------------------
35
36The older "ram disk" mechanism created a synthetic block device out of
Randy Dunlap18107322007-10-16 23:29:29 -070037an area of RAM and used it as backing store for a filesystem. This block
Rob Landley7f46a242005-11-07 01:01:09 -080038device was of fixed size, so the filesystem mounted on it was of fixed
39size. Using a ram disk also required unnecessarily copying memory from the
40fake block device into the page cache (and copying changes back out), as well
41as creating and destroying dentries. Plus it needed a filesystem driver
42(such as ext2) to format and interpret this data.
43
44Compared to ramfs, this wastes memory (and memory bus bandwidth), creates
45unnecessary work for the CPU, and pollutes the CPU caches. (There are tricks
46to avoid this copying by playing with the page tables, but they're unpleasantly
47complicated and turn out to be about as expensive as the copying anyway.)
48More to the point, all the work ramfs is doing has to happen _anyway_,
Randy Dunlap18107322007-10-16 23:29:29 -070049since all file access goes through the page and dentry caches. The RAM
50disk is simply unnecessary; ramfs is internally much simpler.
Rob Landley7f46a242005-11-07 01:01:09 -080051
52Another reason ramdisks are semi-obsolete is that the introduction of
53loopback devices offered a more flexible and convenient way to create
54synthetic block devices, now from files instead of from chunks of memory.
55See losetup (8) for details.
56
57ramfs and tmpfs:
58----------------
59
60One downside of ramfs is you can keep writing data into it until you fill
61up all memory, and the VM can't free it because the VM thinks that files
62should get written to backing store (rather than swap space), but ramfs hasn't
63got any backing store. Because of this, only root (or a trusted user) should
64be allowed write access to a ramfs mount.
65
66A ramfs derivative called tmpfs was created to add size limits, and the ability
67to write the data to swap space. Normal users can be allowed write access to
68tmpfs mounts. See Documentation/filesystems/tmpfs.txt for more information.
69
70What is rootfs?
71---------------
72
Rob Landleye7b69052006-06-25 05:49:00 -070073Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which is
74always present in 2.6 systems. You can't unmount rootfs for approximately the
75same reason you can't kill the init process; rather than having special code
76to check for and handle an empty list, it's smaller and simpler for the kernel
77to just make sure certain lists can't become empty.
Rob Landley7f46a242005-11-07 01:01:09 -080078
Rob Landleye7b69052006-06-25 05:49:00 -070079Most systems just mount another filesystem over rootfs and ignore it. The
Rob Landley7f46a242005-11-07 01:01:09 -080080amount of space an empty instance of ramfs takes up is tiny.
81
Rob Landley6e19ede2013-09-11 14:26:13 -070082If CONFIG_TMPFS is enabled, rootfs will use tmpfs instead of ramfs by
83default. To force ramfs, add "rootfstype=ramfs" to the kernel command
84line.
85
Rob Landley7f46a242005-11-07 01:01:09 -080086What is initramfs?
87------------------
88
89All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is
90extracted into rootfs when the kernel boots up. After extracting, the kernel
91checks to see if rootfs contains a file "init", and if so it executes it as PID
921. If found, this init process is responsible for bringing the system the
93rest of the way up, including locating and mounting the real root device (if
94any). If rootfs does not contain an init program after the embedded cpio
95archive is extracted into it, the kernel will fall through to the older code
96to locate and mount a root partition, then exec some variant of /sbin/init
97out of that.
98
99All this differs from the old initrd in several ways:
100
Rob Landleye7b69052006-06-25 05:49:00 -0700101 - The old initrd was always a separate file, while the initramfs archive is
102 linked into the linux kernel image. (The directory linux-*/usr is devoted
103 to generating this archive during the build.)
Rob Landley7f46a242005-11-07 01:01:09 -0800104
105 - The old initrd file was a gzipped filesystem image (in some file format,
Rob Landleye7b69052006-06-25 05:49:00 -0700106 such as ext2, that needed a driver built into the kernel), while the new
Rob Landley7f46a242005-11-07 01:01:09 -0800107 initramfs archive is a gzipped cpio archive (like tar only simpler,
Rob Landleye7b69052006-06-25 05:49:00 -0700108 see cpio(1) and Documentation/early-userspace/buffer-format.txt). The
109 kernel's cpio extraction code is not only extremely small, it's also
Randy Dunlap18107322007-10-16 23:29:29 -0700110 __init text and data that can be discarded during the boot process.
Rob Landley7f46a242005-11-07 01:01:09 -0800111
112 - The program run by the old initrd (which was called /initrd, not /init) did
113 some setup and then returned to the kernel, while the init program from
114 initramfs is not expected to return to the kernel. (If /init needs to hand
115 off control it can overmount / with a new root device and exec another init
116 program. See the switch_root utility, below.)
117
118 - When switching another root device, initrd would pivot_root and then
119 umount the ramdisk. But initramfs is rootfs: you can neither pivot_root
120 rootfs, nor unmount it. Instead delete everything out of rootfs to
121 free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs
122 with the new root (cd /newmount; mount --move . /; chroot .), attach
123 stdin/stdout/stderr to the new /dev/console, and exec the new init.
124
Denis Cheng33b13022008-02-03 14:50:11 +0200125 Since this is a remarkably persnickety process (and involves deleting
Rob Landley7f46a242005-11-07 01:01:09 -0800126 commands before you can run them), the klibc package introduced a helper
127 program (utils/run_init.c) to do all this for you. Most other packages
128 (such as busybox) have named this command "switch_root".
129
130Populating initramfs:
131---------------------
132
133The 2.6 kernel build process always creates a gzipped cpio format initramfs
134archive and links it into the resulting kernel binary. By default, this
Rob Landleye7b69052006-06-25 05:49:00 -0700135archive is empty (consuming 134 bytes on x86).
136
frans1838e392008-11-22 15:39:06 +0100137The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig,
138and living in usr/Kconfig) can be used to specify a source for the
139initramfs archive, which will automatically be incorporated into the
140resulting binary. This option can point to an existing gzipped cpio
141archive, a directory containing files to be archived, or a text file
142specification such as the following example:
Rob Landley7f46a242005-11-07 01:01:09 -0800143
144 dir /dev 755 0 0
145 nod /dev/console 644 0 0 c 5 1
146 nod /dev/loop0 644 0 0 b 7 0
147 dir /bin 755 1000 1000
148 slink /bin/sh busybox 777 0 0
149 file /bin/busybox initramfs/busybox 755 0 0
150 dir /proc 755 0 0
151 dir /sys 755 0 0
152 dir /mnt 755 0 0
153 file /init initramfs/init.sh 755 0 0
154
Rob Landley99aef422006-01-08 01:03:43 -0800155Run "usr/gen_init_cpio" (after the kernel build) to get a usage message
156documenting the above file format.
157
Rob Landleye7b69052006-06-25 05:49:00 -0700158One advantage of the configuration file is that root access is not required to
Rob Landley7f46a242005-11-07 01:01:09 -0800159set permissions or create device nodes in the new archive. (Note that those
160two example "file" entries expect to find files named "init.sh" and "busybox" in
161a directory called "initramfs", under the linux-2.6.* directory. See
162Documentation/early-userspace/README for more details.)
163
Rob Landleye7b69052006-06-25 05:49:00 -0700164The kernel does not depend on external cpio tools. If you specify a
165directory instead of a configuration file, the kernel's build infrastructure
166creates a configuration file from that directory (usr/Makefile calls
167scripts/gen_initramfs_list.sh), and proceeds to package up that directory
168using the config file (by feeding it to usr/gen_init_cpio, which is created
169from usr/gen_init_cpio.c). The kernel's build-time cpio creation code is
170entirely self-contained, and the kernel's boot-time extractor is also
171(obviously) self-contained.
172
173The one thing you might need external cpio utilities installed for is creating
174or extracting your own preprepared cpio files to feed to the kernel build
175(instead of a config file or directory).
176
177The following command line can extract a cpio image (either by the above script
178or by the kernel build) back into its component files:
Rob Landley99aef422006-01-08 01:03:43 -0800179
180 cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenames
181
Rob Landleye7b69052006-06-25 05:49:00 -0700182The following shell script can create a prebuilt cpio archive you can
183use in place of the above config file:
184
185 #!/bin/sh
186
187 # Copyright 2006 Rob Landley <rob@landley.net> and TimeSys Corporation.
188 # Licensed under GPL version 2
189
190 if [ $# -ne 2 ]
191 then
192 echo "usage: mkinitramfs directory imagename.cpio.gz"
193 exit 1
194 fi
195
196 if [ -d "$1" ]
197 then
198 echo "creating $2 from $1"
199 (cd "$1"; find . | cpio -o -H newc | gzip) > "$2"
200 else
201 echo "First argument must be a directory"
202 exit 1
203 fi
204
205Note: The cpio man page contains some bad advice that will break your initramfs
206archive if you follow it. It says "A typical way to generate the list
207of filenames is with the find command; you should give find the -depth option
208to minimize problems with permissions on directories that are unwritable or not
209searchable." Don't do this when creating initramfs.cpio.gz images, it won't
210work. The Linux kernel cpio extractor won't create files in a directory that
211doesn't exist, so the directory entries must go before the files that go in
212those directories. The above script gets them in the right order.
213
214External initramfs images:
215--------------------------
216
217If the kernel has initrd support enabled, an external cpio.gz archive can also
218be passed into a 2.6 kernel in place of an initrd. In this case, the kernel
219will autodetect the type (initramfs, not initrd) and extract the external cpio
220archive into rootfs before trying to run /init.
221
222This has the memory efficiency advantages of initramfs (no ramdisk block
223device) but the separate packaging of initrd (which is nice if you have
224non-GPL code you'd like to run from initramfs, without conflating it with
225the GPL licensed Linux kernel binary).
226
Randy Dunlap18107322007-10-16 23:29:29 -0700227It can also be used to supplement the kernel's built-in initramfs image. The
Rob Landleye7b69052006-06-25 05:49:00 -0700228files in the external archive will overwrite any conflicting files in
229the built-in initramfs archive. Some distributors also prefer to customize
230a single kernel image with task-specific initramfs images, without recompiling.
231
Rob Landley99aef422006-01-08 01:03:43 -0800232Contents of initramfs:
233----------------------
234
Rob Landleye7b69052006-06-25 05:49:00 -0700235An initramfs archive is a complete self-contained root filesystem for Linux.
Rob Landley7f46a242005-11-07 01:01:09 -0800236If you don't already understand what shared libraries, devices, and paths
237you need to get a minimal root filesystem up and running, here are some
238references:
239http://www.tldp.org/HOWTO/Bootdisk-HOWTO/
240http://www.tldp.org/HOWTO/From-PowerUp-To-Bash-Prompt-HOWTO.html
241http://www.linuxfromscratch.org/lfs/view/stable/
242
243The "klibc" package (http://www.kernel.org/pub/linux/libs/klibc) is
244designed to be a tiny C library to statically link early userspace
245code against, along with some related utilities. It is BSD licensed.
246
247I use uClibc (http://www.uclibc.org) and busybox (http://www.busybox.net)
Rob Landley99aef422006-01-08 01:03:43 -0800248myself. These are LGPL and GPL, respectively. (A self-contained initramfs
Rob Landleye7b69052006-06-25 05:49:00 -0700249package is planned for the busybox 1.3 release.)
Rob Landley7f46a242005-11-07 01:01:09 -0800250
251In theory you could use glibc, but that's not well suited for small embedded
252uses like this. (A "hello world" program statically linked against glibc is
253over 400k. With uClibc it's 7k. Also note that glibc dlopens libnss to do
254name lookups, even when otherwise statically linked.)
255
Rob Landleye7b69052006-06-25 05:49:00 -0700256A good first step is to get initramfs to run a statically linked "hello world"
257program as init, and test it under an emulator like qemu (www.qemu.org) or
258User Mode Linux, like so:
259
260 cat > hello.c << EOF
261 #include <stdio.h>
262 #include <unistd.h>
263
264 int main(int argc, char *argv[])
265 {
266 printf("Hello world!\n");
267 sleep(999999999);
268 }
269 EOF
fransdd1c53a2008-10-15 22:01:30 -0700270 gcc -static hello.c -o init
Rob Landleye7b69052006-06-25 05:49:00 -0700271 echo init | cpio -o -H newc | gzip > test.cpio.gz
272 # Testing external initramfs using the initrd loading mechanism.
273 qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zero
274
275When debugging a normal root filesystem, it's nice to be able to boot with
276"init=/bin/sh". The initramfs equivalent is "rdinit=/bin/sh", and it's
277just as useful.
278
Rob Landley99aef422006-01-08 01:03:43 -0800279Why cpio rather than tar?
280-------------------------
281
282This decision was made back in December, 2001. The discussion started here:
283
284 http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1538.html
285
286And spawned a second thread (specifically on tar vs cpio), starting here:
287
288 http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1587.html
289
290The quick and dirty summary version (which is no substitute for reading
291the above threads) is:
292
2931) cpio is a standard. It's decades old (from the AT&T days), and already
294 widely used on Linux (inside RPM, Red Hat's device driver disks). Here's
295 a Linux Journal article about it from 1996:
296
297 http://www.linuxjournal.com/article/1213
298
299 It's not as popular as tar because the traditional cpio command line tools
300 require _truly_hideous_ command line arguments. But that says nothing
301 either way about the archive format, and there are alternative tools,
302 such as:
303
Masanari Iida1f8ee462012-02-13 22:42:58 +0900304 http://freecode.com/projects/afio
Rob Landley99aef422006-01-08 01:03:43 -0800305
3062) The cpio archive format chosen by the kernel is simpler and cleaner (and
307 thus easier to create and parse) than any of the (literally dozens of)
308 various tar archive formats. The complete initramfs archive format is
309 explained in buffer-format.txt, created in usr/gen_init_cpio.c, and
310 extracted in init/initramfs.c. All three together come to less than 26k
311 total of human-readable text.
312
3133) The GNU project standardizing on tar is approximately as relevant as
314 Windows standardizing on zip. Linux is not part of either, and is free
315 to make its own technical decisions.
316
3174) Since this is a kernel internal format, it could easily have been
318 something brand new. The kernel provides its own tools to create and
319 extract this format anyway. Using an existing standard was preferable,
320 but not essential.
321
3225) Al Viro made the decision (quote: "tar is ugly as hell and not going to be
323 supported on the kernel side"):
324
325 http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1540.html
326
327 explained his reasoning:
328
329 http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1550.html
330 http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1638.html
331
332 and, most importantly, designed and implemented the initramfs code.
333
Rob Landley7f46a242005-11-07 01:01:09 -0800334Future directions:
335------------------
336
Rob Landleye7b69052006-06-25 05:49:00 -0700337Today (2.6.16), initramfs is always compiled in, but not always used. The
Rob Landley7f46a242005-11-07 01:01:09 -0800338kernel falls back to legacy boot code that is reached only if initramfs does
339not contain an /init program. The fallback is legacy code, there to ensure a
340smooth transition and allowing early boot functionality to gradually move to
341"early userspace" (I.E. initramfs).
342
343The move to early userspace is necessary because finding and mounting the real
344root device is complex. Root partitions can span multiple devices (raid or
345separate journal). They can be out on the network (requiring dhcp, setting a
Randy Dunlap18107322007-10-16 23:29:29 -0700346specific MAC address, logging into a server, etc). They can live on removable
Rob Landley7f46a242005-11-07 01:01:09 -0800347media, with dynamically allocated major/minor numbers and persistent naming
348issues requiring a full udev implementation to sort out. They can be
349compressed, encrypted, copy-on-write, loopback mounted, strangely partitioned,
350and so on.
351
352This kind of complexity (which inevitably includes policy) is rightly handled
353in userspace. Both klibc and busybox/uClibc are working on simple initramfs
Rob Landleye7b69052006-06-25 05:49:00 -0700354packages to drop into a kernel build.
Rob Landley7f46a242005-11-07 01:01:09 -0800355
Rob Landleye7b69052006-06-25 05:49:00 -0700356The klibc package has now been accepted into Andrew Morton's 2.6.17-mm tree.
357The kernel's current early boot code (partition detection, etc) will probably
358be migrated into a default initramfs, automatically created and used by the
359kernel build.