-
Notifications
You must be signed in to change notification settings - Fork 10
feat: implement vsock configuration for VMs #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
norwoodj
wants to merge
1
commit into
rik-org:main
Choose a base branch
from
norwoodj:jnorwood/vsock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| use crate::builder::{Builder, BuilderError}; | ||
| use firepilot_models::models::Vsock; | ||
|
|
||
| use super::assert_not_none; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct VsockBuilder { | ||
| pub guest_cid: Option<i32>, | ||
| pub uds_path: Option<String>, | ||
| } | ||
|
|
||
| impl Default for VsockBuilder { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl VsockBuilder { | ||
| pub fn new() -> VsockBuilder { | ||
| VsockBuilder { | ||
| guest_cid: None, | ||
| uds_path: None, | ||
| } | ||
| } | ||
|
|
||
| pub fn with_guest_cid(mut self, guest_cid: i32) -> VsockBuilder { | ||
| self.guest_cid = Some(guest_cid); | ||
| self | ||
| } | ||
|
|
||
| pub fn with_uds_path(mut self, uds_path: String) -> VsockBuilder { | ||
| self.uds_path = Some(uds_path); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Builder<Vsock> for VsockBuilder { | ||
| fn try_build(self) -> Result<Vsock, BuilderError> { | ||
| assert_not_none(stringify!(self.guest_cid), &self.guest_cid)?; | ||
| assert_not_none(stringify!(self.uds_path), &self.uds_path)?; | ||
| Ok(Vsock { | ||
| guest_cid: self.guest_cid.unwrap(), | ||
| uds_path: self.uds_path.unwrap(), | ||
| vsock_id: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::builder::vsock::VsockBuilder; | ||
| use crate::builder::Builder; | ||
|
|
||
| #[test] | ||
| fn full_kernel() { | ||
| VsockBuilder::new() | ||
| .with_guest_cid(3) | ||
| .with_uds_path("/tmp/fc.sock".to_string()) | ||
| .try_build() | ||
| .unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn partial_kernel() { | ||
| VsockBuilder::new().with_guest_cid(3).try_build().unwrap(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,7 +27,7 @@ use tracing::{debug, error, info, instrument, trace}; | |||||||||||||||||||
|
|
||||||||||||||||||||
| use crate::machine::FirepilotError; | ||||||||||||||||||||
| use firepilot_models::models::vm::Vm; | ||||||||||||||||||||
| use firepilot_models::models::{BootSource, Drive, NetworkInterface}; | ||||||||||||||||||||
| use firepilot_models::models::{BootSource, Drive, NetworkInterface, Vsock}; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Interface to determine how to execute commands on the socket and where to do it | ||||||||||||||||||||
| pub trait Execute { | ||||||||||||||||||||
|
|
@@ -61,7 +61,7 @@ impl From<ExecuteError> for FirepilotError { | |||||||||||||||||||
| fn from(e: ExecuteError) -> FirepilotError { | ||||||||||||||||||||
| match e { | ||||||||||||||||||||
| ExecuteError::CommandExecution(e) => FirepilotError::Setup(e), | ||||||||||||||||||||
| ExecuteError::Request(url, e) => FirepilotError::Configure(format!("{}: {}", url, e)), | ||||||||||||||||||||
| ExecuteError::Request(url, e) => FirepilotError::Configure(format!("{url}: {e}")), | ||||||||||||||||||||
| ExecuteError::Serialize(e) => FirepilotError::Configure(e.to_string()), | ||||||||||||||||||||
| ExecuteError::Socket(e) => FirepilotError::Configure(e), | ||||||||||||||||||||
| ExecuteError::WorkspaceCreation(e) => FirepilotError::Setup(e), | ||||||||||||||||||||
|
|
@@ -102,6 +102,12 @@ pub struct Executor { | |||||||||||||||||||
| id: String, | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| impl Default for Executor { | ||||||||||||||||||||
| fn default() -> Self { | ||||||||||||||||||||
| Self::new() | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| impl Executor { | ||||||||||||||||||||
|
Comment on lines
+105
to
111
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| /// Create a new Executor with no implementation, and with id "default" | ||||||||||||||||||||
| pub fn new() -> Executor { | ||||||||||||||||||||
|
|
@@ -135,7 +141,7 @@ impl Executor { | |||||||||||||||||||
| /// Return the configured executor, or panic if none is configured | ||||||||||||||||||||
| fn executor(&self) -> &dyn Execute { | ||||||||||||||||||||
| match &self.firecracker { | ||||||||||||||||||||
| Some(firecracker) => return firecracker, | ||||||||||||||||||||
| Some(firecracker) => firecracker, | ||||||||||||||||||||
| None => panic!("No executor found"), | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -195,8 +201,7 @@ impl Executor { | |||||||||||||||||||
| String::from_utf8(body.to_vec()).unwrap() | ||||||||||||||||||||
| ); | ||||||||||||||||||||
| return Err(ExecuteError::CommandExecution(format!( | ||||||||||||||||||||
| "Failed to send request to {}, status: {}", | ||||||||||||||||||||
| url, status | ||||||||||||||||||||
| "Failed to send request to {url}, status: {status}" | ||||||||||||||||||||
| ))); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -318,6 +323,19 @@ impl Executor { | |||||||||||||||||||
| Ok(()) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Apply vsock configuration on the VM | ||||||||||||||||||||
| #[instrument(skip_all, fields(id = %self.id))] | ||||||||||||||||||||
| pub async fn configure_vsock(&self, vsock: Vsock) -> Result<(), ExecuteError> { | ||||||||||||||||||||
| debug!("Configure vsock"); | ||||||||||||||||||||
| let json = serde_json::to_string(&vsock).map_err(ExecuteError::Serialize)?; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| let path = "/vsock"; | ||||||||||||||||||||
| let url: hyper::Uri = Uri::new(self.chroot().join("firecracker.socket"), path).into(); | ||||||||||||||||||||
| self.send_request(url, Method::PUT, json).await?; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /// Create needed folders where the VM will be configured | ||||||||||||||||||||
| #[instrument(skip(self), fields(id = %self.id))] | ||||||||||||||||||||
| pub fn create_workspace(&self) -> Result<(), ExecuteError> { | ||||||||||||||||||||
|
|
||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than having a panic with no context (if it fails), please add the expected behaviour in a
expect