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
Some checks failed
Tests / check (push) Failing after 39s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
commit
fe401b9a12
18 changed files with 937 additions and 0 deletions
60
packer/scripts/check.sh
Normal file
60
packer/scripts/check.sh
Normal 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
20
packer/scripts/clean.sh
Normal 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
10
packer/scripts/debian.sh
Normal 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
|
||||
9
packer/scripts/release.sh
Normal file
9
packer/scripts/release.sh
Normal 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
63
packer/scripts/rocky.sh
Normal 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
91
packer/scripts/target.sh
Normal 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
22
packer/scripts/two.sh
Normal 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
|
||||
8
packer/seed/user-data.pkrtpl
Normal file
8
packer/seed/user-data.pkrtpl
Normal 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
116
packer/two-image.pkr.hcl
Normal 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",
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue