Skip to content

Commit

Permalink
disk-test: Add non-default helper bin for testing APIs as sudo
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeycode committed Jan 21, 2025
1 parent 3627f0a commit c48c090
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ default-members = [
]

members = [
"disk-test",
"crates/*",
]

[workspace.dependencies]
log = "0.4.21"
linux-raw-sys = "0.7.0"
nix = { version = "0.29.0", features = ["fs", "mount"] }
serde = { version = "1.0" }
serde_json = "1.0"
serde_with = "3.0"
Expand Down
9 changes: 9 additions & 0 deletions disk-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "disk-test"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
disks = { path = "../crates/disks" }
partitioning = { path = "../crates/partitioning" }
52 changes: 52 additions & 0 deletions disk-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0

use std::fs;

use disks::BlockDevice;
use partitioning::{loopback, sparsefile};

// Demo of various disk APIs, enumeration, loopback and sparse.
fn main() -> Result<(), Box<dyn std::error::Error>> {
// create 35GB img, attach loopback
sparsefile::create("hello.world", 35 * 1024 * 1024 * 1024)?;
let device = loopback::LoopDevice::create()?;
device.attach("hello.world")?;
eprintln!("loop device: {}", &device.path);

// discover all loop devices
let loop_devices = BlockDevice::discover()?
.into_iter()
.filter_map(|device| {
if let BlockDevice::Loopback(loop_device) = device {
Some(loop_device)
} else {
None
}
})
.collect::<Vec<_>>();

// print loop devices
for loop_device in loop_devices {
if let Some(file) = loop_device.file_path() {
if let Some(disk) = loop_device.disk() {
println!(
"Loopback device: {} (backing file: {})",
loop_device.name(),
file.display()
);
println!("└─Disk: {} ({})", disk.name(), disk.model().unwrap_or("Unknown"));
for partition in disk.partitions() {
println!(" ├─{} {partition}", partition.name);
}
}
}
}

// detach loopback, remove img
device.detach()?;
fs::remove_file("hello.world")?;

Ok(())
}

0 comments on commit c48c090

Please sign in to comment.