Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage mangment API: disks infos #1953

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

christophehenry
Copy link

@christophehenry christophehenry commented Sep 17, 2024

The problem

Rel: YunoHost/issues#1823.
Add API for getting infos on hard drives.

share/actionsmap.yml Outdated Show resolved Hide resolved
@christophehenry christophehenry marked this pull request as ready for review September 18, 2024 15:50
@christophehenry
Copy link
Author

christophehenry commented Sep 18, 2024

Ok I think this is a good first step. Here is an exemple of the result this produces:

{
    "sda": {
        "devname": "/dev/sda",
        "model": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "serial": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "size": xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
        "links": [
            "/dev/disk/by-diskseq/1",
            "/dev/disk/by-id/ata-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "/dev/disk/by-id/wwn-xxxxxxxxxxxxxxxxxxxxxxxx",
            "/dev/disk/by-path/pci-0000:01:00.1-ata-1",
            "/dev/disk/by-path/pci-0000:01:00.1-ata-1.0"
        ],
        "partitions": {
            "sda1": {
                "devname": "/dev/sda",
                "filesystem": "btrfs",
                "encrypted": true,
                "mountpoint": "/home"
            }
        }
    },
    "sdb": {
        "devname": "/dev/sdb",
        "model": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "serial": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "size": xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
        "links": [
            "/dev/disk/by-diskseq/2",
            "/dev/disk/by-id/ata-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "/dev/disk/by-id/wwn-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "/dev/disk/by-path/pci-0000:0a:00.0-ata-6",
            "/dev/disk/by-path/pci-0000:0a:00.0-ata-6.0"
        ],
        "partitions": {
            "sdb1": {
                "devname": "/dev/sdb",
                "filesystem": "vfat",
                "encrypted": false,
                "mountpoint": "/boot/efi"
            },
            "sdb2": {
                "devname": "/dev/sdb",
                "filesystem": "ext4",
                "encrypted": false,
                "mountpoint": "/boot"
            },
            "sdb3": {
                "devname": "/dev/sdb",
                "filesystem": "btrfs",
                "encrypted": true,
                "mountpoint": "/"
            }
        }
    }
}

In a future PR, I'd like to add the device type (HDD, SSD, …) and RPM for HDDs. I don't really know how to do that. TrueNAS does it by using its own library. In the meantime, this is ready for review.

src/disks.py Outdated Show resolved Hide resolved
@christophehenry christophehenry changed the title WIP: Storage mangment API: disks infos Storage mangment API: disks infos Sep 18, 2024
@christophehenry christophehenry force-pushed the storage-api-disks-infos branch 2 times, most recently from 81d9fa7 to e83e7b3 Compare September 22, 2024 15:05
@christophehenry
Copy link
Author

A few notes here: I rewrote this PR to use the udisks2 API which provides much more details about disks and is easier to use that udev + other sources of infos.

But as a pure dbus API, the code is less self-explanatory. It's mainly about parsing dictionary.

Maybe I can make use of something like dbus-fast, IDK.

Tell me if it's OK to add the udisk daemon to Ynh. I'm not aware of everything it entails.

@alexAubin
Copy link
Member

Naively udisk sounds fine to me 👍

@zamentur
Copy link
Member

Yes i think udisk is a good idea, also because it could help to automount external usb or disk...

@zamentur
Copy link
Member

In a future PR, I'd like to add the device type (HDD, SSD, …) and RPM for HDDs. I don't really know how to do that. TrueNAS does it by using its own library. In the meantime, this is ready for review.

You can know if it's a HDD or SSD by checking directly the info inside /sys...

cat /sys/block/sda/queue/rotational

0 -> SSD
1 -> HDD

/sys/block/sda/queue/removable to know if it's an usb removable disk i guess

We could also imagine check for ssd how many written is made, in order to compute an end of life percentage.

@christophehenry
Copy link
Author

You can know if it's a HDD or SSD by checking directly the info inside /sys...

udisks2 provides the information so I don't need a solution anymore. Thanks.

@alexAubin alexAubin added the 🏗️ Major project Big decision label Oct 15, 2024
@christophehenry christophehenry force-pushed the storage-api-disks-infos branch 8 times, most recently from 7d9c4de to 06e2219 Compare January 18, 2025 14:51
src/disks.py Outdated
Comment on lines 30 to 32
for k, v in manager.get_dbus_method(
"GetManagedObjects", "org.freedesktop.DBus.ObjectManager"
)().items():
Copy link
Author

@christophehenry christophehenry Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does someone knows a more efficient way to interact with dbus than just list all the objects managed by Udisks2 here?

def infos():
result = {}

bus = dbus.SystemBus()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be file-scoped? I don't know how it is efficient to create a bus at each call.

Copy link
Member

@zamentur zamentur Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could use this kind of info:

/sys/block/sda/size -> Size in Byte
/sys/block/sda/removable -> USB or not
/sys/block/sda/queue/rotational -> HDD or not
/sys/block/sda/ro -> read only mode
/sys/block/sda/stat -> https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block
/sys/block/sda/sdaX -> partition name and info

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but maybe dbus is ok... (i am not a dbus expert)

@christophehenry
Copy link
Author

I don't believe it is useful to test this. We'd basically be testing the udisks2 library which is never a good idea.

@christophehenry christophehenry force-pushed the storage-api-disks-infos branch from 06e2219 to bcfd1ae Compare January 19, 2025 11:35
@christophehenry christophehenry force-pushed the storage-api-disks-infos branch from bcfd1ae to ea7051a Compare January 19, 2025 11:53
src/disks.py Outdated Show resolved Hide resolved
@christophehenry christophehenry force-pushed the storage-api-disks-infos branch from ea7051a to 0961615 Compare January 22, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants