Running Arch Linux in Chroot on Android ARM

Hey guys!
In this tutorial I'll show you how to run Arch Linux (ARM edition) within Android in a chroot environment.

Undue Credit

This post was a base for mine. Do check it out! :)

 

Things you'll need

  • (Recommended) A Linux distribution on your PC (If you don't have any PC at hand, continue with all steps using a terminal emulator)
  • ADB (Android Debugging Bridge)
  • A rooted Android phone
  • The Arch Linux ARM rootfs tar.gz for specific phone architecture (ARM v5, v6 or v7 or AArch64. Download the multi-arch version for your specific architecture) Get Here
  • Any Terminal Emulator on Android (Suggestion: JuiceSSH)
  • a 1GB or greater microSD card (recommended atleast 2GB for the entire distro)
  • A basic knowledge of using ADB
  • An interest for commandline-fu

 

Reasons for choosing Arch Linux

Arch Linux (due to its rolling release) can support the quite old 3.0.101 kernel running on my device. Other distros seem to require a higher minimum kernel version.
Also, arch is a really minimal distro and so, it's a perfect fit for Android devices where storage space and RAM is frequently constrained (I have mine running on my 512MB RAM device quite smoothly)

 

Notations

I'll use a few notations for clarity:
$ - PC normal user terminal
# - Device root terminal 
/emmc/ - External SDCard
/sdcard/ - Internal Storage
arch.tar.gz - Arch Linux rootfs in tar.gz

 

Method

  1. Download the Arch rootfs and transfer to your device
  2. Create a disk image file for the distro.
    1. If you're on your PC, enter the following in a terminal:
           $ truncate --size 1024M arch.img 
      The above creates a 1GB (1 * 1024MB) disk image. If you want more, increase the 1024 part (SIZE_IN_GB * 1024)
    2. If on the device, use dd (EXERCISE EXTREME CAUTION WHEN USING DD!)
           # dd if=/dev/zero of=arch.img bs=1048576 count=1024
      Again, the above creates a 1GB file (For those who are wondering what "bs" is, it's block size and in the above command, it is set to 1MB [1024 * 1KB]). If you need a bigger image, change the "count=..." and set it equal to (SIZE_IN_GB * 1024)
  3. Make an ext4 file system on the disk image by entering:
         $ mkfs.ext4 arch.img
  4. Transfer the image file to the device (Use the external storage if possible)
  5. Open a terminal, connect to the device using ADB and open a root shell (or if you're already using a terminal emulator, continue). Then, mount the disk image by typing:
         # mkdir /data/chroot
         # losetup /dev/block/loop7 /emmc/arch.img
         # mount -t ext4 /dev/block/loop7 /data/chroot
  6. Now, extract the Arch Linux rootfs by entering:
         # cd /data/chroot/
         # tar xf /emmc/arch.tar.gz
  7. Next, set it up by issuing:
         # rm /data/chroot/etc/resolv.conf
         # echo "nameserver 8.8.8.8" > /data/chroot/etc/resolv.conf
         # echo "export HOME=/root" >> /data/chroot/etc/profile
         # echo "unset LD_PRELOAD" >> /data/chroot/etc/profile
         # echo "cd /root" >> /data/chroot/etc/profile
  8. Finally, enter the chroot by these final commands:
         # mount -o bind /dev/ /data/chroot/dev/
         # mount -t proc proc /data/chroot/proc/
         # mount -t sysfs sysfs /data/chroot/sys/
         # mount -t tmpfs tmpfs /data/chroot/tmp/
         # mount -t devpts devpts /data/chroot/dev/pts/
         # chroot /data/chroot/ /bin/bash -l
  9. If successful, the terminal should read [root@localhost ~]
Congratulations, Your Arch Linux chroot is up and running!

 

After Setup

  • Update the entire distro by issuing "pacman -Syu". Wait for it to finish
  • Remove the linux kernel and firmware files from the chroot by issuing "pacman -Rs linux-<architecture>" where <architecture> is one among armv5, armv6, armv7 and aarch64. This can save a considerable 200MB of space.
  • If you're planning to use the chroot for penetration testing, install the BlackArch Repository for an ocean of tools.
  • Here's a tiny script to automate the mounting and starting process:
    --------------START OF SCRIPT---------------------- 

    #!/bin/sh
    losetup /dev/block/loop7 /emmc/arch.img
    mount -t ext4 /dev/block/loop7 /data/chroot/
    mount -o bind /dev/ /data/chroot/dev/
    mount -t proc proc /data/chroot/proc/
    mount -t sysfs sysfs /data/chroot/sys/
    mount -t tmpfs tmpfs /data/chroot/tmp/
    chroot /data/chroot/ /bin/bash -l
    ---------------------END OF SCRIPT---------------------- 
  • You can install a VNC server to enable a GUI (Package: tigervnc)
As always, please do comment, follow and share my posts! :)
Cheers!
-Technohacker

Comments

  1. Can truly relate and retain this outstanding post. Very well written. chroot linux

    ReplyDelete
  2. Thank you very much for this. There is only a small mistake: the command "echo "cd /root" > /data/chroot/etc/profile" replaces all the content of profile, the right command is: "echo "cd /root" >> /data/chroot/etc/profile".

    ReplyDelete
    Replies
    1. Ah good point! I'll update my post to match :D

      Delete

Post a Comment

Popular Posts