I generate an ext4 partition image that contains a minimal Debian file system, then write that partition image into a full disk image. I am looking for the fastest way to do the write.
Consider a minimal test case:
truncate -s 30GB disk.img
parted -s disk.img mklabel gpt
parted -s disk.img mkpart small ext4 1MiB 301MiB
parted -s disk.img mkpart root ext4 301MiB 100%
truncate -s 20GB rootfs.img
truncate -s 300MB small.img
mkfs.ext4 rootfs.img
mkfs.ext4 small.img
# In reality, here we might put something on the partitions, like:
# mount rootfs.img /mnt/root; debootstrap /mnt/root
# The part we want to optimize:
dd if=small.img of=disk.img seek=1 bs=1M conv=notrunc
dd if=rootfs.img of=disk.img seek=301 bs=1M conv=notrunc
The rootfs.img
and small.img
files generated in the code above contain just an empty ext4 filesystem and are thus very small (~67MB for a 5G filesystem, 120MB for a 30G). Despite this, dd
takes a long time to copy them over, even with conv=sparse
. Presumably this is because dd
looks at the full 20GB and not just the used area as printed by du -sh
. Plus, using conv=sparse
is a bug if the target is not zeroed out.
What is the fastest way to copy a sparse partition image to the middle of a disk image? An ext4-only solution is sufficient, but filesystem-independent methods are even better.