From e32d2f0e8e980aa40971716898c4a600cdb4ca66 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 15 Jan 2024 21:57:35 +0100 Subject: [PATCH] Mount package manager caches to their canonical locations Let's reduce the amount of configuration by mounting package manager caches to their canonical locations. --- mkosi/distributions/debian.py | 7 ++++++- mkosi/installer/__init__.py | 17 +++++++++-------- mkosi/installer/apt.py | 4 ++-- mkosi/installer/dnf.py | 4 ++-- mkosi/installer/pacman.py | 2 +- mkosi/installer/zypper.py | 2 +- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/mkosi/distributions/debian.py b/mkosi/distributions/debian.py index cd3b3d2e5..8c56722f9 100644 --- a/mkosi/distributions/debian.py +++ b/mkosi/distributions/debian.py @@ -136,7 +136,12 @@ class Installer(DistributionInstaller): # then extracting the tar file into the chroot. for deb in essential: - with open(deb, "rb") as i, tempfile.NamedTemporaryFile() as o: + with ( + # The deb paths will be in the form of "/var/cache/apt/" so we transform them to the corresponding + # path in mkosi's package cache directory. + open(context.cache_dir / Path(deb).relative_to("/var"), "rb") as i, + tempfile.NamedTemporaryFile() as o + ): run(["dpkg-deb", "--fsys-tarfile", "/dev/stdin"], stdin=i, stdout=o, sandbox=context.sandbox()) extract_tar(context, Path(o.name), context.root, log=False) diff --git a/mkosi/installer/__init__.py b/mkosi/installer/__init__.py index e4b99722b..52dcc150c 100644 --- a/mkosi/installer/__init__.py +++ b/mkosi/installer/__init__.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1+ import os +from pathlib import Path from mkosi.config import ConfigFeature from mkosi.context import Context @@ -72,16 +73,16 @@ def finalize_package_manager_mounts(context: Context) -> list[PathString]: ] mounts += flatten( - ["--bind", d, d] + ["--bind", context.cache_dir / d, Path("/var") / d] for d in ( - context.cache_dir / "lib/apt", - context.cache_dir / "cache/apt", - context.cache_dir / "cache" / dnf_subdir(context), - context.cache_dir / "lib" / dnf_subdir(context), - context.cache_dir / "cache/pacman/pkg", - context.cache_dir / "cache/zypp", + "lib/apt", + "cache/apt", + f"cache/{dnf_subdir(context)}", + f"lib/{dnf_subdir(context)}", + "cache/pacman/pkg", + "cache/zypp", ) - if d.exists() + if (context.cache_dir / d).exists() ) return mounts diff --git a/mkosi/installer/apt.py b/mkosi/installer/apt.py index 0996039fd..bc8298049 100644 --- a/mkosi/installer/apt.py +++ b/mkosi/installer/apt.py @@ -65,8 +65,8 @@ def apt_cmd(context: Context, 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={context.cache_dir / 'cache/apt'}", - "-o", f"Dir::State={context.cache_dir / 'lib/apt'}", + "-o", "Dir::Cache=/var/cache/apt", + "-o", "Dir::State=/var/lib/apt", "-o", f"Dir::State::Status={context.root / 'var/lib/dpkg/status'}", "-o", f"Dir::Log={context.workspace}", "-o", f"Dir::Bin::DPkg={find_binary('dpkg', root=context.config.tools())}", diff --git a/mkosi/installer/dnf.py b/mkosi/installer/dnf.py index 74d743411..86c15c47e 100644 --- a/mkosi/installer/dnf.py +++ b/mkosi/installer/dnf.py @@ -88,8 +88,8 @@ def dnf_cmd(context: Context) -> list[PathString]: f"--releasever={context.config.release}", f"--installroot={context.root}", "--setopt=keepcache=1", - f"--setopt=cachedir={context.cache_dir / 'cache' / dnf_subdir(context)}", - f"--setopt=persistdir={context.cache_dir / 'lib' / dnf_subdir(context)}", + f"--setopt=cachedir=/var/cache/{dnf_subdir(context)}", + f"--setopt=persistdir=/var/lib/{dnf_subdir(context)}", f"--setopt=install_weak_deps={int(context.config.with_recommends)}", "--setopt=check_config_file_age=0", "--disable-plugin=*" if dnf.endswith("dnf5") else "--disableplugin=*", diff --git a/mkosi/installer/pacman.py b/mkosi/installer/pacman.py index 08fa7e200..9028359ed 100644 --- a/mkosi/installer/pacman.py +++ b/mkosi/installer/pacman.py @@ -76,7 +76,7 @@ def pacman_cmd(context: Context) -> list[PathString]: "pacman", "--root", context.root, "--logfile=/dev/null", - "--cachedir", context.cache_dir / "cache/pacman/pkg", + "--cachedir=/var/cache/pacman/pkg", "--hookdir", context.root / "etc/pacman.d/hooks", "--arch", context.config.distribution.architecture(context.config.architecture), "--color", "auto", diff --git a/mkosi/installer/zypper.py b/mkosi/installer/zypper.py index 5af2a6977..708a71cd0 100644 --- a/mkosi/installer/zypper.py +++ b/mkosi/installer/zypper.py @@ -66,7 +66,7 @@ def zypper_cmd(context: Context) -> list[PathString]: "HOME=/", "zypper", f"--installroot={context.root}", - f"--cache-dir={context.cache_dir / 'cache/zypp'}", + "--cache-dir=/var/cache/zypp", "--gpg-auto-import-keys" if context.config.repository_key_check else "--no-gpg-checks", "--non-interactive", ] -- 2.47.3