Automate the grub instructions when booting

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
helino
Posts: 9
Joined: Mon Jan 16, 2012 8:11 am

Automate the grub instructions when booting

Post by helino »

Hi,

I've followed the steps of http://wiki.osdev.org/Bare_bones and I'm successfully booting my kernel (yay :D).

However, it will get tiresome to write the commands for locating the kernel and booting at the grub prompt in bochs every time I want to start my kernel. That is, I don't want to type

Code: Select all

grub> kernel 200+10
grub> boot
every time.

Does anyone know how to do this? If anyone has a solution that doesn't rely on using sudo, it would be even nicer (since I can't use sudo at the computers in my school)!
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Automate the grub instructions when booting

Post by Jezze »

Code: Select all

#!/bin/sh

dd if=/dev/zero of=kernel.img bs=512 count=2880
dd if=boot/grub/stage1 conv=notrunc of=kernel.img bs=512 seek=0
dd if=boot/grub/stage2 conv=notrunc of=kernel.img bs=512 seek=1
dd if=menu.lst conv=notrunc of=kernel.img bs=512 seek=200
dd if=boot/kernel conv=notrunc of=kernel.img bs=512 seek=300
dd if=boot/initrd conv=notrunc of=kernel.img bs=512 seek=400

grub <<EOF
device (fd0) kernel.img
install (fd0)0+1 (fd0) (fd0)1+199 (fd0)200+1
EOF
I think this is sufficiant. It was a long time ago I used this but I think it is correct. It creates an image with a kernel and a menu.lst (with optional initrd). Grub will automatically find your menu.lst file and boot the kernel.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
helino
Posts: 9
Joined: Mon Jan 16, 2012 8:11 am

Re: Automate the grub instructions when booting

Post by helino »

Thanks for your quick answer!

I understand what the dd commands do, but I don't understand the last part of the script does?

More specifically, the lines I don't understand are:

Code: Select all

grub <<EOF
device (fd0) kernel.img
install (fd0)0+1 (fd0) (fd0)1+199 (fd0)200+1
EOF
I also wonder what menu.lst should contain?

Once again, thanks for your help!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Automate the grub instructions when booting

Post by Solar »

The Bare Bones tutorial creates a floppy image, because it is easiest (i.e., keeping the tutorial short).

Your next step should be to set up not a floppy image, but a hard drive image, where the kernel image is written to a partition. (Eventually, you will want your OS to boot from hard disk, not floppy.) Then set up GRUB in the "normal" way - i.e. not booting into the GRUB command line, but using a configuration file to display a boot menu (or, if you prefer, booting from the image directly, but still under control of the grub.cfg). Because, again, in the end you'll want to have the GRUB on your real hard drive to select between operating systems.

Check out the GRUB Wiki page.
More specifically, the lines I don't understand are:

Code: Select all

grub <<EOF
device (fd0) kernel.img
install (fd0)0+1 (fd0) (fd0)1+199 (fd0)200+1
EOF
I also wonder what menu.lst should contain?
I'd try the GRUB documentation...
Last edited by Solar on Mon Jan 16, 2012 9:40 am, edited 2 times in total.
Every good solution is obvious once you've found it.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Automate the grub instructions when booting

Post by Jezze »

Oops sorry. Forgot the menu.lst

Code: Select all

title MyOS
root (fd0)
kernel 300+100
module 400+300
If you are not using a initrd - remove the module section. Also remember that depending on the size of your executables you might need to adjust the limits.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
helino
Posts: 9
Joined: Mon Jan 16, 2012 8:11 am

Re: Automate the grub instructions when booting

Post by helino »

Solar wrote:The Bare Bones tutorial creates a floppy image, because it is easiest (i.e., keeping the tutorial short).

Your next step should be to set up not a floppy image, but a hard drive image, where the kernel image is written to a partition. (Eventually, you will want your OS to boot from hard disk, not floppy.)
I don't think I fully understand this part, if my OS is very small (it will probably not have support for creating nor writing files), why is booting from a floppy a bad idea?

Jezze and Solar, thanks for taking your time and answering my questions, I really appreciate it!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Automate the grub instructions when booting

Post by Solar »

Most modern computers don't even have a floppy drive anymore.

Floppy images are somewhat useful for the first few steps when using an emulator, but their usefulness quickly diminishes. As soon as your OS would have to load further binaries from its boot media - do you really want to write a floppy driver? Given the fact that there is no floppy drive that your OS will ever be booted from?
Every good solution is obvious once you've found it.
helino
Posts: 9
Joined: Mon Jan 16, 2012 8:11 am

Re: Automate the grub instructions when booting

Post by helino »

Ok, it seems reasonable to use some other form of media, not floppy.

Based on my limited experience with this, a bootable CD seems like a good option, what do you think about that?

Which one of the following tutorials would you recommend? Or do you have any other literature that you think is better (tutorial, guide, wiki) on how to create a bootable CD?

Once again, thanks for your help Solar!

EDIT: Fixed formatting
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Automate the grub instructions when booting

Post by Solar »

Erm... is it even possible to have a floppy with the GRUB stage(s) and a functioning filesystem installed?

(I skipped the floppy / FAT part, but I think I recall that installing GRUB on a floppy more or less annihilates the filesystem...)
Every good solution is obvious once you've found it.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Automate the grub instructions when booting

Post by thepowersgang »

@Solar: It is, I've been running a FAT/Grub floppy for years. Don't really know how to make one from scratch, but I'm reasonably sure it is still possible.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: Automate the grub instructions when booting

Post by Velko »

With GRUB2 it is pretty easy to build an ISO image:

Code: Select all

grub-mkrescue -o ostest.iso runimage/
With runimage/ directory's contents being:

Code: Select all

runimage/boot/grub/grub.cfg
runimage/boot/kernel.bin
And grub.cfg containing:

Code: Select all

set default=0
set timeout=0

menuentry "My OS" {
        set root='(hd96)'
        multiboot /boot/kernel.bin
}
Have fun!
If something looks overcomplicated, most likely it is.
helino
Posts: 9
Joined: Mon Jan 16, 2012 8:11 am

Re: Automate the grub instructions when booting

Post by helino »

Thanks for all the great replies!

For now, I ended up using the floppy disk with GRUB preloaded onto it from JamesM's kernel tutorial (see the link in the beginning of http://www.jamesmolloy.co.uk/tutorial_h ... setup.html).

However, ISO images seems like a nicer alternative, so I will probably look into Velkos suggestions when I have time.

If you have any more ideas about how to solve this problem, please post them, I will continue to follow this thread!
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Automate the grub instructions when booting

Post by Kevin »

thepowersgang wrote:@Solar: It is, I've been running a FAT/Grub floppy for years. Don't really know how to make one from scratch, but I'm reasonably sure it is still possible.
Create the FAT file system on the floppy image, copy all the files there (including a /boot/grub directory containing stage1, stage2 and your menu.lst) and then run this in a grub shell:

Code: Select all

device (fd0) $IMAGE_PATH
root (fd0)
setup (fd0)
Developer of tyndur - community OS of Lowlevel (German)
Post Reply