complete the format-disk script

This commit is contained in:
Andreas Zweili 2021-12-19 20:10:49 +01:00
parent 3ec32d711d
commit 9a34894861
1 changed files with 46 additions and 10 deletions

View File

@ -58,7 +58,7 @@ def _partition_suffix(disk):
return ""
def _create_boot_partition(disk):
def create_boot_partition(disk):
boot_partition = "{}{}1".format(disk, _partition_suffix(disk))
print("Create boot partition {}.".format(boot_partition))
_run_command(["parted", disk, "--", "mkpart",
@ -69,21 +69,55 @@ def _create_boot_partition(disk):
def _create_main_partition(disk):
print("Create main partition")
_run_command(["parted", disk, "--", "mkpart", "primary", "512MiB", "100%"])
_run_command(["lvcreate", "-l", "100%FREE", "MainGroup", "-n", "root"])
_run_command(["mkfs.ext4", "-L", "nixos", "/dev/MainGroup/root"])
def _encrypt_disk():
def _create_swap_partition(disk):
memory = _get_system_memory()
print("Create swap partition of {} GiB in size".format(memory))
_run_command(["lvcreate",
"-L",
"{}G".format(memory),
"MainGroup",
"-n",
"swap"])
_run_command(["mkswap", "-L", "swap", "/dev/MainGroup/swap"])
def _encrypt_disk(partition_path, container_name):
print("Encrypting disk.")
pass
_run_command(["cryptsetup", "luksFormat",
"--type", "luks1", partition_path])
_run_command(["cryptsetup", "open", partition_path, "cryptlvm"])
def format_disk(disk_to_format, swap_partition, encryption):
print("Formatting disk: {}.".format(disk_to_format))
_create_boot_partition(disk_to_format)
_create_main_partition(disk_to_format)
def _setup_lvm(lvm_target):
print("Set up LVM.")
_run_command(["pvcreate", lvm_target])
_run_command(["vgcreate", "MainGroup", lvm_target])
def mount_partitions():
_run_command(["mount", "/dev/MainGroup/root", "/mnt"])
os.mkdir("/mnt/boot")
_run_command(["mount", "/dev/disk/by-label/BOOT", "/mnt/boot"])
def format_disk(disk, swap_partition, encryption):
print("Formatting disk: {}.".format(disk))
_run_command(["parted", disk, "--", "mkpart", "primary", "512MiB", "100%"])
main_partition_path = "{}{}2".format(disk, _partition_suffix(disk))
lvm_target = ""
if encryption:
lvm_target = "/dev/mapper/cryptlvm"
_encrypt_disk(main_partition_path, lvm_target)
else:
lvm_target = main_partition_path
_setup_lvm(lvm_target)
if swap_partition:
memory = _get_system_memory()
print("Create swap partition of {} GiB in size".format(memory))
_create_swap_partition(disk)
_create_main_partition(disk)
def main():
@ -92,7 +126,9 @@ def main():
disk_to_format = disks[get_disk_to_format()]
swap_partition = _y_n("Do you need a swap partition?")
encryption = _y_n("Do you want to ecrypt your data?")
create_boot_partition(disk_to_format)
format_disk(disk_to_format, swap_partition, encryption)
mount_partitions()
if __name__ == "__main__":