Static Mapping of Serial Devices in Linux

 

 

Some time ago, I wrote about how to setup a Linux-based console server. But, there is one problem… if you reboot, your serial devices may have moved. So, to fix that, we will use UDEV to statically map the serial devices to non-changing names.

Plug in your usb converter and run the command ‘dmesg’. Look for where it was mapped to. In my case, it is ttyUSB1

usb 3-2: new full speed USB device using uhci_hcd and address 5
usb 3-2: configuration #1 chosen from 1 choice
pl2303 3-2:1.0: pl2303 converter detected
usb 3-2: pl2303 converter now attached to ttyUSB1

Now, the problem is, I plan on mapping ttyUSB1 to be connected to Switch3, but the next time I reboot, this converter could be mapped to ttyUSB0 (which may go to SW1 in my configs), and everything would be befungled.

So, we need to map it to a static name that will always point to the right switch. I will call the devices ttyCon1 thru ttyCon6. Here is how, and we will use USB1 to start.

First, we need to find some attributes to use to uniquely identify this specific adapter. Since we have multiple adapters from the same Manufacturer, it’s best we use the SERIAL number assigned to these devices. Let’s get it from the command ‘lsusb -v’, which will list all USB devices verbosely.

We only care about the “Prolific” adapters, and the device descriptor (since we are identifying at the ‘device’ level)

Bus 002 Device 003: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x067b Prolific Technology, Inc.
  idProduct          0x2303 PL2303 Serial Port
  bcdDevice            4.00
  iManufacturer           1 Prolific Technology Inc. 
  iProduct                2 USB-Serial Controller D
  iSerial                 0 
  bNumConfigurations      1

Bus 003 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x067b Prolific Technology, Inc.
  idProduct          0x2303 PL2303 Serial Port
  bcdDevice            4.00
  iManufacturer           1 Prolific Technology Inc. 
  iProduct                2 USB-Serial Controller D
  iSerial                 0 
  bNumConfigurations      1

Bus 004 Device 002: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x067b Prolific Technology, Inc.
  idProduct          0x2303 PL2303 Serial Port
  bcdDevice            4.00
  iManufacturer           1 Prolific Technology Inc. 
  iProduct                2 USB-Serial Controller D
  iSerial                 0 
  bNumConfigurations      1

Bus 004 Device 003: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x067b Prolific Technology, Inc.
  idProduct          0x2303 PL2303 Serial Port
  bcdDevice            4.00
  iManufacturer           1 Prolific Technology Inc. 
  iProduct                2 USB-Serial Controller D
  iSerial                 0 
  bNumConfigurations      1

Now, here is where you would look at the “iSerial” to uniquely identify each adapter. But, you can see, I have a problem. Looks like the everything is the same. Even the serial number (really cheap devices??). We need to use something UNIQUE to identify each adapter. I guess the only way is to use the ID or port that it’s plugged into.

It would probably be best to use the Vendor, Product, and Serial to identify your adapters.

We will use udevinfo for that. Use this for each ttyUSB# that you have attached to your system.

[root@sys1 ~]# udevinfo -a -p /sys/bus/usb-serial/devices/ttyUSB1

looking at device '/devices/pci0000:00/0000:00:1d.1/usb3/3-2/3-2:1.0/ttyUSB1':
  KERNEL=="ttyUSB1"
  SUBSYSTEM=="usb-serial"

looking at parent device '/devices/pci0000:00/0000:00:1d.1/usb3/3-2/3-2:1.0':
  ID=="3-2:1.0"
  BUS=="usb"
  DRIVER=="pl2303"
  SYSFS{modalias}=="usb:v067Bp2303d0400dc00dsc00dp00icFFisc00ip00"
  SYSFS{bInterfaceProtocol}=="00"
  SYSFS{bInterfaceSubClass}=="00"
  SYSFS{bInterfaceClass}=="ff"
  SYSFS{bNumEndpoints}=="03"
  SYSFS{bAlternateSetting}==" 0"
  SYSFS{bInterfaceNumber}=="00"

So, I’m going to use the ID==”3-2:1.0”. I could be wrong, but I read this as “Hub #3, port #2, device #1. Which means, I’m mapping the physical port, not the adapter. Of course, if I ever MOVE this adapter or hub, everything changes. This is why it’s best to use the Serial rather than the port. Since this is only for my lab, I’m ok with that.

UDEV Rules are put in the directory /etc/udev/rules.d/, and are processed alphabetically (and must end in .rules). So, I’m going to call mine 45-swcon.rules.

BUS=="usb", ID=="3-2:1.0", SYMLINK+="ttyCon2"

Doing this, maps the adapter in 3-2:1.0 to be /dev/ttyCon2. So, if my USB adapter changes to ttyUSB102, it will still be mapped to ttyCon2, and THAT is what I use in my configs.

I can now do this for my remaining adapters. My file /etc/udev/rules.d/45-swcon.rules file now looks like this:

BUS=="usb", ID=="2-2:1.0", SYMLINK+="ttyCon1"
BUS=="usb", ID=="3-2:1.0", SYMLINK+="ttyCon2"
BUS=="usb", ID=="4-1:1.0", SYMLINK+="ttyCon3"
BUS=="usb", ID=="4-2:1.0", SYMLINK+="ttyCon4"

Now, this maps the adapter based on port. Obviously, this isn’t very flexible, but it’s all I can do, since these are VERY cheap adapters. You get what you pay for, right? If your adapters have serial numbers, use that instead. Maybe something like:

BUS=="usb", SERIAL=="0a123bcd4e1cba", SYMLINK+="ttyCon1"

That way, no matter where you plug this bad boy in, it will always be mapped to ttyCon1.

Good luck!

Share This Page : Share on TwitterShare on FacebookShare on GooglePlusShare on PinterestShare on Linkedin