Skip to content

Commit

Permalink
Add classmethod to convert filesystem directly to disk image
Browse files Browse the repository at this point in the history
  • Loading branch information
Zedeldi committed Oct 12, 2024
1 parent bba1700 commit e83f1ee
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions igelfs/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Disk:

def __init__(self, path: str | Path) -> None:
"""Initialize disk instance."""
self.path = path
self.path = Path(path).absolute()

def allocate(self, size: int, zero: bool = False) -> None:
"""Create empty file of specified size."""
Expand All @@ -40,7 +40,7 @@ def partition(
- Partitions for each partition in IGEL filesystem
- Partition names matching partition_minor if lxos_config specified
"""
device = parted.getDevice(self.path)
device = parted.getDevice(self.path.as_posix())
disk = parted.freshDisk(device, "gpt")
for partition_minor in filesystem.partition_minors_by_directory:
sections = filesystem.find_sections_by_directory(partition_minor)
Expand Down Expand Up @@ -72,6 +72,20 @@ def write(self, filesystem: Filesystem) -> None:
with open(partition, "wb") as fd:
fd.write(payload)

@classmethod
def from_filesystem(
cls: type["Disk"],
path: str | Path,
filesystem: Filesystem,
lxos_config: LXOSParser | None = None,
) -> "Disk":
"""Convert filesystem to disk image and return Disk."""
disk = cls(path)
if not disk.path.exists():
disk.allocate(filesystem.size)
disk.partition(filesystem, lxos_config)
disk.write(filesystem)
return disk

@contextmanager
def loop_device(path: str | Path) -> Iterator[str]:
Expand Down

0 comments on commit e83f1ee

Please sign in to comment.