Run linux code on android

ODROID

[GUIDE] Running Linux programs under Android (linux chroot)

memeka Posts: 4420 Joined: Mon May 20, 2013 10:22 am languages_spoken: english ODROIDs: XU rev2 + eMMC + UART
U3 + eMMC + IO Shield + UART Has thanked: 2 times Been thanked: 61 times Contact:

[GUIDE] Running Linux programs under Android (linux chroot)

Post by memeka » Thu Aug 08, 2013 9:02 am

root@android:/ # uname -a Linux localhost 3.0.51-g4732a09 #3 SMP PREEMPT Sun Aug 4 07:26:36 CST 2013 armv7l GNU/Linux 

Why then it cannot run Linux software? Well, the answer is simple: while the kernel is the same, there are different libraries in Android. Is like Darwin and Mac OS X.
But then, if you would have the linux libraries on Android, you would be able to run linux programs. Of course, every system expects to find libraries in certain path locations. So it’s not a good idea to mix them.
Enter the world of chroot. When running chroot in a folder, for that session of your shell only, the folder becomes «/», the root of your system. So then you can have your linux libraries in /whatever/path/you/want/lib for example, then tun chroot on /whatever/path/you/want, and then you have a shell with the lib folder in /lib.

This means that linux on android has no overhead, it’s not running in any kind of «virtual machine», or needs any additional things to load. All is native.

Of course, things are not as simple as that When you chroot, you get into that «system», but you are not «booting» into it. So all initializations done during boot will be missing. This include some required virtual filesystems without the system would not be able to function, such as /dev and /proc, or the daemons that start automatically. Fortunately, these are not major issue. Since Android is based in Linux, it makes use of the same virtual filesystems, so we can do away just by linking the android mount points with the linux counterparts. Also, when doing the first chroot, we make sure to start the daemons.

The following is a step-by-step guide how you can do these things. The end result is that you will be able to get into a linux userspace from your android system, and run any linux program, console or GUI based. Of course, X included.

1. You need the linux libraries.
Although I guess you could mix them, it’s safer (especially due to Android making some its partitions read-only) to have them completely separate. The easiest way is to have a linux image, containing a complete linux system, which you will then mount and use it as the base of chroot. You can make your own image if you wish, but I am linking here the LinuxOnAndroid project, from where you can download images for several distributions.
It’s worth mentioning that there is a LinuxOnAndroid app in the PlayStore, which will automate things for you. My tutorial shows how things work under the hood. I am using myself the debian core image, because it has no X (I like to think as Android being my X ), and because upstart, the launcher used in ubuntu to start daemons, has some issues in chroot.
So, let’s say that we have the debian image saved on the FAT32 partition of our Android system:

root@android:/sdcard/debian # ls debian-v4-core.zip debian.img root@android:/sdcard/debian # 
root@android:/sdcard/debian # busybox mknod /dev/block/loop127 b 7 127 root@android:/sdcard/debian # busybox losetup /dev/block/loop127 debian.img root@android:/sdcard/debian # mkdir /data/local/debian root@android:/sdcard/debian # busybox mount -t ext2 /dev/block/loop127 /data/local/debian/ 
root@android:/sdcard/debian # ls -la /data/local/debian drwx------ root root 2012-03-26 20:05 .Trash-0 -rw------- root root 35 2013-08-07 08:17 .bash_history drwxr-xr-x root root 2012-03-29 09:13 bin drwxr-xr-x root root 2008-08-05 16:38 boot drwxr-xr-x root root 2012-03-29 09:19 dev drwxr-xr-x root root 2012-06-07 14:19 etc drwxr-xr-x root root 2012-06-07 14:09 external_sd drwxr-xr-x root root 2009-01-10 18:58 home drwxr-xr-x root root 2012-03-29 09:13 lib drwx------ root root 2008-11-08 03:56 lost+found drwxr-xr-x root root 2008-11-08 04:50 media drwxr-xr-x root root 2008-08-05 16:38 mnt drwxr-xr-x root root 2008-11-08 04:50 opt drwxr-xr-x root root 2008-08-05 16:38 proc drwxr-xr-x root root 2012-06-30 14:45 root drwxr-xr-x root root 2012-03-29 09:13 sbin drwxr-xr-x root root 2012-06-07 14:09 sdcard drwxr-xr-x root root 2008-09-16 07:48 selinux drwxr-xr-x root root 2008-11-08 04:50 srv drwxr-xr-x root root 2008-08-12 14:26 sys drwxrwxrwt root root 2012-06-07 14:19 tmp drwxr-xr-x root root 2009-01-08 21:35 usr drwxr-xr-x root root 2008-11-08 04:50 var 
root@android:/sdcard/debian # busybox mount -t devpts devpts /data/local/debian/dev/pts root@android:/sdcard/debian # busybox mount -t proc proc /data/local/debian/proc root@android:/sdcard/debian # busybox mount -t sysfs sysfs /data/local/debian/sys 

You can mount other stuff from android to linux that might be useful for you, such as the FAT32 partition (that contains, of course, the image itself )

root@android:/sdcard/debian # mkdir /data/local/debian/media/sdcard root@android:/sdcard/debian # busybox mount -o bind /sdcard /data/local/debian/media/sdcard 
root@android:/sdcard/debian # busybox sysctl -w net.ipv4.ip_forward=1 
root@android:/sdcard/debian # busybox chroot /data/local/debian/ /bin/bash root@localhost:/# root@localhost:/root# cat /etc/issue Debian GNU/Linux 5.0 \n \l 

6. Setting up stuff that should be set up during boot

Читайте также:  Linux mint install firmware

If you are not able to run anything right now (not even the cat from my previous code), don’t be alarmed! This is because your linux system is not set up, including the PATH environment variable.
This is why it’s useful to have a script that sets up your system, simulating some of the stuff done during boot, such as setting environment variables and starting up daemons.
Linux on android comes with such a script, called init.sh, in the /root folder. You can start that straight from chroot like this:

root@android:/sdcard/debian # busybox chroot /data/local/debian/ /bin/bash /root/init.sh root@localhost:/# 

But I will include here fore completeness what’s basically in that script.

These should be run every time you chroot in your linux system:

root@android:/sdcard/debian # busybox chroot /data/local/debian/ /bin/bash root@localhost:/# root@localhost:/# export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin root@localhost:/# export TERM=linux root@localhost:/# export HOME=/root 

The following should be run just the first time you chroot in your linux system. It’s just stuff like setting up a non-root user account and setting some permissions:

root@localhost:/# chmod a+rw /dev/null root@localhost:/# chmod a+rw /dev/ptmx root@localhost:/# chmod 1777 /tmp root@localhost:/# chmod 1777 /dev/shm root@localhost:/# echo "shm /dev/shm tmpfs nodev,nosuid,noexec 0 0" >> /etc/fstab root@localhost:/# useradd marianmi root@localhost:/# mkdir /home/marianmi root@localhost:/# cp /etc/skel/.bashrc /home/marianmi/ root@localhost:/# cp /etc/skel/.profile /home/marianmi/ root@localhost:/# cp /etc/skel/.bash_logout /home/marianmi/ root@localhost:/# chown -R marianmi.marianmi /home/marianmi root@localhost:/# usermod -a -G android_bt,android_bt-net,android_inet,android_net-raw marianmi 
root@localhost:/# cd /media/sdcard root@localhost:/media/sdcard# groupadd -g 1015 sdcard-rw root@localhost:/media/sdcard# usermod -a -G sdcard-rw debian root@localhost:/media/sdcard# ls -la total 108 d---rwxr-x 10 marianmi sdcard-rw 8192 Aug 7 23:49 . drwxr-xr-x 3 root root 4096 Aug 7 23:18 .. d---rwxr-x 2 marianmi sdcard-rw 8192 Aug 4 07:58 .android_secure d---rwxr-x 3 marianmi sdcard-rw 8192 Aug 4 05:26 Android d---rwxr-x 2 marianmi sdcard-rw 8192 Aug 4 06:03 LOST.DIR ----rwxr-x 1 marianmi sdcard-rw 431 Aug 3 19:49 boot.scr ----rwxr-x 1 marianmi sdcard-rw 431 Jul 3 02:43 boot.scr.1080 ----rwxr-x 1 marianmi sdcard-rw 429 Feb 7 09:14 boot.scr.720 d---rwxr-x 2 marianmi sdcard-rw 8192 Aug 7 08:01 debian d---rwxr-x 3 marianmi sdcard-rw 8192 Aug 4 05:18 updater 
root@localhost:/# dpkg-divert --local --rename --add /sbin/initctl > /dev/null 2>&1 root@localhost:/# ln -s /bin/true /sbin/initctl > /dev/null 2>&1 

Lastly, again, every time you chroot to linux, start your daemons. That’s why I find it easier to replace init.sh with my own code, containing the above + the daemons I use. Every time I install a new daemon, I add it to init.sh.

/etc/init.d/ssh start /etc/init.d/apache2 start /etc/init.d/mysql start 

Lastly, my very own addition: df will not work since there is no mtab file containing the currently mounted filesystems. So I added this little fix to init.sh:

grep -Ev "rootfs|tmpfs|cgroup|mmcblk|usbfs|asec|storage" /proc/mounts | sort -r | uniq > /etc/mtab 

The script contains some init stuff for VNC if you want X, which I will not cover here.
Thanks to android having a linux kernel, thanks to many people that put on some effort to create images, write scripts to automate things, and fix little issues, you are no able to run any linux program under android.

Читайте также:  Ruby linux or windows

memeka Posts: 4420 Joined: Mon May 20, 2013 10:22 am languages_spoken: english ODROIDs: XU rev2 + eMMC + UART
U3 + eMMC + IO Shield + UART Has thanked: 2 times Been thanked: 61 times Contact:

Источник

How to run Linux commands in my Android app?

I want to run a Linux command in my Android app. I mean I’m an Android programmer and I want to commit a command in my codes in Java which I’m writing in Android Studio. For example this is what I want in my codes in Java in Android Studio:

ImaginarylinuxClass.theFunctionWhichRunsTheCommands("sudo apt-get update && git clone && etc"); 

I hope I was able to say what I mean. I tried to Google about it but it was all apps which they are Linux emulators like termux but it’s not what I want actually.

I am not sure on what you mean, where do you want the code to be executed? Are you trying to execute the command on a remote machine? Are you trying to execute the command in the android shell? what do you want to happen when you execute ImaginarylinuxClass.theFunctionWhichRunsTheCommands(«sudo apt-get update»); ?

What does additional installed apps do when I run «sudo apt-get update» in them ? i wnat to do the same in my app

I know they make a linux emulator and run commands as they make folders like dev , env etc. I just want to do the same in my app . I hope there should be an api for this

if you are thinking of running apt-get update for android apps, no. android, is not a GNU/Linux system (it has a modified version of linux kernel), and it doesn’t have aptitude or any such package manager to run from shell. this is the reason you found Termux during your research. it was created by the people who loved the idea of having apt or yum or yay in their palm top and to have strong operating system to fiddle with.

Читайте также:  Удаленный запуск компьютера linux

Источник

Compile, Install, Run Linux Apps on Android

Geeknizer

The advantage of using a POSIX based mobile OS is that you can run and install any Linux applications on your mobile (smartphone) with ease. And thanks to Open Source, its even easier to Compile, Install & Run Linux Applications on Android.

To get basic Linux apps running on Android, you need BusyBox. To give you some background, BusyBox is a software application that provides many standard Unix tools, much like the larger (but more capable) GNU Core Utilities. BusyBox is designed to be a small executable for use with the Linux kernel, which makes it ideal for use with embedded devices. It has been self-dubbed “The Swiss Army Knife of Embedded Linux”.

Using this guide, you’ll be able to:

How to Compile, Run Linus Apps on Android

Step 1. Install BusyBox from Play Store (requires root). If you don’t have Root access, you can follow steps mentioned in the video which involves adb push for busybox binary to /data/ and setting permissions.

BusyBox would allow you to install various Linux apps on Android coz BusyBox is bundled with all the runtime dependecies.

Step 2. To make your environment even more capable, lets go ahead and install BostBrew Basil from Play store.

BostBrew Basil bootstraps the base system and do some basic package management by using uses Dpkg and Apt instead of Opkg. This will let you install various linux packages, this is where BostBrew shines.

Step 3. Install Linux apps using APT Package manager

To install apps using apt package manager, all you need to do is:

su
bostbrew
apt-get install gcc g++

This will install gcc, g++ compilers and you can specify any other package name and ARM version should be automatically installed to your android.

Step 4. Compiling C, C++ source code on Android

Compile any source file using g++ and run it:

g++ ./sourceCode.cpp
./a.out

That’s it. You’ve successfully compiled and run your own C code.

You can watch the video below, it essentially covers the same steps in an easy to follow tutorial:

We write latest and greatest in Tech Guides, Apple, iPhone, Tablets, Android, Open Source, Latest in Tech, subscribe to us @geeknizer OR on Facebook Fanpage, Google+.

Источник

Оцените статью
Adblock
detector