Page 1 of 1

Using a script to produce a bootable floopy image using grub

Posted: Thu Jul 24, 2008 1:05 pm
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

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

Posted: Thu Jul 24, 2008 1:25 pm
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.

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

Posted: Thu Jul 24, 2008 2:42 pm
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

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

Posted: Thu Jul 24, 2008 9:20 pm
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

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

Posted: Fri Jul 25, 2008 2:00 am
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.

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

Posted: Fri Jul 25, 2008 11:31 am
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?

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

Posted: Fri Jul 25, 2008 3:53 pm
by Adek336
As a sidenote, what I posted depended on the loopback device (the loop option).