index : lsbly
Linux utility to get information on disks/partitions, just like lsblk but better
about summary refs log tree commit diff
author y2w8 <yousefalharby416@gmail.com> 2026-04-09 01:11:45 +0300
committer y2w8 <yousefalharby416@gmail.com> 2026-04-09 01:11:45 +0300
commit bf9b2c99accd6d03bd5c49af1dd787a8c486580f [patch]
tree d2fb64a0175e33971b8e11bcdb4177a2f68de5b1
parent 36d904af8a5d28da1f247614790da7294439e1e9
download bf9b2c99accd6d03bd5c49af1dd787a8c486580f.tar.gz

feat: more blockdevice information.

Diffstat

-rw-r--r-- src/disks.rs 70
-rw-r--r-- src/main.rs 13
 2 files changed, 70 insertions(+), 13 deletions(-)

diff --git a/src/disks.rs b/src/disks.rs
index 518fd1c..799fe76 100644
--- a/src/disks.rs
+++ b/src/disks.rs
@@ -15,23 +15,34 @@
    // like "/home"
    pub mountpoint: Option<PathBuf>,

    // like "/home"
    pub mountpoints: Option<Vec<PathBuf>>,

    // like "aaa123-bbb456-ccc789"
    pub uuid: Option<String>,

    pub vendor: Option<String>,

    pub label: Option<String>,

    pub children: Option<Vec<BlockDevice>>,

    pub partlabel: Option<String>,

    pub partflags: Option<String>,

    pub partuuid: Option<String>,

    pub size: Option<ByteSize>,

    pub fssize: Option<ByteSize>,

    pub used: Option<ByteSize>,
    pub fsused: Option<ByteSize>,

    pub available: Option<ByteSize>,
    pub fsavailable: Option<ByteSize>,
}

pub struct Disks();


// TODO: find library or using code not command.
impl Disks {
    pub fn list() -> Result<Vec<BlockDevice>, LsblyError> {
@@ -40,31 +51,66 @@
        let mut disks: Vec<BlockDevice> = Vec::new();
        if let Some(device_list) = json_disks["blockdevices"].as_array() {
            for disk in device_list {
                let mut mountpoints: Vec<PathBuf> = Vec::new();
                if let Some(mountpoints_list) = disk["mountpoints"].as_array() {
                    for mountpoint in mountpoints_list {
                        mountpoints.push(PathBuf::from(mountpoint.as_str().unwrap_or_default()));
                    }
                }

                let mut children: Vec<BlockDevice> = Vec::new();
                if let Some(child_list) = disk["children"].as_array() {
                    for child in child_list {
                        let mut mountpoints: Vec<PathBuf> = Vec::new();
                        if let Some(mountpoints_list) = child["mountpoints"].as_array() {
                            for mountpoint in mountpoints_list {
                                mountpoints
                                    .push(PathBuf::from(mountpoint.as_str().unwrap_or_default()));
                            }
                        }
                        children.push(BlockDevice {
                            name: Some(child["name"].as_str().unwrap_or_default().to_string()),
                            path: Some(PathBuf::from(child["path"].as_str().unwrap_or_default())),
                            mountpoint: Some(PathBuf::from(child["mountpoint"].as_str().unwrap_or_default())),
                            mountpoint: Some(PathBuf::from(
                                child["mountpoint"].as_str().unwrap_or_default(),
                            )),
                            mountpoints: Some(mountpoints),
                            uuid: Some(child["uuid"].as_str().unwrap_or_default().to_string()),
                            vendor: Some(child["vendor"].as_str().unwrap_or_default().to_string()),
                            label: Some(child["label"].as_str().unwrap_or_default().to_string()),
                            size: Some(ByteSize::b(child["fssize"].as_u64().unwrap_or(0))),
                            used: Some(ByteSize::b(child["fsused"].as_u64().unwrap_or(0))),
                            partlabel: Some(child["partlabel"].as_str().unwrap_or_default().to_string()),
                            partflags: Some(child["partflags"].as_str().unwrap_or_default().to_string()),
                            partuuid: Some(child["partuuid"].as_str().unwrap_or_default().to_string()),
                            size: Some(ByteSize::b(child["size"].as_u64().unwrap_or(0))),
                            fssize: Some(ByteSize::b(child["fssize"].as_u64().unwrap_or(0))),
                            fsavailable: Some(ByteSize::b(
                                child["fssize"].as_u64().unwrap_or(0)
                                    - child["fsused"].as_u64().unwrap_or(0),
                            )),
                            fsused: Some(ByteSize::b(child["fsused"].as_u64().unwrap_or(0))),
                            ..Default::default()
                        });
                    }
                }

                disks.push(BlockDevice {
                    name: Some(disk["name"].as_str().unwrap_or_default().to_string()),
                    path: Some(PathBuf::from(disk["path"].as_str().unwrap_or_default())),
                    mountpoint: Some(PathBuf::from(disk["mountpoint"].as_str().unwrap_or_default())),
                    mountpoint: Some(PathBuf::from(
                        disk["mountpoint"].as_str().unwrap_or_default(),
                    )),
                    mountpoints: Some(mountpoints),
                    uuid: Some(disk["uuid"].as_str().unwrap_or_default().to_string()),
                    vendor: Some(disk["vendor"].as_str().unwrap_or_default().to_string()),
                    label: Some(disk["label"].as_str().unwrap_or_default().to_string()),
                    size: Some(ByteSize::b(disk["fssize"].as_u64().unwrap_or(0))),
                    available: Some(ByteSize::b(disk["fssize"].as_u64().unwrap_or(0) - disk["fsused"].as_u64().unwrap_or(0))),
                    used: Some(ByteSize::b(disk["fsused"].as_u64().unwrap_or(0))),
                    size: Some(ByteSize::b(disk["size"].as_u64().unwrap_or(0))),
                    fssize: Some(ByteSize::b(disk["fssize"].as_u64().unwrap_or(0))),
                    fsavailable: Some(ByteSize::b(
                        disk["fssize"].as_u64().unwrap_or(0) - disk["fsused"].as_u64().unwrap_or(0),
                    )),
                    fsused: Some(ByteSize::b(disk["fsused"].as_u64().unwrap_or(0))),
                    children: Some(children),
                    ..Default::default()
                });
            }
        }
@@ -75,7 +121,7 @@
        let output = Command::new("lsblk")
            .args([
                "-o",
                "NAME,MOUNTPOINT,PATH,SIZE,UUID,LABEL,FSSIZE,FSUSED",
                "NAME,MOUNTPOINT,PATH,SIZE,UUID,LABEL,PARTLABEL,PARTFLAGS,PARTUUID,FSSIZE,FSUSED",
                "-Jb",
            ])
            .output()
diff --git a/src/main.rs b/src/main.rs
index a08131a..72810ba 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,16 +14,27 @@

    let disks = Disks::list()?;

    // TODO: add size, used, available
    // TODO: add mountpoints, vendor, uuid && use ratatui and crossterm instead od raw output
    for disk in disks {
        println!("name: {}", disk.name.as_deref().unwrap_or_default());
        println!("path: {}", disk.path.unwrap().as_os_str().to_str().unwrap_or_default());
        println!("label: {}", disk.label.as_deref().unwrap_or_default());
        println!("size: {}", disk.size.unwrap().display());
        println!("fssize: {}", disk.fssize.unwrap().display());
        println!("fsused: {}", disk.fsused.unwrap().display());
        println!("fsavailable: {}", disk.fsavailable.unwrap().display());
        if let Some(child_list) = disk.children {
            for partition in child_list {
                println!("   name: {}", partition.name.as_deref().unwrap_or_default());
                println!("   path: {}", partition.path.unwrap().as_os_str().to_str().unwrap_or_default());
                println!("   label: {}", partition.label.as_deref().unwrap_or_default());
                println!("   partlabel: {}", partition.partlabel.as_deref().unwrap_or_default());
                println!("   partflags: {}", partition.partflags.as_deref().unwrap_or_default());
                println!("   partuuid: {}", partition.partuuid.as_deref().unwrap_or_default());
                println!("   size: {}", partition.size.unwrap().display());
                println!("   fssize: {}", partition.fssize.unwrap().display());
                println!("   fsused: {}", partition.fsused.unwrap().display());
                println!("   fsavailable: {}", partition.fsavailable.unwrap().display());
                println!("   ---");
            }
        }