LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
#!/bin/sh
# /usr/lib/lordos/immutable/update — PROTOTYPE A/B updater of the immutable LordOS image
# (docs/IMMUTABLE.md). It follows systemd-sysupdate's on-disk conventions (Debian 13 does not ship
# systemd-sysupdate): partition labels lordos_usr_<v> / lordos_usr_v_<v>, partition UUIDs derived
# from the dm-verity root hash, and a UKI lordos_<v>+<tries>.efi in the ESP for systemd-boot's
# boot counting. The product version would live in LordOS's own package engine (C), not in sh.
#
# update <base URL> install the newest signed version into the inactive slot
# update --status show the slots and the boot entries
set -eu
KEYRING=/usr/share/lordos/immutable/update-keyring.gpg
ESP=/efi
TRIES=3
T_USR=8484680c-9521-48c6-9c11-b0720656f69e
T_USR_VERITY=77ff5f63-e7b6-4633-acf4-1565b864c0e6
STATE=/var/lib/lordos/immutable
die() { echo "update: $*" >&2; exit 1; }
uuid_of() { echo "$1" | sed 's/^\(.\{8\}\)\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)\(.\{12\}\).*/\1-\2-\3-\4-\5/'; }
[ "$(id -u)" = 0 ] || die "run as root"
# shellcheck source=/dev/null
CUR=$(. /usr/lib/os-release; echo "${IMAGE_VERSION:-}")
[ -n "$CUR" ] || die "not an immutable LordOS image (no IMAGE_VERSION)"
CURHASH=$(sed -n 's/.*usrhash=\([0-9a-f]\{64\}\).*/\1/p' /proc/cmdline)
[ -n "$CURHASH" ] || die "booted without usrhash="
CURU=$(uuid_of "$CURHASH")
ROOTDEV=$(findmnt -no SOURCE /)
DISK=/dev/$(lsblk -no PKNAME "$ROOTDEV")
# the usr data / verity partitions in disk order: slot A first, slot B second
parts() { lsblk -nrpo NAME,PARTTYPE,PARTUUID,PARTLABEL "$DISK" | awk -v t="$1" '$2 == t'; }
partnum() { cat "/sys/class/block/$(basename "$1")/partition"; }
status() {
echo "running: image version $CUR, /usr = $(findmnt -no SOURCE,FSTYPE,OPTIONS /usr | cut -d, -f1)"
i=0
parts $T_USR | while read -r n _ u l; do
i=$((i + 1)); slot=$(echo AB | cut -c$i)
mark=""; [ "$u" = "$CURU" ] && mark=" <- running"
echo "slot $slot: $n label=$l$mark"
done
echo "boot entries (ESP $ESP/EFI/Linux):"
find "$ESP/EFI/Linux" -maxdepth 1 -name '*.efi' -printf ' %f\n' | sort
}
[ "${1:-}" = --status ] && { status; exit 0; }
URL=${1:?usage: update <base URL> | --status}
mkdir -p "$STATE"; W="$STATE/download"; rm -rf "$W"; mkdir -p "$W"
echo "checking $URL"
curl -fsS -o "$W/SHA256SUMS" "$URL/SHA256SUMS" || die "cannot download SHA256SUMS"
curl -fsS -o "$W/SHA256SUMS.gpg" "$URL/SHA256SUMS.gpg" || die "cannot download SHA256SUMS.gpg"
gpgv --keyring "$KEYRING" "$W/SHA256SUMS.gpg" "$W/SHA256SUMS" >/dev/null 2>&1 \
|| die "SHA256SUMS: BAD or missing signature, nothing installed"
echo "signature: good (update key)"
NEW=$(sed -n 's/^[0-9a-f]\{64\} lordos_\([0-9]\{1,9\}\)\.efi$/\1/p' "$W/SHA256SUMS" | sort -n | tail -1)
[ -n "$NEW" ] || die "no version listed"
[ "$NEW" -gt "$CUR" ] || { echo "up to date (running $CUR, newest $NEW)"; exit 0; }
# a version that already used up its boot tries on this machine is not installed again
for bad in "$ESP"/EFI/Linux/lordos_"$NEW"+0-*.efi; do
[ -e "$bad" ] && { echo "version $NEW could not start on this computer (all tries failed); staying on $CUR until a newer version is published"; exit 0; }
done
echo "installing version $NEW (running $CUR)"
for f in lordos_usr_$NEW.erofs lordos_usr_$NEW.verity lordos_usr_$NEW.roothash lordos_$NEW.efi; do
grep -q " $f\$" "$W/SHA256SUMS" || die "$f is not in the signed list"
curl -fsS -o "$W/$f" "$URL/$f" || die "cannot download $f"
done
(cd "$W" && grep -E " lordos_(usr_)?$NEW\." SHA256SUMS | sha256sum -c --quiet) || die "SHA-256 mismatch"
NEWHASH=$(cat "$W/lordos_usr_$NEW.roothash")
echo "$NEWHASH" | grep -qx '[0-9a-f]\{64\}' || die "bad root hash"
# the inactive slot: the usr partition that is not backing the running /usr
# shellcheck disable=SC2046 # four words: device and PARTUUID of each slot
set -- $(parts $T_USR | awk '{print $1, $3}')
[ $# -eq 4 ] || die "expected two usr slots"
if [ "$2" = "$CURU" ]; then DATA=$3; IDX=2; elif [ "$4" = "$CURU" ]; then DATA=$1; IDX=1; else die "running slot not found"; fi
HASHDEV=$(parts $T_USR_VERITY | awk -v i=$IDX 'NR == i {print $1}')
[ -b "$DATA" ] && [ -b "$HASHDEV" ] || die "inactive slot not found"
[ "$(stat -c %s "$W/lordos_usr_$NEW.erofs")" -le "$(blockdev --getsize64 "$DATA")" ] || die "image larger than the slot"
OLDLABEL=$(lsblk -no PARTLABEL "$DATA")
echo "writing slot $(echo AB | cut -c$IDX) ($DATA, was $OLDLABEL)"
# 1. invalidate the slot first, so a crash half-way never leaves a bootable-looking slot
sfdisk -q --no-reread --no-tell-kernel --part-label "$DISK" "$(partnum "$DATA")" _empty
sfdisk -q --no-reread --no-tell-kernel --part-label "$DISK" "$(partnum "$HASHDEV")" _empty
OLDV=$(echo "$OLDLABEL" | sed -n 's/^lordos_usr_\([0-9]*\)$/\1/p')
[ -n "$OLDV" ] && rm -f "$ESP"/EFI/Linux/lordos_"$OLDV".efi "$ESP"/EFI/Linux/lordos_"$OLDV"+*.efi
# 2. write and check the blocks against the signed root hash
dd if="$W/lordos_usr_$NEW.erofs" of="$DATA" bs=4M conv=fsync status=none
dd if="$W/lordos_usr_$NEW.verity" of="$HASHDEV" bs=4M conv=fsync status=none
veritysetup verify "$DATA" "$HASHDEV" "$NEWHASH" || die "written slot does not verify"
echo "slot verified against usrhash $NEWHASH"
# 3. name it: UUIDs from the root hash (how the UKI finds it), labels with the version
U1=$(uuid_of "$NEWHASH"); U2=$(uuid_of "$(echo "$NEWHASH" | cut -c33-64)")
sfdisk -q --no-reread --no-tell-kernel --part-uuid "$DISK" "$(partnum "$DATA")" "$U1"
sfdisk -q --no-reread --no-tell-kernel --part-uuid "$DISK" "$(partnum "$HASHDEV")" "$U2"
sfdisk -q --no-reread --no-tell-kernel --part-label "$DISK" "$(partnum "$DATA")" "lordos_usr_$NEW"
sfdisk -q --no-reread --no-tell-kernel --part-label "$DISK" "$(partnum "$HASHDEV")" "lordos_usr_v_$NEW"
partx -u --nr "$(partnum "$DATA")" "$DISK" 2>/dev/null || true
partx -u --nr "$(partnum "$HASHDEV")" "$DISK" 2>/dev/null || true
# 4. last: the boot entry, with TRIES boot attempts before systemd-boot gives up on it
cp "$W/lordos_$NEW.efi" "$ESP/EFI/Linux/.lordos_$NEW.efi.tmp"
sync
mv "$ESP/EFI/Linux/.lordos_$NEW.efi.tmp" "$ESP/EFI/Linux/lordos_$NEW+$TRIES.efi"
sync
rm -rf "$W"
echo "version $NEW installed; it boots next ($TRIES tries), version $CUR stays as the fallback."
echo "Restart to finish."