Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

efi: sync bootentry label with Anaconda #685

Merged
merged 1 commit into from
Jul 16, 2024
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ openat = "0.1.20"
openat-ext = ">= 0.2.2, < 0.3.0"
openssl = "^0.10"
os-release = "0.1.0"
regex = "1.10.4"
rustix = { version = "0.38.34", features = ["process", "fs"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
Expand Down
27 changes: 22 additions & 5 deletions src/efi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,28 @@ impl Efi {
log::debug!("Not booted via EFI, skipping firmware update");
return Ok(());
}
// Read /etc/os-release
let release: OsRelease = OsRelease::new()?;
let product_name: &str = &release.name;
let product_name = get_product_name()?;
log::debug!("Get product name: {product_name}");
assert!(product_name.len() > 0);
// clear all the boot entries that match the target name
clear_efi_target(product_name)?;
create_efi_boot_entry(device, espdir, vendordir, product_name)
clear_efi_target(&product_name)?;
create_efi_boot_entry(device, espdir, vendordir, &product_name)
}
}

#[context("Get product name")]
fn get_product_name() -> Result<String> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Should this change to fn get_product_name(root: &Dir) to pass root dir as parameter?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but not a blocker, just a nice to have IMO.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, will update in the later PR.

let file_path = Path::new("/etc/system-release");
if file_path.exists() {
let content = std::fs::read_to_string(file_path)?;
let re = regex::Regex::new(r" *release.*").unwrap();
return Ok(re.replace_all(&content, "").to_string());
}
// Read /etc/os-release
let release: OsRelease = OsRelease::new()?;
Ok(release.name)
}

/// Convert a nul-terminated UTF-16 byte array to a String.
fn string_from_utf16_bytes(slice: &[u8]) -> String {
// For some reason, systemd appends 3 nul bytes after the string.
Expand Down Expand Up @@ -659,4 +670,10 @@ Boot0003* test";
);
Ok(())
}
#[test]
fn test_get_product_name() -> Result<()> {
let name = get_product_name()?;
Copy link
Member

Choose a reason for hiding this comment

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

I think we shouldn't read global state in a unit test. This will fail on e.g. Debian or other systems and we want to support those.

We can unit test parsing from a &str easily enough though.

(That said of course, we could use openat or better cap-std to load from a test root...see what we do in https://github.com/containers/bootc/blob/c866bba1e96e912ab2e36b9eec3985e93e7a0c9b/lib/src/generator.rs#L90 - but unfortunately OsRelease doesn't support that yet)

Copy link
Member

Choose a reason for hiding this comment

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

This will fail on e.g. Debian or other systems and we want to support those.

I was wrong, it shouldn't because the system-release is optional.

However, it does still make sense to avoid reading global state longer term.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Yes, you are correct, will switch to os-release if not exist system-release.

assert!(name.len() > 0);
Ok(())
}
}
Loading