Archive

Posts Tagged ‘console’

Freeing up resources for use by GNU/Linux on your android device

December 7, 2014 1 comment

I had an old samsung galaxy 3(GT-15801) which was lying around unused with a broken screen and decided why not install GNU/Linux on it. If raspberry pi can run raspbian well, then this phone could try to do as well. Have a look at my earlier post here on how I installed debian wheezy on it. While I was able to install and run debian successfully, this phone has only 256 MB of RAM and android uses most of it, there was only 4-6 MB that was free at any time. I removed all the extra apps from installed play store and some bloatware installed on the system partition and kept only the essential ones in case I need it later.

But android always makes use of all the available free memory to make apps open faster and I’m only going use debian on it. I connect remotely to debian on the phone using ssh. But simply killing the running android system processes wont work as android will restart them immediately.

My guide builds on the one used here :https://wiki.debian.org/ChrootOnAndroid#Available_memory

Remember these commands must be run on an adb shell and not to be typed directly on your android device.

Get root on your android device and issue,

stop zygote   #or just stop

This stops the zygote and system server process from running and will not be restarted again automatically. Now if you check the available RAM in your device, you can see more that 50% of it is free. As there is no android’s virtual machine running none of the android apps will run and you will not be able use your device physically. If you still need more memory you can just use the stop command to free up other unused system processes.

To restart it if your want to use android back on the device, just use

start zygote #or just start

In general, issue ps command and look for processes that have its PPID (parent process id) as 1, which means that these processes are started by init as a service. you can get service name for this processes from /init.rc file on android and use stop command to prevent it from running.

Let’s take a look at how to stop mediaserver process for an example.

Filter the output of ps command to look for processes with parent process id 1. Below is the output from my phone, when i grep the ps output to get processes having id 1.

# ps | grep ” 1 ”
root      1     0     272    248   c030f5dc 0000ef8c S /init
system    2164  1     176    116   c050be58 0001264c S /system/bin/akmd2
shell     2165  1     680    316   c03fc378 afd0da1c S /system/bin/sh
system    2166  1     752    296   c0510ed4 afd0dcbc S /system/bin/servicemanager
root      2167  1     3688   580   ffffffff afd0e35c S /system/bin/vold
system    2168  1     1852   336   ffffffff afd0e35c S /system/bin/notified_event
root      2169  1     3672   552   ffffffff afd0e35c S /system/bin/netd
root      2170  1     628    308   c0559cec afd0e67c S /system/bin/debuggerd
system    2172  1     8764   868   ffffffff afd0e67c S /system/bin/drexe
system    2173  1     1036   348   c0292b1c afd0eca8 S /system/bin/npsmobex
media     2175  1     50960  7276  ffffffff afd0dcbc S /system/bin/mediaserver
bluetooth 2179  1     1208   720   c030f5dc afd0eb2c S /system/bin/dbus-daemon
root      2181  1     756    320   c05eba1c afd0da1c S /system/bin/installd
keystore  2182  1     1560   404   c0559cec afd0e67c S /system/bin/keystore
shell     2183  1     3308   176   ffffffff 0000ecd4 S /sbin/adbd
shell     2199  1     308    128   c030f5dc 0000fffc S /system/bin/immvibed
radio     2298  1     14480  2172  ffffffff afd0e35c S /system/bin/rild
root      4282  1     6120   1040  c030f5dc 40419364 S /usr/sbin/sshd
wifi      4333  1     2300   1340  c030f5dc afd0dde4 S /system/bin/wpa_supplicant
root      7823  1     122864 28164 c030f5dc afd0dde4 S zygote
dhcp      9231  1     800    404   c030f5dc afd0eb2c S /system/bin/dhcpcd

From the above output, to stop /system/bin/mediaserver we have to find the service name for the process used by android. To find it, look for the process name in /init.rc file used by init process.

# grep “/system/bin/mediaserver” /init.rc
service media /system/bin/mediaserver

This is my output, when i look for the process mediaserver in /init.rc file. The service name for the process is the second word of the output which is media here. Now to stop mediaserver process just use,

stop media

In general to stop an android service use the format,

stop <servicce-name>

This will stop the service from running. If you need to start a stopped service use the format,

start <service name>

This way more than 60-70 % of RAM can be made available for use by your debian or ubuntu or any other GNU/Linux installed on android and android will not get in your way.

One important thing to note is that if your root access is controlled by an android app like Superuser or SuperSU, to notify or to get permission from user for root access, once zygote is stopped and you exit as root on your shell, you’ll not be able to gain root again without restarting the device, as you stopped the android runtime and your su will fail with segmentation fault without the controlling app running.

Installing Debian wheezy on android (chroot method)

December 7, 2014 6 comments

This is a yet another guide on how to install Debian GNU/Linux on your android phone. While there are huge number of resources available on the Internet dedicated on how to install GNU/Linux to your android phone, this guide aims to show how to do it step by step manually without the use of any additional android applications and tools.

Before we begin, there are some prerequisites that should be met to install GNU/Linux on your android device,

  • Root access to your android device and busybox installed.
  • Your android’s Linux kernel should support loop devices and one of etx2/3/4 filesystems
  • A system running Debian GNU/Linux

If these prerequisites are met, then let’s get down to our first step, which is creating a chroot image.

Note: commands which required to be run as root are prefixed with sudo.

Debian has a tool called debootstrap which automates the process of creating a base debian system which can be chrooted into. Install debootstrap if you don’t have it installed already.

sudo apt-get install debootstrap

Now run debootstrap in terminal with the following arguments,

sudo debootstrap –arch armel –foreign wheezy ./wheezy ftp://ftp.uk.debian.org/debian

Note: Issue with WordPress fonts makes double dashes appear as single dash. single dash which appears before arch and foreign needs to be replaced with double dash

As we are creating a Debian system for a different architecture than an x86 system in which debootstrap is run, –arch armel argument is used to instruct debootstrap to create debian base system for arm architecture. –foreign instructs it to do initial unpack only, a second stage install is done on the actual hardware. wheezy instructs it to create a wheezy system. Replace with any other name like squeeze or jessie or testing or stable to create an appropriate system image. ./wheezy tells it to download the packages to directory named wheezy in the current directory from where debootstrap is run. And finally a repository url to fetch the packages from. You can use your local repository here, but make sure it has packages for the architecture armel. See man page of debootstrap if you need more information.

While this process runs let’s create an image file to hold the Debian system. I prefer to use an image file instead of creating an dedicated partition on the sdcard, as this way its portable and can be resized later easily when you need more space. To create an image file with size 1GB run the following command,

dd if=/dev/zero of=./linux.img bs=1M count=1024

If you need any other size replace count with appropriate value. Any size can be used according to the free space on your android’s sdcard, but a minimun of 1GB is recommended.Once it completes a file named linux.img with specified size is in the current directory.

Before creating a file system on this file, check what ext filesystem your android’s Linux kernel supports by running the following command in your android terminal.

grep ext /proc/filesystems

My phone’s kernel supports only ext2, so I’m creating an ext2 filesystem on this image file.

sudo mkfs.ext2 linux.img

Once filesystem is created, this image file can be mounted anywhere on the system, like an ordinary disk drive. I’m creating a directory named tmpmntpnt under tmp and for mounting this image file,

mkdir /tmp/tmpmntpnt

sudo mount -o loop linux.img /tmp/tmpmntpnt/

After this is done, check whether the debootstrap has completed successfully. If it is complete, copy the files under wheezy directory created by debootstrap to this linux.img mounted under /tmp/tmpmntpnt/ using,

cp -rp ./wheezy/* /tmp/tmpmntpnt/

-rp is used to recursively copy the files and preserving the files ownership and permissions. Once the files are copied, unmount the image using the command,

sudo umount /tmp/tmpmntpnt

Now the first step which is creating a chroot image is complete.

Before moving on to the second step, copy the linux.img just created to your android’s sdcard.

Moving on to second step, preparing your android device and mounting the image.

There is a lot of typing that needs to be done on the android device, so its preferable you use adb shell to type commands from your workstation to android device instead typing it directly in the device. Install android-tools-adb in debian which enables us to connect to android device over usb.

sudo apt-get install android-tools-adb

While installation is going on, enable android usb debugging in your android device settings under developer tools menu and connect the device to your workstation using usb cable and after adb-tools is installed,run

adb shell

to connect to your android shell. Once it connects and shows the $ prompt issue su command to get root. All the commands below needs to be run as root in your android shell.

Sdcards in android are mounted with nodev,noexec and nosuid. As we’ll be bind mounting device files and executing binaries with suid root, we’ll need to remount sdcard as,

busybox mount -o remount,exec,dev,suid /sdcard

Then, create a loop device to mount our linux.img,

loopno=254

mknod /dev/block/loop$loopno b 7 $loopno

This creates a block device with major number 7, minor number 254. These numbers are chosen by looking at the existing loop devices created by android os in my phone by running,

ls -l /dev/block/loop*

brw——- root     root       7,   0 2014-12-06 16:50 loop0
brw——- root     root       7,   1 2014-12-06 16:50 loop1
brw——- root     root       7,   2 2014-12-06 16:50 loop2

My android creates loop devices with major number 7 with incrementing minor numbers. You just have to choose the same major number and a minor number thats not yet used. As you install more apps to sdcard, it’ll create loop device for each of them, to prevent conflict I used 254, which is unlikely to be used any time soon. You can just use the same numbers I used without looking for all this stuff if you are not interested.

Once the loop device is created, associate our linux.img to this loop device using,

losetup /dev/block/loop$loopno /sdcard/linux.img

Now to mount it, create a directory under /data/local

mntpt=/data/local/linux

mkdir $mntpt

Then mount the loop device to this directory,

busybox mount -t ext2 -o relatime /dev/block/loop$loopno $mntpt

Take care to replace ext2 with the filesystem you used for the linux.img.

This completes the second step.

Moving on to the third step, which is completing second-stage installation and setting up debian system.

Once the image has been mounted, bind the kernel directories from your android to debian using,

for f in dev dev/pts proc sys ; do mount -o bind /$f $mntpt/$f ; done

This above step bind mount /dev /dev/pts/ /proc/ /sys to debian image, which means that it will be available to both android and debian and changes made from debian to any of the files in these directories will reflect in android and vice versa. These directories are required for the proper functioning of debian.

You can also optionally bind mount your sdcard to make it accessible under debian by using,

sdcard=$(readlink -f /sdcard)

mkdir $mntpt/media/sdcard

mount -o bind $sdcard $mntpt/media/sdcard

This will make files in sdcard available to both debian and android.

Now our debian system is ready to be chrooted into. Before doing that we’ll need to set some environment variables, so that the binaries in debian work properly. If this not set up correctly, debootstrap will fail to run.

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
export HOME=/root

Then setup /etc/resolv.conf file to enable dns resolution so that external addresses can be connected to,

echo “nameserver 8.8.8.8” > $mntpt/etc/resolv.conf
echo “nameserver 8.8.4.4” >> $mntpt/etc/resolv.conf

Now chroot into debian and complete the second stage installation,

chroot $mntpt /bin/bash -l
debootstrap/debootstrap --second-stage

This will take a while to complete. Once it completes, your debian system is ready 🙂

You can now use apt-get to install additional binaries. Let’s take a look at how to install openssh-server to remotely connect to our debian system.

Before using apt-get, repository needs to configured in /etc/apt/sources.list, just as you would do on a normal debian system. To do this run,

echo “deb http://ftp.uk.debian.org/debian/ wheezy main contrib non-free” > /etc/apt/sources.list

Now refresh repository information and install openssh-server

apt-get update

apt-get install openssh-server

After this is complete don’t forget to set password for root using passwd command to remotely log in to. Then try connecting to your device by using ssh from your workstation.

ssh root@<ip address of your android device>

If you are able to connect successfully, then congrats, you have installed yourself a debian system on your android from scratch. To unmount the debian system logout from the debian environment and unmount the directories mounted above using umount command.

Automate this mount and unmount process by putting the commands inside a script and installing it under /system/bin, so that it’ll save you from typing the commands over and over.

A quick hacked up script to mount and log in to the debian system and unmount it.

#!/system/bin/sh

arg=$1

loopno=254
loopdev=/dev/block/loop$loopno
mntpt=/data/local/linux
sdcard=$(readlink -f /sdcard)

if [ “$arg” == “unmount” ] ; then
umount $mntpt/media/sdcard
for f in dev/pts dev proc sys ; do umount $mntpt/$f ; done
umount $mntpt
losetup -d $loopdev
rm $loopdev
exit 0;
fi

mknod $loopdev b 7 $loopno
losetup $loopdev /sdcard/linux.img
busybox mount -t ext2 -o relatime $loopdev $mntpt
for f in dev dev/pts proc sys ; do mount -o bind /$f $mntpt/$f ; done
mount -o bind $sdcard $mntpt/media/sdcard
export HOME=/root
export USER=root
export TERM=screen
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH

chroot $mntpt /bin/bash -l

Save this script under /system/bin by mounting /system partition as rw and assign executable permissions to the script by

mount -o remount,rw $(mount | grep system | cut -d " " -f 1,2)
cp /sdcard/linux /system/bin/linux
chmod 755 /system/bin/linux
mount -o remount,ro $(mount | grep system | cut -d " " -f 1,2)

From now on, you’ll be able to login to debian by just using linux and unmount it by issuing linux umount from android shell.
Reference: https://wiki.debian.org/ChrootOnAndroid#Manual_installation_in_a_chroot
Update: to free memory used by android and make it available to Debian see https://blulin.wordpress.com/2014/12/07/freeing-up-resources-for-use-by-gnulinux-on-your-android-device/