@contextlib.contextmanager
def mount_cache(state: MkosiState) -> Iterator[None]:
- if state.installer is not None:
- cache_paths = state.installer.cache_path()
- else:
- cache_paths = []
+ cache_paths = state.installer.cache_path()
# We can't do this in mount_image() yet, as /var itself might have to be created as a subvolume first
with complete_step("Mounting Package Cache", "Unmounting Package Cache"), contextlib.ExitStack() as stack:
if cached:
return
- install: Callable[[MkosiState], None]
-
- if state.installer is not None:
- install = state.installer.install
- else:
- die("No Installer")
-
with mount_cache(state):
- install(state)
+ state.installer.install(state)
# Link /var/lib/rpm→/usr/lib/sysimage/rpm for compat with old rpm.
# We do this only if the new location is used, which depends on the dnf
if not state.config.remove_packages:
return
- remove: Callable[[List[str]], Any]
-
- if state.installer is not None:
- remove = lambda p: state.installer.remove_packages(state, p) # type: ignore
- else:
- die(f"Removing packages is not supported for {state.config.distribution}")
-
with complete_step(f"Removing {len(state.config.packages)} packages…"):
- remove(state.config.remove_packages)
+ try:
+ state.installer.remove_packages(state, state.config.remove_packages)
+ except NotImplementedError:
+ die(f"Removing packages is not supported for {state.config.distribution}")
def reset_machine_id(state: MkosiState) -> None:
if not kver.is_dir():
continue
- if state.installer is not None:
- kimg = state.installer.kernel_image(kver.name, state.config.architecture)
- else:
- kimg = Path("lib/modules") / kver.name / "vmlinuz"
+ kimg = state.installer.kernel_image(kver.name, state.config.architecture)
yield kver.name, kimg
machine_id: str
for_cache: bool
environment: Dict[str, str] = dataclasses.field(init=False)
+ installer: DistributionInstaller = dataclasses.field(init=False)
cache_pre_inst: Optional[Path] = None
cache_pre_dev: Optional[Path] = None
self.environment['IMAGE_ID'] = self.config.image_id
if self.config.image_version is not None:
self.environment['IMAGE_VERSION'] = self.config.image_version
+ try:
+ distro = str(self.config.distribution)
+ mod = importlib.import_module(f"mkosi.distributions.{distro}")
+ installer = getattr(mod, f"{distro.title().replace('_','')}Installer")
+ instance = installer() if issubclass(installer, DistributionInstaller) else None
+ except (ImportError, AttributeError):
+ instance = None
+ if instance is None:
+ die("No installer for this distribution.")
+ self.installer = instance
@property
def root(self) -> Path:
return None
return self.partition_table.partitions.get(ident)
- @property
- def installer(self) -> Optional["DistributionInstaller"]:
- try:
- # we do a getattr here because we wnt to use the missing attribute
- # as a sentinel, since we will use None as a sentinel further down
- # for the case of distros that are not migrated to this interface
- return cast(Optional["DistributionInstaller"], getattr(self, "_installer"))
- except AttributeError:
- try:
- distro = str(self.config.distribution)
- mod = importlib.import_module(f"mkosi.distributions.{distro}")
- installer = getattr(mod, f"{distro.title().replace('_','')}Installer")
- instance = installer() if issubclass(installer, DistributionInstaller) else None
- except (ImportError, AttributeError):
- instance = None
- setattr(self, "_installer", instance)
- return cast(Optional["DistributionInstaller"], instance)
-
def should_compress_fs(config: Union[argparse.Namespace, MkosiConfig]) -> Union[bool, str]:
"""True for the default compression, a string, or False.
@classmethod
def remove_packages(cls, state: "MkosiState", remove: List[str]) -> None:
- pass
+ raise NotImplementedError
if cached:
return
- if state.installer is not None:
- skeletons_after_bootstrap = state.installer.needs_skeletons_after_bootstrap
- else:
- skeletons_after_bootstrap = False
- if not late and skeletons_after_bootstrap:
+ if not late and state.installer.needs_skeletons_after_bootstrap:
return
with complete_step("Copying in skeleton file trees…"):