Add function to check for fstab entry, Prevent double entries

This commit is contained in:
Lucas Noki 2020-01-28 09:14:09 +01:00
parent ed44d25d8d
commit b9bf1454ad

View File

@ -17,7 +17,8 @@ class SwapModule(AbstractModule):
"Get swap location": self._get_swap_location, "Get swap location": self._get_swap_location,
"Get swap size": self._get_swap_size, "Get swap size": self._get_swap_size,
"Show swapiness": self._show_swapiness, "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 = [ menu = [
{ {
@ -82,14 +83,18 @@ class SwapModule(AbstractModule):
def _make_swap_persistent(self): def _make_swap_persistent(self):
swap_location = self._get_swap_location() swap_location = self._get_swap_location()
persistence_entry = self._check_fstab_entry()
if swap_location is None: if swap_location is None:
print("Swap file doesn't exist!") print("Swap file doesn't exist!")
return return
backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" 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) enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location)
subprocess.call(backup_fstab, shell=True) if persistence_entry is True:
subprocess.call(enable_persistence, shell=True) print("Swap is already persistent!")
print("Swap is now persistent!") else:
subprocess.call(backup_fstab, shell=True)
subprocess.call(enable_persistence, shell=True)
print("Swap is now persistent!")
def _show_swapiness(self): def _show_swapiness(self):
get_swapiness = "cat /proc/sys/vm/swappiness" get_swapiness = "cat /proc/sys/vm/swappiness"
@ -127,3 +132,15 @@ class SwapModule(AbstractModule):
if not output_swapon: if not output_swapon:
print("Swap deleted!") print("Swap deleted!")
print(output_free.strip()) 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