android-install: the filesystem on the SD card (SD_MODE), never over a foreign ext4 or a card Android keeps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Abdurrahman Kaan DikeçandClaude Opus 5.5 committed 2026-10-04 21:41:59 +03:00
1 parent 26b6890829
commit 8892094400
4 files changed
+199 -2

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ macOS and Windows (`.github/workflows/tests.yml`).
| `test_vpn.py` | `mu300-vpn`: VLESS URI parsing, JSON, which networks stay out of the tunnel, the sing-box config |
| `test_update.py` | `mu300-update`: release files per system and kernel, boot image byte helpers, whether a kernel bundle may go onto this device |
| `test_boot_init.py` | `boot/init`: the card is looked for before the internal region, under any `mmcblkN`; a foreign or empty card falls back; the 300 s timer and the conditional wait |
| `test_android_install.py` | `tools/android-install.sh` on the SD card: a blank card is formatted, a foreign ext4 and an adopted or busy card are refused |
| `test_boot_image.py` | `boot/build-boot-image.py`: the generic ramdisk, the U30 Air's modules and order |
| `installer.Tests.ps1` | `install.ps1`: `T` with every translation, `NormalizeAnswer`, `Gib` |
+118
View File
@@ -0,0 +1,118 @@
"""tools/android-install.sh: the SD card branch, cut out between its markers and run with stubs for Android's
tools (sm, mke2fs, umount) and files standing in for block devices."""
import re
from helpers import TOP, ShellTest
from test_boot_init import fake_ext4
SRC = (TOP / 'tools' / 'android-install.sh').read_text()
class SdCard(ShellTest):
def setUp(self):
super().setUp()
m = re.search(r'# --- sd begin\n(.*?)# --- sd end', SRC, re.S)
self.assertIsNotNone(m, 'android-install.sh has no sd block')
self.fn = m.group(1)
self.dev = self.tmp / 'mmcblk1p1'
self.stub('mke2fs', 'echo "mke2fs $*" >> "$STUBLOG/calls"')
self.stub('umount', 'echo "umount $*" >> "$STUBLOG/calls"')
(self.tmp / 'mounts').write_text('')
def run_fn(self, shell, call, volumes='', still_mounted=False):
(self.tmp / 'volumes').write_text(volumes)
after = volumes if still_mounted else volumes.replace(' mounted ', ' unmounted ')
(self.tmp / 'volumes.after').write_text(after)
self.stub('sm', 'case "$1" in list-volumes) cat "$STUBLOG/volumes" ;; '
'unmount) echo "sm unmount $2" >> "$STUBLOG/calls"; cp "$STUBLOG/volumes.after" "$STUBLOG/volumes" ;; esac')
code = ('say() { echo "[device] $*"; }\n' + f'MU300_MOUNTS="{self.tmp}/mounts"\n' + self.fn
+ f'\n{call}\necho "rc=$?"')
r = self.sh(shell, code)
calls = (self.tmp / 'calls').read_text() if (self.tmp / 'calls').exists() else ''
(self.tmp / 'calls').unlink(missing_ok=True)
return r.stdout, calls
def test_blank_card_is_formatted(self):
self.dev.write_bytes(bytes(4096))
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=0', out)
self.assertIn(f'mke2fs -t ext4 -L mu300sd -F {self.dev}', calls)
def test_foreign_ext4_is_refused(self):
fake_ext4(self.dev, 'photos')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=1', out)
self.assertIn('photos', out)
self.assertNotIn('mke2fs', calls)
def test_existing_installation_is_kept_or_replaced(self):
fake_ext4(self.dev, 'mu300sd')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 0')
self.assertIn('rc=0', out); self.assertNotIn('mke2fs', calls)
out, calls = self.run_fn(shell, f'sd_prepare {self.dev} 1')
self.assertIn('rc=0', out); self.assertIn('mke2fs', calls)
def test_keep_without_installation_fails(self):
self.dev.write_bytes(bytes(4096))
for shell in self.each_shell():
out, _ = self.run_fn(shell, f'sd_prepare {self.dev} 0')
self.assertIn('rc=1', out)
def test_android_releases_the_card(self):
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}', volumes='public:179,1 mounted ABCD-1234\n')
self.assertIn('rc=0', out)
self.assertIn('sm unmount public:179,1', calls)
def test_card_android_keeps_is_refused(self):
for shell in self.each_shell():
out, _ = self.run_fn(shell, f'sd_release {self.dev}', volumes='public:179,1 mounted ABCD-1234\n',
still_mounted=True)
self.assertIn('rc=1', out)
def test_adopted_card_is_refused(self):
# a card adopted as internal storage is encrypted and part of Android's data: never ours to take
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}', volumes='private:179,3 mounted 1234-uuid\n')
self.assertIn('rc=1', out)
self.assertNotIn('sm unmount', calls)
def test_builtin_private_storage_is_not_an_adopted_card(self):
# Android lists its own data partition as "private mounted null" (no colon): that is no reason to refuse
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}',
volumes='private mounted null\nemulated;0 mounted null\n'
'public:179,1 mounted ABCD-1234\n')
self.assertIn('rc=0', out)
self.assertIn('sm unmount public:179,1', calls)
def test_leftover_mounts_of_the_card_only(self):
# whatever is still mounted from the card goes; the eMMC (mmcblk0) and another card (mmcblk10) stay
t = self.tmp
(t / 'mounts').write_text(f'{t}/mmcblk0p40 /data ext4 rw 0 0\n'
f'{t}/mmcblk1p1 /mnt/media_rw/ABCD vfat rw 0 0\n'
f'{t}/mmcblk1p2 /mnt/other ext4 rw 0 0\n'
f'{t}/mmcblk10p1 /mnt/ten vfat rw 0 0\n')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {self.dev}')
self.assertIn('rc=0', out)
self.assertIn('umount /mnt/media_rw/ABCD', calls)
self.assertIn('umount /mnt/other', calls)
self.assertNotIn('/data', calls); self.assertNotIn('/mnt/ten', calls)
self.assertNotIn('mmcblk0', calls); self.assertNotIn('mmcblk10', calls)
def test_whole_card_never_reaches_the_emmc(self):
# a card without a partition table: SD_DEV is mmcblk1 itself, and mmcblk0 must stay mounted
t = self.tmp
(t / 'mounts').write_text(f'{t}/mmcblk0 /emmc ext4 rw 0 0\n'
f'{t}/mmcblk0p40 /data ext4 rw 0 0\n'
f'{t}/mmcblk1 /mnt/media_rw/ABCD vfat rw 0 0\n')
for shell in self.each_shell():
out, calls = self.run_fn(shell, f'sd_release {t}/mmcblk1')
self.assertIn('rc=0', out)
self.assertIn('umount /mnt/media_rw/ABCD', calls)
self.assertNotIn('/data', calls); self.assertNotIn('/emmc', calls)
self.assertNotIn('mmcblk0', calls)
+65 -1
View File
@@ -2,7 +2,10 @@
# 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
# SD_MODE=0|1 SD_DEV with SD_MODE=1 the filesystem (label mu300sd) is the SD card block device SD_DEV instead of
# that region; OFF/SIZE are then not used
# INTERNAL_EXISTS=0|1 with SD_MODE=1: an internal mu300root exists at OFF/SIZE and gets the root-on-sd marker
# FORMAT=0|1 create the ext4 filesystem (mu300root in the region, mu300sd on the card)
# 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
@@ -18,7 +21,52 @@ T=/data/local/tmp
. $T/mu300-install.env
M=$T/mu300root
say() { echo "[device] $*"; }
# --- sd begin
sd_label() { # the ext4 label of a device, empty when it is not ext4
[ "$(dd if="$1" bs=1 skip=1080 count=2 2>/dev/null | od -An -tx1 | tr -d ' \n')" = 53ef ] || return 0
dd if="$1" bs=1 skip=1144 count=16 2>/dev/null | tr -d '\000'
}
sd_release() { # sd_release DEV: make Android let go of the card; nothing has been written when this fails
vols=$(sm list-volumes 2>/dev/null)
# adopted as internal storage: encrypted, and part of Android's data. Android's own data partition is the
# line "private mounted null" (no colon), which is not a card.
echo "$vols" | grep -q '^private:[^ ]* mounted' && {
say "the SD card is adopted as Android internal storage; format it as portable storage first"; return 1; }
for v in $(echo "$vols" | sed -n 's/^\(public:[^ ]*\) mounted.*/\1/p'); do
say "asking Android to unmount $v"
sm unmount "$v" 2>/dev/null || true
done
if sm list-volumes 2>/dev/null | grep -q '^public:[^ ]* mounted'; then
say "Android keeps the SD card mounted; eject it under Settings > Storage and run the installer again"; return 1
fi
# anything still mounted from the card (any of its partitions). The disk is DEV without a trailing p<digits>;
# a whole-card DEV (mmcblk1) stays as it is, so the eMMC (mmcblk0) can never match
d=$1
case $d in *mmcblk[0-9]*p[0-9]*) d=${d%p*} ;; esac
for m in $(sed -n -e "s|^$d \([^ ]*\) .*|\1|p" -e "s|^${d}p[0-9][0-9]* \([^ ]*\) .*|\1|p" \
"${MU300_MOUNTS:-/proc/mounts}" 2>/dev/null); do
umount "$m" 2>/dev/null || umount -f "$m" 2>/dev/null || true
done
}
sd_prepare() { # sd_prepare DEV FORMAT: create mu300sd, or check that it is there
label=$(sd_label "$1")
if [ "$2" = 1 ]; then
[ -z "$label" ] || [ "$label" = mu300sd ] || { say "refusing to format: foreign ext4 ($label) on the SD card"; return 1; }
say "creating ext4 mu300sd on $1"
mke2fs -t ext4 -L mu300sd -F "$1" >/dev/null
elif [ "$label" != mu300sd ]; then
say "no mu300sd filesystem on $1 (run with FORMAT=1)"; return 1
fi
}
# --- sd end
if [ "${SD_MODE:-0}" = 1 ]; then
[ -b "${SD_DEV:?SD_DEV is not set}" ] || { say "no block device $SD_DEV (is the SD card inserted?)"; exit 1; }
sd_release "$SD_DEV" || exit 1
sd_prepare "$SD_DEV" "$FORMAT" || exit 1
MU300_SD_DEV=$SD_DEV sh $T/android-mount-mu300root.sh $M
trap 'sync; sh $T/android-mount-mu300root.sh -u $M >/dev/null 2>&1; true' EXIT
else
# --- the region must not overlap any partition (checked again here, on the device itself)
end=0
for p in /sys/block/mmcblk0/mmcblk0p*; do
@@ -54,6 +102,7 @@ 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
fi
if [ "$WIPE_LEGACY" = 1 ] && { [ -x $M/lib/systemd/systemd ] || [ -L $M/lib ]; }; then
say "removing the root-level Ubuntu"
@@ -127,6 +176,8 @@ for os in $OSES; do
fi
rm -rf $M/$os && mv $M/$os.new $M/$os
R=$M/$os
# Ubuntu remounts / by label; on the card that label is mu300sd
if [ "${SD_MODE:-0}" = 1 ] && [ -f $R/etc/fstab ]; then sed -i 's|^LABEL=mu300root |LABEL=mu300sd |' $R/etc/fstab; fi
mkdir -p $R/etc/mu300
if [ -n "$ssid" ] && ! { [ "${UPDATE:-0}" = 1 ] && [ -s $R/etc/mu300/hotspot.conf ]; }; then
umask 077
@@ -154,5 +205,18 @@ esac
rm -f $M/boot/installed.tag
[ -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"
# A device with an internal installation as well: tell its init that the card is what should boot (it then waits
# for the card instead of taking the internal system at once). Best effort - the card boots without it whenever
# the SD host is quick enough.
if [ "${SD_MODE:-0}" = 1 ] && [ -n "${OFF:-}" ] && [ "${INTERNAL_EXISTS:-0}" = 1 ]; then
I=$T/mu300root-internal
if MU300_OFF=$OFF MU300_SIZE=$SIZE sh $T/android-mount-mu300root.sh $I >/dev/null 2>&1; then
# in an if, so that a failed write (set -e) cannot fail an installation that is already complete
if mkdir -p $I/.mu300 && : > $I/.mu300/root-on-sd; then
say "marked the internal installation: the SD card boots first"
fi
sync; sh $T/android-mount-mu300root.sh -u $I >/dev/null 2>&1 || true
fi
fi
rm -f $T/mu300-install.env
echo MU300-INSTALL-OK # install.sh checks for this line (set -e stops before it on any failure)
+15 -1
View File
@@ -1,6 +1,7 @@
#!/system/bin/sh
# Attach the mu300root ext4 (free eMMC region) on Android via a bounded loop device and mount it.
# usage: android-mount-mu300root.sh <mountpoint> | android-mount-mu300root.sh -u <mountpoint>
# MU300_SD_DEV=/dev/block/mmcblk1p1 android-mount-mu300root.sh <mountpoint> (the mu300sd ext4 on the SD card)
set -e
# region of the free eMMC space after the last GPT partition (install.sh passes the values it computed)
OFF=${MU300_OFF:-27762098176}
@@ -11,7 +12,20 @@ SIZE=${MU300_SIZE:-}
if [ "$1" = -u ]; then
L=$(grep " $2 " /proc/mounts | cut -d' ' -f1)
umount "$2"; [ -n "$L" ] && losetup -d "$L"; echo UNMOUNTED; exit 0
# only a loop is ours to detach: the SD card is a block device of its own
umount "$2"; case "$L" in */loop*) losetup -d "$L" ;; esac; echo UNMOUNTED; exit 0
fi
# the SD card: a block device of its own, no loop. Same checks as for the region, for the card's label.
if [ -n "${MU300_SD_DEV:-}" ]; then
grep -q "^$MU300_SD_DEV " /proc/mounts && { echo "ALREADY-MOUNTED $MU300_SD_DEV"; exit 1; }
magic=$(dd if="$MU300_SD_DEV" bs=1 skip=1080 count=2 2>/dev/null | od -An -tx1 | tr -d ' \n')
[ "$magic" = 53ef ] || { echo "NO-EXT4 magic=$magic"; exit 1; }
label=$(dd if="$MU300_SD_DEV" bs=1 skip=1144 count=16 2>/dev/null | tr -d '\000')
[ "$label" = mu300sd ] || { echo "NOT-MU300SD label=$label"; exit 1; }
mkdir -p "$1"
mount -t ext4 -o noatime "$MU300_SD_DEV" "$1"
echo "MOUNTED $MU300_SD_DEV on $1"
exit 0
fi
MP=$1
# never attach the same ext4 twice (two loops mounting one filesystem corrupts it); a leftover loop from an