LinuxコマンドラインからHDDのsetmaxを解除する

だいぶ前に試せずに終わったので、リトライする。


HDDのsetmaxを解除する
http://d.hatena.ne.jp/kinneko/20151201/p76


テスト環境は、不本意ながら、CentOS7。


見た目は、1TBのドライブ。

# fdisk -l /dev/sda

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes, 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048      419839      208896   83  Linux
/dev/sda2          419840     1241087      410624   83  Linux
/dev/sda3         1241088     3338239     1048576   82  Linux swap / Solaris
/dev/sda4         3338240  1953523711   975092736    5  Extended
/dev/sda5         3340288     4401151      530432   83  Linux
/dev/sda6         4403200  1953523711   974560256   83  Linux

実際は2TBのドライブがsetmaxされて1GBに見えている。
中身の構成は、某NASのドライブの1つのようだね。

# hdparm -N /dev/sda

/dev/sda:
 max sectors   = 1953525168/3907029168, HPA is enabled
# hdparm -N -p3907029168 /dev/sda

/dev/sda:
 attempting to set PIO mode to -387938128
 HDIO_SET_PIO_MODE failed: Inappropriate ioctl for device
 max sectors   = 1953525168/3907029168, HPA is enabled

ん? -pだとPIO強制モード指定だから、オプション間違ってる?
コマンドのヘルプには詳しいことは書いてないな。

-N Get/set max visible number of sectors (HPA) (VERY DANGEROUS)

直接数字指定して出来そうな感じ。

# hdparm -N 3907029168 /dev/sda

/dev/sda:
 setting max visible sectors to 3907029168 (temporary)
 max sectors   = 3907029168/3907029168, HPA is disabled
# hdparm -N /dev/sda

/dev/sda:
 max sectors   = 3907029168/3907029168, HPA is disabled

直後はfdiskの中身は変わっていない。

# fdisk -l /dev/sda

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes, 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00000000
(snip)

SATAでの再認識が面倒なので、rebootするのが楽だけど、今回はコマンドで取り外し。

# cat /sys/class/scsi_device/2\:0\:0\:0/device/model
WDC WD20EARX-00P
# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/delete

ドライブを挿し直す。
dmesgで認識を確認。

[ 6018.835467] ata3.00: HPA detected: current 1953525168, native 3907029168
[ 6018.835570] ata3.00: ATA-8: WDC WD20EARX-00PASB0, 51.0AB51, max UDMA/133
[ 6018.835580] ata3.00: 1953525168 sectors, multi 0: LBA48 NCQ (depth 31/32), AA
[ 6018.843671] ata3.00: configured for UDMA/133
[ 6018.843691] ata3: EH complete
[ 6018.843932] scsi 2:0:0:0: Direct-Access     ATA      WDC WD20EARX-00P AB51 PQ: 0 ANSI: 5
[ 6018.846720] sd 2:0:0:0: [sda] 1953525168 512-byte logical blocks: (1.00 TB/931 GiB)
[ 6018.846734] sd 2:0:0:0: [sda] 4096-byte physical blocks
[ 6018.846926] sd 2:0:0:0: Attached scsi generic sg0 type 0
[ 6018.847441] sd 2:0:0:0: [sda] Write Protect is off
[ 6018.847459] sd 2:0:0:0: [sda] Mode Sense: 00 3a 00 00
[ 6018.847591] sd 2:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[ 6018.894591]  sda: sda1 sda2 sda3 sda4 < sda5 sda6 >
[ 6018.896531] sd 2:0:0:0: [sda] Attached SCSI disk

書き換わってないな。

# fdisk -l /dev/sda

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes, 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00000000
# hdparm -N /dev/sda

/dev/sda:
 max sectors   = 1953525168/3907029168, HPA is enabled

戻ってるし。
なんか書き込みコマンドがあるのかな?

# hdparm -N 3907029168 /dev/sda

/dev/sda:
 setting max visible sectors to 3907029168 (temporary)
 max sectors   = 3907029168/3907029168, HPA is disabled
# hdparm -V
hdparm v9.43

しょうがないのでmanを見る。

-N Get/set max visible number of sectors, also known as the Host
Protected Area setting. Without a parameter, -N displays the
current setting, which is reported as two values: the first
gives the current max sectors setting, and the second shows the
native (real) hardware limit for the disk. The difference
between these two values indicates how many sectors of the disk
are currently hidden from the operating system, in the form of a
Host Protected Area (HPA). This area is often used by computer
makers to hold diagnostic software, and/or a copy of the
originally provided operating system for recovery purposes.
Another possible use is to hide the true capacity of a very
large disk from a BIOS/system that cannot normally cope with
drives of that size (eg. most current {2010} BIOSs cannot deal
with drives larger than 2TB, so an HPA could be used to cause a
3TB drive to report itself as a 2TB drive). To change the
current max (VERY DANGEROUS, DATA LOSS IS EXTREMELY LIKELY), a
new value should be provided (in base10) immediately following
the -N option. This value is specified as a count of sectors,
rather than the "max sector address" of the drive. Drives have
the concept of a temporary (volatile) setting which is lost on
the next hardware reset, as well as a more permanent (non-
volatile) value which survives resets and power cycles. By
default, -N affects only the temporary (volatile) setting. To
change the permanent (non-volatile) value, prepend a leading p
character immediately before the first digit of the value.
Drives are supposed to allow only a single permanent change per
session. A hardware reset (or power cycle) is required before
another permanent -N operation can succeed. Note that any
attempt to set this value may fail if the disk is being accessed
by other software at the same time. This is because setting the
value requires a pair of back-to-back drive commands, but there
is no way to prevent some other command from being inserted
between them by the kernel. So if it fails initially, just try
again. Kernel support for -N is buggy for many adapter types
across many kernel versions, in that an incorrect (too small)
max size value is sometimes reported. As of the 2.6.27 kernel,
this does finally seem to be working on most hardware.

p付けないと、一時的な変更になるって書いてあるわ。

# hdparm -N p3907029168 /dev/sda

/dev/sda:
 setting max visible sectors to 3907029168 (permanent)
 max sectors   = 3907029168/3907029168, HPA is disabled

外して挿し直し。

# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/delete
(挿し直し)
# cat /sys/class/scsi_device/2\:0\:0\:0/device/model
WDC WD20EARX-00P
# hdparm -N /dev/sda

/dev/sda:
 max sectors   = 3907029168/3907029168, HPA is disabled
# fdisk -l /dev/sda

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x00000000

書き換わっている。