• Ubuntu: How to automatically launch scripts/software upon inserting USB devices

    Have you ever thought about performing a custom action or launch a specific software when plugging your favorite USB device on Ubuntu?

    USB Cable

    I’ve been investigating this possibility recently and I’ve found a quite satisfactory solution. The idea has born when reading this blog about how to read udev rules. Udev is the device manager introduced with Kernel 2.6.13 and it manages all the /dev devices dynamically. Using upstart rules, we can use specific files (rules) to tell to our OS that a specific action/script should be run when a device (or a specific device) is attached to the system. The files are

    So I thought that it would be good to launch a simple script when I attach my Palm Pre2 to my Ubuntu via USB and run a rsync that will maintain a local backup of my USB partition on the phone.

    Thanks to the work done at stackoverflow I was able to understand how to create the rules, tailoring them for my device and then use notify-send to have a graphical view of what the script is doing.

    First thing to do is to identify your device:

    launch from a terminal window the command: udevadm monitor –env

    This will monitor the ports of your machine and when you plug a device like an usb pen, you’ll see a few lines identifying different pieces of information.

    What you need to write down are: ID_SERIAL_SHORT, idVendor and product.

    For example our ID_SERIAL_SHORT can be something like: 156aab412740f93d04869cac6c0837229d48456e, the idVendor a small number like “8332” like the product “8045”.

    Now that we have this information, we can create our upstart rule by creating a file named like 91-backup.rules in /etc/udev/rules.d (please note that the file should start with two digits and having an higher number of the other files in the same directory, so you’ll be sure that it’s executed first).

    Mine looks like:

    SUBSYSTEM==”block”, ID_SERIAL_SHORT==156aab412740f93d04869cac6c0837229d48456e, NAME=”PALM PRE”, SYSFS{idVendor}=”8332″, SYSFS{product}=”8045″,

    RUN+=”/home/user/backup/copy.sh”

    Please note the last line that calls a specific script, but you can choose a software or anything else.

    In my case, the content of that “copy.sh” is:

    #!/bin/sh

    sleep 2 #wait for the system to mount the drive

     if [ -d /media/PALM PRE/ ] # if the drive has been mounted

    then

     su stefano -c “DISPLAY=”:0.0″ notify-send “Mount” “Palm_PRE2 ready” -t 2000 -i “/home/user/backup/pre.jpeg”” #show on the OSD a specific message with icon

     rsync -ru /media/PALM PRE/ /home/user/backup/PRE/ #do a backup of my external media using rsync

            su stefano -c “DISPLAY=”:0.0″ notify-send “Palm_PRE2” “Syncronization_complete” -t 2000 -i “/home/user/backup/images.jpeg”” #reports that the backup is completed on the OSD

    fi

    I hope that this information will help you in doing something similar (and useful!) on your system. Enjoy!

  • Windows XP / Vista / 7: missing, lost or not found DVD drive

    One of the most common issues using Windows 7 is that sometimes it seems that your CD/DVD drives disappears and there is no way to have them back!

    This not only used to happen on Vista previously, but it was happening in Windows XP as well…

     

    Missing CD or DVD

    So, how do you fix this in XP?

    Easy, you open the Registry Editor and in the navigation pane, locate and then click the following registry subkey:

    HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlClass{4D36E965-E325-11CE-BFC1-08002BE10318}

    In the right pane, you should have UpperFilters. Click on it. You may also see an UpperFilters.bak registry entry but you do not have to remove it. Click UpperFilters only. On the Edit menu, click Delete and confirm the deletion by choosing “Yes“. If you do not see the UpperFilters registry entry, you still might have to remove the LowerFilters registry entry. Again, click on Edit and delete it.

    At the end you can close the Registry Editor and reboot the machine. Problem solved…

    How do you fix that in Vista? Well, in the same way… And how do you fix it in Windows 7? Mmm, again, in the same way.

    So why is this happening? Is something that has not been properly fixed by Microsoft?

    To be honest these keys are not normally created on a standard Windows 7 machines, but are actually related to Filter Drivers that allow the existing Microsoft CD/DVD drivers to use external modules to perform specific operations. Typical example is the filter driver installed by burning software. This kind of software needs to perform special operations that are not covered by the standard Microsoft drivers and that’s why they need to extend those functionalities.

    In fact by deleting those keys you may then notice that some function of your 3rd party burning software is not performing as expected or that is not working at all. Reinstallation of the software will restore those keys and fix the driver (module) used to extend the standard Microsoft’s driver.

  • Skype and Ubuntu: green, black or scrambled video

    A new beta version of Skype for Linux has been released some time ago (2.2.0.35) and is now available for download.

    Even if a couple of bugs have been fixed we are still missing a native 64-bits version and there are still issues with some webcams working fine under other applications but still showing black, green or scrambled output.

    One possible solution has been around for a while but I found a lot of poorly written/confusing posts.

    Basically it seems that even if Skype support the v4l2 programming interface, some driver implementations are not working fully with the standard.

    The one example is the gspca implementation found for some webcams (like the Logitech Quickcam IM/Connect or the Creative Webcam Instant) can work only if we force Skype to use the old v4l implementation by pre-loading the old library.

    The best way to do this is by creating a script that automatically pre-load the library and launches Skype (Ubuntu 32 bits):

    1. Install the libv4l-0 package:

    sudo apt-get install libv4l-0

    1b. If you have Ubuntu 64 bits you’ll have to install two packages:

    sudo apt-get install libv4l-0 lib32v4l-0

    2. Then use the following line to launch Skype:

    LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so skype

    2b. if it doesn’t work, then look at the output in the terminal. If it is complaining about “error unexpected width / height in JPEG headerexpected: 320×240, header: 1600×1200” then try the following line:

    LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so skype

    If you have Ubuntu 64 bits you’ll have to slightly change the two lines into:

    LD_PRELOAD=/usr/lib32/libv4l/v4l2convert.so skype

    LD_PRELOAD=/usr/lib32/libv4l/v4l2convert.so skype

    As pointed out by realmagnum, if you have Lubuntu 32bits, the line will have to be: LD_PRELOAD=/usr/lib/i386-linux-gnu/libv4l/v4l2convert.so skype

    3. Once you’ve found the line that works for you, create a new empty file, and put inside:

    #!/bin/sh
    LD_PRELOAD=/usr/libxx/xxxxxxx.xx skype

    4. chmod a+x filenameyou’vecreated

    Now everytime you’ll need to launch Skype, you’ll be able to do so (and use the webcam) by launching the script you’ve created!