示例#1
0
    def _create_volume_with_capacity(self, cb, params):
        pool_name = params.pop('pool')
        vol_xml = """
        <volume>
          <name>%(name)s</name>
          <allocation unit='bytes'>%(allocation)s</allocation>
          <capacity unit='bytes'>%(capacity)s</capacity>
          <source>
          </source>
          <target>
            <format type='%(format)s'/>
          </target>
        </volume>
        """
        allocation = 0
        if params['pool_type'] == "logical":
            allocation = params['capacity']
        params.setdefault('allocation', allocation)
        params.setdefault('format', 'qcow2')

        name = params['name']
        try:
            pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            xml = vol_xml % params
        except KeyError, item:
            raise MissingParameter("KCHVOL0004E", {
                'item': str(item),
                'volume': name
            })
示例#2
0
    def _create_volume_with_capacity(self, cb, params):
        pool_name = params.pop('pool')
        vol_xml = """
        <volume>
          <name>%(name)s</name>
          <allocation unit='bytes'>%(allocation)s</allocation>
          <capacity unit='bytes'>%(capacity)s</capacity>
          <source>
          </source>
          <target>
            <format type='%(format)s'/>
          </target>
        </volume>
        """
        allocation = 0
        if params['pool_type'] == "logical":
            allocation = params['capacity']
        params.setdefault('allocation', allocation)
        params.setdefault('format', 'qcow2')

        name = params['name']
        try:
            pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            xml = vol_xml % params
        except KeyError, item:
            raise MissingParameter("KCHVOL0004E", {'item': str(item),
                                                   'volume': name})
示例#3
0
 def get_list(self, pool_name):
     pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
     if not pool.isActive():
         raise InvalidOperation("KCHVOL0006E", {'pool': pool_name})
     try:
         pool.refresh(0)
     except Exception, e:
         wok_log.error("Pool refresh failed: %s" % str(e))
示例#4
0
 def get_list(self, pool_name):
     pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
     if not pool.isActive():
         raise InvalidOperation('KCHVOL0006E', {'pool': pool_name})
     try:
         pool.refresh(0)
     except Exception as e:
         wok_log.error(f'Pool refresh failed: {e}')
     return sorted(pool.listVolumes())
示例#5
0
 def get_list(self, pool_name):
     pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
     if not pool.isActive():
         raise InvalidOperation('KCHVOL0006E', {'pool': pool_name})
     try:
         pool.refresh(0)
     except Exception as e:
         wok_log.error(f'Pool refresh failed: {e}')
     return sorted(pool.listVolumes())
示例#6
0
    def _create_volume_with_capacity(self, cb, params):
        pool_name = params.pop('pool')
        vol_xml = """
        <volume>
          <name>%(name)s</name>
          <allocation unit='bytes'>%(allocation)s</allocation>
          <capacity unit='bytes'>%(capacity)s</capacity>
          <source>
          </source>
          <target>
            <format type='%(format)s'/>
          </target>
        </volume>
        """
        allocation = 0
        if params['pool_type'] == 'logical':
            allocation = params['capacity']
        params.setdefault('allocation', allocation)
        params.setdefault('format', 'qcow2')

        name = params['name']
        try:
            pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            xml = vol_xml % params
        except KeyError as item:
            raise MissingParameter('KCHVOL0004E', {
                'item': str(item),
                'volume': name
            })

        try:
            pool.createXML(xml, 0)
        except libvirt.libvirtError as e:
            raise OperationFailed(
                'KCHVOL0007E',
                {
                    'name': name,
                    'pool': pool_name,
                    'err': e.get_error_message()
                },
            )

        vol_info = StorageVolumeModel(conn=self.conn,
                                      objstore=self.objstore).lookup(
                                          pool_name, name)
        vol_path = vol_info['path']

        if params.get('upload', False):
            upload_volumes[vol_path] = {
                'lock': threading.Lock(),
                'offset': 0,
                'cb': cb,
                'expected_vol_size': params['capacity'],
            }
            cb('ready for upload')
        else:
            cb('OK', True)
示例#7
0
    def _clone_task(self, cb, params):
        """Asynchronous function which performs the clone operation.

        This function copies all the data inside the original volume into the
        new one.

        Arguments:
        cb -- A callback function to signal the Task's progress.
        params -- A dict with the following values:
            "pool": The name of the original pool.
            "name": The name of the original volume.
            "new_pool": The name of the destination pool.
            "new_name": The name of the new volume.
        """
        orig_pool_name = params['pool']
        orig_vol_name = params['name']
        new_pool_name = params['new_pool']
        new_vol_name = params['new_name']

        try:
            cb('setting up volume cloning')
            orig_vir_vol = StorageVolumeModel.get_storagevolume(
                orig_pool_name, orig_vol_name, self.conn
            )
            orig_vol = self.lookup(orig_pool_name, orig_vol_name)
            new_vir_pool = StoragePoolModel.get_storagepool(
                new_pool_name, self.conn)

            cb('building volume XML')
            root_elem = E.volume()
            root_elem.append(E.name(new_vol_name))
            root_elem.append(E.capacity(
                str(orig_vol['capacity']), unit='bytes'))
            target_elem = E.target()
            target_elem.append(E.format(type=orig_vol['format']))
            root_elem.append(target_elem)
            new_vol_xml = ET.tostring(
                root_elem, encoding='utf-8', pretty_print=True
            ).decode('utf-8')

            cb('cloning volume')
            new_vir_pool.createXMLFrom(new_vol_xml, orig_vir_vol, 0)
        except (InvalidOperation, NotFoundError, libvirt.libvirtError) as e:
            raise OperationFailed(
                'KCHVOL0023E',
                {
                    'name': orig_vol_name,
                    'pool': orig_pool_name,
                    'err': e.get_error_message(),
                },
            )

        self.lookup(new_pool_name, new_vol_name)

        cb('OK', True)
示例#8
0
    def _clone_task(self, cb, params):
        """Asynchronous function which performs the clone operation.

        This function copies all the data inside the original volume into the
        new one.

        Arguments:
        cb -- A callback function to signal the Task's progress.
        params -- A dict with the following values:
            "pool": The name of the original pool.
            "name": The name of the original volume.
            "new_pool": The name of the destination pool.
            "new_name": The name of the new volume.
        """
        orig_pool_name = params['pool']
        orig_vol_name = params['name']
        new_pool_name = params['new_pool']
        new_vol_name = params['new_name']

        try:
            cb('setting up volume cloning')
            orig_vir_vol = StorageVolumeModel.get_storagevolume(
                orig_pool_name, orig_vol_name, self.conn
            )
            orig_vol = self.lookup(orig_pool_name, orig_vol_name)
            new_vir_pool = StoragePoolModel.get_storagepool(
                new_pool_name, self.conn)

            cb('building volume XML')
            root_elem = E.volume()
            root_elem.append(E.name(new_vol_name))
            root_elem.append(E.capacity(
                str(orig_vol['capacity']), unit='bytes'))
            target_elem = E.target()
            target_elem.append(E.format(type=orig_vol['format']))
            root_elem.append(target_elem)
            new_vol_xml = ET.tostring(
                root_elem, encoding='unicode', pretty_print=True
            )

            cb('cloning volume')
            new_vir_pool.createXMLFrom(new_vol_xml, orig_vir_vol, 0)
        except (InvalidOperation, NotFoundError, libvirt.libvirtError) as e:
            raise OperationFailed(
                'KCHVOL0023E',
                {
                    'name': orig_vol_name,
                    'pool': orig_pool_name,
                    'err': e.get_error_message(),
                },
            )

        self.lookup(new_pool_name, new_vol_name)

        cb('OK', True)
示例#9
0
 def get_list(self, pool_name):
     pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
     if not pool.isActive():
         raise InvalidOperation("KCHVOL0006E", {'pool': pool_name})
     try:
         pool.refresh(0)
         return sorted(map(lambda x: x.decode('utf-8'), pool.listVolumes()))
     except libvirt.libvirtError as e:
         raise OperationFailed("KCHVOL0008E",
                               {'pool': pool_name,
                                'err': e.get_error_message()})
示例#10
0
 def get_storagevolume(poolname, name, conn):
     pool = StoragePoolModel.get_storagepool(poolname, conn)
     if not pool.isActive():
         raise InvalidOperation("KCHVOL0006E", {"name": pool})
     try:
         return pool.storageVolLookupByName(name.encode("utf-8"))
     except libvirt.libvirtError as e:
         if e.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_VOL:
             raise NotFoundError("KCHVOL0002E", {"name": name, "pool": poolname})
         else:
             raise
示例#11
0
 def get_list(self, pool_name):
     pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
     if not pool.isActive():
         raise InvalidOperation("KCHVOL0006E", {'pool': pool_name})
     try:
         pool.refresh(0)
         return sorted(map(lambda x: x.decode('utf-8'), pool.listVolumes()))
     except libvirt.libvirtError as e:
         raise OperationFailed("KCHVOL0008E", {
             'pool': pool_name,
             'err': e.get_error_message()
         })
示例#12
0
 def get_storagevolume(poolname, name, conn):
     pool = StoragePoolModel.get_storagepool(poolname, conn)
     if not pool.isActive():
         raise InvalidOperation("KCHVOL0006E", {'name': pool})
     try:
         return pool.storageVolLookupByName(name.encode("utf-8"))
     except libvirt.libvirtError as e:
         if e.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_VOL:
             raise NotFoundError("KCHVOL0002E", {'name': name,
                                                 'pool': poolname})
         else:
             raise
示例#13
0
 def get_storagevolume(poolname, name, conn):
     pool = StoragePoolModel.get_storagepool(poolname, conn)
     if not pool.isActive():
         raise InvalidOperation('KCHVOL0006E', {'pool': poolname})
     try:
         return pool.storageVolLookupByName(name)
     except libvirt.libvirtError as e:
         if e.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_VOL:
             raise NotFoundError(
                 'KCHVOL0002E', {'name': name, 'pool': poolname})
         else:
             raise
示例#14
0
    def _create_volume_with_capacity(self, cb, params):
        pool_name = params.pop('pool')
        vol_xml = """
        <volume>
          <name>%(name)s</name>
          <allocation unit='bytes'>%(allocation)s</allocation>
          <capacity unit='bytes'>%(capacity)s</capacity>
          <source>
          </source>
          <target>
            <format type='%(format)s'/>
          </target>
        </volume>
        """
        allocation = 0
        if params['pool_type'] == 'logical':
            allocation = params['capacity']
        params.setdefault('allocation', allocation)
        params.setdefault('format', 'qcow2')

        name = params['name']
        try:
            pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            xml = vol_xml % params
        except KeyError as item:
            raise MissingParameter(
                'KCHVOL0004E', {'item': str(item), 'volume': name})

        try:
            pool.createXML(xml, 0)
        except libvirt.libvirtError as e:
            raise OperationFailed(
                'KCHVOL0007E',
                {'name': name, 'pool': pool_name, 'err': e.get_error_message()},
            )

        vol_info = StorageVolumeModel(conn=self.conn, objstore=self.objstore).lookup(
            pool_name, name
        )
        vol_path = vol_info['path']

        if params.get('upload', False):
            upload_volumes[vol_path] = {
                'lock': threading.Lock(),
                'offset': 0,
                'cb': cb,
                'expected_vol_size': params['capacity'],
            }
            cb('ready for upload')
        else:
            cb('OK', True)
示例#15
0
    def get_list(self):
        iso_volumes = []
        conn = self.conn.get()
        pools = conn.listStoragePools()
        pools += conn.listDefinedStoragePools()

        for pool_name in pools:
            try:
                pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
                pool.refresh(0)
                volumes = pool.listVolumes()
            except Exception, e:
                # Skip inactive pools
                wok_log.debug("Shallow scan: skipping pool %s because of " "error: %s", (pool_name, e.message))
                continue

            for volume in volumes:
                res = self.storagevolume.lookup(pool_name, volume.decode("utf-8"))
                if res["format"] == "iso" and res["bootable"]:
                    res["name"] = "%s" % volume
                    iso_volumes.append(res)
示例#16
0
    def _clone_task(self, cb, params):
        """Asynchronous function which performs the clone operation.

        This function copies all the data inside the original volume into the
        new one.

        Arguments:
        cb -- A callback function to signal the Task's progress.
        params -- A dict with the following values:
            "pool": The name of the original pool.
            "name": The name of the original volume.
            "new_pool": The name of the destination pool.
            "new_name": The name of the new volume.
        """
        orig_pool_name = params["pool"]
        orig_vol_name = params["name"]
        new_pool_name = params["new_pool"]
        new_vol_name = params["new_name"]

        try:
            cb("setting up volume cloning")
            orig_vir_vol = StorageVolumeModel.get_storagevolume(orig_pool_name, orig_vol_name, self.conn)
            orig_vol = self.lookup(orig_pool_name, orig_vol_name)
            new_vir_pool = StoragePoolModel.get_storagepool(new_pool_name, self.conn)

            cb("building volume XML")
            root_elem = E.volume()
            root_elem.append(E.name(new_vol_name))
            root_elem.append(E.capacity(unicode(orig_vol["capacity"]), unit="bytes"))
            target_elem = E.target()
            target_elem.append(E.format(type=orig_vol["format"]))
            root_elem.append(target_elem)
            new_vol_xml = ET.tostring(root_elem, encoding="utf-8", pretty_print=True)

            cb("cloning volume")
            new_vir_pool.createXMLFrom(new_vol_xml, orig_vir_vol, 0)
        except (InvalidOperation, NotFoundError, libvirt.libvirtError), e:
            raise OperationFailed(
                "KCHVOL0023E", {"name": orig_vol_name, "pool": orig_pool_name, "err": e.get_error_message()}
            )
示例#17
0
    def get_list(self):
        iso_volumes = []
        conn = self.conn.get()
        pools = conn.listStoragePools()
        pools += conn.listDefinedStoragePools()

        for pool_name in pools:
            try:
                pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
                pool.refresh(0)
                volumes = pool.listVolumes()
            except Exception, e:
                # Skip inactive pools
                wok_log.debug("Shallow scan: skipping pool %s because of "
                              "error: %s", (pool_name, e.message))
                continue

            for volume in volumes:
                res = self.storagevolume.lookup(pool_name,
                                                volume.decode("utf-8"))
                if res['format'] == 'iso' and res['bootable']:
                    res['name'] = '%s' % volume
                    iso_volumes.append(res)
示例#18
0
    def _create_volume_with_capacity(self, cb, params):
        pool_name = params.pop("pool")
        vol_xml = """
        <volume>
          <name>%(name)s</name>
          <allocation unit='bytes'>%(allocation)s</allocation>
          <capacity unit='bytes'>%(capacity)s</capacity>
          <source>
          </source>
          <target>
            <format type='%(format)s'/>
          </target>
        </volume>
        """
        params.setdefault("allocation", 0)
        params.setdefault("format", "qcow2")

        name = params["name"]
        try:
            pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            xml = vol_xml % params
        except KeyError, item:
            raise MissingParameter("KCHVOL0004E", {"item": str(item), "volume": name})
示例#19
0
    def get_list(self):
        iso_volumes = []
        conn = self.conn.get()
        pools = conn.listStoragePools()
        pools += conn.listDefinedStoragePools()

        for pool_name in pools:
            try:
                pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
                pool.refresh(0)
                volumes = pool.listVolumes()
            except Exception as e:
                # Skip inactive pools
                wok_log.debug(
                    f'Shallow scan: skipping pool {pool_name} because of ' f'error: {e}'
                )
                continue

            for volume in volumes:
                res = self.storagevolume.lookup(pool_name, volume)
                if res['format'] == 'iso' and res['bootable']:
                    res['name'] = f'{volume}'
                    iso_volumes.append(res)
        return iso_volumes
示例#20
0
    def get_list(self):
        iso_volumes = []
        conn = self.conn.get()
        pools = conn.listStoragePools()
        pools += conn.listDefinedStoragePools()

        for pool_name in pools:
            try:
                pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
                pool.refresh(0)
                volumes = pool.listVolumes()
            except Exception as e:
                # Skip inactive pools
                wok_log.debug(
                    f'Shallow scan: skipping pool {pool_name} because of '
                    f'error: {e}')
                continue

            for volume in volumes:
                res = self.storagevolume.lookup(pool_name, volume)
                if res['format'] == 'iso' and res['bootable']:
                    res['name'] = f'{volume}'
                    iso_volumes.append(res)
        return iso_volumes
示例#21
0
    def _create_volume_with_url(self, cb, params):
        pool_name = params["pool"]
        name = params["name"]
        url = params["url"]

        pool_model = StoragePoolModel(conn=self.conn, objstore=self.objstore)
        pool = pool_model.lookup(pool_name)

        if pool["type"] in ["dir", "netfs"]:
            file_path = os.path.join(pool["path"], name)
        else:
            file_path = tempfile.mkstemp(prefix=name)[1]

        with contextlib.closing(urllib2.urlopen(url)) as response:
            with open(file_path, "w") as volume_file:
                remote_size = response.info().getheader("Content-Length", "-")
                downloaded_size = 0

                try:
                    while True:
                        chunk_data = response.read(READ_CHUNK_SIZE)
                        if not chunk_data:
                            break

                        volume_file.write(chunk_data)
                        downloaded_size += len(chunk_data)
                        cb("%s/%s" % (downloaded_size, remote_size))
                except (IOError, libvirt.libvirtError) as e:
                    if os.path.isfile(file_path):
                        os.remove(file_path)

                    raise OperationFailed("KCHVOL0007E", {"name": name, "pool": pool_name, "err": e.message})

        if pool["type"] in ["dir", "netfs"]:
            virt_pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            virt_pool.refresh(0)
        else:

            def _stream_handler(stream, nbytes, fd):
                return fd.read(nbytes)

            virt_stream = virt_vol = None

            try:
                task = self.create(
                    pool_name,
                    {"name": name, "format": "raw", "capacity": downloaded_size, "allocation": downloaded_size},
                )
                self.task.wait(task["id"])
                virt_vol = StorageVolumeModel.get_storagevolume(pool_name, name, self.conn)

                virt_stream = self.conn.get().newStream(0)
                virt_vol.upload(virt_stream, 0, downloaded_size, 0)

                with open(file_path) as fd:
                    virt_stream.sendAll(_stream_handler, fd)

                virt_stream.finish()
            except (IOError, libvirt.libvirtError) as e:
                try:
                    if virt_stream:
                        virt_stream.abort()
                    if virt_vol:
                        virt_vol.delete(0)
                except libvirt.libvirtError, virt_e:
                    wok_log.error(virt_e.message)
                finally:
                    raise OperationFailed("KCHVOL0007E", {"name": name, "pool": pool_name, "err": e.message})
            finally:
示例#22
0
    def _create_volume_with_url(self, cb, params):
        pool_name = params['pool']
        name = params['name']
        url = params['url']

        pool_model = StoragePoolModel(conn=self.conn,
                                      objstore=self.objstore)
        pool = pool_model.lookup(pool_name)

        if pool['type'] in ['dir', 'netfs']:
            file_path = os.path.join(pool['path'], name)
        else:
            file_path = tempfile.mkstemp(prefix=name)[1]

        with contextlib.closing(urllib2.urlopen(url)) as response:
            with open(file_path, 'w') as volume_file:
                remote_size = response.info().getheader('Content-Length', '-')
                downloaded_size = 0

                try:
                    while True:
                        chunk_data = response.read(READ_CHUNK_SIZE)
                        if not chunk_data:
                            break

                        volume_file.write(chunk_data)
                        downloaded_size += len(chunk_data)
                        cb('%s/%s' % (downloaded_size, remote_size))
                except (IOError, libvirt.libvirtError) as e:
                    if os.path.isfile(file_path):
                        os.remove(file_path)

                    raise OperationFailed('KCHVOL0007E', {'name': name,
                                                          'pool': pool_name,
                                                          'err': e.message})

        if pool['type'] in ['dir', 'netfs']:
            virt_pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
            virt_pool.refresh(0)
        else:
            def _stream_handler(stream, nbytes, fd):
                return fd.read(nbytes)

            virt_stream = virt_vol = None

            try:
                task = self.create(pool_name, {'name': name,
                                               'format': 'raw',
                                               'capacity': downloaded_size,
                                               'allocation': downloaded_size})
                self.task.wait(task['id'])
                virt_vol = StorageVolumeModel.get_storagevolume(pool_name,
                                                                name,
                                                                self.conn)

                virt_stream = self.conn.get().newStream(0)
                virt_vol.upload(virt_stream, 0, downloaded_size, 0)

                with open(file_path) as fd:
                    virt_stream.sendAll(_stream_handler, fd)

                virt_stream.finish()
            except (IOError, libvirt.libvirtError) as e:
                try:
                    if virt_stream:
                        virt_stream.abort()
                    if virt_vol:
                        virt_vol.delete(0)
                except libvirt.libvirtError, virt_e:
                    wok_log.error(virt_e.message)
                finally:
                    raise OperationFailed('KCHVOL0007E', {'name': name,
                                                          'pool': pool_name,
                                                          'err': e.message})
            finally: