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.
Key Takeaways
- You cannot partition a .bin file directly in Bash. It must be attached as a loop device first.
- Use
losetup --find --showto attach the file and get the device path (/dev/loop0). - Create a partition table with
parted mklabel gptbefore adding any partitions. - For multi-partition images,
kpartxexposes each partition as a/dev/mappernode. - Always detach the loop device with
losetup -dwhen 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.
Single Partition in a New .bin Image Easy
- Create the .bin file
Rundd if=/dev/zero of=disk.bin bs=1M count=1024to create a 1 GiB blank image. Adjustcountfor a different size. Verify withls -lh disk.binand confirm it shows roughly 1.0G. - Attach as a loop device
Runsudo losetup --find --show disk.bin. The--findflag picks the next free loop device and--showprints it, for example/dev/loop0. Confirm withlosetup -lto see all active loop devices. - Write a partition table and create a partition
Runsudo parted /dev/loop0to open the interactive prompt. Typemklabel gptand press Enter to write a GPT partition table. Then typemkpart primary ext4 0% 100%to create one partition spanning the whole image. Typeprintto verify, thenquit. - Format the partition
Runsudo mkfs.ext4 /dev/loop0p1. If the device node doesn't exist yet, runsudo partprobe /dev/loop0first to force a re-read of the partition table, then retrymkfs.ext4. - Mount and verify
Runsudo mkdir -p /mnt/disk-imagethensudo mount /dev/loop0p1 /mnt/disk-image. Check withdf -h /mnt/disk-image. You should see roughly 1 GiB available. - Unmount and detach
When done:sudo umount /mnt/disk-imagethensudo losetup -d /dev/loop0. Always detach cleanly. Leaving loop devices attached causes stale mappings that can block futurelosetup --findcalls.
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.
Two-Partition Boot Image Intermediate
- Inspect the existing image (if it already has content)
Attach first:sudo losetup --find --show disk.bin. Then runsudo parted /dev/loop0 printto see any existing partition table. If it says 'unrecognised disk label', there's no table yet and you're starting fresh. - Create the partition table
Insidesudo parted /dev/loop0, runmklabel gpt. This is destructive if there's an existing table. Back up the .bin first if it has data. - Create two partitions with explicit sizes
Still inside parted:mkpart primary ext4 1MiB 257MiBfor the first (256 MiB boot) partition, thenmkpart primary ext4 257MiB 100%for the root partition. Runalign-check optimal 1andalign-check optimal 2. Both should report 'aligned'. Thenquit. - Format both partitions
sudo mkfs.ext4 /dev/loop0p1for boot,sudo mkfs.ext4 /dev/loop0p2for root. If you need FAT32 on the boot partition (common for EFI):sudo mkfs.vfat -F32 /dev/loop0p1. - Mount and validate
sudo mkdir -p /mnt/boot-img /mnt/root-img, thensudo mount /dev/loop0p1 /mnt/boot-imgandsudo mount /dev/loop0p2 /mnt/root-img. Rundf -hto confirm both show up with the right sizes.
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.
kpartx Multi-Partition Image Advanced
- Create a larger raw image
dd if=/dev/zero of=disk.bin bs=512 count=4194304creates a ~2 GiB image using 512-byte blocks. This matches typical disk geometry and avoids alignment edge cases when kpartx reads the partition table. - Attach and initialise kpartx mappings
sudo losetup --find --show disk.binto attach (note the device, e.g./dev/loop0). Thensudo kpartx -av /dev/loop0. The-aflag adds mappings,-vprints 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. - Partition with aligned parted
sudo parted /dev/loop0 --align opt mklabel gpt, thensudo parted /dev/loop0 --align opt mkpart primary ext4 1MiB 513MiBandsudo parted /dev/loop0 --align opt mkpart primary ext4 513MiB 100%. The--align optflag enforces optimal alignment on every operation. - Refresh kpartx mappings
After writing the partition table, remove and re-add mappings:sudo kpartx -dv /dev/loop0thensudo kpartx -av /dev/loop0. You should now see output likeadd map loop0p1andadd map loop0p2. Verify withls /dev/mapper/loop0p*. - Format via mapper nodes
sudo mkfs.ext4 /dev/mapper/loop0p1andsudo mkfs.ext4 /dev/mapper/loop0p2. Using the mapper nodes rather than/dev/loop0p1is more reliable across kernel versions. - Mount, test, and clean up
sudo mkdir -p /mnt/img-boot /mnt/img-root, mount both partitions, rundf -hto verify. When done:sudo umount /mnt/img-boot /mnt/img-root, thensudo kpartx -dv /dev/loop0, thensudo losetup -d /dev/loop0. Order matters. Detach kpartx before losetup or you'll get 'device busy' errors.
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).
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.
Partitioning .bin disk images involves loop device management, partition table creation, and filesystem formatting, all of which can be done cleanly via remote session. If you're hitting errors with losetup, kpartx mappings, or partition nodes not appearing, our team can walk through it with you live.
Get remote helpPreventing 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.


