which system runs scripts when USB devices plugged in?

John Austin ja at jaa.org.uk
Wed Nov 10 15:48:20 UTC 2010


On Wed, 2010-11-10 at 09:21 -0430, Patrick O'Callaghan wrote:
> On 11/10/2010 09:06 AM, Tom Horsley wrote:
> > I have no idea if this is applicable, but I wrote this silly
> > program:
> >
> > http://home.comcast.net/~tomhorsley/software/xdbusd/xdbusd.html
> >
> > It lets you run arbitrary scripts when dbus messages
> > happen. If dbus-monitor shows messages that appear on
> > dbus when you plug your device in, you might be able to
> > use xdbusd to do what you want.
> 
> Very interesting. I wonder if I can hack that to make my iPhone 3GS 
> connect properly? Hmmm ...
> 
> poc

Hi

Don't know if this script is useful to anyone but its working OK with
F13

There must be a better way !

John

[root at milos ~]# cat sd_mount 
#!/usr/bin/perl
#Terrible script to control the mounting of "hotplug" usb and eSATA disks
#The following udev rules are currently associated with the script
#[root at milos ~]# cat /lib/udev/rules.d/81-aaja.rules 
#ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", ENV{ID_FS_TYPE}=="ext3|ext4", RUN:="/root/sd_mount %k"
#ACTION=="remove",KERNEL=="sd*" , SUBSYSTEM=="block", RUN:="/root/sd_umount %k"
#If the device is an SSD
#1. The scheduler will be changed to "noop"
#2. If the partition is ext3 or ext4 the "noatime" mount option is set
#3. If the partition is ext4 and the disk supports TRIM the "discard" mount option is set
#If the device is a hard disk then it is mounted "normally"
#An entry is made in /media/.hal-mtab to fool Thunar/Dolphin so that they will umount the partition
#Lists of the partition characteristics are defined later in this script (they should be in a /etc file!)

open(MESS,">/tmp/mess");
print MESS `date`;
$dev = $ARGV[0];
print MESS $dev,"\n";

# Is $0 a whole disk or a partition?
if ( "$dev" =~ /sd[a-g]/ ) {$disk = $dev;}
if ( "$dev" =~ /sd[a-g][1-9]/ ) {$disk = $dev;chop($disk);}
if ( "$dev" =~ /sd[a-g][1-2][0-9]/ ) {$disk = $dev;chop($disk);chop($disk);}
print MESS "dev= ",$dev,"   ","disk= ",$disk,"\n";

#Pick up the Variables available to blkid
#This seems tricky - RTFM
# -p does not give LABEL -c /dev/null does not give LABEL
#Yet the "-p" option is vital whien invoked by udev script !!
#The output of blkid seems to vary in an undefined manner !!
$udev_envs = `/sbin/blkid -p -o udev /dev/$dev`;

#The udev info is not set at this stage so this doesnt work!!
#$udev_envs = `udevadm info --name=/dev/$dev --query=property`;

print MESS "udev_envs ",$udev_envs;
@udev_envs = split(/\n/,$udev_envs);
print MESS "No of envs ",$#udev_envs;
print MESS "\n\n";

#Create hash array for the variables
#Must be a better way!
for ($j = 0; $j <= $#udev_envs; $j++){
#       print MESS $udev_envs[$j];
        @line = split(/=/,$udev_envs[$j]);
        $keyy = $line[0];
        $vall = $line[1];
        $env{"$keyy"} = $vall;
        print MESS $keyy,"    ",$env{$keyy},"\n";
        }

#Find who is logged into the Console - Only one?
$who = `w`;
print MESS $who;
@lines = split (/\n/, $who);
#Skip the first line - it has ":" in it !
for ($j =1 ; $j <= $#lines; $j++){
#       Split on 1 or more white spaces
        @line = split (/\s+/, $lines[$j]);
#       Find line with :[0-9] in 2nd column
        if ( $line[1] =~ /\:\d/ ){
                print MESS "Console User is ",$line[0],"\n";
                $user = $line[0];
                }
        }

($uid,$gid)= (stat("/home/$user"))[4,5];
print MESS $uid,"  ",$gid,"\n";

$label = $env{"ID_FS_LABEL"};
$fstype = $env{"ID_FS_TYPE"};
print MESS "LABEL = ",$label,"  FS_TYPE = ",$fstype,"\n";

#Partition lists for various Flash devices
@ssd_trim_list = qw(ssd1 ssd2 ssd3);
@ssd_non_trim_list=qw(cb1 cb2 corsair1 throttle);
@ssd_full_list = (@ssd_trim_list, @ssd_non_trim_list);

print MESS "full list ", at ssd_full_list," end\n";
$ssd_disk_type = "non_ssd";
foreach $j (@ssd_full_list){
        if ("$label" eq "$j"){ $ssd_disk_type = "ssd";}}

$supports_trim="false";
foreach $j (@ssd_trim_list){
        if ("$label" eq "$j"){ $supports_trim = "true";}}

print MESS "Disk type is ",$ssd_disk_type,"   Supports TRIM ",$supports_trim,"\n";

#Process the options in a very clear way ?
#Hard disk
if( "$ssd_disk_type" eq "non_ssd"){$options = "";}

#SSD with No TRIM support but ext3 or ext4
if( "$ssd_disk_type" eq "ssd" and $supports_trim eq "false" and ("$fstype" eq "ext3" or "$fstype" eq "ext4"))
        { $options = "-o noatime";}

#SSD with TRIM and ext4
if( "$supports_trim" eq "true" and "$fstype" eq "ext4")
        { $options = "-o noatime,discard";}

#SSD with TRIM and ext3
if( "$supports_trim" eq "true" and "$fstype" eq "ext3")
        { $options = "-o noatime";}

print MESS $label,"\n";
print MESS "Mount options are  ",$options,"\n";

#If the partition is on an SSD disk has the scheduler been configured?
if( "$ssd_disk_type" eq "ssd"){
        $command="cat /sys/block/"."$disk"."/queue/scheduler";
        $sched_now = `$command`;
        $sched_now =~ s/^.*\[//;
        $sched_now =~ s/\].*$//;
        print MESS "current setting is ",$sched_now,"\n";
        if( "$sched_now" ne "noop" ) {
                $command="echo noop > /sys/block/"."$disk"."/queue/scheduler";
                `$command`;
        }
}
#Create the mount point - it will be deleted by Thunar/Dolphin but not by Pcmanfm at present
mkdir ("/media/$label",0777);
chmod 0777,"/media/$label";
$no = chown $uid,$gid,"/media/$label";

#($de,$ino,$mode,$nlink,$ui,$gi,$rdev,$size,$atime,$mtime,$blksiz,$blocks) = stat("/media/$label");
#print MESS $de,"  ",$ui,"\n";
#print MESS "No of files whose ownership was changed  ",$no,"\n";

#Mount the partiton as required
`/bin/mount -t auto $options /dev/$dev /media/$label`;

#Change uid and gid of the mounted file system
$no = chown $uid,$gid,"/media/$label";

#Fool Thunar/Dolphin into thinking that hal did the mount
open(FRIG,">>/media/.hal-mtab");
print FRIG "/dev/".$dev."\t"."$uid\t"."$gid\t"."$fstype\t"."nosuid,nodev\t"."/media/".$label."\n";
close FRIG;
close MESS;




More information about the users mailing list