最近组装了一台家庭服务器,上面的硬盘绝大多数时间都处于空闲状态。如果能够将硬盘设置为自动休眠,不仅可以延长硬盘寿命,也能节省点电费。
首先安装hdparm:sudo apt install hdparm
检查硬盘信息:
blunt@homelab:~$ sudo fdisk -l
Disk /dev/nvme0n1: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: YMTC YMSS1ED04B21MC
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 64C29332-4863-4C13-AD46-5E8F4F4638BD
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 1050623 1048576 512M EFI System
/dev/nvme0n1p2 1050624 498116607 497065984 237G Linux filesystem
Disk /dev/sda: 12.73 TiB, 14000519643136 bytes, 27344764928 sectors
Disk model: WDC WD140EDFZ-11
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: C91B005D-3B44-45C2-A43A-C639004A27D4
可以看到机械硬盘路径为/dev/sda。
接下来检查硬盘休眠状态
blunt@homelab:~$ sudo hdparm -C /dev/sda
/dev/sda:
drive state is: unknown
这里状态显示未unknown,表示暂时未识别,不要紧接下来就给它设置自动休眠。
查看硬盘是否支持write cache:
blunt@homelab:~$ sudo hdparm -I /dev/sda | grep "Write cache"
* Write cache
write cache前面有星号代表支持。
修改hdparm配置,打开/etc/hdparm.conf文件,向文件末尾追加:
/dev/sda {
# 如果上一步write cache前没有星号,将on改为off
write_cache = on
spindown_time = 120
}
重载配置文件:
blunt@homelab:~$ sudo /usr/lib/pm-utils/power.d/95hdparm-apm resume
/dev/sda:
setting Advanced Power Management level to 0xfe (254)
APM_level = 254
/dev/sda:
setting standby to 120 (10 minutes)
看到setting standby to 120 (10 minutes)意味着硬盘已经设置为空闲10分钟后休眠
最后过十分钟来检查下硬盘是否真的进入休眠状态:
blunt@homelab:~$ sudo hdparm -C /dev/sda
/dev/sda:
drive state is: active/idle
--------15 minutes after--------
blunt@homelab:~$ sudo hdparm -C /dev/sda
/dev/sda:
drive state is: standby
drive state is: standby表示硬盘成功进入休眠状态~