• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Chrooted Ubuntu working on SGS2

lotus49

Android Expert
May 9, 2011
1,513
232
Yorkshire
By using these instructions How to Install Ubuntu on Android! I have managed to install a chrooted Ubuntu on my SGS2.

I had to make quite a few changes to the procedure (I'll write them up if anyone is interested) but it wasn't that hard. You would have to know Linux pretty well to debug the above instructions on the SGS2 though.

I can now ssh into my Ubuntu installation from any other machine on my network including, of course, the SGS2 itself. Tomorrow I shall try to install a GUI. That's a little ugly, requiring VNC, but it looks simple enough.

Perhaps someone will write an Android X server. Perhaps they already have, I shall have to investigate further.
 
I thought I had replied to this yesterday but it appears not.

Before you can do this you need to be able to log in to your phone as root. I was unable to do this using adb but was able to install a ssh server and since I have rooted my phone, I could then log in using ssh.

If you confirm that you can do this, I'll write up the steps but if you saw my comment near the bottom of the page, I explained most of what I did there.

I have subsequently made further changes as I realised that /proc and /dev should have been bound into the chrooted file system so I can post my bootubuntu file if that would be helpful. Debugging that file was the only hard bit of the process.
 
Upvote 0
ok, i am in ;-)

Maybe I can also tell you the reason why you didn't manage to get root with adb: maybe you didn't install "superuser" from the market (i did). Anyway if you install it, when you type "su" on adb you need to unlock your screen and allow the privileges for adb (you will see a popup).

I also read your post on androlinux, but I didn't try it yet...could you explain me the remaining passages please?

thank you very much
 
Upvote 0
ok, i am in ;-)

Maybe I can also tell you the reason why you didn't manage to get root with adb: maybe you didn't install "superuser" from the market (i did). Anyway if you install it, when you type "su" on adb you need to unlock your screen and allow the privileges for adb (you will see a popup).

I also read your post on androlinux, but I didn't try it yet...could you explain me the remaining passages please?

thank you very much

You are right, I didn't install "superuser" but since I had already rooted my phone and installed sshd, I was able just to ssh in as root.

OK, there isn't all that much more to it. I decided, in the interests of saving space, to install ubuntu onto my SD card and the changes I made to the bootubuntu script reflect that.

All you need to do in addition to the process as described in my link is to replace the file bootubuntu with my amended version BEFORE you run the ubuntu.sh script.

This is what it looks like:

Code:
#modprobe ext2
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
export kit=/sdcard/external_sd/ubuntu
export bin=/system/bin
if ! test -d /data/local/ubuntu
then
mkdir /data/local/ubuntu
fi
export mnt=/data/local/ubuntu
export PATH=$bin:/usr/bin:/usr/sbin:/bin:$PATH
export TERM=linux
export HOME=/root
#mknod /dev/loop1 b 7 0
losetup /dev/block/loop7 /sdcard/external_sd/ubuntu/ubuntu.img
mount -t ext2 /dev/block/loop7 /data/local/ubuntu
#mount -o loop,noatime -t ext2 $kit/ubuntu.img $mnt
mount -o bind /proc $mnt/proc
mount -o bind /dev $mnt/dev
mount -t devpts devpts $mnt/dev/pts
mount -t proc proc $mnt/proc
mount -t sysfs sysfs $mnt/sys
sysctl -w net.ipv4.ip_forward=1
echo "Setting /etc/resolv.conf to Google Open DNS 8.8.8.8 and 8.8.4.4"
echo "nameserver 8.8.8.8" > $mnt/etc/resolv.conf
echo "nameserver 8.8.4.4" >> $mnt/etc/resolv.conf
echo "Setting localhost on /etc/hosts "
echo "127.0.0.1 localhost" > $mnt/etc/hosts
echo "READY TO ROCK AND ROLL BABY! "
echo "Brought to you by NexusOneHacks.net and the open source community! "
echo " "
chroot $mnt /bin/bash

#After exit command is executed clear it all up
echo " "

echo "Shutting down Ubuntu"
umount $mnt/dev/pts
umount $mnt/proc 
umount $mnt/sys 
umount $mnt
losetup -d /dev/block/loop7
The changes I made (and feel free to compare the two scripts yourself) were these:
1. comment out the mknod line as there was already a usable loop device
2. insert "external_sd/" into all the paths so that they pointed to the SD card
3. use loop7 rather than loop1 (which was already in use)
4. insert the two "mount -o bind..." lines, these should have been in the script already so that /proc and /dev were properly accessible after the chroot, I probably should have had one for /sys as well now that I think about it
5. My phone barfed on the if [ ! -d ...] syntax so I changed it slightly so it does the same thing but with different syntax

The version of ubuntu in ubuntu.img is rather old (Karmic). However, there is a link on that page explaining how to create a new image. The instructions worked first time with no changes on a Maverick system and Natty system. I am using the latest and although I haven't tested it a great deal (so many other things to play with on my snazzy new phone) it seems to work very well. If you don't already have a computer running ubuntu, you will struggle to do this bit and you'll have to resort to a VM or a live CD.

I cannot promise to be able to fix any problem you have but I am an old hand at Unix since long before Linux was even thought of and I have been with Linux almost since its birth so I should be able to help.

The usual warnings apply so if you manage to bugger up your phone, it's not my fault but I cannot really see how you could do that with this process. It adds to what's there already but changes nothing else so it should be completely safe.

Oh, the last thing was that I kept getting Permission denied errors running scripts so just:
Code:
# sh bootubuntu
rather than just
Code:
# bootubuntu
if this happens to you.

Have fun.
 
Upvote 0
Thank you so much! IT WORKS! It's really AMAZING!

Just a few things:

Probably it's a stupid question (and it's because I didn't understand very well how it works) but...how can I shut ubuntu down? (and above all, is it necessary?)

The version of ubuntu in ubuntu.img is rather old (Karmic). However, there is a link on that page explaining how to create a new image. The instructions worked first time with no changes on a Maverick system and Natty system. I am using the latest and although I haven't tested it a great deal (so many other things to play with on my snazzy new phone) it seems to work very well. If you don't already have a computer running ubuntu, you will struggle to do this bit and you'll have to resort to a VM or a live CD.

No problem for that, I am a strong linux supporter (unfortunately I'm not also a very powerful linux user :) )

Anyway, I will surely try to build a natty image.

I am very happy that you are a powerful linux/unix user. So I can ask you some question:

- what terminal emulator do you use on the phone? I use android terminal emulator by Jack Palevich. It's not so bad, but there are some things that I would really like to have: colours (for example for "ls" different kind of files) and above all gestures. Did you ever use "mobile terminal" on the iphone? the gestures are really good (for example top-left swipe for Tab)

- why the "Tab" key does not work on android terminal??? Nor in emulator, in adb and with ssh...

- why the terminal is so...strange? for example, there isn't the classic "user@pc_name:~/folder$". It would be quite useful.

- do you know a way to install many linux classic tools? for example nano,...


Any advice for cool nerd stuff is really appreciated ;)

Thank you very much again
 
Upvote 0
When you log out of the shell in ubuntu it closes down as a result of that. You don't have to do anything else.

I use ConnectBot and it does do colours. It won't allow you to change port, which is a bit annoying so I am looking for an alternative.

When you refer to the strangeness of the environment it depends if you are talking about Android or Ubuntu. I presume you mean Android and the reason it seems unfriendly is that it's not meant for use directly. It's an OS powering a phone and it was designed to provide a minimal environment to enable a single-user GUI to run on top of it.

Your best bet for installing additional software into Android is generally not to, but if you need say an editor, install busybox. Have a look at the free "BusyBox Installer" app in the marketplace. A recent version of busybox will provide an editor as well as a lot more.
 
Upvote 0
Some problems:

When you log out of the shell in ubuntu it closes down as a result of that. You don't have to do anything else.

I don't know why, but if I type "exit" in ubuntu (on android) shell it tells me:

Code:
root@localhost:/# exit
exit
 
Shutting down Ubuntu
failed.
losetup: /dev/block/loop7: Device or resource busy
#

But then it exit anyway. But if I try to connect with VNC it's still there! Anyway I think it's kind of "freezed" when you don't use it, because my battery isn't draining at all.

Another thing: I tried 2 VNC client but it seems that the keybord input works really badly: all the keys are wrong. Maybe it's necessary to choose the correct keyboard in ubuntu.
 
Upvote 0
If you are using the ubuntu.img that you downloaded rather than building your own, I think the locale is set to German because when I tried to use apt-get the messages were in German. Try building your own and see if that helps.

I shall look into the issue you describe about shutting down. I haven't done much testing of the script and I suspect that it had already been cobbled together before I started to hack it about too.
 
Upvote 0
Try creating your own ubuntu.img istead of downloading it(as the language and keymap is probably all messed up).
The procedure is given on the following page:
How to Build CHROOT ARM Ubuntu Images for Android!

It works pretty fine for me except that i end up screwing the GUI cause of using ubuntu 11.04 (unity is not supported by vnc). So have to use lxde instead(which is frustrating).

Also the error:

root@localhost:/# exit
exit

Shutting down Ubuntu
failed.
losetup: /dev/block/loop7: Device or resource busy
#

does not affect the functioning of ubuntu. But still for some people who dont like any errors, you can run d "sh ubuntu.sh" command everytime you restart your phone.

Also remember to type "vncserver -kill: 1(or your corresponding port/display number)" in ubuntu terminal before rebooting your device so that you dont have to change the port everytime you reboot your phone.

All other scripts work fine. No need to edit those unless you know exactly wat you are doing or you want to end up getting a lot of errors.
 
Upvote 0
That looks like a cut and paste error to me.

Since the letters k3 do not appear in the script but do appear in the error message, I suspect that you have copied the script incorrectly.

Have another go and report back.

If that doesn't work, try:

Code:
# sh -x bootubuntu
and post the results so I can work out what the problem is.
 
Upvote 0
That looks like a cut and paste error to me.

Since the letters k3 do not appear in the script but do appear in the error message, I suspect that you have copied the script incorrectly.

Have another go and report back.

If that doesn't work, try:

Code:
# sh -x bootubuntu
and post the results so I can work out what the problem is.

I ended up going with Back Track 5 for Android, apparently they had a similar problem, after an update, worked perfectly.

Thanks for posting though.
 
Upvote 0
Howdy. I'm getting a similar error to Otester. I grabbed the output of sh -x bootubuntu.


Code:
export PATH=/data/local/bin:$PATH
# export PATH=/data/local/bin:$PATH
# cd /sdcard/external_sd/ubuntu
# sh -x bootubuntu
+ mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
mount: No such file or directory
+ export kit=/sdcard/external_sd/ubuntu
+ export bin=/system/bin
bootubuntu: 38: Syntax error: end of file unexpected (expecting "then")
#
This is an exact copy/paste of the bootubuntu script im using

Code:
#modprobe ext2
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
export kit=/sdcard/external_sd/ubuntu
export bin=/system/bin
if ! test -d /data/local/ubuntu
then
mkdir /data/local/ubuntu
fi
export mnt=/data/local/ubuntu
export PATH=$bin:/usr/bin:/usr/sbin:/bin:$PATH
export TERM=linux
export HOME=/root
#mknod /dev/loop1 b 7 0
losetup /dev/block/loop7 /sdcard/external_sd/ubuntu/ubuntu.img
mount -t ext2 /dev/block/loop7 /data/local/ubuntu
#mount -o loop,noatime -t ext2 $kit/ubuntu.img $mnt
mount -o bind /proc $mnt/proc
mount -o bind /dev $mnt/dev
mount -t devpts devpts $mnt/dev/pts
mount -t proc proc $mnt/proc
mount -t sysfs sysfs $mnt/sys
sysctl -w net.ipv4.ip_forward=1
echo "Setting /etc/resolv.conf to Google Open DNS 8.8.8.8 and 8.8.4.4"
echo "nameserver 8.8.8.8" > $mnt/etc/resolv.conf
echo "nameserver 8.8.4.4" >> $mnt/etc/resolv.conf
echo "Setting localhost on /etc/hosts "
echo "127.0.0.1 localhost" > $mnt/etc/hosts
echo "READY TO ROCK AND ROLL BABY! "
echo "Brought to you by NexusOneHacks.net and the open source community! "
echo " "
chroot $mnt /bin/bash

#After exit command is executed clear it all up
echo " "

echo "Shutting down Ubuntu"
umount $mnt/dev/pts
umount $mnt/proc

EDIT:
Turns out to be an encoding problem. DON'T USE WINDOWS TO EDIT THE bootubuntu FILE. Use linux to get the right file encoding on the script. Works a charm that way.
Thanks a heap to lotus49 for the script!
 
Upvote 0
Has anyone managed to fix the Loop7 Device busy error?
I edited the bootubuntu script so that it doesn't unmount the directories or attempt to remove the loop link when i quit the shell. I find running an SSH server in the background all the time handy and more than sufficient, but the ability to release the resources the ubuntu is using without rebooting my phone would be handy...
 
Upvote 0
Has anyone managed to fix the Loop7 Device busy error?
I edited the bootubuntu script so that it doesn't unmount the directories or attempt to remove the loop link when i quit the shell. I find running an SSH server in the background all the time handy and more than sufficient, but the ability to release the resources the ubuntu is using without rebooting my phone would be handy...


Put this line right after the "export bin=/system/bin" line

freeloop=$(expr 1 + $(ls /dev/block/loop* |cut -f 16- | sort -n|tail -n 1))

and then replace all "loop7" in the script with "loop$freeloop"


The thing is, loop7 wont be free if say, you have moved some apps to sd card .. the freeloop=line gets the next free loop device.

Linux veterans might complain losetup -f should give the next free loop device .. but the problem is losetup looks at /dev/ for loop devices , while android seems to store them in /dev/block/ folder .. so losetup will report loop0 as the free loop, making necessary the above circus
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones