Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to edit the USB MSD, to show a message whenever i plug a device (like a flash drive) to my computer. How do i do it? Which file exactly is to be edited and how?

share|improve this question
    
    
UsbDriveDoSomethingHowto –  Achu Mar 19 at 5:11
    
Did you ever try my solution below? –  terdon Mar 19 at 12:46
    
yes i did! But what i wanna know is, editing the code of class driver of a particular device, so that when it is attached to the system, the console should show a message. That's what my manager has told me to do. –  akshay Mar 20 at 5:10
    
I read somewhere that i can use /linux/drivers/usb/storage But i exactly dont know. –  akshay Mar 21 at 4:37

1 Answer 1

The driver has nothing to do with this, it just manages the device and allows the kernel to communicate with it. What you want is a much higher lever function.

The way to do this is using udev, the device manager for the Linux kernel:

  1. Create a script that will send the notifications. Save the following lines in a file in your home directory, for example ~/usbnotify.sh:

    #!/bin/bash
    export DISPLAY=":0"
    notify-send "New device plugged in: $@"
    

    Make the script executable by running chmod +x ~/usbnotify.sh

  2. Create a new file called /etc/udev/rules.d/95-usbnotify.rules with the following contents (adapted from here):

    KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
    ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"
    
    # Get label
    PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
    # use basename to correctly handle labels such as ../mnt/foo
    PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
    ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"
    
    ACTION=="add", ENV{dir_name}!="", RUN+="/home/akshay/usbnotify.sh %c", GOTO="media_by_label_auto_mount_end"
    
    # Exit
    LABEL="media_by_label_auto_mount_end"  
    

    Make sure ti use the correct path to the script, I used /home/akshay/usbnotify.sh bur edit to point to your home dircetory.

Save the script and that's it. You should now receive a notification for every device that you plug in that is mounted as a drive. This will probably not work for cameras and the like but any storage device that is attached as /dev/sd* should work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.