Skip to content

Commit

Permalink
Storage mangment API: disks infos
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry committed Jan 18, 2025
1 parent 54e5aae commit 7d9c4de
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Depends: python3-all (>= 3.11),
, python3-ldap, python3-zeroconf (>= 0.47), python3-lexicon,
, python3-cryptography, python3-jwt, python3-passlib, python3-magic
, python-is-python3, python3-pydantic, python3-email-validator
, udisks2,
, nginx, nginx-extras (>=1.22)
, apt, apt-transport-https, apt-utils, aptitude, dirmngr
, openssh-server, iptables, fail2ban, bind9-dnsutils
Expand Down
15 changes: 15 additions & 0 deletions share/actionsmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2000,3 +2000,18 @@ diagnosis:
help: Remove a filter (it should be an existing filter as listed with "ignore --list")
nargs: "*"
metavar: CRITERIA


#############################
# Storage #
#############################
storage:
category_help: Manage hard-drives, filesystem, pools
subcategories:
disk:
subcategory_help: Manage et get infos about hard-drives
actions:
# storage_disks_list
infos:
action_help: Gets infos about hard-drives currently attached to this system
api: GET /storage/disk/infos
54 changes: 54 additions & 0 deletions src/disks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import dataclasses
from typing import Optional

import dbus
from moulinette.utils.log import getActionLogger

logger = getActionLogger("yunohost.storage")


UDISK_DRIVE_PATH = "/org/freedesktop/UDisks2/drives/"
UDISK_BLOCK_PATH = "/org/freedesktop/UDisks2/block_devices/"
UDISK_PART_TABLE_IFC = "org.freedesktop.UDisks2.PartitionTable"
UDISK_BLOCK_IFC = "org.freedesktop.UDisks2.Block"
UDISK_DRIVE_IFC = "org.freedesktop.UDisks2.Drive"
UDISK_ENCRYPTED_IFC = "org.freedesktop.UDisks2.Encrypted"
UDISK_FILESYSTEM_IFC = "org.freedesktop.UDisks2.Filesystem"


@dataclasses.dataclass
class DiskInfos:
name: str
model: str
serial: str
size: int
type: str
rpm: Optional[int] = None


def infos():
result = {}

bus = dbus.SystemBus()
manager = bus.get_object("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2")

for k, v in manager.get_dbus_method(
"GetManagedObjects", "org.freedesktop.DBus.ObjectManager"
)().items():
if k.startswith(UDISK_DRIVE_PATH):
# These are hard drives
drive = v[UDISK_DRIVE_IFC]
name = k.removeprefix(UDISK_DRIVE_PATH)
rotation_rate = drive["RotationRate"]
result[name] = dataclasses.asdict(
DiskInfos(
name=name,
model=drive["Model"],
serial=drive["Serial"],
size=drive["Size"],
type="HDD" if rotation_rate != 0 else "SSD",
rpm=rotation_rate if rotation_rate != 0 else None,
)
)

return result
4 changes: 4 additions & 0 deletions src/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def storage_disk_infos():
from yunohost.disks import infos

return infos()

0 comments on commit 7d9c4de

Please sign in to comment.