Ubuntu Server 20.04.1 LTSでapt-mirrorが使えるようにする

 Ubuntu Serverを導入して、まず考えたのがアップデートなどのパッケージをどうやってミラーするかでした。インターネットにつながっていないサーバーなどありますので、パッケージのミラー方法は重要です。CentOSのころは公開FTPディレクトリーでrpmファイルがあるところをまるごとrsyncなどでミラーしていましたが、Ubuntuにはapt-mirrorというその名もズバリ、ミラーをしてくれるコマンドがあります。

sudo apt install apt-mirror

にてインストールして/etc/apt/mirror.listを自分の環境にあわせて編集します。set base_pathをデフォルトの/var以下では容量がきびしいので/home以下にして、あとはそのまま。debの指定は/etc/apt/sources.listからコメントされていない行を抜き出して指定。実際には以下のような感じ。

deb http://jp.archive.ubuntu.com/ubuntu focal main restricted
deb http://jp.archive.ubuntu.com/ubuntu focal-updates main restricted
deb http://jp.archive.ubuntu.com/ubuntu focal universe
deb http://jp.archive.ubuntu.com/ubuntu focal-updates universe
deb http://jp.archive.ubuntu.com/ubuntu focal multiverse
deb http://jp.archive.ubuntu.com/ubuntu focal-updates multiverse
deb http://jp.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
deb http://jp.archive.ubuntu.com/ubuntu focal-security main restricted
deb http://jp.archive.ubuntu.com/ubuntu focal-security universe
deb http://jp.archive.ubuntu.com/ubuntu focal-security multiverse 

  これでsudo apt-mirrorと実行してしばらく後ミラーの完了です。容量は115GBぐらいでした。
 cronで実行される/etc/cron.d/apt-mirrorにファイルがあってインストール直後はコメントアウトで定期実行されないようになっていますが、編集して任意の時間に実行設定可能です。
 ローカルにパッケージがきたので今後はそこを見るように/etc/apt/sources.listは全部コメントアウトして、代わりに/etc/apt/sources.list.d以下にmy.listとして配置します。

deb http://myhost/ubuntu focal main restricted
deb http://myhost/ubuntu focal-updates main restricted
deb http://myhost/ubuntu focal universe
deb http://myhost/ubuntu focal-updates universe
deb http://myhost/ubuntu focal multiverse
deb http://myhost/ubuntu focal-updates multiverse
deb http://myhost/ubuntu focal-backports main restricted universe multiverse
deb http://myhost/ubuntu focal-security main restricted
deb http://myhost/ubuntu focal-security universe
deb http://myhost/ubuntu focal-security multiverse

 souces.listのurlはhttpの他にfile、ftpなども使えるようですが、このファイルにあるようにhttpでのアクセスが手っ取り早そうなので

sudo apt install apache2

もインストール。

ln -s /home/apt-mirror/mirror/jp.archive.ubuntu.com/ubuntu /var/www/html/ubuntu

とリンクを張ってサーバー側は完了です。
 しかしまだ使えるようにはなりません。

sudo apt update

と実行すると

Hit:1 http://myhost/ubuntu focal InRelease
Get:2 http://myhost/ubuntu focal-updates InRelease [114 kB]
Get:3 http://myhost/ubuntu focal-backports InRelease [101 kB]
Get:4 http://myhost/ubuntu focal-security InRelease [109 kB]
Ign:5 http://myhost/ubuntu focal-updates/main amd64 Packages
Ign:6 http://myhost/ubuntu focal-updates/main Translation-en
Ign:7 http://myhost/ubuntu focal-updates/universe amd64 Packages
Ign:8 http://myhost/ubuntu focal-updates/universe amd64 c-n-f Metadata
Get:5 http://myhost/ubuntu focal-updates/main amd64 Packages [752 kB]
Get:6 http://myhost/ubuntu focal-updates/main Translation-en [185 kB]
Get:7 http://myhost/ubuntu focal-updates/universe amd64 Packages [723 kB]
Ign:8 http://myhost/ubuntu focal-updates/universe amd64 c-n-f Metadata
Ign:8 http://myhost/ubuntu focal-updates/universe amd64 c-n-f Metadata
Err:8 http://myhost/ubuntu focal-updates/universe amd64 c-n-f Metadata
404 Not Found [IP: 127.0.1.1 80]
Ign:9 http://myhost/ubuntu focal-security/main amd64 Packages
Ign:10 http://myhost/ubuntu focal-security/main Translation-en
Ign:11 http://myhost/ubuntu focal-security/restricted amd64 Packages
Ign:12 http://myhost/ubuntu focal-security/restricted Translation-en
Ign:13 http://myhost/ubuntu focal-security/restricted amd64 c-n-f Metadata
Ign:14 http://myhost/ubuntu focal-security/universe amd64 Packages
Ign:15 http://myhost/ubuntu focal-security/universe amd64 c-n-f Metadata
Get:9 http://myhost/ubuntu focal-security/main amd64 Packages [438 kB]
Get:10 http://myhost/ubuntu focal-security/main Translation-en [97.2 kB]
Get:11 http://myhost/ubuntu focal-security/restricted amd64 Packages [107 kB]
Get:12 http://myhost/ubuntu focal-security/restricted Translation-en [15.9 kB]
Ign:13 http://myhost/ubuntu focal-security/restricted amd64 c-n-f Metadata
Get:14 http://myhost/ubuntu focal-security/universe amd64 Packages [523 kB]
Ign:15 http://myhost/ubuntu focal-security/universe amd64 c-n-f Metadata
Ign:13 http://myhost/ubuntu focal-security/restricted amd64 c-n-f Metadata
Ign:15 http://myhost/ubuntu focal-security/universe amd64 c-n-f Metadata
Err:13 http://myhost/ubuntu focal-security/restricted amd64 c-n-f Metadata
404 Not Found [IP: 127.0.1.1 80]
Ign:15 http://myhost/ubuntu focal-security/universe amd64 c-n-f Metadata
Fetched 3,165 kB in 1s (3,258 kB/s)
Reading package lists... Done
E: Failed to fetch http://myhost/ubuntu/dists/focal-updates/universe/cnf/Commands-amd64 404 Not Found [IP: 127.0.1.1 80]
E: Failed to fetch http://myhost/ubuntu/dists/focal-security/restricted/cnf/Commands-amd64 404 Not Found [IP: 127.0.1.1 80]
E: Some index files failed to download. They have been ignored, or old ones used instead.

といくつかのエラーが出てローカルのパッケージが一部使えない状況です。調べてみるとこちらの情報を見つけました。@がファイル名にあるもので一部ミラーできてないとのこと。
 こちらに修正したapt-mirrorが公開されてましたので、apt-mirrorファイルのみを入手して/usr/bin/apt-mirrorファイルを置き換えてます。apt-mirrorファイルはperlスクリプトです。そして再度ミラーを実行。今度はapt updateも問題なく完了しました。
 以上Ubuntu Server 20.04.1 LTSでapt-mirrorが利用できるまでの顛末でした。