Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: Writing udev Rules

  1. #11
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Writing udev Rules

    @francois.marot

    Could I suggest following my guide and see if that works, if it does it's just a case of editing it to do what you want. If you don't want to do that it would be helpful if you posted your udev rule and your script here so that I can take a look at them. In the mean time I'm going to run some tests to see if my rule is actually working as expected.

    Edit: Hey, looks like my rule is faulty too - the data I want doesn't seem to appear in the output of the df command until after the script has exited. I'll post back if I find a solution.
    Last edited by Penguin Guy; November 18th, 2009 at 07:47 PM.

  2. #12
    Join Date
    Oct 2008
    Beans
    12

    Question Re: Writing udev Rules

    Hey Pengin Guy, I was about to try your suggestion when I saw your edit. You confirm my problem ?
    I imagine that your solution used to work but doesn't work any more on Karmic, am I right ?

  3. #13
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Writing udev Rules

    Quote Originally Posted by francois.marot View Post
    Hey Pengin Guy, I was about to try your suggestion when I saw your edit. You confirm my problem ?
    I imagine that your solution used to work but doesn't work any more on Karmic, am I right ?
    Yes, that's right. I'll put a note on the first post, but I'm not sure how to work around the problem.

  4. #14
    Join Date
    Nov 2009
    Beans
    1

    Re: Writing udev Rules

    Thank you, this howto was very useful!

  5. #15
    Join Date
    Oct 2008
    Beans
    12

    Question Re: Writing udev Rules

    Nobody still able to retreive the mountpoint by running a script launched from udev under Jaunty ?
    I desperatly need help on this one !
    Bounty123 are you on Karmic ? If yes did yyou upgrade with latest packages and could you provide your complete scripts if different than those given by Penguin Guy ?

  6. #16
    Join Date
    Jun 2009
    Location
    0000:0400
    Beans
    Hidden!

    Re: Writing udev Rules

    This rule works under Karmic.

    Code:
    SUBSYSTEM=="usb", KERNEL=="sd??", ACTION=="add", PROGRAM="/usr/local/bin/USB %k"
    Little did I realize there's a great man page on udev and writing rules. And of course, if you haven't seen it, this is just about the only other comprehensive resource available.

    Also, might I suggest:

    ...a simpler way to get the UUID of a drive? If $1 is a device file, such as /dev/sda1, then:
    Code:
    blkid $1 | sed -n 's/.*UUID=\"\([0-9a-z\-]*\)\".*/\1/p'
    Would return the UUID without worrying about parsing ls.

    You can also have udev mount the drive for you rather than waiting for HAL to do it. I use the following rule to mount all my removable media (since I don't use HAL at all).
    Code:
    KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_only_auto_mount_end"
    ACTION=="add", PROGRAM!="/lib/initcpio/udev/vol_id --label %N", GOTO="media_by_label_only_auto_mount_end"
    ACTION=="add", RUN+="/bin/mkdir -p /media/$env{ID_FS_LABEL}"
    
    # Global mount options
    ACTION=="add", ENV{mount_options}="relatime,users"
    # Filesystem specific options
    ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id -t %N", RESULT=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,uid=1000,gid=1000,umask=002"
    
    ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/$env{ID_FS_LABEL}"
    ACTION=="remove", ENV{ID_FS_LABEL}=="?*", RUN+="/bin/umount -l /media/$env{ID_FS_LABEL}", RUN+="/bin/rmdir /media/$env{ID_FS_LABEL}"
    LABEL="media_by_label_only_auto_mount_end"
    Mind you, Ubuntu no longer carries vol_id by default.
    Last edited by falconindy; January 3rd, 2010 at 02:31 AM.

  7. #17
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Writing udev Rules

    @ falconindy

    Thanks for your help, when I said that it doesn't work under Karmic, I meant the script - it appears that Ubuntu now waits for udev to finish before starting HAL. Your script is to complicated for me, but hopefully someone else will find it helpful. Thanks.

  8. #18
    Join Date
    Oct 2008
    Beans
    12

    Re: Writing udev Rules

    Well falconindy, thanks too for the script but it was too much complicated for me too !
    I found another method which seems to work quite well for now: i made a script monitoring the mtab file and runnning a Groovy script (Groovy being a java based language) whenever a change is detected in order to get the mount point on which a change happened.

    Here is my monitorMtab.sh script (make sure to install the inotifywait tool in order to use it):
    Code:
    MTAB=/etc/mtab
    MTAB_BAK=/tmp/mtab
    cd /path/to/current/directory
    
    cp $MTAB $MTAB_BAK
    while true
    do
    	inotifywait -e modify,close_write,create $MTAB
    	echo "- - - - - - - - - - - - - USB change detected on `date` - - - - - - - - - - -"
    	groovy analyzeMTab $MTAB_BAK $MTAB 
    	cp $MTAB $MTAB_BAK 
    done
    Here's my analyzeMTab.groovy file that detects modified lines and path in mtab:
    Code:
    #!/usr/bin/env groovy
    
    println "in analyzeMTab..."
    // the groovy script to be run with new mount point as parameter
    File mountPointAnalyzer = new File("mountPointAnalyzer.groovy")
    
    if (args.length != 2) {
    	println "Bad args number..." + args.length +". Exiting..."
    	System.exit(-1)
    }
    
    String newFileName = args[1]
    File oldFile = new File(args[0])
    File newFile = new File(newFileName)
    
    List oldLines = oldFile.readLines()
    List newLines = newFile.readLines()
    
    // Fill 'modifiedEntries' with new or modified entries in mtab
    def modifiedEntries = []
    newLines.each {
    	try {
    		if ( ! oldLines.contains(it) ) {
    			modifiedEntries.add(getMountedPath(it))
    		}
    	} catch(Exception e) {println e}
    }
    
    modifiedEntries.each{
    	println "In ${newFileName}, following mountpoint has changed and will be analyzed: "+it
    	//run mountPointAnalyzer groovy script with the path to analyze as parameter
    	new GroovyShell().run(mountPointAnalyzer, [it])
    }
    
    /** Return the mount path from a line in the mtab file*/
    String getMountedPath(String mtabLine) {
    	mtabLine.split(' ')[1]
    }

    ... then my mountPointAnalyzer.groovy script runs and makes the operations I want according to the path given. For completeness, it is mostly retreiving photos from my various USB keys and sdcards and backuping them on my computer.
    Last edited by francois.marot; February 7th, 2010 at 01:28 AM. Reason: added variable definitions in monitorMtab.sh

  9. #19
    Join Date
    Jul 2007
    Location
    Murcia, Spain
    Beans
    33
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Writing udev Rules

    Hey, I know it's a bit late, but someone might find it useful.

    I found an utterly easy workaround for the issue you are having here:
    Since udev waits for the "RUN" program to finish before it continues, you can launch it background so that udev can continue.

    To do it I just run the program/script from within bash. This is my udev rule. If everything else is working you should only worry about the RUN string.

    Code:
    SUBSYSTEM=="block", ATTR{size}=="15679488", ATTRS{serial}=="0019E02D40CF5A8A130308AB", RUN+="/bin/bash -c '/home/chikitulfo/bin/pruebackups &'"
    The script I want to run is /home/chikitulfo/bin/pruebackups. I also make the script wait 10 seconds before it starts to do things in the fs, just to give udev time to finish.
    Last edited by Chikitulfo; September 2nd, 2010 at 01:24 PM. Reason: Better explanation

  10. #20
    Join Date
    Oct 2008
    Beans
    12

    Smile Re: Writing udev Rules

    Thanks Chikitulfo, this might be REALLY usefull
    Just for the record, which version of Ubuntu are you running and are you up to date ?

Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •