James Little

The Asus Eee PC 1005PE & Ubuntu

View Comments

It seems there’s some mixed information about whether the wireless chip in this model works out-of-the-box with Ubuntu. I’ve tested with Karmic and the Lucid Beta (Netbook Remix versions) and for me it didn’t work. The chip in my model is the Atheros AR2427, which although not a new chip, seems to have an an unfamiliar hardware device ID of 002c, and so is not picked up by the ath9k wifi driver found in the kernel. The driver has been patched by the Linux Wireless developers but this patch has not worked its way into a kernel release yet, so the solution is to compile and install the latest bleeding edge wireless drivers, which is actually a pretty painless operation. It’s also a very safe operation, because the installer leaves the original kernel drivers intact and provides an uninstall script; but I have not needed to revert.

Read the rest of this entry »

Written by James

March 31st, 2010 at 2:13 pm

Posted in Linux

Tagged with , , ,

Call to undefined function imagetypes()

View Comments

I came across this PHP error after installing the Wordpress plugin Contact Form 7 and then using Really Simple CAPTCHA. The exact error given was:
Fatal error: Call to undefined function imagetypes() in ... on line 201.

After searching around for a while, I found that the GD library is required on the server, and (on linux at least) it’s a simple case of installing the library from the repository. So on Ubuntu/Debian, run:
sudo apt-get install php5-gd

and on Redhat/CentOS, etc. sudo yum install php-gd should do the trick. This will install several dependencies, such as the underlying C Library itself.

Obviously if you don’t have full control over your server, you will have to ask your hosting company or administrator to do this for you.

Written by James

October 22nd, 2009 at 8:56 am

Posted in Linux, PHP

Tagged with , ,

Macbook Uptime

View Comments

I knew there was a reason I bought a Mac! 47 days of uptime is pretty impressive for any non-server system, let alone a laptop which often rattles around in my bag (when cycling), comes on holiday, stays “asleep” for a whole weekend, etc. etc.

My Macbook's Uptime

My Macbook's Uptime

And I guess that’s the point really: the sleep function on Apple’s laptops “just works”. In this case I only had to reboot after 47 days for a system update. Of course, it’s generally easy to avoid reboots on most *nix systems, but I’m yet to find a Linux desktop distro/hardware combination where the sleep function reliably works. If anyone out there would like to correct me, that would be most appreciated :)

Also, who can beat 47 days on a Macbook (or MB Pro)?

Written by James

October 21st, 2009 at 3:46 pm

Posted in Hardware

Tagged with ,

Detect visitor’s country with PHP & MySQL

View Comments

Of course you don’t have to use PHP, or MySQL for that matter. But it’s my method of choice for most web apps, and it’s also a pretty common one. The general gist is to do a lookup on a database of geographical locations for IP addresses, having taken your visitor’s IP address from the PHP superglobal array $_SERVER. Yes there are caveats: the database is not 100% complete/accurate, and some ISPs (like AOL!) use proxies across different countries so the user will appear to come from somewhere other than their true country of origin. Boo hoo, let’s do it anyway; according to MaxMind, their free(!) GeoLite Country database is 99.3% accurate, and their licensed version, 99.8%.

The database is released monthly in CSV format, so I’ll have to import it into MySQL using mysqlimport, or LOAD DATA INFILE. I prefer the first option. Those of you that are MySQL fans probably know that there is a CSV storage engine available, but that’s only in version 5.1 which is still in Release Candidate stage, so I’ll stick to mysqlimport.

Download the GeoLite database from Maxmind, extract the CSV file and rename it to something more convenient; mysqlimport uses the filename for the name of the MySQL table it imports into:
root@jim-desktop:/home/jim/data# wget http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
root@jim-desktop:/home/jim/data# unzip GeoIPCountryCSV.zip
root@jim-desktop:/home/jim/data# mv GeoIPCountryWhois.csv geo_csv.csv

Before we import the data into MySQL we need to create a table for it to go into. The following DDL accurately describes the structure of the data. Obviously create a new database if you want; here I have one called geo_ip:
CREATE TABLE  `geo_ip`.`geo_csv` (
`start_ip` char(15) NOT NULL,
`end_ip` char(15) NOT NULL,
`start` int(10) unsigned NOT NULL,
`end` int(10) unsigned NOT NULL,
`cc` char(2) NOT NULL,
`cn` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

If you look at the data in the CSV file you’ll see it’s delimited by commas and the text is qualified by double quotes. With that in mind, we use the following statement to import the data:
root@jim-desktop:/home/jim/data# mysqlimport --fields-terminated-by=","  --fields-optionally-enclosed-by="\"" --lines-terminated-by="\n" geo_ip /home/jim/data/geo_csv.csv
geo_ip.geo_csv: Records: 102957  Deleted: 0  Skipped: 0  Warnings: 0

If the mysqlimport binary is not in your environment path, use locate to find it. If you don’t have it at all then use LOAD DATA INFILE.

So we now have the raw data imported into MySQL, but how do we use it? First let’s take a look at the data:
mysql> select * from geo_csv order by rand() limit 10;
+---------------+----------------+------------+------------+----+----------------+
| start_ip      | end_ip         | start      | end        | cc | cn             |
+---------------+----------------+------------+------------+----+----------------+
| 207.209.7.0   | 207.209.7.255  | 3486582528 | 3486582783 | AU | Australia      |
| 79.99.200.0   | 79.99.207.255  | 1331939328 | 1331941375 | BE | Belgium        |
| 217.27.192.0  | 217.27.207.255 | 3642474496 | 3642478591 | DE | Germany        |
| 194.59.180.0  | 194.59.180.255 | 3258692608 | 3258692863 | FR | France         |
| 81.16.160.0   | 81.16.175.255  | 1360044032 | 1360048127 | SE | Sweden         |
| 62.23.198.192 | 62.23.198.207  | 1041745600 | 1041745615 | GB | United Kingdom |
| 64.49.231.240 | 64.49.232.15   | 1077012464 | 1077012495 | US | United States  |
| 83.217.68.32  | 83.217.68.95   | 1406747680 | 1406747743 | BE | Belgium        |
| 91.193.20.0   | 91.193.23.255  | 1539380224 | 1539381247 | CH | Switzerland    |
| 194.37.249.0  | 194.37.249.255 | 3257268480 | 3257268735 | SE | Sweden         |
+---------------+----------------+------------+------------+----+----------------+
10 rows in set (0.22 sec)

The table is essentially a big list (~103k records) of IP ranges, given in both dot-decimal and decimal form. The decimal form is the most efficient to search on as the datatype int requires less memory than char, and with integers we can reliably make use of operators such as greater than, less than, BETWEEN, etc. Exactly how you will use the data will depend on your scenario. I began looking into this when I was working on a German website that wanted to know when a visitor was from Switzerland, so it could display prices in CHF rather than Euros. So in fact, all I needed to know was whether the visitor was Swiss, and if they were from any other country, they would see Euros. So the only columns I’ll need from the table are start and end, and all the rows belonging to Switzerland, or ‘CH’. So to make searches more efficient I’ll grab only the data I need and put it in a new table called ch_ip:
mysql> create table ch_ip as select start,end from geo_csv where cc='CH';
Query OK, 2023 rows affected (0.05 sec)
Records: 2023  Duplicates: 0  Warnings: 0

Great, that’s cut the data from nearly 103 thousand records to just over 2 thousand, and we’ve also lopped off four columns. I’ll now be searching on a table that’s 18K in size, rather than the original 5.3MB. Maybe you need every row of data in your scenario, but in many cases you only need a fraction. And in any case, you really don’t need the start_ip and end_ip columns (as you will see shortly). You could also split off the country names (cn column) into another table so that cc becomes a foreign key. Or you could ditch the country names completely and create an array of CC => CN in your application; there are only 239 unique CCs after all:
mysql> select count(distinct cc) from geo_csv;
+--------------------+
| count(distinct cc) |
+--------------------+
|                239 |
+--------------------+
1 row in set (0.05 sec)

So I have my table of 2,023 Swiss IP ranges. Now I need to grab a visitor’s IP address and convert it into decimal notation. For this we can use PHP’s built-in function ip2long. We use sprintf to ensure the result is always unsigned:
<?php
$ip_num = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
?>

Once we have $ip_num we can create our MySQL query:
$qry = "SELECT '' FROM ch_ip WHERE $ip_num BETWEEN start AND end";

All we need to know is whether the query returns > 0 rows. If it does, then the visitor is Swiss and we’ll set their locale appropriately. Obviously we don’t want to be performing this query on every page, so once it has been performed once for the visitor we’ll set a session variable. So the final code looks like this:
<?php
session_start();
if (!session_is_registered("locale")) { //check if the session variable has already been set first
$con = mysql_connect('localhost', 'geo_user', 'geo_password');
if ($con) {
$ip_num = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
mysql_select_db("geo_ip", $con);
$result = mysql_query( "SELECT '' FROM ch_ip WHERE $ip_num BETWEEN start AND end" );
$num_rows = mysql_num_rows($result);

if ($num_rows > 0) {
$_SESSION['locale'] = "ch";
}
else { $_SESSION['locale'] = "de"; }
}
else { $_SESSION['locale'] = "de"; //If no db connection can be made then set their locale to German }
}
?>

Written by James

June 7th, 2008 at 6:17 pm

Posted in MySQL, PHP

Tagged with , ,

mysqlslap for MySQL 5.0

View Comments

mysqlslap is a very useful tool for emulating client load, something that would normally be very difficult in the real world (until you go live!). The binary is bundled into the MySQL 5.1 releases (still at Release Candidate stage) but not 5.0, so the only option is to compile from 5.1 source and then you can use it with your 5.0 server installation.

Fortunately when compiling you can save some time by configuring with the --without-server option, which will compile just the client tools (mysqldump, mysqlbinlog, mysql CLI, etc.). The following worked for me on an installation of CentOS5 64-bit, on the same machine that runs my 5.0 server.

1. Download the MySQL 5.1 source code in compressed tar format (.tar.gz). Go to the download page, or do a wget (in my case from the Mirror Service from the University of Kent, UK). Version 5.1.23-rc was current at the time of writing:
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.23-rc.tar.gz/from/http://www.mirrorservice.org/sites/ftp.mysql.com/

2. Unpack the archive:
tar -xvvzf mysql-5.1.23-rc.tar.gz

3. Install required development packages and compile:
cd mysql-5.1.23-rc
yum install glibc gcc libtool ncurses-devel gcc-c++
./configure --without-server --disable-shared
make
make install

The ‘make’ stage will take some time. Why configure with --disable-shared? I wanted my binary to be portable so I could share amongst a group of similar-spec machines, and I can’t be sure that the shared libraries are all in the same location. See the MySQL Installation pages for more details on configuration options; you may want to make use of --libdir=... instead.

4. Copy/move the mysqlslap binary (it will be in /usr/local/bin by default) to wherever the rest of your v5.0 client binaries are. For example:
cp /usr/local/bin/mysqlslap /usr/local/mysql/bin/mysqlslap
5. Test it out! Run something like:
./mysqlslap --user=root --auto-generate-sql --concurrency=100 --iterations=5
as a quick test, and check out the MySQL Documentation for a more detailed use-guide.

Since compiling I have thrown the binary around various other Intel machines (and VMs) running CentOS, without any problems. Read the rest of this entry »

Written by James

April 2nd, 2008 at 3:18 pm

Posted in Linux, MySQL

Tagged with ,

mprime for Mac

View Comments

For those of you that don’t know what a Mersenne Prime is (most people I suspect), it’s a prime number of the form 2^n -1 where n is a natural number. The largest known prime number, 2^32,582,657 -1 is a Mersenne prime. Many, no doubt fascinating individuals (like me for example) like the idea of finding the first prime number that is > 10 million digits long because 1) there’s a $100,000 reward and 2) because it would be cool (sort of). Check out GIMPS and Wikipedia for more info.

I searched around for ages for a version of mprime (highly optimized software used to search for Mersenne Primes) compiled for Intel Macs and didn’t have much luck; for some reason it doesn’t seem to be linked from the GIMPS website. Eventually I found a random link to this page which contains all the software downloads. So if you want to contribute to the worldwide distributed search and you use a Mac, please download it. And you never know, you might get lucky!

For those of you that couldn’t care less about Mersenne primes (most people I suspect), but want to do some stress testing/benchmarking on their Mac, mprime is a very useful tool. Overclockers of the Windows variety will be familiar with the Prime95 app, a Windows GUI version of mprime.

Written by James

March 8th, 2008 at 9:50 pm

Posted in Mathematics

Tagged with ,

Installing CentOS 5 on a Dell R200

View Comments

This is the latest value rackmount offering from Dell. It’s a great spec for the price, but installation of CentOS 5 seems surprisingly difficult, thanks to the SATA DVD drive used in the unit. RHEL/CentOS doesn’t have the driver for the controller so can’t find the DVD during installation. I’m sure there are other workarounds (network install, USB DVD drive etc.), but eventually I got hold of a Redhat driver disk from Dell Support (it doesn’t seem to be on the Dell site btw).

I’ve uploaded the disk image here; just uncompress it and copy the .img file onto a USB key. At the installation command-prompt type ‘linux dd’ and then hit Enter. You’ll then be asked for the location of the driver disk image; just navigate to the USB disk, select the image, and you’re in business.

SATA driver disk
.zip version

Written by James

February 15th, 2008 at 7:51 pm

Posted in Hardware, Linux

Tagged with , ,

How to Access MySQL with an SSH Tunnel

View Comments

This is a particularly useful method for gaining access to your remote MySQL databases, such as those held on a web hosting account where the MySQL port may not be open. You can use this method to gain access to other services too (SMTP, IMAP, FTP), but in this post I’ll explain how I use it in combination with MySQL Query Browser to administrate my DBs with a GUI. You need to have SSH access to your remote server (normally over port 22) for this to work. My instructions are for Ubuntu but it’s easily transferred to other distros, Mac OS X, and Windows (just download an SSH client).

Run sudo apt-get install ssh if it isn’t installed already, which will install several SSH connectivity tools (more info here). Query Browser is an excellent tool with which to run queries, updates, create views and stored procedures, and loads more besides. Run sudo apt-get install mysql-query-browser. Now to create the SSH tunnel by using port forwarding; here’s how I access a MySQL instance on my local network:
james@james-laptop:~$ ssh -L 3307:localhost:3306 root@192.168.1.211
root@192.168.1.211's password:

Essentially this forwards all traffic on port 3307 on the local machine (james-laptop) to port 3306 on 192.168.1.211. The general format is
ssh -L localport:host:hostport
. Note that in my example I used localhost, but this is resolved after the connection has been made to 192.168.1.211 and so it refers to that IP address.

Effectively we can now access port 3306 (the default MySQL port) on 192.168.1.211 via port 3307 on james-laptop even though port 22 (the SSH port) is the only port open on 192.168.1.211. Keep the connection open (i.e. don’t close the terminal) and open Query Browser. In the connection dialogue set the hostname to 127.0.0.1 and the port to 3307 and enter a username/password as required. Hit connect and you should see a graphical representation of your database(s). Note that in *nix OSs (including Mac OS X) you must use 127.0.0.1 rather than ‘localhost’ or the connection will be made via a named pipe rather than TCP.

Written by James

September 10th, 2007 at 10:18 pm

Posted in Linux, MySQL

Tagged with , ,

Installing Ubuntu Feisty on Santa Rosa

View Comments

If you’ve recently bought a laptop with an Intel Core 2 Duo CPU, the chances are it’s running on the Santa Rosa platform. Having wiped off the unfortunate Windows installation, you’ll want to install some flavour of linux, such as Ubuntu Feisty. Unfortunately the installation process is not as smooth as you might as expect. In fact, it prooved so troublesome for me that in the end I decided that upgrading to Gutsy would be a better idea. But first I’ll talk you through how you can go about getting Feisty installed:

When booting from the Feisty Live CD, the first problem you’ll encounter will probably be something like:
/bin/sh: can't access tty: job control turned off
(initramfs)

and the OS will not continue to load. I’m not sure on the cause of this, but to get round it hit F6 at the boot menu and append break=top to the list of options. You should then see a slightly different error message, and underneath it you should type:
modprobe piix
exit

The LiveCD should then continue to load the OS. After a minute or so you’ll probably see a lovely blue screen with a “Failed to start the X server…” error message. Hit Yes or No (whatever you fancy) and you’ll be dumped at the command line where you should type:
sudo dpkg-reconfigure xserver-xorg

Remember the sudo bit or you’ll be reminded that you are not a superuser; no password is required. The Xorg configuration screen will appear and you’ll be asked (a lot) of questions. Keep the driver on “vesa” and continue through the questions, providing the default answers. At the bit where it asks you something about monitor config options and gives you the choice of “Simple”, “Medium” and “Advanced” I hit Medium and continued through. The vesa driver might not support your resolution if you have a widescreen aspect ratio (1280×800, 1440×900, etc.). Don’t worry about this for now and continue. Eventually you’ll be dumped back to the command line. Now type:
sudo /etc/init.d/gdm restart
and the Gnome desktop environment should start to load. From here you can perform your installation as required.

When you boot Feisty for the first time you’ll notice some more problems, such as the lack of an Intel graphics driver if your laptop uses the onboard Intel GMA965 chip, and non-functional sound & wireless. For me it was the lack of graphics driver that prompted me to upgrade to Gutsy, since the solution for Feisty involved upgrading the kernel to 2.6.22 anyway, which is the kernel version used in Gutsy (see http://ubuntuforums.org/showthread.php?t=494943 for more info). At the time of writing, Gutsy Tribe 5 has just been released and it seems to be stable. You can save yourself a lot of hassle where Santa Rosa is concerned by downloading the tribe 5 live CD which boots with no problems whatsoever for me, including desktop effects, wireless (my laptop has the 4965AGN chip) and sound. If you’ve already installed feisty you can upgrade to Gutsy by running “update-manager -d”.

Written by James

August 30th, 2007 at 9:40 pm

Posted in Linux

Tagged with , ,

How to Mount VMDK files in Linux

View Comments

There’s various tools for Windows that allow you to mount VMware images, which offers a convenient way to transfer files to/from the Virtual Machine. The solution in linux seems to be a little less well-known, but it is effectve nonetheless, and I find it preferable to using SSH/FTP/NFS etc. to transfer files.

First you’ll need to download a copy of VMware server, which is free. You may be using VMware player to run your virtual machines, but unfortunately it doesn’t include the tool needed to mount VMware images. It’s not a major problem though; just unpack the VMware server archive you just downloaded and copy the files “vmware-mount.pl” and “vmware-loop” from its “bin” directory to your /usr/bin directory (or whatever directory you have VMware Player installed). There’s no need to install VMware Server if you already have Player. If you have neither installed then obviously you will need to install one of them. Now navigate to that installation directory and follow my lead:

jim@jim-desktop:/usr/bin$ ./vmware-mount.pl

This script requires 3 (not 0) mandatory argument(s).

Usage: ./vmware-mount.pl
-p : Print the partition table
disk : Name of the Virtual Hard Disk file
or
disk : Name of the Virtual Hard Disk file
partition : Number of the partition
[-t type] : Partition type
[-o options]: Partition mount options(s)
mount-point : Directory where to mount the partition

Here we see the usage instructions for the vmware-mount script. I’ll run with the ‘-p’ option so I can see the partition structure of my image:

root@jim-desktop:/usr/bin# ./vmware-mount.pl -p /home/jim/'My Virtual Machines'/windows.vmdk'

--------------------------------------------
VMware for Linux - Virtual Hard Disk Mounter
Version: 1.0 build-44356
Copyright 1998 VMware, Inc. All rights reserved. -- VMware Confidential
--------------------------------------------

Nr Start Size Type Id Sytem
-- ---------- ---------- ---- -- ------------------------
1 63 2097088 BIOS 6 FAT16

That tells me that the partition number I’ll need is 1, and it’s the only partition available. So to mount the image I run the following:

root@jim-desktop:/usr/bin# mkdir /media/vmware-image
root@jim-desktop:/usr/bin# ./vmware-mount.pl /home/jim/'My Virtual Machines'/windows.vmdk 1 /media/vmware-image

The script warns you about untested support for kernels > 2.4 and has a whinge about network block devices, but type ‘Y’ on both counts to continue and the image will mount to your specified location (in this case, /media/vmware-image). You can now open another terminal window and explore the image. In Ubuntu w/ Gnome I’m able navigate via Nautilus, and it even creates a nice image on the desktop for me. You may find additional switches such as “-o ro” to make the image read-only will come in handy.

Written by James

August 29th, 2007 at 9:31 pm

Posted in Linux

Tagged with ,