logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>

target.py (1581B)


  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2023 fosslinux <fosslinux@aussies.space>
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. """
  5. Contains a class that represents a target directory
  6. """
  7. import enum
  8. import os
  9. from lib.utils import mount, create_disk
  10. class TargetType(enum.Enum):
  11. """Different types of target dirs we can have"""
  12. NONE = 0
  13. TMPFS = 1
  14. class Target:
  15. """
  16. Represents a target directory
  17. """
  18. _disks = {}
  19. _mountpoints = {}
  20. def __init__(self, path="target"):
  21. self.path = os.path.abspath(path)
  22. self._type = TargetType.NONE
  23. if not os.path.exists(self.path):
  24. os.mkdir(self.path)
  25. def tmpfs(self, size="8G"):
  26. """Mount a tmpfs"""
  27. print(f"Mounting tmpfs on {self.path}")
  28. mount("tmpfs", self.path, "tmpfs", f"size={size}")
  29. self._type = TargetType.TMPFS
  30. # pylint: disable=too-many-arguments,too-many-positional-arguments
  31. def add_disk(self,
  32. name,
  33. size="16G",
  34. filesystem="ext4",
  35. tabletype="msdos",
  36. bootable=False,
  37. mkfs_args=None):
  38. """Add a disk"""
  39. disk_path = os.path.join(self.path, f"{name}.img")
  40. create_disk(disk_path,
  41. tabletype,
  42. filesystem,
  43. size,
  44. bootable,
  45. mkfs_args)
  46. self._disks[name] = disk_path
  47. def get_disk(self, name):
  48. """Get the path to a device of a disk"""
  49. return self._disks.get(name)