bootc_lib/bootc_composefs/
digest.rs1use std::fs::File;
4use std::io::BufWriter;
5use std::os::fd::OwnedFd;
6use std::sync::Arc;
7
8use anyhow::{Context, Result};
9use camino::Utf8Path;
10use cap_std_ext::cap_std;
11use cap_std_ext::cap_std::fs::Dir;
12use composefs::dumpfile;
13use composefs::fsverity::{Algorithm, FsVerityHashValue};
14use composefs_boot::BootOps as _;
15use composefs_ctl::composefs;
16use composefs_ctl::composefs_boot;
17use tempfile::TempDir;
18
19use crate::store::ComposefsRepository;
20
21#[fn_error_context::context("Creating new temp composefs repo")]
26pub(crate) fn new_temp_composefs_repo() -> Result<(TempDir, Arc<ComposefsRepository>)> {
27 let td_guard = tempfile::tempdir_in("/var/tmp")?;
28 let td_path = td_guard.path();
29 let td_dir = Dir::open_ambient_dir(td_path, cap_std::ambient_authority())?;
30
31 td_dir.create_dir("repo")?;
32 let repo_dir = td_dir.open_dir("repo")?;
33 let (mut repo, _created) =
34 ComposefsRepository::init_path(&repo_dir, ".", Algorithm::SHA512, false)
35 .context("Init cfs repo")?;
36 repo.set_insecure();
38 Ok((td_guard, Arc::new(repo)))
39}
40
41#[fn_error_context::context("Computing composefs digest")]
59pub(crate) async fn compute_composefs_digest(
60 path: &Utf8Path,
61 write_dumpfile_to: Option<&Utf8Path>,
62) -> Result<String> {
63 if path.as_str() == "/" {
64 anyhow::bail!("Cannot operate on active root filesystem; mount separate target instead");
65 }
66
67 let (_td_guard, repo) = new_temp_composefs_repo()?;
68
69 let dirfd: OwnedFd = rustix::fs::open(
71 path.as_std_path(),
72 rustix::fs::OFlags::RDONLY | rustix::fs::OFlags::DIRECTORY | rustix::fs::OFlags::CLOEXEC,
73 rustix::fs::Mode::empty(),
74 )
75 .with_context(|| format!("Opening {path}"))?;
76 let mut fs = composefs::fs::read_container_root(
77 dirfd,
78 std::path::PathBuf::from("."),
79 Some(repo.clone()),
80 )
81 .await
82 .context("Reading container root")?;
83 fs.transform_for_boot(&repo).context("Preparing for boot")?;
84 let id = fs.compute_image_id();
85 let digest = id.to_hex();
86
87 if let Some(dumpfile_path) = write_dumpfile_to {
88 let mut w = File::create(dumpfile_path)
89 .with_context(|| format!("Opening {dumpfile_path}"))
90 .map(BufWriter::new)?;
91 dumpfile::write_dumpfile(&mut w, &fs).context("Writing dumpfile")?;
92 }
93
94 Ok(digest)
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use std::fs::{self, Permissions};
101 use std::os::unix::fs::PermissionsExt;
102
103 fn create_test_filesystem(root: &std::path::Path) -> Result<()> {
105 fs::create_dir_all(root.join("boot"))?;
107 fs::create_dir_all(root.join("sysroot"))?;
108
109 let usr_bin = root.join("usr/bin");
111 fs::create_dir_all(&usr_bin)?;
112
113 let hello_path = usr_bin.join("hello");
115 fs::write(&hello_path, "test\n")?;
116 fs::set_permissions(&hello_path, Permissions::from_mode(0o755))?;
117
118 let etc = root.join("etc");
120 fs::create_dir_all(&etc)?;
121
122 let config_path = etc.join("config");
124 fs::write(&config_path, "test\n")?;
125 fs::set_permissions(&config_path, Permissions::from_mode(0o644))?;
126
127 Ok(())
128 }
129
130 #[tokio::test(flavor = "multi_thread")]
131 async fn test_compute_composefs_digest() {
132 let td = tempfile::tempdir().unwrap();
134 create_test_filesystem(td.path()).unwrap();
135
136 let path = Utf8Path::from_path(td.path()).unwrap();
138 let digest = compute_composefs_digest(path, None).await.unwrap();
139
140 assert_eq!(
142 digest.len(),
143 128,
144 "Expected 512-bit hex digest, got length {}",
145 digest.len()
146 );
147 assert!(
148 digest.chars().all(|c| c.is_ascii_hexdigit()),
149 "Digest contains non-hex characters: {digest}"
150 );
151
152 let digest2 = compute_composefs_digest(path, None).await.unwrap();
154 assert_eq!(
155 digest, digest2,
156 "Digest should be consistent across multiple computations"
157 );
158 }
159
160 #[tokio::test]
161 async fn test_compute_composefs_digest_rejects_root() {
162 let result = compute_composefs_digest(Utf8Path::new("/"), None).await;
163 assert!(result.is_err());
164 let err = result.unwrap_err();
165 let found = err.chain().any(|e| {
166 e.to_string()
167 .contains("Cannot operate on active root filesystem")
168 });
169
170 assert!(found, "Unexpected error chain: {err:?}");
171 }
172}