Skip to content

Commit

Permalink
Add nixos-kexec utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Jan 31, 2025
1 parent 381a57a commit 13b1b0b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 26 deletions.
1 change: 1 addition & 0 deletions home-modules/jared/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ in
nix-output-monitor
nix-prefetch-scripts
nix-tree
nixos-kexec
nixos-shell
nload
nmap
Expand Down
66 changes: 40 additions & 26 deletions nixos-configurations/pea/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,61 @@ in
{
hardware.chromebook.asurada-spherion.enable = true;

system.extraSystemBuilderCmds = ''
export PATH=$PATH:${
lib.makeBinPath (
with pkgs.buildPackages;
[
dtc
ubootTools
vboot_reference
xz
]
)
}
lzma --threads $NIX_BUILD_CORES <${config.system.build.kernel}/${config.system.boot.loader.kernelFile} >kernel.lzma
cp ${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile} initrd
cp ${config.hardware.deviceTree.package}/${config.hardware.deviceTree.name} fdt
cp ${./fitimage.its} fitimage.its # needs to be in the same directory
mkimage -D "-I dts -O dtb -p 2048" -f fitimage.its vmlinux.uimg
dd status=none if=/dev/zero of=bootloader.bin bs=512 count=1
echo "init=$out/init ${toString config.boot.kernelParams}" >kernel-params
futility vbutil_kernel \
--pack $out/kpart \
--version 1 \
--vmlinuz vmlinux.uimg \
--arch aarch64 \
--keyblock ${pkgs.vboot_reference}/share/vboot/devkeys/kernel.keyblock \
--signprivate ${pkgs.vboot_reference}/share/vboot/devkeys/kernel_data_key.vbprivk \
--config kernel-params \
--bootloader bootloader.bin
'';

system.build.testImage = pkgs.callPackage (
{
dtc,
runCommand,
ubootTools,
util-linux,
vboot_reference,
xz,
zstd,
}:

runCommand "test-image"
{
nativeBuildInputs = [
dtc
ubootTools
util-linux
vboot_reference
xz
zstd
];
}
''
mkdir -p $out
lzma --threads $NIX_BUILD_CORES <${config.system.build.kernel}/${config.system.boot.loader.kernelFile} >kernel.lzma
cp ${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile} initrd
cp ${config.hardware.deviceTree.package}/${config.hardware.deviceTree.name} fdt
cp ${./fitimage.its} fitimage.its # needs to be in the same directory
mkimage -D "-I dts -O dtb -p 2048" -f fitimage.its vmlinux.uimg
dd status=none if=/dev/zero of=bootloader.bin bs=512 count=1
echo "init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}" >kernel-params
futility vbutil_kernel \
--pack $out/kpart \
--version 1 \
--vmlinuz vmlinux.uimg \
--arch aarch64 \
--keyblock ${vboot_reference}/share/vboot/devkeys/kernel.keyblock \
--signprivate ${vboot_reference}/share/vboot/devkeys/kernel_data_key.vbprivk \
--config kernel-params \
--bootloader bootloader.bin
${config.system.build.toplevel}/kpart
dd if=/dev/zero of=$out/image bs=4M count=20
sfdisk --no-reread --no-tell-kernel $out/image <<EOF
Expand All @@ -73,6 +83,10 @@ in
zstd -T$NIX_BUILD_CORES --rm $out/image
''
) { };

boot.loader.systemd-boot.extraInstallCommands = ''
${lib.getExe' pkgs.coreutils "dd"} bs=4M if=$1/kpart of=/dev/disk/by-partlabel/kernel
'';
}
{
custom.desktop.enable = true;
Expand Down
3 changes: 3 additions & 0 deletions nixos-configurations/pea/fitimage.its
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
compression = "lzma";
load = <0>;
entry = <0>;
hash-1 {
algo = "sha256";
};
};

ramdisk-1 {
Expand Down
2 changes: 2 additions & 0 deletions nixos-modules/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ in
{
system.stateVersion = mkDefault "25.05";

environment.systemPackages = [ pkgs.nixos-kexec ];

# We always build on x86_64-linux.
#
# "If it don't cross-compile, it don't go in the config!"
Expand Down
23 changes: 23 additions & 0 deletions overlays/pkgs/nixos-kexec/nixos-kexec.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# shellcheck shell=bash

declare kexec_jq

choice=${1:-}

if [[ -z $choice ]]; then
declare -a choices

while read -r system_closure; do
choices+=("$system_closure")
done < <(find /nix/var/nix/profiles -name 'system-*')

choice=$(echo "${choices[@]}" | zf)
fi

if [[ -z $choice ]]; then
exit 1
fi

eval "$(jq --raw-output --from-file "$kexec_jq" <"${choice}/boot.json")"

systemctl kexec
5 changes: 5 additions & 0 deletions overlays/pkgs/nixos-kexec/nixos-kexec.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(."org.nixos.bootspec.v1".kernel) as $linux
| (."org.nixos.bootspec.v1".initrd) as $initrd
| (."org.nixos.bootspec.v1".kernelParams | join(" ")) as $otherParams
| (."org.nixos.bootspec.v1".init) as $init
| "kexec -l \($linux) --initrd=\($initrd) --command-line=\"init=\($init) \($otherParams)\""
21 changes: 21 additions & 0 deletions overlays/pkgs/nixos-kexec/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
jq,
lib,
writeShellApplication,
zf,
}:

writeShellApplication {
name = "nixos-kexec";

runtimeInputs = [
jq
zf
];

text =
''
kexec_jq=${./nixos-kexec.jq}
''
+ lib.fileContents ./nixos-kexec.bash;
}

0 comments on commit 13b1b0b

Please sign in to comment.