Tuesday, September 15, 2009

Using Thinkpad Dock Events

I've been using this for a while, but I always seem to forget to post up my coding stuff, so I thought I'd go ahead and throw this one up now.

If you don't know, I use a Thinkpad T61 laptop with one of their docks, into which I plug my external harddrives and mouse, etc. The nice thing about this is that it's really easy to just take out my laptop when I need it for class -- I don't have to unplug the ethernet cable, the usb drives, etc. one by one. The downside is that when I take it out, all of the entries in the Mediatomb database disappear! (I mentioned this in an earlier post when I discussed my Mediatomb config file.) Since I have the directories saved in the config file for Mediatomb, I need only restart the server to have it add them back in. But, it got to be a pain to call up a terminal and reset the server, so I decided to look into automation. Check out what I found after the break.

I found a great resource here. That tells you basically all you need to know, but I'll go over it really quick here as well.

1. First, open a terminal or a file browser and go to /etc/udev/. If you have a rules.d folder inside, go there. Next, create a file like "55-thinkpad-t61.rules". I'm not sure if the filename matters, but since I have a t61, that's what I used. In that file, paste the following:

# run a script on dock/undock
KERNEL=="dock.0", ATTR{docked}=="1", RUN+="/home/dhleong/code/ondock 1"
KERNEL=="dock.0", ATTR{docked}=="0", RUN+="/home/dhleong/code/ondock 0"

Note that you should replace your own path in place of /home/dhleong/code. That's just where I decided to put my file.

Next, create the file you pointed to in the RUN+="" section above. For me, this would be /home/dhleong/code/ondock

Into that file, paste the following:

Notice the while loop at line 15. There, I'm waiting for one of my externals to get re-mounted. After all, Mediatomb won't be able to find the files if the harddrive isn't connected! You should replace this with the names of one of your harddrives, if you want to use this script for that.

Last, is the line 21. This is a script I wrote to restart Mediatomb. It's quite simple, and I've replicated it here:

That's it! Just don't forget to chmod +x all the scripts, so that they're executable.

No comments:

Post a Comment