USB Festplatten unter Linux sicher in Standby schicken

USB Festplatten unter Linux sicher in Standby schicken

#!/bin/bash
#
# Automatic disk standby using kernel diskstats and hdparm https://github.com/lynix/scripts
#
# Version 1.1, 2011 by Alexander Koch <lynix47@gmail.com>
# Version 1.2 2015 Allow device links instead of sda/sdb... and retry non-existing devices every 600 seconds
#

# DEVICES syntax: DEVICE:TIMEOUT_SEC
DEVICES=( "sda:120" "sdb:120" "bigwd8:120" "bigwd9:120" "bigwd6:120" )

function device_worker() {
 logger -t hdd-spindown.sh "spawned monitor thread for $1"
 COUNT_R=0
 COUNT_W=0
 INSTDBY=0
 while true; do
 while ! test -e "/dev/$1"; do
 sleep 600
 COUNT_R=0
 COUNT_W=0
 INSTDBY=0
 done
 devname=$(basename $(readlink -f "/dev/$1") )
 NEW_R=$(awk '{print $1}' /sys/block/$devname/stat)
 NEW_W=$(awk '{print $5}' /sys/block/$devname/stat)
 if [ $COUNT_R -eq $NEW_R ] && [ $COUNT_W -eq $NEW_W ]; then
 if [ $INSTDBY -eq 0 ] || [[ $(hdparm -C /dev/$1 | grep active) > 0 ]]; then
 INSTDBY=1
 logger -t hdd-spindown.sh "suspending $1"
 hdparm -qy /dev/$1
 if [ $? -gt 0 ]; then
 logger "failed to suspend $1"
 return 1
 fi
 fi
 else
 COUNT_R=$NEW_R
 COUNT_W=$NEW_W
 INSTDBY=0
 fi
 sleep $2
 done
}

for D in ${DEVICES[@]}; do
 device_worker $(echo "$D" | cut -d ':' -f 1) $(echo "$D" | cut -d ':' -f 2) &
done

exit 0

 

Schreibe einen Kommentar