sg_dd introduction and examples

The sg_dd utility is a variant of the standard Unix command dd which copies files. The sg_dd utility is specialized for devices that use the SCSI command set in the Linux operating system. The sg_dd utility is found in the sg3_utils package which targets the linux kernel 2.4 and 2.6 series.

Only block oriented SCSI peripheral device types are supported by the sg_dd utility. Obviously those device types that deal with enclosures and medium changers don't have addressable blocks and are not supported. The supported device types are direct access devices (e.g. disks) and cd/dvd devices. The SCSI tape device type is not be supported (but could be to a limited degree). When instructed, the sg_dd utility issues SCSI commands that are defined in SPC-4 (primary commands), SBC-3 (commands for direct access devices (e.g. disks)) and MMC-5 (commands for CD/DVD devices). These SCSI command sets can be found at www.t10.org .

sg_dd (copies data to and from sg and raw devices), dd like examples:

This will copy 1 million 512 byte blocks from the device associated

with /dev/sg0 (which should have 512 byte blocks) to a file called t.

sg_dd if=/dev/sg0 of=t bs=512 count=1MB

Assuming /dev/sda and /dev/sg0 are the same device then the above is

equivalent to:

dd if=/dev/sda of=t bs=512 count=1000000

although dd's speed may improve if bs was larger and count was suitably reduced.

sg_dd, some options that are extra to dd

Number of times to retry an error on a sg device READ or WRITE command

sg_dd if=/dev/sg0 of=/dev/sg1 bs=512 retrie=10

sync=0 | 1 option

when set, sends SYNCHRONIZE CACHE SCSI command to OFILE if it is a sg device or accessed via the SG_IO ioctl.

time=0 | 1 option

when set print elapsed time and throughput calculation at the completion of the copy

verbose=VERB

larger VERB is the greater the debug output. 1 and 2 print the cdbs for setup commands; 3 and 4 print the cdbs for all commands

bpt=BPT

where BPT is the blocks per transfer (granularity of each IO). Default is 128 when BS < 2048 (bytes) else the default is 32. For block devices (BPT * BS) is constrained by /sys/block/<device>/queue/max_sectors_kb .