Page 1 of 1
GRUB install into .img file's MBR
Posted: Sat May 12, 2012 2:15 pm
by kammce
I have followed the instruction in the "Loopback Device" tutorial to the end and I was wondering how I install Grub in the MBR Sector area. I have tried doing research on this but I have not found anything that can help me.
When I attempt :
losetup -o32256 /dev/loop0 file_system.img
qemu -fda /dev/loop0
I get the output "no bootable device" in all fields, which makes sense because the set up ofgsets the beginning 32256 bytes (where the first partition starts).
But when I attempt
losetup /dev/loop0 file_system.img
qemu -fda /dev/loop0
I get "Booting from Floppy" and then it stalls forever and my CPU usage goes up to 50% which hints at a infinite loop.
So, I can assume that simply placing the appropriate files in the appropriate folder in the file system will not magically cause Qemu to boot from Grub.
But, my question is, how do I do so? If you guys need any additional info, I can supply it.
Re: GRUB install into .img file's MBR
Posted: Sun May 13, 2012 9:02 am
by kammce
After doing a lot of research, I believe I have found the solution but I am not sure which one will work. I will make copies of my file system to check to see if any of these works:
#after using losetup for the whole .img file, I will put stage1 from grub into the first 512 bytes of the image file.
dd if=stage1 of=/dev/loop0 bs=512 count=1
#or only use 446 bytes
dd if=stage1 of=/dev/loop0 bs=446 count=1
I am not sure which one will work. I think that the last 64+2 bytes of data are reserved for the partition table. But I could be wrong.
Re: GRUB install into .img file's MBR
Posted: Sun May 13, 2012 9:37 am
by bluemoon
Or you could simply dd into the first 512 byte of image file.
Code: Select all
@dd if=$< of=$(IMAGE_FILE) bs=446 count=1 conv=notrunc >/dev/null 2>&1
and why you using -fda to boot a hard disk image(which consist of MBR)? try -hda
I do this to install my boot sector into test image, which I pre-calculated the offset from the beginning of harddisk image (2048'th sector).
Code: Select all
@dd if=$< of=$(IMAGE_FILE) bs=1 skip=62 seek=1048638 count=450 conv=notrunc >/dev/null 2>&1
Re: GRUB install into .img file's MBR
Posted: Sun May 13, 2012 10:41 am
by kammce
Bluemoon, thank you for your input.
I have tried both strategies and both have failed me. I wrote directly to the .img file and Qemu told me that there is a "Grub Read Error" for -hda and it stalls out in -fda with output "Grub."
My first and only partition of the .img file has the bootable flag on it but it is offset to 32256 bytes from the start of the file. Do I write to the first (512 | 446) bytes from the offset start?
Re: GRUB install into .img file's MBR
Posted: Sun May 13, 2012 12:16 pm
by kammce
I am using an HD image for my file system. I want file system support so I created a HD file to hold it. I will use it to boot from and use my ext2 drivers to write to it. Is there some other way to create a virtual file system like environment without a HD file?
Re: GRUB install into .img file's MBR
Posted: Sun May 13, 2012 5:42 pm
by kammce
Please tell me if I am wrong in my assumption that you must create an image file in order to create a filesystem? can you create a theoretical virtual filesystem within a kernel? What does the Linux kernel do in order to have a filesystem within it's kernel?
Well it seems that finding information on OS development seems to be far more sparse than finding information on high level programming, that is for sure.
Thank you for any assistance.
Re: GRUB install into .img file's MBR
Posted: Sun May 13, 2012 10:26 pm
by kammce
I still am in need of filesystem support on my operating system.
Re: GRUB install into .img file's MBR
Posted: Wed May 16, 2012 5:49 pm
by 0x53h
I was excited to see someone else searching for the solution to the same problem! I'm sorry I don't have advise to offer now, but maybe we can work on this together. I want to learn the OS startup process by creating a simple file system and booting to it. I'm really having a hard time finding relevant information. Ultimately I'm trying to create a super-slim image to put on a CF card and boot an embedded system, but right now I'm in dire need of education.
Here's what I have so far:
Code: Select all
# 1. Create a simple blank .IMG file using DD.
$ dd if=/dev/zero of=./rootfs.img bs=512 count=2097152
# 2. Create a filesystem
$ mke2fs -t ext2 ./rootfs.img
# 3. Mount the .IMG file and populate with a simple, simple file structure.
$ sudo mount ./rootfs.img /mnt/rootfs/
$ sudo mkdir /mnt/rootfs/boot bin sbin proc dev etc...
# 4. Use chroot to test it's in some way valid; unmount.
$ sudo chroot /mnt/rootfs /bin/bash
$ sync
$ sudo umount /mnt/rootfs
# /bin/bash is a statically compiled Hello World program.
Here's where things get fuzzy. My next move would be to launch with qemu specifying an outside kernel. I just want to get to init, then grub installation would be next.
Code: Select all
qemu -kernel ./bzImage -hda ./rootfs.img -append "root=/dev/hda init=/bin/bash"
Kernel panic - not syncing: No init found. Try passing init= option to kernel.
At this point I've mutilated and mixed every tutorial and I'm feeling hopeless. I've been reading everything I can find: documents from The Linux Doc Project, grub tutorials, forums, lilo manual. I'd just spent a couple hours trying to install grub onto my .IMG thinking that would help the startup process. Any help or insight would be super-appreciated and I'll provide the same as I move along. Point me to a document if I'm severely lacking the knowledge on something specific - I'm not afraid to read, but at this point I've been so inundated with a broad range of material I don't know where to go next.
Re: GRUB install into .img file's MBR
Posted: Thu May 17, 2012 12:46 am
by Combuster
From what I see, you are trying to create an ext2 formatted harddisk image, after which you want to install grub on it, no?
What you are trying next is booting linux on an empty disk. Even if you add a binary on there that you call init, you won't have any userspace, and probably not even any libraries that very program needs to start at all.
Instead, you'd better go find yourself an existing linux install on an image - something like a livecd that comes with a grub install would do. Next you can start the VM with your empty harddisk as harddisk, linux as the cdrom, and from the resulting system you can safely call fdisk, mke2fs and grub on the virtual harddisk for profit.
When you're done, make a backup of your fresh image so you don't have to boot a virtual linux on each build cycle.
Re: GRUB install into .img file's MBR
Posted: Thu May 17, 2012 5:06 am
by egos
Combuster wrote:From what I see, you are trying to create an ext2 formatted harddisk image, after which you want to install grub on it, no?
Right.
To install legacy GRUB into MBR by hand follow the steps from my last post
here. It's possible to make stage1+stage1_5 image and patch it, then to put this image at the start of the disk image, then to write partition table (i.e. to create partitions) and so on.
Re: GRUB install into .img file's MBR
Posted: Fri May 18, 2012 11:54 am
by 0x53h
...go find yourself an existing linux install...
That feels like cheating.
...install legacy GRUB into MBR by hand...
That would probably work. However, I couldn't find where to download grub legacy from gnu.org and patching the assembly in the MBR seems extreme. Though after a second glance at the linked post I see that patching shouldn't be necessary in my situation.
I got it figured out somewhere in between the two fine suggestions, but using LILO instead. You can create a lilo config instructing lilo to install an MBR onto a loop device (/dev/loop0), but when booting to look at /dev/hda1 as the root. I'd like offer the instructions for doing this, but in testing LILO loads and the kernel boots but init still doesn't execute. I've still got something not quite right.
Maybe you guys could help me further, but it looks like yous are more into the development of your own OS, and not so much the piecing together of your own Linux. I'm going to explore your forum a little further now. I was just so excited to see someone stuck in the same place as myself I had to register/post. Any sage advise would be much appreciated.
Re: GRUB install into .img file's MBR
Posted: Fri May 18, 2012 2:25 pm
by Combuster
0x53h wrote:...go find yourself an existing linux install...
That feels like cheating.
Ever wondered how the typical kernel developers on a windows machine get to use grub? Making good use of someone else's work is nothing to be ashamed of.
Re: GRUB install into .img file's MBR
Posted: Sat May 19, 2012 10:08 am
by egos
0x53h wrote:That would probably work. However, I couldn't find where to download grub legacy from gnu.org and patching the assembly in the MBR seems extreme.
It's that GRUB installer does. GRUB 2 is installed in same way, but it doesn't use stage1_5 so it is required to load stage2 directly by stage1 that is more complex because you should patch stage2 (instead stage1_5) usually located in the partition with file system.
Re: GRUB install into .img file's MBR
Posted: Sun May 20, 2012 9:57 pm
by kammce
Oh, wow, I was hoping for this result, whilst I was gone.
It is nice to see the replies and the fact that I am not the only one with this issue. To bad I will not be able to work on my OS until 3 weeks from now.
keep it up guys, I am sure we will find our answer... And I guess using a small Linux kernel to start things off is not totally bad, but it would take away from making an OS from scratch. But after I make a basic kernel and interface for my OS, I will make OS from a hacked(by myself) version of Ubuntu 10.04LTS in order to make an Ubuntu close OS built just for me.
Lastly, everything that I have tried has failed. All I end up doing is destroying my .img's first partition