User:Tingo/sg3 utils

From NDWiki
< User:Tingo
Revision as of 18:28, 7 September 2016 by Tingo (talk | contribs) (→‎Resurrection: added link to setblocksize subpage)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The sg3_utils package contains utilities that send SCSI commands to devices. It contains lots of programs for interacting with SCSI devices, one of those programs is sg_format., which can be used to format, resize or brick a SCSI hard drive.

Trying sg_format on a sacrificial drive

I use a machine with an old version of FreeBSD installed

root@music1# uname -a
FreeBSD music1.kg4.no 7.0-STABLE FreeBSD 7.0-STABLE #0: Fri Aug 15 12:56:35 CEST 2008     root@music1.kg4.no:/usr/obj/usr/src/sys/GENERIC  i386

It has an old version of sg3_utils installed

root@music1# portversion -vF sg3*
sg3_utils-1.25              =  up-to-date with port 

Drive info

The drive I tested on is an old Seagate ST31200N. FreeBSD detects it like so:

root@music1# dmesg | grep da0
da0 at ahc0 bus 0 target 0 lun 0
da0: <SEAGATE ST31200N 8008> Fixed Direct Access SCSI-2 device
da0: 10.000MB/s transfers (10.000MHz, offset 15)
da0: Command Queueing Enabled
da0: 1006MB (2061108 512 byte sectors: 64H 32S/T 1006C)

and

root@music1# camcontrol devlist
<SEAGATE ST31200N 8008>            at scbus0 target 0 lun 0 (pass0,da0)

Before testing

read capacity

root@music1# sg_readcap /dev/da0
Read Capacity results:
   Last logical block address=2061107 (0x1f7333), Number of blocks=2061108
   Logical block length=512 bytes
Hence:
   Device size: 1055287296 bytes, 1006.4 MiB, 1.06 GB

read format

root@music1# sg_format /dev/da0
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=512 [0x200]
Read Capacity (10) results:
   Number of blocks=2061108
   Block size=512 bytes
No changes made. To format use '--format'. To resize use '--resize'

Test: try to format it

Try a format, with 1024 as size

root@music1# sg_format --format --size=1024 /dev/da0
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=512 [0x200]

A FORMAT will commence in 10 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
A FORMAT will commence in 5 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
Format command, illegal parameter
FORMAT failed
    try '-v' for more information

failed, try with '-v'

root@music1# sg_format -v --format --size=1024 /dev/da0
    inquiry cdb: 12 00 00 00 24 00
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
      PROTECT=0
    mode sense (10) cdb: 5a 00 01 00 00 00 00 00 fc 00
    mode sense (10): requested 252 bytes but got 28 bytes
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=1024 [0x400]

A FORMAT will commence in 10 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
A FORMAT will commence in 5 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
    format cdb: 04 18 00 00 00 00
format unit:  Fixed format, current;  Sense key: Illegal Request
 Additional sense: Invalid field in parameter list
  Field replaceable unit code: 4
  Sense Key Specific: Error in Data byte 1 bit 1
Format command, illegal parameter
FORMAT failed

try with '-vvv'

root@music1# sg_format -vvv --format --size=1024 /dev/da0
    inquiry cdb: 12 00 00 00 24 00
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
      PROTECT=0
    mode sense (10) cdb: 5a 00 01 00 00 00 00 00 fc 00
    mode sense (10): requested 252 bytes but got 28 bytes
    mode sense (10): response
        00 1a 00 00 00 00 00 08  00 00 00 00 00 00 04 00   
        81 0a 00 21 30 00 00 00  16 00 ff ff               
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=1024 [0x400]

A FORMAT will commence in 10 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
A FORMAT will commence in 5 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
    format cdb: 04 18 00 00 00 00
    format parameter list:
        00 02 00 00                                        
format unit:  Fixed format, current;  Sense key: Illegal Request
 Additional sense: Invalid field in parameter list
  Field replaceable unit code: 4
  Sense Key Specific: Error in Data byte 1 bit 1
 Raw sense data (in hex):
        70 00 05 00 00 00 00 0a  00 00 00 00 26 00 04 89   
        00 01                                              
Format command, illegal parameter
FORMAT failed

not sure what the real problem is. Perhaps the drive doesn't support the format unit command, or it didn't like the 1024 for size.

It still reports 1024 as block size

root@music1# sg_format /dev/da0
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=1024 [0x400]
No changes made. To format use '--format'. To resize use '--resize'

Ok. Can I format it back to 512 block size then?

root@music1# sg_format --format --size=512 /dev/da0
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=1024 [0x400]

A FORMAT will commence in 10 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
A FORMAT will commence in 5 seconds
    ALL data on /dev/da0 will be DESTROYED
        Press control-C to abort
Format command, illegal parameter
FORMAT failed
    try '-v' for more information

No, that didn't work either. I also tried other variations of the sg_format command; none did work.

The aftermath: a bricked drive

After I rebooted the the machine, I got the usual drive info in dmesg output:

da0 at ahc0 bus 0 target 0 lun 0
da0: <SEAGATE ST31200N 8008> Fixed Direct Access SCSI-2 device
da0: 10.000MB/s transfers (10.000MHz, offset 15)
da0: Command Queueing Enabled

followed by the (more unusual)

da0: Attempt to query device size failed: MEDIUM ERROR, Medium format corrupted
(da0:ahc0:0:0:0): READ CAPACITY. CDB: 25 0 0 0 0 0 0 0 0 0
(da0:ahc0:0:0:0): CAM Status: SCSI Status Error
(da0:ahc0:0:0:0): SCSI Status: Check Condition
(da0:ahc0:0:0:0): MEDIUM ERROR asc:31,0
(da0:ahc0:0:0:0): Medium format corrupted field replaceable unit: 1
(da0:ahc0:0:0:0): Retrying Command (per Sense Data)

and this repeats a good number of times.

read capacity says that the drive is broken

root@music1# sg_readcap /dev/da0
READ CAPACITY (10) failed [res=-1], try with '-v'
root@music1# sg_readcap -v /dev/da0
    read capacity (10) cdb: 25 00 00 00 00 00 00 00 00 00
read capacity (10):  Fixed format, current;  Sense key: Medium Error
 Additional sense: Medium format corrupted
  Field replaceable unit code: 1

sg_format says that the drive is back to 512 block size

root@music1# sg_format /dev/da0
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=512 [0x200]
No changes made. To format use '--format'. To resize use '--resize'

So that was a bricked drive.

Other tools

setblocksize by Michael Baeuerle - this one is written for Seagate disks, and has been tested on ST31200N. It is Linux only

Resurrection

As it turns out, everything was not lost. With the help of a machine running Linux (Fedora 24 server) and setblocksize, I was able to bring the drive back to life - with 1024 byte block size. dmesg output:

root@music1# dmesg | grep da0
da0 at ahc0 bus 0 target 0 lun 0
da0: <SEAGATE ST31200N 8008> Fixed Direct Access SCSI-2 device 
da0: 10.000MB/s transfers (10.000MHz, offset 15)
da0: Command Queueing Enabled
da0: 1049MB (1074591 1024 byte sectors: 255H 63S/T 66C)

Here is what the sg tools thinks bout the drive after it has been changed.

sg_format

root@music1# sg_format /dev/da0
    SEAGATE   ST31200N          8008   peripheral_type: disk [0x0]
Mode Sense (block descriptor) data, prior to changes:
  Number of blocks=0 [0x0]
  Block size=1024 [0x400]
Read Capacity (10) results:
   Number of blocks=1074591
   Block size=1024 bytes
No changes made. To format use '--format'. To resize use '--resize'

looks good.

sg_readcap

root@music1# sg_readcap /dev/da0
Read Capacity results:
   Last logical block address=1074590 (0x10659e), Number of blocks=1074591
   Logical block length=1024 bytes
Hence:
   Device size: 1100381184 bytes, 1049.4 MiB, 1.10 GB

looks so much better than it did after sg_format had mangled it.