From cceb34efc429d844d4bace6bea2479ecc53540b1 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Wed, 20 Oct 2004 19:57:53 +0000 Subject: Add helper tools for automounting USB devices --- sd2usbguid | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 sd2usbguid (limited to 'sd2usbguid') diff --git a/sd2usbguid b/sd2usbguid new file mode 100755 index 0000000..794d2c2 --- /dev/null +++ b/sd2usbguid @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +###################################################################### +# +# usb2guid and sd2usbguid: by greenfly +# +# This function gets passed the a scsi hard drive device and returns +# the GUID assigned to it from /proc/scsi/usb-storage-0/. +# +# it basically is the complement to usbguid2sd +# +# usage: sd2usbguid +# +# example: sd2usbguid sda +# output: 05e307020000000000000000 +# +# +###################################################################### + +use strict; + +my $scsi_drive = shift; +my $logfile = "/var/log/syslog"; +my $procdir = "/proc/scsi/usb-storage-0"; + +my $scsi_host; +my $guid; + +$scsi_host = get_scsi_host_from_drive($scsi_drive); +$guid = get_guid_from_scsi_host($scsi_host); + +print "$guid"; + + + + +###################################################################### +# +# this function gets passed a scsi host and reads all the files in +# $procdir to determine what guid it was assigned +# +sub get_guid_from_scsi_host +{ + my $scsi_host = shift; + my $file; + my $guid; + + opendir(DIR, $procdir) or die "can't opendir $procdir: $!"; + while (defined($file = readdir(DIR))) + { + next if $file =~ /^\.\.?$/; # skip . and .. + + open FILE, "$procdir/$file" or die "can't open $procdir/$file: $!"; + while() + { + if(/Host (\w+): usb-storage/) + { + last if($1 ne $scsi_host); + } + elsif(/GUID: (\w+)/) + { + $guid = $1; + } + } + close FILE; + } + closedir(DIR); + return $guid; +} + + +###################################################################### +# +# this function gets passed a scsi drive, such as "sda" and reads +# $logfile to determine what scsi host it was assigned +# +sub get_scsi_host_from_drive +{ + my $scsi_drive = shift; + my $scsi_host; + + open SYSLOG, $logfile or die "Can't open $logfile: $!"; + + while() + { + if(/kernel: Attached scsi disk $scsi_drive at (\w+),/) + { + $scsi_host = $1; + } + } + close SYSLOG; + + return "$scsi_host"; +} -- cgit v1.2.3