Using a script to produce a bootable floopy image using grub

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
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Using a script to produce a bootable floopy image using grub

Post by Puffy »

I'm trying to automate the process of creating a bootable floppy image using grub.

Currently I am using the following method in my makefile:

Code: Select all

floppy.img: kernel.bin	
     #@cat ./grub/stage1 ./grub/stage2 ./grub/pad kernel.bin > ./image/floppy.img
But this requires me to type 'kernel 200+18' then 'boot' into bochs whenever I want to test my kernel.

So, I'm trying to use a shell script that uses a loopback device. This is my script:
echo "mypasswordbutthisisnotit" | sudo -S dd if=/dev/zero of=./image/floppy.img bs=512 count=2880
sudo losetup /dev/loop0 ./image/floppy.img
sudo mkfs -t ext2 /dev/loop0
sudo mount -t ext2 /dev/loop0 ./image/floppy/
sudo mkdir -p ./image/floppy/grub/boot/
sudo cp ./grub/stage1 ./image/floppy/grub/boot/stage1
sudo cp ./grub/stage2 ./image/floppy/grub/boot/stage2
sudo cp ./grub/menu.lst ./image/floppy/grub/boot/menu.lst
sudo cp kernel.bin ./image/floppy/kernel.bin
sudo umount /dev/loop0
I then try to boot the image with bochs I have a bochsrc.txt file in the same directory as the image which is as follows:

Code: Select all

boot: floppy
floppya: 1_44="floppy.img", status=inserted
But bochs says that there is no bootable device.

The same bochsrc.txt file allows me to boot Bran's kernel image which you can download at the end of the tutorial.

Also, I've never been able to get the loopback method to work manually (i.e. typing the comands at the terminal myself). I'm a bit stuck.

Your wonderful help and advice would be very welcome.

Cheers,

Puffy
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Using a script to produce a bootable floopy image using grub

Post by Combuster »

I use the grub shell to get a working floppy with GRUB. (basically have it install itself in the way it expects that to happen) I forgot to add some of the needed files to the repository, so I don't have a complete example, but you should get the idea. Makefile excerpt:

Code: Select all

install-ia-pc-multiboot: $(BUILDROOT)/gzImage
	@echo [make install] mounting floppy
	@mount /dev/fd0 /mnt/floppy
	@cp $(BUILDROOT)/gzImage /mnt/floppy/
	@echo [make install] copying files
	@-mkdir /mnt/floppy/boot
	@cp /boot/grub/stage1 /mnt/floppy/boot/
	@cp /boot/grub/stage2 /mnt/floppy/boot/
	@cp kernel/ia_pc/grub.cfg /mnt/floppy/boot/ 
	@echo [make install] configuring grub
	@cat kernel/ia_pc/grub.sh | grub --batch > /dev/null
	@echo [make install] unmounting floppy
	@umount /dev/fd0
However this assumes quite a lot of your installation (in my case, I can only build grub images under linux. In my case it's hardly a problem since I normally use my own bootloader).

The easier way would be to create a FAT+grub floppy just once, then pack it into an floppy image. Then when you need to build your final floppy image, just make a copy of that prepared image, and then append the kernel and grub config.

Hope that gives some ideas.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Re: Using a script to produce a bootable floopy image using grub

Post by Puffy »

Thanks for your response, but could you give me a bit more of a description about what's going on in these lines?

Code: Select all

@cp kernel/ia_pc/grub.cfg /mnt/floppy/boot/
   @echo [make install] configuring grub
   @cat kernel/ia_pc/grub.sh | grub --batch > /dev/null
   @echo [make install] unmounting floppy
   @umount /dev/fd0
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: Using a script to produce a bootable floopy image using grub

Post by thepowersgang »

That code copies the default grub config to the floppy, prints a message to the screen, runs "grub --batch" passing the contents of srub.sh to it and ignoring the output, prints another message and unmounts the floppy
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

Re: Using a script to produce a bootable floopy image using grub

Post by Adek336 »

Basically, puffy's script copies files but does not alter the bootsector nor does it run the grub installer.
Puffy wrote:But this requires me to type 'kernel 200+18' then 'boot' into bochs whenever I want to test my kernel.
Then you don't really need a script that makes a grub disk from scratch, you'd do pretty well with a script that just updates your kernel's binaries on a grub-floppy-disk-image in a known place.

I'm using such an entry in /etc/fstab

Code: Select all

/srv/tftp/grub.img /mnt/fpy auto  defaults,loop,user 0 0
and then such a script does the job

Code: Select all

mount /mnt/fpy
cp core.x /mnt/fpy
umount /mnt/fpy
if you're stuck without a grub disk image check out http://panalix.sourceforge.net/download ... 8.7.19.ima and just change the config and the files.
Puffy
Member
Member
Posts: 26
Joined: Mon May 26, 2008 7:00 am

Re: Using a script to produce a bootable floopy image using grub

Post by Puffy »

I'd still prefer to make my own grub image to begin with - otherwise I'm ignorant of part of my compilation process and I'm trying to learn.

I don't have a floppy drive on my laptop so I'm having to use a loopback device. When I get to the grub console it doesn't recognise the loopback, is there anyway around this?
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

Re: Using a script to produce a bootable floopy image using grub

Post by Adek336 »

As a sidenote, what I posted depended on the loopback device (the loop option).
Post Reply