Friday, June 23, 2017

Progress Bar For dd(1)

The dd(1) command is frequently used to both make and write out image files, such as .iso files for Linux releases.  It's mostly intended for this and works fine, but a common complaint from users is the lack of a progress indicator.  It's true:  dd is silent until it's complete.  But you can get a progress bar with the pv(1) command.

pv is short for Pipe Viewer.  You can get it over here or install it from your distribution's packaging system.  For example, Fedora users can do dnf install pv.

Once you have pv installed, you can start getting progress output for console commands.  The man page has some good examples, including one for tar(1).  The man page also suggests using pv by itself as a replacement for dd entirely, but I have had limited success with that.  Here's what I do:
dd if=ISOFILE | pv -p -e -a -s BYTES | dd of=DEVICE bs=4096
OK, that's a lot, but it's not that bad.  dd reads from stdin when not told otherwise.  Likewise, it writes to stdout when not told otherwise.  In this example, we connect two invocations of dd using pv, which also has the same behavior.  By not specifying either on pv, it reads from stdin and writes to stdout.

The ISOFILE is the name of the image file I want to write, such as release-3.2.0-ppc64-20170623.iso.  BYTES is the size of the image file in bytes.  This is obtained any number of ways, such as the long listing from ls(1).  DEVICE is the device node you want to write the image to, such as /dev/sdh.

You could extend this and have the command determine the size of the ISOFILE directly, such as:
dd if=ISOFILE | pv -p -e -a -s $(stat -c %s ISOFILE) | dd of=DEVICE bs=4096
Now this is starting to look like something you could put in to a helper script in your own environment.  Just another tool for your command line adventures.

No comments: