Nicolas Massé 3 months ago
parent
commit
12b4a9b3f2
  1. 1
      bootc/base/Containerfile
  2. 4
      bootc/base/root/etc/systemd/system/flightctl-agent.service.d/override.conf
  3. 1
      bootc/scenario3a/root/etc/default/bootstrap-vm-nextcloud.env
  4. 45
      bootc/scenario3a/root/usr/local/bin/bootstrap-vm.sh

1
bootc/base/Containerfile

@ -30,4 +30,5 @@ if [ -n "$ADMIN_USERNAME" -a -f "/etc/ssh/authorized_keys/$ADMIN_USERNAME.keys"
fi fi
semanage fcontext -a -t ssh_home_t "/etc/ssh/authorized_keys(/.*)?" semanage fcontext -a -t ssh_home_t "/etc/ssh/authorized_keys(/.*)?"
restorecon -Rf /etc/ssh/authorized_keys restorecon -Rf /etc/ssh/authorized_keys
install -d -m 0700 -o root -g root /var/lib/private/flightctl
EOF EOF

4
bootc/base/root/etc/systemd/system/flightctl-agent.service.d/override.conf

@ -1,3 +1,3 @@
[Service] [Service]
# If the config file has been injected into the qcow2 image in /var, move it to the right place # If the config file has been injected into the qcow2 image in /var/lib/private, move it to the right place
ExecStartPre=/bin/bash -Eeuo pipefail -c 'if [ -f /var/lib/flightctl/config.yaml -a ! -f /etc/flightctl/config.yaml ]; then mv /var/lib/flightctl/config.yaml /etc/flightctl/config.yaml; restorecon -RF /etc/flightctl/config.yaml; fi' ExecStartPre=/bin/bash -Eeuo pipefail -c 'if [ -f /var/lib/private/flightctl/config.yaml -a ! -f /etc/flightctl/config.yaml ]; then mv /var/lib/private/flightctl/config.yaml /etc/flightctl/config.yaml; restorecon -RF /etc/flightctl/config.yaml; fi; if [ -f /var/lib/private/flightctl/auth.json -a ! -f /etc/ostree/auth.json ]; then mv /var/lib/private/flightctl/auth.json /etc/ostree/auth.json; restorecon -RF /etc/ostree/auth.json; fi'

1
bootc/scenario3a/root/etc/default/bootstrap-vm-nextcloud.env

@ -3,3 +3,4 @@ DOMAIN_RAM=8192
DOMAIN_DISK_SIZE=100 DOMAIN_DISK_SIZE=100
DOMAIN_OS_VARIANT=rhel9.6 DOMAIN_OS_VARIANT=rhel9.6
DOMAIN_MAC_ADDRESS=04:00:00:00:00:01 DOMAIN_MAC_ADDRESS=04:00:00:00:00:01
FLIGHTCTL_LABELS_OVERRIDE={ "type": "virtualmachine", "vm.name": "nextcloud", "scenario": "scenario1" }

45
bootc/scenario3a/root/usr/local/bin/bootstrap-vm.sh

@ -8,23 +8,55 @@ if [[ $# -ne 1 ]]; then
fi fi
VM="${1}" VM="${1}"
if [ -f "/var/lib/libvirt/images/${VM}/root.qcow2" ]; then
echo "VM ${VM} already exists. Please remove it first."
exit 1
fi
temp_dir=$(mktemp -d) temp_dir=$(mktemp -d)
cleanup() { cleanup() {
local exit_code=$?
rm -rf "$temp_dir" 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 -f "/var/lib/libvirt/images/${VM}/root.qcow2"
fi
} }
trap cleanup EXIT trap cleanup EXIT
mkdir -p "/var/lib/libvirt/images/${VM}" # Create a temporary directory to hold the VM image and copy the base image there
cp -a "/usr/local/libvirt/images/${VM}/qcow2/disk.qcow2" "/var/lib/libvirt/images/${VM}/root.qcow2" install -m 0710 -o root -g qemu --context=system_u:object_r:virt_image_t:s0 -d "$temp_dir"
install -m 0770 -o root -g qemu --context=system_u:object_r:virt_image_t:s0 "/usr/local/libvirt/images/${VM}/qcow2/disk.qcow2" "$temp_dir/root.qcow2"
# Inject the Flightctl configuration file (w/ enrollment certificates) into the VM image # 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 [ -f /etc/flightctl/config.yaml ]; then
yq e '.default-labels += { "type": "virtualmachine" }' /etc/flightctl/config.yaml > "$temp_dir/config.yaml" if [ -n "${FLIGHTCTL_LABELS_OVERRIDE:-}" ]; then
guestfish --add /var/lib/libvirt/images/${VM}/root.qcow2 -m /dev/sda4 <<EOF echo "Overriding default labels with: ${FLIGHTCTL_LABELS_OVERRIDE}"
copy-in $temp_dir/config.yaml /ostree/deploy/default/var/lib/flightctl/ 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
guestfish --add "$temp_dir/root.qcow2" -m /dev/sda4 <<EOF
copy-in $temp_dir/config.yaml /ostree/deploy/default/var/lib/private/flightctl/
EOF
fi
# Inject the OSTree auth.json file into the VM image if it exists on the host
# 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/ostree/auth.json ]; then
guestfish --add "$temp_dir/root.qcow2" -m /dev/sda4 <<'EOF'
copy-in /etc/ostree/auth.json /ostree/deploy/default/var/lib/private/flightctl/
EOF EOF
fi fi
# Copy the VM image to the libvirt images directory
install -m 0710 -o root -g qemu -Z -d "/var/lib/libvirt/images/${VM}"
install -m 0660 -o root -g qemu -Z "$temp_dir/root.qcow2" "/var/lib/libvirt/images/${VM}/root.qcow2"
# Create and start the VM using virt-install
virt-install --name "${VM}" \ virt-install --name "${VM}" \
--autostart \ --autostart \
--cpu=host-passthrough \ --cpu=host-passthrough \
@ -38,3 +70,6 @@ virt-install --name "${VM}" \
--import \ --import \
--network=network=default,mac=${DOMAIN_MAC_ADDRESS} \ --network=network=default,mac=${DOMAIN_MAC_ADDRESS} \
--noautoconsole --noautoconsole
echo "VM ${VM} has been created and started."
exit 0

Loading…
Cancel
Save