Implement manual partition management (create/delete/mountpoints)

This commit is contained in:
2026-02-03 17:07:45 +01:00
parent efe25f3e11
commit 22e1fa8f62
2 changed files with 123 additions and 22 deletions

View File

@@ -99,3 +99,42 @@ def mount_partitions(partition_info, mount_root="/mnt"):
run_command(["swapon", partition_info["swap"]])
logger.info(f"Partitions mounted at {mount_root}")
def create_partition(disk_device, size_mb, type_code="8300", name="Linux filesystem"):
"""
Creates a new partition on the disk.
type_code: ef00 (EFI), 8200 (Swap), 8300 (Linux)
"""
# Find next available partition number
res = run_command(["sgdisk", "-p", disk_device])
# Very basic parsing to find the next number
existing_nums = []
for line in res.stdout.splitlines():
parts = line.split()
if parts and parts[0].isdigit():
existing_nums.append(int(parts[0]))
next_num = 1
while next_num in existing_nums:
next_num += 1
run_command([
"sgdisk",
"-n", f"{next_num}:0:+{size_mb}M",
"-t", f"{next_num}:{type_code}",
"-c", f"{next_num}:{name}",
disk_device
])
run_command(["partprobe", disk_device])
return next_num
def delete_partition(disk_device, part_num):
"""Deletes a partition by number."""
run_command(["sgdisk", "-d", str(part_num), disk_device])
run_command(["partprobe", disk_device])
def wipe_disk(disk_device):
"""Zaps the disk and creates a new GPT table."""
run_command(["sgdisk", "-Z", disk_device])
run_command(["sgdisk", "-o", disk_device])
run_command(["partprobe", disk_device])