openwrtntpntpd

Disable chrony NTP server when unsync


I have a OpenWRT router running chrony (chronyd). The hardware doesn't have RTC, so after a power loss, the system clock goes crazy.

I want to have a LAN NTP server, but if somehow have no internet connection and the clock is wrong because a power loss, the NTP server passes the wrong time to the clients. Consider I'm using 'cheap' NTP clients (Android/Arduino/other routers/cameras) that have no way to prevent a big clock slew.

I would like to disable chrony NTP server on reboot and wait to a first sync to enable it. I thought this was a common setting, but couldn't find anywhere.

I've also tried ntpd and I'm open to alternatives (but it must run on OpenWRT).

Note: I'm already trying to use a USB GPS as a more reliable clock source even without internet connection.


Solution

  • My solution so far:

    On chrony.conf, set to be disabled on startup.

    deny all
    

    Then, I created a script to check every minute if clock is sync (using chronyc) and then enable it again.

    until ! chronyc tracking | grep "Reference ID" | grep -q "()"
        do sleep 60
    done
    
    chronyc allow all
    

    Finally, set to run script on startup.

    Seems this in only possible with chrony (not ntpd), as it have a client to change settings on the fly.