Home / Alt manpages / sgm_dd(8)

  • sgm_dd(8)
  • Admin command
  • linux

Copy SCSI Data Safely with sgm_dd

You will finish with a repeatable way to copy data between regular files, SCSI generic devices and raw devices using `sgm_dd`. The examples make the block size and block count explicit, test the command without copying, and verify a harmless file copy before you approach real storage. Allow about fifteen minutes for a file test, longer if you must identify a device carefully.

This guide describes the installed command from `sg3-utils` package version `1.46-3ubuntu4`. Its own version output reports `1.67 20200510`, so use 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 require membership of the appropriate group or elevated privileges.

$ command -v sgm_dd
/usr/bin/sgm_dd
$ sgm_dd --version
sgm_dd: : 1.67 20200510

For a SCSI device, identify the mapping before copying. The manual specifically recommends checking the `sg` mapping with `lsscsi`. Do not infer a disk from its position in `/dev`, and do not continue until the model and serial information match the device you intend to use.

$ 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

There may be no success message: informative output goes to standard error. Exit status zero means the copy completed. `cmp` adds a separate content 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`, which is 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=direct` requests direct I/O on a normal input or output file and has alignment requirements. `oflag=dio` is a different write-side direct-I/O path that is only allowed when the input is an `sg` device. These flags can fail or fall back, so do not add them as performance decoration. `sync=1` requests a SCSI synchronise-cache command at the end, but only when the output is an `sg` device.

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

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.

The utility stops when it encounters an error. It is not the recovery-oriented tool for copying through bad sectors; the manual points to `sg_dd` for copy-on-error behaviour. Do not replace a failing copy with guessed flags. 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

  • The installed binary and version were checked.
  • The SCSI generic path was matched to the intended device with `lsscsi`.
  • A dry run completed before the real copy.
  • `bs` matches the physical media block size and `count` is intentional.
  • The copy returned status zero and its output was checked independently.
  • No tape device or unplanned destination was included.