History
I found that virDomainDetachDevice did not work with inactive domains
and submitted a patch [1] to fix this. As it turns out, some drivers do
not support attaching/detaching devices on inactive domains and it was
preferred that virDomain{Attach,Detach}Device only be permitted on
active domains.
I posted a follow-up patch [2] that restricted
virDomain{Attach,Detach}Device to active domains only, although as
Daniel Veillard pointed out this restriction should have been enforced
in the libvirt front-end. Cole Robinson suggested improving virsh [3]
to handle the case of inactive vs. active by redefining the domain with
new device (or removed device) when domain is inactive. While
implementing this improvement I asked some questions on IRC that
resulted in another approach suggested by Dan Berrange as outlined below.
Introduce two new APIs
virDomainAttachDeviceFlags(virDomainPtr dom, const char *xml, unsigned
flags)
virDomainDetachDeviceFlags(virDomainPtr dom, const char *xml, unsigned
flags)
with flags being one or more from VIR_DOMAIN_DEVICE_ATTACH_CURRENT,
VIR_DOMAIN_DEVICE_ATTACH_LIVE, VIR_DOMAIN_DEVICE_ATTACH_CONFIG.
(I'm not sure about the ATTACH in these names. Perhaps CHANGE or MODIFY
would be more appropriate.)
If caller specifies CURRENT (default), add or remove the device
depending on the current state of domain. E.g. if domain is active add
or remove the device to/from live domain, if it is inactive change the
persistent config. If caller specifies LIVE, only change the active
domain. If caller specifies CONFIG, only change persistent config -
even if the domain is active. If caller specifies both LIVE and CONFIG,
then change both.
If a driver could not satisfy the exact requested flags it must return
an error. E.g if user specified LIVE but the driver can only change
live and persisted config, the driver must fail the request.
The existing virDomain{Attach,Detach}Device would be declared to be
equivalent to virDomain{Attach,Detach}DeviceFlags(LIVE).
Finally, virsh {attach,detach}-{disk,interface,device} would be modified
to add a --persistent flag in order to set the appropriate flags when
calling the new APIs.
Regards,
Jim
[1]
https://www.redhat.com/archives/libvir-list/2009-November/msg00489.html
[2]
https://www.redhat.com/archives/libvir-list/2009-November/msg00571.html
[3]
https://www.redhat.com/archives/libvir-list/2009-November/msg00676.html