From fd7778c01a6c7e61d66f53f698f985e184ea57c6 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 6 Jan 2024 20:49:31 -0600 Subject: [PATCH] mkvm: Add script to create FCOS VM Fedora CoreOS can be provisioned on a QEMU virtual machine by providing the Ignition configuration via `fw_cfg` value. Unfortunately, the `string` method does not work with JSON values, so we have to use `file`. The configuration file has to be uploaded via SFTP, rather than `virsh vol-import`, since the latter would create the file with the wrong permissions, and QEMU does not automatically adjust the permissions of files used this way (like it does for disks). --- Makefile | 7 ++++++- mkvm.sh | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 mkvm.sh diff --git a/Makefile b/Makefile index 7422eb1..ee548ab 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ .PHONY: \ all \ clean \ - publish + publish \ + vm .DEFAULT_GOAL := all @@ -26,3 +27,7 @@ $(foreach t,$(wildcard *.yaml),$(eval $(call genrules,$(t)))) publish: \ nvr1.ign rsync -rti $^ files.pyrocufflink.blue:public_html/ + +vm: \ + $(VMNAME).ign + sh mkvm.sh $(VMNAME) diff --git a/mkvm.sh b/mkvm.sh new file mode 100644 index 0000000..124c456 --- /dev/null +++ b/mkvm.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# vim: set sw=4 ts=4 sts=4 et : + +: "${POOL:=default}" +: "${VCPUS:=2}" +: "${MEMORY:=2048}" +: "${DISK_SIZE:=10}" +: "${NETWORK=network=prod}" + +VMNAME="$1" + +pooldir=$(virsh pool-dumpxml "${POOL}" | xmllint --xpath '//path/text()' -) + +vmhost=$(virsh uri | cut -d/ -f3) +ign="${pooldir}/${VMNAME}.ign" +if [ -n "${vmhost}" ]; then + scp -s "${VMNAME}".ign "${vmhost}:${ign}" +else + cp "${VMNAME}".ign "${ign}" +fi + +image=$(virsh vol-list "${POOL}" \ + | awk '/fedora-coreos-.*-qemu/{print $2}' \ + | sort -V \ + | tail -n1) + +virt-install \ + --name "${VMNAME}" \ + --vcpus "${VCPUS}" \ + --memory "${MEMORY}" \ + --os-variant fedora-coreos-stable \ + --graphics none \ + --sound none \ + --disk size="${DISK_SIZE},backing_store=${image}" \ + ${DISK:+--disk "${DISK}"} \ + --network "${NETWORK}" \ + --qemu-commandline="--fw_cfg name=opt/com.coreos/config,file=${ign}" \ + --import +