Skip to content

Commit

Permalink
Sandboxer: Replace features with bin parameter
Browse files Browse the repository at this point in the history
Fix #54
  • Loading branch information
dierbei committed Aug 3, 2023
1 parent 052022d commit 70c7e7f
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 236 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target
!**/src/api/mod.rs
shim/Cargo.lock
vmm/common/Cargo.lock
!/vmm/sandbox/src/bin/
11 changes: 6 additions & 5 deletions vmm/sandbox/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions vmm/sandbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ edition = "2021"
[profile.release]
panic = 'abort'

[features]
default = []
qemu = ["qapi/qmp", "qapi/async-tokio-all", "unshare", "os_pipe"]
cloud_hypervisor = ["api_client"]
stratovirt = ["qapi/qmp", "qapi/async-tokio-all", "unshare", "os_pipe"]
# [features]
# default = []
# qemu = ["qapi/qmp", "qapi/async-tokio-all", "unshare", "os_pipe"]
# cloud_hypervisor = ["api_client"]
# stratovirt = ["qapi/qmp", "qapi/async-tokio-all", "unshare", "os_pipe"]

[dependencies]
tokio = { version = "1.19.2", features = ["full"] }
Expand All @@ -36,13 +36,18 @@ prost-types = "0.10.1"
time = "0.3.5"
log = { version = "0.4.17", features = ["std"] }
uuid = { version = "1.1.2", features = ["v4"] }
unshare = { version = "0.7.0", optional = true }
os_pipe = { version = "0.9.2", optional = true }
qapi = { version = "0.8.0", features = ["qmp", "async-tokio-all"], optional = true }
unshare = "0.7.0"
# unshare = { version = "0.7.0", optional = true }
# os_pipe = { version = "1.1.4", optional = true }
os_pipe = { version = "1.1.4" }
# qapi = { version = "0.13.0", features = ["qmp", "async-tokio-all"], optional = true }
qapi = { version = "0.8.0", features = ["qmp", "async-tokio-all"] }
sandbox-derive = { path = "derive" }
api_client = { git = "https://github.com/cloud-hypervisor/cloud-hypervisor.git", optional = true }
# api_client = { git = "https://github.com/cloud-hypervisor/cloud-hypervisor.git", optional = true }
api_client = { git = "https://github.com/cloud-hypervisor/cloud-hypervisor.git" }
rtnetlink = "0.12"
netlink-packet-route = "0.15"
netlink-packet-core = "0.5.0"
ttrpc = { version = "0.7", features = ["async"] }
protobuf = "3.2"
proc-macro2 = "1.0.66"
65 changes: 65 additions & 0 deletions vmm/sandbox/src/bin/cloud_hypervisor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2022 The Kuasar Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::str::FromStr;

use anyhow::Result;
use vmm_sandboxer::{
cloud_hypervisor::{
config::CloudHypervisorVMConfig, factory::CloudHypervisorVMFactory,
hooks::CloudHypervisorHooks,
},
load_config,
sandbox::KuasarSandboxer,
CONFIG_CLH_PATH,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize logging
let mut builder = env_logger::Builder::from_default_env();
builder.format_timestamp_micros();

// Initialize sandboxer
let sandboxer = init_cloud_hypervisor_sandboxer().await?;

// If 'log_level' field isn't set in the config file, keep the log level from the default env
// Otherwise, set the log level configured in the config file
if !sandboxer.log_level().is_empty() {
let log_level = log::LevelFilter::from_str(sandboxer.log_level())?;
builder.filter_level(log_level);
}
builder.init();

// Run the sandboxer
containerd_sandbox::run("kuasar-sandboxer", sandboxer)
.await
.unwrap();

Ok(())
}

async fn init_cloud_hypervisor_sandboxer(
) -> Result<KuasarSandboxer<CloudHypervisorVMFactory, CloudHypervisorHooks>> {
let (config, persist_dir_path) =
load_config::<CloudHypervisorVMConfig>(CONFIG_CLH_PATH).await?;
let hooks = CloudHypervisorHooks {};
let mut s = KuasarSandboxer::new(config.sandbox, config.hypervisor, hooks);
if !persist_dir_path.is_empty() {
s.recover(&persist_dir_path).await?;
}
Ok(s)
}
79 changes: 79 additions & 0 deletions vmm/sandbox/src/bin/qemu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2022 The Kuasar Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::{env, path::Path, str::FromStr};

use anyhow::Result;
use vmm_sandboxer::{
kata_config::KataConfig,
qemu::{factory::QemuVMFactory, hooks::QemuHooks},
sandbox::KuasarSandboxer,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize logging
let mut builder = env_logger::Builder::from_default_env();
builder.format_timestamp_micros();

// Initialize sandboxer
let sandboxer = init_qemu_sandboxer().await?;

// If 'log_level' field isn't set in the config file, keep the log level from the default env
// Otherwise, set the log level configured in the config file
if !sandboxer.log_level().is_empty() {
let log_level = log::LevelFilter::from_str(sandboxer.log_level())?;
builder.filter_level(log_level);
}
builder.init();

// Run the sandboxer
containerd_sandbox::run("kuasar-sandboxer", sandboxer)
.await
.unwrap();

Ok(())
}

async fn init_qemu_sandboxer() -> Result<KuasarSandboxer<QemuVMFactory, QemuHooks>> {
// For compatibility with kata config
let config_path = env::var("KATA_CONFIG_PATH")
.unwrap_or_else(|_| "/usr/share/defaults/kata-containers/configuration.toml".to_string());

let path = Path::new(&config_path);
if path.exists() {
KataConfig::init(path).await?;
}

let vmm_config = KataConfig::hypervisor_config("qemu", |h| h.clone()).await?;
let vmm_config = vmm_config.to_qemu_config()?;
let sandbox_config = KataConfig::sandbox_config("qemu").await?;
let hooks = QemuHooks::new(vmm_config.clone());
let mut s = KuasarSandboxer::new(sandbox_config, vmm_config, hooks);

// Check for "--dir" argument and recover from persisted directory
let os_args: Vec<_> = env::args_os().collect();
for i in 0..os_args.len() {
if os_args[i].to_str().unwrap() == "--dir" {
let persist_dir_path = os_args[i + 1].to_str().unwrap().to_string();
if Path::new(&persist_dir_path).exists() {
s.recover(&persist_dir_path).await?;
}
}
}

Ok(s)
}
65 changes: 65 additions & 0 deletions vmm/sandbox/src/bin/stratovirt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2022 The Kuasar Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use std::str::FromStr;

use anyhow::Result;
use env_logger::Builder;
use vmm_sandboxer::{
load_config,
sandbox::KuasarSandboxer,
stratovirt::{
config::StratoVirtVMConfig, factory::StratoVirtVMFactory, hooks::StratoVirtHooks,
},
CONFIG_STRATOVIRT_PATH,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize logging
let mut builder = Builder::from_default_env();
builder.format_timestamp_micros();

// Initialize sandboxer
let sandboxer = init_stratovirt_sandboxer().await?;

// If 'log_level' field isn't set in the config file, keep the log level from the default env
// Otherwise, set the log level configured in the config file
if !sandboxer.log_level().is_empty() {
let log_level = log::LevelFilter::from_str(sandboxer.log_level())?;
builder.filter_level(log_level);
}
builder.init();

// Run the sandboxer
containerd_sandbox::run("kuasar-sandboxer", sandboxer)
.await
.unwrap();

Ok(())
}

async fn init_stratovirt_sandboxer() -> Result<KuasarSandboxer<StratoVirtVMFactory, StratoVirtHooks>>
{
let (config, persist_dir_path) =
load_config::<StratoVirtVMConfig>(CONFIG_STRATOVIRT_PATH).await?;
let hooks = StratoVirtHooks::new(config.hypervisor.clone());
let mut s = KuasarSandboxer::new(config.sandbox, config.hypervisor, hooks);
if !persist_dir_path.is_empty() {
s.recover(&persist_dir_path).await?;
}
Ok(s)
}
2 changes: 1 addition & 1 deletion vmm/sandbox/src/cloud_hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl VM for CloudHypervisorVM {
}

async fn wait_channel(&self) -> Option<Receiver<(u32, i128)>> {
return self.wait_chan.clone();
self.wait_chan.clone()
}
}

Expand Down
9 changes: 5 additions & 4 deletions vmm/sandbox/src/kata_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ use tokio::sync::{RwLock, RwLockReadGuard};

use crate::{sandbox::SandboxConfig, utils::read_file, vm::ShareFsType};

cfg_qemu! {
use crate::qemu::config::QemuVMConfig;
}
// cfg_qemu! {
// use crate::qemu::config::QemuVMConfig;
// }
use crate::qemu::config::QemuVMConfig;

lazy_static! {
pub static ref CONFIG: RwLock<KataConfig> = {
Expand Down Expand Up @@ -215,7 +216,7 @@ impl KataConfig {
}
}

#[cfg(feature = "qemu")]
// #[cfg(feature = "qemu")]
impl Hypervisor {
#[allow(clippy::field_reassign_with_default)]
pub fn to_qemu_config(&self) -> Result<QemuVMConfig> {
Expand Down
Loading

0 comments on commit 70c7e7f

Please sign in to comment.