linux-tools/modules/swap/module.py
Lucas Noki 526cfda6a8 Add get swap size option, Add get swap location option
Remove unnecessary comments
Correct size output
2020-01-19 15:24:18 +01:00

88 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import subprocess
from AbstractModule import AbstractModule
from PyInquirer import prompt
class SwapModule(AbstractModule):
def __init__(self):
super().__init__()
self.swap_file = None
def run(self):
actions = ["Create/Resize", "Delete", "Get location", "Get swap size"]
menu = [
{
'type': 'list',
'message': 'Select action',
'name': 'action',
'choices': list(map(lambda x: {"name": x}, actions))
}
]
selected_action = prompt(menu)['action']
if selected_action is "Create/Resize":
self.create_resize_swap()
elif selected_action is "Delete":
self.delete_swap()
elif selected_action is "Get location":
self.swap_location_check()
elif selected_action is "Get swap size":
self.get_swap_size()
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")
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!")
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)
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
print(output_free.strip())
else:
print("")
print("Swap already installed! You can resize it!")
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())
def delete_swap(self):
self.swap_location_check()
disable_swapfile = """sudo swapoff {0} &&
sudo rm {0}""".format(self.swap_file)
subprocess.call(disable_swapfile, shell=True)
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
if not output_swapon:
print("Swap deleted!")
print(output_free.strip())