sg_read_attr pulls SCSI tape cartridge attributes: capacity, load count, TapeAlert flags and more. You will finish with a repeatable way to inspect them, list the partitions a device reports, narrow the response to one attribute, and save raw response data for later decoding. The examples use sg_read_attr from sg3-utils 1.46, installed here as Debian package version 1.46-3ubuntu4.
Allow about fifteen minutes, plus time to identify the correct SCSI generic device. You need a shell, sg3-utils, and access to the tape drive or library. Most examples read device state, but they still send SCSI commands to hardware. Confirm the device path before running one. Use elevated privileges only if your account cannot open the device; do not use sudo as a substitute for checking the path.
Start with read-only local checks. These do not contact a drive:
$ command -v sg_read_attr
/usr/bin/sg_read_attr
$ dpkg-query -W -f='${Package} ${Version}\n' sg3-utils
sg3-utils 1.46-3ubuntu4
$ sg_read_attr --version
sg_read_attr version: 1.13 20191220
The package version and the utility's own version string are separate pieces of information. Keep both in a support report. The installed manpage is for the 1.46 release of the sg3_utils collection, while this particular utility reports its source version as 1.13.
Checkpoint: ask the program for its usage text if an option is unfamiliar:
$ sg_read_attr --help
Usage: sg_read_attr [--cache] [--element=EA] ... DEVICE
Option spellings can vary between older and newer builds. The local help output is the authority for the binary you are about to run.
sg_read_attr takes one device argument, normally an SCSI generic node such as /dev/sg1. On a busy host, do not assume that the number in a device path identifies a particular tape drive. Inventory the host with the tools used by your environment, then match the path to the drive's serial number, operating-system inventory, or library documentation.
This command only lists generic SCSI nodes; it does not identify their attached hardware:
$ find /dev -maxdepth 1 -type c -name 'sg*' -printf '%f\n' | sort -V
sg0
sg1
Replace /dev/sg1 in the following examples with the verified path. If the node is missing, stop and fix device discovery or permissions first. A missing path is not evidence that the tape has no attributes.
Use --enumerate to print the attribute identifiers and service actions known to this build. With this option, a supplied device is ignored, so it is safe to run without touching hardware:
$ sg_read_attr --enumerate
Attribute ID Length Format Name
------------------------------------------
0x0000: 8 binary Remaining capacity in partition [MiB]
0x0001: 8 binary Maximum capacity in partition [MiB]
0x0002: 8 binary TapeAlert flags
...
The complete list depends on the utility's built-in knowledge, not on the particular tape currently loaded. It is a useful lookup for choosing a filter, but it does not prove that every listed attribute exists on your media.
Checkpoint: select the service action before querying the drive. Service action 0, also named av, fetches attribute values. al lists attribute names, pl lists partitions, and sa lists supported attributes.
Run the default service action against the verified device. The command opens the device read-write by default, even though READ ATTRIBUTE is a read operation. Add --readonly to make the open mode explicit:
$ sg_read_attr --readonly /dev/sg1
Attribute values:
Remaining capacity in partition [MiB]: 1386103
Maximum capacity in partition [MiB]: 1386103
TapeAlert flags: 0
Load count: 7
...
These values are examples of the output shape, not predictions for your tape. A successful command returns exit status 0. Check it immediately:
$ printf 'sg_read_attr status: %s\n' "$?"
sg_read_attr status: 0
If the command fails, preserve the SCSI sense information printed by the utility. Common causes include a non-tape device, no medium loaded, a drive that does not implement READ ATTRIBUTE, an incorrect partition or volume number, and insufficient permission to open the node. Do not retry repeatedly against a library device while it is loading or unloading media.
Ask for the partition list with service action pl:
$ sg_read_attr --readonly --sa=pl /dev/sg1
Partition number list:
First partition number: 0
Number of partitions available: 2
For a device with one partition, partition number zero is required. When more than one partition is reported, pass the relevant number with --pn:
$ sg_read_attr --readonly --sa=av --pn=1 /dev/sg1
Attribute values:
Remaining capacity in partition [MiB]: 691203
...
Likewise, --lvn selects a logical volume number. Both fields default to zero. Do not carry a partition or volume number from one cartridge to another without checking the new device's list first.
Use --filter with an attribute identifier from the enumeration output. Decimal, hexadecimal with a 0x prefix, and a trailing h are accepted forms. The default -1 matches all attributes:
$ sg_read_attr --readonly --filter=0x0002 /dev/sg1
Attribute values:
TapeAlert flags: 0
Use --first when you need attributes from a starting identifier onwards. It is not the same as a single-attribute filter, and returned attributes remain in ascending identifier order:
$ sg_read_attr --readonly --first=0x020a /dev/sg1
Attribute values:
Density vendor/serial number at last load: ...
If a filter produces no output or the drive rejects the request, remove the extra selector and establish a complete response first. Narrowing options are useful for scripts, but they can also hide a wrong identifier.
For a reproducible support record, redirect the three-H hexadecimal form to a file. This changes no device state, but the output is data rather than a terminal display:
$ sg_read_attr --readonly --hex --hex --hex /dev/sg1 > av.hex
$ test -s av.hex && echo 'saved response'
saved response
Decode it later with --in. Because the saved response is the default attribute-values service action, --sa=av makes that assumption explicit:
$ sg_read_attr --sa=av --in=av.hex
Attribute values:
Remaining capacity in partition [MiB]: 1386103
...
The input file is expected to contain space, tab, or comma-delimited ASCII hexadecimal. Lines from # onwards are ignored, so comments can be added to a copy of the file. If you instead capture binary with --raw, pass --raw when reading it. Treat these files as operational records: they may contain tape identifiers and vendor serial information, so protect them according to your data-handling policy.
Recovery: sg_read_attr has no undo operation because these examples do not write to the drive. If an operation is unexpectedly disruptive, stop issuing commands, retain the utility's diagnostics, and involve the storage operator before changing media or library state.
--enumerate to choose a valid service action or attribute identifier.--readonly and checked the exit status.