Page 1 of 1

Toy kernel, GRUB and filesystems

Posted: Fri Jul 16, 2010 11:07 am
by crazy2k
Hello.

We're writing a toy educational kernel with a friend of mine, and currently we're creating a diskette.img file doing something like:

Code: Select all

cat stage1 stage2 padding kernel.bin > diskette.img
where stage1 and stage2 are GRUB Legacy's stages, kernel.bin is our kernel in ELF format and padding is some file used to align kernel.bin to the start of a sector (512 bytes).

So far, everything works OK. We're using bochs, booting from the "1.44M floppy" that diskette.img represents. However, we have to type:

Code: Select all

kernel <sector where kernel.bin starts>,<kernel.bin's length in sectors>
boot
every time we run bochs, and that's ugly.

So, I have some questions:
1. If we want our kernel to start automatically, is there some way to do it without creating a filesystem in the disk?
2. If the answer to that question is "no", then, do you recommend switching to grub2 instead of grub legacy for some reason? I'm asking this because I've been told grub2 needs a filesystem in order to work.
3. If we switch to grub2, do we need to change our kernel in some way? I've been told grub2 doesn't respect grub legacy's multiboot specification.
4. In case we add a filesystem, which one do you recommend, considering we might want to read files from it, for example for executing user's process, and we'd like not to spend our whole life writing a handler for it?

Thanks,

Re: Toy kernel, GRUB and filesystems

Posted: Fri Jul 16, 2010 11:56 am
by Owen
crazy2k wrote:So, I have some questions:
1. If we want our kernel to start automatically, is there some way to do it without creating a filesystem in the disk?
Not without building a custom kernel
2. If the answer to that question is "no", then, do you recommend switching to grub2 instead of grub legacy for some reason? I'm asking this because I've been told grub2 needs a filesystem in order to work.
Tickle your pickle however you want. Grub2 is orthagonal to to this issue
3. If we switch to grub2, do we need to change our kernel in some way? I've been told grub2 doesn't respect grub legacy's multiboot specification.
It does, and in fact does it better. It will introduce a new, better version too however
4. In case we add a filesystem, which one do you recommend, considering we might want to read files from it, for example for executing user's process, and we'd like not to spend our whole life writing a handler for it?
A simple option which GRUB supports is ISO9660 (aka make a CD ROM image)

Re: Toy kernel, GRUB and filesystems

Posted: Fri Jul 16, 2010 5:36 pm
by crazy2k
Owen wrote:
crazy2k wrote:So, I have some questions:
1. If we want our kernel to start automatically, is there some way to do it without creating a filesystem in the disk?
Not without building a custom kernel
What? What does the kernel have to do with this? I'm not sure I understand what you said. I was mainly talking about GRUB. If there was some way to make it boot our kernel without the need of us typing those lines in its shell.
Owen wrote:
crazy2k wrote:2. If the answer to that question is "no", then, do you recommend switching to grub2 instead of grub legacy for some reason? I'm asking this because I've been told grub2 needs a filesystem in order to work.
Tickle your pickle however you want. Grub2 is orthagonal to to this issue
Yes. Somehow, I mixed two topics here. Anyway, I'd like to hear opinions on GRUB2 versus GRUB Legacy for toy kernels like this one.
Owen wrote:
crazy2k wrote:3. If we switch to grub2, do we need to change our kernel in some way? I've been told grub2 doesn't respect grub legacy's multiboot specification.
It does, and in fact does it better. It will introduce a new, better version too however
So, it's backward-compatible? Our kernel will work with GRUB2 without any changes?
Owen wrote:
crazy2k wrote:4. In case we add a filesystem, which one do you recommend, considering we might want to read files from it, for example for executing user's process, and we'd like not to spend our whole life writing a handler for it?
A simple option which GRUB supports is ISO9660 (aka make a CD ROM image)
OK. Have you implemented it? Any reference implementations?

Thanks,

Re: Toy kernel, GRUB and filesystems

Posted: Fri Jul 16, 2010 7:13 pm
by Owen
crazy2k wrote:
Owen wrote:
crazy2k wrote:So, I have some questions:
1. If we want our kernel to start automatically, is there some way to do it without creating a filesystem in the disk?
Not without building a custom kernel
What? What does the kernel have to do with this? I'm not sure I understand what you said. I was mainly talking about GRUB. If there was some way to make it boot our kernel without the need of us typing those lines in its shell.
Brainfart! I meant custom bootloader
Owen wrote:
crazy2k wrote:2. If the answer to that question is "no", then, do you recommend switching to grub2 instead of grub legacy for some reason? I'm asking this because I've been told grub2 needs a filesystem in order to work.
Tickle your pickle however you want. Grub2 is orthagonal to to this issue
Yes. Somehow, I mixed two topics here. Anyway, I'd like to hear opinions on GRUB2 versus GRUB Legacy for toy kernels like this one.
[/quote]
I've jused both; theres not much difference. Grub 2 is slightly buggier, but Multiboot 2 will be good
crazy2k wrote:
Owen wrote:
crazy2k wrote:3. If we switch to grub2, do we need to change our kernel in some way? I've been told grub2 doesn't respect grub legacy's multiboot specification.
It does, and in fact does it better. It will introduce a new, better version too however
So, it's backward-compatible? Our kernel will work with GRUB2 without any changes?
Correct. You'll need to add a Multiboot2 header if you want the new features.
crazy2k wrote:
Owen wrote:
crazy2k wrote:4. In case we add a filesystem, which one do you recommend, considering we might want to read files from it, for example for executing user's process, and we'd like not to spend our whole life writing a handler for it?
A simple option which GRUB supports is ISO9660 (aka make a CD ROM image)
OK. Have you implemented it? Any reference implementations?

Thanks,
Sorry, no, but ISO9660 isn't really that complex besides the dual-endian data structures.

Re: Toy kernel, GRUB and filesystems

Posted: Sat Jul 17, 2010 1:05 am
by egos

Re: Toy kernel, GRUB and filesystems

Posted: Sat Jul 17, 2010 3:56 am
by Creature
In short, for CD-Rom images you can use ISO's created using GRUB's Eltorito stage and mkisofs. Probably the hardest part is actually finding/building the eltorito stage, but you can pretty much grab that off most hobby OS' around here (since most of us use ISO's in some way or another anyway). Once you did that, all you have to do is set up a directory structure where the files that need to be on the ISO are in (including the Eltorito stage at the right spot) and call mkisofs with the right parameters on the directory.

Re: Toy kernel, GRUB and filesystems

Posted: Sun Jul 18, 2010 4:29 am
by TylerH
If you're deving on a *nix, you could make a file large enough to hold your OS, mkfs it, mount it as a loopback dev, install grub2 to it, mount it to a dir, and a boot/grub/grub.conf to it that automatically boots your kernel. From this point you would probably want to make a makefile build target to update the image. Make the build target compile your kernel/OS, copy it to your image, boot qemu/bochs/whatever with your image as HDD.

It's a pain to setup the first time, but from then on, it makes debugging a breeze.

Re: Toy kernel, GRUB and filesystems

Posted: Mon Jul 19, 2010 3:02 am
by crazy2k
TylerAnon wrote:If you're deving on a *nix, you could make a file large enough to hold your OS, mkfs it, mount it as a loopback dev, install grub2 to it, mount it to a dir, and a boot/grub/grub.conf to it that automatically boots your kernel. From this point you would probably want to make a makefile build target to update the image. Make the build target compile your kernel/OS, copy it to your image, boot qemu/bochs/whatever with your image as HDD.

It's a pain to setup the first time, but from then on, it makes debugging a breeze.
Heh, before reading what you said I just did it. Actually, I used GRUB Legacy instead of GRUB 2. Also, I used mkdosfs to create the filesystem, so it must be FAT.

The only thing I don't like about this is that I can't create the whole disk in the Makefile, since parts of the procedure to create it need root access. So the Makefile only compiles the kernel and copies it to the image using mcopy.