From 33f71fdfbb9f3ca791ebfd929c8c379faa8016b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= <6317548+theCalcaholic@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:48:36 +0200 Subject: [PATCH] virt/incus: Add support for building and running incus containers --- configs/virt/incus/README.md | 33 +++++++++++++++++ configs/virt/incus/buildroot/firmware | 3 ++ configs/virt/incus/buildroot/kernel | 2 ++ configs/virt/incus/buildroot/rootfs | 2 ++ configs/virt/incus/buildroot/shell | 3 ++ configs/virt/incus/extensions/Makefile | 6 ++++ configs/virt/incus/metadata/commands | 1 + configs/virt/incus/metadata/dependencies | 1 + configs/virt/incus/metadata/description | 1 + configs/virt/incus/scripts/buildimage.sh | 37 +++++++++++++++++++ configs/virt/incus/scripts/exec.sh | 14 ++++++++ configs/virt/incus/scripts/run.sh | 45 ++++++++++++++++++++++++ 12 files changed, 148 insertions(+) create mode 100644 configs/virt/incus/README.md create mode 100644 configs/virt/incus/buildroot/firmware create mode 100644 configs/virt/incus/buildroot/kernel create mode 100644 configs/virt/incus/buildroot/rootfs create mode 100644 configs/virt/incus/buildroot/shell create mode 100644 configs/virt/incus/extensions/Makefile create mode 100644 configs/virt/incus/metadata/commands create mode 100644 configs/virt/incus/metadata/dependencies create mode 100644 configs/virt/incus/metadata/description create mode 100755 configs/virt/incus/scripts/buildimage.sh create mode 100755 configs/virt/incus/scripts/exec.sh create mode 100755 configs/virt/incus/scripts/run.sh diff --git a/configs/virt/incus/README.md b/configs/virt/incus/README.md new file mode 100644 index 00000000..3fc34063 --- /dev/null +++ b/configs/virt/incus/README.md @@ -0,0 +1,33 @@ +# Containerized: Incus + +This package builds a SkiffOS system for use as a Incus container image. + +[Incus](https://linuxcontainers.org/incus/) containers are versatile system containers that, as opposed to application containers like docker, emulate a full init system by default ([more details here](https://linuxcontainers.org/incus/docs/main/explanation/containers_and_vms/)) and are therefore well suited for multi service/container deployments. Incus utilizes the [LXC](https://linuxcontainers.org/) APIs internally and provides a powerful toolset on top of them. + +Building and starting the container requires incus to be installed and running on the host and the executing user to be in the `incus-admin` group. + +## Example + +```bash +SKIFF_CONFIG=virt/incus make configure compile # Compile the system +make cmd/virt/incus/buildimage +make cmd/virt/incus/run +``` + +Executing a shell in the container: + +```sh +$ make cmd/virt/incus/exec +# alternatively +$ incus exec skiff -- sh +``` + +## Persistence + +When using the included command `cmd/virt/incus/run` for creating the container, a persistence volume will be created in the first incus storage pool available and attached to the container. If you want to reset the persistence volume, just delete it, so it will be recreated: + +```sh +# You can find the right storage pool with: +# incus storage list -c n -f csv | head -n 1 +incus volume delete "${STORAGE_POOL}" skiff-persist +``` diff --git a/configs/virt/incus/buildroot/firmware b/configs/virt/incus/buildroot/firmware new file mode 100644 index 00000000..6b67a5f8 --- /dev/null +++ b/configs/virt/incus/buildroot/firmware @@ -0,0 +1,3 @@ +# BR2_PACKAGE_LINUX_FIRMWARE is not set +# BR2_PACKAGE_RTL8812AU_AIRCRACK_NG is not set +# BR2_PACKAGE_RTL8821CU is not set \ No newline at end of file diff --git a/configs/virt/incus/buildroot/kernel b/configs/virt/incus/buildroot/kernel new file mode 100644 index 00000000..8bdee3b3 --- /dev/null +++ b/configs/virt/incus/buildroot/kernel @@ -0,0 +1,2 @@ +# We don't need a kernel for Docker. +# BR2_LINUX_KERNEL is not set diff --git a/configs/virt/incus/buildroot/rootfs b/configs/virt/incus/buildroot/rootfs new file mode 100644 index 00000000..f5c5e1cd --- /dev/null +++ b/configs/virt/incus/buildroot/rootfs @@ -0,0 +1,2 @@ +BR2_TARGET_ROOTFS_TAR=y +# BR2_TARGET_ROOTFS_ISO9660 is not set \ No newline at end of file diff --git a/configs/virt/incus/buildroot/shell b/configs/virt/incus/buildroot/shell new file mode 100644 index 00000000..7a0c3b6e --- /dev/null +++ b/configs/virt/incus/buildroot/shell @@ -0,0 +1,3 @@ +BR2_PACKAGE_BASH=y +BR2_PACKAGE_WHICH=y +BR2_SYSTEM_BIN_SH_BASH=y \ No newline at end of file diff --git a/configs/virt/incus/extensions/Makefile b/configs/virt/incus/extensions/Makefile new file mode 100644 index 00000000..56f0fb8d --- /dev/null +++ b/configs/virt/incus/extensions/Makefile @@ -0,0 +1,6 @@ +buildimage: + @$(SKIFF_CURRENT_CONF_DIR)/scripts/buildimage.sh +run: + @$(SKIFF_CURRENT_CONF_DIR)/scripts/run.sh +exec: + @$(SKIFF_CURRENT_CONF_DIR)/scripts/exec.sh diff --git a/configs/virt/incus/metadata/commands b/configs/virt/incus/metadata/commands new file mode 100644 index 00000000..9a041448 --- /dev/null +++ b/configs/virt/incus/metadata/commands @@ -0,0 +1 @@ +run Start the incus container \ No newline at end of file diff --git a/configs/virt/incus/metadata/dependencies b/configs/virt/incus/metadata/dependencies new file mode 100644 index 00000000..8398f5f3 --- /dev/null +++ b/configs/virt/incus/metadata/dependencies @@ -0,0 +1 @@ +util/rootlogin \ No newline at end of file diff --git a/configs/virt/incus/metadata/description b/configs/virt/incus/metadata/description new file mode 100644 index 00000000..eb4ae361 --- /dev/null +++ b/configs/virt/incus/metadata/description @@ -0,0 +1 @@ +Run Skiff in an inucs container \ No newline at end of file diff --git a/configs/virt/incus/scripts/buildimage.sh b/configs/virt/incus/scripts/buildimage.sh new file mode 100755 index 00000000..c87ab3ff --- /dev/null +++ b/configs/virt/incus/scripts/buildimage.sh @@ -0,0 +1,37 @@ +#!/bin/bash +set -ex + +IMAGE_NAME=skiffos/testing +CONTAINER=skiff +OUTPUT_PATH="$BUILDROOT_DIR/output" +IMAGES_PATH="$BUILDROOT_DIR/images" +WORKING_PATH="${BUILDROOT_DIR}/nspawn-run" +PERSIST_PATH="${WORKING_PATH}/nspawn-persist" +INCUS_IMAGE_PATH="${IMAGE_PATH}/incus.tar.gz" +roottar="${IMAGES_PATH}/rootfs.tar" + +if ! command -v incus > /dev/null 2>&1 +then + echo "Failed to find the incus command. Is incus installed on your host system?" + exit 1 +fi + +mkdir -p "${PERSIST_PATH}" +TMPDIR="${WORKING_PATH}/incus-tmp" +trap 'rm -rf "$TMPDIR";' EXIT +rm -rf "$TMPDIR" +mkdir -p "${TMPDIR}/rootfs" +tar -xf "$roottar" -C "$TMPDIR/rootfs" +pushd "$TMPDIR" +creationtime="$(date +%s)" +default_metadata="architecture: x86_64 +creation_date: $creationtime +properties: + description: SkiffOS + os: Linux x86 + release: unknown" +echo "${INCUS_METADATA:-"${default_metadata}"}" > "${TMPDIR}/metadata.yaml" +tar caf "${INCUS_IMAGE_PATH}" rootfs metadata.yaml +popd + +echo "Incus image generated successfully as ${IMAGE_NAME}" diff --git a/configs/virt/incus/scripts/exec.sh b/configs/virt/incus/scripts/exec.sh new file mode 100755 index 00000000..aaa417c0 --- /dev/null +++ b/configs/virt/incus/scripts/exec.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -e + +command -v incus > /dev/null 2>&1 || { + echo "Failed to find the incus command. Is incus installed on your host system?" + exit 1 +} + +incus show skiff > /dev/null || { + echo "Failed to find the skiff container. Did you execute \`make cmd/virt/incus/run\`?" + exit 1 +} + +incus exec skiff -- sh diff --git a/configs/virt/incus/scripts/run.sh b/configs/virt/incus/scripts/run.sh new file mode 100755 index 00000000..34d776b7 --- /dev/null +++ b/configs/virt/incus/scripts/run.sh @@ -0,0 +1,45 @@ +#!/bin/bash +set -e + +IMAGE_NAME=skiffos/testing +CONTAINER=skiff +IMAGES_PATH="$BUILDROOT_DIR/images" +INCUS_IMAGE_PATH="${IMAGE_PATH}/incus.tar.gz" + +if ! command -v incus > /dev/null 2>&1 +then + echo "Failed to find the incus command. Is incus installed on your host system?" + exit 1 +fi + +if ! [[ -f "${INCUS_IMAGE_PATH}" ]] > /dev/null +then + echo "${INCUS_IMAGE_PATH} not found. Did you execute \`make cmd/virt/incus/buildimage\`?" + exit 1 +fi + +if incus image show "${IMAGE_NAME}" >/dev/null 2>&1 +then + incus image delete "${IMAGE_NAME}" +fi +incus image import --alias "${IMAGE_NAME}" "${INCUS_IMAGE_PATH}" + +storage_pool="$(incus storage list -c n -f csv | head -n 1)" + +if ! incus storage volume show "$storage_pool" skiff-persist >/dev/null 2>&1 +then + incus storage volume create "$storage_pool" "skiff-persist" +fi + +if incus show "${CONTAINER}" +then + incus stop "${CONTAINER}" ||: + incus rm -f "${CONTAINER}" ||: +fi + +incus create "${IMAGE_NAME}" "${CONTAINER}" +incus storage volume attach "$storage_pool" skiff-persist skiff /mnt/persist +#incus config device add "${CONTAINER}" images-disk source="${IMAGES_PATH}" path=/mnt/rootfs +incus start "${CONTAINER}" + +echo "Incus container started as ${CONTAINER}."