NSLU2-Linux
view · edit · print · history

Edit July08 by MicRo
I took the freedom to edit this page and tried to make it better readable.
The -l parameter in the ntpclient command does not work in scripts on my NSLU2 (with Unslung 6.10beta) at all. In my case it stopped the NSLU2 boot procedure while executing the /opt/etc/init.d/S10ntpclient script which I found out only because the Ready/Status LED was still blinking after the boot procedure should have finished. Consequently I removed the -l parameter out of the scripts below.

RF: (30Oct2008) The documentation for ntpclient says:

On to ntpclient -l. This is actually easy, if you performed and understood the previous steps. Run
     ntpclient -l -h $NTPHOST
in the background.

which to me implies that you must deliberately run ntpclient in the background when using -l.


The Slug (NSLU2) has buggy timekeeping facilities, which Cisco apparently tried to fix, but didn't do a very good job of fixing. As of January 2008, on an Unslung system, bcrowell observed that the clock was set to 1999 by default, and setting it correctly didn't "stick" across reboots. This causes all kinds of problems with file synchronization, unpacking tarballs, etc. The following is a fix for the problem for Unslung 6.8:

Install ntpclient:

ipkg install ntpclient

Create the file /opt/etc/init.d/S10ntpclient containing the following:

#!/bin/sh    
if [ -n "`pidof ntpclient`" ]; then    
    /bin/killall ntpclient 2>/dev/null    
fi    
/opt/sbin/adjtimex -t 10000    
/opt/bin/ntpclient -s -h pool.ntp.org > /var/log/ntp.log
/usr/sbin/hwclock --systohc --utc

In /etc/crontab , delete the line containing hwclock and replace it with the following:

1 * * * * root /opt/bin/ntpclient -s -h pool.ntp.org > /var/log/ntp.log && /usr/sbin/hwclock --systohc --utc

Make /opt/etc/init.d/S10ntpclient script executable:

 chmod +rx /opt/etc/init.d/S10ntpclient

Run the S10ntpclient script once by hand, and then it will run automatically every time you reboot.

Explanation:

See http://en.wikipedia.org/wiki/Network_Time_Protocol for an explanation of what NTP is.

The adjtimex line changes the rate at which the system clock runs by 1% relative to the factory default. See the technical explanation at http://www.culcon.com/~henry/nslu2.html(approve sites).
There's a man page for adjtimex at http://linuxcommand.org/man_pages/adjtimex8.html(approve sites) .

Documentation for the ntpclient command is available at http://doolittle.icarus.com/ntpclient/(approve sites) .
The -s means to set the clock, -h selects the NTP host to get the time from, -l says to lock the local clock to the server (MicRo: -l does not work in scripts because the ntpclient -l command never completes which makes a script using the -l parameter hang!).
pool.ntp.org is an organization that tries to make it easy for people to find a low-level NTP server that isn't too busy, the idea being to keep from overloading the higher-level servers as some clueless consumer electronics manufacturers have done in the past.

The crontab sets the clock once per hour, and copies the resulting time to the hardware clock.

The above is an attempt by me (bcrowell) to summarize and organize some previous material. Here are some questions I have. If my understanding of these points is correct, please change them to affirmative statements.

  1. I think ntpclient is normally meant to run continuously in the background. However, this setup seems to run it and then terminate it, starting it again later based on a cron job. This would have the advantage of not taking up memory by having ntpclient always running. Is this correct?
  2. The scripts use > rather than >> to write to ntp.log. I assume this is because old ntp log entries are not of interest, and there's no point in gradually using up more and more disk space. Is this correct?

NB: This timer problem has now been fixed in the Openslug & UcSlugC source HEAD.


I've done a bit of work on this. Here's a scheme that keeps the hardware clock in-line, gets an accurate time at boot, and runs the client.

  • Early in boot, rc.rsttimezone initializes the timezone, calibrates the hardware clock, and sets the initial time.
  • When crond initializes, we edit /etc/crontab to get the hourly updates
  • When the network comes up, S10ntpclient gets the network time, sets the system time, and starts the daemon.
  • From then on, the client keeps the system time accurate, and the crond job keeps the hardware clock accurate.

The requisite scripts are provided below. The diversion script edits, rather than replaces, crontab. This will minimize interactions with other users of crontab.

It would be nice if the ntpclient kit were updated with these, but I don't have time at the moment..

Be sure to name and place them exactly as specified. Be sure to delete the leading space from each line. And be sure that you get the very long lines -- without wrapping. [but note: you've spelled rstimezone two different ways! :^)]

Also, remember to:

    chmod +x /unslung/rc.rstimezone /unslung/rc.crond /opt/etc/init.d/S10ntpclient

Enjoy,

    --tlhackque

/unslung/rc.rstimezone

#! /bin/sh
# Diversion script to get control of time
#
# Extract the GUI timezone from the .conf file
# Copy the corresponding /usr/zoneinfo file over /usr/local/localtime
/usr/sbin/Set_TimeZone >/dev/null
#
# Adjust the hardware clock for accumulated drift
#
/usr/sbin/hwclock --adjust
#
# Initialize the kernel with the corrected time
#
/usr/sbin/hwclock --hctosys >/dev/null
#
# ntpclient will (shortly) start correcting the system clock
#
# The hwclock drift will be computed & recorded once an hour (crontab)
#
# Do not execute the linksys script
#
return 0
# EOF - include this line

/unslung/rc.crond

#! /bin/sh
# Diversion script: customize before starting crond
#
# Reason: change the periodic update of the hwclock to use --adjust
#
if [ ! -e /etc/crontab.orig ]; then cp -p /etc/crontab /etc/crontab.orig; fi
sed -i -e 's/1 \* \* \* \* root \/usr\/sbin\/hwclock -s \&>\/dev\/null/1 \* \* \* \* root \/usr\/sbin\/hwclock --adjust --utc \&>\/dev\/null/' /etc/crontab
#
# Continue to start crond
#
return 1
# EOF - include this line

/etc/crontab After editing, it will look like this. The original is saved as crontab.orig.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/
# ---------- ---------- Default is Empty ---------- ---------- #
0 0-23/8 * * * root /usr/sbin/CheckDiskFull &>/dev/null
0 0 * * * root /usr/sbin/WatchDog &>/dev/null
1 * * * * root /usr/sbin/hwclock --adjust --utc &>/dev/null

**** Don't copy crontab to your system; let the script do the edits ****

/opt/etc/init.d/S10ntpclient

#!/bin/sh
#
if [ -n "`pidof ntpclient`" ]; then
    /bin/killall ntpclient 2>/dev/null
fi
#
# Initialize kernel variables?
#
#/opt/sbin/adjtimex -t 10000
#
# Simple set first.  This will abruptly ensure that we are in sync with the server
# if the server responds, we'll set the hwclock to match the response
#
/opt/bin/ntpclient -s -l -h 0.pool.ntp.org > /var/log/ntp.log && hwclock --systohc --utc
#
# Now start daemon to keep us in sync
#
/opt/bin/ntpclient -i 600 -l -h 0.pool.ntp.org >> /var/log/ntp.log & 
# EOF - include this line

Don't forget to set your timezone

In Debian, tzconfig


Gordon Baskin adds:

The file /opt/etc/init.d/S10ntpclient could be changed to eliminate the output of adjtimex:

#!/bin/sh    
if [ -n "`pidof ntpclient`" ]; then    
    /bin/killall ntpclient 2>/dev/null    
fi    
/opt/sbin/adjtimex -t 10000 >/dev/null   
/opt/bin/ntpclient -s -h pool.ntp.org > /var/log/ntp.log
/usr/sbin/hwclock --systohc --utc

Chong Lee adds: Or you could use the -q flag:

/opt/sbin/adjtimex -q -t 10000

At least for ntpclient version 2007_365-1


view · edit · print · history · Last edited by Chong Lee.
Based on work by Chong Lee, Ralph Finch, RalphFinch, Gordon Baskin, MicRo, JW, tomb, bcrowell, Reedy Boy, an anonymous user of NSLU2-Linux, emm_is, cnczane, blaster8, UncleOp, rob, tlhackque, tman, perlguru, and jbuzbee.
Originally by rwhitby.
Page last modified on January 30, 2009, at 11:55 PM