-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repair: New functionality to detect (future: fix) inodes
Initial code to detect the situation resulting from ostreedev/ostree@de6fddc
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//! System repair functionality | ||
|
||
use std::collections::{BTreeMap, BTreeSet}; | ||
|
||
use anyhow::{anyhow, Context, Result}; | ||
use cap_std::fs::Dir; | ||
use cap_tempfile::cap_std; | ||
use fn_error_context::context; | ||
use ostree::{gio, glib}; | ||
use std::os::unix::fs::MetadataExt; | ||
|
||
use crate::sysroot::SysrootLock; | ||
|
||
// Find the inode numbers for objects | ||
fn gather_inodes( | ||
prefix: &str, | ||
dir: &Dir, | ||
little_inodes: &mut BTreeMap<u32, String>, | ||
big_inodes: &mut BTreeMap<u64, String>, | ||
) -> Result<()> { | ||
for child in dir.entries()? { | ||
let child = child?; | ||
let metadata = child.metadata()?; | ||
if !(metadata.is_file() || metadata.is_symlink()) { | ||
continue; | ||
} | ||
let name = child.file_name(); | ||
let name = name | ||
.to_str() | ||
.ok_or_else(|| anyhow::anyhow!("Invalid {name:?}"))?; | ||
let object_rest = name | ||
.split_once('.') | ||
.ok_or_else(|| anyhow!("Invalid object {name}"))? | ||
.0; | ||
let checksum = format!("{prefix}{object_rest}"); | ||
let inode = metadata.ino(); | ||
if let Some(little) = u32::try_from(inode).ok() { | ||
little_inodes.insert(little, checksum); | ||
} else { | ||
big_inodes.insert(inode, checksum); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[context("Analyzing commit for derivation")] | ||
fn commit_is_derived(commit: &glib::Variant) -> Result<bool> { | ||
let commit_meta = &glib::VariantDict::new(Some(&commit.child_value(0))); | ||
if commit_meta | ||
.lookup::<String>(crate::container::store::META_MANIFEST_DIGEST)? | ||
.is_some() | ||
{ | ||
return Ok(true); | ||
} | ||
if commit_meta | ||
.lookup::<bool>("rpmostree.clientlayer")? | ||
.is_some() | ||
{ | ||
return Ok(true); | ||
} | ||
Ok(false) | ||
} | ||
|
||
#[context("Repairing inodes")] | ||
pub(crate) fn repair(sysroot: &SysrootLock, dry_run: bool) -> Result<()> { | ||
let repo = &sysroot.repo(); | ||
let repo_dir = repo.dfd_as_dir()?; | ||
let objects = repo_dir.open_dir("objects")?; | ||
|
||
println!("Attempting analysis of ostree state for files that may be incorrectly linked"); | ||
println!("For more information, see https://github.com/ostreedev/ostree/pull/2874/commits/de6fddc6adee09a93901243dc7074090828a1912"); | ||
println!(); | ||
println!("Gathering inodes..."); | ||
let mut little_inodes = BTreeMap::new(); | ||
let mut big_inodes = BTreeMap::new(); | ||
|
||
for child in objects.entries()? { | ||
let child = child?; | ||
if !child.file_type()?.is_dir() { | ||
continue; | ||
} | ||
let name = child.file_name(); | ||
if name.len() != 2 { | ||
continue; | ||
} | ||
let name = name | ||
.to_str() | ||
.ok_or_else(|| anyhow::anyhow!("Invalid {name:?}"))?; | ||
let objdir = child.open_dir()?; | ||
gather_inodes(name, &objdir, &mut little_inodes, &mut big_inodes) | ||
.with_context(|| format!("Processing {name:?}"))?; | ||
} | ||
|
||
let mut colliding_inodes = BTreeMap::new(); | ||
for (big_inum, big_inum_checksum) in big_inodes { | ||
let truncated = big_inum as u32; | ||
if let Some(small_inum_object) = little_inodes.get(&truncated) { | ||
println!( | ||
r#"collision: | ||
inode (>32 bit): {big_inum} | ||
object: {big_inum_checksum} | ||
inode (truncated): {truncated} | ||
object: {small_inum_object} | ||
"# | ||
); | ||
colliding_inodes.insert(big_inum, big_inum_checksum); | ||
} | ||
} | ||
|
||
if !colliding_inodes.is_empty() { | ||
let l = colliding_inodes.len(); | ||
eprintln!("warning: Found {l} potentially colliding objects"); | ||
} else { | ||
println!("No colliding objects found."); | ||
return Ok(()); | ||
} | ||
|
||
println!("Analyzing deployments..."); | ||
|
||
let mut potentially_corrupted_commits = BTreeSet::new(); | ||
for (_refname, digest) in repo.list_refs(None, gio::Cancellable::NONE)? { | ||
let commit = repo.load_commit(&digest)?.0; | ||
if commit_is_derived(&commit)? { | ||
eprintln!("Found potentially corrupted derived commit: {commit}"); | ||
potentially_corrupted_commits.insert(digest); | ||
} | ||
} | ||
|
||
if potentially_corrupted_commits.is_empty() { | ||
println!("No derived commits found."); | ||
} | ||
|
||
if !dry_run { | ||
anyhow::bail!("Repair mode not implemented yet"); | ||
} | ||
|
||
Ok(()) | ||
} |