SG3 Utils¶
sg3_utils — low-level SCSI command utilities (sg_inq, sg_readcap,
sg_vpd, sg_luns, etc.) for querying and manipulating SCSI devices
below the block layer.
sts.sg3_utils
¶
Python wrappers for sg3_utils SCSI commands.
Design decision: sg3_utils classes remain plain Python (not Pydantic). They are stateless command runners -- no fields to validate, no state to manage. See docs/superpowers/plans/2026-07-27-master-modernization-roadmap.md Decision Log.
RescanScsiBus
¶
Bases: Sg3UtilsCommand
SCSI bus rescanning using rescan-scsi-bus.sh.
Source code in sts_libs/src/sts/sg3_utils.py
764 765 766 767 | |
ScsiRescan
¶
Bases: Sg3UtilsCommand
SCSI rescanning using scsi-rescan.
Source code in sts_libs/src/sts/sg3_utils.py
770 771 772 773 | |
Sg3UtilsCommand
¶
Base class for sg3_utils commands with shared execution and output parsing.
Source code in sts_libs/src/sts/sg3_utils.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
clean_line(line)
staticmethod
¶
Strip and normalize whitespace in a line.
Source code in sts_libs/src/sts/sg3_utils.py
201 202 203 204 | |
extract_hex_keys(output)
staticmethod
¶
Extract all hex keys (0x... format) from output.
Example
output = 'registered reservation key follows:\n 0x1234\n 0xabcd'
keys = Sg3UtilsCommand.extract_hex_keys(output)
# ['0x1234', '0xabcd']
Source code in sts_libs/src/sts/sg3_utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 | |
extract_section_lines(output, section_header)
staticmethod
¶
Extract lines after a section header until the next section or end.
Example
output = 'PR generation=0x6, Reservation follows:\n Key=0x1234\n scope: LU_SCOPE'
lines = Sg3UtilsCommand.extract_section_lines(output, 'Reservation follows:')
# ['Key=0x1234', 'scope: LU_SCOPE']
Source code in sts_libs/src/sts/sg3_utils.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
find_line_containing(output, pattern, *, case_sensitive=False)
staticmethod
¶
Find first line containing a specific pattern.
Source code in sts_libs/src/sts/sg3_utils.py
206 207 208 209 210 211 212 213 214 215 216 | |
help()
¶
Get help information for the tool.
Source code in sts_libs/src/sts/sg3_utils.py
114 115 116 | |
parse_key_value_pairs(output, separators='=:')
staticmethod
¶
Parse 'key=value' or 'key: value' lines from output.
Example
output = 'Vendor identification: Samsung\nKey=0x1234'
pairs = Sg3UtilsCommand.parse_key_value_pairs(output)
# {'Vendor identification': 'Samsung', 'Key': '0x1234'}
Source code in sts_libs/src/sts/sg3_utils.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
run(*args, device=None, **options)
¶
Run the command with arbitrary flags and options.
Kwargs are translated to CLI flags (underscores to hyphens,
bools to bare flags, values to --key=value).
Source code in sts_libs/src/sts/sg3_utils.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
version()
¶
Get version information for the tool.
Source code in sts_libs/src/sts/sg3_utils.py
110 111 112 | |
SgDd
¶
Bases: Sg3UtilsCommand
SCSI dd utility using sg_dd.
Source code in sts_libs/src/sts/sg3_utils.py
662 663 664 665 | |
SgFormat
¶
Bases: Sg3UtilsCommand
SCSI FORMAT UNIT commands using sg_format.
Source code in sts_libs/src/sts/sg3_utils.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | |
format_device(device, block_size=None, *, dry_run=True)
¶
Format a SCSI device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
block_size
|
int | None
|
Block size for formatting |
None
|
dry_run
|
bool
|
Perform dry run without actual formatting (default: True for safety) |
True
|
Source code in sts_libs/src/sts/sg3_utils.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | |
SgGetConfig
¶
Bases: Sg3UtilsCommand
SCSI GET CONFIGURATION commands using sg_get_config.
Source code in sts_libs/src/sts/sg3_utils.py
686 687 688 689 | |
SgIdent
¶
Bases: Sg3UtilsCommand
SCSI device identification using sg_ident.
Source code in sts_libs/src/sts/sg3_utils.py
692 693 694 695 | |
SgInq
¶
Bases: Sg3UtilsCommand
SCSI INQUIRY command using sg_inq.
Source code in sts_libs/src/sts/sg3_utils.py
475 476 477 478 479 480 481 482 483 484 485 486 487 | |
inquiry(device, *, vpd=False)
¶
Perform SCSI INQUIRY on a device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
vpd
|
bool
|
Request Vital Product Data pages |
False
|
Source code in sts_libs/src/sts/sg3_utils.py
480 481 482 483 484 485 486 487 | |
SgLogs
¶
Bases: Sg3UtilsCommand
SCSI LOG SENSE commands using sg_logs.
Source code in sts_libs/src/sts/sg3_utils.py
550 551 552 553 554 555 556 557 558 559 560 561 562 | |
get_log_page(device, page)
¶
Get a log page from a device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
page
|
str
|
Log page identifier (e.g., 'temp', 'ie', 'sp') |
required |
Source code in sts_libs/src/sts/sg3_utils.py
555 556 557 558 559 560 561 562 | |
SgLuns
¶
Bases: Sg3UtilsCommand
SCSI REPORT LUNS commands using sg_luns.
Source code in sts_libs/src/sts/sg3_utils.py
698 699 700 701 | |
SgMap
¶
Bases: Sg3UtilsCommand
SCSI device mapping using sg_map.
Source code in sts_libs/src/sts/sg3_utils.py
634 635 636 637 638 639 640 641 | |
map_devices()
¶
Map SCSI generic devices to block devices.
Source code in sts_libs/src/sts/sg3_utils.py
639 640 641 | |
SgMap26
¶
Bases: Sg3UtilsCommand
SCSI device mapping using sg_map26.
Source code in sts_libs/src/sts/sg3_utils.py
758 759 760 761 | |
SgModes
¶
Bases: Sg3UtilsCommand
SCSI MODE SENSE commands using sg_modes.
Source code in sts_libs/src/sts/sg3_utils.py
704 705 706 707 | |
SgOpcodes
¶
Bases: Sg3UtilsCommand
SCSI REPORT SUPPORTED OPERATION CODES using sg_opcodes.
Source code in sts_libs/src/sts/sg3_utils.py
710 711 712 713 | |
SgPersist
¶
Bases: Sg3UtilsCommand
SCSI Persistent Reservations using sg_persist.
Coordinates access to shared SCSI devices between multiple initiators via registration keys, reservations, and preemption.
Source code in sts_libs/src/sts/sg3_utils.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
get_transport_ids(device)
¶
Get transport IDs of all registered initiators.
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of dicts with 'type' (iscsi/sas/fc/unknown), 'id', and 'key'. |
Source code in sts_libs/src/sts/sg3_utils.py
372 373 374 375 376 377 378 379 380 381 382 383 | |
read_full_status(device)
¶
Read full status (keys and reservation) for a device.
Source code in sts_libs/src/sts/sg3_utils.py
363 364 365 366 367 368 369 370 | |
read_keys(device)
¶
Read all registered reservation keys for a device.
Source code in sts_libs/src/sts/sg3_utils.py
341 342 343 344 345 346 347 348 | |
read_reservation(device)
¶
Read current reservation holder for a device.
Returns:
| Type | Description |
|---|---|
tuple[str | None, str | None]
|
(holder_key, reservation_type) or (None, None) if no reservation |
Source code in sts_libs/src/sts/sg3_utils.py
350 351 352 353 354 355 356 357 358 359 360 361 | |
register(device, key, transport_id=None)
¶
Register a key for persistent reservations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
key
|
str
|
Registration key (e.g., '0xaaaa') |
required |
transport_id
|
str | None
|
Transport-specific identifier (e.g., 'sas,5001405f31c32fa2') |
None
|
Example
persist.register('/dev/sdb', '0xaaaa')
persist.register('/dev/sda', '0x2', transport_id='sas,5001405f31c32fa2')
Source code in sts_libs/src/sts/sg3_utils.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
release(device, key, prout_type=1)
¶
Release a reservation on a device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
key
|
str
|
Reservation key |
required |
prout_type
|
int | str
|
PR type to release (default: 1 = Write Exclusive) |
1
|
Source code in sts_libs/src/sts/sg3_utils.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
report_capabilities(device)
¶
Check whether a device supports persistent reservation operations.
Source code in sts_libs/src/sts/sg3_utils.py
460 461 462 463 464 465 466 467 | |
reserve(device, key, prout_type=1)
¶
Create a reservation on a device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
key
|
str
|
Reservation key (must be previously registered) |
required |
prout_type
|
int | str
|
PR type (default: 1 = Write Exclusive, see PR_TYPE_* constants) |
1
|
Source code in sts_libs/src/sts/sg3_utils.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
unregister(device, key_to_remove)
¶
Delete an existing registration (sets SARK to 0 for the given key).
Source code in sts_libs/src/sts/sg3_utils.py
415 416 417 418 419 420 421 422 423 424 | |
SgRbuf
¶
Bases: Sg3UtilsCommand
SCSI READ BUFFER commands using sg_rbuf.
Source code in sts_libs/src/sts/sg3_utils.py
716 717 718 719 | |
SgRead
¶
Bases: Sg3UtilsCommand
SCSI READ commands using sg_read.
Source code in sts_libs/src/sts/sg3_utils.py
510 511 512 513 514 515 516 517 | |
read_blocks(device, lba, count, block_size=512)
¶
Read blocks from a SCSI device.
Source code in sts_libs/src/sts/sg3_utils.py
515 516 517 | |
SgReadBuffer
¶
Bases: Sg3UtilsCommand
SCSI READ BUFFER commands using sg_read_buffer.
Source code in sts_libs/src/sts/sg3_utils.py
644 645 646 647 | |
SgReadLong
¶
Bases: Sg3UtilsCommand
SCSI READ LONG commands using sg_read_long.
Source code in sts_libs/src/sts/sg3_utils.py
650 651 652 653 | |
SgReadcap
¶
Bases: Sg3UtilsCommand
SCSI READ CAPACITY commands using sg_readcap.
Source code in sts_libs/src/sts/sg3_utils.py
520 521 522 523 524 525 526 527 528 529 530 531 532 | |
get_capacity(device, *, long_format=False)
¶
Get device capacity information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
long_format
|
bool
|
Use READ CAPACITY(16) instead of READ CAPACITY(10) |
False
|
Source code in sts_libs/src/sts/sg3_utils.py
525 526 527 528 529 530 531 532 | |
SgSafte
¶
Bases: Sg3UtilsCommand
SCSI SAF-TE enclosure management using sg_safte.
Source code in sts_libs/src/sts/sg3_utils.py
722 723 724 725 | |
SgSatIdentify
¶
Bases: Sg3UtilsCommand
SATA IDENTIFY DEVICE via SAT using sg_sat_identify.
Source code in sts_libs/src/sts/sg3_utils.py
728 729 730 731 | |
SgScan
¶
Bases: Sg3UtilsCommand
SCSI device scanning using sg_scan.
Source code in sts_libs/src/sts/sg3_utils.py
624 625 626 627 628 629 630 631 | |
scan_devices()
¶
Scan for SCSI devices.
Source code in sts_libs/src/sts/sg3_utils.py
629 630 631 | |
SgSenddiag
¶
Bases: Sg3UtilsCommand
SCSI SEND DIAGNOSTIC commands using sg_senddiag.
Source code in sts_libs/src/sts/sg3_utils.py
565 566 567 568 569 570 571 572 573 574 575 576 577 | |
send_diagnostic(device, test_type='default')
¶
Send diagnostic command to device.
Source code in sts_libs/src/sts/sg3_utils.py
570 571 572 573 574 575 576 577 | |
SgSes
¶
Bases: Sg3UtilsCommand
SCSI Enclosure Services using sg_ses.
Source code in sts_libs/src/sts/sg3_utils.py
734 735 736 737 | |
SgStart
¶
Bases: Sg3UtilsCommand
SCSI START STOP UNIT commands using sg_start.
Source code in sts_libs/src/sts/sg3_utils.py
740 741 742 743 | |
SgSync
¶
Bases: Sg3UtilsCommand
SCSI SYNCHRONIZE CACHE commands using sg_sync.
Source code in sts_libs/src/sts/sg3_utils.py
746 747 748 749 | |
SgTurs
¶
Bases: Sg3UtilsCommand
SCSI TEST UNIT READY commands using sg_turs.
Source code in sts_libs/src/sts/sg3_utils.py
752 753 754 755 | |
SgVerify
¶
Bases: Sg3UtilsCommand
SCSI VERIFY commands using sg_verify.
Source code in sts_libs/src/sts/sg3_utils.py
609 610 611 612 613 614 615 616 | |
verify_blocks(device, lba, count)
¶
Verify blocks on a SCSI device.
Source code in sts_libs/src/sts/sg3_utils.py
614 615 616 | |
SgVpd
¶
Bases: Sg3UtilsCommand
SCSI Vital Product Data using sg_vpd.
Source code in sts_libs/src/sts/sg3_utils.py
490 491 492 493 494 495 496 497 498 499 500 501 502 | |
get_vpd_page(device, page)
¶
Get a specific VPD page from a device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str | Path
|
Device path |
required |
page
|
str
|
VPD page identifier (e.g., 'sn', 'di', 'bl') |
required |
Source code in sts_libs/src/sts/sg3_utils.py
495 496 497 498 499 500 501 502 | |
SgWrite
¶
Bases: Sg3UtilsCommand
SCSI WRITE commands using sg_write_*.
Source code in sts_libs/src/sts/sg3_utils.py
535 536 537 538 539 540 541 542 | |
write_same(device, lba, count, data_pattern='0x00')
¶
Write same data pattern to multiple blocks.
Source code in sts_libs/src/sts/sg3_utils.py
540 541 542 | |
SgWriteBuffer
¶
Bases: Sg3UtilsCommand
SCSI WRITE BUFFER commands using sg_write_buffer.
Source code in sts_libs/src/sts/sg3_utils.py
656 657 658 659 | |
Sginfo
¶
Bases: Sg3UtilsCommand
SCSI device information using sginfo.
Source code in sts_libs/src/sts/sg3_utils.py
680 681 682 683 | |
SgmDd
¶
Bases: Sg3UtilsCommand
SCSI dd utility using sgm_dd (memory-mapped).
Source code in sts_libs/src/sts/sg3_utils.py
668 669 670 671 | |
SgpDd
¶
Bases: Sg3UtilsCommand
SCSI dd utility using sgp_dd (POSIX threads).
Source code in sts_libs/src/sts/sg3_utils.py
674 675 676 677 | |
get_pr_type_name(pr_type)
¶
Get human-readable name for a PR type number.
Source code in sts_libs/src/sts/sg3_utils.py
51 52 53 | |