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?

Upgrading Fedora 8 to Fedora 9

Saturday, May 17th, 2008

It’s time for another Fedora upgrade. Whilst the installations aren’t necessarily getting any better – fixing them is becoming a bit faster, because the problems are pretty similar every time and these notes I’m keeping are helping.

  1. System won”t boot. Met with a grub> prompt, and no menu
    • Boot the system from the DVD again, and select “rescue an install system”.
    • ‘chroot /mnt/sysimage/’
    • “nano /boot/grub/grub.conf”
    • Change this line, “root (hd2,0)” to root “(hd0,0)”. (I’m not sure why Grub seems incapable of finding the right hard drive to boot from each time… It doesn’t change.)
    • Uncomment the line at the top #boot
  2. Cyrus imapd won”t start. Complaining of libcrypto.so.6 library missing.
    • Upgrade Cyrus – “yum upgrade -y cyrus”
    • Restart the service – ‘service cyrus-imapd restart’
  3. Rss2imap no longer posts the messages into Cyrus IMAPD
    • Difficult to place the blame on this one. One turning on Rss2imap debug mode, I see i@m getting the error ‘Message contains bare newlines’, and although Rss2imap has posted the message. It’s been discarded by Cyrus. This is actually proper RFC behaviour for Cyrus – as message’s shouldn’t contain the broken ‘\n’ character, but rather ‘\r\n’. At the same time, I also think, ‘so what?’. Either way, the Cyrus behaviour has changed, or Mail::IMAPClient modules have.
    • So the fix… Edit Rss2imap\RSS2IMAPLIB\Rss2imap.pm, and add the bold text.
       my $message = ($headers . $body);
      $message =~ s/\n/\r\n/g;
      utf8::encode ( $message );

      and
       my $folder = $this->get_real_folder_name ($this->{'last-modified-folder'});
      $body =~ s/\n/\r\n/g;
      $this->{imap}->append_string ($folder, $body);

      You’ll notice we’ve replaced all the \n line endings with the more correct \r\n. Let’s just hope we don’t have to undo that later when the problem gets resolved elsewhere ;)

Remarkably, this was the first time the wireless card started up immediately after the upgrade, perhaps things are improving after all ;)

Linux server as a wireless bridge

Thursday, April 3rd, 2008

Since learning the XBox 360 doesn’t come with a wifi adapter, and that buying such a thing would add another 60 pounds to the cost of the console… I’ve been thinking of different ways to get an Internet connection from my high-speed wired network in the living room out to the router way out in the spare room. For many reasons, it’s just not practical for me to wire the whole flat.

Most of the solutions I came up with involved things like ‘spending money’, which I’m slightly adverse to do if I can do it with the existing kit, so these are the really basic steps to turn the existing linux box (with both wireless and wired cards) into a useful bridge. As usual, don’t be fooled into thinking this guide is here for anyone else, as much as it’s here for me when I need to rebuild the machine and I’ve forgotten it all. But feel free to leave a comment about how much warmer and fuzzier I’ve made your life through your use of these “instructions”.

The current configuration

Fedora 8 installation
Wireless Ethernet (ath0) card is connected as 192.168.0.8
Wired Ethernet (eth1) card is connected as 192.168.1.8
Named/bind/DNS server already configured and set-up to accept requests on 192.168.1.8
The Xbox will be wired, on 192.168.1.20

Setting up Fedora

Setup the forwarding rules

#Outgoing requests
iptables -t nat -A POSTROUTING -s 192.168.1.8/24 -o ath0 -j MASQUERADE
iptables -A FORWARD -s 192.168.1.0/24 -o ath0 -j ACCEPT
iptables -A FORWARD -d 192.168.1.0/24 -m state --state ESTABLISHED,RELATED -i eth1 -j ACCEPT
#Incoming requests (port forwarding)
iptables -t nat -A PREROUTING -p tcp -i ath0 -d 192.168.0.8 --dport 88 -j DNAT --to 192.168.1.20:88
iptables -t nat -A PREROUTING -p tcp -i ath0 -d 192.168.0.8 --dport 3074 -j DNAT --to 192.168.1.20:3074

Save your rules so they’re applied on start-up
sudo iptables-save > /etc/sysconfig/iptables

Enable ipv4 port forwarding
nano /etc/sysctl.conf
Change this line
net.ipv4.ip_forward = 0
to
net.ipv4.ip_forward = 1

The client machine / Xbox 360

Now on your client machine (note this will only have a wired connection).
IP Address: 192.168.1.20
Subnet mask: 255.255.255.0
Default Gateway: 192.168.1.8
DNS Server: 192.168.1.8

You’re going to need a DHCP server so that these settings are automatically assigned to the XBox 360. If don’t have one for your subnet already, you can set it up through dhcpd. You’ll probably want to do something about

Edit /etc/dhcpd.conf

ddns-update-style interim;
# HardWired
subnet 192.168.1.0 netmask 255.255.255.0 {
option routers 192.168.1.8;
option domain-name-servers 192.168.1.8;
authoritative;
range 192.168.1.10 192.168.1.20;
}
# Assign a static IP
host xbox {
hardware ethernet 00:45:40:10:FE:12;
fixed-address 192.168.1.20;
}

Then restart dhcpd
/etc/rc.d/init.d/dhcpd restart

Router

Remember those ports we forwarded on the server? You’ll also need to add port forwarding to your router, to forward the same ports (88, 3074) to the Fedora machine on 192.168.0.8.

And that’s really it. You should now be able to connect to the Internet without any troubles.

Upgrading Fedora 7 to Fedora 8

Sunday, November 11th, 2007

Another Fedora upgrade later, and my system is back up and running. Hopefully this version will fix some of the problems from the previous Fedora 6 to 7 upgrade. When will I ever learn?

  1. System won’t boot. GRUB complains of Error 15: File not found when attempting to load kernel.
    • Something bizarre happened to the IDE / SATA ordering in this release? Boot the system from the DVD again, and select ‘rescue an install system’.
    • Open up ‘nano /boot/grub/grub.conf’
    • Change this line, ‘root (hd1,0)’ to root ‘(hd0,0)’
  2. Wireless card won’t start.
    • Plug in Ethernet cable.
    • Make sure livna repository is enabled.
    • Upgrade the kernel to the latest: ‘yum -y upgrade kernel’
    • Reboot into new kernel.
    • Install madwifi drivers. Why does this have to be done for EVERY upgrade? ‘yum install madwifi’
    • Restart the network ‘/etc/rc.d/init.d/network restart’
  3. Cyrus imapd won’t start. Complaining of libdb-4.5 library missing.
    • Upgrade Cyrus, and make sure it installs the db4-utils package, this time. ‘yum upgrade -y cyrus’

One of these days I’ll move to a real Linux distribution, rather than the current hobbyist affair provided by Fedora. One of these days.

Myth TV on Fedora 7

Tuesday, June 19th, 2007

IMG_0037Well, I’ve decided this blog was getting far too technology-based over the last few entries. I’m in desperate need of another hobby, writing about my travels is all very well – but I only have 27 days holiday each year to spare for that sort of thing. Code is falling in from the ceiling and spilling out onto the posts. In my deepest moments of insanity I’ve starting writing about branding. Being able to claim the moral high ground in the past, I’ve now found myself getting into debates over logos, design and the public perception.

Well it has to change. Right now.. So this evening is all about the introduction to my Myth TV posts, which will serve as some sort of inadequate guide to getting an open-source media center up and running with digital tv recording, playback, tv guides as well as all the networkable, internet-ready, upgradeable, linux hardware hell you’d expect from such a project. Much more fun than going down the shop and buying a complete system. You can’t imagine.

Wait. Shit.

Okay, there will be code. There will certainly be text-only configuration files. Hopefully, I can get a llama in there somewhere… Hopefully.