First get scsi Device information .
[[email protected] ~]# lsscsi [2:0:0:0] disk VMware, VMware Virtual S 1.0 /dev/sda [
4:0:0:0] cd/dvd NECVMWar VMware SATA CD01 1.00 /dev/sr0
Some operating systems don't lsscsi command , You can use the following method to get scsi Device information .
[[email protected] ~]# ll /sys/bus/scsi/drivers/sd/ total 0 lrwxrwxrwx 1 root root 0
Jun22 17:29 2:0:0:0 -> ../../../../devices/pci0000:00/0000:00:10.0
/host2/target2:0:0/2:0:0:0 --w------- 1 root root 4096 Jun 22 17:29 bind
--w-------1 root root 4096 Jun 22 2017 uevent --w------- 1 root root 4096 Jun 22
17:29 unbind [[email protected] ~]# ll /sys/bus/scsi/drivers/sd/2\:0\:0\:0/block/
total0 drwxr-xr-x 10 root root 0 Jun 22 2017 sda
Then check /proc/scsi/scsi file , Get corresponding scsi Device details .
[[email protected] ~]# cat /proc/scsi/scsi Attached devices: Host: scsi2 Channel:
00 Id: 00 Lun: 00 Vendor: VMware, Model: VMware Virtual S Rev: 1.0 Type: Direct
-Access ANSI SCSI revision:02 Host: scsi4 Channel: 00 Id: 00 Lun: 00 Vendor:
NECVMWar Model: VMware SATA CD01 Rev:1.00 Type: CD-ROM ANSI SCSI revision: 05
Host: scsi2 Channel: 00 Id: 01 Lun: 00 Vendor: VMware, Model: VMware Virtual S
Rev:1.0 Type: Direct-Access ANSI SCSI revision: 02

Here , There are two direct links (Direct-Access) Of scsi disk , One through the optical drive cd-rom Connected disc . We only consider scsi disk , So these two disks are scsi The locators in are 2:0:0:0 and 2:0:1:0.
If you continue to insert a disk , So the new dish is scsi The locators in are 2:0:2:0, This string is very important .

1.1 Hot plug


After inserting a disk into the computer , The kernel will not generate any event notifications because it cannot recognize it , So in /sys No files will be generated in the directory , No tool can read it . Rebooting the system can definitely be solved , however Linux Support hot plug .

The way to hot plug a new disk is to /proc/scsi/scsi Write new in scsi Device information . This is done as follows :
echo "scsi add-single-device a b c d" >/proc/scsi/scsi
among :

      a == hostadapter id (first one being 0)

      b == SCSI channel on hostadapter (first one being 0)

      c == ID

      d == LUN (first one being 0)

For example, the above example , The following information should be added :
[[email protected] ~]# echo "scsi add-single-device 2:0:2:0" >/proc/scsi/scsi
of course , Rescan scsi The bus can also realize the function of hot plug . Because in the example above ,scsi host
id by 2( Namely host2), So what we're scanning is host2, such host2 this scsi All devices on will be rescanned .
[[email protected] ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
If you don't know which one to scan host, Direct use cycle full scan .
[[email protected] ~]# for i in /sys/class/scsi_host/host*/scan;do echo "- - -" >$i;
done
After hot plug ,fdisk -l Wait for the command to recognize the disk .

1.2 Hot drawing

The way to hot unplug a disk is to /proc/scsi/scsi Remove the corresponding scsi Device information . This is done as follows :
echo "scsi remove-single-device a b c d" >/proc/scsi/scsi
For example delete 2:0:2:0 This disk .
[[email protected] ~]# echo "scsi remove-single-device 2 0 2 0" >/proc/scsi/scsi
Because the device to be deleted already exists ,/sys It has complete information in , So it is also deleted from its own device .

First look scsi Device information .
[[email protected] ~]# lsscsi [2:0:0:0] disk VMware, VMware Virtual S 1.0 /dev/sda [
2:0:1:0] disk VMware, VMware Virtual S 1.0 /dev/sdb [4:0:0:0] cd/dvd NECVMWar
VMware SATA CD011.00 /dev/sr0
  For example, to delete /dev/sdb, Namely 2:0:1:0. First look at its file information .
[[email protected] ~]# ls /sys/bus/scsi/drivers/sd/2\:0\:1\:0/ block/
evt_lun_change_reported model scsi_level bsg/ evt_media_change power/ state
delete evt_mode_parameter_change_reported queue_depth subsystem/
device_blocked evt_soft_threshold_reached queue_ramp_up_period timeout
device_busy generic/ queue_type type dh_state iocounterbits rescan uevent driver
/ iodone_cnt rev unpriv_sgio eh_timeout ioerr_cnt scsi_device/ vendor
evt_capacity_change_reported iorequest_cnt scsi_disk/ vpd_pg80
evt_inquiry_change_reported modalias scsi_generic/ vpd_pg83

Among them are 3 Files :delete,rescan and state. among state It records whether the device is running . and delete and rescan Files are used to delete and rescan the device .

for example , Delete this device , Hot drawing .
[[email protected] ~]# echo 1 > /sys/bus/scsi/drivers/sd/2\:0\:1\:0/delete