Initial commit

This commit is contained in:
Alex D. 2023-10-06 19:35:44 +00:00
commit 1497548404
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 74 additions and 0 deletions

42
init.sh Executable file
View File

@ -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

11
notes.md Normal file
View File

@ -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.

21
switch Normal file
View File

@ -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"