Skip to content

Commit

Permalink
Merge pull request #110 from morningtzh/tzh-version
Browse files Browse the repository at this point in the history
vmm: add version info
  • Loading branch information
Burning1020 authored Jan 15, 2024
2 parents af46231 + b4d451f commit 311e0ed
Show file tree
Hide file tree
Showing 12 changed files with 550 additions and 40 deletions.
421 changes: 408 additions & 13 deletions vmm/sandbox/Cargo.lock

Large diffs are not rendered by default.

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

[build-dependencies]
built = { version = "0.7.0", features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }

[dependencies]
built = { version = "0.7.0", features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }
clap = { version="4.4.2", features=["derive"] }
tokio = { version = "1.19.2", features = ["full"] }
containerd-sandbox = {git="https://github.com/kuasar-io/rust-extensions.git"}
containerd-shim = { git="https://github.com/kuasar-io/rust-extensions.git", features=["async"] }
Expand Down
18 changes: 18 additions & 0 deletions vmm/sandbox/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2024 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.
*/
fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
}
36 changes: 36 additions & 0 deletions vmm/sandbox/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 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 clap::Parser;

#[derive(Parser, Debug)]
#[command(author, about, long_about = None)]
pub struct Args {
/// Version info
#[arg(short, long)]
pub version: bool,

/// Config file path, only for cloud hypervisor and stratovirt
#[arg(short, long, value_name = "FILE")]
pub config: Option<String>,

/// Sandboxer working directory
#[arg(short, long, value_name = "DIR")]
pub dir: Option<String>,

/// Address for sandboxer's server
#[arg(short, long, value_name = "FILE")]
pub listen: Option<String>,
}
12 changes: 10 additions & 2 deletions vmm/sandbox/src/bin/cloud_hypervisor/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use vmm_sandboxer::{cloud_hypervisor::init_cloud_hypervisor_sandboxer, utils::init_logger};
use clap::Parser;
use vmm_sandboxer::{
args, cloud_hypervisor::init_cloud_hypervisor_sandboxer, utils::init_logger, version,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = args::Args::parse();
if args.version {
version::print_version_info();
return Ok(());
}
// Initialize sandboxer
let sandboxer = init_cloud_hypervisor_sandboxer().await?;
let sandboxer = init_cloud_hypervisor_sandboxer(&args).await?;

// Initialize log
init_logger(sandboxer.log_level());
Expand Down
11 changes: 9 additions & 2 deletions vmm/sandbox/src/bin/qemu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use vmm_sandboxer::{qemu::init_qemu_sandboxer, utils::init_logger};
use clap::Parser;
use vmm_sandboxer::{args, qemu::init_qemu_sandboxer, utils::init_logger, version};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = args::Args::parse();
if args.version {
version::print_version_info();
return Ok(());
}

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

// Initialize log
init_logger(sandboxer.log_level());
Expand Down
11 changes: 9 additions & 2 deletions vmm/sandbox/src/bin/stratovirt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use vmm_sandboxer::{stratovirt::init_stratovirt_sandboxer, utils::init_logger};
use clap::Parser;
use vmm_sandboxer::{args, stratovirt::init_stratovirt_sandboxer, utils::init_logger, version};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = args::Args::parse();
if args.version {
version::print_version_info();
return Ok(());
}

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

// Initialize log
init_logger(sandboxer.log_level());
Expand Down
4 changes: 3 additions & 1 deletion vmm/sandbox/src/cloud_hypervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use vmm_common::SHARED_DIR_SUFFIX;

use self::{factory::CloudHypervisorVMFactory, hooks::CloudHypervisorHooks};
use crate::{
args::Args,
cloud_hypervisor::{
client::ChClient,
config::{CloudHypervisorConfig, CloudHypervisorVMConfig, VirtiofsdConfig},
Expand Down Expand Up @@ -346,9 +347,10 @@ fn spawn_wait(
}

pub async fn init_cloud_hypervisor_sandboxer(
args: &Args,
) -> Result<KuasarSandboxer<CloudHypervisorVMFactory, CloudHypervisorHooks>> {
let (config, persist_dir_path) =
load_config::<CloudHypervisorVMConfig>(CONFIG_CLH_PATH).await?;
load_config::<CloudHypervisorVMConfig>(args, CONFIG_CLH_PATH).await?;
let hooks = CloudHypervisorHooks {};
let mut s = KuasarSandboxer::new(config.sandbox, config.hypervisor, hooks);
if !persist_dir_path.is_empty() {
Expand Down
26 changes: 15 additions & 11 deletions vmm/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use anyhow::Context;
use serde::de::DeserializeOwned;

use crate::{config::Config, sandbox::KuasarSandbox};
use crate::{args::Args, config::Config, sandbox::KuasarSandbox};

#[macro_use]
mod device;
Expand All @@ -30,31 +31,34 @@ mod param;
mod storage;
mod vm;

pub mod args;
pub mod cloud_hypervisor;
pub mod config;
pub mod kata_config;
pub mod qemu;
pub mod sandbox;
pub mod stratovirt;
pub mod utils;
pub mod version;

async fn load_config<T: DeserializeOwned>(
args: &Args,
default_config_path: &str,
) -> anyhow::Result<(Config<T>, String)> {
let os_args: Vec<_> = std::env::args_os().collect();
let mut config_path = default_config_path.to_string();
let mut dir_path = String::new();
for i in 0..os_args.len() {
if os_args[i].to_str().unwrap() == "--config" {
config_path = os_args[i + 1].to_str().unwrap().to_string()
}
if os_args[i].to_str().unwrap() == "--dir" {
dir_path = os_args[i + 1].to_str().unwrap().to_string();
if !std::path::Path::new(&dir_path).exists() {
tokio::fs::create_dir_all(&dir_path).await.unwrap();
}
if let Some(c) = &args.config {
config_path = c.to_string();
}
if let Some(d) = &args.dir {
dir_path = d.to_string();
if !std::path::Path::new(&dir_path).exists() {
tokio::fs::create_dir_all(&dir_path)
.await
.with_context(|| format!("Failed to mkdir for {}", dir_path))?;
}
}

let path = std::path::Path::new(&config_path);
let config: Config<T> = if path.exists() {
Config::parse(path).await?
Expand Down
13 changes: 5 additions & 8 deletions vmm/sandbox/src/qemu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use unshare::Fd;

use self::{factory::QemuVMFactory, hooks::QemuHooks};
use crate::{
args::Args,
device::{BusType, DeviceInfo, SlotStatus, Transport},
impl_recoverable,
kata_config::KataConfig,
Expand Down Expand Up @@ -533,7 +534,7 @@ impl QemuVM {

impl_recoverable!(QemuVM);

pub async fn init_qemu_sandboxer() -> Result<KuasarSandboxer<QemuVMFactory, QemuHooks>> {
pub async fn init_qemu_sandboxer(args: &Args) -> Result<KuasarSandboxer<QemuVMFactory, QemuHooks>> {
// For compatibility with kata config
let config_path = std::env::var("KATA_CONFIG_PATH")
.unwrap_or_else(|_| "/usr/share/defaults/kata-containers/configuration.toml".to_string());
Expand All @@ -550,13 +551,9 @@ pub async fn init_qemu_sandboxer() -> Result<KuasarSandboxer<QemuVMFactory, Qemu
let mut s = KuasarSandboxer::new(sandbox_config, vmm_config, hooks);

// Check for "--dir" argument and recover from persisted directory
let os_args: Vec<_> = std::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 std::path::Path::new(&persist_dir_path).exists() {
s.recover(&persist_dir_path).await?;
}
if let Some(persist_dir_path) = &args.dir {
if std::path::Path::new(&persist_dir_path).exists() {
s.recover(persist_dir_path).await?;
}
}

Expand Down
4 changes: 3 additions & 1 deletion vmm/sandbox/src/stratovirt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use self::{
hooks::StratoVirtHooks,
};
use crate::{
args::Args,
device::{Bus, BusType, DeviceInfo, Slot, SlotStatus},
impl_recoverable, load_config,
param::ToCmdLineParams,
Expand Down Expand Up @@ -554,9 +555,10 @@ impl StratoVirtVM {
impl_recoverable!(StratoVirtVM);

pub async fn init_stratovirt_sandboxer(
args: &Args,
) -> Result<KuasarSandboxer<StratoVirtVMFactory, StratoVirtHooks>> {
let (config, persist_dir_path) =
load_config::<StratoVirtVMConfig>(CONFIG_STRATOVIRT_PATH).await?;
load_config::<StratoVirtVMConfig>(args, 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() {
Expand Down
29 changes: 29 additions & 0 deletions vmm/sandbox/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 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.
*/
pub mod built_info {
// The file has been placed there by the build script.
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

pub fn print_version_info() {
if let Some(v) = built_info::GIT_VERSION {
match built_info::GIT_DIRTY {
Some(true) => println!("Version: {}-dirty", v),
_ => println!("Version: {}", v),
}
}
println!("Build Time: {}", built_info::BUILT_TIME_UTC)
}

0 comments on commit 311e0ed

Please sign in to comment.