summaryrefslogtreecommitdiff
path: root/sd2usbguid
blob: 794d2c2791e3f9fbfe6338a88fa3516aff1aaee8 (plain)
  1. #!/usr/bin/perl
  2. ######################################################################
  3. # usb2guid and sd2usbguid: by greenfly <greenfly@greenfly.org>
  4. #
  5. # This function gets passed the a scsi hard drive device and returns
  6. # the GUID assigned to it from /proc/scsi/usb-storage-0/.
  7. #
  8. # it basically is the complement to usbguid2sd
  9. #
  10. # usage: sd2usbguid <scsi drive>
  11. #
  12. # example: sd2usbguid sda
  13. # output: 05e307020000000000000000
  14. #
  15. #
  16. ######################################################################
  17. use strict;
  18. my $scsi_drive = shift;
  19. my $logfile = "/var/log/syslog";
  20. my $procdir = "/proc/scsi/usb-storage-0";
  21. my $scsi_host;
  22. my $guid;
  23. $scsi_host = get_scsi_host_from_drive($scsi_drive);
  24. $guid = get_guid_from_scsi_host($scsi_host);
  25. print "$guid";
  26. ######################################################################
  27. #
  28. # this function gets passed a scsi host and reads all the files in
  29. # $procdir to determine what guid it was assigned
  30. #
  31. sub get_guid_from_scsi_host
  32. {
  33. my $scsi_host = shift;
  34. my $file;
  35. my $guid;
  36. opendir(DIR, $procdir) or die "can't opendir $procdir: $!";
  37. while (defined($file = readdir(DIR)))
  38. {
  39. next if $file =~ /^\.\.?$/; # skip . and ..
  40. open FILE, "$procdir/$file" or die "can't open $procdir/$file: $!";
  41. while(<FILE>)
  42. {
  43. if(/Host (\w+): usb-storage/)
  44. {
  45. last if($1 ne $scsi_host);
  46. }
  47. elsif(/GUID: (\w+)/)
  48. {
  49. $guid = $1;
  50. }
  51. }
  52. close FILE;
  53. }
  54. closedir(DIR);
  55. return $guid;
  56. }
  57. ######################################################################
  58. #
  59. # this function gets passed a scsi drive, such as "sda" and reads
  60. # $logfile to determine what scsi host it was assigned
  61. #
  62. sub get_scsi_host_from_drive
  63. {
  64. my $scsi_drive = shift;
  65. my $scsi_host;
  66. open SYSLOG, $logfile or die "Can't open $logfile: $!";
  67. while(<SYSLOG>)
  68. {
  69. if(/kernel: Attached scsi disk $scsi_drive at (\w+),/)
  70. {
  71. $scsi_host = $1;
  72. }
  73. }
  74. close SYSLOG;
  75. return "$scsi_host";
  76. }