Archive for the 'Uncategorized' Category

Auto running commands when plugging in usb drives with udev in Linux

Wednesday, January 20th, 2010

Backups of Backups

My backup strategy for my machines at home can be effectively described as “scatty, but thorough“.  Or at least until I actually have a major crash and need to recover that important file that was outside of my normal documents, code repositories, and archived folders.

I am one of those sorts of people that has backups everywhere.  Folders stacked away with old dusty (and probably now useless) dvds.  Old hard drives filled with duplicates and archived files, stacked up in the back of cupboards and flung next to jam jars.  SD cards, USB keys, backups to other drives, backups to the machines on the other side of my flat, backups off-site.  Backups of backups backed up during the last backup.

So I decided to add one more with an external hard drive that I could plug in every now and then.  But backups aren’t really the point of this post.

Linux has a really powerful device manager called udev which detects when things are plugged into your machine (including hard drives), which you can write rules against, and have commands automagically executed.  This is really cool for my new hard drive, which can now automatically start backing up without regular cron jobs checking to see if it’s plugged in or not.  This is not the same as autorun files, it relies on that hard drive being plugged into that machine.  So don’t start crying about all the security risks with autorun, please.

Here’s how you do it.

udevadm info -a -p  $(udevadm info -q path -n /dev/sdc)

You’ll get a whole bunch of output from that, including (among a lot of other output) a line that might look a little like this:

ATTR{serial}=="312581808"

There are a lot more you can use as well, but this will identify your device.

Now create a new file inside /etc/udev/rules.d/, such as /etc/udev/rules.d/81-usb-drive.rules

KERNEL=="sd?1", ATTRS{serial}=="312581808", SYMLINK+="backup-drive", RUN+="/bin/sh /home/user/scripts/backup-to-drive.sh"

Now every time you plug in that drive, that command is going to be executed (so include for example, the script that is going to run your backups).  Cool, eh?

I started thinking about other applications; syncing podcasts, ebooks and music are the obvious choices. Even as a crude simple method of executing commands on a box you don’t even usually login to.  Or you could use this technique to build yourself some poor man usb-based security.  Keep decryption keys on your thumb drive, and have it auto decrypt volumes on your machine when you plug in that particular drive.

Just don’t forget to keep a backup of the thumb drive, yeah?

Google Wave – My First Bot

Sunday, November 1st, 2009

Last week, I went to the Google Technology User Group in London, which was all on the subject of Google Wave.  Lars and Steph, of Google Wave video, and Google Maps fame gave an excellent talk on Wave, how it was doing, where it was going, and the challenges they’re still facing in getting Wave ready for prime time and a public release.

I’ve had a developer account for some time now, and the talk finally got me motivated into messing around with more of the APIs.  So I created a bot.  Then I created another one.  Because the first one didn’t do anything.

1.  Get Eclipse

So far, the choices for creating Google bots are rather limited because there are rules that they must be hosted on AppEngine (for now).   So first off, get Eclipse – because it makes the entire process of doing that incredibly easy.  You can download Eclipse here.

2.  Get the AppEngine SDK.

Once you’ve got Eclipse installed and running, go to Help -> Install New Software.  Enter this URL to get at the Google AppEngine SDK.

http://dl.google.com/eclipse/plugin/3.5

3.  Create your project.

Go to File->New, and select Web Application Project.  If you don’t have that option, something’s gone wrong with your SDK download, so check step 2.

Uncheck the Googe Web Toolkit, we don’t need that.  But otherwise fill out the Project Name and Package as you see fit.

4.  Add the libraries from the Wave extensions SDK

Download wave-robots-api.jar, json.jar, and jsonrpc.jar and drop those into your project under war/WEB-INF/lib/.

Once you’ve done that, select File->Refresh, then Project->Properties from the main menu, and select Java Build Path.  Click Libraries, and Add JARs, to select the three that you’ve just added.

5.  Write your servlet class.

This is where the bulk of your bot logic (or lack of it), goes.

package helloworld;
import com.google.wave.api.*;
public class HelloWorldServlet extends AbstractRobotServlet {
	public void processEvents(RobotMessageBundle bundle) {
		for (Event e: bundle.getEvents()) {
			if (e.getType() == EventType.BLIP_SUBMITTED) {
			Blip blip = e.getBlip().createChild();
			TextView textView = blip.getDocument();
			textView.append("Hello.  Are you the world?");
			break;
			}
		}
	}
}

6. Add a servlet mapping.

Edit the file, war/WEB-INF/web.xml and add a servet-mapping just below the one you have already.

<servlet-mapping>
<servlet-name>HelloWorld<servlet-name>
<url-pattern>/_wave/robot/jsonrpc</url-pattern>
</servlet-mapping>

7. Add a capabilities file.

Add a folder under war/_wave. Create a file under that called capabilities.xml. This tells Wave which events your robot is going to respond to. In our case, we’re going to respond whenever a blip is saved (blip_submitted)./ There is a full list of capabilities in the full api docs.

<?xml version="1.0" encoding="utf-8"?>
<w:robot xmlns:w="http://wave.google.com/extensions/robots/1.0">
<w:capabilities>
<w:capability name="BLIP_SUBMITTED" content="true" />
</w:capabilities>
<w:version>3</w:version>
</w:robot>

8.  Get an AppEngine account.

That kind of finishes off the Wave-bot.  So you’ll need an AppEngine account to continue.  So go ahead and sign up, and create an application.  Incidently, I couldn’t find my created apps since I have a Google Apps / Domain account.  if that’s the case for you as well, you can find your apps list at http://appengine.google.com/a/<domain>.  Not sure why Google don’t detect that, but there we go.

9.  Deploy to AppEngine

Click the friendly little icon ae_deploy_button from your Eclipse toolbar, and enter your details to deploy you new robot.  Try not to scream “fly my pretties” as you do so.  I dare you.  Make sure you click the App Engine Project Settings button, and provide you Application ID, the same as you created in step 8.

10.  Add your app to a wave

The address of your robot will be applicationid@appspot.com, and you can add it just like any other robot.

You should now be able to interact with your bot.

bot

And if you’d like to see my first bot in action – please drop in and say hello by adding it to your wave: insulteveryone@appspot.com.

Now. World peace.  Where did I leave that file?

Google Wave – Embedding for the first time

Thursday, October 22nd, 2009

This is the first of the embedded Waves using the embed api (embeddy@appspot.com).  If you’re in the Wave, you can add comments to this one and see them both here, and on my blog.  This really demonstrates a lot of the appeal of Wave for me.  Although it’s a bit chicken-and-egg, as it’s still in a relatively closed beta.  So if you can’t see anything below, and you don’t have a Wave account.  Sorry :)

If you want to do the same sort of thing, just add embeddy@appspot.com to any Wave, and you can get your hands on some embed code, and your Wave ID.  Cool, eh?

Germany Photo Story

Saturday, October 10th, 2009

Having taken the shiny new Nikon D90 with me on my trip to Berlin and Hamburg, I’ve come back with just over 1,000 photos. A large amount, even for me. So in a departure from the norm, I’ve chosen to only upload a selection of them to the public view of Blakepics.


As usual, you can also find a full quality version of this video on Blakepics. The music for this one is from the soundtrack of Die Riesen Kommen entitled "Decollage", the show from Royal De Luxe put on in Berlin to prompt this trip.

Fare thee well, giants, fare thee well.

Thursday, October 8th, 2009
Reuinted giants resting on their way home

Reunited giants resting on their way home

Today was the finale of Die Riesen Kommen, which saw them both waking up from the Brandenburg Gate. Obviously they did what I wished I had and had a sunday lie-in, so didn’t wake up until 90 minutes after the alarm clock was supposed to go off. It did mean I got a spot right down the front for the first part of the show and to join in chants with hundreds of Germans of which I *hope* was something along the lines of “why are we waiting?”, rather than “The French are almost as lazy as the Brits”.

Once they did get moving, playing leapfrog with the rest of the crowds seemed to work very well. The Elephant and the Sultan had crowds of people following them through the London streets. In Berlin, with a lot more space and a lot more shortcuts, it was much easier to run ahead and meet the giants as they passed the next time.

All along the parade, giant symbols crashed in front of the giants, and a cannon, yes, a frickin’ mail cannon propelled letters the little girl had brought with her on her journey high into the sky to scatter onto the crowd below. Right now, that’s a much better service than we get with Royal Mail. We just need a giant cannon mounted on the front of the HMS Belfast, and we can launch all the undelivered mail across South Bank.

Children frantically darted in and out of the crowd running back to their parents with letters grasped tightly in their hands. I suspected I wasn’t going to see what was written on the letters. I had to get mean. Knock a few kids heads together, steal the mail from their pockets, or generally intercept and intimidate them before they got back to their parents.

I waited patiently instead, and now I have a sizeable stack of said mail. Except it’s all in German. I should have seen that coming.

Still, it kept everyone entertained in between following the giants around. Reunited as they were, the giants crossed the bridge towards the new Hauptbahnof station, and boarded a boat where they laid down for a nap together as they were transported out of Berlin with people clapping and cheering all the way along the river.

I caught only a small final glimpse of the giants before they left, as I was crossing the river to the East Berlin Wall Gallery, I saw their boat in the distance. They’d since been covered up, and were well on their journey home as they passed the bridge underneath me.

Well done Royal De Luxe, for an another excellent show, and Berlin for being such a fantastic host. Royal De Luxe and their giants have undoubtedly succeeded in bringing another city together under the banner of imagination. Something which may be especially poignant for Berlin, over the weekend of German unity.