Automatic partition sizes

This commit is contained in:
2026-02-02 12:17:13 +01:00
parent f341fe0d60
commit 0f448bfa6c
2 changed files with 103 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import json
import os
import subprocess
import gi
@@ -41,6 +42,97 @@ CSS = """
"""
def get_total_memory() -> int:
"""Returns total system memory in bytes."""
try:
return os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
except (ValueError, OSError):
with open("/proc/meminfo", "r") as f:
for line in f:
if line.startswith("MemTotal:"):
parts = line.split()
return int(parts[1]) * 1024
return 0
def calculate_auto_partitions(disk_device):
"""
Generates an automatic partition layout.
- 2GB EFI
- RAM + 2GB Swap
- Rest Root (ext4)
"""
disk_size = 0
try:
# Get disk size in bytes
result = subprocess.run(
["lsblk", "-J", "-b", "-o", "NAME,SIZE,TYPE"],
capture_output=True,
text=True,
)
if result.returncode == 0:
data = json.loads(result.stdout)
devices = data.get("blockdevices", [])
dev_name = disk_device.replace("/dev/", "")
for dev in devices:
if dev.get("name") == dev_name:
disk_size = int(dev.get("size", 0))
break
except Exception as e:
print(f"Error getting disk size for auto partitioning: {e}")
return []
if disk_size == 0:
return []
ram_size = get_total_memory()
# Sizes in bytes
efi_size = 2 * 1024 * 1024 * 1024
swap_size = ram_size + (2 * 1024 * 1024 * 1024)
# Check if disk is large enough
min_root = 10 * 1024 * 1024 * 1024 # minimum 10GB for root
if disk_size < (efi_size + swap_size + min_root):
print("Disk too small for automatic partitioning scheme.")
return []
root_size = disk_size - efi_size - swap_size
partitions = [
{
"type": "partition",
"name": "EFI System",
"filesystem": "vfat",
"mount_point": "/boot/efi",
"size": f"{efi_size / (1024**3):.1f} GB",
"bytes": efi_size,
"style_class": "part-efi",
},
{
"type": "partition",
"name": "Swap",
"filesystem": "swap",
"mount_point": "[SWAP]",
"size": f"{swap_size / (1024**3):.1f} GB",
"bytes": swap_size,
"style_class": "part-swap",
},
{
"type": "partition",
"name": "Root",
"filesystem": "ext4",
"mount_point": "/",
"size": f"{root_size / (1024**3):.1f} GB",
"bytes": root_size,
"style_class": "part-root",
},
]
return partitions
class PartitionSegment(Gtk.Button):
def __init__(self, part_data, page, *args, **kwargs):
super().__init__(*args, **kwargs)