Disk class
- class diskinfo.Disk(disk_name=None, serial_number=None, wwn=None, byid_name=None, bypath_name=None, _device=None)[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
ValueErrorexception will be raised in case of missing or invalid disk identifier.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:
- Raises:
ValueError – in case of missing or invalid parameters
RuntimeError – in case of any system error
Example
This example shows how to create a
Diskclass 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
Diskclass 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-id/...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"]
- 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"]
- 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:
- get_filesystem()[source]
Returns the
FileSystemobject if the disk has a raw filesystem (i.e. a filesystem created directly on the block device without a partition table). ReturnsNoneif no raw filesystem is detected.- Returns:
filesystem information or None
- Return type:
Union[FileSystem, None]
Example
An example about the use of this function:
>>> from diskinfo import Disk >>> d = Disk("sdb") >>> fs = d.get_filesystem() >>> if fs: ... print(fs.get_fs_type()) ... ext4
- 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:
- get_logical_block_size()[source]
Returns the logical block size of the disk in bytes. Typical value 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:
- 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:
- 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:
- get_partition_list()[source]
Returns the list of partitions on the disk. See
Partitionclass for more details about 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:
- 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:
- 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:
- get_physical_block_size()[source]
Returns the physical block size of the disk in bytes. Typical value is 512 bytes for SSDs/NVMEs, and 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:
- 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:
- 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:
- 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:
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 the
smartctlcommand from the smartmontools package. It needs to be installed.Note
smartctlcommand needs root privilege for reading device SMART attributes. This function has to be used as root user or called withsudo=Trueparameter.In case of HDDs, the
smartctlcommand will access the disk directly and the HDD could be woken up. If thenocheck=Trueparameter is used then the current power state of the disk will be preserved.- Parameters:
- Returns:
SMART information of the disk (see more details at
DiskSmartDataclass) or None if smartctl cannot be executed- Return type:
Example
The following example shows 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_attributeslist:>>> 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_attributesfield:>>> 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 execute 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:
- Returns:
disk temperature in degrees Celsius or None if the temperature cannot be determined
- Return type:
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
DiskTypeclass:DiskType.HDDfor hard disks (with spinning platters)DiskType.SSDfor SDDs on SATA or USB interfaceDiskType.NVMEfor NVME disksDiskType.LOOPfor LOOP disks
Example
An example about the use of this function:
>>> from diskinfo import Disk >>> d = Disk("sdc") >>> d.get_type() 2
- Return type:
- get_type_str()[source]
Returns the name of the disk type. See the return values in
DiskTypeclass:DiskType.HDD_STRfor hard disks (with spinning platters)DiskType.SSD_STRfor SDDs on SATA or USB interfaceDiskType.NVME_STRfor NVME disksDiskType.LOOP_STRfor LOOP disks
- Raises:
RuntimeError – in case of unknown disk type.
- Return type:
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 a disk.
Example
An example about the use of this function:
>>> from diskinfo import Disk >>> d = Disk("sdc") >>> d.get_wwn() "0x5002539c417223be"
- Return type:
- has_filesystem()[source]
Returns
Trueif the disk has a raw filesystem (i.e. a filesystem created directly on the block device without a partition table), otherwiseFalse.Example
An example about the use of this function:
>>> from diskinfo import Disk >>> d = Disk("sdb") >>> d.has_filesystem() True
- Return type:
- 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:
- 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: