📜 ⬆️ ⬇️

Adding and removing on the go SATA / SCSI devices

Modern Linux (2.6+) can detect new devices (on buses that support hotplug). They can also be disabled by unmounting filesystems and making sync. Among hotplug buses, not only USB, but also SATA, SCSI and SAS (in theory, the same applies to PATA, but there are a lot of stupid controllers who cannot react adequately to the disappearance of the device).

It is better to turn them off without jerking a physical device on the go, but telling the kernel to completely forget about it (thereby ensuring that no I / O operations will be performed with the device, even if the flash cache). In addition, it is sometimes necessary to perform a plug-unplug emulation procedure without physically jerking the power / data bus (which is not very good for hardware). The most typical situation is debugging udev scripts.

Device removal

 echo 1> / sys / block / sdX / device / delete

(x is the device letter, sda, sdb, etc).
This command deletes the specified device. Note that this is a low-level command that does not check the cache and the status of the mount, so it is better to first say umount & sync.

Unfortunately, I don’t know a method to combine turning off the spindle of a disk with its removal from the point of view of the kernel. The spindle can be turned off with the scsi-spin command; however, when you try to remove a device, it will be unwound and removed again. And the remote device cannot stop anything (there is no device). So this part of the problem has not been solved yet.

Adding a device

We cannot “add” a device, we can give the controller the command “re-read” the list of devices connected to one or another port. If there is something interesting there, they will let the kernel know.

 echo "- - -"> / sys / class / scsi_host / hostX / scan

X is the bus number that matches the SATA port number on the motherboard. If you do not know, you can safely do for all the hosts in turn, it will not give you anything except a small lag in disk operations that are imperceptible for software and the file system.

Note that hosts are numbered from 0, not from 1. (and in dmesg ata, devices are numbered from 1).

USB-SATA adapters are also scanned (usb-boxes and external hard drives - they simply appear as another scsi_host).

If we are talking about SCSI, then instead of "- - -" you can specify the exact number of the device / bus / LUN of the scanned device (for example, "200 1 2"). SATA, due to architectural features (one target for one initiator) accepts there only “0 0 0”.

More about deleting ... If you do not know the letters of the device, but you know its physical connection, then you can delete it by "plain text", by writing "1" in "/ sys / bus / scsi / devices / targetX: 0: 0 / X: 0 : 0: 0 / delete ".

Source: https://habr.com/ru/post/102387/

All Articles