drivetempでハードディスクの温度を確認する

先日Debian sid amd64環境でapt upgradeしたときにhddtempのNEWSがあることに気づきました.

$ zcat /usr/share/doc/hddtemp/NEWS.Debian.gz
hddtemp (0.3-beta15-54) unstable; urgency=medium

  hddtemp has been dead upstream for many years and is therefore in a minimal
  maintenance mode. It will be shipped in the Debian Bullseye release, but
  will not be present in the Debian Bookworm release.

  Nowadays the 'drivetemp' kernel module is a better alternative. It uses the
  Linux Hardware Monitoring kernel API (hwmon), so the temperature is returned
  the same way and using the same tools as other sensors.

  Loading this module is as easy as creating a file in the /etc/modules-load.d
  directory:

    echo drivetemp > /etc/modules-load.d/drivetemp.conf

 -- Aurelien Jarno <aurel32@debian.org>  Tue, 02 Feb 2021 20:27:44 +0100

hddtempは対応しているストレージの温度を取得できます.

$ sudo hddtemp /dev/sda
/dev/sda: Seagate BarraCuda SSD ZA0100MC0100 2    : 41°C

しかしこのNEWSによると hddtemp はもう上流でメンテナンスされていないのでDebianの次期バージョンの Bullseye には入るけどその次の Bookworm からは除かれる予定のようです.

そして drivetemp というカーネルモジュールが代替になるとのこと.

てことで少し試してみました.

drivetempについてはkernelのドキュメントを確認します.

例 1. zcat /usr/share/doc/linux-doc-5.10/Documentation/hwmon/drivetemp.rst.gz | rst2html | w3m -T text/html

Kernel driver drivetemp

References

ANS T13/1699-D Information technology – AT Attachment 8 – ATA/ATAPI Command Set
(ATA8-ACS)

ANS Project T10/BSR INCITS 513 Information technology – SCSI Primary Commands –
4 (SPC-4)

ANS Project INCITS 557 Information technology – SCSI / ATA Translation – 5
(SAT-5)

Description

This driver supports reporting the temperature of disk and solid state drives
with temperature sensors.

If supported, it uses the ATA SCT Command Transport feature to read the current
drive temperature and, if available, temperature limits as well as historic
minimum and maximum temperatures. If SCT Command Transport is not supported,
the driver uses SMART attributes to read the drive temperature.

Usage Note

Reading the drive temperature may reset the spin down timer on some drives.
This has been observed with WD120EFAX drives, but may be seen with other drives
as well. The same behavior is observed if the ‘hdtemp’ or ‘smartd’ tools are
used to access the drive. With the WD120EFAX drive, reading the drive
temperature using the drivetemp driver is still possible after it
transitioned to standby mode, and reading the drive temperature in this mode
will not cause the drive to change its mode (meaning the drive will not spin
up). It is unknown if other drives experience similar behavior.

A known workaround for WD120EFAX drives is to read the drive temperature at
intervals larger than twice the spin-down time. Otherwise affected drives will
never spin down.

Sysfs entries

Only the temp1_input attribute is always available. Other attributes are
available only if reported by the drive. All temperatures are reported in
milli-degrees Celsius.

┌─────────────┬───────────────────────────────────────────────────────────────┐
│temp1_input │Current drive temperature │
├─────────────┼───────────────────────────────────────────────────────────────┤
│temp1_lcrit │Minimum temperature limit. Operating the device below this │
│ │temperature may cause physical damage to the device. │
├─────────────┼───────────────────────────────────────────────────────────────┤
│temp1_min │Minimum recommended continuous operating limit │
├─────────────┼───────────────────────────────────────────────────────────────┤
│temp1_max │Maximum recommended continuous operating temperature │
├─────────────┼───────────────────────────────────────────────────────────────┤
│temp1_crit │Maximum temperature limit. Operating the device above this │
│ │temperature may cause physical damage to the device. │
├─────────────┼───────────────────────────────────────────────────────────────┤
│temp1_lowest │Minimum temperature seen this power cycle │
├─────────────┼───────────────────────────────────────────────────────────────┤
│temp1_highest│Maximum temperature seen this power cycle │
└─────────────┴───────────────────────────────────────────────────────────────┘

drivetempを読み込むとSysfs で temp1_input として出てくるようです.

現在のセンサーの一覧を取得します.
$ find /sys/ -name "temp1_input" > /tmp/before.list 2>/dev/null
$ cat /tmp/before.list
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon3/temp1_input
/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp1_input
/sys/devices/virtual/thermal/thermal_zone0/hwmon0/temp1_input
drivetemp モジュールの読み込み
$ sudo modprobe -v drivetemp
insmod /lib/modules/5.10.0-3-amd64/kernel/drivers/hwmon/drivetemp.ko
Note
永続化したい場合は
$ echo drivetemp | sudo tee /etc/modules-load.d/drivetemp.conf
増えたセンサを確認
$ find /sys/ -name "temp1_input" 2>/dev/null | diff -u /tmp/before.list -
--- /tmp/before.list    2021-02-05 01:45:58.691517588 +0900
+++ -   2021-02-05 01:46:00.178371154 +0900
@@ -1,3 +1,4 @@
 /sys/devices/platform/thinkpad_hwmon/hwmon/hwmon3/temp1_input
 /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp1_input
+/sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon6/temp1_input
 /sys/devices/virtual/thermal/thermal_zone0/hwmon0/temp1_input
温度を確認
$ cat /sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon6/temp1_input
44000
$ sudo hddtemp /dev/sda
/dev/sda: Seagate BarraCuda SSD ZA0100MC0100 2    : 44°C

hddtemp と同じ結果が取得できました.同じなので drivetemp の値は摂氏のようですね :)

とりあえず温度は取得できましたがデバイスから温度を取得したりデーモンなどは使えないのでそのへんは少し考えないといけなさそうです.

HDDのS.M.A.R.Tから温度を取得して表示してくれるhddtemp

hddtempというコマンドを知りました.熱いし熱が気になる時期だしお手軽に温度を知れるのは良さそうと試してみました.

導入

$ sudo apt install hddtemp

help

$ hddtemp -h
 Usage: hddtemp [OPTIONS] [TYPE:]DISK1 [[TYPE:]DISK2]...
 
   hddtemp displays the temperature of drives supplied in argument.
   Drives must support S.M.A.R.T.
 
  TYPE could be SATA, PATA or SCSI. If omitted hddtemp will try to guess.
 
  -b   --drivebase   :  display database file content that allow hddtemp to
                        recognize supported drives.
  -D   --debug       :  display various S.M.A.R.T. fields and their values.
                        Useful to find a value that seems to match the
                        temperature and/or to send me a report.
                        (done for every drive supplied).
  -d   --daemon      :  run hddtemp in TCP/IP daemon mode (port 7634 by default.)
  -f   --file=FILE   :  specify database file to use.
  -F   --foreground  :  don't daemonize, stay in foreground.
  -l   --listen=addr :  listen on a specific interface (in TCP/IP daemon mode).
  -n   --numeric     :  print only the temperature.
  -p   --port=#      :  port to listen to (in TCP/IP daemon mode).
  -s   --separator=C :  separator to use between fields (in TCP/IP daemon mode).
  -S   --syslog=s    :  log temperature to syslog every s seconds.
  -u   --unit=[C|F]  :  force output temperature either in Celsius or Fahrenheit.
  -q   --quiet       :  do not check if the drive is supported.
  -v   --version     :  display hddtemp version number.
  -w   --wake-up     :  wake-up the drive if need.
  -4                 :  listen on IPv4 sockets only.
  -6                 :  listen on IPv6 sockets only.
 
Report bugs or new drives to <hddtemp@guzu.net>.
hddtemp version 0.3-beta15

デバイスを指定すると温度が取得できます./dev/sd[a-z]とか/dev/sd?とかも使えました.
/dev/sdd, /dev/sdeはS.M.A.R.Tを使えないUSBアダプタ経由で繋いでいるので取得できませんでした.

$ sudo hddtemp /dev/sda
/dev/sda: VB0250EAVER: 37°C
$ sudo hddtemp /dev/sda /dev/sdb
/dev/sda: VB0250EAVER: 38°C
/dev/sdb: Hitachi HDS5C3030ALA630: 39°C
$ sudo hddtemp /dev/sd?
/dev/sda: VB0250EAVER: 37°C
/dev/sdb: Hitachi HDS5C3030ALA630: 37°C
/dev/sdc: WDC WD30EZRX-00MMMB0: 40°C
/dev/sdd: WDC WD30EZRX-00DC0B0: S.M.A.R.T. not available
/dev/sde: TOSHIBA DT01ACA300: S.M.A.R.T. not available

デーモン化もできます.tcp 7634にアクセスると値が取得できます.

$ sudo hddtemp -d /dev/sda
$ nc localhost 7634
|/dev/sda|VB0250EAVER|37|C|

停止

$ pgrep hddtemp
11832
$ sudo kill 11832

複数デバイスも行けるけどそのままでは見にくいですね.

$ sudo hddtemp -d /dev/sd?
$ nc localhost 7634
|/dev/sda|VB0250EAVER|38|C||/dev/sdb|Hitachi HDS5C3030ALA630|39|C||/dev/sdc|WDC WD30EZRX-00MMMB0|41|C||/dev/sdd|WDC WD30EZRX-00DC0B0|NA|*||/dev/sde|TOSHIBA DT01ACA300|NA|*|
$ nc localhost 7634|sed -e 's/||/|\n|/g'
|/dev/sda|VB0250EAVER|37|C|
|/dev/sdb|Hitachi HDS5C3030ALA630|38|C|
|/dev/sdc|WDC WD30EZRX-00MMMB0|40|C|
|/dev/sdd|WDC WD30EZRX-00DC0B0|NA|*|
|/dev/sde|TOSHIBA DT01ACA300|NA|*|

S.M.A.R.Tの値を元にしているのでこのツールを使わず大抵の環境で入っているsmartctlでも良い気もします.

$ sudo smartctl -a /dev/sda|grep -i temp
190 Airflow_Temperature_Cel 0x0022   063   056   045    Old_age   Always       -       37 (Min/Max 31/43)
194 Temperature_Celsius     0x0022   037   044   000    Old_age   Always       -       37 (0 11 0 0 0)

環境

$ lsb_release -d
Description:    Ubuntu 16.04.4 LTS
$ uname -m
x86_64
$ dpkg-query -W hddtemp smartmontools
hddtemp 0.3-beta15-52
smartmontools   6.4+svn4214-1