I have a loopback file that contains a single filesystem, let's call it fs.image. I have another, much larger loopback file, that contains multiple partitions and multiple filesystems, let's call it disk.image.

fs.image is exactly the same size as one of the partitions in disk.image. I would like to copy the data from it into the disk.image file into the correct location.

I have a script that computes the proper offset, and attempts to do this with dd. I assumed this would work because it works with a block device, but this isn't the case. disk.image is truncated at the position immediately following the data that was just written into it.

Is there any way to keep dd from truncating the file?

fs.image is compressed as fs.image.bz2, and the command I'm executing is like this:

bzcat fs.image.bz2 | dd of=disk.image bs=4M seek=$OFFSET

where $OFFSET is my computed offset.

I've thought about other ways of accomplishing this, but this seems the simplest (if it worked). Once option would be to copy data up to the point of insertion to another file, append the filesystem, and finally append the remainder of the original file. However, this would be slow because I'd be copying a lot of data I don't really need to. I'm open to other options.

up vote 1 down vote accepted

GNU's dd (at least version 8.23) has the following conversion flag

notrunc do not truncate the output file

which does exactly what you want; here is a small example:

$ cat foo
foobar
$ echo -n XX | dd of=foo bs=1c seek=1 conv=notrunc
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000283698 s, 7.0 kB/s
$ cat foo
fXXbar

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.