clan-app/devshell: add script to launch qemu VMs for testing

This commit is contained in:
DavHau
2025-05-19 18:36:50 +07:00
parent 4b1060e009
commit 2b4e624ee8
3 changed files with 70 additions and 4 deletions

View File

@@ -0,0 +1,47 @@
#!/usr/bin/env bash
set -euo pipefail
getIso() {
# get the ssh public key of the current user
ssh_key=$(ssh-add -L)
# TODO: make this cross platform compatible by downloading a pre-built image
# and injecting the ssh private key into it
nix build --impure --expr "
let
pkgs = import <nixpkgs> { crossSystem = \"x86_64-linux\"; };
in
(pkgs.nixos {
services.openssh.enable = true;
environment.systemPackages = [ pkgs.nixos-facter ];
users.users.root.openssh.authorizedKeys.keys = [ \"$ssh_key\" ];
users.users.root.password = \"root\";
}).config.system.build.images.iso
" -o iso
iso=$(realpath iso)
# iso=/nix/store/xgkfnwhi3c2lcpsvlpcw3dygwgifinbq-nixos-minimal-23.05pre483386.f212785e1ed-x86_64-linux.iso
# nix-store -r "$iso"
}
id="${1:-1}"
CPUS="${CPUS:-2}"
MEMORY="${MEMORY:-4096}"
IMAGE_SIZE="${IMAGE_SIZE:-10G}"
SSH_PORT="${SSH_PORT:-2200${id}}"
img_file="nixos-nvme${id}.img"
getIso
truncate -s"$IMAGE_SIZE" "$img_file"
set -x
qemu-system-x86_64 \
-m "${MEMORY}" \
-boot n \
-smp "${CPUS}" \
-enable-kvm \
-cpu max \
-netdev "user,id=mynet0,hostfwd=tcp::${SSH_PORT}-:22" \
-device virtio-net-pci,netdev=mynet0 \
-drive "file=$img_file,if=none,id=nvme1,format=raw" \
-device nvme,serial=deadbeef1,drive=nvme1 \
-cdrom "$iso"/iso/*.iso

17
pkgs/clan-app/bin/start-vm Executable file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail
script_dir=$(dirname "$(realpath "$0")")
trap 'rm -rf "$tmpdir"' EXIT
tmpdir=$(mktemp -d)
cd "$tmpdir"
number_vms="$1"
for i in $(seq 1 "$number_vms"); do
"$script_dir/start-qemu-vm.sh" "$i" &
done
while true; do sleep 1; done

View File

@@ -9,6 +9,10 @@
json2ts, json2ts,
self', self',
}: }:
let
GREEN = "\\033[1;32m";
NC = "\\033[0m";
in
mkShell { mkShell {
name = "ui"; name = "ui";
@@ -72,10 +76,8 @@ mkShell {
if test -f "$GIT_ROOT/pkgs/clan-app/.local.env"; then if test -f "$GIT_ROOT/pkgs/clan-app/.local.env"; then
source "$GIT_ROOT/pkgs/clan-app/.local.env" source "$GIT_ROOT/pkgs/clan-app/.local.env"
fi fi
# Define the yellow color code
YELLOW='\033[1;33m'
# Define the reset color code
NC='\033[0m'
export PC_CONFIG_FILES="$CLAN_CORE_PATH/pkgs/clan-app/process-compose.yaml" export PC_CONFIG_FILES="$CLAN_CORE_PATH/pkgs/clan-app/process-compose.yaml"
echo -e "${GREEN}To launch a qemu VM for testing, run:\n start-vm <number of VMs>${NC}"
''; '';
} }