[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[virt-tools-list] [PATCH 11/11] KvmHypervisor can now manage domain lifecycles.



It can define and undefine, as well as create and destroy, domains.
---
 src/virtlib/hypervisors/kvm.py |  181 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 181 insertions(+), 0 deletions(-)

diff --git a/src/virtlib/hypervisors/kvm.py b/src/virtlib/hypervisors/kvm.py
index a76aa3c..9a4fa04 100644
--- a/src/virtlib/hypervisors/kvm.py
+++ b/src/virtlib/hypervisors/kvm.py
@@ -17,8 +17,10 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 
 import libvirt
+import virtinst
 
 from base import Hypervisor
+from virtlib.config import DomainConfig
 
 DEFAULT_POOL_TARGET_PATH='/var/lib/libvirt/images'
 
@@ -29,6 +31,7 @@ class KvmHypervisor(Hypervisor):
 
     url = 'qemu:///system'
     __conn = None
+    __virt_types = None
 
     def set_url(self, url):
         '''
@@ -44,6 +47,8 @@ class KvmHypervisor(Hypervisor):
 
     def do_connect(self):
         self.__conn = libvirt.open(self.url)
+        self.__capabilities = virtinst.CapabilitiesParser.parse(self.__conn.getCapabilities())
+        self.__virt_types = None
 
     def do_disconnect(self):
         if self.__conn is not None:
@@ -178,3 +183,179 @@ class KvmHypervisor(Hypervisor):
         if _pool.isActive():
             _pool.destroy()
 
+    def do_define_domain(self, config):
+        location = extra = kickstart = None
+
+        if config.install_type == DomainConfig.LOCAL_INSTALL:
+            if config.use_cdrom_source:
+                iclass = virtinst.DistroInstaller
+                location = config.install_media
+            else:
+                iclass = virtinst.LiveCDInstaller
+                location = config.iso_path
+        elif config.install_type == DomainConfig.NETWORK_INSTALL:
+            iclass = virtinst.DistroInstaller
+            location = config.install_url
+            extra = config.kernel_options
+            kickstart = config.kickstart_url
+        elif config.install_type == DomainConfig.PXE_INSTALL:
+            iclass = virtinst.PXEInstaller
+
+        installer = iclass(conn = self.__conn,
+                           type = self._get_hypervisor(config.virt_type),
+                           os_type = self._get_os_type(config.virt_type))
+        self.__guest = installer.guest_from_installer()
+        self.__guest.name = config.guest_name
+        self.__guest.vcpus = config.cpus
+        self.__guest.memory = config.memory
+        self.__guest.maxmemory = config.memory
+
+        self.__guest.installer.location = location
+        if config.use_cdrom_source:
+            self.__guest.installer.cdrom = True
+
+        extraargs = ""
+        if extra:
+            extraargs += extra
+        if kickstart:
+            extraargs += " ks=%s" % kickstart
+        if extraargs:
+            self.__guest.installer.extraarags = extraargs
+
+        self.__guest.uuid = virtinst.util.uuidToString(virtinst.util.randomUUID())
+
+        if config.os_type != "generic":
+            self.__guest.os_type = config.os_type
+        if config.os_variant != "generic":
+            self.__guest.os_variant = config.os_variant
+
+        self.__guest._graphics_dev = virtinst.VirtualGraphics(type = virtinst.VirtualGraphics.TYPE_VNC)
+        self.__guest.sound_devs = []
+        self.__guest.sound_devs.append(virtinst.VirtualAudio(model = "es1370"))
+
+        self._setup_nics(config)
+        self._setup_disks(config)
+
+        self.__guest.conn = self.__conn
+        self.__domain = self.__guest.start_install(False)
+
+    def do_undefine_domain(self, domain):
+        _domain = self.get_domain(domain)
+
+        if _domain is None: raise Exception("No such domain: %s" % domain)
+
+        _domain.undefine()
+
+    def do_create_domain(self, domain):
+        _domain = self.get_domain(domain);
+
+        if _domain is None: raise Exception("No such domain: %s" % domain)
+        if not _domain.isActive():
+            _domain.create()
+
+    def do_destroy_domain(self, domain):
+        _domain = self.get_domain(domain)
+
+        if _domain is None: raise Exception("No such domain: %s" % domain)
+        if _domain.isActive():
+            _domain.destroy()
+
+    def _get_hypervisor(self, virt_type):
+        '''
+        Returns the hypervisor for the specified virtualization type.
+
+        virt_type -- The virtualization type.
+        '''
+
+        types = self._get_virt_types()
+        for type in types:
+            if type[0] == virt_type:
+                return type[1]
+        return None
+
+    def _get_os_type(self, virt_type):
+        '''
+        Returns the operating system types for the given virtualization type.
+
+        virt_type -- The virtualization type.
+        '''
+
+        types = self._get_virt_types()
+        for type in types:
+            if type[0] == virt_type:
+                return type[2]
+        return None
+
+    def _get_virt_types(self):
+        # if we already have the types, then return them
+        result = self.__virt_types
+        if result is not None:
+            return result
+
+        result = []
+        for guest in self.__capabilities.guests:
+            guest_type = guest.os_type
+            for domain in guest.domains:
+                domain_type = domain.hypervisor_type
+                label = domain_type
+
+                if domain_type is "kvm" and guest_type is "xen":
+                    label = "xenner"
+                elif domain_type is "xen":
+                    if guest_type is "xen":
+                        label = "xen (paravirt)"
+                    elif guest_type is "kvm":
+                        label = "xen (fullvirt)"
+                elif domain_type is "test":
+                    if guest_type is "xen":
+                        label = "test (xen)"
+                    elif guest_type is "hvm":
+                        label = "test (hvm)"
+
+                for row in result:
+                    if row[0] == label:
+                        label = None
+                        break
+                if label is None: continue
+
+                result.append([label, domain_type, guest_type])
+
+        self.__virt_types = result
+
+        return result
+
+    def _setup_nics(self, config):
+        self.__guest.nics = []
+        nic = virtinst.VirtualNetworkInterface(type = virtinst.VirtualNetworkInterface.TYPE_VIRTUAL,
+                                               bridge = config.network_bridge,
+                                               network = config.network_bridge,
+                                               macaddr = config.mac_address)
+        self.__guest.nics.append(nic)
+        # ensure the network is running
+        if config.network_bridge not in self.get_networks(defined = False):
+            network = self.create_network(config.network_bridge)
+
+    def _setup_disks(self, config):
+        self.__guest.disks = []
+        if config.enable_storage:
+            path = None
+            if config.use_local_storage:
+                if self.storage_pool_exists("default") is False:
+                    self.define_storage_pool("default")
+                pool = self.get_storage_pool("default")
+                path = virtinst.Storage.StorageVolume.find_free_name(config.guest_name,
+                                                                     pool_object = pool,
+                                                                     suffix = ".img")
+                path = os.path.join(DEFAULT_POOL_TARGET_PATH, path)
+            else:
+                volume = self.get_storage_volume(config.storage_pool,
+                                                 config.storage_volume)
+                path = volume.path()
+
+            if path is not None:
+                storage= virtinst.VirtualDisk(conn = self.__conn,
+                                              path = path,
+                                              size = config.storage_size)
+                self.__guest.disks.append(storage)
+        self.__guest.conn = self.__conn
+
-- 
1.7.4.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]