build-host.sh: Style and syntax fixes

.gitlab/ci/build-host.sh:
Set shebang to /usr/bin/env bash to be more portable/flexible.
Turn all posix statements ([]) to bash style statements ([[]]), as we are using bash.
Terminate the list of parameters to rm or cp with --.

Replace the implementation of finding a local ISO to use with one that relies on a sorted list of potential images.

Use virtio-net-pci for networking with qemu.
Set the cow_spacesize to 4G for the archiso environment.

Use --needed in the call to pacman to not re-install already up-to-date targets.
Attempt a full system upgrade (but ignore the kernel).
Increase the timeout for when installing packages to the archiso environment using pacman to 120s, as a system upgrade
is being done as well.

Use systemctl poweroff -i to shut down the virtual machine as it is more future proof and robust.
This commit is contained in:
David Runge 2021-01-31 20:50:40 +01:00
parent 428bf47370
commit b588c52665
No known key found for this signature in database
GPG Key ID: 7258734B41C31549

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
# build-host.sh runs build-inside-vm.sh in a qemu VM running the latest Arch installer iso # build-host.sh runs build-inside-vm.sh in a qemu VM running the latest Arch installer iso
# #
# nounset: "Treat unset variables and parameters [...] as an error when performing parameter expansion." # nounset: "Treat unset variables and parameters [...] as an error when performing parameter expansion."
@ -17,21 +17,30 @@ function init() {
# Do some cleanup when the script exits # Do some cleanup when the script exits
function cleanup() { function cleanup() {
rm -rf "${TMPDIR}" rm -rf -- "${TMPDIR}"
jobs -p | xargs --no-run-if-empty kill jobs -p | xargs --no-run-if-empty kill
} }
trap cleanup EXIT trap cleanup EXIT
# Use local Arch iso or download the latest iso and extract the relevant files # Use local Arch iso or download the latest iso and extract the relevant files
function prepare_boot() { function prepare_boot() {
if LOCAL_ISO="$(ls "${ORIG_PWD}/"archlinux-*-x86_64.iso 2>/dev/null)"; then local iso
echo "Using local iso: ${LOCAL_ISO}" local isos=()
ISO="${LOCAL_ISO}"
# retrieve any local images and sort them
for iso in "${ORIG_PWD}/"archlinux-*-x86_64.iso; do
if [[ -f "$iso" ]]; then
isos+=("${iso}")
fi
done
if (( ${#isos[@]} >= 1 )); then
ISO="$(printf '%s\n' "${isos[@]}" | sort -r | head -n1)"
printf "Using local iso: %s\n" "$ISO"
fi fi
if [ -z "${LOCAL_ISO}" ]; then if (( ${#isos[@]} < 1 )); then
LATEST_ISO="$(curl -fs "${MIRROR}/iso/latest/" | grep -Eo 'archlinux-[0-9]{4}\.[0-9]{2}\.[0-9]{2}-x86_64.iso' | head -n 1)" LATEST_ISO="$(curl -fs "${MIRROR}/iso/latest/" | grep -Eo 'archlinux-[0-9]{4}\.[0-9]{2}\.[0-9]{2}-x86_64.iso' | head -n 1)"
if [ -z "${LATEST_ISO}" ]; then if [[ -z "${LATEST_ISO}" ]]; then
echo "Error: Couldn't find latest iso'" echo "Error: Couldn't find latest iso'"
exit 1 exit 1
fi fi
@ -55,11 +64,10 @@ function start_qemu() {
-machine accel=kvm:tcg \ -machine accel=kvm:tcg \
-smp "$(nproc)" \ -smp "$(nproc)" \
-m 4096 \ -m 4096 \
-net nic \ -device virtio-net-pci,romfile=,netdev=net0 -netdev user,id=net0 \
-net user \
-kernel vmlinuz-linux \ -kernel vmlinuz-linux \
-initrd initramfs-linux.img \ -initrd initramfs-linux.img \
-append "archisobasedir=arch archisolabel=${ISO_VOLUME_ID} ip=dhcp net.ifnames=0 console=ttyS0 mirror=${MIRROR}" \ -append "archisobasedir=arch archisolabel=${ISO_VOLUME_ID} cow_spacesize=4G ip=dhcp net.ifnames=0 console=ttyS0 mirror=${MIRROR}" \
-drive file=scratch-disk.img,format=raw,if=virtio \ -drive file=scratch-disk.img,format=raw,if=virtio \
-drive file="${ISO}",format=raw,if=virtio,media=cdrom,read-only \ -drive file="${ISO}",format=raw,if=virtio,media=cdrom,read-only \
-virtfs "local,path=${ORIG_PWD},mount_tag=host,security_model=none" \ -virtfs "local,path=${ORIG_PWD},mount_tag=host,security_model=none" \
@ -81,9 +89,9 @@ function expect() {
# read should never exit with a non-zero exit code, # read should never exit with a non-zero exit code,
# but it can happen if the fd is EOF or it times out # but it can happen if the fd is EOF or it times out
IFS= read -r -u 10 -n 1 -t "${timeout}" c IFS= read -r -u 10 -n 1 -t "${timeout}" c
if [ "${1:${i}:1}" = "${c}" ]; then if [[ "${1:${i}:1}" = "${c}" ]]; then
i="$((i + 1))" i="$((i + 1))"
if [ "${length}" -eq "${i}" ]; then if [[ "${length}" -eq "${i}" ]]; then
break break
fi fi
else else
@ -118,7 +126,7 @@ function main() {
expect "# " expect "# "
send "mkfs.ext4 /dev/vda && mkdir /mnt/scratch-disk/ && mount /dev/vda /mnt/scratch-disk && cd /mnt/scratch-disk\n" send "mkfs.ext4 /dev/vda && mkdir /mnt/scratch-disk/ && mount /dev/vda /mnt/scratch-disk && cd /mnt/scratch-disk\n"
expect "# " expect "# "
send "cp -a /mnt/project/{.gitlab,archiso,configs,scripts} .\n" send "cp -a -- /mnt/project/{.gitlab,archiso,configs,scripts} .\n"
expect "# " expect "# "
send "mkdir pkg && mount --bind pkg /var/cache/pacman/pkg\n" send "mkdir pkg && mount --bind pkg /var/cache/pacman/pkg\n"
expect "# " expect "# "
@ -132,18 +140,18 @@ function main() {
expect "# " expect "# "
# Install required packages # Install required packages
send "pacman -Sy --noconfirm qemu-headless jq dosfstools e2fsprogs libisoburn mtools squashfs-tools\n" send "pacman -Syu --ignore linux --noconfirm --needed qemu-headless jq dosfstools e2fsprogs libisoburn mtools squashfs-tools\n"
expect "# " expect "# " 120
## Start build and copy output to local disk ## Start build and copy output to local disk
send "bash -x ./.gitlab/ci/build-inside-vm.sh ${PROFILE}\n " send "bash -x ./.gitlab/ci/build-inside-vm.sh ${PROFILE}\n "
expect "# " 1000 # mksquashfs can take a long time expect "# " 1000 # mksquashfs can take a long time
send "cp -r --preserve=mode,timestamps output /mnt/project/tmp/$(basename "${TMPDIR}")/\n" send "cp -r --preserve=mode,timestamps -- output /mnt/project/tmp/$(basename "${TMPDIR}")/\n"
expect "# " 60 expect "# " 60
mv output/* "${OUTPUT}/" mv output/* "${OUTPUT}/"
# Shutdown the VM # Shutdown the VM
send "shutdown now\n" send "systemctl poweroff -i\n"
wait wait
} }
main main