From 6884cd52cbbceccf1fc240854a1433918bb66e3d Mon Sep 17 00:00:00 2001 From: chestwood96 Date: Fri, 10 Mar 2023 15:07:36 +0100 Subject: Initial swapfile prototype (#1558) * Initial swapfile prototype * add_swapfile different exception handling * Added error handling to add_swapfile prototype --------- Co-authored-by: Anton Hvornum --- archinstall/lib/installer.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 2b0830d3..55bf5570 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -132,6 +132,7 @@ class Installer: # if HSM is not used to encrypt the root volume. Check mkinitcpio() function for that override. self.HOOKS = ["base", "systemd", "autodetect", "keyboard", "sd-vconsole", "modconf", "block", "filesystems", "fsck"] self.KERNEL_PARAMS = [] + self.FSTAB_ENTRIES = [] self._zram_enabled = False @@ -318,6 +319,26 @@ class Installer: partition.mount(f'{self.target}{mountpoint}', options=options) + def add_swapfile(self, size = '4G', enable_resume = True, file='/swapfile'): + if file[:1] != '/': + file = f"/{file}" + if len(file.strip()) <= 0 or file == '/': + raise ValueError(f"The filename for the swap file has to be a valid path, not: {self.target}{file}") + + SysCommand(f'dd if=/dev/zero of={self.target}{file} bs={size} count=1') + SysCommand(f'chmod 0600 {self.target}{file}') + SysCommand(f'mkswap {self.target}{file}') + + self.FSTAB_ENTRIES.append(f'{file} none swap defaults 0 0') + + if enable_resume: + resume_uuid = SysCommand(f'findmnt -no UUID -T {self.target}{file}').decode('UTF-8').strip() + resume_offset = SysCommand(f'/usr/bin/filefrag -v {self.target}{file}').decode('UTF-8').split('0:', 1)[1].split(":", 1)[1].split("..", 1)[0].strip() + + self.HOOKS.append('resume') + self.KERNEL_PARAMS.append(f'resume=UUID={resume_uuid}') + self.KERNEL_PARAMS.append(f'resume_offset={resume_offset}') + def post_install_check(self, *args :str, **kwargs :str) -> List[str]: return [step for step, flag in self.helper_flags.items() if flag is False] @@ -431,6 +452,11 @@ class Installer: if hasattr(plugin, 'on_genfstab'): if plugin.on_genfstab(self) is True: break + + with open(f"{self.target}/etc/fstab", 'a') as fstab_fh: + for entry in self.FSTAB_ENTRIES: + fstab_fh.write(f'{entry}\n') + return True -- cgit v1.2.3-54-g00ecf