2020-01-17 19:21:48 +01:00
|
|
|
|
import subprocess
|
|
|
|
|
from AbstractModule import AbstractModule
|
2020-01-18 04:15:08 +01:00
|
|
|
|
from PyInquirer import prompt
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
class SwapModule(AbstractModule):
|
|
|
|
|
|
2020-01-17 19:21:48 +01:00
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
2020-01-18 04:15:08 +01:00
|
|
|
|
self.swap_file = None
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
def run(self):
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-19 21:52:29 +01:00
|
|
|
|
actions = ["Create/Resize temp. swap", "Create/Resize persistent swap", "Delete swap",
|
|
|
|
|
"Make temp. swap persistent", "Get swap location", "Get swap size",
|
2020-01-20 14:58:24 +01:00
|
|
|
|
"Show swapiness", "Adjust temp. swapiness"]
|
2020-01-18 04:15:08 +01:00
|
|
|
|
menu = [
|
|
|
|
|
{
|
|
|
|
|
'type': 'list',
|
|
|
|
|
'message': 'Select action',
|
|
|
|
|
'name': 'action',
|
|
|
|
|
'choices': list(map(lambda x: {"name": x}, actions))
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
selected_action = prompt(menu)['action']
|
2020-01-19 21:52:29 +01:00
|
|
|
|
if selected_action == "Create/Resize temp. swap":
|
2020-01-18 04:15:08 +01:00
|
|
|
|
self.create_resize_swap()
|
2020-01-19 21:52:29 +01:00
|
|
|
|
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()
|
2020-01-19 21:03:38 +01:00
|
|
|
|
elif selected_action == "Delete":
|
2020-01-18 04:15:08 +01:00
|
|
|
|
self.delete_swap()
|
2020-01-19 21:03:38 +01:00
|
|
|
|
elif selected_action == "Get swap location":
|
2020-01-18 19:24:09 +01:00
|
|
|
|
self.swap_location_check()
|
2020-01-19 21:03:38 +01:00
|
|
|
|
elif selected_action == "Get swap size":
|
2020-01-18 19:24:09 +01:00
|
|
|
|
self.get_swap_size()
|
2020-01-20 14:58:24 +01:00
|
|
|
|
elif selected_action == "Adjust temp. swapiness":
|
|
|
|
|
self.adjust_swapiness_temp()
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
def swap_location_check(self) -> None:
|
|
|
|
|
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")
|
|
|
|
|
li = list(output_swaps.split())
|
|
|
|
|
final_list = li
|
|
|
|
|
try:
|
|
|
|
|
print("Swap is located here:", final_list[5])
|
|
|
|
|
self.swap_file = final_list[5]
|
|
|
|
|
except IndexError:
|
|
|
|
|
print("Swap file doesn´t exist!\n")
|
2020-01-18 19:24:09 +01:00
|
|
|
|
|
|
|
|
|
def get_swap_size(self) -> None:
|
|
|
|
|
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
|
|
|
|
li = list(swap_size.split())
|
|
|
|
|
final_list = li
|
|
|
|
|
try:
|
|
|
|
|
print("Swap is {}b big!".format(final_list[7]))
|
|
|
|
|
except IndexError:
|
|
|
|
|
print("Swap file doesn´t exist!")
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
def create_resize_swap(self):
|
|
|
|
|
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
|
|
|
|
if not output_swapon:
|
|
|
|
|
size = input("How big should the swap be(numbers in Gigabyte)? ")
|
|
|
|
|
create_swapfile = """sudo fallocate -l {}G /swapfile &&
|
|
|
|
|
sudo chmod 600 /swapfile &&
|
|
|
|
|
sudo mkswap /swapfile &&
|
|
|
|
|
sudo swapon /swapfile""".format(size)
|
|
|
|
|
subprocess.call(create_swapfile, shell=True)
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
|
|
|
|
print(output_free.strip())
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
else:
|
2020-01-19 21:52:29 +01:00
|
|
|
|
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
|
|
|
|
li = list(swap_size.split())
|
|
|
|
|
final_list = li
|
2020-01-18 04:15:08 +01:00
|
|
|
|
print("")
|
|
|
|
|
print("Swap already installed! You can resize it!")
|
2020-01-19 21:52:29 +01:00
|
|
|
|
print("Curr. swap size: {}b".format(final_list[7]))
|
2020-01-18 04:15:08 +01:00
|
|
|
|
resize = input("How big should your swap become(numbers in Gigabyte)? ")
|
|
|
|
|
resize_swapfile = """sudo swapoff /swapfile &&
|
|
|
|
|
sudo fallocate -l {}G /swapfile &&
|
|
|
|
|
sudo mkswap /swapfile &&
|
|
|
|
|
sudo swapon /swapfile""".format(resize)
|
|
|
|
|
subprocess.call(resize_swapfile, shell=True)
|
|
|
|
|
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
|
|
|
|
print(output_free.strip())
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-19 21:52:29 +01:00
|
|
|
|
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)
|
|
|
|
|
|
2020-01-20 14:58:24 +01:00
|
|
|
|
def adjust_swapiness_temp(self):
|
|
|
|
|
actions = ["Light", "Default", "Aggressive"]
|
|
|
|
|
menu = [
|
|
|
|
|
{
|
|
|
|
|
'type': 'list',
|
|
|
|
|
'message': 'Select action',
|
|
|
|
|
'name': 'action',
|
|
|
|
|
'choices': list(map(lambda x: {"name": x}, actions))
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
selected_swapiness = prompt(menu)['action']
|
|
|
|
|
|
|
|
|
|
if selected_swapiness == "Light":
|
|
|
|
|
adjust = "sudo sysctl vm.swappiness=25"
|
|
|
|
|
subprocess.call(adjust, shell=True)
|
|
|
|
|
print("Temporary swapiness is 25")
|
|
|
|
|
elif selected_swapiness == "Default":
|
|
|
|
|
adjust = "sudo sysctl vm.swappiness=60"
|
|
|
|
|
subprocess.call(adjust, shell=True)
|
|
|
|
|
print("Temporary swapiness is 60")
|
|
|
|
|
elif selected_swapiness == "Aggressive":
|
|
|
|
|
adjust = "sudo sysctl vm.swappiness=100"
|
|
|
|
|
subprocess.call(adjust, shell=True)
|
|
|
|
|
print("Temporary swapiness is 100")
|
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
def delete_swap(self):
|
|
|
|
|
self.swap_location_check()
|
|
|
|
|
disable_swapfile = """sudo swapoff {0} &&
|
|
|
|
|
sudo rm {0}""".format(self.swap_file)
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-18 04:15:08 +01:00
|
|
|
|
subprocess.call(disable_swapfile, shell=True)
|
|
|
|
|
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
2020-01-17 19:21:48 +01:00
|
|
|
|
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
2020-01-18 04:15:08 +01:00
|
|
|
|
if not output_swapon:
|
|
|
|
|
print("Swap deleted!")
|
|
|
|
|
print(output_free.strip())
|