commit 14975484046ca1b65ea4cd94219bf85d152f63f6 Author: Alex Denes Date: Fri Oct 6 19:35:44 2023 +0000 Initial commit diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..bc50ec6 --- /dev/null +++ b/init.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# nnd-init +# Adapted from mkinitfs of Alpine Linux Project + +sysroot=/sysroot +init=/sbin/init +init_args='' + +# Set-up basic utilities +/bin/busybox mkdir -p /usr/bin /usr/sbin /proc /sys /dev /tmp $sysroot +/bin/busybox --install -s +export PATH=/usr/bin:/bin:/usr/sbin:/sbin + +# Prepare /dev/null to void unwanted error messages if none are pre-provided by the kernel +[ -c /dev/null ] || mknod -m 666 /dev/null c 1 3 + +# Mount essential filesystems +mount -t devtmpfs -o exec,nosuid,mode=0755,size=2M devtmpfs /dev 2>/dev/null \ + || mount -t tmpfs -o exec,nosuid,mode=0755,size=2M tmpfs /dev + +# Prepare kmsg for earlyprintk support +[ -c /dev/kmsg ] || mknod -m 660 /dev/kmsg c 1 11 + +# Prepare pseudo-terminal for any early ttys +[ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2 +[ -d /dev/pts ] || mkdir -m 755 /dev/pts +mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts + +# Set-up rest of filesystems +# These should ideally be carried over in the true root via mount moving +mount -t sysfs -o noexec,nosuid,nodev sysfs /sys +mount -t proc -o noexec,nosuid,nodev,hidepid=2 proc /proc +mount -t tmpfs -o nosuid,nodev none /run + +# Provided mounts at this point: +# /dev +# /dev/pts +# /sys +# /proc +# /run + +exec sh diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..6d6c712 --- /dev/null +++ b/notes.md @@ -0,0 +1,11 @@ +After basic bootstrapping, the source for the rootfs needs to be found, via hardware coldplugging and if wanted, a device manager to extend what the kernel provides by default. + +This is provided by nlplug-findfs on Alpine Linux. + +Once the block is present it can be mounted, but care must be taken so that the filesystem is either autodetected or provided to the mount call. + +Once sysroot is mounted, all special filesystems that have been so far mounted and optionally everything else must be moved over to new root. + +switch_root claims to move /dev, /proc, /sys and /run but this is probably only true for the coreutils switch_root. Care should be taken that these mounts are moved manually ahead of time to ensure presence. + +switch_root must be exec'd to overtake PID 1 and continue the init in the true rootfs. diff --git a/switch b/switch new file mode 100644 index 0000000..963343f --- /dev/null +++ b/switch @@ -0,0 +1,21 @@ +# NOTE: switch_root MAY move the mounts itself and the busybox implementation doesn't, so we move them ourselves + +# Usual case +cat /proc/mounts | while read DEV DIR TYPE OPTS ; do + if [ "$DIR" != "/" -a "$DIR" != "/sysroot" -a -d "$DIR" ]; then + mkdir -p /sysroot/$DIR + mount -o move $DIR /sysroot/$DIR + fi +done +sync +exec switch_root /sysroot /sbin/init + +# Template +cat /proc/mounts | while read DEV DIR TYPE OPTS ; do + if [ "$DIR" != "/" -a "$DIR" != "$sysroot" -a -d "$DIR" ]; then + mkdir -p $sysroot/$DIR + mount -o move $DIR $sysroot/$DIR + fi +done +sync +exec switch_root "$sysroot" "$init" "$init_args"