ntp before anything else

This forum is for topics specific to the Raspberry Pi and Arch Linux ARM

ntp before anything else

Postby cetu86 » Fri Aug 23, 2013 10:04 am

Hi,

I'm currently using the standard ntp configuration shipped with archlinux to sync the raspberry's clock.
After bootup there is a ~2 minute delay until ntpd has fetched the actual time.
This is kind of stupid, because systemd starts all kinds of services before the clock is synchronised.
When I do "systemctl status" I get reports like "this service has been running for 43 years". Yeah!
Also files are touched before synchronisation.

I would like clock-synchronisation to be the first and only thing my system does after loading the kernel.
I have got 2 ideas:

1. Hack systemd (I'm not an expert on systemd, but it shoulnd't be that hard)
2. or Use a custom init, which sets up networking, sets the the time and then "exec"s systemd

How do you guys do that?
cetu86
 
Posts: 3
Joined: Fri Aug 23, 2013 9:46 am

Re: ntp before anything else

Postby pepedog » Fri Aug 23, 2013 4:22 pm

pepedog
Developer
 
Posts: 2431
Joined: Mon Jun 07, 2010 3:30 pm
Location: London UK

Re: ntp before anything else

Postby cetu86 » Fri Aug 23, 2013 4:29 pm

You mean to speed up the synchronisation-process?
Tahnks, that is good to know.

But my main concern is not how long it takes for synchronisation, but I want time-synchronisation to be done before anything else.
That is the rest of the boot process has to be stalled until the clock is correct.
Because I don't want false uptime entries for processes and I don't want wrong timestamps in my filesystem either.
cetu86
 
Posts: 3
Joined: Fri Aug 23, 2013 9:46 am

Re: ntp before anything else

Postby grayman4hire » Fri Aug 23, 2013 11:33 pm

Great question!

I had a similar need, where Plex Media Server required the correct time, or more specifically the time not changing by 40+ years after start up. My solution was to force a time update using ExecStartPre prior to starting the service.

$this->bbcode_second_pass_code('', '
[Unit]
Description=Plex Media Server for Linux
After=network.target

[Service]
EnvironmentFile=-/etc/conf.d/plexmediaserver
ExecStartPre=/usr/bin/ntpdate -u time.nist.gov
ExecStart=/opt/plexmediaserver/start_pms
KillMode=process
Type=forking

[Install]
WantedBy=multi-user.target
')

Another solution would be to create a systemd service, forcetimeupdate.service, that runs the ntpdate command and then change your other services to start after forcetimeupdate starts.

I would love to other thoughts on this issue/solution. Thanks
grayman4hire
 
Posts: 350
Joined: Sat Sep 29, 2012 12:03 am
Location: SF, CA

Re: ntp before anything else

Postby kmihelich » Sat Aug 24, 2013 12:02 am

The way I'm doing things in new rootfs images/tarballs is to add this to the bottom of the eth0 netctl profile:
$this->bbcode_second_pass_code('', 'ExecUpPost='/usr/bin/ntpd -gq || true'')

This makes sure that ntp sets the correct date as soon as possible: as soon as networking is up, which is as soon as it can ever happen.

If you need more than that, you really need to buy a better system that has an RTC with battery backup.
Arch Linux ARM exists and continues to grow through community support, please donate today!
kmihelich
Developer
 
Posts: 1133
Joined: Tue Jul 20, 2010 6:55 am
Location: aka leming #archlinuxarm

Re: ntp before anything else

Postby grayman4hire » Sat Aug 24, 2013 12:05 am

Nice!

For whatever reason, the command "/usr/bin/ntpd -gq || true" doesn't instantly update the time.
grayman4hire
 
Posts: 350
Joined: Sat Sep 29, 2012 12:03 am
Location: SF, CA

Re: ntp before anything else

Postby cetu86 » Tue Aug 27, 2013 9:43 pm

Hi guys,
thank you for your replies. :-)
I've implemented a rather radical solution.
I setup time before systemd starts. That way systemd never gets to see the year 1970.
All I had to do was change /boot/cmdline.txt to have init=/init and write my own init-script /init, that sets up network and ntp like this:
$this->bbcode_second_pass_code('', '
#!/bin/sh

echo "configuring network interface"

source /etc/conf.d/network

# sometimes eth0 isn't ready yet, so retry
a=0
while [ $a -lt 60 ]; do
echo "."
if /sbin/ip link set dev ${interface} up; then
break
fi
sleep 2
a=`expr $a + 1`
done

/sbin/ip addr add ${address}/${netmask} broadcast ${broadcast} dev ${interface}
/sbin/ip route add default via ${gateway}

echo "setting time"
# give ntpd 120 seconds to set the time
timeout 120s ntpd -q -g

# show date to tell that it works
date

echo "starting systemd"
exec /bin/init $@

')

I use a static network configuration, but dhcp should be doable as well, if I ever need it.
cetu86
 
Posts: 3
Joined: Fri Aug 23, 2013 9:46 am

Re: ntp before anything else

Postby pezz » Thu Aug 29, 2013 2:28 am

That's awesome, can't wait to give this a try tonight.
pezz
 
Posts: 76
Joined: Fri Sep 14, 2012 11:19 pm
Location: Geelong, Australia

Re: ntp before anything else

Postby crashmaster » Fri Aug 30, 2013 4:29 am

Thanks cetu86, this makes sense.
crashmaster
 
Posts: 119
Joined: Wed Mar 13, 2013 1:00 am

Re: ntp before anything else

Postby ajs124 » Fri Aug 30, 2013 5:32 pm

I like it, except I named it /ddh for dirty, dirty hack.
ajs124
 
Posts: 5
Joined: Wed Jan 16, 2013 5:21 pm

Next

Return to Raspberry Pi

Who is online

Users browsing this forum: No registered users and 35 guests