Add functions to make swap persistent, add function to show swapiness

This commit is contained in:
Lucas Noki 2020-01-19 21:52:29 +01:00
parent aa48b95652
commit 68b2a8074c

View File

@ -11,7 +11,9 @@ class SwapModule(AbstractModule):
def run(self): def run(self):
actions = ["Create/Resize", "Delete", "Get swap location", "Get swap size"] actions = ["Create/Resize temp. swap", "Create/Resize persistent swap", "Delete swap",
"Make temp. swap persistent", "Get swap location", "Get swap size",
"Show swapiness"]
menu = [ menu = [
{ {
'type': 'list', 'type': 'list',
@ -21,8 +23,15 @@ class SwapModule(AbstractModule):
} }
] ]
selected_action = prompt(menu)['action'] selected_action = prompt(menu)['action']
if selected_action == "Create/Resize": if selected_action == "Create/Resize temp. swap":
self.create_resize_swap() self.create_resize_swap()
elif selected_action == "Create/Resize persistent swap":
self.create_resize_swap()
self.make_swap_persistent()
elif selected_action == "Make temp. swap persistent":
self.make_swap_persistent()
elif selected_action == "Show swapiness":
self.show_swapiness()
elif selected_action == "Delete": elif selected_action == "Delete":
self.delete_swap() self.delete_swap()
elif selected_action == "Get swap location": elif selected_action == "Get swap location":
@ -63,8 +72,12 @@ class SwapModule(AbstractModule):
print(output_free.strip()) print(output_free.strip())
else: else:
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
li = list(swap_size.split())
final_list = li
print("") print("")
print("Swap already installed! You can resize it!") print("Swap already installed! You can resize it!")
print("Curr. swap size: {}b".format(final_list[7]))
resize = input("How big should your swap become(numbers in Gigabyte)? ") resize = input("How big should your swap become(numbers in Gigabyte)? ")
resize_swapfile = """sudo swapoff /swapfile && resize_swapfile = """sudo swapoff /swapfile &&
sudo fallocate -l {}G /swapfile && sudo fallocate -l {}G /swapfile &&
@ -74,6 +87,20 @@ class SwapModule(AbstractModule):
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
print(output_free.strip()) print(output_free.strip())
def make_swap_persistent(self):
self.swap_location_check()
backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak"
enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file)
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"
subprocess.call(get_swapiness, shell=True)
def delete_swap(self): def delete_swap(self):
self.swap_location_check() self.swap_location_check()
disable_swapfile = """sudo swapoff {0} && disable_swapfile = """sudo swapoff {0} &&