1
0

Compare commits

...

4 Commits

Author SHA1 Message Date
5dd0c6e3c0 rewrite preinit as very small C program
By using the kernel "nolibc" header to avoid requiring a C library, we
can bring the initramfs size to around 4k

This does involve a tiny bit of inline mips assembly which I'm not
sure about. gcc seems unwilling to generate the code to load $gp at
function entry of main(), so we do it by hand - but I'd rather find
out why gcc doesn't.
2023-04-15 18:27:39 +01:00
b1a89ae8c3 jffs2: squash uids, enable lzo compression 2023-04-15 17:23:58 +01:00
11f2715d18 mips-vm: enlarge mtd 2023-04-15 17:22:35 +01:00
1cc0b13b57 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
2023-04-15 17:21:27 +01:00
8 changed files with 215 additions and 98 deletions

View File

@ -16,85 +16,25 @@ in
};
};
config = mkIf config.boot.initramfs.enable {
kernel.config.BLK_DEV_INITRD = "y";
kernel.config.INITRAMFS_SOURCE = builtins.toJSON "${config.outputs.initramfs}";
kernel.config = {
BLK_DEV_INITRD = "y";
INITRAMFS_SOURCE = builtins.toJSON "${config.outputs.initramfs}";
# INITRAMFS_COMPRESSION_LZO = "y";
};
outputs = {
initramfs =
let
bb1 = pkgs.busybox.override {
enableStatic = true;
enableMinimal = true;
enableAppletSymlinks = false;
extraConfig = ''
CONFIG_DESKTOP n
CONFIG_ASH n
CONFIG_HUSH y
CONFIG_HUSH_TICK y
CONFIG_HUSH_LOOPS y
CONFIG_HUSH_CASE y
CONFIG_HUSH_ECHO y
CONFIG_HUSH_SET 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_BASH_IS_NONE y
CONFIG_SH_IS_NONE y
CONFIG_SH_IS_ASH n
CONFIG_FEATURE_SH_STANDALONE y
CONFIG_FEATURE_PREFER_APPLETS y
CONFIG_BUSYBOX_EXEC_PATH "/bin/busybox"
'';
};
bb = bb1.overrideAttrs(o: {
makeFlags = [];
});
slashinit = pkgs.writeScript "init" ''
#!/bin/hush
exec >/dev/console
echo Running in initramfs
mount -t proc none /proc
set -- `cat /proc/cmdline`
for i in "$@" ; do
case "''${i}" in
root=*)
rootdevice="''${i#root=}"
;;
esac
done
echo mount -t jffs2 ''${rootdevice} /target/persist
mount -t jffs2 ''${rootdevice} /target/persist
mount -o bind /target/persist/nix /target/nix
hush /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;
let inherit (pkgs.pkgsBuildBuild) gen_init_cpio;
in runCommand "initramfs.cpio" {} ''
cat << SPECIALS | ${gen_init_cpio}/bin/gen_init_cpio /dev/stdin > $out
dir /proc 0755 0 0
dir /dev 0755 0 0
nod /dev/console 0600 0 0 c 5 1
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/hush /bin/busybox 0755 0 0
slink /bin/chroot /bin/busybox 0755 0 0
file /init ${slashinit} 0755 0 0
file /init ${pkgs.preinit}/bin/preinit 0755 0 0
SPECIALS
'';
};

View File

@ -13,11 +13,18 @@ in
./initramfs.nix
];
config = mkIf (config.rootfsType == "jffs2") {
kernel.config.JFFS2_FS = "y";
kernel.config = {
JFFS2_FS = "y";
JFFS2_LZO = "y";
JFFS2_RTIME = "y";
JFFS2_COMPRESSION_OPTIONS = "y";
JFFS2_ZLIB = "y";
JFFS2_CMODE_SIZE = "y";
};
boot.initramfs.enable = true;
outputs = rec {
systemConfiguration =
pkgs.pkgsBuildBuild.systemconfig config.filesystem.contents;
pkgs.systemconfig config.filesystem.contents;
rootfs =
let
inherit (pkgs.pkgsBuildBuild) runCommand mtdutils;
@ -27,13 +34,14 @@ in
depsBuildBuild = [ mtdutils ];
} ''
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 {
rootPaths = [ systemConfiguration ];
}}
cp $pkgClosure/registration nix-path-registration
grafts=$(sed < $pkgClosure/store-paths 's/^\(.*\)$/--graft \1:\1/g')
mkfs.jffs2 ${endian} -e ${config.hardware.flash.eraseBlockSize} --pad --root $TMPDIR/empty --output $out $grafts
mkfs.jffs2 --compression-mode=size ${endian} -e ${config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root $TMPDIR/empty --output $out $grafts --squash --faketime
'';
jffs2boot =
let o = config.outputs; in

View File

@ -40,6 +40,7 @@
# https://sourceforge.net/p/squashfs/mailman/message/26599379/
lzma = callPackage ./lzma {};
preinit = callPackage ./preinit {};
swconfig = callPackage ./swconfig {};
openwrt = callPackage ./openwrt {};

View File

@ -30,7 +30,8 @@ fi
test -n "$2" || usage
rootfs=$(mktemp mips-vm-fs-XXXXXX)
dd if=$2 of=$rootfs bs=65536 conv=sync
dd if=/dev/zero of=$rootfs bs=1M count=16 conv=sync
dd if=$2 of=$rootfs bs=65536 conv=sync,nocreat,notrunc
if test -n "$3"; then
initramfs="-initrd $3"
@ -43,7 +44,7 @@ qemu-system-mips \
-M malta -m 256 \
-echr 16 \
-append "liminix default console=ttyS0,38400n8 panic=10 oops=panic init=$INIT loglevel=8 root=/dev/mtdblock0 block2mtd.block2mtd=/dev/vda,65536" \
-drive file=$rootfs,format=raw,readonly=on,if=virtio \
-drive file=$rootfs,format=raw,readonly=off,if=virtio,index=0 \
${initramfs} \
-netdev socket,id=access,mcast=230.0.0.1:1234,localaddr=127.0.0.1 \
-device virtio-net-pci,disable-legacy=on,disable-modern=off,netdev=access,mac=ba:ad:1d:ea:21:02 \

30
pkgs/preinit/default.nix Normal file
View File

@ -0,0 +1,30 @@
{
stdenv
, fetchzip
, gdb
}:
let kernel = fetchzip {
name = "linux";
url = "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.71.tar.gz";
hash = "sha256-pq6QNa0PJVeheaZkuvAPD0rLuEeKrViKk65dz+y4kqo=";
};
in
stdenv.mkDerivation {
name = "preinit";
src = ./.;
# NIX_DEBUG=2;
hardeningDisable = [ "all" ];
CFLAGS = "-Os -nostartfiles -nostdlib -fno-stack-protector -fpic -fPIC -I ./ -I ${kernel}/tools/include/nolibc";
postBuild = ''
$STRIP --remove-section=.note --remove-section=.comment preinit
'';
makeFlags = ["preinit"];
stripAllList = ["bin"];
installPhase = ''
mkdir -p $out/bin
cp preinit $out/bin
'';
}

87
pkgs/preinit/preinit.c Normal file
View File

@ -0,0 +1,87 @@
#ifdef USE_LIBC
#include <string.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <string.h>
#else
#include <nolibc.h>
#endif
#include <asm/setup.h>
#define ERR(x) write(2, x, strlen(x))
#define AVER(c) do { if(c < 0) ERR("failed: " #c); } while(0)
static int begins_with(char * str, char * prefix)
{
while(*prefix) {
if(*str == '\0') return 0;
if(*str != *prefix) return 0;
str++;
prefix++;
}
return 1;
}
static void fork_exec(char * command, char *args[])
{
int fork_pid = fork();
AVER(fork_pid);
if(fork_pid > 0)
wait(NULL);
else
AVER(execve(command, args, NULL));
}
char banner[] = "Running pre-init...\n";
char buf[COMMAND_LINE_SIZE];
int main(int argc, char *argv[], char *envp[])
{
asm("la $gp, _gp\nsw $gp,16($sp)");
char *rootdevice = 0;
char *p = buf;
write(1, banner, strlen(banner));
mount("none", "/proc", "proc", 0, NULL);
int cmdline = open("/proc/cmdline", O_RDONLY, 0);
if(cmdline>=0) {
int len = read(cmdline, buf, sizeof buf - 1);
buf[len]='\0';
write(1, "cmdline ", 8);
write(1, buf, len);
};
while(*p) {
if(begins_with(p, "root=")) {
rootdevice = p + 5;
while(*p && (*p != ' ')) p++;
*p= '\0';
}
while(*p && (*p != ' ')) p++;
p++;
}
if(rootdevice) {
write(1, "rootdevice ", 11);
write(1, rootdevice, strlen(rootdevice));
write(1, "\n", 1);
AVER(mount(rootdevice, "/target/persist", "jffs2", 0, NULL));
AVER(mount("/target/persist/nix", "/target/nix",
"bind", MS_BIND, NULL));
char *exec_args[] = { "activate", "/target" };
fork_exec("/target/persist/activate", exec_args);
AVER(chdir("/target"));
AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL));
AVER(chroot("."));
argv[0] = "init";
argv[1] = NULL;
AVER(execve("/persist/init", argv, envp));
}
}

View File

@ -1,13 +1,13 @@
# 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,
# /bin, /home according to whatever the configuration says
# they should contain
{
writeText
, runCommand
, lib
, stdenv
}:
let
inherit (lib.attrsets) mapAttrsToList;
@ -32,33 +32,46 @@ let
assert gid == 0;
let
pathname = "${prefix}/${filename}";
chmod =
let m = if mode != null then mode else
(if type == "d" then "0755" else "0644");
in (if type == "s"
then ""
else "\nchmod ${m} ${pathname}");
qpathname = builtins.toJSON pathname;
mode' = if mode != null
then mode
else
(if type == "d" then "0755" else "0644");
cmds = {
"f" = "printf \"${escaped file}\" > ${pathname}";
"d" = "mkdir ${pathname}\n" +
"f" = "PRINTFILE(${qpathname}, ${mode'}, ${builtins.toJSON (escaped file)});";
"d" = "MKDIR(${qpathname}, ${mode'});\n" +
(builtins.concatStringsSep "\n"
(visit pathname contents));
"c" = "mknod ${pathname} c ${major} ${minor}";
"b" = "mknod ${pathname} b ${major} ${minor}";
"s" = "ln -s ${target} ${pathname}";
"l" = "ln ${target} ${pathname}";
"i" = "mknod ${pathname} p";
"c" = "MKNOD_C(${qpathname}, ${mode'}, ${major}, ${minor});";
"b" = "MKNOD_B(${qpathname}, ${mode'}, ${major}, ${minor});";
"s" = "LN_S(${builtins.toJSON target}, ${qpathname});";
"l" = "LN(${builtins.toJSON target}, ${qpathname})";
"i" = "MKNOD_P(${qpathname}, ${mode'});";
};
cmd = cmds.${type};
in "${cmd}${chmod}";
in "${cmd}";
in mapAttrsToList (makeFile prefix) attrset;
activateScript = attrset: writeText "systemConfig" ''
#!/bin/sh
t=$1
${(builtins.concatStringsSep "\n" (visit "$t" attrset))}
activateScript = attrset: writeText "makedevs.c" ''
#include "defs.h"
int main(int argc, char* argv[]) {
chdir(argv[1]);
${(builtins.concatStringsSep "\n" (visit "." attrset))}
}
'';
in attrset:
runCommand "make-stuff" {} ''
mkdir -p $out
ln -s ${activateScript attrset} $out/activate
''
stdenv.mkDerivation {
name="make-stuff";
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)