8

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:

enter image description here

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?

11

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

  1. 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
    
  2. 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
    
  • @Mikhail curious to see you calculated the partition size when it was already given as part of the fdisk output. – roaima Jun 15 '15 at 13:45
  • 1
    What's wrong with some maths? Besides, it's good to know that you can easily get the right sector number in that way, just in case... – Mikhail Morfikov Jun 15 '15 at 16:20
7

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

  • 1
    Not sure why this doesn't have more votes. IMO it's the best answer...! – Jeremy Davis Jun 2 '17 at 1:25
3

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 what fdisk 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 as gparted expect to make use of when they tell mkfs 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.

0

Though this topic is not directly related, it mentions a lot of the same and related information.

Debian wiki | Raspberry Pi and qemu-user-static

If you cannot use apt to install some of the commands mentioned in this post, try using apt-cache search [package_name]. This may not turn up any results if the command comes from a package of a different name.

For instance, losetup could formerly be installed as losetup using apt install losetup, but it is now part of util-linux in Ubuntu's repository. The way you find out what package acts as a container for another package, you must use the search for the online repository for you Linux distribution. Or, if you must install it from another source, use a Web search engine.

Some packages worth checking out...

util-linux genisoimage dosfstools squashfs-tools fsarchiver xfsprogs reiserfsprogs reiser4progs jfsutils ntfsprogs btrfs-tools

Every Linux distribution also has its own online manpages. Sometimes it is easier to use the manpages than a tutorial. The manual pages will also tell you all the command options and parameters. A tutorial usually will only focus on the ones used.

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.