pub struct MicroVm { /* private fields */ }Expand description
A lightweight Linux virtual machine.
MicroVm provides a secure, isolated environment for running applications with their own filesystem, network, and resource constraints.
§Examples
use microsandbox_core::vm::{MicroVm, Rootfs};
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let vm = MicroVm::builder()
.rootfs(Rootfs::Native(temp_dir.path().to_path_buf()))
.ram_mib(1024)
.exec_path("/bin/echo")
.args(["Hello, World!"])
.build()?;
// Start the MicroVm
vm.start()?; // This would actually run the VMImplementations§
Source§impl MicroVm
impl MicroVm
Sourcepub fn get_config(&self) -> &MicroVmConfig
pub fn get_config(&self) -> &MicroVmConfig
The configuration for the MicroVm.
Source§impl MicroVm
impl MicroVm
Sourcepub fn from_config(config: MicroVmConfig) -> MicrosandboxResult<Self>
pub fn from_config(config: MicroVmConfig) -> MicrosandboxResult<Self>
Creates a new MicroVm from the given configuration.
This is a low-level constructor - prefer using MicroVm::builder()
for a more ergonomic interface.
§Errors
Returns an error if:
- The configuration is invalid
- Required resources cannot be allocated
- The system lacks required capabilities
Sourcepub fn builder() -> MicroVmBuilder<(), ()>
pub fn builder() -> MicroVmBuilder<(), ()>
Creates a builder for configuring a new MicroVm instance.
This is the recommended way to create a new MicroVm.
§Examples
use microsandbox_core::vm::{MicroVm, Rootfs};
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let vm = MicroVm::builder()
.rootfs(Rootfs::Native(temp_dir.path().to_path_buf()))
.ram_mib(1024)
.exec_path("/bin/echo")
.build()?;Sourcepub fn start(&self) -> MicrosandboxResult<i32>
pub fn start(&self) -> MicrosandboxResult<i32>
Starts the MicroVm and waits for it to complete.
This function will block until the MicroVm exits. The exit status of the guest process is returned.
§Examples
use microsandbox_core::vm::{MicroVm, Rootfs};
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let vm = MicroVm::builder()
.rootfs(Rootfs::Native(temp_dir.path().to_path_buf()))
.ram_mib(1024)
.exec_path("/usr/bin/python3")
.args(["-c", "print('Hello from MicroVm!')"])
.build()?;
// let status = vm.start()?;
// assert_eq!(status, 0); // Process exited successfully§Notes
- This function takes control of stdin/stdout
- The MicroVm is automatically cleaned up when this returns
- A non-zero status indicates the guest process failed
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MicroVm
impl RefUnwindSafe for MicroVm
impl Send for MicroVm
impl Sync for MicroVm
impl Unpin for MicroVm
impl UnwindSafe for MicroVm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more