Skip to content

Instantly share code, notes, and snippets.

@KyonLi
Last active April 24, 2023 11:21
Show Gist options
  • Select an option

  • Save KyonLi/d1e8b6243ad0c903fe0990bb63f37697 to your computer and use it in GitHub Desktop.

Select an option

Save KyonLi/d1e8b6243ad0c903fe0990bb63f37697 to your computer and use it in GitHub Desktop.

Revisions

  1. KyonLi revised this gist Apr 24, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    /share/Public/scripts/disk_standby.sh start
    ```
    2. 在共享文件夹`Public`中创建`scripts`目录,向其中放入[disk_standby.sh](#disk_standby.sh),添加执行权限`chmod +x disk_standby.sh`
    2. 在共享文件夹`Public`中创建`scripts`目录,向其中放入[disk_standby.sh](#file-disk_standby-sh),添加执行权限`chmod +x disk_standby.sh`
    3. 添加定时任务
    ```shell
    echo "0 4 * * * /share/Public/scripts/disk_standby.sh readd" >> /etc/config/crontab
  2. KyonLi revised this gist Apr 24, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    /share/Public/scripts/disk_standby.sh start
    ```
    2. 在共享文件夹`Public`中创建`scripts`目录,向其中放入[disk_standby.sh](disk_standby.sh),添加执行权限`chmod +x disk_standby.sh`
    2. 在共享文件夹`Public`中创建`scripts`目录,向其中放入[disk_standby.sh](#disk_standby.sh),添加执行权限`chmod +x disk_standby.sh`
    3. 添加定时任务
    ```shell
    echo "0 4 * * * /share/Public/scripts/disk_standby.sh readd" >> /etc/config/crontab
  3. KyonLi created this gist Apr 24, 2023.
    15 changes: 15 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # QNAP硬盘休眠
    适用于系统盘为SSD数据盘有HDD的情况,实现开机自动解除HDD系统占用,每天4:00~4:30自动同步数据避免系统出错
    1. 启用开机自启,参考[wiki](https://wiki.qnap.com/wiki/Running_Your_Own_Application_at_Startup),编辑autorun.sh加入以下内容
    ```shell
    #!/bin/sh

    /share/Public/scripts/disk_standby.sh start
    ```
    2. 在共享文件夹`Public`中创建`scripts`目录,向其中放入[disk_standby.sh](disk_standby.sh),添加执行权限`chmod +x disk_standby.sh`
    3. 添加定时任务
    ```shell
    echo "0 4 * * * /share/Public/scripts/disk_standby.sh readd" >> /etc/config/crontab
    echo "30 4 * * * /share/Public/scripts/disk_standby.sh remove" >> /etc/config/crontab
    crontab /etc/config/crontab && /etc/init.d/crond.sh restart
    ```
    60 changes: 60 additions & 0 deletions disk_standby.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/bin/bash

    MD_DEVICES="md9 md13"
    SYS_DEV=$(df $(getcfg SHARE_DEF defVolMP -f /etc/config/def_share.info) | awk '/^\/dev/ {print $1}')

    turn_off_swap(){
    echo "Turning off swap on HDD..."
    for partition in $(cat /proc/swaps | grep partition | awk '{print $1}'); do
    swapoff $partition
    done
    }

    remove_from_raid(){
    for md in $MD_DEVICES; do
    echo "Disconnecting HDD from /dev/${md}..."
    save=
    for partition in $(mdadm -D /dev/${md} | grep 'active sync' | awk 'NF{ print $NF }'); do
    disk=${partition//[[:digit:]]/}
    disk=${disk#"/dev/"}
    if [ $(cat /sys/block/${disk}/queue/rotational) = 1 ]; then
    mdadm /dev/${md} --fail ${partition}
    save=${save}${partition}$'\n'
    fi
    done
    [ -n "$save" ] && echo -n "$save" > /tmp/disk_standby_${md}
    done
    }

    readd_to_raid(){
    for md in $MD_DEVICES; do
    [ ! -f "/tmp/disk_standby_${md}" ] && break
    echo "Reconnecting HDD to /dev/${md}..."
    for partition in $(cat /tmp/disk_standby_${md}); do
    mdadm /dev/${md} --re-add ${partition}
    done
    rm "/tmp/disk_standby_${md}"
    done
    }

    case "$1" in
    start)
    turn_off_swap
    remove_from_raid
    ;;
    remove)
    remove_from_raid
    ;;
    readd)
    readd_to_raid
    ;;
    *)
    echo "Usage:"
    echo " $0 [command]"
    echo " command:"
    echo " start - turn off swap and remove hdd from raid1."
    echo " remove - remove hdd from raid1."
    echo " readd - readd hdd to raid1."
    exit 0
    ;;
    esac