two-images: construction reproductible de rocky10 et debian13 par Packer, test de démarrage syonad/two#53
Some checks failed
Tests / check (push) Failing after 39s

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-24 13:53:56 +02:00
commit fe401b9a12
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
18 changed files with 937 additions and 0 deletions

View file

@ -0,0 +1,18 @@
name: Tests
on:
push:
branches: ['**']
pull_request:
jobs:
check:
runs-on: docker
steps:
- uses: actions/checkout@v3
- name: Outils
run: |
apt-get update
apt-get install -y curl unzip shellcheck openssh-client
- name: build.sh check
run: ./build.sh check

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/output/
/dist/

131
LICENSE Normal file
View file

@ -0,0 +1,131 @@
# PolyForm Noncommercial License 1.0.0
<https://polyformproject.org/licenses/noncommercial/1.0.0>
## Acceptance
In order to get any license under these terms, you must agree
to them as both strict obligations and conditions to all
your licenses.
## Copyright License
The licensor grants you a copyright license for the
software to do everything you might do with the software
that would otherwise infringe the licensor's copyright
in it for any permitted purpose. However, you may
only distribute the software according to [Distribution
License](#distribution-license) and make changes or new works
based on the software according to [Changes and New Works
License](#changes-and-new-works-license).
## Distribution License
The licensor grants you an additional copyright license
to distribute copies of the software. Your license
to distribute covers distributing the software with
changes and new works permitted by [Changes and New Works
License](#changes-and-new-works-license).
## Notices
You must ensure that anyone who gets a copy of any part of
the software from you also gets a copy of these terms or the
URL for them above, as well as copies of any plain-text lines
beginning with `Required Notice:` that the licensor provided
with the software. For example:
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
## Changes and New Works License
The licensor grants you an additional copyright license to
make changes and new works based on the software for any
permitted purpose.
## Patent License
The licensor grants you a patent license for the software that
covers patent claims the licensor can license, or becomes able
to license, that you would infringe by using the software.
## Noncommercial Purposes
Any noncommercial purpose is a permitted purpose.
## Personal Uses
Personal use for research, experiment, and testing for
the benefit of public knowledge, personal study, private
entertainment, hobby projects, amateur pursuits, or religious
observance, without any anticipated commercial application,
is use for a permitted purpose.
## Noncommercial Organizations
Use by any charitable organization, educational institution,
public research organization, public safety or health
organization, environmental protection organization,
or government institution is use for a permitted purpose
regardless of the source of funding or obligations resulting
from the funding.
## Fair Use
You may have "fair use" rights for the software under the
law. These terms do not limit them.
## No Other Rights
These terms do not allow you to sublicense or transfer any of
your licenses to anyone else, or prevent the licensor from
granting licenses to anyone else. These terms do not imply
any other licenses.
## Patent Defense
If you make any written claim that the software infringes or
contributes to infringement of any patent, your patent license
for the software granted under these terms ends immediately. If
your company makes such a claim, your patent license ends
immediately for work on behalf of your company.
## Violations
The first time you are notified in writing that you have
violated any of these terms, or done anything with the software
not covered by your licenses, your licenses can nonetheless
continue if you come into full compliance with these terms,
and take practical steps to correct past violations, within
32 days of receiving notice. Otherwise, all your licenses
end immediately.
## No Liability
***As far as the law allows, the software comes as is, without
any warranty or condition, and the licensor will not be liable
to you for any damages arising out of these terms or the use
or nature of the software, under any kind of legal claim.***
## Definitions
The **licensor** is the individual or entity offering these
terms, and the **software** is the software the licensor makes
available under these terms.
**You** refers to the individual or entity agreeing to these
terms.
**Your company** is any legal entity, sole proprietorship,
or other kind of organization that you work for, plus all
organizations that have control over, are under the control of,
or are under common control with that organization. **Control**
means ownership of substantially all the assets of an entity,
or the power to direct its management and policies by vote,
contract, or otherwise. Control can be direct or indirect.
**Your licenses** are all the licenses granted to you for the
software under these terms.
**Use** means anything you do with the software requiring one
of your licenses.

45
README.md Normal file
View file

@ -0,0 +1,45 @@
# two-images
Construit les images qcow2 des VM utilisées par le lab de [two](https://git.g3e.fr/syonad/two), de
façon **reproductible** : chaque entrée est épinglée avec sa somme (Packer, plugin qemu, image du
fournisseur), et une image se reconstruit à la demande plutôt que de se télécharger.
| Image | Source | Personnalisation |
|-------|--------|------------------|
| `rocky10` | Rocky Linux 10.2 GenericCloud Base `20260525.0` (SHA-256 signée par Rocky) | exigences two + `docs/deploiement/image-qcow2.rst` (sshd, utilisateur `syonad`, SELinux désactivé…) |
| `debian13` | Debian 13 genericcloud `20261021-2638` (SHA-512) | exigences two |
Exigences two, communes : source de métadonnées NoCloud vers `169.254.169.254`, `network-config`
réappliqué à chaque démarrage (`boot-legacy`), DHCP IPv4 sur toutes les interfaces (systemd-networkd
ou NetworkManager), liens `/dev/xvdX` des disques additionnels.
## Méthode
Celle de `docs/deploiement/image-qcow2.rst` dans two : une VM de construction **x86_64** démarre sur
un overlay de l'image du fournisseur, avec deux disques de plus — la **cible** et un disque de
**travail**. Dans la VM, l'image du fournisseur est téléchargée sur le disque de travail, vérifiée,
puis écrite en brut sur la cible ; la cible est montée sur `/mnt` et personnalisée (chroot), nettoyée
(identité cloud-init, machine-id, clés d'hôte SSH, journaux) et contrôlée. L'image produite est la
cible, compressée ; l'image du fournisseur n'est jamais démarrée.
Avant d'écrire, la cible est reconnue par sa taille et doit être vierge (ni table de partitions, ni
système de fichiers, premier Mio nul).
## Utilisation
```
./build.sh check formatage, validation des templates, shellcheck
./build.sh <image> construit dist/<image>.qcow2 et met à jour dist/SHA512SUMS
./test.sh <image> démarre l'image (seed NoCloud, 2 interfaces, 1 disque xvdb) et la vérifie
```
Prérequis : `qemu-system-x86_64`, `qemu-img`, `curl`, `unzip`, `ssh-keygen`, `shellcheck` (pour
`check`), `hdiutil` (macOS) ou `genisoimage`/`xorriso` (Linux). Packer et le plugin qemu sont
téléchargés et vérifiés dans `~/.cache/two-images` (`TWO_IMAGES_CACHE`).
Sous Linux la VM tourne avec KVM ; sur un Mac arm64 en émulation TCG, plus lente : le Mac ne sert
qu'aux essais.
## Licence
PolyForm Noncommercial 1.0.0 — voir `LICENSE`.

142
build.sh Executable file
View file

@ -0,0 +1,142 @@
#!/usr/bin/env bash
set -euo pipefail
PACKER_VERSION="1.16.2"
PACKER_SUMS="3d7e4df8e7e8c21bdf495c4f63ad9189e22540dc8126dee6db791aa8519e8b3a packer_1.16.2_darwin_arm64.zip
abbfe0cf74985084112659b53e3565f264f70a20914c3b7c4f789f54f61c2414 packer_1.16.2_linux_amd64.zip"
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
CACHE="${TWO_IMAGES_CACHE:-${HOME}/.cache/two-images}"
OUTPUT="${TWO_IMAGES_OUTPUT:-${ROOT}/output}"
DIST="${TWO_IMAGES_DIST:-${ROOT}/dist}"
die () { echo "!! ${1}" >&2; exit 1; }
usage () {
echo "usage : ${0} check | <image>" >&2
echo "images : $(images | tr '\n' ' ')" >&2
exit 1
}
images () {
local f
for f in "${ROOT}"/images/*.pkrvars.hcl; do
basename "${f}" .pkrvars.hcl
done
}
sha () {
local bits="${1}"
shift
if command -v "sha${bits}sum" >/dev/null 2>&1; then
"sha${bits}sum" "$@"
else
shasum -a "${bits}" "$@"
fi
}
platform () {
case "$(uname -s)/$(uname -m)" in
Darwin/arm64) echo darwin_arm64 ;;
Linux/x86_64) echo linux_amd64 ;;
*) die "plateforme non prise en charge : $(uname -s)/$(uname -m)" ;;
esac
}
packer_bin () {
local zip dir
zip="packer_${PACKER_VERSION}_$(platform).zip"
dir="${CACHE}/packer/${PACKER_VERSION}"
if [ ! -x "${dir}/packer" ]; then
mkdir -p "${dir}"
echo "== téléchargement de ${zip}" >&2
curl -fsSL -o "${dir}/${zip}" "https://releases.hashicorp.com/packer/${PACKER_VERSION}/${zip}"
(cd "${dir}" && grep " ${zip}\$" <<< "${PACKER_SUMS}" | sha 256 -c --quiet -) >&2 || die "somme de ${zip} incorrecte"
(cd "${dir}" && unzip -q -o "${zip}" packer) >&2
fi
echo "${dir}/packer"
}
packer () {
local bin
bin=$(packer_bin)
PACKER_PLUGIN_PATH="${CACHE}/plugins" PACKER_CACHE_DIR="${CACHE}/packer_cache" CHECKPOINT_DISABLE=1 "${bin}" "$@"
}
accelerator () {
case "$(uname -s)" in
Linux) echo "kvm host" ;;
*) echo "tcg max" ;;
esac
}
KEYDIR=""
drop_key () {
[ -n "${KEYDIR}" ] || return 0
rm -f "${KEYDIR:?}/build" "${KEYDIR:?}/build.pub"
rmdir "${KEYDIR}"
KEYDIR=""
}
new_key () {
KEYDIR=$(mktemp -d "${TMPDIR:-/tmp}/two-images.XXXXXX")
trap drop_key EXIT
ssh-keygen -q -t ed25519 -N '' -C two-images-build -f "${KEYDIR}/build"
}
packer_vars () {
local image="${1}" output="${2}" accel cpu
read -r accel cpu <<< "$(accelerator)"
printf '%s\n' \
-var-file "${ROOT}/images/${image}.pkrvars.hcl" \
-var "name=${image}" \
-var "accelerator=${accel}" \
-var "cpu_model=${cpu}" \
-var "ssh_public_key=$(cat "${KEYDIR}/build.pub")" \
-var "ssh_private_key_file=${KEYDIR}/build" \
-var "output_directory=${output}"
}
check () {
local image script
packer init "${ROOT}/packer"
new_key
packer fmt -check -recursive "${ROOT}/packer" "${ROOT}/images"
for image in $(images); do
mapfile -t vars < <(packer_vars "${image}" "${KEYDIR}/output")
packer validate "${vars[@]}" "${ROOT}/packer" || die "${image} : template invalide"
done
drop_key
command -v shellcheck >/dev/null || die "shellcheck absent"
for script in "${ROOT}/build.sh" "${ROOT}/test.sh" "${ROOT}"/packer/scripts/*.sh; do
bash -n "${script}"
shellcheck "${script}"
done
echo "== check : OK"
}
build () {
local image="${1}" disk
[ -f "${ROOT}/images/${image}.pkrvars.hcl" ] || usage
command -v qemu-system-x86_64 >/dev/null || die "qemu-system-x86_64 absent"
command -v qemu-img >/dev/null || die "qemu-img absent"
packer init "${ROOT}/packer"
new_key
mapfile -t vars < <(packer_vars "${image}" "${OUTPUT}/${image}")
packer build -force "${vars[@]}" "${ROOT}/packer" || die "${image} : construction en échec"
drop_key
disk="${OUTPUT}/${image}/${image}-1"
[ -f "${disk}" ] || die "disque cible ${disk} introuvable"
mkdir -p "${DIST}"
qemu-img convert -c -O qcow2 "${disk}" "${DIST}/${image}.qcow2"
(cd "${DIST}" && sha 512 ./*.qcow2 | sed 's| \./| |' > SHA512SUMS)
echo "== ${DIST}/${image}.qcow2"
grep " ${image}.qcow2\$" "${DIST}/SHA512SUMS"
}
case "${1:-}" in
"") usage ;;
check) check ;;
*) build "${1}" ;;
esac

View file

@ -0,0 +1,4 @@
family = "debian"
image_url = "https://cloud.debian.org/images/cloud/trixie/20261021-2638/debian-13-genericcloud-amd64-20261021-2638.qcow2"
image_checksum = "sha512:16e0965312c56feb331049d45d2b1b5778f3f942a33d4300eedbb97527707f2bea87c0006f3e4cc7a25fadbff739c4e235a51fb9bcd1b66fd554f7f8e80ba45c"
target_gib = 3

View file

@ -0,0 +1,4 @@
family = "rocky"
image_url = "https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base-10.2-20260525.0.x86_64.qcow2"
image_checksum = "sha256:9fc9e9ff16888bb68ac39b0392e25c9c92684d50c85f1cce6ab549363bbc4b48"
target_gib = 10

25
metadata.py Normal file
View file

@ -0,0 +1,25 @@
import os
import sys
def main():
root = os.path.realpath(sys.argv[1])
request = sys.stdin.buffer.readline().decode("latin-1").split()
while sys.stdin.buffer.readline() not in (b"\r\n", b"\n", b""):
pass
name = request[1].lstrip("/") if len(request) > 1 else ""
path = os.path.realpath(os.path.join(root, name))
out = sys.stdout.buffer
if name and os.path.dirname(path) == root and os.path.isfile(path):
with open(path, "rb") as f:
body = f.read()
out.write(b"HTTP/1.0 200 OK\r\n")
else:
body = b""
out.write(b"HTTP/1.0 404 Not Found\r\n")
out.write(b"Content-Length: %d\r\nConnection: close\r\n\r\n" % len(body))
out.write(body)
out.flush()
main()

60
packer/scripts/check.sh Normal file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env bash
set -euo pipefail
FAILED=0
check () {
local desc="${1}"
shift
if "$@"; then
echo "RÉUSSI: ${desc}"
else
echo "ÉCHOUÉ: ${desc}"
FAILED=1
fi
}
empty_or_absent () {
[ ! -e "${1}" ] || [ -z "$(find "${1}" -mindepth 1 -print -quit)" ]
}
no_match () {
local dir="${1}"
shift
[ ! -d "${dir}" ] || [ -z "$(find "${dir}" "$@" -print -quit)" ]
}
has_line () {
grep -qxF -- "${2}" "${1}"
}
no_build_user () {
! grep -q '^build:' /mnt/etc/passwd
}
machine_id_blank () {
[ ! -s /mnt/etc/machine-id ]
}
check "/var/lib/cloud vide" empty_or_absent /mnt/var/lib/cloud
check "aucune clé d'hôte SSH" no_match /mnt/etc/ssh -maxdepth 1 -name 'ssh_host_*'
check "aucun authorized_keys sous /root" no_match /mnt/root -name authorized_keys
check "aucun authorized_keys sous /home" no_match /mnt/home -name authorized_keys
check "machine-id vide" machine_id_blank
check "utilisateur de construction absent" no_build_user
check "aucun journal" no_match /mnt/var/log ! -type d
check "source NoCloud vers 169.254.169.254" has_line /mnt/etc/cloud/cloud.cfg.d/99_metadata.cfg " seedfrom: 'http://169.254.169.254:80/'"
check "network-config réappliqué à chaque démarrage" has_line /mnt/etc/cloud/cloud.cfg.d/98_network_updates.cfg " when: [boot-new-instance, boot, boot-legacy]"
check "règle udev xvd" test -s /mnt/etc/udev/rules.d/90-two-xvd.rules
case "${FAMILY}" in
debian)
check "DHCP sur toutes les interfaces (networkd)" has_line /mnt/etc/systemd/network/90-all-en.network "Name=en*"
;;
rocky)
check "DHCP sur toutes les interfaces (NetworkManager)" has_line /mnt/etc/NetworkManager/system-connections/90-all-en.nmconnection "interface-name=en*"
check "SELinux désactivé" has_line /mnt/etc/selinux/config "SELINUX=disabled"
check "utilisateur par défaut syonad" has_line /mnt/etc/cloud/cloud.cfg.d/20_user.cfg " name: syonad"
;;
esac
[ "${FAILED}" -eq 0 ] || { echo "!! image refusée" >&2; exit 1; }

20
packer/scripts/clean.sh Normal file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -euo pipefail
empty_dir () {
[ -d "${1}" ] && find "${1}" -mindepth 1 -delete
return 0
}
for d in /mnt/var/cache/dnf /mnt/var/cache/yum /mnt/root/.ssh /mnt/tmp /mnt/var/tmp /mnt/var/lib/dhcp /mnt/var/lib/cloud; do
empty_dir "${d}"
done
rm -f /mnt/root/.bash_history
find /mnt/var/log ! -type d -delete
find /mnt/etc/ssh -maxdepth 1 -name 'ssh_host_*' -delete
if [ -f /mnt/etc/machine-id ]; then
: > /mnt/etc/machine-id
fi
if [ -f /mnt/var/lib/dbus/machine-id ] && [ ! -L /mnt/var/lib/dbus/machine-id ]; then
rm -f /mnt/var/lib/dbus/machine-id
fi

10
packer/scripts/debian.sh Normal file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
cat > /mnt/etc/systemd/network/90-all-en.network <<'EOF'
[Match]
Name=en*
[Network]
DHCP=ipv4
EOF

View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail
mapfile -t filesystems < <(findmnt -rno TARGET,FSTYPE -R /mnt | awk '$2 == "ext4" || $2 == "xfs" || $2 == "vfat" { print $1 }')
for fs in "${filesystems[@]}"; do
fstrim -v "${fs}"
done
umount -R /mnt
sync

63
packer/scripts/rocky.sh Normal file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env bash
set -euo pipefail
die () { echo "!! ${1}" >&2; exit 1; }
unit_exists () {
[ -e "/mnt/usr/lib/systemd/system/${1}" ] || [ -e "/mnt/etc/systemd/system/${1}" ]
}
echo "The default user for Syonad VMs is 'syonad'." > /mnt/etc/banner
augtool -r /mnt -s <<'EOF'
set /files/etc/ssh/sshd_config/X11Forwarding no
set /files/etc/ssh/sshd_config/PermitTunnel no
set /files/etc/ssh/sshd_config/PermitRootLogin no
set /files/etc/ssh/sshd_config/RSAAuthentication yes
set /files/etc/ssh/sshd_config/PubkeyAuthentication yes
set /files/etc/ssh/sshd_config/PasswordAuthentication no
set /files/etc/ssh/sshd_config/UseDNS no
set /files/etc/ssh/sshd_config/ChallengeResponseAuthentication no
set /files/etc/ssh/sshd_config/GSSAPIAuthentication no
set /files/etc/ssh/sshd_config/Match[1]/Condition/User "root,centos,ubuntu,debian,ec2-user"
set /files/etc/ssh/sshd_config/Match[1]/Settings/Banner "/etc/banner"
EOF
cat > /mnt/etc/cloud/cloud.cfg.d/20_user.cfg <<'EOF'
system_info:
default_user:
name: syonad
EOF
install -m 0600 /dev/stdin /mnt/etc/NetworkManager/system-connections/90-all-en.nmconnection <<'EOF'
[connection]
id=two-all-en
type=ethernet
autoconnect-priority=-100
multi-connect=3
[match]
interface-name=en*
[ipv4]
method=auto
[ipv6]
method=ignore
EOF
chroot /mnt systemctl enable fstrim.timer
for unit in rpcbind.service rpcbind.socket; do
if unit_exists "${unit}"; then
chroot /mnt systemctl disable "${unit}"
fi
done
augtool -r /mnt -s set /files/etc/selinux/config/SELINUX disabled
mapfile -t cockpit < <(chroot /mnt rpm -qa 'cockpit*')
if [ "${#cockpit[@]}" -gt 0 ]; then
chroot /mnt dnf -y -q --disablerepo='*' remove "${cockpit[@]}"
fi
[ -z "$(chroot /mnt rpm -qa 'cockpit*')" ] || die "cockpit toujours installé"
rm -rf /mnt/run/cockpit

91
packer/scripts/target.sh Normal file
View file

@ -0,0 +1,91 @@
#!/usr/bin/env bash
set -euo pipefail
die () { echo "!! ${1}" >&2; exit 1; }
disk_of_size () {
lsblk -bdnpo NAME,SIZE,TYPE | awk -v s="${1}" '$2 == s && $3 == "disk" { print $1 }'
}
assert_blank () {
local disk="${1}"
[ -z "$(lsblk -nro FSTYPE,PTTYPE "${disk}" | tr -d ' \n')" ] || die "${disk} porte déjà une table ou un système de fichiers"
[ "$(lsblk -nro NAME "${disk}" | wc -l)" -eq 1 ] || die "${disk} a des partitions"
if findmnt -rno SOURCE | grep -qF "${disk}"; then die "${disk} est monté"; fi
cmp -s -n 1048576 "${disk}" /dev/zero || die "${disk} n'est pas vierge (premier Mio non nul)"
}
wait_cloud_init () {
local rc=0
cloud-init status --wait >/dev/null || rc=$?
[ "${rc}" -eq 0 ] || [ "${rc}" -eq 2 ] || die "cloud-init de la VM de construction en échec (code ${rc})"
}
install_tools () {
case "${FAMILY}" in
rocky) dnf -y -q install qemu-img augeas xfsprogs ;;
debian) DEBIAN_FRONTEND=noninteractive apt-get -qq update \
&& DEBIAN_FRONTEND=noninteractive apt-get -qq install -y --no-install-recommends qemu-utils e2fsprogs ;;
esac
}
mount_part () {
if [ "$(lsblk -nro FSTYPE "${1}")" = xfs ]; then
mount -o nouuid "${1}" "${2}"
else
mount "${1}" "${2}"
fi
}
partition_by_uuid () {
lsblk -lnpo NAME,UUID,TYPE "${TARGET}" | awk -v u="${1}" '$2 == u && $3 == "part" { print $1 }'
}
mount_target () {
local root uuid mnt part
root=$(lsblk -lnbpo NAME,SIZE,TYPE,FSTYPE "${TARGET}" | awk '$3 == "part" && ($4 == "ext4" || $4 == "xfs")' | sort -k2 -n | tail -n 1 | cut -d' ' -f1)
[ -n "${root}" ] || die "aucune partition racine sur ${TARGET}"
mkdir -p /mnt
mount_part "${root}" /mnt
while read -r uuid mnt; do
part=$(partition_by_uuid "${uuid}")
[ -n "${part}" ] || die "partition UUID=${uuid} (${mnt}) introuvable sur ${TARGET}"
mount_part "${part}" "/mnt${mnt}"
done < <(awk '$1 ~ /^UUID=/ && ($2 == "/boot" || $2 == "/boot/efi") { sub(/^UUID=/, "", $1); print $1, $2 }' /mnt/etc/fstab | sort -k2)
mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
}
[ -n "${FAMILY:-}" ] && [ -n "${IMAGE_URL:-}" ] && [ -n "${IMAGE_CHECKSUM:-}" ] || die "FAMILY, IMAGE_URL, IMAGE_CHECKSUM requis"
[ "${TARGET_BYTES:-0}" -gt 0 ] && [ "${WORK_BYTES:-0}" -gt 0 ] || die "TARGET_BYTES et WORK_BYTES requis"
[ "${TARGET_BYTES}" -ne "${WORK_BYTES}" ] || die "disque cible et disque de travail de même taille : impossible de les distinguer"
wait_cloud_init
install_tools
[ "$(disk_of_size "${TARGET_BYTES}" | wc -l)" -eq 1 ] || die "pas exactement un disque de ${TARGET_BYTES} octets"
[ "$(disk_of_size "${WORK_BYTES}" | wc -l)" -eq 1 ] || die "pas exactement un disque de ${WORK_BYTES} octets"
TARGET=$(disk_of_size "${TARGET_BYTES}")
WORK=$(disk_of_size "${WORK_BYTES}")
ROOT_DISK=/dev/$(lsblk -nro PKNAME "$(findmnt -nro SOURCE /)")
[ "${TARGET}" != "${ROOT_DISK}" ] && [ "${WORK}" != "${ROOT_DISK}" ] || die "le disque système a la taille d'un disque de construction"
assert_blank "${TARGET}"
assert_blank "${WORK}"
echo "== cible ${TARGET}, travail ${WORK}, système ${ROOT_DISK}"
mkfs.ext4 -q -F "${WORK}"
mkdir -p /work
mount "${WORK}" /work
curl -fsSL -o /work/source.qcow2 "${IMAGE_URL}"
ALGO=${IMAGE_CHECKSUM%%:*}
echo "${IMAGE_CHECKSUM#*:} /work/source.qcow2" | "${ALGO}sum" -c --quiet - || die "somme ${ALGO} de l'image incorrecte"
VSIZE=$(qemu-img info --output=json /work/source.qcow2 | python3 -c 'import json, sys; print(json.load(sys.stdin)["virtual-size"])')
[ -n "${VSIZE}" ] && [ "${VSIZE}" -le "${TARGET_BYTES}" ] || die "taille virtuelle de l'image (${VSIZE:-illisible}) supérieure au disque cible"
qemu-img convert -n --target-is-zero -f qcow2 -O raw /work/source.qcow2 "${TARGET}"
umount /work
blockdev --rereadpt "${TARGET}"
udevadm settle
printf 'TARGET=%s\n' "${TARGET}" > /run/two-images.env
mount_target
findmnt -R /mnt

22
packer/scripts/two.sh Normal file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
cat > /mnt/etc/cloud/cloud.cfg.d/99_metadata.cfg <<'EOF'
datasource_list: [ NoCloud ]
datasource:
NoCloud:
seedfrom: 'http://169.254.169.254:80/'
timeout: 5
max_wait: 10
EOF
cat > /mnt/etc/cloud/cloud.cfg.d/98_network_updates.cfg <<'EOF'
updates:
network:
when: [boot-new-instance, boot, boot-legacy]
EOF
cat > /mnt/etc/udev/rules.d/90-two-xvd.rules <<'EOF'
SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL_SHORT}=="xvd[a-z]", SYMLINK+="$env{ID_SERIAL_SHORT}"
SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", ENV{ID_SERIAL_SHORT}=="xvd[a-z]", SYMLINK+="$env{ID_SERIAL_SHORT}%n"
EOF

View file

@ -0,0 +1,8 @@
#cloud-config
users:
- name: build
shell: /bin/bash
lock_passwd: true
sudo: "ALL=(ALL) NOPASSWD:ALL"
ssh_authorized_keys:
- ${key}

116
packer/two-image.pkr.hcl Normal file
View file

@ -0,0 +1,116 @@
packer {
required_version = "= 1.16.2"
required_plugins {
qemu = {
source = "github.com/hashicorp/qemu"
version = "= 1.1.7"
}
}
}
variable "name" {
type = string
}
variable "family" {
type = string
validation {
condition = contains(["debian", "rocky"], var.family)
error_message = "La famille doit valoir debian ou rocky."
}
}
variable "image_url" {
type = string
}
variable "image_checksum" {
type = string
validation {
condition = can(regex("^sha(256:[0-9a-f]{64}|512:[0-9a-f]{128})$", var.image_checksum))
error_message = "La somme doit être de la forme sha256:<64 hex> ou sha512:<128 hex>."
}
}
variable "target_gib" {
type = number
}
variable "work_gib" {
type = number
default = 2
}
variable "accelerator" {
type = string
}
variable "cpu_model" {
type = string
}
variable "ssh_public_key" {
type = string
}
variable "ssh_private_key_file" {
type = string
}
variable "output_directory" {
type = string
}
source "qemu" "image" {
vm_name = var.name
iso_url = var.image_url
iso_checksum = var.image_checksum
disk_image = true
use_backing_file = true
skip_resize_disk = true
skip_compaction = true
format = "qcow2"
disk_interface = "virtio"
disk_additional_size = ["${var.target_gib}G", "${var.work_gib}G"]
disk_discard = "unmap"
disk_detect_zeroes = "unmap"
accelerator = var.accelerator
cpu_model = var.cpu_model
cpus = 2
memory = 2048
headless = true
output_directory = var.output_directory
cd_label = "cidata"
cd_content = {
"meta-data" = "instance-id: two-images-build\n"
"user-data" = templatefile("${path.root}/seed/user-data.pkrtpl", { key = var.ssh_public_key })
}
communicator = "ssh"
ssh_username = "build"
ssh_private_key_file = var.ssh_private_key_file
ssh_timeout = "60m"
shutdown_command = "sudo shutdown -P now"
}
build {
sources = ["source.qemu.image"]
provisioner "shell" {
execute_command = "sudo {{ .Vars }} bash '{{ .Path }}'"
environment_vars = [
"FAMILY=${var.family}",
"IMAGE_URL=${var.image_url}",
"IMAGE_CHECKSUM=${var.image_checksum}",
"TARGET_BYTES=${var.target_gib * 1073741824}",
"WORK_BYTES=${var.work_gib * 1073741824}",
]
scripts = [
"${path.root}/scripts/target.sh",
"${path.root}/scripts/two.sh",
"${path.root}/scripts/${var.family}.sh",
"${path.root}/scripts/clean.sh",
"${path.root}/scripts/check.sh",
"${path.root}/scripts/release.sh",
]
}
}

167
test.sh Executable file
View file

@ -0,0 +1,167 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
DIST="${TWO_IMAGES_DIST:-${ROOT}/dist}"
PORT="${TWO_IMAGES_SSH_PORT:-2229}"
RUN=""
QEMU_PID=""
FAILED=0
KEEP=0
die () { KEEP=1; echo "!! ${1}" >&2; exit 1; }
stop_qemu () {
local rc=0
kill "${QEMU_PID}"
wait "${QEMU_PID}" || rc=$?
[ "${rc}" -eq 0 ] || [ "${rc}" -eq 143 ] || echo "!! QEMU sorti avec le code ${rc}" >&2
QEMU_PID=""
}
cleanup () {
if [ -n "${QEMU_PID}" ] && kill -0 "${QEMU_PID}"; then
stop_qemu
fi
[ -n "${RUN}" ] || return 0
rm -f "${RUN:?}/key" "${RUN:?}/key.pub" "${RUN:?}/root.qcow2" "${RUN:?}/data.qcow2" \
"${RUN:?}/seed/meta-data" "${RUN:?}/seed/user-data" "${RUN:?}/seed/vendor-data"
rmdir "${RUN}/seed"
if [ "${KEEP}" -eq 1 ]; then
echo "!! console conservée : ${RUN}/console.log" >&2
return 0
fi
rm -f "${RUN:?}/console.log"
rmdir "${RUN}"
}
vm_closing () {
local rc=0
vm "$@" || rc=$?
[ "${rc}" -eq 0 ] || [ "${rc}" -eq 255 ] || die "commande « $* » en échec (code ${rc})"
}
boot_id () {
vm 'cat /proc/sys/kernel/random/boot_id'
}
wait_reboot () {
local before="${1}" id=""
for _ in $(seq 1 120); do
if id=$(boot_id 2>/dev/null) && [ -n "${id}" ] && [ "${id}" != "${before}" ]; then
return 0
fi
sleep 10
done
die "pas de redémarrage constaté"
}
wait_exit () {
for _ in $(seq 1 60); do
kill -0 "${QEMU_PID}" 2>/dev/null || { wait "${QEMU_PID}"; QEMU_PID=""; return 0; }
sleep 5
done
die "la VM ne s'arrête pas"
}
check () {
local desc="${1}"
shift
if "$@"; then
echo "RÉUSSI: ${desc}"
else
echo "ÉCHOUÉ: ${desc}"
FAILED=1
fi
}
accel () {
case "$(uname -s)" in
Linux) echo "-accel kvm -cpu host" ;;
*) echo "-accel tcg,thread=multi -cpu max" ;;
esac
}
vm () {
ssh -i "${RUN}/key" -p "${PORT}" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR -o ConnectTimeout=5 -o BatchMode=yes -n syonad@127.0.0.1 "$@"
}
wait_ssh () {
for _ in $(seq 1 120); do
vm true 2>/dev/null && return 0
sleep 10
done
die "SSH injoignable (console : ${RUN}/console.log)"
}
boot () {
local -a accel_args
read -r -a accel_args <<< "$(accel)"
qemu-system-x86_64 "${accel_args[@]}" -machine pc -m 1536 -smp 2 -nographic \
-serial "file:${RUN}/console.log" -monitor none \
-drive "file=${RUN}/root.qcow2,format=qcow2,if=none,id=root" \
-device virtio-blk-pci,drive=root,bootindex=0 \
-device virtio-scsi-pci,id=scsi0 \
-drive "file=${RUN}/data.qcow2,format=qcow2,if=none,id=data" \
-device scsi-hd,drive=data,bus=scsi0.0,serial=xvdb \
-netdev "user,id=n0,net=169.254.169.0/24,hostfwd=tcp:127.0.0.1:${PORT}-:22,guestfwd=tcp:169.254.169.254:80-cmd:python3 -I ${ROOT}/metadata.py ${RUN}/seed" \
-device virtio-net-pci,netdev=n0 \
-netdev user,id=n1,net=10.0.3.0/24 -device virtio-net-pci,netdev=n1 &
QEMU_PID=$!
}
two_leases () {
[ "$(vm "ip -4 -o addr show | awk '\$2 ~ /^en/ { print \$2 }' | sort -u | wc -l")" -ge 2 ]
}
second_nic_by_two () {
local nic
nic=$(vm "ip -o link show | awk -F': ' '\$2 ~ /^en/ { print \$2 }' | sort | sed -n 2p")
[ -n "${nic}" ] || return 1
if vm 'command -v nmcli' >/dev/null; then
[ "$(vm "nmcli -g GENERAL.CONNECTION device show ${nic}")" = two-all-en ]
else
vm "networkctl status ${nic}" | grep -q 'Network File: /etc/systemd/network/90-all-en.network'
fi
}
cloud_init_done () {
vm 'cloud-init status --wait' | grep -q 'status: done'
}
image="${1:-}"
[ -n "${image}" ] && [ -f "${DIST}/${image}.qcow2" ] || die "usage : ${0} <image> (fichier ${DIST}/<image>.qcow2)"
RUN=$(mktemp -d "${TMPDIR:-/tmp}/two-images-test.XXXXXX")
trap cleanup EXIT
mkdir "${RUN}/seed"
ssh-keygen -q -t ed25519 -N '' -C two-images-test -f "${RUN}/key"
printf 'instance-id: two-images-test\nlocal-hostname: two-images-test\n' > "${RUN}/seed/meta-data"
printf '#cloud-config\nusers:\n - name: syonad\n shell: /bin/bash\n sudo: "ALL=(ALL) NOPASSWD:ALL"\n ssh_authorized_keys:\n - %s\n' \
"$(cat "${RUN}/key.pub")" > "${RUN}/seed/user-data"
: > "${RUN}/seed/vendor-data"
qemu-img create -q -f qcow2 -b "${DIST}/${image}.qcow2" -F qcow2 "${RUN}/root.qcow2"
qemu-img create -q -f qcow2 "${RUN}/data.qcow2" 1G
boot
wait_ssh
check "cloud-init terminé sans erreur" cloud_init_done
check "nom d'hôte servi par 169.254.169.254" test "$(vm hostname)" = two-images-test
check "bail DHCP sur les deux interfaces" two_leases
check "seconde interface configurée par le fichier de two" second_nic_by_two
check "disque additionnel en /dev/xvdb" vm 'test -b /dev/xvdb'
check "aucune clé de construction" vm '! sudo grep -rq two-images-build /home /root /etc/ssh'
if vm 'command -v getenforce' >/dev/null; then
check "SELinux désactivé" test "$(vm getenforce)" = Disabled
fi
BOOT=$(boot_id)
vm_closing 'sudo systemctl reboot'
wait_reboot "${BOOT}"
check "bail DHCP sur les deux interfaces après redémarrage" two_leases
check "disque additionnel en /dev/xvdb après redémarrage" vm 'test -b /dev/xvdb'
vm_closing 'sudo systemctl poweroff'
wait_exit
[ "${FAILED}" -eq 0 ] || die "${image} : test en échec"
echo "== ${image} : test OK"