From bcec589853a4e20c7a88e0d54454b73f0f098f9d Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Fri, 22 Dec 2023 15:39:44 +0100 Subject: [PATCH] Make sure we cache state from package managers as well To do this we start using cache/ and lib/ directories underneath the cache directory. We also fix our cleanup logic to only remove the relevant directories in case the cache directory is configured to be /var or similar. --- kernel-install/50-mkosi.install | 2 +- mkosi/__init__.py | 6 +++++- mkosi/installer/apt.py | 7 +++++-- mkosi/installer/dnf.py | 4 ++-- mkosi/installer/pacman.py | 7 +++---- mkosi/installer/zypper.py | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel-install/50-mkosi.install b/kernel-install/50-mkosi.install index 0f9e9e386..ca19fe48f 100644 --- a/kernel-install/50-mkosi.install +++ b/kernel-install/50-mkosi.install @@ -104,7 +104,7 @@ def main() -> None: "--format", str(format), "--output", output, "--workspace-dir=/var/tmp", - "--cache-dir=/var/cache", + "--cache-dir=/var", "--output-dir", context.staging_area, "--extra-tree", f"/usr/lib/modules/{context.kernel_version}:/usr/lib/modules/{context.kernel_version}", "--extra-tree=/usr/lib/firmware:/usr/lib/firmware", diff --git a/mkosi/__init__.py b/mkosi/__init__.py index c0f60dda2..9ed6b50fa 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -1921,7 +1921,11 @@ def unlink_output(args: MkosiArgs, config: MkosiConfig) -> None: if remove_package_cache: if config.cache_dir and config.cache_dir.exists() and any(config.cache_dir.iterdir()): with complete_step("Clearing out package cache…"): - empty_directory(config.cache_dir) + rmtree(*( + config.cache_dir / p / d + for p in ("cache", "lib") + for d in ("apt", "dnf", "libdnf5", "pacman", "zypp") + )) def cache_tree_paths(config: MkosiConfig) -> tuple[Path, Path, Path]: diff --git a/mkosi/installer/apt.py b/mkosi/installer/apt.py index 9ccf001e2..e14fc95c5 100644 --- a/mkosi/installer/apt.py +++ b/mkosi/installer/apt.py @@ -20,6 +20,9 @@ def setup_apt(state: MkosiState, repos: Sequence[str]) -> None: (state.root / "var/lib/dpkg").mkdir(parents=True, exist_ok=True) (state.root / "var/lib/dpkg/status").touch() + (state.cache_dir / "lib/apt").mkdir(exist_ok=True, parents=True) + (state.cache_dir / "cache/apt").mkdir(exist_ok=True, parents=True) + # We have a special apt.conf outside of pkgmngr dir that only configures "Dir::Etc" that we pass to APT_CONFIG to # tell apt it should read config files from /etc/apt in case this is overridden by distributions. This is required # because apt parses CLI configuration options after parsing its configuration files and as such we can't use CLI @@ -65,8 +68,8 @@ def apt_cmd(state: MkosiState, command: str) -> list[PathString]: "-o", "APT::Get::Allow-Change-Held-Packages=true", "-o", "APT::Get::Allow-Remove-Essential=true", "-o", "APT::Sandbox::User=root", - "-o", f"Dir::Cache={state.cache_dir / 'apt'}", - "-o", f"Dir::State={state.cache_dir / 'apt'}", + "-o", f"Dir::Cache={state.cache_dir / 'cache/apt'}", + "-o", f"Dir::State={state.cache_dir / 'lib/apt'}", "-o", f"Dir::State::Status={state.root / 'var/lib/dpkg/status'}", "-o", f"Dir::Etc::Trusted={trustedkeys}", "-o", f"Dir::Log={state.workspace}", diff --git a/mkosi/installer/dnf.py b/mkosi/installer/dnf.py index 90aa8f2b1..58d609f02 100644 --- a/mkosi/installer/dnf.py +++ b/mkosi/installer/dnf.py @@ -76,7 +76,8 @@ def dnf_cmd(state: MkosiState) -> list[PathString]: f"--releasever={state.config.release}", f"--installroot={state.root}", "--setopt=keepcache=1", - f"--setopt=cachedir={state.cache_dir / ('libdnf5' if dnf.endswith('dnf5') else 'dnf')}", + f"--setopt=cachedir={state.cache_dir / 'cache' / ('libdnf5' if dnf.endswith('dnf5') else 'dnf')}", + f"--setopt=persistdir={state.cache_dir / 'lib' / ('libdnf5' if dnf.endswith('dnf5') else 'dnf')}", f"--setopt=install_weak_deps={int(state.config.with_recommends)}", "--setopt=check_config_file_age=0", "--disable-plugin=*" if dnf.endswith("dnf5") else "--disableplugin=*", @@ -107,7 +108,6 @@ def dnf_cmd(state: MkosiState) -> list[PathString]: "--config=/etc/dnf/dnf.conf", "--setopt=reposdir=/etc/yum.repos.d", "--setopt=varsdir=/etc/dnf/vars", - "--setopt=persistdir=/var/lib/dnf", ] return cmdline diff --git a/mkosi/installer/pacman.py b/mkosi/installer/pacman.py index a822e8a50..f0bbe30d6 100644 --- a/mkosi/installer/pacman.py +++ b/mkosi/installer/pacman.py @@ -26,6 +26,8 @@ def setup_pacman(state: MkosiState, repositories: Iterable[PacmanRepository]) -> with umask(~0o755): (state.root / "var/lib/pacman").mkdir(exist_ok=True, parents=True) + (state.cache_dir / "cache/pacman/pkg").mkdir(parents=True, exist_ok=True) + config = state.pkgmngr / "etc/pacman.conf" if config.exists(): return @@ -67,14 +69,11 @@ def setup_pacman(state: MkosiState, repositories: Iterable[PacmanRepository]) -> def pacman_cmd(state: MkosiState) -> list[PathString]: - with umask(~0o755): - (state.cache_dir / "pacman/pkg").mkdir(parents=True, exist_ok=True) - return [ "pacman", "--root", state.root, "--logfile=/dev/null", - "--cachedir", state.cache_dir / "pacman/pkg", + "--cachedir", state.cache_dir / "cache/pacman/pkg", "--hookdir", state.root / "etc/pacman.d/hooks", "--arch", state.config.distribution.architecture(state.config.architecture), "--color", "auto", diff --git a/mkosi/installer/zypper.py b/mkosi/installer/zypper.py index c5b684cc2..7e2626bd4 100644 --- a/mkosi/installer/zypper.py +++ b/mkosi/installer/zypper.py @@ -61,7 +61,7 @@ def zypper_cmd(state: MkosiState) -> list[PathString]: "HOME=/", "zypper", f"--installroot={state.root}", - f"--cache-dir={state.cache_dir / 'zypp'}", + f"--cache-dir={state.cache_dir / 'cache/zypp'}", "--gpg-auto-import-keys" if state.config.repository_key_check else "--no-gpg-checks", "--non-interactive", ] -- 2.47.2