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 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,11 +83,15 @@ 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)
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!")
@ -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