Put boot image into ESP

This commit is contained in:
Ehmry - 2021-03-30 22:39:22 +02:00
parent fe3e0da51d
commit 5006257230
3 changed files with 37 additions and 33 deletions

View File

@ -10,6 +10,8 @@ pkgs.stdenv.mkDerivation {
zstd zstd
]; ];
inherit espImage;
buildCommand = '' buildCommand = ''
img=./temp.raw img=./temp.raw
blockSize=512 blockSize=512
@ -24,8 +26,8 @@ pkgs.stdenv.mkDerivation {
truncate --size=$esbByteOffset $img truncate --size=$esbByteOffset $img
# Concatentenate the ESP # Concatentenate the ESP
echo "Concatenate ESP ${espImage}" echo "Concatenate ESP $espImage"
zstdcat ${espImage} >> $img zstdcat $espImage >> $img
imgBytes=$(stat --format=%s $img) imgBytes=$(stat --format=%s $img)
echo "Image is $(( $imgBytes >> 20 )) MiB with ESP partition" echo "Image is $(( $imgBytes >> 20 )) MiB with ESP partition"

View File

@ -23,29 +23,32 @@ in pkgs.stdenv.mkDerivation {
"configfile" "configfile"
"efi_gop" "efi_gop"
"efi_uga" "efi_uga"
"ext2" "fat"
"gzio" "gzio"
"multiboot" "multiboot"
"multiboot2" "multiboot2"
"normal" "normal"
"part_gpt" "part_gpt"
"search_fs_uuid" "search"
]; ];
buildCommand = '' buildCommand = ''
img=tmp.raw img=tmp.raw
bootdir=EFI/boot/ bootdir=./espRoot/boot/
mkdir -p $bootdir grubdir=./espRoot/boot/grub
efidir=./espRoot/EFI/boot
mkdir -p $bootdir $efidir $grubdir
cat <<EOF > embedded.cfg cat <<EOF > embedded.cfg
insmod configfile insmod configfile
insmod efi_gop insmod efi_gop
insmod efi_uga insmod efi_uga
insmod ext2 insmod fat
insmod normal insmod normal
insmod part_gpt insmod part_gpt
insmod search_fs_uuid insmod search_fs_uuid
search.fs_uuid ${config.genode.boot.storeFsUuid} root search --set=root --label EFIBOOT
set prefix=($root)/boot/grub set prefix=($root)/boot/grub
configfile /boot/grub/grub.cfg configfile /boot/grub/grub.cfg
EOF EOF
@ -54,17 +57,34 @@ in pkgs.stdenv.mkDerivation {
${grub'}/bin/grub-mkimage \ ${grub'}/bin/grub-mkimage \
--config=embedded.cfg \ --config=embedded.cfg \
--output=$bootdir/boot${targetArch}.efi \ --output=$efidir/boot${targetArch}.efi \
--prefix=/boot/grub \ --prefix=/sigil/grub \
--format=${grub'.grubTarget} \ --format=${grub'.grubTarget} \
$MODULES $MODULES
cat > extraPrepareConfig.sh <<< '${config.boot.loader.grub.extraPrepareConfig}'
substituteInPlace extraPrepareConfig.sh \
--replace '${pkgs.coreutils}' '${pkgs.buildPackages.coreutils}' \
--replace '@bootPath@' "$bootdir"
source extraPrepareConfig.sh
cat <<EOF > $grubdir/grub.cfg
set timeout=3
set default=0
set gfxpayload=auto
${config.boot.loader.grub.extraEntries}
EOF
grub-script-check $grubdir/grub.cfg
# Make the ESP image twice as large as necessary # Make the ESP image twice as large as necessary
imageBytes=$(du --summarize --block-size=4096 --total $bootdir | tail -1 | awk '{ print int($1 * 8192) }') imageBytes=$(du --summarize --block-size=4096 --total espRoot | tail -1 | awk '{ print int($1 * 8192) }')
truncate --size=$imageBytes $img truncate --size=$imageBytes $img
mkfs.vfat -n EFIBOOT --invariant $img mkfs.vfat -n EFIBOOT --invariant $img
mcopy -sv -i $img EFI :: mcopy -sv -i $img espRoot/* ::
fsck.vfat -nv $img fsck.vfat -nv $img
zstd --verbose --no-progress ./$img -o $out zstd --verbose --no-progress ./$img -o $out

View File

@ -1,8 +1,6 @@
{ config, lib, pkgs, extraInputs ? [ ], contents }: { config, lib, pkgs, extraInputs ? [ ], contents }:
let let
grub' = pkgs.buildPackages.grub2_efi;
copyEris = lib.strings.concatMapStrings ({ source, target }: '' copyEris = lib.strings.concatMapStrings ({ source, target }: ''
cp -a --reflink=auto "${source}" "./rootImage/${target}" cp -a --reflink=auto "${source}" "./rootImage/${target}"
'') contents; '') contents;
@ -12,7 +10,6 @@ in pkgs.stdenv.mkDerivation {
nativeBuildInputs = with pkgs.buildPackages; [ nativeBuildInputs = with pkgs.buildPackages; [
e2fsprogs.bin e2fsprogs.bin
grub'
libfaketime libfaketime
perl perl
fakeroot fakeroot
@ -23,23 +20,7 @@ in pkgs.stdenv.mkDerivation {
img=temp.raw img=temp.raw
# Create nix/store before copying path # Create nix/store before copying path
mkdir -p ./rootImage/boot/grub ./rootImage/nix/store mkdir -p ./rootImage/nix/store
cat > extraPrepareConfig.sh <<< '${config.boot.loader.grub.extraPrepareConfig}'
substituteInPlace extraPrepareConfig.sh \
--replace '${pkgs.coreutils}' '${pkgs.buildPackages.coreutils}' \
--replace '@bootPath@' './rootImage/boot'
source extraPrepareConfig.sh
cat <<EOF > ./rootImage/boot/grub/grub.cfg
set timeout=3
set default=0
set gfxpayload=auto
${config.boot.loader.grub.extraEntries}
EOF
grub-script-check ./rootImage/boot/grub/grub.cfg
( (
GLOBIGNORE=".:.." GLOBIGNORE=".:.."
@ -65,7 +46,8 @@ in pkgs.stdenv.mkDerivation {
faketime -f "1970-01-01 00:00:01" fakeroot mkfs.ext2 -L NIXOS_GENODE -U ${config.genode.boot.storeFsUuid} -d ./rootImage $img faketime -f "1970-01-01 00:00:01" fakeroot mkfs.ext2 -L NIXOS_GENODE -U ${config.genode.boot.storeFsUuid} -d ./rootImage $img
export EXT2FS_NO_MTAB_OK=yes export EXT2FS_NO_MTAB_OK=yes
# I have ended up with corrupted images sometimes, I suspect that happens when the build machine's disk gets full during the build. # I have ended up with corrupted images sometimes, I suspect that
# happens when the build machine's disk gets full during the build.
if ! fsck.ext2 -n -f $img; then if ! fsck.ext2 -n -f $img; then
echo "--- Fsck failed for EXT2 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks) ---" echo "--- Fsck failed for EXT2 image of $bytes bytes (numInodes=$numInodes, numDataBlocks=$numDataBlocks) ---"
cat errorlog cat errorlog