Android and Linux have an intricate, symbiotic relationship. Android relies on the Linux kernel for core functionality like memory management and device drivers. Meanwhile, innovative tools like UserLAnd harness Android‘s strengths to run desktop Linux distros like Ubuntu on mobile devices.

In this comprehensive, 4500+ word guide, we‘ll unpack the technical nitty gritty of getting Ubuntu up and running on Android. Follow along as we:

  • Dive into the Linux underpinnings powering Android
  • Breakdown how UserLAnd delivers Ubuntu on mobile
  • Benchmark performance considerations
  • And step through an example Ubuntu WebAssembly project

So let‘s get started!

Android‘s Linux Kernel Under the Hood

The key to understanding how tools like UserLAnd can essentially run dual operating systems on Android is first grasping Android‘s foundations.

Android is an open source mobile operating system based on a modified Version 3.x Linux kernel, leveraging the Linux kernel for the following lower level OS functionality:

Memory Management

The Linux kernel handles important memory management tasks like:

  • Page caching for improved performance
  • Swapping less frequently used application memory pages from RAM to disk
  • Allocating memory between kernel and user space processes

Device Drivers

The Linux kernel includes device drivers that allow Android to communicate with:

  • Hardware like the touchscreen digitizer
  • Storage devices
  • Network and wifi chipsets
  • Camera modules
  • Bluetooth/NFC adapters

And many other components.

This abstracts away vendor specific hardware details from the higher level Android OS stack.

Process Scheduling

Linux has advanced process schedulers like the Completely Fair Scheduler (CFS) built-in. This allows the Android OS to appropriately allocate CPU time for applications and system processes running on the device.

Power Management

Advanced Linux power management techniques help maximize battery life on mobile devices. This includes CPU scaling, screen brightness control and shutting down unnecessary hardware.

Security

Leveraging Linux capabilities and control groups, Android assigns apps only the minimum OS permissions required. Improving security by limiting potential damage from rogue apps.

This Linux kernel foundation provides Android with sophisticated low-level memory and processes management, hardware interfacing, power efficiency and security.

But Android then builds significantly on top of the Linux kernel, adding mobile focused userspace features like:

  • Touch friendly UI layer
  • State-of-the-art Graphics stacks optimised for mobile SoC GPUs
  • Specialist runtime environments for accelerated application code
  • Media capabilities like advanced cameras support
  • Cellular and telephony functionality
  • Location services
  • Android services and APIs for persistent storage, payments and more

So Android marries the strengths of Linux for robust, secure low-level OS functionality – with it‘s own uniquely mobile-centric capabilities.

This dichotomy allows tools like UserLAnd to then containerise and run an additional desktop Linux OS like Ubuntu directly leveraging the same Linux kernel – while keeping it isolated from the rest of Android using containerization.

Containerized Ubuntu OSes with UserLAnd

UserLAnd is an ingenious open source application that enables Linux distros (like Ubuntu) to be downloaded and installed into isolated "chroot jails". Allowing desktop Linux OSes to run seamlessly on existing Android phones and tablets.

UserLAnd Ubuntu on Android

These Linux chroot containers interact directly with the host Android device‘s Linux kernel. But are isolated from the rest of the system by acting as their own self-contained root filesystems, similar to how virtual machines are provided their own virtual hard disks.

This allows the Ubuntu user space (applications, libraries etc) to run unimpeded. Whilst ensuring apps and resources are completely walled off between Android and Ubuntu. Delivering robust separation of concerns and security.

Communication between the isolated Ubuntu environment and host Android system is still possible using specialized RPC methods if explicitly configured. We will cover setup options for storage and hardware access later like GPU acceleration support.

But first, how does the UserLAnd application actually work under the hood?

UserLAnd is composed of two key components:

1. UserLAnd Android Application

This frontfacing Android app provides the graphical user interface to:

  • Download Linux distro images
  • Configure chroot options
  • Launch distros
  • Manage existing container instances

It is this app you download from the Play Store to get started.

2. UserLAnd Server

Whilst the Android app is the user interface, the heavy lifting of managing Linux instances falls to a background system service called the UserLAnd Server.

The UserLAnd server runs with elevated privileges to handle creation and configuration of Linux chroot jails, as well as facilitating communication between the containers and host OS.

When the UserLAnd app wants to create a new Ubuntu instance for example, it communicates via Android‘s Binder IPC system to the UserLAnd server. The server then handles acquiring the Ubuntu image, configuring filesystem mappings and finally launching the chrooted environment.

This server architecture allows the UserLAnd app UI itself to run without extensive permissions. Enhancing security by limiting exposure of elevated privileges to the back-end service only.

With the app frontend simply acting as a console into the containerized Ubuntu instance.

Configuring Ubuntu Container Options

Whilst the default, out-of-the-box configuration works well for general experimenting with Ubuntu on Android. UserLAnd provides a number of configuration tweaks possible to the Linux chroot jails it creates to optimize the experience:

Storage Access

By default UserLAnd creates a contained /sdcard directory within the jail that maps through to the /storage partition available globally on Android for sharing files between apps.

For full host filesystem access, the UserLAnd app‘s preferences includes an option to directly mount the host /data and /storage partitions inside Ubuntu.

Granting Ubuntu‘s filesystem complete visibility of Android‘s directory structure for advanced usage.

Hardware Acceleration

For tasks like 3D programming or GPU intensive operations, UserLAnd supports passing through the hardware accelerated OpenGL drivers from the host Android OS into the Ubuntu jail.

This enables apps and tools leveraging OpenGL to tap into the full 3D capabilities of the mobile device‘s graphics chipset. Providing significant performance gains over stock software rendering.

Software leveraging OpenGL, like the Unity game engine for example, can see 10-100x FPS improvements from enabling this option.

SSH Access

As well as terminal access direct within the UserLAnd app, Ubuntu (and other Linux distro) instances can be exposed on the host network interfaces too.

Allowing you to SSH into the containers from other PCs using an IPv4 address assigned to each jail on the internal network.

Handy for remote device administration without needing to fiddle with USB tethers or cables.

Performance Considerations

A question that often arises when running desktop operating systems in lightweight virtualized environments is how much of a performance hit is incurred?

Some applications like video games require pixel perfect rendering timing and make use of bleeding edge graphics API extensions. Typically these struggle terribly in virtual machines.

However as outlined above, UserLAnd‘s Linux containerization approach is very lightweight – chrooted environments with their own dedicated root filesystems. Containers introduce negligible I/O and storage access latency unlike traditional VMs traversing simulated hardware.

Plus features like OpenGL hardware acceleration passthrough mitigate 3D graphics compatibility issues.

As a result, responsiveness within Ubuntu under UserLAnd is excellent for most applications like:

  • Web Browsing
  • Document Editing
  • Media Playback
  • 2D gaming
  • Programming IDEs
  • Terminal tools

Workloads leveraging GPUs do suffer under emulated graphics drivers though. So intensive 3D gaming or CAD is better handled by the host Android OS directly.

But tasks relying primarily on CPU power and system RAM cope very well inside UserLAnd Linux containers, benefiting from direct access to the full performance potential of the mobile hardware.

With an octa-core Android phone and additional storage, running productivity tools under Ubuntu via UserLAnd is a responsive, performant experience. Completely usable as a daily Linux driver on your mobile.

Project Example – WebAssembly Programming

To better understand a real world use case leveraging desktop Linux capabilities on Android, let‘s examine a WebAssembly project workflow inside UserLAnd‘s Ubuntu:

WebAssembly is a binary instruction format that can be executed in web browsers. It provides near native application performance leveraging compilation tools that output .wasm modules.

Combined with WebGL access to GPU hardware acceleration, WebAssembly unlocks demanding applications like games, 3D tools and video editing previously unachievable in JavaScript directly.

We‘ll compile a C project to WebAssembly then test and iterate on the output inside UserLAnd‘s Ubuntu on Android.

Step 1 – Install Toolchain

After setting up Ubuntu on Android using UserLAnd as covered previously, update the environment and install the WebAssembly build requirements:

apt update
apt install git build-essential cmake python3 nodejs npm

Plus the WebAssembly binary toolkit tools:

apt install binaryen  

Step 2 – Compile C Project

Write a simple C application, like the following square.c which exports a function that returns the square of a number:

#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
int square(int x) {
  return x * x;
}

With the toolchain installed, compile to WebAssembly using Emscripten:

emcc square.c -s EXTRA_EXPORTED_RUNTIME_METHODS=‘["ccall", "cwrap"]‘ -o square.wasm

This outputs a square.wasm WebAssembly module, with additional JavaScript bindings generator for integration.

Step 3 – WebAssembly Module Setup

Write a simple HTML page to leverage the WebAssembly module. Importing it and calling the exported square() function from JavaScript:

WebAssembly.instantiateStreaming(fetch(‘square.wasm‘), {})
  .then(results => {
    instance = results.instance;
    document.body.textContent = instance.exports.square(9);  
  });

With everything co-located in UserLAnd‘s Ubuntu chroot jail, we can run a simple local web server:

python3 -m http.server 8000  

Then on Android‘s host browser access the page on localhost:8000.

Which invokes the WebAssembly exported function to square 9, outputting 81.

Step 4 – Development Iterations

Here is where running Linux on mobile really shines for development workflows.

Make a change to the source C code under UserLAnd Ubuntu, recompile to WebAssembly and refresh the browser to instantly test the output.

No deploying to a hosting provider or external devices needed for rapid prototyping iterations leveraging WebAssembly or other programming languages.

Conclusion

Thecombination of Android and Linux provide the ideal foundations for delivering mobile access to the versatility and power of desktop Ubuntu.

Tools like UserLAnd seamlessly bridge the gap – unlocking use cases spanning:

  • General computing and productivity
  • Specialist developer tooling
  • IoT and electronics prototyping
  • Gaming and multimedia

Docker-like containerization ensures desktop Linux security by isolating instances in lightweight chroot jails. Whilst retaining the streamlined access Android provides to touchscreens, wifi, cameras, GPS sensors and everything smartphones excel at.

So whether your inner Linux enthusiast hankers for bash on the bus. Or advanced use cases demand desktop capabilities untethered. Ubuntu on Android delivers Linux, without limits or latency.

Similar Posts