From a9d847e2c0a76ff01cb9eb026237cc227e2d8618 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 6 Nov 2023 21:52:31 +0000 Subject: [PATCH] add ext4 as rootfsType --- modules/base.nix | 7 +++++- modules/ext4fs.nix | 45 ++++++++++++++++++++++++++++++++++++ tests/ci.nix | 3 ++- tests/ext4/configuration.nix | 19 +++++++++++++++ tests/ext4/script.expect | 11 +++++++++ tests/ext4/test.nix | 19 +++++++++++++++ 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 modules/ext4fs.nix create mode 100644 tests/ext4/configuration.nix create mode 100644 tests/ext4/script.expect create mode 100644 tests/ext4/test.nix diff --git a/modules/base.nix b/modules/base.nix index 67d6ed7..75bb3b3 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -39,7 +39,12 @@ in { }; rootfsType = mkOption { default = "squashfs"; - type = types.enum ["squashfs" "jffs2" "ubifs"]; + type = types.enum [ + "ext4" + "jffs2" + "squashfs" + "ubifs" + ]; }; boot = { commandLine = mkOption { diff --git a/modules/ext4fs.nix b/modules/ext4fs.nix new file mode 100644 index 0000000..6a8c7c8 --- /dev/null +++ b/modules/ext4fs.nix @@ -0,0 +1,45 @@ +{ + config +, pkgs +, lib +, ... +}: +let + inherit (lib) mkIf mkOption types; +in +{ + imports = [ + ./initramfs.nix + ]; + config = mkIf (config.rootfsType == "ext4") { + kernel.config = { + EXT4_FS = "y"; + EXT4_USE_FOR_EXT2 = "y"; + FS_ENCRYPTION = "y"; + }; + boot.initramfs.enable = true; + system.outputs = rec { + systemConfiguration = + pkgs.systemconfig config.filesystem.contents; + rootfs = + let + inherit (pkgs.pkgsBuildBuild) runCommand e2fsprogs; + in runCommand "mkfs.ext4" { + depsBuildBuild = [ e2fsprogs ]; + } '' + mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets + cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate + ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init + mkdir -p $TMPDIR/empty/nix/store + for path in $(cat ${systemConfiguration}/etc/nix-store-paths) ; do + (cd $TMPDIR/empty && cp -a $path .$path) + done + size=$(du -s --apparent-size --block-size 1024 $TMPDIR/empty |cut -f1) + # add 25% for filesystem overhead + size=$(( 5 * $size / 4)) + dd if=/dev/zero of=$out bs=1024 count=$size + mke2fs -t ext4 -j -d $TMPDIR/empty $out + ''; + }; + }; +} diff --git a/tests/ci.nix b/tests/ci.nix index 315112c..c84ae07 100644 --- a/tests/ci.nix +++ b/tests/ci.nix @@ -3,7 +3,8 @@ pseudofiles = import ./pseudofiles/test.nix; wlan = import ./wlan/test.nix; pppoe = import ./pppoe/test.nix; - jffs2 = import ./jffs2/test.nix; + jffs2 = import ./jffs2/test.nix; + ext4 = import ./ext4/test.nix; min-copy-closure = import ./min-copy-closure/test.nix; fennel = import ./fennel/test.nix; } diff --git a/tests/ext4/configuration.nix b/tests/ext4/configuration.nix new file mode 100644 index 0000000..258e2b3 --- /dev/null +++ b/tests/ext4/configuration.nix @@ -0,0 +1,19 @@ +{ config, pkgs, lib, ... } : +let + inherit (pkgs.pseudofile) dir symlink; +in { + imports = [ + ../../vanilla-configuration.nix + ../../modules/squashfs.nix + ../../modules/ext4fs.nix + ]; + config.rootfsType = "ext4"; + config.filesystem = dir { + hello = { + type = "f"; + uid = 7; + gid = 24; + file = "hello world"; + }; + }; +} diff --git a/tests/ext4/script.expect b/tests/ext4/script.expect new file mode 100644 index 0000000..4d78c5e --- /dev/null +++ b/tests/ext4/script.expect @@ -0,0 +1,11 @@ +set timeout 10 + +spawn socat unix-connect:vm/console - +send "\r\n" +expect "login:" { send "root\r\n" } +expect "#" +send "echo HELLO WORLD > /hello\r\n" +expect "#" +send "cat /hello\r\n" +expect 'HELLO WORLD' +close \ No newline at end of file diff --git a/tests/ext4/test.nix b/tests/ext4/test.nix new file mode 100644 index 0000000..f972583 --- /dev/null +++ b/tests/ext4/test.nix @@ -0,0 +1,19 @@ +{ + liminix +, nixpkgs +}: +let img = (import liminix { + device = import "${liminix}/devices/qemu/"; + liminix-config = ./configuration.nix; + }).outputs.vmroot; + pkgs = import { overlays = [(import ../../overlay.nix)]; }; +in pkgs.runCommand "check" { + nativeBuildInputs = with pkgs; [ + expect + socat + ] ; +} '' +mkdir vm +${img}/run.sh --background ./vm +expect ${./script.expect} >$out +''