17

I read through this popular IBM doc (I see it referred quite often on the web) explaining the function of the initial RAM disk.

I hit a wall in conceptualizing how this works though.

In the doc it says

The boot loader, such as GRUB, identifies the kernel that is to be loaded and copies this kernel image and any associated initrd into memory

I'm already confused: Does it copy the entire kernel into memory or just part of it? If the entire kernel is in memory then why do we even need the initial RAM disk?

I thought the purpose of initrd was to be able to have a small generalized kernel image and initrd will install the correct modules in it before the kernel image is loaded. But if the entire kernel is already in memory why do we need initrd?

That also brings up another thing that confuses me - where are the modules that get loaded into the kernel located? Are all the kernel modules stored inside initrd?

muru
  • 69,900
  • 13
  • 192
  • 292
user1028270
  • 1,024
  • 1
  • 15
  • 31
  • Yes. The entire kernel. And its first rootfs. But linux kernels havent used initrd in many years. – mikeserv Jan 26 '16 at 18:22
  • Right I was reading that. Its been largely replaced by initramfs right? And its still a similar process with initramfs correct? – user1028270 Jan 26 '16 at 18:35
  • 2
    its been completely replaced for all kernels since the 2.6 series. But the [process is somewhat similar](https://kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt) except that initramfs is never unmounted - its always there and its always root, too. You have to mount your root dev over it, actually. There are also the benefits of not needing to emulate a separate block dev and similar. – mikeserv Jan 26 '16 at 18:38
  • Cool I'm going to read that article you linked to. So the kernel modules are store in the initramfs rootfs? – user1028270 Jan 26 '16 at 18:43
  • not all.. toby's answer is correct. – mikeserv Jan 26 '16 at 18:44
  • @mikeserv are drivers of devices which are not connected to my computer also loaded? – Alex Jones Jan 26 '16 at 18:52
  • @edwardtorvalds - that depends on whether or not you load them. typically, no, they are not, but theres nothing prohibiting it, so maybe. in the main, it doesnt happen on my computer – mikeserv Jan 26 '16 at 18:54
  • @mikeserv if drivers of device that are not detected during boot are not loaded then how will kernel recognise any new device connected? by loading those drivers instantly? – Alex Jones Jan 26 '16 at 18:56
  • 1
    @edwardtorvalds - thats all handled by [`udev`](https://wiki.archlinux.org/index.php/udev) usually, and yes, automatically. – mikeserv Jan 26 '16 at 18:57
  • "If the entire kernel is in memory then why do we even need the initial RAM disk?" - because the initial RAM disk contains things that are not part of the kernel. – user253751 Jan 26 '16 at 19:53

3 Answers3

21

The entire kernel is loaded into memory at boot, typically along with an initramfs nowadays. (It is still possible to set up a system to boot without an initramfs but that's unusual on desktops and servers.)

The initramfs's role is to provide the functionality needed to mount the "real" filesystems and continue booting the system. That involves kernel modules, and also various binaries: you need at least udev, perhaps some networking, and kmod which loads modules.

Modules can be loaded into the kernel later than just boot, so there's no special preparation of the kernel by the initramfs. They can be stored anywhere: the initramfs, /lib/modules on the real filesystem, in a development tree if you're developing a module... The initramfs only needs to contain the modules which are necessary to mount the root filesystem (which contains the rest).

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • OK that makes sense to me. I think I was conflating the kernel image and the file system it uses which obviously are totally separate. – user1028270 Jan 26 '16 at 18:45
6

The entire kernel (but not its modules) will be loaded into memory. If there are modules which the kernel will need before any filesystems are available (this usually means the drivers for the filesystems and their devices), then those modules will be in the initramfs (in memory), and the kernel will load them from there. Other modules can be loaded later from the file system.

mikeserv
  • 57,448
  • 9
  • 113
  • 229
Toby Speight
  • 8,460
  • 3
  • 26
  • 50
3

The kernel in most modern Linux setups is heavily module based, i.e., the kernel proper (loaded on boot into RAM) includes just the bare minimum functionality, all the rest is compiled as modules (loadable at runtime). To make this work even when e.g. the devices or filesystems required for boot are modules, an initramfs is loaded with the kernel (as the name implies, this is a RAM area with a simple filesystem, mounted on boot). This temporary filesystem is mounted on /, and contains startup programs and the required modules. Once the startup on initramfs is done, Linux executes a pivot_root(8), mounting the real / and dropping the initramfs contents.

The point of this complexity is that e.g. a distribution can compile one kernel (minimal kernel and full set of modules), and on installation of the kernel create an initramfs tailored to the hardware and setup of the target machine. All this is required due to the wild variety of devices and configurations of "Personal Computers".

Toby Speight
  • 8,460
  • 3
  • 26
  • 50
vonbrand
  • 18,156
  • 2
  • 37
  • 59