Skip to content

Commit

Permalink
feat(first-boot): install gapps and root device
Browse files Browse the repository at this point in the history
  • Loading branch information
Shmayro committed Oct 15, 2024
1 parent 608404b commit 0028c67
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 43 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ Thumbs.db

# Ignore IDE files
.vscode/
.idea/
.idea/

# Docker compose data
data/
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

41 changes: 26 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ FROM ubuntu:20.04
# Install necessary packages
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
openjdk-11-jdk-headless \
openjdk-17-jdk-headless \
wget \
curl \
git \
lzip \
unzip \
supervisor \
qemu-kvm \
Expand All @@ -18,38 +20,46 @@ RUN apt-get update && \
# Set up Android SDK
RUN mkdir -p /opt/android-sdk/cmdline-tools && \
cd /opt/android-sdk/cmdline-tools && \
wget https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip -O cmdline-tools.zip && \
wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -O cmdline-tools.zip && \
unzip cmdline-tools.zip -d latest && \
rm cmdline-tools.zip && \
mv latest/cmdline-tools/* latest/ || true && \
rm -rf latest/cmdline-tools || true

ENV ANDROID_HOME=/opt/android-sdk
ENV ANDROID_AVD_HOME=/data
ENV ADB_DIR="$ANDROID_HOME/platform-tools"
ENV PATH="$ANDROID_HOME/cmdline-tools/latest/bin:$ADB_DIR:$PATH"

# Initializing the required directories.
RUN mkdir /root/.android/ && \
touch /root/.android/repositories.cfg && \
mkdir /data && \
mkdir /extras

# Copy emulator.zip
COPY emulator.zip /root/emulator.zip
COPY emulator/package.xml /root/package.xml
#COPY emulator.zip /root/emulator.zip
#COPY emulator/package.xml /root/package.xml


# Detect architecture and set environment variable
RUN if [ "$(uname -m)" = "aarch64" ]; then \
unzip /root/emulator.zip -d $ANDROID_HOME && \
mv /root/package.xml $ANDROID_HOME/emulator/package.xml && \
rm /root/emulator.zip && \
yes | sdkmanager --sdk_root=$ANDROID_HOME "platform-tools" "platforms;android-29" "system-images;android-29;default;arm64-v8a" && \
echo "no" | avdmanager create avd -n test -k "system-images;android-29;default;arm64-v8a"; \
else \
yes | sdkmanager --sdk_root=$ANDROID_HOME "emulator" "platform-tools" "platforms;android-29" "system-images;android-29;default;x86_64" && \
echo "no" | avdmanager create avd -n test -k "system-images;android-29;default;x86_64"; \
fi
RUN yes | sdkmanager --sdk_root=$ANDROID_HOME "emulator" "platform-tools" "platforms;android-29" "system-images;android-29;default;x86_64"
# RUN if [ "$(uname -m)" = "aarch64" ]; then \
# unzip /root/emulator.zip -d $ANDROID_HOME && \
# mv /root/package.xml $ANDROID_HOME/emulator/package.xml && \
# rm /root/emulator.zip && \
# yes | sdkmanager --sdk_root=$ANDROID_HOME "platform-tools" "platforms;android-29" "system-images;android-29;default;arm64-v8a" && \
# echo "no" | avdmanager create avd -n test -k "system-images;android-29;default;arm64-v8a"; \
# else \
# yes | sdkmanager --sdk_root=$ANDROID_HOME "emulator" "platform-tools" "platforms;android-29" "system-images;android-29;default;x86_64" && \
# echo "no" | avdmanager create avd -n test -k "system-images;android-29;default;x86_64"; \
# fi

# Copy supervisor config
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Copy the rootAVD repository
COPY rootAVD /root/rootAVD
#COPY rootAVD /root/rootAVD

# Copy the first-boot script
COPY first-boot.sh /root/first-boot.sh
Expand All @@ -69,3 +79,4 @@ CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
# docker run -d --name dockerify-android --device /dev/kvm --privileged -p 5555:5555 dockerify-android
# docker run -d --name dockerify-android --device /dev/kvm --privileged -p 5555:5555 shmayro/dockerify-android
# docker exec -it dockerify-android tail -f /var/log/supervisor/emulator.out
# docker exec -it dockerify-android tail -f /var/log/supervisor/first-boot.out.log
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
dockerify-android:
image: dockerify-android
ports:
- "5555:5555"
volumes:
- ./data:/data
- ./extras:/extras
- ./patch/ramdisk.img:/opt/android-sdk/system-images/android-29/default/x86_64/ramdisk.img
- ./patch/ramdisk.img.backup:/opt/android-sdk/system-images/android-29/default/x86_64/ramdisk.img.backup
environment:
- DNS=1.1.1.1
- RAM_SIZE=8192
privileged: true
devices:
- /dev/kvm
container_name: dockerify-android
2 changes: 2 additions & 0 deletions extras/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignore added magisk modules
*.zip
89 changes: 68 additions & 21 deletions first-boot.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,84 @@
#!/bin/bash

# Wait until the emulator is listed in adb devices
RETRY_COUNT=0
MAX_RETRIES=10
SLEEP_INTERVAL=5
# apply settings
apply_settings() {
adb wait-for-device
# Waiting for the boot sequence to be completed.
COMPLETED=$(adb shell getprop sys.boot_completed | tr -d '\r')
while [ "$COMPLETED" != "1" ]; do
COMPLETED=$(adb shell getprop sys.boot_completed | tr -d '\r')
sleep 5
done
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0
adb shell settings put global stay_on_while_plugged_in 0
adb shell settings put system screen_off_timeout 15000
adb shell settings put global private_dns_mode hostname
adb shell settings put global private_dns_specifier dns2024.haroun.dev
adb shell svc wifi disable
}

# Detect ip and forward ADB ports from the container's network
# interface to localhost.
LOCAL_IP=$(ip addr list eth0 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1)
socat tcp-listen:"5555",bind="$LOCAL_IP",fork tcp:127.0.0.1:"5555" &

while ! adb devices | grep emulator-5554; do
if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
echo "Emulator did not become healthy after $MAX_RETRIES attempts. Exiting."
exit 1
fi
echo "Waiting for emulator to be healthy..."
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep $SLEEP_INTERVAL
done

echo "Emulator is healthy. Proceeding..."

# Check if the script has already run
if [ -f /root/.first-boot-done ]; then
exit 0
if [ -f /data/.first-boot-done ]; then
apply_settings
exit 0
fi

# Create a marker file to indicate the script has run
touch /root/.first-boot-done
sleep $MAX_RETRIES
echo "Init ADV ..."

echo "no" | avdmanager create avd -n test -k "system-images;android-29;default;x86_64"

echo "Preparation ..."

adb wait-for-device
adb root
adb shell avbctl disable-verification
adb disable-verity
adb reboot
adb wait-for-device
adb root
adb remount
for f in $(ls /extras/*); do
adb push $f /sdcard/Download/
done

echo "Installing GAPPS ..."

wget https://deac-fra.dl.sourceforge.net/project/opengapps/x86_64/20220503/open_gapps-x86_64-10.0-pico-20220503.zip?viasf=1 -O gapps-10.zip
unzip gapps-10.zip 'Core/*' -d gapps-10 && rm gapps-10.zip
rm gapps-10/Core/setup*
lzip -d gapps-10/Core/*.lz
for f in $(ls gapps-10/Core/*.tar); do
tar -x --strip-components 2 -f $f -C gapps-10
done

adb push gapps-10/etc /system
adb push gapps-10/framework /system
adb push gapps-10/app /system
adb push gapps-10/priv-app /system

echo "Root Script Starting..."

# Root the VM
cd /root/rootAVD
./rootAVD.sh system-images/android-29/default/x86_64/ramdisk.img
git clone https://gitlab.com/newbit/rootAVD.git
pushd rootAVD
sed -i 's/read -t 10 choice/choice=2/' rootAVD.sh
./rootAVD.sh system-images/android-29/default/x86_64/ramdisk.img
popd
echo "Root Done"
sleep 15
echo "Cleanup ..."
# done
rm -r gapps-10
rm -r rootAVD
apply_settings
touch /data/.first-boot-done
echo "Sucess !!"
Binary file added patch/ramdisk.img
Binary file not shown.
Binary file added patch/ramdisk.img.backup
Binary file not shown.
9 changes: 6 additions & 3 deletions supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ user=root
serverurl=unix:///var/run/supervisor.sock

[program:emulator]
command=/bin/bash -c "/opt/android-sdk/emulator/emulator -avd test -no-window -no-audio -no-boot-anim -skip-adb-auth -gpu swiftshader_indirect -no-snapshot -qemu -m ${RAM_SIZE:-2048}"
command=/bin/bash -c "/opt/android-sdk/emulator/emulator -avd test -writable-system -no-window -no-audio -no-boot-anim -skip-adb-auth -gpu swiftshader_indirect -no-snapshot -qemu -m ${RAM_SIZE:-4096}"
stdout_logfile=/var/log/supervisor/emulator.out.log
stderr_logfile=/var/log/supervisor/emulator.err.log
autorestart=true
priority=10
startretries=3
retry_interval=10
priority=20

[program:first-boot]
command=/root/first-boot.sh
stdout_logfile=/var/log/supervisor/first-boot.out.log
stderr_logfile=/var/log/supervisor/first-boot.err.log
autorestart=false
autorestart=false
priority=10

0 comments on commit 0028c67

Please sign in to comment.