chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

kernel.sh (7368B)


      1 #!/bin/bash
      2 # our kernel is cachyos's linux-cachyos-lts, repacked as apks: chamesle-kernel-cachyos and chamesle-kernel-cachyos-headers
      3 #   tools/kernel/kernel.sh            the newest linux-cachyos-lts cachyos has
      4 # why cachyos's: tuned for desktops (full preemption, 1000 Hz, sched_ext), built for any x86-64 cpu, and on a crash
      5 # a blue screen with a qr code that phone cameras open as a readable report. the same lts series void ships, so
      6 # out-of-tree drivers (nvidia, broadcom-wl through dkms) build the same way. a kernel doesn't care about the libc
      7 # the packages call void's kernel hooks like void's kernel does: initramfs (dracut), dkms and the boot menu.
      8 # void's own kernel stays installed below it in the boot menu, for when this one doesn't start
      9 set -euo pipefail
     10 HERE=$(cd "$(dirname "$0")" && pwd)
     11 TOP=$HERE/../..
     12 APK=${APK:-apk}
     13 REPO=${REPO:-$TOP/work/repo}
     14 CACHE=${CACHE:-$TOP/work/kernel}
     15 KEY=$(ls "$HOME"/.config/chamesle/chamesle-*.rsa | sort | tail -n1)
     16 MIRROR=${MIRROR:-https://mirror.cachyos.org/repo/x86_64/cachyos}
     17 # the cachyos packaging key, as their cachyos-keyring lists it trusted
     18 FPR=882DCFE48E2051D48E2562ABF3B607488DB35A47
     19 ARCH=x86_64
     20 mkdir -p "$CACHE" "$REPO/$ARCH"
     21 
     22 # a big file in 6 pieces at once: one connection is slow on a phone hotspot
     23 fetch() {
     24     local url=$1 out=$2 n size step i s e
     25     [ -f "$out" ] && return
     26     size=$(curl -sIL "$url" | tr -d '\r' | awk 'tolower($1)=="content-length:"{n=$2} END{print n}')
     27     [ -n "$size" ] || { echo "kernel: can't reach $url" >&2; exit 1; }
     28     n=6 step=$(( (size + 5) / 6 ))
     29     for i in $(seq 0 $((n - 1))); do
     30         s=$((i * step)) e=$((i * step + step - 1)); [ $e -lt $size ] || e=$((size - 1))
     31         ( for try in 1 2 3 4 5 6 7 8; do
     32             curl -fsSL --range $s-$e -o "$out.$i" "$url" && [ "$(stat -c%s "$out.$i")" = $((e - s + 1)) ] && break
     33             sleep 3
     34           done ) &
     35     done
     36     wait
     37     for i in $(seq 0 $((n - 1))); do cat "$out.$i"; done > "$out.new"
     38     rm -f "$out".[0-9]
     39     [ "$(stat -c%s "$out.new")" = "$size" ] || { echo "kernel: $url came down broken" >&2; exit 1; }
     40     mv "$out.new" "$out"
     41 }
     42 
     43 verify() {
     44     local f=$1 g
     45     g=$(mktemp -d)
     46     GNUPGHOME=$g gpg -q --keyserver hkps://keyserver.ubuntu.com --recv-keys $FPR 2>/dev/null
     47     GNUPGHOME=$g gpg --status-fd 1 --verify "$f.sig" "$f" 2>/dev/null | grep -q "^\[GNUPG:\] VALIDSIG $FPR " ||
     48         { rm -rf "$g"; echo "kernel: $f isn't signed by cachyos" >&2; exit 1; }
     49     rm -rf "$g"
     50     echo "$(basename "$f"): signed by cachyos"
     51 }
     52 
     53 curl -fsSL -o "$CACHE/cachyos.db" "$MIRROR/cachyos.db"
     54 DB=$CACHE/db; rm -rf "$DB"; mkdir -p "$DB"; bsdtar -xf "$CACHE/cachyos.db" -C "$DB"
     55 for p in linux-cachyos-lts linux-cachyos-lts-headers; do
     56     d=$(ls -d "$DB/$p"-[0-9]* | sort -V | tail -n1)
     57     file=$(sed -n '/^%FILENAME%$/{n;p}' "$d/desc")
     58     fetch "$MIRROR/$file" "$CACHE/$file"
     59     [ -f "$CACHE/$file.sig" ] || curl -fsSL -o "$CACHE/$file.sig" "$MIRROR/$file.sig"
     60     verify "$CACHE/$file"
     61     eval "${p//-/_}=\$CACHE/\$file"
     62 done
     63 
     64 W=$TOP/work/kernel/build
     65 rm -rf "$W"; mkdir -p "$W/k" "$W/h" "$W/e" "$W/scripts"
     66 bsdtar -xf "$linux_cachyos_lts" -C "$W/k"
     67 bsdtar -xf "$linux_cachyos_lts_headers" -C "$W/h"
     68 KV=$(ls "$W/k/usr/lib/modules")                               # 6.18.50-1-cachyos-lts
     69 ver=$(echo "$KV" | sed 's/-\([0-9]*\)-cachyos-lts$/-r\1/')     # 6.18.50-r1
     70 rm -f "$W"/k/.[A-Z]* "$W"/h/.[A-Z]*
     71 # where void's kernels are and the hooks look: /boot/vmlinuz-VERSION, the config next to it
     72 mkdir -p "$W/k/boot"
     73 mv "$W/k/usr/lib/modules/$KV/vmlinuz" "$W/k/boot/vmlinuz-$KV"
     74 cp "$W/h/usr/lib/modules/$KV/build/.config" "$W/k/boot/config-$KV"
     75 rm -f "$W/k/usr/lib/modules/$KV/pkgbase" "$W/h/usr/lib/modules/$KV/build/../pkgbase"
     76 rm -rf "$W/h/usr/src" "$W/h/usr/share/doc"
     77 # modules.dep and friends in the package: an image built without running scripts can still load modules
     78 depmod -b "$W/k/usr" "$KV" 2>/dev/null || depmod -b "$W/k" "$KV"
     79 
     80 hooks() {  # hooks TARGET [VERSION]: what void's kernel package runs, for this kernel
     81     echo "( cd / && kernel_hooks_version=${2:-$KV} /usr/libexec/xbps-triggers/kernel-hooks run $1 chamesle-kernel ${2:-$KV} no )"
     82 }
     83 # boot line and boot menu, before the hooks write the menu: the crash screen is the qr code, and this kernel
     84 # is the first entry even when void's kernel has a higher version
     85 grubsettings() {
     86     cat <<P
     87 g=/etc/default/grub
     88 if [ -f \$g ]; then
     89     sed -i 's/drm\.panic_screen=kmsg/drm.panic_screen=qr_code/' \$g
     90     grep -q 'drm.panic_screen=' \$g || sed -i 's/^\(GRUB_CMDLINE_LINUX_DEFAULT="[^"]*\)"/\1 drm.panic_screen=qr_code"/' \$g
     91     sed -i '/^GRUB_TOP_LEVEL=/d' \$g
     92     echo 'GRUB_TOP_LEVEL=/boot/vmlinuz-$KV' >> \$g
     93 fi
     94 P
     95 }
     96 # a removed version leaves modules (dkms, depmod's files) and its initramfs behind, without its vmlinuz:
     97 # those go, and its menu entry with them
     98 cleanup() {
     99     cat <<'P'
    100 for f in /usr/lib/modules/*-lts /boot/initramfs-*-lts.img; do
    101     [ -e "$f" ] || continue
    102     v=${f##*/}; v=${v#initramfs-}; v=${v%.img}
    103     [ -e "/boot/vmlinuz-$v" ] && continue
    104     rm -rf "/usr/lib/modules/$v" "/boot/initramfs-$v.img"
    105     ( cd / && kernel_hooks_version=$v /usr/libexec/xbps-triggers/kernel-hooks run post-remove chamesle-kernel $v no )
    106 done
    107 P
    108 }
    109 cat > "$W/scripts/post-install" <<EOF
    110 #!/bin/sh
    111 $(grubsettings)
    112 depmod -a $KV
    113 $(hooks post-install)
    114 exit 0
    115 EOF
    116 cat > "$W/scripts/post-upgrade" <<EOF
    117 #!/bin/sh
    118 $(grubsettings)
    119 depmod -a $KV
    120 $(hooks post-install)
    121 $(cleanup)
    122 exit 0
    123 EOF
    124 cat > "$W/scripts/pre-deinstall" <<EOF
    125 #!/bin/sh
    126 sed -i '/^GRUB_TOP_LEVEL=/d' /etc/default/grub 2>/dev/null
    127 $(hooks pre-remove)
    128 exit 0
    129 EOF
    130 cat > "$W/scripts/post-deinstall" <<EOF
    131 #!/bin/sh
    132 rm -f /boot/initramfs-$KV.img
    133 $(hooks post-remove)
    134 exit 0
    135 EOF
    136 # chamesle-kernel was arch's linux-lts up to 0.5: now empty, it brings the cachyos one and clears arch's away
    137 cat > "$W/scripts/moved" <<EOF
    138 #!/bin/sh
    139 $(cleanup)
    140 exit 0
    141 EOF
    142 
    143 pack() {  # pack NAME VERSION DIR DESC DEPENDS SCRIPTS...
    144     local name=$1 v=$2 dir=$3 desc=$4 deps=$5 info s; shift 5
    145     info=(--info "name:$name" --info "version:$v" --info "arch:$ARCH" --info "description:$desc"
    146           --info "url:https://chamesle.org" --info "license:GPL-2.0-only"
    147           --info "maintainer:Chamesle Linux <packages@chamesle.org>" --info "depends:$deps")
    148     for s in "$@"; do info+=(--script "${s%%=*}:$W/scripts/${s#*=}"); done
    149     unshare --map-root-user --map-auto "$APK" mkpkg --files "$dir" --output "$REPO/$ARCH/$name-$v.apk" \
    150         --sign-key "$KEY" "${info[@]}"
    151     echo "built $REPO/$ARCH/$name-$v.apk"
    152 }
    153 chmod 755 "$W"/scripts/*
    154 # headers first in the depends: dkms builds drivers in the kernel's post-install, against them
    155 pack chamesle-kernel-cachyos-headers $ver "$W/h" "Headers to build drivers for the Chamesle kernel" "pahole make"
    156 pack chamesle-kernel-cachyos $ver "$W/k" "The Chamesle kernel: CachyOS's Linux $KV, with a blue screen and a qr code on a crash" \
    157     "chamesle-kernel-cachyos-headers=$ver kmod dracut linux-firmware" \
    158     post-install=post-install post-upgrade=post-upgrade pre-deinstall=pre-deinstall post-deinstall=post-deinstall
    159 # above arch's last one, 6.18.58-r1, so upgrades take it
    160 pack chamesle-kernel-headers 6.18.58-r2 "$W/e" "Moved to chamesle-kernel-cachyos-headers" "chamesle-kernel-cachyos-headers"
    161 pack chamesle-kernel 6.18.58-r2 "$W/e" "Moved to chamesle-kernel-cachyos" "chamesle-kernel-cachyos" post-upgrade=moved