How to hot-swap SATA disks on Linux

From Leo's Notes
Revision as of 20:43, 28 August 2021 by Leo (talk | contribs)
This page was last edited on 28 August 2021, at 20:43.

This page contains some information on hot swapping hard drives on Linux.

Removing a Drive

If a device becomes unresponsive or if you simply just want to remove a SATA disk from a running system:

  1. Ensure the disk is unmounted.
  2. Ensure the disk isn't used by swap or LVM groups.
  3. Remove the disk from the system by running echo 1 > /sys/block/sdX/device/delete. This should also power off the drive.

Once the drive spins down, you can disconnect the power and SATA connector.

Rescanning SATA Bus

Depending on the chipset and SATA controller, when you connect a drive to the system, you may need to force a rescan of the bus before the drive shows up on the system. On Linux, you will need to trigger a rescan.

Determine the controller the disk is attached to and trigger a rescan. Listing the scsi_host directory should give you a clue on what host is associated with which device:

# ls -al /sys/class/scsi_host
lrwxrwxrwx  1 root root 0 Feb 18 22:21 host0 -> ../../devices/pci0000:00/0000:00:06.0/ata1/host0/scsi_host/host0/
lrwxrwxrwx  1 root root 0 Feb 18 22:21 host1 -> ../../devices/pci0000:00/0000:00:06.0/ata2/host1/scsi_host/host1/
lrwxrwxrwx  1 root root 0 Feb 18 22:21 host2 -> ../../devices/pci0000:00/0000:00:04.0/0000:01:06.0/ata3/host2/scsi_host/host2/

Trigger a rescan by writing 3 dashes (which denotes wildcards) to the scan command. Each of the 3 fields represents the channel, SCSI target ID, and LUN, respectively.

# echo "- - -" > /sys/class/scsi_host/host#/scan

If you're not sure which controller your disk is connected to, you may rescan all controllers with shell globbing.

# echo "- - -" > /sys/class/scsi_host/host*/scan

Once the rescan is triggered, look at dmesg to see if a new disk was attached to the system.