-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
disk-test: Add non-default helper bin for testing APIs as sudo
Signed-off-by: Ikey Doherty <[email protected]>
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |