Skip to content

Commit c48c090

Browse files
committed
disk-test: Add non-default helper bin for testing APIs as sudo
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 3627f0a commit c48c090

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

Cargo.lock

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ default-members = [
66
]
77

88
members = [
9+
"disk-test",
910
"crates/*",
1011
]
1112

1213
[workspace.dependencies]
1314
log = "0.4.21"
15+
linux-raw-sys = "0.7.0"
16+
nix = { version = "0.29.0", features = ["fs", "mount"] }
1417
serde = { version = "1.0" }
1518
serde_json = "1.0"
1619
serde_with = "3.0"

disk-test/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "disk-test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
disks = { path = "../crates/disks" }
9+
partitioning = { path = "../crates/partitioning" }

disk-test/src/main.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
use std::fs;
6+
7+
use disks::BlockDevice;
8+
use partitioning::{loopback, sparsefile};
9+
10+
// Demo of various disk APIs, enumeration, loopback and sparse.
11+
fn main() -> Result<(), Box<dyn std::error::Error>> {
12+
// create 35GB img, attach loopback
13+
sparsefile::create("hello.world", 35 * 1024 * 1024 * 1024)?;
14+
let device = loopback::LoopDevice::create()?;
15+
device.attach("hello.world")?;
16+
eprintln!("loop device: {}", &device.path);
17+
18+
// discover all loop devices
19+
let loop_devices = BlockDevice::discover()?
20+
.into_iter()
21+
.filter_map(|device| {
22+
if let BlockDevice::Loopback(loop_device) = device {
23+
Some(loop_device)
24+
} else {
25+
None
26+
}
27+
})
28+
.collect::<Vec<_>>();
29+
30+
// print loop devices
31+
for loop_device in loop_devices {
32+
if let Some(file) = loop_device.file_path() {
33+
if let Some(disk) = loop_device.disk() {
34+
println!(
35+
"Loopback device: {} (backing file: {})",
36+
loop_device.name(),
37+
file.display()
38+
);
39+
println!("└─Disk: {} ({})", disk.name(), disk.model().unwrap_or("Unknown"));
40+
for partition in disk.partitions() {
41+
println!(" ├─{} {partition}", partition.name);
42+
}
43+
}
44+
}
45+
}
46+
47+
// detach loopback, remove img
48+
device.detach()?;
49+
fs::remove_file("hello.world")?;
50+
51+
Ok(())
52+
}

0 commit comments

Comments
 (0)