forked from dan/liminix
1
0
Fork 0

extract common code to make root filesystem hierarchy

which is then used by the filesystem image creators (ubifs, ext4,
jffs2 etc)
This commit is contained in:
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 let
inherit (lib) mkIf mkOption types; inherit (lib) mkIf mkOption types;
o = config.system.outputs;
in in
{ {
imports = [ imports = [
@ -18,7 +19,7 @@ in
FS_ENCRYPTION = "y"; FS_ENCRYPTION = "y";
}; };
boot.initramfs.enable = true; boot.initramfs.enable = true;
system.outputs = rec { system.outputs = {
systemConfiguration = systemConfiguration =
pkgs.systemconfig config.filesystem.contents; pkgs.systemconfig config.filesystem.contents;
rootfs = rootfs =
@ -27,18 +28,16 @@ in
in runCommand "mkfs.ext4" { in runCommand "mkfs.ext4" {
depsBuildBuild = [ e2fsprogs ]; depsBuildBuild = [ e2fsprogs ];
} '' } ''
mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets cp -a ${o.rootfsFiles} tmp
cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate ${if config.boot.loader.extlinux.enable
ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init then "(cd tmp && ln -s ${o.extlinux} boot)"
mkdir -p $TMPDIR/empty/nix/store else ""
for path in $(cat ${systemConfiguration}/etc/nix-store-paths) ; do }
(cd $TMPDIR/empty && cp -a $path .$path) size=$(du -s --apparent-size --block-size 1024 tmp |cut -f1)
done
size=$(du -s --apparent-size --block-size 1024 $TMPDIR/empty |cut -f1)
# add 25% for filesystem overhead # add 25% for filesystem overhead
size=$(( 5 * $size / 4)) size=$(( 5 * $size / 4))
dd if=/dev/zero of=$out bs=1024 count=$size 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 let
inherit (lib) mkIf mkOption types; inherit (lib) mkIf mkOption types;
o = config.system.outputs;
in in
{ {
imports = [ imports = [
@ -22,7 +23,7 @@ in
JFFS2_CMODE_SIZE = "y"; JFFS2_CMODE_SIZE = "y";
}; };
boot.initramfs.enable = true; boot.initramfs.enable = true;
system.outputs = rec { system.outputs = {
systemConfiguration = systemConfiguration =
pkgs.systemconfig config.filesystem.contents; pkgs.systemconfig config.filesystem.contents;
rootfs = rootfs =
@ -33,11 +34,12 @@ in
in runCommand "make-jffs2" { in runCommand "make-jffs2" {
depsBuildBuild = [ mtdutils ]; depsBuildBuild = [ mtdutils ];
} '' } ''
mkdir -p $TMPDIR/empty/nix/store/ $TMPDIR/empty/secrets cp -a ${o.rootfsFiles} tmp
cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate ${if config.boot.loader.extlinux.enable
ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init then "(cd tmp && ln -s ${o.extlinux} boot)"
grafts=$(sed < ${systemConfiguration}/etc/nix-store-paths 's/^\(.*\)$/--graft \1:\1/g') else ""
mkfs.jffs2 --compression-mode=size ${endian} -e ${toString config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root $TMPDIR/empty --output $out $grafts --squash --faketime }
(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 let
inherit (lib) mkOption types concatStringsSep; inherit (lib) mkOption types concatStringsSep;
inherit (pkgs) liminix callPackage writeText; inherit (pkgs) liminix callPackage writeText;
o = config.system.outputs;
in in
{ {
imports = [ imports = [
@ -58,6 +59,13 @@ in
out what's in the image, which is nice if it's unexpectedly huge 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 { rootfs = mkOption {
type = types.package; type = types.package;
internal = true; internal = true;
@ -69,7 +77,6 @@ in
}; };
config = { config = {
system.outputs = rec { system.outputs = rec {
# tftpd = pkgs.buildPackages.tufted;
kernel = liminix.builders.kernel.override { kernel = liminix.builders.kernel.override {
inherit (config.kernel) config src extraPatchPhase; inherit (config.kernel) config src extraPatchPhase;
}; };
@ -87,6 +94,18 @@ in
inherit kernel; inherit kernel;
inherit dtb; 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); manifest = writeText "manifest.json" (builtins.toJSON config.filesystem.contents);
}; };
}; };

View File

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