Files
initrd/src/blockdev.rs

22 lines
702 B
Rust
Raw Permalink Normal View History

2024-04-29 12:54:25 +02:00
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt, Result};
/// Checks if a block device is uninitialized.
/// It assumed that a initialized device always has a non-zero byte in the first 8kiB.
pub async fn is_uninitialized(dev_path: &str) -> Result<bool> {
let mut dev = fs::File::open(dev_path).await?;
let mut buf = [0u8; 8 << 10];
dev.read_exact(&mut buf).await?;
Ok(buf.into_iter().all(|b| b == 0))
}
/// zeroes the first 8kiB of the device so it looks uninitialized again.
pub async fn uninitialize(dev_path: &str) -> Result<()> {
let mut dev = fs::File::options().write(true).open(dev_path).await?;
let buf = [0u8; 8 << 10];
dev.write_all(&buf).await
}