# Setting up a Bionic/Linux (Android) chroot environment on FreeBSD.



## Krush206 (Nov 2, 2021)

_Why have you done this?_
Mainly due curiosity and research purposes. Android makes use of the Linux kernel + Bionic libc (userland), so I thought an Android chroot environment would work, just like any GNU/Linux chroot environment does.
It's important to note, however, essential Android apps [such as surfaceflinger (Android windowing system) and dalvikvm (Android runtime)] do not work (possibly due missing Linux kernel functionalities). All CLI apps are working, though.

_What is Bionic?_
Bionic is Android userland's core. Practically speaking, every Android app (GUI and CLI) one runs is handled by Bionic. Android programs run on top of Bionic, which runs on top of the Linux kernel.
Bionic is a C library (libc) implementation for the Linux kernel, developed by Google.
During early Android development, Google wanted to stay away from GPL. It was then decided to make their own C library implementation, derived from *BSD, for use in low-cost hardware, with limited resources.

_Many Android CLI apps are present in GNU. What is the point in using Bionic instead of GNU?_
Licensing: Bionic is *BSD-licensed software.
Size: Bionic is half the size of glibc (the GNU version of libc).
Speed: Bionic is small and simple, having fast code paths, including a very fast, small custom pthread implementation.

Now that you know of Bionic, here's the guide to get an Android chroot environment on FreeBSD:
(NOTE: Only tested with Android-*x86* 4.2. The latest release, Android-*x86_64* 9, does not work!)

1) Download an Android-x86 image of your choice, either from Google Code or the official website.

2) Mount the downloaded Android-x86 image with mdconfig:

```
# mkdir /mnt/img /mnt/img2 /mnt/img3
# mdconfig -f <path/to/android.iso>
# mount_cd9660 /dev/mdX /mnt/img
```

3) Install squashfs-tools and extract Android-x86's contents (*ramdisk.img*, *initrd.img* and *system.sfs*/*system.img*):

```
# kldload fusefs ; squashfuse /mnt/img/system.sfs /mnt/img2
# mdconfig -o ro -f /mnt/img2/system.img
# mount -o ro -t ext2fs /dev/md1 /mnt/img3
# mkdir /compat ; tar -C / -cf - /mnt/img3 | tar -C /compat -xvf -
# ( cd /compat ; mv mnt android ; mv android/img3 android/system )
# tar -C /compat/android -xvf /mnt/img/ramdisk.img
# tar -C /compat/android -xvf /mnt/img/initrd.img
```

4) (Optional.) To make use of Android CLI apps (chroot-lessly), link the *system* directory to the root file system:

```
# ln -s compat/android/system /system
$ set path=(/system/bin /system/xbin $path)
```

That's it! You're all set!


----------



## Lamia (Feb 9, 2022)

Good Job! You are reminding me of the days of building FirefoxOS, using N900 (best phone ever) and  Android on ARM.


----------

