For what?
At first glance, it may seem strange that someone is trying to run an operating system on a mobile device that, in principle, is not designed to work with a small screen and without a sufficiently precise manipulator (mouse) and keyboard. However, you should not make hasty conclusions. A Linux distribution can give a smartphone owner quite a lot of advantages, including a set of old proven tools such as command line utilities, advanced editors, FTP and SSH servers, networking tools and application development tools. By running Linux without a graphical shell on a smartphone with a hardware keyboard (Motorola Droid, for example), you can use all this quite comfortably on the go without having to leave Android itself. All tools are available at any time, and the smartphone continues to be a smartphone, allowing you to receive calls and listen to Internet radio.
The second argument for installing Linux on a smartphone is the ability to use it as a portable workstation that can be connected to any PC and immediately access the terminal using an SSH/Telnet client or a VNC/RDesktop client. This is by definition better than flash drives with Linux installed, since there is no need, firstly, to reboot the machine, and secondly, to drive data back and forth; the results of your work will be available immediately after you disconnect your smartphone from the computer.
Finally, Linux gives the greatest benefit on tablets, the screen of which allows you to work more or less tolerably in a graphical environment, and the ability to connect a mouse and keyboard via an OTG cable generally gives the chance to turn the tablet into a full-fledged workstation. At the same time, there is no particular difference between installing a Linux distribution on a tablet and a smartphone.
Cons of Postmarket OS
- The OS is not suitable for most users, as it is in the very early stages of development, there are bugs, reboots and other delights of earlier versions of the OS
- Complicated installation. All custom firmware for Android devices are distributed in the form of ready-made .zip packages for installation via recovery, and Postmarket still needs to be compiled through a terminal in Linux OS, which will alienate many users
- While there is no app store, all programs must be ported from another Linux distribution yourself
How?
It's really easy to port Linux to Android and the Linux kernel plays a major role here. Any Linux distribution is a set of applications and libraries that run on top of the Linux kernel, and since Android itself is based on an almost unchanged Linux kernel, these applications and libraries can be run inside the Android environment without any problems. You just need to find a distribution for which there is a port to the ARM platform (don’t forget that 99% of all Android devices run on ARM), install it using an ARM emulator on a virtual hard disk (that is, to a file), drop this file on SD card of the device, open a terminal, mount the image as a loopback device and chroot it inside. All! It's as simple as running an FTP server in a chrooted environment - a simple method that's been proven for decades.
The only stumbling block when you decide to run a Linux distribution inside Android is the graphical environment. While there are no difficulties with accessing the console thanks to the presence of a full-fledged terminal emulator, problems begin with graphical applications - there is no native X server for Android, and it is impossible to run a regular X server within the distribution itself due to fundamental differences in the graphics architecture green robot subsystems. Despite the fact that it uses a standard Linux Framebuffer on top of which you can run an X server, the exclusive right to use it initially belongs to higher-level Android libraries, so you are left with either loading a Linux distribution instead of Android (which is completely impractical) or coming up with workarounds .
Problem solving
If an error occurs during image creation:
Making new disk image (4095 MB) … fail dd: opening `/storage/emulated/0/linux.img': No such file or directory
Replace 0 in the path with legacy.
And if there is an error like this:
dd: opening `/storage/sdcard0/linux.img': Read-only file system
This means that your system uses namespaces for different users, that is, the flash drive is mounted for the user, but not for root. Therefore, open SuperSU and in the settings uncheck the Create named space division checkbox:
Checking the operation of the necessary modules
Keep in mind that support for loopback devices and ext2/ext3 file systems required to mount the image is not available in all Linux kernels installed on Android smartphones. You can check for support using the lsmod | grep -e loop -e ext2.
Enthusiasts got out of this situation by using a simple method of “remotely” connecting to the desktop using any VNC client available for Android. Inside the chroot environment, the Xvnc X server runs and all applications run under its control. The user just has to install the VNC client, enter the local address - and voila, a full-fledged desktop appears on the screen.
The only bottleneck when using Remote Desktop is performance. Even when working locally, VNC cannot provide the proper level of power, which would be enough for smooth scrolling or moving windows without lag. It has not yet been possible to solve this problem; projects to develop a native X server that would use the Android graphics subsystem are still very crude and cannot be used to run full-fledged graphical environments. However, no one forbids using them; for example, X Server from Darkside Technologies Pty Ltd (goo.gl/ap3uD) is quite suitable for running simple software.
Initially, Linux for Android existed only in the form of an image with an already installed system, as well as explanatory instructions on how to connect and use this image. Then scripts appeared that automated the process of connecting the image and starting Linux, but they also required some brain work. Finally, recently there have been installers available on Google Play (for example, goo.gl/RSA1j), which to some extent automate the process of launching the distribution, although, in essence, this is still the same installation guide, but interactive, with direct links to downloading images and scripts.
Alternative options
I already mentioned above that the Linux distribution can be loaded instead of Android, thanks to which it will be possible to use Framebuffer for direct access to the video adapter and significantly speed up the operation of the graphical interface. However, doing this on a smartphone is almost pointless - Linux is unsuitable as the main system on small screens, and it will be impossible to receive calls and use the Internet. But on a tablet Linux will look quite decent.
Typically, the so-called native version of the Linux distribution is installed on a device that initially runs Android as follows. An additional partition is created on the internal NAND drive of the tablet, onto which the Linux distribution is copied. The U-Boot boot loader (used by most tablets) is then configured to use this partition as the boot partition. As a result, the tablet will automatically boot into the Linux system after turning on the power.
To leave the possibility of booting Android, the U-Boot boot loader is reconfigured so that the partition with the Linux system is not the main one, but serves as a “recovery partition” (Recovery Mode), accessible by turning on the device with the volume key held down (the same one which is used to flash the device and perform various recovery operations). This way you can get a dual-boot device: Android by default and a Linux distribution when booted into recovery mode. Recovery Mode itself remains accessible only with the help of special tools.
If the NAND memory is not enough to accommodate a full-fledged Linux system, parts of it (usually the /usr partition) are moved to an image or partition on an SD card. By the way, the ext2 partition on the memory card can also be used to install Linux running in a chroot environment.
Installing a native Linux distribution is more difficult than installing one running in a chroot environment, but it's worth it if you have a tablet and an OTG cable with which you can connect a keyboard and mouse.
Key Features
The desktop version of the OS uses the Unity graphical interface, which is loved by non-professional Linux users. In the mobile phone, the manufacturer moved away slightly from it - only the side and top panels remained unchanged.
All basic actions in the new OS are performed using gestures. For example, if you drag between the edges of the screen, the multitasking menu will appear on it. For Android users, this solution will be somewhat inconvenient, but innovative and unusual.
In addition, Ubuntu has a Terminal on your smartphone! This is still an inferior version of it, and it is still far from the functionality of the desktop one. However, some functions are still there, and the techniques work.
Practical lesson
As I already said, only distributions ported to the ARM architecture are suitable for running Android. First of all, these are Ubuntu and Debian, and the first, for obvious reasons, is of much greater interest among robotics. You can also install Gentoo and several specialized distributions, such as Backtrack. Let's consider the most typical case, that is, installing Ubuntu according to the standard scheme, without using any automated installers or anything else.
BackTrack running on a tablet
Other articles in the issue:
Hacker #164. Iron colander
- Contents of the issue
- Subscription to "Hacker" -70%
First, we need a hard disk image with the distribution installed. You can create it yourself using the QEMU emulator, however, due to the fact that the installation procedure is absolutely standard and typical, I will not describe it, but will simply direct you to the address goo.gl/9nvBi. Here is an archive with an image on which Ubuntu 12.04 with the LXDE graphical environment is preinstalled (it would be unwise to run Unity/Gnome on a phone/tablet). The archive should be unpacked and the ubuntu.img file should be placed on the memory card.
Next, you need to mount the image and chroot it into the distribution environment. To do this, you need root rights, firmware with support for block loopback devices, and busybox installed (search the Market for “busybox installer”; CyanogenMod has it by default). Sequencing:
- Open the terminal emulator in Android (if not, you can install Terminal Emulator from the Market) .
Or we connect the smartphone/tablet to the computer and access the terminal using adb: $ cd path-to-Android-SDK/platform-tools $ sudo ./adb shell Don’t forget that the debugging mode in this case must be enabled: “Settings” -> For developers -> Debugging Android." - We get root rights: $ su
- We create a block loopback device, connect a disk image to it and mount it: # mknod /dev/block/loop255 b 7 255 # mount -o remount,rw / # mkdir /mnt/ubuntu # mount -o loop,noatime -t ext2 \ /sdcard/ubuntu.img /mnt/ubuntu
The contents of the image should appear in the /sdcard/ubuntu directory. Check that this is the case. - We connect all the virtual file systems necessary for the distribution to work: # mount -t proc proc /mnt/ubuntu/proc # mount -t sysfs sysfs /mnt/ubuntu/sys # mount -o bind /dev /mnt/ubuntu/dev
- We configure it so that you can get full access to the Network from the chroot environment: # sysctl -w net.ipv4.ip_forward=1 # echo “nameserver 8.8.8.8” > /mnt/ubuntu/etc/resolv.conf # echo “nameserver 8.8.4.4" >> /mnt/ubuntu/etc/resolv.conf # echo "127.0.0.1 localhost" > /mnt/ubuntu/etc/hosts
- Let's go to the chroot environment: # chroot /mnt/ubuntu
Actually, this is where the installation ends. Now you can launch console software, update the system, start network services and do almost everything that can be done with a regular desktop Linux system, not forgetting, of course, that some software that directly interacts with hardware and various specialized pseudo-devices will not work. Also, do not forget that virtual FS should be unmounted after completion of work.
Now we need to install and run the Xvnc X server, which exports display and input devices using the VNC protocol. TightVNCserver is already in the presented image and is even configured, but so that you can better understand the process and be able to solve any problems that arise, I will describe in detail the process of installing and launching it.
- Update and install TightVNCserver: # apt-get update # apt-get install tightvncserver
- Create a file /root/.vnc/xstartup and write the following into it: #!/bin/sh xrdb $HOME/.Xresources xsetroot -solid gray export XKL_XMODMAP_DISABLE=1 icewm & lxsession
The third command is needed here to fix problems that may arise due to the physical absence of a keyboard on the device. - We launch Xvnc using the vncserver wrapper with root rights: # export USER=root # vncserver -geometry 1024×800
As a result of executing the last command, a request for a password to access the VNC server will be displayed on the screen, it is better to specify something simple like “123 " You can set virtually any resolution, but it is better if it matches the physical resolution of the device screen. - Install the AndroidVNC application on your smartphone, launch it, specify the IP address and port 5901, and connect. The LXDE desktop should appear on your screen.
In order not to bother with manually entering all the commands, you can use the ubuntu.sh script, located here: goo.gl/xSpK4. Just put it and the ubuntu.img image in the ubuntu directory on the SD card and run the script with the command sh ubuntu.sh, and after 5-10 seconds connect to the desktop using AndroidVNC. Keep in mind that the script mounts the image to the /data/local/mnt directory.
AndroidVNC - connect to the desktop
Installing Gentoo on an ext2 partition
So, we installed Ubuntu using an image with a file system and tricks with a loopback device and a chroot environment. This turned out to be easy to do, and with the use of scripts it is generally very easy, but what if we go further and install a more hardcore distribution, and not using images, but on a dedicated ext2 partition on the memory card? This way we can solve the problem of some firmware and kernels without support for loopback devices and, moreover, we can enjoy a normal distribution installed according to all the rules.
The Ubuntu for Android project in action
Let's take Gentoo as an experimental system. To install it on an ext2 partition, we need a memory card with a capacity of at least 2 GB and a rooted smartphone with busybox installed. The sequence of actions is as follows.
- We make a backup of the data from the memory card and create an additional partition on it, with a capacity of at least two gigabytes . This can be done using any disk partitioning program, but keep in mind that if you want to continue to use the SD card for its intended purpose, then you should create a FAT32 partition at the beginning of the card, so that it becomes the first one, and an additional partition for installing the distribution should be the second one.
- Format the SD card partitions: $ sudo mkfs.vfat /dev/sdc1 $ sudo mkfs.ext2 /dev/sdc2
- We take the phone, go to “Settings -> About phone” and look at what processor is installed. Next, go to the page goo.gl/PRfux and download stage3 for the desired architecture, for example stage3 for ARM v7 is in the current-stage3-armv7a directory.
- We mount the ext2 partition of the memory card on the computer and unpack the contents of the resulting archive into it: $ sudo mount /dev/sdc2 /mnt $ sudo tar -xxpf stage3-*.bz2 -C /mnt
Immediately edit the configs and everything you need, to your taste , including editing /etc/resolv.conf according to the example from the previous section. - We launch the terminal emulator (or run the “adb shell”), mount everything we need and go to chroot (almost the same as in the case of Ubuntu): # mount -o remount,rw / # mkdir /mnt/gentoo # mount /dev/ block/mmcblk0p2 /mnt/gentoo # mount -t proc proc /mnt/ubuntu/proc # mount -t sysfs sysfs /mnt/ubuntu/sys # mount -o bind /dev /mnt/ubuntu/dev # sysctl -w net. ipv4.ip_forward=1 # chroot /mnt/gentoo
Access to the desktop is done in the same way as in Ubuntu, except that now you have to build a bunch of software directly on the phone :). However, you can set up an environment for cross-compilation on your computer, but this is a topic for a separate article.
Ubuntu on Galaxy Tab 10.1
Native installation
Having launched Ubuntu using a VNC server, you will notice the slowness of its operation, which is associated with the VNC protocol overhead for transmitting images “over the network”. To avoid this problem, you can install Ubuntu as the main system next to Android so that it can use the video adapter directly. Unfortunately, there is no universal way to do this. Each device is unique in its own way, including different partition tables of the NAND memory on which it is installed, various devices and drivers for their operation.
Native X server for Android
Fortunately, the process of installing the native version of the distribution is well described for many devices in Russian-language forums, so finding instructions will not be difficult. However, it is worth immediately paying attention to several features of this type of installation:
- Separate or main NAND partition. The Linux distribution can be installed either in a pre-created partition in NAND memory or in the main boot partition. In the first case, the firmware developer usually leaves the option of loading Android using a special script or by loading a Linux distribution in recovery mode; in the second, it will be installed instead of Android, and to return the ability to boot the robot, you will have to reflash the device again.
- Dual boot capability. If the Linux distribution is installed on a separate partition, the developer can leave the option to boot Android. However, you should immediately pay attention to how this download occurs: using recovery mode or a script launched from a regular computer. Still, the second method will be inconvenient on the road.
- Hardware support. The original Linux kernel of the Android firmware already includes all the necessary drivers that may be needed for the operation of a full-fledged Linux system, but not all Linux firmwares will start everything by itself. Problems often arise with the Wi-Fi adapter and the touch screen, which does not respond adequately to touch. Therefore, before installing the firmware, you should carefully read about possible complications.
In any case, be prepared for the fact that during the installation of the Linux distribution all your data will be destroyed. There is no way without this.
Starting Ubuntu using the startup script
Is it possible to install Linux on a tablet or phone instead of Android?
It is quite possible to install Linux instead of Android. This is usually done on a tablet PC, since their screen is large enough to display the command line or graphical interface in full.
There are practically no mobile devices (smartphones) that support the Linux operating system in stock condition. Today there is only one single smartphone model that runs on Ubuntu, an OS also based on Linux and Debian GNU. Finding and purchasing such a device is quite difficult, since it is not freely available in every country.
If you have a strong desire, then you can replace the standard pre-installed OS on an Android tablet, but you should understand that the computer platform will not fully work on portable devices.
Note! There is active development in this area, and many programmers are trying to create Linux-Android compatible firmware, but the reality is that you still have to try hard to run even a stripped-down version of Unix on your phone or tablet.
Some companies are already starting to produce phones with Linux pre-installed.
What's next
A Linux distribution installed next to the original Android system can become a very convenient working tool, but at the moment “Linux inside Android” is considered more of a toy and a way to show off to friends than a serious solution. I am sure that soon, when a full-fledged implementation of the Wayland graphical server appears for Android, the situation will begin to change and we will see distributions with an interface adapted for small screens, as well as full-fledged Linux applications distributed in the form of regular APK packages. Also, do not forget about the “Ubuntu for Android” project - within its framework, work is underway on an official port of Ubuntu for Android, which will allow you to use your smartphone as a portable system unit connected to any monitor.