Skip to content

Commit

Permalink
deblobbify initial commit
Browse files Browse the repository at this point in the history
Potential WIP/PoC solution to termux#60
  • Loading branch information
robertkirkman committed Jan 12, 2025
1 parent e3c861f commit b06df76
Show file tree
Hide file tree
Showing 80 changed files with 357 additions and 2,385 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
system
data
96 changes: 31 additions & 65 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,51 @@
# Bootstrap Termux environment.
FROM scratch AS bootstrap

ARG BOOTSTRAP_VERSION=2023.02.19-r1%2Bapt-android-7
ARG BOOTSTRAP_ARCH=i686
ARG SYSTEM_TYPE=x86

# Docker uses /bin/sh by default, but we don't have it currently.
# Docker uses /bin/sh by default, but we don't have it.
SHELL ["/system/bin/sh", "-c"]
ENV PATH /system/bin
ENV PATH=/system/bin

# Install bionic libc, toybox, mksh, dnsmasq, and iputils
COPY /system /system

# Copy libc, linker and few utilities.
COPY /system/$SYSTEM_TYPE /system
# Install the termux bootstrap
COPY --chown=1000 /data /data

# Copy entrypoint script.
COPY /entrypoint.sh /entrypoint.sh
COPY /entrypoint_root.sh /entrypoint_root.sh

# Extract bootstrap archive and create symlinks.
ADD https://github.com/termux/termux-packages/releases/download/bootstrap-$BOOTSTRAP_VERSION/bootstrap-$BOOTSTRAP_ARCH.zip /bootstrap.zip
RUN busybox mkdir -p /data/data/com.termux/files && \
cd /data/data/com.termux/files && \
busybox mkdir ../cache ./usr ./home && \
busybox unzip -d usr /bootstrap.zip && \
busybox rm /bootstrap.zip && \
cd ./usr && \
busybox cat SYMLINKS.txt | while read -r line; do \
dest=$(echo "$line" | busybox awk -F '←' '{ print $1 }'); \
link=$(echo "$line" | busybox awk -F '←' '{ print $2 }'); \
busybox ln -s "$dest" "$link"; \
done && \
busybox rm SYMLINKS.txt && \
busybox ln -s /data/data/com.termux/files/usr /usr && \
busybox ln -s /data/data/com.termux/files/usr/bin /bin && \
busybox ln -s /data/data/com.termux/files/usr/tmp /tmp

# Link some utilities to busybox.
# Some utilities in $PREFIX are actually a wrapper of the same binary
# from /system/bin. See termux-tools/build.sh#L29.
RUN for tool in df mount ping ping6 su top umount; do \
busybox ln -s /system/bin/busybox /system/bin/$tool; \
done

# Set ownership and file access modes:
# * User content is owned by 1000:1000.
# * Termux file modes are set only for user.
# * Rest is owned by root and has 755/644 modes.
RUN busybox chown -Rh 0:0 /system && \
busybox chown -Rh 1000:1000 /data/data/com.termux && \
busybox ln -s /system/etc/passwd /etc/passwd && \
busybox ln -s /system/etc/group /etc/group && \
busybox find /system -type d -exec busybox chmod 755 "{}" \; && \
busybox find /system -type f -executable -exec busybox chmod 755 "{}" \; && \
busybox find /system -type f ! -executable -exec busybox chmod 644 "{}" \; && \
busybox find /data -type d -exec busybox chmod 755 "{}" \; && \
busybox find /data/data/com.termux/files -type f -o -type d -exec busybox chmod g-rwx,o-rwx "{}" \; && \
cd /data/data/com.termux/files/usr && \
busybox find ./bin ./lib/apt ./libexec -type f -exec busybox chmod 700 "{}" \;

# Install updates and cleanup when not building for arm.
ENV PATH /data/data/com.termux/files/usr/bin
RUN if [ ${SYSTEM_TYPE} = 'arm' ]; then exit; else \
/system/bin/mksh -T /dev/ptmx -c "/system/bin/dnsmasq -u root -g root --pid-file /dnsmasq.pid" && sleep 1 && \
su - system -c "/data/data/com.termux/files/usr/bin/apt update" && \
su - system -c "/data/data/com.termux/files/usr/bin/apt upgrade -o Dpkg::Options::=--force-confnew -yq" && \
# Install updates and cleanup
# Start dnsmasq to resolve hostnames, and,
# for some reason the -c argument of toybox-su is not working,
# so this odd-looking script forces the update process
# to work using the -s argument of toybox-su instead, which is working.
RUN sh -T /dev/ptmx -c "/system/bin/dnsmasq -u root -g root --pid-file /dnsmasq.pid" && \
sleep 1 && \
echo '#!/system/bin/sh' > /update.sh && \
echo 'PATH=/data/data/com.termux/files/usr/bin' >> /update.sh && \
echo 'pkg update' >> /update.sh && \
echo 'apt-get upgrade -o Dpkg::Options::=--force-confnew -y' >> /update.sh && \
chmod +x /update.sh && \
su system -s /update.sh && \
rm -f /update.sh && \
rm -rf /data/data/com.termux/files/usr/var/lib/apt/* && \
rm -rf /data/data/com.termux/files/usr/var/log/apt/* && \
rm -rf /data/data/com.termux/cache/apt/* ;\
fi
rm -rf /data/data/com.termux/cache/apt/*

##############################################################################
# Create final image.
FROM scratch

ENV ANDROID_DATA /data
ENV ANDROID_ROOT /system
ENV HOME /data/data/com.termux/files/home
ENV LANG en_US.UTF-8
ENV PATH /data/data/com.termux/files/usr/bin
ENV PREFIX /data/data/com.termux/files/usr
ENV TMPDIR /data/data/com.termux/files/usr/tmp
ENV TZ UTC
ENV ANDROID_DATA=/data
ENV ANDROID_ROOT=/system
ENV HOME=/data/data/com.termux/files/home
ENV LANG=en_US.UTF-8
ENV PATH=/data/data/com.termux/files/usr/bin
ENV PREFIX=/data/data/com.termux/files/usr
ENV TMPDIR=/data/data/com.termux/files/usr/tmp
ENV TZ=UTC
ENV TERM=xterm

COPY --from=bootstrap / /

Expand Down
46 changes: 0 additions & 46 deletions build-all.sh

This file was deleted.

11 changes: 6 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/system/bin/sh

if [ "$(id -u)" = "0" ]; then
if [ -z "$(/system/bin/busybox pidof dnsmasq)" ]; then
/system/bin/mksh -T /dev/ptmx -c "/system/bin/dnsmasq -u root -g root --pid-file /dnsmasq.pid" >/dev/null 2>&1
if [ -z "$(/system/bin/pidof dnsmasq)" ]; then
/system/bin/sh -T /dev/ptmx -c "/system/bin/dnsmasq -u root -g root --pid-file /dnsmasq.pid" >/dev/null 2>&1
sleep 1
if [ -z "$(/system/bin/busybox pidof dnsmasq)" ]; then
if [ -z "$(/system/bin/pidof dnsmasq)" ]; then
echo "[!] Failed to start dnsmasq, host name resolution may fail." >&2
fi
fi
Expand All @@ -21,7 +21,7 @@ if [ $# -lt 1 ]; then
set -- /data/data/com.termux/files/usr/bin/login
fi

exec /system/bin/su -s /data/data/com.termux/files/usr/bin/env system -- \
exec su -s /data/data/com.termux/files/usr/bin/env system -- \
-i \
ANDROID_DATA="$ANDROID_DATA" \
ANDROID_ROOT="$ANDROID_ROOT" \
Expand All @@ -30,5 +30,6 @@ exec /system/bin/su -s /data/data/com.termux/files/usr/bin/env system -- \
PATH="$PATH" \
PREFIX="$PREFIX" \
TMPDIR="$TMPDIR" \
TZ=UTC \
TZ="$TZ" \
TERM="$TERM" \
"$@"
6 changes: 3 additions & 3 deletions entrypoint_root.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ if [ "$(id -u)" != "0" ]; then
exit 1
fi

if [ -z "$(/system/bin/busybox pidof dnsmasq)" ]; then
/system/bin/mksh -T /dev/ptmx -c "/system/bin/dnsmasq -u root -g root --pid-file /dnsmasq.pid" >/dev/null 2>&1
if [ -z "$(/system/bin/pidof dnsmasq)" ]; then
/system/bin/sh -T /dev/ptmx -c "/system/bin/dnsmasq -u root -g root --pid-file /dnsmasq.pid" >/dev/null 2>&1
sleep 1
if [ -z "$(/system/bin/busybox pidof dnsmasq)" ]; then
if [ -z "$(/system/bin/pidof dnsmasq)" ]; then
echo "[!] Failed to start dnsmasq, host name resolution may fail." >&2
fi
fi
Expand Down
Loading

0 comments on commit b06df76

Please sign in to comment.