Skip to content

Commit bfd1cd4

Browse files
committed
fix: remove kernel handling
1 parent 1c89242 commit bfd1cd4

File tree

5 files changed

+99
-183
lines changed

5 files changed

+99
-183
lines changed

src/bootupd.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::efi;
1212
use crate::freezethaw::fsfreeze_thaw_cycle;
1313
use crate::model::{ComponentStatus, ComponentUpdatable, ContentMetadata, SavedState, Status};
1414
use crate::{ostreeutil, util};
15-
use anyhow::{anyhow, Context, Result};
15+
use anyhow::{anyhow, bail, Context, Result};
1616
use camino::{Utf8Path, Utf8PathBuf};
1717
use clap::crate_version;
1818
use fn_error_context::context;
@@ -25,10 +25,12 @@ use std::fs::{self, File};
2525
use std::io::{BufRead, BufReader, BufWriter, Write};
2626
use std::path::{Path, PathBuf};
2727

28+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2829
pub(crate) enum ConfigMode {
2930
None,
3031
Static,
3132
WithUUID,
33+
SystemdBoot,
3234
}
3335

3436
impl ConfigMode {
@@ -37,6 +39,7 @@ impl ConfigMode {
3739
ConfigMode::None => None,
3840
ConfigMode::Static => Some(false),
3941
ConfigMode::WithUUID => Some(true),
42+
ConfigMode::SystemdBoot => Some(false),
4043
}
4144
}
4245
}
@@ -96,14 +99,18 @@ pub(crate) fn install(
9699
let meta = component
97100
.install(&source_root, dest_root, device, update_firmware)
98101
.with_context(|| format!("installing component {}", component.name()))?;
99-
log::warn!("Installed {} {}", component.name(), meta.meta.version);
102+
log::info!("Installed {} {}", component.name(), meta.meta.version);
100103
state.installed.insert(component.name().into(), meta);
101-
// Yes this is a hack...the Component thing just turns out to be too generic.
102-
if let Some(vendor) = component.get_efi_vendor(&source_root)? {
103-
assert!(installed_efi_vendor.is_none());
104-
installed_efi_vendor = Some(vendor);
104+
// If not systemd-boot, try to get the efi vendor
105+
if configs != ConfigMode::SystemdBoot {
106+
// Yes this is a hack...the Component thing just turns out to be too generic.
107+
if let Some(vendor) = component.get_efi_vendor(&source_root)? {
108+
assert!(installed_efi_vendor.is_none());
109+
installed_efi_vendor = Some(vendor);
110+
}
105111
}
106112
}
113+
107114
let sysroot = &openat::Dir::open(dest_root)?;
108115

109116
match configs.enabled_with_uuid() {
@@ -122,6 +129,17 @@ pub(crate) fn install(
122129
None => {}
123130
}
124131

132+
if configs == ConfigMode::SystemdBoot {
133+
let efi = crate::efi::Efi::default();
134+
log::warn!("Installing systemd-boot entries");
135+
if let Ok(Some(mnt)) = efi.get_mounted_esp(Path::new(dest_root)) {
136+
let esp_dir = openat::Dir::open(&mnt).context("Opening mounted ESP")?;
137+
crate::systemd_boot_configs::install(&esp_dir, false)?;
138+
} else {
139+
bail!("ESP not mounted, cannot install systemd-boot entries");
140+
}
141+
}
142+
125143
// Unmount the ESP, etc.
126144
drop(target_components);
127145

@@ -303,7 +321,7 @@ pub(crate) fn adopt_and_update(
303321
return Ok(Some(update));
304322
} else {
305323
// Nothing adopted, skip
306-
log::warn!("Component '{}' skipped adoption", component.name());
324+
log::info!("Component '{}' skipped adoption", component.name());
307325
return Ok(None);
308326
}
309327
}

src/cli/bootupd.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ pub struct InstallOpts {
7373
/// then only enable installation to the ESP.
7474
#[clap(long)]
7575
auto: bool,
76+
77+
/// The bootloader to use
78+
#[clap(long, default_value = "systemd-boot")]
79+
bootloader: Option<String>,
7680
}
7781

7882
#[derive(Debug, Parser)]
@@ -103,13 +107,18 @@ impl DCommand {
103107

104108
/// Runner for `install` verb.
105109
pub(crate) fn run_install(opts: InstallOpts) -> Result<()> {
106-
let configmode = if opts.write_uuid {
110+
let mut configmode = if opts.write_uuid {
107111
ConfigMode::WithUUID
108112
} else if opts.with_static_configs {
109113
ConfigMode::Static
110114
} else {
111115
ConfigMode::None
112116
};
117+
118+
if opts.bootloader.as_deref() == Some("systemd-boot") {
119+
configmode = ConfigMode::SystemdBoot;
120+
}
121+
113122
bootupd::install(
114123
&opts.src_root,
115124
&opts.dest_root,

src/efi.rs

Lines changed: 2 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ use cap_std::fs::Dir;
1616
use cap_std_ext::cap_std;
1717
use chrono::prelude::*;
1818
use fn_error_context::context;
19-
use log::{info, warn};
2019
use openat_ext::OpenatDirExt;
2120
use os_release::OsRelease;
22-
use std::io::Write;
2321
use rustix::fd::BorrowedFd;
2422
use walkdir::WalkDir;
25-
use widestring::U16CString;
2623

2724
use crate::bootupd::RootContext;
2825
use crate::freezethaw::fsfreeze_thaw_cycle;
@@ -151,35 +148,6 @@ impl Efi {
151148
clear_efi_target(&product_name)?;
152149
create_efi_boot_entry(device, espdir, vendordir, &product_name)
153150
}
154-
155-
/// Find a kernel and optional initramfs/uki file in the update directory for systemd-boot
156-
fn find_kernel_and_initrd(dir: &openat::Dir) -> Result<(String, Option<String>)> {
157-
let mut kernel: Option<String> = None;
158-
let mut initrd: Option<String> = None;
159-
log::warn!("Searching for kernel and initrd in update dir: {:?}", dir.recover_path());
160-
for entry in dir.list_dir(".")? {
161-
log::warn!("Found entry: {:?}", entry);
162-
let entry = entry?;
163-
let fname = entry.file_name().to_string_lossy();
164-
if fname.starts_with("vmlinuz") || fname.ends_with(".efi") || fname.ends_with(".uki") {
165-
log::warn!("Found kernel/UKI file: {}", fname);
166-
if kernel.is_some() {
167-
log::warn!("Multiple kernel/UKI files found in update dir");
168-
bail!("Multiple kernel/UKI files found in update dir");
169-
}
170-
kernel = Some(fname.to_string());
171-
} else if fname.starts_with("initrd") || fname.ends_with(".img") || fname.ends_with(".cpio.gz") {
172-
log::warn!("Found initramfs file: {}", fname);
173-
if initrd.is_some() {
174-
log::warn!("Multiple initramfs files found in update dir");
175-
bail!("Multiple initramfs files found in update dir");
176-
}
177-
initrd = Some(fname.to_string());
178-
}
179-
}
180-
let kernel = kernel.ok_or_else(|| anyhow::anyhow!("No kernel or UKI file found in update dir"))?;
181-
Ok((kernel, initrd))
182-
}
183151
}
184152

185153
#[context("Get product name")]
@@ -207,11 +175,6 @@ impl Component for Efi {
207175
return Ok(None);
208176
};
209177

210-
// // Don't adopt if the system is booted with systemd-boot or
211-
// // systemd-stub since those will be managed with bootctl.
212-
// if skip_systemd_bootloaders() {
213-
// return Ok(None);
214-
// }
215178
crate::component::query_adopt_state()
216179
}
217180

@@ -306,11 +269,10 @@ impl Component for Efi {
306269
device: &str,
307270
update_firmware: bool,
308271
) -> Result<InstalledContent> {
309-
log::warn!("Installing component: {}", self.name());
310272
let Some(meta) = get_component_update(src_root, self)? else {
311273
anyhow::bail!("No update metadata for component {} found", self.name());
312274
};
313-
log::warn!("Found metadata {}", meta.version);
275+
log::debug!("Found metadata {}", meta.version);
314276
let srcdir_name = component_updatedirname(self);
315277
let ft = crate::filetree::FileTree::new_from_dir(&src_root.sub_dir(&srcdir_name)?)?;
316278

@@ -329,15 +291,10 @@ impl Component for Efi {
329291
self.mount_esp_device(Path::new(dest_root), Path::new(&esp_device))?
330292
};
331293

332-
let destpath_clone = destpath.clone();
333-
334294
let destd = &openat::Dir::open(&destpath)
335295
.with_context(|| format!("opening dest dir {}", destpath.display()))?;
336296
validate_esp_fstype(destd)?;
337297

338-
// let using_systemd_boot = skip_systemd_bootloaders();
339-
let using_systemd_boot = true;
340-
341298
// TODO - add some sort of API that allows directly setting the working
342299
// directory to a file descriptor.
343300
std::process::Command::new("cp")
@@ -347,71 +304,7 @@ impl Component for Efi {
347304
.current_dir(format!("/proc/self/fd/{}", src_root.as_raw_fd()))
348305
.run()?;
349306

350-
// Configure systemd-boot loader config/entry
351-
if using_systemd_boot {
352-
let loader_dir = destpath.join("loader");
353-
if !loader_dir.exists() {
354-
log::warn!("Creating systemd-boot loader directory at {}", loader_dir.display());
355-
std::fs::create_dir_all(&loader_dir)
356-
.with_context(|| format!("creating systemd-boot loader directory {}", loader_dir.display()))?;
357-
}
358-
let entries_dir = loader_dir.join("entries");
359-
log::warn!("Using systemd-boot, creating entries dir at {}", entries_dir.display());
360-
std::fs::create_dir_all(&entries_dir)
361-
.with_context(|| format!("creating entries dir {}", entries_dir.display()))?;
362-
log::warn!("Installing systemd-boot entry in {}/bootupd.conf", entries_dir.display());
363-
// Write loader.conf if it doesn't exist
364-
let loader_conf_path = loader_dir.join("loader.conf");
365-
if !loader_conf_path.exists() {
366-
let mut loader_conf = std::fs::File::create(&loader_conf_path)
367-
.with_context(|| format!("creating loader.conf at {}", loader_conf_path.display()))?;
368-
writeln!(loader_conf, "default auto")?;
369-
writeln!(loader_conf, "timeout 20")?;
370-
writeln!(loader_conf, "editor no")?;
371-
}
372-
log::warn!("Installed: {}/bootupd.conf", entries_dir.display());
373-
374-
// let sysroot = Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
375-
// let product_name = get_product_name(&sysroot).unwrap_or("Unknown Product".to_string());
376-
// log::warn!("Get product name: '{product_name}'");
377-
378-
// Find the kernel/UKI and optional initramfs in the update dir
379-
log::warn!(
380-
"Searching for kernel and initrd in update dir: {:?}",
381-
crate::model::BOOTUPD_UPDATES_DIR
382-
);
383-
let update_dir = src_root.sub_dir(&srcdir_name)
384-
.context("opening update dir")?;
385-
log::debug!("Searching for kernel and initrd in update dir: {:?}", update_dir.recover_path());
386-
let (kernel_file, initrd_file) = Self::find_kernel_and_initrd(&update_dir)?;
387-
388-
log::warn!(
389-
"Installing systemd-boot entry for kernel: {}, initrd: {:?}",
390-
kernel_file, initrd_file
391-
);
392-
crate::systemd_boot_configs::install(
393-
&destd,
394-
true,
395-
"Unknown Product",
396-
&kernel_file,
397-
initrd_file.as_deref(),
398-
).context("installing systemd-boot entry")?;
399-
}
400-
401-
// If using systemd-boot, run `bootctl install` to set up the bootloader.
402-
if using_systemd_boot {
403-
log::warn!("Using systemd-boot, running bootctl install");
404-
let status = std::process::Command::new("bootctl")
405-
.args(["install", "--esp-path", destpath_clone.to_str().unwrap()])
406-
.status()
407-
.context("running bootctl install")?;
408-
log::warn!("bootctl install status: {}", status);
409-
if !status.success() {
410-
bail!("bootctl install failed with status: {}", status);
411-
}
412-
}
413-
414-
if update_firmware && !using_systemd_boot {
307+
if update_firmware {
415308
if let Some(vendordir) = self.get_efi_vendor(&src_root)? {
416309
self.update_firmware(device, destd, &vendordir)?
417310
}
@@ -750,12 +643,9 @@ fn get_efi_component_from_usr<'a>(
750643
.filter_map(|entry| {
751644
let entry = entry.ok()?;
752645
if !entry.file_type().is_dir() || entry.file_name() != "EFI" {
753-
info!("Skipping non-directory or non-EFI entry: {}", entry.path().display());
754646
return None;
755647
}
756648

757-
info!("Found EFI component at {}", entry.path().display());
758-
759649
let abs_path = entry.path();
760650
let rel_path = abs_path.strip_prefix(sysroot).ok()?;
761651
let utf8_rel_path = Utf8PathBuf::from_path_buf(rel_path.to_path_buf()).ok()?;

0 commit comments

Comments
 (0)