Skip to content

Commit

Permalink
feat: implement VM pause and resume
Browse files Browse the repository at this point in the history
  • Loading branch information
00xc committed Oct 4, 2023
1 parent 2ba41bf commit 40b3272
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions firepilot/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use hyperlocal::{UnixClientExt, UnixConnector, Uri};
use tracing::{debug, error, info, instrument, trace};

use crate::machine::FirepilotError;
use firepilot_models::models::vm::Vm;
use firepilot_models::models::{BootSource, Drive, NetworkInterface};

/// Interface to determine how to execute commands on the socket and where to do it
Expand Down Expand Up @@ -213,6 +214,17 @@ impl Executor {
Ok(())
}

/// Sets the microVM the to the specified state
#[instrument(skip_all, fields(id = %self.id))]
pub async fn set_vm_state(&self, state: Vm) -> Result<(), ExecuteError> {
debug!("Change VM state: {:#?}", state);
let json = serde_json::to_string(&state).map_err(ExecuteError::Serialize)?;

let url: hyper::Uri = Uri::new(self.chroot().join("firecracker.socket"), "/vm").into();
self.send_request(url, Method::PATCH, json).await?;
Ok(())
}

/// Full path to the chroot of the machine which contains the socket, drives, kernel, etc...
pub fn chroot(&self) -> PathBuf {
self.executor().chroot().join(&self.id)
Expand Down
14 changes: 14 additions & 0 deletions firepilot/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use crate::{
executor::{Action, Executor},
};

use firepilot_models::models::vm::{State, Vm};

#[derive(Debug)]
pub enum FirepilotError {
/// Mostly problems related to directories error or unavailable files
Expand Down Expand Up @@ -151,4 +153,16 @@ impl Machine {
self.executor.send_action(Action::SendCtrlAltDel).await?;
Ok(())
}

/// Pause a running VM
pub async fn pause(&self) -> Result<(), FirepilotError> {
self.executor.set_vm_state(Vm::new(State::Paused)).await?;
Ok(())
}

/// Resume a paused VM
pub async fn resume(&self) -> Result<(), FirepilotError> {
self.executor.set_vm_state(Vm::new(State::Resumed)).await?;
Ok(())
}
}

0 comments on commit 40b3272

Please sign in to comment.