-1

Writing OS experiment from scratch and curious about Grub2 and my FS

I haven't tried very much with this just yet, at least not in practice. I've been reading about GRUB2 and how to set it up but am getting slightly lost. I apologize, I don't have any source code at the moment to offer you. I just wanted to know how to set up GRUB2 to use a File System that I have written from scratch myself.

user3840170
  • 26,597
  • 4
  • 30
  • 62

1 Answers1

0

Write a GRUB driver module for your FS, like how it has modules for XFS, ext2/3/4, and so on. https://wiki.osdev.org/Writing_GRUB_Modules


https://www.gnu.org/software/grub/manual/grub/grub.html#Images explains the different components of GRUB2. The filesystem driver is loaded as part of core.img, which is built from other images.

Keep in mind that many new filesystems don't get GRUB support right away, and having a separate /boot partition that GRUB can read is not rare. e.g. on a single disk instead of a complicated RAID (although GRUB does have some md support), not encrypted, and with a long-established filesystem that GRUB does have a driver for. And that doesn't need a complicated log replay on dirty shutdowns before even reading.

Also, the earliest stage of GRUB booting needs to load core.img from a block list that's hard-coded into the small first thing the firmware loads, so filesystems where the blocks of a file's data can move around when the file wasn't modified aren't usually a good fit for /boot at all. You'd never know when you had to re-install GRUB after any change to anything on /boot.


But if you still want to make your FS bootable in GRUB, check the GRUB source code, there's probably some documentation.

https://www.gnu.org/software/grub/grub-documentation.html

https://www.gnu.org/software/grub/grub-development.html

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Thank you so very much, guys. This has been quite enlightening. I'm also learning that there is a ton of work behind writing an installer for my OS, should I ever want to take it public. :) I'm also picturing this in my mind... I'm going to have to do all the initial dev in an established FS before I can start formatting with my FS, then at that point writing the GRUB module becomes more important... Sorry. My writings are like verbose level 1,000. It helps me think things out and discover any bugs in my thought train as well. :) – wolverine79936 May 05 '19 at 03:00
  • @wolverine79936: You don't have to use GRUB at all. A simple EFI application can be the whole bootloader that loads your kernel + initrd. e.g. my `/boot` is a 350MB VFAT filesystem that UEFI on my PC can read (it's the "EFI system partition"). My `/` partition is BTRFS (with Arch Linux). I use `systemd-boot` instead of GRUB. https://wiki.archlinux.org/index.php/Arch_boot_process#Boot_loader lists various bootloaders, including that, which can boot from UEFI or turn your kernel itself into a UEFI application. – Peter Cordes May 05 '19 at 03:20
  • Thank you, @Peter. I will try to remember about EFI. Unfortunately, my current dev computer does NOT have EFI. It's still a BIOS. I'm looking to build a new dev comp in the near future out of Intel hardware instead of AMD, though, in the near future. ;) – wolverine79936 May 05 '19 at 03:38
  • I am trying to find my own answer here. One site says that GRUB2 can handle having /boot/grub on a NTFS partition, another site says no to that and that it needs to be on a FAT partition. If it does indeed need to be a FAT partiton, can I use FAT32 or does it have to be FAT16? – wolverine79936 May 05 '19 at 03:43
  • @wolverine79936: No idea, I haven't had a dual-boot setup for ages. IDK why you'd use either of those FSes for a `/boot`, though, because presumably you're using GRUB to boot a non-Windows OS that supports a non-terrible (non-FAT) filesystem that's easily and well-supported by open-source drivers (unlike NTFS). e.g. EXT2 or maybe MINIX would be good lowest-common-denominator choices for a small `/boot` – Peter Cordes May 05 '19 at 03:49
  • @wolverine79936: I'm pretty sure GRUB supports FAT32, though; https://wiki.archlinux.org/index.php/GRUB#Common_installation_errors mentions that installing GRUB onto the EFI system partition requires it to be FAT32. I assume GRUB is then able to read the partition UEFI loaded it from. – Peter Cordes May 05 '19 at 03:52
  • got another question... Oracle VirtualBox emulates uefi. From C++ in Visual Studio 2019, how would I go about compiling my code into a UEFI program? I've been looking around and the tutorials I'm finding are in ASM, yet the manuals for UEFI are using C(++?) examples. – wolverine79936 May 06 '19 at 13:19
  • @wolverine79936: No idea, I don't use Visual Studio, and I've never written a UEFI application. – Peter Cordes May 06 '19 at 20:50
  • 1
    I have not had a chance to test this at all but I did find https://pete.akeo.ie/2015/01/easily-create-uefi-applications-using.html?m=1. Hopefully it is what I'm looking for. :) – wolverine79936 May 06 '19 at 23:43