inline swap_file member as swap_location
This commit is contained in:
parent
59050e0665
commit
9636e817cb
@ -7,16 +7,14 @@ class SwapModule(AbstractModule):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.swap_file = None
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|
||||||
actions = {
|
actions = {
|
||||||
"Create/Resize temp. swap": self._create_resize_swap,
|
"Create/Resize temp. swap": self._create_resize_swap,
|
||||||
"Create/Resize persistent swap": self._create_resize_persistent_swap,
|
"Create/Resize persistent swap": self._create_resize_persistent_swap,
|
||||||
"Delete swap": self._delete_swap,
|
"Delete swap": self._delete_swap,
|
||||||
"Make temp. swap persistent": self._make_swap_persistent,
|
"Make temp. swap persistent": self._make_swap_persistent,
|
||||||
"Get swap location": self._swap_location_check,
|
"Get swap location": self._get_swap_location,
|
||||||
"Get swap size": self._get_swap_size,
|
"Get swap size": self._get_swap_size,
|
||||||
"Show swapiness": self._show_swapiness,
|
"Show swapiness": self._show_swapiness,
|
||||||
"Adjust temp. swapiness": self._adjust_swapiness_temp
|
"Adjust temp. swapiness": self._adjust_swapiness_temp
|
||||||
@ -36,14 +34,15 @@ class SwapModule(AbstractModule):
|
|||||||
self._create_resize_swap()
|
self._create_resize_swap()
|
||||||
self._make_swap_persistent()
|
self._make_swap_persistent()
|
||||||
|
|
||||||
def _swap_location_check(self):
|
def _get_swap_location(self):
|
||||||
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")
|
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")
|
||||||
try:
|
try:
|
||||||
swap_location = output_swaps.split()[5]
|
swap_location = output_swaps.split()[5]
|
||||||
print("Swap is located here:", swap_location)
|
print("Swap is located here:", swap_location)
|
||||||
self.swap_file = swap_location
|
return swap_location
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print("Swap file doesn´t exist!")
|
print("Swap file doesn´t exist!")
|
||||||
|
return None
|
||||||
|
|
||||||
def _get_swap_size(self):
|
def _get_swap_size(self):
|
||||||
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
||||||
@ -82,9 +81,12 @@ class SwapModule(AbstractModule):
|
|||||||
print(output_free.strip())
|
print(output_free.strip())
|
||||||
|
|
||||||
def _make_swap_persistent(self):
|
def _make_swap_persistent(self):
|
||||||
self._swap_location_check()
|
swap_location = self._get_swap_location()
|
||||||
|
if swap_location is None:
|
||||||
|
print("Swap file doesn't exist!")
|
||||||
|
return
|
||||||
backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak"
|
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)
|
enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location)
|
||||||
subprocess.call(backup_fstab, shell=True)
|
subprocess.call(backup_fstab, shell=True)
|
||||||
subprocess.call(enable_persistence, shell=True)
|
subprocess.call(enable_persistence, shell=True)
|
||||||
print("Swap is now persistent!")
|
print("Swap is now persistent!")
|
||||||
@ -114,12 +116,11 @@ class SwapModule(AbstractModule):
|
|||||||
print("Temporary swapiness is " + str(options[selected_swapiness]))
|
print("Temporary swapiness is " + str(options[selected_swapiness]))
|
||||||
|
|
||||||
def _delete_swap(self):
|
def _delete_swap(self):
|
||||||
self._swap_location_check()
|
swap_location = self._get_swap_location()
|
||||||
if self.swap_file is None:
|
if swap_location is None:
|
||||||
return
|
return
|
||||||
disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \
|
disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \
|
||||||
"sudo rm {}".format(self.swap_file)
|
"sudo rm {}".format(swap_location)
|
||||||
|
|
||||||
subprocess.call(disable_swapfile, shell=True)
|
subprocess.call(disable_swapfile, shell=True)
|
||||||
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
|
||||||
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
|
||||||
|
Loading…
Reference in New Issue
Block a user