| 1 |
use std::{path::PathBuf, process::Command}; |
| 2 |
|
| 3 |
use bytesize::ByteSize; |
| 4 |
use color_eyre::{Result, eyre::Context}; |
| 5 |
|
| 6 |
#[derive(Debug, Default, PartialEq, Clone)] |
| 7 |
pub struct BlockDevice { |
| 8 |
|
| 9 |
pub name: Option<String>, |
| 10 |
|
| 11 |
|
| 12 |
pub path: Option<PathBuf>, |
| 13 |
|
| 14 |
|
| 15 |
pub mountpoint: Option<PathBuf>, |
| 16 |
|
| 17 |
pub mountpoints: Option<Vec<PathBuf>>, |
| 18 |
|
| 19 |
|
| 20 |
pub uuid: Option<String>, |
| 21 |
|
| 22 |
pub vendor: Option<String>, |
| 23 |
|
| 24 |
pub label: Option<String>, |
| 25 |
|
| 26 |
pub children: Option<Vec<BlockDevice>>, |
| 27 |
|
| 28 |
pub partlabel: Option<String>, |
| 29 |
|
| 30 |
pub partflags: Option<String>, |
| 31 |
|
| 32 |
pub partuuid: Option<String>, |
| 33 |
|
| 34 |
pub size: Option<ByteSize>, |
| 35 |
|
| 36 |
pub fssize: Option<ByteSize>, |
| 37 |
|
| 38 |
pub fsused: Option<ByteSize>, |
| 39 |
|
| 40 |
pub fsavailable: Option<ByteSize>, |
| 41 |
} |
| 42 |
|
| 43 |
pub struct Disks(); |
| 44 |
|
| 45 |
|
| 46 |
impl Disks { |
| 47 |
pub fn list() -> Result<Vec<BlockDevice>> { |
| 48 |
let json_disks: serde_json::Value = self::Disks::run_lsblk()?; |
| 49 |
|
| 50 |
let mut disks: Vec<BlockDevice> = Vec::new(); |
| 51 |
if let Some(device_list) = json_disks["blockdevices"].as_array() { |
| 52 |
for disk in device_list { |
| 53 |
let mut mountpoints: Vec<PathBuf> = Vec::new(); |
| 54 |
if let Some(mountpoints_list) = disk["mountpoints"].as_array() { |
| 55 |
for mountpoint in mountpoints_list { |
| 56 |
mountpoints.push(PathBuf::from(mountpoint.as_str().unwrap_or_default())); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
let mut children: Vec<BlockDevice> = Vec::new(); |
| 61 |
if let Some(child_list) = disk["children"].as_array() { |
| 62 |
for child in child_list { |
| 63 |
let mut mountpoints: Vec<PathBuf> = Vec::new(); |
| 64 |
if let Some(mountpoints_list) = child["mountpoints"].as_array() { |
| 65 |
for mountpoint in mountpoints_list { |
| 66 |
mountpoints |
| 67 |
.push(PathBuf::from(mountpoint.as_str().unwrap_or_default())); |
| 68 |
} |
| 69 |
} |
| 70 |
children.push(BlockDevice { |
| 71 |
name: Some(child["name"].as_str().unwrap_or_default().to_string()), |
| 72 |
path: Some(PathBuf::from(child["path"].as_str().unwrap_or_default())), |
| 73 |
mountpoint: Some(PathBuf::from( |
| 74 |
child["mountpoint"].as_str().unwrap_or_default(), |
| 75 |
)), |
| 76 |
mountpoints: Some(mountpoints), |
| 77 |
uuid: Some(child["uuid"].as_str().unwrap_or_default().to_string()), |
| 78 |
vendor: Some(child["vendor"].as_str().unwrap_or_default().to_string()), |
| 79 |
label: Some(child["label"].as_str().unwrap_or_default().to_string()), |
| 80 |
partlabel: Some(child["partlabel"].as_str().unwrap_or_default().to_string()), |
| 81 |
partflags: Some(child["partflags"].as_str().unwrap_or_default().to_string()), |
| 82 |
partuuid: Some(child["partuuid"].as_str().unwrap_or_default().to_string()), |
| 83 |
size: Some(ByteSize::b(child["size"].as_u64().unwrap_or(0))), |
| 84 |
fssize: Some(ByteSize::b(child["fssize"].as_u64().unwrap_or(0))), |
| 85 |
fsavailable: Some(ByteSize::b( |
| 86 |
child["fssize"].as_u64().unwrap_or(0) |
| 87 |
- child["fsused"].as_u64().unwrap_or(0), |
| 88 |
)), |
| 89 |
fsused: Some(ByteSize::b(child["fsused"].as_u64().unwrap_or(0))), |
| 90 |
..Default::default() |
| 91 |
}); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
disks.push(BlockDevice { |
| 96 |
name: Some(disk["name"].as_str().unwrap_or_default().to_string()), |
| 97 |
path: Some(PathBuf::from(disk["path"].as_str().unwrap_or_default())), |
| 98 |
mountpoint: Some(PathBuf::from( |
| 99 |
disk["mountpoint"].as_str().unwrap_or_default(), |
| 100 |
)), |
| 101 |
mountpoints: Some(mountpoints), |
| 102 |
uuid: Some(disk["uuid"].as_str().unwrap_or_default().to_string()), |
| 103 |
vendor: Some(disk["vendor"].as_str().unwrap_or_default().to_string()), |
| 104 |
label: Some(disk["label"].as_str().unwrap_or_default().to_string()), |
| 105 |
size: Some(ByteSize::b(disk["size"].as_u64().unwrap_or(0))), |
| 106 |
fssize: Some(ByteSize::b(disk["fssize"].as_u64().unwrap_or(0))), |
| 107 |
fsavailable: Some(ByteSize::b( |
| 108 |
disk["fssize"].as_u64().unwrap_or(0) - disk["fsused"].as_u64().unwrap_or(0), |
| 109 |
)), |
| 110 |
fsused: Some(ByteSize::b(disk["fsused"].as_u64().unwrap_or(0))), |
| 111 |
children: Some(children), |
| 112 |
..Default::default() |
| 113 |
}); |
| 114 |
} |
| 115 |
} |
| 116 |
Ok(disks) |
| 117 |
} |
| 118 |
|
| 119 |
pub fn run_lsblk() -> Result<serde_json::Value> { |
| 120 |
let output = Command::new("lsblk") |
| 121 |
.args([ |
| 122 |
"-o", |
| 123 |
"NAME,MOUNTPOINT,PATH,SIZE,UUID,LABEL,PARTLABEL,PARTFLAGS,PARTUUID,FSSIZE,FSUSED", |
| 124 |
"-Jb", |
| 125 |
]) |
| 126 |
.output().context("failed to run lsblk")?; |
| 127 |
|
| 128 |
let stdout_str = String::from_utf8(output.stdout).context("failed to extract string from cmd output")?; |
| 129 |
|
| 130 |
let json_output: serde_json::Value = |
| 131 |
serde_json::from_str(&stdout_str).context("failed to parse cmd output")?; |
| 132 |
|
| 133 |
Ok(json_output) |
| 134 |
} |
| 135 |
} |