Automatic partition sizes
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
import gi
|
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):
|
class PartitionSegment(Gtk.Button):
|
||||||
def __init__(self, part_data, page, *args, **kwargs):
|
def __init__(self, part_data, page, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ gi.require_version("Adw", "1")
|
|||||||
from gi.repository import Adw, Gtk
|
from gi.repository import Adw, Gtk
|
||||||
|
|
||||||
from .pages.install_mode import InstallModePage
|
from .pages.install_mode import InstallModePage
|
||||||
from .pages.partitioning import PartitioningPage
|
from .pages.partitioning import PartitioningPage, calculate_auto_partitions
|
||||||
from .pages.storage import StoragePage
|
from .pages.storage import StoragePage
|
||||||
from .pages.user import UserPage
|
from .pages.user import UserPage
|
||||||
from .pages.welcome import WelcomePage
|
from .pages.welcome import WelcomePage
|
||||||
@@ -137,11 +137,19 @@ class InstallerWindow(Adw.ApplicationWindow):
|
|||||||
|
|
||||||
if current_page_name == "user":
|
if current_page_name == "user":
|
||||||
print("Install process triggered!")
|
print("Install process triggered!")
|
||||||
print(f"Disk: {self.storage_page.get_selected_disk()}")
|
disk = self.storage_page.get_selected_disk()
|
||||||
|
print(f"Disk: {disk}")
|
||||||
mode = self.install_mode_page.get_mode()
|
mode = self.install_mode_page.get_mode()
|
||||||
print(f"Mode: {mode}")
|
print(f"Mode: {mode}")
|
||||||
|
|
||||||
|
partitions_config = {}
|
||||||
if mode == "manual":
|
if mode == "manual":
|
||||||
print(f"Partitioning: {self.partitioning_page.get_config()}")
|
partitions_config = self.partitioning_page.get_config()
|
||||||
|
elif mode == "automatic":
|
||||||
|
partitions = calculate_auto_partitions(disk)
|
||||||
|
partitions_config = {"partitions": partitions}
|
||||||
|
|
||||||
|
print(f"Partitioning: {partitions_config}")
|
||||||
return
|
return
|
||||||
|
|
||||||
if next_index < len(self.page_ids):
|
if next_index < len(self.page_ids):
|
||||||
|
|||||||
Reference in New Issue
Block a user