mirror of
				https://github.com/linuxkit/linuxkit.git
				synced 2025-11-04 03:59:14 +00:00 
			
		
		
		
	Fix dhcp ntp on osx
1. Upstream issue in Alpine that default directory is not used. 2. dhcp appends ntp servers, so delete default first. Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
		@@ -6,7 +6,7 @@ initrd.img: Dockerfile mkinitrd.sh init $(ETCFILES)
 | 
			
		||||
	$(MAKE) -C kernel
 | 
			
		||||
	$(MAKE) -j -C packages
 | 
			
		||||
	tar cf - \
 | 
			
		||||
	  Dockerfile etc init mkinitrd.sh \
 | 
			
		||||
	  Dockerfile etc usr init mkinitrd.sh \
 | 
			
		||||
	  -C kernel usr etc sbin lib -C .. \
 | 
			
		||||
	  -C packages/proxy usr sbin etc -C ../.. \
 | 
			
		||||
	  -C packages/transfused sbin etc -C ../.. \
 | 
			
		||||
 
 | 
			
		||||
@@ -46,10 +46,20 @@ setxtrarg() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
start() {
 | 
			
		||||
	[ "$(mobyplatform)" = "windows" ] && exit 0
 | 
			
		||||
 | 
			
		||||
	[ "$(mobyplatform)" = "aws" ] && \
 | 
			
		||||
		sed -i -e "s/^server [^ ]\+/server 0.amazon.pool.ntp.org/g" /etc/chrony/chrony.conf
 | 
			
		||||
	case "$(mobyplatform)" in
 | 
			
		||||
		"windows")
 | 
			
		||||
			exit 0
 | 
			
		||||
		;;
 | 
			
		||||
		"mac")
 | 
			
		||||
			sed -i '/server pool.ntp.org/d' /etc/chrony/chrony.conf
 | 
			
		||||
		;;
 | 
			
		||||
		"aws")
 | 
			
		||||
			sed -i -e "s/^server [^ ]\+/server 0.amazon.pool.ntp.org/g" /etc/chrony/chrony.conf
 | 
			
		||||
		;;
 | 
			
		||||
		"azure")
 | 
			
		||||
			# TODO needs an ntp solution
 | 
			
		||||
		;;
 | 
			
		||||
	esac
 | 
			
		||||
 | 
			
		||||
	checkconfig || return $?
 | 
			
		||||
	setxtrarg
 | 
			
		||||
 
 | 
			
		||||
@@ -1,26 +0,0 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
if [ "x$interface" != "xeth0" ] ; then exit 0 ; fi
 | 
			
		||||
 | 
			
		||||
if cat /proc/cmdline | grep -q '\bntp=gateway\b' ; then
 | 
			
		||||
    if [ -n "$router" ] ; then
 | 
			
		||||
        logger -t udhcpc "ntp=gateway used, using \$router=$router as \$ntpsrv"
 | 
			
		||||
        server="$router trust"
 | 
			
		||||
    else
 | 
			
		||||
        logger -t udhcpc "ntp=gateway used but \$router not provided"
 | 
			
		||||
    fi
 | 
			
		||||
elif [ -n "$ntpsrv" ] ; then
 | 
			
		||||
    # Just take the first
 | 
			
		||||
    set -- $ntpsrv
 | 
			
		||||
    server="$1"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ -z "$server" ] ; then
 | 
			
		||||
    logger -t udhcpc "No NTP server via DHCP"
 | 
			
		||||
    exit 0
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
sed -i -e "s/^server [^ ]\+/server $server/g" /etc/chrony/chrony.conf
 | 
			
		||||
 | 
			
		||||
logger -t udhcpc "Restarting chronyd with server $server"
 | 
			
		||||
service -q chronyd conditionalrestart
 | 
			
		||||
							
								
								
									
										141
									
								
								alpine/usr/lib/dhcpcd/dhcpcd-hooks/50-ntp.conf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										141
									
								
								alpine/usr/lib/dhcpcd/dhcpcd-hooks/50-ntp.conf
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,141 @@
 | 
			
		||||
# Sample dhcpcd hook script for NTP
 | 
			
		||||
# It will configure either one of NTP, OpenNTP or Chrony (in that order)
 | 
			
		||||
# and will default to NTP if no default config is found.
 | 
			
		||||
 | 
			
		||||
# Like our resolv.conf hook script, we store a database of ntp.conf files
 | 
			
		||||
# and merge into /etc/ntp.conf
 | 
			
		||||
 | 
			
		||||
# You can set the env var NTP_CONF to override the derived default on
 | 
			
		||||
# systems with >1 NTP client installed.
 | 
			
		||||
# Here is an example for OpenNTP
 | 
			
		||||
#   dhcpcd -e NTP_CONF=/usr/pkg/etc/ntpd.conf
 | 
			
		||||
# or by adding this to /etc/dhcpcd.conf
 | 
			
		||||
#   env NTP_CONF=/usr/pkg/etc/ntpd.conf
 | 
			
		||||
# or by adding this to /etc/dhcpcd.enter-hook
 | 
			
		||||
#   NTP_CONF=/usr/pkg/etc/ntpd.conf
 | 
			
		||||
# To use Chrony instead, simply change ntpd.conf to chrony.conf in the
 | 
			
		||||
# above examples.
 | 
			
		||||
 | 
			
		||||
: ${ntp_confs:=ntp.conf ntpd.conf chrony.conf}
 | 
			
		||||
: ${ntp_conf_dirs=/etc /usr/pkg/etc /usr/local/etc /etc/chrony}
 | 
			
		||||
ntp_conf_dir="$state_dir/ntp.conf"
 | 
			
		||||
 | 
			
		||||
# If NTP_CONF is not set, work out a good default
 | 
			
		||||
if [ -z "$NTP_CONF" ]; then
 | 
			
		||||
	for d in ${ntp_conf_dirs}; do
 | 
			
		||||
		for f in ${ntp_confs}; do
 | 
			
		||||
			if [ -e "$d/$f" ]; then
 | 
			
		||||
				NTP_CONF="$d/$f"
 | 
			
		||||
				break 2
 | 
			
		||||
			fi
 | 
			
		||||
		done
 | 
			
		||||
	done
 | 
			
		||||
	[ -e "$NTP_CONF" ] || NTP_CONF=/etc/ntp.conf
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Derive service name from configuration
 | 
			
		||||
if [ -z "$ntp_service" ]; then
 | 
			
		||||
	case "$NTP_CONF" in
 | 
			
		||||
	*chrony.conf)		ntp_service=chronyd;;
 | 
			
		||||
	*)			ntp_service=ntpd;;
 | 
			
		||||
	esac
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Debian has a seperate file for DHCP config to avoid stamping on
 | 
			
		||||
# the master.
 | 
			
		||||
if [ "$ntp_service" = ntpd ] && type invoke-rc.d >/dev/null 2>&1; then
 | 
			
		||||
	[ -e /var/lib/ntp ] || mkdir /var/lib/ntp
 | 
			
		||||
	: ${ntp_service:=ntp}
 | 
			
		||||
	: ${NTP_DHCP_CONF:=/var/lib/ntp/ntp.conf.dhcp}
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
: ${ntp_restart_cmd:=service_condcommand $ntp_service restart}
 | 
			
		||||
 | 
			
		||||
ntp_conf=${NTP_CONF}
 | 
			
		||||
NL="
 | 
			
		||||
"
 | 
			
		||||
 | 
			
		||||
build_ntp_conf()
 | 
			
		||||
{
 | 
			
		||||
	local cf="$state_dir/ntp.conf.$ifname"
 | 
			
		||||
	local interfaces= header= srvs= servers= x=
 | 
			
		||||
 | 
			
		||||
	# Build a list of interfaces
 | 
			
		||||
	interfaces=$(list_interfaces "$ntp_conf_dir")
 | 
			
		||||
 | 
			
		||||
	if [ -n "$interfaces" ]; then
 | 
			
		||||
		# Build the header
 | 
			
		||||
		for x in ${interfaces}; do
 | 
			
		||||
			header="$header${header:+, }$x"
 | 
			
		||||
		done
 | 
			
		||||
 | 
			
		||||
		# Build a server list
 | 
			
		||||
		srvs=$(cd "$ntp_conf_dir";
 | 
			
		||||
			key_get_value "server " $interfaces)
 | 
			
		||||
		if [ -n "$srvs" ]; then
 | 
			
		||||
			for x in $(uniqify $srvs); do
 | 
			
		||||
				servers="${servers}server $x$NL"
 | 
			
		||||
			done
 | 
			
		||||
		fi
 | 
			
		||||
	fi
 | 
			
		||||
 | 
			
		||||
	# Merge our config into ntp.conf
 | 
			
		||||
	[ -e "$cf" ] && rm -f "$cf"
 | 
			
		||||
	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
 | 
			
		||||
 | 
			
		||||
	if [ -n "$NTP_DHCP_CONF" ]; then
 | 
			
		||||
		[ -e "$ntp_conf" ] && cp "$ntp_conf" "$cf"
 | 
			
		||||
		ntp_conf="$NTP_DHCP_CONF"
 | 
			
		||||
	elif [ -e "$ntp_conf" ]; then
 | 
			
		||||
		remove_markers "$signature_base" "$signature_base_end" \
 | 
			
		||||
			"$ntp_conf" > "$cf"
 | 
			
		||||
	fi
 | 
			
		||||
 | 
			
		||||
	if [ -n "$servers" ]; then
 | 
			
		||||
		echo "$signature_base${header:+ $from }$header" >> "$cf"
 | 
			
		||||
		printf %s "$servers" >> "$cf"
 | 
			
		||||
		echo "$signature_base_end${header:+ $from }$header" >> "$cf"
 | 
			
		||||
	else
 | 
			
		||||
		[ -e "$ntp_conf" -a -e "$cf" ] || return
 | 
			
		||||
	fi
 | 
			
		||||
 | 
			
		||||
	# If we changed anything, restart ntpd
 | 
			
		||||
	if change_file "$ntp_conf" "$cf"; then
 | 
			
		||||
		[ -n "$ntp_restart_cmd" ] && eval $ntp_restart_cmd
 | 
			
		||||
	fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
add_ntp_conf()
 | 
			
		||||
{
 | 
			
		||||
	local cf="$ntp_conf_dir/$ifname" x=
 | 
			
		||||
 | 
			
		||||
	[ -e "$cf" ] && rm "$cf"
 | 
			
		||||
	[ -d "$ntp_conf_dir" ] || mkdir -p "$ntp_conf_dir"
 | 
			
		||||
	if [ -n "$new_ntp_servers" ]; then
 | 
			
		||||
		for x in $new_ntp_servers; do
 | 
			
		||||
			echo "server $x" >> "$cf"
 | 
			
		||||
		done
 | 
			
		||||
	fi
 | 
			
		||||
	build_ntp_conf
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
remove_ntp_conf()
 | 
			
		||||
{
 | 
			
		||||
	if [ -e "$ntp_conf_dir/$ifname" ]; then
 | 
			
		||||
		rm "$ntp_conf_dir/$ifname"
 | 
			
		||||
	fi
 | 
			
		||||
	build_ntp_conf
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# For ease of use, map DHCP6 names onto our DHCP4 names
 | 
			
		||||
case "$reason" in
 | 
			
		||||
BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
 | 
			
		||||
	new_ntp_servers="$new_dhcp6_sntp_servers"
 | 
			
		||||
;;
 | 
			
		||||
esac
 | 
			
		||||
 | 
			
		||||
if $if_up; then
 | 
			
		||||
	add_ntp_conf
 | 
			
		||||
elif $if_down; then
 | 
			
		||||
	remove_ntp_conf
 | 
			
		||||
fi
 | 
			
		||||
		Reference in New Issue
	
	Block a user