| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
set -Eeuo pipefail |
| 89 |
|
| 90 |
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 91 |
REPO_ROOT="$(cd "$HERE/../.." && pwd)" |
| 92 |
SCRATCH="${VM_STATE:-$HERE/state}" |
| 93 |
export VM_STATE="$SCRATCH" |
| 94 |
|
| 95 |
KEY="${VMTEST_KEY:-$HOME/.ssh/id_ed25519}" |
| 96 |
ISO="${ALLOY_ISO:-$REPO_ROOT/output/install.iso}" |
| 97 |
DISK_NAME="${VMTEST_DISK:-vda}" |
| 98 |
HOSTNAME_="${VMTEST_HOSTNAME:-alloytest}" |
| 99 |
USER_="${VMTEST_USER:-tester}" |
| 100 |
PASSWORD="${VMTEST_PASSWORD:-alloytest}" |
| 101 |
PORT="${VMTEST_PORT:-2222}" |
| 102 |
KEEP_STATE=0 |
| 103 |
|
| 104 |
|
| 105 |
VIA="${VMTEST_VIA:-ssh}" |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
LAYER_TIMEOUT="${VMTEST_LAYER_TIMEOUT:-900}" |
| 110 |
INSTALL_TIMEOUT="${VMTEST_INSTALL_TIMEOUT:-1800}" |
| 111 |
SSH_TIMEOUT="${VMTEST_SSH_TIMEOUT:-300}" |
| 112 |
|
| 113 |
QEMU_PID="" |
| 114 |
|
| 115 |
die() { printf 'error: %s\n' "$*" >&2; exit 3; } |
| 116 |
fail() { printf 'FAIL: %s\n' "$*" >&2; FAILED=$((FAILED + 1)); } |
| 117 |
say() { printf '==> %s\n' "$*"; } |
| 118 |
FAILED=0 |
| 119 |
|
| 120 |
while [ $# -gt 0 ]; do |
| 121 |
case "$1" in |
| 122 |
--key) KEY="${2:?--key needs a path}"; shift 2 ;; |
| 123 |
--iso) ISO="${2:?--iso needs a path}"; shift 2 ;; |
| 124 |
--disk) DISK_NAME="${2:?--disk needs a name}"; shift 2 ;; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
--keep-state) KEEP_STATE=1; shift ;; |
| 131 |
-h|--help) sed -n '2,80p' "$0"; exit 0 ;; |
| 132 |
*) die "unknown argument: $1" ;; |
| 133 |
esac |
| 134 |
done |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
for tool in qemu-system-x86_64 swtpm swtpm_setup python3 ssh; do |
| 139 |
command -v "$tool" >/dev/null 2>&1 || die "no $tool" |
| 140 |
done |
| 141 |
[ -f /usr/share/OVMF/OVMF_CODE_4M.fd ] || die "no OVMF at /usr/share/OVMF" |
| 142 |
[ -f "$KEY" ] || die "no private key at $KEY" |
| 143 |
[ -f "$KEY.pub" ] || die "no public key at $KEY.pub" |
| 144 |
[ "$KEEP_STATE" = 1 ] || [ -f "$ISO" ] || die "no ISO at $ISO; build/build-iso.sh first" |
| 145 |
|
| 146 |
SSH_OPTS=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null |
| 147 |
-o LogLevel=ERROR -o ConnectTimeout=10 -o BatchMode=yes |
| 148 |
-p "$PORT" -i "$KEY") |
| 149 |
|
| 150 |
qmp() { python3 "$HERE/qmp.py" "$@"; } |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
guest() { ssh "${SSH_OPTS[@]}" "$USER_@127.0.0.1" 'sh -s' "$@"; } |
| 166 |
|
| 167 |
boot() { |
| 168 |
|
| 169 |
"$HERE/run-vm.sh" "$1" >"$SCRATCH/qemu-$1.log" 2>&1 & |
| 170 |
QEMU_PID=$! |
| 171 |
sleep 1 |
| 172 |
kill -0 "$QEMU_PID" 2>/dev/null || { |
| 173 |
cat "$SCRATCH/qemu-$1.log" >&2 |
| 174 |
die "qemu died on '$1'" |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
shutdown_vm() { |
| 179 |
[ -n "$QEMU_PID" ] || return 0 |
| 180 |
qmp cmd quit >/dev/null 2>&1 || true |
| 181 |
|
| 182 |
|
| 183 |
for _ in $(seq 1 40); do |
| 184 |
kill -0 "$QEMU_PID" 2>/dev/null || { QEMU_PID=""; return 0; } |
| 185 |
sleep 0.25 |
| 186 |
done |
| 187 |
kill -9 "$QEMU_PID" 2>/dev/null || true |
| 188 |
wait "$QEMU_PID" 2>/dev/null || true |
| 189 |
QEMU_PID="" |
| 190 |
} |
| 191 |
|
| 192 |
cleanup() { |
| 193 |
[ -n "$QEMU_PID" ] && kill -9 "$QEMU_PID" 2>/dev/null |
| 194 |
return 0 |
| 195 |
} |
| 196 |
trap cleanup EXIT |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if [ "$KEEP_STATE" = 1 ]; then |
| 201 |
[ -f "$SCRATCH/target.qcow2" ] || die "--keep-state, but there is no state/target.qcow2 to keep" |
| 202 |
say "reusing the install in state/target.qcow2" |
| 203 |
else |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
say "resetting state/" |
| 208 |
rm -f "$SCRATCH/target.qcow2" "$SCRATCH/OVMF_VARS.fd" |
| 209 |
rm -rf "$SCRATCH/tpm" |
| 210 |
|
| 211 |
say "booting the installer medium" |
| 212 |
ALLOY_ISO="$ISO" boot live |
| 213 |
|
| 214 |
say "driving the wizard" |
| 215 |
rc=0 |
| 216 |
python3 "$HERE/install_drive.py" \ |
| 217 |
--via "$VIA" --key "$KEY" --pubkey "$(cat "$KEY.pub")" --disk "$DISK_NAME" \ |
| 218 |
--hostname "$HOSTNAME_" --user "$USER_" --password "$PASSWORD" \ |
| 219 |
--install-timeout "$INSTALL_TIMEOUT" || rc=$? |
| 220 |
shutdown_vm |
| 221 |
[ "$rc" -eq 0 ] || { |
| 222 |
[ "$rc" -eq 1 ] && { printf 'FAIL: the install itself failed, so the first boot was never reached\n' >&2; exit 1; } |
| 223 |
die "the wizard could not be driven to a verdict" |
| 224 |
} |
| 225 |
say "installed" |
| 226 |
fi |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
say "booting the installed machine with its link down" |
| 231 |
boot installed |
| 232 |
|
| 233 |
|
| 234 |
qmp cmd set_link '{"name": "nic0", "up": false}' >/dev/null |
| 235 |
|
| 236 |
say "waiting up to ${LAYER_TIMEOUT}s for the machine to lay its components down and reboot" |
| 237 |
REBOOTED=1 |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
qmp wait RESET "$LAYER_TIMEOUT" '{"guest": true}' >/dev/null 2>&1 || REBOOTED=0 |
| 242 |
[ "$REBOOTED" = 1 ] && say "it rebooted" |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
say "putting the link back up" |
| 250 |
qmp cmd set_link '{"name": "nic0", "up": true}' >/dev/null |
| 251 |
|
| 252 |
say "waiting up to ${SSH_TIMEOUT}s for ssh" |
| 253 |
reachable=0 |
| 254 |
end=$((SECONDS + SSH_TIMEOUT)) |
| 255 |
while [ "$SECONDS" -lt "$end" ]; do |
| 256 |
if echo true | guest >/dev/null 2>&1; then reachable=1; break; fi |
| 257 |
sleep 5 |
| 258 |
done |
| 259 |
[ "$reachable" = 1 ] || { |
| 260 |
shutdown_vm |
| 261 |
die "the machine never answered ssh, so nothing can be read off it. \ |
| 262 |
state/serial-installed.log and state/qemu-installed.log are what there is." |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
CONSOLE=1 |
| 270 |
echo 'test -x /usr/bin/alloy' | guest >/dev/null 2>&1 || CONSOLE=0 |
| 271 |
|
| 272 |
if [ "$REBOOTED" = 0 ] && [ "$CONSOLE" = 1 ]; then |
| 273 |
|
| 274 |
|
| 275 |
shutdown_vm |
| 276 |
die "this machine had already been first-booted: it has a console and never \ |
| 277 |
ran the unit. Reset state/ and install again, or drop --keep-state." |
| 278 |
fi |
| 279 |
|
| 280 |
if [ "$REBOOTED" = 0 ]; then |
| 281 |
fail "the machine never rebooted, so alloy-layer-components.service did not finish" |
| 282 |
fi |
| 283 |
if [ "$CONSOLE" = 1 ]; then |
| 284 |
say "/usr/bin/alloy: present ($(echo '/usr/bin/alloy --version' | guest 2>/dev/null | head -1))" |
| 285 |
else |
| 286 |
fail "/usr/bin/alloy is absent: the machine came up with no console" |
| 287 |
printf '\nwhat the unit said:\n' >&2 |
| 288 |
echo 'journalctl -u alloy-layer-components.service --no-pager -o cat' \ |
| 289 |
| guest 2>&1 | tail -40 >&2 || true |
| 290 |
fi |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
enabled_repos() { |
| 302 |
guest "$1" <<'AWKEOF' 2>/dev/null || true |
| 303 |
awk ' |
| 304 |
/^\[/ { id = substr($0, 2, index($0, "]") - 2) } |
| 305 |
/^enabled[ \t]*=[ \t]*1[ \t]*$/ { if (id != "") print id } |
| 306 |
' "$1"/*.repo | sort -u |
| 307 |
AWKEOF |
| 308 |
} |
| 309 |
LIVE_REPOS="$(enabled_repos /etc/yum.repos.d)" |
| 310 |
IMAGE_REPOS="$(enabled_repos /usr/etc/yum.repos.d)" |
| 311 |
|
| 312 |
if [ -z "$LIVE_REPOS" ]; then |
| 313 |
fail "no repo is enabled on the machine at all, so the fence was never lowered" |
| 314 |
elif [ "$LIVE_REPOS" != "$IMAGE_REPOS" ]; then |
| 315 |
fail "the enabled repo set does not match the image's own" |
| 316 |
printf ' /etc: %s\n' "$(echo "$LIVE_REPOS" | paste -sd' ')" >&2 |
| 317 |
printf ' /usr/etc: %s\n' "$(echo "$IMAGE_REPOS" | paste -sd' ')" >&2 |
| 318 |
else |
| 319 |
count="$(printf '%s\n' "$LIVE_REPOS" | wc -l)" |
| 320 |
echo "$LIVE_REPOS" | grep -qx 'alloy-local' \ |
| 321 |
|| fail "alloy-local is not enabled, so the carried repo is not reachable" |
| 322 |
[ "$count" -eq 5 ] \ |
| 323 |
|| fail "$count repos are enabled, not the five this expects (alloy-local plus Fedora's four): $(echo "$LIVE_REPOS" | paste -sd' ')" |
| 324 |
[ "$count" -eq 5 ] && say "repos: $(echo "$LIVE_REPOS" | paste -sd' ')" |
| 325 |
fi |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
say "running check-installed.sh on the guest" |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
CI_RAN=0 |
| 346 |
ci=0 |
| 347 |
if ! ssh "${SSH_OPTS[@]}" "$USER_@127.0.0.1" "sh -c 'cat > /tmp/check-installed.sh'" \ |
| 348 |
< "$REPO_ROOT/build/check-installed.sh"; then |
| 349 |
printf 'note: could not write check-installed.sh to the guest (above)\n' >&2 |
| 350 |
else |
| 351 |
CI_RAN=1 |
| 352 |
guest <<EOF || ci=$? |
| 353 |
printf '%s\n' '$PASSWORD' | sudo -S -p '' bash /tmp/check-installed.sh |
| 354 |
EOF |
| 355 |
fi |
| 356 |
|
| 357 |
if [ "$CI_RAN" = 0 ]; then |
| 358 |
: |
| 359 |
else |
| 360 |
case "$ci" in |
| 361 |
0) say "check-installed: clean" ;; |
| 362 |
1) fail "check-installed found a defect on the installed machine (above)" ;; |
| 363 |
|
| 364 |
|
| 365 |
*) printf 'note: check-installed could not run (exit %s)\n' "$ci"; CI_RAN=0 ;; |
| 366 |
esac |
| 367 |
fi |
| 368 |
|
| 369 |
shutdown_vm |
| 370 |
|
| 371 |
if [ "$FAILED" -ne 0 ]; then |
| 372 |
printf '\n%s check(s) failed. The machine did not survive an offline first boot.\n' "$FAILED" >&2 |
| 373 |
exit 1 |
| 374 |
fi |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if [ "$CI_RAN" = 0 ]; then |
| 380 |
printf '\nThe offline first boot passed: a console was laid down and the repo set is the ordinary one.\n' |
| 381 |
die "but check-installed.sh did not run, so the labels are unchecked" |
| 382 |
fi |
| 383 |
|
| 384 |
printf '\nAn offline first boot laid the console down, left the ordinary repo set, and labelled /etc correctly.\n' |
| 385 |
|