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 mut host = Host::new(spec);
427 host.status = HostStatus {
428 staged,
429 booted,
430 rollback,
431 other_deployments,
432 rollback_queued,
433 ty,
434 };
435 Ok((deployments, host))
436}
437
438pub(crate) async fn get_host() -> Result<Host> {
439 let env = crate::store::Environment::detect()?;
440 if env.needs_mount_namespace() {
441 crate::cli::prepare_for_write()?;
442 }
443
444 let Some(storage) = BootedStorage::new(env).await? else {
445 return Ok(Host::default());
447 };
448
449 let host = match storage.kind() {
450 Ok(kind) => match kind {
451 BootedStorageKind::Ostree(booted_ostree) => {
452 let (_deployments, host) = get_status(&booted_ostree)?;
453 host
454 }
455 BootedStorageKind::Composefs(booted_cfs) => {
456 crate::bootc_composefs::status::get_composefs_status(&storage, &booted_cfs).await?
457 }
458 },
459 Err(_) => {
460 Host::default()
463 }
464 };
465
466 Ok(host)
467}
468
469#[context("Status")]
471pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
472 match opts.format_version.unwrap_or_default() {
473 0 | 1 => {}
475 o => anyhow::bail!("Unsupported format version: {o}"),
476 };
477 let mut host = get_host().await?;
478
479 if opts.booted {
482 host.filter_to_slot(Slot::Booted);
483 }
484
485 let out = std::io::stdout();
489 let mut out = out.lock();
490 let legacy_opt = if opts.json {
491 OutputFormat::Json
492 } else if std::io::stdout().is_terminal() {
493 OutputFormat::HumanReadable
494 } else {
495 OutputFormat::Yaml
496 };
497 let format = opts.format.unwrap_or(legacy_opt);
498 match format {
499 OutputFormat::Json => host
500 .to_canon_json_writer(&mut out)
501 .map_err(anyhow::Error::new),
502 OutputFormat::Yaml => serde_yaml::to_writer(&mut out, &host).map_err(anyhow::Error::new),
503 OutputFormat::HumanReadable => human_readable_output(&mut out, &host, opts.verbose),
504 }
505 .context("Writing to stdout")?;
506
507 Ok(())
508}
509
510#[derive(Debug, Clone, Copy)]
511pub enum Slot {
512 Staged,
513 Booted,
514 Rollback,
515}
516
517impl std::fmt::Display for Slot {
518 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
519 let s = match self {
520 Slot::Staged => "staged",
521 Slot::Booted => "booted",
522 Slot::Rollback => "rollback",
523 };
524 f.write_str(s)
525 }
526}
527
528fn write_row_name(mut out: impl Write, s: &str, prefix_len: usize) -> Result<()> {
530 let n = prefix_len.saturating_sub(s.chars().count());
531 let mut spaces = std::io::repeat(b' ').take(n as u64);
532 std::io::copy(&mut spaces, &mut out)?;
533 write!(out, "{s}: ")?;
534 Ok(())
535}
536
537fn render_verbose_ostree_info(
539 mut out: impl Write,
540 ostree: &crate::spec::BootEntryOstree,
541 slot: Option<Slot>,
542 prefix_len: usize,
543) -> Result<()> {
544 write_row_name(&mut out, "StateRoot", prefix_len)?;
545 writeln!(out, "{}", ostree.stateroot)?;
546
547 write_row_name(&mut out, "Deploy serial", prefix_len)?;
549 writeln!(out, "{}", ostree.deploy_serial)?;
550
551 let is_staged = matches!(slot, Some(Slot::Staged));
553 write_row_name(&mut out, "Staged", prefix_len)?;
554 writeln!(out, "{}", if is_staged { "yes" } else { "no" })?;
555
556 Ok(())
557}
558
559fn write_soft_reboot(
561 mut out: impl Write,
562 entry: &crate::spec::BootEntry,
563 prefix_len: usize,
564) -> Result<()> {
565 write_row_name(&mut out, "Soft-reboot", prefix_len)?;
567 writeln!(
568 out,
569 "{}",
570 if entry.soft_reboot_capable {
571 "yes"
572 } else {
573 "no"
574 }
575 )?;
576
577 Ok(())
578}
579
580fn write_download_only(
582 mut out: impl Write,
583 slot: Option<Slot>,
584 entry: &crate::spec::BootEntry,
585 prefix_len: usize,
586) -> Result<()> {
587 if matches!(slot, Some(Slot::Staged)) {
589 write_row_name(&mut out, "Download-only", prefix_len)?;
590 writeln!(out, "{}", if entry.download_only { "yes" } else { "no" })?;
591 }
592 Ok(())
593}
594
595fn human_render_slot(
597 mut out: impl Write,
598 slot: Option<Slot>,
599 entry: &crate::spec::BootEntry,
600 image: &crate::spec::ImageStatus,
601 verbose: bool,
602) -> Result<()> {
603 let transport = &image.image.transport;
604 let imagename = &image.image.image;
605 let imageref = if transport == "registry" {
607 Cow::Borrowed(imagename)
608 } else {
609 Cow::Owned(format!("{transport}:{imagename}"))
611 };
612 let prefix = match slot {
613 Some(Slot::Staged) => " Staged image".into(),
614 Some(Slot::Booted) => format!("{} Booted image", crate::glyph::Glyph::BlackCircle),
615 Some(Slot::Rollback) => " Rollback image".into(),
616 _ => " Other image".into(),
617 };
618 let prefix_len = prefix.chars().count();
619 writeln!(out, "{prefix}: {imageref}")?;
620
621 let arch = image.architecture.as_str();
622 write_row_name(&mut out, "Digest", prefix_len)?;
623 let digest = &image.image_digest;
624 writeln!(out, "{digest} ({arch})")?;
625
626 if let Some(composefs) = &entry.composefs {
628 write_row_name(&mut out, "Verity", prefix_len)?;
629 writeln!(out, "{}", composefs.verity)?;
630 }
631
632 let timestamp = image
635 .timestamp
636 .as_ref()
637 .map(|t| t.to_utc().format("%Y-%m-%dT%H:%M:%SZ"));
639 if let Some(version) = image.version.as_deref() {
641 write_row_name(&mut out, "Version", prefix_len)?;
642 if let Some(timestamp) = timestamp {
643 writeln!(out, "{version} ({timestamp})")?;
644 } else {
645 writeln!(out, "{version}")?;
646 }
647 } else if let Some(timestamp) = timestamp {
648 write_row_name(&mut out, "Timestamp", prefix_len)?;
650 writeln!(out, "{timestamp}")?;
651 }
652
653 if entry.pinned {
654 write_row_name(&mut out, "Pinned", prefix_len)?;
655 writeln!(out, "yes")?;
656 }
657
658 if verbose {
659 if let Some(ostree) = &entry.ostree {
661 render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
662
663 write_row_name(&mut out, "Commit", prefix_len)?;
665 writeln!(out, "{}", ostree.checksum)?;
666 }
667
668 if let Some(signature) = &image.image.signature {
670 write_row_name(&mut out, "Signature", prefix_len)?;
671 match signature {
672 crate::spec::ImageSignature::OstreeRemote(remote) => {
673 writeln!(out, "ostree-remote:{remote}")?;
674 }
675 crate::spec::ImageSignature::ContainerPolicy => {
676 writeln!(out, "container-policy")?;
677 }
678 crate::spec::ImageSignature::Insecure => {
679 writeln!(out, "insecure")?;
680 }
681 }
682 }
683
684 write_soft_reboot(&mut out, entry, prefix_len)?;
686
687 write_download_only(&mut out, slot, entry, prefix_len)?;
689 }
690
691 tracing::debug!("pinned={}", entry.pinned);
692
693 Ok(())
694}
695
696fn human_render_slot_ostree(
698 mut out: impl Write,
699 slot: Option<Slot>,
700 entry: &crate::spec::BootEntry,
701 ostree_commit: &str,
702 verbose: bool,
703) -> Result<()> {
704 let prefix = match slot {
706 Some(Slot::Staged) => " Staged ostree".into(),
707 Some(Slot::Booted) => format!("{} Booted ostree", crate::glyph::Glyph::BlackCircle),
708 Some(Slot::Rollback) => " Rollback ostree".into(),
709 _ => " Other ostree".into(),
710 };
711 let prefix_len = prefix.len();
712 writeln!(out, "{prefix}")?;
713 write_row_name(&mut out, "Commit", prefix_len)?;
714 writeln!(out, "{ostree_commit}")?;
715
716 if entry.pinned {
717 write_row_name(&mut out, "Pinned", prefix_len)?;
718 writeln!(out, "yes")?;
719 }
720
721 if verbose {
722 if let Some(ostree) = &entry.ostree {
724 render_verbose_ostree_info(&mut out, ostree, slot, prefix_len)?;
725 }
726
727 write_soft_reboot(&mut out, entry, prefix_len)?;
729
730 write_download_only(&mut out, slot, entry, prefix_len)?;
732 }
733
734 tracing::debug!("pinned={}", entry.pinned);
735 Ok(())
736}
737
738fn human_render_slot_composefs(
740 mut out: impl Write,
741 slot: Slot,
742 entry: &crate::spec::BootEntry,
743 erofs_verity: &str,
744) -> Result<()> {
745 let prefix = match slot {
747 Slot::Staged => " Staged composefs".into(),
748 Slot::Booted => format!("{} Booted composefs", crate::glyph::Glyph::BlackCircle),
749 Slot::Rollback => " Rollback composefs".into(),
750 };
751 let prefix_len = prefix.len();
752 writeln!(out, "{prefix}")?;
753 write_row_name(&mut out, "Commit", prefix_len)?;
754 writeln!(out, "{erofs_verity}")?;
755 tracing::debug!("pinned={}", entry.pinned);
756 Ok(())
757}
758
759fn human_readable_output_booted(mut out: impl Write, host: &Host, verbose: bool) -> Result<()> {
760 let mut first = true;
761 for (slot_name, status) in [
762 (Slot::Staged, &host.status.staged),
763 (Slot::Booted, &host.status.booted),
764 (Slot::Rollback, &host.status.rollback),
765 ] {
766 if let Some(host_status) = status {
767 if first {
768 first = false;
769 } else {
770 writeln!(out)?;
771 }
772
773 if let Some(image) = &host_status.image {
774 human_render_slot(&mut out, Some(slot_name), host_status, image, verbose)?;
775 } else if let Some(ostree) = host_status.ostree.as_ref() {
776 human_render_slot_ostree(
777 &mut out,
778 Some(slot_name),
779 host_status,
780 &ostree.checksum,
781 verbose,
782 )?;
783 } else if let Some(composefs) = &host_status.composefs {
784 human_render_slot_composefs(&mut out, slot_name, host_status, &composefs.verity)?;
785 } else {
786 writeln!(out, "Current {slot_name} state is unknown")?;
787 }
788 }
789 }
790
791 if !host.status.other_deployments.is_empty() {
792 for entry in &host.status.other_deployments {
793 writeln!(out)?;
794
795 if let Some(image) = &entry.image {
796 human_render_slot(&mut out, None, entry, image, verbose)?;
797 } else if let Some(ostree) = entry.ostree.as_ref() {
798 human_render_slot_ostree(&mut out, None, entry, &ostree.checksum, verbose)?;
799 }
800 }
801 }
802
803 Ok(())
804}
805
806fn human_readable_output(mut out: impl Write, host: &Host, verbose: bool) -> Result<()> {
808 if host.status.booted.is_some() {
809 human_readable_output_booted(out, host, verbose)?;
810 } else {
811 writeln!(out, "System is not deployed via bootc.")?;
812 }
813 Ok(())
814}
815
816fn container_inspect_print_human(
818 inspect: &crate::spec::ContainerInspect,
819 mut out: impl Write,
820) -> Result<()> {
821 let mut rows: Vec<(&str, String)> = Vec::new();
823
824 if let Some(kernel) = &inspect.kernel {
825 rows.push(("Kernel", kernel.version.clone()));
826 let kernel_type = if kernel.unified { "UKI" } else { "vmlinuz" };
827 rows.push(("Type", kernel_type.to_string()));
828 } else {
829 rows.push(("Kernel", "<none>".to_string()));
830 }
831
832 let kargs = if inspect.kargs.is_empty() {
833 "<none>".to_string()
834 } else {
835 inspect.kargs.join(" ")
836 };
837 rows.push(("Kargs", kargs));
838
839 let max_label_len = rows
841 .iter()
842 .map(|(label, _)| label.width())
843 .max()
844 .unwrap_or(0);
845
846 for (label, value) in rows {
847 write_row_name(&mut out, label, max_label_len)?;
848 writeln!(out, "{value}")?;
849 }
850
851 Ok(())
852}
853
854pub(crate) fn container_inspect(
856 rootfs: &camino::Utf8Path,
857 json: bool,
858 format: Option<OutputFormat>,
859) -> Result<()> {
860 let root = cap_std_ext::cap_std::fs::Dir::open_ambient_dir(
861 rootfs,
862 cap_std_ext::cap_std::ambient_authority(),
863 )?;
864 let kargs = crate::bootc_kargs::get_kargs_in_root(&root, std::env::consts::ARCH)?;
865 let kargs: Vec<String> = kargs.iter_str().map(|s| s.to_owned()).collect();
866 let kernel = crate::kernel::find_kernel(&root)?;
867 let inspect = crate::spec::ContainerInspect { kargs, kernel };
868
869 let format = format.unwrap_or(if json {
871 OutputFormat::Json
872 } else {
873 OutputFormat::HumanReadable
874 });
875
876 let mut out = std::io::stdout().lock();
877 match format {
878 OutputFormat::Json => {
879 serde_json::to_writer_pretty(&mut out, &inspect)?;
880 }
881 OutputFormat::Yaml => {
882 serde_yaml::to_writer(&mut out, &inspect)?;
883 }
884 OutputFormat::HumanReadable => {
885 container_inspect_print_human(&inspect, &mut out)?;
886 }
887 }
888 Ok(())
889}
890
891#[cfg(test)]
892mod tests {
893 use super::*;
894
895 fn human_status_from_spec_fixture(spec_fixture: &str) -> Result<String> {
896 let host: Host = serde_yaml::from_str(spec_fixture).unwrap();
897 let mut w = Vec::new();
898 human_readable_output(&mut w, &host, false).unwrap();
899 let w = String::from_utf8(w).unwrap();
900 Ok(w)
901 }
902
903 fn human_status_from_spec_fixture_verbose(spec_fixture: &str) -> Result<String> {
906 let host: Host = serde_yaml::from_str(spec_fixture).unwrap();
907 let mut w = Vec::new();
908 human_readable_output(&mut w, &host, true).unwrap();
909 let w = String::from_utf8(w).unwrap();
910 Ok(w)
911 }
912
913 #[test]
914 fn test_human_readable_base_spec() {
915 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-staged-booted.yaml"))
917 .expect("No spec found");
918 let expected = indoc::indoc! { r"
919 Staged image: quay.io/example/someimage:latest
920 Digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566 (arm64)
921 Version: nightly (2023-10-14T19:22:15Z)
922
923 ● Booted image: quay.io/example/someimage:latest
924 Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
925 Version: nightly (2023-09-30T19:22:16Z)
926 "};
927 similar_asserts::assert_eq!(w, expected);
928 }
929
930 #[test]
931 fn test_human_readable_rfe_spec() {
932 let w = human_status_from_spec_fixture(include_str!(
934 "fixtures/spec-rfe-ostree-deployment.yaml"
935 ))
936 .expect("No spec found");
937 let expected = indoc::indoc! { r"
938 Staged ostree
939 Commit: 1c24260fdd1be20f72a4a97a75c582834ee3431fbb0fa8e4f482bb219d633a45
940
941 ● Booted ostree
942 Commit: f9fa3a553ceaaaf30cf85bfe7eed46a822f7b8fd7e14c1e3389cbc3f6d27f791
943 "};
944 similar_asserts::assert_eq!(w, expected);
945 }
946
947 #[test]
948 fn test_human_readable_staged_spec() {
949 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-ostree-to-bootc.yaml"))
951 .expect("No spec found");
952 let expected = indoc::indoc! { r"
953 Staged image: quay.io/centos-bootc/centos-bootc:stream9
954 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (s390x)
955 Version: stream9.20240807.0
956
957 ● Booted ostree
958 Commit: f9fa3a553ceaaaf30cf85bfe7eed46a822f7b8fd7e14c1e3389cbc3f6d27f791
959 "};
960 similar_asserts::assert_eq!(w, expected);
961 }
962
963 #[test]
964 fn test_human_readable_booted_spec() {
965 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-only-booted.yaml"))
967 .expect("No spec found");
968 let expected = indoc::indoc! { r"
969 ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
970 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
971 Version: stream9.20240807.0
972 "};
973 similar_asserts::assert_eq!(w, expected);
974 }
975
976 #[test]
977 fn test_human_readable_staged_rollback_spec() {
978 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-staged-rollback.yaml"))
980 .expect("No spec found");
981 let expected = "System is not deployed via bootc.\n";
982 similar_asserts::assert_eq!(w, expected);
983 }
984
985 #[test]
986 fn test_via_oci() {
987 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-via-local-oci.yaml"))
988 .unwrap();
989 let expected = indoc::indoc! { r"
990 ● Booted image: oci:/var/mnt/osupdate
991 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (amd64)
992 Version: stream9.20240807.0
993 "};
994 similar_asserts::assert_eq!(w, expected);
995 }
996
997 #[test]
998 fn test_convert_signatures() {
999 use std::str::FromStr;
1000 let ir_unverified = &OstreeImageReference::from_str(
1001 "ostree-unverified-registry:quay.io/someexample/foo:latest",
1002 )
1003 .unwrap();
1004 let ir_ostree = &OstreeImageReference::from_str(
1005 "ostree-remote-registry:fedora:quay.io/fedora/fedora-coreos:stable",
1006 )
1007 .unwrap();
1008
1009 let ir = ImageReference::from(ir_unverified.clone());
1010 assert_eq!(ir.image, "quay.io/someexample/foo:latest");
1011 assert_eq!(ir.signature, None);
1012
1013 let ir = ImageReference::from(ir_ostree.clone());
1014 assert_eq!(ir.image, "quay.io/fedora/fedora-coreos:stable");
1015 assert_eq!(
1016 ir.signature,
1017 Some(ImageSignature::OstreeRemote("fedora".into()))
1018 );
1019 }
1020
1021 #[test]
1022 fn test_human_readable_booted_pinned_spec() {
1023 let w = human_status_from_spec_fixture(include_str!("fixtures/spec-booted-pinned.yaml"))
1025 .expect("No spec found");
1026 let expected = indoc::indoc! { r"
1027 ● Booted image: quay.io/centos-bootc/centos-bootc:stream9
1028 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38 (arm64)
1029 Version: stream9.20240807.0
1030 Pinned: yes
1031
1032 Other image: quay.io/centos-bootc/centos-bootc:stream9
1033 Digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b37 (arm64)
1034 Version: stream9.20240807.0
1035 Pinned: yes
1036 "};
1037 similar_asserts::assert_eq!(w, expected);
1038 }
1039
1040 #[test]
1041 fn test_human_readable_verbose_spec() {
1042 let w =
1044 human_status_from_spec_fixture_verbose(include_str!("fixtures/spec-only-booted.yaml"))
1045 .expect("No spec found");
1046
1047 assert!(w.contains("StateRoot:"));
1049 assert!(w.contains("Deploy serial:"));
1050 assert!(w.contains("Staged:"));
1051 assert!(w.contains("Commit:"));
1052 assert!(w.contains("Soft-reboot:"));
1053 }
1054
1055 #[test]
1056 fn test_human_readable_staged_download_only() {
1057 let w =
1060 human_status_from_spec_fixture(include_str!("fixtures/spec-staged-download-only.yaml"))
1061 .expect("No spec found");
1062 let expected = indoc::indoc! { r"
1063 Staged image: quay.io/example/someimage:latest
1064 Digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566 (arm64)
1065 Version: nightly (2023-10-14T19:22:15Z)
1066
1067 ● Booted image: quay.io/example/someimage:latest
1068 Digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34 (arm64)
1069 Version: nightly (2023-09-30T19:22:16Z)
1070 "};
1071 similar_asserts::assert_eq!(w, expected);
1072 }
1073
1074 #[test]
1075 fn test_human_readable_staged_download_only_verbose() {
1076 let w = human_status_from_spec_fixture_verbose(include_str!(
1078 "fixtures/spec-staged-download-only.yaml"
1079 ))
1080 .expect("No spec found");
1081
1082 assert!(w.contains("Download-only: yes"));
1084 }
1085
1086 #[test]
1087 fn test_human_readable_staged_not_download_only_verbose() {
1088 let w = human_status_from_spec_fixture_verbose(include_str!(
1090 "fixtures/spec-staged-booted.yaml"
1091 ))
1092 .expect("No spec found");
1093
1094 assert!(w.contains("Download-only: no"));
1096 }
1097
1098 #[test]
1099 fn test_container_inspect_human_readable() {
1100 let inspect = crate::spec::ContainerInspect {
1101 kargs: vec!["console=ttyS0".into(), "quiet".into()],
1102 kernel: Some(crate::kernel::Kernel {
1103 version: "6.12.0-100.fc41.x86_64".into(),
1104 unified: false,
1105 }),
1106 };
1107 let mut w = Vec::new();
1108 container_inspect_print_human(&inspect, &mut w).unwrap();
1109 let output = String::from_utf8(w).unwrap();
1110 let expected = indoc::indoc! { r"
1111 Kernel: 6.12.0-100.fc41.x86_64
1112 Type: vmlinuz
1113 Kargs: console=ttyS0 quiet
1114 "};
1115 similar_asserts::assert_eq!(output, expected);
1116 }
1117
1118 #[test]
1119 fn test_container_inspect_human_readable_uki() {
1120 let inspect = crate::spec::ContainerInspect {
1121 kargs: vec![],
1122 kernel: Some(crate::kernel::Kernel {
1123 version: "6.12.0-100.fc41.x86_64".into(),
1124 unified: true,
1125 }),
1126 };
1127 let mut w = Vec::new();
1128 container_inspect_print_human(&inspect, &mut w).unwrap();
1129 let output = String::from_utf8(w).unwrap();
1130 let expected = indoc::indoc! { r"
1131 Kernel: 6.12.0-100.fc41.x86_64
1132 Type: UKI
1133 Kargs: <none>
1134 "};
1135 similar_asserts::assert_eq!(output, expected);
1136 }
1137
1138 #[test]
1139 fn test_container_inspect_human_readable_no_kernel() {
1140 let inspect = crate::spec::ContainerInspect {
1141 kargs: vec!["console=ttyS0".into()],
1142 kernel: None,
1143 };
1144 let mut w = Vec::new();
1145 container_inspect_print_human(&inspect, &mut w).unwrap();
1146 let output = String::from_utf8(w).unwrap();
1147 let expected = indoc::indoc! { r"
1148 Kernel: <none>
1149 Kargs: console=ttyS0
1150 "};
1151 similar_asserts::assert_eq!(output, expected);
1152 }
1153}