From e9ab8d71837c6b16295578dbcbeb7375ea31fb69 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 4 Dec 2023 14:08:13 +0100 Subject: [PATCH] modules/outputs/ubivolume: introduce ubinization It creates an UBI image based on an UBI volume configuration. For now, it creates only an empty rootfs. --- modules/all-modules.nix | 2 + modules/outputs/ubivolume.nix | 91 +++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 modules/outputs/ubivolume.nix diff --git a/modules/all-modules.nix b/modules/all-modules.nix index 6b432ce..f6a8908 100644 --- a/modules/all-modules.nix +++ b/modules/all-modules.nix @@ -31,6 +31,8 @@ ./ssh ./outputs/tftpboot.nix ./outputs/ubifs.nix + ./ubifs.nix + ./ubinize.nix ./users.nix ./vlan ./watchdog diff --git a/modules/outputs/ubivolume.nix b/modules/outputs/ubivolume.nix new file mode 100644 index 0000000..45aa889 --- /dev/null +++ b/modules/outputs/ubivolume.nix @@ -0,0 +1,91 @@ +{ + config +, pkgs +, lib +, ... +}: +let + inherit (pkgs) liminix; + inherit (lib) mkIf mkOption types concatStringsSep optionalString; +in + { + imports = [ + ./initramfs.nix + ./ubifs.nix + ]; + + options.hardware.ubi = { + minIOSize = mkOption { type = types.str; }; + eraseBlockSize = mkOption { type = types.str; }; # LEB + maxLEBcount = mkOption { type = types.str; }; # LEB + }; + + config = mkIf (config.rootfsType == "ubifs") { + kernel.config = { + MTD_UBI="y"; + + UBIFS_FS = "y"; + UBIFS_FS_SECURITY = "n"; + }; + boot.initramfs.enable = true; + + system.outputs.rootfs = + let + inherit (pkgs.pkgsBuildBuild) runCommand; + ubiVolume = ({ name, volumeId, image, flags ? [] }: + '' + [${name}] + mode=ubi + vol_id=${toString volumeId} + vol_type=dynamic + vol_name=${name} + vol_alignment=1 + ${optionalString (image != null) '' + image=${image} + ''} + ${optionalString (image == null) '' + vol_size=1MiB + ''} + ${optionalString (flags != []) '' + vol_flags=${concatStringsSep "," flags} + ''} + ''); + + ubiImage = (volumes: + let + ubinizeConfig = pkgs.writeText "ubinize.conf" (concatStringsSep "\n" volumes); + inherit (pkgs.pkgsBuildBuild) mtdutils; + in + runCommand "ubinize" { + depsBuildBuild = [ mtdutils ]; + # block size := 128kb + # page size := 2048 + # ubninize opts := -E 5 + } '' + ubinize -Q "$SOURCE_DATE_EPOCH" -o $out \ + -p ${config.hardware.ubi.physicalEraseBlockSize} -m ${config.hardware.ubi.minIOSize} \ + -e ${config.hardware.ubi.logicalEraseBlockSize} \ + ${ubinizeConfig} + ''); + + ubiDisk = ({ initramfs }: + let + initramfsUbi = ubiVolume { + name = "rootfs"; + volumeId = 0; + image = initramfs; + flags = [ "autoresize" ]; + }; + in + ubiImage [ + initramfsUbi + ]); + + disk = ubiDisk { + initramfs = config.system.outputs.rootubifs; # liminix.builders.squashfs config.filesystem.contents; # # assert this is a proper FIT. + }; + + in + disk; + }; +}