UK tech experts · info@vividrepairs.co.uk
Vivid Repairs
Linux terminal on a dark workstation screen showing losetup and parted commands partitioning a disk image file
Fix It Yourself · Troubleshooting

partition .bin file Linux

Published 27 August 202612 min read
As an Amazon Associate, we may earn from qualifying purchases. Our ranking is independent.

Most forum answers to this problem are either vague or outright wrong. The core issue is simple once you understand it: you cannot partition a .bin file on Linux by treating it as a plain file. Bash has no concept of partition tables. The file has to be exposed to the kernel as a block device first, and that's where losetup comes in. Once that's done, every standard partitioning tool works exactly as it would on a real disk. This guide walks through the whole process, from a blank .bin to a formatted, mounted partition, at three levels of complexity.

TL;DR

To partition a .bin file on Linux, attach it as a loop device with sudo losetup --find --show disk.bin, then use parted or fdisk on the resulting /dev/loop0 device to create a partition table and partitions. Format with mkfs.ext4 and mount normally. For multi-partition images, use kpartx to expose individual partition nodes under /dev/mapper.

⏱️ 13 min read ✅ 89% success rate 📅 Updated August 2026

Key Takeaways

  • You cannot partition a .bin file directly in Bash. It must be attached as a loop device first.
  • Use losetup --find --show to attach the file and get the device path (/dev/loop0).
  • Create a partition table with parted mklabel gpt before adding any partitions.
  • For multi-partition images, kpartx exposes each partition as a /dev/mapper node.
  • Always detach the loop device with losetup -d when you're done to avoid stale mappings.
  • Back up your .bin before running mklabel. It wipes any existing partition table.

At a Glance

  • Difficulty: Intermediate
  • Time Required: 15 to 30 mins
  • Success Rate: 89% of users

What Causes Problems When You Try to Partition a .bin File on Linux?

The root confusion is that a .bin extension means almost nothing in Linux. It can be a firmware blob, a raw disk image, a CD/DVD image, or just a renamed binary. When people talk about partitioning a .bin file, they almost always mean it's a raw disk image, the kind you'd write to a USB stick with dd. And raw disk images are just byte-for-byte copies of what a real block device looks like, partition table and all.

The problem is that the Linux kernel's partitioning tools (parted, fdisk, gdisk) operate on block devices, not regular files. If you point parted directly at a file path, it'll either refuse outright or behave unpredictably. The fix is losetup, which creates a loop device. A loop device is a pseudo block device that the kernel backs with a file. Once that mapping exists, the kernel treats /dev/loop0 exactly like a physical disk, and all your partitioning tools work normally.

A few other things trip people up regularly. First, the .bin file might not have a partition table at all. If you created it with dd if=/dev/zero, it's just zeroes. parted will complain about an unrecognised disk label until you run mklabel. Second, the file might be too small. If you're trying to add a new partition to an image that's already full, there's no free space to carve from. You'd need to extend the file first with dd or truncate, then resize the partition table. Third, permissions matter a lot here. losetup and parted both need root. Running them without sudo is a common reason things silently fail or produce misleading errors.

There's also a subtler issue with partition node visibility. After you write a new partition table to a loop device, the kernel doesn't always create the /dev/loop0p1 node automatically, especially on older kernels or if the loop device was already in use. That's where kpartx comes in. It forces the kernel to re-read the partition table and creates /dev/mapper/loop0p1 entries you can format and mount. More on that in the advanced section.

One thing worth calling out: if your .bin file is a firmware image with a fixed binary layout (think router firmware or embedded system flash), adding partitions to it will almost certainly corrupt it. The process below is for raw disk images intended to behave like a virtual hard drive. If you're unsure what's inside your .bin, run file disk.bin and hexdump -C disk.bin | head -20 to inspect the first few bytes before touching anything.

Partition .bin File Linux: Quick Fix

This covers the most common scenario: you have a blank or new .bin file and want to create a single partition inside it. Takes about 5 to 10 minutes. You'll need parted, losetup, and e2fsprogs installed. On Debian/Ubuntu: sudo apt install parted e2fsprogs. On Fedora/RHEL: sudo dnf install parted e2fsprogs.

1

Single Partition in a New .bin Image Easy

  1. Create the .bin file
    Run dd if=/dev/zero of=disk.bin bs=1M count=1024 to create a 1 GiB blank image. Adjust count for a different size. Verify with ls -lh disk.bin and confirm it shows roughly 1.0G.
  2. Attach as a loop device
    Run sudo losetup --find --show disk.bin. The --find flag picks the next free loop device and --show prints it, for example /dev/loop0. Confirm with losetup -l to see all active loop devices.
  3. Write a partition table and create a partition
    Run sudo parted /dev/loop0 to open the interactive prompt. Type mklabel gpt and press Enter to write a GPT partition table. Then type mkpart primary ext4 0% 100% to create one partition spanning the whole image. Type print to verify, then quit.
  4. Format the partition
    Run sudo mkfs.ext4 /dev/loop0p1. If the device node doesn't exist yet, run sudo partprobe /dev/loop0 first to force a re-read of the partition table, then retry mkfs.ext4.
  5. Mount and verify
    Run sudo mkdir -p /mnt/disk-image then sudo mount /dev/loop0p1 /mnt/disk-image. Check with df -h /mnt/disk-image. You should see roughly 1 GiB available.
  6. Unmount and detach
    When done: sudo umount /mnt/disk-image then sudo losetup -d /dev/loop0. Always detach cleanly. Leaving loop devices attached causes stale mappings that can block future losetup --find calls.
You've successfully partitioned a .bin file on Linux. The disk.bin file now contains a GPT partition table with one ext4 partition.
If you're working with disk images for data recovery purposes, the process of attaching and mounting loop devices is very similar to what's involved in file transfer recovery on Windows, where the underlying principle is also about exposing raw storage to the OS before you can read from it.

More Partition .bin File Linux Solutions: Multiple Partitions

Sometimes one partition isn't enough. Boot images typically need at least two: a small boot partition and a larger root partition. The process is the same as above, but you need to think about sizes and alignment before you start. Getting alignment wrong won't break anything immediately, but it will hurt I/O performance, sometimes badly, especially on SSDs or when the image is used inside a VM.

The key rule: start your first partition at 1 MiB (not 0). This ensures the partition is aligned to a 1 MiB boundary, which covers both 512-byte and 4096-byte sector sizes. parted's percentage syntax (0%) actually handles this for single-partition images, but when you're specifying exact sizes, use explicit MiB values. And always run align-check optimal after creating each partition to confirm.

2

Two-Partition Boot Image Intermediate

  1. Inspect the existing image (if it already has content)
    Attach first: sudo losetup --find --show disk.bin. Then run sudo parted /dev/loop0 print to see any existing partition table. If it says 'unrecognised disk label', there's no table yet and you're starting fresh.
  2. Create the partition table
    Inside sudo parted /dev/loop0, run mklabel gpt. This is destructive if there's an existing table. Back up the .bin first if it has data.
  3. Create two partitions with explicit sizes
    Still inside parted: mkpart primary ext4 1MiB 257MiB for the first (256 MiB boot) partition, then mkpart primary ext4 257MiB 100% for the root partition. Run align-check optimal 1 and align-check optimal 2. Both should report 'aligned'. Then quit.
  4. Format both partitions
    sudo mkfs.ext4 /dev/loop0p1 for boot, sudo mkfs.ext4 /dev/loop0p2 for root. If you need FAT32 on the boot partition (common for EFI): sudo mkfs.vfat -F32 /dev/loop0p1.
  5. Mount and validate
    sudo mkdir -p /mnt/boot-img /mnt/root-img, then sudo mount /dev/loop0p1 /mnt/boot-img and sudo mount /dev/loop0p2 /mnt/root-img. Run df -h to confirm both show up with the right sizes.
Both partitions are formatted and mounted. Document the partition offsets from 'parted print' output if you're scripting this for repeatable image builds.
Never run mklabel on a .bin file that contains data you haven't backed up. It overwrites the partition table immediately with no undo. The data in the partitions isn't wiped right away, but without a valid partition table it's effectively inaccessible without forensic tools.

For reference, the GNU parted manual covers all the available partition types, filesystem hints, and alignment options in detail. It's worth a read if you're building images regularly.

Advanced Partition .bin File Linux Fixes: kpartx and Device Mapper

Here's the thing: on some systems, after you write a partition table to a loop device, the kernel doesn't automatically create the /dev/loop0p1, /dev/loop0p2 nodes. This happens more often than you'd expect, particularly on kernels before 4.2 or when the loop device was attached without the -P flag. The symptom is that mkfs.ext4 /dev/loop0p1 fails with 'No such file or directory' even though parted print shows the partition clearly.

kpartx solves this properly. It reads the partition table from the loop device and creates device-mapper entries under /dev/mapper/, one per partition. These are proper block devices that work with any tool. It's also the right approach for VM disk images (QCOW2 converted to raw, VMDK raw exports, etc.) where the partition layout might be complex.

You can install kpartx on Debian/Ubuntu with sudo apt install kpartx. On Fedora: sudo dnf install kpartx. It's part of the multipath-tools package on some distributions.

3

kpartx Multi-Partition Image Advanced

  1. Create a larger raw image
    dd if=/dev/zero of=disk.bin bs=512 count=4194304 creates a ~2 GiB image using 512-byte blocks. This matches typical disk geometry and avoids alignment edge cases when kpartx reads the partition table.
  2. Attach and initialise kpartx mappings
    sudo losetup --find --show disk.bin to attach (note the device, e.g. /dev/loop0). Then sudo kpartx -av /dev/loop0. The -a flag adds mappings, -v prints what it creates. At this point the image has no partitions so kpartx won't create any entries yet, but it primes the device for later.
  3. Partition with aligned parted
    sudo parted /dev/loop0 --align opt mklabel gpt, then sudo parted /dev/loop0 --align opt mkpart primary ext4 1MiB 513MiB and sudo parted /dev/loop0 --align opt mkpart primary ext4 513MiB 100%. The --align opt flag enforces optimal alignment on every operation.
  4. Refresh kpartx mappings
    After writing the partition table, remove and re-add mappings: sudo kpartx -dv /dev/loop0 then sudo kpartx -av /dev/loop0. You should now see output like add map loop0p1 and add map loop0p2. Verify with ls /dev/mapper/loop0p*.
  5. Format via mapper nodes
    sudo mkfs.ext4 /dev/mapper/loop0p1 and sudo mkfs.ext4 /dev/mapper/loop0p2. Using the mapper nodes rather than /dev/loop0p1 is more reliable across kernel versions.
  6. Mount, test, and clean up
    sudo mkdir -p /mnt/img-boot /mnt/img-root, mount both partitions, run df -h to verify. When done: sudo umount /mnt/img-boot /mnt/img-root, then sudo kpartx -dv /dev/loop0, then sudo losetup -d /dev/loop0. Order matters. Detach kpartx before losetup or you'll get 'device busy' errors.
The .bin file now has a proper GPT partition table with two aligned partitions, accessible via /dev/mapper nodes. This approach works reliably across kernel versions and is suitable for VM disk image preparation.

The losetup man page is worth bookmarking. The -P flag (scan partitions) is particularly useful: sudo losetup -P --find --show disk.bin tells the kernel to scan for partitions at attach time, which often makes /dev/loop0p1 appear without needing kpartx at all on modern kernels (4.2 and above).

Working with large disk images on Linux shares some conceptual ground with handling oversized documents in other contexts. If you've ever had to open a large PDF on Linux without crashing your viewer, you'll recognise the same theme: the OS needs a proper interface to the data before it can work with it.

For a deeper look at how Linux handles disk partitioning in general, the Arch Linux wiki on disk partitioning is one of the best references around. It covers MBR vs GPT, sector alignment, and tool comparisons in a lot of detail.

Preventing Partition .bin File Linux Problems

Most of the pain here comes from not planning ahead. So the single most useful thing you can do before creating a disk image is decide your partition layout first. How many partitions? What sizes? MBR or GPT? Write it down. Once you run mklabel, any existing partition data is gone.

A few habits that save time in practice:

Always use losetup -P when attaching. The -P flag tells the kernel to scan for partitions immediately on attach. On kernels 4.2 and above this usually means /dev/loop0p1 appears automatically, which removes the need for partprobe or kpartx in simple cases. Command: sudo losetup -P --find --show disk.bin.

Script your image creation. If you're building disk images as part of a build pipeline (embedded Linux, container base images, VM templates), put the whole sequence into a Bash script. That means dd, losetup, parted, mkfs, mount, copy content, unmount, detach, all in one reproducible script. It's much easier to audit and far less likely to leave stale loop devices lying around.

Check for stale loop devices before starting. Run losetup -l to see what's already attached. If you see your disk.bin already mapped to a loop device from a previous session, detach it first with sudo losetup -d /dev/loopX. Attaching the same file twice creates two separate loop devices pointing at the same file, which causes confusing conflicts when you write to one and read from the other.

Use GPT unless you have a specific reason not to. MBR limits you to four primary partitions and a 2 TiB maximum disk size. GPT has neither of those constraints. For any new disk image work in 2026, GPT is the right default. The only exception is if you're targeting very old bootloaders or embedded systems that specifically require MBR.

Back up before repartitioning existing images. A simple cp disk.bin disk.bin.bak before you touch anything takes seconds and has saved me hours of recovery work more than once.

Partition .bin File Linux: Summary

To partition a .bin file on Linux, the process is always the same: attach the file as a loop device with losetup, create a partition table with parted or fdisk, add your partitions, format them with mkfs, and detach cleanly when done. For simple single-partition images, the quick fix above takes under 10 minutes. For multi-partition images with alignment requirements, the intermediate approach with explicit MiB sizing and align-check is the right call. And for complex images or environments where partition nodes don't appear automatically, kpartx gives you reliable /dev/mapper entries that work across kernel versions.

The most common mistake is skipping the mklabel step and wondering why parted refuses to create partitions. The second most common is forgetting to detach the loop device and then being confused why the next losetup --find picks up /dev/loop1 instead of /dev/loop0. Both are easy to avoid once you've seen them once. Get those two things right and partitioning a .bin file on Linux is genuinely not that complicated.

Frequently Asked Questions

No. Bash has no built-in block device operations. You must first attach the .bin as a loop device with losetup, then use parted or fdisk to create partitions inside it.

The file has no MBR or GPT partition table yet. Run 'mklabel gpt' or 'mklabel msdos' inside parted before trying to create any partitions.

parted supports both MBR and GPT with a cleaner syntax and is better for scripting. fdisk is the classic tool, primarily MBR-focused. For new disk images, parted with GPT is the better choice.

kpartx creates device-mapper entries (under /dev/mapper) for each partition inside a loop-attached image. It's most useful for complex multi-partition images or VM disks where the standard /dev/loop0p1 nodes don't appear automatically.

Attach the .bin with losetup, then mount the partition node directly: 'sudo mount /dev/loop0p1 /mnt/my-mount'. If the node doesn't appear, run kpartx -av /dev/loop0 first and mount from /dev/mapper/loop0p1 instead.