@@ -13,6 +13,7 @@ import (
1313 "github.com/lima-vm/lima/pkg/guestagent/api"
1414 "github.com/lima-vm/lima/pkg/guestagent/iptables"
1515 "github.com/lima-vm/lima/pkg/guestagent/procnettcp"
16+ "github.com/lima-vm/lima/pkg/guestagent/timesync"
1617 "github.com/sirupsen/logrus"
1718 "github.com/yalue/native_endian"
1819)
@@ -37,6 +38,7 @@ func New(newTicker func() (<-chan time.Time, func()), iptablesIdle time.Duration
3738 }
3839
3940 go a .setWorthCheckingIPTablesRoutine (auditClient , iptablesIdle )
41+ go a .fixSystemTimeSkew ()
4042 return a , nil
4143}
4244
@@ -244,3 +246,31 @@ func (a *agent) Info(ctx context.Context) (*api.Info, error) {
244246 }
245247 return & info , nil
246248}
249+
250+ const deltaLimit = 2 * time .Second
251+
252+ func (a * agent ) fixSystemTimeSkew () {
253+ for {
254+ ticker := time .NewTicker (10 * time .Second )
255+ for now := range ticker .C {
256+ rtc , err := timesync .GetRTCTime ()
257+ if err != nil {
258+ logrus .Warnf ("fixSystemTimeSkew: lookup error: %s" , err .Error ())
259+ continue
260+ }
261+ d := rtc .Sub (now )
262+ logrus .Debugf ("fixSystemTimeSkew: rtc=%s systime=%s delta=%s" ,
263+ rtc .Format (time .RFC3339 ), now .Format (time .RFC3339 ), d )
264+ if d > deltaLimit || d < - deltaLimit {
265+ err = timesync .SetSystemTime (rtc )
266+ if err != nil {
267+ logrus .Warnf ("fixSystemTimeSkew: set system clock error: %s" , err .Error ())
268+ continue
269+ }
270+ logrus .Infof ("fixSystemTimeSkew: system time synchronized with rtc" )
271+ break
272+ }
273+ }
274+ ticker .Stop ()
275+ }
276+ }
0 commit comments