From b9bf1454ad729d3ab7df53a39d08e710124fb6ab Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Tue, 28 Jan 2020 09:14:09 +0100 Subject: [PATCH] Add function to check for fstab entry, Prevent double entries --- modules/swap/module.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index b03140d..ffedbab 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -17,7 +17,8 @@ class SwapModule(AbstractModule): "Get swap location": self._get_swap_location, "Get swap size": self._get_swap_size, "Show swapiness": self._show_swapiness, - "Adjust temp. swapiness": self._adjust_swapiness_temp + "Adjust temp. swapiness": self._adjust_swapiness_temp, + "Check fstab for entry": self._check_fstab_entry } menu = [ { @@ -82,14 +83,18 @@ class SwapModule(AbstractModule): def _make_swap_persistent(self): swap_location = self._get_swap_location() + persistence_entry = self._check_fstab_entry() if swap_location is None: print("Swap file doesn't exist!") return backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location) - subprocess.call(backup_fstab, shell=True) - subprocess.call(enable_persistence, shell=True) - print("Swap is now persistent!") + if persistence_entry is True: + print("Swap is already persistent!") + else: + subprocess.call(backup_fstab, shell=True) + subprocess.call(enable_persistence, shell=True) + print("Swap is now persistent!") def _show_swapiness(self): get_swapiness = "cat /proc/sys/vm/swappiness" @@ -127,3 +132,15 @@ class SwapModule(AbstractModule): if not output_swapon: print("Swap deleted!") print(output_free.strip()) + + def _check_fstab_entry(self): + swap_location = self._get_swap_location() + fstab_entry = "{} none swap sw 0 0\n".format(swap_location) + with open("/etc/fstab", "r") as fstab_file: + line = fstab_file.readlines()[-1] + if line != fstab_entry: + print("No entry in /etc/fstab") + return False + else: + print("fstab entry:", line.strip()) + return True