Since I’ve been using Ubuntu, I’ve been blessed in comparison with Linux of even just a few years ago. Hotplugging cameras, disks, and almost any other USB device “just works”. As a general rule, I don’t have to worry about configuring it, installing drivers, or compiling my kernel. Still, there are some times I need to figure out how to do things at the lower level. Today, I wanted to figure out how to mount a usb drive automatically on a headless Linux box. Since Gnome isn’t running and no one is logged in, the usual methods don’t work. After some poking around, plugging and unplugging a disk several times, I have a working solution. The first thing to know is udev. udev is a user-space process that interacts with the Linux kernel to set up devices at boot time and, in the case of USB devices, as they are plugged in. On Ubuntu, you can drop a file in /etc/udev/rules.d to tell udev to execute commands when it sees a particular device. Setting up a USB drive is a two stage process. First, udev sees the disk and fires off a series of events you can hook into. Then, after you probe the disk, udev will fire off another list of events for the partitions. To intercept these and call a script to probe the drive and mount the partitions, create a file named /etc/udev/rules.d/50-usbdisk.rules (there is a README file in that directory that explains the naming convention). Into 50-usbdisk.rules put the following text:
ACTION=="add", DEVTYPE=="disk", RUN="/usr/local/bin/usb-add-disk" ACTION=="add", DEVTYPE=="partition", RUN="/usr/local/bin/usb-mount-partition" ACTION=="remove", DEVTYPE=="partition", RUN="/usr/local/bin/usb-unmount-partition"
This will cause udev to run usb-add-disk when a disk is plugged in, usb-mount-partition when it sees the partitions, and usb-umount-partition when the drive is unplugged. Next, create /usr/local/bin/usb-add-disk with the following contents:
#!/bin/sh -e # Maximum times to probe a disk MAX=30 logger "Probing ${DEVNAME} using parted" COUNT=0 ERR=1 while [ $COUNT -lt $MAX -a $ERR -ne 0 ]; do sleep 1 ERR=0 parted ${DEVNAME} || ERR=$? COUNT=$(($COUNT + 1)) done if [ $ERR -ne 0 ]; then logger "Couldn't probe $DEVNAME for media after $COUNT times" fi exit 0
This script will log a message that it is “Probing…” to syslog, probe a a disk up to MAX times (30 in this case), and, if it isn’t successful after 30 probes, log a message to syslog about its failure. A couple of notes. I’m not sure about the $((…)) syntax. It works in bash and dash, but I’m not sure it is a POSIX standard. Second, you may be wondering why I’m pausing and probing so many times. Usually this should work on the first try. Still the “disk” I was using in this case was my Blackberry‘s SD card. Since I’m a little paranoid, I have a password on my Blackberry. Every time I plug it into a computer, it prompts me for the password. Until I enter the password, Ubuntu can see the drive, but thinks the drive is empty. Once I enter the password, the disk’s partitions appear. (Ubuntu doesn’t appear to see the partitions until after parted probes the drive. If you know a better way to get the partions to show up besides probing the drive like this, please let me know.) At this point (and, on most usb disks, this is almost immediately since you don’t have to provide a password), udev will call usb-mount-partition. Let’s give udev something to run. In /usr/local/bin/usb-mount-partition put the following:
#!/bin/sh -e BASE=`basename $DEVNAME` if [ -x /media/$BASE ]; then logger "Can't mount usbdisk, /media/$BASE already exists" else mkdir -p /media/$BASE mount ${DEVNAME} /media/$BASE logger "Mounted usbdisk at /media/$BASE" fi exit 0
If everything works smoothly, the disk will now be mounted under /media. Whether the script is able to mount the disk or not, a message will be sent to syslog letting you know what happens. When you want to unmount the disk, you should run umount first and then remove the USB drive. That’s what you should do. But you might forget. If you do forget, then you’ll be left with a dead mount point. In that case, we have one more script to handle the clean up: usb-umount-partition. In /usr/local/bin/usb-umount-partition, put the following text:
#!/bin/sh -e BASE=`basename $DEVNAME` if [ -d /media/$BASE ]; then logger "Unmounting usbdisk from /media/$BASE" umount /media/$BASE || true rmdir /media/$BASE else logger "Couldn't find mount point for $DEVNAME" fi exit 0
This script will umount any dead mount points and remove the mountpoint that usb-mount-partition created. Make sure the scripts you just created in /usr/local/bin are executable (sudo chmod +x /usr/local/bin/usb-*)and that’s it: your headless Ubuntu box should now be automatically mount disks. If you want to signal some other program or run a script when the drive is mounted, you can add that to usb-mount-partition. A couple of notes on these scripts. First, it is a good idea to start your shell scripts with the -e flag. This will force you to handle any error conditions. For example, in usb-umount-partition, I run umount to unmount the drive. But suppose you already did this (as you should have). The umount command would return an error. Since I’m using the -e flag, I need to handle that, so I added || true. Handling errors like this really helps during testing to make sure errors don’t hide in your scripts. logger is extremely helpful for debugging. When I was testing my udev rule files, I found it helpful to pipe env to logger. I could just tail -f /var/log/messages and find out what environment my scripts were getting.
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
ubuntu |
One of the other developers for IntraHealth has been abusing PHP in every way possible. This time Carl may have gone too far. We’re putting together a “shelf-top” appliance for installations of iHRIS that don’t have a server room to rack-mount equipment. As much as possible, we want to make it easy to plug the box into your network and go with as little fiddling as possible. To that end, I found the Mini-Box M200 with an LCD panel that we could set up with a simple menuing and configuration system. In all the other projects we’ve been working on, we’ve used PHP because of, among other things, the low barrier to entry it offers compared to, say, Java. PHP has been irritating at times, but I think it may be worth it if it means that we can get more people involved in the client countries. When I first saw the LCD panel, my thought was “You know, it’d be nice to have an ncurses front-end for that thing.” Then you could test in terminal window and deploy it without changing your code. Carl had the same thought. So he began hacking out an ncurses-based menu system for the LCD display. And it’s written in PHP. I don’t think I’m going too far out on a limb here to say that this is the first PHP code designed to be used through an LCD panel. This only confirms my suspicion that Carl is a genius, of the evil, insane variety.
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
intrahealth,
php |
Today, I started using Virtual Machine Manager (VMM) to help me with my development. Previously, I had been using VirtualBox. Both are GPLed, but VirtualBox is Dual Licensed with a more “feature-full” version that isn’t GPL’d. The GPL’d version of VirtualBox doesn’t seem like it is really intended for hard-core use. So far I’ve really been liking VMM. It can bring up my instances at boot time and can run them headless. Nice. This will really help with the packaging work I’m doing right now for IntraHealth’s iHRIS Suite. Still, networking is a bear in both VirtualBox and VMM. I was able to use my TUN/TAP bridged network in VMM that I had set up before in VirtualBox. But only after I told HAL about the TAP interface. Here’s the deal: VMM uses HAL to discover the network interface. It then lets you use any of those interfaces that are bridged for your virtual machines. Alternatively, you can just use user-mode NAT, but that is less than ideal since ping doesn’t work and it is harder to test network apps or ssh into your virtual box. The problem: HAL only knows about physical network devices, not any TUN/TAP interfaces you have set up. Since my bridge was on a TUN/TAP interface, HAL didn’t know about it and VMM couldn’t use it. After a little poking around, I found the command line utility hal-device that would let me add the interface dynamically. Now, when VMM queries HAL, the interface shows up. (By the way, since HAL communicates over D-Bus, I was able to use Emacs’ D-Bus integration for debugging.) Here’s the script I wrote to add the TUN/TAP interface:
#!/bin/sh hal-device --add '/org/freedesktop/Hal/devices/net_tap0' <<EOF info.capabilities = {'net'} (string list) info.category = 'net' (string) info.product = 'Networking Interface' (string) info.subsystem = 'net' (string) info.udi = '/org/freedesktop/Hal/devices/net_tap0' (string) net.address = 'ea:08:21:d4:c8:90' (string) net.interface = 'tap0' (string) linux.subsystem = 'net' (string) linux.sysfs_path = '/sys/devices/virtual/net/tap0' (string) net.interface = 'tap0' (string) EOF
I hope to have something more generic that will work with HAL and Ubuntu’s networking soon so that any TUN/TAP interface is automatically recognised by HAL.
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
ubuntu |
I have this “thing” about owning my own data. I probably won’t be a real user of Tim O’Reilly’s Internet OS. I like running my own mail server — not relying on Google for Domains, not giving Google more and more data to feed into their advertising. But I’ve been using Google Reader. I’m not sure what Google is doing with that information, but, well, I can run my own feed reader on the desktop. So I switched to Liferea. Besides nice things like task-bar notifications, integration with del.icio.us — or, with a SMOP, my own GotNoBlog — it allows me to pull RSS feeds that require authentication so that I can read LiveJournal friends-only feeds now. I’ve managed to reclaim a little bit of my data. Hurrah!
|
Posted by
hexmode |
Categories:
programming | Tagged:
linux,
ubuntu |
Evidently, I’m one of the few, proud recipients of a free Whopper from Burger King. It wasn’t too hard to find 10 people on my “friends” list to dump, but now it seems like most of you won’t have that opportunity to get free food the same way. I gave the burger to my son, anyway. It is rare that I eat anything more than fries from a fast food joint.
|
Posted by
hexmode |
Categories:
Uncategorized |
In order to avoid joining the club of wimps (some people can rationalize anything), I went out today and yesterday, the two coldest days of the week, and did 30 miles. Part of it is the 6000 mile goal I set for myself. If I slack now, I’ll have to ride doubly hard later on. Part of it was that I skipped todays this week already. I had to get the miles in to make my weekly goal of 120miles. And I did! I made 130 miles for the week. Yay, me! The other part is just the fun of telling people “Oh yeah, I rode 30 miles in the freezing cold.” Something about the surprise that it generates makes it more fun. I won’t have the opportunity to provoke such reactions in the summer. But I think 30 miles is my limit. It takes me around 2 hours to ride that far and by the time I get back home, my toes are numb — even after I put on extra socks.
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
cycling |
In response to a NYT article about Mark Driscoll’s Mars Hill church, my friend Jim writes “I personally find it a bit of a mystery that some people find comfort and hope in that sort of theological framework”. By contrast, I can totally understand it. I understand it, but disagree with it. My experience as a Christian, and a little healthy doubt, has lead me to reject my one-time fascination for hard-core, predestined-from-the-womb Calvinism. But, while I’m not comfortable with a Calvinistic god who is completely arbitrary — one who has no real way of showing love — I doubt an individualized god who looks like a friendly neighbor who practices a “live-and-let-live” philosophy. It seems the Mars Hill congregation does not want a god who will smile on their imperfections, but what they’ve been offered, what they’ve found to fill their “God-shaped hole”, is indeed not anthropomorphic. It is true that anthropomorphizing God, making him like our tolerant neighbor, is dangerously wrong-headed. But just because we have an incomprehensible god does not mean that we have a view of the right one. A hint of what is so attractive about this “New Calvinism” can be found in Dostoevsky:
Taking freedom to mean the increase and prompt satisfaction of needs, they distort their own nature, for they generate many meaningless and foolish desires, habits, and the most absurd fancies in themselves. (source)
Mars Hill parishioners have pursued this false freedom and found it wanting. Naturally, they turn away from that. Of course, we are always in danger of following the wrong leader, but especially so when we feel weak and are offered something that looks unbending. By way of contrast, I offer this quote from Father Stephen. His whole post is an excellent defense of un-individual, Trinitarian Christianity, but this is quote seemed most relevant:
An excellent example of this occurred once in an inquirer’s class I was teaching before I was Orthodox (I was an Anglican priest). I was teaching a class on Christian morality and offered as authoritative the traditional teachings of the Christian faith in matters of sex and marriage, etc. One of the couples in the class seemed upset by my presentation and asked, “What right does the Church have to tell me how to live my life?” I admit that I was stunned by the question, if only because of its honesty. I gave them a short answer, “Because you are raising my children.” The complete answer has more depth, but I thought they might find it helpful to consider that the world included someone other than themselves.
(Took some more pics on my ride yesterday. Played with macro a bit, too.) Last night, I gave a talk at the Central PA Linux Users Group (CPLUG) that I called “Bringing Open Source to Africa”. While I could have made it more general and talked about the various Open Source projects that have grown out of or are centered around Africa — and perhaps this is a topic for another time — I focused on the projects that IntraHealth is doing and how we’ve created or used Open Source. The talk was followed by some good discussion and I got to talk about KnowledgeTree some as one of my friends there had heard of it and thought it would be useful, but was a little scared of setting it up. The biggest drawback was that, due to my poor planning, I didn’t get a set of slides done till just before the talk. I was lucky here because other people at IntraHealth have done so many talks so I was able to use their material to create my own presentation. (copy of my slides.)
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
intrahealth,
linux |
(Today, I took my camera with me on my ride. 30miles at 30degrees. Click the photo for a couple more.) After flirting with Google’s Picasa for Linux for managing my photos, I’m back to using f-spot. I like the timeline and that it doesn’t ask me to give a single “folder” for all the pictures I’m importing, but, instead, creates a directory structure so that my photos are sorted by date. Interestingly enough, it is actually better than Picasa for importing. Picasa won’t automatically rotate images when importing them from the camera where f-spot will. I still use Picasa for uploading pictures to be printed out (F-spot doesn’t support uploading to Wal-Mart), and I love the online Picasa’s ability to help recognise and tag people in photographs — 7000 faces in 10,000 photos are no fun to do by themselves, but when Google picks out the faces and suggests names, it becomes a little game to see how good it gets and to see which person Google thinks looks like each of my kids. (Yeah, I’m probably helping them improve their facial recognition software and they’ll end up selling that to the TSA, but … oh well.)
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
cycling,
linux,
photography |
Yesterday was a horrible day for riding. I thought of taking an hour at lunch on my beater bike, but I never managed to do more than think about it. This morning, though, I was up and out of the house, biking down 272 towards Lancaster at 6:00 AM. I’m falling behind on my goal. Today I should be able to catch up some.
|
Posted by
hexmode |
Categories:
Uncategorized | Tagged:
cycling,
winter |