AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 968303
Accepted
Adam
Adam
Asked: 2019-05-22 15:01:45 +0800 CST2019-05-22 15:01:45 +0800 CST 2019-05-22 15:01:45 +0800 CST

如何在计时器上使硬盘待机(降速)?

  • 772

我想让我的硬盘在空闲时减速,比如说 20 分钟。任何分钟数都可以,因为这个 NAS 很少使用。

我尝试过但没有奏效的方法:

  • ataidle -S 20 /dev/ada0只需立即停止驱动器,计时器就不起作用。一旦驱动器由于访问而重新旋转,它们就会保持旋转。

  • camcontrol standby /dev/ada0 -t 1200相同的行为ataidle。

  • FreeNAS UI 的存储 -> 磁盘 -> 高级。电源管理器设置只是调用camcontrol,同样计时器没有效果。如果选择了允许待机的电源设置(例如 127),则驱动器几乎立即降速(可能在 8 秒后),并且如果有任何访问,则不断地升速和降速。更新:请参阅 如何让 FreeNAS 降低磁盘转速? 有关如何进行这项工作的说明并跳过手动脚本。

如何获得正常的预期“如果一段时间不使用则待命”行为?

使用FreeBSD 11.2-STABLE通过FreeNAS 11.2. 驱动器是 4x Samsung 2TB 2.5" .T2000LM003`

# smartctl -P show /dev/ada0
smartctl 6.6 2017-11-05 r4594 [FreeBSD 11.2-STABLE amd64] (local build)
Copyright (C) 2002-17, Bruce Allen, Christian Franke, www.smartmontools.org

Drive found in smartmontools Database.  Drive identity strings:
MODEL:              ST2000LM003 HN-M201RAD
FIRMWARE:           2BC10007
match smartmontools Drive Database entry:
MODEL REGEXP:       ST(1500|2000)LM0(03|04|06|07|10) HN-M[0-9]*RAD
FIRMWARE REGEXP:    .*
MODEL FAMILY:       Seagate Samsung SpinPoint M9T
ATTRIBUTE OPTIONS:  None preset; no -v options are required.
freebsd
  • 2 2 个回答
  • 3777 Views

2 个回答

  • Voted
  1. Best Answer
    Adam
    2019-05-29T20:01:10+08:002019-05-29T20:01:10+08:00

    更新:请参阅 https://serverfault.com/a/965694/184095 ,了解如何使 GUI 工作并跳过下面的手动脚本编写过程。


    我最终非常手动地完成了这项工作。编写一个脚本来检查驱动器活动,如果没有驱动器则关闭驱动器,然后将此脚本安排为 cron 作业。

    我选择了一种方法,iostat用于在一段时间内监控驱动器,在我的情况下为 600 秒(10 分钟)。如果iostat报告没有使用情况,请调用ataidle -s /dev/ada[0123]以挂起驱动器。我设置了一个 cron 作业,每 15 分钟调用一次此脚本:*/15 * * * * spindown_if_idle.sh 600

    spindown_if_idle.sh:

    #!/bin/bash
    
    # argument is the number of seconds to test for drive use, default to 5
    if [ -z "$1" ]; then
        SECONDS=5
    else
        SECONDS="$1"
    fi
    
    function list_ada_drives {
        # emit a list of hard drives, i.e. ada0 ada1 ada2 ...
        iostat -x | grep "ada" | awk '{print $1}'
    }
    
    function spindown {
        # for every hard drive use ataidle to place it in standby
        for ADA in $(list_ada_drives); do
            ataidle -s /dev/$ADA
        done
    }
    
    function are_drives_used {
        # argument is number of seconds to test over
    
        # run iostat for the specified number of seconds and ask to report any usage
        # -z means to omit any drives that are idle
        # iostat will print two reports, the first since boot and the second during the interval
        # The first tail and grep strip off the first useless report. The second tail strips off the header
        # The final grep strips off drives we're not interested in
        iostat -x -z -d $SECONDS 2 | tail -n +2 | grep -A5000  "extended" | tail -n +3 | grep "ada" -q
    }
    
    if are_drives_used $SECONDS ; then
        echo "Drives are being used"
    else
        echo "Drives are idle, going to standby"
        spindown
    fi
    

    收集的指标

    我尝试通过查询collectd. 我曾经rrdtool fetch查询存储在 中的指标/var/db/collectd/rrd/localhost/disk-ada0,但这些指标有几分钟的滞后。依赖它们意味着脚本可能会在最近空闲的情况下使正在使用的驱动器处于备用状态。

    测试驱动器是否旋转

    以下脚本将报告每个驱动器是否空闲或旋转。对测试很有用。

    is_spinning.sh:

    #!/bin/sh
    
    camcontrol devlist | grep ada | awk -F\( '{print $2'} | awk -F",|\\\\)" '{print $2}' |while read LINE
    do
    CM=$(camcontrol cmd $LINE -a "E5 00 00 00 00 00 00 00 00 00 00 00" -r - | awk '{print $10}')
    if [ "$CM" = "FF" ] ; then
    echo "$LINE: SPINNING"
    elif [ "$CM" = "00" ] ; then
    echo "$LINE: IDLE"
    else
    echo "$LINE: UNKNOWN"
    fi
    done
    

    基于此论坛帖子。

    • 1
  2. pelipro
    2019-06-05T11:07:04+08:002019-06-05T11:07:04+08:00

    感谢这个想法。几天来,我一直在为这个问题苦苦挣扎。由于我不习惯编写 bash 脚本,所以我在 python3 中重写了它。也许有人觉得它有用:

    import subprocess
    import time
    
    drives_to_check = ['ada3', 'ada4', 'ada5']
    no_sleep_hours = ['00', '01']
    seconds = 600
    
    if time.strftime("%H") in no_sleep_hours:
        exit()
    
    o = subprocess.check_output(f'/usr/sbin/iostat -x -z -d {seconds} 2', shell=True)
    
    for drive in drives_to_check:
        if drive not in o.decode().split('extended')[-1]:
            p = subprocess.check_output(f'/sbin/camcontrol cmd {drive} -a "E5 00 00 00 00 00 00 00 00 00 00 00" -r -', shell=True)
            if p.decode()[27:29] != '00':
                q = subprocess.check_output(f'/usr/local/sbin/ataidle -s /dev/{drive}', shell=True)
    

    我的小插件是:当您不希望脚本运行时,可以将小时数添加到 no_sleep_hours。并且您必须将要检查的驱动器添加到“drives_to_check”,这样您就可以排除不应进行减速的驱动器。也许你需要调整 iostat 和 ataidle 的路径,这些是 FreeNAS 中使用的路径。您可以使用:which adaidle或which iostat. 您可以将其添加到 cron */15 * * * * /usr/local/bin/python3 /path/to/the/folder/check_disk_usage_and_spindown.py

    • 1

相关问题

  • Mono 2.4 Ahead-Of-Time (AOT) 可以在 FreeBSD x86(或 x64)上编译吗?

  • FreeBSD 和 Linux 有什么区别?[关闭]

  • 在 FreeBSD 上安装和运行 MySql

  • 在 freeBSD 上安装 netbeans 的问题

  • 用于 Web 应用服务器的 FreeBSD 磁盘分区(Apache/MySQL/PHP)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve