PC Nerding

Android, Linux and Chroot

I felt the hacky desire to install a chrooted Linux under my Android phone, I’ll probably end up not using it, but I just like doing this king of stuff.
Note this guide requires a rooted phone, loop devices support and BusyBox (I have a Samsung Galaxy S3 with rooted stock JB 4.1.1). Using apps with BusyBox included (like SSHDroid or Better Terminal Emulator Pro) MAY save you the need of installing BusyBox on your phone. I said ‘may’ because I did not test them.
I followed this guide from Sergio Rubio to install my Arch Linux and there’s no reason to write again from scratch a good guide, so your first step is following his guide. What Mr. Rubio’s guide lacks (on purpose) was a faster way to actually mount/umount the chrooted system and get access to Arch shell, think my guide as “Part 2” for his one.

The author installed it in /sdcard/archlinux folder, my guide will assume you used the same directory.
TIP: I suggest you to increase the archlinux.img file size, 750MB may be too small for installing other packets (eg. Mono takes up to 200MB). I personally made it 1,5GB.

After installing Arch, I suggest rebooting your phone. Android will forget about the mounted partitions and we can start from scratch.

Mounting and umounting five partitions take time, a lot if it on a super small android touch keyboard, so we will create two scripts to make life easier.
Using a file manager on Android or connecting it to a pc, create a two files in your sdcard folder
The first is, write this stuff in
mount -v /sdcard/archlinux.img /sdcard/archlinux
mount -v -o bind /dev/ /sdcard/archlinux/dev/
mount -v -t proc proc /sdcard/archlinux/proc/
mount -v -t sysfs sysfs /sdcard/archlinux/sys/
mount -v -t devpts devpts /sdcard/archlinux/dev/pts/
cp /etc/resolv.conf /sdcard/archlinux/etc/resolv.conf

and the second is
umount -v /sdcard/archlinux/dev/pts/
umount -v /sdcard/archlinux/sys/
umount -v /sdcard/archlinux/proc/
umount -v /sdcard/archlinux/dev/
umount -v /sdcard/archlinux

Those two scripts will take care of mounting and umounting the partitions, just run them on your phone using Android Terminal Emulator.
su sh /sdcard/archlinux/
su sh /sdcard/archlinux/

After mounting you can get access to an Arch shell by running
su chroot /sdcard/archlinux/ /bin/bash

But unless you are using an hardware keyboard, those functions are still too long to type, we will shorten them using shell functions. In Android Terminal Emulator go to “Preferences” -> “Initial Command” and add those lines and restart the app.
function archmount() { su sh /sdcard/archlinux/ ; }
function archumount() { su su sh /sdcard/archlinux/ ; }
function archsh() { su chroot /sdcard/archlinux/ /bin/bash ; }

Now everytime you want to use Arch, mount partitions by running archmount, then type archsh to open an Arch shell. Now you have full access to a command line linux. When you are ready to quit type exit to return to Android terminal and type archumount to unmount Arch’s partitions.
If you want to use Arch later without having to remount it, just skip archumount and only run archsh whenever you need it.

A final note:
For some reason my Arch linux had an empty PATH variable, causing “command not found” error with almost every command ran in Arch’s shell. From Arch’s shell run export PATH=/bin:/usr/bin
to solve the issue. I recommend adding that same line it to .bashrc file, to access it run nano /.bashrc
After editing Press CTRL + O to save and CTRL + X to exit. Use Hacker’s Keyboard for your CTRL needs.

By Andrea Giorgio "Muu?" Cerioli

Italian dad, developer, designer, maker who loves everything in technology: AI, mechanics, electronics, IT, 3D printing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.