commit cc3cfee247a0091c7fc683a1d3dcaa79988dbe4d
parent 09a1351b2f117dee46be1745e8a55bfc35b027f2
Author: MikoĊaj Lenczewski <mblenczewski@gmail.com>
Date: Tue, 9 Apr 2024 12:51:03 +0000
Add bsd-init, start generating sysroot archive, slightly refactor config
Diffstat:
8 files changed, 247 insertions(+), 24 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -2,4 +2,6 @@ sources/
crosstools/
clfs/
+clfs.tar
+
**/.*.swp
diff --git a/README.md b/README.md
@@ -0,0 +1,3 @@
+# clfs
+
+A project to produce a linux system capable of compiling itself, from sources.
diff --git a/clfs.sh b/clfs.sh
@@ -15,6 +15,10 @@ SYSROOT="$CLFS/sysroot"
INITRAMFS="$CLFS/initramfs"
BOOTROOT="$CLFS/bootroot"
+KERNEL="linuxstub.efi"
+INITRD="initrd.cpio"
+ROOTFS="sysroot.tar"
+
. "$TARGETS/$TARGET-config.sh"
set -e
diff --git a/dist.sh b/dist.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+set -ex
+
+tar cf clfs.tar \
+ .editorconfig \
+ README.md \
+ TODO.md \
+ clean.sh \
+ dist.sh \
+ clfs-env.sh \
+ clfs.sh \
+ stage1.sh \
+ stage2.sh \
+ stage3.sh \
+ stage4.sh \
+ targets/
diff --git a/stage3.sh b/stage3.sh
@@ -20,6 +20,221 @@ cd $WORK/$IANA_ETC_DIR
cp protocols services $SYSROOT/etc/
+# TODO: build packages that would allow clfs to become self-hosting
+# --
+# llvm (clang + lld)
+# curl
+# GNU tar
+# GNU make
+# cmake
+# TODO: all linux kernel build-time deps
+#
+# TODO: build packages that would make using clfs nicer
+# GNU bash
+# meson
+# git
+
+## NOTE: the remainder of stage3.sh simply configures the expected files under
+## $SYSROOT/etc and friends for the clfs system, no new binary packages will
+## be installed past this point
+
+# generate bsd-style init system
+# --
+# https://www.linuxfromscratch.org/hints/downloads/files/bsd-init.txt
+
+cat > $SYSROOT/etc/inittab <<'EOF'
+# /etc/inittab
+
+id:2:initdefault:
+
+si:S:sysinit:/etc/rc.d/rc.sysinit
+
+l0:0:wait:/etc/rc.d/rc.0 # shutdown
+l1:1:wait:/etc/rc.d/rc.1 # single user
+l2:2:wait:/etc/rc.d/rc.2 # multi user
+l3:3:wait:/etc/rc.d/rc.3 # linked to rc.1
+l4:4:wait:/etc/rc.d/rc.4 # linked to rc.1
+l5:5:wait:/etc/rc.d/rc.5 # linked to rc.1
+l6:6:wait:/etc/rc.d/rc.6 # reboot, linked to rc.0
+
+ca:12345:ctrlaltdel:/sbin/shutdown -r now
+
+su:S1:respawn:/sbin/sulogin
+
+c1:2345:respawn:/sbin/agetty tty1 38400 linux
+c2:2345:respawn:/sbin/agetty tty2 38400 linux
+c3:2345:respawn:/sbin/agetty tty3 38400 linux
+c4:2345:respawn:/sbin/agetty tty4 38400 linux
+c5:2345:respawn:/sbin/agetty tty5 38400 linux
+c6:2345:respawn:/sbin/agetty tty6 38400 linux
+EOF
+
+mkdir -p $SYSROOT/etc/rc.d
+
+cat > $SYSROOT/etc/rc.d/rc.sysinit <<'EOF'
+#!/bin/sh
+
+echo "Mounting root device read-only..."
+/bin/mount -n -o remount,ro /
+
+/sbin/fsck -A -a -C
+if [ $? -gt 1 ]; then
+ echo
+ echo "ERROR:"
+ echo "Your filesystem has been severely damaged. Dropping to rescue shell."
+ echo "After repair is complete, the system will reboot."
+ export PS1="(fsck)# "
+ /sbin/sulogin
+ /bin/umount -a -r
+ /sbin/reboot -f
+fi
+
+echo "Remounting root device read-write..."
+/bin/mount -n -o remount,rw /
+
+echo "Mounting /dev ..."
+/bin/mount -t devtmpfs none /dev
+
+echo "Mounting /proc ..."
+/bin/mount -t proc none /proc
+
+echo "mounting /sys ..."
+/bin/mount -t sysfs none /sys
+
+echo "Mounting other local filesystems..."
+/bin/mount -a -V -tnonfs
+
+echo "Setting hostname..."
+/bin/hostname $(cat /etc/hostname | cut -d . -f1)
+/bin/domainname $(cat /etc/hostname | cut -d . -f2-)
+
+echo "Removing stale PID files..."
+/bin/rm /var/run/*.pid
+/bin/rm /etc/dhcpc/*.pid
+
+echo "Loading keymap..."
+/usr/bin/loadkeys -d
+
+echo "Setting system time from hardware clock..."
+/sbin/hwclock --hctosys --utc
+
+echo "Starting system and kernel log daemons..."
+/usr/sbin/sysklogd
+/usr/sbin/klogd -c3
+
+echo "Updating module dependencies..."
+/sbin/depmod -a
+
+echo "Starting daemons..."
+if [ -x /etc/rc.d/rc.daemons ]; then
+ /etc/rc.d/rc.daemons
+fi
+EOF
+
+# reboot runlevel
+cat > $SYSROOT/etc/rc.d/rc.0 <<'EOF'
+#!/bin/sh
+
+echo "Sending all processes the TERM signal..."
+/sbin/killall5 -15
+sleep 1
+
+echo "Sending all processes the KILL signal..."
+/sbin/killall5 -9
+sleep 1
+
+echo "Saving the system time to hardware clock..."
+/sbin/hwclock --systohc --utc
+
+echo "Unmounting remote filesystems..."
+/bin/umount -a -f -tnfs
+
+case "$0" in
+ *6) /sbin/reboot -w ;;
+ *0) /sbin/halt -w ;;
+esac
+
+echo "Remounting root filesystem read-only..."
+/bin/mount -n -o remount,ro /
+
+echo "Flushing filesystem buffers..."
+/bin/sync
+
+echo "Umounting local filesystems..."
+/bin/umount -a -t nonfs
+
+case "$0" in
+ *6)
+ echo "Rebooting..."
+ /sbin/reboot -d -f -i
+ ;;
+ *0)
+ echo "Shutting down..."
+ /sbin/halt -d -f -p
+ ;;
+esac
+EOF
+
+ln -s rc.0 $SYSROOT/etc/rc.d/rc.6
+
+# single-user runlevel
+cat > $SYSROOT/etc/rc.d/rc.1 <<'EOF'
+#!/bin/sh
+
+echo "Unmounting remote filesystems..."
+/bin/umount -a -tnfs
+
+# TODO: bring down network interfaces
+#/sbin/ifconfig eth0 down
+
+echo "Sending all processes the TERM signal..."
+/sbin/killall5 -15
+sleep 1
+
+echo "Sending all processes the KILL signal..."
+/sbin/killall5 -9
+EOF
+
+# multi-user runlevel
+cat > $SYSROOT/etc/rc.d/rc.2 << 'EOF'
+#!/bin/sh
+
+echo "Setting up loopback networking..."
+/sbin/ifconfig lo 127.0.0.1
+/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo
+
+echo "Setting up eth0..."
+/sbin/ifconfig eth0 192.168.0.1 broadcast 192.168.0.255 netmask 255.255.255.0
+/sbin/route add -net default gw 192.168.0.1 netmask 0.0.0.0
+
+echo "Mounting remote filesystems..."
+/bin/mount -a -v -tnfs
+
+if [ -x /etc/rc.d/rc.local ]; then
+ /etc/rc.d/rc.local
+fi
+EOF
+
+ln -s rc.2 $SYSROOT/etc/rc.d/rc.3
+ln -s rc.2 $SYSROOT/etc/rc.d/rc.4
+ln -s rc.2 $SYSROOT/etc/rc.d/rc.5
+
+cat > $SYSROOT/etc/rc.d/rc.daemons <<'EOF'
+#!/bin/sh
+EOF
+
+cat > $SYSROOT/etc/rc.d/rc.local <<'EOF'
+#!/bin/sh
+EOF
+
+chmod 754 \
+ $SYSROOT/etc/rc.d/rc.sysinit \
+ $SYSROOT/etc/rc.d/rc.0 \
+ $SYSROOT/etc/rc.d/rc.1 \
+ $SYSROOT/etc/rc.d/rc.2 \
+ $SYSROOT/etc/rc.d/rc.daemons \
+ $SYSROOT/etc/rc.d/rc.local
+
# fixup sysroot directories
touch $SYSROOT/var/log/lastlog
chmod 664 $SYSROOT/var/log/lastlog
@@ -59,24 +274,6 @@ nogroup:x:65533:
nobody:x:65534:
EOF
-# generate default /etc/inittab
-cat > $SYSROOT/etc/inittab <<EOF
-# /etc/inittab
-# TODO: https://www.linuxfromscratch.org/hints/downloads/files/bsd-init.txt
-
-::sysinit:/etc/rc.d/startup
-
-tty1::respawn:/sbin/getty 38400 tty1
-tty2::respawn:/sbin/getty 38400 tty2
-tty3::respawn:/sbin/getty 38400 tty3
-tty4::respawn:/sbin/getty 38400 tty4
-tty5::respawn:/sbin/getty 38400 tty5
-tty6::respawn:/sbin/getty 38400 tty6
-
-::shutdown:/etc/rc.d/shutdown
-::ctrlaltdel:/sbin/reboot
-EOF
-
# generate default /etc/fstab
cat > $SYSROOT/etc/fstab <<EOF
# file-system mount-point type options dump fsck
@@ -111,4 +308,8 @@ export HISTFILESIZE=1000
#export EDITOR=/usr/bin/vim
EOF
+# build sysroot archive
+echo "Building $TARGET sysroot tar archive..."
+( cd $SYSROOT ; find . | tar -cf $BOOTROOT/$ROOTFS -T - )
+
echo "Built $TARGET userland!"
diff --git a/stage4.sh b/stage4.sh
@@ -80,7 +80,7 @@ chmod +x $INITRAMFS/init
# build cpio archive
echo "Building $TARGET initrd cpio archive..."
-( cd $INITRAMFS ; find . | cpio -o -H newc --quiet ) > $BOOTROOT/$INITRD
+( cd $INITRAMFS ; find . | cpio -o -H newc --quiet > $BOOTROOT/$INITRD )
# build linux-kernel
echo "Building $TARGET $LINUX_DIR kernel..."
diff --git a/targets/aarch64-linux-musl-config.sh b/targets/aarch64-linux-musl-config.sh
@@ -11,11 +11,6 @@ MESON_CPU="aarch64"
MESON_ENDIAN="little"
LINUX_ARCH="arm64"
-
-# NOTE: LLVM=1 is needed if your hostcc is clang
LINUX_HOSTCC_OPTS="
$(cc --version | grep -q clang && echo "LLVM=1")
"
-
-KERNEL=kernel8.img
-INITRD=initrd.cpio
diff --git a/targets/aarch64-linux-musl-qemu.sh b/targets/aarch64-linux-musl-qemu.sh
@@ -4,6 +4,7 @@ set -ex
KERNEL="$BOOTROOT/$KERNEL"
INITRD="$BOOTROOT/$INITRD"
+ROOTFS="$BOOTROOT/$ROOTFS" # TODO: unused, write this to a temporary disk
QEMU_OPTS="
-kernel $KERNEL