Makefile: Use QEMU directly for virtual hosts

This commit moves away from `virsh`, which requires setup and a
persistent daemon, and is rather complex and opaque, and will now have
virtual hosts use QEMU directly. Port mappings are now also made
automatically, albeit on higher ports -- port 8022 for SSH, port 8080
for HTTP, port 8443 for HTTPS. More port mappings will be added in the
future, with direct mappings made where possible.
This commit is contained in:
Alex Palaistras 2022-12-28 22:57:19 +00:00
parent dc91311f2b
commit 05c77bc234
2 changed files with 32 additions and 34 deletions

View File

@ -1,10 +1,11 @@
# CoreOS options.
NAME := coreos-home-server
STREAM := stable
VERSION := 36.20221030.3.0
VERSION := 37.20221127.3.0
ARCH := x86_64
IMAGE_URI := https://builds.coreos.fedoraproject.org/prod/streams/
HOST := $(if $(filter deploy-virtual,$(MAKECMDGOALS)),virtual,$(HOST))
HOST := $(if $(HOST),$(HOST),$(error Please specify a valid HOST to deploy))
TYPE := $(if $(filter virtual,$(HOST)),virtual,$(if $(TYPE),$(TYPE),$(error Please specify a valid deployment TYPE)))
# Default Makefile options.
VERBOSE :=
@ -16,31 +17,15 @@ ADDRESS = $(shell ip -o route get 1 | awk '{for (i=1; i<=NF; i++) {if ($$
CONTAINERFILES = $(wildcard service/*/Containerfile)
# Build-time dependencies.
BUTANE ?= $(call find-cmd,butane)
PODMAN ?= $(call find-cmd,podman)
CURL ?= $(call find-cmd,curl) $(if $(VERBOSE),,--progress-bar) --fail
GPG ?= $(call find-cmd,gpg) $(if $(VERBOSE),,-q)
VIRSH ?= $(call find-cmd,virsh) --connect=qemu:///system $(if $(VERBOSE),,-q)
VIRTINSTALL ?= $(call find-cmd,virt-install) --connect=qemu:///system
NC ?= $(call find-cmd,nc) -vv -r -l
BUTANE ?= $(call find-cmd,butane)
PODMAN ?= $(call find-cmd,podman)
CURL ?= $(call find-cmd,curl) $(if $(VERBOSE),,--progress-bar) --fail
GPG ?= $(call find-cmd,gpg) $(if $(VERBOSE),,-q)
QEMU ?= $(call find-cmd,qemu-system-x86_64) -enable-kvm
NC ?= $(call find-cmd,nc) -vv -r -l
## Builds and deploys Fedora CoreOS for HOST on ADDRESS.
deploy: $(TMPDIR)deploy/host/$(HOST)/spec.ign
@printf "Serving Ignition config '$<' over HTTP...\n"
@printf 'HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s\n' "$$(wc -c < $<)" "$$(cat $<)" | $(NC) -s $(ADDRESS) || exit 0
## Prepares and deploys CoreOS release for local, virtual environment.
deploy-virtual: $(TMPDIR)images/fedora-coreos-$(VERSION)-qemu.$(ARCH).qcow2.xz $(TMPDIR)deploy/host/$(HOST)/spec.ign
@printf "Preparing virtual environment...\n"
$Q $(VIRTINSTALL) --import --name=$(NAME) --os-variant=fedora36 \
--graphics=none --vcpus=2 --memory=2048 --cpu=host --virt-type=kvm \
--disk="size=20,backing_store=$(TMPDIR)images/fedora-coreos-$(VERSION)-qemu.$(ARCH).qcow2" \
--qemu-commandline="-fw_cfg name=opt/com.coreos/config,file=$(TMPDIR)deploy/host/$(HOST)/spec.ign"
## Stop and remove virtual environment for CoreOS.
destroy-virtual:
$Q $(VIRSH) destroy $(NAME) || true
$Q $(VIRSH) undefine --remove-all-storage $(NAME) || true
## Builds and deploys Fedora CoreOS for HOST of TYPE.
deploy: deploy-$(TYPE)
## Remove deployment configuration files required for build.
clean:
@ -58,9 +43,22 @@ help:
@printf "This Makefile contains tasks for processing auxiliary actions, such as\n"
@printf "building binaries, packages, or running tests against the test suite.\n\n"
@printf "$(UNDERLINE)Available Tasks$(RESET)\n\n"
@awk -F ':|##' '/^##/ {c=$$2; getline; printf "$(BLUE)%16s$(RESET)%s\n", $$1, c}' $(MAKEFILE_LIST)
@awk -F ':|##' '/^##/ {c=$$2; getline; printf "$(BLUE)%6s$(RESET)%s\n", $$1, c}' $(MAKEFILE_LIST)
@printf "\n"
# Builds and deploys Fedora CoreOS for HOST on ADDRESS.
deploy-metal: $(TMPDIR)deploy/host/$(HOST)/spec.ign
@printf "Serving Ignition config '$<' over HTTP...\n"
@printf 'HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s\n' "$$(wc -c < $<)" "$$(cat $<)" | $(NC) -s $(ADDRESS) || exit 0
# Prepares and deploys CoreOS release for local, virtual environment.
deploy-virtual: $(TMPDIR)images/fedora-coreos-$(VERSION)-qemu.$(ARCH).qcow2.xz $(TMPDIR)deploy/host/$(HOST)/spec.ign
@printf "Preparing virtual environment (press C-a h for help)...\n"
$Q $(QEMU) -m 2048 -cpu host -nographic -snapshot \
-fw_cfg name=opt/com.coreos/config,file=$(TMPDIR)deploy/host/$(HOST)/spec.ign \
-drive if=virtio,file=$(TMPDIR)images/fedora-coreos-$(VERSION)-qemu.$(ARCH).qcow2 \
-nic user,model=virtio,hostfwd=tcp::8022-:22,hostfwd=tcp::8080-:80,hostfwd=tcp::8443-:443
# Build container file locally using 'podman build'.
$(CONTAINERFILES):
@printf "Building container for '$(notdir $(@D))'...\n"
@ -108,7 +106,7 @@ $(TMPDIR)make.depend: $(shell find $(ROOTDIR) -name '*.bu' -type f 2>/dev/null)
@printf "$(foreach i,$^,\n$(patsubst $(ROOTDIR)%.bu,$(TMPDIR)deploy/%.ign, \
$(i)): $(addprefix $(TMPDIR)deploy/, $(shell awk -F '[ ]+local:[ ]*' '/^[ ]+(-[ ]+)?local:/ {print $$2}' $(i))))" >> $@
.PHONY: deploy deploy-virtual destroy-virtual clean purge help $(CONTAINERFILES)
.PHONY: deploy deploy-metal deploy-virtual clean purge help $(CONTAINERFILES)
# Conditional command echo control.
Q := $(if $(VERBOSE),,@)

View File

@ -10,7 +10,7 @@ on your host:
- `gpg` for secret management and image validation.
- `butane` for rendering out host and service configuration.
- `virsh` and `virt-install` with `qemu` for virtual host testing.
- `qemu` for virtual host testing.
All of the requirements are checked during the various Makefile invocations, and will return fatal
errors unless fulfilled. In addition to the aforementioned build-time dependencies, the build host
@ -48,11 +48,11 @@ network -- support for additional targets may be added in the future.
## Testing
A virtual host is included for development and testing; using this requires that you have `virsh`
and `virt-install` installed on your system. Using the virtual environment is simple:
A virtual host is included for development and testing; using this requires that you have `qemu`
installed on your system. Using the virtual environment is simple:
```
make deploy-virtual
make deploy HOST=virtual
```
This will automatically download the Fedora CoreOS image for the `VERSION` specified in the
@ -60,8 +60,8 @@ Makefile, compile included Butane files, and start a virtual machine on the term
`make` command. If you want to see the various command run under the hood, add the `VERBOSE=1`
parameter to the `make` invocation.
By default, you can use the `<Ctrl>]` key-combination to escape the virtual machine, and can use the
`make destroy-virtual` command to drop any resources initialized for the virtual host.
By default, you can use the `<Ctrl>a x` key-combination to escape the virtual machine, and can use the
`<Ctrl>a h` key-combination to get additional options related to the current virtual session.
## Included Services