better nvme
This commit is contained in:
parent
c2ee77a6cb
commit
bb8db59534
143
efi-image.nix
143
efi-image.nix
|
@ -1,61 +1,126 @@
|
||||||
{ config, pkgs, lib, inputs, modulesPath, ... }:
|
{ config, pkgs, lib, inputs, modulesPath, ... }:
|
||||||
|
|
||||||
# prior art:
|
# 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/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/NixOS/nixpkgs/blob/master/nixos/modules/installer/sd-card/sd-image.nix
|
||||||
# https://github.com/nix-community/nixos-generators/blob/master/formats/raw-efi.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
|
let
|
||||||
firmware = pkgs.callPackage "${inputs.nixos-hardware}/starfive/visionfive/v2/firmware.nix" { };
|
firmware = pkgs.callPackage "${inputs.nixos-hardware}/starfive/visionfive/v2/firmware.nix" { };
|
||||||
in {
|
in
|
||||||
|
{
|
||||||
|
options.efiImage = with lib; {
|
||||||
|
|
||||||
options.efiImage = {
|
imageBaseName = mkOption {
|
||||||
imageBaseName = lib.mkOption {
|
|
||||||
default = "nixos-efi-image";
|
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 = {
|
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";
|
#assertions = [
|
||||||
#boot.loader.grub.efiSupport = true;
|
# {
|
||||||
#boot.loader.grub.efiInstallAsRemovable = true;
|
# assertion = config.boot.loader.systemd-boot.enable;
|
||||||
|
# message = "Building EFI Image requires systemd-boot";
|
||||||
|
# }
|
||||||
|
#];
|
||||||
|
|
||||||
system.build.efiImage = import "${modulesPath}/../lib/make-disk-image.nix" {
|
#fileSystems.${config.boot.loader.efi.efiSysMountPoint}.neededForBoot = true;
|
||||||
inherit config lib pkgs;
|
#fileSystems."/".autoResize = true;
|
||||||
name = "${config.efiImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-starfive-visionfive2.img";
|
#boot.growPartition = true;
|
||||||
#diskSize = 8*1024; # megabytes, defaults to "auto"
|
|
||||||
bootSize = "512M"; # nixos-install default
|
|
||||||
|
|
||||||
partitionTableType = "efi";
|
system.build.efiImage = pkgs.runCommand "efi-image" {
|
||||||
installBootLoader = true;
|
nativeBuildInputs = with pkgs; [
|
||||||
touchEFIVars = true;
|
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
|
rootSizeBlocks="$(du -B 512 --apparent-size "$rootImage" | awk '{ print $1 }')"
|
||||||
#efiVariables
|
espSizeBlocks="$(( espSize * 1024 * 1024 / 512 ))"
|
||||||
|
imageSize=$(( rootSizeBlocks * 512 + espSizeBlocks * 512 + skipSize * 1024 * 1024 + 1024 * 1024 ))
|
||||||
|
|
||||||
fsType = config.fileSystems."/".fsType;
|
truncate -s "$imageSize" "$img"
|
||||||
# TODO: zstd
|
|
||||||
format = "raw"; # one of qcow2, qcow2-compressed, vdi, vpc, raw.
|
|
||||||
|
|
||||||
# TODO: make this thing able to rebuild
|
sfdisk $img <<EOF
|
||||||
#configFile = pkgs.writeText "configuration.nix" ''
|
label: gpt
|
||||||
# {
|
unit: sectors
|
||||||
# imports = [ <./machine-config.nix> ];
|
sector-size: 512
|
||||||
# }
|
|
||||||
#'';
|
start=4096, size=4096, type=2E54B353-1271-4842-806F-E436D6AF6985, name=UBOOT_SPL
|
||||||
};
|
start=8192, size=8192, type=5B193300-FC78-40CD-8002-E86C45580B47, name=UBOOT_FIRMWARE
|
||||||
|
start=$(( skipSize + espSize ))M, type=linux, attrs="LegacyBIOSBootable"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
eval "$(partx $img -o START,SECTORS --nr 1 --pairs)"
|
||||||
|
dd conv=notrunc if=${firmware.spl}/share/starfive-visionfive2/spl.bin of=$img seek=$START count=$SECTORS
|
||||||
|
|
||||||
|
eval "$(partx $img -o START,SECTORS --nr 2 --pairs)"
|
||||||
|
dd conv=notrunc if=${firmware.uboot-fit-image}/share/starfive-visionfive2/visionfive2_fw_payload.img of=$img seek=$START count=$SECTORS
|
||||||
|
|
||||||
|
eval "$(partx $img -o START,SECTORS --nr 3 --pairs)"
|
||||||
|
dd conv=notrunc if="$rootImage" of="$img" seek="$START" count="$SECTORS"
|
||||||
|
|
||||||
|
if test -n "$compressImage"; then
|
||||||
|
zstd -T$NIX_BUILD_CORES --rm $img
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
|
||||||
|
boot.postBootCommands = ''
|
||||||
|
# On the first boot do some maintenance tasks
|
||||||
|
if [ -f /nix-path-registration ]; then
|
||||||
|
set -euo pipefail
|
||||||
|
set -x
|
||||||
|
|
||||||
|
# Register the contents of the initial Nix store
|
||||||
|
${config.nix.package.out}/bin/nix-store --load-db < /nix-path-registration
|
||||||
|
|
||||||
|
# nixos-rebuild also requires a "system" profile and an /etc/NIXOS tag.
|
||||||
|
touch /etc/NIXOS
|
||||||
|
${config.nix.package.out}/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system
|
||||||
|
|
||||||
|
# create the first boot profile
|
||||||
|
NIXOS_INSTALL_BOOTLOADER=1 /nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
||||||
|
rm -f /boot/loader/entries/initial-nixos.conf
|
||||||
|
|
||||||
|
# Prevents this from running on later boots.
|
||||||
|
rm -f /nix-path-registration
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
|
||||||
assertions = [
|
|
||||||
{
|
|
||||||
assertion = config.boot.loader.systemd-boot.enable;
|
|
||||||
message = "Building EFI Image requires systemd-boot";
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
59
flake.nix
59
flake.nix
|
@ -72,7 +72,7 @@
|
||||||
|
|
||||||
# install method
|
# install method
|
||||||
vf2-qemu = { lib, ...}: {
|
vf2-qemu = { lib, ...}: {
|
||||||
imports = [ "${nixos-hardware}/starfive/visionfive/v2" ./qemu.nix ];
|
imports = [ "${nixos-hardware}/starfive/visionfive/v2" ./qemu-vm.nix ];
|
||||||
};
|
};
|
||||||
vf2-sd-installer = {
|
vf2-sd-installer = {
|
||||||
imports = [ "${nixos-hardware}/starfive/visionfive/v2/sd-image-installer.nix" ];
|
imports = [ "${nixos-hardware}/starfive/visionfive/v2/sd-image-installer.nix" ];
|
||||||
|
@ -82,22 +82,38 @@
|
||||||
imports = [ "${nixos-hardware}/starfive/visionfive/v2/sd-image.nix" ];
|
imports = [ "${nixos-hardware}/starfive/visionfive/v2/sd-image.nix" ];
|
||||||
sdImage.compressImage = false;
|
sdImage.compressImage = false;
|
||||||
};
|
};
|
||||||
vf2-nvme = { vf2-firmware, pkgs, ... }: {
|
vf2-nvme = { pkgs, ... }: let
|
||||||
|
firmware = pkgs.callPackage "${inputs.nixos-hardware}/starfive/visionfive/v2/firmware.nix" { };
|
||||||
|
in {
|
||||||
imports = [ "${nixos-hardware}/starfive/visionfive/v2" ];
|
imports = [ "${nixos-hardware}/starfive/visionfive/v2" ];
|
||||||
#fileSystems."/boot" = { device = "/dev/nvme0n1p1"; fsType = "vfat"; };
|
fileSystems."/".device = "/dev/disk/by-label/NIXOS_ROOT";
|
||||||
#fileSystems."/" = { device = "/dev/nvme0n1p2"; fsType = "ext4"; };
|
fileSystems."/".fsType = "ext4";
|
||||||
|
fileSystems."/boot/spl".device = "/dev/disk/by-label/UBOOT_SPL";
|
||||||
|
fileSystems."/boot/spl".fsType = "vfat";
|
||||||
|
fileSystems."/boot/spl".options = [ "nofail" "noauto" ]; # not critical
|
||||||
|
fileSystems."/boot/firmware".device = "/dev/disk/by-label/UBOOT_FIRMWARE";
|
||||||
|
fileSystems."/boot/firmware".fsType = "vfat";
|
||||||
|
fileSystems."/boot/firmware".options = [ "nofail" "noauto" ]; # not critical
|
||||||
boot.initrd.kernelModules = [ "nvme" ];
|
boot.initrd.kernelModules = [ "nvme" ];
|
||||||
boot.loader.systemd-boot.enable = true;
|
#boot.loader.systemd-boot.enable = true;
|
||||||
boot.loader.efi.canTouchEfiVariables = true;
|
#boot.loader.efi.efiSysMountPoint = "/boot";
|
||||||
boot.loader.efi.efiSysMountPoint = "/boot";
|
#boot.loader.efi.canTouchEfiVariables = true;
|
||||||
environment.systemPackages = [
|
environment.systemPackages = [
|
||||||
pkgs.efibootmgr
|
pkgs.efibootmgr
|
||||||
pkgs.efivar
|
pkgs.efivar
|
||||||
vf2-firmware.updater-flash
|
firmware.updater-flash
|
||||||
|
(pkgs.writeShellApplication {
|
||||||
|
name = "visionfive2-firmware-update-ssd";
|
||||||
|
runtimeInputs = [ pkgs.mtdutils ];
|
||||||
|
text = ''
|
||||||
|
flashcp -v ${firmware.spl}/share/starfive-visionfive2/spl.bin /dev/nvme0n1p1
|
||||||
|
flashcp -v ${firmware.uboot-fit-image}/share/starfive-visionfive2/visionfive2_fw_payload.img /dev/nvme0n1p2
|
||||||
|
'';
|
||||||
|
})
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
vf2-nvme-image = {
|
vf2-nvme-image = {
|
||||||
_module.args = { inherit inputs; }; # TODO: needed?
|
_module.args = { inherit inputs; };
|
||||||
imports = [ vf2-nvme ./efi-image.nix ];
|
imports = [ vf2-nvme ./efi-image.nix ];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -148,21 +164,16 @@
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
packages = forAllSystems ({ system, mkNixos, ... }: with self.nixosModules; rec {
|
packages = forAllSystems ({ system, mkNixos, ... }: let
|
||||||
#default = asgaut-sd-installer;
|
mkAll = hostname: modules: with self.nixosModules; {
|
||||||
demo-sd-installer = (mkNixos [ demo vf2-r12a-8gb vf2-sd-installer ]).config.system.build.sdImage;
|
"${hostname}-sd-installer" = (mkNixos (modules ++ [ vf2-r12a-8gb vf2-sd-installer ])).config.system.build.sdImage;
|
||||||
demo-sd-image = (mkNixos [ demo vf2-r12a-8gb vf2-sd-image ]).config.system.build.sdImage;
|
"${hostname}-sd-image" = (mkNixos (modules ++ [ vf2-r12a-8gb vf2-sd-image ])).config.system.build.sdImage;
|
||||||
demo-nvme-image = (mkNixos [ demo vf2-r12a-8gb vf2-nvme-image ]).config.system.build.efiImage;
|
"${hostname}-nvme-image" = (mkNixos (modules ++ [ vf2-r12a-8gb vf2-nvme-image ])).config.system.build.efiImage;
|
||||||
demo-qemu = (mkNixos [ demo rv64 vf2-qemu ]).config.system.build.vm;
|
"${hostname}-qemu" = (mkNixos (modules ++ [ rv64 vf2-qemu ])).config.system.build.vm;
|
||||||
asgaut-sd-installer = (mkNixos [ asgaut vf2-r12a-8gb vf2-sd-installer ]).config.system.build.sdImage;
|
};
|
||||||
asgaut-sd-image = (mkNixos [ asgaut vf2-r12a-8gb vf2-sd-image ]).config.system.build.sdImage;
|
in (mkAll "demo" [ self.nixosModules.demo ])
|
||||||
asgaut-nvme-image = (mkNixos [ asgaut vf2-r12a-8gb vf2-nvme-image ]).config.system.build.efiImage;
|
// (mkAll "asgaut" [ self.nixosModules.asgaut ])
|
||||||
asgaut-qemu = (mkNixos [ asgaut rv64 vf2-qemu ]).config.system.build.vm;
|
// (mkAll "gunder" [ self.nixosModules.gunder ]));
|
||||||
gunder-sd-installer = (mkNixos [ gunder vf2-r12a-8gb vf2-sd-installer ]).config.system.build.sdImage;
|
|
||||||
gunder-sd-image = (mkNixos [ gunder vf2-r12a-8gb vf2-sd-image ]).config.system.build.sdImage;
|
|
||||||
gunder-nvme-image = (mkNixos [ gunder vf2-r12a-8gb vf2-nvme-image ]).config.system.build.efiImage;
|
|
||||||
gunder-qemu = (mkNixos [ gunder rv64 vf2-qemu ]).config.system.build.vm;
|
|
||||||
});
|
|
||||||
|
|
||||||
nixosConfigurations.demo = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ demo vf2-nvme ]; };
|
nixosConfigurations.demo = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ demo vf2-nvme ]; };
|
||||||
nixosConfigurations.asgaut = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ asgaut vf2-nvme ]; };
|
nixosConfigurations.asgaut = nixpkgs.lib.nixosSystem { system = "x86_64-linux"; modules = with self.nixosModules; [ asgaut vf2-nvme ]; };
|
||||||
|
|
Loading…
Reference in New Issue