[Android General] mirfatif:
Why Linux-on-Android? And Possible Methods of Android-Linux Dual Booting

. . . continued . . .

When there are so much differences,

WHY WE NEED TO INSTALL LINUX ON ANDROID DEVICE?​

Because Android is non-POSIX Linux. Though kernels are very same (only a few syscalls differ), C libraries are quite different making it impossible to run every Linux program on Android natively.

Since Android has its origins from Linux, it can be considered a highly customized embedded Linux (distro?); mainly for ARM processors and Qualcomm SoC's. Android, unlike PC's, exposes users to a limited Linux and we can't utilize powers of Linux. This limited nature is justified somewhat considering the less powerful hardware resources and "mobile" nature of the devices, Android is designed for. Restricted access to core OS is necessary for security and privacy of users who want a ready made perfectly working out-of-the-box type solution. For those who want some more from Android device, the first thing to do after buying a device is unlocking bootloader and "root" the device. This makes possible at least the user access to shell (CLI), which is the biggest common feature Android shares with Linux. Then to compensate the absence of glibc, busybox type handy solution is availed. But it's designed with a small number of commandline utilities and that too with limited functionality.

You don't have access to repositories of thousands of free packages available for a wide variety of tasks, almost anything you can think of.

While on Android, even you don't have an option to encrypt your SD card.

Or you need to encrypt a folder containing your private pics for online backup?

Or what if you accidentally deleted some important stuff on internal memory and want to recover that?

Or if you want to make your device a tiny server for easy access from other phone or PC?

Android provides very limited data backup options as compared to Linux.

Compile programs in full environment

You are totally dependent on apps and their developers to achieve all this, what functionality they offer and what they do to you and to your private data. Thus one may wish to install Linux on an Android device.

And after all it's fun to have a phone with many possibilities that could be played with. Be a power user!

CHROOT, NAMESPACES, CONTAINERS and VM​

Due to (almost) same kernel, it's the chroot operation of Unix-like OS's which makes it possible to provide an isolated environment for new apps or a new OS inside a running OS without needing a dedicated rootfs. You can also use hardware emulator like QEMU to run cross-architecture Linux OS. Even Windows can be run in an emulator on Android device if your hardware has capability such as on Limbo PC Emulator. But it will be unbearably slow. Let's make a comparison of methods used to isolate OS environments on a single machine.

CHROOT

Chroot just changes the root of single or more processes, not booting a new OS, thus changing the filesystem hierarchy and binary environment for applications.

OS's still can view each others' processes, mounts etc. because every process is running on same OS natively.

Can't start init process and spawn all services and daemons like syslogd, cron etc. from it. Need to just start the required processes manually.

NAMESPACES, CGROUPS AND CONTAINERS

Containers are kernel feature which additionally make use of namespaces and cgroups to isolate the guest OS from host.

Namespaces limit what resources a process can see and use, while cgroups limit how much a process can use a resource.

Network namespaces provide isolated view of iface, firewall rules and sockets. It is necessary so that daemons like sshd can be started independently.

Mount namespace hides Linux mounts from host OS (Android in our case) or even from spawned processes. As well as it gives flexibility to unmount / move / remount all Android shared filesystems except rootfs (pivot_root fails on Android :(

User namespace maps unused UID/GID's on host (like higher number 10000) to the UID's/GID's of the guest thus avoiding conflict of identical ID's and letting ID's work independently. It also restricts unwanted access from guest to host OS.

UTS NS allows setting hostname independent of Android.

PID NS will show only Linux processes, not those of Android.

cgroups (/sys/fs/cgroup) and cgroup namespaces (/proc/[pid]/cgroup) make it possible to group processes with a defined limit on allocated system resources (RAM, CPU, I/O i.e. storage devices, network, devices allocation).

But both OS share same kernel, drivers and modules, using process isolation methods provided by Linux kernel.

Can't boot different OS's with incompatible kernels.

All hardware related stuff is handled by host kernel.

RAM allocation to running processes and CPU usage can be managed more efficiently by a single kernel with negligible overhead because namespaces and cgroups are already being used by host OS to manage it's own processes.

VIRTUAL MACHINES

Hypervisors or virtual machines (kvm, VirtualBox etc.) emulate the hardware and provide a complete machine environemnt to use an independent kernel (and even a bootloader and BIOS as well), booting any OS which the virtualizer is compatible with.

Guest kernel thinks it's dealing with the real hardware, not emulated.

So there is a thick layer of software stack between OS and actual hardware, making it very slower.

Hardware resources are divided in between kernels and are allocated / managed independently.

Native or bare-metal hypervisors like Xen and Hyper-V give better performance as they talk directly to hardware, not through an OS.

systemd-nspawn is an isolation method provided by systemd and is very popular in containerization these days. It's something in between chroot and containers (LXC, docker etc.)

PERFORMANCE & SECURITY

Performance: chroot > containers > virtualization

Isolation: chroot < containers < virtualization

In a nutshell, chroot needs more human effort to setup because it varies from device to device but it's least overhead on system resources.

LINUX CONTAINERS, CHROOT & PIVOT_ROOT ON ANDROID

We need a modified Android kernel with support for containers, pivot_root and all namespaces etc. Android by default supports entering namespaces using init .rc files on boot before starting a process.

In order to hide mounts from host, we can make use of mount namespace with private or slave propagation.

In order to hide future mounts from guest, we can make use of mount namespace with private propagation.

Already mounted mounts can be hidden from guest by using "unshare + pivot_root + chroot" (or may be "unshare + mount --move + chroot" as referred here, but so far can't figure out how). But pivot_root doesn't work on Android because of special kernel loading process. Unlike Linux, Android rootfs mounted from initramfs is retained and pivot_root is not used in booting process to switch rootfs to real hard drive partition.

So, we have to rely on namespaces + chroot combination. "unshare + chroot" both work on Android devices, though busybox 'unshare -m' doesn't work.

Only MNT and NET namespaces work on Android devices by default. PID, UTS and USER namespaces can be enabled by rebuilding kernel.

Moving towards practical details:

[HOW TO] INSTALL LINUX ON ANDROID DEVICE (LINK TO COME)

That's all from my side. You can correct me if I'm wrong or complete me if I missed something worth mentioning.