CLOVER🍀

That was when it all began.

現在認識しているaptリポジトリーの一覧や、リポジトリーに含まれているパッケージを調べたい

これは、なにをしたくて書いたもの?

現在認識しているaptリポジトリーや、特定のリポジトリーに含まれているパッケージを把握するにはどうしたらいいのかな?ということで、
ちょっと調べてみました。

環境

確認環境は、こちらです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal


$ uname -srvmpio
Linux 5.4.0-124-generic #140-Ubuntu SMP Thu Aug 4 02:23:37 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ apt --version
apt 2.0.9 (amd64)

現在認識しているaptリポジトリーの一覧を確認する

aptリポジトリーは、/etc/apt/sources.listファイルおよび/etc/apt/sources.list.dディレクトリ内のファイルに定義します。

Ubuntu Manpage: sources.list - List of configured APT data sources

これをgrepで検索すると良さそうです。

$ grep -r --include '*.list' '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d | perl -wp -e 's!.+(http.+)!$1!'
http://us.archive.ubuntu.com/ubuntu focal main restricted
http://us.archive.ubuntu.com/ubuntu focal-updates main restricted
http://us.archive.ubuntu.com/ubuntu focal universe
http://us.archive.ubuntu.com/ubuntu focal-updates universe
http://us.archive.ubuntu.com/ubuntu focal multiverse
http://us.archive.ubuntu.com/ubuntu focal-updates multiverse
http://us.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
http://security.ubuntu.com/ubuntu focal-security main restricted
http://security.ubuntu.com/ubuntu focal-security universe
http://security.ubuntu.com/ubuntu focal-security multiverse
https://download.docker.com/linux/ubuntu focal stable
https://packages.redis.io/deb focal main

または、apt-cache policyでもパッケージの取得元(=aptリポジトリー)が確認できるので、こちらを使用してもよいでしょう。

$ apt-cache policy | grep http | perl -wp -e 's!.+(https?://[^ ]+) ([^ ]+) [^ ]+.+!$1 $2!' | sort -u
http://security.ubuntu.com/ubuntu focal-security/main
http://security.ubuntu.com/ubuntu focal-security/multiverse
http://security.ubuntu.com/ubuntu focal-security/restricted
http://security.ubuntu.com/ubuntu focal-security/universe
http://us.archive.ubuntu.com/ubuntu focal-backports/main
http://us.archive.ubuntu.com/ubuntu focal-backports/universe
http://us.archive.ubuntu.com/ubuntu focal-updates/main
http://us.archive.ubuntu.com/ubuntu focal-updates/multiverse
http://us.archive.ubuntu.com/ubuntu focal-updates/restricted
http://us.archive.ubuntu.com/ubuntu focal-updates/universe
http://us.archive.ubuntu.com/ubuntu focal/main
http://us.archive.ubuntu.com/ubuntu focal/multiverse
http://us.archive.ubuntu.com/ubuntu focal/restricted
http://us.archive.ubuntu.com/ubuntu focal/universe
https://download.docker.com/linux/ubuntu focal/stable
https://packages.redis.io/deb focal/main

aptリポジトリーに含まれるパッケージを調べたい

デフォルトで入っているaptリポジトリー以外に追加した場合、そのリポジトリーにはなにが含まれているのか知りたくなることがたまに
ありまして。

たとえば、今回の環境では以下の2つを追加しています。

https://download.docker.com/linux/ubuntu focal stable
https://packages.redis.io/deb focal main

aptリポジトリーにどのようなパッケージが含まれているのかを調べるには、/var/lib/apt/listsディレクトリ内のファイルを使用します。
このディレクトリは、aptのキャッシュファイルが配置されています。

Ubuntu Manpage: apt-cache - query the APT cache

先ほどの2つのリポジトリーの場合は、これらのファイルを確認すれば良さそうです。

$ ls -l /var/lib/apt/lists/download.docker.com_linux_ubuntu_dists_focal_*_Packages
-rw-r--r-- 1 root root 117636  8月 12 17:09 /var/lib/apt/lists/download.docker.com_linux_ubuntu_dists_focal_stable_binary-amd64_Packages



$ ls -l /var/lib/apt/lists/packages.redis.io_deb_dists_focal_*_Packages
-rw-r--r-- 1 root root 15619  8月  8 19:07 /var/lib/apt/lists/packages.redis.io_deb_dists_focal_main_binary-all_Packages
-rw-r--r-- 1 root root 70638  8月  8 19:07 /var/lib/apt/lists/packages.redis.io_deb_dists_focal_main_binary-amd64_Packages
-rw-r--r-- 1 root root 14644  8月  8 19:07 /var/lib/apt/lists/packages.redis.io_deb_dists_focal_main_binary-i386_Packages

実際のファイル名は、その時の状態を確認しましょうという感じですね。

こんなコマンドを用意して

$ grep '^Package' /var/lib/apt/lists/[aptリポジトリー名]_*_Packages | perl -wanl -F'\s' -e 'print $F[1]' | sort -u

確認

$ grep '^Package' /var/lib/apt/lists/download.docker.com_linux_ubuntu_dists_focal_*_Packages | perl -wanl -F'\s' -e 'print $F[1]' | sort -u
containerd.io
docker-ce
docker-ce-cli
docker-ce-rootless-extras
docker-compose-plugin
docker-scan-plugin


$ grep '^Package' /var/lib/apt/lists/packages.redis.io_deb_dists_focal_*_Packages | perl -wanl -F'\s' -e 'print $F[1]' | sort -u
memtier-benchmark
redis
redis-sentinel
redis-server
redis-stack-server
redis-tools

こんな感じでしょうか。