Files
homelab-config/virtual-machines/vm-test/build-image.sh
T

61 lines
1.2 KiB
Bash
Raw Normal View History

2026-09-27 02:06:13 -05:00
#!/usr/bin/env bash
set -euo pipefail
2026-09-27 02:11:09 -05:00
# Usage:
# ./build-image.sh <alias> [vmname]
#
# Examples:
# ./build-image.sh nixos/custom/vm
# ./build-image.sh nixos/custom/my-vm my-vm
if [[ $# -lt 1 || $# -gt 2 ]]; then
echo "Usage: $0 <alias> [vmname]"
echo
echo " alias Required Incus image alias"
echo " vmname NixOS configuration name (default: vm)"
exit 1
fi
ALIAS="$1"
VMNAME="${2:-vm}"
2026-09-27 02:06:13 -05:00
# Build the NixOS LXC metadata tarball
TARBALL_DIR="$(nix build \
2026-09-27 02:11:09 -05:00
".#nixosConfigurations.${VMNAME}.config.system.build.metadata" \
--print-out-paths)"
2026-09-27 02:06:13 -05:00
2026-09-27 02:11:09 -05:00
# Find the tarball
2026-09-27 02:06:13 -05:00
TARBALL="$(find "$TARBALL_DIR" -type f -name '*.tar.xz' -print -quit)"
if [[ -z "$TARBALL" ]]; then
2026-09-27 02:11:09 -05:00
echo "Could not find a .tar.xz tarball in: $TARBALL_DIR"
2026-09-27 02:06:13 -05:00
exit 1
fi
2026-09-27 02:11:09 -05:00
echo "Metadata tarball: $TARBALL"
2026-09-27 02:06:13 -05:00
# Build the QEMU disk image
2026-09-27 02:11:09 -05:00
QCOW2_DIR="$(nix build \
".#nixosConfigurations.${VMNAME}.config.system.build.qemuImage" \
--print-out-paths)"
QCOW2="$QCOW2_DIR/nixos.qcow2"
2026-09-27 02:06:13 -05:00
if [[ ! -f "$QCOW2" ]]; then
2026-09-27 02:11:09 -05:00
echo "QEMU image not found: $QCOW2"
2026-09-27 02:06:13 -05:00
exit 1
fi
2026-09-27 02:11:09 -05:00
echo "Found QEMU image: $QCOW2"
echo
echo "Importing image into Incus..."
2026-09-27 02:06:13 -05:00
incus image import \
--alias "$ALIAS" \
"$TARBALL" \
"$QCOW2"
2026-09-27 02:11:09 -05:00
echo
echo "Successfully imported image: $ALIAS"
2026-09-27 02:06:13 -05:00