Is it safe to replace hardware while a PC is suspended to RAM?

Solution 1:

Is it safe to replace hardware while a PC is suspended to RAM?

No, as Toryan already wrote in their answer, a PC in sleep mode is still powered on.

Specifically: Can you unplug an external hard disk without losing data?

If the OS flushed the disk cache prior to dropping to sleep mode: Probably yes.
If is was not done and you still are halfway though a change on the disk: No.

Can you replace components like a drive or a PCI-express card?

Replacing a PCI-e card on a powered-on consumer motherboard is a really bad idea. Even ignoring the risk of shorting things when plugging and unplugging cards on a powered on computer, you also run into initialisation problems. Normally a computer boots, detects hardware and configures it for usage. When the OS is already booted then the all devices are already configured. Adding new hardware will require this new device to catch up.

This is possible, but your OS needs to support it. (e.g. this is why you can plug in express-card, thunderbolt or USB devices). I have serious doubt about comsumer OS's and support for adding, let alone removing PCI-e cards.

SATA and SCSI drives are an exception to this. SCSI support hotplugging. As does SATA in AHCI mode (AHCI is normal SATA mode, as opposed to ancient IDE emulation mode).

(I don't know why one would require this, but I'm curious)

For disks the answer is easy. A failed drive can be replaced without bringing the server down. This works perfectly in combination with RAID. For SoHO tasks: Just power it down.

I'm especially interested in Linux, but I don't know if the OS matters.

The OS matter because it needs support to actually use the new devices. Or support to remove them. Which is something you would need to do before sleep mode. (Much akin to how you need to use safe removal for USB pen drives before unplugging them). If your PC is already in sleep mode then you are to late for this.

Solution 2:

No, it's a really bad idea to start fiddling around inside the case while the PC is still powered on (which it is when in sleep mode). At best, the PC will fail to wake from sleep, at worst you can actually damage the components you're replacing.

A specific exception to this is in the case of hot-swappable components in servers, such as hard drives, PSUs, etc.

(You can of course still connect/disconnect anything that you normally would while the PC is powered on, such as USB devices. Just take the same level of care as you would during normal operation).

Solution 3:

Note: Answering specifically for PCIe devices

Theoretically, it should be possible to safely replace PCIe devices while suspended (S3 sleep or deeper), even on motherboards that aren't designed for PCIe hotplug. Assuming you unload any drivers using the devices beforehand, so they're not surprised by a missing device, and your OS is correctly configured for PCIe hotplug.

The PCIe standard defines various power states for the PCIe device, of which the powered-off state is the D3cold state:

5.3.1.4.2. D3cold State

A Function transitions to the D3cold state when its main power is removed. A power-on sequence with its associated cold reset transitions a Function from the D3cold state to the D0uninititialized state. At this point, software must perform a full initialization of the Function in order to re-establish all functional context, completing the restoration of the Function to its D0active state.

(Note that this doesn't include trickle power for PCIe-triggered wakeup events, like Wake-on-LAN. But even a "normally" S4/S5 powered off system will have this trickle power still available)

While a S4 "hibernate" or S5 "soft off" sleep (i.e., entire computer is off) will force all devices into D3cold, this isn't true for S3 "suspend" sleep. However, Linux will put the PCIe host ports into D3hot on S3 suspend, forcing all downstream devices connected to the ports into either D3hot (powered) or D3cold (unpowered). (Windows does something similar)

The point of having separate D3hot and D3cold states is to allow enumerating the device in D3hot. But once the host port is set to D3hot, communications with downstream devices are completely severed, meaning from a software point of view the downstream devices are in D3cold (but not necessarily electrically).

Since one goal of the D3 state is for power savings, optimal power savings would happen when power is shut off. So an optimal implementation of the host port when entering D3 would include removing bus power (i.e., downstream devices electrically enter D3cold).

Since I don't have access to any motherboards schematics to verify things myself, I can only guess that motherboards will implement that "optimal" strategy.
But they might also go the cost-saving route and wire the power rails directly to the PSU, in which case electrical D3cold is only possible when the PSU is suspended (S4 sleep or deeper).

TL;DR: An ideal power-saving implementation will power off PCIe devices and ports on S3 suspend-to-RAM, making it safe to add or remove devices. But a cost-saving implementation might not.