Skip to content

Commit d8a145d

Browse files
author
Gareth Widlansky
committed
cleanup
Signed-off-by: Gareth Widlansky <gareth.widlansky@proton.me>
1 parent 1de5f0f commit d8a145d

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed

crates/lib/src/bootc_composefs/boot.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ use cap_std_ext::{
7878
};
7979
use clap::ValueEnum;
8080
use composefs::fs::read_file;
81-
use composefs::generic_tree::{Directory, Inode, LeafContent};
8281
use composefs::tree::RegularFile;
8382
use composefs_boot::BootOps;
8483
use composefs_boot::{
@@ -133,6 +132,8 @@ const SYSTEMD_LOADER_CONF_PATH: &str = "loader/loader.conf";
133132
const INITRD: &str = "initrd";
134133
const VMLINUZ: &str = "vmlinuz";
135134

135+
const BOOTC_AUTOENROLL_PATH: &str = "usr/lib/bootc/keys";
136+
136137
/// We want to be able to control the ordering of UKIs so we put them in a directory that's not the
137138
/// directory specified by the BLS spec. We do this because we want systemd-boot to only look at
138139
/// our config files and not show the actual UKIs in the bootloader menu
@@ -1143,40 +1144,41 @@ pub(crate) fn setup_composefs_uki_boot(
11431144
Ok(())
11441145
}
11451146

1146-
pub struct AuthFile {
1147-
pub filename: Utf8PathBuf,
1148-
pub file: RegularFile<Sha512HashValue>,
1147+
pub enum AutoEnroll {
1148+
None,
1149+
Keys { dir: Dir, keys: Vec<Utf8PathBuf> },
11491150
}
11501151

1151-
fn get_autoenroll_keys(root: &Directory<RegularFile<Sha512HashValue>>) -> Result<Vec<AuthFile>> {
1152+
fn get_systemd_boot_autoenroll(fs: &Dir, p: &str) -> Result<AutoEnroll> {
11521153
let mut entries = vec![];
1153-
match root.get_directory("/usr/lib/bootc/keys".as_ref()) {
1154-
Ok(keys_dir) => {
1155-
for (filename, inode) in keys_dir.entries() {
1156-
if !filename.as_bytes().ends_with(b".auth") {
1157-
continue;
1158-
}
11591154

1160-
let Inode::Leaf(leaf) = inode else {
1161-
bail!("/usr/lib/bootc/keys/{filename:?} is a directory");
1162-
};
1163-
1164-
let LeafContent::Regular(file) = &leaf.content else {
1165-
bail!("/usr/lib/bootc/keys/{filename:?} is not a regular file");
1166-
};
1167-
let path = match Utf8PathBuf::from_os_string(filename.into()) {
1168-
Ok(p) => p,
1169-
Err(_) => bail!("couldn't get pathbuf: /usr/lib/bootc/keys/{filename:?}"),
1170-
};
1171-
entries.push(AuthFile {
1172-
filename: path,
1173-
file: file.clone(),
1174-
});
1175-
}
1155+
let keys_dir = fs.open_dir(p)?;
1156+
1157+
for entry in keys_dir.entries()? {
1158+
let file = entry?;
1159+
1160+
let name = file.file_name();
1161+
if !file.file_type()?.is_file() {
1162+
bail!("/{p}/{name:?} is a not a regular file");
11761163
}
1177-
Err(other) => Err(other)?,
1178-
};
1179-
Ok(entries)
1164+
1165+
if !name.as_bytes().ends_with(b".auth") {
1166+
continue;
1167+
}
1168+
1169+
let path = match Utf8PathBuf::from_os_string(name.clone()) {
1170+
Ok(p) => p,
1171+
Err(_) => bail!("couldn't get pathbuf: /{p}/{name:?}"),
1172+
};
1173+
entries.push(path);
1174+
}
1175+
if entries.len() > 0 {
1176+
return Ok(AutoEnroll::Keys {
1177+
dir: keys_dir,
1178+
keys: entries,
1179+
});
1180+
}
1181+
return Ok(AutoEnroll::None);
11801182
}
11811183

11821184
#[context("Setting up composefs boot")]
@@ -1197,8 +1199,6 @@ pub(crate) fn setup_composefs_boot(
11971199

11981200
let postfetch = PostFetchState::new(state, &mounted_fs)?;
11991201

1200-
let keys = get_autoenroll_keys(&fs.root)?;
1201-
12021202
let boot_uuid = root_setup
12031203
.get_boot_uuid()?
12041204
.or(root_setup.rootfs_uuid.as_deref())
@@ -1220,8 +1220,7 @@ pub(crate) fn setup_composefs_boot(
12201220
&root_setup.physical_root_path,
12211221
&state.config_opts,
12221222
None,
1223-
keys,
1224-
&repo,
1223+
get_systemd_boot_autoenroll(&mounted_fs, BOOTC_AUTOENROLL_PATH)?,
12251224
)?;
12261225
}
12271226

crates/lib/src/bootloader.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use camino::Utf8Path;
77
use cap_std_ext::cap_std::ambient_authority;
88
use cap_std_ext::cap_std::fs::Dir;
99
use cap_std_ext::dirext::CapStdExtDirExt;
10-
use composefs::fs::read_file;
1110
use fn_error_context::context;
1211

1312
use bootc_blockdev::{Partition, PartitionTable};
1413
use bootc_mount as mount;
1514

16-
use crate::bootc_composefs::boot::{mount_esp, AuthFile};
15+
use crate::bootc_composefs::boot::{mount_esp, AutoEnroll};
1716
use crate::{discoverable_partition_specification, utils};
1817

1918
/// The name of the mountpoint for efi (as a subdirectory of /boot, or at the toplevel)
@@ -23,6 +22,8 @@ pub(crate) const EFI_DIR: &str = "efi";
2322
#[allow(dead_code)]
2423
const BOOTUPD_UPDATES: &str = "usr/lib/bootupd/updates";
2524

25+
const SYSTEMD_AUTOENROLL: &str = "loader/keys/auto";
26+
2627
#[allow(dead_code)]
2728
pub(crate) fn esp_in(device: &PartitionTable) -> Result<&Partition> {
2829
device
@@ -78,8 +79,9 @@ pub(crate) fn install_systemd_boot(
7879
_rootfs: &Utf8Path,
7980
_configopts: &crate::install::InstallConfigOpts,
8081
_deployment_path: Option<&str>,
81-
autoenroll: Vec<AuthFile>,
82-
repo: &crate::store::ComposefsRepository,
82+
// autoenroll: Vec<Utf8PathBuf>,
83+
// bootc_keys_dir: Option<Dir>,
84+
autoenroll: AutoEnroll,
8385
) -> Result<()> {
8486
let esp_part = device
8587
.find_partition_of_type(discoverable_partition_specification::ESP)
@@ -95,25 +97,23 @@ pub(crate) fn install_systemd_boot(
9597
.log_debug()
9698
.run_inherited_with_cmd_context()?;
9799

98-
if autoenroll.len() < 1 {
99-
return Ok(());
100-
}
101-
102-
println!("Autoenrolling keys");
103-
let path = esp_path.join("loader/keys/auto");
104-
create_dir_all(&path)?;
105-
106-
let keys_dir = Dir::open_ambient_dir(&path, ambient_authority())
107-
.with_context(|| format!("Opening {path}"))?;
108-
for a in autoenroll.iter() {
109-
let p = path.join(a.filename.clone());
110-
keys_dir
111-
.atomic_write(
112-
&a.filename,
113-
read_file(&a.file, &repo).context("reading file")?,
114-
)
115-
.with_context(|| format!("Writing secure boot key: {p}"))?;
116-
println!("Wrote secure boot key: {p}");
100+
match autoenroll {
101+
AutoEnroll::None => return Ok(()),
102+
AutoEnroll::Keys { dir, keys } => {
103+
println!("Autoenrolling keys");
104+
let path = esp_path.join(SYSTEMD_AUTOENROLL);
105+
create_dir_all(&path)?;
106+
107+
let keys_dir = Dir::open_ambient_dir(&path, ambient_authority())
108+
.with_context(|| format!("Opening {path}"))?;
109+
for filename in keys.iter() {
110+
let p = path.join(filename.clone());
111+
keys_dir
112+
.atomic_write(&filename, dir.read(&filename)?)
113+
.with_context(|| format!("Writing secure boot key: {p}"))?;
114+
println!("Wrote secure boot key: {p}");
115+
}
116+
}
117117
}
118118
Ok(())
119119
}

0 commit comments

Comments
 (0)