Copy SCSI Data Safely with sgm_dd
Before you point sgm_dd at a real disk, rehearse it on two throwaway files so a typo in a device path costs you nothing. You will finish with a repeatable way to copy data between regular files, SCSI generic devices and raw devices, with the block size and block count always explicit. Allow about fifteen minutes for a file test, longer if you must identify a device carefully.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide describes the installed command from sg3-utils package version 1.46-3ubuntu4. Its own version output reports 1.67 20200510, so treat the installed command and its local manual as the authority when another host differs.
1. Check the command and the source device
Start with ordinary, read-only checks. You need a shell, the sg3-utils package, a readable source, and enough space at the destination. Reading device names is normally unprivileged, but access to /dev/sg* or a disk may need membership of the appropriate group, or elevated privileges.
$ command -v sgm_dd
/usr/bin/sgm_dd
$ sgm_dd --version
sgm_dd: : 1.67 20200510
- Identify the mapping before copying anything. The manual specifically recommends checking the
sgmapping withlsscsi. - Do not infer a disk from its position in
/dev. Confirm model and serial information match the device you intend to use before continuing.
$ lsscsi -g
$ ls -l /dev/sgX /dev/sdY
Checkpoint
Write down the exact input and output paths. A one-character error in a device path can turn a backup operation into an overwrite.
2. Learn the copy shape with regular files
sgm_dd uses dd-style operands, but it does not perform conversions. The usual form is if= for the input, of= for the output, bs= for the physical block size, and count= for the number of blocks.
Create a small disposable source in a temporary directory, then ask sgm_dd to prepare the operation. This is a dry run: it parses the operands and may open the paths, but it does not copy data.
$ printf "%s" "sgm_dd test data" > /tmp/sgm-dd-source
$ sgm_dd bs=1 count=16 if=/tmp/sgm-dd-source of=/tmp/sgm-dd-copy --dry-run
The dry run should exit quietly with status zero on this example. Confirm that immediately:
$ printf "%s\n" "$?"
0
Do not treat a successful dry run as proof that a device is healthy or that the destination has enough space. It only checks preparation. It may still open an existing output, so use paths whose consequences you understand.
3. Perform and verify a safe file copy
Run the same operation without --dry-run. The example copies sixteen one-byte blocks. Because the output is a normal file, it does not need sudo in /tmp.
$ sgm_dd bs=1 count=16 if=/tmp/sgm-dd-source of=/tmp/sgm-dd-copy
$ cmp -- /tmp/sgm-dd-source /tmp/sgm-dd-copy
$ printf "%s\n" "$?"
0
- Expect no success message. Informative output goes to standard error; exit status zero means the copy completed.
- Let
cmpdo the real check. If it reports a difference, stop and inspect the block count and paths rather than copying again over an important destination.
Existing regular output files are not truncated by default. They are overwritten from the beginning, which can leave old trailing data if the new copy is shorter. Use a new destination for a first run. The oflag=append flag appends instead, but it cannot be combined with seek=.
4. Set the block size for real media
For this utility, bs must be the physical block size of the device. The default is 512, commonly correct for disks but normally wrong for CD, DVD and Blu-ray media, where 2048 is typical. Unlike ordinary dd, do not use an arbitrary multiple as a shortcut.
For a block device or SCSI generic device, specify a finite count for a bounded image and use a destination that is new or intentionally disposable. Replace every placeholder after confirming it against your hardware:
$ sudo sgm_dd bs=512 count=2097152 if=/dev/sgX of=/path/to/new-disk-image.img time=1
This writes about 1 GiB: 512 * 2097152 bytes. The command reads from the SCSI generic device and writes to the image. time=1 prints transfer timing to standard error. Elevated privileges are only for device or destination permissions; they do not make an incorrect device path safe.
For optical media, use the device physical block size and a count appropriate to the medium:
$ sudo sgm_dd bs=2048 count=BLOCKS if=/dev/sgX of=/path/to/new-optical-image.iso time=1
BLOCKS is a placeholder, not a value to paste. Establish the medium capacity first. A count that is too small produces an incomplete image; a count that is too large can produce read errors or waste time.
5. Use offsets only when the layout is known
skip= starts reading after that many bs-sized blocks, while seek= starts writing after that many blocks in the output. Both default to zero. This is useful for a documented partition or image layout, but dangerous when the unit is misunderstood.
$ sgm_dd bs=512 count=1024 skip=START_BLOCK if=/dev/sgX of=/tmp/partition-part.bin
Replace START_BLOCK only after checking the partition table and confirming that the source block size is 512 bytes. A wrong offset silently gives you the wrong data. Use --dry-run first, then verify the output size and a known signature or checksum. Numeric values may use the suffixes documented by sg3_utils(8); keep plain decimal values in scripts when clarity matters.
6. Choose performance and diagnostic flags deliberately
The default transfer uses memory-mapped I/O when an input or output is an sg device. bpt= controls how many blocks are included in each I/O transaction: the default is 128 for block sizes below 2048 bytes and 32 otherwise, normally about 64 KiB. Leave it at the default until a measured workload gives you a reason to change it.
oflag=directrequests direct I/O on a normal input or output file and has alignment requirements.oflag=diois a different write-side direct-I/O path, only allowed when the input is ansgdevice.- Neither is performance decoration. Both flags can fail or fall back, so add them only for a measured reason.
sync=1requests a SCSI synchronise-cache command at the end, but only when the output is ansgdevice.
For diagnosis, use verbose=1 or -v. Higher values expose more SCSI commands and read or write calls, and levels 3 and 4 can be very noisy. All diagnostics go to standard error, leaving standard output suitable for data when of=- is used.
7. Know the hard safety boundaries
Safety boundary
Do not use sgm_dd on tape devices. Its SCSI READ and WRITE commands are intended for disks and CD, DVD or Blu-ray drives. A raw device must already be bound to a block device, and the manual points to raw(8) for that separate setup.
- It stops on error. This is not the recovery-oriented tool for copying through bad sectors; the manual points to
sg_ddfor copy-on-error behaviour. - Do not guess your way through a failure. Preserve the source, record the error, and choose a recovery procedure that matches the media.
There is no undo for writing to a device. If a file copy is wrong, remove only the disposable output after checking its path. For a device write, stop before running the command unless you have a tested backup and a documented restore plan.
Done means
- Checked the binary and version.
- Matched the SCSI generic path to the intended device with
lsscsi. - Ran a dry run before the real copy.
- Set
bsto the physical media block size and madecountintentional. - Confirmed status zero and checked the output independently.
- Kept tape devices and unplanned destinations out of it.