14

Is there a way to instruct the (Linux) kernel not to wake up a secondary hdd after a system sleep/wake-up cycle? I'm asking because in my laptop I have an SSD as a primary drive containing the system root and a secondary HDD where I keep a backup system and files I access sporadically. Since the laptop usually goes through many daily sleep/wake-up cycles (I mean Suspend to RAM, not hibernation) for many days, I would like to put the secondary HDD to sleep manually and instruct the kernel to keep it asleep across system sleep/wake-up cycles.

1
  • 1
    If you want to send your harddisk manually to sleep you can use hdparm -Y /dev/sdb. If you system accesses your disk it will automatically spin up again. You can also define this in /etc/hdparm.conf with /dev/disk/by-label/DATA4 { spindown_time = 180 } I recommend using labels for your disks as you can never rely on which disk is detected first by your system. be aware that you need a newer version of hdparm (mine is 9.43) to send disks with label to sleep.
    – syss
    Mar 4, 2015 at 12:47

3 Answers 3

1

since you only use the secondary hdd for backup, I would suggest telling fstab not to mount the drive automatically and to make a backup script that mounts the drive, makes the backup and unmounts the drive again.

example of the fstab line with the noauto option:

/dev/sdb1 /media/backup ext4 user,noauto 0 0

bash script for the backup would than begin with mount /media/backup and end with umount /media/backup

2
  • 1
    This does not prevent the kernel from probing the disk upon boot.
    – Marc.2377
    Aug 4, 2019 at 6:38
  • @Marc.2377 that's true, the bios will still brobe the disk.
    – switch87
    Oct 21, 2021 at 8:11
1

Never spinning up the disk

Spinning up the disks seems to be hard-coded in the kernel (Thanks to Veon for digging this out.)
But it can of course be patched: Patch for an older version of the kernel.
The whole thing is weird, as I thought I remembered there being a kernel parameter/switch for this once.

Since this is a hack, I don’t recommend it, as you will have to patch and build the kernel on every update, and sometimes even adapt the patch to changed code. A better way would be to submit a patch (that offers an actual switch and works for all systems) to the kernel developers themselves. I think that is beyond the scope of this answer. :)


Stopping the disk again (SystemD/Linux)

Building onto Marc.2377’s solution, this here worked to shut a HDD down on SystemD/Linux Mint.

[Unit]
Description="Put woken HDD back to sleep and tell it to go to sleep after 2 min of not being used."
After=systemd-suspend.service systemd-hybrid-sleep.service systemd-hibernate.service systemd-suspend-then-hibernate.service

[Service]
Type=simple
ExecStart=/usr/sbin/hdparm -q -S 24 -q -Y /dev/disk/by-id/MYDISK

[Install]
WantedBy=sleep.target

Replace MYDISK by an actual device file name in /dev/disk/by-id, and save it as /etc/systemd/system/wake_hdd-stop.service.

Then run systemctl daemon-reload to activate it.


Stopping the disk again (GNU/Linux)

On normal GNU/Linux system, using pm-utils you can just put the following in /etc/pm/sleep.d/99_hdd-stop:

#! /bin/sh
case "$1" in
  resume|thaw)
    /usr/sbin/hdparm -q -S 24 -q -Y /dev/disk/by-id/MYDISK
    ;;
esac

Replacing MYDISK, of course.
Then make the file executable with chmod +x /etc/pm/sleep.d/99_hdd-stop


NOTE

that in any case, you want to run the command as late as possible, or the disk might spin right up again due to some initialisation process accessing it.

Also, my disk seems to completely ignore the -S command. So YMMV. (I created a .desktop file to run above script, and used my desktop environment’s global keyboard shortcut settings to map a shortcut to it. So I can stop it instantly, whenever I want.)


0

I don't think that is possible. The drive is probed by the BIOS / UEFI upon boot, not just the kernel.

What I do, though, is instruct the system to put the drive in sleep mode right after the system boots. Check https://wiki.archlinux.org/index.php/Hdparm#Putting_a_drive_to_sleep_directly_after_boot.

In my case I have:

/usr/local/lib/systemd/system/rsleephdd.service

[Unit]
Description="Ranolfi's script to sleep hard drives on boot"

[Service]
Type=oneshot
ExecStart=/usr/bin/hdparm -q -S 120 -Y /dev/sdc

[Install]
WantedBy=multi-user.target

It makes no difference whether the drive or its partitions are mounted by fstab (or crypttab, for that matter) or not.

(Edit): Strictly speaking, it appears to be possible to get the kernel to ignore a disk by patching it. That won't prevent the drive from waking up, though - quite the opposite, the patch I linked to will prevent you from accessing the drive, you'll be unable to access your files even sporadically, and also unable to put the drive to sleep.

3
  • Adding that service file does nothing. Even enabling it doesn’t. And while starting the service does the job, it still does nothing after waking up from sleep. How does this work for when you’ve been given the SystemD?
    – anon
    Apr 8, 2023 at 17:55
  • @Evi1M4chine I'm sure to have improved upon this on my machine. Will hopefully expand this answer later - please feel free to hit me up tomorrow if I don't.
    – Marc.2377
    Apr 8, 2023 at 19:03
  • I found more information and decided to expand on it myself, with a nod to your answer. :)
    – anon
    Apr 16, 2023 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .