LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud
User Name
Password
Linux - Virtualization and Cloud This forum is for the discussion of all topics relating to Linux Virtualization and Linux Cloud platforms. Xen, KVM, OpenVZ, VirtualBox, VMware, Linux-VServer and all other Linux Virtualization platforms are welcome. OpenStack, CloudStack, ownCloud, Cloud Foundry, Eucalyptus, Nimbus, OpenNebula and all other Linux Cloud platforms are welcome. Note that questions relating solely to non-Linux OS's should be asked in the General forum.

Notices


Reply
  Search this Thread
Old 02-12-2018, 03:04 AM   #1
atelszewski
Member
 
Registered: Aug 2007
Distribution: Slackware
Posts: 885

Rep: Reputation: Disabled
libvirt/QEMU: How to update guest time when restoring saved machine (from managedsave)


Hi,

I was pretty sure I've already asked this question here.
But it turns out I've been wrong ;-)

So, the question is:
After restoring guest from managedsave, the guest time is set to the time when the guest state was last saved.
How can I update it, i.e. how can I sync it to the current time?

Because I found the solution, I'd thought I'd share it here.
The reasons are that I though I asked the question here so I feel obliged to share the solution ;-)
And, I was searching the web really hard to find the solution, but I haven't found any that I liked.
(I.e. I didn't want to use NTP for that).


Read on to next post if you're interested.

--
Best regards,
Andrzej Telszewski
 
Old 02-12-2018, 03:11 AM   #2
atelszewski
Member
 
Registered: Aug 2007
Distribution: Slackware
Posts: 885

Original Poster
Rep: Reputation: Disabled
Hi,

I came up with the following solution.
It requires QEMU Guest Agent to be configured and running within the machine.

libvirt has the possibility to run hooks scripts for particular hypervisor and machine state.
You can read more about it in Hooks for specific system management.
This possibility immediately drew my attention.

I wrote the following hook for QEMU hypervisor:
Code:
$ cat /etc/libvirt/hooks/qemu
#!/bin/bash

PATH=/usr/sbin:/sbin:/usr/bin:/bin

################################################################################
function guest_update_time() {
################################################################################

  for ((cnt = 0; cnt < 10; ++cnt)); do
    virsh qemu-agent-command "${1}" '{"execute" : "guest-ping"}' && break
    sleep 0.3
  done

  timeout --kill-after=1 1 virsh domtime "${1}" --now || true
}

# Executes after the QEMU process has successfully started up.
# <https://www.libvirt.org/hooks.html#qemu>
if [[ (${1} != "-") && (${2} = "started") && (${3} = "begin") && (${4} = "-") ]]; then
  guest_update_time "${1}" </dev/null >/dev/null 2>/dev/null &
fi
It works in the following way:

It tests to see if the machine state is "started" (${2} = "started").
If so, then it executes guest_update_time() function in the background.
(It is important to execute the function in the background, otherwise the script blocks and the machine startup does not proceed).
For a maximum of 3 seconds, guest_update_time() tries to check if Guest Agent is running.
As soon as it determines the Guest Agent is running, the machine time is updated with virsh domtime

Things to keep in mind:
1. The hook is executed on behalf of the user executing the QEMU process.
I'm not 100% sure about that, but this is what I observed when running virtual
machine in qemu:///session connection.

2. The hook is run for all started machines, and for all users.
Better solution would be to start it only for the restored machines, but I didn't want to mess with preserving the state of the machine.
If needed, it could be implemented.

--
Best regards,
Andrzej Telszewski
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
libvirt/QEMU: changing "save" directory for qemu:///session connection atelszewski Linux - Virtualization and Cloud 0 01-10-2018 02:44 PM
[SOLVED] libvirt: how to lock qemu image disk in qemu:///session ? atelszewski Linux - Virtualization and Cloud 1 12-18-2017 11:23 AM
libvirt, virt-manager: change default shutdown mode to use QEMU Guest Agent atelszewski Linux - Virtualization and Cloud 0 11-21-2016 12:46 AM
libvirt qemu cgroup problem faicker Linux - Virtualization and Cloud 0 07-19-2012 07:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration