Best Python code snippet using lisa_python
modprobe.py
Source:modprobe.py  
...11    def _initialize(self, *args: Any, **kwargs: Any) -> None:12        self._command = "modprobe"13    # hv_netvsc needs a special case, since reloading it has the potential14    # to leave the node without a network connection if things go wrong.15    def _reload_hv_netvsc(self) -> None:16        # These commands must be sent together, bundle them up as one line17        self.node.execute(18            "modprobe -r hv_netvsc; modprobe hv_netvsc; "19            "ip link set eth0 down; ip link set eth0 up;"20            "dhclient -r eth0; dhclient eth0",21            sudo=True,22            shell=True,23        )24    def is_module_loaded(25        self,26        mod_name: str,27        force_run: bool = False,28        no_info_log: bool = True,29        no_error_log: bool = True,30    ) -> bool:31        result = self.run(32            f"-nv {mod_name}",33            sudo=True,34            force_run=force_run,35            no_info_log=no_info_log,36            no_error_log=no_error_log,37        )38        # Example possible outputs39        # 1) insmod /lib/modules/.../floppy.ko.xz40        #    Exists but is not loaded - return False41        # 2) FATAL: Module floppy not found.42        #    Module does not exist, therefore is not loaded - return False43        # 3) (no output)44        #    Module is loaded - return True45        could_be_loaded = result.stdout and "insmod" in result.stdout46        does_not_exist = (result.stderr and "not found" in result.stderr) or (47            result.stdout and "not found" in result.stdout48        )49        return not (could_be_loaded or does_not_exist)50    def remove(self, mod_names: List[str], ignore_error: bool = False) -> None:51        for mod_name in mod_names:52            if ignore_error:53                # rmmod support the module file, so use it here.54                self.node.execute(f"rmmod {mod_name}", sudo=True)55            else:56                if self.is_module_loaded(mod_name, force_run=True):57                    self.run(58                        f"-r {mod_name}",59                        force_run=True,60                        sudo=True,61                        expected_exit_code=0,62                        expected_exit_code_failure_message="Fail to remove module "63                        f"{mod_name}",64                    )65    def load(66        self,67        modules: Union[str, List[str]],68        dry_run: bool = False,69    ) -> bool:70        if isinstance(modules, list):71            modules_str = "-a " + " ".join(modules)72        else:73            modules_str = modules74        command = f"{modules_str}"75        if dry_run:76            command = f"--dry-run {command}"77        result = self.run(78            command,79            force_run=True,80            sudo=True,81        )82        if dry_run:83            return result.exit_code == 084        result.assert_exit_code(85            expected_exit_code=0,86            message=f"Fail to load module[s]: {modules_str}.",87            include_output=True,88        )89        return True90    def module_exists(self, modules: Union[str, List[str]]) -> bool:91        return self.load(modules, dry_run=True)92    def reload(93        self,94        mod_names: List[str],95    ) -> None:96        for mod_name in mod_names:97            if self.is_module_loaded(mod_name, force_run=True):98                # hv_netvsc reload requires resetting the network interface99                if mod_name == "hv_netvsc":100                    # handle special case101                    self._reload_hv_netvsc()102                else:103                    # execute the command for regular non-network modules104                    self.node.execute(105                        f"modprobe -r {mod_name}; modprobe {mod_name};",106                        sudo=True,107                        shell=True,108                    )109    def load_by_file(self, file_name: str) -> None:110        # the insmod support to load from file.111        self.node.execute(112            f"insmod {file_name}",113            sudo=True,114            expected_exit_code=0,115            expected_exit_code_failure_message=f"failed to load module {file_name}",...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
