2009-03-19

pv (Pipe Viewer) in action!

Recently I needed to copy a 4GB of data to a 6GB partition (both of which were VirtualBox disks). Remembering my resolution to use pv program (pipe viewer) in such case, I used it this time while copying to see the progress of the whole process. I started with getting the expected size of copied data:
# fdisk -l -u

Disk /dev/sda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x664a0e1b

Device Boot Start End Blocks Id System
/dev/sda1 * 63 8369864 4184901 7 HPFS/NTFS

Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders, total 12582912 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Next, after consulting the pv's manual, I used it as a filter between two dd programs (one for reading from the source and one for writing to the destination). I played a bit with pv's -B option (which specifies the size of internal buffer), but it turned out the default size was the best. Anyway, the progress I achieved looked like this:

# dd if=/dev/sda bs=4096 | pv -pterb -s 4294967296 -B 4096 | dd of=/dev/sdb bs=4096
35.5MB 0:00:20 [1.61MB/s] [> ] 0% ETA 0:41:38
8469+0 records in
8468+0 records out
34684928 bytes (35 MB) copied, 20.6268 s, 1.7 MB/s
8451+0 records in
8451+0 records out
34615296 bytes (35 MB) copied, 20.6437 s, 1.7 MB/s

Pretty neat IMHO, just what I wanted. Now I only need to remember the parameters for another use: "pterb" for progress, and "-s" for size...