home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / lib / mut / disks.tcl next >
Encoding:
Tcl/Tk script  |  2006-06-17  |  26.5 KB  |  924 lines

  1. #!/usr/bin/tclsh
  2. #
  3. # MUT written by Jesse Liley for Puppy Linux 2005
  4. # bugs, fixes, suggestions, email: Jesse.Liley@gmail.com
  5. #
  6. # Free software, use at own risk! LGPL, please keep comments.
  7. #
  8. # Images used taken from Mandrake Linux download 10.0 /usr/share/icons
  9. # So I hope they are free to distribute :-)
  10. #
  11.  
  12. package require Tcl 8.5
  13.  
  14.  
  15. #Here you can set devices that start out not being scanned, e.g. for if the scan time takes a while and you don't really want to wait every refresh.
  16. array unset NoScanMedia   ;# this line defines the array.
  17. set NoScanMedia(fd0) "noscan"  ;# floppy disk is fd0
  18. set NoScanMedia(fd1) "noscan"  ;# second floppy disk is fd1, this is ok if one or none floppy drives in system.
  19. #set NoScanMedia(fd0) "noscan" ;# add others, replace fd0 with device name
  20.  
  21.  
  22. # This function calculates home path of application, yay, no need to configure it :-) even if symlinked to it.
  23. # this is generic, it could apply to any tcl app that has an application directory structure.
  24. proc homepath { } {
  25.     set cmd "pwd"
  26.     catch $cmd this_pwd
  27.     if {[string index $::argv0 0] == "/"} {
  28.         set path "$::argv0"
  29.     } else {
  30.         set path "$this_pwd/$::argv0"
  31.     }
  32.     # if it is a symlink from say /usr/local/bin/mut, follow the link.
  33.     if { [file type $path] == "link" } {
  34.         set path "$this_pwd/[file readlink $path]"
  35.     }
  36.     set path "$path/.."
  37.     set plist [split $path "/"]
  38.     set n [llength $plist]
  39.     for {set i 0} {$i < $n} {incr i} {
  40.         if {[lindex $plist $i] == "."} { 
  41.             set a [expr "$i - 1" ]
  42.             set b [expr "$i + 1" ]
  43.             set plist [concat [lrange $plist 0 $a] [lrange $plist $b end ] ]
  44.             incr n -1
  45.         }
  46.     }
  47.     set n [llength $plist]
  48.     set olist ""
  49.     for {set i 0} {$i < $n} {incr i} {
  50.         if {[lindex $plist $i] == ".."} {
  51.             set olist [lrange $olist 0 [expr "[llength $olist]-2"]]
  52.         } else {
  53.             lappend olist [lindex $plist $i]
  54.         }
  55.     }
  56.     set path "[join $olist "/"]/"
  57.     return $path
  58. }
  59.  
  60.  
  61. proc full_refresh_display {} {
  62.     array unset ::actions
  63.     set ::infolist {}
  64.     run_full_scan
  65.     Show_Devices
  66. }
  67.  
  68.  
  69. proc remove_empties str {
  70.     set i 0
  71.     set i [lsearch $str {}]
  72.     while {$i >= 0} {
  73.         set str [lreplace $str $i $i]
  74.         set i [lsearch $str {}]
  75.     }
  76.     return $str
  77. }
  78.  
  79. proc grab_fs_vol {fs dev} {
  80.     set results ""
  81.     if {[lsearch [list "iso9660" "vfat" "ext2"] $fs]=={}} {
  82.         if {$::debug} {puts "Unknown fs $fs for grab_fs_vol"}
  83.         return ""
  84.     }
  85.     catch "exec $::mut/bin/label_$fs /dev/$dev" results
  86.     set results [lindex [split $results "\n"] 0]
  87.     set results [string trimleft [string trimright $results]]
  88.     return $results
  89. }
  90.  
  91. proc majorBus__get_list {} {
  92.     set rec false
  93.     array unset ::majorBus
  94.     array unset ::majorBusRev
  95.     catch {
  96.         set fid [open "/proc/devices" r]
  97.         if {$::debug} {puts "in majorBus__get_list"}
  98.         while {![eof $fid]} {
  99.             gets $fid str
  100.             set line [remove_empties [split $str " "]]
  101.             if {$rec == true && [lindex $line 1] > 1} {
  102.                 set ::majorBus([lindex $line 0]) [lindex $line 1]
  103.                 set ::majorBusRev([lindex $line 1]) [lindex $line 0]
  104.     if {$::debug} {puts " ::majorBus([lindex $line 0]) $::majorBus([lindex $line 0]) "}
  105.             }
  106.             if {[lindex $line 0 ] == "Block"} {set rec true}
  107.         }
  108.         close $fid
  109.     }
  110. }
  111.  
  112. # scan media or model file.
  113. proc scan_ide_medium {bus lun scan_me} {
  114.     if {$bus=="" || $lun=="" || $scan_me==""} {return ""}
  115.     set medium ""
  116.     catch {
  117.         set bd_fd [open "/proc/ide/$bus/$lun/$scan_me" r]
  118.         gets $bd_fd medium
  119.         close $bd_fd
  120.     }
  121.     return $medium
  122. }
  123.  
  124. proc main_or_part {str} {
  125.     for {set i 0} {$i < [string length $str] } {incr i} {
  126.         if {[string index $str $i] >= "0" && [string index $str $i] <= "9"} {
  127.             if {[string range $str $i end] >= 1 && [string range $str $i end] <= 16} {
  128.                 return part
  129.             }
  130.         }
  131.     }
  132.     return main
  133. }
  134.  
  135. proc diskStats26__get_list {quick} {
  136.     set rec false
  137.     array unset ::diskStats
  138.     catch {
  139.         set fid [open "/proc/diskstats" r]
  140.         if {$::debug} {puts "in diskStats26__get_list"}
  141.         while {![eof $fid]} {
  142.             gets $fid str
  143.             set line [remove_empties [split $str " "]]
  144.             foreach did $::grabdevs {
  145.                 if {[string range [lindex $line 2] 0 1] == $did} {
  146.                     set media ""
  147.                     if {$did == "fd"} {
  148.                         set media floppy
  149.                     }
  150.                     if {$did == "hd"} {
  151.                         if {[main_or_part [lindex $line 2]] == "main"} {
  152.                             set media [scan_ide_medium $::majorBus([lindex $line 0]) [lindex $line 2] "media"]
  153.                         } else {
  154.                             set media part
  155.                         }
  156.                     }
  157.                     if {$did == "sd"} {
  158.                         if {[main_or_part [lindex $line 2]] == "main"} {
  159.                             set media disk
  160.                         } else {
  161.                             set media part
  162.                         }
  163.                     }
  164.                     if {$did == "scd" || $did=="sr"} {
  165.                         if {[main_or_part [lindex $line 2]] == "main"} {
  166.                             set media cdrom
  167.                         } else {
  168.                             set media part
  169.                         }
  170.                     }
  171.                     set ::diskStats([lindex $line 2]) "[lindex $line 0].[lindex $line 1].$::majorBus([lindex $line 0]).$media"
  172.                     if {$::debug} {puts "set ::diskStats([lindex $line 2]) $::diskStats([lindex $line 2])"}
  173.                 }
  174.             }
  175.             if {[lindex $line 0 ] == "Block"} {set rec true}
  176.         }
  177.         close $fid
  178.     }
  179. }
  180.  
  181. proc diskStats24__get_list {quick} {
  182.     set rec false
  183.     array unset ::diskStats
  184.     if {$::debug} {puts "in diskStats24__get_list"}
  185.     set addfd false
  186.     catch {
  187.         if {$::majorBusRev(fd)  >= 0} {
  188.             set ::diskStats(fd0) "$::majorBusRev(fd).0.fd.floppy"
  189.             if {$::debug} {puts "set ::diskStats(fd0) $::diskStats(fd0)"}
  190.             #second floppy disk here:
  191.             #set ::diskStats(fd1) "$::majorBusRev(fd).1.fd.floppy"
  192.             #if {$::debug} {puts "set ::diskStats(fd1) $::diskStats(fd1)"}
  193.         }
  194.     }
  195.     foreach dev [lsort [array names ::partitions]] {
  196.         set str $::partitions($dev)
  197.         set line [remove_empties [list [lindex $str 1] [lindex $str 2] $dev ]]
  198.         if {[llength $line] < 3 } {continue}
  199.         foreach did $::grabdevs {
  200.             if {[string range [lindex $line 2] 0 1] == $did} {
  201.                 set media ""
  202.                 if {$did == "fd"} {
  203.                     set media floppy
  204.                 }
  205.                 if {$did == "hd"} {
  206.                     if {[main_or_part [lindex $line 2]] == "main"} {
  207.                         set media [scan_ide_medium $::majorBus([lindex $line 0]) [lindex $line 2] "media"]
  208.                     } else {
  209.                         set media part
  210.                     }
  211.                 }
  212.                 if {$did == "sd"} {
  213.                     if {[main_or_part [lindex $line 2]] == "main"} {
  214.                         set media disk
  215.                     } else {
  216.                         set media part
  217.                     }
  218.                 }
  219.                 if {$did == "scd" || $did=="sr"} {
  220.                     if {[main_or_part [lindex $line 2]] == "main"} {
  221.                         set media cdrom
  222.                     } else {
  223.                         set media part
  224.                     }
  225.                 }
  226.                 set ::diskStats([lindex $line 2]) "[lindex $line 0].[lindex $line 1].$::majorBus([lindex $line 0]).$media"
  227.                 if {$::debug} {puts "set ::diskStats([lindex $line 2]) $::diskStats([lindex $line 2])"}
  228.             }
  229.         }
  230.         if {[lindex $line 0 ] == "Block"} {set rec true}
  231.     }
  232. }
  233.  
  234. proc diskStats__get_list {quick} {
  235.     if {$::Kernel_Version=="2.4"} {diskStats24__get_list $quick}
  236.     if {$::Kernel_Version=="2.6"} {diskStats26__get_list $quick}
  237. }
  238.  
  239. proc partitions__get_list {} {
  240.     array unset ::partitions
  241.     catch {
  242.         set fid [open "/proc/partitions" r]
  243.         while {![eof $fid]} {
  244.             gets $fid str
  245.             set shortlist [remove_empties [split $str]]
  246.             set dev [lindex $shortlist 3]
  247.             if {[llength $str] == 4 && [lindex $str 0] > 0 && [lindex $shortlist 2] > 1 } {
  248.                 set mytimer [clock clicks -milliseconds]
  249.                 set openok true
  250.                 set catchinfo ""
  251.                 if {$::Kernel_Version=="2.4" && [string range $dev 0 1] == "sd"} {
  252.                     # Sigh, we have to force the kernel to look at the hardware:
  253.                     if {[info exists ::NoScanMedia($dev)]} {
  254.                         if {$::debug} { puts "not scan $dev"}
  255.                     } else {
  256.                         set openok false
  257.                         catch {
  258.                             if {$::debug} { puts "about to scan $dev"}
  259.                             set bd_fd [open "/dev/$dev" r]
  260.                             close $bd_fd
  261.                             set openok true
  262.                         } catchinfo
  263.                     }
  264.                 }
  265.                 if {$openok == false} {
  266.                     if {$::debug} { puts "device not working? /dev/$dev  $catchinfo" }
  267.                     if {[info exists ::NoScanMedia($dev)]} {
  268.                         if {$::debug} {puts "already in NoScanMedia $::NoScanMedia($dev)"}
  269.                     } else {
  270.                         #if it exists but cannot be opened, make it a noscan object
  271.                         if {[file exists /dev/[lindex $shortlist 3] ] == 1} {
  272.                             set ::NoScanMedia([lindex $shortlist 3]) "noscan"
  273.                         }
  274.                     }
  275.                 }
  276.                 set time_length [expr "[clock clicks  -milliseconds] - $mytimer"]
  277.                 if {$::debug} { puts "open scan time is $time_length 1000ths of a second, $shortlist"}
  278.                 set ::partitions([lindex $shortlist 3]) \
  279.                     [list [format "%1.1f Mb" [expr "[lindex $shortlist 2]/1000.0"]] [lindex $shortlist 0] [lindex $shortlist 1]]
  280.                 if {$::debug} {puts "set  ::partitions($dev) = $::partitions($dev)"}
  281.             }
  282.             if {[llength $str] == 4 && [lindex $str 0] > 0 && [lindex $shortlist 2] == 1 } {
  283.                 set ::partitions($dev) "extended"
  284.             }
  285.         }
  286.         close $fid
  287.     }
  288.     
  289.     # we have to scan for extra devices here in kernel 2.4
  290.     # Scan for IDE removable disc drives (cdroms)
  291.     if {$::Kernel_Version=="2.4"} {
  292.         set ide_devs [glob /dev/hd*]
  293.         set ide_scan {}
  294.         set devlist [array names ::partitions]
  295.         foreach device $ide_devs {
  296.             set dev [lindex [split $device / ] 2]
  297.             set atpos [lsearch -exact $devlist $dev]
  298.             if {$atpos < 0 && [string length $dev] == 3} {
  299.                 set found false
  300.                 set gl ""
  301.                 catch {
  302.                     set  gl [split [glob "/proc/ide/ide*/$dev"] /]
  303.                 }
  304.                 array unset ::cdromMedium $dev
  305.                 if {$gl == ""} {continue}
  306.                 set info ""
  307.                 lappend ide_scan $dev
  308.                 catch { exec $::mut/bin/stat_dev /dev/$dev } info
  309.                 if {$info ==""} {continue}
  310.                 set ::partitions($dev) [concat "device" [split $info |]]
  311.         if {$::debug} { puts "set  ::partitions($dev) = $::partitions($dev)" }
  312.                 #set ::diskStats($dev) "[lindex $info 0].[lindex $info 1].sr.cdrom"
  313.         #if {$::debug} { puts "set  ::diskStats($dev) = $::diskStats($dev)" }
  314.             }
  315.         }
  316.         if {$::debug} { puts "scan these devices... [lsort $ide_scan]" }
  317.     }
  318.     # Scan for SCSI/USB removable disc drives (cdroms)
  319.     if {$::Kernel_Version=="2.4" || $::Kernel_Version=="2.6"} {
  320.         set sr_devs [glob /dev/sr*]
  321.         set sr_scan {}
  322.         set devlist [array names ::partitions]
  323.         foreach device $sr_devs {
  324.             set dev [lindex [split $device / ] 2]
  325.             set atpos [lsearch -exact $devlist $dev]
  326.             if {$atpos < 0 && [string length $dev] == 3} {
  327.                 set found false
  328.                 set gl ""
  329.                 set cdinfo ""
  330.                 catch { exec $::mut/bin/cdrominfo /dev/$dev -p } cdinfo
  331.                 if {$cdinfo == ""} {continue}
  332.                 set info [split $cdinfo |]
  333.                 if {[llength $info ] < 10} {continue}
  334.                 lappend sr_scan $dev
  335.                 catch { exec $::mut/bin/stat_dev /dev/$dev } info
  336.                 set ::partitions($dev) [concat "device" [split $info |]]
  337.         if {$::debug} { puts "set  ::partitions($dev) = $::partitions($dev)" }
  338.             }
  339.         }
  340.         if {$::debug} { puts "scan these devices... [lsort $sr_scan]" }
  341.     }
  342. }
  343.  
  344. proc device_nodes__scan_for_make_additional {} {
  345.     foreach name [lsort [array names ::partitions]] {
  346.         if {[lsearch -exact $::existing_nodes $name] >= 0} {continue}
  347.         set info ""
  348.         catch { exec $::mut/bin/stat_dev /dev/$name } info
  349.         set info [split $info |]
  350.         if {[lindex $info 0]>0 && [llength $info] ==2} {
  351.             lappend ::existing_nodes $name
  352.         } else {
  353.             if {$::debug} {puts "ADDME $name info $info"}
  354.             if {$::debug} {puts "makedevs /dev/$name b [lindex $::partitions($name) 1] [lindex $::partitions($name) 2] 0 16 s" }
  355.             catch "exec makedevs /dev/$name b [lindex $::partitions($name) 1] [lindex $::partitions($name) 2] 0 16 s " ex_info
  356.             if {$::debug} {puts "execute info $ex_info" }
  357.         }
  358.     }
  359.     if {$::debug} {puts "devnodes include $::existing_nodes"}
  360. }
  361.  
  362. proc cdromMedium_get_list {quick} {
  363.     if {$quick=="true"} {return}
  364.     array unset ::cdromMedium
  365.     foreach dev [array names ::diskStats] {
  366.         set ds [split $::diskStats($dev) '.']
  367.         if {[lindex $ds 3] =="cdrom"} {
  368.             catch "exec $::mut/bin/cdrominfo /dev/$dev -p" cdinfo
  369.             set ::cdromMedium($dev) $cdinfo
  370.             if {$::debug} {puts "set  ::cdromMedium($dev) which is $cdinfo"}
  371.         }
  372.     }
  373. }
  374.  
  375. proc additional_scan_for {mnt} {
  376.     catch { exec $::mut/bin/stat_dev /dev/$mnt } info
  377.     set m [lindex [split $info |] 0]
  378.     set s [lindex [split $info |] 1]
  379.     if {$m !="" && $s!= ""} {
  380.         foreach dev [array names ::diskStats] {
  381.             if {$m == [lindex [split $::diskStats($dev) .] 0] &&
  382.                 $s == [lindex [split $::diskStats($dev) .] 1]} {
  383.                 if {$dev !="" && $mnt !=""} {
  384.                     set ::etc_fstab($dev) "/mnt/$mnt"
  385.                     if {$::debug} {puts "set ::etc_fstab($dev) = $::etc_fstab($dev)"}
  386.                     return
  387.                 }
  388.             }
  389.         }
  390.     }
  391. }
  392.  
  393. proc etc_fstab__get_list {} {
  394.     if {$::debug} {puts "in etc_fstab__get_list"}
  395.     array unset ::etc_fstab 
  396.     catch {
  397.         set fid [open "/etc/fstab" r]
  398.         while {![eof $fid]} {
  399.             gets $fid str
  400.             set sl [remove_empties [split $str]]
  401.             set dev [lindex $sl 0]
  402.             set mnt [lindex $sl 1]
  403.             set dev [lindex [remove_empties [split $dev /]] end]
  404.             if { $dev != "" &&
  405.                 $dev != "none" && 
  406.                 $dev != "tmpfs" && 
  407.                 $dev != "rootfs" && 
  408.                 $dev != "loop"} {
  409.                 if {$dev !="" && $mnt !=""} {
  410.                     set ::etc_fstab($dev) "$mnt"
  411.                     if {$::debug} {puts "set ::etc_fstab($dev) = $::etc_fstab($dev)"}
  412.                 }
  413.             }
  414.         }
  415.     }
  416.     close $fid
  417.     additional_scan_for dvd
  418.     additional_scan_for cdrom
  419.  
  420.     # Multisession DVD/CD should get mounted at /mnt/home if possible
  421.     set myhome /mnt/myhome
  422.     if {[file exists /etc/multisessionmediatype]} {
  423.         set cdburnerdrive ""
  424.         if {[file exists /etc/cdburnerdrive ]} {
  425.             catch {
  426.                 set fid [open /etc/cdburnerdrive r]
  427.                 gets $fid cdburnerdrive
  428.                 close $fid
  429.             }
  430.         }
  431.         if {$cdburnerdrive != ""} {
  432.             set do_set true
  433.             catch {
  434.                 if {$::etc_fstab($cdburnerdrive) == $myhome } {
  435.                     set do_set false
  436.                 }
  437.             }
  438.             if {$do_set} {
  439.                 set ::etc_fstab($cdburnerdrive) $myhome
  440.                 if {$::debug} {puts "set ::etc_fstab($cdburnerdrive) = $::etc_fstab($cdburnerdrive)"}
  441.             }
  442.         }
  443.     }
  444. }
  445.  
  446. proc mounteds__get_list {} {
  447.     if {$::debug} {puts "in mounteds__get_list"}
  448.  
  449.     set fs_list ""
  450.     catch "exec mount" fs_list
  451.     foreach line [split $fs_list '\n'] {
  452.         set sl [remove_empties [split $line]]
  453.         set dev [lindex $sl 0]
  454.         set mnt [lindex $sl 2]
  455.         if { [string compare -length 4 $dev "/dev"] == 0} {
  456.             set ::mounteds($dev) "$mnt|[join [lrange $sl 4 end ] |]"
  457.             if {$::debug} { puts " ::mounteds($dev) = $::mounteds($dev)"}
  458.         }
  459.     }
  460.  
  461.     # Kernel 2.6 swaps:
  462.     # Filename                                Type            Size    Used    Priority
  463.     # /dev/hda8                               partition       682720  0       -1
  464.  
  465.  
  466.     catch {
  467.         set fid [open "/proc/swaps" r]
  468.         while {![eof $fid]} {
  469.             gets $fid str
  470.             set sl [remove_empties [split $str]]
  471.             set swap [lindex $sl 0]
  472.             if { [string compare -length 4 $swap "/dev"] == 0} {
  473.                 set ::mounteds($swap) "swap|swap|"
  474.                 if {$::debug} { puts " ::mounteds($swap) = $::mounteds($swap)"}
  475.             }
  476.             if { [string compare -length 11 $swap "/initrd/dev"] == 0} {
  477.                 set swap [string range $swap 7 end]
  478.                 set ::mounteds($swap) "initrdswap|initrdswap|"
  479.                 if {$::debug} { puts " ::mounteds($swap) = $::mounteds($swap)"}
  480.             }
  481.         }
  482.         close $fid
  483.     }
  484. }
  485.  
  486.  
  487. # dmesg example:
  488. # sd 6:0:0:0: Attached scsi removable disk sdb
  489.  
  490.  
  491. proc scsi_host_to_sd_location {} {
  492.     array unset ::scsi_drvr
  493.     array unset ::scsi_node
  494.     set info ""
  495.     catch { exec dmesg -s 65535 } info  
  496.     #puts " dmesg length [llength $info] "
  497.     set lines [split $info \n]
  498.     foreach line $lines {
  499.         set sl [remove_empties [split $line]]
  500.         if {[string range [lindex $sl 0] 0 3] == "scsi"} {
  501.             set n [string range [lindex $sl 0] 4 end]
  502.             #puts "scsi$n has driver: [lrange $sl 2 end]"
  503.             if {[join [lrange $sl 2 end]] == "SCSI emulation for USB Mass Storage devices"} {
  504.                 set ::scsi_drvr($n) "usb_storage"
  505.             } else {
  506.                 set ::scsi_drvr($n) [lrange $sl 2 end]
  507.             }
  508.         }
  509.         if {[lindex $sl 0] == "sd"} {
  510.             if {[lindex $sl 2] == "Attached"} {
  511.                 if {[lindex $sl 3] == "scsi"} {
  512.                     if {[lindex $sl 4] == "removable"} {
  513.                         set n [lindex [split [lindex $sl 1] :] 0]
  514.                         #puts "scsi$n == [lindex $sl 6] "
  515.                         set ::scsi_node([lindex $sl 6]) $n
  516.                     }
  517.                     if {[lindex $sl 4] == "disk"} {
  518.                         set n [lindex [split [lindex $sl 1] :] 0]
  519.                         #puts "scsi$n == [lindex $sl 5] "
  520.                         set ::scsi_node([lindex $sl 5]) $n
  521.                     }
  522.                 }
  523.             }
  524.         }
  525.     }
  526.  
  527. #Jun 15 03:21:33 (none) user.info kernel: scsi2 : sata_nv
  528. #Jun 15 03:21:33 (none) user.info kernel: scsi3 : sata_nv
  529. #Jun 15 03:21:33 (none) user.notice kernel: sd 2:0:0:0: Attached scsi disk sda
  530. #Jun 14 19:22:26 (none) user.info kernel: scsi4 : SCSI emulation for USB Mass Storage devices
  531. #Jun 14 19:22:26 (none) user.info kernel: scsi5 : SCSI emulation for USB Mass Storage devices
  532. #Jun 14 19:22:31 (none) user.notice kernel: sd 4:0:0:0: Attached scsi removable disk sdb
  533. #Jun 14 19:22:31 (none) user.notice kernel: sd 4:0:0:1: Attached scsi removable disk sdc
  534. #Jun 14 19:22:31 (none) user.notice kernel: sd 4:0:0:2: Attached scsi removable disk sdd
  535. #Jun 14 19:22:31 (none) user.notice kernel: sd 4:0:0:3: Attached scsi removable disk sde
  536. #Jun 14 19:22:31 (none) user.notice kernel: sd 5:0:0:0: Attached scsi removable disk sdf
  537. #Jun 14 19:22:31 (none) user.notice kernel: sd 5:0:0:1: Attached scsi removable disk sdg
  538. #Jun 15 03:21:33 (none) user.notice kernel: sd 2:0:0:0: Attached scsi disk sda
  539.  
  540.  
  541.     set fid [open "/var/log/messages" r]
  542.     while {![eof $fid]} {
  543.         gets $fid str
  544.         set sl [remove_empties [split $str]]
  545.         if {[lindex $sl 4] == "user.info"} {
  546.             if {[lindex $sl 5] == "kernel:"} {
  547.                 #puts "[string range [lindex $sl 6] 0 3]"
  548.                 if {[string range [lindex $sl 6] 0 3] == "scsi"} {
  549.                     set n [string range [lindex $sl 6] 4 end]
  550.                     #puts "scsi$n has driver: [lrange $sl 8 end]"
  551.                     if {[join [lrange $sl 8 end]] == "SCSI emulation for USB Mass Storage devices"} {
  552.                         set ::scsi_drvr($n) "usb_storage"
  553.                     } else {
  554.                         set ::scsi_drvr($n) [lrange $sl 8 end]
  555.                     }
  556.                 }
  557.             }
  558.         }
  559.         if {[lindex $sl 6] == "sd"} {
  560.             if {[lindex $sl 8] == "Attached"} {
  561.                 if {[lindex $sl 9] == "scsi"} {
  562.                     if {[lindex $sl 10] == "removable"} {
  563.                         set n [lindex [split [lindex $sl 7] :] 0]
  564.                         #puts "scsi$n == [lindex $sl 12] "
  565.                         set ::scsi_node([lindex $sl 12]) $n
  566.                     }
  567.                     if {[lindex $sl 10] == "disk"} {
  568.                         set n [lindex [split [lindex $sl 7] :] 0]
  569.                         #puts "scsi$n == [lindex $sl 11] "
  570.                         set ::scsi_node([lindex $sl 11]) $n
  571.                     }
  572.                 }
  573.             }
  574.         }
  575.         
  576.     }
  577.     close $fid
  578. }
  579.  
  580. proc scsi_or_usb_grabinfos {} {
  581.     array unset ::scsi_N
  582.  
  583.     array unset scsi_ctrlr
  584.  
  585.     catch {
  586.         set fid [open "/proc/scsi/scsi" r]
  587.         set host_n ""
  588.         set vend ""
  589.         set modl ""
  590.         while {![eof $fid]} {
  591.             gets $fid str
  592.             set sl [remove_empties [split $str]]
  593.             if {[lindex $sl 0] == "Host:"} {
  594.                 set host_n [string map {scsi {}} [lindex $sl 1]]
  595.                 set scsi_ctrlr($host_n) "Present"
  596.             }
  597.             if {[lindex $sl 0] == "Vendor:"} {
  598.                 set m_at [lsearch $sl "Model:"]
  599.                 set r_at [lsearch $sl "Rev:"]
  600.                 set vend ""
  601.                 set modl ""
  602.                 if {0 != [expr $m_at-1]} {
  603.                     set vend [join [lrange $sl 1 [expr $m_at-1]]]
  604.                 }
  605.                 if {$m_at != [expr $r_at-1]} {
  606.                     set modl [join [lrange $sl [expr $m_at+1] [expr $r_at-1]]]
  607.                 }
  608.             }
  609.             if {[lindex $sl 0] == "Type:"} {
  610.                 set r_at [lsearch $sl "ANSI"]
  611.                 set type ""
  612.                 if {0 != [expr $m_at-1]} {
  613.                     set type [join [lrange $sl 1 [expr $r_at-1]]]
  614.                 }
  615.                 set ::scsi_N($host_n)  "$vend|$modl|$type"
  616.                 if {$::debug} { puts " ::scsi_N($host_n) = $vend|$modl|$type"}
  617.             }
  618.         }
  619.         close $fid
  620.     }
  621.     
  622.     catch {
  623.         foreach ctrlr [array names scsi_ctrlr] {
  624.             set hostis "scsi"
  625.             catch {
  626.                 if {[string match -nocase *usb* $::scsi_drvr($ctrlr)] == 1} {
  627.                     set hostis "usb"
  628.                 }
  629.             }
  630.             catch {
  631.                 set fid [open "/proc/scsi/usb-storage/$ctrlr" r]
  632.                 while {![eof $fid]} {
  633.                     gets $fid str
  634.                     set hostis "usb"
  635.                 }
  636.                 close $fid
  637.             }
  638.             
  639.             catch {
  640.                 if {[string match -nocase *sata* $::scsi_drvr($ctrlr)] == 1} {
  641.                     set hostis "sata"
  642.                 }
  643.             }
  644.             set vend [lindex [split $::scsi_N($ctrlr) |] 0]
  645.             if {$vend == "ATA"} { 
  646.                 set hostis "sata"
  647.             }
  648.             #puts "scsi$ctrlr Host is really $hostis"
  649.             set info $::scsi_N($ctrlr)
  650.             set ::scsi_N($ctrlr) "$hostis|$info"
  651.         }
  652.     }
  653. }
  654.  
  655.  
  656. proc mainDeviceList__derive {} {
  657.     set dev_list [lsort -dictionary [array names ::diskStats]]
  658. if {$::debug} {puts "$$ dev_list  $dev_list"}
  659.     set omit_parts "false"
  660.     foreach dev $dev_list {
  661.  
  662.         if {[string match "$omit_parts*" $dev] == 1} {
  663.             continue;
  664.         }
  665.         set omit_parts "false"
  666.  
  667.         set ds [split $::diskStats($dev) .]
  668.         set dev_type [lindex $ds 3]
  669.         set hw_type ""
  670.         set fs ""
  671.         set part ""
  672.         set mnt ""
  673.         set vol ""
  674.         set hw_name ""
  675.         set driver ""
  676.         catch {set part [lindex $::partitions($dev) 0] }
  677.         catch {set mnt [lindex [split $::mounteds(/dev/$dev) |] 0] }
  678.         catch {set fs [lindex [split $::mounteds(/dev/$dev) |] 1] }
  679.         if {$part == "extended"} { continue }
  680.         set noscan false
  681.         switch $dev_type {
  682.             floppy {
  683.                 set fdinfo ""
  684.                 set hw_type "floppy"
  685.                 catch " exec $::mut/bin/fdinfo /dev/$dev" fdinfo
  686.                 if {[llength [split $fdinfo |]]>3} {
  687.                     set fdx [lindex [split $fdinfo |] 1]
  688.                     if {$fdx != 5} { set fdx 3 }
  689.                 }
  690.                 catch {
  691.                     if {$::NoScanMedia($dev)=="noscan"} { set noscan true }
  692.                 }
  693.             }
  694.             disk {
  695.                 set noscan true
  696.                 if {[lindex $ds 2] == "sd" } {
  697.                     set hw_type "unknown_scsidisk"
  698.                     catch {
  699.                         set ctrlr $::scsi_node($dev)
  700.                         set ctrlr_info [split $::scsi_N($ctrlr) |]
  701.                         set hw_type "[lindex $ctrlr_info 0]disk"
  702.                         set hw_name [lindex $ctrlr_info 2]
  703.                         set driver $::scsi_drvr($ctrlr)
  704.                     }
  705.                     
  706.             #Here We check for SuperFloppy format:
  707.                     if {$part != "extended"} {
  708.                         set fsinfo ""
  709.                         catch "exec $::mut/bin/guess_fstype /dev/$dev" fsinfo
  710.                         if {$fsinfo != ""} {set fs $fsinfo}
  711.                         if {[lindex [split $fsinfo] 0] == "unknown"} {set fs ""}
  712.                         if {$fs != ""} {
  713.                             set parts 0
  714.                             set unknownparts 0
  715.                             foreach d $dev_list {
  716.                                 if {[string match "$dev*" $d]==1} { 
  717.                                     if {$d != $dev} {
  718.                                         set fsinfo ""
  719.                                         catch "exec $::mut/bin/guess_fstype /dev/$d" fsinfo
  720.                                         if {$fsinfo == "unknown"} { incr unknownparts }
  721.                                         incr parts
  722.                                     }
  723.                                 }
  724.                             }
  725.                             if {$parts == $unknownparts} {
  726.                                 set noscan false
  727.                                 set dev_type superfloppy
  728.                                 set omit_parts "$dev"
  729.                             } else {
  730.                                 set fs ""
  731.                             }
  732.                         }
  733.                     }
  734.                 } else {
  735.                     if {[string range [lindex $ds 2] 0 2] == "ide" } {
  736.                         set hw_type "idedisk"
  737.                         set driver "ide-disk"
  738.                         set hw_name [scan_ide_medium [lindex $ds 2] $dev model]
  739.                     }
  740.                 }
  741.             }
  742.             part {
  743.                 set noscan false
  744.                 if {$fs == "" && $part != "extended"} {
  745.                     set fsinfo ""
  746.                     catch "exec $::mut/bin/guess_fstype /dev/$dev" fsinfo
  747.                     if {$fsinfo != ""} {set fs $fsinfo}
  748.                 }
  749.             }
  750.             cdrom {
  751.                 set cdinfo [split $::cdromMedium($dev) |]
  752.                 set ok1 "false"; set ok2 "false"
  753.                 set noscan true
  754.  
  755.                 if {[lindex $cdinfo 7] == "Audio"} { 
  756.                     if { [lindex $cdinfo 8] > 0 } {
  757.                         set fs [lindex $cdinfo 7]; set vol "Audio [lindex $cdinfo 8] tracks" 
  758.                     }
  759.                 }
  760.                 if {[lindex $cdinfo 7] == "Mixed"} { 
  761.                     if { [lindex $cdinfo 8] > 0 } {
  762.                         set fs [lindex $cdinfo 7]; set vol "Mixed [lindex $cdinfo 8] tracks" 
  763.                     }
  764.                 }
  765.                 if {[lindex $cdinfo 7] == "Data" || [lindex $cdinfo 7] == "Mixed"} {
  766.                     if { [lindex $cdinfo 9] > 0 } {
  767.                         set fs [lindex $cdinfo 7]; 
  768.                         set vol [grab_fs_vol iso9660 $dev]
  769.                     }
  770.                 }
  771.                 if {[string range [lindex $ds 2] 0 2] == "ide" } {
  772.                     set hw_type "idecdrom"
  773.                     set hw_name [scan_ide_medium [lindex $ds 2] $dev model]
  774.                     set driver "ide-cdrom"
  775.                 } else {
  776.                     set hw_type "nonidecdrom"
  777.                     set driver "unknown"
  778.                 }
  779.  
  780.             }
  781.             default { if {$::debug} {puts "Unknown device type $dev_type on $dev"} }
  782.         }
  783.         if {$fs=="" && $noscan==false && $part!="extended"} {
  784.             set fsinfo ""
  785.             catch "exec $::mut/bin/guess_fstype /dev/$dev" fsinfo
  786.             if {$fsinfo != ""} {set fs $fsinfo}
  787.             if {$fs == "unknown"} { set fs "" }
  788.         }
  789.  
  790.         if {$vol=="" && $noscan==false} {
  791.             if {$fs == "unknown"} { set fs "" }
  792.             if {$fs == "iso9660" && $vol==""} { set vol [grab_fs_vol iso9660 $dev] }
  793.             if {$fs == "vfat"} { set vol [grab_fs_vol vfat $dev] }
  794.             if {$fs == "ext2"} { set vol [grab_fs_vol ext2 $dev] }
  795.         }
  796.         if {$dev_type == "floppy"} {
  797.             set driver fd
  798.             if {$noscan} {
  799.                 set part "unknown"
  800.             } else {
  801.                 if {$part == "" && $fs!=""} {
  802.                     set part "Disc"
  803.                 } else {
  804.                     set part "No_Disc"
  805.                 }
  806.             }
  807.         }
  808.  
  809.         if {$part =="" && $fs=="" && $mnt == "" &&  $vol == ""} {continue}
  810.  
  811.         lappend ::infolist "$dev|$hw_type|$hw_name|$part|$mnt|$fs|$vol|$dev_type|$driver|"
  812.         if {$::debug} {puts "::infolist  [lindex $::infolist end]"}
  813.     }
  814. }
  815.  
  816. proc grab_block_devices {{quick false}} {
  817.     majorBus__get_list
  818.     partitions__get_list
  819.     device_nodes__scan_for_make_additional
  820.     diskStats__get_list quick
  821.     cdromMedium_get_list quick
  822.     etc_fstab__get_list
  823.     mounteds__get_list
  824.     scsi_host_to_sd_location 
  825.     scsi_or_usb_grabinfos
  826.     mainDeviceList__derive
  827. }
  828.  
  829. proc run_full_scan {} {
  830.     array unset ::partitions
  831.     array unset ::mounteds
  832.  
  833.     #scan hardware
  834.     grab_block_devices true
  835.  
  836. }
  837.  
  838.  
  839.  
  840.  
  841. proc Show_Devices {} {
  842.     foreach str $::infolist {
  843.         set info [split $str "|"]
  844.  
  845.         set dev [lindex $info 0]
  846.         set physical_type [lindex $info 1]
  847.         set hw_name [lindex $info 2]
  848.         set part [lindex $info 3]
  849.         set mnt [lindex $info 4]
  850.         set fs [lindex $info 5]
  851.         set vol [lindex $info 6]
  852.         set dev_type [lindex $info 7]
  853.         set driver [lindex $info 8]
  854.  
  855.     puts "/dev/$dev|$dev_type|$physical_type|$hw_name|$fs|$vol|$driver"
  856.  
  857.     }
  858. }
  859.  
  860.  
  861.  
  862. ## Initialise global variables here:
  863.  
  864. set Version "0.0.9b";                      # Version Number
  865. set mut [homepath];                       # Home path to app directory, Now done automatically! :-)
  866. set debug 0;                              # Choose output of debugging information, output is to stdout
  867. set last_hotplug 0;                       # USB SCSI hotplug last refresh info, for hot [un]plug scanning.
  868. set Kernel_Version ""
  869. set Program_Initialized false;            # Make start quicker.
  870.  
  871. # get the Kernel version.
  872. proc get_kv {} {
  873.     set ::Kernel_Version "Unknown"
  874.     catch {
  875.         set fid [open "/proc/version" r]
  876.         while {![eof $fid]} {
  877.             gets $fid str
  878.             set ::Kernel_Version "[string range [lindex [split $str] 2] 0 2]"
  879.             break;
  880.         }
  881.         close $fid
  882.     }
  883. }
  884. get_kv
  885.  
  886. ## global variables used to describe attached hardware:
  887.  
  888.  
  889. array unset partitions ;                  # List of info from /proc/partitions
  890. array unset mounteds ;                    # List of info from /proc/mounts
  891. set Stack_rearrange 0 ;                   # Stack the window rearrange events
  892. array unset etc_fstab ;                   # save info from /etc/fstab file.
  893. array unset cdromMedium ;                 # List of info from bin/cdrominfo about cdrom drives in system
  894. array unset diskStats ;                   # info about block devices in system
  895. set grabdevs {fd hd sd scd sr} ;          # list of device types to scan in /proc/diskstats & /proc/partitions
  896. set existing_nodes "" ;                   # Keep track of nodes that exist in /dev directory.
  897. array unset majorBus ;                    # block device info from /proc/devices
  898. array unset majorBusRev ;                 # reverse lookup of majorBus, not always best to use! heh :)
  899. set infolist "";                          # The list of displayed devices & partitions.
  900.  
  901. ## Process command line arguments, debuging flag only at this point
  902.  
  903. if {$argc > 0} {
  904.     for {set i 0} {$i < $argc} {incr i} {
  905.         set sw [string trimleft [lindex $argv $i] "- "]
  906.         switch $sw {
  907.             "d" {set debug 1 ; puts "debugging on"}
  908.             "debug" {set debug 1 ; puts "debugging on"}
  909.             default {}
  910.         }
  911.     }
  912. }
  913.  
  914. ## MUT version information
  915.  
  916. if {$debug} {puts "'MUT version $::Version' on a $::Kernel_Version kernel, bugs, fixes, suggestions, email: Jesse.Liley@gmail.com"}
  917.  
  918. if {$debug} {puts "Program started"}
  919.  
  920. #only one output list for this program
  921. full_refresh_display
  922.  
  923.  
  924.