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?

5

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .