Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | Using the initial RAM disk (initrd) |
| 2 | =================================== |
| 3 | |
| 4 | Written 1996,2000 by Werner Almesberger <werner.almesberger@epfl.ch> and |
| 5 | Hans Lermen <lermen@fgan.de> |
| 6 | |
| 7 | |
| 8 | initrd provides the capability to load a RAM disk by the boot loader. |
| 9 | This RAM disk can then be mounted as the root file system and programs |
| 10 | can be run from it. Afterwards, a new root file system can be mounted |
| 11 | from a different device. The previous root (from initrd) is then moved |
| 12 | to a directory and can be subsequently unmounted. |
| 13 | |
| 14 | initrd is mainly designed to allow system startup to occur in two phases, |
| 15 | where the kernel comes up with a minimum set of compiled-in drivers, and |
| 16 | where additional modules are loaded from initrd. |
| 17 | |
| 18 | This document gives a brief overview of the use of initrd. A more detailed |
| 19 | discussion of the boot process can be found in [1]. |
| 20 | |
| 21 | |
| 22 | Operation |
| 23 | --------- |
| 24 | |
| 25 | When using initrd, the system typically boots as follows: |
| 26 | |
| 27 | 1) the boot loader loads the kernel and the initial RAM disk |
| 28 | 2) the kernel converts initrd into a "normal" RAM disk and |
| 29 | frees the memory used by initrd |
| 30 | 3) initrd is mounted read-write as root |
| 31 | 4) /linuxrc is executed (this can be any valid executable, including |
| 32 | shell scripts; it is run with uid 0 and can do basically everything |
| 33 | init can do) |
| 34 | 5) linuxrc mounts the "real" root file system |
| 35 | 6) linuxrc places the root file system at the root directory using the |
| 36 | pivot_root system call |
| 37 | 7) the usual boot sequence (e.g. invocation of /sbin/init) is performed |
| 38 | on the root file system |
| 39 | 8) the initrd file system is removed |
| 40 | |
| 41 | Note that changing the root directory does not involve unmounting it. |
| 42 | It is therefore possible to leave processes running on initrd during that |
| 43 | procedure. Also note that file systems mounted under initrd continue to |
| 44 | be accessible. |
| 45 | |
| 46 | |
| 47 | Boot command-line options |
| 48 | ------------------------- |
| 49 | |
| 50 | initrd adds the following new options: |
| 51 | |
| 52 | initrd=<path> (e.g. LOADLIN) |
| 53 | |
| 54 | Loads the specified file as the initial RAM disk. When using LILO, you |
| 55 | have to specify the RAM disk image file in /etc/lilo.conf, using the |
| 56 | INITRD configuration variable. |
| 57 | |
| 58 | noinitrd |
| 59 | |
| 60 | initrd data is preserved but it is not converted to a RAM disk and |
| 61 | the "normal" root file system is mounted. initrd data can be read |
| 62 | from /dev/initrd. Note that the data in initrd can have any structure |
| 63 | in this case and doesn't necessarily have to be a file system image. |
| 64 | This option is used mainly for debugging. |
| 65 | |
| 66 | Note: /dev/initrd is read-only and it can only be used once. As soon |
| 67 | as the last process has closed it, all data is freed and /dev/initrd |
| 68 | can't be opened anymore. |
| 69 | |
Greg Kroah-Hartman | 890fbae | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 70 | root=/dev/ram0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | initrd is mounted as root, and the normal boot procedure is followed, |
| 73 | with the RAM disk still mounted as root. |
| 74 | |
Thomas Horsley | 0a5eca6 | 2006-07-30 03:04:12 -0700 | [diff] [blame^] | 75 | Compressed cpio images |
| 76 | ---------------------- |
| 77 | |
| 78 | Recent kernels have support for populating a ramdisk from a compressed cpio |
| 79 | archive, on such systems, the creation of a ramdisk image doesn't need to |
| 80 | involve special block devices or loopbacks, you merely create a directory on |
| 81 | disk with the desired initrd content, cd to that directory, and run (as an |
| 82 | example): |
| 83 | |
| 84 | find . | cpio --quiet -c -o | gzip -9 -n > /boot/imagefile.img |
| 85 | |
| 86 | Examining the contents of an existing image file is just as simple: |
| 87 | |
| 88 | mkdir /tmp/imagefile |
| 89 | cd /tmp/imagefile |
| 90 | gzip -cd /boot/imagefile.img | cpio -imd --quiet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | Installation |
| 93 | ------------ |
| 94 | |
| 95 | First, a directory for the initrd file system has to be created on the |
| 96 | "normal" root file system, e.g. |
| 97 | |
| 98 | # mkdir /initrd |
| 99 | |
| 100 | The name is not relevant. More details can be found on the pivot_root(2) |
| 101 | man page. |
| 102 | |
| 103 | If the root file system is created during the boot procedure (i.e. if |
| 104 | you're building an install floppy), the root file system creation |
| 105 | procedure should create the /initrd directory. |
| 106 | |
| 107 | If initrd will not be mounted in some cases, its content is still |
Greg Kroah-Hartman | 890fbae | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 108 | accessible if the following device has been created: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | # mknod /dev/initrd b 1 250 |
| 111 | # chmod 400 /dev/initrd |
| 112 | |
| 113 | Second, the kernel has to be compiled with RAM disk support and with |
| 114 | support for the initial RAM disk enabled. Also, at least all components |
| 115 | needed to execute programs from initrd (e.g. executable format and file |
| 116 | system) must be compiled into the kernel. |
| 117 | |
| 118 | Third, you have to create the RAM disk image. This is done by creating a |
| 119 | file system on a block device, copying files to it as needed, and then |
| 120 | copying the content of the block device to the initrd file. With recent |
| 121 | kernels, at least three types of devices are suitable for that: |
| 122 | |
| 123 | - a floppy disk (works everywhere but it's painfully slow) |
| 124 | - a RAM disk (fast, but allocates physical memory) |
| 125 | - a loopback device (the most elegant solution) |
| 126 | |
| 127 | We'll describe the loopback device method: |
| 128 | |
| 129 | 1) make sure loopback block devices are configured into the kernel |
| 130 | 2) create an empty file system of the appropriate size, e.g. |
| 131 | # dd if=/dev/zero of=initrd bs=300k count=1 |
| 132 | # mke2fs -F -m0 initrd |
| 133 | (if space is critical, you may want to use the Minix FS instead of Ext2) |
| 134 | 3) mount the file system, e.g. |
| 135 | # mount -t ext2 -o loop initrd /mnt |
Greg Kroah-Hartman | 890fbae | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 136 | 4) create the console device: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | # mkdir /mnt/dev |
| 138 | # mknod /mnt/dev/console c 5 1 |
| 139 | 5) copy all the files that are needed to properly use the initrd |
| 140 | environment. Don't forget the most important file, /linuxrc |
| 141 | Note that /linuxrc's permissions must include "x" (execute). |
| 142 | 6) correct operation the initrd environment can frequently be tested |
| 143 | even without rebooting with the command |
| 144 | # chroot /mnt /linuxrc |
| 145 | This is of course limited to initrds that do not interfere with the |
| 146 | general system state (e.g. by reconfiguring network interfaces, |
| 147 | overwriting mounted devices, trying to start already running demons, |
| 148 | etc. Note however that it is usually possible to use pivot_root in |
| 149 | such a chroot'ed initrd environment.) |
| 150 | 7) unmount the file system |
| 151 | # umount /mnt |
| 152 | 8) the initrd is now in the file "initrd". Optionally, it can now be |
| 153 | compressed |
| 154 | # gzip -9 initrd |
| 155 | |
| 156 | For experimenting with initrd, you may want to take a rescue floppy and |
| 157 | only add a symbolic link from /linuxrc to /bin/sh. Alternatively, you |
| 158 | can try the experimental newlib environment [2] to create a small |
| 159 | initrd. |
| 160 | |
| 161 | Finally, you have to boot the kernel and load initrd. Almost all Linux |
| 162 | boot loaders support initrd. Since the boot process is still compatible |
| 163 | with an older mechanism, the following boot command line parameters |
| 164 | have to be given: |
| 165 | |
| 166 | root=/dev/ram0 init=/linuxrc rw |
| 167 | |
Greg Kroah-Hartman | 890fbae | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 168 | (rw is only necessary if writing to the initrd file system.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | With LOADLIN, you simply execute |
| 171 | |
| 172 | LOADLIN <kernel> initrd=<disk_image> |
| 173 | e.g. LOADLIN C:\LINUX\BZIMAGE initrd=C:\LINUX\INITRD.GZ root=/dev/ram0 |
| 174 | init=/linuxrc rw |
| 175 | |
| 176 | With LILO, you add the option INITRD=<path> to either the global section |
| 177 | or to the section of the respective kernel in /etc/lilo.conf, and pass |
| 178 | the options using APPEND, e.g. |
| 179 | |
| 180 | image = /bzImage |
| 181 | initrd = /boot/initrd.gz |
| 182 | append = "root=/dev/ram0 init=/linuxrc rw" |
| 183 | |
| 184 | and run /sbin/lilo |
| 185 | |
| 186 | For other boot loaders, please refer to the respective documentation. |
| 187 | |
| 188 | Now you can boot and enjoy using initrd. |
| 189 | |
| 190 | |
| 191 | Changing the root device |
| 192 | ------------------------ |
| 193 | |
| 194 | When finished with its duties, linuxrc typically changes the root device |
| 195 | and proceeds with starting the Linux system on the "real" root device. |
| 196 | |
| 197 | The procedure involves the following steps: |
| 198 | - mounting the new root file system |
| 199 | - turning it into the root file system |
| 200 | - removing all accesses to the old (initrd) root file system |
| 201 | - unmounting the initrd file system and de-allocating the RAM disk |
| 202 | |
| 203 | Mounting the new root file system is easy: it just needs to be mounted on |
| 204 | a directory under the current root. Example: |
| 205 | |
| 206 | # mkdir /new-root |
| 207 | # mount -o ro /dev/hda1 /new-root |
| 208 | |
| 209 | The root change is accomplished with the pivot_root system call, which |
| 210 | is also available via the pivot_root utility (see pivot_root(8) man |
| 211 | page; pivot_root is distributed with util-linux version 2.10h or higher |
| 212 | [3]). pivot_root moves the current root to a directory under the new |
| 213 | root, and puts the new root at its place. The directory for the old root |
| 214 | must exist before calling pivot_root. Example: |
| 215 | |
| 216 | # cd /new-root |
| 217 | # mkdir initrd |
| 218 | # pivot_root . initrd |
| 219 | |
| 220 | Now, the linuxrc process may still access the old root via its |
| 221 | executable, shared libraries, standard input/output/error, and its |
| 222 | current root directory. All these references are dropped by the |
| 223 | following command: |
| 224 | |
| 225 | # exec chroot . what-follows <dev/console >dev/console 2>&1 |
| 226 | |
| 227 | Where what-follows is a program under the new root, e.g. /sbin/init |
Greg Kroah-Hartman | 890fbae | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 228 | If the new root file system will be used with udev and has no valid |
| 229 | /dev directory, udev must be initialized before invoking chroot in order |
| 230 | to provide /dev/console. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | Note: implementation details of pivot_root may change with time. In order |
| 233 | to ensure compatibility, the following points should be observed: |
| 234 | |
| 235 | - before calling pivot_root, the current directory of the invoking |
| 236 | process should point to the new root directory |
| 237 | - use . as the first argument, and the _relative_ path of the directory |
| 238 | for the old root as the second argument |
| 239 | - a chroot program must be available under the old and the new root |
| 240 | - chroot to the new root afterwards |
| 241 | - use relative paths for dev/console in the exec command |
| 242 | |
| 243 | Now, the initrd can be unmounted and the memory allocated by the RAM |
| 244 | disk can be freed: |
| 245 | |
| 246 | # umount /initrd |
Greg Kroah-Hartman | 890fbae | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 247 | # blockdev --flushbufs /dev/ram0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | It is also possible to use initrd with an NFS-mounted root, see the |
| 250 | pivot_root(8) man page for details. |
| 251 | |
| 252 | Note: if linuxrc or any program exec'ed from it terminates for some |
| 253 | reason, the old change_root mechanism is invoked (see section "Obsolete |
| 254 | root change mechanism"). |
| 255 | |
| 256 | |
| 257 | Usage scenarios |
| 258 | --------------- |
| 259 | |
| 260 | The main motivation for implementing initrd was to allow for modular |
| 261 | kernel configuration at system installation. The procedure would work |
| 262 | as follows: |
| 263 | |
| 264 | 1) system boots from floppy or other media with a minimal kernel |
| 265 | (e.g. support for RAM disks, initrd, a.out, and the Ext2 FS) and |
| 266 | loads initrd |
| 267 | 2) /linuxrc determines what is needed to (1) mount the "real" root FS |
| 268 | (i.e. device type, device drivers, file system) and (2) the |
| 269 | distribution media (e.g. CD-ROM, network, tape, ...). This can be |
| 270 | done by asking the user, by auto-probing, or by using a hybrid |
| 271 | approach. |
| 272 | 3) /linuxrc loads the necessary kernel modules |
| 273 | 4) /linuxrc creates and populates the root file system (this doesn't |
| 274 | have to be a very usable system yet) |
| 275 | 5) /linuxrc invokes pivot_root to change the root file system and |
| 276 | execs - via chroot - a program that continues the installation |
| 277 | 6) the boot loader is installed |
| 278 | 7) the boot loader is configured to load an initrd with the set of |
| 279 | modules that was used to bring up the system (e.g. /initrd can be |
| 280 | modified, then unmounted, and finally, the image is written from |
| 281 | /dev/ram0 or /dev/rd/0 to a file) |
| 282 | 8) now the system is bootable and additional installation tasks can be |
| 283 | performed |
| 284 | |
| 285 | The key role of initrd here is to re-use the configuration data during |
| 286 | normal system operation without requiring the use of a bloated "generic" |
| 287 | kernel or re-compiling or re-linking the kernel. |
| 288 | |
| 289 | A second scenario is for installations where Linux runs on systems with |
| 290 | different hardware configurations in a single administrative domain. In |
| 291 | such cases, it is desirable to generate only a small set of kernels |
| 292 | (ideally only one) and to keep the system-specific part of configuration |
| 293 | information as small as possible. In this case, a common initrd could be |
| 294 | generated with all the necessary modules. Then, only /linuxrc or a file |
| 295 | read by it would have to be different. |
| 296 | |
| 297 | A third scenario are more convenient recovery disks, because information |
| 298 | like the location of the root FS partition doesn't have to be provided at |
| 299 | boot time, but the system loaded from initrd can invoke a user-friendly |
| 300 | dialog and it can also perform some sanity checks (or even some form of |
| 301 | auto-detection). |
| 302 | |
| 303 | Last not least, CD-ROM distributors may use it for better installation |
| 304 | from CD, e.g. by using a boot floppy and bootstrapping a bigger RAM disk |
| 305 | via initrd from CD; or by booting via a loader like LOADLIN or directly |
| 306 | from the CD-ROM, and loading the RAM disk from CD without need of |
| 307 | floppies. |
| 308 | |
| 309 | |
| 310 | Obsolete root change mechanism |
| 311 | ------------------------------ |
| 312 | |
| 313 | The following mechanism was used before the introduction of pivot_root. |
| 314 | Current kernels still support it, but you should _not_ rely on its |
| 315 | continued availability. |
| 316 | |
| 317 | It works by mounting the "real" root device (i.e. the one set with rdev |
| 318 | in the kernel image or with root=... at the boot command line) as the |
| 319 | root file system when linuxrc exits. The initrd file system is then |
| 320 | unmounted, or, if it is still busy, moved to a directory /initrd, if |
| 321 | such a directory exists on the new root file system. |
| 322 | |
| 323 | In order to use this mechanism, you do not have to specify the boot |
| 324 | command options root, init, or rw. (If specified, they will affect |
| 325 | the real root file system, not the initrd environment.) |
| 326 | |
| 327 | If /proc is mounted, the "real" root device can be changed from within |
| 328 | linuxrc by writing the number of the new root FS device to the special |
| 329 | file /proc/sys/kernel/real-root-dev, e.g. |
| 330 | |
| 331 | # echo 0x301 >/proc/sys/kernel/real-root-dev |
| 332 | |
| 333 | Note that the mechanism is incompatible with NFS and similar file |
| 334 | systems. |
| 335 | |
| 336 | This old, deprecated mechanism is commonly called "change_root", while |
| 337 | the new, supported mechanism is called "pivot_root". |
| 338 | |
| 339 | |
| 340 | Resources |
| 341 | --------- |
| 342 | |
| 343 | [1] Almesberger, Werner; "Booting Linux: The History and the Future" |
| 344 | http://www.almesberger.net/cv/papers/ols2k-9.ps.gz |
| 345 | [2] newlib package (experimental), with initrd example |
| 346 | http://sources.redhat.com/newlib/ |
| 347 | [3] Brouwer, Andries; "util-linux: Miscellaneous utilities for Linux" |
| 348 | ftp://ftp.win.tue.nl/pub/linux-local/utils/util-linux/ |