def get_iso_info(self, iso): iso_prefixes = ['/', 'http', 'https', 'ftp', 'ftps', 'tftp'] if len(filter(iso.startswith, iso_prefixes)) == 0: raise InvalidParameter("KCHTMPL0006E", {'param': iso}) try: iso_img = IsoImage(iso) return iso_img.probe() except IsoFormatError: raise InvalidParameter("KCHISO0001E", {'filename': iso})
def get_iso_info(self, iso): iso_prefixes = ["/", "http", "https", "ftp", "ftps", "tftp"] if len(filter(iso.startswith, iso_prefixes)) == 0: raise InvalidParameter("KCHTMPL0006E", {"param": iso}) try: iso_img = IsoImage(iso) return iso_img.probe() except IsoFormatError: raise InvalidParameter("KCHISO0001E", {"filename": iso})
def updater(iso_info): iso_name = os.path.basename(iso_info['path'])[:-3] duplicates = "%s/%s*" % (params['pool_path'], iso_name) for f in glob.glob(duplicates): iso_img = IsoImage(f) if (iso_info['distro'], iso_info['version']) == \ iso_img.probe(): return iso_path = iso_name + hashlib.md5(iso_info['path']).hexdigest() + \ '.iso' link_name = os.path.join(params['pool_path'], os.path.basename(iso_path)) os.symlink(iso_info['path'], link_name)
def updater(iso_info): iso_name = os.path.basename(iso_info['path'])[:-3] duplicates = '%s/%s*' % (params['pool_path'], iso_name) for f in glob.glob(duplicates): iso_img = IsoImage(f) if (iso_info['distro'], iso_info['version']) == iso_img.probe(): return iso_path = (iso_name + hashlib.md5( iso_info['path'].encode('utf-8')).hexdigest() + '.iso') link_name = os.path.join(params['pool_path'], os.path.basename(iso_path)) os.symlink(iso_info['path'], link_name)
def lookup(self, pool, name): vol = StorageVolumeModel.get_storagevolume(pool, name, self.conn) path = vol.path() info = vol.info() xml = vol.XMLDesc(0) try: fmt = xpath_get_text(xml, "/volume/target/format/@type")[0] except IndexError: # Not all types of libvirt storage can provide volume format # infomation. When there is no format information, we assume # it's 'raw'. fmt = 'raw' iso_img = None # 'raw' volumes from 'logical' pools may actually be 'iso'; # libvirt always reports them as 'raw' pool_info = self.storagepool.lookup(pool) if pool_info['type'] == 'logical' and fmt == 'raw': try: iso_img = IsoImage(path) except IsoFormatError: # not 'iso' afterall pass else: fmt = 'iso' # 'raw' volumes can not be valid image disks (e.g. XML, PDF, TXT are # raw files), so it's necessary check the 'content' of them isvalid = True if fmt == 'raw': try: ms = magic.open(magic.NONE) ms.load() if ms.file(path).lower() not in VALID_RAW_CONTENT: isvalid = False ms.close() except UnicodeDecodeError: isvalid = False used_by = get_disk_used_by(self.objstore, self.conn, path) res = dict(type=VOLUME_TYPE_MAP[info[0]], capacity=info[1], allocation=info[2], path=path, used_by=used_by, format=fmt, isvalid=isvalid) if fmt == 'iso': if os.path.islink(path): path = os.path.join(os.path.dirname(path), os.readlink(path)) os_distro = os_version = 'unknown' try: if iso_img is None: iso_img = IsoImage(path) os_distro, os_version = iso_img.probe() bootable = True except IsoFormatError: bootable = False res.update( dict(os_distro=os_distro, os_version=os_version, path=path, bootable=bootable)) return res
def lookup(self, pool, name): vol = StorageVolumeModel.get_storagevolume(pool, name, self.conn) path = vol.path() info = vol.info() xml = vol.XMLDesc(0) try: fmt = xpath_get_text(xml, '/volume/target/format/@type')[0] except IndexError: # Not all types of libvirt storage can provide volume format # infomation. When there is no format information, we assume # it's 'raw'. fmt = 'raw' iso_img = None # 'raw' volumes from 'logical' pools may actually be 'iso'; # libvirt always reports them as 'raw' pool_info = self.storagepool.lookup(pool) if pool_info['type'] == 'logical' and fmt == 'raw': try: iso_img = IsoImage(path) except IsoFormatError: # not 'iso' afterall pass else: fmt = 'iso' # 'raw' volumes can not be valid image disks (e.g. XML, PDF, TXT are # raw files), so it's necessary check the 'content' of them isvalid = True if fmt == 'raw': # Check if file is a symlink to a real block device, # if so, don't check it's contents for validity if not os.path.islink(path): try: ms = magic.open(magic.NONE) ms.load() if ms.file(path).lower() not in VALID_RAW_CONTENT: isvalid = False ms.close() except UnicodeDecodeError: isvalid = False else: # We are a symlink if '/dev/dm-' in os.path.realpath(path): # This is most likely a real blockdevice isvalid = True wok_log.error('symlink detected, validated the disk') else: # Doesn't point to a known blockdevice isvalid = False used_by = get_disk_used_by(self.conn, path) if self.libvirt_user is None: self.libvirt_user = UserTests().probe_user() ret, _ = probe_file_permission_as_user(os.path.realpath(path), self.libvirt_user) res = dict( type=VOLUME_TYPE_MAP[info[0]], capacity=info[1], allocation=info[2], path=path, used_by=used_by, format=fmt, isvalid=isvalid, has_permission=ret, ) if fmt == 'iso': if os.path.islink(path): path = os.path.join(os.path.dirname(path), os.readlink(path)) os_distro = os_version = 'unknown' try: if iso_img is None: iso_img = IsoImage(path) os_distro, os_version = iso_img.probe() bootable = True except IsoFormatError: bootable = False res.update( dict( os_distro=os_distro, os_version=os_version, path=path, bootable=bootable, )) return res
def lookup(self, pool, name): vol = StorageVolumeModel.get_storagevolume(pool, name, self.conn) path = vol.path() info = vol.info() xml = vol.XMLDesc(0) try: fmt = xpath_get_text(xml, '/volume/target/format/@type')[0] except IndexError: # Not all types of libvirt storage can provide volume format # infomation. When there is no format information, we assume # it's 'raw'. fmt = 'raw' iso_img = None # 'raw' volumes from 'logical' pools may actually be 'iso'; # libvirt always reports them as 'raw' pool_info = self.storagepool.lookup(pool) if pool_info['type'] == 'logical' and fmt == 'raw': try: iso_img = IsoImage(path) except IsoFormatError: # not 'iso' afterall pass else: fmt = 'iso' # 'raw' volumes can not be valid image disks (e.g. XML, PDF, TXT are # raw files), so it's necessary check the 'content' of them isvalid = True if fmt == 'raw': # Check if file is a symlink to a real block device, # if so, don't check it's contents for validity if not os.path.islink(path): try: ms = magic.open(magic.NONE) ms.load() if ms.file(path).lower() not in VALID_RAW_CONTENT: isvalid = False ms.close() except UnicodeDecodeError: isvalid = False else: # We are a symlink if '/dev/dm-' in os.path.realpath(path): # This is most likely a real blockdevice isvalid = True wok_log.error('symlink detected, validated the disk') else: # Doesn't point to a known blockdevice isvalid = False used_by = get_disk_used_by(self.conn, path) if self.libvirt_user is None: self.libvirt_user = UserTests().probe_user() ret, _ = probe_file_permission_as_user( os.path.realpath(path), self.libvirt_user ) res = dict( type=VOLUME_TYPE_MAP[info[0]], capacity=info[1], allocation=info[2], path=path, used_by=used_by, format=fmt, isvalid=isvalid, has_permission=ret, ) if fmt == 'iso': if os.path.islink(path): path = os.path.join(os.path.dirname(path), os.readlink(path)) os_distro = os_version = 'unknown' try: if iso_img is None: iso_img = IsoImage(path) os_distro, os_version = iso_img.probe() bootable = True except IsoFormatError: bootable = False res.update( dict( os_distro=os_distro, os_version=os_version, path=path, bootable=bootable, ) ) return res