extract common code to make root filesystem hierarchy

which is then used by the filesystem image creators (ubifs, ext4,
jffs2 etc)
pull/2/head
Daniel Barlow 2023-12-05 17:29:01 +00:00
parent f08c10c8ba
commit ed925588f7
4 changed files with 46 additions and 28 deletions

View File

@ -6,6 +6,7 @@
}:
let
inherit (lib) mkIf mkOption types;
o = config.system.outputs;
in
{
imports = [
@ -18,7 +19,7 @@ in
FS_ENCRYPTION = "y";
};
boot.initramfs.enable = true;
system.outputs = rec {
system.outputs = {
systemConfiguration =
pkgs.systemconfig config.filesystem.contents;
rootfs =
@ -27,18 +28,16 @@ in
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)
cp -a ${o.rootfsFiles} tmp
${if config.boot.loader.extlinux.enable
then "(cd tmp && ln -s ${o.extlinux} boot)"
else ""
}
size=$(du -s --apparent-size --block-size 1024 tmp |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
mke2fs -t ext4 -j -d tmp $out
'';
};
};

View File

@ -6,6 +6,7 @@
}:
let
inherit (lib) mkIf mkOption types;
o = config.system.outputs;
in
{
imports = [
@ -22,7 +23,7 @@ in
JFFS2_CMODE_SIZE = "y";
};
boot.initramfs.enable = true;
system.outputs = rec {
system.outputs = {
systemConfiguration =
pkgs.systemconfig config.filesystem.contents;
rootfs =
@ -33,11 +34,12 @@ in
in runCommand "make-jffs2" {
depsBuildBuild = [ mtdutils ];
} ''
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
grafts=$(sed < ${systemConfiguration}/etc/nix-store-paths 's/^\(.*\)$/--graft \1:\1/g')
mkfs.jffs2 --compression-mode=size ${endian} -e ${toString config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root $TMPDIR/empty --output $out $grafts --squash --faketime
cp -a ${o.rootfsFiles} tmp
${if config.boot.loader.extlinux.enable
then "(cd tmp && ln -s ${o.extlinux} boot)"
else ""
}
(cd tmp && mkfs.jffs2 --compression-mode=size ${endian} -e ${toString config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root . --output $out --squash --faketime )
'';
};
};

View File

@ -7,6 +7,7 @@
let
inherit (lib) mkOption types concatStringsSep;
inherit (pkgs) liminix callPackage writeText;
o = config.system.outputs;
in
{
imports = [
@ -58,6 +59,13 @@ in
out what's in the image, which is nice if it's unexpectedly huge
'';
};
rootfsFiles = mkOption {
type = types.package;
internal = true;
description = ''
directory of files to package into root filesystem
'';
};
rootfs = mkOption {
type = types.package;
internal = true;
@ -69,7 +77,6 @@ in
};
config = {
system.outputs = rec {
# tftpd = pkgs.buildPackages.tufted;
kernel = liminix.builders.kernel.override {
inherit (config.kernel) config src extraPatchPhase;
};
@ -87,6 +94,18 @@ in
inherit kernel;
inherit dtb;
};
rootfsFiles =
let
inherit (pkgs.pkgsBuildBuild) runCommand;
in runCommand "mktree" { } ''
mkdir -p $out/nix/store/ $out/secrets $out/boot
cp ${o.systemConfiguration}/bin/activate $out/activate
ln -s ${pkgs.s6-init-bin}/bin/init $out/init
mkdir -p $out/nix/store
for path in $(cat ${o.systemConfiguration}/etc/nix-store-paths) ; do
(cd $out && cp -a $path .$path)
done
'';
manifest = writeText "manifest.json" (builtins.toJSON config.filesystem.contents);
};
};

View File

@ -6,6 +6,7 @@
}:
let
inherit (lib) mkIf mkOption types;
o = config.system.outputs;
in
{
imports = [
@ -20,12 +21,11 @@ in
config = mkIf (config.rootfsType == "ubifs") {
kernel.config = {
MTD_UBI="y";
UBIFS_FS = "y";
UBIFS_FS_SECURITY = "n";
};
boot.initramfs.enable = true;
system.outputs = rec {
system.outputs = {
systemConfiguration =
pkgs.systemconfig config.filesystem.contents;
rootfs =
@ -35,15 +35,13 @@ in
in runCommand "mkfs.ubifs" {
depsBuildBuild = [ mtdutils ];
} ''
mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets $TMPDIR/empty/boot
cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate
ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init
cp ${config.system.outputs.uimage} $TMPDIR/empty/boot/uimage
mkdir -p $TMPDIR/empty/nix/store
for path in $(cat ${systemConfiguration}/etc/nix-store-paths) ; do
(cd $TMPDIR/empty && cp -a $path .$path)
done
mkfs.ubifs -x favor_lzo -c ${cfg.maxLEBcount} -m ${cfg.minIOSize} -e ${cfg.eraseBlockSize} -y -r $TMPDIR/empty --output $out --squash-uids -o $out
mkdir tmp
cp -a ${o.rootfsFiles} tmp
${if config.boot.loader.extlinux.enable
then "(cd tmp && ln -s ${o.extlinux} boot)"
else ""
}
mkfs.ubifs -x favor_lzo -c ${cfg.maxLEBcount} -m ${cfg.minIOSize} -e ${cfg.eraseBlockSize} -y -r tmp --output $out --squash-uids -o $out
'';
};
};