# Autopkgtest
## 在 Ubuntu 24.04 上安裝 autopkgtest 使用 autopkgtest-build-lxc 來建立 arm64 的測試環境
* **有 Bug 要先解決** 目前無法正常運作
```bash
$ sudo apt install autopkgtest
正在讀取套件清單... 完成
正在重建相依關係... 完成
正在讀取狀態資料... 完成
下列的額外套件將被安裝:
autodep8 dctrl-tools retry
建議套件:
docker-cli | docker.io docker.io fakemachine genisoimage incus lxc lxd mmdebstrap ovmf-ia32 podman qemu-efi-aarch64 qemu-efi-arm qemu-efi-riscv64 qemu-system schroot uidmap vmdb2 zerofree debtags
下列【新】套件將會被安裝:
autodep8 autopkgtest dctrl-tools retry
升級 0 個,新安裝 4 個,移除 0 個,有 0 個未被升級。
需要下載 327 kB 的套件檔。
此操作完成之後,會多佔用 1,165 kB 的磁碟空間。
是否繼續進行 [Y/n]? [Y/n]
...
$ sudo autopkgtest-build-lxc ubuntu noble arm64
ERROR: command debootstrap not found, provided by package debootstrap
ERROR: command lxc-config not found, provided by package lxc
ERROR: command newuidmap not found, provided by package uidmap
ERROR: file /usr/share/lxc/templates/lxc-ubuntu not found, provided by package lxc-templates
$ sudo apt install debootstrap lxc uidmap lxc-templates
正在讀取套件清單... 完成
正在重建相依關係... 完成
正在讀取狀態資料... 完成
下列的額外套件將被安裝:
arch-test bridge-utils cloud-image-utils fakechroot genisoimage libfakechroot liblxc-common liblxc1t64 libpam-cgfs libsubid4 lxcfs mmdebstrap
建議套件:
ifupdown mtools squid-deb-proxy-client debian-archive-keyring wodim cdrkit-doc btrfs-progs criu python3-lxc qemu-user-static apt-transport-tor genext2fs perl-doc qemu-user squashfs-tools-ng
下列【新】套件將會被安裝:
arch-test bridge-utils cloud-image-utils debootstrap fakechroot genisoimage libfakechroot liblxc-common liblxc1t64 libpam-cgfs libsubid4 lxc lxc-templates lxcfs mmdebstrap uidmap
升級 0 個,新安裝 16 個,移除 0 個,有 0 個未被升級。
需要下載 4,968 kB 的套件檔。
此操作完成之後,會多佔用 35.9 MB 的磁碟空間。
是否繼續進行 [Y/n]? [Y/n]
...
$ sudo autopkgtest-build-lxc ubuntu noble arm64
lxc-create: autopkgtest-noble-arm64: ../src/lxc/storage/btrfs.c: btrfs_create: 938 Inappropriate ioctl for device - Failed to create btrfs subvolume "/var/lib/lxc/autopkgtest-noble-arm64/rootfs"
lxc-create: autopkgtest-noble-arm64: ../src/lxc/storage/zfs.c: zfs_create: 735 Failed to create zfs dataset "zfs:lxc/autopkgtest-noble-arm64": lxc-create: autopkgtest-noble-arm64: ../src/lxc/utils.c: run_command_internal: 1608 Failed to exec command
lxc-create: autopkgtest-noble-arm64: ../src/lxc/storage/lvm.c: do_lvm_create: 165 Failed to create logical volume "autopkgtest-noble-arm64": Volume group "lxc" not found
Cannot process volume group lxc
lxc-create: autopkgtest-noble-arm64: ../src/lxc/storage/lvm.c: lvm_create: 623 Error creating new logical volume "lvm:/dev/lxc/autopkgtest-noble-arm64" of size "1073741824 bytes"
can't create arm64 container on amd64
lxc-create: autopkgtest-noble-arm64: ../src/lxc/lxccontainer.c: create_run_template: 1628 Failed to create container from template
lxc-create: autopkgtest-noble-arm64: ../src/lxc/tools/lxc_create.c: main: 317 Failed to create container autopkgtest-noble-arm64
ERROR:autopkgtest-build-lxc:Command '['/usr/share/autopkgtest/lib/build-lxc.sh', 'ubuntu', 'noble', 'arm64', '']' returned non-zero exit status 1.
...
$ sudo snap install lxd --channel=latest/stable
$ sudo usermod -a -G lxd $USER
$ newgrp lxd
$ sudo apt install qemu-user-static binfmt-support
正在讀取套件清單... 完成
正在重建相依關係... 完成
正在讀取狀態資料... 完成
下列【新】套件將會被安裝:
binfmt-support qemu-user-static
升級 0 個,新安裝 2 個,移除 0 個,有 13 個未被升級。
需要下載 14.7 MB 的套件檔。
此操作完成之後,會多佔用 137 MB 的磁碟空間。
...
```
## autopkgtest-build-lxc
### 安裝 lxc 跟 autopkgtest
```
$ sudo apt install lxc-templates lxc lxc-utils autopkgtest
```
### 建立 Ubuntu 18.04 的 LXC container
```
$ MIRROR=http://tw.archive.ubuntu.com/ubuntu/ \
sudo autopkgtest-build-lxc ubuntu bionic amd64 auto.sh \
# 可以使用自己準備的 auto.sh 來設定環境
```
### 可以先試試看 file 這個軟體套件
```
$ apt source file
$ sudo autopkgtest file_5.38-4.dsc -- lxc autopkgtest-bionic-amd64
$ sudo autopkgtest file_5.38-4.dsc --summary-file=summary.log \
--log-file=complete.log -- lxc autopkgtest-bionic-amd64 # 另存紀錄
```
### 建立 Debian sid 的 LXD container
```
$ sudo autopkgtest-build-lxc debian sid
```
### 對 Debian package 做測試
```
$ sudo autopkgtest fonts-cns11643_11403.01+20250325-2.dsc -- lxc autopkgtest-sid
```
### 將 LXC container 清掉
```
$ sudo lxc-destroy -n autopkgtest-bionic-amd64
```
## autopkgtest-build-lxd
* Debian
```bash
$ autopkgtest-build-lxd images:debian/sid/amd64
$ autopkgtest kore_4.2.3-1.dsc -- lxd images:debian/sid/amd64
```
* Ubuntu
```bash
$ sudo apt install lxd
$ autopkgtest-build-lxd images:ubuntu/bionic/amd64
$ sudo autopkgtest file_5.38-4.dsc -- lxd autopkgtest/ubuntu/bionic/amd64
```
## autopkgtest-build-incus
```bash
$ sudo apt install incus incus-client
$ sudo systemctl restart incus.service
$ sudo usermod -a -G incus $USER
$ newgrp incus
$ incus image list images:debian | grep amd64
$ autopkgtest-build-incus --vm images:debian/buster/amd64
```
## autopkgtest-build-qemu
* 先到 https://people.debian.org/~gio/dqib/ 下載 Images for armhf-virt 檔案
* https://github.com/vfdev-5/qemu-rpi2-vexpress/tree/master 上面有 DTB 檔案
* Debian (在 Lenovo ThinkPad P16v Gen 1 裡面的 Ubuntu 24.04 可以正常運行)
```bash
$ sudo autopkgtest-build-qemu sid debian-sid.img --arch=armhf --size=8G
$ sudo autopkgtest kore_4.2.3-2.dsc -- qemu --qemu-architecture armhf --dpkg-architecture armhf --qemu-options "-machine virt" debian-sid.img
```
* ubuntu
```bash
$ sudo apt install --no-install-recommends vmdb2 qemu-user-static
$ autopkgtest-build-qemu
usage: /usr/bin/autopkgtest-build-qemu [<options...>] <release> <image> [<mirror>] [<architecture>] [<script>] [<size>]
--apt-proxy=http://PROXY:PORT Set apt proxy [default: auto]
--arch=ARCH, --architecture=ARCH Set architecture, e.g. i386
[default: amd64]
--mirror=URL Set apt mirror [default:
http://deb.debian.org/debian]
--script=SCRIPT Run an extra customization script
--size=SIZE Set image size [default: 25G]
$ sudo vi /usr/lib/python3/dist-packages/vmdb/plugins/grub_plugin.py +174
# '--force-extra-removable',
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=951766
$ sudo vi /usr/lib/python3/dist-packages/vmdb/plugins/debootstrap_plugin.py +44
vmdb.runcmd(['debootstrap', '--components=main,universe', '--variant', variant, suite, target, mirror])
# https://paste.ubuntu.com/p/psV77MzHbq/
$ sudo autopkgtest-build-qemu bionic ~/ubuntu-bionic.img \
--arch=amd64 \
--mirror=http://tw.archive.ubuntu.com/ubuntu \
--size=8G \
--script=$(readlink -f auto.sh)
$ sudo autopkgtest file_5.38-4.dsc -- qemu ~/ubuntu-bionic.img
```
## 注意事項
* 要測試的軟體套件的 source list 要先在 vm 內準備好
* auto.sh 要設成可執行才能使用在 autopkgtest-build-qemu
* vm 內的 apt 不會自動更新
* 可以使用 --apt-upgrade | -U 來更新,例如 sudo autopkgtest -U file_5.38-4.dsc
* 任何 stderr 的輸出都會被當成測試失敗
* 除非在 Restrictions: restriction-name [, another-restriction-name ...] 加上 allow-stderr
* autopkgtest-build-lxc distribution release [architecture] [script]
* The script must be a **POSIX shell script**, and should not depend on bash-specific features.
* 有些測試只能在 qemu 裡面跑,就可以使用 Restrictions: isolation-machine
* 有些測試在 chroot/schroot 裡面跑會失敗,就可以使用 Restrictions: isolation-container
* 有時候測試的 lxc container 會遺留下來造成問題,測試前可先觀察一下 sudo lxc-ls -f 輸出是否正常
* lxc 所使用的 mirror site 會在第一次建立 container 時固定下來,除非使用 `sudo lxc-create -t ubuntu -n temporary -- --mirror http://mirror01.idc.hinet.net/ubuntu --flush-cache` 當中的 --flush-cache 來將 mirror site 改動一下
```
$ sudo lxc-ls -f
NAME STATE AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED
autopkgtest-bionic-amd64 STOPPED 0 - - - false
autopkgtest-focal-amd64 STOPPED 0 - - - false
autopkgtest-lxc-aazmgf RUNNING 0 - 10.0.3.102 - false
```
我實際遇過 autopkgtest-lxc-aazmgf 引起問題
## 小技巧
1. 可以在 debian/tests/control 加上 Test-Command: false
這樣就可以用 sudo autopkgtest -U --test-name=false --shell-fail -- lxc autopkgtest-bionic-amd64 進入測試環境
## 參考資料
https://people.debian.org/~mpitt/autopkgtest/README.running-tests.html
https://people.debian.org/~mpitt/autopkgtest/README.package-tests.html
https://autopkgtest.ubuntu.com/
https://ci.debian.net/
https://wiki.debian.org/ContinuousIntegration/autopkgtest
https://packaging.ubuntu.com/html/auto-pkg-test.html
https://ci.debian.net/doc/file.TUTORIAL.html
https://kali.org/docs/development/contributing-runtime-tests/