|
|
|
@@ -0,0 +1,77 @@
|
|
|
|
|
#!/command/with-contenv bash
|
|
|
|
|
# Generate chrony configuration from add-on options
|
|
|
|
|
|
|
|
|
|
CONFIG_FILE="/etc/chrony/chrony.conf"
|
|
|
|
|
OPTIONS_FILE="/data/options.json"
|
|
|
|
|
|
|
|
|
|
echo "[INFO] Generating Chrony configuration..."
|
|
|
|
|
|
|
|
|
|
# Create config directory
|
|
|
|
|
mkdir -p /etc/chrony /var/lib/chrony /var/log/chrony
|
|
|
|
|
|
|
|
|
|
# Start with base config
|
|
|
|
|
cat > "$CONFIG_FILE" << 'EOF'
|
|
|
|
|
# Chrony configuration - generated by Home Assistant add-on
|
|
|
|
|
driftfile /var/lib/chrony/chrony.drift
|
|
|
|
|
makestep 1.0 3
|
|
|
|
|
rtcsync
|
|
|
|
|
logdir /var/log/chrony
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# Parse options.json with jq
|
|
|
|
|
if [ -f "$OPTIONS_FILE" ]; then
|
|
|
|
|
# Add NTP pools
|
|
|
|
|
pool_count=$(jq '.ntp_pools | length' "$OPTIONS_FILE")
|
|
|
|
|
for i in $(seq 0 $((pool_count - 1))); do
|
|
|
|
|
pool_addr=$(jq -r ".ntp_pools[$i].pool" "$OPTIONS_FILE")
|
|
|
|
|
iburst=$(jq -r ".ntp_pools[$i].iburst" "$OPTIONS_FILE")
|
|
|
|
|
maxsources=$(jq -r ".ntp_pools[$i].maxsources" "$OPTIONS_FILE")
|
|
|
|
|
|
|
|
|
|
pool_line="pool ${pool_addr}"
|
|
|
|
|
[ "$iburst" = "true" ] && pool_line="${pool_line} iburst"
|
|
|
|
|
[ "$maxsources" != "null" ] && pool_line="${pool_line} maxsources ${maxsources}"
|
|
|
|
|
|
|
|
|
|
echo "$pool_line" >> "$CONFIG_FILE"
|
|
|
|
|
echo "[INFO] Added NTP pool: ${pool_addr}"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Add NTP servers
|
|
|
|
|
server_count=$(jq '.ntp_servers | length' "$OPTIONS_FILE")
|
|
|
|
|
for i in $(seq 0 $((server_count - 1))); do
|
|
|
|
|
server_addr=$(jq -r ".ntp_servers[$i].server" "$OPTIONS_FILE")
|
|
|
|
|
iburst=$(jq -r ".ntp_servers[$i].iburst" "$OPTIONS_FILE")
|
|
|
|
|
|
|
|
|
|
if [ "$iburst" = "true" ]; then
|
|
|
|
|
echo "server ${server_addr} iburst" >> "$CONFIG_FILE"
|
|
|
|
|
else
|
|
|
|
|
echo "server ${server_addr}" >> "$CONFIG_FILE"
|
|
|
|
|
fi
|
|
|
|
|
echo "[INFO] Added NTP server: ${server_addr}"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Configure as NTP server if enabled
|
|
|
|
|
enable_server=$(jq -r '.enable_ntp_server' "$OPTIONS_FILE")
|
|
|
|
|
if [ "$enable_server" = "true" ]; then
|
|
|
|
|
echo "[INFO] NTP server mode enabled"
|
|
|
|
|
|
|
|
|
|
# Add allowed client networks
|
|
|
|
|
for network in $(jq -r '.allow_clients[]' "$OPTIONS_FILE"); do
|
|
|
|
|
echo "allow ${network}" >> "$CONFIG_FILE"
|
|
|
|
|
echo "[INFO] Allowing clients from: ${network}"
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Set logging
|
|
|
|
|
detailed_logging=$(jq -r '.detailed_logging' "$OPTIONS_FILE")
|
|
|
|
|
if [ "$detailed_logging" = "true" ]; then
|
|
|
|
|
echo "log tracking measurements statistics" >> "$CONFIG_FILE"
|
|
|
|
|
echo "[INFO] Detailed logging enabled"
|
|
|
|
|
else
|
|
|
|
|
echo "log tracking" >> "$CONFIG_FILE"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "[INFO] Chrony configuration generated successfully"
|
|
|
|
|
|
|
|
|
|
# Start chrony in foreground
|
|
|
|
|
exec chronyd -d -f "$CONFIG_FILE"
|