mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-04 05:31:32 +00:00
hypha: implement loop device support directly to avoid devd
This commit is contained in:
36
hypha/src/autoloop.rs
Normal file
36
hypha/src/autoloop.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use crate::error::{HyphaError, Result};
|
||||
use loopdev::{LoopControl, LoopDevice};
|
||||
use std::path::Path;
|
||||
use xenclient::BlockDeviceRef;
|
||||
|
||||
pub struct AutoLoop {
|
||||
control: LoopControl,
|
||||
}
|
||||
|
||||
impl AutoLoop {
|
||||
pub(crate) fn new(control: LoopControl) -> AutoLoop {
|
||||
AutoLoop { control }
|
||||
}
|
||||
|
||||
pub fn loopify(&self, file: &Path) -> Result<BlockDeviceRef> {
|
||||
let device = self.control.next_free()?;
|
||||
device.with().read_only(true).attach(file)?;
|
||||
let path = device
|
||||
.path()
|
||||
.ok_or(HyphaError::new("unable to get loop device path"))?
|
||||
.to_str()
|
||||
.ok_or(HyphaError::new(
|
||||
"unable to convert loop device path to string",
|
||||
))?
|
||||
.to_string();
|
||||
let major = device.major()?;
|
||||
let minor = device.minor()?;
|
||||
Ok(BlockDeviceRef { path, major, minor })
|
||||
}
|
||||
|
||||
pub fn unloop(&self, device: &str) -> Result<()> {
|
||||
let device = LoopDevice::open(device)?;
|
||||
device.detach()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user