def handle_parms(self):
        if self.__target_driver == "vfio-pci":
            device = "vfio-pci"
        else:
            device = "pci-assign"

        device_option = "-device {},host={}".format(device, self.__host_bdf)
        if self.__bus:
            device_option = "{},bus={}" .format(device_option, self.__bus)
        if self.__addr:
            device_option = "{},addr={}" .format(device_option, self.__addr)
        if self.__multifunction:
            device_option = "{},multifunction={}".format(device_option, self.__multifunction)

        logger.info("{}".format(device_option))
        self.add_option(device_option)
Beispiel #2
0
    def __add_entry(self, entry):
        """
        add single entry in /etc/apt/sources.list
        """
        if entry is None:
            # set to defaut entry
            entry = "deb https://dl.bintray.com/infrasim/deb xenial main"

        if self.__check_if_entry_exists(entry):
            print "{} exists".format(entry)
            return

        source_entry = aptsources.sourceslist.SourceEntry(entry)
        source_list = source_entry.mysplit(entry)
        if (source_list[0] not in ["deb", "deb-src"
                                   ]) or (len(source_list) < 4):
            logger.error("Invalid entry ({}).".format(entry))
            return

        typ = source_list[0]
        uri = None
        distribution = None
        attribute = None
        components = []
        for source in source_list:
            if source in ["deb", "deb-src"]:
                continue
            elif re.search(r'^https?:\/\/\w+', source):
                uri = source
            elif re.search(r'^\[.*\]', source):
                attribute = source
            elif source in ["trusty", "xenial"]:
                distribution = source
            else:
                components.append(source)

        if uri is None or distribution is None:
            return

        if attribute:
            uri = " ".join([attribute, uri])
        sources_list = aptsources.sourceslist.SourcesList()
        sources_list.backup(".orig")
        logger.info("adding source list entry: {} {} {} {}".format(
            typ, uri, distribution, " ".join(components)))
        sources_list.add(typ, uri, distribution, components)
        sources_list.save()
    def handle_parms(self):
        if self.__target_driver == "vfio-pci":
            device = "vfio-pci"
        else:
            device = "pci-assign"

        device_option = "-device {},host={}".format(device, self.__host_bdf)
        if self.__bus:
            device_option = "{},bus={}".format(device_option, self.__bus)
        if self.__addr:
            device_option = "{},addr={}".format(device_option, self.__addr)
        if self.__multifunction:
            device_option = "{},multifunction={}".format(
                device_option, self.__multifunction)

        logger.info("{}".format(device_option))
        self.add_option(device_option)
    def do_cleanup(self, action=None):
        if action not in frozenset(['autoremove', 'autoclean']):
            raise AssertionError('Expected "autoremove" or "autoclean" clean action.')

        if self.__force:
            force_yes = "--force-yes"
        else:
            force_yes = ""

        cmd = "{} -y {} {}".format("apt-get", force_yes, action)
        try:
            _, out = run_command(cmd)
            print out
            logger.info(out)
        except Exception as e:
            logger.exception(e)
            raise e
    def __add_entry(self, entry):
        """
        add single entry in /etc/apt/sources.list
        """
        if entry is None:
            # set to defaut entry
            entry = "deb https://dl.bintray.com/infrasim/deb xenial main"

        if self.__check_if_entry_exists(entry):
            print "{} exists".format(entry)
            return

        source_entry = aptsources.sourceslist.SourceEntry(entry)
        source_list = source_entry.mysplit(entry)
        if (source_list[0] not in ["deb", "deb-src"]) or (len(source_list) < 4):
            logger.error("Invalid entry ({}).".format(entry))
            return

        typ = source_list[0]
        uri = None
        distribution = None
        attribute = None
        components = []
        for source in source_list:
            if source in ["deb", "deb-src"]:
                continue
            elif re.search(r'^https?:\/\/\w+', source):
                uri = source
            elif re.search(r'^\[.*\]', source):
                attribute = source
            elif source in ["trusty", "xenial"]:
                distribution = source
            else:
                components.append(source)

        if uri is None or distribution is None:
            return

        if attribute:
            uri = " ".join([attribute, uri])
        sources_list = aptsources.sourceslist.SourcesList()
        sources_list.backup(".orig")
        logger.info("adding source list entry: {} {} {} {}".format(typ, uri, distribution, " ".join(components)))
        sources_list.add(typ, uri, distribution, components)
        sources_list.save()
    def bind(self):
        device_prefix = "/sys/bus/pci/devices"
        driver_prefix = "/sys/bus/pci/drivers"
        for device_bdf in self.__dev_sysfs:
            fd = os.open(
                "{}/{}/driver_override".format(device_prefix, device_bdf),
                os.O_RDWR)
            os.write(fd, self.__target_driver)
            driver_path = "{}/{}/driver".format(device_prefix, device_bdf)
            if os.path.exists(driver_path):
                current_driver = os.path.basename(os.readlink(driver_path))
                logger.info("Binding driver from {} to {} for {}".format(
                    current_driver, self.__target_driver, device_bdf))
                if current_driver == self.__target_driver:
                    logger.warn("{} is already bound to {}".format(
                        device_bdf, self.__target_driver))
                    continue
                else:
                    fd = os.open("{}/unbind".format(driver_path), os.O_WRONLY)
                    os.write(fd, device_bdf)
                    logger.info("Unbound {} from {}".format(
                        device_bdf, current_driver))

                    vendor_id = self.__read_config(device_bdf, 0, 2)
                    device_id = self.__read_config(device_bdf, 2, 2)

                    logger.info("write {:04x} {:04x} to {}/{}/new_id".format(
                        vendor_id, device_id, driver_prefix,
                        self.__target_driver))
                    new_id_fd = os.open(
                        "{}/{}/new_id".format(driver_prefix,
                                              self.__target_driver),
                        os.O_WRONLY)
                    os.write(new_id_fd,
                             "{:04x} {:04x}".format(vendor_id, device_id))

            fd2 = os.open("/sys/bus/pci/drivers_probe", os.O_WRONLY)
            os.write(fd2, device_bdf)

            iommu_group_path = os.readlink(
                os.path.join(device_prefix, self.__host_bdf, "iommu_group"))
            iommu_group_id = int(iommu_group_path.split('/')[-1])
            if os.path.exists("/dev/fio/{}".format(iommu_group_id)):
                os.chown("/dev/fio/{}".format(iommu_group_id),
                         os.environ.get("SUDO_UID"),
                         os.environ.get("SUDO_GID"))

            if os.path.exists("/dev/fio/fio"):
                os.chmod("/dev/fio/fio", 0664)
Beispiel #7
0
    def do_remove(self, pkgname, version="latest"):
        package = None
        installed, installed_version, upgradable, has_files = self.__check_package_status(
            pkgname, version, state='remove')
        logger.info(
            "remove(): installed {}, installed_version {}, upgradable {}, has_files {}"
            .format(installed, installed_version, upgradable, has_files))
        if (installed and version == "latest") or installed_version or (
                has_files and self.__purge):
            package = pkgname

        if not package:
            return

        else:
            if self.__force:
                force_yes = "--force-yes"
            else:
                force_yes = ""

            if self.__purge:
                purge = "--purge"
            else:
                purge = ""

            if self.__autoremove:
                autoremove = "--auto-remove"
            else:
                autoremove = ""

            cmd = "{} -q -y {} {} {} remove {}".format("apt-get", purge,
                                                       force_yes, autoremove,
                                                       package)

            logger.info(cmd)
            try:
                rc, out = run_command(cmd)
                print out
                logger.info(out)
            except Exception as e:
                logger.exception(e)
                raise e
    def bind(self):
        device_prefix = "/sys/bus/pci/devices"
        driver_prefix = "/sys/bus/pci/drivers"
        for device_bdf in self.__dev_sysfs:
            fd = os.open("{}/{}/driver_override".format(device_prefix, device_bdf), os.O_RDWR)
            os.write(fd, self.__target_driver)
            driver_path = "{}/{}/driver".format(device_prefix, device_bdf)
            if os.path.exists(driver_path):
                current_driver = os.path.basename(os.readlink(driver_path))
                logger.info("Binding driver from {} to {} for {}".format(current_driver,
                                                                         self.__target_driver, device_bdf))
                if current_driver == self.__target_driver:
                    logger.warn("{} is already bound to {}".format(device_bdf, self.__target_driver))
                    continue
                else:
                    fd = os.open("{}/unbind".format(driver_path), os.O_WRONLY)
                    os.write(fd, device_bdf)
                    logger.info("Unbound {} from {}".format(device_bdf, current_driver))

                    vendor_id = self.__read_config(device_bdf, 0, 2)
                    device_id = self.__read_config(device_bdf, 2, 2)

                    logger.info("write {:04x} {:04x} to {}/{}/new_id".format(vendor_id,
                                                                             device_id,
                                                                             driver_prefix,
                                                                             self.__target_driver))
                    new_id_fd = os.open("{}/{}/new_id".format(driver_prefix, self.__target_driver),
                                        os.O_WRONLY)
                    os.write(new_id_fd, "{:04x} {:04x}".format(vendor_id, device_id))

            fd2 = os.open("/sys/bus/pci/drivers_probe", os.O_WRONLY)
            os.write(fd2, device_bdf)

            iommu_group_path = os.readlink(os.path.join(device_prefix, self.__host_bdf, "iommu_group"))
            iommu_group_id = int(iommu_group_path.split('/')[-1])
            if os.path.exists("/dev/fio/{}".format(iommu_group_id)):
                os.chown("/dev/fio/{}".format(iommu_group_id), os.environ.get("SUDO_UID"),
                         os.environ.get("SUDO_GID"))

            if os.path.exists("/dev/fio/fio"):
                os.chmod("/dev/fio/fio", 0664)
    def do_remove(self, pkgname, version="latest"):
        package = None
        installed, installed_version, upgradable, has_files = self.__check_package_status(pkgname,
                                                                                          version, state='remove')
        logger.info(
            "remove(): installed {}, installed_version {}, upgradable {}, has_files {}".format(installed,
                                                                                               installed_version,
                                                                                               upgradable,
                                                                                               has_files))
        if (installed and version == "latest") or installed_version or (has_files and self.__purge):
            package = pkgname

        if not package:
            return

        else:
            if self.__force:
                force_yes = "--force-yes"
            else:
                force_yes = ""

            if self.__purge:
                purge = "--purge"
            else:
                purge = ""

            if self.__autoremove:
                autoremove = "--auto-remove"
            else:
                autoremove = ""

            cmd = "{} -q -y {} {} {} remove {}".format("apt-get", purge, force_yes, autoremove, package)

            logger.info(cmd)
            try:
                rc, out = run_command(cmd)
                print out
                logger.info(out)
            except Exception as e:
                logger.exception(e)
                raise e
Beispiel #10
0
    def do_install(self, pkg_name, version='latest', default_release=None):
        combined_package_version = ""
        installed, installed_version, upgradable, has_files = self.__check_package_status(
            pkg_name, version, state='install')

        logger.info(
            "install(): installed {}, installed_version {}, upgradable {}, has_files {}"
            .format(installed, installed_version, upgradable, has_files))
        if (not installed and not self.__only_upgrade) or (installed and not installed_version) or \
                (self.__only_upgrade and upgradable):
            if version != "latest":
                combined_package_version = "'{}={}'".format(pkg_name, version)
            else:
                combined_package_version = "'{}'".format(pkg_name)

        if installed_version and upgradable and version:
            if version != "latest":
                combined_package_version = "'{}={}'".format(pkg_name, version)
            else:
                combined_package_version = "'{}'".format(pkg_name)

        logger.info(
            "combined_package_version: {}".format(combined_package_version))

        if combined_package_version:
            if self.__force:
                force_yes = "--force-yes"
            else:
                force_yes = ''

            if self.__autoremove:
                autoremove = '--auto-remove'
            else:
                autoremove = ''

            if self.__only_upgrade:
                only_upgrade = '--only-upgrade'
            else:
                only_upgrade = ''

            cmd = "{} -y {} {} {} install {}".format("apt-get", only_upgrade,
                                                     force_yes, autoremove,
                                                     combined_package_version)

            if default_release:
                cmd += " -t '{}'".format(default_release)

            if self.__install_recommends is False:
                cmd += ' -o APT::Install-Recommends=no'
            else:
                cmd += ' -o APT::Install-Recommends=yes'

            if self.__allow_unauthenticated:
                cmd += " --allow-unauthenticated"

            logger.info(cmd)
            try:
                rc, out = run_command(cmd)
                print out
                logger.info(out)
                self.__mark_installed_manually(pkg_name)
            except Exception as e:
                logger.exception(e)
                raise e
Beispiel #11
0
    def __check_package_status(self, pkgname, version, state):
        try:
            package = self.__cache[pkgname]
            ll_package = self.__cache._cache[pkgname]
        except KeyError:
            if state == 'install':
                try:
                    provided_packages = self.__cache.get_providing_packages(
                        pkgname)
                    if provided_packages:
                        is_installed = False
                        upgradable = False
                        version_ok = False

                        if self.__cache.is_virtual_package(pkgname) and len(
                                provided_packages) == 1:
                            package = provided_packages[0]
                            installed, version_ok, upgradable, has_files = self.__check_package_status(
                                package.name, version, state='install')
                            if installed:
                                is_installed = True
                        return is_installed, version_ok, upgradable, False
                except AttributeError:
                    return False, False, True, False
            else:
                return False, False, False, False

        try:
            has_files = len(package.installed_files) > 0
        except UnicodeDecodeError:
            has_files = True
        except AttributeError:
            has_files = False

        try:
            package_is_installed = ll_package.current_state == apt_pkg.CURSTATE_INSTALLED
        except AttributeError:
            try:
                package_is_installed = package.is_installed
            except AttributeError as e:
                logger.exception(e)
                raise e

        version_is_installed = package_is_installed
        if version:
            versions = self.__get_package_versions(pkgname, package,
                                                   self.__cache._cache)
            available_upgrades = fnmatch.filter(versions, version)

            if package_is_installed:
                try:
                    installed_version = package.installed.version
                except AttributeError as e:
                    logger.exception(e)
                    raise e

                logger.info("installed version: {}".format(installed_version))
                version_is_installed = fnmatch.fnmatch(installed_version,
                                                       version)

                package_is_upgradable = False
                for candidate in available_upgrades:
                    if self.__package_version_compare(candidate,
                                                      installed_version) > 0:
                        package_is_upgradable = True
                        break
            else:
                package_is_upgradable = bool(available_upgrades)
        else:
            try:
                package_is_upgradable = package.is_upgradable
            except AttributeError as e:
                logger.exception(e)
                raise e

        return package_is_installed, version_is_installed, package_is_upgradable, has_files
    def do_install(self, pkg_name, version='latest', default_release=None):
        combined_package_version = ""
        installed, installed_version, upgradable, has_files = self.__check_package_status(pkg_name,
                                                                                          version, state='install')

        logger.info(
            "install(): installed {}, installed_version {}, upgradable {}, has_files {}".format(installed,
                                                                                                installed_version,
                                                                                                upgradable,
                                                                                                has_files))
        if (not installed and not self.__only_upgrade) or (installed and not installed_version) or \
                (self.__only_upgrade and upgradable):
            if version != "latest":
                combined_package_version = "'{}={}'".format(pkg_name, version)
            else:
                combined_package_version = "'{}'".format(pkg_name)

        if installed_version and upgradable and version:
            if version != "latest":
                combined_package_version = "'{}={}'".format(pkg_name, version)
            else:
                combined_package_version = "'{}'".format(pkg_name)

        logger.info("combined_package_version: {}".format(combined_package_version))

        if combined_package_version:
            if self.__force:
                force_yes = "--force-yes"
            else:
                force_yes = ''

            if self.__autoremove:
                autoremove = '--auto-remove'
            else:
                autoremove = ''

            if self.__only_upgrade:
                only_upgrade = '--only-upgrade'
            else:
                only_upgrade = ''

            cmd = "{} -y {} {} {} install {}".format("apt-get", only_upgrade, force_yes,
                                                     autoremove, combined_package_version)

            if default_release:
                cmd += " -t '{}'".format(default_release)

            if self.__install_recommends is False:
                cmd += ' -o APT::Install-Recommends=no'
            else:
                cmd += ' -o APT::Install-Recommends=yes'

            if self.__allow_unauthenticated:
                cmd += " --allow-unauthenticated"

            logger.info(cmd)
            try:
                rc, out = run_command(cmd)
                print out
                logger.info(out)
                self.__mark_installed_manually(pkg_name)
            except Exception as e:
                logger.exception(e)
                raise e
    def __check_package_status(self, pkgname, version, state):
        try:
            package = self.__cache[pkgname]
            ll_package = self.__cache._cache[pkgname]
        except KeyError:
            if state == 'install':
                try:
                    provided_packages = self.__cache.get_providing_packages(pkgname)
                    if provided_packages:
                        is_installed = False
                        upgradable = False
                        version_ok = False

                        if self.__cache.is_virtual_package(pkgname) and len(provided_packages) == 1:
                            package = provided_packages[0]
                            installed, version_ok, upgradable, has_files = self.__check_package_status(package.name,
                                                                                                       version,
                                                                                                       state='install')
                            if installed:
                                is_installed = True
                        return is_installed, version_ok, upgradable, False
                except AttributeError:
                    return False, False, True, False
            else:
                return False, False, False, False

        try:
            has_files = len(package.installed_files) > 0
        except UnicodeDecodeError:
            has_files = True
        except AttributeError:
            has_files = False

        try:
            package_is_installed = ll_package.current_state == apt_pkg.CURSTATE_INSTALLED
        except AttributeError:
            try:
                package_is_installed = package.is_installed
            except AttributeError as e:
                logger.exception(e)
                raise e

        version_is_installed = package_is_installed
        if version:
            versions = self.__get_package_versions(pkgname, package, self.__cache._cache)
            available_upgrades = fnmatch.filter(versions, version)

            if package_is_installed:
                try:
                    installed_version = package.installed.version
                except AttributeError as e:
                    logger.exception(e)
                    raise e

                logger.info("installed version: {}".format(installed_version))
                version_is_installed = fnmatch.fnmatch(installed_version, version)

                package_is_upgradable = False
                for candidate in available_upgrades:
                    if self.__package_version_compare(candidate, installed_version) > 0:
                        package_is_upgradable = True
                        break
            else:
                package_is_upgradable = bool(available_upgrades)
        else:
            try:
                package_is_upgradable = package.is_upgradable
            except AttributeError as e:
                logger.exception(e)
                raise e

        return package_is_installed, version_is_installed, package_is_upgradable, has_files