You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
6.0 KiB

1 year ago
  1. ---
  2. title: "Mapping of Physical USB ports to Device Name on GNU/Linux"
  3. date: 2023-08-29T11:19:15+05:30
  4. weight: 10
  5. description: "Mapping of Physical USB ports to Device Names (/dev/sda) on GNU/Linux"
  6. tags: ["usb", "linux", "gnu/linux" , "gnu"]
  7. type: post
  8. ---
  9. ## How to Map Physical USB ports to Device Names on GNU/Linux
  10. ```bash{filename="lsusb command"}
  11. $ lsusb
  12. Bus 002 Device 004: ID 04b3:3025 IBM Corp. NetVista Full Width Keyboard
  13. Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  14. Bus 003 Device 123: ID 0951:1666 Kingston Technology DataTraveler 100 G3/G4/SE9 G2/50
  15. Bus 003 Device 002: ID 2109:0815 VIA Labs, Inc. USB3.0 Hub
  16. $ lsusb -t
  17. /: Bus 06.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 10000M
  18. |__ Port 2: Dev 2, If 0, Class=Hub, Driver=hub/7p, 5000M
  19. /: Bus 05.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
  20. |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
  21. /: Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 480M
  22. /: Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
  23. |__ Port 4: Dev 2, If 0, Class=Hub, Driver=hub/4p, 5000M
  24. |__ Port 2: Dev 123, If 0, Class=Mass Storage, Driver=usb-storage, 5000M
  25. /: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
  26. |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
  27. |__ Port 3: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
  28. /: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 480M
  29. |__ Port 4: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
  30. ```
  31. "lsusb" is a utility of "usbutils" in GNU/Linux to display information about the USB Buses and the USB's attached to the buses.The output of the command displays the VendorID:ProductID and to which bus it is attached,the "Kingston Technology DataTraveler" bearing the VendorID:ProductID (0951:1666) attached to bus 003, the lsusb command with option "-t" provides a tree like output in a hierarchial structure.
  32. "lsblk" is a tree-like structure to identify devices and their partitions,and also displays device name (/dev/sd* if its a hard disk,/dev/nvme0n1 in case of SSD),size of the drive/partition, whether it is a disk/partition and the device's mountpoints,here the /dev/sda* is the hard disk and sda1/2/5 are the partitions of the hard disk and /dev/sdb is the Kingston USB drive connected.
  33. ```bash
  34. $ lsblk
  35. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
  36. sda 8:0 0 119.2G 0 disk
  37. ├─sda1 8:1 0 118.3G 0 part /
  38. ├─sda2 8:2 0 1K 0 part
  39. └─sda5 8:5 0 976M 0 part [SWAP]
  40. sdb 8:16 1 28.9G 0 disk
  41. sr0 11:0 1 1024M 0 rom
  42. ```
  43. ## Suppose if there are multiple USB sticks attached to mutiple physical USB ports how to know which device names is mapped to which USB port ?
  44. If there are multiple USB's attached how do we get to know USB drive's device file and to which port that USB is connected ?
  45. The answer is by /dev/disk/*
  46. Everything in GNU/Linux is either a file or a directory,so even the disk are represented as files in GNU/Linux.
  47. ```bash
  48. $ ls -ltrh /dev/disk/
  49. total 0
  50. drwxr-xr-x 2 root root 100 Aug 28 12:28 by-partuuid
  51. drwxr-xr-x 2 root root 100 Aug 29 09:49 by-uuid
  52. drwxr-xr-x 2 root root 260 Aug 29 09:49 by-path
  53. drwxr-xr-x 2 root root 60 Aug 29 09:49 by-label
  54. drwxr-xr-x 2 root root 160 Aug 29 09:49 by-id
  55. drwxr-xr-x 2 root root 260 Aug 29 09:49 by-diskseq
  56. ```
  57. The USB disks can be identified by the Partition UUID, UUID of the disk, by path, by label of the disk and also by disk sequence.
  58. ### By UUID(Universally Unique Identifier)
  59. ```bash{filename="by uuid"}
  60. $ cat /etc/fstab
  61. UUID=3341b336-0c26-4079-b3aa-faca8e2dd8b6 / ext4 errors=remount-ro 0 1
  62. UUID=53b53d97-b5e4-43fe-b560-2a01f119b6cf none swap sw 0 0
  63. /dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
  64. $ ls -ltrh /dev/disk/by-uuid/
  65. total 0
  66. lrwxrwxrwx 1 root root 10 Aug 22 10:40 3341b336-0c26-4079-b3aa-faca8e2dd8b6 -> ../../sda1
  67. lrwxrwxrwx 1 root root 10 Aug 22 10:40 53b53d97-b5e4-43fe-b560-2a01f119b6cf -> ../../sda5
  68. lrwxrwxrwx 1 root root 9 Aug 29 09:49 2010-10-06-11-43-04-00 -> ../../sdb
  69. ```
  70. The device /dev/disk/by-uuid/3341b336-0c26-4079-b3aa-faca8e2dd8b6 is simply a symbolic link to actual an device,the reason being this is device name may change depending whether disk is attached or not,whereas these links will point to the same drive,so henceforth safer to use.
  71. ### By-Label
  72. ```bash{filename="by-label"}
  73. $ ls -ltrh /dev/disk/by-label/
  74. total 0
  75. lrwxrwxrwx 1 root root 9 Aug 29 09:49 VINAY-USB -> ../../sdb
  76. ```
  77. Labels are easy, it avoids confusion in identifying disk, instead of remembering /dev/sda device file names.
  78. ### By-Path
  79. ```bash{filename="by-path"}
  80. $ ls -ltrh /dev/disk/by-path/
  81. total 0
  82. lrwxrwxrwx 1 root root 9 Aug 22 10:40 pci-0000:00:1f.2-ata-2.0 -> ../../sda
  83. lrwxrwxrwx 1 root root 9 Aug 22 10:40 pci-0000:00:1f.2-ata-2 -> ../../sda
  84. lrwxrwxrwx 1 root root 10 Aug 22 10:40 pci-0000:00:1f.2-ata-2-part2 -> ../../sda2
  85. lrwxrwxrwx 1 root root 10 Aug 22 10:40 pci-0000:00:1f.2-ata-2.0-part2 -> ../../sda2
  86. lrwxrwxrwx 1 root root 10 Aug 22 10:40 pci-0000:00:1f.2-ata-2-part1 -> ../../sda1
  87. lrwxrwxrwx 1 root root 10 Aug 22 10:40 pci-0000:00:1f.2-ata-2.0-part1 -> ../../sda1
  88. lrwxrwxrwx 1 root root 10 Aug 22 10:40 pci-0000:00:1f.2-ata-2-part5 -> ../../sda5
  89. lrwxrwxrwx 1 root root 10 Aug 22 10:40 pci-0000:00:1f.2-ata-2.0-part5 -> ../../sda5
  90. lrwxrwxrwx 1 root root 9 Aug 22 10:40 pci-0000:00:1f.2-ata-3.0 -> ../../sr0
  91. lrwxrwxrwx 1 root root 9 Aug 22 10:40 pci-0000:00:1f.2-ata-3 -> ../../sr0
  92. lrwxrwxrwx 1 root root 9 Aug 29 09:49 pci-0000:00:14.0-usb-0:4.2:1.0-scsi-0:0:0:0 -> ../../sdb
  93. ```
  94. Here /dev/sdb is the USB device attached and "pci-0000:00:14.0-usb-0:4.2:1.0-scsi-0:0:0:0" file which represents the USB device describes that the USB device is connected from PCI bus to SCSI adapter."by-path" is the pci path of the disk device. this device file name is created depending on the shortest physical path to the device.
  95. "/dev/sda" the first SCSI drive on the first SCSI bus,/dev/sdb is the second SCSI drive and /dev/sdc is the third SCSI drive and so on.
  96. ```
  97. :wq
  98. ```