Termux Chroot Into Arch Linux

Table of Contents

Preface

Here's how to chroot into an Arch Linux Arm installation from Termux. This will require a rooted android and a little bit of time

Why should I use chroot/proot instead of Termux natively

Chrooting into an offical linux image will give you the same package support as any other distribution. You can install packages like qemu, not deal with broken termux dependencies, and properly follow guides on compiling software. You can also have different environments and run multiple official distributions of linux in one termux instance

The official Termux documentations suggests using PROOT to chroot into a rootfs without requiring root privledges. This is a userspace implentation of the chroot that hijacks some system calls to make the chroot work without root. It's quite a bit slower than native chroot so I prefer chroot itself.

Setting up Proot on android is a lot easier than using chroot. If you don't care about the performance loss, follow this guide: https://wiki.termux.com/wiki/PRoot

Setting up the Chroot Environment

Downloading the Arch RootFS

This is Arch Linux ARM (alarma). The arm image is required since we're on Android and want to avoid emulating other architectures to chroot

mkdir Alarm ; cd Alarm
wget http://os.archlinuxarm.org/os/ArchLinuxARM-aarch64-latest.tar.gz

Extracting the Tarball

Per the ALARM instruction for extracting the tarbar, we'll use BSD tar instead of the native tar. The documentation says to extract as the root user and not as the sudo.

pkg install bsdtar
sudo su
/data/data/com.termux/files/usr/bin/bsdtar -xpf ArchLinuxARM-aarch64-latest.tar.gz

When I extracted the file it had errors. I ignored these and it worked fine.

Chrooting

First mount the psuedo filesystems from your system onto the rootfs. Note that we're still in the Alaram folder with the extracted files.

  mount -t proc /proc proc/
  mount -t sysfs /sys sys/
  mount --rbind /dev dev/

To make sure name resolution works in chroot, lets copy the resolve.conf file from termux and into the rootfs folder

cp -f /data/data/com.termux/files/usr/etc/resolv.conf etc/resolv.conf
chmod 0644 etc/resolv.conf

Now lets chroot! After chrooting we source the /etc/profile file so the PATH variable gets loaded properly

  chroot . /bin/bash
  source /etc/profile

Now lets initialize the pacman keyring so we can install packages

  pacman-key --init
  pacman-key --populate archlinuxarm

Now lets stop pacman from checking if there's enough diskspace when upgrading packages. There's a weird bug where if you try to install or upgrade any program, pacman will report that the cache directory is full or it cant write to it. The second sed command will enable parallel downloads

  sed -i 's/CheckSpace/#CheckSpace/g' /etc/pacman.conf
  sed -i 's/#ParallelDownloads = 5/ParallelDownloads = 5/g' /etc/pacman.conf

Finally we can upgrade the system!

pacman -Syyu

Recommendation

To make chrooting faster, create this shell script in the extracted Alarm folder. Executing it will automatically do all the steps to chroot

#!/bin/bash
basedir=$(dirname $0)

if [ "$EUID" -ne 0 ]; then
    echo "Please run as root (NOT SUDO)"
    exit
fi

mount -t proc /proc proc/
mount -t sysfs /sys sys/
mount --rbind /dev dev/

chroot . /bin/bash