forked from dan/liminix
1
0
Fork 0

rewrite systemconfig in C and link statically

systemconfig (a.k.a "activate") is run from the initramfs. Converting
it from a shell script to an executable means it doesn't depend on
there being a shell in the initramfs
This commit is contained in:
Daniel Barlow 2023-04-15 17:15:44 +01:00
parent c744ef8c17
commit 1cc0b13b57
3 changed files with 77 additions and 26 deletions

View File

@ -17,7 +17,7 @@ in
boot.initramfs.enable = true; boot.initramfs.enable = true;
outputs = rec { outputs = rec {
systemConfiguration = systemConfiguration =
pkgs.pkgsBuildBuild.systemconfig config.filesystem.contents; pkgs.systemconfig config.filesystem.contents;
rootfs = rootfs =
let let
inherit (pkgs.pkgsBuildBuild) runCommand mtdutils; inherit (pkgs.pkgsBuildBuild) runCommand mtdutils;
@ -27,7 +27,8 @@ in
depsBuildBuild = [ mtdutils ]; depsBuildBuild = [ mtdutils ];
} '' } ''
mkdir -p $TMPDIR/empty/nix/store/ mkdir -p $TMPDIR/empty/nix/store/
cp ${systemConfiguration}/activate $TMPDIR/empty/activate cp ${systemConfiguration}/bin/activate $TMPDIR/empty/activate
ln -s ${pkgs.s6-init-bin}/bin/init $TMPDIR/empty/init
pkgClosure=${closureInfo { pkgClosure=${closureInfo {
rootPaths = [ systemConfiguration ]; rootPaths = [ systemConfiguration ];
}} }}

View File

@ -1,13 +1,13 @@
# The ideal is that a Liminix system can boot with only the files in # The ideal is that a Liminix system can boot with only the files in
# /nix/store. This package generates a script that is run at early # /nix/store. This package generates a small program that is run at early
# boot (from the initramfs) to populate directories such as /etc, # boot (from the initramfs) to populate directories such as /etc,
# /bin, /home according to whatever the configuration says # /bin, /home according to whatever the configuration says
# they should contain # they should contain
{ {
writeText writeText
, runCommand
, lib , lib
, stdenv
}: }:
let let
inherit (lib.attrsets) mapAttrsToList; inherit (lib.attrsets) mapAttrsToList;
@ -32,33 +32,46 @@ let
assert gid == 0; assert gid == 0;
let let
pathname = "${prefix}/${filename}"; pathname = "${prefix}/${filename}";
chmod = qpathname = builtins.toJSON pathname;
let m = if mode != null then mode else mode' = if mode != null
(if type == "d" then "0755" else "0644"); then mode
in (if type == "s" else
then "" (if type == "d" then "0755" else "0644");
else "\nchmod ${m} ${pathname}");
cmds = { cmds = {
"f" = "printf \"${escaped file}\" > ${pathname}"; "f" = "PRINTFILE(${qpathname}, ${mode'}, ${builtins.toJSON (escaped file)});";
"d" = "mkdir ${pathname}\n" + "d" = "MKDIR(${qpathname}, ${mode'});\n" +
(builtins.concatStringsSep "\n" (builtins.concatStringsSep "\n"
(visit pathname contents)); (visit pathname contents));
"c" = "mknod ${pathname} c ${major} ${minor}"; "c" = "MKNOD_C(${qpathname}, ${mode'}, ${major}, ${minor});";
"b" = "mknod ${pathname} b ${major} ${minor}"; "b" = "MKNOD_B(${qpathname}, ${mode'}, ${major}, ${minor});";
"s" = "ln -s ${target} ${pathname}"; "s" = "LN_S(${builtins.toJSON target}, ${qpathname});";
"l" = "ln ${target} ${pathname}"; "l" = "LN(${builtins.toJSON target}, ${qpathname})";
"i" = "mknod ${pathname} p"; "i" = "MKNOD_P(${qpathname}, ${mode'});";
}; };
cmd = cmds.${type}; cmd = cmds.${type};
in "${cmd}${chmod}"; in "${cmd}";
in mapAttrsToList (makeFile prefix) attrset; in mapAttrsToList (makeFile prefix) attrset;
activateScript = attrset: writeText "systemConfig" '' activateScript = attrset: writeText "makedevs.c" ''
#!/bin/sh #include "defs.h"
t=$1 int main(int argc, char* argv[]) {
${(builtins.concatStringsSep "\n" (visit "$t" attrset))} chdir(argv[1]);
${(builtins.concatStringsSep "\n" (visit "." attrset))}
}
''; '';
in attrset: in attrset:
runCommand "make-stuff" {} '' stdenv.mkDerivation {
mkdir -p $out name="make-stuff";
ln -s ${activateScript attrset} $out/activate src = ./.;
''
CFLAGS = "-Os";
LDFLAGS = "-static";
postConfigure = ''
cp ${activateScript attrset} makedevs.c
'';
makeFlags = ["makedevs"];
installPhase = ''
mkdir -p $out/bin
$STRIP --remove-section=.note --remove-section=.comment --strip-all makedevs -o $out/bin/activate
'';
}

37
pkgs/systemconfig/defs.h Normal file
View File

@ -0,0 +1,37 @@
#include <sys/stat.h>
#include <unistd.h>
#include <sys/sysmacros.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
void print_file(char * path, mode_t mode, char * text) {
int fd = open(path, O_CREAT | O_WRONLY, mode);
char *p, *nxt;
char b[1];
if(fd >=0) {
p = text;
while(nxt = strchr(p, '\\')) {
char upper = nxt[2];
char lower = nxt[3];
upper = (upper>'9') ? ((upper | 32) - 'a' + 10) : (upper - '0');
lower = (lower>'9') ? ((lower | 32) - 'a' + 10) : (lower - '0');
b[0] = (upper << 4) + lower;
write(fd, p, nxt-p);
write(fd, b, 1);
p=nxt+4;
}
write(fd, p, strlen(p));
close(fd);
}
}
#define PRINTFILE(path, mode, text) print_file(path, (mode_t) mode, text)
#define MKDIR(path, mode) mkdir(path, mode)
#define MKNOD_C(path, mode, major,minor) mknod(path, mode | S_IFCHR, makedev(major, minor))
#define MKNOD_B(path, mode, major,minor) mknod(path, mode | S_IFBLK, makedev(major, minor))
#define LN_S(target, path) (void)symlink(target, path)
#define LN(target, path) link(target, path)
#define MKNOD_P(path, mode) mkfifo(path, mode)