dd Copy partition image file into disk image file

1

I am creating a disk image and copy a mbr on it:

dd if=/dev/zero bs=2M count=256 > ./hd.img
dd if=mbr.bin of=hd.img conv=notrunc
sfdisk --force ./hd.img < partitions.sfdisk

where partitions.sfdisk looks like this:

2048,,0x83,*

fdisk -lu hd.img now lists this:

./hd.img1  *     2048 1048575  1046528  511M 83 Linux

with sectors of 512 bytes.

Now I want to format hd.img1 with ext4 without creating a loop device. So I created a second image hd.img1 with the size of 512MB - (2048 x 512) bytes. I executed

mkfs.ext4 hd.img1

and now I want to copy hd.img1 into hd.img on the position of the created partition hd1.img.

Is that even possible? Do I have to dd hd.img1 with an offset (skip) of 2048 x 512?

dd if=hd.img1 of=hd.img skip=2048 bs=512

When I do that it seems that I've overwritten my partition table created with sfdisk so I am obviously doing something wrong.

Stefan

Posted 2017-08-14T06:35:24.080

Reputation: 113

Instead of dd if=/dev/zero ... you can use truncate or fallocate. Check their manuals. – Kamil Maciorowski – 2017-08-14T06:58:07.907

Answers

1

skip skips inside the input. To move your starting position in the output file use seek.

From man dd:

seek=BLOCKS

skip BLOCKS obs-sized blocks at start of output

skip=BLOCKS

skip BLOCKS ibs-sized blocks at start of input

Kamil Maciorowski

Posted 2017-08-14T06:35:24.080

Reputation: 38 429