Panther Coder
Panther Coder

Reputation: 1078

How Grub (legacy) recognizes and processes various file systems?

I have been reading about Grub legacy boot loader. And what struck me is how Grub handles file systems. Grub supports a large subset of file systems. My doubt is, How Grub recognizes these file systems from one another? Even if they are recognized, How are they been processed? Like every file system has their own implementation. Does Grub loads runtime libraries from disk?

I know the question is too broad. But please give me some tips to kick start.

Thanks.

Upvotes: 0

Views: 345

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 363980

FS detection: magic number in the first 4B of the partition, or similar signature detection, I assume.

FS processing: stage1 loads a stage1.5 embedded in the space between the partition table and the start of the first partition. This contains a read-only driver for the filesystem containing /boot at least, where it can load stage2 from.

If there isn't room for a stage1.5, you're stuck embedding the sector address of the full stage2, like Netch describes. This can change on upgrades to GRUB, or just copying around files in /boot, so it's not preferred.

The Grub Wikipedia article describes the operation of GRUB-legacy, and it's probably also documented in the GRUB-legacy docs.


On an ancient Debian machine I still have around, which boots with GRUB 0.97, /boot/grub contains:

-rw-r--r-- 1 root root    197 2008-08-29 23:57 default
-rw-r--r-- 1 root root     46 2008-08-29 23:57 device.map
-rw-r--r-- 1 root root     31 2008-08-29 23:57 device.map~
-rw-r--r-- 1 root root   7552 2008-08-29 23:57 e2fs_stage1_5
-rw-r--r-- 1 root root   7424 2008-08-29 23:57 fat_stage1_5
-rw-r--r-- 1 root root   8192 2008-08-29 23:57 jfs_stage1_5
-rw-r--r-- 1 root root   3377 2012-04-19 20:41 menu.lst
-rw-r--r-- 1 root root   3377 2012-04-19 20:41 menu.lst~
-rw-r--r-- 1 root root   6848 2008-08-29 23:57 minix_stage1_5
-rw-r--r-- 1 root root   9248 2008-08-29 23:57 reiserfs_stage1_5
-rw-r--r-- 1 root root    512 2008-08-29 23:57 stage1
-rw-r--r-- 1 root root 108328 2008-08-29 23:57 stage2
-rw-r--r-- 1 root root   8872 2008-08-29 23:57 xfs_stage1_5

The FS-specific stage1_5 files are not read from there at boot time: the one needed to read /boot is embedded by grub-install.

Upvotes: 2

Netch
Netch

Reputation: 4562

The typical setup is that GRUB stage 1 (which is 1-sector binary) knows location of stage 2 and loads it. This location is written into stage 1 by installation command (setup or install). (More precisely, start of stage 2 file is updated with list of physical locations of other file parts; stage 1 loads a starting sector of stage 2, and stage 2 continues this bootstrapping with rest of stage 2.) If stage 1 isn't updated with real stage 2 setup, it expects stage 2 in sectors just after stage 1 (a variant e.g. for GRUB diskette).

Once stage 2 is loaded, it can do all following operation. Stage 2 embeds a set of read-only drivers for all supported FSes; such drivers are much smaller than full-function drivers. Installation process includes also coding of boot drive and partition into stage 2, so, after boot it knows where to find the effective config ([/boot]/grub/menu.lst for this version). Suddenly, FS type isn't coded in stage 2 config aread. Detection of FS type is done in *_mount() functions (file set stage2/fsys_*.c); each one does its best to detect that it's for it, including signature check and partition type check in partition table. I don't know why this style is choosed, but seems they expected FS type detection to be rock stable.

Since stage 2 is loaded, fed with proper internal config (device, partition) and opened its FS, it is able to load the menu and continue with higher level logic.

(This description didn't include stage1.5 because the latter is used under rather specific circumstances.)

So, answering your specific questions,

How Grub recognizes these file systems from one another?

Both with signatures and partition types (DOS, BSD...)

Does Grub loads runtime libraries from disk?

No, it uses own read-only drivers.

But please give me some tips to kick start.

If you can read C, the best way is to clone its repository, checkout grub-legacy branch and read its sources.

Upvotes: 4

Related Questions