2020-01-17 19:21:48 +01:00
|
|
|
|
import subprocess
|
2020-02-01 03:28:46 +01:00
|
|
|
|
from typing import List
|
|
|
|
|
|
2020-01-17 19:21:48 +01:00
|
|
|
|
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-02-01 03:28:46 +01:00
|
|
|
|
def _run_shell_command(cmd: List[str]) -> str:
|
|
|
|
|
return subprocess.check_output(cmd).decode("UTF-8").strip()
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
def run(self):
|
2020-01-20 22:42:01 +01:00
|
|
|
|
actions = {
|
2020-01-23 12:06:00 +01:00
|
|
|
|
"Create/Resize temp. swap": self._create_resize_swap,
|
|
|
|
|
"Create/Resize persistent swap": self._create_resize_persistent_swap,
|
|
|
|
|
"Delete swap": self._delete_swap,
|
|
|
|
|
"Make temp. swap persistent": self._make_swap_persistent,
|
2020-01-25 02:39:17 +01:00
|
|
|
|
"Get swap location": self._get_swap_location,
|
2020-01-23 12:06:00 +01:00
|
|
|
|
"Get swap size": self._get_swap_size,
|
|
|
|
|
"Show swapiness": self._show_swapiness,
|
2020-01-28 09:14:09 +01:00
|
|
|
|
"Adjust temp. swapiness": self._adjust_swapiness_temp,
|
2020-02-01 03:28:46 +01:00
|
|
|
|
"Check fstab for entry": self._fstab_entry_exists
|
2020-01-20 22:42:01 +01:00
|
|
|
|
}
|
2020-01-18 04:15:08 +01:00
|
|
|
|
menu = [
|
|
|
|
|
{
|
|
|
|
|
'type': 'list',
|
|
|
|
|
'message': 'Select action',
|
|
|
|
|
'name': 'action',
|
2020-01-20 22:42:01 +01:00
|
|
|
|
'choices': list(map(lambda x: {"name": x}, actions.keys()))
|
2020-01-18 04:15:08 +01:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
selected_action = prompt(menu)['action']
|
2020-01-20 22:42:01 +01:00
|
|
|
|
actions[selected_action]()
|
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _create_resize_persistent_swap(self):
|
|
|
|
|
self._create_resize_swap()
|
|
|
|
|
self._make_swap_persistent()
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-25 02:39:17 +01:00
|
|
|
|
def _get_swap_location(self):
|
2020-02-01 03:28:46 +01:00
|
|
|
|
output_swaps = _run_shell_command(["cat", "/proc/swaps"])
|
2020-01-18 04:15:08 +01:00
|
|
|
|
try:
|
2020-01-25 02:38:12 +01:00
|
|
|
|
swap_location = output_swaps.split()[5]
|
2020-01-25 01:01:37 +01:00
|
|
|
|
print("Swap is located here:", swap_location)
|
2020-01-25 02:39:17 +01:00
|
|
|
|
return swap_location
|
2020-01-18 04:15:08 +01:00
|
|
|
|
except IndexError:
|
2020-01-21 23:58:34 +01:00
|
|
|
|
print("Swap file doesn´t exist!")
|
2020-01-25 02:39:17 +01:00
|
|
|
|
return None
|
2020-01-18 19:24:09 +01:00
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _get_swap_size(self):
|
2020-02-01 03:28:46 +01:00
|
|
|
|
swap_size = _run_shell_command(['swapon', '--show'])
|
2020-01-18 19:24:09 +01:00
|
|
|
|
try:
|
2020-01-25 02:38:12 +01:00
|
|
|
|
swap_size = swap_size.split()[7]
|
2020-01-25 01:01:37 +01:00
|
|
|
|
print("Swap is {}b big!".format(swap_size))
|
2020-01-18 19:24:09 +01:00
|
|
|
|
except IndexError:
|
|
|
|
|
print("Swap file doesn´t exist!")
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _create_resize_swap(self):
|
2020-02-01 03:28:46 +01:00
|
|
|
|
output_swapon = _run_shell_command(['swapon', '--show'])
|
|
|
|
|
if output_swapon is not "":
|
|
|
|
|
swap_size = output_swapon.split()[7]
|
2020-01-18 04:15:08 +01:00
|
|
|
|
print("")
|
|
|
|
|
print("Swap already installed! You can resize it!")
|
2020-01-25 01:01:37 +01:00
|
|
|
|
print("Curr. swap size: {}b".format(swap_size))
|
2020-01-25 02:38:41 +01:00
|
|
|
|
resize = input("How big should your swap become (numbers in Gigabyte)? ")
|
2020-01-21 23:26:57 +01:00
|
|
|
|
resize_swapfile = "sudo swapoff /swapfile && " + \
|
|
|
|
|
"sudo fallocate -l {}G /swapfile && ".format(resize) + \
|
|
|
|
|
"sudo mkswap /swapfile && " + \
|
|
|
|
|
"sudo swapon /swapfile"
|
2020-01-18 04:15:08 +01:00
|
|
|
|
subprocess.call(resize_swapfile, shell=True)
|
2020-02-01 03:28:46 +01:00
|
|
|
|
output_free = _run_shell_command(["free", "-h"])
|
2020-01-18 04:15:08 +01:00
|
|
|
|
print(output_free.strip())
|
2020-01-17 19:21:48 +01:00
|
|
|
|
|
2020-01-25 02:10:12 +01:00
|
|
|
|
else:
|
|
|
|
|
size = input("How big should the swap be (numbers in Gigabyte)? ")
|
|
|
|
|
create_swapfile = "sudo fallocate -l {}G /swapfile && ".format(size) + \
|
|
|
|
|
"sudo chmod 600 /swapfile && " + \
|
|
|
|
|
"sudo mkswap /swapfile && " + \
|
|
|
|
|
"sudo swapon /swapfile"
|
|
|
|
|
subprocess.call(create_swapfile, shell=True)
|
|
|
|
|
|
2020-02-01 03:28:46 +01:00
|
|
|
|
output_free = _run_shell_command(["free", "-h"])
|
2020-01-25 02:10:12 +01:00
|
|
|
|
print(output_free.strip())
|
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _make_swap_persistent(self):
|
2020-01-25 02:39:17 +01:00
|
|
|
|
swap_location = self._get_swap_location()
|
|
|
|
|
if swap_location is None:
|
|
|
|
|
print("Swap file doesn't exist!")
|
|
|
|
|
return
|
2020-01-19 21:52:29 +01:00
|
|
|
|
backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak"
|
2020-01-25 02:39:17 +01:00
|
|
|
|
enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location)
|
2020-02-01 03:28:46 +01:00
|
|
|
|
if self._fstab_entry_exists():
|
2020-01-28 09:14:09 +01:00
|
|
|
|
print("Swap is already persistent!")
|
|
|
|
|
else:
|
|
|
|
|
subprocess.call(backup_fstab, shell=True)
|
|
|
|
|
subprocess.call(enable_persistence, shell=True)
|
|
|
|
|
print("Swap is now persistent!")
|
2020-01-19 21:52:29 +01:00
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _show_swapiness(self):
|
2020-02-01 03:28:46 +01:00
|
|
|
|
print(_run_shell_command(["cat", "/proc/sys/vm/swappiness"]))
|
2020-01-19 21:52:29 +01:00
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _adjust_swapiness_temp(self):
|
2020-01-25 01:02:59 +01:00
|
|
|
|
options = {
|
2020-01-20 22:33:50 +01:00
|
|
|
|
"Light": 25,
|
|
|
|
|
"Default": 60,
|
|
|
|
|
"Aggressive": 100
|
|
|
|
|
}
|
2020-01-20 14:58:24 +01:00
|
|
|
|
menu = [
|
|
|
|
|
{
|
|
|
|
|
'type': 'list',
|
|
|
|
|
'message': 'Select action',
|
|
|
|
|
'name': 'action',
|
2020-01-25 01:02:59 +01:00
|
|
|
|
'choices': list(map(lambda x: {"name": x}, options.keys()))
|
2020-01-20 14:58:24 +01:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
selected_swapiness = prompt(menu)['action']
|
|
|
|
|
|
2020-01-25 01:02:59 +01:00
|
|
|
|
adjust = "sudo sysctl vm.swappiness=" + str(options[selected_swapiness])
|
2020-01-20 22:33:50 +01:00
|
|
|
|
subprocess.call(adjust, shell=True)
|
2020-01-25 01:02:59 +01:00
|
|
|
|
print("Temporary swapiness is " + str(options[selected_swapiness]))
|
2020-01-20 14:58:24 +01:00
|
|
|
|
|
2020-01-23 12:06:00 +01:00
|
|
|
|
def _delete_swap(self):
|
2020-01-25 02:39:17 +01:00
|
|
|
|
swap_location = self._get_swap_location()
|
|
|
|
|
if swap_location is None:
|
2020-01-25 01:03:30 +01:00
|
|
|
|
return
|
2020-01-25 02:39:17 +01:00
|
|
|
|
disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \
|
|
|
|
|
"sudo rm {}".format(swap_location)
|
2020-02-01 03:28:46 +01:00
|
|
|
|
if self._fstab_entry_exists():
|
2020-01-29 08:52:50 +01:00
|
|
|
|
with open("/etc/fstab", "r") as fstab_out:
|
|
|
|
|
content = fstab_out.readlines()
|
|
|
|
|
with open("/etc/fstab", "w") as fstab_in:
|
|
|
|
|
content = content[:-1]
|
|
|
|
|
for line in content:
|
|
|
|
|
fstab_in.write(line)
|
|
|
|
|
else:
|
|
|
|
|
print("No entry in /etc/fstab!")
|
2020-01-25 02:12:10 +01:00
|
|
|
|
subprocess.call(disable_swapfile, shell=True)
|
2020-02-01 03:28:46 +01:00
|
|
|
|
output_swapon = _run_shell_command(['swapon', '--show'])
|
|
|
|
|
output_free = _run_shell_command(["free", "-h"])
|
2020-01-25 02:12:10 +01:00
|
|
|
|
if not output_swapon:
|
|
|
|
|
print("Swap deleted!")
|
|
|
|
|
print(output_free.strip())
|
2020-01-28 09:14:09 +01:00
|
|
|
|
|
2020-02-01 03:28:46 +01:00
|
|
|
|
def _fstab_entry_exists(self):
|
2020-01-28 09:14:09 +01:00
|
|
|
|
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
|