From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 4DEE61FF0BC for ; Mon, 16 Nov 2026 13:21:49 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 2B0D2216FA; Mon, 16 Nov 2026 13:21:46 +0100 (CET) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 16 Nov 2026 13:21:41 +0100 Message-Id: To: "Fiona Ebner" , Subject: Re: [PATCH qemu-server v6 3/3] fix #5032: agent: sync guest time via QGA when the clock falls behind From: "Jakob Klocker" X-Mailer: aerc 0.20.0 References: <20261110104601.200493-1-j.klocker@proxmox.com> <20261110104601.200493-4-j.klocker@proxmox.com> In-Reply-To: X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794831701155 X-SPAM-LEVEL: Spam detection results: 0 AWL 1.245 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: AOYOM73FAPTIF4SYZMHFJW27EEWPR7JB X-Message-ID-Hash: AOYOM73FAPTIF4SYZMHFJW27EEWPR7JB X-MailFrom: j.klocker@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Thanks for the feedback! On Fri Nov 13, 2026 at 3:10 PM CET, Fiona Ebner wrote: > Needs a rebase ack > > Am 10.11.26 um 11:49 AM schrieb Jakob Klocker: >> diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm >> index 7830d09b..df2076a6 100644 >> --- a/src/PVE/API2/Qemu.pm >> +++ b/src/PVE/API2/Qemu.pm >> @@ -3599,6 +3599,24 @@ __PACKAGE__->register_method({ >> description =3D> 'Whether to migrate conntrack entries = for running VMs.', >> }, >> 'nets-host-mtu' =3D> get_standard_option('pve-qm-nets-host-= mtu'), >> + 'sync-time-on-resume' =3D> { >> + type =3D> 'boolean', >> + description =3D> >> + "Override the 'sync-time-on-resume' setting from th= e VM's guest agent (QGA)" >> + . " config for this start only. Controls whether th= e guest's clock is" > > Should explicitly state that this is for migration and hibernate and not > regular start. Agreed about mentioning hibernate here; I'll make that explicit in v7. It is not used for migration though, see below. > >> + . " synchronized after the RAM state is restored. T= he guest agent must be" >> + . " enabled.", > > Nit: For this and all others: maybe we should drop the last sentence for > the short description? It already is mentioned that it's about the guest > agent setting. But fine either way. > ack, will remove=20 >> + verbose_description =3D> >> + "Override the 'sync-time-on-resume' setting from th= e VM's guest agent (QGA)" >> + . " config for this start only. When enabled, the '= guest-set-time' QEMU guest" >> + . " agent command is issued to synchronize the gues= t's clock to the host's current" >> + . " time after the RAM state is restored, which hap= pens here when starting a VM" >> + . " that was hibernated (suspended to disk). Withou= t synchronization the guest's" >> + . " clock may still reflect the time from when the = VM was hibernated. The time is" > > It's also for migration, or? > No. The migration target is started through this endpoint, but the RAM only arrives after vm_start returns AFAICT, so at that point the guest is not running yet and the agent can't respond. That's why the sync in vm_start_nolock is skipped when 'migratedfrom' is set.=20 For migration, the clock is synced in vm_resume on the target, when=20 the source sends the 'resume' command through the migration tunnel=20 after the switchover. That always uses the value from the agent config. >> + . " only synchronized when the QEMU Guest Agent opt= ion is enabled in the guest's" >> + . " configuration and the agent is running inside o= f the guest.", >> + optional =3D> 1, >> + }, >> }, >> }, >> returns =3D> { > > ---snip 8<--- > >> diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm >> index 8176afe5..e24544a7 100644 >> --- a/src/PVE/QemuMigrate.pm >> +++ b/src/PVE/QemuMigrate.pm >> @@ -1778,6 +1778,9 @@ sub phase3_cleanup { >> $self->{errors} =3D 1; >> } >> } >> + $self->log('info', "guest clock sync requested, will run on= target after resume") >> + if !$self->{errors} >> + && PVE::QemuServer::Agent::should_sync_time_on_resume($= conf->{agent}); > > Style nit: multiline post-if ack > >> } >> =20 >> if ( >> diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm >> index 8149184e..55733ee1 100644 >> --- a/src/PVE/QemuServer.pm >> +++ b/src/PVE/QemuServer.pm >> @@ -5047,7 +5047,10 @@ sub vmconfig_update_agent { >> =20 >> die "skip\n" if !$conf->{$opt}; >> =20 >> - my $hotplug_options =3D { fstrim_cloned_disks =3D> 1 }; >> + my $hotplug_options =3D { >> + fstrim_cloned_disks =3D> 1, >> + 'sync-time-on-resume' =3D> 1, >> + }; >> =20 >> my $old_agent =3D parse_guest_agent($conf->{agent}); >> my $agent =3D parse_guest_agent($value); >> @@ -5910,6 +5913,19 @@ sub vm_start_nolock { >> ); >> } >> =20 >> + # $resume =3D suspend-to-disk, $statefile =3D rollback with RAM. Ro= llback carries the snapshot's >> + # agent setting in params; hibernate resume falls back to the live = config >> + if ($resume || ($statefile && !$migratedfrom)) { >> + my $agent =3D $params->{agent} // $conf->{agent}; >> + if (PVE::QemuServer::Agent::should_sync_time_on_resume( >> + $agent, >> + $params->{'sync-time-on-resume'}, >> + )) { >> + eval { PVE::QemuServer::Agent::guest_set_time($vmid); 1 }; > > Did you intend to pass 1 as an argument? > No, this shouldn't be `quiet`. I'm not sure how this sneaked in. Will be removed in v7. > >> + warn "could not sync guest time after resume from saved sta= te - $@" if $@; >> + } >> + } >> + >> return $res; >> } >> =20 >> diff --git a/src/PVE/QemuServer/Agent.pm b/src/PVE/QemuServer/Agent.pm >> index be6df443..5aaf76b2 100644 >> --- a/src/PVE/QemuServer/Agent.pm >> +++ b/src/PVE/QemuServer/Agent.pm >> @@ -4,6 +4,8 @@ use v5.36; >> =20 >> use JSON; >> use MIME::Base64 qw(decode_base64 encode_base64); >> +use Time::HiRes qw(time); >> +use POSIX qw(strftime); >> =20 >> use PVE::JSONSchema; >> =20 >> @@ -52,6 +54,25 @@ our $agent_fmt =3D { >> optional =3D> 1, >> default =3D> 1, >> }, >> + # FIXME: MAJOR VERSION: 10.0.0 - reconsider setting option on as de= fault >> + 'sync-time-on-resume' =3D> { >> + description =3D> >> + "Synchronize the guest's clock through QGA after operations= that can leave" >> + . " the guest's clock behind.", >> + verbose_description =3D> >> + "Whether to issue the 'guest-set-time' QEMU guest agent com= mand to synchronize the" >> + . " guest's clock to the host's current time after operatio= ns that can leave the guest's" >> + . " clock behind. This happens when resuming from hibernati= on, resuming from a paused" >> + . " state, taking a snapshot, after a migration, after a ba= ckup in suspend mode, and" >> + . " after rolling back to a snapshot that includes RAM. In = these cases the guest's" >> + . " clock may still reflect an earlier time. The time is on= ly synchronized when the" >> + . " QEMU Guest Agent option is enabled in the guest's confi= guration and the agent is" >> + . " running inside of the guest. For resume, hibernation re= sume and snapshot rollback," >> + . " the synchronization can be overridden per operation.", >> + type =3D> 'boolean', >> + optional =3D> 1, >> + default =3D> 0, >> + }, >> type =3D> { >> description =3D> "Select the agent type", >> type =3D> 'string', >> @@ -332,4 +353,47 @@ sub guest_fs_freeze_applicable($agent_str, $vmid, $= logfunc =3D undef) { >> return 1; >> } >> =20 >> +=3Dhead3 should_sync_time_on_resume >> + >> +Returns whether the guest's clock should be synchronized to the host's = via the QEMU Guest Agent >> +when the VM is resumed from saved state. Requires the guest agent to be= enabled. An explicit > > This function can deal just fine with the situation where the agent is > not enabled. Maybe state: "Returns 0 if the agent is not enabled.". > Agreed, will change in v7 >> +C<$override> (from an API parameter) takes precedence over the C agent >> +config property when defined; otherwise the config value is used (defau= lting to off). Does B >> +check whether the agent is actually running. >> + >> +=3Dcut >> + >> +sub should_sync_time_on_resume($agent_str, $override =3D undef) { >> + my $agent =3D parse_guest_agent($agent_str); >> + return 0 if !$agent->{enabled}; >> + return $override ? 1 : 0 if defined($override); >> + return $agent->{'sync-time-on-resume'} // 0; >> +} >> + >> +=3Dhead3 guest_set_time >> + >> +Sets the guest's clock to the current host time via the QEMU Guest Agen= t's >> +C command. The host time is passed explicitly (as nanos= econds >> +since the UNIX epoch) because the argument-less form reads the time fro= m the >> +guest's RTC, which isn't supported on all platforms (e.g. Windows); pas= sing >> +the value directly works regardless of guest OS. >> + >> +Returns 1 if the time was set, 0 if it was skipped because the agent is= not running. >> +Dies if the agent returns an error. > > Style nit: above used a length limit of 100 for wrapping, the first > paragraph here 80, the second paragraph seems to be something in between. > ack >> + >> +=3Dcut >> + >> +sub guest_set_time($vmid, $quiet =3D undef) { >> + if (!qga_check_running($vmid, 1)) { >> + print "skipping guest set time - agent configured but not runni= ng?\n" if !$quiet; >> + return 0; >> + } >> + my $time_ns =3D time() * 1_000_000_000; >> + my $res =3D PVE::QemuServer::Monitor::mon_cmd($vmid, 'guest-set-tim= e', time =3D> int($time_ns)); >> + check_agent_error($res, "unable to set guest time"); >> + my $time_str =3D strftime("%F %T", localtime($time_ns / 1_000_000_0= 00)); >> + print "synced guest clock via guest agent to $time_str\n" if !$quie= t; >> + return 1; >> +} >> + >> 1; >> diff --git a/src/PVE/QemuServer/RunState.pm b/src/PVE/QemuServer/RunStat= e.pm >> index 889ddd28..2588dabb 100644 >> --- a/src/PVE/QemuServer/RunState.pm >> +++ b/src/PVE/QemuServer/RunState.pm >> @@ -131,7 +131,7 @@ sub vm_suspend($vmid, $skiplock =3D undef, $includes= tate =3D undef, $statestorage =3D >> # $nocheck is set when called as part of a migration - in this context = the >> # location of the config file (source or target node) is not determinis= tic, >> # since migration cannot wait for pmxcfs to process the rename >> -sub vm_resume($vmid, $skiplock =3D undef, $nocheck =3D undef) { >> +sub vm_resume($vmid, $skiplock =3D undef, $nocheck =3D undef, $sync_tim= e =3D undef, $quiet =3D undef) { >> =20 >> PVE::QemuConfig->lock_config( >> $vmid, >> @@ -183,6 +183,19 @@ sub vm_resume($vmid, $skiplock =3D undef, $nocheck = =3D undef) { >> if $resume_cmd eq 'cont'; >> =20 >> mon_cmd($vmid, $resume_cmd); >> + >> + # A VM in 'prelaunch' never ran, and a VM that shut down du= ring a backup is reset >> + # above. Both boot fresh, so the clock is not behind and th= e agent is not up yet. >> + my $guest_was_running =3D !$reset && ($res->{status} // '')= ne 'prelaunch'; > > Nit: could also skip 'shutdown' state for completeness. > That's already covered: $reset is only set for the `shutdown` state, and $guest_was_running checks !$reset. I'll check the status directly in v7, so this is more obvious. Something along the lines: my $status =3D $res->{status} // ''; my $guest_was_running =3D $status ne 'prelaunch' && $status ne 'shutdown'; >> + if ( >> + $resume_cmd eq 'cont' >> + && $guest_was_running >> + && PVE::QemuServer::Agent::should_sync_time_on_resume($= conf->{agent}, >> + $sync_time) >> + ) { >> + eval { PVE::QemuServer::Agent::guest_set_time($vmid, $q= uiet); }; >> + warn "could not sync guest time after resume - $@" if $= @; >> + } >> }, >> ); >> }