我有幾台同時配備 SSD 和傳統硬碟的 Ubuntu 機器,其中硬碟只是偶爾使用。
出於降低噪音、熱量和功耗以及延長硬碟使用壽命的原因,我想在每次啟動時將其關閉,僅在需要時才喚醒它。
hdparm -y
(或-Y
)在命令列上運行得很好並且完全符合我的要求。
但是寫一個systemd服務來執行hdparm
是行不通的。更準確地說:它工作了,磁碟確實進入睡眠狀態(如調試所示),但它立即再次被喚醒(並保持喚醒狀態),因為系統上有某些東西確實訪問了硬碟(從而喚醒它)在systemd 啟動過程的最後。
那麼我怎麼才能將hdparm -y
啟動進程放入足夠晚的時間,以便不再有任何其他進程跟隨。
我的最後一個猜測是將 systemd 的預設目標從圖形更改為新的(sleepydisks),然後該目標取決於先前的圖形目標。
但是有沒有一種更簡單、更簡單的方法來關閉磁碟呢?
問候
答案1
開啟磁碟公用程式並將磁碟機設定為在 10 分鐘不活動後自動停止旋轉。
答案2
使用 hdparm 的 -S 選項。這設定了旋轉超時,該超時應該由驅動器維護。
$ hdparm --help
-S Set standby (spindown) timeout
$ hdparm -S 60 /dev/sdd
/dev/sdd:
setting standby to 60 (5 minutes)
請注意,這是一個大寫的“S”。
答案3
我使用以下腳本「強制」空閒磁碟旋轉,其中強制是指對磁碟機的直接指令,因為許多 WD 磁碟機有自旋轉問題。也許您可以將其擴展以滿足您的需求。該腳本有很好的註釋,因此您應該能夠輕鬆地修改它。我每 20 分鐘調用一次cron
,根據需要進行調整:
spindown.sh
#!/usr/bin/env bash
logger "[SPINDOWN] Checking disk activity and spinning down idles..."
# Exit during maintenance
if pgrep snapraid > /dev/null; then
logger "[SPINDOWN] Detected snapRAID maintenance. Exiting.";
exit 0
fi
# Specify any drives you want to ignore; separate multiple drives by spaces; e.g. "sda sdb"
IGNORE_DRIVES=""
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
# Check for idle disks and spin them down unless smartd is running tests
# Create a file on the ramdisk and cycle it to test for disk activity
( if [ ! -f /dev/shm/diskstats_1 ]; then
logger "[SPINDOWN] Creating initial state file on ramdisk";
touch /dev/shm/diskstats_1;
fi ;
logger "[SPINDOWN] Updating state files on ramdisk";
mv /dev/shm/diskstats_1 /dev/shm/diskstats_2;
cat /proc/diskstats > /dev/shm/diskstats_1 ) > /dev/null 2>&1
# Find all removable USB drives, so we can ignore them later,
# see http://superuser.com/a/465953
REMOVABLE_DRIVES=""
for _device in /sys/block/*/device; do
if echo $(readlink -f "$_device")|egrep -q "usb"; then
_disk=$(echo "$_device" | cut -f4 -d/)
REMOVABLE_DRIVES="$REMOVABLE_DRIVES $_disk"
fi
done
# Append detected removable drives to manually ignored drives
IGNORE_DRIVES="$IGNORE_DRIVES $REMOVABLE_DRIVES"
# Loop through all the array disks and spin down the idle disks. Will find all drives sda > sdz AND sdaa > sdaz...
logger "[SPINDOWN] Looping through drives to detect idle state"
for disk in `find /dev/ -regex '/dev/sd[a-z]+' | cut -d/ -f3`
do
# Skip removable USB drives and those the user wants to ignore
if [[ $IGNORE_DRIVES =~ $disk ]]; then
continue
fi
# Skip SSDs
if [[ $(cat /sys/block/$disk/queue/rotational) -eq 0 ]]; then
continue
fi
# Check if drive exists
if [ -e /dev/$disk ]; then
logger "[SPINDOWN] Checking drive $disk"
# Check if drive is currently spinning
if [ "$(smartctl -i -n standby /dev/$disk | grep "ACTIVE or IDLE")" ]; then
logger "[SPINDOWN] Disk $disk is ACTIVE or IDLE. Checking for selftest"
# Check if smartctl is currently not running a self test
if [ $(smartctl -a /dev/$disk | grep -c "Self-test routine in progress") = 0 ]; then
logger "[SPINDOWN] Disk $disk is not running a self test. Checking activity."
# Check if drive has been non idle since last run
if [ "$(diff /dev/shm/diskstats_1 /dev/shm/diskstats_2 | grep $disk )" = "" ]; then
logger "[SPINDOWN] Spinning down /dev/$disk `df -h | grep /dev/$disk | rev | cut -d ' ' -f 1 | rev`"
hdparm -y /dev/$disk
fi
else
logger "[SPINDOWN] /dev/$disk is running Self-test routine. Skipping."
fi
fi
fi
done
根據特定問題,如何使用 systemd 來做到這一點? ....建立一個呼叫此腳本的計時器檔案(參考:https://wiki.archlinux.org/index.php/Systemd/Timers)