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

こんな感じでしょうか。

Ubuntu Linux 20.04 LTSに、Redisをaptリポジトリからインストールする

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

Redisのインストール方法は、公式にはずっとソースコードからビルドするものだと思っていたのですが、どうやら今は違うみたいです。

Ubuntu LinuxDebianだとaptでインストール可能になっていたので、試してみることにしました。

Redisのインストール方法いろいろ

Redisのダウンロードページを見ると、ソースコードtar.gzで配布していることが確認できます。

Download | Redis

あとは、Docker HubでDockerイメージが利用可能なことも確認できますね。

ここでドキュメントを見ると、もう少し選択肢があることがわかります。

Installing Redis | Redis

Linuxだとこちら。

Install Redis on Linux | Redis

Ubuntu LinuxDebianだと、aptリポジトリが提供されているのでこちらを使います。

いつから使えたんだろう?と思ってインストール後にapt-cache showpkg redisで確認してみたところ、現時点でこんな感じでした。

Provides:
6:7.0.4-1rl1~focal1 -
6:7.0.3-1rl1~focal1 -
6:7.0.2-1rl1~focal1 -
6:7.0.1-1rl1~focal1 -
6:7.0.0-1rl1~focal1 -
6:6.2.7-1rl1~focal1 -
6:6.2.6-3rl1~focal1 -
6:6.2.6-2rl1~focal1 -
6:6.2.6-1rl1~focal1 -
6:6.2.5-1rl1~focal1 -
6:6.2.4-1rl1~focal1 -
6:6.0.16-3rl1~focal1 -
6:6.0.16-2rl1~focal1 -
6:6.0.16-1rl1~focal1 -
6:6.0.15-1rl1~focal1 -
6:6.0.14-1rl1~focal1 -
5:5.0.7-2ubuntu0.1 -
5:5.0.7-2 -

Ubuntu Linux 20.04 LTSでの確認ですが。

CentOS等では、snapを使うと良さそうです。

Install Redis on Linux | Snap Store

ちなみに、macOSだとHomebrew、

Install Redis on macOS | Redis

WindowsだとWSL2を使ってaptでインストールしてね、という感じでした。

Install Redis on Windows | Redis

Windowsは、まあ仕方ないですね…。

今回は、Ubuntu Linuxで試してみようと思います。

環境

今回の環境はこちら。Ubuntu Linux 20.04 LTSです。

$ 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


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

PPAからRedisをインストールする

では、ドキュメントに沿ってRedisをインストールしていきます。

Install Redis on Linux | Redis

まずはGPG鍵の取り込み。

$ curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

aptリポジトリの追加。

$ echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

リポジトリの情報を更新。

$ sudo apt update

redisパッケージの情報を見てみます。

$ apt show redis
Package: redis
Version: 6:7.0.4-1rl1~focal1
Priority: optional
Section: database
Maintainer: Redis Core Team <redis@redis.io>
Installed-Size: 50.2 kB
Depends: redis-server (<< 6:7.0.4-1rl1~focal1.1~), redis-server (>= 6:7.0.4-1rl1~focal1)
Homepage: https://redis.io/
License: unknown
Vendor: none
Download-Size: 38.2 kB
APT-Sources: https://packages.redis.io/deb focal/main amd64 Packages
Description: Persistent key-value database with network interface (metapackage)
 Redis is a key-value database in a similar vein to memcache but the dataset
 is non-volatile. Redis additionally provides native support for atomically
 manipulating and querying data structures such as lists and sets.
 .
 The dataset is stored entirely in memory and periodically flushed to disk.
 .
 This package depends on the redis-server package.

N: 追加レコードが 17 件あります。表示するには '-a' スイッチを付けてください。

Redis 7.0.4(現時点での最新バージョン)を扱っていそうですね。

インストール。

$ sudo apt install redis

こんな表示が出ます。

以下の追加パッケージがインストールされます:
  redis-server redis-tools
提案パッケージ:
  ruby-redis
以下のパッケージが新たにインストールされます:
  redis redis-server redis-tools
アップグレード: 0 個、新規インストール: 3 個、削除: 0 個、保留: 0 個。
1,366 kB のアーカイブを取得する必要があります。
この操作後に追加で 8,585 kB のディスク容量が消費されます。

見た感じ、redis-serverが文字通りRedisサーバーで、redis-toolsはクライアントツールですね。

redisパッケージはドキュメントが含まれています。

インストールが終わると、その段階でRedisサーバーが起動しています。

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-08-13 18:14:27 JST; 4min 24s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 1851 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2273)
     Memory: 2.7M
     CGroup: /system.slice/redis-server.service
             └─1851 /usr/bin/redis-server 127.0.0.1:6379

 8月 13 18:14:27 ubuntu2004.localdomain systemd[1]: Starting Advanced key-value store...
 8月 13 18:14:27 ubuntu2004.localdomain systemd[1]: Started Advanced key-value store.

systemdで操作可能になっているので、起動や停止やsystemctlで。

### 起動
$ sudo systemctl start redis-server


### 停止
$ sudo systemctl stop redis-server

自動起動は有効になっていないので、有効にする場合はsystemctl enableで。

$ sudo systemctl enable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable redis-server
Created symlink /etc/systemd/system/redis.service → /lib/systemd/system/redis-server.service.
Created symlink /etc/systemd/system/multi-user.target.wants/redis-server.service → /lib/systemd/system/redis-server.service.

設定ファイルは、/etc/redis/redis.confにインストールされます。

デフォルトの内容を見てみましょう。

$ sudo grep -vE '^#|^$' /etc/redis/redis.conf
bind 127.0.0.1 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised auto
pidfile /run/redis/redis-server.pid
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /var/lib/redis
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
aof-timestamp-enabled no

slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

けっこう設定されていますね…。

bindが以下なので、デフォルトはローカルのみで接続可ですね。

bind 127.0.0.1 -::1

またrequirepassがないので、パスワードは設定していません。

ちなみに、Docker Hubにあるコンテナイメージでは特にデフォルトの設定があるわけではなさそうです。

https://github.com/docker-library/redis/blob/5c0bbb5dfce3d4999649cbc3ba8bf7c123bcadff/7.0/Dockerfile#L120

バージョンの確認。

$ redis-server --version
Redis server v=7.0.4 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=c7d71d4b63066c


$ redis-cli --version
redis-cli 7.0.4

動作確認

Redis CLIで接続してみます。Redisサーバーと一緒にインストールされたものですね。

$ redis-cli
127.0.0.1:6379>

値の登録、取得。

127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"

Redisサーバーを再起動しても、RDB(Redis Database)が有効になっているので

$ sudo systemctl restart redis-server

データが残っています。

$ redis-cli
127.0.0.1:6379> get key1
"value1"

ログは、/var/log/redis/redis-server.logに出力されます。

$ head -n 10 /var/log/redis/redis-server.log
1851:C 13 Aug 2022 18:14:27.126 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.
1851:C 13 Aug 2022 18:14:27.126 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1851:C 13 Aug 2022 18:14:27.126 # Redis version=7.0.4, bits=64, commit=00000000, modified=0, pid=1851, just started
1851:C 13 Aug 2022 18:14:27.126 # Configuration loaded
1851:M 13 Aug 2022 18:14:27.126 * monotonic clock: POSIX clock_gettime
1851:M 13 Aug 2022 18:14:27.127 # Warning: Could not create server TCP listening socket ::1:6379: bind: Cannot assign requested address
1851:M 13 Aug 2022 18:14:27.128 * Running mode=standalone, port=6379.
1851:M 13 Aug 2022 18:14:27.128 # Server initialized
1851:M 13 Aug 2022 18:14:27.128 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1851:M 13 Aug 2022 18:14:27.128 * Ready to accept connections

確認は、こんなところでしょうか。

いつもソースコードからビルドして使っていたので、aptで利用できると嬉しいですね。