Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

documentation: Documentation/initrd.txt

Final clearification of the pivot_root mechanism, which brings this
document really up-to-date.

Signed-off-by: Domenico Andreoli <cavok@dandreoli.com>
Cc: "Randy.Dunlap" <rdunlap@xenotime.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Domenico Andreoli and committed by
Linus Torvalds
9d9a2000 a778b73f

+46 -28
+46 -28
Documentation/initrd.txt
··· 27 27 1) the boot loader loads the kernel and the initial RAM disk 28 28 2) the kernel converts initrd into a "normal" RAM disk and 29 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 30 + 3) if the root device is not /dev/ram0, the old (deprecated) 31 + change_root procedure is followed. see the "Obsolete root change 32 + mechanism" section below. 33 + 4) root device is mounted. if it is /dev/ram0, the initrd image is 34 + then mounted as root 35 + 5) /sbin/init is executed (this can be any valid executable, including 32 36 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 37 + init can do). 38 + 6) init mounts the "real" root file system 39 + 7) init places the root file system at the root directory using the 36 40 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 41 + 8) init execs the /sbin/init on the new root filesystem, performing 42 + the usual boot sequence 43 + 9) the initrd file system is removed 40 44 41 45 Note that changing the root directory does not involve unmounting it. 42 46 It is therefore possible to leave processes running on initrd during that ··· 74 70 root=/dev/ram0 75 71 76 72 initrd is mounted as root, and the normal boot procedure is followed, 77 - with the RAM disk still mounted as root. 73 + with the RAM disk mounted as root. 78 74 79 75 Compressed cpio images 80 76 ---------------------- ··· 141 137 # mkdir /mnt/dev 142 138 # mknod /mnt/dev/console c 5 1 143 139 5) copy all the files that are needed to properly use the initrd 144 - environment. Don't forget the most important file, /linuxrc 145 - Note that /linuxrc's permissions must include "x" (execute). 140 + environment. Don't forget the most important file, /sbin/init 141 + Note that /sbin/init's permissions must include "x" (execute). 146 142 6) correct operation the initrd environment can frequently be tested 147 143 even without rebooting with the command 148 - # chroot /mnt /linuxrc 144 + # chroot /mnt /sbin/init 149 145 This is of course limited to initrds that do not interfere with the 150 146 general system state (e.g. by reconfiguring network interfaces, 151 147 overwriting mounted devices, trying to start already running demons, ··· 158 154 # gzip -9 initrd 159 155 160 156 For experimenting with initrd, you may want to take a rescue floppy and 161 - only add a symbolic link from /linuxrc to /bin/sh. Alternatively, you 157 + only add a symbolic link from /sbin/init to /bin/sh. Alternatively, you 162 158 can try the experimental newlib environment [2] to create a small 163 159 initrd. 164 160 ··· 167 163 with an older mechanism, the following boot command line parameters 168 164 have to be given: 169 165 170 - root=/dev/ram0 init=/linuxrc rw 166 + root=/dev/ram0 rw 171 167 172 168 (rw is only necessary if writing to the initrd file system.) 173 169 174 170 With LOADLIN, you simply execute 175 171 176 172 LOADLIN <kernel> initrd=<disk_image> 177 - e.g. LOADLIN C:\LINUX\BZIMAGE initrd=C:\LINUX\INITRD.GZ root=/dev/ram0 178 - init=/linuxrc rw 173 + e.g. LOADLIN C:\LINUX\BZIMAGE initrd=C:\LINUX\INITRD.GZ root=/dev/ram0 rw 179 174 180 175 With LILO, you add the option INITRD=<path> to either the global section 181 176 or to the section of the respective kernel in /etc/lilo.conf, and pass ··· 182 179 183 180 image = /bzImage 184 181 initrd = /boot/initrd.gz 185 - append = "root=/dev/ram0 init=/linuxrc rw" 182 + append = "root=/dev/ram0 rw" 186 183 187 184 and run /sbin/lilo 188 185 ··· 194 191 Changing the root device 195 192 ------------------------ 196 193 197 - When finished with its duties, linuxrc typically changes the root device 194 + When finished with its duties, init typically changes the root device 198 195 and proceeds with starting the Linux system on the "real" root device. 199 196 200 197 The procedure involves the following steps: ··· 220 217 # mkdir initrd 221 218 # pivot_root . initrd 222 219 223 - Now, the linuxrc process may still access the old root via its 220 + Now, the init process may still access the old root via its 224 221 executable, shared libraries, standard input/output/error, and its 225 222 current root directory. All these references are dropped by the 226 223 following command: ··· 252 249 It is also possible to use initrd with an NFS-mounted root, see the 253 250 pivot_root(8) man page for details. 254 251 255 - Note: if linuxrc or any program exec'ed from it terminates for some 256 - reason, the old change_root mechanism is invoked (see section "Obsolete 257 - root change mechanism"). 258 - 259 252 260 253 Usage scenarios 261 254 --------------- ··· 263 264 1) system boots from floppy or other media with a minimal kernel 264 265 (e.g. support for RAM disks, initrd, a.out, and the Ext2 FS) and 265 266 loads initrd 266 - 2) /linuxrc determines what is needed to (1) mount the "real" root FS 267 + 2) /sbin/init determines what is needed to (1) mount the "real" root FS 267 268 (i.e. device type, device drivers, file system) and (2) the 268 269 distribution media (e.g. CD-ROM, network, tape, ...). This can be 269 270 done by asking the user, by auto-probing, or by using a hybrid 270 271 approach. 271 - 3) /linuxrc loads the necessary kernel modules 272 - 4) /linuxrc creates and populates the root file system (this doesn't 272 + 3) /sbin/init loads the necessary kernel modules 273 + 4) /sbin/init creates and populates the root file system (this doesn't 273 274 have to be a very usable system yet) 274 - 5) /linuxrc invokes pivot_root to change the root file system and 275 + 5) /sbin/init invokes pivot_root to change the root file system and 275 276 execs - via chroot - a program that continues the installation 276 277 6) the boot loader is installed 277 278 7) the boot loader is configured to load an initrd with the set of ··· 290 291 such cases, it is desirable to generate only a small set of kernels 291 292 (ideally only one) and to keep the system-specific part of configuration 292 293 information as small as possible. In this case, a common initrd could be 293 - generated with all the necessary modules. Then, only /linuxrc or a file 294 + generated with all the necessary modules. Then, only /sbin/init or a file 294 295 read by it would have to be different. 295 296 296 297 A third scenario are more convenient recovery disks, because information ··· 334 335 335 336 This old, deprecated mechanism is commonly called "change_root", while 336 337 the new, supported mechanism is called "pivot_root". 338 + 339 + 340 + Mixed change_root and pivot_root mechanism 341 + ------------------------------------------ 342 + 343 + In case you did not want to use root=/dev/ram0 to trig the pivot_root mechanism, 344 + you may create both /linuxrc and /sbin/init in your initrd image. 345 + 346 + /linuxrc would contain only the following: 347 + 348 + #! /bin/sh 349 + mount -n -t proc proc /proc 350 + echo 0x0100 >/proc/sys/kernel/real-root-dev 351 + umount -n /proc 352 + 353 + Once linuxrc exited, the kernel would mount again your initrd as root, 354 + this time executing /sbin/init. Again, it would be duty of this init 355 + to build the right environment (maybe using the root= device passed on 356 + the cmdline) before the final execution of the real /sbin/init. 337 357 338 358 339 359 Resources