Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved]Confused about initramfs
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
lazykoala
n00b
n00b


Joined: 08 Jan 2014
Posts: 22

PostPosted: Wed Dec 23, 2015 10:41 pm    Post subject: [Solved]Confused about initramfs Reply with quote

Hi,

After reading the following wiki article, I'm confused about how Initramfs and bootloader works.
https://wiki.gentoo.org/wiki/Initramfs/Guide

In this article, it says when init takes over, it will start other services and mount the file system. The problem is to mount a file system, the mount command must be usable. If mount command is in a file system which needs to be mounted then init can't finish this process. My question is kernel image resides in some file system, how bootloader finds the kernel image, how kernel image finds the init script. If bootloader can find the kernel image, kernel can find init script, why init can't find mount command?

Any explanation will be appreciated.


Last edited by lazykoala on Thu Dec 24, 2015 12:45 am; edited 1 time in total
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 43178
Location: 56N 3W

PostPosted: Wed Dec 23, 2015 11:34 pm    Post subject: Reply with quote

lazykoala,

The boot sequence goes like this. Bootstrapping from the greek god who pulled himself up to heaven by tugging on his own bootstraps, involves breaking a number of infinite loops. Its the art of loading an operating system from a hard drive that needs the operating system to read it.
The detail in what follows is based on BIOS but the problems are the same for everyone.

When you power up the CPU it fetches its first instruction from a hard coded address which is the start address for the BIOS.
For x86, it real mode address 0xffff0. Its been that ever since the 8086.
The BIOS runs, doing self checks and setting up the hardware. It can read exactly one disk block. LBA 0.
When the BIOS is done, it reads its one disc block into RAM and jumps to the load address of LBA 0.

LBA 0 is the start of the boot loader. This is grubs stage 0 Its at most 446 bytes long so all it does is to load a better loader by making BIOS calls.
This is why grub uses BIOS drive discovery order. The kernel is not yet loaded.
Grubs stage1.5 is in the blocks following stage0. stage1.5 is special, It can read exactly one filesystem type.
Stage 0 loads stage 1.5 and jumps to its start address. Stage 1.5 reads the filesystem on the boot partition to load and jump to the start of stage2.
stage2 displays the menu. When you have made your selection, stage2 loads the kernel and optionally the initrd into memory and jumps to the kernel start address.

Thats the boot loader done its thing. The compressed kernel and compressed initrd are in memory and the kernel has control of the CPU.
The kernel decompresses itself, does some initalisation and looks at its command line to see what to do next.
If there is an initrd it is mounted as root. I'll come back to that. For now, there is no initrd.

The kernel should find a root= statement to tell it where the root filesystem is. No partitions are mounted yet so /etc/fstab cannot yet be read.
The boot loader made its own arrangements to read the HDD.
Providing all the needed kernel code is built into the kernel, it mounts root and executes /sbin/init, or whatever it was told by init=.
The kernel can mount the root filesystem without the use of the mount command, provided its not given as UUID or LABEL.

If there is an initrd, the initrd is mounted as root. Its just a root filesystem in a file. It can do whatever is needed to be done before the HDD root is mounted. Typically, it provides modules, mdadm so that raid can be assembled for root on raid, LVM for users with root on a logical volume, crypto. The userspace mount, so that mount can be used to mount the root filesystem.

Once the intird has done its thing, it switches to the hard drive root and runs /sbin/init.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
lazykoala
n00b
n00b


Joined: 08 Jan 2014
Posts: 22

PostPosted: Thu Dec 24, 2015 12:10 am    Post subject: Reply with quote

Thanks NeddySeagoon. It's very helpful.

I have some questions about this statement:

Quote:
The kernel can mount the root filesystem without the use of the mount command, provided its not given as UUID or LABEL.


1. How does the kernel mount a file system without the use of the mount command?
2. In the wiki doc I mentioned above, it seems that initramfs as a temp file system provides tools to mount the real root file system. If the kernel can mount a file system without the use of mount command, why do we bother to use initramfs to mount a file system?
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 43178
Location: 56N 3W

PostPosted: Thu Dec 24, 2015 12:32 am    Post subject: Reply with quote

lazykoala,

You don't need an initrd.
If you make your own kernel and that kernel has all the options set to <*> to be able to read root, no initrd is required.
The kernel can mount the filesystem given as root= as root. Root is special in that it is the top of the filesystem tree. It has no parents.

When the kernel needs to be able to run on almost any hardware, the kernel is typically made fully modular and all the modules needed to mount root are in the initrd. The RAM needed by the initrd is freed once its no longer in use. This keeps the kernel RAM footprint small, since many modules will load, find they are not needed and unload again.

The initrd can do anything a real root filesystem can do. It is often used as a home for user space tools that are needed before real root is mounted.
It can also just contain the image to be displayed on a framebuffer console.

I need an initrd as my root is a logical volume on raid5. I need mdadm to start the raid set, then logical volume manager to activate the logical volumes before I can see root.

In the case of root being a filesystem on a partition, the kernel can mount it itself.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
lazykoala
n00b
n00b


Joined: 08 Jan 2014
Posts: 22

PostPosted: Thu Dec 24, 2015 12:45 am    Post subject: Reply with quote

Thank you very much NeddySeagoon. Have a nice day and merry Christmas!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum