From 6a12299fd966f523a3bbed18185499e18dcb503a Mon Sep 17 00:00:00 2001 From: pdev <300827144+pdev-labs@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:23:49 +0530 Subject: [PATCH] feat: install custom Linux ISO and rootfs images Add a ninth guided-setup option and `lfa install --iso/--rootfs` so a user-supplied userland can run under proot next to the built-in distros. - Accept rootfs tarballs, extracted directories, live ISOs and raw images; extract the live squashfs (including Fedora's nested ext4) with unsquashfs when available and 7z as the fallback. - Check the CPU architecture before installing, since proot cannot emulate another CPU. - Read /etc/os-release, map the image to a package-manager family and reuse the normal desktop/software setup. Unknown families install terminal-only instead of calling a package manager that is absent. - Stage the extracted tree during guided setup so the family is known before the desktop and software menus, then move it into the proot-distro layout. Clean the stage and temporary extraction directories on interrupt. - Reject tar members with absolute or parent-relative paths, and handle zipped rootfs trees with a single top-level directory. - Record CUSTOM_SOURCE, CUSTOM_NAME and CUSTOM_ID in the guest config. Tests cover a known-family tarball, an unknown family, a live ISO, wrong-architecture refusal, CLI validation and the guided flow. The host suite is now 917 checks. --- CHANGELOG.md | 9 + README.md | 61 +++- docs/ARCHITECTURE.md | 9 + install_linux.sh | 647 ++++++++++++++++++++++++++++++++++++++++++- tests/test-all.sh | 127 ++++++++- 5 files changed, 831 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85a7790..c81fa40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,15 @@ tag names, define the order. See the table at the bottom. ### Added +- Custom Linux userlands. Guided setup gained option 9, and `lfa install` + gained `--iso PATH` and `--rootfs PATH`, so a rootfs tarball, an extracted + directory, or a live ISO can run under proot next to the eight built-in + distros. LFA checks the CPU architecture, extracts a live squashfs (or the + ext4 inside Fedora's `squashfs.img`), reads `/etc/os-release`, and maps the + image to a package-manager family so the normal desktop/software setup can + run. Unknown families install terminal-only instead of calling a package + manager that does not exist. The ISO is never booted, proot cannot emulate + another CPU, and installer-only images still need a rootfs tarball. - LFA notices now reach Android. `ui_ok`, `ui_warn`, and `ui_err` post a `termux-notification` through Termux:API as well as printing the terminal line, so install, update, backup, restore, repair, import, and error diff --git a/README.md b/README.md index b733c1f..317a787 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ hardware, not simulated. Full details, root causes, and evidence are in | Alpine 3.18 (Android 5) | Yes | Yes | legacy VNC desktop, panel works; X11 and Both hidden | v1.7.1 | | Ubuntu GNOME 46 (experimental) | Yes | Yes | shell and apps run; session services missing | v1.7.0 | -> The test suite runs `881` checks with 0 failures. The same suite runs on the +> The test suite runs `917` checks with 0 failures. The same suite runs on the > devices (`tests/device-matrix.sh`). The table is as of v1.7.1. Ubuntu was > re-verified on v1.7.1, and Alpine 3.18 was re-verified on Android 5 on > 2026-11-04; the other rows were last verified on v1.7.0. A row is @@ -461,7 +461,7 @@ is accepted. | Command | What it does | | --- | --- | -| `lfa install` (`i`, `in`) | install without menus; accepts `-d/--distro`, `-D/--desktop`, `-b/--backend`, `-a/--autostart`, `-u/--user`, `-H/--hostname`, `-t/--timezone`, `-s/--software`, and `-i` | +| `lfa install` (`i`, `in`) | install without menus; accepts `-d/--distro`, `--iso`, `--rootfs`, `-D/--desktop`, `-b/--backend`, `-a/--autostart`, `-u/--user`, `-H/--hostname`, `-t/--timezone`, `-s/--software`, and `-i` | | `lfa list` (`ls`, `l`) | list installed distributions (`-j/--json` for scripts) | | `lfa status` | show desktop, backend, autostart, and running state (`-j/--json`); also checks for a newer manager release | | `lfa start` (`desktop`) | start the desktop; `-x/--x11` or `-v/--vnc` forces a backend, `-r/--restart` replaces a running session | @@ -494,6 +494,8 @@ Examples: ```bash lfa install --distro ubuntu --desktop xfce4 --backend both -i ubuntu +lfa install --rootfs ~/storage/downloads/debian-rootfs.tar.gz -i debian-custom +lfa install --iso ~/storage/downloads/alpine-live-arm64.iso -i alpine-live lfa start -i ubuntu --vnc lfa shell -i ubuntu --user lfa switch x11 -i ubuntu --remove-old @@ -710,9 +712,56 @@ different instance name in guided setup. Each instance has its own rootfs. The chosen image is recorded as `IMAGE="..."` in `/etc/termux-linux-manager.conf` inside the guest. -Arbitrary rootfs tarballs are not supported by the installer. `proot-distro` -can install a custom rootfs through a plugin, but the manager will not offer it -in the menu, and its launchers assume one of the eight supported distros. +### Custom ISO and rootfs images + +The installer can also unpack a userland you provide. This is the same proot +container model as the supported distros, not a virtual machine: the image +must match the phone's CPU, and the ISO itself is never booted. + +| Source | What happens | +| --- | --- | +| Rootfs tarball (`.tar`, `.tar.gz`, `.tar.xz`, `.tar.bz2`, `.tar.zst`) | Extracted directly; a single top-level directory is flattened. | +| Extracted rootfs directory | Copied into the instance. | +| Live ISO or raw image (`.iso`, `.img`, `.zip`) | `7z` unpacks the image, LFA finds the live squashfs (or the ext4 inside Fedora's `squashfs.img`) and extracts that root filesystem. `7zip` is installed automatically when missing. | + +Choose **Custom Linux ISO / rootfs** in guided setup (option 9), or use the +CLI: + +```bash +lfa install --rootfs ~/storage/downloads/debian-rootfs.tar.gz -i debian-custom +lfa install --iso ~/storage/downloads/alpine-live-arm64.iso -i alpine-live +lfa install --iso ~/storage/downloads/debian-live-arm64.iso -i debian-live \ + --desktop xfce4 --backend vnc +``` + +`--iso` and `--rootfs` imply `--distro custom`; do not combine them with +another `--distro`. On the CLI, a custom install defaults to `--desktop none`, +because LFA only knows how to drive a desktop for the families it recognises. + +LFA reads `/etc/os-release` after extraction and maps the image to Debian, +Ubuntu, Kali, Fedora, openSUSE, Void, Arch, or Alpine. A recognised family +gets the normal desktop/software setup and the same launchers as a distro +installed from proot-distro. If the package manager is unknown, the instance +is installed terminal-only and LFA prints the command to open a root shell; +install a desktop inside the guest with its own package manager if you want +one. + +Things that will not work: + +- **A different CPU.** proot does not emulate, so an `x86_64` ISO cannot run + on an arm64 phone. Use an arm64 image (most desktop ISOs are amd64-only). +- **Installer-only ISOs.** Images without a live squashfs/root filesystem + (for example Ubuntu Server) have nothing for LFA to extract. Use the + distro's rootfs tarball or cloud image instead. +- **Booting, kernels, and block devices.** There is no kernel, initramfs, + systemd, loop mount, or block layer under proot. Live-ISO extraction gives + you the filesystem, not the boot experience. +- **OCI/Docker archives.** These need `proot-distro` v5's own installer. + Export a plain rootfs instead: `docker export NAME -o rootfs.tar`. + +The chosen source and detected identity are recorded as `CUSTOM_SOURCE`, +`CUSTOM_NAME`, and `CUSTOM_ID` in +`/etc/termux-linux-manager.conf` inside the guest. ### Testing an older version @@ -1564,7 +1613,7 @@ plus `script` from util-linux for the pty test, which is skipped if absent: ```bash chmod +x tests/test-all.sh -./tests/test-all.sh # expect: PASS=881 FAIL=0 +./tests/test-all.sh # expect: PASS=917 FAIL=0 ``` The suite is deliberately stubbed. It cannot prove a desktop actually appears. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 16af8c3..3e3d07e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -63,6 +63,15 @@ saved config ─┘ │ └─ 7. verify core components, then "Installation complete" ``` +A custom source replaces step 2. Guided setup unpacks the chosen ISO, tarball, +or directory into a private staging tree before the desktop/software menus so +it can read `/etc/os-release` and pick the package-manager family. The stage +is moved into the proot-distro container layout once the instance name is +known; `install_linux()` reuses it instead of unpacking twice. If the family is +unknown, the desktop/software menus and the guest setup script are skipped and +the instance is terminal-only. The EXIT trap removes a stage that never +reaches an installed instance. + Step 3 is the unusual one. Instead of a long chain of `proot-distro login ... -- cmd` calls (each paying proot startup and losing environment), the installer writes one guest bash script and runs it once. It is fast and can diff --git a/install_linux.sh b/install_linux.sh index 8d0203a..07e08ea 100755 --- a/install_linux.sh +++ b/install_linux.sh @@ -34,8 +34,23 @@ CLI_INSTANCE="${CLI_INSTANCE:-}" # failed install (a stray copy used to survive forever). # ------------------------------------------------------------ LFA_TMP_ARTIFACTS=() +LFA_TMP_DIRS=() lfa_track_tmp() { LFA_TMP_ARTIFACTS+=("$1"); } +lfa_track_tmpdir() { LFA_TMP_DIRS+=("$1"); } lfa_cleanup() { + # A staged custom userland is a directory, not a file. Only remove it + # while it still lives under the manager's private staging prefix; once + # install_linux moves it into place the variable is cleared. + case "${LFA_CUSTOM_STAGE:-}" in + "$PREFIX"/var/lib/proot-distro/.lfa-custom-*) + rm -rf "$LFA_CUSTOM_STAGE" 2>/dev/null || true ;; + esac + local _d + for _d in ${LFA_TMP_DIRS[@]+"${LFA_TMP_DIRS[@]}"}; do + case "$_d" in + "$PREFIX"/tmp/lfa-custom-*) rm -rf "$_d" 2>/dev/null || true ;; + esac + done [ "${LFA_KEEP_SETUP_SCRIPT:-0}" = "1" ] && return 0 local _f for _f in ${LFA_TMP_ARTIFACTS[@]+"${LFA_TMP_ARTIFACTS[@]}"}; do @@ -910,6 +925,7 @@ SERVER="$SERVER" RESOLUTION="$RESOLUTION" COLOR_MODE="$COLOR_MODE" APP_CHOICES="$APP_CHOICES" +CUSTOM_SOURCE="$(lfa_custom_conf_escape "${LFA_CUSTOM_SOURCE:-}")" INSTANCE_NAME="$INSTANCE_NAME" SETUP_USER="$SETUP_USER" SETUP_USER_B64="$SETUP_USER_B64" @@ -932,6 +948,9 @@ setup_summary() { printf '\n %sSetup Summary%s\n' "$C_BOLD$C_WHITE" "$C_RESET" ui_rule printf ' %s%-13s%s %s\n' "$C_GRAY" "Distribution" "$C_RESET" "$DISTRO" + if [ "${LFA_CUSTOM:-0}" = "1" ] && [ -n "${LFA_CUSTOM_NAME:-}" ]; then + printf ' %s%-13s%s %s\n' "$C_GRAY" "Userland" "$C_RESET" "$LFA_CUSTOM_NAME" + fi printf ' %s%-13s%s %s\n' "$C_GRAY" "Desktop" "$C_RESET" "$DE" [ "$DE" != "none" ] && printf ' %s%-13s%s %s\n' "$C_GRAY" "Wallpaper" "$C_RESET" "$COLOR_MODE" printf ' %s%-13s%s %s\n' "$C_GRAY" "Server" "$C_RESET" "$SERVER" @@ -996,7 +1015,8 @@ setup_guided() { "5|OpenSUSE|Zypper · enterprise" \ "6|Void Linux|XBPS · lightweight, rolling" \ "7|Arch Linux|Pacman · rolling (may be unstable on ARM64)" \ - "8|Alpine Linux|APK · musl · best for Android 5/6 + low RAM"; then + "8|Alpine Linux|APK · musl · best for Android 5/6 + low RAM" \ + "9|Custom Linux ISO / rootfs|extract any proot-compatible userland"; then show_menu return fi @@ -1012,8 +1032,36 @@ setup_guided() { 6) DISTRO="void"; break ;; 7) DISTRO="archlinux"; break ;; 8) DISTRO="alpine"; break ;; + 9) DISTRO="custom"; break ;; esac done + if [ "$DISTRO" = "custom" ]; then + if [ -z "${LFA_CUSTOM_SOURCE:-}" ]; then + if ! lfa_custom_ask_source; then + show_menu + return + fi + fi + if ! lfa_custom_stage_source; then + ui_pause + show_menu + return 1 + fi + if [ "$LFA_CUSTOM_FAMILY" = "unknown" ]; then + LFA_CUSTOM_UNKNOWN=1 + fi + fi + if [ "${LFA_CUSTOM_UNKNOWN:-0}" = "1" ]; then + # No package manager LFA can drive: keep the questions that still + # apply (name, user, passwords, timezone, SSH) and skip the rest. + DE="none"; SERVER="none"; COLOR_MODE="light" + RESOLUTION="1280x720"; NEED_VIRGL=0; AUTOSTART_SERVER="none" + APP_CHOICES="" + echo "" + echo "[!] LFA does not know this userland's package manager." + echo " It installs as a terminal-only system. Add a desktop inside" + echo " the guest with its own package manager if you want one." + else # 2. Desktop local _de _dedef if [ "$IS_LEGACY" = "1" ] || [ "$(setup_ram_mb)" -le 2048 ]; then _de="None (CLI only)"; _dedef=4; else _de="XFCE4"; _dedef=1; fi @@ -1021,7 +1069,7 @@ setup_guided() { show_menu return fi - if [ "$DISTRO" = "alpine" ] && [ "$DE" = "lxde" ]; then + if { [ "$DISTRO" = "alpine" ] || [ "${LFA_CUSTOM_FAMILY:-}" = "alpine" ]; } && [ "$DE" = "lxde" ]; then echo "[*] LXDE is not packaged for Alpine -> using XFCE4 instead." DE="xfce4" fi @@ -1101,6 +1149,7 @@ setup_guided() { return fi APP_CHOICES="$UI_MULTI" + fi # 5. Instance name echo "" while true; do @@ -1161,7 +1210,8 @@ setup_guided() { 2) PSTYLE="informative" ;; esac # 12-14. Autostart / storage / SSH / widget - if ui_confirm "Autostart at boot via Termux:Boot? (needs the Termux:Boot app)" n; then + if [ "${LFA_CUSTOM_UNKNOWN:-0}" != "1" ] && \ + ui_confirm "Autostart at boot via Termux:Boot? (needs the Termux:Boot app)" n; then DO_AUTOSTART=1 if [ "$SERVER" = "both" ]; then if ! ui_select "Autostart backend" "1" \ @@ -1191,6 +1241,10 @@ setup_guided() { UI_NO_CLEAR=1 if ! ui_confirm "Proceed with installation?" y; then echo "Setup cancelled." + case "${LFA_CUSTOM_STAGE:-}" in + "$PREFIX"/var/lib/proot-distro/.lfa-custom-*) + rm -rf "$LFA_CUSTOM_STAGE"; LFA_CUSTOM_STAGE="" ;; + esac show_menu return fi @@ -1216,7 +1270,9 @@ setup_load_saved() { : "${TIMEZONE:=UTC}" "${HOSTNAME:=localhost}" "${PSTYLE:=minimal}" : "${NEED_VIRGL:=1}" "${DO_AUTOSTART:=0}" "${DO_STORAGE:=1}" : "${DO_SSH:=0}" "${DO_WIDGET:=0}" - : "${AUTOSTART_SERVER:=}" + : "${AUTOSTART_SERVER:=}" "${CUSTOM_SOURCE:=}" + LFA_CUSTOM_SOURCE="$CUSTOM_SOURCE" + if [ "${DISTRO:-}" = "custom" ]; then LFA_CUSTOM=1; fi if [ -z "$AUTOSTART_SERVER" ]; then case "$SERVER" in both) AUTOSTART_SERVER="vnc" ;; @@ -1461,7 +1517,9 @@ Global short option: Install options: -d, --distro NAME ubuntu, debian, kali, fedora, opensuse, - void, archlinux, alpine + void, archlinux, alpine, custom + --iso PATH custom live ISO (its root filesystem is extracted) + --rootfs PATH custom rootfs tarball or extracted directory -D, --desktop NAME xfce4, lxde, mate, gnome, none -b, --backend NAME x11, vnc, both, none -a, --autostart NAME x11, vnc, none @@ -1484,6 +1542,8 @@ Compatibility options: Examples: lfa install --distro ubuntu --desktop xfce4 --backend both -i ubuntu + lfa install --iso ~/storage/downloads/debian-live-arm64.iso -i debian-live + lfa install --rootfs ~/storage/downloads/alpine-rootfs.tar.gz -i alpine-custom lfa start -i ubuntu --vnc lfa shell -i ubuntu --user lfa switch x11 -i ubuntu --remove-old @@ -5133,14 +5193,14 @@ lfa_instance_session_running() { # $1 = instance } lfa_cli_status_one() { # $1 = instance, $2 = json - local _inst="$1" _json="${2:-0}" _conf _d _de _srv _auto _pid _run="none" + local _inst="$1" _json="${2:-0}" _conf _d _de _srv _auto _pid _run="none" _cname="" _conf="$(pd_conf "$_inst" 2>/dev/null)" _d=""; _de=""; _srv="none"; _auto="none" if [ -f "$_conf" ]; then # shellcheck disable=SC1090 - { IFS= read -r _d; IFS= read -r _de; IFS= read -r _srv; IFS= read -r _auto; } < <( + { IFS= read -r _d; IFS= read -r _de; IFS= read -r _srv; IFS= read -r _auto; IFS= read -r _cname; } < <( . "$_conf" 2>/dev/null - printf '%s\n%s\n%s\n%s\n' "${DISTRO:-}" "${DE:-none}" "${SERVER:-none}" "${AUTOSTART_SERVER:-none}" + printf '%s\n%s\n%s\n%s\n%s\n' "${DISTRO:-}" "${DE:-none}" "${SERVER:-none}" "${AUTOSTART_SERVER:-none}" "${CUSTOM_NAME:-}" ) fi if [ -f "$PREFIX/tmp/lfa-vnc-$_inst.pid" ]; then @@ -5158,6 +5218,7 @@ lfa_cli_status_one() { # $1 = instance, $2 = json else echo "instance: $_inst" echo "distro: ${_d:-unknown}" + [ -n "$_cname" ] && echo "userland: $_cname" echo "desktop: ${_de:-none}" echo "backend: ${_srv:-none}" echo "autostart: ${_auto:-none}" @@ -5965,9 +6026,13 @@ lfa_software_choices() { # $1 = comma-separated names -> space-separated menu n lfa_cli_install() { local _distro="" _de="" _backend="" _inst="" _auto="" _user="user" _tz="" _host="localhost" _software="" + local _source="" while [ $# -gt 0 ]; do case "$1" in --distro|-d) _distro="${2:-}"; shift 2 ;; + --iso|--rootfs|--rootfs-tar) + [ $# -ge 2 ] || { echo "[!] $1 needs a path." >&2; return 2; } + _source="$2"; shift 2 ;; --desktop|-D) _de="${2:-}"; shift 2 ;; --backend|-b) _backend="${2:-}"; shift 2 ;; -i|--instance) _inst="${2:-}"; shift 2 ;; @@ -5980,6 +6045,15 @@ lfa_cli_install() { *) echo "Unknown option: $1" >&2; return 2 ;; esac done + if [ -n "$_source" ] && [ -z "$_distro" ]; then _distro="custom"; fi + if [ "$_distro" = "custom" ] && [ -z "$_source" ]; then + echo "[!] A custom install needs --iso PATH or --rootfs PATH." >&2 + return 2 + fi + if [ "$_distro" != "custom" ] && [ -n "$_source" ]; then + echo "[!] --iso/--rootfs imply --distro custom; drop the other --distro." >&2 + return 2 + fi local _software_choices="" if [ -n "$_software" ]; then _software_choices="$(lfa_software_choices "$_software")" || return 2 @@ -6003,9 +6077,13 @@ lfa_cli_install() { if [ -z "$_distro" ]; then [ "$IS_LEGACY" = "1" ] && _distro="alpine" || _distro="debian" fi - case "$_distro" in ubuntu|debian|kali|fedora|opensuse|void|archlinux|alpine) ;; *) echo "Unknown distro: $_distro" >&2; return 2 ;; esac + case "$_distro" in ubuntu|debian|kali|fedora|opensuse|void|archlinux|alpine|custom) ;; *) echo "Unknown distro: $_distro" >&2; return 2 ;; esac if [ -z "$_de" ]; then - if [ "$IS_LEGACY" = "1" ] || [ "$(setup_ram_mb)" -le 2048 ]; then _de="none"; else _de="xfce4"; fi + if [ "$_distro" = "custom" ] || [ "$IS_LEGACY" = "1" ] || [ "$(setup_ram_mb)" -le 2048 ]; then + _de="none" + else + _de="xfce4" + fi fi case "$_de" in xfce|xfce4) _de="xfce4" ;; lxde|mate|gnome|none) ;; *) echo "Unknown desktop: $_de" >&2; return 2 ;; esac if [ "$_de" = "none" ] && [ -n "$_backend" ] && [ "$_backend" != "none" ]; then @@ -6034,6 +6112,10 @@ lfa_cli_install() { fi _tz="${_tz:-$(setup_guess_timezone)}" DISTRO="$_distro"; DE="$_de"; SERVER="$_backend" + if [ "$_distro" = "custom" ]; then + LFA_CUSTOM=1 + LFA_CUSTOM_SOURCE="$_source" + fi INSTANCE_NAME="$_inst" SETUP_USER="$_user" SETUP_USER_B64="$(b64enc "${LFA_USER_PASSWORD:-root}")" @@ -6042,6 +6124,7 @@ lfa_cli_install() { RESOLUTION="${LFA_RESOLUTION:-1280x720}" APP_CHOICES="$_software_choices" NEED_VIRGL=1; [ "$IS_LEGACY" = "1" ] && NEED_VIRGL=0 + [ "$_de" = "none" ] && NEED_VIRGL=0 DO_AUTOSTART=0; AUTOSTART_SERVER="none" if [ -n "$_auto" ] && [ "$_auto" != "none" ]; then case "$_auto" in x11|vnc) ;; *) echo "Unknown autostart backend: $_auto" >&2; return 2 ;; esac @@ -6379,7 +6462,8 @@ select_os_flavor() { "5|OpenSUSE|Zypper" \ "6|Void Linux|XBPS" \ "7|Arch Linux|Pacman · may be unstable on ARM64" \ - "8|Alpine Linux|APK · musl · best for Android 5/6"; then + "8|Alpine Linux|APK · musl · best for Android 5/6" \ + "9|Custom Linux ISO / rootfs|extract any proot-compatible userland"; then return 1 fi case "$UI_SELECTED" in @@ -6391,6 +6475,7 @@ select_os_flavor() { 6) DISTRO="void" ;; 7) DISTRO="archlinux" ;; 8) DISTRO="alpine" ;; + 9) DISTRO="custom" ;; esac } @@ -6446,6 +6531,451 @@ pd_instance_exists() { [ -d "$base/installed-rootfs/$inst/var" ] || [ -d "$base/installed-rootfs/$inst/usr" ] } +# ============================================================ +# Custom Linux userlands (ISO, rootfs tarball, directory) +# +# proot has no kernel, no loop devices and no real mounts, so an ISO +# cannot be booted. What can run is its root filesystem: LFA extracts +# the live squashfs and treats the result like any other container. +# The userland must match the phone CPU and carry its own package +# manager; LFA only automates the distros it recognises. +# ============================================================ +LFA_CUSTOM=0 +LFA_CUSTOM_SOURCE="" +LFA_CUSTOM_FAMILY="" +LFA_CUSTOM_ID="" +LFA_CUSTOM_NAME="" +LFA_CUSTOM_ARCH="" +LFA_CUSTOM_STAGE="" +LFA_CUSTOM_UNKNOWN=0 + +lfa_custom_conf_escape() { # $1 = text for a double-quoted conf value + printf '%s' "${1:-}" | sed 's/[\\"$`]/\\&/g' +} + +lfa_custom_expand_path() { # $1 = user path; prints an absolute path + local _p="${1:-}" _tilde="~" + _p="${_p%\"}"; _p="${_p#\"}" + _p="${_p%\'}"; _p="${_p#\'}" + case "$_p" in + "$_tilde") _p="$HOME" ;; + "$_tilde"/*) _p="$HOME/${_p:2}" ;; + esac + case "$_p" in + /*) ;; + *) _p="$PWD/$_p" ;; + esac + printf '%s\n' "$_p" +} + +lfa_custom_host_arch() { + case "$(uname -m)" in + aarch64|arm64|armv8b) printf 'aarch64\n' ;; + armv7l|armv7|armv6l|arm) printf 'arm\n' ;; + x86_64|amd64) printf 'x86_64\n' ;; + i386|i486|i586|i686) printf 'i386\n' ;; + *) uname -m ;; + esac +} + +lfa_custom_has_sh() { # $1 = rootfs + local _r="$1" + if [ -e "$_r/bin/sh" ] || [ -L "$_r/bin/sh" ]; then return 0; fi + if [ -e "$_r/usr/bin/sh" ] || [ -L "$_r/usr/bin/sh" ]; then return 0; fi + return 1 +} + +lfa_custom_check_arch() { # $1 = rootfs + local _g _h + _g="$(lfa_usb_elf_arch "$1")" + _h="$(lfa_custom_host_arch)" + LFA_CUSTOM_ARCH="$_g" + if [ "$_g" = "unknown" ]; then + echo "[!] Could not read the userland architecture; continuing anyway." + return 0 + fi + [ "$_g" = "$_h" ] && return 0 + case "$_h:$_g" in + aarch64:arm|x86_64:i386) + echo "[!] This is a $_g userland on a $_h device. It can work, but" + echo " the 32-bit runtime is not installed by default." + return 0 ;; + esac + echo "[!] This image is $_g, but this device is $_h." + echo " proot does not emulate another CPU, so it cannot run here." + echo " Use an $_h rootfs or ISO (on phones that is usually arm64)." + return 1 +} + +lfa_custom_read_osrelease() { # $1 = rootfs; sets ID/NAME globals + local _root="$1" _rel="" _name + LFA_CUSTOM_ID="" + LFA_CUSTOM_NAME="" + for _rel in "$_root/etc/os-release" "$_root/usr/lib/os-release"; do + [ -r "$_rel" ] && break + _rel="" + done + [ -n "$_rel" ] || return 0 + LFA_CUSTOM_ID="$(sed -n 's/^ID=//p' "$_rel" | head -n1 | tr -d "\"'")" + LFA_CUSTOM_NAME="$(sed -n 's/^PRETTY_NAME=//p' "$_rel" | head -n1 | tr -d "\"'")" + if [ -z "$LFA_CUSTOM_NAME" ]; then + _name="$(sed -n 's/^NAME=//p' "$_rel" | head -n1 | tr -d "\"'")" + LFA_CUSTOM_NAME="$_name" + fi +} + +lfa_custom_family() { # $1 = rootfs; prints a family LFA can drive + local _root="$1" _like="" _rel="" _tok + lfa_custom_read_osrelease "$_root" + for _rel in "$_root/etc/os-release" "$_root/usr/lib/os-release"; do + [ -r "$_rel" ] && break + _rel="" + done + if [ -n "$_rel" ]; then + _like="$(sed -n 's/^ID_LIKE=//p' "$_rel" | head -n1 | tr -d "\"'")" + fi + for _tok in $LFA_CUSTOM_ID $_like; do + case "$_tok" in + kali) printf 'kali\n'; return 0 ;; + ubuntu|linuxmint|pop|elementary|zorin|neon|kubuntu|xubuntu|lubuntu) + printf 'ubuntu\n'; return 0 ;; + debian|devuan|raspbian|mx|parrot) + printf 'debian\n'; return 0 ;; + fedora|rhel|centos|rocky|almalinux|nobara|bazzite|amzn) + printf 'fedora\n'; return 0 ;; + opensuse*|suse|sles|tumbleweed|leap) + printf 'opensuse\n'; return 0 ;; + void) printf 'void\n'; return 0 ;; + arch|archlinux|archarm|manjaro|endeavouros|garuda|artix|cachyos) + printf 'archlinux\n'; return 0 ;; + alpine|postmarketos) + printf 'alpine\n'; return 0 ;; + esac + done + printf 'unknown\n' +} + +lfa_custom_type() { # $1 = source; prints dir|tar|image|unknown + local _p="$1" _magic + if [ -d "$_p" ]; then printf 'dir\n'; return 0; fi + case "$_p" in + *.iso|*.ISO|*.img|*.IMG|*.zip|*.ZIP) printf 'image\n'; return 0 ;; + *.tar|*.tar.gz|*.tgz|*.tar.xz|*.txz|*.tar.bz2|*.tbz2|*.tar.zst|*.tzst) + printf 'tar\n'; return 0 ;; + esac + _magic="$(od -An -tx1 -N6 "$_p" 2>/dev/null | tr -d ' \n')" + case "$_magic" in + 1f8b*|fd377a585a00*|425a68*|28b52ffd*|504b0304*) + printf 'tar\n'; return 0 ;; + esac + if [ "$(dd if="$_p" bs=1 skip=32769 count=5 2>/dev/null)" = "CD001" ]; then + printf 'image\n'; return 0 + fi + printf 'unknown\n' +} + +lfa_custom_is_oci() { # $1 = tar; true for an OCI/docker archive + local _list _rc=1 + _list="$(mktemp "$PREFIX/tmp/lfa-oci.XXXXXX")" || return 1 + lfa_track_tmp "$_list" + if tar -tf "$1" > "$_list" 2>/dev/null; then + grep -qxE 'oci-layout|manifest\.json' "$_list" && _rc=0 + fi + rm -f "$_list" + return $_rc +} + +lfa_custom_ask_source() { + local _p + echo "" + echo " Custom Linux userland (runs under proot)" + echo " Point at a rootfs tarball, an extracted directory, or a live ISO." + echo " For an ISO, LFA extracts the live root filesystem; proot cannot" + echo " boot the ISO itself." + echo "" + echo " Example: ~/storage/downloads/debian-rootfs.tar.gz" + echo "" + while :; do + if ! read -r -p "Path to ISO / rootfs archive / directory: " _p; then + echo "" + return 1 + fi + if [ -z "$_p" ]; then + echo "[!] Enter a path (or press Ctrl-C to go back)." + continue + fi + _p="$(lfa_custom_expand_path "$_p")" + if [ ! -e "$_p" ]; then + echo "[!] Not found: $_p" + continue + fi + LFA_CUSTOM_SOURCE="$_p" + return 0 + done +} + +lfa_custom_legacy_layout() { + [ "${IS_LEGACY:-0}" = "1" ] && return 0 + command -v proot-distro >/dev/null 2>&1 && pd_is_v4 && return 0 + return 1 +} + +lfa_custom_rootfs_new() { # $1 = instance; prints the directory to create + local _base="$PREFIX/var/lib/proot-distro" + if lfa_custom_legacy_layout; then + printf '%s' "$_base/installed-rootfs/$1" + elif [ -d "$_base/installed-rootfs" ] && [ ! -d "$_base/containers" ]; then + printf '%s' "$_base/installed-rootfs/$1" + else + printf '%s' "$_base/containers/$1/rootfs" + fi +} + +lfa_custom_default_name() { + local _n="${LFA_CUSTOM_ID:-custom}" + _n="$(printf '%s' "$_n" | tr -c 'A-Za-z0-9_.-' '-' | sed 's/^-*//; s/-*$//')" + [ -n "$_n" ] || _n="custom" + printf '%s\n' "$_n" +} + +lfa_custom_install_tool() { # $1 = command, $2 = package + command -v "$1" >/dev/null 2>&1 && return 0 + echo "[*] Installing $2 (needed to unpack this image)..." + pkg install -y "$2" >/dev/null 2>&1 || pkg install "$2" -y >/dev/null 2>&1 || true + command -v "$1" >/dev/null 2>&1 +} + +lfa_custom_flatten() { # $1 = rootfs; lift a single top-level directory + local _root="$1" _entry + lfa_custom_has_sh "$_root" && return 0 + local -a _entries=() + for _entry in "$_root"/*; do + [ -e "$_entry" ] || continue + _entries+=("$_entry") + done + [ "${#_entries[@]}" -eq 1 ] || return 1 + [ -d "${_entries[0]}" ] || return 1 + lfa_custom_has_sh "${_entries[0]}" || return 1 + ( shopt -s dotglob; mv "${_entries[0]}"/* "$_root"/ ) || return 1 + rmdir "${_entries[0]}" 2>/dev/null || true + return 0 +} + +lfa_custom_extract_tar() { # $1 = archive, $2 = target + local _src="$1" _dst="$2" _list + # Refuse absolute or parent-relative members. A rootfs tarball has no + # reason to escape its destination, and the target is user-supplied. + if _list="$(tar -tf "$_src" 2>/dev/null)"; then + if printf '%s\n' "$_list" | grep -Eq '(^/|(^|/)\.\.(/|$))'; then + echo "[!] Refusing an archive with absolute or parent-relative paths." + return 1 + fi + fi + if tar -xf "$_src" -C "$_dst" 2>/dev/null; then return 0; fi + case "$_src" in + *.tar.gz|*.tgz) tar -xzf "$_src" -C "$_dst" ;; + *.tar.xz|*.txz) tar -xJf "$_src" -C "$_dst" ;; + *.tar.bz2|*.tbz2) tar -xjf "$_src" -C "$_dst" ;; + *.tar.zst|*.tzst) tar --zstd -xf "$_src" -C "$_dst" ;; + *) return 1 ;; + esac +} + +lfa_custom_extract_image() { # $1 = ISO/raw image, $2 = target + local _img="$1" _dst="$2" _work="" _stage="" _cand _found=0 + local -a _cands=() + local _log="$PREFIX/tmp/lfa-custom-extract.log" + lfa_track_tmp "$_log" + _work="$(mktemp -d "$PREFIX/tmp/lfa-custom-iso.XXXXXX")" || return 1 + lfa_track_tmpdir "$_work" + _stage="$(mktemp -d "$PREFIX/tmp/lfa-custom-root.XXXXXX")" || { + rm -rf "$_work"; return 1 + } + lfa_track_tmpdir "$_stage" + if ! lfa_custom_install_tool 7z 7zip; then + echo "[!] 7z is required to unpack this image." + echo " Install it with: pkg install 7zip" + rm -rf "$_work" "$_stage" + return 1 + fi + echo "[*] Unpacking $(basename "$_img") (this can take a while)..." + if ! 7z x -y -o"$_work" "$_img" >"$_log" 2>&1; then + echo "[!] Could not unpack this image." + tail -n 5 "$_log" 2>/dev/null || true + rm -rf "$_work" "$_stage" + return 1 + fi + # A zipped rootfs often has one top-level directory; flatten it before + # deciding that this image has no root filesystem at all. + lfa_custom_flatten "$_work" || true + if lfa_custom_has_sh "$_work"; then + cp -a "$_work/." "$_dst/" || { + echo "[!] Could not copy the extracted root filesystem." + rm -rf "$_work" "$_stage" + return 1 + } + rm -rf "$_work" "$_stage" + return 0 + fi + while IFS= read -r _cand; do + [ -n "$_cand" ] && _cands+=("$_cand") + done < <(find "$_work" -type f \( \ + -name 'filesystem.squashfs' -o -name 'rootfs.squashfs' -o \ + -name 'airootfs.squashfs' -o -name '*.squashfs' -o \ + -name 'squashfs.img' -o -name 'rootfs.img' -o -name '*.ext4' \) \ + 2>/dev/null | sort) + if [ "${#_cands[@]}" -eq 0 ]; then + echo "[!] No root filesystem was found in this image." + echo " Live ISOs store one as squashfs; installer-only images do not." + echo " Use a rootfs tarball for those." + rm -rf "$_work" "$_stage" + return 1 + fi + for _cand in "${_cands[@]}"; do + echo "[*] Found $(basename "$_cand"); extracting..." + find "$_stage" -mindepth 1 -delete 2>/dev/null || true + # unsquashfs preserves ownership, modes and symlinks better than a + # generic archive unpacker. Fall back to 7z when it is missing or + # when the candidate is an ext4 image rather than squashfs. + if command -v unsquashfs >/dev/null 2>&1 && \ + unsquashfs -f -d "$_stage" "$_cand" >"$_log" 2>&1; then + : + else + find "$_stage" -mindepth 1 -delete 2>/dev/null || true + if ! 7z x -y -o"$_stage" "$_cand" >"$_log" 2>&1; then + echo "[!] Could not unpack $(basename "$_cand")." + continue + fi + fi + lfa_custom_flatten "$_stage" || true + if ! lfa_custom_has_sh "$_stage"; then + # Fedora LiveOS keeps an ext4 root inside squashfs.img. + local _nested _nested_dir + _nested="$(find "$_stage" -maxdepth 2 -type f \( -name '*.img' -o -name '*.ext4' \) 2>/dev/null | head -n1)" + if [ -n "$_nested" ]; then + echo "[*] Unpacking the nested $(basename "$_nested")..." + _nested_dir="$(mktemp -d "$PREFIX/tmp/lfa-custom-root2.XXXXXX")" || continue + lfa_track_tmpdir "$_nested_dir" + if 7z x -y -o"$_nested_dir" "$_nested" >"$_log" 2>&1; then + lfa_custom_flatten "$_nested_dir" || true + if lfa_custom_has_sh "$_nested_dir"; then + find "$_stage" -mindepth 1 -delete 2>/dev/null || true + cp -a "$_nested_dir/." "$_stage/" || true + fi + fi + rm -rf "$_nested_dir" + fi + fi + if lfa_custom_has_sh "$_stage"; then + cp -a "$_stage/." "$_dst/" || { + echo "[!] Could not copy the extracted root filesystem." + rm -rf "$_work" "$_stage" + return 1 + } + _found=1 + break + fi + done + rm -rf "$_work" "$_stage" + if [ "$_found" != "1" ]; then + echo "[!] This image does not contain a usable root filesystem." + echo " Installer-only ISOs need a kernel and loop devices, which proot" + echo " does not provide. Use the distro's rootfs/cloud image instead." + return 1 + fi + return 0 +} + +lfa_custom_prepare() { # $1 = source, $2 = target rootfs + local _src _dst _type + _src="$(lfa_custom_expand_path "${1:-}")" + _dst="${2:-}" + [ -n "$_dst" ] || return 1 + LFA_CUSTOM_SOURCE="$_src" + LFA_CUSTOM_FAMILY="unknown" + LFA_CUSTOM_ID="" + LFA_CUSTOM_NAME="" + LFA_CUSTOM_ARCH="" + if [ ! -e "$_src" ]; then + echo "[!] Not found: $_src" + return 1 + fi + if [ ! -r "$_src" ]; then + echo "[!] Cannot read: $_src" + return 1 + fi + _type="$(lfa_custom_type "$_src")" + if [ "$_type" = "unknown" ] && [ -f "$_src" ] && lfa_custom_is_oci "$_src"; then + echo "[!] This is an OCI/Docker archive, which needs proot-distro v5:" + echo " proot-distro install --name NAME \"$_src\"" + echo " Or export a plain rootfs tarball: docker export NAME -o rootfs.tar" + return 1 + fi + case "$_type" in + dir) + echo "[*] Copying the root filesystem from $_src..." + cp -a "$_src/." "$_dst/" || { + echo "[!] Could not copy this directory." + return 1 + } + ;; + tar) + echo "[*] Extracting $(basename "$_src")..." + if ! lfa_custom_extract_tar "$_src" "$_dst"; then + echo "[!] Could not extract this archive." + echo " Supported: .tar, .tar.gz, .tar.xz, .tar.bz2, .tar.zst" + return 1 + fi + lfa_custom_flatten "$_dst" || true + ;; + image) + lfa_custom_extract_image "$_src" "$_dst" || return 1 + ;; + *) + echo "[!] Unsupported source: $_src" + echo " Use a rootfs directory, a rootfs tarball, or a live ISO." + return 1 + ;; + esac + if ! lfa_custom_has_sh "$_dst"; then + echo "[!] The extracted tree has no /bin/sh, so it is not a root filesystem." + echo " For an ISO, LFA needs the live squashfs, not just the boot files." + return 1 + fi + lfa_custom_check_arch "$_dst" || return 1 + # Read os-release in this shell: a command substitution would run + # lfa_custom_family in a subshell and throw away the ID/NAME it sets. + lfa_custom_read_osrelease "$_dst" + LFA_CUSTOM_FAMILY="$(lfa_custom_family "$_dst")" + [ -n "$LFA_CUSTOM_ID" ] || LFA_CUSTOM_ID="custom" + [ -n "$LFA_CUSTOM_NAME" ] || LFA_CUSTOM_NAME="Custom Linux ($LFA_CUSTOM_FAMILY)" + echo "[*] Detected: $LFA_CUSTOM_NAME (${LFA_CUSTOM_FAMILY}, ${LFA_CUSTOM_ARCH:-unknown})" + return 0 +} + +lfa_custom_stage_source() { + # Extract the chosen userland into a private staging directory. The + # guided setup calls this before the desktop/software menus so the + # package family is known; install_linux reuses the same tree instead + # of unpacking a second time. The EXIT trap removes a stage that never + # reaches an installed instance. + [ -n "${LFA_CUSTOM_SOURCE:-}" ] || return 1 + LFA_CUSTOM_STAGE="$PREFIX/var/lib/proot-distro/.lfa-custom-$$" + rm -rf "$LFA_CUSTOM_STAGE" + if ! mkdir -p "$LFA_CUSTOM_STAGE"; then + echo "[!] Could not create the staging directory." + LFA_CUSTOM_STAGE="" + return 1 + fi + if ! lfa_custom_prepare "$LFA_CUSTOM_SOURCE" "$LFA_CUSTOM_STAGE"; then + rm -rf "$LFA_CUSTOM_STAGE" + LFA_CUSTOM_STAGE="" + return 1 + fi + LFA_CUSTOM=1 + return 0 +} + select_installed_instance() { INSTANCE_NAME="" if [ -n "$CLI_INSTANCE" ]; then @@ -8813,6 +9343,37 @@ install_linux() { return fi fi + # Custom userland: unpack it now so the family is known before the + # desktop and software menus. The final instance name is asked later, + # so the tree waits in a staging directory until then. + if [ "${LFA_CUSTOM:-0}" = "1" ] || [ "$DISTRO" = "custom" ]; then + LFA_CUSTOM=1 + if [ -z "${LFA_CUSTOM_SOURCE:-}" ]; then + if ! lfa_custom_ask_source; then + show_menu + return + fi + fi + # Guided setup already unpacked the tree to learn the family. The + # CLI and classic menu arrive here with no stage and unpack now. + if [ -z "${LFA_CUSTOM_STAGE:-}" ]; then + if ! lfa_custom_stage_source; then + ui_pause + show_menu + return 1 + fi + fi + DISTRO="$LFA_CUSTOM_FAMILY" + if [ "$DISTRO" = "unknown" ]; then + LFA_CUSTOM_UNKNOWN=1 + DISTRO="custom" + DE="none"; SERVER="none"; AUTOSTART_SERVER="none" + echo "" + echo "[!] LFA does not know this userland's package manager." + echo " It installs as a terminal-only system. Add a desktop inside" + echo " the guest with its own package manager if you want one." + fi + fi # 32-bit ARM warning (Oppo A37fw and similar): some images lack armv7 builds if [ "$IS_32BIT_ARM" = "1" ]; then case "$DISTRO" in @@ -8827,7 +9388,13 @@ install_linux() { # first guest command (verified live). Debian auto-switches to the # bullseye plug-in (glibc 2.31, verified); Alpine (musl) is proven OK. PD_ALIAS="$DISTRO" - if [ "$IS_LEGACY" = "1" ]; then + if [ "${LFA_CUSTOM:-0}" = "1" ]; then + PD_ALIAS="" + if [ "$IS_LEGACY" = "1" ]; then + echo "[!] Android 5/6 runs a 3.10 kernel; modern glibc userlands fail." + echo " A musl rootfs (Alpine) is the safe choice on this device." + fi + elif [ "$IS_LEGACY" = "1" ]; then case "$DISTRO" in alpine) ;; debian) @@ -8856,7 +9423,7 @@ install_linux() { esac fi - if [ "$SETUP_ANSWERED" != "1" ]; then + if [ "$SETUP_ANSWERED" != "1" ] && [ "${LFA_CUSTOM_UNKNOWN:-0}" != "1" ]; then DE="none" SERVER="none" AUTOSTART_SERVER="" @@ -8923,7 +9490,7 @@ install_linux() { fi # SOFTWARE SELECTOR - if [ "$SETUP_ANSWERED" != "1" ]; then + if [ "$SETUP_ANSWERED" != "1" ] && [ "${LFA_CUSTOM_UNKNOWN:-0}" != "1" ]; then if ! ui_multi "Software Selector (optional)" \ "1|Python|python3 + pip" \ "2|Git|version control" \ @@ -8968,18 +9535,47 @@ install_linux() { 14) INSTALL_VSCODE=1 ;; esac done + if [ "${LFA_CUSTOM_UNKNOWN:-0}" = "1" ]; then + INSTALL_PYTHON=0; INSTALL_GIT=0; INSTALL_NODEJS=0; INSTALL_CURL=0 + INSTALL_WGET=0; INSTALL_IDE=0; INSTALL_FIREFOX=0; INSTALL_CHROMIUM=0 + INSTALL_LIBREOFFICE=0; INSTALL_VLC=0; INSTALL_GIMP=0 + INSTALL_HTOP=0; INSTALL_NEOFETCH=0; INSTALL_VSCODE=0 + APP_CHOICES="" + fi echo "" if [ "$SETUP_ANSWERED" != "1" ]; then read -p "Enter a custom name for this installation (Leave blank for auto-generation): " INSTANCE_NAME fi if [ -z "$INSTANCE_NAME" ]; then - if pd_instance_exists "$DISTRO" || [ -f "$PREFIX/etc/proot-distro/$DISTRO.sh" ]; then + if [ "${LFA_CUSTOM:-0}" = "1" ]; then + INSTANCE_NAME="$(lfa_custom_default_name)" + elif pd_instance_exists "$DISTRO" || [ -f "$PREFIX/etc/proot-distro/$DISTRO.sh" ]; then INSTANCE_NAME="${DISTRO}-$(date +%M%S)" else INSTANCE_NAME="$DISTRO" fi fi + if [ "${LFA_CUSTOM:-0}" = "1" ] && [ -n "${LFA_CUSTOM_STAGE:-}" ]; then + local _custom_final + _custom_final="$(lfa_custom_rootfs_new "$INSTANCE_NAME")" + if pd_instance_exists "$INSTANCE_NAME"; then + echo "[!] '$INSTANCE_NAME' already exists; using a timestamped name." + INSTANCE_NAME="${INSTANCE_NAME}-$(date +%M%S)" + _custom_final="$(lfa_custom_rootfs_new "$INSTANCE_NAME")" + fi + mkdir -p "$(dirname "$_custom_final")" + if ! mv "$LFA_CUSTOM_STAGE" "$_custom_final"; then + echo "[!] Could not move the userland into place." + rm -rf "$LFA_CUSTOM_STAGE" + LFA_CUSTOM_STAGE="" + ui_pause + show_menu + return 1 + fi + LFA_CUSTOM_STAGE="" + echo "[*] Userland ready as '$INSTANCE_NAME'." + fi echo "" echo "=========================================" @@ -9017,6 +9613,9 @@ install_linux() { return fi + if [ "${LFA_CUSTOM:-0}" = "1" ]; then + echo "[*] Using the extracted custom userland (as $INSTANCE_NAME)..." + else echo "[*] Installing $DISTRO (as $INSTANCE_NAME)..." verify_proot_distro_plugin "$PD_ALIAS" || { ui_pause @@ -9049,6 +9648,7 @@ install_linux() { else proot-distro install --override-alias "$INSTANCE_NAME" "$PD_ALIAS" 2>/dev/null || proot-distro install "$PD_ALIAS" fi + fi ROOTFS="$(pd_rootfs "$INSTANCE_NAME")" mkdir -p "$PREFIX/tmp" @@ -9242,6 +9842,13 @@ install_linux() { if [ "$INSTALL_NEOFETCH" == "1" ]; then SELECTED_PKGS="$SELECTED_PKGS fastfetch"; fi if [ "$INSTALL_VSCODE" == "1" ]; then echo "[!] VS Code has no Alpine build, skipping."; fi ;; + *) + # Custom userland whose package manager LFA does not know. + UPDATE_CMD="true" + INSTALL_CMD="true" + XFCE_PKG=""; LXDE_PKG=""; MATE_PKG=""; GNOME_PKG="" + VNC_PKG=""; LOADER_PKG=""; SUDO_PKG=""; SELECTED_PKGS="" + ;; esac # Save the configuration for dynamic boot! @@ -9279,12 +9886,21 @@ install_linux() { echo "HOSTNAME=\"${HOSTNAME:-localhost}\"" >> "$ROOTFS/etc/termux-linux-manager.conf" echo "NEED_VIRGL=\"${NEED_VIRGL:-1}\"" >> "$ROOTFS/etc/termux-linux-manager.conf" echo "PSTYLE=\"${PSTYLE:-minimal}\"" >> "$ROOTFS/etc/termux-linux-manager.conf" + if [ "${LFA_CUSTOM:-0}" = "1" ]; then + echo "CUSTOM_SOURCE=\"$(lfa_custom_conf_escape "${LFA_CUSTOM_SOURCE:-}")\"" >> "$ROOTFS/etc/termux-linux-manager.conf" + echo "CUSTOM_NAME=\"$(lfa_custom_conf_escape "${LFA_CUSTOM_NAME:-}")\"" >> "$ROOTFS/etc/termux-linux-manager.conf" + echo "CUSTOM_ID=\"$(lfa_custom_conf_escape "${LFA_CUSTOM_ID:-}")\"" >> "$ROOTFS/etc/termux-linux-manager.conf" + fi if [ -n "${IMAGE:-}" ]; then echo "IMAGE=\"$IMAGE\"" >> "$ROOTFS/etc/termux-linux-manager.conf" fi # (Removed broken universal wallpaper download) + if [ "${LFA_CUSTOM_UNKNOWN:-0}" = "1" ]; then + echo "[*] Skipping guest setup: no known package manager." + echo " Open a root shell with: lfa shell -i $INSTANCE_NAME --root" + else cat << EOF > "$SETUP_SCRIPT" #!/bin/bash export DEBIAN_FRONTEND=noninteractive @@ -9923,6 +10539,7 @@ EOF ui_pause return 1 fi + fi echo "[*] Setting up Home-Screen Widget Integration..." mkdir -p ~/.shortcuts diff --git a/tests/test-all.sh b/tests/test-all.sh index 81ee90b..f754150 100755 --- a/tests/test-all.sh +++ b/tests/test-all.sh @@ -219,7 +219,7 @@ check "B install done" 'Installation complete!' "$T/b.log" echo "===== C: guided debian CLI, custom user, validation loops =====" fresh_env -printf '%s\n' 2 9 2 4 99 'x y' '' '' 'Bad Name!' 'dev' '' 's3cret' '' 'oppo-test' 5 2 'y' '' 'n' 'n' '' '' '13' \ +printf '%s\n' 2 10 2 4 99 'x y' '' '' 'Bad Name!' 'dev' '' 's3cret' '' 'oppo-test' 5 2 'y' '' 'n' 'n' '' '' '13' \ | bash "$REPO/install_linux.sh" > "$T/c.log" 2>&1; rc=$?; [ "$rc" = "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: run c exit $rc"; } S=$(setup_sh_of) check "C user" 'useradd -m -s /bin/bash dev' "$S" @@ -2252,6 +2252,131 @@ check "Y11b README command" 'lfa usb' "$REPO/README.md" check "Y11b docs index" 'USB.md' "$REPO/docs/README.md" check "Y11b incompatible points to guide" 'USB.md' "$REPO/docs/INCOMPATIBLE.md" +echo "===== Y12: custom rootfs tarball (known family) =====" +fresh_env +mkdir -p "$T/custom-deb/bin" "$T/custom-deb/etc" +printf '#!/bin/sh\n' > "$T/custom-deb/bin/sh" +chmod +x "$T/custom-deb/bin/sh" +printf 'ID=debian\nPRETTY_NAME="Test Debian"\n' > "$T/custom-deb/etc/os-release" +tar -czf "$T/custom-deb.tar.gz" -C "$T/custom-deb" . +timeout 90 bash "$REPO/install_linux.sh" --cli install \ + --rootfs "$T/custom-deb.tar.gz" -i customdeb --desktop none --user user \ + > "$T/y12.log" 2>&1; rc=$? +[ "$rc" = "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12 custom rootfs exit $rc"; } +check "Y12 completes" 'Installation complete!' "$T/y12.log" +check "Y12 detected name" 'Test Debian' "$T/y12.log" +CROOT="$T/prefix/var/lib/proot-distro/containers/customdeb/rootfs" +[ -d "$CROOT" ] || CROOT="$T/prefix/var/lib/proot-distro/installed-rootfs/customdeb" +[ -x "$CROOT/bin/sh" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12 rootfs not extracted"; } +check "Y12 conf family" 'DISTRO="debian"' "$CROOT/etc/termux-linux-manager.conf" +check "Y12 conf name" 'CUSTOM_NAME="Test Debian"' "$CROOT/etc/termux-linux-manager.conf" +check "Y12 conf id" 'CUSTOM_ID="debian"' "$CROOT/etc/termux-linux-manager.conf" +check "Y12 conf source" 'CUSTOM_SOURCE="' "$CROOT/etc/termux-linux-manager.conf" +check_absent "Y12 no proot-distro install" 'install' "$T/home/pd-calls.log" +check "Y12 guest setup" 'apt-get install' "$(setup_sh_of)" + +echo "===== Y12b: custom rootfs, unknown package manager =====" +fresh_env +mkdir -p "$T/custom-unk/bin" "$T/custom-unk/etc" +printf '#!/bin/sh\n' > "$T/custom-unk/bin/sh" +chmod +x "$T/custom-unk/bin/sh" +printf 'ID=customos\nPRETTY_NAME="Test Custom"\n' > "$T/custom-unk/etc/os-release" +tar -czf "$T/custom-unk.tar.gz" -C "$T/custom-unk" . +timeout 90 bash "$REPO/install_linux.sh" --cli install \ + --rootfs "$T/custom-unk.tar.gz" -i customunk --desktop none --user user \ + > "$T/y12b.log" 2>&1; rc=$? +[ "$rc" = "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12b unknown rootfs exit $rc"; } +check "Y12b unknown notice" 'does not know this userland' "$T/y12b.log" +check "Y12b skipped setup" 'Skipping guest setup' "$T/y12b.log" +check "Y12b shell hint" 'lfa shell -i customunk --root' "$T/y12b.log" +CROOT="$T/prefix/var/lib/proot-distro/containers/customunk/rootfs" +[ -d "$CROOT" ] || CROOT="$T/prefix/var/lib/proot-distro/installed-rootfs/customunk" +check "Y12b conf family" 'DISTRO="custom"' "$CROOT/etc/termux-linux-manager.conf" +check "Y12b conf id" 'CUSTOM_ID="customos"' "$CROOT/etc/termux-linux-manager.conf" +check "Y12b conf name" 'CUSTOM_NAME="Test Custom"' "$CROOT/etc/termux-linux-manager.conf" +YS=$(setup_sh_of) +if [ -z "$YS" ] || [ ! -s "$YS" ]; then pass=$((pass+1)); else fail=$((fail+1)); echo "FAIL: Y12b generated a guest setup script"; fi + +echo "===== Y12c: custom live ISO (squashfs extraction) =====" +fresh_env +mkdir -p "$T/iso-root/bin" "$T/iso-root/etc" +printf '#!/bin/sh\n' > "$T/iso-root/bin/sh" +chmod +x "$T/iso-root/bin/sh" +printf 'ID=alpine\nPRETTY_NAME="Test Alpine"\n' > "$T/iso-root/etc/os-release" +: > "$T/fake.iso" +cat > "$T/fakebin/7z" < "\$out/live/filesystem.squashfs" ;; + *.squashfs) cp -a "$T/iso-root/." "\$out/" ;; + *) exit 1 ;; +esac +EOF +chmod +x "$T/fakebin/7z" +timeout 90 bash "$REPO/install_linux.sh" --cli install \ + --iso "$T/fake.iso" -i customiso --desktop none --user user \ + > "$T/y12c.log" 2>&1; rc=$? +rm -f "$T/fakebin/7z" +[ "$rc" = "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12c custom ISO exit $rc"; } +check "Y12c unpacks image" 'Unpacking fake.iso' "$T/y12c.log" +check "Y12c finds squashfs" 'Found filesystem.squashfs' "$T/y12c.log" +check "Y12c detected name" 'Test Alpine' "$T/y12c.log" +CROOT="$T/prefix/var/lib/proot-distro/containers/customiso/rootfs" +[ -d "$CROOT" ] || CROOT="$T/prefix/var/lib/proot-distro/installed-rootfs/customiso" +check "Y12c conf family" 'DISTRO="alpine"' "$CROOT/etc/termux-linux-manager.conf" + +echo "===== Y12d: custom rootfs with the wrong CPU architecture =====" +fresh_env +mkdir -p "$T/custom-bad/bin" "$T/custom-bad/etc" +case "$(uname -m)" in + aarch64|arm64) printf '\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3e\x00' > "$T/custom-bad/bin/sh" ;; + *) printf '\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb7\x00' > "$T/custom-bad/bin/sh" ;; +esac +printf 'ID=debian\nPRETTY_NAME="Wrong Arch"\n' > "$T/custom-bad/etc/os-release" +tar -czf "$T/custom-bad.tar.gz" -C "$T/custom-bad" . +timeout 90 bash "$REPO/install_linux.sh" --cli install \ + --rootfs "$T/custom-bad.tar.gz" -i custombad --desktop none \ + > "$T/y12d.log" 2>&1; rc=$? +[ "$rc" != "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12d wrong arch was accepted"; } +check "Y12d refuses" 'proot does not emulate another CPU' "$T/y12d.log" +[ ! -e "$T/prefix/var/lib/proot-distro/containers/custombad" ] && \ +[ ! -e "$T/prefix/var/lib/proot-distro/installed-rootfs/custombad" ] && \ +pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12d left a partial rootfs"; } + +echo "===== Y12e: custom source validation =====" +fresh_env +timeout 30 bash "$REPO/install_linux.sh" --cli install --distro custom -i nosrc \ + > "$T/y12e1.log" 2>&1; rc=$? +[ "$rc" = "2" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12e missing source exit $rc"; } +check "Y12e missing source hint" 'needs --iso PATH or --rootfs PATH' "$T/y12e1.log" +timeout 30 bash "$REPO/install_linux.sh" --cli install --rootfs "$T/does-not-exist.tar.gz" -i nosrc \ + > "$T/y12e2.log" 2>&1; rc=$? +[ "$rc" != "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12e bad path was accepted"; } +check "Y12e bad path message" 'Not found' "$T/y12e2.log" + +echo "===== Y12f: guided custom rootfs, unknown family =====" +fresh_env +mkdir -p "$T/guided/bin" "$T/guided/etc" +printf '#!/bin/sh\n' > "$T/guided/bin/sh" +chmod +x "$T/guided/bin/sh" +printf 'ID=customos\nPRETTY_NAME="Guided Custom"\n' > "$T/guided/etc/os-release" +tar -czf "$T/guided.tar.gz" -C "$T/guided" . +printf '%s\n' 2 9 "$T/guided.tar.gz" '' '' '' '' '' '' 1 n n n y '' 13 13 \ + | timeout 90 bash "$REPO/install_linux.sh" > "$T/y12f.log" 2>&1; rc=$? +[ "$rc" = "0" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12f guided custom exit $rc"; } +check "Y12f summary userland" 'Guided Custom' "$T/y12f.log" +check "Y12f skipped setup" 'Skipping guest setup' "$T/y12f.log" +CROOT="$T/prefix/var/lib/proot-distro/containers/customos/rootfs" +[ -d "$CROOT" ] || CROOT="$T/prefix/var/lib/proot-distro/installed-rootfs/customos" +check "Y12f conf family" 'DISTRO="custom"' "$CROOT/etc/termux-linux-manager.conf" +check "Y12f conf name" 'CUSTOM_NAME="Guided Custom"' "$CROOT/etc/termux-linux-manager.conf" +[ -z "$(find "$T/prefix/var/lib/proot-distro" -maxdepth 1 -name '.lfa-custom-*' 2>/dev/null)" ] \ + && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Y12f left a staging directory"; } + echo "" echo "===== Z: documentation links =====" [ -s "$REPO/docs/TROUBLESHOOTING.md" ] && pass=$((pass+1)) || { fail=$((fail+1)); echo "FAIL: Z troubleshooting guide missing"; }