The  Linux sg3_utils package

  1. The  Linux sg3_utils package
    1. Introduction
    2. Contents of sg3_utils
    3. Changing mode page settings
    4. Examples
    5. Notes
    6. Download

Introduction

The sg3_utils package contains utilities that send SCSI commands to "SCSI" devices. As well as devices on transports associated with SCSI (e.g. Fibre Channel (FCP) and the SCSI Parallel Interface(SPI)) many other devices use SCSI command sets. ATAPI cd/dvd drives and SATA disks that use a translation layer or bridge device are examples of devices that use SCSI command sets.

SCSI command sets are divided into a common set and several device class specific sets. The common set of commands is referred to as the SCSI Primary Commands (SPC) with SPC-3 being the most recent standard. The mandatory SCSI INQUIRY command is defined in SPC-3. The SCSI Block Commands (SBC) cover direct access devices such as disks. The MultiMedia Commands (MMC) cover CD and DVD drives and the media within them. SCSI command sets and transport definitions can be found at the www.t10.org site with this page giving a good diagrammatic overview: www.t10.org/scsi-3.htm .

The sg3_utils package targets the linux kernel 2.4 and 2.6 series and is still being enhanced. An earlier package called sg_utils targeted the linux kernel 2.2 series with some support for the 2.0 series. See an earlier version of this web page for further information about sg_utils.  This document describes version 1.22 of sg3_utils .  A subset of these utilities have been ported to the FreeBSD, Tru64 and Windows operating systems.

In the linux kernel 2.4 series most of these utilities must be used with a SCSI generic (sg) driver device name (e.g. /dev/sg0). In the lk 2.6 series almost all of these utilities can be used with the primary device names as well (e.g. /dev/sda, /dev/scd0, /dev/st0 and /dev/hdd (if it is an ATAPI device)). The SCSI generic (sg) interface still represents a cleaner interface than the primary device names since the drivers behind primary device names have their own policies, may interfere with error processing and, in some cases, run their own state machines.

A list of SCSI and storage utility programs that was at the bottom of this page has been moved to the tools page.

Contents of sg3_utils

Here is a listing in alphabetical order of the utilities in the sg3_utils package:

Table 1 Utilities in sg3_utils
Utility
Main SCSI commands
subdirectory
Ported
Notes
hxascdmp
-
utils

converts stdin (assumed binary) to ASCII hex and ASCII, sending its output to stdout (like Unix od command)
rescan-scsi-bus.sh
-
archive

copy of Kurt Garloff's useful script
scsi_inquiry
INQUIRY
examples

uses deprecated SCSI_IOCTL_SEND_COMMAND ioctl
sdparm
MODE SENSE/SELECT, INQUIRY
****

was in sg3_utils for a short time; now in own package, see sdparm . sdparm manipulates mode pages, reads VPD pages and sends simple commands.
sg_chk_asc
-
utils

check asc/ascq codes from www.t10.org page against sg3_utils table
sginfo
MODE SENSE/SELECT, READ DEFECT


symbolic decoding (optional changing) of mode pages. Can also output (disk) defect lists. In lk 2.4 can be given primary device nodes (e.g. /dev/sda) as well as sg device nodes. See scsiinfo package notes below.
sgm_dd
READ, WRITE


sg_dd variant that uses memory mapped IO (only on sg devices)
sgp_dd
READ, WRITE


sg_dd variant that uses POSIX threads
sg_dd
READ, WRITE


Unix dd command variant, uses SG_IO ioctl to send SCSI commands to copy data. See the sg_dd page.
sg_emc_trespass
MODE SELECT


utility specialized for EMC Clariion series
sg_format
FORMAT

fb,t64,w
format or resize a SCSI disk
sg_get_config
GET CONFIGURATION

fb,t64,w fetch features and profiles of a cd/dvd drive and/or its current media
sg_ident
REPORT/SET DEVICE IDENTIFIER

fb,t64,w default is to report (fetch) the device identifier. With the '--set' option a new identifier is sent to the device.
sg_inq
INQUIRY

fb,t64,w fetch standard response, VPD pages (including device identification) or version descriptors. Also can perform IDENTIFY (PACKET) DEVICE ATA command. VPD page decoding now also performed by sg_vpd .
sg_logs
LOG SENSE

fb,t64,w fetch log sense pages, decode standard pages
sg_luns
REPORT LUNS

fb,t64,w fetch luns reported by a device (lun 0 or "well known lu")
sg_map
INQUIRY


shows mapping between sg devices and primary device node (if any). In lk 2.6 see lsscsi .
sg_map26



maps between single sg device and primary device node (and vice versa). Also does mapping in to, and out of, sysfs. For the linux 2.6 series.
sg_modes
MODE SENSE

fb,t64,w dump mode pages (mainly in hex)
sg_opcodes
REPORT SUPPORTED OPERATION CODES


fetch supported SCSI commands or supported task management functions
sg_persist
PERSISTENT RESERVE IN/OUT

fb,t64,w control persistent reservations and report reservation status
sg_prevent
PREVENT ALLOW MEDIUM REMOVAL

fb,t64,w control media removal, mainly for those SCSI devices which have removable media (e.g. CD/DVD and tape drives)
sg_rbuf
READ BUFFER


read from SCSI device cache. Typically for testing the SCSI transport (for throughput or errors)
sg_rdac
MODE SENSE/SELECT

fb,t64,w display or modify RDAC redundant controller mode page
sg_read
READ


read continually from same offset. Syntax similar to sg_dd (without write side). Can test SCSI device cache and transport performance (like sg_rbuf).
sg_readcap
READ CAPACITY

fb,t64,w fetch the number of blocks and the individual block size for disks and CD/DVD media
sg_read_long
READ LONG

fb,t64,w read data from given lba which includes the block and ECC data.
sg_reassign
REASSIGN BLOCKS

fb,t64,w reassign a lba from one sector on a disk (typically damaged) to a new (spare) sector. User data copied if it is recoverable.
sg_requests
REQUEST SENSE

fb,t64,w fetch sense data from the given SCSI device. Useful if no autosense , to get progress indication (e.g. during a format) or find the power condition state.
sg_reset
-


Issue a driver, (SCSI) bus or device (target or lun?) reset.
sg_rmsn
READ MEDIA SERIAL NUMBER

fb,t64,w Relatively new command added to SPC-3. Format of response is vendor specific so this utility outputs it in hex (default) or binary.
sg_rtpg
REPORT TARGET PORT GROUPS

fb,t64,w Specialized for multi-ported SCSI devices where one port (or a group of them) is preferred for IO over another (or others).
sg_sat_identify
ATA PASS-THROUGH
fb,t64,w
Sends IDENTIFY DEVICE or IDENTIFY PACKET DEVICE ATA commands via the SAT ATA PASS-THROUGH (16 or 12) SCSI command.
sg__sat_identify
ATA PASS-THROUGH
examples

Simpler version of sg_sat_identify that uses the linux SG_IO ioctl directly. See also sg_sat_chk_power, sg_sat_set_features and sg_sat_smart_rd_data in the examples subdirectory.
sg_scan
[INQUIRY]

w
maps each sg device name to the corresponding numeric <host, channel, target', lun> tuple. In lk 2.6 series the "lsscsi -g" command is similar. In Windows shows one device per line, with the device's various names and INQUIRY response string on that line.
sg_senddiag
SEND DIAGNOSTIC

fb,t64,w Issues either a default self test or a short/extended foreground/background self test. With no arguments it uses RECEIVE DIAGNOSTIC RESULTS to list all supported diagnostic pages.
sg_ses
SEND/RECEIVE DIAGNOSTIC

fb,t64,w Fetches status diagnostic pages from, and sends some control pages to, a SCSI Enclosure Services (SES) device.
sg_simple1,2,3,4,5
INQUIRY, TEST UNIT READY
examples

Simple code examples of using the scsi generic (sg) driver interface
sg_simple16
READ
examples

Simple code example of sending a 16 byte cdb SCSI READ command
sg_start
START STOP UNIT

fb,t64,w Controls the power condition state of a SCSI device. Primary use is to spin up and down SCSI disks.
sg_sync
SYNCHRONIZE CACHE

fb,t64,w Causes disk caches to be flushed to media
sg_test_rwbuf
READ/WRITE BUFFER


Random pattern written to SCSI device buffer then read back and checked. Used in testing for data corruption.
sg_turs
TEST UNIT READY

fb,t64,w Issue one or more Test Unit Ready commands. Can be used to time SCSI command overhead.
sg_verify VERIFY
fb,t64,w reads indicated blocks on a SCSI disks, stops on the first error found. Does not yield any data. Useful for media scans.
sg_vpd
INQUIRY

fb,t64,w
Decodes standard and some vendor Vital Product Data (VPD) pages.
sg_write_long
WRITE LONG

fb,t64,w writes to a lba, data which includes the block and ECC data. Suitable data typically fetched by prior sg_read_long utility.
sg_wr_mode
MODE SELECT

fb,t64,w writes mode pages supplied in ASCII hex (e.g. from "sg_modes -r") to the given SCSI device. See sdparm for another method of setting mode page parameters.

More SCSI commands may be issued than shown in the "main SCSI commands" column. For example many utilities issue a SCSI INQUIRY command to find out the peripheral device type of the given SCSI device. Some SCSI commands listed above are only relevant to a specific device type (e.g. FORMAT UNIT) and should not be sent to a device belonging to other peripheral device types (and in some cases these utilities will block inappropriate attempts). See the COVERAGE file in the main directory of the tarball for a more exhaustive list of SCSI (and ATA) commands issued by the sg3_utils utilities.

If the "subdirectory" column is empty in the above table then that utility is in the main directory and more documentation can be found in its "man" page. All utilities in the main directory have "man" pages and some in the subdirectories have "man" pages.

If the "Ported" column is empty then the utility is only found in Linux. Support for other ports is indicated by "fb" for FreeBSD, "t64" for Tru64 (OSF) and "w" for Windows. See the README.freebsd, README.tru64 and README.win32 files for more information.

This paragraph contains Linux specific information. In the above table all utilities in the main directory except sgp_dd issue SG_IO ioctls when they want to send SCSI commands to the given device. The sgp_dd utility issues SCSI commands using the sg driver's asynchronous ( write()/read() ) interface to device nodes that have the sg driver's major device number (i.e. "char" major 21). This means that all utilities in the main directory are "safe" with any given device node. If the device node does not support the SG_IO ioctl then that is reported and the utility exits. [In version 1.14 sgm_dd also use the sg driver's asynchronous ( write()/read() ) interface but that has been changed to the SG_IO ioctl in version 1.15 since the asynchronous action is not needed. sgm_dd will only use mmapped IO on sg device nodes.]

Irrespective of the device node used to access a device, care should be taken not to interfere with a device while it is "active". For example invoking a "sg_format -F" utility on a disk with mounted file systems on it is going to cause damage.

Each process containing one of these command line utilities completes with an exit status 0 when successful. Prior to version 1.21 all errors yielded an exit status of 1. From version 1.21 the exit status is one of:
Many of the above exit statuses will be repeatable so executing the utility again with one or more '-v' options may yield more information. Unit attentions (exit status 6) are only reported once per condition. Notice that some of the lower exit status values correspond to the SCSI sense key values that they correspond to.

Changing mode page settings

SCSI devices store settings (metadata) that could possibly be changed by the user (called the "application client" in SCSI jargon) in mode pages. It is a common requirement to find mode page settings and in some cases change them. An example is the Writeback Cache Enable (WCE) bit in the Caching mode page of SCSI disks. Usually the manufacturer's default setting for WCE is set (on) however in some RAID configurations it may be cleared (off).

Generic command line tools to change mode page settings tend to be difficult to use (which in some small part is due to the SCSI rules for manipulating mode pages). Here is a list of some linux utilities for changing mode pages followed by some notes:
The author's recommendation is to use sdparm unless the features of another utility better suit your needs.

Examples

The fundamental SCSI command whose support is mandatory for all SCSI devices is INQUIRY. All devices should respond to a "standard" (i.e. when no Vital Product Pages are requested) INQUIRY.

$ sg_inq /dev/sda
standard INQUIRY:
  PQual=0  Device_type=0  RMB=0  version=0x03  [SPC]
  [AERC=0]  [TrmTsk=0]  NormACA=0  HiSUP=1  Resp_data_format=2
  SCCS=0  ACC=0  TGPS=0  3PC=0  Protect=0
  BQue=0  EncServ=0  MultiP=0  MChngr=0  [ACKREQQ=0]  Addr16=1
  [RelAdr=0]  WBus16=1  Sync=1  Linked=1  [TranDis=1]  CmdQue=1
  Clocking=0x3  QAS=0  IUS=0
    length=144 (0x90)   Peripheral device type: disk
 Vendor identification: SEAGATE
 Product identification: ST318451LW
 Product revision level: 0003
 Unit serial number: xxxxxxxxx

Some SCSI devices have version descriptor information showing which standards (and drafts) they support:

$ sg_inq -d /dev/sdb
standard INQUIRY:
  PQual=0  Device_type=0  RMB=0  version=0x03  [SPC]
  [AERC=0]  [TrmTsk=0]  NormACA=0  HiSUP=0  Resp_data_format=2
  SCCS=0  ACC=0  TGPS=0  3PC=0  Protect=0
  BQue=0  EncServ=0  MultiP=0  MChngr=0  [ACKREQQ=0]  Addr16=1
  [RelAdr=0]  WBus16=1  Sync=1  Linked=1  [TranDis=1]  CmdQue=1
  Clocking=0x0  QAS=0  IUS=0
    length=96 (0x60)   Peripheral device type: disk
 Vendor identification: FUJITSU
 Product identification: MAM3184MP
 Product revision level: 0106
 Unit serial number: xxxxxxxxx

  Version descriptors:
    SAM-2 (no version claimed)
    SPI-3 T10/1302-D revision 10
    SPC ANSI X3.301:1997
    SBC T10/0996-D revision 08c

Many modern SCSI devices also support "Vital Product Data" (VPD) pages. Here is a request to list available VPD pages:

$ sg_inq -e /dev/sg1
VPD INQUIRY, page code=0x00:
   [PQual=0  Peripheral device type: disk]
   Supported VPD pages:
     0x0        Supported VPD pages
     0x80       Unit serial number
     0x83       Device identification

For displaying VPD pages, sg_vpd (or sdparm) may be a better choice than sg_inq as sg_vpd has a simpler, less cluttered command line interface and additional support for vendor specific VPD pages.

# sg_vpd /dev/sdh
Supported VPD pages VPD page:
  Supported VPD pages [sv]
  Unit serial number [sn]
  Implemented operating definition (obs) [iod]
  Device identification [di]

The following displays a subset of the device identification VPD page, namely the designators for the target port:

# sg_vpd --page=di_port /dev/sdh
Device Identification VPD page:
  Target port:
    desig_type: NAA,  code_set: Binary
     transport: Serial Attached SCSI (SAS)
      0x5000c500005208ee
    desig_type: Relative target port,  code_set: Binary
     transport: Serial Attached SCSI (SAS)
      Relative target port: 0x2

The sg_scan and sg_map utilities show the relationships between sg devices and their <bus, channel, target, lun> tuples and their primary device node names:

Example: given these 3 SCSI devices:
$ cat /proc/scsi/scsi
Attached devices:
Host: scsi1 Channel: 00 Id: 00 Lun: 00
  Vendor: SEAGATE  Model: ST318451LW       Rev: 0003
  Type:   Direct-Access                    ANSI SCSI revision: 03
Host: scsi2 Channel: 00 Id: 04 Lun: 00
  Vendor: PIONEER  Model: DVD-ROM DVD-303  Rev: 1.10
  Type:   CD-ROM                           ANSI SCSI revision: 02
Host: scsi2 Channel: 00 Id: 06 Lun: 00
  Vendor: YAMAHA   Model: CRW4416S         Rev: 1.0g
  Type:   CD-ROM                           ANSI SCSI revision: 02

then the output from sg_scan is:
$ sg_scan
/dev/sg0: scsi1 channel=0 id=0 lun=0  type=0
/dev/sg1: scsi2 channel=0 id=4 lun=0  type=5

/dev/sg2: scsi2 channel=0 id=6 lun=0  type=5

INQUIRY data can be added to that output with the "-i" option. The sg_map utility shows the mapping between scsi generic (sg) devices and the corresponding primary device node. For some peripheral device types, SCSI enclosures for example, there is no mapping and the enclosure device must be accessed via a sg device node.

$ sg_map
/dev/sg0  /dev/sda
/dev/sg1  /dev/scd0
/dev/sg2  /dev/scd1

In the lk 2.6 series of kernels sg_scan and sg_map are less important as most utilities in the sg3_utils package can be issued directly against the primary device node (e.g. /dev/sda).  However the sg driver is still needed to "talk" to devices such as enclosures which have no specialized driver in linux.

Notes

Starting with sg3_utils-1.09 a library called libsgutils is built and is required by most utilities. The library is made up of two source files: sg_lib.c and sg_cmds.c; and their associated header files . The sg_lib.c file contains SCSI command, sense key and additional sense code strings and various related helper functions. The sg_cmds.c file contains low level wrapper functions for commonly used SCSI commands. The library is built both as a shared object and a static library. The binary rpm package only contains the shared library while the sg3_utils-devel rpm package contains the static library, sg_lib.h and sg_cmds.h . If users do not want libraries (shared or static) then they can refer to the "no_lib" subdirectory in the tarball which contains "no_lib" versions of the Makefile and the rpm "spec" file.

Many of the utilities in the sg3_utils package use the SG_IO ioctl (rather than the older write()/read() interface) in the sg driver. In the lk 2.6  series the SG_IO ioctl (or at least a stripped down version of it) has been implemented in the block subsystem. That means that commands that previously only worked on sg device names will now work on block device names as well (e.g. "sg_inq /dev/sda"). To use this facility safely version sg3_utils-1.02 or later should be used. Note that SCSI tape devices (both st and osst device drivers) are char devices and support the SG_IO ioctl from lk 2.6.6 onwards.

See the notes about header file problems in the README file inside each package.

Download

Various tarballs that include the source, a README, CHANGELOG, INSTALL and a Makefile can be found in the table 2 on the main page. There are also source, i386 binary rpm and deb packages available. There is no './configure' logic so the correct Makefile must be chosen for the OS.

Table 2 Building from source tarball
OS
build to use shared library
build without library
build static executables
Notes
Linux
make
./make_no_lib.sh
./make_no_lib_static.sh
rpm spec file and debian subdirectory included
FreeBSD
make -f Makefile.freebsd
./make_no_lib_freebsd.sh
 
 
Tru64 (OSF1)
make -f Makefile.osf1
./make_no_lib_osf1.sh
 
 
Windows

make -f Makefile.win32
 
Windows NT and latter supported (uses SPT interface). Builds in a cygwin environment and requires cygwin1.dll to run.


Both sg3_utils and the older sg_utils packages can be found on http://freshmeat.net .

Return to main page.

Last updated: 14th October 2006