From 9debff637fb4fedde14d6e76789042fae7cec059 Mon Sep 17 00:00:00 2001 From: Joel Low Date: Fri, 14 Aug 2026 15:46:27 +0800 Subject: [PATCH] feat: add guest-to-host time synchronisation --- ansible/files/phc2sys.service.j2 | 10 +++++ ansible/tasks/setup-kvm-timesync.yml | 55 ++++++++++++++++++++++++++++ ansible/tasks/setup-system.yml | 6 +++ 3 files changed, 71 insertions(+) create mode 100644 ansible/files/phc2sys.service.j2 create mode 100644 ansible/tasks/setup-kvm-timesync.yml diff --git a/ansible/files/phc2sys.service.j2 b/ansible/files/phc2sys.service.j2 new file mode 100644 index 000000000..3e949d3a4 --- /dev/null +++ b/ansible/files/phc2sys.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Sync system clock to KVM PTP clock +After=dev-ptp0.device + +[Service] +ExecStart=/usr/sbin/phc2sys -s /dev/ptp0 -O 0 -m +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/ansible/tasks/setup-kvm-timesync.yml b/ansible/tasks/setup-kvm-timesync.yml new file mode 100644 index 000000000..243c2a33d --- /dev/null +++ b/ansible/tasks/setup-kvm-timesync.yml @@ -0,0 +1,55 @@ +# Replace systemd-timesyncd with phc2sys which uses the KVM-exposed PTP timesource. Don't run NTP syncs within the VM +# since that drifts more, and requires network access. phc2sys doesn't require any network access. +- name: phc2sys - install + ansible.builtin.apt: + pkg: + - linuxptp + +- name: phc2sys - create service file + ansible.builtin.template: + src: files/phc2sys.service.j2 + dest: /etc/systemd/system/phc2sys.service + +- name: phc2sys - enable service + ansible.builtin.systemd_service: + daemon_reload: true + enabled: true + name: 'phc2sys' + state: 'stopped' + +- name: phc2sys - remove systemd-timesyncd + ansible.builtin.apt: + pkg: + - systemd-timesyncd + purge: true + state: absent + when: + - ansible_distribution == "Ubuntu" and ansible_distribution_version == "24.04" + +- name: ptp_kvm.ko - enable + ansible.builtin.copy: + content: | + ptp_kvm + dest: /etc/modules-load.d/ptp_kvm.conf + +# On Ubuntu 24.04, ptp_kvm.ko is installed with the much larger linux-modules-extra package until after Kernel 6.8. +# https://bugs.launchpad.net/ubuntu/+source/linux-gcp/+bug/2110241 +# +# I didn't bisect every version because rebuilding a test image takes ~45 minutes. We can remove when we've fully +# migrated to Ubuntu 26.04. +- name: Upgrade running kernel + ansible.builtin.apt: + pkg: + - linux-virtual-7.0 + when: + - ansible_distribution == "Ubuntu" and ansible_distribution_version == "24.04" + +- name: Remove old kernel + ansible.builtin.apt: + pkg: + - linux-image-virtual + autoremove: true + purge: true + state: absent + when: + - ansible_distribution == "Ubuntu" and ansible_distribution_version == "24.04" diff --git a/ansible/tasks/setup-system.yml b/ansible/tasks/setup-system.yml index 6777b7248..fda2627b1 100644 --- a/ansible/tasks/setup-system.yml +++ b/ansible/tasks/setup-system.yml @@ -91,6 +91,12 @@ changed_when: false failed_when: false +- name: Ensure guest VM time synchronisation + ansible.builtin.import_tasks: + file: tasks/setup-kvm-timesync.yml + when: + - qemu + - name: Execute more tasks when stage1 when: not stage2 block: