I created an img
file via the following command:
dd if=/dev/zero bs=2M count=200 > binary.img
It's just a file with zeroes, but I can use it in fdisk
and create a partition table:
# fdisk binary.img
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x51707f21.
Command (m for help): p
Disk binary.img: 400 MiB, 419430400 bytes, 819200 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
Disklabel type: dos
Disk identifier: 0x51707f21
and, let's say, one partition:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-819199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-819199, default 819199):
Created a new partition 1 of type 'Linux' and of size 399 MiB.
Command (m for help): w
The partition table has been altered.
Syncing disks.
When I check the partition table, I get the following result:
Command (m for help): p
Disk binary.img: 400 MiB, 419430400 bytes, 819200 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
Disklabel type: dos
Disk identifier: 0x7f3a8a6a
Device Boot Start End Sectors Size Id Type
binary.img1 2048 819199 817152 399M 83 Linux
So the partition exists. When I try to format this partition via gparted, I get the following error:
I don't know why it looks for binary.img1
, and I have no idea how to format the partition from command live.
Does anyone know how to format it using ext4 filesystem?
Best Answer
You can access the disk image and its individual partitions via the loopback feature. You have already discovered that some disk utilities will operate (reasonably) happily on disk images. However,
mkfs
is not one of them (but strangelymount
is).Here is output from
fdisk -lu binary.img
:To access the partition you've created you have a couple of choices
The explicit route
The output
/dev/loop0
is the name of the loop device that has been allocated. The--offset
parameter is just the partition's offset (Start
) multiplied by the sector size (512
). Whereas--sizelimit
is the size of the partition, and you can calculate it in the following way: End-Start+1, which is 819199-2048+1=817152 , and that number also has to be multiplied by the sector size.You can then use
/dev/loop0
as your reference to the partition:The implicit route
The output
/dev/loop0
is the name of the primary loop device that has been allocated. In addition, the--partscan
option tells the kernel to scan the device for a partition table and assign subsidiary loop devices automatically. In your case with the one partition you also get/dev/loop0p1
, which you can then use as your reference to the partition: