Fixing up kvm detection to actually use libvirt (which seems to work better across distros).

This commit is contained in:
Joshua Harlow 2012-02-08 12:55:23 -08:00
parent f1eaff50b8
commit 84c5e3da59
2 changed files with 73 additions and 18 deletions
devstack

@ -176,13 +176,6 @@ QUANTUM_OPENSWITCH_OPS = {
'quantum_use_dhcp': None,
}
#ensure libvirt restarted
#TODO: maybe this should be a subclass that handles these differences
LIBVIRT_RESTART_CMD = {
settings.RHEL6: ['service', 'libvirtd', 'restart'],
settings.UBUNTU11: ['/etc/init.d/libvirt-bin', 'restart'],
}
#this is a special conf
CLEANER_DATA_CONF = 'clean_iptables.sh'
CLEANER_CMD_ROOT = [sh.joinpths("/", "bin", 'bash')]
@ -206,10 +199,10 @@ class NovaUninstaller(comp.PythonUninstallComponent):
sh.execute(*cmd, run_as_root=True)
def _clear_libvirt_domains(self):
libvirt_type = self.cfg.get('nova', 'libvirt_type')
inst_prefix = self.cfg.get('nova', 'instance_name_prefix')
virt_driver = self.cfg.get('nova', 'virt_driver')
if virt_driver == virsh.VIRT_TYPE:
libvirt_type = virsh.default(self.cfg.get('nova', 'libvirt_type'))
virsh.clear_libvirt_domains(libvirt_type, inst_prefix)
@ -439,10 +432,14 @@ class NovaRuntime(comp.PythonRuntime):
return result
def pre_start(self):
#ensure libvirt started
cmd = LIBVIRT_RESTART_CMD.get(self.distro)
if cmd:
sh.execute(*cmd, run_as_root=True)
virt_driver = self.cfg.get('nova', 'virt_driver')
if virt_driver == virsh.VIRT_TYPE:
virt_type = virsh.default(self.cfg.get('nova', 'libvirt_type'))
if not virsh.virt_ok(virt_type, self.distro):
msg = ("Libvirt type %s for distro %s does not seem to be active or configured correctly, "
"perhaps you should be using %s instead." % (virt_type, self.distro, virsh.DEFAULT_VIRT))
raise exceptions.StartException(msg)
virsh.restart(self.distro)
def _get_param_map(self, app_name):
params = comp.PythonRuntime._get_param_map(self, app_name)
@ -513,8 +510,9 @@ class NovaConfigurator(object):
nova_conf.add('sql_connection', self.cfg.get_dbdsn('nova'))
#configure anything libvirt releated?
libvirt_type = self._getstr('libvirt_type')
if libvirt_type:
virt_driver = self._getstr('virt_driver')
if virt_driver == virsh.VIRT_TYPE:
libvirt_type = virsh.default(self._getstr('libvirt_type'))
self._configure_libvirt(libvirt_type, nova_conf)
#how instances will be presented
@ -677,11 +675,8 @@ class NovaConfigurator(object):
os.chmod(instances_path, stat.S_IRWXO | stat.S_IRWXG | stat.S_IRWXU)
def _configure_libvirt(self, virt_type, nova_conf):
if virt_type == 'kvm' and not sh.exists("/dev/kvm"):
LOG.warn("No kvm found at /dev/kvm, switching to qemu mode.")
virt_type = "qemu"
if virt_type == 'lxc':
#TODO
#TODO need to add some goodies here
pass
nova_conf.add('libvirt_type', virt_type)

@ -14,7 +14,9 @@
# License for the specific language governing permissions and limitations
# under the License.
from devstack import exceptions as excp
from devstack import log as logging
from devstack import settings
from devstack import shell as sh
from devstack import utils
@ -28,6 +30,27 @@ LIBVIRT_PROTOCOL_MAP = {
}
VIRT_TYPE = 'libvirt'
VIRT_LIB = VIRT_TYPE
DEFAULT_VIRT = 'qemu'
#how libvirt is restarted
LIBVIRT_RESTART_CMD = {
settings.RHEL6: ['service', 'libvirtd', 'restart'],
settings.FEDORA16: ['service', 'libvirtd', 'restart'],
#whyyyy??
settings.UBUNTU11: ['service', 'libvirt-bin', 'restart'],
}
#how we check its status
LIBVIRT_STATUS_CMD = {
settings.RHEL6: ['service', 'libvirtd', 'status'],
settings.FEDORA16: ['service', 'libvirtd', 'status'],
#whyyyy??
settings.UBUNTU11: ['service', 'libvirt-bin', 'status'],
}
#status is either dead or alive!
_DEAD = 'DEAD'
_ALIVE = 'ALIVE'
def _get_virt_lib():
@ -36,6 +59,15 @@ def _get_virt_lib():
return utils.import_module(VIRT_LIB)
def _status(distro):
cmd = LIBVIRT_STATUS_CMD[distro]
(sysout, _) = sh.execute(*cmd, run_as_root=False, check_exit_code=False)
if sysout.find("running") != -1:
return _ALIVE
else:
return _DEAD
def _destroy_domain(conn, dom_name):
libvirt = _get_virt_lib()
if not libvirt or not dom_name:
@ -49,6 +81,34 @@ def _destroy_domain(conn, dom_name):
LOG.warn("Could not clear out libvirt domain (%s) due to [%s]" % (dom_name, e.message))
def restart(distro):
if _status(distro) != _ALIVE:
cmd = LIBVIRT_RESTART_CMD[distro]
sh.execute(*cmd, run_as_root=True)
def default(virt_type):
if not virt_type or not LIBVIRT_PROTOCOL_MAP.get(virt_type):
return DEFAULT_VIRT
else:
return virt_type
def virt_ok(virt_type, distro):
virt_protocol = LIBVIRT_PROTOCOL_MAP.get(virt_type)
if not virt_protocol:
return False
#ensure we can do this
restart(distro)
#this is our sanity check to ensure that we can actually use that virt technology
cmd = ['virsh', '-c', virt_protocol, 'uri']
try:
sh.execute(*cmd, run_as_root=True)
return True
except excp.ProcessExecutionError:
return False
def clear_libvirt_domains(virt_type, inst_prefix):
libvirt = _get_virt_lib()
virt_protocol = LIBVIRT_PROTOCOL_MAP.get(virt_type)