commit c85b8c65decc3bd0325242b93c8e91553e7155f0
parent 1b0732e714547f9d3d3989669b3f3ae5eefd53ee
Author: torter <koubbamohamedrayan@gmail.com>
Date: Sat, 14 Nov 2026 14:55:07 +0000
iso: mkiso, mkinitramfs, init, installer and profile; booted in qemu with BIOS and UEFI
Diffstat:
10 files changed, 310 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,2 @@
+work/
+*.iso
diff --git a/handbook/handbook.md b/handbook/handbook.md
@@ -90,8 +90,32 @@ Check it on arrival: `sha256sum -c chamesle-kernel-6.18.63.sha256`.
## 4. The ISO
-One ISO boots on any machine and has oksh as the login shell and vim installed. It is a squashfs root file system
-made by `chamesle-bootstrap`, the kernel from chapter 3 and limine on top. The builder is not written yet.
+The ISO is an installer: it boots a small live system, and `chamesle-install` puts that on a disk. It boots on BIOS
+and UEFI machines with limine.
+
+ iso/mkiso -k chamesle-kernel-6.18.63.tar.xz -r $HOME/src/mag/repo:/usr/pkgsrc
+
+What it does, in order: `chamesle-bootstrap` builds a root from `iso/profile/packages`, the files of
+`iso/profile/overlay` are copied over it, the kernel tarball from chapter 3 is unpacked into it, `mkinitramfs`
+makes the initrd (a few static tools, the modules in `iso/profile/modules` with what they depend on, `iso/init`),
+the root becomes a zstd squashfs, and `xorriso` writes the ISO with limine's BIOS and UEFI boot images.
+`-R dir` uses a root you already have instead of building one (it is copied, not changed).
+
+At boot `init` looks for the medium that has `LiveOS/rootfs.sfs`, mounts it read-only, puts a tmpfs overlay over it,
+and switches into the result. The medium and the squashfs stay mounted under `/run/live/`, which is where the
+installer copies from. An installed system boots with `root=UUID=...` on the same initrd.
+
+`chamesle-install` asks for the disk, a host name and a root password, then makes a GPT with a 1M BIOS boot
+partition, a 512M EFI partition (also `/boot`) and the rest as ext4, copies the system, writes fstab and
+limine.conf and installs limine for both BIOS and UEFI.
+
+Try an ISO without a machine:
+
+ qemu-system-x86_64 -cpu max -m 1G -cdrom chamesle.iso -boot d
+ qemu-system-x86_64 -cpu max -m 1G -bios /usr/share/qemu/edk2-x86_64-code.fd -cdrom chamesle.iso # uefi
+
+mkiso needs `mksquashfs`, `xorriso` (or any mkisofs: set `MKISOFS`), `zstd`, `bsdcpio` and `limine` on the machine
+that builds. `LIMINE` says where limine's files are (default /usr/share/limine).
## 5. Files
diff --git a/iso/init b/iso/init
@@ -0,0 +1,54 @@
+#!/bin/sh
+# the first process in the initramfs. mounts the medium or the root named on the command line and hands over.
+export PATH=/bin
+mount -t proc proc /proc
+mount -t sysfs sysfs /sys
+mount -t devtmpfs devtmpfs /dev
+
+while read -r ko; do insmod "$ko" 2>/dev/null; done < /modules
+
+read -r cmdline < /proc/cmdline
+for w in $cmdline; do
+ case $w in
+ root=UUID=*) uuid=${w#root=UUID=};;
+ root=*) dev=${w#root=};;
+ esac
+done
+
+fail() { echo "init: $*"; echo "init: dropping to a shell"; exec sh; }
+
+mkdir -p /mnt/root
+
+if [ -n "$uuid$dev" ]; then
+ n=0
+ while [ $n -lt 15 ]; do
+ [ -n "$uuid" ] && dev=$(blkid -U "$uuid" 2>/dev/null)
+ [ -b "$dev" ] && break
+ sleep 1; n=$((n + 1))
+ done
+ [ -b "$dev" ] || fail "root device not found"
+ mount -o ro "$dev" /mnt/root || fail "cannot mount $dev"
+else
+ mkdir -p /mnt/medium /mnt/lower /mnt/rw
+ n=0
+ found=
+ while [ -z "$found" ] && [ $n -lt 20 ]; do
+ for d in /dev/sr* /dev/sd* /dev/nvme*n*p* /dev/vd* /dev/mmcblk*; do
+ [ -b "$d" ] || continue
+ mount -o ro "$d" /mnt/medium 2>/dev/null || continue
+ [ -f /mnt/medium/LiveOS/rootfs.sfs ] && { found=$d; break; }
+ umount /mnt/medium
+ done
+ [ -n "$found" ] || { sleep 1; n=$((n + 1)); }
+ done
+ [ -n "$found" ] || fail "no medium with LiveOS/rootfs.sfs"
+ mount -t squashfs -o loop,ro /mnt/medium/LiveOS/rootfs.sfs /mnt/lower || fail "cannot mount the squashfs"
+ mount -t tmpfs tmpfs /mnt/rw
+ mkdir -p /mnt/rw/up /mnt/rw/work
+ mount -t overlay overlay -o lowerdir=/mnt/lower,upperdir=/mnt/rw/up,workdir=/mnt/rw/work /mnt/root || fail "cannot mount the overlay"
+ mkdir -p /mnt/root/run/live/medium /mnt/root/run/live/rootfs
+ mount --move /mnt/medium /mnt/root/run/live/medium
+ mount --move /mnt/lower /mnt/root/run/live/rootfs
+fi
+
+exec switch_root /mnt/root /sbin/init
diff --git a/iso/mkinitramfs b/iso/mkinitramfs
@@ -0,0 +1,38 @@
+#!/bin/sh
+# mkinitramfs [-m modules-file] ROOTFS KVER OUT: a cpio of the few tools init needs, the modules named in the
+# modules file with what they depend on, and init itself.
+set -e
+here=$(cd "$(dirname "$0")" && pwd)
+while getopts m: o; do case $o in m) list=$OPTARG;; *) exit 2;; esac; done
+shift $((OPTIND - 1))
+[ $# = 3 ] || { echo "usage: mkinitramfs [-m modules] ROOTFS KVER OUT" >&2; exit 2; }
+root=$1 kver=$2 out=$3
+tmp=$(mktemp -d)
+trap 'rm -rf "$tmp"' EXIT
+mkdir -p "$tmp"/bin "$tmp"/dev "$tmp"/proc "$tmp"/sys "$tmp"/run "$tmp"/mnt "$tmp"/lib/modules
+for t in sh mount umount switch_root insmod mkdir sleep blkid; do
+ f=
+ for d in usr/bin bin usr/sbin sbin; do [ -x "$root/$d/$t" ] && { f=$root/$d/$t; break; }; done
+ [ -n "$f" ] || { echo "mkinitramfs: no $t in $root" >&2; exit 1; }
+ cp "$f" "$tmp/bin/$t"
+ ldd "$f" 2>/dev/null | sed -n 's|.*=> \(/[^ ]*\) .*|\1|p; s|^[[:space:]]*\(/[^ ]*\) (.*|\1|p' | while read -r l; do
+ [ -f "$root$l" ] && src=$root$l || src=$l
+ mkdir -p "$tmp$(dirname "$l")"
+ cp -L "$src" "$tmp$l"
+ done
+done
+: > "$tmp/modules"
+if [ -n "$list" ]; then
+ grep -v '^#' "$list" | while read -r m; do
+ [ -n "$m" ] || continue
+ modprobe -d "$root" -S "$kver" --show-depends "$m" 2>/dev/null | sed -n 's/^insmod //p'
+ done | awk '!seen[$0]++' | while read -r ko; do
+ ko=${ko#$root}
+ mkdir -p "$tmp$(dirname "$ko")"
+ cp "$root$ko" "$tmp$ko"
+ echo "$ko" >> "$tmp/modules"
+ done
+fi
+cp "$here/init" "$tmp/init"
+chmod 755 "$tmp/init"
+(cd "$tmp" && find . | bsdcpio -o -H newc 2>/dev/null) | zstd -19 -q -o "$out" -f
diff --git a/iso/mkiso b/iso/mkiso
@@ -0,0 +1,65 @@
+#!/bin/sh
+# mkiso: builds the Chamesle installer ISO from a profile.
+# mkiso -k kernel.tar.xz -r $HOME/src/mag/repo:/usr/pkgsrc
+# mkiso -R /srv/rootfs -k kernel.tar.xz -o chamesle.iso
+set -e
+here=$(cd "$(dirname "$0")" && pwd)
+profile=$here/profile
+work=$PWD/work
+out=chamesle.iso
+while getopts p:r:k:R:w:o: o; do
+ case $o in
+ p) profile=$OPTARG;;
+ r) MAG_REPO=$OPTARG;;
+ k) kernel=$OPTARG;;
+ R) rootfs=$OPTARG;;
+ w) work=$OPTARG;;
+ o) out=$OPTARG;;
+ *) echo "usage: mkiso [-p profile] [-r repos] [-k kernel.tar.xz] [-R rootfs] [-w workdir] [-o out.iso]" >&2; exit 2;;
+ esac
+done
+export MAG_REPO
+: "${MKISOFS:=xorriso -as mkisofs}"
+: "${LIMINE:=/usr/share/limine}"
+die() { echo "mkiso: $*" >&2; exit 1; }
+[ -n "$kernel" ] || die "no kernel: build one with kernel/build.sh and give it with -k"
+[ -d "$LIMINE" ] && [ -f "$LIMINE/limine-bios-cd.bin" ] || die "no limine files in $LIMINE (set LIMINE)"
+for t in mksquashfs xorriso zstd bsdcpio limine; do command -v $t >/dev/null || die "$t is not installed"; done
+
+iso=$work/iso
+mkdir -p "$work"
+rm -rf "$iso"
+mkdir -p "$iso/boot/limine" "$iso/EFI/BOOT" "$iso/LiveOS"
+
+if [ -z "$rootfs" ]; then
+ rootfs=$work/rootfs
+ rm -rf "$rootfs"
+ "$here/../tools/chamesle-bootstrap" -s "$(grep -v '^#' "$profile/packages" | tr '\n' ' ')" -n chamesle-live "$rootfs"
+else
+ rm -rf "$work/rootfs"
+ cp -a "$rootfs" "$work/rootfs"
+ rootfs=$work/rootfs
+fi
+
+tar -C "$profile/overlay" -cf - . | tar -C "$rootfs" -xf -
+xz -dc "$kernel" | tar -C "$rootfs" -xf -
+vmlinuz=$(ls "$rootfs"/boot/vmlinuz-* | head -n 1)
+kver=$(ls "$rootfs/lib/modules" | head -n 1)
+[ -f "$vmlinuz" ] && [ -n "$kver" ] || die "the kernel tarball has no boot/vmlinuz-* or lib/modules/*"
+cp "$vmlinuz" "$iso/boot/vmlinuz"
+
+"$here/mkinitramfs" -m "$profile/modules" "$rootfs" "$kver" "$iso/boot/initrd"
+
+rm -f "$iso/LiveOS/rootfs.sfs"
+mksquashfs "$rootfs" "$iso/LiveOS/rootfs.sfs" -comp zstd -Xcompression-level 19 -b 256K -noappend -no-progress >/dev/null
+
+cp "$LIMINE"/limine-bios.sys "$LIMINE"/limine-bios-cd.bin "$LIMINE"/limine-uefi-cd.bin "$iso/boot/limine/"
+cp "$LIMINE"/BOOTX64.EFI "$iso/EFI/BOOT/"
+cp "$profile/limine.conf" "$iso/boot/limine/limine.conf"
+
+$MKISOFS -R -r -J -V CHAMESLE \
+ -b boot/limine/limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table \
+ --efi-boot boot/limine/limine-uefi-cd.bin -efi-boot-part --efi-boot-image --protective-msdos-label \
+ -o "$out" "$iso" 2>/dev/null
+limine bios-install "$out" >/dev/null
+echo "$out: $(du -h "$out" | cut -f1)"
diff --git a/iso/profile/limine.conf b/iso/profile/limine.conf
@@ -0,0 +1,7 @@
+timeout: 3
+
+/Chamesle installer
+ protocol: linux
+ path: boot():/boot/vmlinuz
+ module_path: boot():/boot/initrd
+ cmdline: quiet
diff --git a/iso/profile/modules b/iso/profile/modules
@@ -0,0 +1,21 @@
+# modules the initramfs loads to find the medium or the root, with what they depend on
+squashfs
+overlay
+loop
+isofs
+sr_mod
+cdrom
+sd_mod
+usb_storage
+uas
+xhci_pci
+ehci_pci
+ahci
+nvme
+virtio_blk
+virtio_pci
+virtio_scsi
+ext4
+vfat
+nls_cp437
+nls_iso8859_1
diff --git a/iso/profile/overlay/etc/motd b/iso/profile/overlay/etc/motd
@@ -0,0 +1,3 @@
+
+ Chamesle. run chamesle-install to put it on a disk.
+
diff --git a/iso/profile/overlay/usr/bin/chamesle-install b/iso/profile/overlay/usr/bin/chamesle-install
@@ -0,0 +1,76 @@
+#!/bin/sh
+# chamesle-install: partitions a disk, copies the system on it, sets up limine.
+set -e
+live=/run/live/rootfs
+die() { echo "chamesle-install: $*" >&2; exit 1; }
+[ "$(id -u)" = 0 ] || die "run it as root"
+[ -d $live ] || die "this is not the live system"
+
+echo "disks:"
+lsblk -dno NAME,SIZE,MODEL | grep -v '^\(loop\|sr\|ram\)'
+printf 'install on which disk (name, like sda or nvme0n1)? '
+read -r d
+disk=/dev/$d
+[ -b "$disk" ] || die "$disk is not a disk"
+printf 'host name [chamesle]: '
+read -r name
+name=${name:-chamesle}
+printf 'root password: '
+stty -echo 2>/dev/null; read -r pw; stty echo 2>/dev/null; echo
+[ -n "$pw" ] || die "empty password"
+printf 'everything on %s will be erased. type yes to go on: ' "$disk"
+read -r ok
+[ "$ok" = yes ] || die "stopped"
+
+case $disk in *[0-9]) p=${disk}p;; *) p=$disk;; esac
+sfdisk -q "$disk" <<P
+label: gpt
+size=1M, type=21686148-6449-6E6F-744E-656564454649
+size=512M, type=U
+type=L
+P
+sleep 1
+mkfs.fat -F32 -n CHAMESLE "${p}2" >/dev/null
+mkfs.ext4 -q -F -L chamesle "${p}3"
+
+t=/mnt/target
+mkdir -p $t
+mount "${p}3" $t
+mkdir -p $t/boot
+mount "${p}2" $t/boot
+echo "copying the system..."
+tar -C $live -cf - . | tar -C $t -xf -
+mkdir -p $t/proc $t/sys $t/dev $t/run $t/tmp
+chmod 1777 $t/tmp
+
+root_uuid=$(blkid -s UUID -o value "${p}3")
+esp_uuid=$(blkid -s UUID -o value "${p}2")
+echo "$name" > $t/etc/hostname
+cat > $t/etc/fstab <<P
+UUID=$root_uuid / ext4 defaults 0 1
+UUID=$esp_uuid /boot vfat defaults 0 2
+proc /proc proc defaults 0 0
+sysfs /sys sysfs defaults 0 0
+tmpfs /tmp tmpfs mode=1777 0 0
+P
+hash=$(openssl passwd -1 "$pw")
+sed -i "s|^root:[^:]*:|root:$hash:|" $t/etc/passwd
+rm -f $t/etc/motd
+
+cp /run/live/medium/boot/vmlinuz $t/boot/vmlinuz
+cp /run/live/medium/boot/initrd $t/boot/initrd
+mkdir -p $t/boot/EFI/BOOT
+cp /run/live/medium/EFI/BOOT/BOOTX64.EFI $t/boot/EFI/BOOT/
+cp /run/live/medium/boot/limine/limine-bios.sys $t/boot/
+cat > $t/boot/limine.conf <<P
+timeout: 3
+
+/Chamesle
+ protocol: linux
+ path: boot():/vmlinuz
+ module_path: boot():/initrd
+ cmdline: root=UUID=$root_uuid rw quiet
+P
+limine bios-install "$disk" >/dev/null
+umount $t/boot $t
+echo "done. take out the installer and reboot."
diff --git a/iso/profile/packages b/iso/profile/packages
@@ -0,0 +1,18 @@
+# what goes into the installer image
+sys-libs/musl
+dev-build/gmake
+app-arch/libarchive
+app-arch/pigz
+app-shells/dash
+app-shells/oksh
+sys-apps/dinit
+sys-apps/util-linux
+sys-apps/kmod
+sys-apps/eudev
+net-misc/dhcpcd
+net-misc/curl
+sys-fs/e2fsprogs
+sys-fs/dosfstools
+sys-boot/limine
+sys-firmware/wireless-regdb
+editors/vim