diff --git a/openwrt/build-rootfs.sh b/openwrt/build-rootfs.sh index 45696947..e8ca12fa 100755 --- a/openwrt/build-rootfs.sh +++ b/openwrt/build-rootfs.sh @@ -5,7 +5,6 @@ # Inputs (same as rootfs/assemble.sh, all optional except modules): # out/modules/*.ko out/modules.builtin* firmware/ android-subset/ android-gpu-subset/ # tools/logdw/logdw tools/bt-init/mu300-bt-init tools/keys/mu300-keys tools/gpu/cltest busybox (static, full) -# xray, hev-socks5-tunnel (tools/fetch-xray.sh) and sing-box (tools/fetch-sing-box.sh), for mu300-vpn # upstream/out/modules/*.ko (optional: out-of-tree WCN modules for the mainline 6.18 kernel) set -eu FLAVOUR=${MU300_FLAVOUR:-openwrt} @@ -44,9 +43,9 @@ ls "$IN/out/modules"/*.ko >/dev/null 2>&1 || { echo "MU300_INPUTS at the directory that has out/modules, firmware/ and android-subset/." >&2 exit 1 } -# The optional ones decide whether the image can use the modem, Wi-Fi, the GPU or the VPN at all. Missing ones +# The optional ones decide whether the image can use the modem, Wi-Fi or the GPU at all. Missing ones # used to be skipped silently, which produces an image that boots and then does nothing useful. -for o in firmware android-subset android-gpu-subset tools/logdw/logdw tools/bt-init/mu300-bt-init tools/keys/mu300-keys tools/gpu/cltest busybox sing-box xray hev-socks5-tunnel upstream/out/modules; do +for o in firmware android-subset android-gpu-subset tools/logdw/logdw tools/bt-init/mu300-bt-init tools/keys/mu300-keys tools/gpu/cltest busybox upstream/out/modules; do [ -e "$IN/$o" ] && echo " + $o" || echo " - $o (missing: the image is built without it)" done # shellcheck disable=SC2046 @@ -56,7 +55,7 @@ docker run --rm --platform linux/arm64 \ $(opt out/modules.builtin modules.builtin) $(opt out/modules.builtin.modinfo modules.builtin.modinfo) \ $(opt firmware firmware) $(opt android-subset android-subset) $(opt android-gpu-subset android-gpu-subset) \ $(opt tools/logdw/logdw logdw) $(opt tools/bt-init/mu300-bt-init bt-init) $(opt tools/keys/mu300-keys keys) $(opt tools/gpu/cltest cltest) \ - $(opt busybox busybox) $(opt sing-box sing-box) $(opt xray xray) $(opt hev-socks5-tunnel hev-socks5-tunnel) $(opt upstream/out/modules mainline-modules) -v "$TOP/openwrt":/out -v "$REGDB":/in/regdb:ro \ + $(opt busybox busybox) $(opt upstream/out/modules mainline-modules) -v "$TOP/openwrt":/out -v "$REGDB":/in/regdb:ro \ -e KREL=$KREL -e OUT="$(basename "$OUT")" -e MU300_VERSION="${MU300_VERSION:-dev}" mu300-$FLAVOUR-base:$VER /bin/sh -eu -c ' mkdir -p /var/lock /var/run /tmp apk update >/dev/null @@ -102,8 +101,7 @@ fi [ -e /in/logdw ] && { cp /in/logdw $R/opt/mu300/bin/logdw; chmod 755 $R/opt/mu300/bin/logdw; } [ -e /in/bt-init ] && { cp /in/bt-init $R/opt/mu300/bin/mu300-bt-init; chmod 755 $R/opt/mu300/bin/mu300-bt-init; } [ -e /in/keys ] && { cp /in/keys $R/opt/mu300/bin/mu300-keys; chmod 755 $R/opt/mu300/bin/mu300-keys; } -[ -f /in/sing-box ] && { cp /in/sing-box $R/opt/mu300/bin/sing-box; chmod 755 $R/opt/mu300/bin/sing-box; } -for b in xray hev-socks5-tunnel; do [ -f /in/$b ] && { cp /in/$b $R/opt/mu300/bin/$b; chmod 755 $R/opt/mu300/bin/$b; }; done +# the VPN engines are not part of the image: they are the vpn extra (mu300-extra install vpn) # full static busybox for the tools OpenWrt busybox leaves out (od, timeout, mountpoint, losetup, rfkill, telnetd) if [ -e /in/busybox ]; then cp /in/busybox $R/opt/mu300/bin/busybox; chmod 755 $R/opt/mu300/bin/busybox @@ -124,8 +122,8 @@ for s in mu300-accounts mu300-vendor mu300-hw mu300-post mu300-toolkit mu300-atd n=$(sed -n "s/^START=//p" $R/etc/init.d/$s) ln -sf ../init.d/$s $R/etc/rc.d/S$n$s done -# busybox PATH is /usr/sbin:/usr/bin:/sbin:/bin, so the commands go into /usr/bin -for c in mu300-toolkit mu300-next-boot mu300-os mu300-update mobile-data mu300-at mu300-vpn wifi-client mu300-ttl mu300-wifi-band mu300-led mu300-usb mu300-nfc mu300-device; do ln -sf /opt/mu300/bin/$c $R/usr/bin/$c; done +# busybox PATH is /usr/sbin:/usr/bin:/sbin:/bin, so the commands go into /usr/bin (the same list as Ubuntu) +for c in $(cat /in/opt-mu300/lib/path-commands); do ln -sf /opt/mu300/bin/$c $R/usr/bin/$c; done # no kernel of its own: OpenWrt kmods (6.12) and grub are unused on this device rm -rf $R/lib/modules/6.* $R/boot # out-of-tree modules for the experimental mainline kernel (upstream/) diff --git a/openwrt/overlay/etc/init.d/mu300-post b/openwrt/overlay/etc/init.d/mu300-post index c4c29a3d..429e7aee 100755 --- a/openwrt/overlay/etc/init.d/mu300-post +++ b/openwrt/overlay/etc/init.d/mu300-post @@ -5,6 +5,9 @@ USE_PROCD=1 export PATH="$PATH:/opt/mu300/busybox-bin" start_service() { + # the commands on the PATH (/usr/bin): new ones on a system installed before they existed, and the extras' + /opt/mu300/bin/mu300-extra link >/dev/null 2>&1 + # the host gives up asking for a DHCP lease long before this device finishes booting; if nothing took an # address, make the gadget re-enumerate once, which looks like a fresh cable to the host procd_open_instance usb-lease diff --git a/rootfs/assemble.sh b/rootfs/assemble.sh index 286ba7f3..02dc0717 100755 --- a/rootfs/assemble.sh +++ b/rootfs/assemble.sh @@ -23,7 +23,7 @@ if [ -n "$miss" ]; then fi # Optional inputs decide whether the image can use the modem, Wi-Fi and the GPU at all. Missing ones used to # pass silently and produce an image that boots and does nothing useful, so say what went in. -for o in /firmware /android-subset /android-gpu-subset /bt-init /keys /cltest /sing-box /xray /hev-socks5-tunnel; do +for o in /firmware /android-subset /android-gpu-subset /bt-init /keys /cltest; do [ -e "$o" ] && echo "assemble.sh: + $o" || echo "assemble.sh: - $o (not mounted; the image will be built without it)" done @@ -56,10 +56,8 @@ if [ -e /cltest ]; then install -D -m755 /cltest $R/opt/mu300/android/system/bin if [ -e /bt-init ]; then cp /bt-init $R/opt/mu300/bin/mu300-bt-init; fi # tools/keys build (static arm64): the buttons (mu300-buttons) if [ -e /keys ]; then install -m755 /keys $R/opt/mu300/bin/mu300-keys; fi -# VLESS clients for mu300-vpn: the xray engine (tools/fetch-xray.sh) and sing-box (tools/fetch-sing-box.sh) -if [ -f /sing-box ]; then install -m755 /sing-box $R/opt/mu300/bin/sing-box; fi -if [ -f /xray ]; then install -m755 /xray $R/opt/mu300/bin/xray; fi -if [ -f /hev-socks5-tunnel ]; then install -m755 /hev-socks5-tunnel $R/opt/mu300/bin/hev-socks5-tunnel; fi +# the VPN engines (Xray, hev-socks5-tunnel, sing-box) are not part of the image: they are the vpn extra +# (tools/make-extra.sh, installed with mu300-extra install vpn) # accounts still the image's (ubuntu/ubuntu) until an installer or mu300-update puts the device's own in place : > $R/etc/.mu300-accounts-from-image for u in mu300-accounts.service:sysinit.target mu300-early-recorder.service:sysinit.target mu300-vendor.service:sysinit.target mu300-lan.service:multi-user.target mu300-ssh-hostkeys.service:sysinit.target mu300-telnetd.service:multi-user.target serial-getty@ttyGS0.service:getty.target ssh.socket:sockets.target mu300-wifi.service:multi-user.target mu300-wifi-client.service:multi-user.target mu300-mobile-data.service:multi-user.target mu300-mobile-data-watch.service:multi-user.target mu300-fixups.service:sysinit.target mu300-zram.service:swap.target mu300-boot-ok.service:multi-user.target mu300-cp_diskserver.service:multi-user.target mu300-refnotify.service:multi-user.target mu300-extra-modules.service:multi-user.target mu300-hotspot.service:multi-user.target mu300-bluetooth.service:multi-user.target mu300-thermal-guard.service:multi-user.target mu300-audio.service:multi-user.target mu300-firewall.service:sysinit.target mu300-kmsg.service:sysinit.target mu300-toolkit.service:multi-user.target mu300-atd.service:multi-user.target mu300-modem-log.service:multi-user.target mu300-buttons.service:multi-user.target systemd-networkd.service:multi-user.target; do @@ -70,8 +68,9 @@ for u in mu300-accounts.service:sysinit.target mu300-early-recorder.service:sysi ln -sfn $src $R/etc/systemd/system/$tgt.wants/$svc done # the commands people are told to run must be on PATH, including sudo's secure_path, which does not contain -# /opt/mu300/bin - without these links every "sudo mu300-os ..." in the README is a "command not found" -for c in mu300-toolkit mu300-next-boot mu300-os mu300-update mobile-data mu300-at mu300-vpn wifi-client mu300-ttl mu300-wifi-band mu300-led mu300-usb mu300-nfc mu300-device; do ln -sfn /opt/mu300/bin/$c $R/usr/local/bin/$c; done +# /opt/mu300/bin - without these links every "sudo mu300-os ..." in the README is a "command not found". One list +# for both images and for mu300-extra link, which adds them at boot on systems installed before a command had one. +for c in $(cat $R/opt/mu300/lib/path-commands); do ln -sfn /opt/mu300/bin/$c $R/usr/local/bin/$c; done # no graphical/serial login noise on a headless dongle; keep ttyS1 console for debugging ln -sfn /dev/null $R/etc/systemd/system/getty@tty1.service cd $R && tar --numeric-owner -czf /w/mu300-ubuntu-$UBUNTU-rootfs.tar.gz . diff --git a/rootfs/overlay/opt/mu300/bin/mu300-extra b/rootfs/overlay/opt/mu300/bin/mu300-extra new file mode 100755 index 00000000..2c0e0f39 --- /dev/null +++ b/rootfs/overlay/opt/mu300/bin/mu300-extra @@ -0,0 +1,152 @@ +#!/bin/sh +# Optional parts that are not in the images, downloaded when you want them (Ubuntu and OpenWrt). +# mu300-extra list what there is, and what is installed +# mu300-extra status the installed extras: release, size, commands +# mu300-extra install NAME download it from the release of this system, check it against the release's +# SHA256SUMS, install it and put its commands on the PATH +# mu300-extra remove NAME +# mu300-extra link put the system's commands and the extras' on the PATH (run at every boot) +# mu300-extra adopt vpn keep the VPN engines of the system an update replaced (.old) as the vpn extra +# +# Extras live on the Linux partition (/mnt/mu300-disk/extra/NAME), not in a system: Ubuntu and OpenWrt share one +# copy, and an update or reinstall of a system keeps it. mu300-update apply brings them to the new release. +# There is one so far: +# vpn Xray-core, hev-socks5-tunnel and sing-box, the engines of mu300-vpn +# +# MU300_RELEASE=TAG takes another release, MU300_RELEASE_URL=URL another server with the same files and SHA256SUMS, +# MU300_EXTRA_FILE=FILE a mu300-extra-NAME.tar.gz on this device (not checked: you chose it). +# The download, the checks and the installation are mu300-update's own (fetch_verified, extra_unpack). +BIN=${MU300_BIN:-/opt/mu300/bin} +_lib=${MU300_LIB:-} +MU300_LIB=1 +. "$BIN/mu300-update" +MU300_LIB=$_lib +STAGE=$DISK/.mu300-extra +OPT=${MU300_OPT:-/opt/mu300} +SYSROOT=${MU300_SYSROOT:-} + +# where commands go: a directory on everybody's PATH, sudo's secure_path included (Ubuntu keeps usr/local across +# updates; OpenWrt's busybox PATH has no /usr/local) +link_dir() { + if [ -n "${MU300_LINKDIR:-}" ]; then echo "$MU300_LINKDIR" + elif [ -e "$SYSROOT/etc/openwrt_release" ]; then echo /usr/bin + else echo /usr/local/bin; fi +} + +# link: the system's commands (one list, the same the images are built with) and every installed extra's. A file +# or a link of somebody else's is never replaced; a link into an extra that is gone is removed. +do_link() { + L=$(link_dir) + mkdir -p "$L" + if [ -r "$OPT/lib/path-commands" ]; then + for c in $(cat "$OPT/lib/path-commands"); do + [ -x "$OPT/bin/$c" ] || continue + [ -e "$L/$c" ] || [ -L "$L/$c" ] || ln -s "$OPT/bin/$c" "$L/$c" 2>/dev/null || true + done + fi + for x in $(extra_installed); do + for f in "$EXTRA/$x"/bin/*; do + [ -x "$f" ] || continue + c=${f##*/} + if [ -L "$L/$c" ]; then + case $(readlink "$L/$c") in "$EXTRA"/*) ln -sfn "$f" "$L/$c" ;; esac + elif [ ! -e "$L/$c" ]; then + ln -s "$f" "$L/$c" 2>/dev/null || true + fi + done + done + for l in "$L"/*; do + [ -L "$l" ] && [ ! -e "$l" ] || continue + case $(readlink "$l") in "$EXTRA"/*) rm -f "$l" ;; esac + done + return 0 +} + +need_root() { [ "$(id -u)" = 0 ] || die "run as root (sudo mu300-extra $*)"; } +need_disk() { [ -d "$DISK/ubuntu" ] || [ -d "$DISK/openwrt" ] || die "the Linux partition is not mounted at $DISK"; } +known() { extra_known "$1" || die "there is no extra called '$1' (mu300-extra list)"; } + +# the release whose extra goes with this system: MU300_RELEASE, the system's own, or the newest +system_release() { + v=$(cat "$SYSROOT/etc/mu300/image-version" 2>/dev/null) + case $v in v[0-9]*) echo "$v" ;; esac +} + +do_install() { + name=$1 + if [ -n "${MU300_EXTRA_FILE:-}" ]; then + [ -s "$MU300_EXTRA_FILE" ] || die "$MU300_EXTRA_FILE not found" + extra_unpack "$name" "$MU300_EXTRA_FILE" || die "$name was not installed" + else + tag=${MU300_RELEASE:-$(system_release)} + [ -n "$tag" ] || tag=$(latest_release) + [ -n "$tag" ] || die "could not reach GitHub (is the device online?)" + asset=$(extra_asset "$name") + say "downloading $asset of $tag" + fetch_verified "$tag" "$asset"; r=$? + # a system from before extras: its release has none, the newest one has + if [ "$r" = 2 ] && [ -z "${MU300_RELEASE:-}" ]; then + newest=$(latest_release) + if [ -n "$newest" ] && [ "$newest" != "$tag" ]; then + tag=$newest + say "downloading $asset of $tag" + fetch_verified "$tag" "$asset"; r=$? + fi + fi + [ "$r" = 0 ] || { rm -rf "$STAGE"; die "$name was not installed (no verified $asset of $tag)"; } + extra_unpack "$name" "$STAGE/$asset" || { rm -rf "$STAGE"; die "$name was not installed"; } + rm -rf "$STAGE" + fi + do_link + printf '%s %s installed in %s\n' "$name" "$(extra_release "$name")" "$EXTRA/$name" +} + +do_remove() { + [ -d "$EXTRA/$1" ] || { echo "$1 is not installed"; return 0; } + rm -rf "$EXTRA/$1" "$EXTRA/.$1.new" "$EXTRA/.$1.old" + sync + do_link + echo "$1 removed" +} + +running() { echo "${MU300_RUNNING_OS:-$(running_os)}"; } + +# adopt vpn: the engines of the image this system replaced (an update to a release without them in the image) +do_adopt() { + [ -d "$EXTRA/$1/bin" ] && { echo "$1 is installed already"; return 0; } + extra_adopt "$1" "$DISK/$(running).old" || return 1 + do_link +} + +do_list() { + for x in $EXTRAS; do + if [ -d "$EXTRA/$x/bin" ]; then st="installed ($(extra_release "$x"))"; else st="not installed"; fi + printf '%-6s %s\n %s\n' "$x" "$st" "$(extra_desc "$x")" + done +} + +do_status() { + any= + for x in $(extra_installed); do + any=1 + printf '%s\n release %s\n size %s\n commands ' "$x" "$(extra_release "$x")" "$(du -sh "$EXTRA/$x" 2>/dev/null | cut -f1)" + for f in "$EXTRA/$x"/bin/*; do printf ' %s' "${f##*/}"; done + printf '\n location %s\n' "$EXTRA/$x" + [ -s "$EXTRA/$x/components" ] && sed 's/^/ contains /' "$EXTRA/$x/components" + done + [ -n "$any" ] || echo "no extras installed (mu300-extra list)" +} + +# tests/ source this file for its functions only +[ -z "${MU300_LIB:-}" ] || return 0 + +case ${1:-list} in + list) do_list ;; + status) do_status ;; + install) [ -n "${2:-}" ] || die "usage: mu300-extra install NAME"; known "$2"; need_root install; need_disk; do_install "$2" ;; + remove) [ -n "${2:-}" ] || die "usage: mu300-extra remove NAME"; known "$2"; need_root remove; need_disk; do_remove "$2" ;; + link) need_root link; do_link ;; + adopt) [ -n "${2:-}" ] || die "usage: mu300-extra adopt NAME"; known "$2"; need_root adopt; need_disk; do_adopt "$2" ;; + -h|--help|help) sed -n '2,18s/^# \{0,1\}//p' "$0" ;; + *) echo "usage: mu300-extra list|status|install NAME|remove NAME|link|adopt NAME" >&2; exit 2 ;; +esac diff --git a/rootfs/overlay/opt/mu300/bin/mu300-toolkit b/rootfs/overlay/opt/mu300/bin/mu300-toolkit index bcd00eb7..4695328f 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-toolkit +++ b/rootfs/overlay/opt/mu300/bin/mu300-toolkit @@ -605,6 +605,11 @@ menu_vpn() { if grep -q '^VLESS_URI=""' "$VPN_CONF" || ! grep -q '^VLESS_URI=' "$VPN_CONF"; then echo " set VLESS_URI first (Edit config)"; pause; continue fi + # the engines are an extra, not part of the system + if ! /opt/mu300/bin/mu300-vpn engines >/dev/null 2>&1; then + echo " the VPN engines are not installed yet; downloading the vpn extra" + /opt/mu300/bin/mu300-extra install vpn || { echo " could not install them (mu300-extra install vpn)"; pause; continue; } + fi sed -i 's/^ENABLE=.*/ENABLE=1/' "$VPN_CONF"; svc enable mu300-vpn; sleep 3 ;; 2) [ -r "$VPN_CONF" ] && sed -i 's/^ENABLE=.*/ENABLE=0/' "$VPN_CONF" svc disable mu300-vpn; /opt/mu300/bin/mu300-vpn off ;; diff --git a/rootfs/overlay/opt/mu300/bin/mu300-update b/rootfs/overlay/opt/mu300/bin/mu300-update index cac5ea53..15d10ee4 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-update +++ b/rootfs/overlay/opt/mu300/bin/mu300-update @@ -9,6 +9,7 @@ # mu300-update kernel [5.4|6.18|7.2] show, or change, the kernel (vendor 5.4, mainline 6.18 LTS or the newest # stable mainline, 7.2) and install it # mu300-update clean delete the kept .old copies +# Installed extras (mu300-extra: the VPN engines) are updated with the release. # # How it works: the new filesystem is unpacked next to the old one as .new on the Linux partition, your settings # and the vendor files are carried over, and only then are the directories swapped ( -> .old, .new -> @@ -58,7 +59,8 @@ fetch_file() { # fetch_file URL OUT (resumes a partial OUT) # already there and matches is not downloaded again, a partial one is resumed STAGE=$DISK/.mu300-update fetch_verified() { - base=https://github.com/$REPO/releases/download/$1 + # MU300_RELEASE_URL: another server with the same files and SHA256SUMS (a local test server), as for install.sh + base=${MU300_RELEASE_URL:-https://github.com/$REPO/releases/download/$1} mkdir -p "$STAGE" # SHA256SUMS is fetched once per run, never trusted from an earlier one: a release published again under the # same tag left a stale copy that every file then failed (and the updater it had verified replaced this one). @@ -152,6 +154,8 @@ check() { printf 'newest release %s\n' "$latest" printf 'systems on disk %s\n' "$(installed_systems)" printf 'running now %s\n' "$(running_os)" + _xs=; for x in $(extra_installed); do _xs="$_xs $x ($(extra_release "$x"))"; done + printf 'extras %s\n' "${_xs:- none (mu300-extra list)}" printf 'kernel %s (boot image: %s; kernel choice %s)\n' "$(uname -r)" "$(cat "$BOOTDIR/installed.tag" 2>/dev/null || echo "from the installer")" "$(kernel_choice)" for os in $(installed_systems); do [ -d "$DISK/$os.old" ] && printf 'rollback available for %s (%s)\n' "$os" "$(cat "$DISK/$os.old/etc/mu300/image-version" 2>/dev/null || echo unknown)" @@ -310,6 +314,120 @@ apply_one() { # apply_one OS TAG printf '%s: updated %s -> %s\n' "$os" "$(cat "$DISK/$os.old$VERSION_FILE" 2>/dev/null || echo unknown)" "$(cat "$old$VERSION_FILE" 2>/dev/null || echo unknown)" } +# ---- extras ----------------------------------------------------------------------------------------------------- +# Optional parts that are not in the images: release assets mu300-extra-.tar.gz (tools/make-extra.sh: ./name, +# ./release, ./components, ./bin/...), installed on the Linux partition next to the systems, so both systems use +# one copy and nothing that replaces a system touches it. mu300-extra is the command; the functions are here +# because an update installs and refreshes them too, and the updater of the new release runs before the system +# that has mu300-extra exists. +EXTRA=$DISK/extra +EXTRAS=vpn +VPN_ENGINES="xray hev-socks5-tunnel sing-box" +extra_desc() { + case $1 in + vpn) echo "the VPN engines for mu300-vpn: Xray-core, hev-socks5-tunnel, sing-box (about 50 MB to download, 120 MB installed)" ;; + esac +} +extra_asset() { echo "mu300-extra-$1.tar.gz"; } +extra_known() { case " $EXTRAS " in *" $1 "*) return 0 ;; esac; return 1; } +extra_installed() { + for d in "$EXTRA"/*; do + [ -d "$d/bin" ] && printf '%s ' "${d##*/}" + done + return 0 +} +extra_release() { cat "$EXTRA/$1/release" 2>/dev/null || echo unknown; } + +# extra_put NAME DIR: DIR (on the same filesystem) becomes the extra; the one it replaces is removed afterwards. +# Running programs keep their files, as with a system that is updated while it runs. +extra_put() { + rm -rf "$EXTRA/.$1.old" + if [ -d "$EXTRA/$1" ]; then mv "$EXTRA/$1" "$EXTRA/.$1.old" || return 1; fi + if ! mv "$2" "$EXTRA/$1"; then + [ -d "$EXTRA/.$1.old" ] && mv "$EXTRA/.$1.old" "$EXTRA/$1" + return 1 + fi + rm -rf "$EXTRA/.$1.old" + sync +} + +# extra_unpack NAME TARBALL: install a downloaded (and verified) extra; the old one stays when anything is wrong +extra_unpack() { + case $1 in ''|*[!a-z0-9-]*) echo "extra: bad name '$1'" >&2; return 1 ;; esac + _new=$EXTRA/.$1.new + rm -rf "$_new"; mkdir -p "$_new" || return 1 + if ! untar -xzf "$2" -C "$_new" 2>/dev/null; then + rm -rf "$_new"; echo "extra $1: $2 is not a readable tar.gz" >&2; return 1 + fi + if [ "$(cat "$_new/name" 2>/dev/null)" != "$1" ]; then + rm -rf "$_new"; echo "extra $1: $2 is not the $1 extra" >&2; return 1 + fi + _n=0 + for _f in "$_new"/bin/*; do [ -f "$_f" ] && [ -x "$_f" ] && _n=$((_n + 1)); done + if [ "$_n" = 0 ]; then rm -rf "$_new"; echo "extra $1: no programs in $2" >&2; return 1; fi + extra_put "$1" "$_new" || { rm -rf "$_new"; echo "extra $1: could not put it in place" >&2; return 1; } +} + +# does the system at ROOT use the VPN? (only the switch is read, never the rest of the file) +extra_vpn_wanted() { grep -q '^ENABLE=1' "$1/etc/mu300/vpn.conf" 2>/dev/null; } +# does the image at ROOT carry the engines itself (the images before the vpn extra)? +vpn_engines_in() { [ -x "$1/opt/mu300/bin/xray" ] || [ -x "$1/opt/mu300/bin/sing-box" ]; } + +# extra_adopt vpn ROOT: the engines of an image from before the vpn extra become the extra, without a download. +# Its release is that system's, so the next update replaces them with the release's. +extra_adopt() { + [ "$1" = vpn ] || { echo "extra $1: nothing to adopt" >&2; return 1; } + vpn_engines_in "$2" || return 1 + _new=$EXTRA/.$1.new + rm -rf "$_new"; mkdir -p "$_new/bin" || return 1 + for _e in $VPN_ENGINES; do + [ -x "$2/opt/mu300/bin/$_e" ] || continue + cp -p "$2/opt/mu300/bin/$_e" "$_new/bin/$_e" || { rm -rf "$_new"; return 1; } + done + echo "$1" > "$_new/name" + cat "$2/etc/mu300/image-version" > "$_new/release" 2>/dev/null || echo unknown > "$_new/release" + echo "taken from the image of $2" > "$_new/components" + extra_put "$1" "$_new" || { rm -rf "$_new"; return 1; } + echo "extra $1: the engines of $2 are kept as the $1 extra" +} + +# extras_to_fetch TAG "OS...": the extras an update to TAG downloads - the installed ones that are not TAG's, and +# the vpn extra for a system being updated that uses the VPN with the engines of its image (they are gone from the +# images since the vpn extra exists; without them the VPN would not come back after the reboot) +extras_to_fetch() { + _want= + for _x in $(extra_installed); do + extra_known "$_x" || continue + [ "$(extra_release "$_x")" = "$1" ] || _want="$_want $_x" + done + if [ ! -d "$EXTRA/vpn/bin" ]; then + for _o in $2; do + extra_vpn_wanted "$DISK/$_o" && { _want="$_want vpn"; break; } + done + fi + echo $_want +} + +# extras_install TAG "OS..." "NAMES": after the systems are in place, install the extras downloaded into $STAGE; +# then make sure every updated system that uses the VPN has engines (the extra, or those of the image it replaced) +extras_install() { + for _x in $3; do + if extra_unpack "$_x" "$STAGE/$(extra_asset "$_x")"; then + printf 'extra %s: %s\n' "$_x" "$1" + EXTRA_CHANGED=1 + else + echo "extra $_x: not updated (see above)" >&2 + fi + rm -f "$STAGE/$(extra_asset "$_x")" + done + for _o in $2; do + extra_vpn_wanted "$DISK/$_o" && [ ! -d "$EXTRA/vpn/bin" ] && ! vpn_engines_in "$DISK/$_o" || continue + extra_adopt vpn "$DISK/$_o.old" && EXTRA_CHANGED=1 || + echo "extra vpn: no VPN engines for $_o; after the reboot run: mu300-extra install vpn" >&2 + done + return 0 +} + # ---- the kernel and the boot image ------------------------------------------------------------------------------ # The boot image in boot_b is the stock header and AVB tail of this device, the kernel, and a ramdisk that holds this # device's own Android files next to the generic part (init, busybox, modules). The kernel unpacks concatenated @@ -612,6 +730,14 @@ apply() { for os in $todo; do fetch_verified "$tag" "$(rootfs_asset "$os")" || { offline_hint; die "nothing was changed; run mu300-update apply again to continue the download"; } done + # the extras: an installed one follows the release, and a system that uses the VPN with the engines of its image + # gets the vpn extra (a release from before extras has none - its images still carry the engines: r=2) + extras= + for x in $(extras_to_fetch "$tag" "$todo"); do + fetch_verified "$tag" "$(extra_asset "$x")"; r=$? + [ "$r" = 0 ] && { extras="$extras $x"; continue; } + [ "$r" = 2 ] || { offline_hint; die "nothing was changed; run mu300-update apply again to continue the download"; } + done boot_asset= if [ -z "${MU300_KERNEL_BUNDLE:-}" ]; then boot_asset=$(kernel_asset) @@ -626,6 +752,8 @@ apply() { apply_one "$os" "$tag" SYSTEM_CHANGED=1 done + EXTRA_CHANGED= + extras_install "$tag" "$todo" "$extras" # the kernel goes with the modules of the systems just installed, so it follows them boot_ok=1 boot_update "$tag" || { boot_ok=; printf '\nThe boot image was not updated (see above)%s.\n' "${SYSTEM_CHANGED:+; the systems were}"; } @@ -633,7 +761,7 @@ apply() { [ -z "$boot_ok" ] || rm -rf "$STAGE" if [ -z "$SYSTEM_CHANGED$BOOT_CHANGED" ]; then [ -n "$boot_ok" ] || return 1 - printf '\nNothing to do.\n' + if [ -n "$EXTRA_CHANGED" ]; then printf '\nThe extras are up to date now.\n'; else printf '\nNothing to do.\n'; fi return 0 fi if [ -z "$SYSTEM_CHANGED" ]; then @@ -695,6 +823,6 @@ case ${1:-check} in kernel) shift; kernel_cmd "$@" ;; accounts) accounts ;; clean) clean ;; - -h|--help|help) sed -n '2,12s/^# \{0,1\}//p' "$0" ;; + -h|--help|help) sed -n '2,13s/^# \{0,1\}//p' "$0" ;; *) echo "usage: mu300-update check|apply [os...]|boot|kernel [5.4|6.18|7.2]|rollback [os]|rollback-boot|clean" >&2; exit 2 ;; esac diff --git a/rootfs/overlay/opt/mu300/bin/mu300-vpn b/rootfs/overlay/opt/mu300/bin/mu300-vpn index 3aa838ec..0ea6d021 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-vpn +++ b/rootfs/overlay/opt/mu300/bin/mu300-vpn @@ -9,6 +9,7 @@ # mu300-vpn off remove the kill switch (the service stop does not, so a crash never leaks), # and any routing an engine left behind if it is no longer running # mu300-vpn status service, tunnel, exit IP +# mu300-vpn engines which engine programs it uses (the vpn extra: mu300-extra install vpn) set -e CONF=${MU300_VPN_CONF:-/etc/mu300/vpn.conf} RUN=${MU300_VPN_RUN:-/run/mu300-vpn} # (MU300_VPN_RUN, MU300_LAN_CONF, MU300_BIN, MU300_LIB: for tests/) @@ -31,14 +32,44 @@ case ",${LAN_CIDRS:-}," in *",$own,"*) ;; *) LAN_CIDRS=${LAN_CIDRS:+$LAN_CIDRS,} # fetched with openssl - both unmarked. sing-box does its bootstrap lookup on its own marked socket, so it works # there. An update keeps vpn.conf, and older ones say KILL_SWITCH=1: they stay on sing-box, which is what they ran. if [ -z "${ENGINE:-}" ]; then [ "$KILL_SWITCH" = 1 ] && ENGINE=sing-box || ENGINE=xray; fi -XRAY=${XRAY:-/opt/mu300/bin/xray}; HEV=${HEV:-/opt/mu300/bin/hev-socks5-tunnel} -BIN=${SING_BOX:-/opt/mu300/bin/sing-box} SOCKS_PORT=10808 -# an image built before the xray engine existed has only sing-box: keep working with what is there -if [ "$ENGINE" = xray ] && { [ ! -x "$XRAY" ] || [ ! -x "$HEV" ]; }; then - [ -x "$BIN" ] && { echo "xray engine not installed, using sing-box" >&2; ENGINE=sing-box; } -fi -case "$ENGINE" in xray) TUN=xtun ;; *) TUN=sbtun ;; esac +# The engines are not part of the images: they are the vpn extra, on the Linux partition (mu300-extra install vpn). +# Images from before the extra carry them in /opt/mu300/bin, which is the second place to look. XRAY, HEV and +# SING_BOX (in vpn.conf or the environment) name other programs. (MU300_OPT, MU300_DISK, MU300_EXTRA_CMD: tests/) +OPTBIN=${MU300_OPT:-/opt/mu300}/bin +VPN_EXTRA=${MU300_DISK:-/mnt/mu300-disk}/extra/vpn/bin +EXTRA_CMD=${MU300_EXTRA_CMD:-$OPTBIN/mu300-extra} +MISSING="the VPN engines are not installed: run mu300-extra install vpn" +engine() { + for _p in "$VPN_EXTRA/$1" "$OPTBIN/$1"; do [ -x "$_p" ] && { echo "$_p"; return 0; }; done + echo "$VPN_EXTRA/$1" +} +_engine=$ENGINE; _xray=${XRAY:-}; _hev=${HEV:-}; _sb=${SING_BOX:-} +pick_engines() { + XRAY=${_xray:-$(engine xray)}; HEV=${_hev:-$(engine hev-socks5-tunnel)}; BIN=${_sb:-$(engine sing-box)} + ENGINE=$_engine + # an image built before the xray engine existed has only sing-box: keep working with what is there + if [ "$ENGINE" = xray ] && { [ ! -x "$XRAY" ] || [ ! -x "$HEV" ]; }; then + [ -x "$BIN" ] && { echo "xray engine not installed, using sing-box" >&2; ENGINE=sing-box; } + fi + case "$ENGINE" in xray) TUN=xtun ;; *) TUN=sbtun ;; esac +} +engines_ok() { case "$ENGINE" in xray) [ -x "$XRAY" ] && [ -x "$HEV" ] ;; *) [ -x "$BIN" ] ;; esac; } +pick_engines + +# The VPN is on and there is no engine: a system updated from an image that carried them by a path that did not +# bring the vpn extra along (an older updater or installer). Take the engines of the image it replaced, or download +# the extra - before the kill switch, which would stop the download. A configured VPN comes back by itself. +ensure_engines() { + engines_ok && return 0 + echo "the VPN engines are not installed; getting the vpn extra" >&2 + if { "$EXTRA_CMD" adopt vpn || "$EXTRA_CMD" install vpn; } >&2; then + pick_engines + engines_ok && return 0 + fi + echo "$MISSING" >&2 + return 1 +} # json_str VALUE : JSON string literal json_str() { printf '"%s"' "$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g')"; } @@ -353,7 +384,10 @@ run_xray() { exit 1 } -gen() { case "$ENGINE" in xray) gen_xray ;; *) gen_singbox ;; esac; } +gen() { + engines_ok || { echo "$MISSING" >&2; exit 1; } + case "$ENGINE" in xray) gen_xray ;; *) gen_singbox ;; esac +} killswitch_on() { nft -f - </dev/null ensure_firewall_zone if [ "$ENGINE" = xray ] && [ "$KILL_SWITCH" = 1 ]; then @@ -436,9 +471,13 @@ case "${1:-status}" in off) killswitch_off; echo "kill switch removed"; routing_cleanup ;; status) echo "engine: $ENGINE enabled: $ENABLE kill switch: $KILL_SWITCH ($(nft list table inet mu300_vpn >/dev/null 2>&1 && echo active || echo inactive))" + engines_ok || echo "$MISSING" ip -br addr show "$TUN" 2>/dev/null || echo "$TUN: down" if ip link show "$TUN" >/dev/null 2>&1; then printf 'exit IP: '; (wget -qO- -T 8 http://api.ipify.org 2>/dev/null || curl -s -m 8 http://api.ipify.org 2>/dev/null || echo unreachable); echo fi ;; - *) echo "usage: $0 gen|run|off|status" >&2; exit 2 ;; + engines) + for e in "$XRAY" "$HEV" "$BIN"; do printf '%s %s\n' "$e" "$([ -x "$e" ] && echo ok || echo missing)"; done + engines_ok || { echo "$MISSING" >&2; exit 1; } ;; + *) echo "usage: $0 gen|run|off|status|engines" >&2; exit 2 ;; esac diff --git a/rootfs/overlay/opt/mu300/bin/rootfs-fixups b/rootfs/overlay/opt/mu300/bin/rootfs-fixups index 435a3fc3..60d54f6b 100755 --- a/rootfs/overlay/opt/mu300/bin/rootfs-fixups +++ b/rootfs/overlay/opt/mu300/bin/rootfs-fixups @@ -7,11 +7,7 @@ done # docker export leaves an empty /etc/resolv.conf instead of the systemd-resolved stub [ -L /etc/resolv.conf ] || ln -sfn ../run/systemd/resolve/stub-resolv.conf /etc/resolv.conf # /opt/mu300/bin is on nobody's PATH, and sudo's secure_path cannot see it at all, so the commands the -# documentation tells people to run need links from a directory that is. Systems installed before these links -# existed get them here instead of only after an update. The same list as the images' (rootfs/assemble.sh, -# openwrt/build-rootfs.sh; tests/test_static.py compares them): a shorter one here left mu300-led off the PATH. -for c in mu300-toolkit mu300-next-boot mu300-os mu300-update mobile-data mu300-at mu300-vpn wifi-client mu300-ttl mu300-wifi-band mu300-led mu300-usb mu300-nfc mu300-device; do - [ -x "/opt/mu300/bin/$c" ] || continue - [ -e "/usr/local/bin/$c" ] || ln -sfn "/opt/mu300/bin/$c" "/usr/local/bin/$c" 2>/dev/null || true -done -true +# documentation tells people to run need links from a directory that is. Systems installed before a command had its +# link get it here instead of only after an update, and the extras' commands (mu300-extra) are linked too. The list +# is the one the images are built with (/opt/mu300/lib/path-commands). +/opt/mu300/bin/mu300-extra link 2>/dev/null || true diff --git a/rootfs/overlay/opt/mu300/lib/path-commands b/rootfs/overlay/opt/mu300/lib/path-commands new file mode 100644 index 00000000..8ca338ef --- /dev/null +++ b/rootfs/overlay/opt/mu300/lib/path-commands @@ -0,0 +1,18 @@ +mu300-toolkit +mu300-next-boot +mu300-os +mu300-update +mu300-extra +mobile-data +mu300-at +mu300-ussd +sms +mu300-voice +mu300-vpn +wifi-client +mu300-ttl +mu300-wifi-band +mu300-led +mu300-usb +mu300-nfc +mu300-device diff --git a/tests/test_extra.py b/tests/test_extra.py new file mode 100644 index 00000000..10cd5ee3 --- /dev/null +++ b/tests/test_extra.py @@ -0,0 +1,287 @@ +"""Extras: optional components (the VPN engines) that are not part of the images and live on the Linux partition +(MU300_DISK/extra/). mu300-update installs and updates them (sourced with MU300_LIB=1 against a fake partition), +mu300-extra is the command, and both systems' boot runs `mu300-extra link`.""" +import hashlib +import io +import os +import tarfile +import unittest + +from helpers import BIN, ShellTest + +ENGINES = ('xray', 'hev-socks5-tunnel', 'sing-box') + + +def extra_tarball(path, name='vpn', release='v2026.10.10', bins=ENGINES, components='xray 26.3.27\n'): + with tarfile.open(path, 'w:gz') as t: + def add(fn, data, mode=0o644): + ti = tarfile.TarInfo(fn) + ti.size, ti.mode = len(data), mode + t.addfile(ti, io.BytesIO(data)) + if name is not None: + add('./name', (name + '\n').encode()) + add('./release', (release + '\n').encode()) + add('./components', components.encode()) + for b in bins: + add(f'./bin/{b}', f'#!/bin/sh\necho {b} {release}\n'.encode(), 0o755) + return path + + +class Extras(ShellTest): + def setUp(self): + super().setUp() + self.disk = self.tmp / 'disk' + for os_ in ('ubuntu', 'openwrt'): + (self.disk / os_ / 'etc/mu300').mkdir(parents=True) + self.root = self.tmp / 'root' + (self.root / 'run/mu300').mkdir(parents=True) + (self.root / 'run/mu300/device').write_text('f50\n') + self.links = self.tmp / 'links' + self.links.mkdir() + self.stub('id', 'echo 0') + + def up(self, shell, code, **env): + return self.sh(shell, f'. "{BIN}/mu300-update"; {code}', MU300_LIB=1, MU300_DISK=self.disk, MU300_BIN=BIN, + MU300_SYSROOT=self.root, **env) + + def ex(self, shell, *args, **env): + e = dict(MU300_DISK=self.disk, MU300_BIN=BIN, MU300_SYSROOT=self.root, MU300_LINKDIR=self.links, + MU300_OPT=BIN.parent, MU300_FETCH_DELAY=0) + e.update(env) + return self.script(shell, BIN / 'mu300-extra', *args, **e) + + def baked(self, os_, enabled): + """an installed system from before extras: engines in its image, and vpn.conf ENABLE=1 or 0""" + b = self.disk / os_ / 'opt/mu300/bin' + b.mkdir(parents=True, exist_ok=True) + for e in ENGINES: + (b / e).write_text(f'#!/bin/sh\necho baked {e}\n') + (b / e).chmod(0o755) + (self.disk / os_ / 'etc/mu300/vpn.conf').write_text(f'ENABLE={enabled}\nVLESS_URI="vless://x@y"\n') + (self.disk / os_ / 'etc/mu300/image-version').write_text('v2026.10.06\n') + + def vpn(self): + return self.disk / 'extra' / 'vpn' + + # ---- mu300-update: the core ----------------------------------------------------------------------------- + def test_unpack_installs_and_replaces(self): + for shell in self.each_shell(): + old = extra_tarball(self.tmp / 'old.tar.gz', release='v2026.10.01') + new = extra_tarball(self.tmp / 'new.tar.gz', release='v2026.10.10') + r = self.up(shell, f'extra_unpack vpn "{old}" && extra_release vpn && extra_installed') + self.assertEqual(r.returncode, 0, r.stderr) + self.assertEqual(r.stdout.split(), ['v2026.10.01', 'vpn']) + r = self.up(shell, f'extra_unpack vpn "{new}" && extra_release vpn') + self.assertEqual(r.stdout.split(), ['v2026.10.10']) + self.assertTrue(os.access(self.vpn() / 'bin/xray', os.X_OK)) + # nothing half-done is left next to it + self.assertEqual(sorted(p.name for p in (self.disk / 'extra').iterdir()), ['vpn']) + + def test_unpack_refuses_the_wrong_payload_and_keeps_the_old(self): + for shell in self.each_shell(): + good = extra_tarball(self.tmp / 'good.tar.gz', release='v1') + self.up(shell, f'extra_unpack vpn "{good}"') + for bad in (extra_tarball(self.tmp / 'other.tar.gz', name='other'), + extra_tarball(self.tmp / 'noname.tar.gz', name=None), + extra_tarball(self.tmp / 'nobin.tar.gz', bins=())): + r = self.up(shell, f'extra_unpack vpn "{bad}"; echo "rc=$?"') + self.assertIn('rc=1', r.stdout, bad.name) + self.assertEqual((self.vpn() / 'release').read_text().strip(), 'v1', bad.name) + junk = self.tmp / 'junk.tar.gz' + junk.write_bytes(b'not a tarball') + r = self.up(shell, f'extra_unpack vpn "{junk}"; echo "rc=$?"') + self.assertIn('rc=1', r.stdout) + r = self.up(shell, f'extra_unpack ../x "{good}"; echo "rc=$?"') + self.assertIn('rc=1', r.stdout) + self.assertEqual(sorted(p.name for p in (self.disk / 'extra').iterdir()), ['vpn']) + + def test_adopt_takes_the_engines_of_an_older_image(self): + for shell in self.each_shell(): + self.baked('ubuntu', 1) + r = self.up(shell, f'extra_adopt vpn "{self.disk}/ubuntu"; echo "rc=$?"; extra_release vpn') + self.assertEqual(r.stdout.split()[-2:], ['rc=0', 'v2026.10.06'], r.stderr) + for e in ENGINES: + self.assertTrue(os.access(self.vpn() / 'bin' / e, os.X_OK), e) + # a root without engines has nothing to give + r = self.up(shell, f'rm -rf "{self.disk}/extra"; extra_adopt vpn "{self.disk}/openwrt"; echo "rc=$?"') + self.assertIn('rc=1', r.stdout) + self.assertFalse(self.vpn().exists()) + + def test_which_extras_an_update_fetches(self): + cases = [ + # (installed release or None, ubuntu ENABLE, systems updated, wanted) + (None, None, 'ubuntu', []), + (None, 0, 'ubuntu', []), + (None, 1, 'ubuntu', ['vpn']), # uses the VPN with the engines of its image + (None, 1, 'openwrt', []), # the system that uses it is not being updated + ('v2026.10.01', None, 'ubuntu', ['vpn']), # installed: follows the release + ('v2026.10.10', 1, 'ubuntu', []), # already this release's + ] + for shell in self.each_shell(): + for inst, enable, todo, want in cases: + (self.disk / 'ubuntu/etc/mu300/vpn.conf').unlink(missing_ok=True) + if enable is not None: + (self.disk / 'ubuntu/etc/mu300/vpn.conf').write_text(f'ENABLE={enable}\n') + self.up(shell, f'rm -rf "{self.disk}/extra"') + if inst: + self.up(shell, f'extra_unpack vpn "{extra_tarball(self.tmp / "i.tar.gz", release=inst)}"') + r = self.up(shell, f'extras_to_fetch v2026.10.10 "{todo}"') + self.assertEqual(r.stdout.split(), want, (inst, enable, todo, r.stderr)) + + def test_update_keeps_a_configured_vpn_working(self): + # the system was updated to an image without engines; the release's extra goes in, and when that fails the + # engines of the image it replaced are taken over + for shell in self.each_shell(): + for fetched in (True, False): + self.up(shell, f'rm -rf "{self.disk}/extra" "{self.disk}/ubuntu.old"') + self.baked('ubuntu', 1) + os.rename(self.disk / 'ubuntu', self.disk / 'ubuntu.old') + (self.disk / 'ubuntu/etc/mu300').mkdir(parents=True) + (self.disk / 'ubuntu/etc/mu300/vpn.conf').write_text('ENABLE=1\n') + stage = self.disk / '.mu300-update' + stage.mkdir(exist_ok=True) + if fetched: + extra_tarball(stage / 'mu300-extra-vpn.tar.gz', release='v2026.10.10') + got = 'vpn' if fetched else '' + r = self.up(shell, f'extras_install v2026.10.10 "ubuntu" "{got}"; echo "rc=$?"; extra_release vpn') + self.assertEqual(r.stdout.split()[-2:], ['rc=0', 'v2026.10.10' if fetched else 'v2026.10.06'], + (fetched, r.stdout, r.stderr)) + self.assertTrue(os.access(self.vpn() / 'bin/xray', os.X_OK)) + self.assertFalse((stage / 'mu300-extra-vpn.tar.gz').exists()) + + def test_fetch_verified_takes_another_server(self): + self.stub('curl', 'for a; do last=$a; done; echo "$last" >> "$STUBLOG/urls"; ' + 'case $last in */SHA256SUMS) echo "0000 x" ;; esac; exit 0') + for shell in self.each_shell(): + (self.tmp / 'urls').unlink(missing_ok=True) + self.up(shell, 'fetch_verified v1 x >/dev/null 2>&1', MU300_RELEASE_URL='http://10.0.0.2:8000/rel') + urls = (self.tmp / 'urls').read_text().split() + self.assertEqual(urls[0], 'http://10.0.0.2:8000/rel/SHA256SUMS') + + # ---- mu300-extra --------------------------------------------------------------------------------------- + def serve(self, release='v2026.10.10', assets=None): + """a release on a 'server': a directory curl (stubbed) reads from, with its SHA256SUMS""" + d = self.tmp / 'server' / release + d.mkdir(parents=True, exist_ok=True) + if assets is None: + assets = {'mu300-extra-vpn.tar.gz': extra_tarball(self.tmp / 'payload.tar.gz', release=release)} + sums = [] + for name, src in assets.items(): + data = src.read_bytes() + (d / name).write_bytes(data) + sums.append(f'{hashlib.sha256(data).hexdigest()} {name}') + (d / 'SHA256SUMS').write_text('\n'.join(sums) + '\n') + # curl -o OUT URL or curl URL (stdout); the URL's last two parts name the release and the file + self.stub('curl', f'out=; url=; while [ $# -gt 0 ]; do case $1 in -o) out=$2; shift ;; -*) ;; *) url=$1 ;; esac; ' + f'shift; done; echo "$url" >> "$STUBLOG/urls"; f=${{url##*/}}; r=${{url%/*}}; r=${{r##*/}}; ' + f'case $url in */releases/latest) echo \'"tag_name": "v2026.10.10"\'; exit 0 ;; esac; ' + f'src="{self.tmp}/server/$r/$f"; [ -f "$src" ] || exit 22; ' + f'if [ -n "$out" ]; then cat "$src" > "$out"; else cat "$src"; fi') + return d + + def test_install_from_the_release_of_the_system(self): + self.serve('v2026.10.10') + for shell in self.each_shell(): + self.up(shell, f'rm -rf "{self.disk}/extra"') + for os_ in ('ubuntu', 'openwrt'): + (self.disk / os_ / 'etc/mu300/image-version').write_text('v2026.10.10\n') + (self.root / 'etc/mu300').mkdir(parents=True, exist_ok=True) + (self.root / 'etc/mu300/image-version').write_text('v2026.10.10\n') + r = self.ex(shell, 'install', 'vpn') + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertEqual((self.vpn() / 'release').read_text().strip(), 'v2026.10.10') + self.assertIn('/v2026.10.10/mu300-extra-vpn.tar.gz', (self.tmp / 'urls').read_text()) + # the commands are on the PATH + self.assertEqual(os.readlink(self.links / 'xray'), str(self.vpn() / 'bin/xray')) + st = self.ex(shell, 'status') + self.assertIn('vpn', st.stdout) + self.assertIn('v2026.10.10', st.stdout) + # the download is not left behind + self.assertFalse((self.disk / '.mu300-extra').exists()) + + def test_install_falls_back_to_the_newest_release(self): + # a system from before extras (v2026.10.06 has no mu300-extra-vpn.tar.gz) takes the newest release's + other = self.tmp / 'other' + other.write_text('x') + self.serve('v2026.10.06', assets={'mu300-update': other}) + self.serve('v2026.10.10') + (self.root / 'etc/mu300').mkdir(parents=True, exist_ok=True) + (self.root / 'etc/mu300/image-version').write_text('v2026.10.06\n') + for shell in self.each_shell(): + self.up(shell, f'rm -rf "{self.disk}/extra"') + r = self.ex(shell, 'install', 'vpn') + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertEqual((self.vpn() / 'release').read_text().strip(), 'v2026.10.10') + + def test_install_refuses_a_bad_checksum(self): + d = self.serve('v2026.10.10') + (d / 'mu300-extra-vpn.tar.gz').write_bytes(b'tampered') + (self.root / 'etc/mu300').mkdir(parents=True, exist_ok=True) + (self.root / 'etc/mu300/image-version').write_text('v2026.10.10\n') + for shell in self.each_shell(): + r = self.ex(shell, 'install', 'vpn', MU300_RELEASE='v2026.10.10') + self.assertNotEqual(r.returncode, 0) + self.assertFalse(self.vpn().exists()) + + def test_install_from_a_local_file_and_remove(self): + f = extra_tarball(self.tmp / 'local.tar.gz', release='dev') + for shell in self.each_shell(): + r = self.ex(shell, 'install', 'vpn', MU300_EXTRA_FILE=f) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertTrue(os.path.islink(self.links / 'sing-box')) + # a file of the user's own with the same name is never replaced + (self.links / 'hev-socks5-tunnel').unlink() + (self.links / 'hev-socks5-tunnel').write_text('mine') + r = self.ex(shell, 'remove', 'vpn') + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertFalse(self.vpn().exists()) + self.assertFalse(os.path.lexists(self.links / 'sing-box')) + self.assertEqual((self.links / 'hev-socks5-tunnel').read_text(), 'mine') + (self.links / 'hev-socks5-tunnel').unlink() + + def test_unknown_and_usage(self): + for shell in self.each_shell(): + r = self.ex(shell, 'install', 'nosuch') + self.assertNotEqual(r.returncode, 0) + self.assertIn('nosuch', r.stderr) + r = self.ex(shell, 'frobnicate') + self.assertEqual(r.returncode, 2) + r = self.ex(shell, 'list') + self.assertEqual(r.returncode, 0) + self.assertIn('vpn', r.stdout) + self.assertIn('not installed', r.stdout) + + def test_link_puts_the_system_commands_on_the_path(self): + for shell in self.each_shell(): + for p in self.links.iterdir(): + p.unlink() + # a link left by an extra that is gone, and one of the user's that points somewhere else + os.symlink(self.disk / 'extra/gone/bin/tool', self.links / 'tool') + os.symlink('/somewhere/else', self.links / 'mine') + r = self.ex(shell, 'link') + self.assertEqual(r.returncode, 0, r.stderr) + self.assertEqual(os.readlink(self.links / 'mu300-ussd'), str(BIN / 'mu300-ussd')) + self.assertEqual(os.readlink(self.links / 'mu300-extra'), str(BIN / 'mu300-extra')) + self.assertFalse(os.path.lexists(self.links / 'tool')) + self.assertTrue(os.path.lexists(self.links / 'mine')) + + def test_adopt_command(self): + for shell in self.each_shell(): + self.up(shell, f'rm -rf "{self.disk}/extra" "{self.disk}/ubuntu.old"') + self.baked('ubuntu', 1) + os.rename(self.disk / 'ubuntu', self.disk / 'ubuntu.old') + (self.disk / 'ubuntu/etc/mu300').mkdir(parents=True) + r = self.ex(shell, 'adopt', 'vpn', MU300_RUNNING_OS='ubuntu') + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertTrue(os.access(self.vpn() / 'bin/xray', os.X_OK)) + # nothing to take: fails, quietly enough for mu300-vpn to try the download next + self.up(shell, f'rm -rf "{self.disk}/extra" "{self.disk}/ubuntu.old"') + r = self.ex(shell, 'adopt', 'vpn', MU300_RUNNING_OS='ubuntu') + self.assertNotEqual(r.returncode, 0) + import shutil + shutil.rmtree(self.disk / 'ubuntu') + (self.disk / 'ubuntu/etc/mu300').mkdir(parents=True) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_static.py b/tests/test_static.py index 178fa4ff..a8a4a6a2 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -104,25 +104,34 @@ class Rules(unittest.TestCase): self.assertIn('mu300sd', (TOP / f).read_text(), f) def test_the_commands_on_path_are_the_same_everywhere(self): - # the Ubuntu image, the OpenWrt image and rootfs-fixups (which adds the links on systems installed before a - # command had one) link the same commands; the fixups' list lagged behind and mu300-led, mu300-device were - # "command not found" on an installed U30 Air - def names(path, pattern): - m = re.search(pattern, (TOP / path).read_text()) - self.assertIsNotNone(m, path) - return m.group(1).split() - lists = { - 'ubuntu': names('rootfs/assemble.sh', r'for c in ([^;]+); do ln -sfn /opt/mu300/bin/\$c \$R/usr/local/bin'), - 'openwrt': names('openwrt/build-rootfs.sh', r'for c in ([^;]+); do ln -sf /opt/mu300/bin/\$c \$R/usr/bin'), - 'fixups': names('rootfs/overlay/opt/mu300/bin/rootfs-fixups', r'for c in ([^;]+); do\n'), - } - self.assertEqual(lists['ubuntu'], lists['openwrt']) - self.assertEqual(lists['ubuntu'], lists['fixups']) - for c in ('mu300-device', 'mu300-led', 'mobile-data', 'mu300-update'): - self.assertIn(c, lists['ubuntu']) - for c in lists['ubuntu']: + # the Ubuntu image, the OpenWrt image and the boot-time links (mu300-extra link, for systems installed before a + # command had one) link the same commands. They used to be three copies of the list: the fixups' lagged behind + # (mu300-led, mu300-device were "command not found" on an installed U30 Air), and all three left mu300-ussd out. + # Now there is one file, and every place reads it. + lst = (TOP / 'rootfs/overlay/opt/mu300/lib/path-commands').read_text().split() + self.assertIn('$R/opt/mu300/lib/path-commands', (TOP / 'rootfs/assemble.sh').read_text()) + self.assertIn('/in/opt-mu300/lib/path-commands', (TOP / 'openwrt/build-rootfs.sh').read_text()) + self.assertIn('/lib/path-commands', (BIN / 'mu300-extra').read_text()) + self.assertIn('mu300-extra link', (BIN / 'rootfs-fixups').read_text()) + self.assertIn('mu300-extra link', (OPENWRT / 'etc/init.d/mu300-post').read_text()) + for c in ('mu300-device', 'mu300-led', 'mobile-data', 'mu300-update', 'mu300-ussd', 'sms', 'mu300-extra'): + self.assertIn(c, lst) + self.assertEqual(len(lst), len(set(lst))) + for c in lst: self.assertTrue((BIN / c).is_file(), c) + def test_images_carry_no_vpn_engine(self): + # the engines are the vpn extra (mu300-extra): ~120 MB that a system without a VPN does not carry + for f in ('rootfs/assemble.sh', 'openwrt/build-rootfs.sh'): + src = (TOP / f).read_text() + for e in ('xray', 'sing-box', 'hev-socks5-tunnel'): + self.assertNotRegex(src, rf'opt/mu300/bin/{e}\b', (f, e)) + rel = (TOP / 'tools/make-release.sh').read_text() + self.assertIn('make-extra.sh', rel) + self.assertIn('mu300-extra-', rel) + # the audit refuses an image that still has one + self.assertRegex(rel, r'opt/mu300/bin/\(xray\|sing-box\|hev-socks5-tunnel\)') + def test_init_finds_partitions_after_the_modules(self): # the eMMC driver is one of the vendor modules: misc and boot_b cannot be found before they are loaded init = (TOP / 'boot' / 'init').read_text() diff --git a/tests/test_vpn.py b/tests/test_vpn.py index 1774e165..c6f1f4f0 100644 --- a/tests/test_vpn.py +++ b/tests/test_vpn.py @@ -1,6 +1,7 @@ """mu300-vpn, sourced for its functions (MU300_LIB=1): the VLESS URI parser, JSON helpers, which networks stay out of the tunnel, and the sing-box config it writes. Ubuntu and OpenWrt both run it (dash, bash, busybox ash).""" import json +import shutil import unittest from helpers import BIN, ShellTest @@ -111,5 +112,92 @@ class Vpn(ShellTest): self.assertIn('check -c', (self.tmp / 'sb.args').read_text()) +class Engines(ShellTest): + """where mu300-vpn finds its engines: the vpn extra on the Linux partition first, an older image's own second; + and what it does when there are none""" + + def setUp(self): + super().setUp() + self.conf = self.tmp / 'vpn.conf' + self.disk = self.tmp / 'disk' + self.opt = self.tmp / 'opt' + (self.opt / 'bin').mkdir(parents=True) + self.extra = self.disk / 'extra/vpn/bin' + self.stub('ip', 'exit 0') + + def put(self, d, names): + d.mkdir(parents=True, exist_ok=True) + for n in names: + (d / n).write_text('#!/bin/sh\n') + (d / n).chmod(0o755) + + def vpn(self, shell, code, conf='ENABLE=1\nENGINE=xray\n', **env): + self.conf.write_text(conf) + e = dict(MU300_LIB=1, MU300_VPN_CONF=self.conf, MU300_VPN_RUN=self.tmp / 'run-vpn', MU300_BIN=BIN, + MU300_LAN_CONF=self.tmp / 'no-lan.conf', MU300_OPT=self.opt, MU300_DISK=self.disk, + MU300_EXTRA_CMD=self.stubs / 'extra') + e.update(env) + return self.sh(shell, f'. "{BIN}/mu300-vpn"; {code}', **e) + + def reset(self): + shutil.rmtree(self.disk, ignore_errors=True) + shutil.rmtree(self.opt / 'bin') + (self.opt / 'bin').mkdir() + + def test_the_extra_comes_first(self): + names = ('xray', 'hev-socks5-tunnel', 'sing-box') + for shell in self.each_shell(): + self.reset() + self.put(self.opt / 'bin', names) + r = self.vpn(shell, 'echo "$XRAY $HEV $BIN"') + self.assertEqual(r.stdout.split(), [str(self.opt / 'bin' / n) for n in names]) + self.put(self.extra, names) + r = self.vpn(shell, 'echo "$XRAY $HEV $BIN"; engines_ok && echo OK') + self.assertEqual(r.stdout.split(), [str(self.extra / n) for n in names] + ['OK']) + # vpn.conf may name its own + r = self.vpn(shell, 'echo "$XRAY"', conf='ENABLE=1\nXRAY=/x/xray\n') + self.assertEqual(r.stdout.strip(), '/x/xray') + + def test_missing_engines_are_reported(self): + for shell in self.each_shell(): + self.reset() + r = self.vpn(shell, 'engines_ok && echo OK || echo "$MISSING"') + self.assertEqual(r.stdout.strip(), 'the VPN engines are not installed: run mu300-extra install vpn') + r = self.vpn(shell, '(gen) && echo rc=0 || echo rc=1') + self.assertIn('mu300-extra install vpn', r.stderr) + self.assertNotIn('rc=0', r.stdout) + + def test_a_configured_vpn_gets_its_engines_back(self): + for shell in self.each_shell(): + for adopt_ok, install_ok, want in ((True, False, 'adopt'), (False, True, 'adopt install'), + (False, False, 'adopt install')): + self.reset() + (self.tmp / 'calls').unlink(missing_ok=True) + self.stub('extra', f'echo "$1" >> "$STUBLOG/calls"; ' + f'case $1 in adopt) ok={int(adopt_ok)} ;; install) ok={int(install_ok)} ;; esac; ' + f'[ $ok = 1 ] || exit 1; d="{self.extra}"; mkdir -p "$d"; ' + 'for n in xray hev-socks5-tunnel sing-box; do printf "#!/bin/sh\\n" > "$d/$n"; chmod 755 "$d/$n"; done') + r = self.vpn(shell, 'ensure_engines && echo rc=0 || echo rc=1; echo "$XRAY"') + self.assertEqual((self.tmp / 'calls').read_text().split(), want.split(), (adopt_ok, install_ok)) + if adopt_ok or install_ok: + self.assertEqual(r.stdout.split()[-2:], ['rc=0', str(self.extra / 'xray')], r.stderr) + else: + self.assertIn('rc=1', r.stdout) + self.assertIn('mu300-extra install vpn', r.stderr) + # engines that are there: nothing is fetched + self.put(self.extra, ('xray', 'hev-socks5-tunnel', 'sing-box')) + (self.tmp / 'calls').unlink(missing_ok=True) + r = self.vpn(shell, 'ensure_engines && echo rc=0 || echo rc=1') + self.assertIn('rc=0', r.stdout) + self.assertFalse((self.tmp / 'calls').exists()) + + def test_run_gets_the_engines_before_anything_else(self): + src = (BIN / 'mu300-vpn').read_text() + run = src[src.index(' run)'):src.index(' off)')] + self.assertLess(run.index('ENABLE'), run.index('ensure_engines')) + self.assertLess(run.index('ensure_engines'), run.index('killswitch_on')) + self.assertLess(run.index('ensure_engines'), run.index('gen')) + + if __name__ == '__main__': unittest.main() diff --git a/tools/make-extra.sh b/tools/make-extra.sh new file mode 100755 index 00000000..09ee50e6 --- /dev/null +++ b/tools/make-extra.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# Build an extra: the optional parts that are not in the images and that mu300-extra installs on the device. +# tools/make-extra.sh NAME OUT.tar.gz [TAG] (TAG: the release it belongs to, default dev) +# Extras: +# vpn Xray-core + hev-socks5-tunnel (tools/fetch-xray.sh) and sing-box (tools/fetch-sing-box.sh), pinned by hash +# Layout (what mu300-update's extra_unpack checks): ./name ./release ./components ./bin/, owned by root. +set -eu +NAME=${1:?usage: tools/make-extra.sh NAME OUT.tar.gz [TAG]} +OUT=${2:?usage: tools/make-extra.sh NAME OUT.tar.gz [TAG]} +TAG=${3:-dev} +TOP=$(cd "$(dirname "$0")/.." && pwd) +tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT +mkdir -p "$tmp/x/bin" +case $NAME in + vpn) + sh "$TOP/tools/fetch-xray.sh" "$tmp/x/bin" >&2 + sh "$TOP/tools/fetch-sing-box.sh" "$tmp/x/bin/sing-box" >&2 + { + printf 'xray %s\n' "$(sed -n 's/^XRAY_VER=//p' "$TOP/tools/fetch-xray.sh")" + printf 'hev-socks5-tunnel %s\n' "$(sed -n 's/^HEV_VER=//p' "$TOP/tools/fetch-xray.sh")" + printf 'sing-box %s\n' "$(sed -n 's/^VER=//p' "$TOP/tools/fetch-sing-box.sh")" + } > "$tmp/x/components" ;; + *) echo "unknown extra '$NAME' (vpn)" >&2; exit 2 ;; +esac +echo "$NAME" > "$tmp/x/name" +echo "$TAG" > "$tmp/x/release" +# python's tarfile, not tar: the same root-owned archive from macOS (bsdtar) and Linux (GNU tar) +python3 - "$tmp/x" "$OUT" <<'PY' +import os, sys, tarfile +src, out = sys.argv[1], sys.argv[2] +def root(ti): + ti.uid = ti.gid = 0 + ti.uname = ti.gname = 'root' + return ti +with tarfile.open(out, 'w:gz', format=tarfile.GNU_FORMAT) as t: + for name in ('name', 'release', 'components', 'bin'): + t.add(os.path.join(src, name), arcname='./' + name, filter=root) +PY +echo "$NAME extra ($TAG): $OUT, $(du -h "$OUT" | cut -f1)" diff --git a/tools/make-release.sh b/tools/make-release.sh index bf5db1ab..b6eae1fe 100755 --- a/tools/make-release.sh +++ b/tools/make-release.sh @@ -31,8 +31,10 @@ docker run --rm -v "$TOP/tools":/src:ro -v "$IN/tools":/o mu300-kbuild sh -c ' mkdir -p /o/keys && gcc -O2 -static -o /o/keys/mu300-keys /src/keys/mu300-keys.c' # cltest links against Android's libraries at build time only; use a local build when there is one [ -f "$TOP/tools/gpu/cltest" ] && cp "$TOP/tools/gpu/cltest" "$IN/tools/gpu/cltest" -sh "$TOP/tools/fetch-sing-box.sh" "$IN/sing-box" -sh "$TOP/tools/fetch-xray.sh" "$IN" + +echo "==> extras" +# not in the images: mu300-extra installs them on the devices that want them (mu300-extra-.tar.gz) +sh "$TOP/tools/make-extra.sh" vpn "$D/mu300-extra-vpn.tar.gz" "$TAG" echo "==> kernel bundle" K=$D/kernel && mkdir -p "$K" @@ -72,7 +74,7 @@ for u in 24.04 26.04; do cltest=""; [ -f "$IN/tools/gpu/cltest" ] && cltest="-v $IN/tools/gpu/cltest:/cltest:ro" # shellcheck disable=SC2086 docker run --rm -v "$B":/w -v "$IN/out/modules":/kmods:ro -v "$IN/out":/kout:ro -v "$IN/tools/logdw/logdw":/logdw:ro \ - -v "$IN/tools/bt-init/mu300-bt-init":/bt-init:ro -v "$IN/tools/keys/mu300-keys":/keys:ro -v "$IN/sing-box":/sing-box:ro -v "$IN/xray":/xray:ro -v "$IN/hev-socks5-tunnel":/hev-socks5-tunnel:ro $cltest \ + -v "$IN/tools/bt-init/mu300-bt-init":/bt-init:ro -v "$IN/tools/keys/mu300-keys":/keys:ro $cltest \ -e MU300_VERSION="$TAG" mu300-ubuntu:$u bash /w/assemble.sh >/dev/null out=mu300-ubuntu-rootfs.tar.gz; [ $u = 24.04 ] || out=mu300-ubuntu-$u-rootfs.tar.gz mv "$B/mu300-ubuntu-$u-rootfs.tar.gz" "$D/$out"; rm -rf "$B" @@ -90,12 +92,18 @@ for a in mu300-kernel mu300-kernel-6.18 mu300-kernel-7.2 mu300-ubuntu-rootfs mu3 -e '^opt/mu300/android/.+' -e '__properties__|dev-properties' \ -e '(^|/)(libmali|libOpenCL|libGLES|libEGL|libvulkan)[^/]*\.so' \ -e '^etc/ssh/ssh_host_' -e '^etc/mu300/(hotspot|vpn|toolkit)\.conf$' -e '^etc/dropbear/dropbear_.*_host_key' \ + -e '^opt/mu300/bin/(xray|sing-box|hev-socks5-tunnel)$' \ | grep -vE '^opt/mu300/android/system/?$|^opt/mu300/android/system/bin/?$|^opt/mu300/android/system/bin/cltest$' || true) if [ -n "$bad" ]; then echo "$a contains files that must not be published:"; echo "$bad" | head -20; fail=1; fi mid=$(tar -xzOf "$D/$a.tar.gz" ./etc/machine-id 2>/dev/null || true) [ -z "$mid" ] || { echo "$a has a machine-id"; fail=1; } done # mu300-update refuses a kernel bundle without the SD host for a system on the card: every bundle must say it has it +# an extra holds what its name says, for the release it is published with (mu300-update compares ./release) +for x in vpn; do + [ "$(tar -xzOf "$D/mu300-extra-$x.tar.gz" ./name)" = $x ] && [ "$(tar -xzOf "$D/mu300-extra-$x.tar.gz" ./release)" = "$TAG" ] || + { echo "mu300-extra-$x.tar.gz is not the $x extra of $TAG"; fail=1; } +done for a in mu300-kernel mu300-kernel-6.18 mu300-kernel-7.2; do tar -xzOf "$D/$a.tar.gz" ./features 2>/dev/null | grep -qx sdcard || { echo "$a does not list sdcard in ./features"; fail=1; } done @@ -123,6 +131,7 @@ first with \`./install.sh --check\`. | mu300-ubuntu-rootfs.tar.gz | Ubuntu 24.04 LTS root filesystem | | mu300-ubuntu-26.04-rootfs.tar.gz | Ubuntu 26.04 LTS root filesystem | | mu300-openwrt-rootfs.tar.gz | OpenWrt 25.12.5 root filesystem | +| mu300-extra-vpn.tar.gz | the VPN engines, not part of the images: \`mu300-extra install vpn\` on the device (or the installer's question) puts them on the Linux partition; Xray-core $(sed -n 's/^XRAY_VER=//p' "$TOP/tools/fetch-xray.sh"), hev-socks5-tunnel $(sed -n 's/^HEV_VER=//p' "$TOP/tools/fetch-xray.sh"), sing-box $(sed -n 's/^VER=//p' "$TOP/tools/fetch-sing-box.sh") | | mu300-update | the on-device updater of this release (\`mu300-update apply\` switches to it before it changes anything) | The images contain **no proprietary files**: the installer pulls the Wi-Fi/Bluetooth firmware and the Android