Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Replacing objcopy with systemd-ukify. #251

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
30 changes: 8 additions & 22 deletions generator/src/bootable/efi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl EfiProgram {
Self { source }
}

pub fn write_unified_efi(&self, objcopy: &Path, outpath: &Path, stub: &Path) -> Result<()> {
pub fn write_unified_efi(&self, ukify: &Path, outpath: &Path, stub: &Path) -> Result<()> {
let generation_path = &self.source.toplevel.0;
let mut kernel_params = NamedTempFile::new()?;

Expand All @@ -27,28 +27,14 @@ impl EfiProgram {
self.source.kernel_params.join(" ")
)?;

// Offsets taken from one of systemd's EFI tests:
// https://github.com/systemd/systemd/blob/01d0123f044d6c090b6ac2f6d304de2bdb19ae3b/test/test-efi-create-disk.sh#L32-L38
let status = Command::new(objcopy)
let status = Command::new(ukify)
.args(&[
"--add-section",
&format!(".osrel={}/etc/os-release", generation_path.display()),
"--change-section-vma",
".osrel=0x20000",
"--add-section",
&format!(".cmdline={}", kernel_params.path().display()),
"--change-section-vma",
".cmdline=0x30000",
"--add-section",
&format!(".linux={}/kernel", generation_path.display()),
"--change-section-vma",
".linux=0x2000000",
"--add-section",
&format!(".initrd={}/initrd", generation_path.display()),
"--change-section-vma",
".initrd=0x3000000",
&stub.display().to_string(),
&outpath.display().to_string(),
"build",
&format!("--linux={}/kernel", generation_path.display()),
&format!("--initrd={}/initrd", generation_path.display()),
&format!("--cmdline=@{}", kernel_params.path().display()),
&format!("--os-release=@{}/etc/os-release", generation_path.display()),
&format!("--output={}", outpath.display().to_string()),
])
.status()?;

Expand Down
14 changes: 6 additions & 8 deletions generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use structopt::StructOpt;
struct Args {
// TODO: --out-dir?
/// The systemd-boot EFI stub used to create a unified EFI file
#[structopt(long, requires_all = &["objcopy", "unified-efi"])]
#[structopt(long, requires_all = &["ukify", "unified-efi"])]
systemd_efi_stub: Option<PathBuf>,
/// The `objcopy` binary
/// The `ukify` binary
#[structopt(long, requires_all = &["systemd-efi-stub", "unified-efi"])]
objcopy: Option<PathBuf>,
ukify: Option<PathBuf>,
/// Whether or not to combine the initrd and kernel into a unified EFI file
#[structopt(long, requires_all = &["systemd-efi-stub", "objcopy"])]
#[structopt(long, requires_all = &["systemd-efi-stub", "ukify"])]
unified_efi: bool,
/// The `systemd-machine-id-setup` binary
// TODO: maybe just pass in machine_id as an arg; if empty, omit from configuration?
Expand Down Expand Up @@ -60,13 +60,11 @@ fn main() -> Result<()> {

systemd_boot::generate(
bootables,
args.objcopy,
args.ukify,
//In fact systemd_efi_stub is no longer needed here.
args.systemd_efi_stub,
args.systemd_machine_id_setup,
)?;

// TODO: grub
// grub::generate(bootables, args.objcopy)?;

Ok(())
}
6 changes: 3 additions & 3 deletions generator/src/systemd_boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Contents {

pub fn generate(
bootables: Vec<Bootable>,
objcopy: Option<PathBuf>,
ukify: Option<PathBuf>,
systemd_efi_stub: Option<PathBuf>,
systemd_machine_id_setup: PathBuf,
) -> Result<()> {
Expand All @@ -55,10 +55,10 @@ pub fn generate(
write!(f, "{}", contents.conf)?;

let unified_dest = contents.unified_dest.unwrap();
let objcopy = objcopy.as_ref().unwrap();
let ukify = ukify.as_ref().unwrap();
let systemd_efi_stub = systemd_efi_stub.as_ref().unwrap();

efi.write_unified_efi(objcopy, Path::new(&unified_dest), systemd_efi_stub)?;
efi.write_unified_efi(ukify, Path::new(&unified_dest), systemd_efi_stub)?;
}
Bootable::Linux(toplevel) => {
let (path, contents) = self::linux_entry_impl(&toplevel, &machine_id)?;
Expand Down
8 changes: 6 additions & 2 deletions nixos-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ in
++ (lib.optionals config.boot.loader.secureboot.enable [
"--unified-efi"

"--objcopy"
"${pkgs.binutils-unwrapped}/bin/objcopy"
"--ukify"
"${config.systemd.package.override { withUkify = true; }}/lib/systemd/ukify"

"--systemd-efi-stub"
"${config.systemd.package}/lib/systemd/boot/efi/linuxx64.efi.stub"
Expand Down Expand Up @@ -77,6 +77,8 @@ in
]));
in
''
mkdir -p /usr/lib/
mount --bind ${config.systemd.package}/lib /usr/lib
set -eux

scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
Expand All @@ -94,6 +96,8 @@ in
--toplevel="$1" \
$([ ! -z ''${NIXOS_INSTALL_BOOTLOADER+x} ] && echo --install) \
${installerArgs}

umount /usr/lib
''
);
};
Expand Down