/
coreos-platform-chrony
67 lines (59 loc) · 2.67 KB
/
coreos-platform-chrony
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
set -euo pipefail
# Configuring the timeserver for the platform is often handled
# by pre-baking a config into a particular image for a platform, but
# that doesn't work for us because we have a single update stream. Hence
# this generator dynamically inspects the platform and reconfigures chrony.
#
# AWS: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html
# Azure: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/time-sync
# GCP: https://cloud.google.com/compute/docs/instances/managing-instances#configure-ntp
#
# Originally spawned from discussion in https://github.com/openshift/installer/pull/3513
# Generators don't have logging right now
# https://github.com/systemd/systemd/issues/15638
exec 1>/dev/kmsg; exec 2>&1
self=$(basename $0)
confpath=/run/coreos-platform-chrony.conf
# Yeah this isn't a completely accurate kernel argument parser but
# we don't have one shared across shell services at the moment.
platform="$(grep -Eo ' ignition.platform.id=[a-z]+' /proc/cmdline | cut -f 2 -d =)"
case "${platform}" in
azure|aws|gcp) ;; # OK, this is a platform we know how to support
*) exit 0 ;;
esac
if ! cmp {/usr,}/etc/chrony.conf >/dev/null; then
echo "$self: /etc/chrony.conf is modified; not changing the default"
exit 0
fi
echo "# Generated by $self - do not edit directly" > "${confpath}"
case "${platform}" in
azure)
(echo '# See also https://docs.microsoft.com/en-us/azure/virtual-machines/linux/time-sync'
sed -e s,'^pool,#pool,' < /etc/chrony.conf
echo 'refclock PHC /dev/ptp0 poll 3 dpoll -2 offset 0'
) >> "${confpath}" ;;
aws)
(echo '# See also https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html'
sed -e s,'^pool,#pool,' < /etc/chrony.conf
echo 'server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4'
) >> "${confpath}" ;;
gcp)
(echo '# See also https://cloud.google.com/compute/docs/instances/managing-instances#configure-ntp'
echo '# and https://cloud.google.com/compute/docs/images/configuring-imported-images'
sed -e s,'^pool,#pool,' < /etc/chrony.conf
echo 'server metadata.google.internal prefer iburst'
) >> "${confpath}" ;;
*) echo "should not be reached" 1>&2; exit 1 ;;
esac
# Policy doesn't allow chronyd to read run_t
chcon --reference=/etc/chrony.conf "${confpath}"
UNIT_DIR="${1:-/tmp}"
unitconfpath="${UNIT_DIR}/chronyd.service.d/coreos-platform-chrony.conf"
mkdir -p $(dirname "${unitconfpath}")
cat >"${unitconfpath}" << EOF
[Service]
ExecStart=
ExecStart=/usr/sbin/chronyd -f ${confpath} \$OPTIONS
EOF
echo "$self: Updated chrony to use ${platform} configuration ${confpath}"