summaryrefslogtreecommitdiffstats
path: root/src/utils/disk.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/disk.py')
-rw-r--r--src/utils/disk.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/utils/disk.py b/src/utils/disk.py
index d7706fe..c424c61 100644
--- a/src/utils/disk.py
+++ b/src/utils/disk.py
@@ -10,6 +10,7 @@ import os
import logging
import shlex
import subprocess
+import json
from src.log import OgError
import fdisk
@@ -106,3 +107,45 @@ def get_filesystem_id(disk_index, part_index):
if proc.returncode != 0:
raise OgError(f'failed to query filesystem UUID for {device}')
return proc.stdout.strip()
+
+
+def get_sector_size(disk):
+ disk_index = disk - 1
+
+ if disk_index < 0 or disk_index >= len(get_disks()):
+ raise OgError(f'Invalid disk number {disk} when trying to find ESP, {len(get_disks())} disks available.')
+
+ device_name = get_disks()[disk_index]
+ file_path = f'/sys/class/block/{device_name}/queue/hw_sector_size'
+
+ try:
+ with open(file_path, 'r') as f:
+ data = f.read().strip()
+ except OSError as e:
+ raise OgError(f'Error while trying to read {file_path}: {e}') from e
+ return int(data)
+
+
+def get_partition_start_offset(disk, partition):
+ disk_name = get_disks()[disk - 1]
+ disk_path = f'/dev/{disk_name}'
+ part_number = partition - 1
+
+ cmd = f'sfdisk -J {disk_path}'
+ proc = subprocess.run(shlex.split(cmd), capture_output=True, text=True)
+
+ if proc.returncode != 0:
+ raise OgError(f'Failed to query sfdisk')
+
+ try:
+ part_data_json = json.loads(proc.stdout)
+ except json.JSONDecodeError as e:
+ raise OgError(f'Invalid sfdisk output: {e}') from e
+
+ try:
+ part_data = part_data_json['partitiontable']['partitions']
+ start_offset = part_data[part_number]['start']
+ except KeyError as e:
+ raise OgError(f'Error while trying to parse sfdisk: {e}') from e
+
+ return start_offset