[libvirt-users] Better error codes when stopping a VM that is already stopped

Hi, Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle. Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me... Two questions about this: Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()? Can't we have a more obvious/precise error code in that case, like VIR_ERR_DOMAIN_NOT_RUNNING or something like that? Guido Winkelmann

On 05/21/2010 08:11 AM, Guido Winkelmann wrote:
Hi,
Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle.
In virt-manager, we basically do: def destroy(vm): if vm.is_running(): vm.destroy() return I'd recommend doing something similar in your app: no reason to run a command if you can check ahead of time if it will fail. You can use virDomainIsActive to check the domain status.
Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me...
Two questions about this:
Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()?
Yes, there are other errors but none that use OPERATION_INVALID.
Can't we have a more obvious/precise error code in that case, like VIR_ERR_DOMAIN_NOT_RUNNING or something like that?
Sounds like a reasonable idea, won't help you though if your app needs to support older versions. - Cole

On Fri, May 21, 2010 at 11:50:10AM -0400, Cole Robinson wrote:
On 05/21/2010 08:11 AM, Guido Winkelmann wrote:
Hi,
Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle.
In virt-manager, we basically do:
def destroy(vm): if vm.is_running(): vm.destroy() return
I'd recommend doing something similar in your app: no reason to run a command if you can check ahead of time if it will fail. You can use virDomainIsActive to check the domain status.
Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me...
Two questions about this:
Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()?
Yes, there are other errors but none that use OPERATION_INVALID.
Can't we have a more obvious/precise error code in that case, like VIR_ERR_DOMAIN_NOT_RUNNING or something like that?
Sounds like a reasonable idea, won't help you though if your app needs to support older versions.
VIR_ERR_INVALID_OPERATION is already precisely defined to mean that the operation request is not valid for the current state of the VM. VIR_ERR_DOMAIN_NOT_RUNNING would just be replicating those semantics, but needlessly restricting itself to the case of not running, so you'd end up having to add ALREADY_RUNNING, ALREADY_PAUSED, etc which is just pointless Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

Am Freitag, 21. Mai 2010 schrieb Cole Robinson:
On 05/21/2010 08:11 AM, Guido Winkelmann wrote:
Hi,
Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle.
In virt-manager, we basically do:
def destroy(vm): if vm.is_running(): vm.destroy() return
I'd recommend doing something similar in your app: no reason to run a command if you can check ahead of time if it will fail. You can use virDomainIsActive to check the domain status.
That's not a real solution. It might reduce the frequency with which the problem occurs, but it's still subject to a race condition.
Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me...
Two questions about this:
Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()?
Yes, there are other errors but none that use OPERATION_INVALID.
After switching from libvirt 0.8.1 to libvirt from git, I'm getting VIR_ERR_RPC when trying to stop a domain that is not running. :( Could this be caused by a mismatch of the libvirt versions between the machine my code is running on (0.8.1) and the version of libvirt on the qemu host (git)? Guido

On Fri, May 21, 2010 at 02:11:47PM +0200, Guido Winkelmann wrote:
Hi,
Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle.
Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me...
Two questions about this:
Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()?
The VIR_ERR_OPERATION_INVALID is a precise code that says the request operation is not valid wrt the current state of the guest. So for the virDomainDestroy() method, the only cause of this message is requesting destroy of a guest that is not running. Similarly you'll get it if you try to start a guest that is already running.
Can't we have a more obvious/precise error code in that case, like VIR_ERR_DOMAIN_NOT_RUNNING or something like that?
The VIR_ERR_OPERATION_INVALID is already very precisely defined as you need. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

On 5/24/2010 6:24 AM, Daniel P. Berrange wrote:
On Fri, May 21, 2010 at 02:11:47PM +0200, Guido Winkelmann wrote:
Hi,
Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle.
Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me...
Two questions about this:
Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()?
The VIR_ERR_OPERATION_INVALID is a precise code that says the request operation is not valid wrt the current state of the guest. So for the virDomainDestroy() method, the only cause of this message is requesting destroy of a guest that is not running.
You are saying there is, and can only ever be, one possible way that the operation could ever be considered invalid? Even within the limited scope of virDomainDestroy() and the particular values it was called with, I find that hard to believe on general principle. I can see that, say, it may not make any sense to try to tell the difference between "never was created" and "created but now destroyed". Such distinctions probably belong in front end distro init scripts and other admin utils. IE: your script or util tracks whether or not it has tried to start a domain and whether or not the fact that it's not currently running is expected or an error. -- bkw

On Mon, May 24, 2010 at 09:07:24AM -0400, Brian K. White wrote:
On 5/24/2010 6:24 AM, Daniel P. Berrange wrote:
On Fri, May 21, 2010 at 02:11:47PM +0200, Guido Winkelmann wrote:
Hi,
Sometimes when stopping a virtual domain using virDomainDestroy(), I come across a domain that is already stopped. (For example when someone already stopped the domain manually using virsh or because the guest OS issued a shutdown.) This is a special case that I absolutely need to catch and handle.
Unfortunately, when this happens, and I call virGetLastError() afterwards, I always just get the error code VIR_ERR_OPERATION_INVALID, which doesn't look very precise to me...
Two questions about this:
Is the domain not running at that moment the only possible condition that can trigger this particular error code when calling virDomainDestroy()?
The VIR_ERR_OPERATION_INVALID is a precise code that says the request operation is not valid wrt the current state of the guest. So for the virDomainDestroy() method, the only cause of this message is requesting destroy of a guest that is not running.
You are saying there is, and can only ever be, one possible way that the operation could ever be considered invalid?
You are debating the semantics of the word 'invalid'. The error code VIR_ERR_OPERATION_INVALID is associated with just one specific sceanario, regardless of how many different semantics you can attach to the word.
Even within the limited scope of virDomainDestroy() and the particular values it was called with, I find that hard to believe on general principle.
A guest VM has a set of lifecycle states (shutoff, running, paused). An operation on a VM can optional have a pre-requisite lifecycle state that the VM must be in. If that pre-requisite lifecycle state is not satisfied, the error code VIR_ERR_OPERATION_INVALID is raised. This error code is not used for any other reason. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
participants (4)
-
Brian K. White
-
Cole Robinson
-
Daniel P. Berrange
-
Guido Winkelmann