Files
mu300-linux/tools/android-install.sh
T
Abdurrahman Kaan DikeçandClaude Opus 5 d6001458bb install/uninstall: detect Linux mode, update instead of overwrite, verified secure erase
- tools/linux-mode.sh: when the device is running MU300 Linux there is no adb device; both scripts detect it, offer
  to reboot it into Android over SSH and wait for adb (install.ps1/uninstall.ps1 do the same on Windows)
- install: "update or wipe" for an existing installation. Update keeps /etc/mu300, users and home directories,
  /usr/local, SSH host keys, OpenWrt UCI config, the hotspot settings and the services enabled by the user
  (symlinks are copied only when the unit still exists; -L, since they look broken from Android)
- uninstall: secure erase is the default - discard if available, overwrite the whole region, then verify 32 samples
  are empty and refuse to report success otherwise; quick (headers only) and keep remain
- mu300-vpn.service: sing-box exits with "missing default interface" when it starts before the modem has a route;
  Restart=always with no start limit instead of giving up
- verified on the device: update kept canaries in /etc/mu300 and /home, the toolkit profile, the Ookla config,
  /usr/local/bin/speedtest and the enabled (and afterwards running) VPN service

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Z17MBkdHmZPvNPNSLxcHk
2026-09-17 22:51:25 +03:00

150 lines
7.7 KiB
Bash
Executable File

#!/system/bin/sh
# Device side of install.sh (runs as root on Android). Settings come from /data/local/tmp/mu300-install.env:
# OFF SIZE free eMMC region (bytes) after the last GPT partition, as strings
# OFF_S SIZE_S the same in 512-byte sectors (Android's mksh has 32-bit arithmetic: never compute with bytes)
# FORMAT=0|1 create the ext4 filesystem "mu300root" in that region
# OSES="ubuntu openwrt" systems to (re)install from /data/local/tmp/mu300-<os>.tar.gz
# (plus mu300-vendor-<os>.tar.gz with the device's own vendor files for prebuilt images)
# WIPE_LEGACY=0|1 remove a first-generation Ubuntu that lives directly in the filesystem root
# UPDATE=0|1 keep the settings and user data of the systems being reinstalled
# BOOT_OS system started by the initramfs
# DEFAULT_LINUX=0|1 keep booting Linux (otherwise every Linux boot is one-shot and returns to Android)
# PWHASH SHA-512 crypt hash for the "ubuntu" (Ubuntu) and "root" (OpenWrt) accounts
# IMPORT_HOTSPOT=0|1 copy Android's hotspot SSID/passphrase into each system
set -e
T=/data/local/tmp
. $T/mu300-install.env
M=$T/mu300root
say() { echo "[device] $*"; }
# --- the region must not overlap any partition (checked again here, on the device itself)
end=0
for p in /sys/block/mmcblk0/mmcblk0p*; do
e=$(( $(cat $p/start) + $(cat $p/size) ))
[ $e -gt $end ] && end=$e
done
disk=$(cat /sys/block/mmcblk0/size)
[ "$OFF_S" -ge $end ] && [ $((OFF_S + SIZE_S)) -le $((disk - 34)) ] || { say "region overlaps partitions or the backup GPT"; exit 1; }
attach() {
for o in /sys/block/loop*/loop/offset; do
[ "$(cat $o 2>/dev/null)" = "$OFF" ] && { say "region already attached (${o%/loop/offset})"; exit 1; }
done
f=$(losetup -f 2>&1 | grep -o '/dev/block/loop[0-9]*' | head -1); n=${f##*loop}
L=/dev/block/loop$n
[ -b "$L" ] || mknod "$L" b $(cut -d: -f1 /sys/block/loop$n/dev) $(cut -d: -f2 /sys/block/loop$n/dev) 2>/dev/null || [ -b "$L" ]
losetup -o $OFF -S $SIZE "$L" /dev/block/mmcblk0
[ "$(cat /sys/block/loop$n/loop/offset)" = "$OFF" ] && [ "$(blockdev --getsize64 $L)" = "$SIZE" ] || { losetup -d $L; say "loop setup mismatch"; exit 1; }
}
sb() { dd if=/dev/block/mmcblk0 bs=512 skip=$OFF_S count=4 2>/dev/null | dd bs=1 skip=$1 count=$2 2>/dev/null; }
magic=$(sb 1080 2 | od -An -tx1 | tr -d ' \n')
label=$(sb 1144 16 | tr -d '\000')
if [ "$FORMAT" = 1 ]; then
if [ "$magic" = 53ef ] && [ "$label" != mu300root ]; then say "refusing to format: foreign ext4 ($label) in the region"; exit 1; fi
attach
say "creating ext4 mu300root on $L ($((SIZE_S / 2048)) MiB)"
mke2fs -t ext4 -L mu300root -F "$L" >/dev/null
losetup -d "$L"
elif [ "$magic" != 53ef ] || [ "$label" != mu300root ]; then
say "no mu300root filesystem in the region (run with FORMAT=1)"; exit 1
fi
MU300_OFF=$OFF MU300_SIZE=$SIZE sh $T/android-mount-mu300root.sh $M
trap 'sync; sh $T/android-mount-mu300root.sh -u $M >/dev/null 2>&1; true' EXIT
if [ "$WIPE_LEGACY" = 1 ] && { [ -x $M/lib/systemd/systemd ] || [ -L $M/lib ]; }; then
say "removing the root-level Ubuntu"
for e in $M/* $M/.[!.]*; do
case "${e##*/}" in lost+found|.mu300|ubuntu|openwrt) ;; *) rm -rf "$e" ;; esac
done
fi
ssid=; psk=
if [ "$IMPORT_HOTSPOT" = 1 ]; then
X=/data/misc/apexdata/com.android.wifi/WifiConfigStoreSoftAp.xml
ssid=$(sed -n 's/.*<string name="WifiSsid">&quot;\(.*\)&quot;<\/string>.*/\1/p; s/.*<string name="WifiSsid">\([^&<]*\)<\/string>.*/\1/p' $X 2>/dev/null | head -1)
psk=$(sed -n 's/.*<string name="Passphrase">\(.*\)<\/string>.*/\1/p' $X 2>/dev/null | head -1 | sed "s/&amp;/\&/g; s/&lt;/</g; s/&gt;/>/g; s/&quot;/\"/g; s/&apos;/'/g")
[ -n "$ssid" ] && [ ${#psk} -ge 8 ] || { say "no usable Android hotspot config, a random password will be generated"; ssid=; psk=; }
fi
for os in $OSES; do
tarball=$T/mu300-$os.tar.gz
[ -f $tarball ] || { say "missing $tarball"; exit 1; }
say "installing $os"
rm -rf $M/$os.new && mkdir $M/$os.new
tar -xzpf $tarball -C $M/$os.new
# prebuilt images: firmware and Android userspace pulled from this device by install.sh (tools/vendor-overlay.py)
if [ -f $T/mu300-vendor-$os.tar.gz ]; then
tar -xzpf $T/mu300-vendor-$os.tar.gz -C $M/$os.new
rm -f $T/mu300-vendor-$os.tar.gz
fi
# update: carry the settings and user data of the previous installation over to the new system
if [ "${UPDATE:-0}" = 1 ] && [ -d $M/$os ]; then
case $os in
ubuntu) keep="etc/mu300 etc/ssh etc/hostname etc/localtime etc/timezone etc/fstab home root srv usr/local var/lib/bluetooth" ;;
openwrt) keep="etc/config etc/mu300 etc/dropbear etc/rc.local root" ;;
esac
kept=
for k in $keep; do
[ -e "$M/$os/$k" ] || continue
mkdir -p "$M/$os.new/$(dirname $k)"
rm -rf "$M/$os.new/$k"
cp -a "$M/$os/$k" "$M/$os.new/$k" && kept="$kept $k"
done
# services the user enabled or disabled themselves: copy the extra symlinks over, but only when the unit
# they point at exists in the new system (stale units from an older release must not come back)
extra=
case $os in
ubuntu)
for w in $M/$os/etc/systemd/system/*.wants; do
[ -d "$w" ] || continue
t=${w##*/}
for l in "$w"/*; do
# -L, not -e: the links point at absolute paths inside the Linux root, so from Android
# they all look broken
[ -L "$l" ] || [ -e "$l" ] || continue
u=${l##*/}
if [ -L "$M/$os.new/etc/systemd/system/$t/$u" ]; then continue; fi
[ -e "$M/$os.new/etc/systemd/system/$u" ] || [ -e "$M/$os.new/usr/lib/systemd/system/$u" ] || continue
mkdir -p "$M/$os.new/etc/systemd/system/$t"
cp -a "$l" "$M/$os.new/etc/systemd/system/$t/$u" && extra="$extra $u"
done
done ;;
openwrt)
for l in $M/$os/etc/rc.d/*; do
[ -L "$l" ] || [ -e "$l" ] || continue
u=${l##*/}
if [ -L "$M/$os.new/etc/rc.d/$u" ]; then continue; fi
[ -x "$M/$os.new/etc/init.d/${u#S??}" ] || [ -x "$M/$os.new/etc/init.d/${u#K??}" ] || continue
cp -a "$l" "$M/$os.new/etc/rc.d/$u" && extra="$extra $u"
done ;;
esac
say "kept from the previous $os:$kept"
[ -n "$extra" ] && say "kept enabled services:$extra"
fi
rm -rf $M/$os && mv $M/$os.new $M/$os
R=$M/$os
mkdir -p $R/etc/mu300
if [ -n "$ssid" ] && ! { [ "${UPDATE:-0}" = 1 ] && [ -s $R/etc/mu300/hotspot.conf ]; }; then
umask 077
printf 'SSID=%s\nPSK=%s\nBAND=5\nCHANNEL=auto\nCOUNTRY=TR\n' "$ssid" "$psk" > $R/etc/mu300/hotspot.conf
chmod 600 $R/etc/mu300/hotspot.conf
umask 022
fi
if [ "$DEFAULT_LINUX" = 1 ]; then echo linux > $R/etc/mu300/default-boot; else rm -f $R/etc/mu300/default-boot; fi
if [ -n "$PWHASH" ]; then
case $os in
ubuntu) sed -i "s|^ubuntu:[^:]*:|ubuntu:$PWHASH:|" $R/etc/shadow ;;
openwrt) sed -i "s|^root:[^:]*:|root:$PWHASH:|" $R/etc/shadow ;;
esac
fi
rm -f $tarball
done
mkdir -p $M/.mu300
echo "$BOOT_OS" > $M/.mu300/boot-os
[ -n "$ssid" ] && say "hotspot: SSID $ssid imported (passphrase ${#psk} chars)"
say "installed: $(ls -d $M/ubuntu $M/openwrt 2>/dev/null | sed "s|$M/||g" | tr '\n' ' ')boot-os=$BOOT_OS default-linux=$DEFAULT_LINUX"
rm -f $T/mu300-install.env
echo MU300-INSTALL-OK # install.sh checks for this line (set -e stops before it on any failure)