You can create a single sparse disk image containing multiple EXT4 partitions by allocating a large file with truncate, defining partition boundaries with fdisk, mapping those partitions to loop devices using losetup, and formatting each slice independently with mkfs.ext4. This approach yields one cohesive binary file that behaves like a physical hard drive.
Allocating the Sparse Image File
The foundation of this process is a single file that reserves space on your host filesystem without immediately writing data to the disk blocks. This is known as a sparse file. Using thetruncate command allows you to specify the total capacity of the virtual disk instantly. For example, creating a 10-gigabyte image takes less than a second regardless of your current disk speed because no actual data is written yet.
You should choose a size that accommodates all your intended partitions plus some overhead for bootloaders or metadata. A common setup involves a small boot partition and a larger root partition. To create a 10GB sparse file named disk.img, run:
truncate -s 10G disk.img
Verify the creation with ls -lh disk.img. You will see the reported size as 10G, but the actual disk usage (shown in the last column) might be significantly lower, often just a few kilobytes. This confirms the file is sparse. If you were to write data to this file later, the kernel would allocate blocks dynamically. This efficiency is crucial when dealing with large images where most space remains empty initially.
Defining Partition Layouts
A raw image file does not have a partition table until you explicitly define one. You must use a partitioning tool to carve the single file into distinct logical sections. The standard utility for this isfdisk. Because you are working with a file rather than a physical block device, you pass the filename directly to the command.
Start fdisk on your image:
sudo fdisk disk.img
Inside the interactive prompt, press n to add a new partition. Accept the default first sector, then specify the size for the first partition. For a boot partition, a size of +512M is usually sufficient. Press p to print the table and verify the start and end sectors. Then, create a second partition for the root filesystem by pressing n again. Accept the defaults to use the remaining space. Finally, press w to write the changes and exit.
It is important to note that these commands only modify the partition table metadata within the file. No filesystem structures are created yet. The image now contains two "partitions," but they are just ranges of bytes. To interact with them as filesystems, you need to map these ranges to kernel loop devices.
Mapping Partitions to Loop Devices
Linux provides loop devices that allow files to be accessed as block devices. However, since our image has multiple partitions, we cannot simply uselosetup on the whole file. We need to tell the kernel exactly which offset and size correspond to each partition.
First, identify the offsets created by fdisk. You can do this by running sfdisk -d disk.img. This outputs a script-like format showing the start sector and size for each partition. Calculate the byte offset by multiplying the start sector by the sector size (usually 512 bytes).
For example, if the first partition starts at sector 2048, the offset is 2048 * 512 = 1048576 bytes. Use losetup with the -o (offset) and --sizelimit flags to attach specific slices:
sudo losetup -f --show -o 1048576 --sizelimit 536870912 disk.img
This command attaches the first 512MB slice to the next available loop device, likely /dev/loop0. Repeat this process for the second partition, adjusting the offset and size accordingly. You might get /dev/loop1. Verify the mappings with losetup -l. Now, /dev/loop0 represents the boot partition, and /dev/loop1 represents the root partition.
Formatting with EXT4
With the partitions mapped to separate loop devices, you can treat them exactly like physical drives. Usemkfs.ext4 to create the filesystem on each device.
Format the boot partition:
sudo mkfs.ext4 /dev/loop0
Format the root partition:
sudo mkfs.ext4 /dev/loop1
These commands initialize the superblocks, inode tables, and block groups for each partition. The resulting disk.img now contains a valid MBR partition table and two distinct EXT4 filesystems. You can detach the loop devices with sudo losetup -d /dev/loop0 and sudo losetup -d /dev/loop1 when finished.
This method ensures that tools expecting a single disk image, such as QEMU or KVM, can boot from it seamlessly. It also simplifies backup procedures since you only manage one file instead of multiple scattered partition images. For more information on maintaining ext4 integrity, see Recovering inode connection on ext4 fs.
Frequently Asked Questions
Can I resize the partitions after creation?
Yes, but it requires careful manipulation. You must remap the loop devices with updated offsets and sizes, then use resize2fs on the mapped device before resizing the partition table in fdisk. Always back up the image first.
How do I mount these partitions on my host system?
Use mount with the -o loop,offset=... option. Specify the exact byte offset and sizelimit in the mount command to access the contents of a specific partition within the single image file.
What happens if I lose power during formatting?
If power is lost while mkfs.ext4 is writing, the filesystem may become corrupted. You can attempt to repair it using e2fsck on the mapped loop device. See Fixing EXT4 Filesystem Superblock Invalid Error After lvextend and Power Failure for details on superblock issues.
Is there a limit to how many partitions I can create?
The primary limit is defined by the partition table type. With MBR, you are limited to four primary partitions. GPT supports up to 128 partitions. Ensure you use gdisk instead of fdisk if you need more than four partitions.
Why is my sparse image taking up full disk space?
If you copy the image using cp without preserving sparse attributes, the zeros may be filled with actual data blocks. Use cp --sparse=always or rsync to maintain the sparse nature of the file.