Freitag, 6. Juli 2012

Scheduled reconnect with Pirelli PRGAV4202N Modem

Motivation


The Pirelli PRGAV4202N is the model often provided by A1 (former Telekom Austria). Some providers tend to limit the IP-lease in their consumer products. In my case A1 limits connections to 24 hours. After 24 hours the connection is cut and the modem must reconnect. This leaves me without an internet connection for a few seconds. This also terminates all my downloads, uploads, VoIP, game connections, etc. So ideally I want this reconnect to occur at some time when I don't care (like e.g. 5 o'clock in the morning). A trivial solution would be to just get up at 5 o'clock once and force the modem to reconnect manually, so that all subsequent reconnects would also occur at roughly 5 o'clock. But this only works as long as there is no other random reconnect which happens every now and then.

How to


This guide requires some linux-based box that is powered on when the reconnect should occur (e.g. an OpenWRT-powered router or a linux-powered home-server).

You need to install the "expect"-script language.

You can use the following script to force your modem to reconnect:

#!/usr/bin/expect

set timeout 1
set cmd {uname -a}

spawn ssh Telek0m@10.0.0.138
expect_after eof { exit 0 }


## interact with SSH
expect "yes/no" { send "yes\r" }
expect "password:" { send "<the-password>\r" }

expect "OpenRG> "
send "conf\r"
expect "conf> "
send "set /dev/ppp0/enabled 0\r"
expect "conf> "
send "reconf 1\r"
expect "conf> "
send "set /dev/ppp0/enabled 1\r"
expect "conf> "
send "reconf 1\r"
expect "conf> "
send "exit\r"
expect "OpenRG> "
send "exit\r"


The "Telek0m"-user that is used for the SSH-connection was used in earlier firmwares as universal support-user. As I'm not sure how legal it is to provide the password here, I'll just skip it (you'll find it using a good search engine).

make sure the script is executable

# chmod +x reconnect.exp

and put it in a cronjob

# crontab -e
0 5 * * * /path/to/reconnect.exp

That's it.

Donnerstag, 5. Juli 2012

Synchronize clock in initial ramdisk


For some reason my server-system running Ubuntu 12.04 with full disk encryption refuses to boot after a crash due to power loss. I soon discovered, that often after powering on after a power loss, the system clock was reset to Jan 1 2002 (or something). This means that after entering the decryption-password the partitions refuse to mount because the "last mount timestamp" is in the future. There is something in the mount-init-script that corrects for such errors as long as the clock is not off by more than 24 hours. In my case it's off by several years. The system only boots when I set the correct date while in initial ramdisk.

To automate this, I thought let's just synchronize the clock via NTP. So I add ntpclient to the initial ramdisk and make sure it's executed automatically.

Step By Step


Download and compile ntpclient (http://doolittle.icarus.com/ntpclient/)
Note that the version provided there does not compile under ubuntu 12.04.
I created a fork on github that fixes that (https://github.com/ChristophGr/ntpclient/zipball/master)
Compile the ntp-client by invoking "make".

$ make
cc -fno-strict-aliasing -std=c89 -W -Wall -O2 -DENABLE_DEBUG -DENABLE_REPLAY   -c -o ntpclient.o ntpclient.c
cc -fno-strict-aliasing -std=c89 -W -Wall -O2 -DENABLE_DEBUG -DENABLE_REPLAY   -c -o phaselock.o phaselock.c
cc   ntpclient.o phaselock.o -lrt  -o ntpclient


now copy the resulting binary somewhere for the initial ramdisk to pick up. It should be a path that is not writable by regular users.

# cp ntpclient /usr/local/


Create a new file in the initial ramdisk source "/etc/initramfs-tools/hooks/ntpclient" and add the following content:

#!/bin/sh
PREREQ=""
prereqs()
{
        echo "$PREREQ"
}

case $1 in
prereqs)
        prereqs
        exit 0
        ;;
esac

. /usr/share/initramfs-tools/hook-functions

#
# Begin real processing
#

cp /usr/local/ntpclient ${DESTDIR}/sbin

make sure it's executable

# chmod +x /etc/initramfs-tools/hooks/ntpclient
In order to automatically invoke the ntpclient when booting, add this script to /etc/iniramfs-tools/scripts/local-premount

#!/bin/sh -e
# initramfs local-premount script for fixrtc

PREREQ=""

# Output pre-requisites
{
        echo "$PREREQ"
}

case "$1" in
    prereqs)
        prereqs
        exit 0
        ;;
esac

/sbin/ntpclient -s -h <ntp-server-ip>

exit 0;

Replace the ntp-server-ip with the IP (not the URL, as we don't have DNS-resolving in initrd) of an NTP-server close to you. To find one you can do

$ nslookup pool.ntp.org

Make sure the script is executable

# chmod +x /etc/initramfs-tools/scripts/local-premount/syncclock

To finish it, update your ramdisk

# update-initramfs -u

And you're done. From now on, your clock is synced via NTP during every boot before mounting.