How to read the fat32 bootsector info from image file

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
michael
Posts: 12
Joined: Fri Nov 12, 2021 1:09 am

How to read the fat32 bootsector info from image file

Post by michael »

I'm now trying to read the disk boot sector info. I compiled my kernel into a floppy and load it into qemu using -fda a.img, and I also created another qcow image file called disk.img and load into qemu using -hda disk.img. When I try to get boot sector info from LBA 0x00 I could only get all 0 in that sector. After that I format the qcow image file into fat32 using

Code: Select all

mkfs.vfat -F 32 -v disk.img
and load it into qemu, but qemu then print

Code: Select all

Booting from Hard Disk
This is not a bootable disk. Please insert a bootable floppy and press any key to try again
The parameters I add to qemu is this

Code: Select all

QemuParameter := -cpu Nehalem,+x2apic -m 512 \
	-enable-kvm -D ./log.txt -s -S -fda a.img -smp cores=$(APUNUM) -hda disk.img
a.img is the image file contains a bootloader and kernel

I'm now wondering how could I read the fat32 boot sector info properly
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to read the fat32 bootsector info from image file

Post by Octocontrabass »

michael wrote:I also created another qcow image file called disk.img and load into qemu using -hda disk.img. When I try to get boot sector info from LBA 0x00 I could only get all 0 in that sector.
You created a blank disk image. It does not have a boot sector because it is blank.
michael wrote:After that I format the qcow image file into fat32 using

Code: Select all

mkfs.vfat -F 32 -v disk.img
The mkfs.vfat program doesn't understand qcow. You should use raw disk images instead. Also, hard disks are typically partitioned, but mkfs.vfat does not partition the disk.
michael wrote:but qemu then print

Code: Select all

Booting from Hard Disk
This is not a bootable disk. Please insert a bootable floppy and press any key to try again
This message is displayed when you boot from a disk that has been formatted using mkfs.vfat. By default, QEMU attempts to boot from the hard disk before the floppy disk. If you want to boot from the floppy disk instead, add "-boot order=a" to your QEMU command line.
michael
Posts: 12
Joined: Fri Nov 12, 2021 1:09 am

Re: How to read the fat32 bootsector info from image file

Post by michael »

Octocontrabass wrote: The mkfs.vfat program doesn't understand qcow. You should use raw disk images instead. Also, hard disks are typically partitioned, but mkfs.vfat does not partition the disk.
I've just created a primary partition inside a raw image file, do you know how to format that partition into fat32?
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: How to read the fat32 bootsector info from image file

Post by Ethin »

There are a few ways of doing it (e.g. you could do it by booting a Linux OS and then partition/format it in there), but another good way (on Linux) is to use qemu-nbd:

Code: Select all

modprobe nbd
qemu-nbd -c /dev/nbd0 image_file
Then, interact with /dev/nbd0 like you would a normal disk. Once done:

Code: Select all

qemu-nbd -d /dev/nbd0
This works over the network, too, but its faster on Linux. Don't forget to load the nbd module (or load it at boot). You could also create a loopback device pointing to your image file, but qemu-nbd is significantly easier. It also has the advantage that, by default, it creates 16 NBD devices for you, so you can have up to 16 disks mounted simultaneously. If you need more (or you want more than 16 partitions, the default maximum), you can specify the any or both of the nbds_max or max_part module parameters in the modprobe command.
Post Reply