How to use pv with dd

ddpvstrace

I'm trying to use dd with pv to write an iso to a usb drive. I did:

iso="myiso.iso"; dd if="$iso" bs=1M | pv --eta --size $(ls -l "$iso" | awk '{print $5}') --progress --bytes --rate --wait > /dev/sdg

Following this guide:
https://gist.github.com/rriemann/1406035

However, what happens is that the iso is "written" very quickly according to the output of pv. I'm guessing it's all put into disk cache or similar. Then pv just hangs. ps shows that pv is still running, but dd isn't. I tried stracing pv, but there is no output, and strace hangs as well! ctrl+c has no effect on strace. In fact, it does not respond to signals 15, 3, 2, or 1. Waited a few minutes for each signal. I was eventually forced to send 9.

According to this:
http://www.reddit.com/r/linux/comments/1xvr25/linux_tip_dont_use_kill_9/
The strace binary is badly behaved.

Here are my questions:

  1. How can I make pv report more useful data? I know it is probably doing what it is "supposed to", ie, just reporting on bytes that it pipes. But that is not helpful for what I want to know, which is rough estimate for bytes written to usb drive, eta, etc.

  2. Why is strace not responding to any signals? My guess is that strace waits for a system call from the process it is watching before handling the next signal, which I would consider a bug. But that is just a guess. strace version 4.5.20.

Best Answer

Maybe you could put dd on the other end of the stick and try to avoid caches by using its direct and sync flags.

pv "$iso" | dd bs=1M oflag=direct,sync of=/dev/sdg

With that you should be caching at most dd's blocksize, in this case 1M... well plus whatever is in the pipe | itself I guess.

Related Question