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 add a new partition for new filesystem (which should finally be FAT32), starting at sector 2048 and using all remaining sectors.

losetup /dev/loop1 mountedImage.img
mkfs -t vfat /dev/loop1

But here I'm hitting problems. If I set up a loop device and format it using mkfs -t vfat, partition table is overwritten and filesystem (FAT32) 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 clarify what you mean by setting up a loop device and post your entire mkfs call. I had to read your post 3 times to understand your problem. While I can't help you, I'm interessted in why you create an image file(!) but still want to leave the first n "sectors" unused.
    – Bananguin
    Commented Aug 17, 2013 at 18:57
  • Ok, edited it. I want to leave first n sectors (what's wrong with sectors?) unused, as I need to place my bootloaders somewhere.
    – user35443
    Commented Aug 18, 2013 at 8:50
  • Crossposted Commented Aug 18, 2013 at 16:05
  • @MichaelMrozek There are considerations that apply to some Linux distributions but not Ubuntu (it matters whether loop is a module or built into the kernel), so I think it is useful for each site to have its version of the question and I voted to reopen. Commented Sep 19, 2013 at 9:27
  • @MichaelMrozek Personally, I feel AskUbuntu is more specific than Unix & Linux, so if a question is cross-posted to both, unless there are specific reasons to close here it's the one on AskUbuntu that should be closed as cross-posted if they were posted close in time to each other, regardless of where it was posted "first" (possibly with a mention of Ubuntu/non-Ubuntu answers here). Here it has an accepted answer, which the one on AskUbuntu does not, which strongly favors keeping this one open IMO. I don't have the rep to vote to close on AskUbuntu but am voting to reopen the question here.
    – user
    Commented Sep 19, 2013 at 9:38

4 Answers 4


If on Linux, when loading the loop module, make sure you pass a max_part option to the module so that the loop devices are partitionable.

Check the current value:

cat /sys/module/loop/parameters/max_part

If it's 0:

modprobe -r loop # unload the module
modprobe loop max_part=31

To make this setting persistent, add the following line to /etc/modprobe.conf or to a file in /etc/modprobe.d if that directory exists on your system:

options loop max_part=31

If modprobe -r loop fails because “Module loop is builtin”, you'll need to add loop.max_part=31 to your kernel command line and reboot. If your bootloader is Grub2, add to it to the value of GRUB_CMDLINE_LINUX in /etc/default/grub and update GRUB using update-grub.

Now, you can create a partitionable loop device:

truncate -s 64M file # no need to fill it with zeros, just make it sparse
fdisk file # create partitions
losetup /dev/loop0 file
mkfs.vfat /dev/loop0p1 # for the first partition.
mount /dev/loop0p1 /mnt/

Unmount after using and detach loop device

umount /mnt
losetup -d /dev/loop0

(note that you need a relatively recent version of Linux).

  • FATAL: Module loop is builtin, can this be solved installing lastest version of ubuntu (I have 12.04)?
    – user35443
    Commented Aug 18, 2013 at 9:17
  • If is builtin just no needed to modprobe, pass the loop.max_part=31 to the kernel command line
    – Alex
    Commented Aug 18, 2013 at 9:26
  • 1
    Ah I see, if you've a recent kernel and you pass the max_part the offset and the sizelimit aren't needed at all
    – Alex
    Commented Aug 18, 2013 at 9:32
  • Hmm, kernel command line, too difficult for me. Still thanks, accept.
    – user35443
    Commented Aug 18, 2013 at 10:01
  • Is it loop.maxpart=31 or loop.max_part=31 on the kernel command line? Commented Oct 4, 2014 at 16:49

losetup /dev/loop0 file -o 1048576 --sizelimit limit

Offset specified should be in bytes (1048576 = 2048 sectors * 512 bytes per sector).

mount -o loop,offset=1048576,sizelimit=limit

For more information see losetup and mount.

  • Hmm, offset, this should work. And what if I had more partitions on it?
    – user35443
    Commented Aug 18, 2013 at 8:58
  • I guess one loop = one partition = one offset.
    – Alex
    Commented Aug 18, 2013 at 9:02
  • uhm, and is there an option to specify also the size, so I could limit it and place there more partitions?
    – user35443
    Commented Aug 18, 2013 at 9:04
  • Sorry my fault to create a new answer with a partial solution, the right answer is from @"Stephane Chazelas", he can just add the offset and sizelimit options (then I'll delete mine)
    – Alex
    Commented Aug 18, 2013 at 9:20
  • No let it be here, or mine +1 will go nowhere.
    – user35443
    Commented Aug 18, 2013 at 9:23

A simple example using ext4 file system and GPT partitioned image.

truncate -s 100MiB mountedImage.img

Create a GPT Table

parted mountedImage.img mklabel gpt

Create Partition P1

parted mountedImage.img mkpart primary ext4 2MiB 30MiB

Create Partition P2

parted mountedImage.img mkpart primary ext4 30MiB 60MiB 

Create Partition P3

parted mountedImage.img mkpart primary ext4 60MiB 99MiB

Say you are using the loop device /dev/loop1 (it's always better to use sudo losetup -f to get a free loop device):

losetup /dev/loop1 mountedImage.img -> should enumerate the partitions to create devices like /dev/loop1px for each partition x represents the partition number.

[use] mkfs.ext4 /dev/loop1px -> to be done on the partition number x

[and not] mkfs.ext4 /dev/loop1 -> will only delete the partition table

Note: The partitions from the image are visible in loop device only if the module is configured for multi partition.

Please refer to the link to my blog, Creating a GPT Partitioned image from Scratch in Linux, for creating partitioned image from scratch using GPT.


The following procedures allow you to mount the partitions of the image to modify them.

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.

losetup -P automation


$ los my.img

$ ls /mnt/loop0p1

$ sudo losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                                                      DIO
/dev/loop1         0      0         0  0 /full/path/to/my.img

$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0


los() (
  dev="$(sudo losetup --show -f -P "$img")"
  echo "$dev"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
    dst="/mnt/$(basename "$part")"
    echo "$dst"
    sudo mkdir -p "$dst"
    sudo mount "$part" "$dst"
losd() (
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  sudo losetup -d "$dev"


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .