How to Keep a Secondary HDD in Sleep Mode Until Accessed: Monitoring, Kernel Tuning & Power Management


2 views

When dealing with secondary storage that needs to remain mounted but inactive, we face three technical challenges: process monitoring, kernel-level power management, and disk sleep configuration. Let's break this down systematically.

To identify what's waking your disk, Linux provides several powerful tools:

# Method 1: Using fatrace (File Activity Trace)
sudo apt install fatrace
sudo fatrace | grep "/mount/point/of/your/disk"

# Method 2: Using inotifywait
sudo apt install inotify-tools
inotifywait -m -r /mount/point | grep --line-buffered "OPEN"

# Method 3: Using systemtap (advanced)
stap -e 'probe vfs.read,vfs.write {
    if (d_name($path) == "your_disk_mount_point")
        printf("%s %s\\n", execname(), probefunc())
}'

Modern Linux kernels (4.15+) include several parameters for fine-tuning disk sleep behavior. These are particularly important for preventing unnecessary wakeups:

# Check current disk power management settings
hdparm -B /dev/sdX
hdparm -S /dev/sdX

# Recommended settings for our use case:
sudo hdparm -B 127 /dev/sdX  # Balanced power mode
sudo hdparm -S 241 /dev/sdX  # 30-minute standby timeout

The key kernel parameters to adjust in /etc/sysctl.conf:

vm.laptop_mode = 5
vm.dirty_writeback_centisecs = 6000
vm.dirty_expire_centisecs = 6000

For persistent configuration that survives reboots, we can create a udev rule:

# /etc/udev/rules.d/99-hdd-power.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[b-z]", RUN+="/usr/bin/hdparm -B 127 -S 241 /dev/%k"

Certain filesystem features can prevent proper disk sleeping:

# For ext4, disable unnecessary access time updates
sudo tune2fs -o nobarrier,noatime,nodiratime /dev/sdX1

# If using NTFS (via ntfs-3g), add these mount options:
rw,noatime,nodiratime,users,umask=000,uid=1000,gid=1000

Create a service to manage disk state more intelligently:

# /etc/systemd/system/hdd-sleep.service
[Unit]
Description=HDD Sleep Manager

[Service]
Type=oneshot
ExecStart=/usr/bin/hdparm -y /dev/sdX
ExecStop=/usr/bin/hdparm -S 241 /dev/sdX

[Install]
WantedBy=multi-user.target

To confirm your disk is sleeping properly:

# Check disk power state
sudo smartctl -n standby /dev/sdX

# Continuous monitoring
watch -n 60 'hdparm -C /dev/sdX | grep -i state'

Remember that some applications (like backup tools or desktop indexers) may still cause periodic wakeups. The monitoring tools mentioned earlier will help identify these offenders.


When dealing with secondary storage devices that need to remain mounted but inactive most of the time, we face two primary technical challenges: preventing unnecessary wake-ups and identifying what causes them. This requires a combination of kernel-level power management and detailed access monitoring.

To track which processes access your HDD, we'll use Linux's audit subsystem. First, install the necessary tools:

sudo apt install auditd
sudo systemctl enable --now auditd

Then create a rule to monitor your device (replace /dev/sdX with your actual device):

sudo auditctl -w /dev/sdX -p rwa -k hdd_access

To view the logs:

sudo ausearch -k hdd_access | aureport -f -i

The key parameters controlling disk spindown are in /sys/block/sdX/device/:

echo 10 > /sys/block/sdX/device/power/autosuspend_delay_ms
echo auto > /sys/block/sdX/device/power/control

For more aggressive power management, add these kernel boot parameters:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash usbcore.autosuspend=-1 scsi_mod.use_blk_mq=1"

hdparm provides direct control over drive power states:

sudo hdparm -S 60 /dev/sdX  # 5-minute timeout (60 * 5 seconds)
sudo hdparm -B 127 /dev/sdX  # Balance between performance and power saving

Certain filesystem operations can trigger wake-ups. For ext4:

tune2fs -o journal_data_writeback /dev/sdX1
tune2fs -E mount_opts=auto_da_alloc /dev/sdX1

Create a custom systemd unit to mount the drive only when needed:

[Unit]
Description=Mount secondary HDD on demand
Requires=network-online.target
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/mount /mnt/secondary
ExecStop=/usr/bin/umount /mnt/secondary

[Install]
WantedBy=multi-user.target

Common culprits and solutions:

# Disable udisks2 polling
sudo systemctl mask udisks2.service

# Prevent cron jobs from scanning
sudo touch /etc/cron.d/noscan
sudo chattr +i /etc/cron.d/noscan

Combine audit logs with power state monitoring:

#!/bin/bash
DEVICE="/dev/sdX"
LOG_FILE="/var/log/hdd_monitor.log"

while true; do
  STATE=$(cat /sys/block/${DEVICE##*/}/device/power/runtime_status)
  if [[ "$STATE" != "suspended" ]]; then
    TIMESTAMP=$(date +"%Y-%m-%d %T")
    PROCESSES=$(sudo lsof $DEVICE)
    echo "[$TIMESTAMP] Device active" >> $LOG_FILE
    echo "$PROCESSES" >> $LOG_FILE
  fi
  sleep 5
done