Synchronization of clock time in the host and guest system under KVM

I have a relatively simple requirement: I want the clocks in the CentOS guests I create under KVM to sync with their CentOS host from the very first boot of the VMs.

It is easy enough to sync them with NTP once they are up and running. However, if the host clock and VM clock are very different when starting NTP, it can cause a large jump in VM time. Many of our applications running under virtual machines fail with this success, so we want to prevent this from happening.

So my question is, how can I configure my VMs to start at the same time as their host? In the test I just ran, my stay time was 14:00 PDT. The VM I created under this host arrived at regular time 21:00 PDT. This was adjusted by NTP to 14:00 PDT shortly after it started before 14:00 PDT coinciding with the host time, and subsequent VM reboots always had the correct time. The problem only occurs on the first boot. I want the VM to come up with 14:00 PDT one very first boot to avoid the NTP time jump.


source to share

1 answer

Ok, I answered my own question. The combination of the settings I used to give me the results I want:

  • Install hwclock on host and use UTC time. This is done with the -utc option of the hwclock command. I am running the following command on my host OS:

    hwclock --utc --set --date = "time string"

  • Tell CentOS that hwclock is using UTC via the / etc / adjtime. For example, you can initialize this file using

    echo -e "0.0 0 0.0 \ n0 \ n \ nUTC"> / etc / adjtime

Create this file on both host and guest VMs. I create a file for my guests before uploading them the first time by accessing the guest filesystem directly from the host.

  1. Set the time zone for the system time. Do this again on both your host and your guests:

    ln -sf / usr / share / zoneinfo / time-zone / etc / localtime
    echo "ZONE = time zone"> / etc / sysconfig / clock
    export TZ = time zone

where timezone is a standard CentOS timezone string such as "US / Pacific".

  1. Set the system time on your host based on hwclock. The --utc parameter is required to tell CentOS that hwclock is in UTC. This will take UTC time and adjust your system time based on the TZ environment variable:

    hwclock --utc --hctosys

  2. The above steps are done once when you set up the host and guests. To keep the time synchronized across all of your servers after they start, you want to configure NTP on the host and guests.



All Articles