I need to create filesystem with just one partition from nothing (/dev/zero). I tried this sequence of commands:

dd if=/dev/zero of=mountedImage.img bs=512 count=131072
fdisk mountedImage.img

Basically, I need to create 64MB image file filled with zeroes. Then I use fdisk to create new partition for new filesystem, starting at sector 2048 and using all remaining sectors. But now I'm hitting problems. If I set up a loop device and format it using mkfs -t vfat, partition table is overwritten and filesystem is placed to disk. I don't need whole disk formatted with FAT32, I just need my primary partition to be so.

Does anybody know how can I format only one partition of raw disk image, not whole image? Please help.

  • @jokerdino Oh, I see: Unix & Linux version. That's a good reason, but please leave the link so that visitors know what's going on. And I see that I argued to leave the crosspost open because there are different considerations on different distributions (even Ubuntu and Debian differ IIRC). Thanks for reopening. – Gilles Oct 2 '13 at 14:40
  • @Gilles Yes, I actually was on two minds when closing it. I think it is better if this question stays open anyway. Cheers. – jokerdino Oct 2 '13 at 14:44

One solution which doesn't require root privileges is to create a file with the size of the partition, call mkfs on that file, then copy the file into place.

truncate -s 64512k part1.vfat
/sbin/mkfs.vfat part1.vfat
dd if=part1.vfat of=mountedImage.img bs=1k skip=2048

Alternatively, with root privileges, you can set up a loop device that gives you access to the image as a device. Since you want to access only part of the image, you'll have to indicate that explicitly.

sudo losetup -o 2048k --sizelimit 64512k đev/loop0 mountedImage.img
sudo mkfs.vfat /dev/loop0
sudo losetup -d /dev/loop0

To work with more comfortable tools, you need root privileges and a reboot. The loop module can provide access to partitions, but this is disabled by default. To enable this access, edit /etc/default/grub and change the line GRUB_CMDLINE_LINUX="" to


If there were other options there already, leave them, of course; they need to be separated by spaces and the whole option string must be inside double quotes, e.g. GRUB_CMDLINE_LINUX="option=value anotheroption loop.max_part=15".

Now, after you've partitioned the disk image, run

sudo losetup /dev/loop0 mountedImage.img
sudo mkfs.vfat /dev/loop0p1

Run losetup -d /dev/loop0 when you've finished working with that image.


Filesystem image files aren't normally partitioned unless they're whole-disk backups or if they're used with virtual machines (QEMU, etc.). For your purposes, you probably want to just create the filesystem directly on the image file. You can then mount it:

sudo mkdosfs mountedImage.img
sudo mount -o loop mountedImage.img /mnt

If you want to subsequently write this image to a USB flash drive or the like, you can write it to a partition:

sudo dd if=mountedImage.img of=/dev/sdc1
  • Well, but I need it to be partitioned as I'm working with Bochs/VirtualBox. – user35443 Aug 18 '13 at 8:57
  • In that case, the easiest solution is to do the partitioning and filesystem creation from within Bochs or VirtualBox. (It is possible to do it outside of the virtual machine environment, but that involves extra work.) – Rod Smith Aug 18 '13 at 20:09


sudo apt-get install kpartx
losetup -fs my.raw
sudo kpartx -a my.img
ls /dev/mapper



where loop0p1 is the first partition, so we can do:

mkdir -p d
sudo mount /dev/mapper/loop0p1 d

Advantage of this method: works on Ubuntu 14.04 without rebooting.

losetup 2.21 -P option

losetup -P -f --show my.img

Creates one /dev/loopXpY per partition.

Advantage: executable pre-installed in many distros (util-linux package).

Disadvantage: quite recent option, not present in Ubuntu 14.04.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.