Gentoo on Samsung Galaxy S2


Gentoo Logo (c) Gentoo FoundationStumbling upon this page you probably came up with the same idea that I have – to run Gentoo on your Samsung Galaxy S2 device. It is definitely possible in a chroot environment. I have not tested dual booting to date, but if you’re into a chrooted Gentoo – read on. There are a few “gotchas” in the process I have come across – described below.

Base system installation

First of all – decide where will you put it. Your natural choice might be one of the SD cards, but see this:

# grep /mnt/sdcard /proc/mounts
/dev/block/vold/179:11 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,[...]
/dev/block/vold/179:17 /mnt/sdcard/external_sd vfat rw,dirsync,nosuid,nodev,noexec,noatime,[...]
tmpfs /mnt/sdcard/external_sd/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0

You might try remounting these without the restrictions with your on-board busybox mount, but the internal SD is a vfat mount, so don’t expect things to work well in a chroot on that. My external SD is also contains a vfat filesystem, so I did not explore further for now. One good approach would be to create an ext2/3/4 filesystem on the external card – and do it SDHC-friendly (block alignment, wear leveling, etc) and hope the phone mounts it properly at boot (proper fs, and proper options – if not, a Tasker “on-boot” scenario could fix it). But again – YMMV, I did not explore this option myself.

Because of the above I put Gentoo  in /data/gentoo mounted as ext4 – not much of neither block space nor inodes, but will work for a small base system without portage tree (for now).

The location is defined, mkdir done, so now grab your stage3 archive for ARM and unpack it there. Back in the day I went with:

8d4c0d6b6f52140acc97b22ae370f770  stage3-armv7a_hardfp-20111108.tar.bz2

Check your favorite mirror at releases/arm/autobuilds/current-stage3-armv7a/ and refer to http://distfiles.gentoo.org/releases/arm/autobuilds/current-stage3-armv7a/ if your mirror does not rsync it.

Do the bind mounts and test it:

# mount -o bind /dev ./dev
# mount -o bind /dev/pts ./dev/pts
# mount -o bind /proc ./proc
# mount -o bind /sys ./sys
# chroot /data/gentoo /bin/bash

Then, set up your make.conf (SYNC, GENTOO_MIRRORS). You might want to add these too:

USE="-X -cups -ipv6"
PORTAGE_NICENESS=1

Set up /etc/resolv.conf (I suggest to use some universal DNS server entry) and your timezone. Optionally a hostname like sgs2🙂

A note on user accounts: Android’s underlying Linux generates UIDs dynamically over 1024. Care must be taken with these UIDs and from now on I will use the root account. Other UIDs <1024 and >0 may also expose issues.

Set up root’s password and PermitRootLogin in /etc/ssh/sshd_config.

Try starting sshd:

# /etc/init.d/sshd --nodeps start

You might try to login via network now; the sshd will be running there until you reboot. If you encounter “no route to host” – let the device’s wifi wake up after a few taps if it was not active.

As usual for a chrooted install you need to take care of your bind mounts. Below is a simplified version of my script for doing that. It also includes other mounts in the /data/gentoo/android directory (create it beforehand!) for convenience. Root mount is not replicated there:

# mount -o bind / /data/gentoo/android/
mount: Invalid argument

The do-mounts script:

#!/bin/sh -x

# do we really need to do it?
grep -q /data/gentoo /proc/mounts && exit 0
# mount list. /data excluded to avoid mount loops; /* directories included
# but do not work:
MOUNTS="`(cat /proc/mounts |awk '{ print $2 }' ; \
  busybox find / -type d -maxdepth 1) |grep -v ^/data |sort |uniq`"

# basic bind mounts:
mount -o bind /dev ./dev
mount -o bind /dev/pts ./dev/pts
mount -o bind /proc ./proc
mount -o bind /sys ./sys

# loops come handy right in /dev:
( cd ./dev; for f in block/loop?; do busybox ln -s $f;done )

# the convenience mounts:
for f in $MOUNTS; do
  busybox mkdir -p ./android/$f
  mount -o bind $f ./android/$f
done

You might want to run this script from Tasker at boot for example. I chose to run it from a wrapper for running a command in Gentoo chroot. It is run every time, but I don’t mind speed and latency in favour of serialization (Tasker currently seems to have issues with that so you may encounter surprises if you create two tasks – one for mounts and one for sshd – a race condition may ruin your day).

You might also find handy a wrapper (mentioned above) for running commands in the Gentoo chroot. Here is a version of mine:

#!/bin/shsu -c /data/gentoo/do-mounts
chroot /data/gentoo /bin/bash -c ". /etc/profile ; \
  env-update --no-ldconfig >/dev/null; . /etc/profile ; \
  export TERM=vt100 ; export HOME=/root ; export USER=root ; \
  export SHELL=/bin/sh ; $*"

Just remember to take care of ldconfig manually when required.

Beyond Gentoo base system – portage

Probably your hands almost typed emerge –sync somewhere above or unpacked a portage snapshot. Maybe not even almost🙂 And most probably you suffered from inode exhaustion. A loop mount to the rescue.

Get a free loop device. The output losetup -f may be inaccurate on Android (at least I have had issues with it lying). I chose minor device 7. And dd an 1G empty file:

dd if=/dev/zero of=/mnt/sdcard/file bs=1M count=1024
losetup /dev/loop7 /mnt/sdcard/file
mkfs.ext2 -O dir_index -i 4096 /dev/loop7   # many small files, right?
busybox mount /dev/loop7 /data/gentoo/usr/portage

Don’t forget to add the losetup and mount to your do-mounts script.

And now you can get a portage snapshot from your favourite mirror and then emerge –sync.

You will however encounter another issue in a few minutes – portage will not be able to download any distfile. It seems that uid 250 on Android is deprived of network connectivity. I am fully aware (and you should be too) what the following hack means. Works for me with all security implications.

sed your /etc/passwd:

s/^portage:x:250:250:/portage:x:0:0:/

And you are done. emerge screen or vim and watch it compile🙂 An alternative would be to emerge -p –fetchonly and wget distfiles as root first. But this would not solve distcc issues…

Notes

Speaking of it – how about a distcc helper on a different box? Possible, I did it. I will share some details soon.

Good luck!

Gentoo Logo disclaimer: The “g” logo is a trademark of Gentoo Foundation, Inc., and that any Gentoo artwork is copyright Gentoo Foundation, Inc. The content, project, site, product or any other type of item with which the “g” logo or Gentoo artwork is associated is not part of the Gentoo project and is not directed or managed by Gentoo Foundation, Inc.

18 Responses to “Gentoo on Samsung Galaxy S2”


  1. 1 ore 25.12.2011 at 21:52

    Post a few screenshot with a working system please

    • 2 Apatsch 25.12.2011 at 22:06

      Hi Ore, thanks for your comment. The system does not have any gui to take screenshots of – you get a chroot shell in a plain base system. Maybe I could put a typescript or two from the shell: uname -a output perhaps?

  2. 3 ore 27.12.2011 at 17:22

    that would be nice, thanks

    • 4 Apatsch 29.12.2011 at 16:32

      Here’s some generic output:

      $ adb shell
      # /data/gentoo/do-cmd bash
      phone / # cat /etc/gentoo-release
      Gentoo Base System release 2.0.3
      phone / # emerge -vp portage

      These are the packages that would be merged, in order:

      Calculating dependencies... done!
      [ebuild R ] sys-apps/portage-2.1.10.11 USE="(ipc) less -build -doc -epydoc -python2 (-python3) (-selinux)" LINGUAS="-pl" 830 kB

      Total: 1 package (1 reinstall), Size of downloads: 830 kB
      phone / # exit

      • 5 plindbe2 22.03.2012 at 02:43

        Hello,

        Excellent guide!

        One minor critique is when you are setting up the loop device, you need to:
        $ mknod /dev/loop7 b 7 7
        before
        $ losetup /dev/loop7 /mnt/sdcard/file

      • 6 Apatsch 22.03.2012 at 22:03

        Hi!

        Thanks for spotting that🙂 Actually on my system I’ve already had loop[0-7] block devices, so I skipped that step, but nevertheless you are absolutely right🙂

        Additionally one might need to:

        # modprobe loop

        but I am not sure if there are any platforms which do not include loop in the kernel nor load the module from initramfs in case of Android.

        Marcin

  3. 7 Andrey 19.02.2013 at 00:40

    How did you uncompresed stage3 archive? I tried tar/bunzip2 but not lucky…

    • 8 Apatsch 19.02.2013 at 00:47

      What’s the trouble exactly?

      • 9 Andrey 19.02.2013 at 10:02

        When I tried bunzip2 on my s2 it returned:
        bunzip error -5
        Then I tried bunzip2 on my pc and then untar on s2 but it also doesn’t work.

      • 10 Apatsch 19.02.2013 at 14:43

        It is difficult to do any guessing from “doesn’t work”. You need root, that’s for sure. If you have busybox provided by a custom rom for example you might try its bzip2/tar combination. Your mileage may vary. Is the md5 sum correct?..

  4. 11 Andrey 19.02.2013 at 15:40

    << It is difficult to do any guessing from “doesn’t work”
    Yes, I understand… I just thought that there may be special case…
    Yes, I have root permissions and busybox installed on device.
    I use terminal emulator and from it I use bzip2/tar… and bzip2/bunzip2 are not able to unpack… reason is: bzip: bunzip error -5… about md5 not sured… will check it later… May be there no enough space on the device… Will be investigating…

    • 12 Apatsch 20.02.2013 at 00:57

      Try busybox bzip2 and busybox tar (yes, call these this way). But that got me thinking anyway. I really cannot recall if I had any issues, probably not, as I would document these in the article. Just checked with a sample tar.bz2 archive and Android’s bzip2/tar extracted it just fine. I’m curious; when you find out what’s the problem please do share what it was🙂

    • 13 Apatsch 26.02.2013 at 21:22

      Andrey, did you manage to solve your issue?

    • 14 Rameswar 14.03.2014 at 01:22

      When i type: /data/local/tmp/rage in the ematluor it kind of work’s and the ematluor force closes but when i try to restart the ematluor it wont load the app! PLEASE HELP!!!

  5. 15 Andrey 28.02.2013 at 10:21

    No. I tried busybox bzip2 and busybox tar and was not lucky with them…
    But I postponed installation, will back to it a bit later. I have several ideas how to workarround. Not sured if they are right but will try, will try, will try. My goal is to install!🙂 I’ll write you back and share in case of any progress


  1. 1 Gentoo on Android – cross compilation with distcc « rootprompt Trackback on 08.02.2012 at 00:42
  2. 2 Samsung Galaxy S2 – rootprompt’s first things first « rootprompt Trackback on 08.02.2012 at 00:44
  3. 3 downfree.net Trackback on 22.04.2012 at 09:51

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Enter your email address to follow this blog and receive notifications of new posts by email.

Join 50 other followers

Marcin Gałkowski

Seasoned admin, Linux hacker, Android fan, Gentoo enthusiast. Doing digital audio on Linux when time permits. At work: IT Team Lead (+labs, +datacenters...)

Latest Tweets

BTC tip jar:

1HprotPu2zi8v8hFZBqVfcTYL7ZYVYBruo

%d bloggers like this: