2024-01-30 10:15:03 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2024-01-21 08:13:05 +00:00
|
|
|
use loopdev::{LoopControl, LoopDevice};
|
|
|
|
use xenclient::BlockDeviceRef;
|
|
|
|
|
|
|
|
pub struct AutoLoop {
|
|
|
|
control: LoopControl,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AutoLoop {
|
2024-02-13 19:48:49 +00:00
|
|
|
pub fn new(control: LoopControl) -> AutoLoop {
|
2024-01-21 08:13:05 +00:00
|
|
|
AutoLoop { control }
|
|
|
|
}
|
|
|
|
|
2024-01-22 01:21:19 +00:00
|
|
|
pub fn loopify(&self, file: &str) -> Result<BlockDeviceRef> {
|
2024-01-21 08:13:05 +00:00
|
|
|
let device = self.control.next_free()?;
|
|
|
|
device.with().read_only(true).attach(file)?;
|
|
|
|
let path = device
|
|
|
|
.path()
|
2024-01-30 10:15:03 +00:00
|
|
|
.ok_or(anyhow!("unable to get loop device path"))?
|
2024-01-21 08:13:05 +00:00
|
|
|
.to_str()
|
2024-01-30 10:15:03 +00:00
|
|
|
.ok_or(anyhow!("unable to convert loop device path to string",))?
|
2024-01-21 08:13:05 +00:00
|
|
|
.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(())
|
|
|
|
}
|
|
|
|
}
|