Skip to content

Commit

Permalink
libvirt: allows attach and detach from all domains
Browse files Browse the repository at this point in the history
Previously attaching/detaching from a shutoff domain would fail.

Fixes bug 1057730

Change-Id: I876872700da125cb078746401b1b80da265880ff
  • Loading branch information
vishvananda committed Sep 27, 2012
1 parent 0d565de commit 81fa6f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
9 changes: 9 additions & 0 deletions nova/tests/fakelibvirt.py
Expand Up @@ -69,8 +69,11 @@ def _reset():
VIR_DOMAIN_CRASHED = 6

VIR_DOMAIN_XML_SECURE = 1

VIR_DOMAIN_UNDEFINE_MANAGED_SAVE = 1

VIR_DOMAIN_AFFECT_CURRENT = 0

VIR_CPU_COMPARE_ERROR = -1
VIR_CPU_COMPARE_INCOMPATIBLE = 0
VIR_CPU_COMPARE_IDENTICAL = 1
Expand Down Expand Up @@ -334,11 +337,17 @@ def attachDevice(self, xml):
self._def['devices']['disks'] += [disk_info]
return True

def attachDeviceFlags(self, xml, _flags):
self.attachDevice(xml)

def detachDevice(self, xml):
disk_info = _parse_disk_info(etree.fromstring(xml))
disk_info['_attached'] = True
return disk_info in self._def['devices']['disks']

def detachDeviceFlags(self, xml, _flags):
self.detachDevice(xml)

def XMLDesc(self, flags):
disks = ''
for disk in self._def['devices']['disks']:
Expand Down
32 changes: 16 additions & 16 deletions nova/virt/libvirt/driver.py
Expand Up @@ -636,9 +636,16 @@ def attach_volume(self, connection_info, instance_name, mountpoint):

if FLAGS.libvirt_type == 'lxc':
self._attach_lxc_volume(conf.to_xml(), virt_dom, instance_name)
# TODO(danms) once libvirt has support for LXC hotplug,
# replace this re-define with use of the
# VIR_DOMAIN_AFFECT_LIVE & VIR_DOMAIN_AFFECT_CONFIG flags with
# attachDevice()
domxml = virt_dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE)
self._conn.defineXML(domxml)
else:
try:
virt_dom.attachDevice(conf.to_xml())
flags = (libvirt.VIR_DOMAIN_AFFECT_CURRENT)
virt_dom.attachDeviceFlags(conf.to_xml(), flags)
except Exception, ex:
if isinstance(ex, libvirt.libvirtError):
errcode = ex.get_error_code()
Expand All @@ -653,13 +660,6 @@ def attach_volume(self, connection_info, instance_name, mountpoint):
connection_info,
mount_device)

# TODO(danms) once libvirt has support for LXC hotplug,
# replace this re-define with use of the
# VIR_DOMAIN_AFFECT_LIVE & VIR_DOMAIN_AFFECT_CONFIG flags with
# attachDevice()
domxml = virt_dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE)
self._conn.defineXML(domxml)

@staticmethod
def _get_disk_xml(xml, device):
"""Returns the xml for the disk mounted at device"""
Expand Down Expand Up @@ -698,20 +698,20 @@ def detach_volume(self, connection_info, instance_name, mountpoint):
raise exception.DiskNotFound(location=mount_device)
if FLAGS.libvirt_type == 'lxc':
self._detach_lxc_volume(xml, virt_dom, instance_name)
# TODO(danms) once libvirt has support for LXC hotplug,
# replace this re-define with use of the
# VIR_DOMAIN_AFFECT_LIVE & VIR_DOMAIN_AFFECT_CONFIG flags with
# detachDevice()
domxml = virt_dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE)
self._conn.defineXML(domxml)
else:
virt_dom.detachDevice(xml)
flags = (libvirt.VIR_DOMAIN_AFFECT_CURRENT)
virt_dom.detachDeviceFlags(xml, flags)
finally:
self.volume_driver_method('disconnect_volume',
connection_info,
mount_device)

# TODO(danms) once libvirt has support for LXC hotplug,
# replace this re-define with use of the
# VIR_DOMAIN_AFFECT_LIVE & VIR_DOMAIN_AFFECT_CONFIG flags with
# detachDevice()
domxml = virt_dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE)
self._conn.defineXML(domxml)

@exception.wrap_exception()
def _attach_lxc_volume(self, xml, virt_dom, instance_name):
LOG.info(_('attaching LXC block device'))
Expand Down

0 comments on commit 81fa6f7

Please sign in to comment.