hdparm is typically used to configure hard drive parameters once at boot, not to run as a continuous daemon. A standard runit service's run script is expected to start a long-running process. If the script exits, runit will try to restart it repeatedly.
To use hdparm with runit, you need a service that performs the configuration and then stays running (or appears to runit as if it's running) to prevent a restart loop. A common pattern for this is to use exec sleep infinity after the configuration commands.
Important Safety Warning:
hdparm is a powerful tool. Incorrect settings can lead to:
- Data loss or corruption: Especially with write cache settings.
- System instability: If performance or power management features are misconfigured.
- Drive damage: In extreme cases, though less common with modern drives.
Always back up your data before making changes, and understand what each hdparm option does. You should test these settings carefully.
Step-by-Step: Creating the hdparm Runit Service
We'll name this service hdparm-config to differentiate it from a continuous daemon.
1. Create the Service Directory
sudo mkdir -p /etc/sv/hdparm-config/log
2. Create the run Script (/etc/sv/hdparm-config/run)
This script will contain your hdparm commands. Modify the hdparm commands to suit your specific drives and desired settings.
#!/bin/sh
# /etc/sv/hdparm-config/run
# Runit service to apply hdparm settings at boot.
# --- IMPORTANT: Configure your devices and hdparm commands below ---
#
# Replace /dev/sda, /dev/sdb, etc., with your actual drive paths.
# You can find these with `lsblk` or `fdisk -l`.
#
# Common hdparm options:
# -B [0-255]: Set Advanced Power Management (APM) level. 254/255 disables it (performance), lower values enable more aggressive power saving.
# -S [0-255]: Set standby (spin-down) timer in 5-second increments (0 = disable).
# -W 0|1: Disable (0) or enable (1) the drive's write cache. Enable for performance, disable for data safety on power loss.
# -A 0|1: Disable (0) or enable (1) the drive's read-ahead.
# -M [0-255]: Set acoustic management level (if supported).
# -d 0|1: Disable (0) or enable (1) DMA. Usually leave at 1 (enabled).
# --- Configuration for /dev/sda (Example: HDD) ---
DEV_SDA="/dev/sda"
if [ -b "$DEV_SDA" ]; then # Check if device exists
echo "Applying hdparm settings to $DEV_SDA..."
# Example: Disable APM (254), disable spindown (0), enable write cache (1)
# This is a common setup for performance on HDDs.
sudo hdparm -B 254 -S 0 -W 1 "$DEV_SDA" &>> /var/log/hdparm-config.log || \
echo "Failed to apply settings to $DEV_SDA" >> /var/log/hdparm-config.log
fi
# --- Configuration for /dev/sdb (Example: SSD) ---
DEV_SDB="/dev/sdb"
if [ -b "$DEV_SDB" ]; then
echo "Applying hdparm settings to $DEV_SDB..."
# For SSDs, often you primarily care about ensuring write cache is enabled for performance.
# APM and spindown are less relevant for SSDs but can be set if needed.
sudo hdparm -W 1 "$DEV_SDB" &>> /var/log/hdparm-config.log || \
echo "Failed to apply settings to $DEV_SDB" >> /var/log/hdparm-config.log
fi
# --- Add more devices and their configurations as needed ---
# DEV_SDC="/dev/sdc"
# if [ -b "$DEV_SDC" ]; then
# echo "Applying hdparm settings to $DEV_SDC..."
# sudo hdparm -W 1 -B 255 "$DEV_SDC" &>> /var/log/hdparm-config.log
# fi
echo "hdparm configuration applied. Service will remain active." >> /var/log/hdparm-config.log
# Crucial: Keep the service running so runit doesn't try to restart it endlessly.
# We exec 'sleep infinity' to achieve this.
exec sleep infinity
3. Create the log/run Script (/etc/sv/hdparm-config/log/run)
This script sets up svlogd to log the output of the main run script.
#!/bin/sh
# /etc/sv/hdparm-config/log/run
# Runit logging for hdparm-config service
exec svlogd -tt /var/log/hdparm-config
4. Set Permissions
Make sure both run scripts are executable.
sudo chmod +x /etc/sv/hdparm-config/run
sudo chmod +x /etc/sv/hdparm-config/log/run
5. Enable the Service
Create a symlink to enable the service. It will start on the next boot, or immediately if runit detects the new symlink.
sudo ln -s /etc/sv/hdparm-config /var/service/
6. Verify Service Status
You can check if the service is running:
sv status hdparm-config
You should see output similar to:
run: hdparm-config: (pid 12345) 1s
7. Check Logs
To see the output of your hdparm commands (including any errors), you can tail the service logs:
sudo tail -f /var/log/hdparm-config/current
# Or using svlogd directly:
# sudo svlogd -f /var/log/hdparm-config
How it Works:
run script: When the service starts, the run script executes.
hdparm commands: Your configured hdparm commands are run.
- Redirection:
&>> /var/log/hdparm-config.log redirects both standard output and standard error of the hdparm command to a log file within the svlogd managed directory. This ensures svlogd picks up the output.
exec sleep infinity: After the hdparm commands finish, exec sleep infinity replaces the current shell process with sleep command. sleep infinity will never exit, so runit sees the service as continuously running, preventing it from attempting to restart it in a loop.
log/run: The svlogd utility captures the output (including the echo messages) and rotates logs automatically.
Reverting/Disabling the Service:
If you need to disable or remove the service:
sudo rm /var/service/hdparm-config # Stop and disable
sudo rm -rf /etc/sv/hdparm-config # Remove service definition
sudo rm -rf /var/log/hdparm-config # Clean up logs
Remember to reboot or manually restart relevant services (if any depend on these hdparm settings) after making changes.