Re: Kernel boot process and root fs in minimal systems

From: Warner Losh <imp_at_bsdimp.com>
Date: Fri, 26 Jul 2024 20:03:16 UTC
On Fri, Jul 26, 2024 at 12:23 PM Bryce <678yym@gmail.com> wrote:

> Hello list,
>
> Ive been using FreeBSD for a few months now, and have been using Linux for
> a few years. I've read some man pages and relevant sections of the manual.
> I'm not opposed to RTFM, but please point to relevant sections.
>
> So what I've gathered:
>
> BSD has a 3 stage bootloader, stage 0 is obviously just for getting past
> the one sector limit, stage 1 passes kernel configuration options(?) to
> stage 3, which finds the kernel on the rootfs (which may be the network),
> and passes stage 2 config onto the kernel (ie, what modules to load etc?).
>
> Then, what I assume happens is that the kernel takes the config options
> from stage 2 and mounts rootfs and then executes /init.
>
> Is this correct, list? If so, is it possible to use a tmpfs-like
> filesystem to mount rootfs in memory alone?
>

Sorta. For BIOS booting, it's indeed a several step process: boot0 acts as
MBR (though standard MBR also works). boot 1 loads boot2 which reads
boot.conf for kernel command line args (very restricted) and passes them to
/boot/loader of the first UFS partition it finds on that disk. /boot/loader
decodes those args, finds the root filesystem, loads the kernel from it
then boots the kernel. This is only on x86.

For UEFI bootlng, loader.efi runs directly, finds the root, loads the
kernel and jumps to that.

There's some other weird cases that you likely don't care about for niche
hardware.


> I know about md(4), and how it bakes the md image into the kernel. This is
> what I'm looking for, but what about the bootloaders? do they need to be
> configured to not look for rootfs on disk?
>

For BIOS booting, you are stuck reading a ram disk off some disk and then
you can use that to boot in various ways. It's a pain and has been a while
since I've done this, but I recall there being several issues that trial
and error would be needed to solve.

For UEFI, you can either have a ram disk from the ESP or you can bundle it
with the loader.efi program. This is possible, but I'd have to go look at
the sources to refresh my memory on what options you'd need for bundling.


> sorry if my questions are uninformed, I'm quite new. If somebody wants to
> point me to the relevant sources in the source tree I'd be happy to read
> them.
>
> also, if this is the wrong list to be writing to, I apologize, I couldn't
> find a more appropriate list.
>

I'm not sure this is the best place for it, but it isn't the worst place
for it.

Maybe if you could describe what you're doing, I can tailor my answers a
bit better.

Warner

Warner


> thanks list.
>
>