I'm trying to create disk device in a file with:

dd if=/dev/zero of=file.img bs=516096 count=1000
sudo losetup /dev/loop0 file.img
(echo n; echo p; echo 1; echo ""; echo ""; echo w;) | sudo fdisk -u -C1000 -S63 -H16 file.img
sudo mke2fs -b1024 /dev/loop0 503968

Thank i mount it with:

sudo mkdir /mnt/fcd
sudo mount -t ext2 /dev/loop0 /mnt/fcd

and writing self-written bootloader:

sudo dd if=loader.bin of=file.img bs=512 count=1 conv=notrunc

and umount it with:

sudo umount /dev/loop0
sudo losetup -d /dev/loop0

I have two questions:

1.I'm getting following output in fdisk:

Using default response p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-1007999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1007999, default 1007999): 

Why is first sector starts from 2048 and not from 0? Is 0-2048 for MBR in ext2 or for something else?

2.After my disk created, i execute:

fdisk -l file.img

And it's output is:

Disk file.img: 516 MB, 516096000 bytes
255 heads, 63 sectors/track, 62 cylinders, total 1008000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

    Device Boot      Start         End      Blocks   Id  System

Why there is no one partition?

Thank you.


1.- Originally, fdisk created partitions trying to make them aligned to cylinder boundaries, leaving the first cylinder on disk free, as it would be used for the MBR, patition table and other stuff. This way, the first partition usually started on block 63 (each block being 512 bytes). The fdisk from distributions like RedHat 6.x, still works this way, but warns you about it and lets you choose the non-DOS compatible schema.

Newer fdisk versions, create partitions aligned to MB boundaries, to ensure partitions are page aligned, as this makes a huge impact on performance (if you're curious about this, you can read more about on my blog: http://sinrega.org/?p=14 and http://sinrega.org/?p=13). 2048 is 1 MB (2048*512).

2.- You're overwriting the partition table when you create the ext2 filesystem on the file backed device. After creating the partition, you should attach another loop device starting at the same block as the partition you've created. In your case, as the partition starts at 2048, the offset should be 1048576 (2048 * 512):

losetup -o 1048576 /dev/loop1 file.img
mkfs.ext2 /dev/loop1
mount /dev/loop1 /mnt
# Do whatever you want with the partition here
umount /mnt
losetup -d /dev/loop1
losetup -d /dev/loop0

This should do the trick.

  • Thank you for answer, but if i will not do fdisk, what will be if i execute mkfs.ext2 /dev/loop0? – 0xAX Sep 27 '14 at 12:23

sfdisk + mke2fs

It is possible to do that with those two tools combined + some careful offset calculations.

This can also be done fully without sudo.

Here I describe a minimal runnable example of such: https://stackoverflow.com/questions/10949169/how-to-create-a-multi-partition-sd-image-without-root-privileges/52850819#52850819

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.