Skip to content

Commit

Permalink
uses lsblk to determine device and partn
Browse files Browse the repository at this point in the history
The regex approach couldn't handle eMMC devices.
It was also very error prone and hard to read.
  • Loading branch information
taukakao committed Mar 29, 2024
1 parent 62b4a56 commit b1b14de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
18 changes: 18 additions & 0 deletions vanilla_installer/core/disks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import subprocess
import json

class Diskutils:
@staticmethod
Expand All @@ -12,6 +13,23 @@ def pretty_size(size: int) -> str:
return f"{round(size / 1024, 2)} KB"
else:
return f"{size} B"

@staticmethod
def separate_device_and_partn(part_dev: str) -> tuple[str, str|None]:
info_json = subprocess.check_output(
"lsblk --json -o NAME,PKNAME,PARTN " + part_dev, shell=True
).decode("utf-8")
info_multiple = json.loads(info_json)["blockdevices"]

if len(info_multiple) > 1:
raise ValueError(f'{part_dev} returned more than one device')
info = info_multiple[0]

if info["partn"] == None:
# part_dev is actually a device, not a partition
return "/dev/" + info["name"], None

return "/dev/" + info["pkname"], str(info["partn"])

class Disk:
def __init__(self, disk: str):
Expand Down
19 changes: 5 additions & 14 deletions vanilla_installer/utils/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from typing import Any, Union

from vanilla_installer.core.system import Systeminfo
from vanilla_installer.core.disks import Diskutils

logger = logging.getLogger("Installer::Processor")

Expand Down Expand Up @@ -326,10 +327,7 @@ def __gen_manual_partition_steps(
# we don't need to create any partitions or label disks (for now).
# But we still need to format partitions.
for part, values in disk_final.items():
disk_regex = r"^/dev/[a-zA-Z]+([0-9]+[a-z][0-9]+)?"
part_regex = r".*[a-z]([0-9]+)"
part_disk = re.match(disk_regex, part, re.MULTILINE)[0]
part_number = re.sub(part_regex, r"\1", part)
part_disk, part_number = Diskutils.separate_device_and_partn(part)

def setup_partition(
part_name: str, encrypt: bool = False, password: str = None
Expand All @@ -346,14 +344,9 @@ def setup_partition(
mountpoints.append([part, values["mp"]])

if values["mp"] == "/":
part_prefix = (
f"{part_disk}p"
if re.match(r"[0-9]", part_disk[-1])
else f"{part_disk}"
)
setup_steps.append([part_disk, "pvcreate", [part_prefix + part_number]])
setup_steps.append([part_disk, "pvcreate", [part]])
setup_steps.append(
[part_disk, "vgcreate", ["vos-root", [part_prefix + part_number]]]
[part_disk, "vgcreate", ["vos-root", [part]]]
)
setup_steps.append(
[part_disk, "lvcreate", ["init", "vos-root", "linear", 512]]
Expand Down Expand Up @@ -498,9 +491,7 @@ def gen_install_recipe(log_path, finals, sys_recipe):
root_b_part,
var_part,
) = Processor.__find_partitions(recipe)
boot_disk = re.match(
r"^/dev/[a-zA-Z]+([0-9]+[a-z][0-9]+)?", boot_part, re.MULTILINE
)[0]
boot_disk, _ = Diskutils.separate_device_and_partn(boot_part)

# Create SystemD units to setup mountpoints
extra_target = "cryptsetup" if encrypt else ""
Expand Down

0 comments on commit b1b14de

Please sign in to comment.