Formatting QEMU HDD image as FAT

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
codyd51
Member
Member
Posts: 77
Joined: Fri May 20, 2016 2:29 pm
Location: London, UK
GitHub: https://github.com/codyd51
Contact:

Formatting QEMU HDD image as FAT

Post by codyd51 »

Hello,

I'm emulating my OS in QEMU and want to test my ATA driver. I created a virtual hard disk image with

Code: Select all

qemu-img create -f raw drive.img 2G
I would like to format this drive as FAT. Is there a utility to format a QEMU virtual drive as FAT, or am I expected to hand-write the MBR, boot record, etc. myself?

Thanks!
mallard
Member
Member
Posts: 280
Joined: Tue May 13, 2014 3:02 am
Location: Private, UK

Re: Formatting QEMU HDD image as FAT

Post by mallard »

You can use GNU parted to create the MBR and partition table ("parted drive.img").

Formatting it using native Linux tools will require attaching the image as block device using "kpartx" ("kpartx -a -v drive.img") and then doing something like "mkfs.vfat /dev/mapper/loop0p1". Since this requires root access, I'd suggest making a (possibly compressed) copy of the "empty" image at this point to use as a starting point in future (especially if you're "creating" the image as part of your build system, using root access as part of a build is a bad idea). mtools can be used to copy data to/from a FAT filesystem in a disk image without root access, but unfortunately can't be used to create one.
Image
Post Reply