2

The Linux kernel contains very minimal code that is necessary to boot on a majority of devices but I don't get the point of having Initrd/Initramfs.
As far as I've understood Initrd/Initramfs job is to temporary provide the file system drivers to actually load the root file system.
If this the case, then why don't we simply add the necessary file system drivers into the kernel itself.
We have to ultimately load both the kernel and the Initrd/Initramfs so why no integrate both into one image file since both of them take the same amount of memory.


*******Update******

1. What actually constitutes the linux kernel? Is it just the vmlinuz image(approximately 5-6MB) or the collection of loadable modules,vmlinuz image, initrd file and other components.
2. Why is the size of the kernel image vmlinuz(approx 5-6MB) less than size of initrd file(approx 18MB). The kernel contains the code for many other things as compared to initrd that just contains code to mount the File system.

  • When you talked about kernel.You should talk the whole kernel process.Linux kernel is having kernel config, system.map, vmlinuz and initramfs also /lib/modules/kernel-version.All linux drivers are in /lib/modules/kernel-version/fs. When you enabled file system driver on kernel config. Kernel will load file system driver automatically without do modprobe command. Linux Kernel is not just initrd/initramfs. – supriady Dec 26 '16 at 13:54
  • @supriady I'm a bit confused now. What exactly is Linux kernel, is it just the vmlinuz image file or all the above components you mentioned. If it constitutes all of the above modules then why is said that linux kernel is so small ? Including these files would take up large memory. – Shivam aggarwal Dec 26 '16 at 13:59
  • You can search about linux kernel when you want a detail information. When you read about how to build linux kernel from source code. You will understand about linux kernel. – supriady Dec 26 '16 at 14:11
  • When you enabled all drivers on kernel.Kernel would take up large memory.Kernel 4.x contains 5 millions source code.Linux kernel is so small when you used embedded linux like vanilla kernel on android. – supriady Dec 26 '16 at 14:18
2

First, it's not just the filesystem drivers. It's also the drivers for whatever storage device the filesystem is on. And possibly more if you use LVM, encryption, etc.

Regardless, it's definitely possible to build them into the kernel. I know slackware at least has a kernel choice named 'huge' which does just that and does not require an initramfs for typical PC hardware.

The downside is that such a kernel has to include everything anyone might possibly need: every possible filesystem that might be used as root, the device drivers for whatever might hold that filesystem, etc. So you end up with a bloated kernel which contains a lot of unused code and wastes memory.

I think there are also some drivers which can only be built as kernel modules but that's more a design choice than a real technical limitation.

Answer to edits:

  1. It probably depends who you ask and in what context. To me, it's everything which runs in kernel space so that includes all the drivers. A kernel developer might have a different view with many more names for different pieces.

  2. Actually, the vast majority of the kernel code is loadable modules and drivers. Some of those are quite large. On my system, btrfs alone is 500 kB compressed. NFS is nearly as large. ext4 is 300 kB. There is a huge number of supported storage devices as well and you might need any of them to get your root filesystem. Just browse through /lib/modules/*/kernel/drivers/ on a linux system and you'll see.

  • Thanks alot for the clear answer. But what is the problem even if the kernel gets bloated because in either case the same memory is used up. Kernel + FS drivers = Kernel + Initrd = Same amount of memory. – Shivam aggarwal Dec 26 '16 at 13:56
  • 2
    @Shivam initrd memory is freed during boot as it is no longer needed once you have the real root fs. Only the modules which were actually loaded need to remain in memory. With the big kernel nothing can be unloaded. I think it can't even be swapped out. Also, the default initrd does not need to contain everything if you have an alternate complete one in case your hardware changes. – Olivier Dec 26 '16 at 14:36
  • what is actually the meaning of linux kernel. What all does it consist of? I've made some edits into the question, please review them and update the answer. It'll be of great help. Thanks. – Shivam aggarwal Dec 26 '16 at 14:48
  • You've now made the question too broad unfortunately. – Philip Kendall Dec 26 '16 at 15:33
  • 1
    @Shivam Yes that's correct. But it is also possible to customize the initrd to contain only the required drivers so it is much smaller. It's also possible to compile a custom kernel for very low memory devices. – Olivier Dec 27 '16 at 13:47
2

It's a bit of a chicken and the egg problem... You can (and usually should) let lots of your drivers be modules, it keep your kernel lean and saves memory (until and if you need the module). However you do need some drivers built into your kernel - specifically for whatever device your booting from (eg. CD-ROM drive, HDD, ...), the filesystem used (eg. iso9660, ext4, ...), and any additional drivers needed to support this (eg. USB-support, SCSI-support, ...). If you don't, your kernel won't be able to access the medium and the filesystem where the modules for additional drivers resides, and thus can't load them into the kernel.

If you make a customized kernel for your own system - where you know in advance what hardware (HDD-type, DVD-drive type, etc.) you've got - this is not a problem. You just need drivers for what you actually got or think you may need. And you only need to include into the kernel, the drivers needed at boot-time.

However, if your making an install-disc for a distro, you must include drivers for all possible hardware users are likely to encounter! Including them all to the kernel, would make the kernel hopelessly bloated and it would take too much RAM on low-end systems. Making all drivers modules won't work, because the modules may be needed for loading them. The solution is initrd!

initrd is the image of a RAM-disk that is loaded into memory at start. All drivers likely to be needed lays on this as modules. As long as the kernel includes drivers to read a DVD, to read the filesystem on a DVD, and to read a RAM-disk; it will be able to access and load on demand the driver-modules from the initrd. This solves the problem for those who makes distros to make the kernel both small and include drivers for every hardware it may run on.

Most users probably keep using initrd-boot after they've installed Linux on their harddrives... This is not really neccessery, as they could build a custom-kernel - perhaps with additional kernel-modules - customized for the hardware they actually got. The two-step process of first loading the initrd, takes a bit of extra time during boot.

  • But now that the distro vendors have included all needed modules(i.e. USB, CD-ROM, HDD, etc) in the initrd and we ultimately have to load initrd in the memory so won't it cause problems on low memory devices since it contains all possible drivers that were removed from the kernel to make less bloaty! – Shivam aggarwal Dec 26 '16 at 15:51
  • 1
    You have to differentiate between two things here: loading the ramfs into memory and loading modules into the kernel. Only the needed modules are linked into the kernel from the initramfs, after which the it can be deleted before passing control to the init program. See Managing the initial ramfs (initramfs) archive. – Johan Myréen Dec 26 '16 at 22:44
  • @JohanMyréen I think you've exactly caught the issue. So are loading modules in kernel and loading ramfs in memory different things? If so, Why? Because even if the modules are getting loaded into the kernel even then memory is being consumed(the kernel space is getting larger). Also if I keep all my drivers into the kernel can't I unload the less useful ones later? or is it just possible to remove ramfs from the memory thereby removing the useless drivers. – Shivam aggarwal Dec 27 '16 at 11:35
  • 1
    Kernel modules are specially built object code files, containing executable code, data and a symbol table containing the names and other attributes of the entry points the module provides, and the names of the symbols in the kernel the module is dependent on. Loading a module means this object code file is linked into the kernel at run time, instead of being linked statically when the kernel image was built. A kernel module in the ramfs is just a file like any other file on a file system. All module files (no matter it they have been loaded or not) can eventually be deleted from the ramfs. – Johan Myréen Dec 27 '16 at 12:30
  • @JohanMyréen Sir I really like your easy way of explanation. Thanks a lot. Sir if possible can you please join me on a chat room( chat.stackexchange.com/rooms/50778/discussion-on-initramfs )so that I can get some more clarifications, I think the comment section is getting too long :) . Otherwise can also I ask my further doubts here itself. Completely up to you Sir. Please do reply. – Shivam aggarwal Dec 27 '16 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.