mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 21:21:32 +00:00
* feature(loopdev): add native loop device implementation The previous loop device implementation required bindgen for no reason, making cross-compilation difficult. Signed-off-by: Ariadne Conill <ariadne@ariadne.space> * feat(runtime): use native krata-loopdev instead of loopdev-3 Signed-off-by: Ariadne Conill <ariadne@ariadne.space> * chore: update cargo workspace lock file Signed-off-by: Ariadne Conill <ariadne@ariadne.space> * chore: appease formatting linter Signed-off-by: Ariadne Conill <ariadne@ariadne.space> --------- Signed-off-by: Ariadne Conill <ariadne@ariadne.space>
43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
use std::{sync::Arc, time::Duration};
|
|
|
|
use anyhow::{anyhow, Result};
|
|
use krataloopdev::{LoopControl, LoopDevice};
|
|
use log::debug;
|
|
use tokio::time::sleep;
|
|
use xenclient::BlockDeviceRef;
|
|
|
|
#[derive(Clone)]
|
|
pub struct AutoLoop {
|
|
control: Arc<LoopControl>,
|
|
}
|
|
|
|
impl AutoLoop {
|
|
pub fn new(control: LoopControl) -> AutoLoop {
|
|
AutoLoop {
|
|
control: Arc::new(control),
|
|
}
|
|
}
|
|
|
|
pub fn loopify(&self, file: &str) -> Result<BlockDeviceRef> {
|
|
debug!("creating loop for file {}", file);
|
|
let device = self.control.next_free()?;
|
|
device.with().read_only(true).attach(file)?;
|
|
let path = device
|
|
.path()
|
|
.ok_or(anyhow!("unable to get loop device path"))?
|
|
.to_str()
|
|
.ok_or(anyhow!("unable to convert loop device path to string",))?
|
|
.to_string();
|
|
let major = device.major()?;
|
|
let minor = device.minor()?;
|
|
Ok(BlockDeviceRef { path, major, minor })
|
|
}
|
|
|
|
pub async fn unloop(&self, device: &str) -> Result<()> {
|
|
let device = LoopDevice::open(device)?;
|
|
device.detach()?;
|
|
sleep(Duration::from_millis(200)).await;
|
|
Ok(())
|
|
}
|
|
}
|