giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.38k forks source link

Extend disk_partitions() to get disk name / ID #1609

Open nicolargo opened 5 years ago

nicolargo commented 5 years ago

It would be fine if PsUtil could return the disk id.

The information is available in the /dev/disk/by-id/ folder.

Example on my Ubuntu system:

$ ll /dev/disk/by-id/
total 0
lrwxrwxrwx 1 root root  9 oct.   5 17:35 ata-LITEONIT_LMT-256M6M_mSATA_256GB_TW0XXM30550854335101 -> ../../sda
lrwxrwxrwx 1 root root 10 oct.   5 17:35 ata-LITEONIT_LMT-256M6M_mSATA_256GB_TW0XXM30550854335101-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 oct.   5 17:35 ata-LITEONIT_LMT-256M6M_mSATA_256GB_TW0XXM30550854335101-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 oct.   5 17:35 ata-LITEONIT_LMT-256M6M_mSATA_256GB_TW0XXM30550854335101-part5 -> ../../sda5
lrwxrwxrwx 1 root root 10 oct.   5 17:35 dm-name-ubuntu--gnome--vg-root -> ../../dm-0
lrwxrwxrwx 1 root root 10 oct.   5 17:35 dm-name-ubuntu--gnome--vg-swap_1 -> ../../dm-1
lrwxrwxrwx 1 root root 10 oct.   5 17:35 dm-uuid-LVM-vQQEtad6bkH4eOo6VGhzSQTIdtb8NGQHu9UmdjpiZIdmTDX2fnY3w1iusURlcxdX -> ../../dm-0
lrwxrwxrwx 1 root root 10 oct.   5 17:35 dm-uuid-LVM-vQQEtad6bkH4eOo6VGhzSQTIdtb8NGQHuBKVfZSp0bRW0VX1W0HwAk32JDpAYnc2 -> ../../dm-1
lrwxrwxrwx 1 root root 10 oct.   5 17:35 lvm-pv-uuid-dYnNY6-fbuz-KvNK-fV2f-RkfI-Y4iF-XlolCY -> ../../sda5

It will be useful to sort diskio per id in the Glances software (see attached git issue).

Thanks !

zatricky commented 2 months ago

Note that the pyudev library can help a lot here, though it naturally only helps on Linux systems with udev which, notably, excludes most SystemV-based systems:

Disk:

>>> import pyudev
>>> context = pyudev.Context()
>>> device = pyudev.Devices.from_name(context, 'block', 'sda')
>>> device
Device('/sys/devices/pci0000:00/0000:00:17.0/ata5/host4/target4:0:0/4:0:0:0/block/sda')
>>> device.get('DEVTYPE')
'disk'
>>> device.get('ID_MODEL')
'Samsung_SSD_870_EVO_1TB'
>>> device.get('ID_SERIAL')
'Samsung_SSD_870_EVO_1TB_SERIAL000000000'
>>> device.get('ID_SERIAL_SHORT')
'SERIAL000000000'

Formatted partition on same disk:

>>> device = pyudev.Devices.from_name(context, 'block', 'sda3')
>>> device
Device('/sys/devices/pci0000:00/0000:00:17.0/ata5/host4/target4:0:0/4:0:0:0/block/sda/sda3')
>>> device.get('DEVTYPE')
'partition'
>>> device.get('PARTN')
'3'
>>> device.get('ID_SERIAL')
'Samsung_SSD_870_EVO_1TB_SERIAL000000000'
>>> device.get('ID_FS_LABEL')
'fedora_hostname'
>>> device.get('ID_FS_TYPE')
'btrfs'
>>> device.get('ID_FS_UUID')
'fakeuuid-aaaa-bbbb-cccc-dddddddddddd'
>>>

Note that partitions share the ID_SERIAL with the "parent" disk - the differentiation is the DEVTYPE and PARTN attributes.

To get a list of potential Device.get() query parameters, run the following from a shell:

# udevadm info --query=all --name=/dev/sda
# udevadm info --query=all --name=/dev/sda3