#!/bin/bash set -Eeuo pipefail if [[ $# -ne 1 ]]; then echo "Usage: $0 " exit 1 fi VM="${1}" if [ -d "/var/lib/libvirt/images/${VM}/" ]; then echo "VM ${VM} already exists. Please remove it first." exit 1 fi temp_dir=$(mktemp -d) cleanup() { local exit_code=$? rm -rf "$temp_dir" if [ $exit_code -ne 0 ]; then echo "An error occurred. Cleaning up..." virsh destroy "${VM}" || true virsh undefine "${VM}" --nvram || true rm -rf "/var/lib/libvirt/images/${VM}/" fi } trap cleanup EXIT # Create a temporary directory to hold the VM image and copy the base image there install -m 0710 -o root -g qemu --context=system_u:object_r:virt_image_t:s0 -d "$temp_dir" # Pull the base image defined in the environment file podman artifact pull "${DOMAIN_DISK_SOURCE}" podman artifact extract "${DOMAIN_DISK_SOURCE}" "$temp_dir/root.qcow2" chown root:qemu "$temp_dir/root.qcow2" chmod 0660 "$temp_dir/root.qcow2" chcon system_u:object_r:virt_image_t:s0 "$temp_dir/root.qcow2" # Inject the Flightctl configuration file (w/ enrollment certificates) into the VM image # Note: The injected config file will be moved to the right place in the VM by a systemd override in the base image if [ -f /etc/flightctl/config.yaml ]; then if [ -n "${FLIGHTCTL_LABELS_OVERRIDE:-}" ]; then echo "Overriding default labels with: ${FLIGHTCTL_LABELS_OVERRIDE}" yq e ". * { \"default-labels\": ${FLIGHTCTL_LABELS_OVERRIDE} }" /etc/flightctl/config.yaml > "$temp_dir/config.yaml" else cp /etc/flightctl/config.yaml "$temp_dir/config.yaml" fi echo "Injecting Flightctl configuration into the VM image..." guestfish --add "$temp_dir/root.qcow2" -m /dev/sda4 <