Files
39b3d18ddf Storage: one lookup for the internal disk, eMMC or UFS (#396, step 1)
The internal disk and its partitions were found in seven places, each with
its own copy of "the mmcblk disk of type MMC": init, early-recorder,
android-vendor-start, mu300-cpu, mu300-update, and on the Android side the
installers' /dev/block/mmcblk0. A unit with UFS storage (the 1 TB U30 Air of
issue #396: sda, partitions sda1..sda75, no mmcblk at all) was found by none.

- rootfs/overlay/opt/mu300/lib/storage.sh: the one lookup. The eMMC as
  before (type MMC, else the first mmcblk disk that is not a card), else a
  SCSI disk below the UFS host that holds misc; never a card or a USB disk.
  Partitions are the disk's own sysfs children (mmcblk0pN or sdaN).
  After kanoqwq's storage.sh (his fe52afe3).
- init sources it from the ramdisk (/etc/mu300-storage.sh, which
  build-boot-image.py puts into every segment that has an init);
  early-recorder, android-vendor-start and mu300-cpu from /opt/mu300/lib.
  mu300-update carries a copy (a self-update runs it on a system that may
  not have the file yet); a test keeps it the same.
- Android side: the internal disk is the one that holds misc, as Android's
  by-name links say (tools/storage.sh's internal_disk, install.ps1,
  uninstall.ps1, and the same lines in android-install.sh,
  android-mount-mu300root.sh and android-web.sh); mmcblk0 when Android does
  not say. On an eMMC unit every device command is the same text as before.

Not here: the make-room path on 4096-byte logical blocks and an identity
without the eMMC CID (step 2).

Co-Authored-By: kanoqwq <kanoqwq@qq.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VVN2zDgf76kMDVSuWhHPUE
2026-10-31 01:31:23 +03:00

104 lines
4.6 KiB
Python

"""Shared helpers for the tests: the shells the scripts run under, stub commands, a scratch directory.
The device scripts run under dash (Ubuntu's /bin/sh), bash and busybox ash (OpenWrt); every shell test runs under
each of them that this machine has (MU300_TEST_SHELLS="dash,busybox sh" picks others). Commands a script calls that
touch the device (nft, ip, id, ...) are replaced by stubs in a directory put first on PATH.
"""
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
TOP = Path(__file__).resolve().parents[1]
BIN = TOP / 'rootfs' / 'overlay' / 'opt' / 'mu300' / 'bin'
# the tests that read what git tracks (file modes, ls-files) skip where there is no git or no checkout of this tree,
# e.g. a container with the worktree mounted but not the repository it belongs to
GIT_CHECKOUT = bool(shutil.which('git')) and subprocess.run(['git', '-C', str(TOP), 'rev-parse', '--git-dir'],
capture_output=True).returncode == 0
def _works(argv):
try:
return subprocess.run(argv + ['-c', 'true'], capture_output=True, timeout=10).returncode == 0
except (OSError, subprocess.TimeoutExpired):
return False
def shells():
"""[argv, ...] of the POSIX shells available here, e.g. [['dash'], ['bash'], ['busybox', 'sh']]."""
names = os.environ.get('MU300_TEST_SHELLS', 'dash,bash,busybox sh,sh').split(',')
found, seen = [], set()
for n in names:
argv = n.split()
path = shutil.which(argv[0])
if not path or not _works(argv):
continue
key = (os.path.realpath(path),) + tuple(argv[1:])
if key not in seen:
seen.add(key)
# by its full path: a test that stubs a command of the same name (busybox) must not replace the shell
found.append([path] + argv[1:])
return found
class ShellTest(unittest.TestCase):
"""A scratch directory (self.tmp) with a stub directory first on PATH (self.stub)."""
def setUp(self):
self.tmp = Path(tempfile.mkdtemp(prefix='mu300-test-'))
self.stubs = self.tmp / 'stubs'
self.stubs.mkdir()
self.shells = shells()
if not self.shells:
self.skipTest('no POSIX shell')
def tearDown(self):
shutil.rmtree(self.tmp, ignore_errors=True)
def stub(self, name, body):
"""A command NAME that runs BODY (sh); $STUBLOG is the scratch directory, for recording calls."""
p = self.stubs / name
p.write_text('#!/bin/sh\n' + body + '\n')
p.chmod(0o755)
def env(self, **extra):
e = dict(os.environ)
e['PATH'] = f'{self.stubs}{os.pathsep}{e.get("PATH", "")}'
e['STUBLOG'] = str(self.tmp)
e['LC_ALL'] = 'C.UTF-8' if os.name != 'nt' else e.get('LC_ALL', '')
# the shared internal-disk lookup that init, early-recorder, android-vendor-start and mu300-cpu source
e.setdefault('MU300_STORAGE_LIB', str(TOP / 'rootfs' / 'overlay' / 'opt' / 'mu300' / 'lib' / 'storage.sh'))
e.update({k: str(v) for k, v in extra.items()})
return e
def sh(self, shell, code, stdin=None, **env):
"""Run CODE with SHELL; returns the CompletedProcess (text)."""
return subprocess.run(shell + ['-c', code], input=stdin, capture_output=True, text=True,
env=self.env(**env), timeout=60)
def script(self, shell, path, *args, stdin=None, **env):
"""Run the script at PATH with SHELL (not its #! line: the point is to try every shell)."""
return subprocess.run(shell + [str(path)] + [str(a) for a in args], input=stdin, capture_output=True,
text=True, env=self.env(**env), timeout=60)
def each_shell(self):
"""The shells, one by one; an assertion that fails meanwhile names the shell it failed under.
Not a subTest around the yield: the loop body's exception never passes through this generator, so the subTest
recorded nothing for it, and when the generator was closed later (the failure's traceback keeps it alive past
the end of the test) the subTest caught the GeneratorExit itself ("generator ignored GeneratorExit") and then
read the finished test's outcome ("'NoneType' object has no attribute 'success'"): two spurious errors."""
try:
for s in self.shells:
self.shell_now = ' '.join(s)
yield s
finally:
self.shell_now = None
def _formatMessage(self, msg, standardMsg):
text = super()._formatMessage(msg, standardMsg)
shell = getattr(self, 'shell_now', None)
return f'{text} [shell={shell}]' if shell else text