Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- . /root/idle_disks.conf
- log() {
- time=$(date '+%b %e %H:%M:%S')
- echo "$time: $*" >> $LOG_FILE
- }
- vlog() {
- if [ $VERBOSE -gt 0 ]; then
- log $@
- fi
- }
- usbid() {
- # Find the USB IDs for a given sdX.
- # If the disk is not USB, these values will be empty
- vid=$(cat /sys/block/$1/device/../../../../idVendor 2>/dev/null)
- pid=$(cat /sys/block/$1/device/../../../../idProduct 2>/dev/null)
- }
- rm $STATUS_DIR/sd[a-z].stat 2>/dev/null
- rm $STATUS_DIR/sd[a-z].idlecount 2>/dev/null
- cd /dev
- while [ true ]; do
- for disk in sd[a-z]; do
- # Check if it is a USB disk, and if it is in a whitelist
- usbid $disk
- grep -q "^$pid" $WHITELIST/$vid 2>/dev/null
- if [ $? -eq 0 ]; then
- stat_file=$STATUS_DIR/$disk.stat
- count_file=$STATUS_DIR/$disk.idlecount
- newstat=$(cat /sys/block/$disk/stat 2>/dev/null)
- curstat=$(cat $stat_file 2>/dev/null)
- count=$(cat $count_file 2>/dev/null)
- if [ "$newstat" != "$curstat" ]; then
- # The status did change, get back to default timeout
- echo "$newstat" > $stat_file
- echo $((TIMEOUT*6)) > $count_file
- if [ "$count" -eq 0 ]; then
- log "/dev/$disk: new activity detected"
- fi
- else
- if [ $count -gt 1 ]; then
- # Decrement the idlecounter, since the status did not change
- echo $(($count-1)) > $count_file
- else
- if [ $count -eq 1 ]; then
- # If the idle counter is at 1, lets stop the disk
- sync
- scsi-stop /dev/$disk
- log "/dev/$disk: spinned down"
- # After the sync, the I/O stat has changed. Update it.
- cp /sys/block/$disk/stat $stat_file
- echo 0 > $count_file
- fi
- fi
- fi
- else
- # Some debugging messages
- if [ "$vid" -a "$pid" ]; then
- vlog "/dev/$disk: ignored (USB ID $vid:$pid not in whitelist)"
- else
- vlog "/dev/$disk: ignored (not an USB device)"
- fi
- fi
- done
- sleep 10
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement