Page 1 of 1

Creating a Disk Image with a FAT32 partition in Ubuntu

Posted: Sat Jun 29, 2013 8:33 am
by singerng
I'm trying to create a blank disk image with a single FAT32 partition in Ubuntu (to test my bootloader). None of the instructions I can find online seem to work. It fails when trying to mount the image. Here's the code I use to create it:

Code: Select all

dd if=/dev/zero of=os.img bs=516096c count=80
fdisk -u -C80 -S63 -H16 os.img
o, n, a, t, c, w
losetup -o 1048576 /dev/loop0 os.img
mkdosfs -F32 /dev/loop0
mount -tvfat /dev/loop0 /mnt/hdd
It doesn't work with mkfs.vfat or mkfs.msdos either.

The error, displayed with dmesg | tail, is "Logical sector size zero."

Re: Creating a Disk Image with a FAT32 partition in Ubuntu

Posted: Sat Jun 29, 2013 9:00 am
by zhiayang
singerng wrote:I'm trying to create a blank disk image with a single FAT32 partition in Ubuntu (to test my bootloader). None of the instructions I can find online seem to work. It fails when trying to mount the image. Here's the code I use to create it:

Code: Select all

dd if=/dev/zero of=os.img bs=516096c count=80
fdisk -u -C80 -S63 -H16 os.img
o, n, a, t, c, w
losetup -o 1048576 /dev/loop0 os.img
mkdosfs -F32 /dev/loop0
mount -tvfat /dev/loop0 /mnt/hdd
It doesn't work with mkfs.vfat or mkfs.msdos either.

The error, displayed with dmesg | tail, is "Logical sector size zero."


If you're doing what I think you're doing, which is to make a HDD image:

Code: Select all

1. Use DD to create a raw disk image.
2. Using fdisk, create the necessary information.
3. Using DD again, use [count] and [skip] on the disk image to create an MBR.img and a fs.img. You'll want the MBR image to be somewhere between 63 and 2047 sectors long. The fs.img should skip over this amount.

4. Create the FS (FAT/EXT whatever) on fs.img.
5. Using cat, [cat mbr.img fs.img > disk.img]


That should work. It works for a GRUB/GRUB2 install, so by any means it should work for your purposes.
Again, this is what works for me, I'm simply trying to offer something useful here.

Re: Creating a Disk Image with a FAT32 partition in Ubuntu

Posted: Sat Jun 29, 2013 9:08 am
by singerng
I'm not exactly clear on what I'm supposed to be doing, but isn't this basically what I did? The loopback device (/dev/loop0) is basically the same as fs.img, isn't it?

Re: Creating a Disk Image with a FAT32 partition in Ubuntu

Posted: Sat Jun 29, 2013 9:36 am
by zhiayang
singerng wrote:I'm not exactly clear on what I'm supposed to be doing, but isn't this basically what I did? The loopback device (/dev/loop0) is basically the same as fs.img, isn't it?
Oh, whoops, I didn't notice the offset flag. In that case... I know that this method definitely works under OS X, so I'm not sure what Linux is doing over here. I have in fact tried this on Ubuntu (12.04) before and it worked to my knowledge...

Re: Creating a Disk Image with a FAT32 partition in Ubuntu

Posted: Sat Jun 29, 2013 9:42 am
by singerng
Well, your technique worked :)... I'm not sure why mine didn't!

Re: Creating a Disk Image with a FAT32 partition in Ubuntu

Posted: Sat Jun 29, 2013 1:00 pm
by zhiayang
singerng wrote:Well, your technique worked :)... I'm not sure why mine didn't!
Glad to be of service :P

Also, your first DD command looked a little weird with the extra 'c', although I do admit I didn't do the math.
It looks like your losetup command uses (2048*512) as an offset -- AFAIK, the first reserved sectors usually stretch to 2047 sectors.
IIRC Windows gives 63 sectors, most Unix utilities do 2047.