0

I'm trying to start a chroot script on boot with an init on my custom build of Lineage 18.1. Its running all of the script that's not inside of the chroot but once it gets inside of the chroot its not able to run a script that's inside of chroot. I have 4 scripts in total two on android shell and two on chroot.

retros_services.rc:

service sudaemon /system/bin/phh-su --daemon
    class main

service retros_start /system/bin/launch_retros
    disabled
    class late_start
    user root
    group root
    oneshot
    seclabel u:object_r:rootfs:s0

on boot
    exec_start su_daemon

on property:sys.boot_completed=1
    exec_start retros_start

launch_retros script:

#!/system/bin/sh

# wait for the filesystems to actually mount
while [ "`getprop sys.boot_completed | tr -d '\r' `" != "1" ] ; do sleep 1; done
sleep 10

# globals
CHROOT=/data/ubuntu
SET_STAGE=$(cat /sdcard/.setup)

monkey -p ai.flow.android -c android.intent.category.LAUNCHER 1

function disableSystemUI() {
  echo "Disabling systemui"
  LOOP_COUNT=0
  while [ $LOOP_COUNT -le 100 ]
  do
    pm disable com.android.systemui
    sleep 0.1
    LOOP_COUNT=$(( $LOOP_COUNT + 1 ))
  done
  echo "Disabled systemui"
}

# make the tmp dir
if [ ! -e "/data/tmp" ]
then
  mkdir /data/tmp
fi

# progress check
if [ ! -e "/sdcard/.setup" ]
then
  echo "0" > /sdcard/.setup
fi

# setup has not completed
# - lt "4"
if [ "$SET_STAGE" -lt "2" ]
then
  # wifi loop
  # do not progress until there's an internet connection
  while true; do
    if ping -c 1 8.8.8.8; then
      break
    elif [[ "$(dumpsys activity activities | grep mResumedActivity | cut -d "{" -f2 | cut -d ' ' -f3)" != 'com.android.settings/.Settings$NetworkDashboardActivity' ]]; then
      am start -a android.settings.WIRELESS_SETTINGS
    fi
    sleep 1
  done
  pm disable com.android.settings
  pm enable com.android.settings
  # TODO: launch the user setup. for now, just move setup along
  # am start org.retropilot.retros.usersetup
  echo "SET STAGE=$SET_STAGE"
fi

if [ "$SET_STAGE" -eq "0" ]
then
  disableSystemUI
  echo "1" > /sdcard/.setup
  SET_STAGE=1
  reboot
fi

# wait here until user setup gets a URL
while true; do
  if [ "$SET_STAGE" -gt "0" ]
  then
    break
  fi
  sleep 1
done


# download and install the chroot
if [ "$SET_STAGE" -lt "2" ]
then
  # ROOTFS_TAR="ubuntu-focal-core-cloudimg-arm64-root-2020.12.10.tar.gz"
  # curl -L -o /sdcard/Download/ubuntu.tar.gz https://github.com/termux/proot-distro/releases/download/v1.2-ubuntu-focal-rootfs/$ROOTFS_TAR
  curl -L -o /sdcard/Download/ubuntu.tar.gz https://github.com/RetroPilot/flowpilot/releases/download/v0.1.0alpha2/flowpilot-chroot.tar.gz
  mkdir -p $CHROOT
  tar xfp /sdcard/Download/ubuntu.tar.gz -C $CHROOT
  mkdir -p $CHROOT/sdcard
  mkdir -p $CHROOT/data

  # DL reqs file, copy userland script into chroot, add stuff to bashrc
  UBUNTU_REQS='https://raw.githubusercontent.com/flowdriveai/flowpilot/master/scripts/install-flowpilot-android'
  curl $UBUNTU_REQS > $CHROOT/root/install_reqs.sh
  # busybox wget $UBUNTU_REQS -P $CHROOT/root/install_reqs.sh
  chmod +x $CHROOT/root/install_reqs.sh
  cp /etc/retros/flowpilot_userland $CHROOT/bin/flowpilot_userland
  cp /etc/retros/flowpilot_startup $CHROOT/bin/flowpilot_startup
  chmod +x $CHROOT/bin/flowpilot_userland
  chmod +x $CHROOT/bin/flowpilot_startup
  # copy tmux config
  cp /etc/retros/.tmux.conf $CHROOT/root/.tmux.conf

  echo "2" > /sdcard/.setup
  SET_STAGE=2
fi

# login to the chroot
if [ "$SET_STAGE" -ge "2" ]
then
  launch_flowpilot
fi

launch_flowpilot script that is mounting the chroot and starting the chroot:

#!/system/bin/sh

while true; do
  if [ -f "/data/tmp/chroot_booted" ]
  then
    exit
  else
    break
  fi
done

monkey -p ai.flow.android -c android.intent.category.LAUNCHER 1


CHROOT=/data/ubuntu
SET_STAGE=$(cat /sdcard/.setup)

function setupUserSpace() {
    # run the other script
    # mount tmpfs and others\
    mount -o remount,dev,suid /data
    mount -t tmpfs -o size=2048M tmpfs /data/tmp
    mount --bind /data/tmp $CHROOT/tmp
    mount --bind /proc $CHROOT/proc
    mount --bind /dev $CHROOT/dev
    mount --bind /dev/null $CHROOT/dev/null
    mount --bind /dev/pts $CHROOT/dev/pts
    mount --bind /sys $CHROOT/sys
    mount --bind /sdcard $CHROOT/sdcard
    mount --bind /data $CHROOT/data
}

# login to the chroot
if [ "$SET_STAGE" -ge "2" ]
then
  if [ ! "`mount | grep "/data/tmp"`" ]; then
   echo "mounting filesystems"
   setupUserSpace
  else
   echo "filesystems already mounted!"
  fi
  log into the new userland
  echo "logging into userspace now"
  chroot $CHROOT /usr/bin/env HOME=/root PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games TERM=xterm-256color LANG=C.UTF-8 SHELL=/bin/bash flowpilot_startup
fi

flowpilot_setup:

#!/usr/bin/env bash

if ! command -v tmux &> /dev/null
then
  sudo apt-get update
  sudo apt-get install tmux
  sudo apt install -y openssh-server

  # setup perms for ssh and set password
  echo "Port 8022" >> /etc/ssh/sshd_config
  echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
  echo "root:retros" | chpasswd

fi

touch /tmp/TEST

log starting sshd
/usr/sbin/sshd -D &

tmux new-session -d -s retropilot /usr/bin/env flowpilot_userland

flowpilot_userland:

#!/usr/bin/env bash

SET_STAGE=$(cat /sdcard/.setup)

set -e

# White
msg() {
    echo -e "$@"
}

# Yellow
info() {
    printf "\033[1;33m$@\033[0m\n"
}

# Green
success() {
    printf "\033[0;32m$@\033[0m\n"
}

# Red
fail() {
    printf "\033[0;31m$@\033[0m\n"
    exit 1
}

add_to_file() {
    grep -qF -- "$1" "$2" || echo "$1" >> "$2"
}

# FLOWPILOT SETUP
if [ "$SET_STAGE" -eq "2" ]
then 
    info "Preparing to install flowpilot..."

    # fix apt
    add_to_file "nameserver 8.8.8.8" /etc/resolv.conf

    groupadd -g 3001 aid_bt || true
    groupadd -g 3002 aid_bt_net || true
    groupadd -g 3003 aid_inet || true
    groupadd -g 3004 aid_net_raw || true
    groupadd -g 3005 aid_admin || true

    usermod -a -G aid_bt,aid_bt_net,aid_inet,aid_net_raw,aid_admin root || true
    usermod -g 3003 _apt || true

    add_to_file "export ANDROID_DATA=''" ~/.bashrc
    add_to_file "export PIP_ROOT_USER_ACTION=ignore" ~/.bashrc
    export ANDROID_DATA=''
    export PIP_ROOT_USER_ACTION=ignore

    apt update && apt install -y sudo git
    DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata
    sudo apt install -y software-properties-common
    yes | sudo add-apt-repository ppa:deadsnakes/ppa || true
    sudo apt install -y python3.9 python3.9-dev python3.9-distutils python-is-python3 python3-pip
    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1

    echo "3" > /sdcard/.setup
    SET_STAGE=3
    # END FLOWPILOT SETUP
fi

if [ "$SET_STAGE" -eq "3" ]
then
    # GIT CLONE FLOWPILOT
    cd ~/ && [ ! -d 'flowpilot' ] && git clone https://github.com/flowdriveai/flowpilot.git -b community || true
    cd ~/flowpilot && git submodule update --init
    # INSTALL DEPS
    ./get_dependencies.sh
    scons
    echo "4" > /sdcard/.setup
    SET_STAGE=4
fi

if [ ! -e "/run/sshd" ]
then
    mkdir /run/sshd
    chmod 755 /run/sshd
fi

# when it's all said and done, launch FlowPilot's env
if [ "$SET_STAGE" -eq "4" ]
then
    cd ~/flowpilot
    ./launch_flowpilot.sh
    # let android know the env is running so flowpilot can be launched
    touch /tmp/chroot_booted
fi

success "Flowpilot successfully installed. launch using cd flowpilot && ./launch_flowpilot.sh"

The init is running on the first boot and completes the two scripts no problem, but on second boot thats when it should be going on to the chroot but it looks like its getting stopped. I used logcat | grep retros_start and I got this

06-08 20:40:07.015     0     0 I [20230608_20:40:07.014807]@3 init: Service 'retros_start' (pid 2948) exited with status 0 waiting took 202.919998 seconds
06-08 20:40:07.016     0     0 I [20230608_20:40:07.014849]@3 init: Sending signal 9 to service 'retros_start' (pid 2948) process group...
06-08 20:40:07.025     0     0 W [20230608_20:40:07.020980]@3 init: Killed 4 additional processes from a oneshot process group for service 'retros_start'. This is new behavior, previously child processes would not be killed in this case.

Current this is the reason the scripts are split up into 4 instead of 2. I think it could be a permissions issue but I'm really not sure at this point. Running launch_retros in adb shell works just fine without ANY issues.

Here's a link to the vendor files that includes the scripts as well: https://github.com/RetroPilot/android_vendor_lineage/tree/lineage-18.1/prebuilt/common

Looks like the closed related here was this but it looks kinda outdated: cannot do chroot with init shell script in android

0 Answers0