ローカルサーバーにapt-mirrorを設置する

## 目的  
* ローカルサーバーにapt-mirrorを設置する。  
  
## 開発環境  
### OS情報  
``` bash:OS情報
master@sv-server:~$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
```
  
### ファイルシステム構成  
``` bash:ファイルシステム構成
master@sv-server:~$ df -h
ファイルシス          サイズ  使用  残り 使用% マウント位置
udev                    951M     0  951M    0% /dev
tmpfs                   195M  2.4M  193M    2% /run
/dev/mapper/vg00-root    18G  8.3G  8.5G   50% /
tmpfs                   975M     0  975M    0% /dev/shm
tmpfs                   5.0M  4.0K  5.0M    1% /run/lock
vmhgfs-fuse            1000G  793G  208G   80% /mnt/hgfs
/dev/nvme0n1p2          463M   99M  340M   23% /boot
/dev/mapper/vg01-home   492G  373G   94G   80% /home
/dev/nvme0n1p1          511M  6.0M  506M    2% /boot/efi
tmpfs                   195M   48K  195M    1% /run/user/115
tmpfs                   195M   44K  195M    1% /run/user/1000
```
  
### ディレクトリー／ファイル構成  
:::note info
ファイルシステムの容量上の問題でhomeディレクトリーに大半のファイルを保存する。  
:::
  
#### /etc/apache2/sites-enabled/  
``` bash:/etc/apache2/sites-enabled/
/etc/apache2/sites-enabled/
├── 000-mirror-debian.conf
└── 000-mirror-ubuntu.conf
```
  
#### /var/spool/apt-mirror/  
``` bash:/var/spool/apt-mirror/
/var/spool/apt-mirror/
├── mirror
│   ├── archive.ubuntu.com -> /home/master/repo/archive.ubuntu.com/
│   └── deb.debian.org -> /home/master/repo/deb.debian.org/
├── skel
│   ├── archive.ubuntu.com
│   └── deb.debian.org
└── var
```
  
#### homeディレクトリー
``` bash:/home/master/
/home/master/
├── repo
│   ├── archive.ubuntu.com
│   │   └── ubuntu
│   │       ├── dists
│   │       │   ├── jammy
│   │       │   ├── jammy-security
│   │       │   ├── jammy-updates
│   │       │   ├── kinetic
│   │       │   ├── kinetic-security
│   │       │   └── kinetic-updates
│   │       └── pool
│   └── deb.debian.org
│       └── debian
│           ├── dists
│           │   ├── bookworm
│           │   ├── bookworm-backports
│           │   ├── bookworm-proposed-updates
│           │   ├── bookworm-updates
│           │   ├── bullseye
│           │   ├── bullseye-backports
│           │   ├── bullseye-backports-sloppy
│           │   ├── bullseye-proposed-updates
│           │   ├── bullseye-updates
│           │   ├── stable
│           │   ├── stable-backports
│           │   ├── stable-backports-sloppy
│           │   ├── stable-proposed-updates
│           │   ├── stable-updates
│           │   ├── testing
│           │   ├── testing-backports
│           │   ├── testing-proposed-updates
│           │   ├── testing-updates
│           │   └── unstable
│           └── pool
└── work
    ├── fsimg
    └── keyring
```
  
## 導入～設定  
### パッケージ導入  
``` bash:パッケージ導入
sudo apt-get install -y apache2 apt-mirror
```
  
### 導入済みパッケージ
``` bash:導入済みパッケージ
master@sv-server:~$ dpkg -l apache2 apt-mirror avahi-daemon bind9*
要望=(U)不明/(I)インストール/(R)削除/(P)完全削除/(H)保持
| 状態=(N)無/(I)インストール済/(C)設定/(U)展開/(F)設定失敗/(H)半インストール/(W)トリガ待ち/(T)トリ >
|/ エラー?=(空欄)無/(R)要再インストール (状態,エラーの大文字=異常)
||/ 名前             バージョン          アーキテクチ 説明
+++-================-===================-============-====================================
ii  apache2          2.4.53-1~deb11u1    amd64        Apache HTTP Server
ii  apt-mirror       0.5.4-1             all          APT sources mirroring tool
ii  avahi-daemon     0.8-5               amd64        Avahi mDNS/DNS-SD daemon
ii  bind9            1:9.16.27-1~deb11u1 amd64        Internet Domain Name Server
ii  bind9-dnsutils   1:9.16.27-1~deb11u1 amd64        Clients provided with BIND 9
ii  bind9-host       1:9.16.27-1~deb11u1 amd64        DNS Lookup Utility
ii  bind9-libs:amd64 1:9.16.27-1~deb11u1 amd64        Shared Libraries used by BIND 9
ii  bind9-utils      1:9.16.27-1~deb11u1 amd64        Utilities for BIND 9
ii  bind9utils       1:9.16.27-1~deb11u1 all          Transitional package for bind9-utils
```
  
### ディレクトリー作成  
``` bash:ディレクトリー作成
sudo rm -rf ~/repo/
sudo mkdir -p ~/repo/deb.debian.org/debian
sudo mkdir -p ~/repo/archive.ubuntu.com/ubuntu
sudo chown -R apt-mirror. ~/repo/
sudo ln -s ~/repo/deb.debian.org/debian/ /var/www/html/
sudo ln -s ~/repo/archive.ubuntu.com/ubuntu/ /var/www/html/
sudo ln -s ~/repo/deb.debian.org/ /var/spool/apt-mirror/mirror/
sudo ln -s ~/repo/archive.ubuntu.com/ /var/spool/apt-mirror/mirror/
```
  
### 設定 
#### apache2   
``` bash:apache2
cat <<- '_EOT_' | sed -e 's/^ //g' | sudo tee /etc/apache2/sites-enabled/000-mirror-debian.conf > /dev/null
<VirtualHost *:80>
 	ServerAdmin webmaster@localhost
 	DocumentRoot /var/www/html
 	ErrorLog ${APACHE_LOG_DIR}/error.log
 	CustomLog ${APACHE_LOG_DIR}/access.log combined
 	Alias /debian /var/spool/apt-mirror/mirror/deb.debian.org/debian
 	<Location /debian>
 		Options FollowSymLinks Indexes
 		Require all granted
 	</Location>
</VirtualHost>
_EOT_
cat <<- '_EOT_' | sed -e 's/^ //g' | sudo tee /etc/apache2/sites-enabled/000-mirror-ubuntu.conf > /dev/null
<VirtualHost *:80>
 	ServerAdmin webmaster@localhost
 	DocumentRoot /var/www/html
 	ErrorLog ${APACHE_LOG_DIR}/error.log
 	CustomLog ${APACHE_LOG_DIR}/access.log combined
 	Alias /ubuntu /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu
 	<Location /ubuntu>
 		Options FollowSymLinks Indexes
 		Require all granted
 	</Location>
</VirtualHost>
_EOT_
```
  
#### mirror.list  
``` bash:mirror.list
if [ ! -f /etc/apt/mirror.list.orig ]; then
	sudo cp -p /etc/apt/mirror.list /etc/apt/mirror.list.orig
fi
# sudo cp -p /etc/apt/mirror.list.orig /etc/apt/mirror.list # recovery
sudo sed -i /etc/apt/mirror.list \
    -e 's/^\(deb\( \|-src \)\)/#\1/g' \
    -e 's/^\(clean \)/#\1/g'
cat <<- '_EOT_' | sed -e 's/^ //g' | sudo tee -a /etc/apt/mirror.list > /dev/null

	# === Debian 8: jessie ========================================================
	#deb http://deb.debian.org/debian/ jessie main contrib non-free
	#deb http://deb.debian.org/debian/ jessie-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ jessie main contrib non-free
	#deb-src http://deb.debian.org/debian/ jessie-updates main contrib non-free

	# === Debian 9: stretch =======================================================
	#deb http://deb.debian.org/debian/ stretch main contrib non-free
	#deb http://deb.debian.org/debian/ stretch-backports main contrib non-free
	#deb http://deb.debian.org/debian/ stretch-backports-sloppy main contrib non-free
	#deb http://deb.debian.org/debian/ stretch-proposed-updates main contrib non-free
	#deb http://deb.debian.org/debian/ stretch-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ stretch main contrib non-free
	#deb-src http://deb.debian.org/debian/ stretch-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ stretch-backports-sloppy main contrib non-free
	#deb-src http://deb.debian.org/debian/ stretch-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ stretch-updates main contrib non-free

	# === Debian 10: buster =======================================================
	#deb http://deb.debian.org/debian/ buster main contrib non-free
	#deb http://deb.debian.org/debian/ buster-backports main contrib non-free
	#deb http://deb.debian.org/debian/ buster-backports-sloppy main contrib non-free
	#deb http://deb.debian.org/debian/ buster-proposed-updates main contrib non-free
	#deb http://deb.debian.org/debian/ buster-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ buster main contrib non-free
	#deb-src http://deb.debian.org/debian/ buster-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ buster-backports-sloppy main contrib non-free
	#deb-src http://deb.debian.org/debian/ buster-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ buster-updates main contrib non-free

	# === Debian 11: bullseye ================================================================
	deb http://deb.debian.org/debian/ bullseye main contrib non-free
	deb http://deb.debian.org/debian/ bullseye-backports main contrib non-free
	deb http://deb.debian.org/debian/ bullseye-backports-sloppy main contrib non-free
	deb http://deb.debian.org/debian/ bullseye-proposed-updates main contrib non-free
	deb http://deb.debian.org/debian/ bullseye-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ bullseye main contrib non-free
	#deb-src http://deb.debian.org/debian/ bullseye-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ bullseye-backports-sloppy main contrib non-free
	#deb-src http://deb.debian.org/debian/ bullseye-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ bullseye-updates main contrib non-free

	# === Debian 12: bookworm =====================================================
	deb http://deb.debian.org/debian/ bookworm main contrib non-free
	deb http://deb.debian.org/debian/ bookworm-backports main contrib non-free
	deb http://deb.debian.org/debian/ bookworm-proposed-updates main contrib non-free
	deb http://deb.debian.org/debian/ bookworm-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ bookworm main contrib non-free
	#deb-src http://deb.debian.org/debian/ bookworm-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ bookworm-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ bookworm-updates main contrib non-free

	# === oldoldoldstable =========================================================
	#deb http://deb.debian.org/debian/ oldoldoldstable main contrib non-free
	#deb http://deb.debian.org/debian/ oldoldoldstable-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldoldstable main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldoldstable-updates main contrib non-free

	# === oldoldstable ============================================================
	#deb http://deb.debian.org/debian/ oldoldstable main contrib non-free
	#deb http://deb.debian.org/debian/ oldoldstable-backports main contrib non-free
	#deb http://deb.debian.org/debian/ oldoldstable-backports-sloppy main contrib non-free
	#deb http://deb.debian.org/debian/ oldoldstable-proposed-updates main contrib non-free
	#deb http://deb.debian.org/debian/ oldoldstable-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldstable main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldstable-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldstable-backports-sloppy main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldstable-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldoldstable-updates main contrib non-free

	# === oldstable ===============================================================
	#deb http://deb.debian.org/debian/ oldstable main contrib non-free
	#deb http://deb.debian.org/debian/ oldstable-backports main contrib non-free
	#deb http://deb.debian.org/debian/ oldstable-backports-sloppy main contrib non-free
	#deb http://deb.debian.org/debian/ oldstable-proposed-updates main contrib non-free
	#deb http://deb.debian.org/debian/ oldstable-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldstable main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldstable-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldstable-backports-sloppy main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldstable-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ oldstable-updates main contrib non-free

	# === stable ==================================================================
	deb http://deb.debian.org/debian/ stable main contrib non-free
	deb http://deb.debian.org/debian/ stable-backports main contrib non-free
	deb http://deb.debian.org/debian/ stable-backports-sloppy main contrib non-free
	deb http://deb.debian.org/debian/ stable-proposed-updates main contrib non-free
	deb http://deb.debian.org/debian/ stable-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ stable main contrib non-free
	#deb-src http://deb.debian.org/debian/ stable-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ stable-backports-sloppy main contrib non-free
	#deb-src http://deb.debian.org/debian/ stable-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ stable-updates main contrib non-free

	# === testing =================================================================
	deb http://deb.debian.org/debian/ testing main contrib non-free
	deb http://deb.debian.org/debian/ testing-backports main contrib non-free
	deb http://deb.debian.org/debian/ testing-proposed-updates main contrib non-free
	deb http://deb.debian.org/debian/ testing-updates main contrib non-free
	deb http://deb.debian.org/debian/ unstable main contrib non-free
	#deb-src http://deb.debian.org/debian/ testing main contrib non-free
	#deb-src http://deb.debian.org/debian/ testing-backports main contrib non-free
	#deb-src http://deb.debian.org/debian/ testing-proposed-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ testing-updates main contrib non-free
	#deb-src http://deb.debian.org/debian/ unstable main contrib non-free

	# === trusty ==================================================================
	# === xenial ==================================================================
	# === bionic ==================================================================
	# === devel ===================================================================
	# === focal ===================================================================
	# === hirsute =================================================================
	# === impish ==================================================================

	# === jammy ===================================================================
	deb http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
	#deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
	#deb http://archive.ubuntu.com/ubuntu/ jammy-proposed main restricted universe multiverse
	deb http://archive.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
	deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ jammy-proposed main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse

	# === kinetic =================================================================
	deb http://archive.ubuntu.com/ubuntu/ kinetic main restricted universe multiverse
	#deb http://archive.ubuntu.com/ubuntu/ kinetic-backports main restricted universe multiverse
	#deb http://archive.ubuntu.com/ubuntu/ kinetic-proposed main restricted universe multiverse
	deb http://archive.ubuntu.com/ubuntu/ kinetic-security main restricted universe multiverse
	deb http://archive.ubuntu.com/ubuntu/ kinetic-updates main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ kinetic main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ kinetic-backports main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ kinetic-proposed main restricted universe multiverse
	#deb-src http://archive.ubuntu.com/ubuntu/ kinetic-updates main restricted universe multiverse

	# === clean ===================================================================
	clean http://archive.ubuntu.com/ubuntu/
_EOT_
```
  
## 実行  
### ダウンロード
``` bash:ダウンロード
sudo systemctl restart apache2
sudo rm -rf /var/spool/apt-mirror/var/*log* \
            /var/spool/apt-mirror/var/*urls* \
            /var/spool/apt-mirror/var/apt-mirror.lock \
            /var/spool/apt-mirror/var/ALL \
            /var/spool/apt-mirror/var/MD5 \
            /var/spool/apt-mirror/var/NEW \
            /var/spool/apt-mirror/var/SHA1 \
            /var/spool/apt-mirror/var/SHA256 \
            /var/spool/apt-mirror/skel/* \
            /var/spool/apt-mirror/mirror/deb.debian.org/debian/* \
            /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/*
sudo -u apt-mirror apt-mirror
```
  
### ログ  
``` bash:ログ
master@sv-server:~$ sudo -u apt-mirror apt-mirror
Downloading 798 index files using 20 threads...
Begin time: Fri Jun 10 22:26:56 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Fri Jun 10 22:27:37 2022

Processing translation indexes: [TTTTTTTTTTTTTTTTTTTTTTTTT]

Downloading 1320 translation files using 20 threads...
Begin time: Fri Jun 10 22:27:37 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Fri Jun 10 22:29:00 2022

Processing DEP-11 indexes: [DDDDDDDDDDDDDDDDDDDDDDDDD]

Downloading 155 dep11 files using 20 threads...
Begin time: Fri Jun 10 22:29:00 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Fri Jun 10 22:29:24 2022

Processing indexes: [PPPPPPPPPPPPPPPPPPPPPPPPP]

367.8 GiB will be downloaded into archive.
Downloading 199403 archive files using 20 threads...
Begin time: Fri Jun 10 22:29:43 2022
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...
End time: Sat Jun 11 08:06:16 2022

0 bytes in 0 files and 0 directories can be freed.
Run /var/spool/apt-mirror/var/clean.sh for this purpose.

Running the Post Mirror script ...
(/var/spool/apt-mirror/var/postmirror.sh)


Post Mirror script has completed. See above output for any possible errors.

master@sv-server:~$ LANG=C df -h /home
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/vg01-home  492G  371G   96G  80% /home
```
  
## 確認  
### mmdebstrap  
``` bash:パッケージ導入
sudo apt-get -y install mmdebstrap
```
  
``` bash:mmdebstrap [debian stable amd64]
sudo rm -rf ./fsimg/
sudo mkdir -p ./fsimg
sudo mmdebstrap \
    --components="main non-free contrib" \
    --variant=standard \
    --mode=sudo \
    --aptopt='Apt::Install-Recommends "true"' \
    --include=" \
    build-essential curl vim \
    open-vm-tools open-vm-tools-desktop \
    clamav \
    bind9 dnsutils \
    openssh-server \
    samba smbclient cifs-utils \
    isc-dhcp-server \
    minidlna \
    linux-headers-amd64 \
    linux-image-amd64 \
    " \
    --architectures=amd64 \
    --keyring=./keyring/ \
    stable \
    "./fsimg/" \
    http://sv-server/debian/
```
  
``` bash:mmdebstrap [ubuntu kinetic amd64]
sudo rm -rf ./fsimg/
sudo mkdir -p ./fsimg
sudo mmdebstrap \
    --components="main multiverse restricted universe" \
    --variant=standard \
    --mode=sudo \
    --aptopt='Apt::Install-Recommends "true"' \
    --include=" \
    build-essential curl vim \
    open-vm-tools open-vm-tools-desktop \
    clamav \
    bind9 dnsutils \
    openssh-server \
    samba smbclient cifs-utils \
    isc-dhcp-server \
    minidlna \
    linux-headers-generic \
    linux-image-generic \
    " \
    --architectures=amd64 \
    --keyring=./keyring/ \
    kinetic \
    "./fsimg/" \
    http://sv-server.local/ubuntu/
```