Copy SCSI Data Safely with sgp_dd

sgp_dd will overwrite a disk or partition without asking twice, so rehearse it on two disposable files before it ever sees a real device path. The examples use the installed sg3-utils package, version 1.46-3ubuntu4, whose program reports sgp_dd 5.77 20210103. Allow 15 minutes for a file-to-file rehearsal, or longer if you must identify real storage devices first.

Warning: work from a root shell only when device access requires it, and re-check every device name immediately before a real copy. Do not rehearse against a device just because it appears in an example.

1. Confirm the installed interface

Check the package and program before building a command:

$ dpkg-query -W -f='${Package} ${Version}\n' sg3-utils
sg3-utils 1.46-3ubuntu4
$ sgp_dd --version
sgp_dd: 5.77 20210103

Checkpoint: sgp_dd --help should show the same operand family, including --dry-run. Informational output is written to standard error, which keeps standard output usable as a data stream.

2. Practise against ordinary files

Before involving a device, create a small input file and ask sgp_dd to prepare the copy without reading or writing it:

dd if=/dev/zero of=/tmp/sgp-dd-input.bin bs=512 count=2 status=none
sgp_dd if=/tmp/sgp-dd-input.bin of=/dev/null bs=512 count=2 --dry-run

Expected output includes:

Due to --dry-run option, bypass copy/read
  remaining block count=2
0+0 records in
0+0 records out

That check validates parsing and preparation. It is not a data-integrity test because --dry-run bypasses the copy. The installed command returned status 0 for this test. The temporary input is disposable; it is safe to leave it in /tmp for this guide, or remove it later once you have confirmed the path.

3. Perform a reversible file copy

Use a new output path for the first real transfer:

sgp_dd if=/tmp/sgp-dd-input.bin \
  of=/tmp/sgp-dd-output.bin bs=512 count=2 time=1

With time=1, completion output includes a throughput calculation. The output file should contain 1024 bytes:

$ stat -c '%n %s bytes' /tmp/sgp-dd-output.bin
/tmp/sgp-dd-output.bin 1024 bytes
$ cmp /tmp/sgp-dd-input.bin /tmp/sgp-dd-output.bin && echo identical
identical

Recovery: for this rehearsal, delete the two temporary files when finished. A destination device has no equivalent undo, so treat the next step as irreversible.

4. Identify the source and destination devices

For a SCSI generic device, inspect the mapping before you copy:

lsscsi -g
cat /proc/scsi/scsi

The lsscsi -g listing can show which /dev/sgN name corresponds to a block device such as /dev/sdX. The proc listing is the check named by the local manpage. Confirm the vendor, model, serial or other identifying detail against the storage you intend to use. Device numbers can change after unplugging hardware or rebooting.

Reading device information is ordinarily unprivileged, but opening a protected device may require sudo. Use elevated privileges only for that access:

sudo sgp_dd if=/dev/sgSOURCE of=/tmp/scsi-image.bin \
  bs=512 count=BLOCKS time=1

Replace sgSOURCE and BLOCKS with values you have verified. An explicit count is safer than relying on automatic sizing when a regular file or unusual device is involved. count, skip and seek accept large values; numeric arguments may also use documented multiplier suffixes or hexadecimal.

5. Copy blocks, not guesses

For a device-to-file image, the core pattern is:

sudo sgp_dd if=/dev/sgSOURCE \
  of=/path/to/new-image.bin bs=512 count=BLOCKS time=1

skip=N starts reading at block N. For a destination device, seek=N starts writing at block N. Both are measured in bs-sized blocks:

sudo sgp_dd if=/dev/sgSOURCE of=/dev/sgDEST \
  bs=512 skip=SOURCE_BLOCK seek=DEST_BLOCK count=BLOCKS

Warning: do not combine oflag=append with seek. Appending is intended for normal files and is usually ignored or rejected for a device. A destination device can be damaged by a wrong path, a wrong block size or a wrong offset. Stop and re-check the mapping if the device reports unexpected capacity or block errors.

6. Handle errors deliberately

The default is to stop on an error.

Either can produce an incomplete or altered image, so record the diagnostic output and do not present the result as an exact copy.

sudo sgp_dd if=/dev/sgSOURCE of=/path/to/recovery.img \
  bs=512 count=BLOCKS iflag=coe time=1 2>/path/to/recovery.log

Use coe=1 only when you intentionally want the older shorthand for both input and output continue-on-error behaviour. It is a retired option in this interface; the separate flags make the decision clearer.

thr=1 reduces the worker count from the default of four if a device behaves poorly under parallel requests. The permitted range is 1 to 1024. dio=1 requests direct I/O but can fall back to indirect I/O, so do not treat it as a guarantee. Leave the defaults alone until a measured transfer gives you a reason to change them.

Done means