In this lesson, you will learn how to safely add and remove PCIe and NVMe storage devices while your Linux system is actively running.
The Concept of Hot-Plugging
Hot-plugging refers to the ability to connect or disconnect hardware without powering down the host machine. While this is common with USB drives, performing this with internal PCIe or NVMe storage requires careful coordination between the hardware and the kernel to prevent data corruption or system crashes.
When you insert an NVMe drive into a compatible slot, the PCIe bus detects the new device. However, the Linux kernel needs a signal to "scan" for this new hardware to assign it a device node (like /dev/nvme0n1).
Safe Addition
To notify the kernel that a new device is present after you have physically plugged it in, you trigger a rescan of the PCIe bus. You can do this by echoing 1 into the rescan file for the specific PCIe slot:
echo 1 | sudo tee /sys/bus/pci/rescan
Once the kernel registers the device, you will see it appear in your storage list via lsblk.
Safe Removal
Removing a drive is more delicate. You must ensure the OS is not actively reading from or writing to the disk to avoid filesystem errors. First, unmount any partitions associated with the device:
sudo umount /mnt/my_nvme_drive
Next, you must logically remove the device from the kernel's awareness so it stops attempting to communicate with the now-absent hardware. This involves "removing" the device from the PCIe bus:
# Replace 0000:01:00.0 with your actual device PCI address
echo 1 | sudo tee /sys/bus/pci/devices/0000:01:00.0/remove
After this command, the kernel finishes pending I/O operations and stops interacting with that specific hardware address, allowing you to safely pull the physical drive from the chassis. Always verify the device is no longer listed in lsblk before proceeding with the physical removal.
Key Takeaway: Hot-plugging requires explicitly notifying the Linux kernel to rescan the PCIe bus when adding hardware and to logically remove the device from the kernel's tree before physically disconnecting it to ensure data integrity.