Get drive name from hdparm

This commit is contained in:
desbma
2015-01-31 21:24:59 +01:00
parent 7c4fdeb583
commit de8a45a449
2 changed files with 28 additions and 4 deletions
+16 -3
View File
@@ -41,18 +41,31 @@ class Drive:
DriveState = enum.Enum("DriveState", ("UNKNOWN", "ACTIVE_IDLE", "STANDBY", "SLEEPING"))
HDPARM_GET_TEMP_HITACHI_REGEX = re.compile("drive temperature \(celsius\) is:\s*([0-9]*)")
HDPARM_GET_MODEL_REGEX = re.compile("Model Number:\s*(.*)")
def __init__(self, device_filepath, stat_filepath, hddtemp_daemon_port):
assert(stat.S_ISBLK(os.stat(device_filepath).st_mode))
self.device_filepath = device_filepath
self.device_filepath = __class__.normalizeDrivePath(device_filepath)
self.stat_filepath = stat_filepath
self.hddtemp_daemon_port = hddtemp_daemon_port
self.pretty_name = self.getPrettyName()
self.logger = logging.getLogger(str(self))
self.supports_hitachi_temp_query = self.supportsHitachiTempQuery()
def __str__(self):
""" Return a pretty drive name. """
return os.path.basename(self.device_filepath).rsplit("-", 1)[-1]
return self.pretty_name
def getPrettyName(self):
""" Return a pretty drive name. """
# get device metadata to grab model string
cmd = ("hdparm", "-I", self.device_filepath)
output = subprocess.check_output(cmd,
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
universal_newlines=True)
model = __class__.HDPARM_GET_MODEL_REGEX.search(output).group(1).strip()
return "%s %s" % (os.path.basename(self.device_filepath), model)
def supportsHitachiTempQuery(self):
# test if drive supports hdparm -H
@@ -109,7 +122,7 @@ class Drive:
if not drive_data:
break
drive_path = drive_data[1]
if __class__.normalizeDrivePath(drive_path) == __class__.normalizeDrivePath(self.device_filepath):
if __class__.normalizeDrivePath(drive_path) == self.device_filepath:
temp_unit = drive_data[4]
if temp_unit != "C":
raise RuntimeError("hddtemp daemon is not returning temp as Celsius")
+12 -1
View File
@@ -37,10 +37,12 @@ class TestDrive(unittest.TestCase):
def setUp(self):
with unittest.mock.patch("hddfancontrol.os.stat") as os_stat_mock, \
unittest.mock.patch("hddfancontrol.stat") as stat_mock, \
unittest.mock.patch("hddfancontrol.subprocess") as subprocess_check_call_mock:
unittest.mock.patch("hddfancontrol.subprocess") as subprocess_check_call_mock, \
unittest.mock.patch("hddfancontrol.Drive.getPrettyName") as drive_getPrettyName:
os_stat_mock.return_value = os.stat_result
stat_mock.stat.S_IFBLK.return_value = True
subprocess_check_call_mock.side_effect = subprocess.CalledProcessError(0, "")
drive_getPrettyName.return_value = "drive_name"
self.drive = hddfancontrol.Drive("/dev/sdz", "/dummy", None)
self.hddtemp_daemon = None
@@ -50,6 +52,15 @@ class TestDrive(unittest.TestCase):
self.hddtemp_daemon.server.server_close()
self.hddtemp_daemon.join()
def test_getPrettyName(self):
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\n\nATA device, with non-removable media\n\tModel Number: WDC WD4003FZEX-00Z4SA0 \n\tSerial Number: WD-WMC5D0D4YY1K\n\tFirmware Revision: 01.01A01\n\tTransport: Serial, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6, SATA Rev 3.0\nStandards:\n\tSupported: 9 8 7 6 5 \n\tLikely used: 9\nConfiguration:\n\tLogical\t\tmax\tcurrent\n\tcylinders\t16383\t16383\n\theads\t\t16\t16\n\tsectors/track\t63\t63\n\t--\n\tCHS current addressable sectors: 16514064\n\tLBA user addressable sectors: 268435455\n\tLBA48 user addressable sectors: 7814037168\n\tLogical Sector size: 512 bytes\n\tPhysical Sector size: 4096 bytes\n\tLogical Sector-0 offset: 0 bytes\n\tdevice size with M = 1024*1024: 3815447 MBytes\n\tdevice size with M = 1000*1000: 4000787 MBytes (4000 GB)\n\tcache/buffer size = unknown\n\tNominal Media Rotation Rate: 7200\nCapabilities:\n\tLBA, IORDY(can be disabled)\n\tQueue depth: 32\n\tStandby timer values: spec'd by Standard, with device specific minimum\n\tR/W multiple sector transfer: Max = 16\tCurrent = 0\n\tDMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6 \n\t Cycle time: min=120ns recommended=120ns\n\tPIO: pio0 pio1 pio2 pio3 pio4 \n\t Cycle time: no flow control=120ns IORDY flow control=120ns\nCommands/features:\n\tEnabled\tSupported:\n\t *\tSMART feature set\n\t \tSecurity Mode feature set\n\t *\tPower Management feature set\n\t *\tWrite cache\n\t *\tLook-ahead\n\t *\tHost Protected Area feature set\n\t *\tWRITE_BUFFER command\n\t *\tREAD_BUFFER command\n\t *\tNOP cmd\n\t *\tDOWNLOAD_MICROCODE\n\t \tPower-Up In Standby feature set\n\t *\tSET_FEATURES required to spinup after power up\n\t \tSET_MAX security extension\n\t *\t48-bit Address feature set\n\t *\tMandatory FLUSH_CACHE\n\t *\tFLUSH_CACHE_EXT\n\t *\tSMART error logging\n\t *\tSMART self-test\n\t *\tGeneral Purpose Logging feature set\n\t *\t64-bit World wide name\n\t *\t{READ,WRITE}_DMA_EXT_GPL commands\n\t *\tSegmented DOWNLOAD_MICROCODE\n\t *\tGen1 signaling speed (1.5Gb/s)\n\t *\tGen2 signaling speed (3.0Gb/s)\n\t *\tGen3 signaling speed (6.0Gb/s)\n\t *\tNative Command Queueing (NCQ)\n\t *\tHost-initiated interface power management\n\t *\tPhy event counters\n\t *\tNCQ priority information\n\t *\tREAD_LOG_DMA_EXT equivalent to READ_LOG_EXT\n\t *\tDMA Setup Auto-Activate optimization\n\t *\tSoftware settings preservation\n\t *\tSMART Command Transport (SCT) feature set\n\t *\tSCT Write Same (AC2)\n\t *\tSCT Features Control (AC4)\n\t *\tSCT Data Tables (AC5)\n\t \tunknown 206[12] (vendor specific)\n\t \tunknown 206[13] (vendor specific)\n\t \tunknown 206[14] (vendor specific)\nSecurity: \n\tMaster password revision code = 65534\n\t\tsupported\n\tnot\tenabled\n\tnot\tlocked\n\tnot\tfrozen\n\tnot\texpired: security count\n\t\tsupported: enhanced erase\n\t424min for SECURITY ERASE UNIT. 424min for ENHANCED SECURITY ERASE UNIT. \nLogical Unit WWN Device Identifier: 50014ee0593d4632\n\tNAA\t\t: 5\n\tIEEE OUI\t: 0014ee\n\tUnique ID\t: 0593d4632\nChecksum: correct\n"
self.assertEqual(self.drive.getPrettyName(), "sdz WDC WD4003FZEX-00Z4SA0")
subprocess_check_output_mock.assert_called_once_with(("hdparm", "-I", "/dev/sdz"),
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
universal_newlines=True)
def test_getState(self):
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.return_value = "\n/dev/sdz:\n drive state is: active/idle\n"