sata hotplug

最後更新: 2017-12-23

目錄

 

 


找出 Sata Port 的 Disk

 

ls -l /sys/block/sd*

lrwxrwxrwx 1 root root 0 May 17 19:24 /sys/block/sda ->
 ../devices/pci0000:00/0000:00:11.0/ata1/host0/target0:0:0/0:0:0:0/block/sda
lrwxrwxrwx 1 root root 0 May 17 19:24 /sys/block/sdb ->
 ../devices/pci0000:00/0000:00:11.0/ata2/host1/target1:0:0/1:0:0:0/block/sdb
lrwxrwxrwx 1 root root 0 May 17 19:24 /sys/block/sdc ->
 ../devices/pci0000:00/0000:00:11.0/ata3/host2/target2:0:0/2:0:0:0/block/sdc
lrwxrwxrwx 1 root root 0 May 17 19:24 /sys/block/sdd ->
 ../devices/pci0000:00/0000:00:11.0/ata4/host3/target3:0:0/3:0:0:0/block/sdd
lrwxrwxrwx 1 root root 0 May 17 19:24 /sys/block/sde ->
 ../devices/pci0000:00/0000:00:11.0/ata5/host4/target4:0:0/4:0:0:0/block/sde

Script

ls -l /sys/block/sd* | sed -n 's/.*\(sd.*\) -.*\(ata.*\)\/h.*/\2 => \1/p'
                                 OR
ls -l /sys/block/sd* | sed -n 's@.*\(sd.*\) -.*\(ata.*\)/h.*@\2 => \1@p'

Output

ata1 => sda
ata4 => sdb
ata5 => sdc

 


Check Removable

 

Further notes

[1] SATA hotplug does not (usually) work until you set BIOS to use AHCI for the chipsets;
      on most motherboards it is set to ‘ide-emulation’ or such by default.

[2] even AHCI is enabled, some motherboards required to set a seprate option for SATA hotplugging.

 * 對於 removable 不是 1 的 Dsk 只不可以 hotplug 它的 !!

Check removable

cat /sys/block/sdb/removable

1

快速找出 removable disk - lsblk

# RM                           which devices are removable
# -d, --nodeps              Do  not  print  holder  devices or slaves.
# --output columns-list

lsblk --nodeps --output NAME,TRAN,RM

NAME  TRAN   RM
sdd   sata    0
sdb   sata    1
..

 

 


Un-plug

 

令 fs clean

sync

umount /dev/sde1

Flush Cache

blockdev --flushbufs /dev/sde

Disk to Sleep ( 非必要 )

hdparm -Y /dev/sde

/dev/sde:
 issuing sleep command

Unregister the device from the kernel

1) Take the path offline

# This will cause any subsequent I/O sent to the device on this path to be failed immediately.

echo offline > /sys/block/sde/device/state           # offline / running

2) Remove the path from the SCSI subsystem

# remove each path to the device from the SCSI subsystem

# /dev/sdb* 會不見了, Start_Stop_Count ( 4 ) 及 Load_Cycle_Count (193) 都會加 1

cat /sys/block/sde/device/model                 # double check

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

dmesg

[ 5644.854469] sd 4:0:0:0: [sde] Synchronizing SCSI cache
[ 5644.864816] ata5.00: exception Emask 0x0 SAct 0x0 SErr 0x0 action 0x6
[ 5644.864821] ata5.00: waking up from sleep
[ 5644.864825] ata5: hard resetting link
[ 5645.340806] ata5: SATA link up 6.0 Gbps (SStatus 133 SControl 300)
[ 5645.345034] ata5.00: configured for UDMA/133
[ 5645.345038] ata5.00: retrying FLUSH 0xea Emask 0x0
[ 5645.345260] ata5: EH complete
[ 5645.345507] sd 4:0:0:0: [sde] Stopping disk
[ 5645.364828] ata5.00: disabled

Poweroff disk

dmesg

[ 5445.981110] ata5: exception Emask 0x10 SAct 0x0 SErr 0x90202 action 0xe frozen
[ 5445.981119] ata5: irq_stat 0x00400000, PHY RDY changed
[ 5445.981124] ata5: SError: { RecovComm Persist PHYRdyChg 10B8B }
[ 5445.981133] ata5: hard resetting link
[ 5446.942605] ata5: SATA link down (SStatus 0 SControl 300)
[ 5446.942629] ata5: EH complete

Unplug disk

dmesg

NO MSG

P.S.

[1] 不用 hdparm -Y /dev/sdb

"echo 1 > /sys/block/sdb/device/delete" 已經很夠, 不用 "hdparm -Y /dev/sdb"

原因係 Start_Stop_Count (  4  ) 及 Load_Cycle_Count (193) 都會加 1 

亦即是說 "echo 1 > delete" 與 "hdparm -Y" 有同樣效果

Remark

smartctl -a /dev/sdb 不會叫醒 HDD, touch /dev/sdb 就會了)

touch /dev/sdb

[256487.855328] ata2.00: exception Emask 0x0 SAct 0x0 SErr 0x0 action 0x6
[256487.855359] ata2.00: waking up from sleep
[256487.855377] ata2: hard resetting link
[256488.331339] ata2: SATA link up 3.0 Gbps (SStatus 123 SControl 300)
[256488.332491] ata2.00: configured for UDMA/133
[256488.332513] ata2: EH complete

[2] 真正的 Path

cd /sys/block/sde/device

pwd -P

# start with ata1
/sys/devices/pci0000:00/0000:00:11.0/ata3/host2/target2:0:0/2:0:0:0

 * 當沒有 Harddisk 時, 只有有

/sys/devices/pci0000:00/0000:00:11.0/ata3/host2

[3] runtime_status

cd /sys/devices/pci0000:00/0000:00:11.0/ata3/host2

cat power/runtime_status

suspended

# unit: ms

cat power/runtime_suspended_time

256356

cat power/runtime_active_time

712280

 


Plug

 

# When you plug the disks, you need to tell the OS to scan for new SATA devices

ls -l  /sys/class/scsi_host/host*

lrwxrwxrwx 1 root root 0 May 30 22:24 /sys/class/scsi_host/host0 ->
  ../../devices/pci0000:00/0000:00:11.0/ata1/host0/scsi_host/host0
lrwxrwxrwx 1 root root 0 May 30 22:24 /sys/class/scsi_host/host1 ->
  ../../devices/pci0000:00/0000:00:11.0/ata2/host1/scsi_host/host1
lrwxrwxrwx 1 root root 0 May 30 22:24 /sys/class/scsi_host/host2 ->
  ../../devices/pci0000:00/0000:00:11.0/ata3/host2/scsi_host/host2
lrwxrwxrwx 1 root root 0 May 30 22:24 /sys/class/scsi_host/host3 ->
  ../../devices/pci0000:00/0000:00:11.0/ata4/host3/scsi_host/host3
lrwxrwxrwx 1 root root 0 May 30 22:24 /sys/class/scsi_host/host4 ->
  ../../devices/pci0000:00/0000:00:11.0/ata5/host4/scsi_host/host4
lrwxrwxrwx 1 root root 0 May 30 22:24 /sys/class/scsi_host/host5 ->
  ../../devices/pci0000:00/0000:00:11.0/ata6/host5/scsi_host/host5

readlink /sys/block/sde

../devices/pci0000:00/0000:00:11.0/ata5/host4/target4:0:0/4:0:0:0/block/sde

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

# The three values stand for channel, SCSI target ID, and LUN.

# The dashes act as wildcards meaning "rescan everything"

[ 5863.406155] ata5: hard resetting link
[ 5863.883220] ata5: SATA link up 6.0 Gbps (SStatus 133 SControl 300)
[ 5863.885170] ata5.00: ATA-8: ST500DM002-1BD142, KC48, max UDMA/133
[ 5863.885174] ata5.00: 976773168 sectors, multi 0: LBA48 NCQ (depth 31/32)
[ 5863.909132] ata5.00: configured for UDMA/133
[ 5863.909146] ata5: EH complete
[ 5863.909729] scsi 4:0:0:0: Direct-Access     ATA      ST500DM002-1BD14 KC48 PQ: 0 ANSI: 5
[ 5863.910387] sd 4:0:0:0: [sde] 976773168 512-byte logical blocks: (500 GB/466 GiB)
[ 5863.910392] sd 4:0:0:0: [sde] 4096-byte physical blocks
[ 5863.910420] sd 4:0:0:0: [sde] Write Protect is off
[ 5863.910424] sd 4:0:0:0: [sde] Mode Sense: 00 3a 00 00
[ 5863.910469] sd 4:0:0:0: [sde] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[ 5863.911265] sd 4:0:0:0: Attached scsi generic sg4 type 0
[ 5867.669104]  sde: sde1
[ 5867.669515] sd 4:0:0:0: [sde] Attached SCSI removable disk

Remark

# 全部 sata port 都 scan

find /sys/devices -iname 'scan' | grep ata

echo "- - -" > /sys/devices/pci0000:00/0000:00:01.1/ata1/host0/scsi_host/host0/scan

echo "- - -" > /sys/devices/pci0000:00/0000:00:01.1/ata2/host1/scsi_host/host1/scan

 


 

Creative Commons license icon Creative Commons license icon