2

I'm trying to do an 'advanced case' of cloning.

There is a pipe writing to a block device, at the end of which there is a partition that should not be touched. I know where this partition starts. In this case I want to override the partition table.

For the sake of simpilicty the pipe starts with dd if=myfile.

dd if=myfile > /dev/sda

I could do something like

dd if=myfile | dd bs=1B count=startofmypartition-~1000000 > /dev/sda

But are there any better ways to do that and are there any drawbacks to this method?

pv has a size option, but it states that it is only used to calcualte the ETA, so I'm not sure if this will work: (from the manpage:)

-s SIZE, --size SIZE Assume the total amount of data to be transferred is SIZE bytes when calculating percentages and ETAs. The same suffixes of "k", "m" etc can be used as with -L.

I know that there will be more write operations, but it should turn out to be the same data on the disk using the same amount space - as long it's not a tape drive.

1

dd bs=1 is very inefficient as it does one read() and one write() system call per byte. It doesn't make much sense to use dd anyway on a block device (unless you want to use special flags like conv=noerror,sync... to handle errors or offsets)

Here you could do:

offset=$(($(cat /sys/class/block/sda3/start) * 512)) &&
 pv -trabSs "$offset" < myfile > /dev/sda

(or use head -c "$offset" if you don't care about the progress information).

pv and head will choose adequate buffer size to optimise performance.

(note that since you're writing data at the start of the disk, you're going to effectively overwrite the partition table!).

Another alternative could be to create a new block device that maps only the sections of /dev/sda that you're willing to overwrite

Like:

echo "0 $(cat /sys/class/block/sda3/start) linear /dev/sda 0" |
  dmsetup create writable_sda &&
cp myfile /dev/mapper/writable_sda
  • Why do you multiply * 512 if the -S option is in bytes and the output of /start is in bytes? – rudib Sep 12 '17 at 14:50
  • 1
    @rudib, the content of /start is in 512-byte units, not bytes. – Stéphane Chazelas Sep 12 '17 at 15:11
  • Ok, thanks, I was making wrong assumptions; sorry I was comparing to fdisk which uses 512-byte units and I thought it were bytes... – rudib Sep 12 '17 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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