Skip to content

feat: Add --timesync flag for guest clock synchronization#89

Merged
slp merged 2 commits into
libkrun:mainfrom
vyasgun:pr/timesync
Feb 3, 2026
Merged

feat: Add --timesync flag for guest clock synchronization#89
slp merged 2 commits into
libkrun:mainfrom
vyasgun:pr/timesync

Conversation

@vyasgun
Copy link
Copy Markdown
Contributor

@vyasgun vyasgun commented Jan 13, 2026

Add a --timesync option which uses vsock to send host time to qemu-guest-agent running in the guest on VM suspend/resume.

Uses IOKit's power management APIs to detect system sleep/wake events. Since the official iokit-sys crate is incomplete, a custom fork is required.

This also maintains feature parity between krunkit and vfkit.

Fixes: #88

To test this, start krunkit with the --timesync <vsock-port> flag and other arguments as follows:

./target/release/krunkit --cpus 4 --memory 10752 --timesync vsockPort=1234 --bootloader efi,variable-store=efistore.nvram,create --device virtio-blk,path=/Users/gvyas/Downloads/Fedora-Cloud-Base-AmazonEC2-42-1.1.aarch64.raw  --device virtio-rng --device virtio-serial,logFilePath=krunkit.log --device virtio-net,unixSocketPath=/tmp/vfkit.sock,mac=5a:94:ef:e4:0c:ee --device virtio-fs,sharedDir=/Users/gvyas,mountTag=dir0 --krun-log-level 3 

Inside the guest, qemu-guest-agent process should be running:

 /usr/bin/qemu-ga --method=vsock-listen --path=3:1234 --logfile=/var/log/qemu-ga.log -v

This is run as a systemd service on CRC which can be configured using cloud-init.

After this, put the host to sleep and check the time in the VM when it wakes up.

Copy link
Copy Markdown
Collaborator

@tylerfanelli tylerfanelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @vyasgun, I've added some comments. I see this is still a draft, so apologies if I address points you were already working on.

Can you add a description of how to test?

Comment thread Cargo.toml Outdated
log = "0.4.0"
env_logger = "0.11.8"
regex = "1.11.1"
IOKit-sys = { git = "https://github.com/vyasgun/iokit-sys", branch = "master" }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would likely need to be an official release before we accept it as a dependency.

Copy link
Copy Markdown
Contributor Author

@vyasgun vyasgun Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An official release is going to be unlikely because this repository was last updated in 2018 😓 I opened a PR there last month: dcuddeback/iokit-sys#14 but doubt it would be merged.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't rely on it then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found another crate with all the bindings which is being actively maintained: https://crates.io/crates/objc2-io-kit

Comment thread src/context.rs Outdated
Comment thread src/sleep_notifier.rs Outdated
Comment thread src/main.rs Outdated
Comment on lines +7 to +9
mod sleep_notifier;
mod status;
mod timesync;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should sleep_notifier perhaps be a part of the timesync module? If there's no other uses, I believe it should.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I put all the related code in timesync module.

Comment thread src/sleep_notifier.rs Outdated
Comment thread src/sleep_notifier.rs Outdated
Comment thread src/sleep_notifier.rs Outdated
Comment on lines +29 to +31
kIOMessageSystemWillSleep => tx.send(Activity::Sleep).unwrap(),
kIOMessageSystemWillPowerOn => tx.send(Activity::Wake).unwrap(),
kIOMessageSystemHasPoweredOn => tx.send(Activity::Wake).unwrap(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should likely return some type of error code on failure, rather than unwrap(). Since it seems to be called by the IO system, perhaps a log message would suffice?

@vyasgun vyasgun marked this pull request as ready for review January 22, 2026 06:22
@jakecorrenti
Copy link
Copy Markdown
Collaborator

Thanks @vyasgun, code LGTM.

I'm having some issues with my Mac Mini, so I just need to sort that out and manually test before I merge.

@vyasgun
Copy link
Copy Markdown
Contributor Author

vyasgun commented Jan 30, 2026

thanks @jakecorrenti - I updated the usage doc as well.

@vyasgun vyasgun force-pushed the pr/timesync branch 2 times, most recently from 2f59d61 to 4952fc1 Compare February 2, 2026 11:12
vyasgun and others added 2 commits February 2, 2026 16:44
Add a --timesync option which uses vsock to send host time to
qemu-guest-agent running in the guest on VM suspend/resume.

Uses IOKit's power management APIs via the objc2-io-kit crate
to detect system sleep/wake events.

This maintains feature parity between krunkit and vfkit.

Assisted by: Claude (Anthropic AI)

Signed-off-by: Gunjan Vyas <vyasgun20@gmail.com>

Co-authored-by: Tyler Fanelli <tfanelli@redhat.com>
Signed-off-by: Gunjan Vyas <vyasgun20@gmail.com>
Copy link
Copy Markdown
Collaborator

@slp slp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on MacBook Pro M3. LGTM, thanks!

@slp slp merged commit f47153b into libkrun:main Feb 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --timesync option for guest clock synchronization

4 participants