diff --git a/Cargo.lock b/Cargo.lock index ad67967d8c..1befff8702 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4005,6 +4005,7 @@ version = "0.1.0" dependencies = [ "anyhow", "common", + "containerd-shim-protos", "go-flag", "logging", "nix 0.26.4", diff --git a/src/runtime-rs/Cargo.toml b/src/runtime-rs/Cargo.toml index ad5e632948..f7f35cd97a 100644 --- a/src/runtime-rs/Cargo.toml +++ b/src/runtime-rs/Cargo.toml @@ -22,6 +22,7 @@ cloud-hypervisor = ["runtimes/cloud-hypervisor"] [dependencies] anyhow = { workspace = true } +containerd-shim-protos = { workspace = true } go-flag = { workspace = true } nix = { workspace = true } tokio = { workspace = true, features = ["rt", "rt-multi-thread"] } diff --git a/src/runtime-rs/crates/shim/src/bin/main.rs b/src/runtime-rs/crates/shim/src/bin/main.rs index acc373ccdb..2c93532b78 100644 --- a/src/runtime-rs/crates/shim/src/bin/main.rs +++ b/src/runtime-rs/crates/shim/src/bin/main.rs @@ -6,10 +6,15 @@ use std::{ ffi::{OsStr, OsString}, + io::Write, path::PathBuf, }; use anyhow::{anyhow, Context, Result}; +use containerd_shim_protos::{ + protobuf::Message, + types::introspection::{RuntimeInfo, RuntimeVersion}, +}; use nix::{ mount::{mount, MsFlags}, sched::{self, CloneFlags}, @@ -29,11 +34,13 @@ enum Action { Delete(Args), Help, Version, + Info, } fn parse_args(args: &[OsString]) -> Result { let mut help = false; let mut version = false; + let mut info = false; let mut shim_args = Args::default(); // Crate `go_flag` is used to keep compatible with go/flag package. @@ -46,6 +53,7 @@ fn parse_args(args: &[OsString]) -> Result { flags.add_flag("publish-binary", &mut shim_args.publish_binary); flags.add_flag("help", &mut help); flags.add_flag("version", &mut version); + flags.add_flag("info", &mut info); }) .context(Error::ParseArgument(format!("{args:?}")))?; @@ -53,6 +61,8 @@ fn parse_args(args: &[OsString]) -> Result { Ok(Action::Help) } else if version { Ok(Action::Version) + } else if info { + Ok(Action::Info) } else if rest_args.is_empty() { Ok(Action::Run(shim_args)) } else if rest_args[0] == "start" { @@ -83,6 +93,8 @@ fn show_help(cmd: &OsStr) { enable debug output in logs -id string id of the task + -info + output the runtime info as protobuf (for containerd v2.0+) -namespace string namespace that owns the shim -publish-binary string @@ -114,6 +126,25 @@ fn show_version(err: Option) { } } +fn show_info() -> Result<()> { + let mut version = RuntimeVersion::new(); + version.version = config::RUNTIME_VERSION.to_string(); + version.revision = config::RUNTIME_GIT_COMMIT.to_string(); + + let mut info = RuntimeInfo::new(); + info.name = config::CONTAINERD_RUNTIME_NAME.to_string(); + info.version = Some(version).into(); + + let data = info + .write_to_bytes() + .context("failed to marshal RuntimeInfo")?; + std::io::stdout() + .write_all(&data) + .context("failed to write RuntimeInfo to stdout")?; + + Ok(()) +} + fn get_tokio_runtime() -> Result { let worker_threads = std::env::var(ENV_TOKIO_RUNTIME_WORKER_THREADS) .unwrap_or_default() @@ -155,6 +186,7 @@ fn real_main() -> Result<()> { } Action::Help => show_help(&args[0]), Action::Version => show_version(None), + Action::Info => show_info().context("show info")?, } Ok(()) }