Disk class

class diskinfo.Disk(disk_name=None, serial_number=None, wwn=None, byid_name=None, bypath_name=None, encoding='utf-8')[source]

The Disk class contains all disk related information. The class can be initialized with specifying one of the five unique identifiers of the disk:

  • a disk name (e.g. sda or nvme0n1) located in /dev/ directory.

  • a disk serial number (e.g. “92837A469FF876”)

  • a disk wwn identifier (e.g. “0x5002638c807270be”)

  • a by-id name of the disk (e.g. “ata-Samsung_SSD_850_PRO_1TB_92837A469FF876”) located in /dev/disk/by-id/ directory

  • a by-path name of the disk (e.g. “pci-0000:00:17.0-ata-3”) located in /dev/disk/by-path/ directory

Based on the specified input parameter the disk will be identified and its attributes will be collected and stored. A ValueError exception will be raised in case of missing or invalid disk identifier. Encoding parameter will be used for processing disk attribute strings.

Operators (<, > and ==) are also implemented for this class to compare different class instances, they use the disk name for comparison.

Note

During the class initialization the disk will not be physically accessed.

Parameters:
  • disk_name (str) – the disk name

  • serial_number (str) – serial number of the disk

  • wwn (str) – wwn identifier of the disk

  • byid_name (str) – by-id name of the disk

  • bypath_name (str) – by-path name of the disk

  • encoding (str) – encoding (default is utf-8)

Raises:

Example

This example shows how to create a Disk class then how to get its path and serial number:

>>> from diskinfo import Disk
>>> d = Disk("sda")
>>> d.get_path()
'/dev/sda'
>>> d.get_serial_number()
'S3D2NY0J819210S'

and here are additional ways how the Disk class can be initialized:

>>> d = Disk(serial_number="92837A469FF876")
>>> d.get_name()
'sdc'
>>> d = Disk(wwn="0x5002539c417223be")
>>> d.get_name()
'sdc'
>>> d = Disk(byid_name="ata-Samsung_SSD_850_PRO_1TB_92837A469FF876")
>>> d.get_name()
'sdc'
>>> d = Disk(bypath_name="pci-0000:00:17.0-ata-3")
>>> d.get_name()
'sdc'
get_byid_path()[source]

Returns disk path in a persistent /dev/disk/by-byid/... form. The result could be one or more path elements in a list.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_byid_path()
['/dev/disk/by-id/ata-Samsung_SSD_850_PRO_1TB_92837A469FF876', '/dev/disk/by-id/wwn-0x5002539c417223be']
Return type:

List[str]

get_bypath_path()[source]

Returns disk path in a persistent /dev/disk/by-path/... form. The result could be one or more path elements in a list.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_bypath_path()
['/dev/disk/by-path/pci-0000:00:17.0-ata-3', '/dev/disk/by-path/pci-0000:00:17.0-ata-3.0']
Return type:

List[str]

get_device_id()[source]

Returns the disk device id in ‘major:minor’ form.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_device_id()
'8:32'
Return type:

str

get_firmware()[source]

Returns the disk firmware string.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_firmware()
'EXM04B6Q'
Return type:

str

get_logical_block_size()[source]

Returns the logical block size of the disk in bytes. Typically, it is 512 bytes.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_logical_block_size()
512
Return type:

int

get_model()[source]

Returns the disk model string.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_model()
'Samsung SSD 850 PRO 1TB'
Return type:

str

get_name()[source]

Returns the disk name.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk(serial_number="92837A469FF876")
>>> d.get_name()
'sdc'
Return type:

str

get_partition_list()[source]

Reads partition information of the disk and returns the list of partitions. See Partition class for more details for a partition entry.

Returns:

list of partitions

Return type:

List[Partition]

Example

>>> from diskinfo import *
>>> disk=Disk("nvme0n1")
>>> plist=disk.get_partition_list()
>>> for item in plist:
...     print(item.get_name())
...
nvme0n1p1
nvme0n1p2
nvme0n1p3
nvme0n1p4
nvme0n1p5
nvme0n1p6
get_partition_table_type()[source]

Returns the type of the partition table on the disk (e.g. mbr or gpt).

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_partition_table_type()
'gpt'
Return type:

str

get_partition_table_uuid()[source]

Returns the UUID of the partition table on the disk.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_partition_table_uuid()
'd3f932e0-7107-455e-a569-9acd5b60d204'
Return type:

str

get_path()[source]

Returns the disk path.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk(serial_number="92837A469FF876")
>>> d.get_path()
'/dev/sdc'

Note

Please note this path is not persistent (i.e. it may refer different physical disk after a reboot).

Return type:

str

get_physical_block_size()[source]

Returns the physical block size of the disk in bytes. Typically, it is 512 bytes for SSDs and NVMEs, and it could be 4096 bytes for HDDs.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_physical_block_size()
512
Return type:

int

get_serial_number()[source]

Returns the disk serial number .

Note

This is a unique and persistent identifier of the disk.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_serial_number()
'92837A469FF876'
Return type:

str

get_size()[source]

Returns the size of the disk in 512-byte units.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> s = d.get_size()
>>> print(f"Disk size: { s * 512 } bytes.")
Disk size: 1024209543168 bytes.
Return type:

int

get_size_in_hrf(units=0)[source]

Returns the size of the disk in a human-readable form.

Parameters:

units (int) –

unit system will be used in result:

  • 0 metric units (default)

  • 1 IEC units

  • 2 legacy units

Read more about units here.

Returns:

size of the disk, proper unit

Return type:

Tuple[float, str]

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> s,u = d.get_size_in_hrf()
>>> print(f"{s:.1f} {u}")
1.0 TB
get_smart_data(nocheck=False, sudo=False, smartctl_path='/usr/sbin/smartctl')[source]

Returns SMART data of the disk. This function will execute smartctl command from smartmontools package, it needs to be installed.

Note

smartctl command needs root priviledge for reading device SMART attributes. This function has to be used as root user or called with sudo=True parameter.

In case of HDDs, the smartctl command will access the disk directly and the HDD could be woken up. If the nocheck=True parameter is used then the current power state of the disk will be preserved.

Parameters:
  • nocheck (bool) – No check should be applied for a HDDs ("-n standby" argument will be used)

  • sudo (bool) – sudo command should be used, default value is False

  • smartctl_path (str) – Path for smartctl command, default value is /usr/sbin/smartctl

Returns:

SMART information of the disk (see more details at DiskSmartData class) or None if smartctl cannot be executed

Return type:

DiskSmartData

Example

The example show the use of the function:

>>> from diskinfo import Disk, DiskSmartData
>>> d = Disk("sda")
>>> d = d.get_smart_data()

In case of SSDs and HDDs the traditional SMART attributes can be accessed via smart_attributes list:

>>> for item in d.smart_attributes:
...     print(f"{item.id:>3d} {item.attribute_name}: {item.raw_value}")
...
  5 Reallocated_Sector_Ct: 0
  9 Power_On_Hours: 6356
 12 Power_Cycle_Count: 2308
177 Wear_Leveling_Count: 2
179 Used_Rsvd_Blk_Cnt_Tot: 0
181 Program_Fail_Cnt_Total: 0
182 Erase_Fail_Count_Total: 0
183 Runtime_Bad_Block: 0
187 Uncorrectable_Error_Cnt: 0
190 Airflow_Temperature_Cel: 28
195 ECC_Error_Rate: 0
199 CRC_Error_Count: 0
235 POR_Recovery_Count: 67
241 Total_LBAs_Written: 9869978356

In case of NVME disks they have their own SMART data in nvme_attributes field:

>>> if d.is_nvme():
...     print(f"Power on hours: {d.nvme_attributes.power_on_hours} h")
...
Power on hours: 1565 h
get_temperature(sudo=False, smartctl_path='/usr/sbin/smartctl')[source]

Returns the current disk temperature. The method will try to read disk temperature from the Linux kernel HWMON interface first then will try to execure the smartctl command. The method has the following requirements:

Disk type

Requirement

SATA HDD/SSD

drivetemp kernel module (Linux kernel version 5.6+) has to be loaded

SCSI/ATA HDD

smartmontools has to be installed

NVME

none

Note

Please note that reading disk temperature from HWMON kernel interface will not access the disk and will not change its power state (e.g. HDD can be in STANDBY state) but reading disk temperature with smartctl will do.

Parameters:
  • sudo (bool) – sudo command should be used for smartctl, default value is False

  • smartctl_path (str) – Path for smartctl command, default value is /usr/sbin/smartctl

Returns:

disk temperature in C degree or None if the temperature cannot be determined

Return type:

float

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_temperature(sudo=True)
28.5
get_type()[source]

Returns the type of the disk. One of the constants in DiskType class:

  • DiskType.HDD for hard disks (with spinning platters)

  • DiskType.SSD for SDDs on SATA or USB interface

  • DiskType.NVME for NVME disks

  • DiskType.LOOP for LOOP disks

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_type()
2
Return type:

int

get_type_str()[source]

Returns the name of the disk type. See the return values in DiskType class:

  • DiskType.HDD_STR for hard disks (with spinning platters)

  • DiskType.SSD_STR for SDDs on SATA or USB interface

  • DiskType.NVME_STR for NVME disks

  • DiskType.LOOP_STR for LOOP disks

Raises:

RuntimeError – in case of unknown disk type.

Return type:

str

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_type_str()
'SSD'
get_wwn()[source]

Returns the world-wide name (WWN) of the disk. Read more about WWN here.

Note

This is a unique and persistent identifier of the disk.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.get_wwn()
'0x5002539c417223be'
Return type:

str

is_hdd()[source]

Returns True if the disk type is HDD, otherwise False.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.is_hdd()
False
Return type:

bool

is_loop()[source]

Returns True if the disk type is LOOP, otherwise False.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("loop0")
>>> d.is_loop()
True
Return type:

bool

is_nvme()[source]

Returns True if the disk type is NVME, otherwise False.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.is_nvme()
False
Return type:

bool

is_ssd()[source]

Returns True if the disk type is SSD, otherwise False.

Example

An example about the use of this function:

>>> from diskinfo import Disk
>>> d = Disk("sdc")
>>> d.is_ssd()
True
Return type:

bool