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