mirror of
https://github.com/dikeckaan/mu300-linux.git
synced 2026-12-06 00:03:00 +00:00
install.sh run from a folder on FAT or exFAT under macOS stored every imported file and directory as 0700 root:root (the file system keeps no modes, macOS reports 0700, tools/vendor-overlay.py took what it was told). modem_control and refnotify run as uid 1000 or mu300vnd and keep no capability that reads past a mode: the property area unreadable ends at "can't get modem type!", vendor/etc unreadable at "can't get modem img!" - the CP is never loaded, the AT ttys answer ENODEV. Reproduced on the F50-B on 7.2.10 and on 5.4.254 (the kernel makes no difference), on Ubuntu and on openwrt-luci. - android-vendor-start: before anything is started, the five imported trees are checked with one find and set right where they are not: root's, directories 0755 (0711 left), programs 0755, the property files 0444, other files 0644. One line says what was found, in the service's log and in the kernel's; marks in the boot timeline. An update carries these files over as they are, so this is what repairs an installed system. - tools/vendor-overlay.py: the mode of an entry comes from where it goes, never from the host's file system. - The Magisk installer and android-install.sh run under umask 022 (magisk --install-module gives 000: sixteen directories 0777 and ld.config.txt 0666 in a system the v2026.10.22 zip installed), and the overlay is packed as root's (bionic was stored as Android's system, uid 1000: the first login account of the installed system). - android-vendor-start makes /var/log before it starts logdw: on OpenWrt it can run before the boot script that creates it, and then there was no vendor log at all - on three of four boots of the F50-B. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVN2zDgf76kMDVSuWhHPUE
803 lines
48 KiB
Bash
Executable File
803 lines
48 KiB
Bash
Executable File
#!/system/bin/sh
|
|
# Device side of install.sh (runs as root on Android). Settings come from $T/mu300-install.env (T below):
|
|
# 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)
|
|
# 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 openwrt-luci" systems to (re)install from $T/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)
|
|
# BOOT_ATTEMPTS=1-6 with DEFAULT_LINUX=1: failed boots in a row before Android (.mu300/boot-attempts)
|
|
# PWHASH SHA-512 crypt hash for the "ubuntu" (Ubuntu) and "root" (OpenWrt) accounts; empty with
|
|
# UPDATE=1: the accounts and passwords of the previous installation stay as they are
|
|
# PW_SET=0|1 the Magisk installer: 1 puts PWHASH into that account even when it is empty (OpenWrt's root
|
|
# without a password, as the image ships it); 0 or not set (install.sh): as above
|
|
# IMPORT_HOTSPOT=0|1 copy Android's hotspot SSID/passphrase into each system
|
|
# KERNEL=5.4|6.18|7.2 the kernel in the new boot image; mu300-update keeps installing that one (boot/kernel)
|
|
# ADB=0|1 ADB_KEY adb over USB to each system on (etc/mu300/adb), for the adb public key ADB_KEY ("BASE64 name",
|
|
# etc/mu300/adb_keys)
|
|
# Extras pushed as $T/mu300-extra-<name>.tar.gz (the work directory, see below) go to extra/<name> on the Linux partition
|
|
# (the VPN module, github.com/dikeckaan/mu300-linux-vpn, as mu300-extra-vpn.tar.gz; each system links it into itself
|
|
# at its first boot: mu300-extra link runs the module's hooks/link).
|
|
# Environment (not the env file): MU300_LOCK_DIR, where the lock of a timed-out SD-card stage lives (default $T;
|
|
# the Magisk installer: /data/adb), MU300_STAGE_STALL / MU300_STAGE_POLL / MU300_BOOT_ID, the stage guard's knobs for
|
|
# the tests (see the stage block).
|
|
set -e
|
|
# the systems' directories are made here: never with the modes of a caller whose umask is not this one
|
|
umask 022
|
|
# install.sh pushes everything to /data/local/tmp. The Magisk installer runs this as root from a directory only root
|
|
# can write (MU300_DEVICE_WORK): files in /data/local/tmp can be replaced by the shell user after they were checked.
|
|
T=${MU300_DEVICE_WORK:-/data/local/tmp}
|
|
. $T/mu300-install.env
|
|
M=$T/mu300root
|
|
say() { echo "[device] $*"; }
|
|
# --- stage begin
|
|
# The long stages on an SD card run bounded (FINDINGS 50). A card, or the SD host, that stops answering leaves a
|
|
# request in the kernel that never completes: the command sits in D state, kill -9 does nothing, and every retry adds
|
|
# another one (38a's dd past the end of the eMMC did the same, one core at 100 % until a reboot). So in SD mode each
|
|
# long stage - the format, the unpack of every system, the flush at the end - runs in a session of its own (setsid:
|
|
# one process group to end) under a deadline, and is ended when the card's completion counters
|
|
# (/sys/class/block/DISK/stat) have not moved for STAGE_STALL seconds. The installer then writes nothing more: no
|
|
# sync and no unmount (both would wait on the same request), the card stays mounted, and a lock keyed by Android's
|
|
# boot id refuses every run until Android has rebooted. On the eMMC the stages run as they are: Android itself keeps
|
|
# the eMMC's counters moving, and the eMMC has not failed this way. (After kanoqwq's TF installer.)
|
|
STAGE_DISK= # the card's disk (mmcblk1), set in SD mode; empty: a stage runs as it is
|
|
STAGE_STALL=${MU300_STAGE_STALL:-120} # seconds without I/O progress that end a stage
|
|
STAGE_POLL=${MU300_STAGE_POLL:-2}
|
|
STAGE_LOCK=${MU300_LOCK_DIR:-$T}/mu300-install.stalled
|
|
STAGE_BOOT_ID=${MU300_BOOT_ID:-/proc/sys/kernel/random/boot_id}
|
|
STALLED= # set once a stage timed out: sd_exit then flushes and unmounts nothing
|
|
boot_id() { _bi=$(cat "$STAGE_BOOT_ID" 2>/dev/null | tr -d ' \n'); echo "${_bi:-unknown}"; }
|
|
stall_check() { # a lock of this Android boot refuses the run; one of an earlier boot is stale and goes
|
|
[ -f "$STAGE_LOCK" ] || return 0
|
|
if [ "$(sed -n 1p "$STAGE_LOCK" 2>/dev/null)" != "$(boot_id)" ]; then rm -f "$STAGE_LOCK"; return 0; fi
|
|
say "an earlier run of the installer timed out in this Android boot ($(sed -n 2p "$STAGE_LOCK")): the SD card request it waited on is still stuck in the kernel"
|
|
say "reboot Android, then run the installer again (nothing was written now; the lock is $STAGE_LOCK)"
|
|
return 1
|
|
}
|
|
stall_clear() { rm -f "$STAGE_LOCK"; }
|
|
stage_stat() { # the card's completion counters: reads and writes completed, sectors read and written (the time
|
|
# fields keep moving while a request only waits); nothing when the file cannot be read. Always true.
|
|
set -- $(cat "${MU300_SYSFS:-/sys}/class/block/$STAGE_DISK/stat" 2>/dev/null)
|
|
[ $# -lt 7 ] || echo "$1 $3 $5 $7"
|
|
}
|
|
stage_stop() { # stage_stop PGID: TERM, then KILL, for the whole session (a process in D state ignores both; it is
|
|
# never waited for). dash and busybox take -PGID, bash wants -- first; a shell that takes neither gets the pid.
|
|
kill -TERM -- "-$1" 2>/dev/null || kill -TERM "-$1" 2>/dev/null || kill -TERM "$1" 2>/dev/null || true
|
|
sleep 1
|
|
kill -KILL -- "-$1" 2>/dev/null || kill -KILL "-$1" 2>/dev/null || kill -KILL "$1" 2>/dev/null || true
|
|
}
|
|
# stage_run NAME LIMIT CMD...: in SD mode CMD in a session of its own, ended after LIMIT seconds, or after
|
|
# STAGE_STALL seconds without I/O progress on STAGE_DISK: the timeout is reported, locks, and returns 1. Otherwise
|
|
# CMD's status. On the eMMC (STAGE_DISK empty) CMD runs as it is.
|
|
stage_run() {
|
|
_sn=$1 _sl=$2; shift 2
|
|
if [ -z "$STAGE_DISK" ]; then "$@"; return; fi
|
|
_sd=$T/mu300-stage.$$; rm -rf "$_sd"; mkdir -p "$_sd"
|
|
_sz=$(stage_stat)
|
|
[ -n "$_sz" ] || say "cannot read the I/O counters of $STAGE_DISK: the $_sn stage is bounded by its limit of $_sl s only"
|
|
# the worker: a session of its own where setsid exists (Android's toybox has it), its pid - the group to end - in
|
|
# one file, the command's status in another. It is never waited for before that status is there: a process stuck
|
|
# in the kernel never ends, and a wait on it would hang the installer the way the dd of 38a did.
|
|
if command -v setsid >/dev/null 2>&1; then _ss=setsid; else _ss=; fi
|
|
$_ss sh -c '_d=$1; shift; echo $$ > "$_d/pid"; "$@"; echo $? > "$_d/rc.tmp" && mv "$_d/rc.tmp" "$_d/rc"' sh "$_sd" "$@" &
|
|
_wp=$!
|
|
_t0=$(date +%s); _tl=$_t0; _why=
|
|
while [ ! -f "$_sd/rc" ]; do
|
|
sleep "$STAGE_POLL"
|
|
[ ! -f "$_sd/rc" ] || break
|
|
_now=$(date +%s); _cur=$(stage_stat)
|
|
if [ "$_cur" != "$_sz" ]; then _tl=$_now; _sz=$_cur; fi
|
|
if [ $((_now - _t0)) -ge "$_sl" ]; then _why="it ran longer than $_sl s"
|
|
elif [ -n "$_cur" ] && [ $((_now - _tl)) -ge "$STAGE_STALL" ]; then _why="no I/O progress on $STAGE_DISK for $STAGE_STALL s"
|
|
fi
|
|
[ -z "$_why" ] || break
|
|
# a worker that is gone without its status (ended from outside): a failed stage, not a hang
|
|
_lp=$(cat "$_sd/pid" 2>/dev/null); kill -0 "${_lp:-$_wp}" 2>/dev/null || { sleep 1; [ -f "$_sd/rc" ] || echo 1 > "$_sd/rc"; }
|
|
done
|
|
if [ -n "$_why" ]; then
|
|
stage_stop "$(cat "$_sd/pid" 2>/dev/null || echo "$_wp")"
|
|
STALLED=1
|
|
{ boot_id; echo "stage $_sn, $(date '+%Y-%m-%d %H:%M'): $_why"; } > "$STAGE_LOCK" 2>/dev/null || true
|
|
say "the $_sn stage timed out ($_why) and was stopped; nothing more is written"
|
|
say "a request the SD card ($STAGE_DISK) did not answer is stuck in the kernel: reboot Android before running the installer again (until then every run is refused: $STAGE_LOCK)"
|
|
say "the card stays mounted at $M, with no sync and no unmount (both would wait on that request)"
|
|
rm -rf "$_sd"
|
|
return 1
|
|
fi
|
|
_rc=$(cat "$_sd/rc"); wait "$_wp" 2>/dev/null || true
|
|
rm -rf "$_sd"
|
|
return "$_rc"
|
|
}
|
|
# --- stage end
|
|
# --- sd begin
|
|
sd_ext4() { # true when the device holds an ext4 filesystem, labelled or not
|
|
[ "$(dd if="$1" bs=1 skip=1080 count=2 2>/dev/null | od -An -tx1 | tr -d ' \n')" = 53ef ]
|
|
}
|
|
sd_label() { # the ext4 label of a device, empty when it is not ext4 or has none
|
|
sd_ext4 "$1" || return 0
|
|
dd if="$1" bs=1 skip=1144 count=16 2>/dev/null | tr -d '\000'
|
|
}
|
|
sd_disk() { # sd_disk DEV: the disk of a partition (mmcblk1p1 -> mmcblk1); a whole disk stays as it is
|
|
case $1 in *mmcblk[0-9]*p[0-9]*) echo "${1%p*}" ;; *) echo "$1" ;; esac
|
|
}
|
|
sd_check() { # sd_check DEV: DEV must be on an SD card, never on the eMMC this Android runs from
|
|
case $1 in
|
|
*/mmcblk0|*/mmcblk0p*) say "refusing $1: that is the internal eMMC, not the SD card"; return 1 ;;
|
|
esac
|
|
d=$(sd_disk "$1")
|
|
# the MMC core names the card type: SD for a memory card (the eMMC says MMC, a Wi-Fi chip SDIO)
|
|
t=$(cat "${MU300_SYSFS:-/sys}/block/${d##*/}/device/type" 2>/dev/null) || t=
|
|
[ "$t" = SD ] || { say "refusing $1: not an SD card (type '$t')"; return 1; }
|
|
}
|
|
sd_release() { # sd_release DEV: make Android let go of the card; nothing has been written when this fails
|
|
# no answer from the volume manager is no proof that Android has let go: refuse
|
|
vols=$(sm list-volumes 2>/dev/null) || { say "cannot ask Android about its volumes (sm list-volumes failed)"; return 1; }
|
|
# adopted as internal storage, in any state: 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:[^ ]* ' && {
|
|
say "the SD card is adopted as Android internal storage; format it as portable storage first"; return 1; }
|
|
# only the card: public:179,N (179 is the MMC block major); a USB stick (public:8,N) is not ours to touch
|
|
for v in $(echo "$vols" | sed -n 's/^\(public:179,[^ ]*\) mounted.*/\1/p'); do
|
|
say "asking Android to unmount $v"
|
|
sm unmount "$v" 2>/dev/null || true
|
|
done
|
|
vols=$(sm list-volumes 2>/dev/null) || { say "cannot ask Android about its volumes (sm list-volumes failed)"; return 1; }
|
|
if echo "$vols" | grep -q '^public:179,[^ ]* 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's own nodes (any of its partitions). vold mounts the card through
|
|
# /dev/block/vold/public:179,N, so this only catches mounts made by hand: the sm check above is the real
|
|
# guard. A whole-card DEV (mmcblk1) stays as it is, so the eMMC (mmcblk0) can never match.
|
|
d=$(sd_disk "$1")
|
|
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
|
|
# any other ext4, with or without a label, is someone's Linux data; FAT, exFAT or a blank card is not
|
|
if sd_ext4 "$1" && [ "$label" != mu300sd ]; then
|
|
say "refusing to format: foreign ext4 (${label:-no label}) on the SD card"; return 1
|
|
fi
|
|
# Android's own mke2fs, not a busybox one that su's PATH may find first. Its default of one inode per
|
|
# 16 KiB is millions of inodes on a big card, and writing their tables is what made 128 GB cards slow and
|
|
# fail (kanoqwq). So fewer: the ratio doubles up to 1 MiB while at least 65536 inodes remain (the two
|
|
# systems, each with an old copy during an update, use about 25000). awk: the byte count is past mksh's
|
|
# 32-bit arithmetic.
|
|
mk=/system/bin/mke2fs; [ -x $mk ] || mk=mke2fs
|
|
s=$(cat "${MU300_SYSFS:-/sys}/class/block/${1##*/}/size" 2>/dev/null) || s=0
|
|
i=$(awk -v s="${s:-0}" 'BEGIN { i = 16384; while (i < 1048576 && s * 512 / (i * 2) >= 65536) i *= 2; print i }')
|
|
say "creating ext4 mu300sd on $1 (one inode per $((i / 1024)) KiB)"
|
|
# bounded (the stage block); mke2fs's own lines stay quiet inside the stage, the stage's are shown
|
|
stage_run format 900 sh -c 'exec "$@" >/dev/null' sh $mk -t ext4 -F -b 4096 -m 0 -i "$i" -L mu300sd "$1"
|
|
elif [ "$label" != mu300sd ]; then
|
|
say "no mu300sd filesystem on $1 (run with FORMAT=1)"; return 1
|
|
fi
|
|
}
|
|
sd_fstab() { # sd_fstab ROOT: Ubuntu remounts / by label; on the card that label is mu300sd
|
|
[ -f "$1/etc/fstab" ] || return 0
|
|
# not sed -i: the same file content, written back in place (keeps its owner and mode)
|
|
sed 's|^LABEL=mu300root |LABEL=mu300sd |' "$1/etc/fstab" > "$1/etc/fstab.mu300" &&
|
|
cat "$1/etc/fstab.mu300" > "$1/etc/fstab" && rm -f "$1/etc/fstab.mu300"
|
|
}
|
|
# 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 - so it always returns 0, also under set -e.
|
|
sd_mark_internal() {
|
|
[ "${SD_MODE:-0}" = 1 ] && [ -n "${OFF:-}" ] && [ "${INTERNAL_EXISTS:-0}" = 1 ] || return 0
|
|
I=$T/mu300root-internal
|
|
MU300_OFF=$OFF MU300_SIZE=$SIZE sh $T/android-mount-mu300root.sh $I >/dev/null 2>&1 || return 0
|
|
# true, not ':': a failed redirection on a special builtin like ':' ends a POSIX shell (dash) outright
|
|
if mkdir -p $I/.mu300 2>/dev/null && true > $I/.mu300/root-on-sd 2>/dev/null; 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
|
|
}
|
|
# An installation into the internal filesystem: a root-on-sd left there by an earlier card installation no longer
|
|
# says what should boot (init would wait for a card first), so it goes.
|
|
sd_unmark() { # sd_unmark ROOT
|
|
[ "${SD_MODE:-0}" = 1 ] || rm -f "$1/.mu300/root-on-sd"
|
|
}
|
|
# The EXIT trap of SD mode: flush and unmount - unless a stage timed out (STALLED): a sync and an unmount would
|
|
# both wait on the request that is stuck, and the installer would hang where it was meant to stop.
|
|
sd_exit() {
|
|
[ -n "$STALLED" ] || { sync; sh $T/android-mount-mu300root.sh -u $M >/dev/null 2>&1; }
|
|
true
|
|
}
|
|
# --- sd end
|
|
|
|
stall_check || exit 1
|
|
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_check "$SD_DEV" || exit 1
|
|
STAGE_DISK=$(sd_disk "$SD_DEV"); STAGE_DISK=${STAGE_DISK##*/}
|
|
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 sd_exit 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
|
|
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
|
|
fi
|
|
|
|
# --- bootchain begin
|
|
# The Linux slot's uboot, sml, teecfg, trustos and vbmeta become the ones of Android's slot (android-bootchain.sh),
|
|
# before anything else is installed: a failure stops here, and the installers then write no boot image. The images
|
|
# the Linux slot held go onto the Linux filesystem first (.mu300/bootchain/<date>), and for install.sh/install.ps1
|
|
# also to $T/mu300-bootchain-backup, which they pull into their work directory.
|
|
if [ "${SYNC_BOOTCHAIN:-0}" = 1 ]; then
|
|
bcdir=$M/.mu300/bootchain/$(date +%Y%m%d-%H%M%S)
|
|
rm -rf $T/mu300-bootchain-backup
|
|
bcout=$(sh $T/android-bootchain.sh sync "$bcdir") && bcrc=0 || bcrc=$?
|
|
echo "$bcout" | grep -v '^BOOTCHAIN-' | sed 's/^/[device] /'
|
|
if [ $bcrc != 0 ]; then
|
|
say "$(echo "$bcout" | sed -n 's/^BOOTCHAIN-[A-Z]*: //p')"
|
|
say "the boot chain was not synced; no system was installed (MU300_SYNC_BOOTCHAIN=0 skips this step)"
|
|
exit 1
|
|
fi
|
|
if [ -z "${MU300_DEVICE_WORK:-}" ] && [ -d "$bcdir" ]; then
|
|
cp -R "$bcdir" $T/mu300-bootchain-backup && chmod -R a+rX $T/mu300-bootchain-backup || true
|
|
fi
|
|
fi
|
|
# --- bootchain end
|
|
|
|
# --- legacy-wipe begin
|
|
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|openwrt-*|extra) ;; *) rm -rf "$e" ;; esac
|
|
done
|
|
fi
|
|
# --- legacy-wipe end
|
|
|
|
# --- extra begin
|
|
# Extras (mu300-extra): optional parts on the Linux partition next to the systems, in DISK/extra/<name>.
|
|
extra_ok() { # extra_ok NAME DIR: DIR (unpacked) is that extra: ./name says so, or it is the VPN module (./VERSION and
|
|
# its programs; for vpn mu300-vpn and its hooks)
|
|
[ "$(cat "$2/name" 2>/dev/null)" = "$1" ] && return 0
|
|
[ -s "$2/VERSION" ] || return 1
|
|
case $1 in
|
|
vpn) [ -f "$2/bin/mu300-vpn" ] && [ -f "$2/hooks/link" ] && [ -f "$2/hooks/unlink" ] ;;
|
|
vpn-mihomo) [ -f "$2/bin/mihomo" ] ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
extra_from_push() { # extra_from_push DISK: install the extras install.sh pushed ($T/mu300-extra-<name>.tar.gz)
|
|
for f in $T/mu300-extra-*.tar.gz; do
|
|
[ -f "$f" ] || continue
|
|
n=${f##*/mu300-extra-}; n=${n%.tar.gz}
|
|
x=$1/extra
|
|
rm -rf "$x/.$n.new"; mkdir -p "$x/.$n.new"
|
|
if tar -xzf "$f" -C "$x/.$n.new" 2>/dev/null && extra_ok "$n" "$x/.$n.new"; then
|
|
rm -rf "$x/$n" "$x/.$n.sha256" && mv "$x/.$n.new" "$x/$n"
|
|
say "extra $n installed ($(cat "$x/$n/release" 2>/dev/null || cat "$x/$n/VERSION" 2>/dev/null))"
|
|
else
|
|
rm -rf "$x/.$n.new"
|
|
say "extra $n: the pushed file is not usable, skipped (on the device later: mu300-extra install $n)"
|
|
fi
|
|
rm -f "$f"
|
|
done
|
|
return 0
|
|
}
|
|
extra_keep_vpn() { # extra_keep_vpn DISK OLDROOT: the system being replaced uses the VPN. The new images have no
|
|
# mu300-vpn: it is the VPN module, linked into the system at its first boot. Without the module the VPN does not
|
|
# come back - say so loudly (the engines of an older image or extra are no use without mu300-vpn).
|
|
grep -q '^ENABLE=1' "$2/etc/mu300/vpn.conf" 2>/dev/null || return 0
|
|
[ ! -f "$1/extra/vpn/bin/mu300-vpn" ] || return 0
|
|
say "WARNING: the VPN was on in the previous system, and the VPN module is not installed: the VPN stays off"
|
|
say " until it is: on the device, sudo mu300-extra install vpn (github.com/dikeckaan/mu300-linux-vpn)"
|
|
}
|
|
# vpn_killswitch_wanted ROOT: the VPN on with its kill switch in the system at ROOT - a copy of the images'
|
|
# lib/vpn-orphan.sh (tests/test_vpn_hooks.py holds the copies to one matrix): ENABLE from vpn.conf, KILL_SWITCH from
|
|
# the store's settings when the store is in use, else vpn.conf; only 0 is off. Read with sed, never sourced.
|
|
vpn_conf_value() {
|
|
[ -r "$1" ] || return 0
|
|
sed -n "s/^$2=//p" "$1" 2>/dev/null | tail -n1 | sed "s/[[:space:]]#.*//; s/[\"' ]//g"
|
|
}
|
|
vpn_killswitch_wanted() {
|
|
[ "$(vpn_conf_value "${1:-}/etc/mu300/vpn.conf" ENABLE)" = 1 ] || return 1
|
|
if [ -d "${1:-}/etc/mu300/vpn/profiles" ] && [ -r "${1:-}/etc/mu300/vpn/settings" ]; then
|
|
_vks=$(vpn_conf_value "${1:-}/etc/mu300/vpn/settings" KILL_SWITCH)
|
|
else
|
|
_vks=$(vpn_conf_value "${1:-}/etc/mu300/vpn.conf" KILL_SWITCH)
|
|
fi
|
|
[ "$_vks" != 0 ]
|
|
}
|
|
extra_vpn_precheck() { # extra_vpn_precheck DISK: before any system is replaced. An update over a system whose VPN is
|
|
# on with its kill switch needs the VPN module (the new images have no mu300-vpn): without it the kill switch
|
|
# would be left to nothing, so nothing is changed, as mu300-update apply does. Kill switch off: a warning later.
|
|
[ "${UPDATE:-0}" = 1 ] || return 0
|
|
[ ! -f "$1/extra/vpn/bin/mu300-vpn" ] || return 0
|
|
for o in $OSES; do
|
|
[ -d "$1/$o" ] && vpn_killswitch_wanted "$1/$o" || continue
|
|
say "the VPN is on with its kill switch in the $o being replaced, and the VPN module is not installed: nothing was changed"
|
|
say " answer yes to the installer's VPN question (or set MU300_VPN_MODULE=FILE), install it on the device first"
|
|
say " (sudo mu300-extra install vpn), or turn the VPN off there first (ENABLE=0 in /etc/mu300/vpn.conf)"
|
|
exit 1
|
|
done
|
|
return 0
|
|
}
|
|
# --- extra end
|
|
extra_from_push $M
|
|
extra_vpn_precheck $M
|
|
|
|
ssid=; psk=
|
|
if [ "$IMPORT_HOTSPOT" = 1 ]; then
|
|
X=/data/misc/apexdata/com.android.wifi/WifiConfigStoreSoftAp.xml
|
|
ssid=$(sed -n 's/.*<string name="WifiSsid">"\(.*\)"<\/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/&/\&/g; s/</</g; s/>/>/g; s/"/\"/g; s/'/'/g")
|
|
[ -n "$ssid" ] && [ ${#psk} -ge 8 ] || { say "no usable Android hotspot config, a random password will be generated"; ssid=; psk=; }
|
|
fi
|
|
|
|
# --- user-packages begin
|
|
# Packages installed on the device are not in the release's image, and an update puts the image in place while it
|
|
# keeps /etc/config (OpenWrt) and /etc/mu300: their programs were gone and their settings stayed - OpenClash's left
|
|
# the device without DNS (#116). So before the switch the update writes down what was added - the packages asked for
|
|
# by hand (OpenWrt: /etc/apk/world; Ubuntu: dpkg's packages that apt did not pull in by itself) less the image's
|
|
# own set (etc/mu300/image-packages, written when the image is built) - into etc/mu300/user-packages/ of the new
|
|
# system, and sets the UCI files of those packages aside (etc/mu300/orphaned-config/): a package that is not there
|
|
# leaves no settings behind. At its first boot the new system installs them again (mu300-user-packages, a service)
|
|
# and puts their settings back. tools/android-install.sh (the installers' update) has a copy of this block; tests
|
|
# hold the two to one text. It runs on Android too: sh, awk, sed, grep, sort - nothing else, and safe under set -e.
|
|
UPK_DIR=etc/mu300/user-packages
|
|
UPK_ORPH=etc/mu300/orphaned-config
|
|
# what is never installed again: kernel modules (the feed's are for another kernel), and what the images take out on
|
|
# purpose (sysupgrade would flash the eMMC; ujail crash-loops services on 5.4)
|
|
upk_skip() {
|
|
case $1 in
|
|
kmod-*|kernel|procd-ujail|procd-seccomp|luci-app-attendedsysupgrade|attendedsysupgrade-common|owut) return 0 ;;
|
|
linux-image-*|linux-modules-*|linux-headers-*|linux-generic*|linux-firmware) return 0 ;;
|
|
esac
|
|
return 1
|
|
}
|
|
upk_kind() { case $1 in ubuntu) echo ubuntu ;; *) echo openwrt ;; esac; }
|
|
# upk_dpkg ROOT [manual]: dpkg's installed packages in ROOT; with manual, less those apt installed as dependencies
|
|
# (what apt-mark showmanual says, read from the files: ROOT need not be the running system)
|
|
upk_dpkg() {
|
|
[ -f "$1/var/lib/dpkg/status" ] || return 0
|
|
{ [ "${2:-}" != manual ] || cat "$1/var/lib/apt/extended_states" 2>/dev/null || :; echo '@dpkg'; cat "$1/var/lib/dpkg/status"; } |
|
|
awk '$0 == "@dpkg" { s = 1; next }
|
|
$1 == "Package:" { p = $2; next }
|
|
!s && $1 == "Auto-Installed:" && $2 == 1 { auto[p] = 1; next }
|
|
s && $1 == "Status:" && $3 == "ok" && $4 == "installed" { inst[p] = 1 }
|
|
END { for (p in inst) if (!(p in auto)) print p }' | sort
|
|
}
|
|
# upk_manual ROOT KIND: the packages asked for by name (OpenWrt: the world file, versions and pins taken off)
|
|
upk_manual() {
|
|
if [ "$2" = ubuntu ]; then upk_dpkg "$1" manual; return 0; fi
|
|
[ -f "$1/etc/apk/world" ] || return 0
|
|
sed 's/[<>=~@ ].*//' "$1/etc/apk/world" | awk 'NF' | sort -u
|
|
}
|
|
# upk_installed ROOT KIND: every installed package
|
|
upk_installed() {
|
|
if [ "$2" = ubuntu ]; then upk_dpkg "$1"; return 0; fi
|
|
[ -f "$1/lib/apk/db/installed" ] || return 0
|
|
sed -n 's/^P://p' "$1/lib/apk/db/installed" | sort -u
|
|
}
|
|
# upk_txt_names FILE: the names in etc/mu300/packages.txt ("apk list --installed": name-version-rN arch ...)
|
|
upk_txt_names() {
|
|
[ -f "$1" ] || return 0
|
|
awk '{print $1}' "$1" | sed 's/-r[0-9][0-9]*$//; s/-[^-]*$//'
|
|
}
|
|
# upk_pending DIR: the packages of a record not installed again yet (nor given up on)
|
|
upk_pending() {
|
|
[ -f "$1/wanted" ] || return 0
|
|
{ for _uf in done failed; do [ ! -f "$1/$_uf" ] || sed 's/^/x /' "$1/$_uf"; done; sed 's/^/w /' "$1/wanted"; } |
|
|
awk '$1 == "x" { d[$2] = 1; next } NF > 1 && !($2 in d) { print $2 }'
|
|
}
|
|
# What belongs to the new image, not to the device, in the directories an update keeps: its version and package
|
|
# lists in etc/mu300 (an update carried the old ones over: every update then reported the version it replaced), and
|
|
# the names of its own files in etc/config (never set aside). image_own_save NEW before the settings are copied,
|
|
# image_own_restore NEW after; upk_record removes what they keep in NEW/.mu300-image.
|
|
image_own_save() {
|
|
rm -rf "$1/.mu300-image"; mkdir -p "$1/.mu300-image" || return 1
|
|
for _uf in image-version image-packages packages.txt; do
|
|
[ ! -f "$1/etc/mu300/$_uf" ] || cp "$1/etc/mu300/$_uf" "$1/.mu300-image/$_uf" || return 1
|
|
done
|
|
for _uf in "$1"/etc/config/*; do [ ! -e "$_uf" ] || echo "${_uf##*/}"; done > "$1/.mu300-image/config"
|
|
}
|
|
image_own_restore() {
|
|
[ -d "$1/.mu300-image" ] || return 0
|
|
mkdir -p "$1/etc/mu300" || return 1
|
|
for _uf in image-version image-packages packages.txt; do
|
|
if [ -f "$1/.mu300-image/$_uf" ]; then cp "$1/.mu300-image/$_uf" "$1/etc/mu300/$_uf" || return 1
|
|
# a list of the old image would describe the wrong one (the version stays: "unknown" says less)
|
|
elif [ "$_uf" != image-version ]; then rm -f "$1/etc/mu300/$_uf"; fi
|
|
done
|
|
}
|
|
# upk_os_codename ROOT: Ubuntu's release name (noble, ...)
|
|
upk_os_codename() {
|
|
sed -n 's/^VERSION_CODENAME=//p' "$1/usr/lib/os-release" 2>/dev/null | tr -d '"' | head -n 1
|
|
}
|
|
# upk_apt_keep OLD NEW [checked]: Ubuntu keeps only parts of /etc, and apt's sources were not among them: a PPA's
|
|
# packages could not come back. The source files added in etc/apt/sources.list.d are copied where the new image
|
|
# has no file of that name (the image's own are never replaced), with the keys they need: one in etc/apt/keyrings
|
|
# or usr/share/keyrings that a kept source names, and etc/apt/trusted.gpg.d's - each only where the new image has
|
|
# no file of that name. checked: the first boot tries apt-get update and disables a kept source that makes it fail
|
|
# (mu300-user-packages); without it (nothing to install again) a source of another Ubuntu release (24.04 -> 26.04)
|
|
# is copied disabled (NAME.disabled, which apt skips) and said so. NEW/.mu300-image/apt-kept: the sources kept on.
|
|
upk_apt_keep() {
|
|
_ua=$2/.mu300-image/apt-kept _ul=
|
|
: > "$_ua" || return 1
|
|
_uoc=$(upk_os_codename "$1") _unc=$(upk_os_codename "$2")
|
|
for _uf in "$1"/etc/apt/sources.list.d/*.list "$1"/etc/apt/sources.list.d/*.sources; do
|
|
[ -f "$_uf" ] || continue
|
|
_ur=etc/apt/sources.list.d/${_uf##*/}
|
|
{ [ ! -e "$2/$_ur" ] && [ ! -L "$2/$_ur" ]; } || continue
|
|
mkdir -p "$2/etc/apt/sources.list.d" || return 1
|
|
if [ -z "${3:-}" ] && [ "$_uoc" != "$_unc" ]; then
|
|
cp -p "$_uf" "$2/$_ur.disabled" || return 1
|
|
echo " apt: /$_ur is for Ubuntu $_uoc, this is $_unc: kept disabled (/$_ur.disabled)"
|
|
else
|
|
cp -p "$_uf" "$2/$_ur" || return 1
|
|
echo "$_ur" >> "$_ua"
|
|
echo " apt: /$_ur kept"
|
|
fi
|
|
_ul="$_ul $_uf"
|
|
done
|
|
[ -n "$_ul" ] || return 0
|
|
# their pinning (etc/apt/preferences.d), where the new image has no file of that name; a pin whose source is
|
|
# turned off later does nothing on its own (mu300-user-packages status names it). NEW/.mu300-image/apt-prefs.
|
|
: > "$2/.mu300-image/apt-prefs" || return 1
|
|
for _uf in "$1"/etc/apt/preferences.d/*; do
|
|
[ -f "$_uf" ] || continue
|
|
_ur=etc/apt/preferences.d/${_uf##*/}
|
|
{ [ ! -e "$2/$_ur" ] && [ ! -L "$2/$_ur" ]; } || continue
|
|
mkdir -p "$2/etc/apt/preferences.d" && cp -p "$_uf" "$2/$_ur" || return 1
|
|
echo "$_ur" >> "$2/.mu300-image/apt-prefs"
|
|
echo " apt: /$_ur kept"
|
|
done
|
|
# shellcheck disable=SC2086
|
|
_us=$(cat $_ul 2>/dev/null)
|
|
for _uf in "$1"/etc/apt/keyrings/* "$1"/usr/share/keyrings/* "$1"/etc/apt/trusted.gpg.d/*; do
|
|
[ -f "$_uf" ] || continue
|
|
_ur=${_uf#"$1"/}
|
|
{ [ ! -e "$2/$_ur" ] && [ ! -L "$2/$_ur" ]; } || continue
|
|
case $_ur in
|
|
etc/apt/trusted.gpg.d/*) ;;
|
|
*) printf '%s\n' "$_us" | grep -qF "/$_ur" || continue ;;
|
|
esac
|
|
mkdir -p "$2/${_ur%/*}" && cp -p "$_uf" "$2/$_ur" || return 1
|
|
done
|
|
}
|
|
# upk_units OLD: "PACKAGE UNIT enabled|disabled|masked" for the systemd units the packages named on stdin have in
|
|
# OLD (dpkg's file lists), as OLD had them - mu300-user-packages puts that back after it installs them again
|
|
upk_units() {
|
|
while read -r _up; do
|
|
for _ul in "$1/var/lib/dpkg/info/$_up.list" "$1/var/lib/dpkg/info/$_up":*.list; do
|
|
[ -f "$_ul" ] || continue
|
|
grep -E '^/(usr/)?lib/systemd/system/[A-Za-z0-9_.:-]+\.(service|socket|timer|path)$' "$_ul" |
|
|
while read -r _uu; do
|
|
# an alias (a link to the unit, smartd.service) follows its unit
|
|
[ ! -L "$1$_uu" ] || continue
|
|
_uu=${_uu##*/}
|
|
case $_uu in *@*) continue ;; esac
|
|
_us=disabled
|
|
for _uw in "$1"/etc/systemd/system/*.wants/"$_uu" "$1"/etc/systemd/system/*.requires/"$_uu"; do
|
|
[ ! -L "$_uw" ] || _us=enabled
|
|
done
|
|
[ "$(readlink "$1/etc/systemd/system/$_uu" 2>/dev/null)" != /dev/null ] || _us=masked
|
|
echo "$_up $_uu $_us"
|
|
done
|
|
done
|
|
done | sort -u
|
|
}
|
|
# upk_record OLD NEW OS: after the settings are in NEW, before the switch. Writes NEW/etc/mu300/user-packages/
|
|
# (wanted: the names; files: "package path" of their files in etc/; rc.d: the services enabled in OLD; from; state)
|
|
# and moves their etc/config files out of the way. MU300_KEEP_PACKAGES=0: they are not installed again (state
|
|
# skipped; mu300-user-packages retry does it later), their settings are set aside all the same.
|
|
upk_record() {
|
|
_uo=$1 _un=$2 _uk=$(upk_kind "$3")
|
|
_ut=$2/.mu300-image _ud=$2/$UPK_DIR
|
|
mkdir -p "$_ut" || return 1
|
|
# the old image's own set; a system from before that list: the new image's and the one packages.txt names
|
|
if [ -s "$_uo/etc/mu300/image-packages" ]; then cat "$_uo/etc/mu300/image-packages"
|
|
else
|
|
cat "$_ut/image-packages" 2>/dev/null || :
|
|
upk_manual "$_un" "$_uk"
|
|
upk_txt_names "$_uo/etc/mu300/packages.txt"
|
|
fi > "$_ut/not"
|
|
upk_installed "$_un" "$_uk" >> "$_ut/not"
|
|
# what an earlier update recorded and the device never got back (offline since, or skipped) stays wanted
|
|
{ upk_manual "$_uo" "$_uk"; upk_pending "$_uo/$UPK_DIR"; } > "$_ut/had"
|
|
{ sed 's/^/x /' "$_ut/not"; sed 's/^/w /' "$_ut/had"; } |
|
|
awk '$1 == "x" { n[$2] = 1; next } NF > 1 && !($2 in n) { print $2 }' | sort -u > "$_ut/added"
|
|
rm -rf "$_ud"
|
|
: > "$_ut/wanted"
|
|
while read -r _up; do upk_skip "$_up" || echo "$_up" >> "$_ut/wanted"; done < "$_ut/added"
|
|
# Ubuntu: the apt sources added on the device (a PPA's packages cannot come back without it), see upk_apt_keep
|
|
if [ "$_uk" = ubuntu ]; then
|
|
upk_apt_keep "$_uo" "$_un" "$([ -s "$_ut/wanted" ] && echo checked)" || return 1
|
|
fi
|
|
if [ ! -s "$_ut/wanted" ]; then
|
|
echo " none: everything installed is part of the new image"
|
|
rm -rf "$_ut"
|
|
return 0
|
|
fi
|
|
mkdir -p "$_ud" || return 1
|
|
mv "$_ut/wanted" "$_ud/wanted"
|
|
[ ! -s "$_ut/apt-kept" ] || mv "$_ut/apt-kept" "$_ud/apt-sources"
|
|
[ ! -s "$_ut/apt-prefs" ] || mv "$_ut/apt-prefs" "$_ud/apt-prefs"
|
|
: > "$_ud/done"; : > "$_ud/failed"
|
|
cat "$_uo/etc/mu300/image-version" > "$_ud/from" 2>/dev/null || echo unknown > "$_ud/from"
|
|
# their files in etc/, from the package database of OLD (and of the record a pending one came from)
|
|
if [ "$_uk" = openwrt ]; then
|
|
{ cat "$_uo/$UPK_DIR/files" 2>/dev/null || :
|
|
[ ! -f "$_uo/lib/apk/db/installed" ] || awk '
|
|
$0 ~ /^P:/ { p = substr($0, 3); next }
|
|
$0 ~ /^F:/ { d = substr($0, 3); next }
|
|
$0 ~ /^R:/ && (d == "etc" || d ~ /^etc\//) { print p " " d "/" substr($0, 3) }' "$_uo/lib/apk/db/installed"
|
|
} | { sed 's/^/w /' "$_ud/wanted"; sed 's/^/f /'; } |
|
|
awk '$1 == "w" { w[$2] = 1; next } ($2 in w) && NF > 2 { print substr($0, 3) }' | sort -u > "$_ud/files"
|
|
for _uf in "$_uo"/etc/rc.d/*; do { [ -L "$_uf" ] || [ -e "$_uf" ]; } && echo "${_uf##*/}"; done > "$_ud/rc.d" || :
|
|
else
|
|
: > "$_ud/files"; : > "$_ud/rc.d"
|
|
upk_units "$_uo" < "$_ud/wanted" > "$_ud/units"
|
|
fi
|
|
if [ "${MU300_KEEP_PACKAGES:-1}" = 0 ]; then echo skipped > "$_ud/state"; else echo pending > "$_ud/state"; fi
|
|
echo " $(tr '\n' ' ' < "$_ud/wanted")"
|
|
if [ "$(cat "$_ud/state")" = skipped ]; then
|
|
echo " The new system does not have them, and they are not installed again (MU300_KEEP_PACKAGES=0 or"
|
|
echo " --no-reinstall; later: mu300-user-packages retry)."
|
|
else
|
|
echo " The new system does not have them: its first boot installs them again once the device is online"
|
|
echo " (the result: mu300-user-packages status)."
|
|
fi
|
|
# their UCI files: a package that is not there must leave nothing behind that acts on the network. Only a file
|
|
# its package owns, in etc/config, that the new image does not have itself (dhcp, firewall, luci: never).
|
|
_um=
|
|
while read -r _up _uf; do
|
|
case $_uf in etc/config/?*) ;; *) continue ;; esac
|
|
{ [ -f "$_un/$_uf" ] && [ ! -L "$_un/$_uf" ]; } || continue
|
|
! grep -qxF "${_uf#etc/config/}" "$_ut/config" 2>/dev/null || continue
|
|
mkdir -p "$_un/$UPK_ORPH/etc/config" && mv -f "$_un/$_uf" "$_un/$UPK_ORPH/$_uf" || return 1
|
|
[ -n "$_um" ] || echo " Their settings are set aside until they are back, so that they cannot act without them:"
|
|
echo " /$_uf -> /$UPK_ORPH/$_uf ($_up)"
|
|
_um=1
|
|
done < "$_ud/files"
|
|
rm -rf "$_ut"
|
|
}
|
|
# stale_config_clean NEW: what the images of v2026.10.18 left in etc/config and an update then copied onto every
|
|
# device (the config files of the image are added to the kept ones): attendedsysupgrade, of a package the images
|
|
# remove on purpose, and the .apk-new copies apk leaves beside a changed config. Gone from NEW's etc/config unless
|
|
# the package is installed there (attendedsysupgrade stays with attendedsysupgrade-common or its LuCI app).
|
|
stale_config_clean() {
|
|
for _uf in "$1"/etc/config/*.apk-new; do [ ! -e "$_uf" ] || rm -f "$_uf"; done
|
|
[ -e "$1/etc/config/attendedsysupgrade" ] || return 0
|
|
upk_installed "$1" openwrt | grep -qx 'attendedsysupgrade-common\|luci-app-attendedsysupgrade' && return 0
|
|
rm -f "$1/etc/config/attendedsysupgrade"
|
|
}
|
|
# --- user-packages end
|
|
# --- install-os begin
|
|
# Accounts are settings too, and etc/shadow is not in keep: an update carries them over the way mu300-update does,
|
|
# with its own function (copied here: this runs on Android and cannot source the new system's mu300-update; tests
|
|
# keep the two the same). The old entry of every account both systems have (hashes, group memberships merged), and
|
|
# the users and groups the old system added (ids 1000-59999). A new password (PWHASH) is put in afterwards.
|
|
ACCOUNTS_MARK=/etc/.mu300-accounts-from-image
|
|
merge_accounts() { # merge_accounts OLD_ROOT NEW_ROOT ("" is the running system)
|
|
o=$1 n=$2
|
|
[ -f "$o/etc/shadow" ] && [ -f "$n/etc/shadow" ] || return 0
|
|
for f in passwd group shadow gshadow; do
|
|
[ -f "$o/etc/$f" ] && [ -f "$n/etc/$f" ] || continue
|
|
awk -F: -v OFS=: -v f="$f" -v op="$o/etc/passwd" -v og="$o/etc/group" '
|
|
BEGIN {
|
|
while ((getline l < op) > 0) { split(l, a, ":"); if (a[3] >= 1000 && a[3] < 60000) lu[a[1]] = 1 }
|
|
while ((getline l < og) > 0) { split(l, a, ":"); if (a[3] >= 1000 && a[3] < 60000) lg[a[1]] = 1 }
|
|
}
|
|
NR == FNR { old[$1] = $0; next }
|
|
{
|
|
seen[$1] = 1
|
|
if ((f == "shadow" || f == "gshadow") && ($1 in old)) { print old[$1]; next }
|
|
if (f == "group" && ($1 in old)) {
|
|
split(old[$1], og4, ":"); m = $4; k = split(og4[4], om, ",")
|
|
for (i = 1; i <= k; i++)
|
|
if (om[i] != "" && index("," m ",", "," om[i] ",") == 0) m = (m == "" ? om[i] : m "," om[i])
|
|
$4 = m
|
|
}
|
|
print
|
|
}
|
|
END {
|
|
for (x in old) if (!(x in seen)) {
|
|
if ((f == "passwd" || f == "shadow") && (x in lu)) print old[x]
|
|
if ((f == "group" || f == "gshadow") && (x in lg)) print old[x]
|
|
}
|
|
}' "$o/etc/$f" "$n/etc/$f" > "$n/etc/$f.mu300" || { rm -f "$n/etc/$f.mu300"; return 1; }
|
|
# renamed into place, never rewritten: a crash (or a reader at boot) never sees a half-written passwd.
|
|
# The copy made with cp -p keeps the file's owner and mode (shadow is root:shadow 0640; no stat on OpenWrt).
|
|
if [ -s "$n/etc/$f.mu300" ]; then
|
|
cp -p "$n/etc/$f" "$n/etc/$f.mu300-new" && cat "$n/etc/$f.mu300" > "$n/etc/$f.mu300-new" && sync &&
|
|
mv -f "$n/etc/$f.mu300-new" "$n/etc/$f" || { rm -f "$n/etc/$f.mu300" "$n/etc/$f.mu300-new"; return 1; }
|
|
fi
|
|
rm -f "$n/etc/$f.mu300"
|
|
done
|
|
rm -f "$n$ACCOUNTS_MARK"
|
|
}
|
|
for os in $OSES; do
|
|
tarball=$T/mu300-$os.tar.gz
|
|
[ -f $tarball ] || { say "missing $tarball"; exit 1; }
|
|
say "installing $os"
|
|
# the writes to the card run bounded (the stage block); on the eMMC as they are
|
|
stage_run "clean $os" 900 rm -rf $M/$os.new
|
|
mkdir $M/$os.new
|
|
stage_run "unpack $os" 3600 tar -xzpf $tarball -C $M/$os.new
|
|
# the account the installer gives a password (ubuntu, OpenWrt's root)
|
|
case $os in ubuntu) pwu=ubuntu ;; *) pwu=root ;; esac
|
|
# 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
|
|
stage_run "unpack $os vendor" 900 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
|
|
# the new image's version and package lists, and the names of its own etc/config files (upk_record)
|
|
image_own_save "$M/$os.new"
|
|
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|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.image"
|
|
[ "$k" = etc/config ] && [ -d "$M/$os.new/$k" ] && mv "$M/$os.new/$k" "$M/$os.new/$k.image"
|
|
rm -rf "$M/$os.new/$k"
|
|
cp -a "$M/$os/$k" "$M/$os.new/$k" && kept="$kept $k"
|
|
# OpenWrt: the old settings, plus the config files of the image's packages the old system did not have
|
|
# (as mu300-update does; the old etc/config alone dropped e.g. /etc/config/sqm)
|
|
if [ -d "$M/$os.new/$k.image" ]; then
|
|
for f in "$M/$os.new/$k.image"/* "$M/$os.new/$k.image"/.[!.]*; do
|
|
[ -e "$f" ] || [ -L "$f" ] || continue
|
|
[ -e "$M/$os.new/$k/${f##*/}" ] || [ -L "$M/$os.new/$k/${f##*/}" ] || cp -a "$f" "$M/$os.new/$k/"
|
|
done
|
|
rm -rf "$M/$os.new/$k.image"
|
|
# the stray files of v2026.10.18 (the old system's, or that image's own) do not stay
|
|
stale_config_clean "$M/$os.new"
|
|
fi
|
|
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|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"
|
|
image_own_restore "$M/$os.new"
|
|
# the packages installed on the device: written down, their settings set aside, put back at the first boot
|
|
# (mu300-user-packages), as mu300-update apply does
|
|
say "packages installed on the device in the previous $os:"
|
|
upk=$(upk_record "$M/$os" "$M/$os.new" "$os") ||
|
|
{ rm -rf $M/$os.new; say "$upk"; say "$os: could not write down the packages installed on the device; $os was not replaced"; exit 1; }
|
|
printf '%s\n' "$upk" | while IFS= read -r l; do say "$l"; done
|
|
rm -rf "$M/$os.new/.mu300-image"
|
|
if [ -f "$M/$os/etc/shadow" ]; then
|
|
merge_accounts "$M/$os" "$M/$os.new" || { say "could not carry the accounts of $os over; nothing was replaced"; exit 1; }
|
|
# the log line says what the shadow will hold: with a hash, or PW_SET=1 (the Magisk installer's
|
|
# MU300_PASSWORD_RESET=yes, or a system that still had the images' accounts), the system's own account
|
|
# gets PWHASH below, and only the other accounts keep their passwords
|
|
if [ -n "$PWHASH" ]; then
|
|
say "kept the other accounts of the previous $os; $pwu's password is the one the installer chose (the images' own, or MU300_PASSWORD)"
|
|
elif [ "${PW_SET:-0}" = 1 ]; then
|
|
say "kept the other accounts of the previous $os; $pwu's password is the images' own again: none, until one is set"
|
|
else
|
|
say "kept the accounts and passwords of the previous $os"
|
|
fi
|
|
fi
|
|
extra_keep_vpn $M $M/$os
|
|
[ -n "$extra" ] && say "kept enabled services:$extra"
|
|
fi
|
|
# The password, before the new system replaces the old one: the hash just chosen (PWHASH; with PW_SET=1 also an
|
|
# empty one, OpenWrt's root without a password), else the accounts carried over, else the image's own (Ubuntu's
|
|
# ubuntu/ubuntu, OpenWrt's root without a password: the well-known ones, issue #167). The accounts are this
|
|
# system's now, whichever they are: never the mark that has mu300-update carry some <os>.old's over at boot.
|
|
if [ -n "$PWHASH" ] || [ "${PW_SET:-0}" = 1 ]; then
|
|
sed -i "s|^$pwu:[^:]*:|$pwu:$PWHASH:|" $M/$os.new/etc/shadow
|
|
fi
|
|
rm -f $M/$os.new$ACCOUNTS_MARK
|
|
stage_run "replace $os" 900 rm -rf $M/$os
|
|
mv $M/$os.new $M/$os
|
|
R=$M/$os
|
|
if [ "${SD_MODE:-0}" = 1 ]; then sd_fstab $R; fi
|
|
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
|
|
# adb over USB to Linux, for the installing computer's key (whoever holds it gets a root shell); ADB=0 changes
|
|
# nothing (an update keeps etc/mu300 and with it the setting and the keys)
|
|
if [ "${ADB:-0}" = 1 ] && [ -n "${ADB_KEY:-}" ]; then
|
|
echo on > $R/etc/mu300/adb
|
|
umask 077
|
|
grep -qF "${ADB_KEY%% *}" $R/etc/mu300/adb_keys 2>/dev/null || echo "$ADB_KEY" >> $R/etc/mu300/adb_keys
|
|
umask 022
|
|
chmod 600 $R/etc/mu300/adb_keys
|
|
say "$os: adb over USB on, for ${ADB_KEY#* }"
|
|
fi
|
|
rm -f $tarball
|
|
done
|
|
mkdir -p $M/.mu300
|
|
sd_unmark $M
|
|
echo "$BOOT_OS" > $M/.mu300/boot-os
|
|
# --- install-os end
|
|
case ${BOOT_ATTEMPTS:-} in [1-6]) echo "$BOOT_ATTEMPTS" > $M/.mu300/boot-attempts ;; esac
|
|
case ${KERNEL:-5.4} in
|
|
5.4|6.18|7.2) mkdir -p $M/boot; echo "${KERNEL:-5.4}" > $M/boot/kernel; echo "${KERNEL:-5.4}" > $M/boot/installed.kernel ;;
|
|
esac
|
|
# the boot image is the installer's now: a release tag left by an earlier mu300-update would describe another one
|
|
rm -f $M/boot/installed.tag
|
|
[ -n "$ssid" ] && say "hotspot: SSID $ssid imported (passphrase ${#psk} chars)"
|
|
say "installed: $(ls -d $M/ubuntu $M/openwrt $M/openwrt-luci 2>/dev/null | sed "s|$M/||g" | tr '\n' ' ')boot-os=$BOOT_OS default-linux=$DEFAULT_LINUX"
|
|
sd_mark_internal
|
|
# the card's dirty pages go out now, bounded like the other card stages (the unmount in sd_exit then has little
|
|
# left to wait for); a timeout here is a failure like any other: the OK line never comes, nothing is armed
|
|
[ "${SD_MODE:-0}" != 1 ] || stage_run flush 1800 sync
|
|
stall_clear
|
|
rm -f $T/mu300-install.env
|
|
echo MU300-INSTALL-OK # install.sh checks for this line (set -e stops before it on any failure)
|