Skip to content

Commit

Permalink
virt/incus: Add support for building and running incus containers
Browse files Browse the repository at this point in the history
  • Loading branch information
theCalcaholic authored and paralin committed Sep 13, 2024
1 parent d9bd2b2 commit 33f71fd
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 0 deletions.
33 changes: 33 additions & 0 deletions configs/virt/incus/README.md
Original file line number Diff line number Diff line change
@@ -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
```
3 changes: 3 additions & 0 deletions configs/virt/incus/buildroot/firmware
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions configs/virt/incus/buildroot/kernel
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# We don't need a kernel for Docker.
# BR2_LINUX_KERNEL is not set
2 changes: 2 additions & 0 deletions configs/virt/incus/buildroot/rootfs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BR2_TARGET_ROOTFS_TAR=y
# BR2_TARGET_ROOTFS_ISO9660 is not set
3 changes: 3 additions & 0 deletions configs/virt/incus/buildroot/shell
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BR2_PACKAGE_BASH=y
BR2_PACKAGE_WHICH=y
BR2_SYSTEM_BIN_SH_BASH=y
6 changes: 6 additions & 0 deletions configs/virt/incus/extensions/Makefile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions configs/virt/incus/metadata/commands
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run Start the incus container
1 change: 1 addition & 0 deletions configs/virt/incus/metadata/dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
util/rootlogin
1 change: 1 addition & 0 deletions configs/virt/incus/metadata/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Run Skiff in an inucs container
37 changes: 37 additions & 0 deletions configs/virt/incus/scripts/buildimage.sh
Original file line number Diff line number Diff line change
@@ -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}"
14 changes: 14 additions & 0 deletions configs/virt/incus/scripts/exec.sh
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions configs/virt/incus/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -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}."

0 comments on commit 33f71fd

Please sign in to comment.