1 .. SPDX-License-Identifier: GPL-2.0 2 3 =========================== 4 Ramfs, rootfs and initramfs 5 =========================== 6 7 October 17, 2005 8 9 :Author: Rob Landley <rob@landley.net> 10 11 What is ramfs? 12 -------------- 13 14 Ramfs is a very simple filesystem that exports 15 mechanisms (the page cache and dentry cache) a 16 RAM-based filesystem. 17 18 Normally all files are cached in memory by Lin 19 backing store (usually the block device the fi 20 around in case it's needed again, but marked a 21 Virtual Memory system needs the memory for som 22 written to files is marked clean as soon as it 23 store, but kept around for caching purposes un 24 memory. A similar mechanism (the dentry cache 25 directories. 26 27 With ramfs, there is no backing store. Files 28 dentries and page cache as usual, but there's 29 This means the pages are never marked clean, s 30 VM when it's looking to recycle memory. 31 32 The amount of code required to implement ramfs 33 work is done by the existing Linux caching inf 34 you're mounting the disk cache as a filesystem 35 an optional component removable via menuconfig 36 space savings. 37 38 ramfs and ramdisk: 39 ------------------ 40 41 The older "ram disk" mechanism created a synth 42 an area of RAM and used it as backing store fo 43 device was of fixed size, so the filesystem mo 44 size. Using a ram disk also required unnecess 45 fake block device into the page cache (and cop 46 as creating and destroying dentries. Plus it 47 (such as ext2) to format and interpret this da 48 49 Compared to ramfs, this wastes memory (and mem 50 unnecessary work for the CPU, and pollutes the 51 to avoid this copying by playing with the page 52 complicated and turn out to be about as expens 53 More to the point, all the work ramfs is doing 54 since all file access goes through the page an 55 disk is simply unnecessary; ramfs is internall 56 57 Another reason ramdisks are semi-obsolete is t 58 loopback devices offered a more flexible and c 59 synthetic block devices, now from files instea 60 See losetup (8) for details. 61 62 ramfs and tmpfs: 63 ---------------- 64 65 One downside of ramfs is you can keep writing 66 up all memory, and the VM can't free it becaus 67 should get written to backing store (rather th 68 got any backing store. Because of this, only 69 be allowed write access to a ramfs mount. 70 71 A ramfs derivative called tmpfs was created to 72 to write the data to swap space. Normal users 73 tmpfs mounts. See Documentation/filesystems/t 74 75 What is rootfs? 76 --------------- 77 78 Rootfs is a special instance of ramfs (or tmpf 79 always present in 2.6 systems. You can't unmo 80 same reason you can't kill the init process; r 81 to check for and handle an empty list, it's sm 82 to just make sure certain lists can't become e 83 84 Most systems just mount another filesystem ove 85 amount of space an empty instance of ramfs tak 86 87 If CONFIG_TMPFS is enabled, rootfs will use tm 88 default. To force ramfs, add "rootfstype=ramf 89 line. 90 91 What is initramfs? 92 ------------------ 93 94 All 2.6 Linux kernels contain a gzipped "cpio" 95 extracted into rootfs when the kernel boots up 96 checks to see if rootfs contains a file "init" 97 1. If found, this init process is responsible 98 rest of the way up, including locating and mou 99 any). If rootfs does not contain an init prog 100 archive is extracted into it, the kernel will 101 to locate and mount a root partition, then exe 102 out of that. 103 104 All this differs from the old initrd in severa 105 106 - The old initrd was always a separate file, 107 linked into the linux kernel image. (The 108 devoted to generating this archive during 109 110 - The old initrd file was a gzipped filesyst 111 such as ext2, that needed a driver built i 112 initramfs archive is a gzipped cpio archiv 113 see cpio(1) and Documentation/driver-api/e 114 The kernel's cpio extraction code is not o 115 __init text and data that can be discarded 116 117 - The program run by the old initrd (which w 118 some setup and then returned to the kernel 119 initramfs is not expected to return to the 120 off control it can overmount / with a new 121 program. See the switch_root utility, bel 122 123 - When switching another root device, initrd 124 umount the ramdisk. But initramfs is root 125 rootfs, nor unmount it. Instead delete ev 126 free up the space (find -xdev / -exec rm ' 127 with the new root (cd /newmount; mount --m 128 stdin/stdout/stderr to the new /dev/consol 129 130 Since this is a remarkably persnickety pro 131 commands before you can run them), the kli 132 program (utils/run_init.c) to do all this 133 (such as busybox) have named this command 134 135 Populating initramfs: 136 --------------------- 137 138 The 2.6 kernel build process always creates a 139 archive and links it into the resulting kernel 140 archive is empty (consuming 134 bytes on x86). 141 142 The config option CONFIG_INITRAMFS_SOURCE (in 143 and living in usr/Kconfig) can be used to spec 144 initramfs archive, which will automatically be 145 resulting binary. This option can point to an 146 archive, a directory containing files to be ar 147 specification such as the following example:: 148 149 dir /dev 755 0 0 150 nod /dev/console 644 0 0 c 5 1 151 nod /dev/loop0 644 0 0 b 7 0 152 dir /bin 755 1000 1000 153 slink /bin/sh busybox 777 0 0 154 file /bin/busybox initramfs/busybox 755 0 0 155 dir /proc 755 0 0 156 dir /sys 755 0 0 157 dir /mnt 755 0 0 158 file /init initramfs/init.sh 755 0 0 159 160 Run "usr/gen_init_cpio" (after the kernel buil 161 documenting the above file format. 162 163 One advantage of the configuration file is tha 164 set permissions or create device nodes in the 165 two example "file" entries expect to find file 166 a directory called "initramfs", under the linu 167 Documentation/driver-api/early-userspace/early 168 169 The kernel does not depend on external cpio to 170 directory instead of a configuration file, the 171 creates a configuration file from that directo 172 usr/gen_initramfs.sh), and proceeds to package 173 using the config file (by feeding it to usr/ge 174 from usr/gen_init_cpio.c). The kernel's build 175 entirely self-contained, and the kernel's boot 176 (obviously) self-contained. 177 178 The one thing you might need external cpio uti 179 or extracting your own preprepared cpio files 180 (instead of a config file or directory). 181 182 The following command line can extract a cpio 183 or by the kernel build) back into its componen 184 185 cpio -i -d -H newc -F initramfs_data.cpio -- 186 187 The following shell script can create a prebui 188 use in place of the above config file:: 189 190 #!/bin/sh 191 192 # Copyright 2006 Rob Landley <rob@landley.net 193 # Licensed under GPL version 2 194 195 if [ $# -ne 2 ] 196 then 197 echo "usage: mkinitramfs directory imagena 198 exit 1 199 fi 200 201 if [ -d "$1" ] 202 then 203 echo "creating $2 from $1" 204 (cd "$1"; find . | cpio -o -H newc | gzip) 205 else 206 echo "First argument must be a directory" 207 exit 1 208 fi 209 210 .. Note:: 211 212 The cpio man page contains some bad advice 213 archive if you follow it. It says "A typic 214 of filenames is with the find command; you 215 option to minimize problems with permission 216 unwritable or not searchable." Don't do th 217 initramfs.cpio.gz images, it won't work. T 218 won't create files in a directory that does 219 entries must go before the files that go in 220 The above script gets them in the right ord 221 222 External initramfs images: 223 -------------------------- 224 225 If the kernel has initrd support enabled, an e 226 be passed into a 2.6 kernel in place of an ini 227 will autodetect the type (initramfs, not initr 228 archive into rootfs before trying to run /init 229 230 This has the memory efficiency advantages of i 231 device) but the separate packaging of initrd ( 232 non-GPL code you'd like to run from initramfs, 233 the GPL licensed Linux kernel binary). 234 235 It can also be used to supplement the kernel's 236 files in the external archive will overwrite a 237 the built-in initramfs archive. Some distribu 238 a single kernel image with task-specific initr 239 240 Contents of initramfs: 241 ---------------------- 242 243 An initramfs archive is a complete self-contai 244 If you don't already understand what shared li 245 you need to get a minimal root filesystem up a 246 references: 247 248 - https://www.tldp.org/HOWTO/Bootdisk-HOWTO/ 249 - https://www.tldp.org/HOWTO/From-PowerUp-To-B 250 - http://www.linuxfromscratch.org/lfs/view/sta 251 252 The "klibc" package (https://www.kernel.org/pu 253 designed to be a tiny C library to statically 254 code against, along with some related utilitie 255 256 I use uClibc (https://www.uclibc.org) and busy 257 myself. These are LGPL and GPL, respectively. 258 package is planned for the busybox 1.3 release 259 260 In theory you could use glibc, but that's not 261 uses like this. (A "hello world" program stat 262 over 400k. With uClibc it's 7k. Also note th 263 name lookups, even when otherwise statically l 264 265 A good first step is to get initramfs to run a 266 program as init, and test it under an emulator 267 User Mode Linux, like so:: 268 269 cat > hello.c << EOF 270 #include <stdio.h> 271 #include <unistd.h> 272 273 int main(int argc, char *argv[]) 274 { 275 printf("Hello world!\n"); 276 sleep(999999999); 277 } 278 EOF 279 gcc -static hello.c -o init 280 echo init | cpio -o -H newc | gzip > test.cp 281 # Testing external initramfs using the initr 282 qemu -kernel /boot/vmlinuz -initrd test.cpio 283 284 When debugging a normal root filesystem, it's 285 "init=/bin/sh". The initramfs equivalent is " 286 just as useful. 287 288 Why cpio rather than tar? 289 ------------------------- 290 291 This decision was made back in December, 2001. 292 293 http://www.uwsg.iu.edu/hypermail/linux/kerne 294 295 And spawned a second thread (specifically on t 296 297 http://www.uwsg.iu.edu/hypermail/linux/kerne 298 299 The quick and dirty summary version (which is 300 the above threads) is: 301 302 1) cpio is a standard. It's decades old (from 303 widely used on Linux (inside RPM, Red Hat's 304 a Linux Journal article about it from 1996: 305 306 http://www.linuxjournal.com/article/1213 307 308 It's not as popular as tar because the trad 309 require _truly_hideous_ command line argume 310 either way about the archive format, and th 311 such as: 312 313 http://freecode.com/projects/afio 314 315 2) The cpio archive format chosen by the kerne 316 thus easier to create and parse) than any o 317 various tar archive formats. The complete 318 explained in buffer-format.txt, created in 319 extracted in init/initramfs.c. All three t 320 total of human-readable text. 321 322 3) The GNU project standardizing on tar is app 323 Windows standardizing on zip. Linux is not 324 to make its own technical decisions. 325 326 4) Since this is a kernel internal format, it 327 something brand new. The kernel provides i 328 extract this format anyway. Using an exist 329 but not essential. 330 331 5) Al Viro made the decision (quote: "tar is u 332 supported on the kernel side"): 333 334 http://www.uwsg.iu.edu/hypermail/linux/k 335 336 explained his reasoning: 337 338 - http://www.uwsg.iu.edu/hypermail/linux/ 339 - http://www.uwsg.iu.edu/hypermail/linux/ 340 341 and, most importantly, designed and impleme 342 343 Future directions: 344 ------------------ 345 346 Today (2.6.16), initramfs is always compiled i 347 kernel falls back to legacy boot code that is 348 not contain an /init program. The fallback is 349 smooth transition and allowing early boot func 350 "early userspace" (I.E. initramfs). 351 352 The move to early userspace is necessary becau 353 root device is complex. Root partitions can s 354 separate journal). They can be out on the net 355 specific MAC address, logging into a server, e 356 media, with dynamically allocated major/minor 357 issues requiring a full udev implementation to 358 compressed, encrypted, copy-on-write, loopback 359 and so on. 360 361 This kind of complexity (which inevitably incl 362 in userspace. Both klibc and busybox/uClibc a 363 packages to drop into a kernel build. 364 365 The klibc package has now been accepted into A 366 The kernel's current early boot code (partitio 367 be migrated into a default initramfs, automati 368 kernel build.
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.