Advertisement
Guest User

Untitled

a guest
Feb 21st, 2010
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.20 KB | None | 0 0
  1. #!/bin/sh
  2. . /root/idle_disks.conf
  3.  
  4. log() {
  5.    time=$(date '+%b %e %H:%M:%S')
  6.    echo "$time: $*" >> $LOG_FILE
  7. }
  8.  
  9. vlog() {
  10.    if [ $VERBOSE -gt 0 ]; then
  11.       log $@
  12.    fi
  13. }
  14.  
  15. usbid() {
  16.    # Find the USB IDs for a given sdX.
  17.    # If the disk is not USB, these values will be empty
  18.    vid=$(cat /sys/block/$1/device/../../../../idVendor  2>/dev/null)
  19.    pid=$(cat /sys/block/$1/device/../../../../idProduct 2>/dev/null)
  20. }
  21.  
  22. rm $STATUS_DIR/sd[a-z].stat 2>/dev/null
  23. rm $STATUS_DIR/sd[a-z].idlecount 2>/dev/null
  24.  
  25. cd /dev
  26. while [ true ]; do
  27.    for disk in sd[a-z]; do
  28.  
  29.       # Check if it is a USB disk, and if it is in a whitelist
  30.       usbid $disk
  31.       grep -q "^$pid" $WHITELIST/$vid 2>/dev/null
  32.       if [ $? -eq 0 ]; then
  33.          stat_file=$STATUS_DIR/$disk.stat
  34.          count_file=$STATUS_DIR/$disk.idlecount
  35.  
  36.          newstat=$(cat /sys/block/$disk/stat 2>/dev/null)
  37.          curstat=$(cat $stat_file 2>/dev/null)
  38.          count=$(cat $count_file 2>/dev/null)
  39.  
  40.          if [ "$newstat" != "$curstat" ]; then
  41.             # The status did change, get back to default timeout
  42.             echo "$newstat" > $stat_file
  43.             echo $((TIMEOUT*6)) > $count_file
  44.             if [ "$count" -eq 0 ]; then
  45.                log "/dev/$disk: new activity detected"
  46.             fi
  47.          else
  48.             if [ $count -gt 1 ]; then
  49.                # Decrement the idlecounter, since the status did not change
  50.                echo $(($count-1)) > $count_file
  51.             else
  52.                if [ $count -eq 1 ]; then
  53.                   # If the idle counter is at 1, lets stop the disk
  54.                   sync
  55.                   scsi-stop /dev/$disk
  56.                   log "/dev/$disk: spinned down"
  57.  
  58.                   # After the sync, the I/O stat has changed. Update it.
  59.                   cp /sys/block/$disk/stat $stat_file
  60.                   echo 0 > $count_file
  61.                fi
  62.             fi
  63.          fi
  64.       else
  65.          # Some debugging messages
  66.          if [ "$vid" -a "$pid" ]; then
  67.             vlog "/dev/$disk: ignored (USB ID $vid:$pid not in whitelist)"
  68.          else
  69.             vlog "/dev/$disk: ignored (not an USB device)"
  70.          fi
  71.       fi
  72.    done
  73.    
  74.    sleep 10
  75. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement