LinuxCommandLibrary

sg_raw

Send raw SCSI commands

TLDR

Send a command to an optical SCSI device assigned to sr0 to load the media in its tray

$ sg_raw /dev/sr0 EA 00 00 00 00 01
copy

Read data from IFILE instead of stdin
$ sg_raw [[-i|--infile]] [path/to/IFILE] [/dev/sgX] [SCSI_command]
copy

Skip the first LEN bytes of input data
$ sg_raw [[-k|--skip]] [LEN] [/dev/sgX] [SCSI_command]
copy

Read SLEN bytes of data and send to the device
$ sg_raw [[-s|--send]] [SLEN] [/dev/sgX] [SCSI_command]
copy

Wait up to SEC seconds for sg_raw to finish processing
$ sg_raw [[-t|--timeout]] [SEC] [/dev/sgX] [SCSI_command]
copy

Increase verbosity level by 1
$ sg_raw [[-v|--verbose]] [/dev/sgX] [SCSI_command]
copy

Dump returned data in binary form
$ sg_raw [[-b|--binary]] [/dev/sgX] [SCSI_command]
copy

Write data received from the specified device to an OFILE
$ sg_raw [[-o|--outfile]] [path/to/OFILE] [/dev/sgX] [SCSI_command]
copy

SYNOPSIS

sg_raw [options] device cdb_bytes [cdb_bytes...]

PARAMETERS

-r, --recv
    Receive data from the device during the data-in phase.

-s, --send
    Send data to the device during the data-out phase.

-i, --input=FILE
    Read data for send operations from the specified FILE or standard input.

-n, --nodata
    Indicates that there is no data-in or data-out phase for the command.

-X, --xferlen=LEN
    Specify the expected transfer length in bytes for data-in/out phases.

-t, --timeout=SECS
    Set the command timeout in seconds (default is 60 seconds).

-v, --verbose
    Increase the verbosity of output.

-x, --six
    Assume the CDB is 6 bytes long.

-y, --ten
    Assume the CDB is 10 bytes long.

-z, --sixteen
    Assume the CDB is 16 bytes long.

-d, --data
    Print data received from the device in both hexadecimal and ASCII formats.

-e, --error
    Print the sense buffer in hexadecimal format after the command completes.

DESCRIPTION

sg_raw is a utility from the sg3_utils package that allows users to send arbitrary SCSI commands directly to a device. It constructs a SCSI Command Descriptor Block (CDB) from hexadecimal bytes provided on the command line or from a file. This command is invaluable for low-level diagnostics, testing device firmware, accessing vendor-specific features not exposed by standard drivers, or recovering data in unusual scenarios.

It operates directly with the SCSI Generic (sg) driver in the Linux kernel, bypassing higher-level file system or block device layers. Users must have a good understanding of SCSI commands and their specific opcode, service action, and parameter fields to use sg_raw effectively. Incorrect usage can lead to data corruption or device damage.

CAVEATS

sg_raw operates at a very low level and bypasses many safety mechanisms provided by higher-level drivers. Incorrectly crafted SCSI commands can lead to irreversible data loss, device corruption, or even physical damage. Users must have a deep understanding of the SCSI command set (e.g., SAM, SBC, SPC standards) relevant to their specific device. Use with extreme caution, ideally on non-production systems or with up-to-date backups. Root privileges are usually required to access /dev/sg* devices directly.

<B>SCSI COMMAND DESCRIPTOR BLOCK (CDB)</B>

The core of sg_raw's functionality revolves around the SCSI CDB. This is an array of bytes that specifies the SCSI command opcode, command-specific parameters, and control bits. Different SCSI commands have varying CDB lengths (e.g., 6, 10, 12, 16, 32 bytes). Users provide these bytes in hexadecimal format, which sg_raw then transmits directly to the device.

<B>SCSI GENERIC (SG) DRIVER</B>

sg_raw utilizes the Linux kernel's SCSI Generic driver, typically accessed through device nodes like /dev/sg0, /dev/sg1, etc. This driver provides a low-level interface that allows applications to send SCSI commands directly to devices without interference from higher-level block or filesystem drivers. This direct access is what enables sg_raw's powerful and precise control over SCSI devices, essential for deep diagnostics and custom command execution.

HISTORY

sg_raw is a foundational component of the sg3_utils package, a collection of Linux utilities designed for sending SCSI commands. These tools leverage the SCSI Generic (sg) driver in the Linux kernel, which provides a direct pass-through interface for SCSI commands. The sg3_utils package has been developed and maintained over many years, continuously evolving to support newer SCSI standards and kernel interfaces. sg_raw was specifically designed to provide a flexible way to send *any* arbitrary SCSI CDB, making it an indispensable tool for advanced users, developers, and testers who require precise, granular control over SCSI devices.

SEE ALSO

sg_map(8), sg_inq(8), sg_vpd(8), sg_opc(8), lsscsi(8)

Copied to clipboard