From 93eb7d42f0744492281ba8dfd52f52cf6bb65e08 Mon Sep 17 00:00:00 2001 From: Cappy Ishihara Date: Fri, 4 Oct 2024 10:18:24 +0700 Subject: [PATCH] Rewrite QEMU scripts in bash --- .gitignore | 3 +- qemu.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.fish | 81 ------------------------------------------------------ 3 files changed, 82 insertions(+), 82 deletions(-) create mode 100755 qemu.sh delete mode 100755 test.fish diff --git a/.gitignore b/.gitignore index 06dcf82f..f70932c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ katsu-work/ -.DS_Store \ No newline at end of file +.DS_Store +*.env* \ No newline at end of file diff --git a/qemu.sh b/qemu.sh new file mode 100755 index 00000000..68d85a30 --- /dev/null +++ b/qemu.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +set -x + +# This script is used to aid in testing Katsu builds. It will help you quickly set up a QEMU VM test environment so +# you can manually test builds. This script shouldn't be used in prod and is meant for development only. + +# USAGE +# ./qemu.sh [qemu-disk|qemu-iso] +# If no argument is provided, qemu-disk is used by default + + +# Variables for test environment + +# load .env if it exists +if [ -f .env ]; then + source .env +fi + +# default values for QEMU +: ${QEMU_MEM:="4G"} +: ${QEMU_CPU:="4"} +: ${QEMU_BOOT:="uefi"} + +TEST_HOME=$(pwd)/.test +KATSU_WORK=$(pwd)/katsu-work + +# Get testing mode from second argument + +if [ -z "$1" ]; then + SCRIPT_MODE="qemu-disk" +else + SCRIPT_MODE=$1 +fi + +function find_iso { + iso_path=$(find $KATSU_WORK -name "*.iso" | head -n 1) + if [ -z "$iso_path" ]; then + echo "No ISO found in $KATSU_WORK" + exit 1 + fi + echo $iso_path +} + + +# Quickly get the absolute path of a file + +function abs_path { + echo $(realpath $1) +} + +KATSU_IMG_PATH="$KATSU_WORK/image/katsu.img" + + +if [ "$SCRIPT_MODE" = "qemu-disk" ]; then + QEMU_ARGS="-drive file="$KATSU_IMG_PATH",format=raw,if=virtio" +else + QEMU_ARGS="-cdrom $(find_iso)" +fi + +# if QEMU_BOOT = "uefi" then add UEFI firmware +if [ "$QEMU_BOOT" = "uefi" ]; then + QEMU_ARGS+=" -bios /usr/share/OVMF/OVMF_CODE.fd" +fi + + +# Run QEMU + + +function setup_test_home { + mkdir -p $TEST_HOME + mkdir -p $KATSU_WORK +} + +qemu-kvm \ + -machine type=q35,accel=kvm \ + -cpu host \ + -m $QEMU_MEM \ + -smp $QEMU_CPU \ + $QEMU_ARGS \ No newline at end of file diff --git a/test.fish b/test.fish deleted file mode 100755 index 37ddf12e..00000000 --- a/test.fish +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env fish - -set fish_trace 1 - -# This script is used to aid in testing Katsu builds. It will help you quickly set up a VM test environment so -# you can manually test builds. This script shouldn't be used in prod and is meant -# for development only. - - -# Variables for test environment -set KATSU_BIN "katsu" - -# Directory for test data -set TEST_HOME (pwd)/.test - -# katsu-work -set KATSU_WORK (pwd)/katsu-work - -# Get testing mode from second argument -# Can be `qemu` or `wipe` -function get_mode - if test (count $argv) -eq 0 - echo "qemu-disk" - else - echo $argv[1] - end -end - -set -x SCRIPT_MODE (get_mode $argv) - -function find_iso - set iso_path (find $KATSU_WORK -name "*.iso" | head -n 1) - if test -z $iso_path - echo "No ISO found in $KATSU_WORK" - exit 1 - end - echo $iso_path -end - -function _qemu_arg_1 - if test $SCRIPT_MODE = "qemu-disk" - echo "-drive" - else - echo "-cdrom" - end -end -set KATSU_IMG_PATH "$KATSU_WORK/image/katsu.img" -function _qemu_arg_2 - if test $SCRIPT_MODE = "qemu-disk" - echo "file=$KATSU_IMG_PATH,format=raw,if=virtio" - else - echo (find_iso) - end -end - -# Quickly get the absolute path of a file -function abs_path - echo (realpath $argv) -end - - -# Create a test directory -mkdir -p $TEST_HOME - - - -sudo chown $USER $KATSU_IMG_PATH - -qemu-kvm -display gtk \ - -machine type=q35,accel=kvm \ - -cpu host \ - -smp 4 \ - -m 4G \ - -enable-kvm \ - -device virtio-net,netdev=vmnic -netdev user,id=vmnic \ - -bios /usr/share/OVMF/OVMF_CODE.fd \ - (_qemu_arg_1) (_qemu_arg_2) - - - -