mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 05:10:55 +00:00
Use native loopdev implementation instead of loopdev-3 (#209)
* 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>
This commit is contained in:
parent
88a62441b1
commit
6f50167798
104
Cargo.lock
generated
104
Cargo.lock
generated
@ -249,26 +249,6 @@ version = "0.22.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bindgen"
|
|
||||||
version = "0.69.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0"
|
|
||||||
dependencies = [
|
|
||||||
"bitflags 2.5.0",
|
|
||||||
"cexpr",
|
|
||||||
"clang-sys",
|
|
||||||
"itertools",
|
|
||||||
"lazy_static",
|
|
||||||
"lazycell",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"regex",
|
|
||||||
"rustc-hash",
|
|
||||||
"shlex",
|
|
||||||
"syn 2.0.57",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@ -365,15 +345,6 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cexpr"
|
|
||||||
version = "0.6.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
|
||||||
dependencies = [
|
|
||||||
"nom",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -411,17 +382,6 @@ version = "0.1.7"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "da987586004ae7c43b7df5e3f7693775068522e1086f8d9b2d74c778a0f43313"
|
checksum = "da987586004ae7c43b7df5e3f7693775068522e1086f8d9b2d74c778a0f43313"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "clang-sys"
|
|
||||||
version = "1.7.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1"
|
|
||||||
dependencies = [
|
|
||||||
"glob",
|
|
||||||
"libc",
|
|
||||||
"libloading",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.5.7"
|
version = "4.5.7"
|
||||||
@ -1027,12 +987,6 @@ version = "0.28.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
|
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "glob"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "h2"
|
name = "h2"
|
||||||
version = "0.3.26"
|
version = "0.3.26"
|
||||||
@ -1526,6 +1480,13 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "krata-loopdev"
|
||||||
|
version = "0.0.11"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "krata-network"
|
name = "krata-network"
|
||||||
version = "0.0.11"
|
version = "0.0.11"
|
||||||
@ -1587,6 +1548,7 @@ dependencies = [
|
|||||||
"ipnetwork",
|
"ipnetwork",
|
||||||
"krata",
|
"krata",
|
||||||
"krata-advmac",
|
"krata-advmac",
|
||||||
|
"krata-loopdev",
|
||||||
"krata-oci",
|
"krata-oci",
|
||||||
"krata-xenclient",
|
"krata-xenclient",
|
||||||
"krata-xenevtchn",
|
"krata-xenevtchn",
|
||||||
@ -1594,7 +1556,6 @@ dependencies = [
|
|||||||
"krata-xenplatform",
|
"krata-xenplatform",
|
||||||
"krata-xenstore",
|
"krata-xenstore",
|
||||||
"log",
|
"log",
|
||||||
"loopdev-3",
|
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"uuid",
|
"uuid",
|
||||||
@ -1708,28 +1669,12 @@ version = "1.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lazycell"
|
|
||||||
version = "1.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.155"
|
version = "0.2.155"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libloading"
|
|
||||||
version = "0.8.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"windows-targets 0.52.4",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.13"
|
version = "0.4.13"
|
||||||
@ -1752,17 +1697,6 @@ version = "0.4.21"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "loopdev-3"
|
|
||||||
version = "0.5.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "90a97d7a5124296ee9124a815acdc3dc4a91f577b72812b3f1f99bb959b46e8d"
|
|
||||||
dependencies = [
|
|
||||||
"bindgen",
|
|
||||||
"errno",
|
|
||||||
"libc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lru"
|
name = "lru"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
@ -1807,12 +1741,6 @@ version = "0.3.17"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "minimal-lexical"
|
|
||||||
version = "0.2.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "miniz_oxide"
|
name = "miniz_oxide"
|
||||||
version = "0.7.2"
|
version = "0.7.2"
|
||||||
@ -1952,16 +1880,6 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nom"
|
|
||||||
version = "7.1.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
|
||||||
dependencies = [
|
|
||||||
"memchr",
|
|
||||||
"minimal-lexical",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ntapi"
|
name = "ntapi"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
@ -2773,12 +2691,6 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "shlex"
|
|
||||||
version = "1.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signal-hook"
|
name = "signal-hook"
|
||||||
version = "0.3.17"
|
version = "0.3.17"
|
||||||
|
15
crates/loopdev/Cargo.toml
Normal file
15
crates/loopdev/Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "krata-loopdev"
|
||||||
|
description = "Loop device handling library for krata"
|
||||||
|
license.workspace = true
|
||||||
|
version.workspace = true
|
||||||
|
homepage.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
edition = "2021"
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "krataloopdev"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libc.workspace = true
|
354
crates/loopdev/src/lib.rs
Normal file
354
crates/loopdev/src/lib.rs
Normal file
@ -0,0 +1,354 @@
|
|||||||
|
use libc::{c_int, ioctl};
|
||||||
|
use std::{
|
||||||
|
fs::{File, OpenOptions},
|
||||||
|
io,
|
||||||
|
os::fd::{AsRawFd, IntoRawFd, RawFd},
|
||||||
|
os::unix::fs::MetadataExt,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(all(not(target_os = "android"), not(target_env = "musl")))]
|
||||||
|
type IoctlRequest = libc::c_ulong;
|
||||||
|
#[cfg(any(target_os = "android", target_env = "musl"))]
|
||||||
|
type IoctlRequest = libc::c_int;
|
||||||
|
|
||||||
|
const LOOP_CONTROL: &str = "/dev/loop-control";
|
||||||
|
const LOOP_PREFIX: &str = "/dev/loop";
|
||||||
|
|
||||||
|
/// Loop control interface IOCTLs.
|
||||||
|
const LOOP_CTL_GET_FREE: IoctlRequest = 0x4C82;
|
||||||
|
|
||||||
|
/// Loop device flags.
|
||||||
|
const LO_FLAGS_READ_ONLY: u32 = 1;
|
||||||
|
const LO_FLAGS_AUTOCLEAR: u32 = 4;
|
||||||
|
const LO_FLAGS_PARTSCAN: u32 = 8;
|
||||||
|
const LO_FLAGS_DIRECT_IO: u32 = 16;
|
||||||
|
|
||||||
|
/// Loop device IOCTLs.
|
||||||
|
const LOOP_SET_FD: IoctlRequest = 0x4C00;
|
||||||
|
const LOOP_CLR_FD: IoctlRequest = 0x4C01;
|
||||||
|
const LOOP_SET_STATUS64: IoctlRequest = 0x4C04;
|
||||||
|
const LOOP_SET_CAPACITY: IoctlRequest = 0x4C07;
|
||||||
|
const LOOP_SET_DIRECT_IO: IoctlRequest = 0x4C08;
|
||||||
|
|
||||||
|
/// Interface which wraps a handle to the loop control device.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct LoopControl {
|
||||||
|
dev_file: File,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Translate ioctl results to errors if appropriate.
|
||||||
|
fn translate_error(ret: i32) -> io::Result<i32> {
|
||||||
|
if ret < 0 {
|
||||||
|
Err(io::Error::last_os_error())
|
||||||
|
} else {
|
||||||
|
Ok(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoopControl {
|
||||||
|
/// Open the loop control device.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Any errors from physically opening the loop control device are
|
||||||
|
/// bubbled up.
|
||||||
|
pub fn open() -> io::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
dev_file: OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.open(LOOP_CONTROL)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Requests the next available loop device from the kernel and opens it.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use krataloopdev::LoopControl;
|
||||||
|
/// let lc = LoopControl::open().unwrap();
|
||||||
|
/// let ld = lc.next_free().unwrap();
|
||||||
|
/// println!("{}", ld.path().unwrap().display());
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Any errors from opening the loop device are bubbled up.
|
||||||
|
pub fn next_free(&self) -> io::Result<LoopDevice> {
|
||||||
|
let dev_num = translate_error(unsafe {
|
||||||
|
ioctl(
|
||||||
|
self.dev_file.as_raw_fd() as c_int,
|
||||||
|
LOOP_CTL_GET_FREE as IoctlRequest,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
LoopDevice::open(format!("{}{}", LOOP_PREFIX, dev_num))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Interface to a loop device itself, e.g. `/dev/loop0`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct LoopDevice {
|
||||||
|
device: File,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRawFd for LoopDevice {
|
||||||
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
|
self.device.as_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoRawFd for LoopDevice {
|
||||||
|
fn into_raw_fd(self) -> RawFd {
|
||||||
|
self.device.into_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LoopDevice {
|
||||||
|
/// Opens a loop device.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Any errors from opening the underlying physical loop device are bubbled up.
|
||||||
|
pub fn open<P: AsRef<Path>>(dev: P) -> io::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
device: OpenOptions::new().read(true).write(true).open(dev)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach a loop device to a file with the given options.
|
||||||
|
pub fn with(&self) -> AttachOptions<'_> {
|
||||||
|
AttachOptions {
|
||||||
|
device: self,
|
||||||
|
info: LoopInfo64::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enables or disables Direct I/O mode.
|
||||||
|
pub fn set_direct_io(&self, direct_io: bool) -> io::Result<()> {
|
||||||
|
translate_error(unsafe {
|
||||||
|
ioctl(
|
||||||
|
self.device.as_raw_fd() as c_int,
|
||||||
|
LOOP_SET_DIRECT_IO as IoctlRequest,
|
||||||
|
if direct_io { 1 } else { 0 },
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach the loop device to a fully-mapped file.
|
||||||
|
pub fn attach_file<P: AsRef<Path>>(&self, backing_file: P) -> io::Result<()> {
|
||||||
|
let info = LoopInfo64 {
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
Self::attach_with_loop_info(self, backing_file, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach the loop device to a file with `LoopInfo64`.
|
||||||
|
fn attach_with_loop_info(
|
||||||
|
&self,
|
||||||
|
backing_file: impl AsRef<Path>,
|
||||||
|
info: LoopInfo64,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
let write_access = (info.lo_flags & LO_FLAGS_READ_ONLY) == 0;
|
||||||
|
let bf = OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(write_access)
|
||||||
|
.open(backing_file)?;
|
||||||
|
self.attach_fd_with_loop_info(bf, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach the loop device to a file descriptor with `LoopInfo64`.
|
||||||
|
fn attach_fd_with_loop_info(&self, bf: impl AsRawFd, info: LoopInfo64) -> io::Result<()> {
|
||||||
|
translate_error(unsafe {
|
||||||
|
ioctl(
|
||||||
|
self.device.as_raw_fd() as c_int,
|
||||||
|
LOOP_SET_FD as IoctlRequest,
|
||||||
|
bf.as_raw_fd() as c_int,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let result = unsafe {
|
||||||
|
ioctl(
|
||||||
|
self.device.as_raw_fd() as c_int,
|
||||||
|
LOOP_SET_STATUS64 as IoctlRequest,
|
||||||
|
&info,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
match translate_error(result) {
|
||||||
|
Err(err) => {
|
||||||
|
let _detach_err = self.detach();
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the path for the loop device.
|
||||||
|
pub fn path(&self) -> Option<PathBuf> {
|
||||||
|
let mut p = PathBuf::from("/proc/self/fd");
|
||||||
|
p.push(self.device.as_raw_fd().to_string());
|
||||||
|
std::fs::read_link(&p).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Detach a loop device.
|
||||||
|
pub fn detach(&self) -> io::Result<()> {
|
||||||
|
translate_error(unsafe {
|
||||||
|
ioctl(
|
||||||
|
self.device.as_raw_fd() as c_int,
|
||||||
|
LOOP_CLR_FD as IoctlRequest,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update a loop device's capacity.
|
||||||
|
pub fn set_capacity(&self) -> io::Result<()> {
|
||||||
|
translate_error(unsafe {
|
||||||
|
ioctl(
|
||||||
|
self.device.as_raw_fd() as c_int,
|
||||||
|
LOOP_SET_CAPACITY as IoctlRequest,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the major device node number.
|
||||||
|
pub fn major(&self) -> io::Result<u32> {
|
||||||
|
self.device
|
||||||
|
.metadata()
|
||||||
|
.map(|m| unsafe { libc::major(m.rdev()) })
|
||||||
|
.map(|m| m as u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the minor device node number.
|
||||||
|
pub fn minor(&self) -> io::Result<u32> {
|
||||||
|
self.device
|
||||||
|
.metadata()
|
||||||
|
.map(|m| unsafe { libc::minor(m.rdev()) })
|
||||||
|
.map(|m| m as u32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct LoopInfo64 {
|
||||||
|
lo_device: u64,
|
||||||
|
lo_inode: u64,
|
||||||
|
lo_rdevice: u64,
|
||||||
|
lo_offset: u64,
|
||||||
|
lo_sizelimit: u64,
|
||||||
|
lo_number: u32,
|
||||||
|
lo_encrypt_type: u32,
|
||||||
|
lo_encrypt_key_size: u32,
|
||||||
|
lo_flags: u32,
|
||||||
|
lo_file_name: [u8; 64],
|
||||||
|
lo_crypt_name: [u8; 64],
|
||||||
|
lo_encrypt_key: [u8; 32],
|
||||||
|
lo_init: [u64; 2],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LoopInfo64 {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
lo_device: 0,
|
||||||
|
lo_inode: 0,
|
||||||
|
lo_rdevice: 0,
|
||||||
|
lo_offset: 0,
|
||||||
|
lo_sizelimit: 0,
|
||||||
|
lo_number: 0,
|
||||||
|
lo_encrypt_type: 0,
|
||||||
|
lo_encrypt_key_size: 0,
|
||||||
|
lo_flags: 0,
|
||||||
|
lo_file_name: [0; 64],
|
||||||
|
lo_crypt_name: [0; 64],
|
||||||
|
lo_encrypt_key: [0; 32],
|
||||||
|
lo_init: [0, 2],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub struct AttachOptions<'d> {
|
||||||
|
device: &'d LoopDevice,
|
||||||
|
info: LoopInfo64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AttachOptions<'_> {
|
||||||
|
pub fn offset(mut self, offset: u64) -> Self {
|
||||||
|
self.info.lo_offset = offset;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size_limit(mut self, size_limit: u64) -> Self {
|
||||||
|
self.info.lo_sizelimit = size_limit;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_only(mut self, read_only: bool) -> Self {
|
||||||
|
if read_only {
|
||||||
|
self.info.lo_flags |= LO_FLAGS_READ_ONLY;
|
||||||
|
} else {
|
||||||
|
self.info.lo_flags &= !LO_FLAGS_READ_ONLY;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn autoclear(mut self, autoclear: bool) -> Self {
|
||||||
|
if autoclear {
|
||||||
|
self.info.lo_flags |= LO_FLAGS_AUTOCLEAR;
|
||||||
|
} else {
|
||||||
|
self.info.lo_flags &= !LO_FLAGS_AUTOCLEAR;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part_scan(mut self, part_scan: bool) -> Self {
|
||||||
|
if part_scan {
|
||||||
|
self.info.lo_flags |= LO_FLAGS_PARTSCAN;
|
||||||
|
} else {
|
||||||
|
self.info.lo_flags &= !LO_FLAGS_PARTSCAN;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_direct_io(mut self, direct_io: bool) -> Self {
|
||||||
|
if direct_io {
|
||||||
|
self.info.lo_flags |= LO_FLAGS_DIRECT_IO;
|
||||||
|
} else {
|
||||||
|
self.info.lo_flags &= !LO_FLAGS_DIRECT_IO;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn direct_io(&self) -> bool {
|
||||||
|
if (self.info.lo_flags & LO_FLAGS_DIRECT_IO) == LO_FLAGS_DIRECT_IO {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn attach(&self, backing_file: impl AsRef<Path>) -> io::Result<()> {
|
||||||
|
self.device
|
||||||
|
.attach_with_loop_info(backing_file, self.info.clone())?;
|
||||||
|
if self.direct_io() {
|
||||||
|
self.device.set_direct_io(self.direct_io())?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn attach_fd(&self, backing_file_fd: impl AsRawFd) -> io::Result<()> {
|
||||||
|
self.device
|
||||||
|
.attach_fd_with_loop_info(backing_file_fd, self.info.clone())?;
|
||||||
|
if self.direct_io() {
|
||||||
|
self.device.set_direct_io(self.direct_io())?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -16,10 +16,10 @@ krata = { path = "../krata", version = "^0.0.11" }
|
|||||||
krata-advmac = { workspace = true }
|
krata-advmac = { workspace = true }
|
||||||
krata-oci = { path = "../oci", version = "^0.0.11" }
|
krata-oci = { path = "../oci", version = "^0.0.11" }
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
loopdev-3 = { workspace = true }
|
|
||||||
serde_json = { workspace = true }
|
serde_json = { workspace = true }
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
uuid = { workspace = true }
|
uuid = { workspace = true }
|
||||||
|
krata-loopdev = { path = "../loopdev", version = "^0.0.11" }
|
||||||
krata-xenclient = { path = "../xen/xenclient", version = "^0.0.11" }
|
krata-xenclient = { path = "../xen/xenclient", version = "^0.0.11" }
|
||||||
krata-xenevtchn = { path = "../xen/xenevtchn", version = "^0.0.11" }
|
krata-xenevtchn = { path = "../xen/xenevtchn", version = "^0.0.11" }
|
||||||
krata-xengnt = { path = "../xen/xengnt", version = "^0.0.11" }
|
krata-xengnt = { path = "../xen/xengnt", version = "^0.0.11" }
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
use krataloopdev::{LoopControl, LoopDevice};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use loopdev::{LoopControl, LoopDevice};
|
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
use xenclient::BlockDeviceRef;
|
use xenclient::BlockDeviceRef;
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@ use std::{fs, net::Ipv4Addr, path::PathBuf, str::FromStr, sync::Arc};
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use ip::IpVendor;
|
use ip::IpVendor;
|
||||||
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
|
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
|
||||||
|
use krataloopdev::LoopControl;
|
||||||
use log::error;
|
use log::error;
|
||||||
use loopdev::LoopControl;
|
|
||||||
use tokio::sync::Semaphore;
|
use tokio::sync::Semaphore;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use xenclient::XenClient;
|
use xenclient::XenClient;
|
||||||
|
Loading…
Reference in New Issue
Block a user