home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/tclsh
- # This code is extracted from:
- # MUT written by Jesse Liley for Puppy Linux 2005
- # ...in other words, what you see below is a hack, a simple commandline
- # tool to grab usb drive information. hacker: Barry Kauler 2005.
-
- # For output of debugging information, output is to stdout
- set debug 1
-
- # List of info from /proc/partitions
- array set partitions {}
- # Scsi controller information (scsi hosts providing a scsi bus)
- array set storages {}
- # Scsi device info (devices on scsi bus)
- array set scsi_host_chips {}
- # List of info from /proc/mounts
- array set mounteds {}
-
- # The list of displayed devices & partitions.
- set infolist ""
-
- # USB SCSI last refresh info, for hot plug scanning.
- set last_hotplug 0
-
-
- proc remove_empties str {
- set i 0
- set i [lsearch $str {}]
- while {$i >= 0} {
- set str [lreplace $str $i $i]
- set i [lsearch $str {}]
- }
- return $str
- }
-
- proc grab_partitions {} {
- set fid [open "/proc/partitions" r]
- while {![eof $fid]} {
- gets $fid str
- set shortlist [remove_empties [split $str]]
- if {[llength $str] == 4 && [lindex $str 0] > 0} {
- if { [lindex $shortlist 2] > 1 } {
- set ::partitions([lindex $shortlist 3]) \
- [format "%1.1f Mb" [expr "[lindex $shortlist 2]/1000.0"]]
- }
- }
- }
- close $fid
- }
-
- proc grab_proc_scsi {} {
- set chip 1
- set do_host ""
- set Location ""
- set Model ""
- set Type ""
- set Device ""
- set alphabet [list a b c d e f g i j k l m n o p q r s t u v w x y z]
- set tape 0
- set disk 0
- set cdrom 0
- set fid [open "/proc/scsi/scsi" r]
- while {![eof $fid]} {
- gets $fid str
- if {[regexp -inline {\S+} $str] == "Host:"} {
- set line [split $str " "]
- lappend Location [lindex $line 1]
- lappend Location [lindex $line 3]
- lappend Location [lindex $line 5]
- lappend Location [lindex $line 7]
- if {[lindex $Location 0] != $do_host} {
- set chip 1
- set do_host [lindex $Location 0]
- } { incr chip }
- }
- if {[regexp -inline {\S+} $str] == "Vendor:"} {
- set line [remove_empties [split $str " "]]
- set i [lsearch $line "Vendor:"]
- set Model ""
- incr i
- while {1} {
- if {[lindex $line $i]=="" || [lindex $line $i]=="Rev:"} {break}
- if {[lindex $line $i] == "Model:"} {
- if {$Model != ""} {set Model [string trimright $Model " "]; set Model "$Model, "}
- } else {
- set Model "$Model[lindex $line $i] "
- }
- incr i
- }
- set Model [string trimright $Model ", "]
- }
- if {[regexp -inline {\S+} $str] == "Type:"} {
- set line [split $str " "]
- set i [lsearch $line "Direct-Access"];
- if {$i >= 0} {set Type "disk"; set Device "sd[lindex $alphabet $disk]"; incr disk}
- set i [lsearch $line "CD-ROM"];
- if {$i >= 0} {set Type "cdrom"; set Device "scd$cdrom"; incr cdrom}
- set i [lsearch $line "Sequential-Access"];
- if {$i >= 0} {set Type "tape"; set Device "st$tape"; incr tape}
- }
- if {$Type != "" && $Location != "" && $Model != "" && $Device != "" && $do_host != ""} {
- set ::scsi_host_chips($do_host) $chip
- catch {
- foreach {h t id} $::scsi_hosts([lindex $Location 0]) {
- if {$h == [lindex $Location 0]} {
- set ::storages($Device) [list $h $Type "$Model" $id $chip]
- if {$::debug} {puts "found possible $Device {$Location} {$Type} {$Model} {$chip}"}
- set devid [open "/dev/$Device" r]
- close $devid
- }
- }
- }
- set Location ""
- set Type ""
- set Model ""
- set Device ""
- }
- }
- close $fid
- }
-
- proc load_scsi_host ctrl {
- catch {
- set usb false
- set host ""
- set product ""
- set vendor ""
- set serial ""
- set protocol ""
- set transport ""
- set guid ""
- set attached ""
- if {$::debug} {puts "finding $ctrl"}
- set hid [open $ctrl r]
- while {![eof $hid]} {
- gets $hid str
- set shortlist [remove_empties [split $str]]
- switch [lindex $shortlist 0] {
- "Host" { set host [string trimright [lindex $shortlist 1] ":"]
- if {[lindex $shortlist 2] == "usb-storage"} {set usb true} {set usb false}
- }
- "Vendor:" { set vendor [join [lrange $shortlist 1 end]] }
- "Product:" { set product [join [lrange $shortlist 1 end]] }
- "Serial" { set serial [join [lrange $shortlist 2 end]] }
- "Protocol:" { set protocol [join [lrange $shortlist 1 end]] }
- "Transport:" { set transport [join [lrange $shortlist 1 end]] }
- "GUID:" { set guid [lindex $shortlist 1] }
- "Attached:" { set attached [lindex $shortlist 1] }
- }
- }
- close $hid
- if {$attached=="Yes" && $usb==true && $transport=="Bulk" && $protocol=="Transparent SCSI"} {
- if { $vendor=="" || $vendor=="Unknown"} {set name $product} {set name "$vendor, $product"}
- if {$::debug} {puts "$host is $name"}
- set ::scsi_hosts($host) [list $host $name "usb" "$serial.$guid"]
- } { if {$::debug} {puts "ignoring $host $product"} }
- }
- }
-
- proc grab_scsi_host_devs {} {
- catch {
- set hosts [glob /proc/scsi/usb-storage*]
- foreach store_host $hosts {
- set controler [glob "$store_host/*"]
- load_scsi_host $controler
- }
- }
- }
-
-
-
- proc form_usb_part_list host {
- #puts " $host [array names ::storages *]"
- foreach dev [array names ::storages *] {
- foreach {h type t id chip} $::storages($dev) {
- if {$host == $h} {
- foreach D [array names ::partitions *] {
- if {0 == [string compare -length [string length $dev] $D $dev]} {
- if {$type=="disk" && [string length $D] > [string length $dev]
- && $::partitions($D) > 1} {
- if {$::debug} {puts "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"}
- lappend ::infolist "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"
- }
- if {$type=="cdrom" && [string length $D] == [string length $dev]
- && $::partitions($D) > 1} {
- if {$::debug} {puts "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"}
- lappend ::infolist "/dev/$D|part|$::partitions($D)|$t (port #$chip)|$id"
- }
- }
- }
- }
- }
- }
- }
-
- proc form_usb_list {} {
- foreach host [array names ::scsi_hosts *] {
- set product ""
- set host_type ""
- set devdev ""
- #puts "host $host"
- foreach dev [array names ::storages *] {
- foreach {h type t id chip} $::storages($dev) {
- if {$host == $h} {
- set product $t
- set host_type $type
- set devdev "/dev/$dev"
- }
- }
- }
- if {$devdev == ""} {set devdev $host}
- #puts "host $host $product $devdev"
-
- #puts "host $host $::scsi_hosts($host) $::scsi_host_chips($host)"
- if { $::scsi_host_chips($host) == 1 } {
- if {$::debug} {puts "$devdev|$host_type|-|[lindex $::scsi_hosts($host) 0]|[lindex $::scsi_hosts($host) 1]" }
- lappend ::infolist "$devdev|$host_type|-|$product|[lindex $::scsi_hosts($host) 1]"
- form_usb_part_list $host
- } else {
- if {$::debug} {puts "$devdev|[lindex $::scsi_hosts($host) 2]|-|[lindex $::scsi_hosts($host) 1] ($::scsi_host_chips($host) port)|[lindex $::scsi_hosts($host) 2]" }
- lappend ::infolist "$devdev|[lindex $::scsi_hosts($host) 2]|-|$product ($::scsi_host_chips($host) port)|[lindex $::scsi_hosts($host) 2]"
- form_usb_part_list $host
- }
- }
- }
-
-
-
-
- array unset ::partitions *
- array unset ::storages *
- array unset ::scsi_host_chips *
- array unset ::scsi_hosts *
- array unset ::mounteds *
-
- # scan usb info
- grab_scsi_host_devs
- grab_proc_scsi
-
- # scan all partition info
- grab_partitions
-
- # construct ordered info about usb
- form_usb_list
-
-
- ###END###
-