Skip to content

Commit

Permalink
Move chrony config from generator to systemd service
Browse files Browse the repository at this point in the history
With latest changes from systemd, we now run certain
steps before NetworkManager.service to access and write
to /etc/sysconfig/network.
Issue: coreos/fedora-coreos-tracker#1402
  • Loading branch information
gursewak1997 committed Mar 3, 2023
1 parent 129880b commit 8080055
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# To inspect the platform and configure chrony
enable coreos-platform-chrony-config.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Override chrony configuration with the output of
# coreos-platform-chrony-config.service.
[Service]
EnvironmentFile=-/run/sysconfig-coreos-chrony
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Configure chrony based on the platform
ConditionKernelCommandLine=|ignition.platform.id=azurestack
ConditionKernelCommandLine=|ignition.platform.id=azure
ConditionKernelCommandLine=|ignition.platform.id=aws
ConditionKernelCommandLine=|ignition.platform.id=gcp
Before=NetworkManager.service
Before=chronyd.service

[Service]
Type=oneshot
ExecStart=/usr/libexec/coreos-platform-chrony-config
RemainAfterExit=yes
47 changes: 22 additions & 25 deletions .../system-generators/coreos-platform-chrony → ...usr/libexec/coreos-platform-chrony-config
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ 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.
# this service 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

. /usr/lib/coreos/generator-lib.sh

self=$(basename $0)
confpath=/run/coreos-platform-chrony.conf

platform=$(karg ignition.platform.id)
case "${platform}" in
azure|azurestack|aws|gcp) ;; # OK, this is a platform we know how to support
*) exit 0 ;;
esac

# Exit early if we have already been run once
if [[ -f "${confpath}" ]]; then
echo "$self: ${confpath} already exists; skipping"
exit 0
fi
self=$(basename "$0")

# Exit early if chrony configuration as been changed from the image default
# Exit early if chrony configuration has been changed from the image default
if ! cmp {/usr,}/etc/chrony.conf >/dev/null; then
echo "$self: /etc/chrony.conf is modified; not changing the default"
exit 0
fi

confpath=/run/coreos-platform-chrony.conf
altenvfilepath=/run/sysconfig-coreos-chrony
cmdline=( $(</proc/cmdline) )
cmdline_arg() {
local name="$1" value
for arg in "${cmdline[@]}"; do
if [[ "${arg%%=*}" == "${name}" ]]; then
value="${arg#*=}"
fi
done
echo "${value}"
}
platform=$(cmdline_arg ignition.platform.id)

# If not set already (by host customization or this script), set
# PEERNTP=no so that DHCP-provided NTP servers are not added to chrony.
# By doing this we assume the better NTP server choice is the
Expand Down Expand Up @@ -82,14 +82,11 @@ 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
# Read in the existing $OPTIONS variable setting from /etc/sysconfig/chrony
# and write out a new $OPTIONS variable (with specified new configuration path)
# to /run/sysconfig-coreos-chrony
source ${altenvfilepath}
echo "OPTIONS='${OPTIONS} -f ${confpath}'" > ${altenvfilepath}

echo "$self: Updated chrony to use ${platform} configuration ${confpath}"
14 changes: 0 additions & 14 deletions tests/kola/ntp/chrony/coreos-platform-chrony-generator-permissions

This file was deleted.

0 comments on commit 8080055

Please sign in to comment.