1use std::borrow::Cow;
2use std::collections::VecDeque;
3use std::io::IsTerminal;
4use std::io::Read;
5use std::io::Write;
6
7use anyhow::{Context, Result};
8use canon_json::CanonJsonSerialize;
9use fn_error_context::context;
10use ostree::glib;
11use ostree_container::OstreeImageReference;
12use ostree_ext::container as ostree_container;
13use ostree_ext::keyfileext::KeyFileExt;
14use ostree_ext::oci_spec;
15use ostree_ext::oci_spec::image::Digest;
16use ostree_ext::oci_spec::image::ImageConfiguration;
17use ostree_ext::sysroot::SysrootLock;
18use unicode_width::UnicodeWidthStr;
19
20use ostree_ext::ostree;
21
22use crate::cli::OutputFormat;
23use crate::spec::BootEntryComposefs;
24use crate::spec::ImageStatus;
25use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType};
26use crate::spec::{ImageReference, ImageSignature};
27use crate::store::BootedStorage;
28use crate::store::BootedStorageKind;
29use crate::store::CachedImageStatus;
30
31impl From<ostree_container::SignatureSource> for ImageSignature {
32 fn from(sig: ostree_container::SignatureSource) -> Self {
33 use ostree_container::SignatureSource;
34 match sig {
35 SignatureSource::OstreeRemote(r) => Self::OstreeRemote(r),
36 SignatureSource::ContainerPolicy => Self::ContainerPolicy,
37 SignatureSource::ContainerPolicyAllowInsecure => Self::Insecure,
38 }
39 }
40}
41
42impl From<ImageSignature> for ostree_container::SignatureSource {
43 fn from(sig: ImageSignature) -> Self {
44 use ostree_container::SignatureSource;
45 match sig {
46 ImageSignature::OstreeRemote(r) => SignatureSource::OstreeRemote(r),
47 ImageSignature::ContainerPolicy => Self::ContainerPolicy,
48 ImageSignature::Insecure => Self::ContainerPolicyAllowInsecure,
49 }
50 }
51}
52
53fn transport_to_string(transport: ostree_container::Transport) -> String {
55 match transport {
56 ostree_container::Transport::Registry => "registry".to_string(),
58 o => {
59 let mut s = o.to_string();
60 s.truncate(s.rfind(':').unwrap());
61 s
62 }
63 }
64}
65
66impl From<OstreeImageReference> for ImageReference {
67 fn from(imgref: OstreeImageReference) -> Self {
68 let signature = match imgref.sigverify {
69 ostree_container::SignatureSource::ContainerPolicyAllowInsecure => None,
70 v => Some(v.into()),
71 };
72 Self {
73 signature,
74 transport: transport_to_string(imgref.imgref.transport),
75 image: imgref.imgref.name,
76 }
77 }
78}
79
80impl From<ImageReference> for OstreeImageReference {
81 fn from(img: ImageReference) -> Self {
82 let sigverify = match img.signature {
83 Some(v) => v.into(),
84 None => ostree_container::SignatureSource::ContainerPolicyAllowInsecure,
85 };
86 Self {
87 sigverify,
88 imgref: ostree_container::ImageReference {
89 transport: img.transport.as_str().try_into().unwrap(),
91 name: img.image,
92 },
93 }
94 }
95}
96
97fn check_selinux_policy_compatible(
100 sysroot: &SysrootLock,
101 booted_deployment: &ostree::Deployment,
102 target_deployment: &ostree::Deployment,
103) -> Result<bool> {
104 if !crate::lsm::selinux_enabled()? {
106 return Ok(true);
107 }
108
109 let booted_fd = crate::utils::deployment_fd(sysroot, booted_deployment)
110 .context("Failed to get file descriptor for booted deployment")?;
111 let booted_policy = crate::lsm::new_sepolicy_at(&booted_fd)
112 .context("Failed to load SELinux policy from booted deployment")?;
113 let target_fd = crate::utils::deployment_fd(sysroot, target_deployment)
114 .context("Failed to get file descriptor for target deployment")?;
115 let target_policy = crate::lsm::new_sepolicy_at(&target_fd)
116 .context("Failed to load SELinux policy from target deployment")?;
117
118 let booted_csum = booted_policy.and_then(|p| p.csum());
119 let target_csum = target_policy.and_then(|p| p.csum());
120
121 match (booted_csum, target_csum) {
122 (None, None) => Ok(true), (Some(_), None) | (None, Some(_)) => {
124 Ok(false)
126 }
127 (Some(booted_csum), Some(target_csum)) => {
128 Ok(booted_csum == target_csum)
130 }
131 }
132}
133
134fn has_soft_reboot_capability(sysroot: &SysrootLock, deployment: &ostree::Deployment) -> bool {
137 if !ostree_ext::systemd_has_soft_reboot() {
138 return false;
139 }
140
141 let has_ostree_karg = deployment
146 .bootconfig()
147 .and_then(|bootcfg| bootcfg.get("options"))
148 .map(|options| options.contains("ostree="))
149 .unwrap_or(false);
150
151 if !ostree::check_version(2025, 7) && !has_ostree_karg {
152 return false;
153 }
154
155 if !sysroot.deployment_can_soft_reboot(deployment) {
156 return false;
157 }
158
159 if let Some(booted_deployment) = sysroot.booted_deployment() {
162 if !check_selinux_policy_compatible(sysroot, &booted_deployment, deployment)
164 .expect("deployment_fd should not fail for valid deployments")
165 {
166 return false;
167 }
168 }
169
170 true
171}
172
173fn get_image_origin(origin: &glib::KeyFile) -> Result<Option<OstreeImageReference>> {
176 origin
177 .optional_string("origin", ostree_container::deploy::ORIGIN_CONTAINER)
178 .context("Failed to load container image from origin")?
179 .map(|v| ostree_container::OstreeImageReference::try_from(v.as_str()))
180 .transpose()
181}
182
183pub(crate) struct Deployments {
184 pub(crate) staged: Option<ostree::Deployment>,
185 pub(crate) rollback: Option<ostree::Deployment>,
186 #[allow(dead_code)]
187 pub(crate) other: VecDeque<ostree::Deployment>,
188}
189
190pub(crate) fn labels_of_config(
191 config: &oci_spec::image::ImageConfiguration,
192) -> Option<&std::collections::HashMap<String, String>> {
193 config.config().as_ref().and_then(|c| c.labels().as_ref())
194}
195
196fn create_imagestatus(
198 image: ImageReference,
199 manifest_digest: &Digest,
200 config: &ImageConfiguration,
201) -> ImageStatus {
202 let labels = labels_of_config(config);
203 let timestamp = labels
204 .and_then(|l| {
205 l.get(oci_spec::image::ANNOTATION_CREATED)
206 .map(|s| s.as_str())
207 })
208 .or_else(|| config.created().as_deref())
209 .and_then(bootc_utils::try_deserialize_timestamp);
210
211 let version = ostree_container::version_for_config(config).map(ToOwned::to_owned);
212 let architecture = config.architecture().to_string();
213 ImageStatus {
214 image,
215 version,
216 timestamp,
217 image_digest: manifest_digest.to_string(),
218 architecture,
219 }
220}
221
222fn imagestatus(
223 sysroot: &SysrootLock,
224 deployment: &ostree::Deployment,
225 image: ostree_container::OstreeImageReference,
226) -> Result<CachedImageStatus> {
227 let repo = &sysroot.repo();
228 let imgstate = ostree_container::store::query_image_commit(repo, &deployment.csum())?;
229 let image = ImageReference::from(image);
230 let cached = imgstate
231 .cached_update
232 .map(|cached| create_imagestatus(image.clone(), &cached.manifest_digest, &cached.config));
233 let imagestatus = create_imagestatus(image, &imgstate.manifest_digest, &imgstate.configuration);
234
235 Ok(CachedImageStatus {
236 image: Some(imagestatus),
237 cached_update: cached,
238 })
239}
240
241#[context("Reading deployment metadata")]
243pub(crate) fn boot_entry_from_deployment(
244 sysroot: &SysrootLock,
245 deployment: &ostree::Deployment,
246) -> Result<BootEntry> {
247 let (
248 CachedImageStatus {
249 image,
250 cached_update,
251 },
252 incompatible,
253 ) = if let Some(origin) = deployment.origin().as_ref() {
254 let incompatible = crate::utils::origin_has_rpmostree_stuff(origin);
255 let cached_imagestatus = if incompatible {
256 CachedImageStatus::default()
258 } else if let Some(image) = get_image_origin(origin)? {
259 imagestatus(sysroot, deployment, image)?
260 } else {
261 CachedImageStatus::default()
263 };
264 (cached_imagestatus, incompatible)
265 } else {
266 (CachedImageStatus::default(), false)
268 };
269
270 let soft_reboot_capable = has_soft_reboot_capability(sysroot, deployment);
271 let download_only = deployment.is_staged() && deployment.is_finalization_locked();
272 let store = Some(crate::spec::Store::OstreeContainer);
273 let r = BootEntry {
274 image,
275 cached_update,
276 incompatible,
277 soft_reboot_capable,
278 download_only,
279 store,
280 pinned: deployment.is_pinned(),
281 ostree: Some(crate::spec::BootEntryOstree {
282 checksum: deployment.csum().into(),
283 deploy_serial: deployment.deployserial().try_into().unwrap(),
285 stateroot: deployment.stateroot().into(),
286 }),
287 composefs: None,
288 };
289 Ok(r)
290}
291
292impl BootEntry {
293 pub(crate) fn query_image(
295 &self,
296 repo: &ostree::Repo,
297 ) -> Result<Option<Box<ostree_container::store::LayeredImageState>>> {
298 if self.image.is_none() {
299 return Ok(None);
300 }
301 if let Some(checksum) = self.ostree.as_ref().map(|c| c.checksum.as_str()) {
302 ostree_container::store::query_image_commit(repo, checksum).map(Some)
303 } else {
304 Ok(None)
305 }
306 }
307
308 pub(crate) fn require_composefs(&self) -> Result<&BootEntryComposefs> {
309 self.composefs.as_ref().ok_or(anyhow::anyhow!(
310 "BootEntry is not a composefs native boot entry"
311 ))
312 }
313
314 pub(crate) fn composefs_boot_digest(&self) -> Result<&String> {
319 self.require_composefs()?
320 .boot_digest
321 .as_ref()
322 .ok_or_else(|| anyhow::anyhow!("Could not find boot digest for deployment"))
323 }
324}
325
326pub(crate) fn get_status_require_booted(
328 sysroot: &SysrootLock,
329) -> Result<(crate::store::BootedOstree<'_>, Deployments, Host)> {
330 let booted_deployment = sysroot.require_booted_deployment()?;
331 let booted_ostree = crate::store::BootedOstree {
332 sysroot,
333 deployment: booted_deployment,
334 };
335 let (deployments, host) = get_status(&booted_ostree)?;
336 Ok((booted_ostree, deployments, host))
337}
338
339#[context("Computing status")]
342pub(crate) fn get_status(
343 booted_ostree: &crate::store::BootedOstree<'_>,
344) -> Result<(Deployments, Host)> {
345 let sysroot = booted_ostree.sysroot;
346 let booted_deployment = Some(&booted_ostree.deployment);
347 let stateroot = booted_deployment.as_ref().map(|d| d.osname());
348 let (mut related_deployments, other_deployments) = sysroot
349 .deployments()
350 .into_iter()
351 .partition::<VecDeque<_>, _>(|d| Some(d.osname()) == stateroot);
352 let staged = related_deployments
353 .iter()
354 .position(|d| d.is_staged())
355 .map(|i| related_deployments.remove(i).unwrap());
356 tracing::debug!("Staged: {staged:?}");
357 if let Some(booted) = booted_deployment.as_ref() {
359 related_deployments.retain(|f| !f.equal(booted));
360 }
361 let rollback = related_deployments.pop_front();
362 let rollback_queued = match (booted_deployment.as_ref(), rollback.as_ref()) {
363 (Some(booted), Some(rollback)) => rollback.index() < booted.index(),
364 _ => false,
365 };
366 let boot_order = if rollback_queued {
367 BootOrder::Rollback
368 } else {
369 BootOrder::Default
370 };
371 tracing::debug!("Rollback queued={rollback_queued:?}");
372 let other = {
373 related_deployments.extend(other_deployments);
374 related_deployments
375 };
376 let deployments = Deployments {
377 staged,
378 rollback,
379 other,
380 };
381
382 let staged = deployments
383 .staged
384 .as_ref()
385 .map(|d| boot_entry_from_deployment(sysroot, d))
386 .transpose()
387 .context("Staged deployment")?;
388 let booted = booted_deployment
389 .as_ref()
390 .map(|d| boot_entry_from_deployment(sysroot, d))
391 .transpose()
392 .context("Booted deployment")?;
393 let rollback = deployments
394 .rollback
395 .as_ref()
396 .map(|d| boot_entry_from_deployment(sysroot, d))
397 .transpose()
398 .context("Rollback deployment")?;
399 let other_deployments = deployments
400 .other
401 .iter()
402 .map(|d| boot_entry_from_deployment(sysroot, d))
403 .collect::<Result<Vec<_>>>()
404 .context("Other deployments")?;
405 let spec = staged
406 .as_ref()
407 .or(booted.as_ref())
408 .and_then(|entry| entry.image.as_ref())
409 .map(|img| HostSpec {
410 image: Some(img.image.clone()),
411 boot_order,
412 })
413 .unwrap_or_default();
414
415 let ty = if booted
416 .as_ref()
417 .map(|b| b.image.is_some())
418 .unwrap_or_default()
419 {
420 Some(HostType::BootcHost)
422 } else {
423 None
424 };
425
426 let usr_overlay = booted_deployment
427 .as_ref()
428 .map(|d| d.unlocked())
429 .and_then(crate::spec::deployment_unlocked_state_to_usr_overlay);
430
431 let mut host = Host::new(spec);
432 host.status = HostStatus {
433 staged,
434 booted,
435 rollback,
436 other_deployments,
437 rollback_queued,
438 ty,
439 usr_overlay,
440 };
441 Ok((deployments, host))
442}
443
444pub(crate) async fn get_host() -> Result<Host> {
445 let env = crate::store::Environment::detect()?;
446 if env.needs_mount_namespace() {
447 crate::cli::prepare_for_write()?;
448 }
449
450 let Some(storage) = BootedStorage::new(env).await? else {
451 return Ok(Host::default());
453 };
454
455 let host = match storage.kind() {
456 Ok(kind) => match kind {
457 BootedStorageKind::Ostree(booted_ostree) => {
458 let (_deployments, host) = get_status(&booted_ostree)?;
459 host
460 }
461 BootedStorageKind::Composefs(booted_cfs) => {
462 crate::bootc_composefs::status::get_composefs_status(&storage, &booted_cfs).await?
463 }
464 },
465 Err(_) => {
466 Host::default()
469 }
470 };
471
472 Ok(host)
473}
474
475#[context("Status")]
477pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
478 match opts.format_version.unwrap_or_default() {
479 0 | 1 => {}
481 o => anyhow::bail!("Unsupported format version: {o}"),
482 };
483 let mut host = get_host().await?;
484
485 if opts.booted {
488 host.filter_to_slot(Slot::Booted);
489 }
490
491 let out = std::io::stdout();
495 let mut out = out.lock();
496 let legacy_opt = if opts.json {
497 OutputFormat::Json
498 } else if std::io::stdout().is_terminal() {
499 OutputFormat::HumanReadable
500 } else {
501 OutputFormat::Yaml
502 };
503 let format = opts.format.unwrap_or(legacy_opt);
504 match format {
505 OutputFormat::Json => host
506 .to_canon_json_writer(&mut out)
507 .map_err(anyhow::Error::new),
508 OutputFormat::Yaml => serde_yaml::to_writer(&mut out, &host).map_err(anyhow::Error::new),
509 OutputFormat::HumanReadable => human_readable_output(&mut out, &host, opts.verbose),
510 }
511 .context("Writing to stdout")?;
512
513 Ok(())
514}
515
516#[derive(Debug, Clone, Copy)]
517pub enum Slot {
518 Staged,
519 Booted,
520 Rollback,
521}
522
523impl std::fmt::Display for Slot {
524 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
525 let s = match self {
526 Slot::Staged => "staged",
527 Slot::Booted => "booted",
528 Slot::Rollback => "rollback",
529 };
530 f.write_str(s)
531 }
532}
533
534fn write_row_name(mut out: impl Write, s: &str, prefix_len: usize) -> Result<()> {
536 let n = prefix_len.saturating_sub(s.chars().count());
537 let mut spaces = std::io::repeat(b' ').take(n as u64);
538 std::io::copy(&mut spaces, &mut out)?;
539 write!(out, "{s}: ")?;
540 Ok(())
541}
542
543fn format_timestamp(t: &chrono::DateTime<chrono::Utc>) -> impl std::fmt::Display {
548 t.format("%Y-%m-%dT%H:%M:%SZ")
549}
550
551fn render_verbose_ostree_info(
553 mut out: impl Write,
554 ostree: &crate::spec::BootEntryOstree,
555 slot: Option<Slot>,
556 prefix_len: usize,
557) -> Result<()> {
558 write_row_name(&mut out, "StateRoot", prefix_len)?;
559 writeln!(out, "{}", ostree.stateroot)?;
560
561 write_row_name(&mut out, "Deploy serial", prefix_len)?;
563 writeln!(out, "{}", ostree.deploy_serial)?;
564
565 let is_staged = matches!(slot, Some(Slot::Staged));
567 write_row_name(&mut out, "Staged", prefix_len)?;
568 writeln!(out, "{}", if is_staged { "yes" } else { "no" })?;
569
570 Ok(())
571}
572
573fn write_soft_reboot(
575 mut out: impl Write,
576 entry: &crate::spec::BootEntry,
577 prefix_len: usize,
578) -> Result<()> {
579 write_row_name(&mut out, "Soft-reboot", prefix_len)?;
581 writeln!(
582 out,
583 "{}",
584 if entry.soft_reboot_capable {
585 "yes"
586 } else {
587 "no"
588 }
589 )?;
590
591 Ok(())
592}
593
594fn write_download_only(
596 mut out: impl Write,
597 slot: Option<Slot>,
598 entry: &crate::spec::BootEntry,
599 prefix_len: usize,
600) -> Result<()> {
601 if matches!(slot, Some(Slot::Staged)) {
603 write_row_name(&mut out, "Download-only", prefix_len)?;
604 writeln!(out, "{}", if entry.download_only { "yes" } else { "no" })?;
605 }
606 Ok(())
607}
608
609fn render_cached_update(
615 mut out: impl Write,
616 cached: &crate::spec::ImageStatus,
617 current: &crate::spec::ImageStatus,
618 prefix_len: usize,
619) -> Result<()> {
620 if cached.image_digest == current.image_digest {
621 return Ok(());
622 }
623
624 if let Some(version) = cached.version.as_deref() {
625 write_row_name(&mut out, "UpdateVersion", prefix_len)?;
626 let timestamp_str = cached
627 .timestamp
628 .as_ref()
629 .map(|t| format!(" ({})", format_timestamp(t)))
630 .unwrap_or_default();
631 writeln!(out, "{version}{timestamp_str}")?;
632 } else {
633 write_row_name(&mut out, "Update", prefix_len)?;
634 writeln!(out, "Available")?;
635 }
636 write_row_name(&mut out, "UpdateDigest", prefix_len)?;
637 writeln!(out, "{}", cached.image_digest)?;
638
639 Ok(())
640}
641
642fn human_render_slot(
644 mut out: impl Write,
645 slot: Option<Slot>,
646 entry: &crate::spec::BootEntry,
647 image: &crate::spec::ImageStatus,
648 host_status: &crate::spec::HostStatus,
649 verbose: bool,
650) -> Result<()> {
651 let transport = &image.image.transport;
652 let imagename = &image.image.image;
653 let imageref = if transport == "registry" {
655 Cow::Borrowed(imagename)
656 } else {
657 Cow::Owned(format!("{transport}:{imagename}"))
659 };
660 let prefix = match slot {
661 Some(Slot::Staged) => " Staged image".into(),
662 Some(Slot::Booted) => format!("{} Booted image", crate::glyph::Glyph::BlackCircle),
663 Some(Slot::Rollback) => " Rollback image".into(),
664 _ => " Other image".into(),
665 };
666 let prefix_len = prefix.chars().count();
667 writeln!(out, "{prefix}: {imageref}")?;
668
669 let arch = image.architecture.as_str();
670 write_row_name(&mut out, "Digest", prefix_len)?;
671 let digest = &image.image_digest;
672 writeln!(out, "{digest} ({arch})")?;
673
674 if let Some(composefs) = &entry.composefs {
676 write_row_name(&mut out, "Verity", prefix_len)?;
677 writeln!(out, "{}", composefs.verity)?;
678 }
679
680 let timestamp = image.timestamp.as_ref().map(format_timestamp);
681 if let Some(version) = image.version.as_deref() {
683 write_row_name(&mut out, "Version", prefix_len)?;
684 if let Some(timestamp) = timestamp {
685 writeln!(out, "{version} ({timestamp})")?;
686 } else {
687 writeln!(out, "{version}")?;
688 }
689 } else if let Some(timestamp) = timestamp {
690 write_row_name(&mut out, "Timestamp", prefix_len)?;
692 writeln!(out, "{timestamp}")?;
693 }
694
695 if entry.pinned {
696 write_row_name(&mut out, "Pinned", prefix_len)?;
697 writeln!(out, "yes")?;
698 }
699
700 if let Some(cached) = &entry.cached_update {
702 render_cached_update(&mut out, cached, image, prefix_len)?;
703 }
704
705 write_usr_overlay(&mut out, slot, host_status, prefix_len)?;
707
708 if verbose {
709 if let Some(ostree) = &entry.ostree {
711 render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
712
713 write_row_name(&mut out, "Commit", prefix_len)?;
715 writeln!(out, "{}", ostree.checksum)?;
716 }
717
718 if let Some(signature) = &image.image.signature {
720 write_row_name(&mut out, "Signature", prefix_len)?;
721 match signature {
722 crate::spec::ImageSignature::OstreeRemote(remote) => {
723 writeln!(out, "ostree-remote:{remote}")?;
724 }
725 crate::spec::ImageSignature::ContainerPolicy => {
726 writeln!(out, "container-policy")?;
727 }
728 crate::spec::ImageSignature::Insecure => {
729 writeln!(out, "insecure")?;
730 }
731 }
732 }
733
734 write_soft_reboot(&mut out, entry, prefix_len)?;
736
737 write_download_only(&mut out, slot, entry, prefix_len)?;
739 }
740
741 tracing::debug!("pinned={}", entry.pinned);
742
743 Ok(())
744}
745
746fn write_usr_overlay(
748 mut out: impl Write,
749 slot: Option<Slot>,
750 host_status: &crate::spec::HostStatus,
751 prefix_len: usize,
752) -> Result<()> {
753 if matches!(slot, Some(Slot::Booted)) {
755 if let Some(ref overlay) = host_status.usr_overlay {
757 write_row_name(&mut out, "/usr overlay", prefix_len)?;
758 writeln!(out, "{}", overlay)?;
759 }
760 }
761 Ok(())
762}
763
764fn human_render_slot_ostree(
766 mut out: impl Write,
767 slot: Option<Slot>,
768 entry: &crate::spec::BootEntry,
769 ostree_commit: &str,
770 host_status: &crate::spec::HostStatus,
771 verbose: bool,
772) -> Result<()> {
773 let prefix = match slot {
775 Some(Slot::Staged) => " Staged ostree".into(),
776 Some(Slot::Booted) => format!("{} Booted ostree", crate::glyph::Glyph::BlackCircle),
777 Some(Slot::Rollback) => " Rollback ostree".into(),
778 _ => " Other ostree".into(),
779 };
780 let prefix_len = prefix.len();
781 writeln!(out, "{prefix}")?;
782 write_row_name(&mut out, "Commit", prefix_len)?;
783 writeln!(out, "{ostree_commit}")?;
784
785 if entry.pinned {
786 write_row_name(&mut out, "Pinned", prefix_len)?;
787 writeln!(out, "yes")?;
788 }
789
790 write_usr_overlay(&mut out, slot, host_status, prefix_len)?;
792
793 if verbose {
794 if let Some(ostree) = &entry.ostree {
796 render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
797 }
798
799 write_soft_reboot(&mut out, entry, prefix_len)?;
801
802 write_download_only(&mut out, slot, entry, prefix_len)?;
804 }
805
806 tracing::debug!("pinned={}", entry.pinned);
807 Ok(())
808}
809
810fn human_render_slot_composefs(
812 mut out: impl Write,
813 slot: Slot,
814 entry: &crate::spec::BootEntry,
815 erofs_verity: &str,
816) -> Result<()> {
817 let prefix = match slot {
819 Slot::Staged => " Staged composefs".into(),
820 Slot::Booted => format!("{} Booted composefs", crate::glyph::Glyph::BlackCircle),
821 Slot::Rollback => " Rollback composefs".into(),
822 };
823 let prefix_len = prefix.len();
824 writeln!(out, "{prefix}")?;
825 write_row_name(&mut out, "Commit", prefix_len)?;
826 writeln!(out, "{erofs_verity}")?;
827 tracing::debug!("pinned={}", entry.pinned);
828 Ok(())
829}
830
831fn human_readable_output_booted(mut out: impl Write, host: &Host, verbose: bool) -> Result<()> {
832 let mut first = true;
833 for (slot_name, status) in [
834 (Slot::Staged, &host.status.staged),
835 (Slot::Booted, &host.status.booted),
836 (Slot::Rollback, &host.status.rollback),
837 ] {
838 if let Some(host_status) = status {
839 if first {
840 first = false;
841 } else {
842 writeln!(out)?;
843 }
844
845 if let Some(image) = &host_status.image {
846 human_render_slot(
847 &mut out,
848 Some(slot_name),
849 host_status,
850 image,
851 &host.status,
852 verbose,
853 )?;
854 } else if let Some(ostree) = host_status.ostree.as_ref() {
855 human_render_slot_ostree(
856 &mut out,
857 Some(slot_name),
858 host_status,
859 &ostree.checksum,
860 &host.status,
861 verbose,
862 )?;
863 } else if let Some(composefs) = &host_status.composefs {
864 human_render_slot_composefs(&mut out, slot_name, host_status, &composefs.verity)?;
865 } else {
866 writeln!(out, "Current {slot_name} state is unknown")?;
867 }
868 }
869 }
870
871 if !host.status.other_deployments.is_empty() {
872 for entry in &host.status.other_deployments {
873 writeln!(out)?;
874
875 if let Some(image) = &entry.image {
876 human_render_slot(&mut out, None, entry, image, &host.status, verbose)?;
877 } else if let Some(ostree) = entry.ostree.as_ref() {
878 human_render_slot_ostree(
879 &mut out,
880 None,
881 entry,
882 &ostree.checksum,
883 &host.status,
884 verbose,
885 )?;
886 }
887 }
888 }
889
890 Ok(())
891}
892
893fn human_readable_output(mut out: impl Write, host: &Host, verbose: bool) -> Result<()> {
895 if host.status.booted.is_some() {
896 human_readable_output_booted(out, host, verbose)?;
897 } else {
898 writeln!(out, "System is not deployed via bootc.")?;
899 }
900 Ok(())
901}
902
903fn container_inspect_print_human(
905 inspect: &crate::spec::ContainerInspect,
906 mut out: impl Write,
907) -> Result<()> {
908 let mut rows: Vec<(&str, String)> = Vec::new();
910
911 if let Some(kernel) = &inspect.kernel {
912 rows.push(("Kernel", kernel.version.clone()));
913 let kernel_type = if kernel.unified { "UKI" } else { "vmlinuz" };
914 rows.push(("Type", kernel_type.to_string()));
915 } else {
916 rows.push(("Kernel", "<none>".to_string()));
917 }
918
919 let kargs = if inspect.kargs.is_empty() {
920 "<none>".to_string()
921 } else {
922 inspect.kargs.join(" ")
923 };
924 rows.push(("Kargs", kargs));
925
926 let max_label_len = rows
928 .iter()
929 .map(|(label, _)| label.width())
930 .max()
931 .unwrap_or(0);
932
933 for (label, value) in rows {
934 write_row_name(&mut out, label, max_label_len)?;
935 writeln!(out, "{value}")?;
936 }
937
938 Ok(())
939}
940
941pub(crate) fn container_inspect(
943 rootfs: &camino::Utf8Path,
944 json: bool,
945 format: Option<OutputFormat>,
946) -> Result<()> {
947 let root = cap_std_ext::cap_std::fs::Dir::open_ambient_dir(
948 rootfs,
949 cap_std_ext::cap_std::ambient_authority(),
950 )?;
951 let kargs = crate::bootc_kargs::get_kargs_in_root(&root, std::env::consts::ARCH)?;
952 let kargs: Vec<String> = kargs.iter_str().map(|s| s.to_owned()).collect();
953 let kernel = crate::kernel::find_kernel(&root)?.map(Into::into);
954 let inspect = crate::spec::ContainerInspect { kargs, kernel };
955
956 let format = format.unwrap_or(if json {
958 OutputFormat::Json
959 } else {
960 OutputFormat::HumanReadable
961 });
962
963 let mut out = std::io::stdout().lock();
964 match format {
965 OutputFormat::Json => {
966 serde_json::to_writer_pretty(&mut out, &inspect)?;
967 }
968 OutputFormat::Yaml => {
969 serde_yaml::to_writer(&mut out, &inspect)?;
970 }
971 OutputFormat::HumanReadable => {
972 container_inspect_print_human(&inspect, &mut out)?;
973 }
974 }
975 Ok(())
976}
977
978#[cfg(test)]
979mod tests {
980 use super::*;
981
982 #[test]
983 fn test_format_timestamp() {
984 use chrono::TimeZone;
985 let cases = [
986 (
988 chrono::Utc.with_ymd_and_hms(2024, 8, 7, 12, 0, 0).unwrap(),
989 "2024-08-07T12:00:00Z",
990 ),
991 (
993 chrono::Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 0).unwrap(),
994 "2023-01-01T00:00:00Z",
995 ),
996 (
998 chrono::Utc
999 .with_ymd_and_hms(2025, 12, 31, 23, 59, 59)
1000 .unwrap(),
1001 "2025-12-31T23:59:59Z",
1002 ),
1003 (
1005 chrono::Utc
1006 .with_ymd_and_hms(2024, 6, 15, 10, 30, 45)
1007 .unwrap()
1008 + chrono::Duration::nanoseconds(123_456_789),
1009 "2024-06-15T10:30:45Z",
1010 ),
1011 ];
1012 for (input, expected) in cases {
1013 let result = format_timestamp(&input).to_string();
1014 assert_eq!(result, expected, "Failed for input {input:?}");
1015 }
1016 }
1017
1018 fn human_status_from_spec_fixture(spec_fixture: &str) -> Result<String> {
1019 let host: Host = serde_yaml::from_str(spec_fixture).unwrap();
1020 let mut w = Vec::new();
1021 human_readable_output(&mut w, &host, false).unwrap();
1022 let w = String::from_utf8(w).unwrap();
1023 Ok(w)
1024 }
1025
1026 fn human_status_from_spec_fixture_verbose(spec_fixture: &str) -> Result<String> {
1029 let host: Host = serde_yaml::from_str(spec_fixture).unwrap();
1030 let mut w = Vec::new();
1031 human_readable_output(&mut w, &host, true).unwrap();
1032 let w = String::from_utf8(w).unwrap();
1033 Ok(w)
1034 }
1035
1036 #[test]
1037 fn test_human_readable_base_spec() {
1038 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-staged-booted.yaml"))
1040 .expect("No spec found");
1041 let expected = indoc::indoc! { r"
1042 Staged image: quay.io/example/someimage:latest
1043 Digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566 (arm64)
1044 Version: nightly (2023-10-14T19:22:15Z)
1045
1046 ● Booted image: quay.io/example/someimage:latest
1047 Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1048 Version: nightly (2023-09-30T19:22:16Z)
1049 "};
1050 similar_asserts::assert_eq!(w, expected);
1051 }
1052
1053 #[test]
1054 fn test_human_readable_rfe_spec() {
1055 let w = human_status_from_spec_fixture(include_str!(
1057 "fixtures/spec-rfe-ostree-deployment.yaml"
1058 ))
1059 .expect("No spec found");
1060 let expected = indoc::indoc! { r"
1061 Staged ostree
1062 Commit: 1c24260fdd1be20f72a4a97a75c582834ee3431fbb0fa8e4f482bb219d633a45
1063
1064 ● Booted ostree
1065 Commit: f9fa3a553ceaaaf30cf85bfe7eed46a822f7b8fd7e14c1e3389cbc3f6d27f791
1066 "};
1067 similar_asserts::assert_eq!(w, expected);
1068 }
1069
1070 #[test]
1071 fn test_human_readable_staged_spec() {
1072 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-ostree-to-bootc.yaml"))
1074 .expect("No spec found");
1075 let expected = indoc::indoc! { r"
1076 Staged image: quay.io/centos-bootc/centos-bootc:stream9
1077 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (s390x)
1078 Version: stream9.20240807.0
1079
1080 ● Booted ostree
1081 Commit: f9fa3a553ceaaaf30cf85bfe7eed46a822f7b8fd7e14c1e3389cbc3f6d27f791
1082 "};
1083 similar_asserts::assert_eq!(w, expected);
1084 }
1085
1086 #[test]
1087 fn test_human_readable_booted_spec() {
1088 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-only-booted.yaml"))
1090 .expect("No spec found");
1091 let expected = indoc::indoc! { r"
1092 ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1093 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1094 Version: stream9.20240807.0
1095 "};
1096 similar_asserts::assert_eq!(w, expected);
1097 }
1098
1099 #[test]
1100 fn test_human_readable_staged_rollback_spec() {
1101 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-staged-rollback.yaml"))
1103 .expect("No spec found");
1104 let expected = "System is not deployed via bootc.\n";
1105 similar_asserts::assert_eq!(w, expected);
1106 }
1107
1108 #[test]
1109 fn test_via_oci() {
1110 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-via-local-oci.yaml"))
1111 .unwrap();
1112 let expected = indoc::indoc! { r"
1113 ● Booted image: oci:/var/mnt/osupdate
1114 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (amd64)
1115 Version: stream9.20240807.0
1116 "};
1117 similar_asserts::assert_eq!(w, expected);
1118 }
1119
1120 #[test]
1121 fn test_convert_signatures() {
1122 use std::str::FromStr;
1123 let ir_unverified = &OstreeImageReference::from_str(
1124 "ostree-unverified-registry:quay.io/someexample/foo:latest",
1125 )
1126 .unwrap();
1127 let ir_ostree = &OstreeImageReference::from_str(
1128 "ostree-remote-registry:fedora:quay.io/fedora/fedora-coreos:stable",
1129 )
1130 .unwrap();
1131
1132 let ir = ImageReference::from(ir_unverified.clone());
1133 assert_eq!(ir.image, "quay.io/someexample/foo:latest");
1134 assert_eq!(ir.signature, None);
1135
1136 let ir = ImageReference::from(ir_ostree.clone());
1137 assert_eq!(ir.image, "quay.io/fedora/fedora-coreos:stable");
1138 assert_eq!(
1139 ir.signature,
1140 Some(ImageSignature::OstreeRemote("fedora".into()))
1141 );
1142 }
1143
1144 #[test]
1145 fn test_human_readable_booted_pinned_spec() {
1146 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-booted-pinned.yaml"))
1148 .expect("No spec found");
1149 let expected = indoc::indoc! { r"
1150 ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1151 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1152 Version: stream9.20240807.0
1153 Pinned: yes
1154
1155 Other image: quay.io/centos-bootc/centos-bootc:stream9
1156 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b37 (arm64)
1157 Version: stream9.20240807.0
1158 Pinned: yes
1159 "};
1160 similar_asserts::assert_eq!(w, expected);
1161 }
1162
1163 #[test]
1164 fn test_human_readable_verbose_spec() {
1165 let w =
1167 human_status_from_spec_fixture_verbose(include_str!("fixtures/spec-only-booted.yaml"))
1168 .expect("No spec found");
1169
1170 assert!(w.contains("StateRoot:"));
1172 assert!(w.contains("Deploy serial:"));
1173 assert!(w.contains("Staged:"));
1174 assert!(w.contains("Commit:"));
1175 assert!(w.contains("Soft-reboot:"));
1176 }
1177
1178 #[test]
1179 fn test_human_readable_staged_download_only() {
1180 let w =
1183 human_status_from_spec_fixture(include_str!("fixtures/spec-staged-download-only.yaml"))
1184 .expect("No spec found");
1185 let expected = indoc::indoc! { r"
1186 Staged image: quay.io/example/someimage:latest
1187 Digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566 (arm64)
1188 Version: nightly (2023-10-14T19:22:15Z)
1189
1190 ● Booted image: quay.io/example/someimage:latest
1191 Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1192 Version: nightly (2023-09-30T19:22:16Z)
1193 "};
1194 similar_asserts::assert_eq!(w, expected);
1195 }
1196
1197 #[test]
1198 fn test_human_readable_staged_download_only_verbose() {
1199 let w = human_status_from_spec_fixture_verbose(include_str!(
1201 "fixtures/spec-staged-download-only.yaml"
1202 ))
1203 .expect("No spec found");
1204
1205 assert!(w.contains("Download-only: yes"));
1207 }
1208
1209 #[test]
1210 fn test_human_readable_staged_not_download_only_verbose() {
1211 let w = human_status_from_spec_fixture_verbose(include_str!(
1213 "fixtures/spec-staged-booted.yaml"
1214 ))
1215 .expect("No spec found");
1216
1217 assert!(w.contains("Download-only: no"));
1219 }
1220
1221 #[test]
1222 fn test_container_inspect_human_readable() {
1223 let inspect = crate::spec::ContainerInspect {
1224 kargs: vec!["console=ttyS0".into(), "quiet".into()],
1225 kernel: Some(crate::kernel::Kernel {
1226 version: "6.12.0-100.fc41.x86_64".into(),
1227 unified: false,
1228 }),
1229 };
1230 let mut w = Vec::new();
1231 container_inspect_print_human(&inspect, &mut w).unwrap();
1232 let output = String::from_utf8(w).unwrap();
1233 let expected = indoc::indoc! { r"
1234 Kernel: 6.12.0-100.fc41.x86_64
1235 Type: vmlinuz
1236 Kargs: console=ttyS0 quiet
1237 "};
1238 similar_asserts::assert_eq!(output, expected);
1239 }
1240
1241 #[test]
1242 fn test_container_inspect_human_readable_uki() {
1243 let inspect = crate::spec::ContainerInspect {
1244 kargs: vec![],
1245 kernel: Some(crate::kernel::Kernel {
1246 version: "6.12.0-100.fc41.x86_64".into(),
1247 unified: true,
1248 }),
1249 };
1250 let mut w = Vec::new();
1251 container_inspect_print_human(&inspect, &mut w).unwrap();
1252 let output = String::from_utf8(w).unwrap();
1253 let expected = indoc::indoc! { r"
1254 Kernel: 6.12.0-100.fc41.x86_64
1255 Type: UKI
1256 Kargs: <none>
1257 "};
1258 similar_asserts::assert_eq!(output, expected);
1259 }
1260
1261 #[test]
1262 fn test_container_inspect_human_readable_no_kernel() {
1263 let inspect = crate::spec::ContainerInspect {
1264 kargs: vec!["console=ttyS0".into()],
1265 kernel: None,
1266 };
1267 let mut w = Vec::new();
1268 container_inspect_print_human(&inspect, &mut w).unwrap();
1269 let output = String::from_utf8(w).unwrap();
1270 let expected = indoc::indoc! { r"
1271 Kernel: <none>
1272 Kargs: console=ttyS0
1273 "};
1274 similar_asserts::assert_eq!(output, expected);
1275 }
1276
1277 #[test]
1278 fn test_human_readable_booted_usroverlay() {
1279 let w =
1280 human_status_from_spec_fixture(include_str!("fixtures/spec-booted-usroverlay.yaml"))
1281 .unwrap();
1282 let expected = indoc::indoc! { r"
1283 ● Booted image: quay.io/example/someimage:latest
1284 Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1285 Version: nightly (2023-09-30T19:22:16Z)
1286 /usr overlay: transient, read/write
1287 "};
1288 similar_asserts::assert_eq!(w, expected);
1289 }
1290
1291 #[test]
1292 fn test_human_readable_booted_with_cached_update() {
1293 let w =
1296 human_status_from_spec_fixture(include_str!("fixtures/spec-booted-with-update.yaml"))
1297 .expect("No spec found");
1298 let expected = indoc::indoc! { r"
1299 ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1300 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1301 Version: stream9.20240807.0 (2024-08-07T12:00:00Z)
1302 UpdateVersion: stream9.20240901.0 (2024-09-01T12:00:00Z)
1303 UpdateDigest: sha256:a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0
1304 "};
1305 similar_asserts::assert_eq!(w, expected);
1306 }
1307
1308 #[test]
1309 fn test_human_readable_cached_update_same_digest_hidden() {
1310 let w = human_status_from_spec_fixture(include_str!(
1313 "fixtures/spec-booted-update-same-digest.yaml"
1314 ))
1315 .expect("No spec found");
1316 assert!(
1317 !w.contains("UpdateVersion:"),
1318 "Should not show update version when digest matches current"
1319 );
1320 assert!(
1321 !w.contains("UpdateDigest:"),
1322 "Should not show update digest when digest matches current"
1323 );
1324 }
1325
1326 #[test]
1327 fn test_human_readable_cached_update_no_version() {
1328 let w = human_status_from_spec_fixture(include_str!(
1330 "fixtures/spec-booted-with-update-no-version.yaml"
1331 ))
1332 .expect("No spec found");
1333 let expected = indoc::indoc! { r"
1334 ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1335 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1336 Version: stream9.20240807.0
1337 Update: Available
1338 UpdateDigest: sha256:b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1
1339 "};
1340 similar_asserts::assert_eq!(w, expected);
1341 }
1342}