| # |
| # Copyright 2022 Google LLC |
| # |
| # This program is free software; you can redistribute it and/or |
| # modify it under the terms of the GNU General Public License |
| # version 2 as published by the Free Software Foundation. |
| # |
| # This program is distributed in the hope that it will be useful, |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| # GNU General Public License for more details. |
| # |
| #!/bin/bash |
| |
| MAX_ATTEMPTS=3 |
| CHRONY_SOURCEDIR=/run/chrony-dhcp |
| SERVERFILE=${CHRONY_SOURCEDIR}/chrony.sources |
| |
| # Delete the files if the sources already exist |
| if [ -f "${SERVERFILE}" ]; then |
| rm -f "${SERVERFILE}" |
| /usr/bin/chronyc reload sources > /dev/null 2>&1 || : |
| fi |
| |
| # retry for leases as sometimes it takes time to bring network fully up. |
| ATTEMPTS=${MAX_ATTEMPTS} |
| until false |
| do |
| if [ -n "$(ls -A /run/systemd/netif/leases/ 2>/dev/null)" ]; then |
| break |
| else |
| sleep 3 |
| _=$((ATTEMPTS--)) |
| fi |
| if [ "${ATTEMPTS}" -eq 0 ]; then |
| break |
| fi |
| done |
| |
| |
| networkd_ntp=$(sed -n 's/NTP=//p' /run/systemd/netif/leases/* 2>/dev/null) |
| mkdir -p "${CHRONY_SOURCEDIR}" |
| |
| |
| for server in ${networkd_ntp}; do |
| echo "server ${server} iburst" >> "${SERVERFILE}" |
| done |
| |
| /usr/bin/chronyc reload sources > /dev/null 2>&1 || : |