2
0
Fork 0

fixup! fixup! fixup! Boot from USB

This commit is contained in:
Ehmry - 2020-12-10 20:21:02 +01:00
parent 7239553135
commit 0d0d649e70
4 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1,69 @@
# Builds a compressed EFI System Partition image
{ config, pkgs, espImage, storeFsImage }:
pkgs.stdenv.mkDerivation {
name = "boot.qcow2";
nativeBuildInputs = with pkgs.buildPackages; [
config.system.build.qemu
utillinux
zstd
];
buildCommand = ''
img=./temp.raw
blockSize=512
sectorSize=$(( $blockSize * 1 ))
imgBytes=0
espSectorOffset=2048
esbByteOffset=$(( $espSectorOffset * $sectorSize ))
# Pad the front of the image
echo "Pad front of image with " $esbByteOffset " bytes"
truncate --size=$esbByteOffset $img
# Concatentenate the ESP
echo "Concatenate ESP ${espImage}"
zstdcat ${espImage} >> $img
imgBytes=$(stat --format=%s $img)
echo "Image is $(( $imgBytes >> 20 )) MiB with ESP partition"
nixSectorOffset=$(( ($imgBytes + $sectorSize - 1) / $sectorSize ))
nixByteOffset=$(( $nixSectorOffset * $sectorSize ))
# Pad the ESP
echo "Pad end of ESP with " $(( $nixByteOffset - $imgBytes )) " bytes"
truncate --size=$nixByteOffset $img
# Concatenate the nix partition
echo "Concatenate store ${storeFsImage}"
zstdcat ${storeFsImage} >> $img
imgBytes=$(stat --format=%s $img)
echo "Image is $(( $imgBytes >> 20 )) MiB with store partition"
endSectorOffset=$(( ($(stat --format=%s $img) + $sectorSize + 1) / $sectorSize ))
endByteOffset=$(( $endSectorOffset * $sectorSize ))
# Pad the end of the image
echo "Pad end of store with $(( $endByteOffset - $imgBytes )) bytes"
truncate --size=$endByteOffset $img
imgBytes=$(stat --format=%s $img)
echo "Image is $(( $imgBytes >> 20 )) MiB with final padding"
efiUuid=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
# Create the partition table
sfdisk $img <<EOF
label: gpt
label-id: 44444444-4444-4444-8888-888888888888
start=$nixSectorOffset, type=${config.genode.boot.storePartUuid}
start=$espSectorOffset, type=$efiUuid
EOF
qemu-img convert $img $out
'';
}

View File

@ -0,0 +1,72 @@
# Builds a compressed EFI System Partition image
{ config, pkgs }:
let
grub' = pkgs.buildPackages.grub2_efi;
# Name used by UEFI for architectures.
targetArch = if pkgs.stdenv.isi686 || config.boot.loader.grub.forcei686 then
"ia32"
else if pkgs.stdenv.isx86_64 then
"x64"
else if pkgs.stdenv.isAarch64 then
"aa64"
else
throw "Unsupported architecture";
in pkgs.stdenv.mkDerivation {
name = "esp.img.zst";
nativeBuildInputs = with pkgs.buildPackages; [ grub' dosfstools mtools zstd ];
MODULES = [
"configfile"
"efi_gop"
"efi_uga"
"ext2"
"gzio"
"multiboot"
"multiboot2"
"normal"
"part_gpt"
"search_fs_uuid"
];
buildCommand = ''
img=tmp.raw
bootdir=EFI/boot/
mkdir -p $bootdir
cat <<EOF > embedded.cfg
insmod configfile
insmod efi_gop
insmod efi_uga
insmod ext2
insmod normal
insmod part_gpt
insmod search_fs_uuid
search.fs_uuid ${config.genode.boot.storeFsUuid} root
set prefix=($root)/boot/grub
configfile /boot/grub/grub.cfg
EOF
grub-script-check embedded.cfg
${grub'}/bin/grub-mkimage \
--config=embedded.cfg \
--output=$bootdir/boot${targetArch}.efi \
--prefix=/boot/grub \
--format=${grub'.grubTarget} \
$MODULES
# Make the ESP image twice as large as necessary
imageBytes=$(du --summarize --block-size=4096 --total $bootdir | tail -1 | awk '{ print int($1 * 8192) }')
truncate --size=$imageBytes $img
mkfs.vfat -n EFIBOOT --invariant $img
mcopy -sv -i $img EFI ::
fsck.vfat -nv $img
zstd --verbose --no-progress ./$img -o $out
'';
}

View File

@ -0,0 +1 @@
"24b69406-18a1-428d-908e-d21a1437122c"

View File

@ -0,0 +1 @@
"9668f8dd-d9a0-4398-a55a-0d499d5e5cbb"