diff --git a/README.md b/README.md index 7d0ebf3..71b576d 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,28 @@ Enabling the control requires granting SYS_TIME capability and a container run-t ... ``` +## Enable the use of a PTP clock + +If you have a `/dev/ptp0`, either a real hardware clock or virtual one provided by a VM host +you can enable the use of it by passing the device to the container. As an example, +using `docker-compose.yaml`, that would look like this: + +```yaml + ... + devices: + - /dev/ptp0:/dev/ptp0 +``` + +This will allow chronyd to use the PTP clock as a reference clock. A virtual clock simply provides +the host's system time with great precision and stability; whether that time is accurate depends +on the host provider. In our experience, some VPS vendors give pretty good time (off by +milliseconds), while others are off by seconds. + +For information on configuring the host to have a virtual PTP clock, see the following: + + * https://opensource.com/article/17/6/timekeeping-linux-vms + + ## Testing your NTP Container From any machine that has `ntpdate` you can query your new NTP container with the follow diff --git a/assets/startup.sh b/assets/startup.sh index 0a5d8d3..04e33e5 100755 --- a/assets/startup.sh +++ b/assets/startup.sh @@ -65,6 +65,11 @@ for N in $NTP_SERVERS; do fi done +# PTP0 configuration: if it has been passed through, it means we want to use it +if [ -e /dev/ptp0 ]; then + echo "refclock PHC /dev/ptp0 poll 3 dpoll -2 stratum 2" >> ${CHRONY_CONF_FILE} +fi + # final bits for the config file { echo diff --git a/docker-compose.yml b/docker-compose.yml index 9f2f05e..ad6dd6f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,9 +7,11 @@ services: container_name: ntp restart: always ports: - - 123:123/udp + - 1123:1123/udp +# devices: +# - /dev/ptp0:/dev/ptp0 environment: - - NTP_SERVERS=time.cloudflare.com +# - NTP_SERVERS=time.cloudflare.com - LOG_LEVEL=0 # - TZ=America/Vancouver # - NOCLIENTLOG=true diff --git a/run.sh b/run.sh index 9e1028b..1a35263 100755 --- a/run.sh +++ b/run.sh @@ -12,20 +12,31 @@ function check_container() { # function to start new docker container function start_container() { + if [ "${ENABLE_PTP:-false}" = true ]; then + echo "PTP requested..." + if [ -e /dev/ptp0 ]; then + echo "PTP device found: /dev/ptp0, passing through..." + PTPARG="--device=/dev/ptp0" + else + echo "PTP device not found: /dev/ptp0" + fi + fi $DOCKER run --name=${CONTAINER_NAME} \ --detach=true \ --restart=always \ - --publish=123:123/udp \ + --publish=1123:1123/udp \ --env=NTP_SERVERS=${NTP_SERVERS} \ --env=ENABLE_NTS=${ENABLE_NTS} \ --env=ENABLE_SYSCLK=${ENABLE_SYSCLK} \ --env=NOCLIENTLOG=${NOCLIENTLOG} \ --env=LOG_LEVEL=${LOG_LEVEL} \ + --env=ENABLE_PTP=${ENABLE_PTP} \ --cap-add=SYS_TIME \ --read-only=true \ --tmpfs=/etc/chrony:rw,mode=1750 \ --tmpfs=/run/chrony:rw,mode=1750 \ --tmpfs=/var/lib/chrony:rw,mode=1750 \ + $PTPARG \ ${DOCKER_OPTS} \ ${IMAGE_NAME}:latest > /dev/null } diff --git a/vars b/vars index 72b9e75..0e6c385 100644 --- a/vars +++ b/vars @@ -28,3 +28,6 @@ LOG_LEVEL=0 # (optional) additional docker run options you may want DOCKER_OPTS="" + +# (optional) ask run.sh to pass /dev/ptp0 +RENABLE_PTP=false