Initrd: Difference between revisions

From OSDev Wiki
Jump to navigation Jump to search
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Filesystems}}
{{Filesystems}}


The term (削除) "Initrd" (削除ここまで)stands for "(削除) Initial RamDisk (削除ここまで)". Basically, it is simply a file loaded into memory which the OS uses as a filesystem containing stuff necessary for it to boot.
The term (追記) '''initrd''' (追記ここまで)stands for "(追記) initial ramdisk (追記ここまで)". Basically, it is simply a file loaded into memory which the OS uses as a filesystem containing stuff necessary for it to boot.


== Uses of an (削除) Initial Ramdisk (削除ここまで)==
== Uses of an (追記) initrd (追記ここまで)==
(削除) Initial Ramdisks (削除ここまで)are (削除) usually (削除ここまで)used by [[Modular Kernel]]s and [[Microkernel]]s, which commonly face a chicken-or-egg problem at boot time: the kernel needs to load its modules from disk, but these modules include the driver that normally allows the kernel to access that disk (although this applies to any other media). An (削除) Initrd (削除ここまで)is an elegant solution to this problem: the boot disk driver is stored within the initrd, which is loaded along with the kernel. Then, when the kernel boots, it retrieves the initrd in memory, loads the boot disk driver from it, and uses the freshly(削除) - (削除ここまで)loaded driver to load the rest of its modules. This is the solution used by Linux (not the early versions), as well as many hobby (削除) OS's (削除ここまで).
(追記) Usually, initial ramdisks (追記ここまで)are used by [[Modular Kernel]]s and [[Microkernel]]s, which commonly face a chicken-or-egg problem at boot time: the kernel needs to load its modules from disk, but these modules include the driver that normally allows the kernel to access that disk (although this applies to any other media). An (追記) initrd (追記ここまで)is an elegant solution to this problem: the boot disk driver is stored within the initrd, which is loaded along with the kernel. Then, when the kernel boots, it retrieves the initrd in memory, loads the boot disk driver from it, and uses the freshly loaded driver to load the rest of its modules. This is the solution used by Linux (not the early versions), as well as many hobby (追記) OSes (追記ここまで).


(追記) == Setting up an initrd == (追記ここまで)
(追記) An initrd is simply a file containing some sort of minimalistic filesystem (if it can be called so). Such a filesystem totally depends on the operating system and its version, but is usually some archive format (compressed or not) such as [[tar]], which has the advantage of being very simple to generate using the ''tar'' command, a simplistic filesystem such as [[SFS]], or just a custom format. The good news is that this custom filesystem can be very simple: in fact, it doesn't need the complications of usual filesystems, such as addition, modification or deletion of files, and the parsing logic can be very simple. (追記ここまで)


== (削除) Setting up an Initial Ramdisk (削除ここまで)==
==(追記) = Setup in a multiboot-compliant kernel = (追記ここまで)==
(削除) An initrd is simply (削除ここまで)a (削除) file containing some sort (削除ここまで)of (削除) minimalistic filesystem (if it can be called so). That filesystem totally depends on (削除ここまで)the (削除) Operating system and its version, but it (削除ここまで)is (削除) usually some archive format (削除ここまで)((削除) compressed or not) such as (削除ここまで)[[(削除) tar (削除ここまで)]](削除) , which has (削除ここまで)the (削除) advantage of being very simple (削除ここまで)to (削除) generate using (削除ここまで)the (削除) ''tar'' command, (削除ここまで)a (削除) simplistic filesystem such as (削除ここまで)[[(削除) SFS (削除ここまで)]], (削除) or just a custom format. The good news (削除ここまで)is (削除) that this custom filesystem can be very simple: (削除ここまで)in (削除) fact, it doesn't need the complications of usual filesystems, such as addition, modification or deletion of files (削除ここまで), and the (削除) parsing logic has to be very simple: you don't want your kernel to spend 5 minutes reading the initrd on each boot, do you ? (削除ここまで)[(削除) http://www.jamesmolloy.co.uk/tutorial_html/8.-The%20VFS%20and%20the%20initrd.html JamesM's tutorials (削除ここまで)] (削除) describe the development of a VFS and of an initrd (削除ここまで).
(追記) If you are using (追記ここまで)a (追記) [[Multiboot]]-compliant kernel, then most (追記ここまで)of the (追記) job (追記ここまで)is (追記) already performed by your bootloader (追記ここまで)((追記) e.g. (追記ここまで)[[(追記) GRUB (追記ここまで)]](追記) ). All you have to do is tell (追記ここまで)the (追記) bootloader (追記ここまで)to (追記) load (追記ここまで)the (追記) initrd along with your kernel as (追記ここまで)a [[(追記) Multiboot (追記ここまで)]] (追記) module (for GRUB (追記ここまで), (追記) this setting (追記ここまで)is (追記) found (追記ここまで)in (追記) <tt>grub.cfg</tt>) (追記ここまで), and (追記) use (追記ここまで)the [(追記) [Multiboot] (追記ここまで)] (追記) structure to find where it has been loaded (追記ここまで).


(削除) === Setup in a Multiboot-compliant kernel === (削除ここまで)
(追記) A (追記ここまで)[[GRUB]] (追記) script would look similar to this: (追記ここまで)
(削除) If you are using a [[Multiboot]]-compliant kernel, then most of the job is already performed by your bootloader (e.g. (削除ここまで)[[GRUB]](削除) ) (削除ここまで). (削除) All you have to do is tell GRUB to load it along with you kernel, as a [[Multiboot]] (削除ここまで)module (削除) (for GRUB, the setting is found in ''menu.lst'' for GRUB Legacy, or ''grub.cfg'' for GRUB2), and use the [[Multiboot]] structure to find where it has been loaded (削除ここまで).
(追記) <pre> (追記ここまで)
(追記) menuentry "myos" { (追記ここまで)
(追記) multiboot /boot/myos (追記ここまで).(追記) bin (追記ここまで)
(追記) (追記ここまで)module (追記) /boot/myos (追記ここまで).(追記) initrd (追記ここまで)
(追記) } (追記ここまで)
(追記) </pre> (追記ここまで)


=== Setup in custom-booted kernels ===
=== Setup in custom-booted kernels ===
Line 19: Line 26:


== Alternatives ==
== Alternatives ==
If you can't (or just don't want to) use an initrd, you can also use a reserved boot partition (which is much the same thing, except you'll need the disk driver before), or just build certain modules directly into the kernel, which might be a bad idea because changing filesystem or disk will require the kernel to be recompiled with different modules, unless you prefer to hard-build several disk drivers into (削除) the (削除ここまで)kernel, which will increase boot time.
If you can't (or just don't want to) use an initrd, you can also use a reserved boot partition (which is much the same thing, except you'll need the disk driver before), or just build certain modules directly into the kernel, which might be a bad idea because changing filesystem or disk will require the kernel to be recompiled with different modules, unless you prefer to hard-build several disk drivers into (追記) your (追記ここまで)kernel, which will increase boot time.


The article on [[Modular Kernel]]s shortly describes some other alternative solutions.
The article on [[Modular Kernel]]s shortly describes some other alternative solutions.


== See Also ==
== See Also ==
(追記) (追記ここまで)
(追記) === Articles === (追記ここまで)
* [[tar]] archive format
* [[tar]] archive format
* [[SFS]] simple filesystem implementation
* [[SFS]] (追記) — (追記ここまで)simple filesystem implementation
(追記) * [[BOOTBOOT]] loader that can load tar, cpio, [http://jamesmolloy.co.uk/tutorial_html/8.-The%20VFS%20and%20the%20initrd.html James Molloy]'s and SFS initrds (追記ここまで)


=== External Links ===
=== External Links ===
* [http://en.wikipedia.org/wiki/Cpio cpio] archive format
* [http://en.wikipedia.org/wiki/Cpio cpio] archive format
* [http://en.wikipedia.org/wiki/SquashFS SquashFS] filesystem designed for small disks
* [http://en.wikipedia.org/wiki/SquashFS SquashFS] (追記) — (追記ここまで)filesystem designed for small disks
* [http://en.wikipedia.org/wiki/Cramfs CramFS] filesystem designed for compressed ram disks
* [http://en.wikipedia.org/wiki/Cramfs CramFS] (追記) — (追記ここまで)filesystem designed for compressed ram disks


[[Category:Filesystems]]
[[Category:Filesystems]]
(追記) [[Category:Booting]] (追記ここまで)

Latest revision as of 18:43, 9 July 2023

Filesystems
Virtual Filesystems

VFS

Disk Filesystems
CD/DVD Filesystems
Network Filesystems
Flash Filesystems

The term initrd stands for "initial ramdisk". Basically, it is simply a file loaded into memory which the OS uses as a filesystem containing stuff necessary for it to boot.

Uses of an initrd

Usually, initial ramdisks are used by Modular Kernels and Microkernels, which commonly face a chicken-or-egg problem at boot time: the kernel needs to load its modules from disk, but these modules include the driver that normally allows the kernel to access that disk (although this applies to any other media). An initrd is an elegant solution to this problem: the boot disk driver is stored within the initrd, which is loaded along with the kernel. Then, when the kernel boots, it retrieves the initrd in memory, loads the boot disk driver from it, and uses the freshly loaded driver to load the rest of its modules. This is the solution used by Linux (not the early versions), as well as many hobby OSes.

Setting up an initrd

An initrd is simply a file containing some sort of minimalistic filesystem (if it can be called so). Such a filesystem totally depends on the operating system and its version, but is usually some archive format (compressed or not) such as tar, which has the advantage of being very simple to generate using the tar command, a simplistic filesystem such as SFS, or just a custom format. The good news is that this custom filesystem can be very simple: in fact, it doesn't need the complications of usual filesystems, such as addition, modification or deletion of files, and the parsing logic can be very simple.

Setup in a multiboot-compliant kernel

If you are using a Multiboot-compliant kernel, then most of the job is already performed by your bootloader (e.g. GRUB). All you have to do is tell the bootloader to load the initrd along with your kernel as a Multiboot module (for GRUB, this setting is found in grub.cfg), and use the Multiboot structure to find where it has been loaded.

A GRUB script would look similar to this:

menuentry "myos" {
	multiboot /boot/myos.bin
	module /boot/myos.initrd
}

Setup in custom-booted kernels

If you are using a custom bootloader, then you will have to load the initrd yourself. You can either load it at a fixed address and hardcode it into the kernel, or determine that address dynamically, and pass it to the kernel.

As an alternative, you can use the cat command to concatenate your kernel image and initrd image, and modify the linker script to setup a constant as the ending address of your kernel, which will match the loading address of your initrd, or just write a simple program to convert your initrd image into a C header containing a byte array containing its bytes, and include that into your kernel.

Alternatives

If you can't (or just don't want to) use an initrd, you can also use a reserved boot partition (which is much the same thing, except you'll need the disk driver before), or just build certain modules directly into the kernel, which might be a bad idea because changing filesystem or disk will require the kernel to be recompiled with different modules, unless you prefer to hard-build several disk drivers into your kernel, which will increase boot time.

The article on Modular Kernels shortly describes some other alternative solutions.

See Also

Articles

  • tar archive format
  • SFS — simple filesystem implementation
  • BOOTBOOT loader that can load tar, cpio, James Molloy's and SFS initrds

External Links

  • cpio archive format
  • SquashFS — filesystem designed for small disks
  • CramFS — filesystem designed for compressed ram disks
Retrieved from "https://wiki.osdev.org/index.php?title=Initrd&oldid=28032"