Categories
PC Nerding

Android, Linux and Chroot

I felt the hacky desire to install a chrooted Linux under my Android phone, I’ll probably end up not using it, but I just like doing this king of stuff.
Note this guide requires a rooted phone, loop devices support and BusyBox (I have a Samsung Galaxy S3 with rooted stock JB 4.1.1). Using apps with BusyBox included (like SSHDroid or Better Terminal Emulator Pro) MAY save you the need of installing BusyBox on your phone. I said ‘may’ because I did not test them.
I followed this guide from Sergio Rubio to install my Arch Linux and there’s no reason to write again from scratch a good guide, so your first step is following his guide. What Mr. Rubio’s guide lacks (on purpose) was a faster way to actually mount/umount the chrooted system and get access to Arch shell, think my guide as “Part 2” for his one.

The author installed it in /sdcard/archlinux folder, my guide will assume you used the same directory.
TIP: I suggest you to increase the archlinux.img file size, 750MB may be too small for installing other packets (eg. Mono takes up to 200MB). I personally made it 1,5GB.

After installing Arch, I suggest rebooting your phone. Android will forget about the mounted partitions and we can start from scratch.

Mounting and umounting five partitions take time, a lot if it on a super small android touch keyboard, so we will create two scripts to make life easier.
Using a file manager on Android or connecting it to a pc, create a two files in your sdcard folder
The first is archmount.sh, write this stuff in
#!/bin/bash
mount -v /sdcard/archlinux.img /sdcard/archlinux
mount -v -o bind /dev/ /sdcard/archlinux/dev/
mount -v -t proc proc /sdcard/archlinux/proc/
mount -v -t sysfs sysfs /sdcard/archlinux/sys/
mount -v -t devpts devpts /sdcard/archlinux/dev/pts/
cp /etc/resolv.conf /sdcard/archlinux/etc/resolv.conf

and the second is archumount.sh
#!/bin/bash
umount -v /sdcard/archlinux/dev/pts/
umount -v /sdcard/archlinux/sys/
umount -v /sdcard/archlinux/proc/
umount -v /sdcard/archlinux/dev/
umount -v /sdcard/archlinux

Those two scripts will take care of mounting and umounting the partitions, just run them on your phone using Android Terminal Emulator.
su sh /sdcard/archlinux/archmount.sh
#or
su sh /sdcard/archlinux/archumount.sh

After mounting you can get access to an Arch shell by running
su chroot /sdcard/archlinux/ /bin/bash

But unless you are using an hardware keyboard, those functions are still too long to type, we will shorten them using shell functions. In Android Terminal Emulator go to “Preferences” -> “Initial Command” and add those lines and restart the app.
function archmount() { su sh /sdcard/archlinux/archmount.sh ; }
function archumount() { su su sh /sdcard/archlinux/archumount.sh ; }
function archsh() { su chroot /sdcard/archlinux/ /bin/bash ; }

Now everytime you want to use Arch, mount partitions by running archmount, then type archsh to open an Arch shell. Now you have full access to a command line linux. When you are ready to quit type exit to return to Android terminal and type archumount to unmount Arch’s partitions.
If you want to use Arch later without having to remount it, just skip archumount and only run archsh whenever you need it.

A final note:
For some reason my Arch linux had an empty PATH variable, causing “command not found” error with almost every command ran in Arch’s shell. From Arch’s shell run export PATH=/bin:/usr/bin
to solve the issue. I recommend adding that same line it to .bashrc file, to access it run nano /.bashrc
After editing Press CTRL + O to save and CTRL + X to exit. Use Hacker’s Keyboard for your CTRL needs.

Categories
PC Nerding

Raspberry PI X11 forwarding with Cygwin

After my little Raspberry PI Mono installation accident, that fucked up everything with Mono, I decided to reinstall Raspbian Wheezy. I know a lot of people that love touchy and clicky interfaces, the tablet market rise reflect this. I personally use my RPi through command line, SSH it over LAN, because I one Desktop environment (my Windows machine) is more than enough for me.
But after a couple mails with a friend trying to view his PI’s lxsession on his Windows machine and explaining him my setup using Cygwin only, he told me there’s no tutorial on this. Nothing new or super cool, just another way.
This guide should work ok with any linux environment, not only RPIs, provide you properly set ssh configurations.

The first thing to do is downloading Cygwin installer, be sure to add xorg, xint and openssh packages while installing. There may be some other needed packages, if you get somenthing like a “command not found”, you have to add that package too.

Now open a Cygwin shell with its nice desktop shortcut and type:
cygstart xwin
cygstart tells Cygwin to run a command and give you back your shell control. Removes the need for two shells (one for launching the Xserver and one for SSH connection). It can be omitted.
xwin Launches the XWin application, which is our Xserver
This is usually enough, but as default setting, xwins create a window big as my current resolution. I prefer using this command:
cygstart xwin -screen 0 1440x900@1 -wgl
Fast explanation:
-screen 0 1440×900@1 tell xwin to create the remote screen window number 0, with a resolution of 1440×900 at monitor 1. Use this parameter as this, modifying only the resolution part. See Note 1 for better explanation.
-wgl will enable OpenGL accelerated graphics on the remote screen window. It is not mandatory and if not enabled the remote screen window will use software rendering.

After that we must make sure that Cywin is aware of this remote display, it can be done by setting and exporting the environment variable DISPLAY.
The newly created window will have name like: Cygwin/X:0.0, make sure to set this variable to whatever is written after the X, like
export DISPLAY=:0.0

Now, using Cygwin, SSH to your PI
ssh -Y pi@address
and after logon, start you windows manager:
lxsession
This should show your Raspberry PI desktop inside your XWin window.

Final notes:
If you want to do all of this stuff at cygwin’s startup, just add the lines
cygstart xwin #+your parameters
export DISPLAY=:0.0

at the end of cygwin\home\user\.bashrc.

Note 1:
See XWin manual for possible parameters.
-screen 0 1440×900@1 explanation:
0 Create windows number 0. It’s like an ID to identify which windows is this. If you want to use multiple remote screens, for each new screen you have to increment this value by one.
1440×900 Is pretty self explanatory. Sets the PI’s max resolution to this size.
@1 Tell XWin on which (real) monitor you want the window to appear. So in case of a multiple monitor setup 1 is the main monitor, while 2, 3, 4, …, are the other monitors. If you have a single monitor this value is always 1.

Categories
PC Nerding

Shortening urls using htaccess

I was looking for a solution for shortening urls for both blog and forums on my indie website Acid.vg. I know there are a lot of shortening services on the web, for both hosted and self-hosted. Both have pros and cons. But the real questions were: Do I really want to send my data to someone else? Do I really want to use more hosting and database space for a self hosted app? Do I really want to keep this app updated?
The answer to all those questions was: NO. Acid.vg is a pretty short domain name, not 4 chars long only like Twitter’s t.co, but short enough to keep his urls short with a little hack and the power of Apache’s mod_rewrite. This will still require a self hosted environment with .htaccess support and an installed cms that identifies its content by IDs (more details as you read).

Acid.vg have a blog cms (WordPress, installed in root / folder) and a forum cms (phpBB3, installed in /forum/ folder), the tutorial splits in two parts, covering both. The same procedure may be adapted for any other cms.

WordPress

On default settings WordPress access his contents (blog post, page, media) through their IDs like mysite.com/?p=ID. Even with seo friendly permalinks on, WordPress still allow to access posts by using ID, we will take advantage from this feature for our hack.
Acid.vg uses Twenty Twelve child theme, this tutorial will assume you are using Twenty Twelve (or a child) but the procedure may be similar with other themes too.

First of we have to decide where to put this shortened link, in my case I went for the entry-meta footer that is shown after each post. This is done by twentytwelve_entry_meta() function found in theme’s functions.php, you can edit it from here (losing changes in case of theme update) or you can copy it in your child theme’s functions.php. An hook filter here would be cooler, but look like its the only way if you want the shorter link to show here.
After copying the whole stuff, add this single line right before the function’s trailing bracket:
printf('<a class="shorter-url" rel="nofollow" href="http://mysite.com/s/%s">Shorter Url</a>.', get_the_ID() );
This function will add the shortened link as http://mysite.com/s/%s, where %s is the post’s ID value we got from WordPress function get_the_ID(). (eg: http://mysite.com/s/1)

Done? Not yet. This only told WordPress how to shorten links using post IDs but it didn’t tell your hosting server how to handle those shortened urls.

Now open your favourite FTP program and navigate to your site’s root, here you will most likely find an .htaccess file, if not create it using your favourite text editor. Add this code to your file. If the file was not empty, make sure to put this code at top.
# BEGIN ShortUrls
<IfModule mod_rewrite.c>
RewriteEngine On
#---TEXT GOES HERE---
</IfModule>
# END ShortUrls

(I will not bother you telling what all this stuff is about, a quick google search will tell you everything.)

Now, delete the entire line were I put TEXT GOES HERE, and paste this stuff inside
RewriteCond %{REQUEST_URI} ^/s/$
RewriteRule ^s/$ http://www.mysite.com/ [L]
RewriteCond %{REQUEST_URI} ^/s/([0-9]+)$
RewriteRule ^s/([0-9]+)$ http://www.mysite.com/?p=$1 [L]

Let’s see what this code does: Read the line in pairs, basically they tell you if RewriteCond condition is met mod_rewrite will rewrite the url using the specified RewriteRule
Line #1 and #2:
Since the shortened url make use of the posts IDs, if this ID is missing (RewriteCond searches for “mysite.com/s/”), we only redirect the browser to the main site.
Line #3 and #4:
If the url ends with a number (eg: mysite.com/s/34), take the numerical part ([0-9]+) after /s/ and point the browser to mysite.com/?p=ID (eg: mysite.com/?p=34).

Now we’re done.

Will the site loose his fancy SEO permalinks?
Absolutely no! WordPress will then take care of loading the correct post and, if permalink are enabled, printing the correct (longer) permalink in the browser address bar.

But this method only shortens the links by 1 char compared to ?p=ID method! I know, mysite.com/?p=1 and mysite.com/s/1 are respectively 15 and 16 chars long. But this is the case only when wordpress is on root. When it is installed on /blog/ or, worse, /wordpress/ folder, the difference between mysite.com/s/1 and mysite.com/wordpress/?p=1 becomes obvious.
Read the phpBB3 part to better understand what I mean.

phpBB3

In my current setup PhpBB3 runs from a subfolder /forum/ and it shows post through a php page viewtopic.php?p=ID, making the full url very long mysite.com/forum/viewtopic.php?p=ID. Having this url short as mysite.com/f/ID would be awesome!

Like we did with WordPress we have to show those shortened ID somewhere in the forums. The best place to put this link is inside viewtopic_body.html, right before or after the topic title. In most themes you can find a line similar to this one
(Note: you don’t have to scan for the entire document, it is usually found in the first 5 lines)
<a href="{U_VIEW_TOPIC}">{TOPIC_TITLE}</a>

Next to this that you can paste something like:
<a class="shortlink" rel="nofollow" href="http://mysite.com/f/{S_TOPIC_ID}">Shorter Url</a>
This will make the post link much shorter, will not work on his own.
Get back to your .htaccess file and paste this code, right after the previous Rewrite codes.
RewriteCond %{REQUEST_URI} ^/f/$
RewriteRule ^f/$ http://www.mysite.com/forum/ [L]
RewriteCond %{REQUEST_URI} ^/f/([0-9]+)$
RewriteRule ^f/([0-9]+)$ http://www.mysite.com/forum/viewtopic.php?p=$1 [L]

Like previous codes
Line #1 and #2:
Will tell browsers to load the forum main page when the browser is trying to load a shortlink with no id.
Line #3 and #4:
Will send browsers to the correct link http://www.mysite.com/forum/viewtopic.php?p=ID.

Here you can see a big difference: mysite.com/forum/viewtopic.php?p=1 is 34 characters long, while mysite.com/f/1 is only 14, 20 characters less.

Do I have to use mysite.com/s/ and mysite.com/f/? No, you can use whatever you want like b/ or s-, just make sure your RewriteConds and RewriteRules use the correct regular expressions.