utmapp / UTM

Virtual machines for iOS and macOS
https://getutm.app
Apache License 2.0
26.35k stars 1.32k forks source link

macOS guest time becomes out of sync #4644

Open ghost opened 1 year ago

ghost commented 1 year ago

guest time can desync from the host os. this issue is valid for a few versions back.

conath commented 1 year ago

Could you please add more information about the issue? Especially need the guest configuration file:

right-click the VM in UTM and choose "Show in Finder". Then right-click the selected file in Finder and choose "Show Package Contents". The config.plist file is now visible

How long after you turn on the VM does it take until the issue occurs? Are all of your macOS VMs affected or only a specific configuration? (please provide!)

ghost commented 1 year ago

config.plist.zip

I only have this VM. This happened to me a few times, and it seems to be fixing itself after a while. Definitely not an easy 1/1 repro, but it does happen and it was happening before 4.1.0.

conath commented 1 year ago

Thanks. Config looks good to me. You could potentially test this further by turning off "Set date and time automatically" in the guest's System Settings. That would in theory prevent the guest time from fixing itself by using the internet time, and force it to rely on the time information it receives from the Apple virtualization backend.

ghost commented 1 year ago

Done. Monitoring the issue.

ghost commented 1 year ago

@conath since I wrote the above comment the time slipped 3 minutes today. So with the time sync set to off in the guest, it is continuously slipping.

jtran19 commented 1 year ago

Having the same issue here with macOS 13 host and guest. I've noticed the VM time slips when the host is suspended due to no activity (i.e. "falls asleep"). An easy way to force the issue is pausing the VM for a few minutes. When you resume, the time will be out of sync.

osy commented 1 year ago

Seems like something we'll have to report to Apple: FB11815959

krypu commented 1 year ago

Using latest release 4.1.5, it's also happening while actively using a MacOS guest VM.

tenedor commented 10 months ago

I regularly hit a similar issue, maybe the same issue. I'm a first-time UTM user as of a couple weeks ago on my new M2 laptop, using UTM 4.4.4 and MacOS Ventura 13.5.2. My guest VM's time constantly falls days behind when not in use despite the Date & Time settings being configured to set the time automatically from the Apple time server.

I can reset the clock to the exact time by opening Date & Time settings and toggling "Set time and date automatically" off and then back on. The time is correct for as long as I am actively using the VM, and then it falls behind again.

antonkratz commented 8 months ago

I observe the same issue, has been ongoing for about a year for me, currently with this config.

Guest system is Ubuntu 23.04 (lunar).

kenji21 commented 3 months ago

Fixed for Qemu, no APIs from Apple for Virtualization.framework

kenji21 commented 2 months ago

Found AppleVirtualPlatformRTCPlugin in Console.app logs when switching on and off the syncing to time.apple.com in Date & Time settings :

Screenshot 36

kenji21 commented 2 months ago

I checked the binary /System//Library/PrivateFrameworks/CoreTime.framework/TimeSources/AppleVirtualPlatformRTCPlugin.bundle/Contents/MacOS/AppleVirtualPlatformRTCPlugin which only uses IOKit to get notified about the change

 r0 = IONotificationPortCreate(*(int32_t *)*_kIOMainPortDefault);
[...]
    r0 = IOServiceNameMatching("AppleVirtualPlatformRTC");
    r0 = IOServiceGetMatchingService(r20, r0);
[...]
r0 = IOServiceAddInterestNotification(r0, r1, "IOGeneralInterest", 0x34bc, r19, r19 + 0x18);
[...]

with the 0x34bc method:

int sub_34bc(int arg0, int arg1, int arg2, int arg3) {
[...]
    r0 = os_log_type_enabled(r20, 0x0);
    if (arg2 == 0xe0000230) {
            if (r0 != 0x0) {
                    _os_log_impl(0x0, r20, 0x0, "Avp rtc state notification received", &var_30, 0x2);
            }
            if (r8 != 0x0) {
                    if (r8 == 0x1) {
                            r20 = *(r19 + 0x10);
                            if (os_log_type_enabled(r20, 0x0) != 0x0) {
                                    _os_log_impl(0x0, r20, 0x0, "Reset time", &var_40, 0x2);
                            }
                            sub_38c0();
                    }
            }
            else {
                    r20 = *(r19 + 0x10);
                    if (os_log_type_enabled(r20, 0x0) != 0x0) {
                            _os_log_impl(0x0, r20, 0x0, "Resync Time", &var_50, 0x2);
                    }
                    sub_38e0();
            }
    }
    else {
            if (r0 != 0x0) {
                    _os_log_impl(0x0, r20, 0x0, "Avp rtc unknown notification received", &var_60, 0x2);
            }
    }
    r0 = [r19 release];
    return r0;
}
kenji21 commented 2 months ago

No clue about how the host macOS send such an IOKit notification to the guest macOS

antonkratz commented 2 months ago

I do not know if my comment helps or not, but I noticed that the time drift certainly appears with Ubuntu guests (many versions tested) but does not seem to appear if the guest is openSUSE LEAP 15.5

kenji21 commented 1 month ago

maybe you are running the openSUSE guest with QEMU (which time sync is supported)

kenji21 commented 1 month ago

Finally found a way to solve this, a macOS LaunchAgent with the swift code like the fix for QEMU, but to make the VM aware of the drift => a Wake On LAN magic packet (reading the UTM config.plist) A launchd service on the VM listens to the WOL packet and run systemctl restart networking & systemctl restart ntp My VM has network in bridge mode, so I lose network connectivity when closing my laptop, moving home, opening it. Hence the WakeOnLAN packet ^^

kenji21 commented 1 month ago

Microsoft solves this issue on their Hypervisor with a RTC device /dev/ptp_hyperv, and configuring chrony to use it, see https://learn.microsoft.com/en-us/azure/virtual-machines/linux/time-sync#chrony