Sync guest wall clock from KVM PTP after standby restore#302
Merged
Conversation
The guest realtime clock resumes from the snapshot's saved time after a standby restore and nothing corrects it: the guest runs no NTP daemon and VMGenID only reseeds the RNG. Add a clock keeper to guest-agent that reads host time from /dev/ptp0 and steps CLOCK_REALTIME when it drifts past one second, checking every 30s and immediately when the vmgenid kmsg line signals a restore. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Setting the guest clock back an hour before standby is deterministic and adds no wall time, and the post-restore convergence check fails hard without the clock keeper instead of relying on accumulated lag. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
yummybomb
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The guest wall clock lags after a standby restore:
CLOCK_REALTIMEresumes from the snapshot's saved time and nothing corrects it. The guest runs no NTP daemon, and VMGenID's only guest-side effect is an RNG reseed (add_vmfork_randomness) — it never touches the clock. Verified on a live instance: guest was ~5–6 minutes behind after standby cycles, while/dev/ptp0(KVM virtual PTP,CONFIG_PTP_1588_CLOCK_KVM=yin the current guest kernel) returned correct host time the whole while.This adds a clock keeper to guest-agent that consumes that already-present time source:
clock_gettimeon/dev/ptp0(FD_TO_CLOCKID) and stepsCLOCK_REALTIMEwhen drift exceeds 1scrng reseeded due to virtual machine fork) signals a restore, so correction is near-instant on resume/dev/ptp0is absentNo host-side changes, no RPC/proto changes, no kernel or hypervisor version bump. Works with the kernels and VMMs already deployed.
Alternative considered: VMClock kernel backport
A VMClock-based path exists (
ch-6.16.9-kernel-0.1-202605301carries the backported generation-counter/notification driver, previously wired up in #254) but was deliberately not taken: even with VMClock the kernel never setsCLOCK_REALTIMEitself, so a userspace clock-setter is needed either way. That path would additionally require jumping guests from 6.12.8 to 6.16.9 and bumping Firecracker to v1.15.1 for the notification support — a much larger blast radius for the same end result. VMClock remains a clean future upgrade for the trigger (device notification instead of the kmsg line), without changing this keeper's structure.Test plan
go build(linux + darwin),go vet, guest_agent unit tests pass locallyTestStandbyAndRestoreextended minimally: the guest clock is skewed back an hour before standby, and after restore the test asserts it converges to within 5s of host time. Deterministic and adds no test wall time; fails without the keeper (guest would stay an hour behind)/dev/kvmnested virt🤖 Generated with Claude Code
Note
Medium Risk
Guest-side
clock_settimeon every restore and periodic drift checks could affect time-sensitive workloads; scope is limited to Linux guests with/dev/ptp0and no host API changes.Overview
Adds a guest-agent clock keeper (Linux only) so
CLOCK_REALTIMErealigns with the host after standby restore, when the guest resumes from snapshot time with no NTP.The keeper reads time from
/dev/ptp0viaclock_gettime, steps the wall clock when drift exceeds 1s, runs a 30s periodic check, and triggers an immediate sync when/dev/kmsgshows the vmgenid fork line (crng reseeded due to virtual machine fork). It starts after guest-agent readiness; non-Linux builds no-op. No host, RPC, or kernel changes.TestStandbyAndRestoreskews the guest clock back one hour before standby and asserts it converges to within ±5s of host time after restore, so the test fails without the keeper.Reviewed by Cursor Bugbot for commit 210cf46. Bugbot is set up for automated code reviews on this repo. Configure here.