Update partitioning.py

This commit is contained in:
2026-02-03 21:30:27 +01:00
parent 00ba6e5c89
commit bb31814233

View File

@@ -54,14 +54,7 @@ def get_total_memory() -> int:
return int(parts[1]) * 1024
return 0
def calculate_auto_partitions(disk_device):
"""
Generates an automatic partition layout.
- 2GB EFI (or 1GB if disk < required)
- Root (Rest)
- RAM + 2GB Swap (at end) (or 0 if disk < required)
"""
disk_size = 0
try:
# Get disk size in bytes
@@ -87,7 +80,7 @@ def get_total_memory() -> int:
return []
ram_size = get_total_memory()
disk_mb = disk_size / (1024*1024)
disk_mb = disk_size / (1024 * 1024)
# Defaults
efi_size = 2 * 1024 * 1024 * 1024
@@ -126,11 +119,12 @@ def get_total_memory() -> int:
"size": f"{root_size / (1024**3):.1f} GB",
"bytes": root_size,
"style_class": "part-root",
}
},
]
if use_swap:
partitions.append({
partitions.append(
{
"type": "partition",
"name": "Swap",
"filesystem": "swap",
@@ -138,7 +132,8 @@ def get_total_memory() -> int:
"size": f"{swap_size / (1024**3):.1f} GB",
"bytes": swap_size,
"style_class": "part-swap",
})
}
)
return partitions
@@ -464,22 +459,41 @@ class PartitioningPage(Adw.Bin):
menu_box.append(btn)
if data.get("type") == "partition":
add_menu_item("Select Mount Point", "folder-open-symbolic",
lambda: self.select_mount_point(data))
add_menu_item(
"Select Mount Point",
"folder-open-symbolic",
lambda: self.select_mount_point(data),
)
separator = Gtk.Separator()
menu_box.append(separator)
add_menu_item("Delete", "user-trash-symbolic",
lambda: self.delete_part(data), destructive=True)
add_menu_item(
"Delete",
"user-trash-symbolic",
lambda: self.delete_part(data),
destructive=True,
)
elif data.get("type") == "empty":
add_menu_item("Create Partition", "list-add-symbolic",
lambda: self.create_part_dialog(data))
add_menu_item(
"Create Partition",
"list-add-symbolic",
lambda: self.create_part_dialog(data),
)
popover.popup()
def select_mount_point(self, data):
win = Adw.Window(title="Select Mount Point", modal=True, transient_for=self.get_root())
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12, margin_top=24, margin_bottom=24, margin_start=24, margin_end=24)
win = Adw.Window(
title="Select Mount Point", modal=True, transient_for=self.get_root()
)
box = Gtk.Box(
orientation=Gtk.Orientation.VERTICAL,
spacing=12,
margin_top=24,
margin_bottom=24,
margin_start=24,
margin_end=24,
)
win.set_content(box)
options = ["/", "/boot/efi", "[SWAP]", "None"]
@@ -511,9 +525,11 @@ class PartitioningPage(Adw.Bin):
win.present()
def delete_part(self, data):
from ...backend.disk import delete_partition
# Extract partition number from name (e.g., nvme0n1p3 -> 3)
import re
from ...backend.disk import delete_partition
match = re.search(r"(\d+)$", data["name"])
if match:
part_num = int(match.group(1))
@@ -521,8 +537,17 @@ class PartitioningPage(Adw.Bin):
self.load_partitions(self.current_disk_path)
def create_part_dialog(self, data):
win = Adw.Window(title="Create Partition", modal=True, transient_for=self.get_root())
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12, margin_top=24, margin_bottom=24, margin_start=24, margin_end=24)
win = Adw.Window(
title="Create Partition", modal=True, transient_for=self.get_root()
)
box = Gtk.Box(
orientation=Gtk.Orientation.VERTICAL,
spacing=12,
margin_top=24,
margin_bottom=24,
margin_start=24,
margin_end=24,
)
win.set_content(box)
# Type dropdown
@@ -534,7 +559,7 @@ class PartitioningPage(Adw.Bin):
# Size entry
size_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=12)
size_entry = Gtk.Entry(text=str(int(data["bytes"] / (1024*1024))))
size_entry = Gtk.Entry(text=str(int(data["bytes"] / (1024 * 1024))))
size_entry.set_width_chars(15)
size_box.append(size_entry)
size_box.append(Gtk.Label(label="MB"))
@@ -547,6 +572,7 @@ class PartitioningPage(Adw.Bin):
def on_create(b):
from ...backend.disk import create_partition
selected_type = type_names[type_dropdown.get_selected()]
# Default name and fstype based on type
@@ -563,7 +589,7 @@ class PartitioningPage(Adw.Bin):
size_text = size_entry.get_text()
try:
size_mb = int(size_text)
max_mb = int(data["bytes"] / (1024*1024))
max_mb = int(data["bytes"] / (1024 * 1024))
if size_mb >= max_mb:
size_mb = 0
except ValueError:
@@ -573,7 +599,9 @@ class PartitioningPage(Adw.Bin):
type_code = types[selected_type]
try:
create_partition(self.current_disk_path, size_mb, type_code, name, fstype)
create_partition(
self.current_disk_path, size_mb, type_code, name, fstype
)
win.close()
self.load_partitions(self.current_disk_path)
except Exception as e: