Persistent paths for dynamic device file

This post is related to playSMS and Kannel, how to configure Kannel when you know that the device file names aren’t persistent.

Intro.

When USB GSM modem plugged to a server Linux kernel assigned dynamic device file /dev/ttyUSB*, such as /dev/ttyUSB0 or /dev/ttyUSB1. For example, USB GSM modem with 2 ports will then be assigned to /dev/ttyUSB0 for port 1 and /dev/ttyUSB1 for port 2.

Problem starts when we unplug the GSM modem and re-plug back afterwards. Linux kernel will then assign different device file to it, was /dev/ttyUSB0 now /dev/ttyUSB2 and was /dev/ttyUSB1 now /dev/ttyUSB3.

Let’s talk about the problem.

Put your attention to this SMSC configuration part of our Kannel:

## SMSC gsm1
group = smsc
smsc = at
smsc-id = gsm1
modemtype = wavecom
device = /dev/ttyUSB0
log-file = /var/log/kannel/smsc-gsm1.log
log-level = 0

## SMSC gsm2
group = smsc
smsc = at
smsc-id = gsm2
modemtype = wavecom
device = /dev/ttyUSB1
log-file = /var/log/kannel/smsc-gsm2.log
log-level = 0

Note that SMSC ID gsm1 is mapped to /dev/ttyUSB0, and SMSC ID gsm2 is mapped to /dev/ttyUSB1.

When we unplug the GSM modem and re-plug it Linux kernel will assign different device files, was /dev/ttyUSB0 then become /dev/ttyUSB2, was /dev/ttyUSB1 then become /dev/ttyUSB3. This will make your Kannel wrongly configured and stop sending or receiving SMS.

What we would do were to change configuration to use newly assigned device file /dev/ttyUSB2 and /dev/ttyUSB3. But we would have to change it back when the server restarted or we unplugged and re-plugged it again and again. Imagine how unstable our system looked like.

What we want is whenever the server restarted, the modem unplugged and re-plugged, whichever the device files would be, we do not need to change our kannel.conf and restart Kannel.

Here’s how to get what you want.

With the help of udev configuration and a script we can dynamically map device file to a specific, and persistent, path, upon plugging the physical device.

Create /etc/udev/rules.d/80-ttyusb-map.rules:

vi /etc/udev/rules.d/80-ttyusb-map.rules

And fill it with this:

ACTION=="add", KERNEL=="ttyUSB[0-9]*", PROGRAM="/etc/udev/rules.d/ttyusb-map.sh %p", SYMLINK+="gsm%c"

Then create /etc/udev/rules.d/ttyusb-map.sh:

touch /etc/udev/rules.d/ttyusb-map.sh
chmod 755 /etc/udev/rules.d/ttyusb-map.sh
vi /etc/udev/rules.d/ttyusb-map.sh

And fill it with this:

#!/usr/bin/perl -w

@items = split("/", $ARGV[0]);
for ($i = 0; $i < @items; $i++) {
    if ($items[$i] =~ m/^usb[0-9]+$/) {
        print $items[$i + 1] . "\n";
        last;
    }
}

That is all.

Now try to plug GSM modem, and then plug it back. We should see that /dev/gsm1-1 symlink to /dev/ttyUSB0 and /dev/gsm2-1 symlink to /dev/ttyUSB1.

See example below:

[anton@srv ~]$ ls -l /dev/gsm*
lrwxrwxrwx 1 root root 7 Mei  4 15:40 /dev/gsm1-1 -> ttyUSB0
lrwxrwxrwx 1 root root 7 Mei  4 15:40 /dev/gsm2-1 -> ttyUSB1

Those symlinks can be different each time you plug and re-plug the GSM modem, or restart the server, but the name of those device files are persistent.

We can then use /dev/gsm1-1 as our map to physical USB port 1 and /dev/gsm2-1 as our map to physical USB port 2.

Your Kannel configuration would then be like this:

## SMSC gsm1
group = smsc
smsc = at
smsc-id = gsm1
modemtype = wavecom
device = /dev/gsm1-1
log-file = /var/log/kannel/smsc-gsm1.log
log-level = 0

## SMSC gsm2
group = smsc
smsc = at
smsc-id = gsm2
modemtype = wavecom
device = /dev/gsm2-1
log-file = /var/log/kannel/smsc-gsm2.log
log-level = 0

Restart your Kannel and tail SMSC log files, see if Kannel works properly.

tail -f /var/log/kannel/smsc-gsm1.log
tail -f /var/log/kannel/smsc-gsm2.log

Try it.

References

3 thoughts on “Persistent paths for dynamic device file

  1. Very useful information, thank you. During installation and configuration PlaySMS with Kannel I encountered with some problems with 3GUSB modem Huawei E3131 (CentOS 6.5 incorrectly configured device determines) and found this solution. Please take a look and add useful information if poschiaete. Thank you for PlaySMS))
    P.S. Sorry for my English)

    Like

Comments are closed.