標籤:linux
Linux核心從2.6.13-rc3開始,提供了在使用者空間,可動態綁定和解除綁定定裝置和裝置驅動之間關係的功能。在這之前,只能通過insmod(modprobe)和rmmod來綁定和解除綁定,而且這種綁定和解除綁定都是針對驅動和所有裝置的。而新的功能可以設定驅動和單個裝置之間的聯絡。
這裡,我們以pci匯流排的nvme ssd為例,首先執行lspci顯示所有的nvme ssd。
# lspci | grep memory01:00.0 Non-Volatile memory controller: Samsung Electronics Co Ltd Device a802 (rev 01)09:00.0 Non-Volatile memory controller: Samsung Electronics Co Ltd Device a802 (rev 01)
有如上這麼多nvme ssd,那麼我們就可以在/dev下看到多個nvme裝置(OS啟動後預設載入nvme驅動)。
# ls /dev/nvme*/dev/nvme0 /dev/nvme0n1p2 /dev/nvme0n1 /dev/nvme0n1p3 /dev/nvme0n1p1 /dev/nvme1 /dev/nvme1n1
同時,對於所有的nvme裝置(這裡我們以pci匯流排bdf號為09:00.0的ssd為例),都可以在nvme驅動下看到。其中,bind和unbind檔案就是涉及到綁定和解除綁定的關鍵檔案。
/sys/bus/pci/drivers/nvme# lltotal 0drwxr-xr-x 2 root root 0 Jan 4 17:10 ./drwxr-xr-x 30 root root 0 Jan 4 17:10 ../lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:01:00.0 -> ../../../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:09:00.0 -> ../../../../devices/pci0000:00/0000:00:03.0/0000:05:00.0/0000:06:0a.0/0000:07:00.0/0000:08:01.0/0000:09:00.0/--w------- 1 root root 4096 Jan 4 20:46 bindlrwxrwxrwx 1 root root 0 Jan 4 20:49 module -> ../../../../module/nvme/--w------- 1 root root 4096 Jan 4 20:49 new_id--w------- 1 root root 4096 Jan 4 20:49 remove_id--w------- 1 root root 4096 Jan 4 17:10 uevent--w------- 1 root root 4096 Jan 4 17:10 unbind
解除綁定一個nvme裝置,只需將裝置的pci匯流排bdf號寫入/sys/bus/pci/drivers/nvme(不同的裝置驅動不同)/unbind即可:
/sys/bus/pci/drivers/nvme# echo -n "0000:09:00.0" > unbind解除綁定成功,再查看目錄下檔案,該驅動下不再有對應的裝置。同時,/dev下也沒有對應的nvme裝置了。/sys/bus/pci/drivers/nvme# lltotal 0drwxr-xr-x 2 root root 0 Jan 4 20:51 ./drwxr-xr-x 30 root root 0 Jan 4 20:49 ../lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:01:00.0 -> ../../../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/--w------- 1 root root 4096 Jan 4 20:46 bindlrwxrwxrwx 1 root root 0 Jan 4 20:49 module -> ../../../../module/nvme/--w------- 1 root root 4096 Jan 4 20:49 new_id--w------- 1 root root 4096 Jan 4 20:49 remove_id--w------- 1 root root 4096 Jan 4 20:49 uevent--w------- 1 root root 4096 Jan 4 20:51 unbind
綁定一個nvme裝置,和解除綁定類似,將裝置的pci匯流排bdf號寫入/sys/bus/pci/drivers/nvme(不同的裝置驅動不同)/bind:
/sys/bus/pci/drivers/nvme# echo -n "0000:09:00.0" > bind綁定成功,再次展示該目錄下所有檔案,可以發現對應裝置再次出現。/sys/bus/pci/drivers/nvme# lltotal 0/sys/bus/pci/drivers/nvme# lltotal 0drwxr-xr-x 2 root root 0 Jan 5 09:13 ./drwxr-xr-x 30 root root 0 Jan 4 20:49 ../lrwxrwxrwx 1 root root 0 Jan 4 20:49 0000:01:00.0 -> ../../../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/lrwxrwxrwx 1 root root 0 Jan 5 09:13 0000:09:00.0 -> ../../../../devices/pci0000:00/0000:00:03.0/0000:05:00.0/0000:06:0a.0/0000:07:00.0/0000:08:01.0/0000:09:00.0/--w------- 1 root root 4096 Jan 5 09:13 bindlrwxrwxrwx 1 root root 0 Jan 4 20:49 module -> ../../../../module/nvme/--w------- 1 root root 4096 Jan 4 20:49 new_id--w------- 1 root root 4096 Jan 4 20:49 remove_id--w------- 1 root root 4096 Jan 4 20:49 uevent--w------- 1 root root 4096 Jan 4 20:51 unbind
LINUX驅動手動綁定和解除綁定定