I personally prefer to append using dd
.
I'm assuming 512-byte sectors here. There may be a case for 2048-byte sectors, so just swap the numbers in and do the maths.
In each case I'm using a 512MB test file for example:
dd if=/dev/zero of=testfs.img bs=512 count=1M
mkfs.ext4 testfs.img
MBR
Composing the image
I personally prefer to append the first MB (2048 sectors) to its beginning:
dd if=testfs.img skip=2048 bs=512 of=full.img
Finally run fdisk to create the partition table (or copy in your own), I created 1 partition using default values.
Verifying
To verify, create loop partitions and autodetect:
sudo losetup -fP full.img
And run file
on the resultant partitioned loopback device:
sudo file -s /dev/loop2p1
/dev/loop2p1: Linux rev 1.0 ext4 filesystem data, UUID=ae2945fd-54b5-486f-8dd0-9b18d6ae01b4 (extents) (large files) (huge files)
GPT
Composing the image
I personally prefer to append the first MB (2048 sectors, as gdisk will default to this number as it is 1 MB aligned) to its beginning for the beginning MBR and 34 sectors to its end (or 2048 for a full MB) for the end GPT (end sector may be different). Omitting the end GPT may lose you data:
dd if=testfs.img skip=2048 bs=512 of=full.img
dd if=/dev/zero seek=1050624 bs=512 of=full.img count=34
Finally run gdisk to create the partition table (or copy in your own), I created 1 partition using default values.
Verifying
To verify, create loop partitions and autodetect:
sudo losetup -fP full.img
And run file
on the resultant partitioned loopback device:
sudo file -s /dev/loop2p1
/dev/loop2p1: Linux rev 1.0 ext4 filesystem data, UUID=ae2945fd-54b5-486f-8dd0-9b18d6ae01b4 (extents) (large files) (huge files)
This method ensures no guessing, resizing or manually aligning.