nixos/scripts/format-disk.py

100 lines
2.5 KiB
Python
Raw Normal View History

2021-12-19 18:41:03 +01:00
#!/usr/bin/env python3
2021-12-19 18:58:31 +01:00
import os
2021-12-19 18:41:03 +01:00
import subprocess
2021-12-19 19:15:01 +01:00
import sys
2021-12-19 18:41:03 +01:00
def _run_command(command):
result = subprocess.run(command,
capture_output=True,
text=True,
check=True)
return result
2021-12-19 18:58:31 +01:00
def _get_system_memory():
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
mem_gib = mem_bytes / (1024.**3)
return round(mem_gib)
2021-12-19 19:14:23 +01:00
def _y_n(question):
answer = input("{} (Y/N): ".format(question))
if answer.lower() == "y":
return True
if answer.lower() == "n":
return False
print("Please only anwser with Y or N!")
sys.exit(1)
2021-12-19 18:47:36 +01:00
def read_disks():
2021-12-19 18:41:03 +01:00
output = _run_command(["lsblk", "-dpno", "name"])
disks = []
for disk in output.stdout.splitlines():
if "loop" in disk:
continue
disks.append(disk)
return disks
2021-12-19 18:47:36 +01:00
def create_menu(disks):
2021-12-19 18:41:03 +01:00
for id, disk in enumerate(disks):
print("{}: {}".format(id, disk))
2021-12-19 18:47:36 +01:00
def get_disk_to_format():
2021-12-19 18:41:03 +01:00
disk_to_format = input("Which disk dou you want to format?: ")
return int(disk_to_format)
2021-12-19 18:41:03 +01:00
def _create_partition_table(disk):
_run_command(["parted", disk, "--", "mklabel", "gpt"])
def _partition_suffix(disk):
if "nvmne" in disk:
return "p"
return ""
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",
"ESP", "fat32", "1MiB", "512MiB"])
_run_command(["parted", disk, "--", "set", 1, "esp", "on"])
_run_command(["mkfs.fat", "-F", 32, "-n", "BOOT", boot_partition])
def _create_main_partition(disk):
2021-12-19 19:15:51 +01:00
print("Create main partition")
_run_command(["parted", disk, "--", "mkpart", "primary", "512MiB", "100%"])
2021-12-19 18:41:03 +01:00
def _encrypt_disk():
2021-12-19 18:41:03 +01:00
print("Encrypting disk.")
pass
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)
if swap_partition:
memory = _get_system_memory()
print("Create swap partition of {} GiB in size".format(memory))
2021-12-19 18:41:03 +01:00
def main():
2021-12-19 18:47:36 +01:00
disks = read_disks()
2021-12-19 18:48:18 +01:00
create_menu(disks)
disk_to_format = disks[get_disk_to_format()]
2021-12-19 19:15:29 +01:00
swap_partition = _y_n("Do you need a swap partition?")
encryption = _y_n("Do you want to ecrypt your data?")
format_disk(disk_to_format, swap_partition, encryption)
2021-12-19 18:41:03 +01:00
if __name__ == "__main__":
main()