refactor the format-disk script

This commit is contained in:
Andreas Zweili 2021-12-23 11:22:42 +01:00
parent 423d55042d
commit 0aa36e9c7f

View File

@ -5,13 +5,13 @@ import subprocess
import sys import sys
def _run_command(command, input=""): def _run_command(command, user_input=""):
if input: if user_input:
result = subprocess.run(command, result = subprocess.run(command,
capture_output=True, capture_output=True,
text=True, text=True,
check=True, check=True,
input=input) input=user_input)
else: else:
result = subprocess.run(command, result = subprocess.run(command,
capture_output=True, capture_output=True,
@ -32,7 +32,7 @@ def _y_n(question):
return True return True
if answer.lower() == "n": if answer.lower() == "n":
return False return False
print("Please only anwser with Y or N!") print("Please only answer with Y or N!")
sys.exit(1) sys.exit(1)
@ -47,8 +47,8 @@ def read_disks():
def create_menu(disks): def create_menu(disks):
for id, disk in enumerate(disks): for position, disk in enumerate(disks):
print("{}: {}".format(id, disk)) print("{}: {}".format(position, disk))
def get_disk_to_format(): def get_disk_to_format():
@ -100,13 +100,12 @@ def _create_swap():
_run_command(["mkswap", "-L", "swap", "/dev/MainGroup/swap"]) _run_command(["mkswap", "-L", "swap", "/dev/MainGroup/swap"])
def _encrypt_disk(partition_path, container_name): def _encrypt_disk(partition_path):
password = getpass.getpass() password = getpass.getpass()
print("Encrypting disk.") print("Encrypting disk.")
_run_command(["cryptsetup", "luksFormat", "-q", _run_command(["cryptsetup", "luksFormat", "-q",
"--type", "luks1", partition_path], input=password) "--type", "luks1", partition_path], user_input=password)
_run_command(["cryptsetup", "open", partition_path, "cryptlvm"], _run_command(["cryptsetup", "open", partition_path, "cryptlvm"], user_input=password)
input=password)
def _setup_lvm(lvm_target): def _setup_lvm(lvm_target):
@ -124,10 +123,9 @@ def mount_partitions():
def create_file_systems(partition, swap, encryption): def create_file_systems(partition, swap, encryption):
print("Creating filesystems.") print("Creating filesystems.")
lvm_target = ""
if encryption: if encryption:
lvm_target = "/dev/mapper/cryptlvm" lvm_target = "/dev/mapper/cryptlvm"
_encrypt_disk(partition, lvm_target) _encrypt_disk(partition)
else: else:
lvm_target = partition lvm_target = partition
_setup_lvm(lvm_target) _setup_lvm(lvm_target)