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 2 commits 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
Comment on lines 31 to 39
encrypted_provider = glob(f"/sys/block/dm-*/slaves/{child_dev.sys_name}")
if encrypted_provider:
# retrive the dm-x part
dm = encrypted_provider[0].split("/")[3]
enc_dev = pyudev.Devices.from_name(device.context, "block", dm)
# This work for LUKS, what about other partition mecanisms?
partname = f"/dev/mapper/{enc_dev.properties['DM_NAME']}"
encrypted = True
else:
Copy link
Author

Choose a reason for hiding this comment

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

This method detects LUKS partitions but I'm not sure it does not detect other unecrypted partitions, like LVM. If someone with more system knowlege than me could check this.

Copy link
Author

Choose a reason for hiding this comment

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

I could use pyparted here but this needs sufficient rights. If I'm not mistaken, yunohost-api is running as root, so this shouldn't be an issue, right?

Copy link
Member

Choose a reason for hiding this comment

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

yes

Copy link
Author

Choose a reason for hiding this comment

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

Turns out this solution detects LVM (non-LUKS) partitions as encrypted. And I can't figure out how to retrieve FS with pyparted. I don't know how gparted and KDE partition manager detect the partition filesystem.

@christophehenry christophehenry changed the title WIP: Storage mangment API: disks infos Storage mangment API: disks infos Sep 18, 2024
@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
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