From bb8db59534032543b2e268ec570475b642c2e71d Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 25 Jun 2023 05:43:17 +0200 Subject: [PATCH] better nvme --- efi-image.nix | 143 +++++++++++++++++++++++++++++----------- flake.nix | 59 ++++++++++------- qemu.nix => qemu-vm.nix | 0 3 files changed, 139 insertions(+), 63 deletions(-) rename qemu.nix => qemu-vm.nix (100%) diff --git a/efi-image.nix b/efi-image.nix index 341b378..07ba492 100644 --- a/efi-image.nix +++ b/efi-image.nix @@ -1,61 +1,126 @@ { config, pkgs, lib, inputs, modulesPath, ... }: + # prior art: +# https://github.com/dramforever/nixos-riscv-efi/ # https://github.com/NixOS/nixos-hardware/blob/master/starfive/visionfive/v2/sd-image.nix -# https://github.com/nix-community/nixos-generators/blob/master/formats/raw.nix -# https://github.com/nix-community/nixos-generators/blob/master/formats/raw-efi.nix +# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/installer/sd-card/sd-image.nix + +# info: +# https://u-boot.readthedocs.io/en/latest/develop/spl.html +# https://github.com/starfive-tech/VisionFive2 +# https://github.com/u-boot/u-boot/blob/master/doc/board/starfive/visionfive2.rst +# https://github.com/starfive-tech/u-boot/commit/2a040b7720825629103c5b1efc5ace023072db8d +# https://www.man7.org/linux/man-pages/man8/sfdisk.8.html + +# guidance +# https://reddit.com/r/RISCV/comments/zy30vl/comment/j3zhzkx/ +# https://forum.rvspace.org/t/nvme-boot-using-visionfive2-software-v2-11-5/2464/1 +# https://forum.rvspace.org/t/i-tried-the-v3-0-4-sdcard-img-from-sdcard-and-from-nvme/2965 + let firmware = pkgs.callPackage "${inputs.nixos-hardware}/starfive/visionfive/v2/firmware.nix" { }; -in { +in +{ + options.efiImage = with lib; { - options.efiImage = { - imageBaseName = lib.mkOption { + imageBaseName = mkOption { default = "nixos-efi-image"; - description = lib.mdDoc "Prefix of the name of the generated image file."; + description = mdDoc "Prefix of the name of the generated image file."; }; + + compressImage = mkOption { + type = types.bool; + default = false; + description = mdDoc "Whether the image should be compressed using {command}`zstd`."; + }; + }; config = { - boot.loader.grub.device = config.fileSystems.${config.boot.loader.efi.efiSysMountPoint}.device; # hack for installBootLoader - fileSystems.${config.boot.loader.efi.efiSysMountPoint}.neededForBoot = true; - fileSystems."/".autoResize = true; - boot.growPartition = true; - #boot.loader.grub.device = "nodev"; - #boot.loader.grub.efiSupport = true; - #boot.loader.grub.efiInstallAsRemovable = true; + #assertions = [ + # { + # assertion = config.boot.loader.systemd-boot.enable; + # message = "Building EFI Image requires systemd-boot"; + # } + #]; - system.build.efiImage = import "${modulesPath}/../lib/make-disk-image.nix" { - inherit config lib pkgs; - name = "${config.efiImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-starfive-visionfive2.img"; - #diskSize = 8*1024; # megabytes, defaults to "auto" - bootSize = "512M"; # nixos-install default + #fileSystems.${config.boot.loader.efi.efiSysMountPoint}.neededForBoot = true; + #fileSystems."/".autoResize = true; + #boot.growPartition = true; - partitionTableType = "efi"; - installBootLoader = true; - touchEFIVars = true; + system.build.efiImage = pkgs.runCommand "efi-image" { + nativeBuildInputs = with pkgs; [ + util-linux # (partx) + ] ++ lib.optional config.efiImage.compressImage zstd; + rootImage = pkgs.callPackage "${modulesPath}/../lib/make-ext4-fs.nix" { + storePaths = [ config.system.build.toplevel ]; + volumeLabel = "NIXOS_ROOT"; + populateImageCommands = '' + mkdir -p ./files/boot + ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot + ''; + }; + skipSize = 1; # initial offset + espSize = 512; # reserved boot partition size + imageName = "${config.efiImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-starfive-visionfive2.img"; + } '' + mkdir -p "$out" + img="$out/$imageName" - #efiFirmware - #efiVariables + rootSizeBlocks="$(du -B 512 --apparent-size "$rootImage" | awk '{ print $1 }')" + espSizeBlocks="$(( espSize * 1024 * 1024 / 512 ))" + imageSize=$(( rootSizeBlocks * 512 + espSizeBlocks * 512 + skipSize * 1024 * 1024 + 1024 * 1024 )) - fsType = config.fileSystems."/".fsType; - # TODO: zstd - format = "raw"; # one of qcow2, qcow2-compressed, vdi, vpc, raw. + truncate -s "$imageSize" "$img" - # TODO: make this thing able to rebuild - #configFile = pkgs.writeText "configuration.nix" '' - # { - # imports = [ <./machine-config.nix> ]; - # } - #''; - }; + sfdisk $img <