Android, rooting, warranty, Linux and more

Tags: apps, software, tutorials.
By lucb1e on 2013-06-05 22:22:57 +0100

As suggested by @wesleypennock, I'll tell you more about running Linux on Android. Actually he asked about Xfce, but that would be a rather short article ;)

Rooted phone
First of all, you need a rooted phone. I was kind of shocked to hear how few people have a rooted phone, since there is no good reason not to do it. Except when you don't need it, like my mother, but many tech people also don't have it because it'd void warranty. Or so it says.

In the Netherlands, you can always return products when they're not functioning as they should, when an advertisement made a false promise, or when you told the seller what you wanted to do with it (e.g. installing Linux) and they didn't warn you that it was impossible. Regardless of the warnings about voided warranty that your phone may give you, any country within the European Union has a minimum of 2 years warranty, and in the Netherlands it's even unlimited. A product should simply work as one might expect. Wear is normal and doesn't fall under warranty of course, but if a TV breaks after 2 years and 2 months when it was hardly used (and properly used), then you can still return it and have it fixed, get a new product, or get your money back. The seller may return less money because you've already used the device for a while, but this amount must again be something reasonable.

When returning broken products, the seller may say that you caused the defect and thus not give you warranty. Here's the thing: You don't need their warranty. They're legally obliged to give you warranty for at the very least two years (or one year in case it's second-hand product), and if something breaks within 6 months, they have to prove it was your fault and not theirs. They may also not refer you to the manufacturer; either they fix it or they send it to the manufacturer themselves.

For example, a rooted phone can never be the reason for a malfunctioning camera lens. Your warranty is not voided by rooting your phone. Unless of course you use the root to overclock your device, and then want to return a burned-out CPU... yeah that's not covered. Truth be told, many shops will be giving you a hard time getting even that camera lens fixed, but you have the legal right to.

So anyway, a rooted phone. Though I'm not sure about other countries, it's not a big deal in Europe and especially in the Netherlands.

Reference for the Dutch people here: http://www.consuwijzer.nl/thema/garantie. There is more info linked at the bottom of the right sidebar.

So Linux on your phone huh
Since Android runs on the Linux kernel, you can run native Linux binaries on it. Here is a little background:
Programs are written in some language, for example C++. Then it's "compiled" to another format: machine code. This is what we call binaries. These binaries have a header, telling the operating system what kind of thing it is, where to find its icon to display, etc. That's why Windows .exe files don't run on Linux natively; Linux doesn't understand it. The second thing about this "machine code" is that they include instructions that are run by the CPU directly, not read and interpreted by the operating system. So if you have a phone that runs on an ARM processor (most phones), you'll be having a hard time running x86 Linux binaries on it. x86 and x86_64 by the way, have like 99.99% of the desktop and laptop market share, so everything is made for it.

Fortunately for us, Linux is open source. Hurray, we can just compile the Linux operating system and all its components for ARM processors!

And basically this is true. Many programs will expect that you have a mouse and keyboard instead of a touchscreen and a home button only, but that's a bit besides the point here. You'll see why.

Also a little bit more background, cause I've always been wondering this: The reason you can't just change the header and then run Windows' .exe files on Linux, is because they contain lots and lots of references to all sorts of Windows components. Things like whether it should create a console window, a form window, or a plain surface to draw on. There are APIs for telling Windows (or Linux) where to draw and what to draw. Libraries covering everything from rounding functions to encryption algorithms themselves. Ways to access to certain hardware... The list goes on and on, and it's very hard to port. The WINE and Mono projects have made good progress, but we're not quite there yet, and I doubt we ever will be. Not that it's an absolute requirement, it's more a matter of convenience when working on Linux. Else we'd have ported everything by now I guess.
Okay, back ontopic.

Installing Linux the easy way
There is this nice app called "Linux Deploy". Search it in the Play Store, I can really recommend it. It's completely safe, and it will not replace Android. It will run next to Android and leave Android itself entirely intact. I'll assume you have rooted phone by now. Here's a quick walkthrough:
When starting the app, press the download-looking icon on the right-bottom. You'll get some options like which distribution you want, which version, which processor architecture... pretty standard. I went for the latest Debian and an image file. You'll just get a file, like /sdcard/linux.img, and within there is your Debian installation. Really easy.

It depends how much free space you have, but I recommend taking at least a few gigabytes of space, perhaps two or three. It'll run on much less though.

My desktop environment of choice is LXDE right now, but for no particular reason. Mind that especially GNOME and KDE are heavier than the other options, but they'll work as well. (Hmm, they might actually take up a gigabyte or two, I've never tried. That's why you should reserve enough space.)

I have it start only an ssh server on boot, but for the first time I recommend you also start a VNC server on startup.

And that's about it. Go to the top and click install now :). This will take a while because it downloads all tools and things you need, and then extracts and installs them. That's why the app itself is so small: it doesn't contain all of Linux.

Linux installed!
Yeah it's that easy. It's cool to see your new desktop environment. Go and get androidVNC (app), and connect to 127.0.0.1 at port 5900 (optionally entering credentials, depends on your settings).

In the Linux Deploy app you can set a desktop resolution. I recommend setting this to your native screen resolution (1280x768 for me), then stop and start Debian in the Linux Deploy app, and connect again. Now you'll see all pixels 1:1. This is how many pixels they've put on that small screen that you're holding in your hand. Ridiculous, we could do with a tenth of this. But whatever, it's not like they're giving us a choice since Apple promoted retina. I have no trouble looking at 90DPI screens like most PCs have (my previous phone, a Nokia E75, had 167), but apparently we all need over 300 DPI nowadays. Or 441 in the case of the latest Samsung Galaxy phone. You can't see this many pixels! Whatever.

So you zoom in on the androidVNC app, and there you have a usable desktop system :). In VNC, have a look at Options -> input mode. You'll be using Touch Mouse Pan and Zoom most of the time, but it's good to know there are more. Particularly, you might want to try Mouse Pointer Control Mode some time.

It's kind of fun to run a Citrix client from here, just because you can. I connected to a Windows XP environment and then showed everyone Look, I run XP on my phone!!1. In fact, I did attempt to install Windows XP via qemu (sort of like VMWare or VirtualBox but more command-line oriented), but my phone didn't seem to have all the necessary hardware requirements. XP is kind of old-fashioned let's say.

When you want to end your VNC session, press the options key and then tap disconnect. It will leave your session running, but it won't take much power. Having the VNC session stay alive (when you press the hardware home button instead of disconnect) will consume more battery though.

Now what?
Good question. Well, what do you want to do? One of my favorite uses of the desktop environment is running Wireshark to see what apps are doing. Besides that, the command line is what I prefer.

Connectbot is a fine app for setting up ssh connections and getting a commandline for your Debian install. It's much better than the Terminal Emulator that you find in the Play Store. Even combined with the BusyBox app, the Terminal Emulator pretty useless. ConnectBot only sets up connections so it's not a better terminal, but your Debian contains a real one like on a PC, so that's pretty great :).

Oh and before I forget: get the Hacker's Keyboard. You'll really want that when you're using connectbot.

So we got connectbot, hacker's keyboard, and we're ready to connect to localhost once again :). You need to type your password every time that you login for now, but we can fix that later.

From here on, it's all dependent on what you want to do. It's like when I first installed Linux on my desktop: "okay... fun. It looks like Windows with a start button and all, and it has a browser that functions just like Windows. Now what?"

You can install things like Audacity, nmap, vim, even some games. I've had some fun playing /usr/games/go-fish against the computer when I was stuck in a 90-minute traffic jam in France and I didn't have internet :P.

Note: I recommend connecting to ssh as root. I use my Debian installation mostly for testing, fun, and yeah a little show-off now and then :P. It's not really needed, in my opinion, to run as regular user, though it would be safer. I'll be assuming you run as root, or know how to use su/sudo. Also it is recommended to have some basic knowledge of how commands work. The post might be a bit of a mess, sometimes explaining basics and perhaps other times not going into depth when I should. If anything is unclear, ask me about it in the comments!

Accessing files
One thing that you'll be wanting to do soon is access files on your internal and external sdcard. I did this with the following commands, but it might differ slightly on your device:
cat > mountme[ENTER]
cd /dev/block[ENTER]
for i in `ls mmc*`; do mkdir /mnt/$i; mount /dev/block/$i /mnt/$i; done[ENTER]
[CTRL+C]
chmod +x mountme[ENTER]
./mountme[ENTER]

Typing this is not even possible with the default keyboard, that's why you installed the Hacker's Keyboard ;).

After typing ./mountme, it'll run the script we just made. It'll throw some warnings because not everything is mountable, but we completely ignore them.

Then it's a trick question which mount you'll need to use. For me (Galaxy Note 2) it was cd /mnt/mmcblk0p16/media to end up in /sdcard, and cd /mnt/mmcblk1p1 to end up on the external sdcard. You can just try them all in the /mnt directory (list directory contents with the ls command).

Fun fact: If you have a microusb->female usb converter, hook up a USB floppy drive and mount it (device is /dev/block/sda usually). Great fun! :P

Fun with the CPU(s)
Because we can.

Go to (use cd) /sys/devices/system/cpu. If you want to see which CPUs are currently active, type: "cat online" (without the quotes). Cat stands for concatenate, and concatenates any input it gets to STDOUT. It's a long story. If you don't know what I mean, let's just say it displays files.

Want to bring down a CPU? No problem, type:
echo 0 > cpuN/online
Where N is the CPU number. You can see which CPUs you have by typing ls.
To bring a CPU online you can echo 1 instead of 0. You can also modify the clock speed this way (though I don't think you can really overclock it like this, you can at least throttle the CPU to either consume less power or be faster), but I recommend using cpufreq for that. Note that if you disable (bring down) cpu0, your system will probably freeze.

Let's change the clock speed. Type:
apt-get install cpufrequtils

And then:
cpufreq-info

This will print out so much info you'll be staring at your screen so long it'll go dark. At least that was my first experience :P. Read the info for one of the CPUs through, after that you'll easily find what you need next time.

Note the current and the maximum CPU speed. I have a max of 1600MHz and it usually is around 800MHz. When I press the power button to turn the screen off, I've noticed the speed instantly goes back to 200MHz for power saving. The lockscreen is 400MHz, and normal use 800. Only at peak it reaches the 1.6GHz limit.

You can set the CPU speed with this command:
cpufreq-set -f 1600MHz

Where -f means "frequency", and 1600MHz is my maximum speed of 1.6GHz. With this utility, you can also set the "governor", some software that decides the CPU speed. By default the governor in Linux is ondemand, and this seems to be the same in Android. Whenever the load is high, extra power is brought in.

After setting the frequency manually, Linux will change the governor to "userspace". Userspace is the opposite of anything that runs in the kernel. Userspace are programs that run on top of the kernel, not in it. In this context, userspace means that a non-kernel program modified the speed and now regulates it. If you don't reset this, your CPU speed will be locked at whatever you set it to. I've noticed that locking it at 1.6GHz for hours didn't decrease the battery life by much, but it still decreased. Unfortunately it also is still slow when powering the screen on (takes one or two seconds; not really as instant and snappy as I'd like), so there is no use for it either. You better change it back I guess.

This is how to set it back to ondemand:
cpufreq-set -g ondemand

Your own desktop always with you
If you can connect to your Debian installation via VNC and SSH from localhost, why can't you also do this from a computer? :). Turns out you can.

You can connect over WiFi, which is easy when you are at home, or when you're mobile it's probably easiest to setup a (secured) wireless hotspot or connect your phone over USB and enable USB tethering.

- WiFi: Is (slightly) more unstable, doesn't power your phone, and is slower in throughput. It has better compatibility though.

- USB tethering: Faster, more stable, charges your phone, but worse compatibility, you have to make sure the cable stays connected (which is a real issue with the default cable I got, but a new one works better for me), and you can't walk around with your phone easily.

- Bluetooth tethering: I guess it's slower than either of the above, but I've never really tried. I always have compatibility issues with BrokenTooth while WiFi works perfectly fine. (Hmm, if BlueTooth is BrokenTooth, then is WiFi Wirking Fine? Nevermind.)

Note that "tethering" nowadays is almost synonymous with "connecting your laptop to mobile internet", but in fact the definition is just connecting one device to another (sic, Wikipedia). You can disable mobile network and enable tethering, to disallow your connected device 3g access, while still allowing connectivity between the two.

Now that we're connected, you need to have a VNC and/or SSH client on your pc. I'll call it a pc, regardless of whether you actually connected a laptop or something else.
I recommend the RealVNC client and PuTTY for Windows. PuTTY is really one of the best for ssh, but for VNC there are probably many that are equally good, I just happen to know this one.
If you have a Linux pc, it's simple as apt-get install vnc4client (ssh is already installed by default).

To connect to your phone, you need to know its IP address. For me the default for WiFi tethering was 192.168.43.1 and USB tethering 192.168.42.129. If you use something else and need to look it up, use ip addr on the command line.

So there you have it, connected to your own, mobile pocket computer. It's not very fast, but you have your own files, settings, and OS! And with 2GB RAM, a quadcore CPU, 42GB storage and around 30MB/s sdcard throughput, it's reasonably speedy and even approaches the speed of my laptop/netbook. At least it's faster than my server (on which this website runs).

LAN-party!
In need of network equipment to throw together a LAN-party at school?
Look no further!

WiFi hotspots can connect up to 10 devices if I recall correctly. You probably average around 54 or 72mbps for each client, though the actual throughput is probably much lower (still a few megabits, which is usually plenty fast for games). The person that's hosting the server will need more upload, so the solution: USB tethering is a dedicated, wired connection with a link speed of nearly 480mbps. That'll do :)

But oh no, you've just tried it and found that the networks are separated. You can't connect USB tethered devices to wireless ones! Thou needest not fear. This is Linux we're talking about. We've got iptables!
I put this handy script in my home directory by now:
cat > switch.sh[ENTER]
iptables -A FORWARD -i wlan0 -o rndis0 -j ACCEPT[ENTER]
iptables -A FORWARD -i rndis0 -o wlan0 -j ACCEPT[ENTER]
[CTRL+C]
chmod +x switch.sh[ENTER]
./switch.sh[ENTER]


Now your phone just turned itself into a network switch, just like that. Try this with Windows! You know, that operating system that you either use illegally or pay top dollar for ;)

If the device names on your phone differ from wlan0 and rndis0, you can figure them out with ip addr once again.

Removing some annoyances
Having to type your password every time that you log in is pretty annoying. There are some things to find on the web on how to fix it, but they aren't really all that clear. Here are step by step instructions:

1. Open connectbot
2. Press options (the physical button on your phone)
3. Choose "Manage Pubkeys"
4. Press options again
5. Choose Generate
6. Give the thing a name. The letter A will do.
7. I used a 768-bit key because it's (very slightly) faster than anything stronger, and we're using it only to connect to localhost anyway. The type was RSA, I'm not sure if there is any reason to use DSA.

8. Leave the password blank.
9. Load it on start, but don't confirm before use.
10. Hit generate and touch the screen some. I do not know how this works, but I don't really trust it. It doesn't really matter now, but if you ever want to generate a secure key, I'd recommend taking random words or digits (truly random ones, for example from random.org if possible) and pretending you're writing those down on the screen.

11. Long-touch the key you just generated, and choose "Load into memory".
12. Long-touch the key you just generated, and choose "Copy public key".
13. Press the back button (on your phone) and connect again, entering the password if needed.

14. Enter this:
cat >> ~/.ssh/authorized_keys
Protip: maybe you can autocomplete the long filename by typing a Tab behind the "a" of "authorized_keys".
Also mind the tilde (~) before the /.ssh.

15. Then do as I say: Press enter; then the options key on your phone; choose "paste"; press enter again; and finally press ctrl+c.

16. The prompt should return. This means that you get "root@localhost ~ #" again, or whatever it is you see right after connecting, when it's ready for you to enter commands. If it doesn't, you might have mistyped ctrl+c. If you messed something up after step 14, just make the file (~/.ssh/authorized_keys) empty and try again.

17. Disconnect (options -> disconnect).

18. Connect.

19. Joy!

20. You didn't have to enter your password anymore :D

If it's not working, make sure the key pasted correctly, and it's your public key (not the private one). It should be a long text of letters and digits. Also make sure that in the Manage Pubkeys menu, the generated key shows as green, else it will not be used. When it's not green, just tap it to enabled it. Or long-press it and choose 'Load key on start' to load it whenever you start the app. If it's still not working, make sure you got the correct file (~/.ssh/authorized_keys).

In any case, you can always connect using the password. No worries of loosing access!

Ever had your connection getting lost?
Let's get rid of that annoyance, too. It happens when you loose wifi signal, or turn 3g off or something. I'm not entirely sure why this is, it should just use the localhost adapter. In any case, here's a quick trick when you want to avoid this from happening. On the command line, type:
screen bash

It doesn't look like it does much, but try entering some command that runs for a while. Like "ping localhost" will keep running indefinitely. Then disconnect from ssh (or loose your connection some other way) and open it again. It's gone! Or is it?
screen -r

There it is, still happily pinging! :)

You can have more than one screen session, and reattach using screen -r N, where N is a number. It will tell you which are available when you have multiple. You'll probably figure this out on your own.

Disconnecting from the screen session without disconnecting from ssh can be done by pressing ctrl+A, then the D key. Don't press ctrl+d(!), just press ctrl+a and then after that the D. If you accidentally type ctrl+S (I get that a lot), your ssh connection will freeze. Type ctrl+Q to resume it again.

For more info on screen, see its manual (command: man screen). If you get an error when you try to use screen, type: apt-get install screen to install it.

Have fun!

Have fun tinkering with the system. That's what I mostly use it for; just playing with it and trying to expand the possibilities. Make it do things it was never designed to. Attach a mouse and keyboard, or setup a JBOD/RAID-like system with sdcards. Write about it and post in the comments (I'll mention you in this post), or submit it to Hackernews. But above all, have fun with it!

I don't promise to know everything, but I'm open to any questions :)

Good luck!
lucb1e.com
Another post tagged 'software': To curl|bash or not to curl|bash

Look for more posts tagged apps, software or tutorials.

Previous post - Next post