|
| 1 | +from collections import OrderedDict |
| 2 | +import dataclasses |
| 3 | +from glob import glob |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +import pyudev |
| 7 | +import psutil |
| 8 | + |
| 9 | +from moulinette.utils.log import getActionLogger |
| 10 | + |
| 11 | + |
| 12 | +from yunohost.utils.disks import filter_device |
| 13 | + |
| 14 | + |
| 15 | +logger = getActionLogger("yunohost.storage") |
| 16 | + |
| 17 | + |
| 18 | +@dataclasses.dataclass |
| 19 | +class DiskParts: |
| 20 | + devname: str |
| 21 | + filesystem: str |
| 22 | + encrypted: bool |
| 23 | + mountpoint: str |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def from_parent_device(device: pyudev.Device, partitions): |
| 27 | + result = OrderedDict() |
| 28 | + for child_dev in sorted( |
| 29 | + filter(filter_device, device.children), key=lambda it: it.device_node |
| 30 | + ): |
| 31 | + encrypted_provider = glob(f"/sys/block/dm-*/slaves/{child_dev.sys_name}") |
| 32 | + if encrypted_provider: |
| 33 | + # retrive the dm-x part |
| 34 | + dm = encrypted_provider[0].split("/")[3] |
| 35 | + enc_dev = pyudev.Devices.from_name(device.context, "block", dm) |
| 36 | + # This work for LUKS, what about other partition mecanisms? |
| 37 | + partname = f"/dev/mapper/{enc_dev.properties['DM_NAME']}" |
| 38 | + encrypted = True |
| 39 | + else: |
| 40 | + partname = child_dev.device_node |
| 41 | + encrypted = False |
| 42 | + |
| 43 | + if partname not in partitions: |
| 44 | + logger.warning( |
| 45 | + f"{child_dev.device_node} not found by 'psutil.disk_partitions'" |
| 46 | + ) |
| 47 | + continue |
| 48 | + |
| 49 | + result[child_dev.sys_name] = DiskParts( |
| 50 | + devname=device.device_node, |
| 51 | + filesystem=partitions[partname].fstype, |
| 52 | + encrypted=encrypted, |
| 53 | + mountpoint=partitions[partname].mountpoint, |
| 54 | + ) |
| 55 | + |
| 56 | + return result |
| 57 | + |
| 58 | + |
| 59 | +@dataclasses.dataclass |
| 60 | +class DiskInfos: |
| 61 | + devname: str |
| 62 | + model: str |
| 63 | + serial: str |
| 64 | + size: int |
| 65 | + links: list[str] |
| 66 | + partitions: Optional[list[DiskParts]] |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def from_device(device, partitions): |
| 70 | + try: |
| 71 | + dev_size = device.attributes.asint("size") |
| 72 | + except (AttributeError, UnicodeError, ValueError): |
| 73 | + dev_size = None |
| 74 | + |
| 75 | + dev_links = list(sorted(it for it in device.device_links)) |
| 76 | + child_parts = DiskParts.from_parent_device(device, partitions) |
| 77 | + |
| 78 | + return DiskInfos( |
| 79 | + devname=device.device_node, |
| 80 | + model=device.get("ID_MODEL", None), |
| 81 | + serial=device.get("ID_SERIAL_SHORT", None), |
| 82 | + size=dev_size, |
| 83 | + links=dev_links, |
| 84 | + partitions=child_parts or None, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def infos(): |
| 89 | + context = pyudev.Context() |
| 90 | + partitions = {it.device: it for it in psutil.disk_partitions()} |
| 91 | + result = OrderedDict() |
| 92 | + |
| 93 | + for it in sorted( |
| 94 | + filter(filter_device, context.list_devices(subsystem="block", DEVTYPE="disk")), |
| 95 | + key=lambda it: it.device_node, |
| 96 | + ): |
| 97 | + result[it.sys_name] = dataclasses.asdict( |
| 98 | + DiskInfos.from_device(it, partitions), dict_factory=OrderedDict |
| 99 | + ) |
| 100 | + |
| 101 | + return result |
0 commit comments