summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornodist <kevin.comas.git@gmail.com>2026-09-02 10:28:25 -0400
committernodist <kevin.comas.git@gmail.com>2026-09-02 10:28:25 -0400
commitaed4392fdb84361bb4f79d231d4576bc0180fa13 (patch)
tree6a26a8f56c1739f0784b05fcd67ab4b05ad55ddf
parent44049e6dfe9ca9570d0863963a14009ff4eb0e70 (diff)
add inital qemu setup script
-rw-r--r--.gitignore1
-rw-r--r--TESTING39
-rwxr-xr-xvm.sh198
3 files changed, 238 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1750fe8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+qemu
diff --git a/TESTING b/TESTING
new file mode 100644
index 0000000..8ff62cd
--- /dev/null
+++ b/TESTING
@@ -0,0 +1,39 @@
+Testing KPL With QEMU
+
+Requirements:
+
+qemu-system-x86-64
+qemu-system-aarch64
+ed2k
+wget
+ssh
+
+Setup:
+
+Run
+./vm.sh -init
+Run
+./vm.sh -cli X64 # Set up root password for X86-64 machine
+./vm.sh -setup # Follow instructions
+./vm.sh -cli A64 # Set up root password for Aarch64 machine
+./vm.sh -setup # Follow instructions
+
+Starting:
+
+Run
+./vm.sh -start # Start both X64 and A64 machines
+
+./vm.sh -ssh X64 # SSH into X86-64
+./vm.sh -ssh A64 # SSH into Aarch64
+
+Testing:
+
+Stopping:
+
+To stop both machines run
+./vm.sh -stop
+
+Cleaning:
+
+To remove the testing environment run
+./vm.sh -clean
diff --git a/vm.sh b/vm.sh
new file mode 100755
index 0000000..29ad9fa
--- /dev/null
+++ b/vm.sh
@@ -0,0 +1,198 @@
+#!/bin/bash
+
+SCRIPT="$0"
+OPTION="$1"
+VM="$2"
+DIR="qemu"
+USER="user"
+CORES="4"
+MEMORY="4G"
+DEVICE="virtio-net-pci,netdev=net0"
+HDA_URL="https://cloud.debian.org/images/cloud/trixie/latest"
+X64="X64"
+A64="A64"
+NAMES=("$X64" "$A64")
+declare -A QEMU=(
+ [$X64]="qemu-system-x86_64"
+ [$A64]="qemu-system-aarch64"
+)
+declare -A HDA=(
+ [$X64]="debian-13-nocloud-amd64.qcow2"
+ [$A64]="debian-13-nocloud-arm64.qcow2"
+)
+declare -A BIOS=(
+ [$X64]="/usr/share/edk2/ovmf/OVMF_CODE.fd"
+ [$A64]="/usr/share/edk2/aarch64/QEMU_EFI.fd"
+)
+declare -A SSH_PORTS=(
+ [$X64]="2222"
+ [$A64]="2223"
+)
+declare -A PID_FILES=(
+ [$X64]="$X64.pid"
+ [$A64]="$A64.pid"
+)
+declare -A MACHINES=(
+ [$X64]="pc"
+ [$A64]="virt"
+)
+
+vm_netdev() {
+ echo -n "user,id=net0,hostfwd=tcp::${SSH_PORTS[$1]}-:22"
+}
+
+vm_init() {
+ if [[ ! -d "$DIR" ]]
+ then
+ mkdir $DIR
+ fi
+ cd $DIR
+ if [[ ! -d "$USER" ]]
+ then
+ mkdir $USER
+ ssh-keygen -t ed25519 -f "$USER/$USER" -N ""
+ fi
+ for H in ${HDA[@]}
+ do
+ if [[ ! -f "$H" ]]
+ then
+ wget "$HDA_URL/$H"
+ fi
+ done
+}
+
+vm_setup() {
+ local PUB=$(cat "$DIR/$USER/$USER.pub")
+ local HEADER="COPY AND RUN IN QEMU:"
+ echo $HEADER
+ echo " sed -i 's/^%sudo\s\+ALL=(ALL:ALL)\sALL$/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers; adduser --disabled-password --gecos "" --shell /bin/bash $USER; adduser $USER sudo; su - $USER"
+ echo $HEADER
+ echo " mkdir .ssh; cd .ssh; echo \"$PUB\" > authorized_keys; chmod 600 authorized_keys; cd ~"
+ echo $HEADER
+ echo " sudo apt update; sudo apt upgrade; sudo apt install ssh make gcc gdb; sudo poweroff"
+}
+
+vm_check() {
+ if [[ "$VM" != "$X64" && "$VM" != "$A64" ]]
+ then
+ echo "Usage $SCRIPT $OPTION $X64|$A64"
+ return 1
+ fi
+ return 0
+}
+
+vm_boot() {
+ local PID_FILE="${PID_FILES[$1]}"
+ if [[ -f "$PID_FILE" ]]
+ then
+ echo "VM $1 is running"
+ return
+ fi
+ local CMD=(
+ "${QEMU[$1]}"
+ "-M" "${MACHINES[$1]}"
+ "-cpu" "max"
+ "-m" "$MEMORY"
+ "-smp" "$CORES"
+ "-device" "$DEVICE"
+ "-netdev" "$(vm_netdev $1)"
+ "-bios" "${BIOS[$1]}"
+ "-hda" "${HDA[$1]}"
+ "-pidfile" "$PID_FILE"
+ )
+ for ARG in "${@:2}"
+ do
+ CMD+=("$ARG")
+ done
+ "${CMD[@]}"
+}
+
+vm_cli() {
+ vm_check
+ if [[ $? != 0 ]]
+ then
+ return
+ fi
+ cd $DIR
+ vm_boot $VM "-nographic"
+}
+
+vm_start() {
+ cd $DIR
+ for NAME in "${NAMES[@]}"
+ do
+ vm_boot $NAME "-display" "none" "-daemonize"
+ done
+}
+
+vm_ssh() {
+ vm_check
+ if [[ $? != 0 ]]
+ then
+ return
+ fi
+ cd $DIR
+ ssh-add "$USER/$USER"
+ ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$USER@localhost" -p "${SSH_PORTS[$VM]}"
+}
+
+vm_stop() {
+ cd $DIR
+ for NAME in "${NAMES[@]}"
+ do
+ local PID_FILE="${PID_FILES[$NAME]}"
+ if [[ -f "$PID_FILE" ]]
+ then
+ kill $(cat $PID_FILE)
+ fi
+ done
+}
+
+vm_clean() {
+ if [[ ! -d "$DIR" ]]
+ then
+ return
+ fi
+ local CHOICE
+ read -n 1 -r -s -p "Are you sure? y/n " CHOICE
+ if [[ "$CHOICE" != "y" ]]
+ then
+ echo
+ return
+ fi
+ rm -r $DIR
+ echo -e "\nRemoved $DIR testing directory"
+}
+
+vm_help() {
+ echo "Usage $SCRIPT -init|-cli|-start|-ssh|-test|-stop|-clean|-help"
+}
+
+case $OPTION in
+ -init)
+ vm_init
+ ;;
+ -setup)
+ vm_setup
+ ;;
+ -cli)
+ vm_cli
+ ;;
+ -start)
+ vm_start
+ ;;
+ -ssh)
+ vm_ssh
+ ;;
+ -test)
+ ;;
+ -stop)
+ vm_stop
+ ;;
+ -clean)
+ vm_clean
+ ;;
+ *)
+ vm_help
+ ;;
+esac