From 0d0d649e70af99d23f4f42ef19253923ced1c971 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 10 Dec 2020 20:21:02 +0100 Subject: [PATCH] fixup! fixup! fixup! Boot from USB --- nixos-modules/lib/make-bootable-image.nix | 69 ++++++++++++++++++++++ nixos-modules/lib/make-esp-fs.nix | 72 +++++++++++++++++++++++ nixos-modules/partition-type | 1 + nixos-modules/store-fs-uuid | 1 + 4 files changed, 143 insertions(+) create mode 100644 nixos-modules/lib/make-bootable-image.nix create mode 100644 nixos-modules/lib/make-esp-fs.nix create mode 100644 nixos-modules/partition-type create mode 100644 nixos-modules/store-fs-uuid diff --git a/nixos-modules/lib/make-bootable-image.nix b/nixos-modules/lib/make-bootable-image.nix new file mode 100644 index 0000000..3a12cbf --- /dev/null +++ b/nixos-modules/lib/make-bootable-image.nix @@ -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 < 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 + ''; +} diff --git a/nixos-modules/partition-type b/nixos-modules/partition-type new file mode 100644 index 0000000..2cca25d --- /dev/null +++ b/nixos-modules/partition-type @@ -0,0 +1 @@ +"24b69406-18a1-428d-908e-d21a1437122c" diff --git a/nixos-modules/store-fs-uuid b/nixos-modules/store-fs-uuid new file mode 100644 index 0000000..7325548 --- /dev/null +++ b/nixos-modules/store-fs-uuid @@ -0,0 +1 @@ +"9668f8dd-d9a0-4398-a55a-0d499d5e5cbb"