GRUB, MBR, and Booting From Disk

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
LukeyTheKid
Posts: 17
Joined: Fri Jun 12, 2020 7:53 am
Libera.chat IRC: lukeythekid

GRUB, MBR, and Booting From Disk

Post by LukeyTheKid »

Up to this point, I have been using an ISO produced by grub-mkrescue, and booting from CDROM. But now I am trying to start exploring file system concepts / implementation, and as part of that I wanted to toy around with some FAT filesystem stuff. To that end, I am moving to working off of a disk image.

I followed the Wiki article for producing a disk image (https://wiki.osdev.org/GRUB_2#Disk_image_instructions), but I am having some trouble getting bochs to load the img file; it gets an IDE time out.
I looked at the hex makeup of the img file, and the MBR code is empty after installing grub; the partition entry exists from fdisk operations and it is bootable with value 0x80, but no actual bootloader code -- bytes 0-446 are all zeroed. This seems, uh, problematic -- I assume this is the root of this issue, but I didn't know if maybe that gets filled in at some point by some process I'm not aware of. Am I just missing a step? I should note that grub-install successfully installs a bunch of stuff under /boot/grub and /boot/efi.

bochsrc disk entry (from bximage)

Code: Select all

ata0-master: type=disk, mode=flat, path=myos_disk.img
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

For completeness, here are the exact steps I took:

Create disk image:

Code: Select all

$ dd if=/dev/zero of=myos_disk.img bs=512 count=131072
Format myos_disk.img:

Code: Select all

$ fdisk myos_disk.img
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-131071, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-131071, default 131071): 

Created a new partition 1 of type 'Linux' and of size 63 MiB.

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

Command (m for help): w
The partition table has been altered. Syncing disks.
Setup disk image on 2 loopback devices -- one for setting up GRUB and the MBR, the other for setting up the OS’s filesystem.

Code: Select all

$ sudo losetup /dev/loop0 myos_disk.img
$ sudo losetup /dev/loop1 myos_disk.img -o 1048576
Format the file system partition

Code: Select all

$ sudo mkdosfs -F32 -f 2 /dev/loop1
Mount the newly formatted partition:

Code: Select all

$ sudo mount /dev/loop1 /mnt
Install GRUB on the first device -- NOT THE OS’s formatted partition

Code: Select all

$ sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot" /dev/loop0
Installing for x86_64-efi platform.
Installation finished. No error reported.
$ sync
Copy kernel, GRUB config, and any other desired files to the filesystem

Code: Select all

$ sudo cp isodir/boot/myos.bin /mnt/boot/myos.bin
$ sudo cp isodir/boot/grub/grub.cfg /mnt/boot/grub/grub.cfg
Unmount the partition, and remove the loopback devices

Code: Select all

$ sudo umount /mnt
$ losetup -d /dev/loop0; losetup -d /dev/loop1
Run bochs, which has the disk entry and a boot order starting with disk

Code: Select all

bochs -q

Thanks in advance for any help or suggestions. I have been looking through the forums/wiki grub docs but have not been able to really find anything
Last edited by LukeyTheKid on Wed Dec 02, 2020 9:13 am, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: GRUB, MBR, and Booting From Disk

Post by Octocontrabass »

Code: Select all

Installing for x86_64-efi platform.
Your host OS was booted from 64-bit UEFI, and grub-install thinks that's what you want. Add "--target=i386-pc" to your grub-install command to get the BIOS version, or boot Bochs in UEFI mode.
LukeyTheKid
Posts: 17
Joined: Fri Jun 12, 2020 7:53 am
Libera.chat IRC: lukeythekid

Re: GRUB, MBR, and Booting From Disk

Post by LukeyTheKid »

Argghh, glossed right over that, of course it's a mistake like that -- I might make a note of the flag in the wiki page if that's okay.
Thanks so much for pointing it out, that fixed it. Also thank you for the link, that is nice to know.
Post Reply