dd – Enlarge Each Input Block to Double Size

dd

I found a few examples that claimed this but I could not manage to get dd to convert a file with a given block size to double that block size.

dd if=disk256bytesectors.img of=disk512bytesectors.img cbs=256 ibs=512 obs=512 conv=sync

My disk img is 10 megabytes in size I was expecting a new image of 20 megabytes, but no.

So what I need to happen is each 256 byte block get converted to a 512 byte block with the second half of each 512 byte block being null, zero, space or anything.

The dd manual state conv=sync

pads any input block shorter than ibs to that size with null bytes before conversion and output.

But the input block will never be shorter than itself?!?! So what does that mean?

Anyway how can I do this?

Best Answer

You might be able to use socat which uses a Unix domain socket when you specify exec: for a command. This can be made of type datagram (type=2) to ensure the input to dd is only 256 bytes.

For example, for 2 bytes per datagram (-b2) padded to 4 (bs=4):

$ echo abcdefx | socat -u -b2 - exec:'dd bs=4 conv=sync',type=2 | od -c   
0000000   a   b  \0  \0   c   d  \0  \0   e   f  \0  \0   x  \n  \0  \0

Note, the above use of a pipe for stdin to socat is just for this test. Normally, you should provide the input file directly on stdin so that socat has no trouble reading whole "blocks" of the wanted -b size (i.e. <file socat -u ...).

Related Question