Initial commit

This commit is contained in:
Fierelier 2023-09-27 23:55:21 +02:00
commit 4cb0805577
7 changed files with 104 additions and 0 deletions

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

13
README.txt Normal file
View File

@ -0,0 +1,13 @@
hdd-speen keeps disks from spinning down, which can prolong their life.
Prerequisites:
* python3
* python3-pyinotify
* hdparm
Installation:
sudo ./install
It installs itself to /etc/boot.d and /etc/pm/sleep.d (since disk properties are lost on sleep). This works if your system doesn't use systemd, and you use pm-utils to suspend.
env contains a bunch of installation-specific settings.

3
bin/boot Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
DISTRO="$(basename "$(realpath -s "$BASH_SOURCE")")"
nohup "$DISTRO" &> /dev/null &

46
bin/main Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python3
import sys
import os
import subprocess
def isDisk(disk):
part = disk.rsplit("-",1)[-1]
for i in "0123456789": part = part.replace(i,"")
if part == "part": return False
try:
diskName = os.path.realpath(disk).rsplit("/",1)[-1]
with open("/sys/class/block/" +diskName+ "/device/type") as f: diskType = f.read().strip("\n")
except:
return False
if diskType != "0": return False
return True
def applyProperties(disk):
print("APPLY: " +disk.rsplit("/",1)[-1]+ " ... ",end="")
sys.stdout.flush()
proc = subprocess.Popen(["hdparm","-B","255","-S","251",disk],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
print(proc.wait())
def applyToAll():
for root,dirs,files in os.walk("/dev/disk/by-id"):
for file in files:
ffile = os.path.join("/dev/disk/by-id",file)
if isDisk(ffile): applyProperties(ffile)
return
# DAEMON
import pyinotify
class handler(pyinotify.ProcessEvent):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
applyToAll()
if "--oneshot" in sys.argv[1:]:
sys.exit(0)
def process_IN_CREATE(self, event):
if isDisk(event.pathname): applyProperties(event.pathname)
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm,handler())
wm.add_watch('/dev/disk/by-id', pyinotify.IN_CREATE)
notifier.loop(daemonize=False)

7
bin/resume Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
DISTRO="$(basename "$(realpath -s "$BASH_SOURCE")")"
case "$1" in
resume|thaw)
"$DISTRO" --oneshot
;;
esac

4
env Normal file
View File

@ -0,0 +1,4 @@
DISTRO="${DISTRO:=hdd-speen}"
INSTALL_DIR="${INSTALL_DIR:=/usr/local/sbin}"
BOOT_DIR="${BOOT_DIR:=/etc/boot.d}"
PMUTILS_DIR="${PMUTILS_DIR:=/etc/pm}"

22
install Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -e
SCRIPT_DIR="$(dirname "$(realpath -s "$BASH_SOURCE")")"
source "$SCRIPT_DIR/env"
# Main program
mkdir -p "$INSTALL_DIR"
cp "$SCRIPT_DIR/bin/main" "$INSTALL_DIR/$DISTRO"
chown root "$INSTALL_DIR/$DISTRO"
chmod 755 "$INSTALL_DIR/$DISTRO"
# Boot script
mkdir -p "$BOOT_DIR"
cp "$SCRIPT_DIR/bin/boot" "$BOOT_DIR/$DISTRO"
chown root "$BOOT_DIR/$DISTRO"
chmod 755 "$BOOT_DIR/$DISTRO"
# Wake script
mkdir -p "$PMUTILS_DIR/sleep.d"
cp "$SCRIPT_DIR/bin/resume" "$PMUTILS_DIR/sleep.d/$DISTRO"
chown root "$PMUTILS_DIR/sleep.d/$DISTRO"
chmod 755 "$PMUTILS_DIR/sleep.d/$DISTRO"