diff --git a/launch_chffrplus.sh b/launch_chffrplus.sh index 9c7df4090..087f48ee9 100755 --- a/launch_chffrplus.sh +++ b/launch_chffrplus.sh @@ -8,7 +8,114 @@ source "$BASEDIR/launch_env.sh" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + # --- BEGIN: auto-fix persist from squashfs -> ext4 (one-time, preserves identity) --- +function persist_convert_if_needed { + LOG="/tmp/persist_fix.log" + { + echo "[persist-fix] ----- start $(date) -----" + + # Guard: if we've already converted on this device, skip + if [ -f /data/.persist_converted ] || [ -f /persist/.converted_to_ext4 ]; then + echo "[persist-fix] Marker exists; skipping." + return 0 + fi + + # Discover persist device + DEV="$(blkid -t LABEL=persist -o device 2>/dev/null || true)" + if [ -z "$DEV" ] && [ -e /dev/disk/by-partlabel/persist ]; then + DEV="$(realpath /dev/disk/by-partlabel/persist)" + fi + if [ -z "$DEV" ]; then + # Fallback commonly used path + if lsblk -no PATH,LABEL | grep -E "persist$" >/dev/null 2>&1; then + DEV="$(lsblk -no PATH,LABEL | awk '$2=="persist"{print $1; exit}')" + else + DEV="/dev/sda2" + fi + fi + echo "[persist-fix] Using device: ${DEV}" + + # Detect device filesystem type + FSTYPE="$(blkid -o value -s TYPE "$DEV" 2>/dev/null || true)" + echo "[persist-fix] Detected $DEV fstype='$FSTYPE'" + UNSQS="$DIR/third_party/bin/unsquashfs" + + # Detect current /persist mount status & fstype + CUR_MNT_TYPE="$(mount | awk '$3==\"/persist\"{print $5}' | head -n1)" + if mountpoint -q /persist; then + echo "[persist-fix] /persist currently mounted as type='${CUR_MNT_TYPE}'" + else + echo "[persist-fix] /persist is not a mountpoint (empty dir)." + fi + + # Only proceed on NEW devices: either /persist is not mounted OR is squashfs, + # AND the underlying persist partition is squashfs (factory RO image). + if { ! mountpoint -q /persist || [ "${CUR_MNT_TYPE}" = "squashfs" ]; } && [ "${FSTYPE}" = "squashfs" ]; then + echo "[persist-fix] NEW device detected (squashfs persist). Converting to ext4." + + # Stream identity files using unsquashfs -cat directly + for f in id_rsa id_rsa.pub color_cal dongle_id; do + if sudo "$UNSQS" -cat "$DEV" "comma/$f" > "/data/$f" 2>/dev/null; then + echo "[persist-fix] Preserved $f" + else + echo "[persist-fix] $f not found in squashfs (ok, may not exist)." + fi + done + + # (Optional) raw backup of the original partition image (once) + if [ ! -f /data/persist_backup.img ]; then + echo "[persist-fix] Creating raw backup of ${DEV} to /data/persist_backup.img" + sudo dd if="$DEV" of=/data/persist_backup.img bs=1M status=none || true + sync + fi + + # Reformat persist as ext4 and label it + echo "[persist-fix] Formatting ${DEV} as ext4..." + if ! sudo mkfs.ext4 -F "$DEV"; then + echo "[persist-fix][ERROR] mkfs.ext4 failed on ${DEV}. Aborting." + return 0 + fi + sudo e2label "$DEV" persist || true + + # Mount the new ext4 persist RW + if ! sudo mount -t ext4 -o rw,discard "$DEV" /persist; then + echo "[persist-fix][ERROR] Failed to mount new ext4 persist. Aborting." + return 0 + fi + + # Recreate expected structure and restore identity + sudo mkdir -p /persist/{comma,params,tracking} + sudo chmod 755 /persist /persist/{comma,params,tracking} + for f in id_rsa id_rsa.pub color_cal dongle_id; do + if [ -f "/data/$f" ]; then + sudo cp -p "/data/$f" "/persist/comma/$f" + fi + done + if [ -f /persist/comma/id_rsa ]; then sudo chmod 600 /persist/comma/id_rsa; fi + sudo chown -R comma:comma /persist/ /persist/comma /persist/params /persist/tracking || true + sudo touch /persist/tracking/.lock + + # Seed minimal params (only if not present) + [ -f /persist/params/HasAcceptedTerms ] || echo -n 1 | sudo tee /persist/params/HasAcceptedTerms >/dev/null + [ -f /persist/params/AlwaysOnDM ] || echo -n 1 | sudo tee /persist/params/AlwaysOnDM >/dev/null + + # Mark complete so we don't run again; reboot once + echo "ext4" | sudo tee /persist/.converted_to_ext4 >/dev/null + sudo touch /data/.persist_converted + sync + echo "[persist-fix] Conversion complete. Rebooting once to finalize..." + sudo reboot + else + echo "[persist-fix] Device is not NEW/squashfs (persist already ext4 or mounted). Skipping." + fi + + echo "[persist-fix] ----- end $(date) -----" + } >>"$LOG" 2>&1 +} +# --- END: auto-fix persist from squashfs -> ext4 --- + function agnos_init { + persist_convert_if_needed # TODO: move this to agnos sudo rm -f /data/etc/NetworkManager/system-connections/*.nmmeta diff --git a/third_party/bin/unsquashfs b/third_party/bin/unsquashfs new file mode 100755 index 000000000..0216206aa Binary files /dev/null and b/third_party/bin/unsquashfs differ