Sunday, February 19, 2017

An Example of How To Auto Update System Time in Auto Resumed Guest OS after host OS boots up in KVM

Environments
  • Host OS: Debian 8
  • Guest OS: Debian 8
Currently, when we suspend a guest OS for N minutes and resume it, the system time in the guest OS is N minutes in the past.

I configure my KVM system such that when the host OS shuts down or reboots,  all the running guest OSes are suspended.  And they are automatically resumed after the host OS boots up.  (Here is the link which shows you how to do that.  Read the section, "Automatic guest management on host shutdown/startup".)

Assuming the host OS and the guest OS are always accessible from the local network, I decide to use SSH and /etc/rc.local to automate the updating of system time of the guest OS after it is automatically resumed.

Here are the steps it takes to do it:
  1. Create a special unix user on the guest OS and, probably, name it, kvmupdatetimeman
  2. Grant sudo permission to user kvmupdatetimeman by adding the line below in the vi editor which shows up when you issue the command `sudo VISUAL=vi visudo`:

  3. Assuming a unix user on the host OS you want to use for this purpose is debian, set up SSH public key authentication for user debian on the host OS so that it can SSH to the guest OS under user kvmupdatetimeman without having to enter a password.  Make sure you SSH from the host OS to the guest OS at least once so that you already have accepted the SSH host key of the guest OS or else our automated script will have problem as we won't handle the accepting of the SSH host key from our script.
  4. On the host OS, add this line to the file /etc/rc.local which is executed every time after the machine boots up.

  5. Add the code below into the file updatesystimeinguest and don't forget to chmod +x it.  Assuming the 2 IP addresses in the code are your guest OSes' IP addresses in your local network:

Now, every time you boot up your host OS after shutting down or rebooting, your auto suspended guest OSes which are automatically resumed will have correct system time automatically.

Related links

No comments:

Post a Comment