Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,18 @@ RUN --mount=type=tmpfs,target=/run --mount=type=tmpfs,target=/tmp \

cd /run/hack/ && SKIP_CONFIGS="${SKIP_CONFIGS}" ./provision-fetch.sh

pkgs_to_install=()
pkgs_to_install=(fsverity-utils)

. /usr/lib/os-release

case "${ID}${ID_LIKE:-}" in
*centos*|*rhel*)
# Enable EPEL for sbsigntools and fsverity-utils
dnf -y install epel-release
;;
esac

if [[ "${seal_state}" == "sealed" ]]; then
. /usr/lib/os-release
case "${ID}${ID_LIKE:-}" in
*centos*|*rhel*)
# Enable EPEL for sbsigntools
dnf -y install epel-release
;;
esac
pkgs_to_install+=(sbsigntools)
fi

Expand Down
1 change: 1 addition & 0 deletions crates/etc-merge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ composefs-ctl = { workspace = true }
fn-error-context = { workspace = true }
owo-colors = { workspace = true }
anstream = { workspace = true }
serde = { workspace = true }

[lints]
workspace = true
156 changes: 106 additions & 50 deletions crates/etc-merge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Lib for /etc merge

#![allow(dead_code)]

use fn_error_context::context;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::io::BufReader;
Expand Down Expand Up @@ -78,14 +77,14 @@ fn stat_eq_ignore_mtime(this: &Stat, other: &Stat) -> bool {
return true;
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct UnmergablePaths {
pub path: PathBuf,
pub reason: String,
}

/// Represents the differences between two directory trees.
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Diff {
/// Paths that exist in the current /etc but not in the pristine
added: Vec<PathBuf>,
Expand Down Expand Up @@ -255,6 +254,8 @@ fn get_modifications(
pristine_leaves: &[Leaf<CustomMetadata>],
current_leaves: &[Leaf<CustomMetadata>],
new: &Directory<CustomMetadata>,
current_etc_fd: &CapStdDir,
pristine_etc_fd: &CapStdDir,
mut current_path: PathBuf,
diff: &mut Diff,
) -> anyhow::Result<()> {
Expand All @@ -281,6 +282,8 @@ fn get_modifications(
pristine_leaves,
current_leaves,
new,
current_etc_fd,
pristine_etc_fd,
current_path.clone(),
diff,
)?;
Expand Down Expand Up @@ -334,61 +337,101 @@ fn get_modifications(
}
}

Inode::Leaf(leaf_id, _) => match pristine.leaf_id(path) {
Ok(old_leaf_id) => {
let leaf = &current_leaves[leaf_id.0];
let old_leaf = &pristine_leaves[old_leaf_id.0];
Inode::Leaf(leaf_id, _) => {
match pristine.leaf_id(path) {
Ok(old_leaf_id) => {
let leaf = &current_leaves[leaf_id.0];
let old_leaf = &pristine_leaves[old_leaf_id.0];

if !stat_eq_ignore_mtime(&old_leaf.stat, &leaf.stat) {
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
current_path.pop();
continue;
}
if !stat_eq_ignore_mtime(&old_leaf.stat, &leaf.stat) {
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
current_path.pop();
continue;
}

match (&old_leaf.content, &leaf.content) {
(Regular(old_meta), Regular(current_meta)) => {
if old_meta.content_hash != current_meta.content_hash {
// File modified in some way
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
match (&old_leaf.content, &leaf.content) {
(Regular(old_meta), Regular(current_meta)) => {
let modified = match (&old_meta.verity, &current_meta.verity) {
(None, None) => {
old_meta.content_hash != current_meta.content_hash
}
// We have fsverity enabled for one file, but not the other
// This could happen but is unlikely. The file may or may not
// have been modified, compute the content hash to check
(None, Some(_)) | (Some(_), None) => {
let mut pristine_hash = old_meta.content_hash.clone();
let mut current_hash = current_meta.content_hash.clone();

let compute_hash = |mut file: cap_std::fs::File| -> anyhow::Result<String> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nesting here is getting really deep, surely we could factor some of this out into a helper function

let mut hasher = openssl::hash::Hasher::new(
openssl::hash::MessageDigest::sha256(),
)?;
std::io::copy(&mut file, &mut hasher)?;
Ok(hex::encode(hasher.finish()?))
};

if pristine_hash.len() == 0 {
let file = pristine_etc_fd.open(&current_path)
.with_context(|| anyhow::anyhow!("Opening {} in pristine etc for computing hash", current_path.display()))?;
pristine_hash = compute_hash(file)?;
}

if current_hash.len() == 0 {
let file = current_etc_fd.open(&current_path)
.with_context(|| anyhow::anyhow!("Opening {} in current etc for computing hash", current_path.display()))?;
current_hash = compute_hash(file)?;
}

pristine_hash != current_hash
}
(Some(old_verity), Some(current_verity)) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we moved this case up, then the match statement for the case above could just be a fallthrough of _

old_verity != current_verity
Comment thread
Johan-Liebert1 marked this conversation as resolved.
Resolved
}
};

if modified {
// File modified in some way
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
}
}
}

(Symlink(old_link), Symlink(current_link)) => {
if old_link != current_link {
// Symlink modified in some way
(Symlink(old_link), Symlink(current_link)) => {
if old_link != current_link {
// Symlink modified in some way
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
}
}

(Symlink(..), Regular(..)) | (Regular(..), Symlink(..)) => {
// File changed to symlink or vice-versa
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
}
}

(Symlink(..), Regular(..)) | (Regular(..), Symlink(..)) => {
// File changed to symlink or vice-versa
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
(a, b) => {
unreachable!("{a:?} modified to {b:?}")
}
}
}

(a, b) => {
unreachable!("{a:?} modified to {b:?}")
}
Err(ImageError::IsADirectory(..)) => {
// A directory was changed to a file
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
}
}

Err(ImageError::IsADirectory(..)) => {
// A directory was changed to a file
diff.modified.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
}
Err(ImageError::NotFound(..)) => {
// File not found in original /etc, file was added
diff.added.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
}

Err(ImageError::NotFound(..)) => {
// File not found in original /etc, file was added
diff.added.push(current_path.clone());
check_if_mergable(new, inode, &current_path, diff)?;
Err(e) => Err(e).with_context(|| format!("Opening pristine {path:?}"))?,
}

Err(e) => Err(e).with_context(|| format!("Opening pristine {path:?}"))?,
},
}
}

current_path.pop();
Expand Down Expand Up @@ -470,6 +513,8 @@ pub fn compute_diff(
pristine_etc_files: &FileSystem<CustomMetadata>,
current_etc_files: &FileSystem<CustomMetadata>,
new_etc_files: &FileSystem<CustomMetadata>,
current_etc_fd: &CapStdDir,
pristine_etc_fd: &CapStdDir,
) -> anyhow::Result<Diff> {
let mut diff = Diff {
added: vec![],
Expand All @@ -484,6 +529,8 @@ pub fn compute_diff(
&pristine_etc_files.leaves,
&current_etc_files.leaves,
&new_etc_files.root,
current_etc_fd,
pristine_etc_fd,
PathBuf::new(),
&mut diff,
)?;
Expand Down Expand Up @@ -637,17 +684,19 @@ fn recurse_dir(
continue;
}

// TODO: Another generic here but constrained to Sha256HashValue
// Regarding this, we'll definitely get DigestMismatch error if SHA512 is being used
// So we query the verity again if we get a DigestMismatch error
// NOTE: Currently this path isn't taken in practice (yet) since "current etc" is a mounted
// overlay which doesn't actually have FSVerity enabled on the files and `fsverity measure
// <file>` fails with `ERROR: FS_IOC_MEASURE_VERITY failed on '<file>': Inappropriate ioctl for device`
//
// Related: https://github.com/bootc-dev/bootc/issues/2627
let measured_verity =
composefs::fsverity::measure_verity_opt::<Sha256HashValue>(entry.open()?);
composefs::fsverity::measure_verity_opt::<Sha512HashValue>(entry.open()?);

let measured_verity = match measured_verity {
Ok(mv) => mv.map(|verity| verity.to_hex()),

Err(composefs::fsverity::MeasureVerityError::InvalidDigestAlgorithm { .. }) => {
composefs::fsverity::measure_verity_opt::<Sha512HashValue>(entry.open()?)?
composefs::fsverity::measure_verity_opt::<Sha256HashValue>(entry.open()?)?
.map(|verity| verity.to_hex())
}

Expand Down Expand Up @@ -705,6 +754,7 @@ impl std::fmt::Display for ModificationType {
}

impl ModificationType {
#[allow(dead_code)]
fn symbol(&self) -> &'static str {
match self {
ModificationType::Added => "+",
Expand Down Expand Up @@ -1007,6 +1057,8 @@ mod tests {
&pristine_etc_files,
&current_etc_files,
new_etc_files.as_ref().unwrap(),
&c,
&p,
)?;

merge(
Expand Down Expand Up @@ -1207,6 +1259,8 @@ mod tests {
&pristine_etc_files,
&current_etc_files,
&new_etc_files.as_ref().unwrap(),
&c,
&p,
)?;
merge(&c, &current_etc_files, &n, &new_etc_files.unwrap(), &diff)?;

Expand Down Expand Up @@ -1287,6 +1341,8 @@ mod tests {
&pristine_etc_files,
&current_etc_files,
&new_etc_files.as_ref().unwrap(),
&c,
&p,
)?;

let merge_res = merge(&c, &current_etc_files, &n, &new_etc_files.unwrap(), &diff);
Expand Down
10 changes: 9 additions & 1 deletion crates/lib/src/bootc_composefs/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub(crate) async fn get_etc_diff(
&new_etc_files
.as_ref()
.unwrap_or(&FileSystem::new(Stat::uninitialized())),
&current_etc,
&pristine_etc,
)?;

Ok(diff)
Expand Down Expand Up @@ -134,7 +136,13 @@ pub(crate) async fn composefs_backend_finalize(
let new_files =
new_files.ok_or_else(|| anyhow::anyhow!("Failed to get dirtree for new etc"))?;

let diff = compute_diff(&pristine_files, &current_files, &new_files)?;
let diff = compute_diff(
&pristine_files,
&current_files,
&new_files,
&current_etc,
&pristine_etc,
)?;
merge(&current_etc, &current_files, &new_etc, &new_files, &diff)?;

// Remove /etc/.updated from the new deployment so that ConditionNeedsUpdate=|/etc
Expand Down
14 changes: 12 additions & 2 deletions crates/lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,9 @@ pub(crate) enum InternalsOpts {
/// Whether to perform the three way merge or not
#[clap(long)]
merge: bool,
/// Output the diff as json
#[clap(long)]
json: bool,
},
#[cfg(feature = "docgen")]
/// Dump CLI structure as JSON for documentation generation
Expand Down Expand Up @@ -2741,6 +2744,7 @@ async fn run_from_opt(opt: Opt) -> Result<CliExitStatus> {
current_etc,
new_etc,
merge,
json,
} => {
let pristine_etc =
Dir::open_ambient_dir(pristine_etc, cap_std::ambient_authority())?;
Expand All @@ -2754,8 +2758,14 @@ async fn run_from_opt(opt: Opt) -> Result<CliExitStatus> {
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Failed to get new directory tree"))?;

let diff = compute_diff(&p, &c, &n)?;
print_diff(&diff, &mut std::io::stdout());
let diff = compute_diff(&p, &c, &n, &current_etc, &pristine_etc)?;

if json {
serde_json::to_writer(std::io::stdout(), &diff)
.context("Writing JSON output")?;
} else {
print_diff(&diff, &mut std::io::stdout());
}

if merge {
etc_merge::merge(&current_etc, &c, &new_etc, &n, &diff)?;
Expand Down
Loading
Loading