feat: more blockdevice information.
2 files changed, 70 insertions(+), 13 deletions(-)
@@ -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()
@@ -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!(" ---");
}
}