How to format a partition inside of an img file?
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 strangely mount
is).
Here is output from fdisk -lu binary.img
:
Disk binary.img: 400 MiB, 419430400 bytes, 819200 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes ... Device Boot Start End Sectors Size Id Type binary.img1 2048 819199 817152 399M 83 Linux
To access the partition you've created you have a couple of choices
The explicit route
losetup --offset $((512*2048)) --sizelimit $((512*817152)) --show --find binary.img /dev/loop0
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:mkfs -t ext4 -L img1 /dev/loop0 mkdir -p /mnt/img1 mount /dev/loop0 /mnt/img1 ... umount /mnt/img1 losetup -d /dev/loop0
The implicit route
losetup --partscan --show --find binary.img /dev/loop0
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:mkfs -t ext4 -L img1 /dev/loop0p1 mkdir -p /mnt/img1 mount /dev/loop0p1 /mnt/img1 ... umount /mnt/img1 losetup -d /dev/loop0
There is another way to do this in general, use kpartx
(not kde related)
sudo kpartx -a binary.img
and now you should have all partition devices defined under /dev/mapper
as loop0p1, loop0p2, ...
and then
sudo mkfs.ext4 /dev/mapper/loop0p1
Optionnaly, when you are done, you can run also
sudo kpartx -d binary.img
to get rid of the loop0p? deivce
I don't know why it looks for
binary.img1
(… and later for binary.img2
buried in the commentary.)
That is because the tools are expecting the filenames to follow a specific pattern. That pattern is the one used by device files for actual discs and disc volumes on your system, namely:
- A device file encompassing the whole disc is named
sda
(or something else). This is whatfdisk
expects to make use of. - Device files for individual slices of the disc, described by its partitioning, are named
sda1
,sda2
,sda3
, and so forth. This is what tools such asgparted
expect to make use of when they tellmkfs
to do things on individual disc volumes.
Of course, ordinary files don't overlap in the manner that disc devices files do. The discussions involving the loopback filesystem that you have seen are all about taking a single whole-disc image file and using loopback to create the 1
, 2
, 3
, and so forth files that reflect the individual slices within it, once the desired partition layout has been written to the partition table.