Skip to content

Commit

Permalink
Further improve the QEMU scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Oct 4, 2024
1 parent 028f4e6 commit be5e52c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
katsu-work/
.DS_Store
*.env*
*.env*
.test/
74 changes: 58 additions & 16 deletions qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ set -x
# ./qemu.sh [qemu-disk|qemu-iso]
# If no argument is provided, qemu-disk is used by default


# Variables for test environment
# You can save these in a .env file in the same directory as this script


# load .env if it exists
if [ -f .env ]; then
source .env
. .env
fi

# default values for QEMU
: ${QEMU_MEM:="4G"}
: ${QEMU_CPU:="4"}
: ${QEMU_BOOT:="uefi"}

: ${QEMU_DISK_SIZE:="20G"}
: ${QEMU_ARGS:=""}
TEST_HOME=$(pwd)/.test
KATSU_WORK=$(pwd)/katsu-work

TEST_DISK="$TEST_HOME/disk.img"

# Get testing mode from second argument

if [ -z "$1" ]; then
Expand All @@ -42,6 +46,9 @@ function find_iso {
echo $iso_path
}

function create_disk {
qemu-img create -f raw "$TEST_DISK" "$QEMU_DISK_SIZE"
}

# Quickly get the absolute path of a file

Expand All @@ -51,30 +58,65 @@ function abs_path {

KATSU_IMG_PATH="$KATSU_WORK/image/katsu.img"

# Mode-specific setup
case "$SCRIPT_MODE" in
qemu-disk)
QEMU_ARGS+=" -drive file="$KATSU_IMG_PATH",format=raw,if=virtio "
;;
qemu-iso)
QEMU_ARGS+=" -cdrom $(find_iso) "
;;
none)
# Boot without any extra disks attached
echo "Booting without any extra disks attached"
;;
cleanup)
# Clean up test environment
sudo umount $KATSU_WORK/chroot --recursive -v
sudo losetup -d $(losetup -j $KATSU_IMG_PATH | cut -d: -f1)
exit 0
;;

wipe)
rm -rf $KATSU_WORK
rm -rf $TEST_HOME
exit 0
;;
*)
echo "Invalid mode: $SCRIPT_MODE"
exit 1
;;
esac

# Setup test environment

if [ "$SCRIPT_MODE" = "qemu-disk" ]; then
QEMU_ARGS="-drive file="$KATSU_IMG_PATH",format=raw,if=virtio"
else
QEMU_ARGS="-cdrom $(find_iso)"
function setup_test_home {
mkdir -p $TEST_HOME
mkdir -p $KATSU_WORK
}

setup_test_home

# create disk if not exist
if [ ! -f "$TEST_DISK" ]; then
create_disk
fi

QEMU_ARGS=" -drive file="$TEST_DISK",format=raw,if=virtio $QEMU_ARGS"

# if QEMU_BOOT = "uefi" then add UEFI firmware
if [ "$QEMU_BOOT" = "uefi" ]; then
QEMU_ARGS+=" -bios /usr/share/OVMF/OVMF_CODE.fd"
QEMU_ARGS+=" -bios /usr/share/OVMF/OVMF_CODE.fd "
fi

# end of mode-specific setup

# Run QEMU


function setup_test_home {
mkdir -p $TEST_HOME
mkdir -p $KATSU_WORK
}
# Finally, run QEMU

qemu-kvm \
-vga qxl \
-machine type=q35,accel=kvm \
-cpu host \
-m $QEMU_MEM \
-smp $QEMU_CPU \
$QEMU_ARGS
$QEMU_ARGS

0 comments on commit be5e52c

Please sign in to comment.