2

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?

akshay
  • 279
  • 6
  • 15
  • You need to see this:[How to get an email notification when a USB storage device is inserted?](http://askubuntu.com/questions/192331/how-to-get-an-email-notification-when-a-usb-storage-device-is-inserted) and this [How do I make udev rules work?](http://askubuntu.com/questions/297412/how-do-i-make-udev-rules-work) – Achu Mar 19 '14 at 05:07
  • [UsbDriveDoSomethingHowto](https://help.ubuntu.com/community/UsbDriveDoSomethingHowto) – Achu Mar 19 '14 at 05:11
  • Did you ever try my solution below? – terdon Mar 19 '14 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 '14 at 05:10
  • I read somewhere that i can use /linux/drivers/usb/storage But i exactly dont know. – akshay Mar 21 '14 at 04:37

1 Answers1

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 to use the correct path to the script, I used /home/akshay/usbnotify.sh but edit to point to your home directory.

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.

terdon
  • 98,183
  • 15
  • 197
  • 293