Skip to content

Commit

Permalink
Merge pull request #6 from Artoria2e5/ptp
Browse files Browse the repository at this point in the history
Add /dev/ptp0 passthrough
  • Loading branch information
simonrupf authored Sep 25, 2024
2 parents d5c59a6 + b59e070 commit 424dc8d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,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
Expand Down
5 changes: 5 additions & 0 deletions assets/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
restart: always
ports:
- 123:123/udp
# devices:
# - /dev/ptp0:/dev/ptp0
environment:
- NTP_SERVERS=time.cloudflare.com
- LOG_LEVEL=0
Expand Down
10 changes: 10 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ 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 \
Expand All @@ -26,6 +35,7 @@ function start_container() {
--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
}
Expand Down
3 changes: 3 additions & 0 deletions vars
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 424dc8d

Please sign in to comment.