Best Python code snippet using lisa_python
kdump.py
Source:kdump.py  
...192        This method returns the path of cfg file where we configure crashkernel memory.193        If distro has a different cfg file path, override it.194        """195        return "/etc/default/grub"196    def _get_crashkernel_cfg_cmdline(self) -> str:197        """198        This method returns the cmdline string where we can configure crashkernel memory199        in the file _get_crashkernel_cfg_file returns.200        If distro has a different cmdline, override it.201        """202        return "GRUB_CMDLINE_LINUX"203    def _get_crashkernel_update_cmd(self, crashkernel: str) -> str:204        """205        After setting crashkernel into grub cfg file, need updating grub configuration.206        This function returns the update command string. If distro has a different207        command, override this method.208        """209        return "grub2-mkconfig -o /boot/grub2/grub.cfg"210    def _get_kdump_service_name(self) -> str:211        """212        This method returns the name of kdump service. If distro has a different name,213        needs override it.214        """215        return "kdump"216    def config_crashkernel_memory(217        self,218        crashkernel: str,219    ) -> None:220        # For Redhat 8 and later version, the cfg_file should be None.221        cfg_file = self._get_crashkernel_cfg_file()222        cmdline = self._get_crashkernel_cfg_cmdline()223        if cfg_file:224            self.node.execute(225                f"ls -lt {cfg_file}",226                expected_exit_code=0,227                expected_exit_code_failure_message=f"{cfg_file} doesn't exist",228                sudo=True,229            )230            cat = self.node.tools[Cat]231            sed = self.node.tools[Sed]232            result = cat.run(cfg_file, sudo=True, force_run=True)233            if "crashkernel" in result.stdout:234                sed.substitute(235                    match_lines=f"^{cmdline}",236                    regexp='crashkernel=[^[:space:]"]*',237                    replacement=f"crashkernel={crashkernel}",238                    file=cfg_file,239                    sudo=True,240                )241            else:242                sed.substitute(243                    match_lines=f"^{cmdline}",244                    regexp='"$',245                    replacement=f' crashkernel={crashkernel}"',246                    file=cfg_file,247                    sudo=True,248                )249            # Check if crashkernel is insert in cfg file250            result = cat.run(cfg_file, sudo=True, force_run=True)251            if f"crashkernel={crashkernel}" not in result.stdout:252                raise LisaException(253                    f'No find "crashkernel={crashkernel}" in {cfg_file} after'254                    "insert. Please double check the grub config file and insert"255                    "process"256                )257        # Update grub258        update_cmd = self._get_crashkernel_update_cmd(crashkernel)259        result = self.node.execute(update_cmd, sudo=True, shell=True)260        result.assert_exit_code(message="Failed to update grub")261    def config_resource_disk_dump_path(self, dump_path: str) -> None:262        """263        If the system memory size is bigger than 1T, the default size of /var/crash264        may not be enough to store the dump file, need to configure the dump path.265        The distro which may not have enough space, need override this method.266        """267        return268    def enable_kdump_service(self) -> None:269        """270        This method enables the kdump service.271        """272        service = self.node.tools[Service]273        service.enable_service(self._get_kdump_service_name())274    def restart_kdump_service(self) -> None:275        """276        This method restarts the kdump service.277        """278        service = self.node.tools[Service]279        service.restart_service(self._get_kdump_service_name())280    def set_unknown_nmi_panic(self) -> None:281        """282        /proc/sys/kernel/unknown_nmi_panic:283        The value in this file affects behavior of handling NMI. When the value is284        non-zero, unknown NMI is trapped and then panic occurs. If need to dump the285        crash, the value should be set 1. Some architectures don't provide architected286        NMIs,such as ARM64, the system doesn't have this file, we don't need to set287        either.288        """289        nmi_panic_file = PurePath("/proc/sys/kernel/unknown_nmi_panic")290        if self.node.shell.exists(nmi_panic_file):291            sysctl = self.node.tools[Sysctl]292            sysctl.write("kernel.unknown_nmi_panic", "1")293    @retry(exceptions=LisaException, tries=60, delay=1)294    def _check_kexec_crash_loaded(self) -> None:295        """296        Sometimes it costs a while to load the value, so define this method as @retry297        """298        # If the dump_path is not "/var/crash", for example it is "/mnt/crash",299        # the kdump service may start before the /mnt is mounted. That will cause300        # "Dump path /mnt/crash does not exist" error. We need to restart it.301        if self.dump_path != "/var/crash":302            self.restart_kdump_service()303        cat = self.node.tools[Cat]304        result = cat.run(self.kexec_crash, force_run=True)305        if "1" != result.stdout:306            raise LisaException(f"{self.kexec_crash} file's value is not 1.")307    def _check_crashkernel_in_cmdline(self, crashkernel_memory: str) -> None:308        cat = self.node.tools[Cat]309        result = cat.run("/proc/cmdline", force_run=True)310        if f"crashkernel={crashkernel_memory}" not in result.stdout:311            raise LisaException(312                f"crashkernel={crashkernel_memory} boot parameter is not present in"313                "kernel cmdline"314            )315    def _check_crashkernel_memory_reserved(self) -> None:316        cat = self.node.tools[Cat]317        result = cat.run(self.iomem, force_run=True)318        if "Crash kernel" not in result.stdout:319            raise LisaException(320                f"No find 'Crash kernel' in {self.iomem}. Memory isn't reserved for"321                "crash kernel"322            )323    def check_crashkernel_loaded(self, crashkernel_memory: str) -> None:324        # Check crashkernel parameter in cmdline325        self._check_crashkernel_in_cmdline(crashkernel_memory)326        # Check crash kernel loaded327        if not self.node.shell.exists(PurePosixPath(self.kexec_crash)):328            raise LisaException(329                f"{self.kexec_crash} file doesn't exist. Kexec crash is not loaded."330            )331        self._check_kexec_crash_loaded()332        # Check if memory is reserved for crash kernel333        self._check_crashkernel_memory_reserved()334class KdumpRedhat(KdumpBase):335    @property336    def command(self) -> str:337        return "kdumpctl"338    def _install(self) -> bool:339        assert isinstance(self.node.os, Redhat)340        self.node.os.install_packages("kexec-tools")341        return self._check_exists()342    def _get_crashkernel_cfg_file(self) -> str:343        if self.node.os.information.version >= "8.0.0-0" and not isinstance(344            self.node.os, Oracle345        ):346            # For Redhat 8 and later version, we can use grubby command to config347            # crashkernel. No need to get the crashkernel cfg file348            return ""349        else:350            return "/etc/default/grub"351    def _get_crashkernel_update_cmd(self, crashkernel: str) -> str:352        if self.node.os.information.version >= "8.0.0-0" and not isinstance(353            self.node.os, Oracle354        ):355            return (356                "grubby --update-kernel=/boot/vmlinuz-$(uname -r)"357                f' --args="crashkernel={crashkernel}"'358            )359        else:360            if self.node.shell.exists(PurePosixPath("/sys/firmware/efi")):361                # System with UEFI firmware362                grub_file_path = self.node.execute(363                    "find /boot/efi/EFI/* -name grub.cfg", shell=True, sudo=True364                )365                return f"grub2-mkconfig -o {grub_file_path}"366            else:367                # System with BIOS firmware368                return "grub2-mkconfig -o /boot/grub2/grub.cfg"369    def config_resource_disk_dump_path(self, dump_path: str) -> None:370        """371        If the system memory size is bigger than 1T, the default size of /var/crash372        may not be enough to store the dump file, need to change the dump path373        """374        kdump_conf = "/etc/kdump.conf"375        self.dump_path = dump_path376        # Change dump path in kdump conf377        sed = self.node.tools[Sed]378        sed.substitute(379            match_lines="^path",380            regexp="path",381            replacement="#path",382            file=kdump_conf,383            sudo=True,384        )385        sed.append(f"path {self.dump_path}", kdump_conf, sudo=True)386class KdumpDebian(KdumpBase):387    @property388    def command(self) -> str:389        return "kdump-config"390    def _install(self) -> bool:391        assert isinstance(self.node.os, Debian)392        self.node.os.install_packages("kdump-tools")393        return self._check_exists()394    def _get_crashkernel_cfg_file(self) -> str:395        return "/etc/default/grub.d/kdump-tools.cfg"396    def _get_crashkernel_update_cmd(self, crashkernel: str) -> str:397        return "update-grub"398    def _get_kdump_service_name(self) -> str:399        return "kdump-tools"400class KdumpSuse(KdumpBase):401    @property402    def command(self) -> str:403        return "kdumptool"404    def _install(self) -> bool:405        assert isinstance(self.node.os, Suse)406        self.node.os.install_packages("kdump")407        return self._check_exists()408class KdumpCBLMariner(KdumpBase):409    @property410    def command(self) -> str:411        return "kdumpctl"412    def _install(self) -> bool:413        assert isinstance(self.node.os, CBLMariner)414        self.node.os.install_packages("kexec-tools")415        return self._check_exists()416    def _get_crashkernel_cfg_file(self) -> str:417        return "/boot/mariner.cfg"418    def _get_crashkernel_cfg_cmdline(self) -> str:419        return "mariner_cmdline"420    def _get_crashkernel_update_cmd(self, crashkernel: str) -> str:...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!!
