[libvirt] [PATCH 4/5] save domain status during vm creation and remove it on shutdown.

This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later. --- src/qemu_driver.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) diff --git a/src/qemu_driver.c b/src/qemu_driver.c index 7804094..d8b87e4 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -182,6 +182,44 @@ qemudAutostartConfigs(struct qemud_driver *driver) { virConnectClose(conn); } + +/** + * qemudRemoveDomainStatus + * + * remove all state files of a domain from statedir + * + * Returns 0 on success + */ +static int +qemudRemoveDomainStatus(virConnectPtr conn, + struct qemud_driver *driver, + virDomainObjPtr vm) +{ + int rc = -1; + char *file = NULL; + + if (asprintf(&file, "%s/%s.xml", driver->stateDir, vm->def->name) < 0) { + qemudReportError(conn, vm, NULL, VIR_ERR_NO_MEMORY, + "%s", _("failed to allocate space for status file")); + goto cleanup; + } + + if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR) { + qemudReportError(conn, vm, NULL, VIR_ERR_INTERNAL_ERROR, + _("Failed to unlink status file %s"), file); + goto cleanup; + } + + if(virFileDeletePid(driver->stateDir, vm->def->name)) + goto cleanup; + + rc = 0; +cleanup: + VIR_FREE(file); + return rc; +} + + /** * qemudStartup: * @@ -532,6 +570,12 @@ static int qemudOpenMonitor(virConnectPtr conn, qemudCheckMonitorPrompt, "monitor", 10000); + if (!(vm->monitorpath = strdup(monitor))) { + qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, + "%s", _("failed to allocate space for monitor path")); + goto error; + } + /* Keep monitor open upon success */ if (ret == 0) return ret; @@ -1046,6 +1090,7 @@ static int qemudStartVMDaemon(virConnectPtr conn, return -1; } } + qemudSaveDomainStatus(conn, qemu_driver, vm); return ret; } @@ -1108,6 +1153,7 @@ static void qemudShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED, /* shut it off for sure */ kill(vm->pid, SIGKILL); + qemudRemoveDomainStatus(conn, driver, vm); vm->pid = -1; vm->def->id = -1; -- 1.6.0.2

On Fri, 2008-12-12 at 19:27 +0100, Guido Günther wrote:
diff --git a/src/qemu_driver.c b/src/qemu_driver.c index 7804094..d8b87e4 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -182,6 +182,44 @@ qemudAutostartConfigs(struct qemud_driver *driver) { virConnectClose(conn); }
+ +/** + * qemudRemoveDomainStatus + * + * remove all state files of a domain from statedir + * + * Returns 0 on success + */ +static int +qemudRemoveDomainStatus(virConnectPtr conn, + struct qemud_driver *driver, + virDomainObjPtr vm) +{ + int rc = -1; + char *file = NULL; + + if (asprintf(&file, "%s/%s.xml", driver->stateDir, vm->def->name) < 0) { + qemudReportError(conn, vm, NULL, VIR_ERR_NO_MEMORY, + "%s", _("failed to allocate space for status file")); + goto cleanup; + }
This needs to set file = NULL on error. (Praise the joy that is asprintf) David

On Fri, Dec 12, 2008 at 07:27:08PM +0100, Guido G?nther wrote:
This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later.
ACK Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

On Mon, Dec 15, 2008 at 11:22:52AM +0000, Daniel P. Berrange wrote:
On Fri, Dec 12, 2008 at 07:27:08PM +0100, Guido G?nther wrote:
This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later.
ACK The attached version uses virAsprintf and calles virSaveDomainStatus in more places - this should cover most of it. O.k. to apply? Cheers, -- Guido

On Thu, Dec 18, 2008 at 10:37:55AM +0100, Guido G?nther wrote:
On Mon, Dec 15, 2008 at 11:22:52AM +0000, Daniel P. Berrange wrote:
On Fri, Dec 12, 2008 at 07:27:08PM +0100, Guido G?nther wrote:
This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later.
ACK The attached version uses virAsprintf and calles virSaveDomainStatus in more places - this should cover most of it. O.k. to apply?
Yep, fine by me. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

On Thu, Dec 18, 2008 at 10:26:15AM +0000, Daniel P. Berrange wrote:
On Thu, Dec 18, 2008 at 10:37:55AM +0100, Guido G?nther wrote:
On Mon, Dec 15, 2008 at 11:22:52AM +0000, Daniel P. Berrange wrote:
On Fri, Dec 12, 2008 at 07:27:08PM +0100, Guido G?nther wrote:
This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later.
ACK The attached version uses virAsprintf and calles virSaveDomainStatus in more places - this should cover most of it. O.k. to apply?
Yep, fine by me. Applied now. -- Guido

Hi Guido,
This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later.
sorry for asking such "dumb" questions here on a developer-list, but would you mind to tell a bit how libvirt with qemu/kvm should work with your patches (all 5 you posted + something you plan for the near future): - libvirt is shut down gracefully, qemu-domain still running Will the qemu-domain get a (soft) shutdown and libvirt waits some time (configurable?) until the domain is down? Or will libvirt make a snapshot of the domain state and save it into some file? Or can I select between these two modes of operation with some config value? The last one would be my favorite as it is often faster and more reliable. - libvirt starts, a qemu-domain has autostart enabled, memory/state is saved to a file into a special directory will the snapshot be loaded or the domain started from scratch (with unclean filesystem)? what will happen if there is a error during loading the state? - libvirt is buggy and gets a segfault, qemu-domain (autostart set) still running, libvirt is restarted what will happen to the still-running qemu? Thank you very much. Kind regards, Gerd

On Mon, Dec 15, 2008 at 05:48:25PM +0100, Gerd v. Egidy wrote:
Hi Guido,
This patch does the actual saving and removal of the vm status. It does so only at vm creation time at the moment. We need to update the config every time the life config changes later.
sorry for asking such "dumb" questions here on a developer-list, but would you mind to tell a bit how libvirt with qemu/kvm should work with your patches (all 5 you posted + something you plan for the near future): First on the list is to make qemu/kvm survive daemon restarts (e.g. updates) and crashes.
- libvirt is shut down gracefully, qemu-domain still running Will the qemu-domain get a (soft) shutdown and libvirt waits some time (configurable?) until the domain is down? Currently we simply kill of qemu/kvm vms on daemon shutdown - this is not the best solution. At least being able to signal shutdown first and kill off later should be an option - maybe suspend/resume later.
Or will libvirt make a snapshot of the domain state and save it into some file? Or can I select between these two modes of operation with some config value? The last one would be my favorite as it is often faster and more reliable. This should then be configurable per vm in the vms XML. "Huge" vms with lots of memory might be quicker to shut down instead of dumping GBs onto the hard disk.
- libvirt starts, a qemu-domain has autostart enabled, memory/state is saved to a file into a special directory will the snapshot be loaded or the domain started from scratch (with unclean filesystem)? I think we'll handle that once we're there. Data consistency should be first priority.
what will happen if there is a error during loading the state?
- libvirt is buggy and gets a segfault, qemu-domain (autostart set) still running, libvirt is restarted what will happen to the still-running qemu? The current code does qemudReconnectVMs before qemudAutostartVMs so if the domain is still running nothing bad will happen cause it gets "reconnected" to libvirtd first and autostart doesn't do anything since it's already there and running. Hope this helps, -- Guido
participants (4)
-
Daniel P. Berrange
-
David Lutterkort
-
Gerd v. Egidy
-
Guido Günther