support jffs2, with initramfs

the jffs2 filesystem contains only /nix/store and a script which is
run in early init (initramfs) and is responsible for recreating
"traditional" directories (/bin /etc/**/* /var &c) based on the
configuration.

this is tested only in qemu so far and could use some cleanup
This commit is contained in:
Daniel Barlow
2023-04-04 23:35:49 +01:00
parent 25d9da967c
commit 54a1ab3529
5 changed files with 206 additions and 3 deletions

79
modules/initramfs.nix Normal file
View File

@@ -0,0 +1,79 @@
{
config
, pkgs
, lib
, ...
}:
let
# inherit (lib) mkOption types concatStringsSep;
inherit (pkgs) runCommand callPackage writeText;
in
{
config = {
kernel.config.BLK_DEV_INITRD = "y";
outputs = {
initramfs =
let
bb = pkgs.busybox.override {
enableStatic = true;
enableMinimal = true;
enableAppletSymlinks = false;
extraConfig = ''
CONFIG_ASH y
CONFIG_LS y
CONFIG_LN y
CONFIG_CAT y
CONFIG_MOUNT y
CONFIG_PRINTF y
CONFIG_FEATURE_MOUNT_FLAGS y
CONFIG_FEATURE_MOUNT_VERBOSE y
CONFIG_ECHO y
CONFIG_CHROOT y
CONFIG_CHMOD y
CONFIG_MKDIR y
CONFIG_MKNOD y
CONFIG_SH_IS_ASH y
CONFIG_BASH_IS_NONE y
'';
};
slashinit = pkgs.writeScript "init" ''
#!/bin/sh
exec >/dev/console
echo IT MOVES
mount -t proc none /proc
mount -t jffs2 mtd0 /target/persist
mount -o bind /target/persist/nix /target/nix
sh /target/persist/activate /target
cd /target
mount -o bind /target /
exec chroot . /bin/init "$@"
'';
refs = pkgs.writeReferencesToFile bb;
gen_init_cpio = pkgs.pkgsBuildBuild.gen_init_cpio;
in runCommand "initramfs" {} ''
cat << SPECIALS | ${gen_init_cpio}/bin/gen_init_cpio /dev/stdin > $out
dir /proc 0755 0 0
dir /dev 0755 0 0
nod /dev/mtdblock0 0600 0 0 b 31 0
dir /target 0755 0 0
dir /target/persist 0755 0 0
dir /target/nix 0755 0 0
nod /dev/console 0600 0 0 c 5 1
dir /bin 0755 0 0
file /bin/busybox ${bb}/bin/busybox 0755 0 0
slink /bin/sh /bin/busybox 0755 0 0
slink /bin/echo /bin/busybox 0755 0 0
slink /bin/mount /bin/busybox 0755 0 0
slink /bin/chroot /bin/busybox 0755 0 0
slink /bin/chmod /bin/busybox 0755 0 0
slink /bin/ln /bin/busybox 0755 0 0
slink /bin/mkdir /bin/busybox 0755 0 0
slink /bin/mknod /bin/busybox 0755 0 0
slink /bin/printf /bin/busybox 0755 0 0
file /init ${slashinit} 0755 0 0
SPECIALS
'';
};
};
}

49
modules/jffs2.nix Normal file
View File

@@ -0,0 +1,49 @@
{
config
, pkgs
, lib
, ...
}:
let
inherit (lib) mkOption types concatStringsSep;
inherit (pkgs) liminix callPackage writeText closureInfo;
in
{
imports = [
./initramfs.nix
];
config = {
kernel.config.JFFS2_FS = "y";
outputs = rec {
systemConfiguration =
let inherit (pkgs.pkgsBuildBuild) systemconfig;
in systemconfig config.filesystem.contents;
jffs2fs =
let
inherit (pkgs.pkgsBuildBuild) runCommand systemconfig mtdutils;
sysconf = systemConfiguration;
in runCommand "make-jffs2" {
depsBuildBuild = [ mtdutils ];
} ''
mkdir -p $TMPDIR/empty/nix/store/
cp ${sysconf}/activate $TMPDIR/empty/activate
pkgClosure=${closureInfo { rootPaths = [ sysconf ]; }}
cp $pkgClosure/registration nix-path-registration
grafts=$(sed < $pkgClosure/store-paths 's/^\(.*\)$/--graft \1:\1/g')
mkfs.jffs2 --pad --big-endian --root $TMPDIR/empty --output $out $grafts --verbose
'';
jffs2boot =
let o = config.outputs; in
pkgs.runCommand "jffs2boot" {} ''
mkdir $out
cd $out
ln -s ${o.jffs2fs} rootfs
ln -s ${o.kernel} vmlinux
ln -s ${o.manifest} manifest
ln -s ${o.initramfs} initramfs
'';
};
};
}