Skip to content

Commit b10926d

Browse files
committed
cli: Add container inspect
Prints JSON of container metadata/attributes of interest. For now this just renders out the kargs embedded in the container under the kargs.d drop-in. Future ideas for enhancements would be to include kernel version and whether or not the image uses a UKI. Closes: #1827 Signed-off-by: John Eckersberg <[email protected]>
1 parent 6a9309e commit b10926d

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

crates/lib/src/cli.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ pub(crate) enum InstallOpts {
295295
/// Subcommands which can be executed as part of a container build.
296296
#[derive(Debug, clap::Subcommand, PartialEq, Eq)]
297297
pub(crate) enum ContainerOpts {
298+
/// Output JSON to stdout containing the container image metadata.
299+
Inspect {
300+
/// Operate on the provided rootfs.
301+
#[clap(long, default_value = "/")]
302+
rootfs: Utf8PathBuf,
303+
},
298304
/// Perform relatively inexpensive static analysis checks as part of a container
299305
/// build.
300306
///
@@ -1345,6 +1351,14 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
13451351
}
13461352
}
13471353
Opt::Container(opts) => match opts {
1354+
ContainerOpts::Inspect { rootfs } => {
1355+
let root = &Dir::open_ambient_dir(&rootfs, cap_std::ambient_authority())?;
1356+
let kargs = crate::bootc_kargs::get_kargs_in_root(root, std::env::consts::ARCH)?;
1357+
let kargs: Vec<String> = kargs.iter_str().map(|s| s.to_owned()).collect();
1358+
let inspect = crate::spec::ContainerInspect { kargs };
1359+
serde_json::to_writer_pretty(std::io::stdout().lock(), &inspect)?;
1360+
Ok(())
1361+
}
13481362
ContainerOpts::Lint {
13491363
rootfs,
13501364
fatal_warnings,

crates/lib/src/spec.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ pub(crate) struct DeploymentEntry<'a> {
275275
pub(crate) pinned: bool,
276276
}
277277

278+
/// The result of a `bootc container inspect` command.
279+
#[derive(Debug, Serialize)]
280+
#[serde(rename_all = "kebab-case")]
281+
pub(crate) struct ContainerInspect {
282+
/// Kernel arguments embedded in the container image.
283+
pub(crate) kargs: Vec<String>,
284+
}
285+
278286
impl Host {
279287
/// Create a new host
280288
pub fn new(spec: HostSpec) -> Self {

crates/tests-integration/src/container.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ pub(crate) fn test_bootc_status() -> Result<()> {
2121
Ok(())
2222
}
2323

24+
pub(crate) fn test_bootc_container_inspect() -> Result<()> {
25+
let sh = Shell::new()?;
26+
let inspect: serde_json::Value =
27+
serde_json::from_str(&cmd!(sh, "bootc container inspect").read()?)?;
28+
29+
// check kargs processing
30+
let kargs = inspect.get("kargs").unwrap().as_array().unwrap();
31+
assert!(kargs.iter().any(|arg| arg == "kargsd-test=1"));
32+
assert!(kargs.iter().any(|arg| arg == "kargsd-othertest=2"));
33+
assert!(kargs.iter().any(|arg| arg == "testing-kargsd=3"));
34+
35+
Ok(())
36+
}
37+
2438
pub(crate) fn test_bootc_upgrade() -> Result<()> {
2539
for c in ["upgrade", "update"] {
2640
let o = Command::new("bootc").arg(c).output()?;
@@ -120,6 +134,7 @@ pub(crate) fn run(testargs: libtest_mimic::Arguments) -> Result<()> {
120134
new_test("install config", test_bootc_install_config),
121135
new_test("printconfig --all", test_bootc_install_config_all),
122136
new_test("status", test_bootc_status),
137+
new_test("container inspect", test_bootc_container_inspect),
123138
new_test("system-reinstall --help", test_system_reinstall_help),
124139
];
125140

0 commit comments

Comments
 (0)