Re: Using python-apt's apt_pkg with a chroot

Johannes Schauer Marin Rodrigues <[email protected]> Tue, 02 Dec 2025 19:17:24 +0100
Newsgroups gmane.linux.debian.apt.devel
Message-ID <176469944467.713870.12799479358962244059@localhost>
Hi,

Quoting Sebastiaan Couwenberg (2025-12-02 18:36:00)
> Using python-apt's apt_pkg with a chroot turns out to be non-trivial unlike apt.cache.
> 
> I'm running a script [0] on a trixie system and need to interact with the cache in a testing chroot.
> 
> The chroot is created with mmdebstrap --mode=unshare, and the script is run via unshare as well.

maybe this is an XY problem.

Why do you need a full chroot for something that is as far as I get from the
script just a "I want apt to resolve the dependencies with my custom
sources.list"? This sounds like something like chdist is doing?

Maybe the mmdebstrap man page has an example which helps you. It is a snippet
which is the very bare-bones of what mmdebstrap does with a few thousand more
lines of code:


    mkdir -p "$2/etc/apt" "$2/var/cache" "$2/var/lib"
    cat << END > "$2/apt.conf"
    Apt::Architecture "$(dpkg --print-architecture)";
    Apt::Architectures "$(dpkg --print-architecture)";
    Dir "$(cd "$2" && pwd)";
    Dir::Etc::Trusted "$(eval "$(apt-config shell v Dir::Etc::Trusted/f)"; printf %s "$v")";
    Dir::Etc::TrustedParts "$(eval "$(apt-config shell v Dir::Etc::TrustedParts/d)"; printf %s "$v"

    END
    echo "deb http://deb.debian.org/debian/ $1 main" > "$2/etc/apt/sources.list"
    APT_CONFIG="$2/apt.conf" apt-get update
    APT_CONFIG="$2/apt.conf" apt-get --yes --download-only install '?essential'
    for f in "$2"/var/cache/apt/archives/*.deb; do dpkg-deb --extract "$f" "$2"; done
    chroot "$2" sh -c "dpkg --install --force-depends /var/cache/apt/archives/*.deb"


> Setting RootDir in the config does not result in the chroot paths to be used:

I think you need Dir.

>   apt_pkg.Error: E:Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied), E:Unable to lock directory /var/lib/apt/lists/

This sounds like you are trying to access something created for the unshared
user as the normal user?

Thanks!

cheers, josch
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEElFhU6KL81LF4wVq58sulx4+9g+EFAmkvLS4ACgkQ8sulx4+9
g+GHnxAAlnTRIGHyEgs1wqIHh+gMbnyS1jF+dfCA6SC+wyzHurpVyWQUUC59nIuL
rBLpSRl1op+gXDpZNvACU3zbHnx+6RftAK5DtTpKjTZ8v5pfTkSqOQmcVQljnMVG
N/bf/BErF4mOhPI0iWY3+xQq0Cx503xfF+KinhKAuTNhLsBPKc8/Q28VWQuy/lQv
U8PI9wt3+A5QgFfG6rLPqbcIp/pDU0ma8hAakURK9Ec8zr2vQIurJTPZ6vsZbzcp
hPh8w78vGDjOpV6D79DBBFB6aqc6IIFuUrBoab3t0+WO2vid8bNL5RPD7c+9zqBp
tcG6MMMHG9mI2CITcqOziPlfIy7B121r4LWclUg76kyCYsyUlw0xYJy8rM/BCvZP
nUU6uOHWPhBFjX0cgm5IFAfvfZDttt/akhTVp7rJNfJ7180t7Ef/jbvRlm/EAmV3
p+vgkoD1iXm26ZK90uKgCfeLytVjTUKJ6H0lOEqHxgBr2r78LnkkXh5k1UGzl7qU
IBytWJZtliifh+Qz9wq3VhWr2qRMm5E/uIlVDxOI8rQMzDHrjM2Z8RUrw9uAH+Hv
E5/G1JXTcmMeDnCKE12+R/s+aE5E64BqmuJuzZZYnmIn4dyMC0I9FpMzRRBUAxgG
2I9vpy7lOx973aqiF10jZo2QlmqWhw8uynVXl+ShswTs2u7x9+8=
=t27t
-----END PGP SIGNATURE-----