Skip to content

Commit

Permalink
Add uefi bios vars, exec cmd line and log to file parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
  • Loading branch information
darkodraskovic committed Aug 7, 2023
1 parent b92806c commit b7662c8
Showing 1 changed file with 108 additions and 56 deletions.
164 changes: 108 additions & 56 deletions cmd/manager/script/launch-qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,59 @@
#
# user changeable parameters
#
HDA_FILE="${HOME}/ubuntu-18.04-desktop.qcow2"

HDA_FILE="cmd/manager/img/focal-server-cloudimg-amd64.qcow2"
GUEST_SIZE_IN_MB="4096"
SEV_GUEST="1"
SMP_NCPUS="4"
CONSOLE="qxl"
QEMU_INSTALL_DIR=/usr/local/bin/
UEFI_BIOS_CODE="/usr/local/share/qemu/OVMF_CODE.fd"
UEFI_BIOS_VARS="OVMF_VARS.fd"
CONSOLE="serial" # for gtk, use qxl
#VNC_PORT=""
USE_VIRTIO="1"

UEFI_BIOS_CODE="/usr/share/OVMF/OVMF_CODE.fd"
UEFI_BIOS_VARS_ORIG="/usr/share/OVMF/OVMF_VARS.fd"
UEFI_BIOS_VARS_COPY="cmd/manager/img/OVMF_VARS.fd"

CBITPOS=51
HOST_HTTP_PORT=9301
GUEST_HTTP_PORT=9031
HOST_GRPC_PORT=7020
GUEST_GRPC_PORT=7002

ENABLE_FILE_LOG="0"
EXEC_QEMU_CMDLINE="0"

usage() {
echo "$0 [options]"
echo "Available <commands>:"
echo " -hda hard disk ($HDA_FILE)"
echo " -nosev disable sev support"
echo " -mem guest memory"
echo " -smp number of cpus"
echo " -console display console to use (serial or gxl)"
echo " -vnc VNC port to use"
echo " -bios bios to use (default $UEFI_BIOS_CODE)"
echo " -kernel kernel to use"
echo " -initrd initrd to use"
echo " -cdrom CDROM image"
echo " -virtio use virtio devices"
echo " -gdb start gdbserver"
echo " -cbitpos location of the C-bit"
echo " -hosthttp host http port"
echo " -guesthttp guest http port"
echo " -hostgrpc host grpc port"
echo " -guestgrpc guest grpc port"
echo " -hda hard disk ($HDA_FILE)"
echo " -nosev disable sev support"
echo " -mem guest memory"
echo " -smp number of cpus"
echo " -console display console to use (serial or gxl)"
echo " -vnc VNC port to use"
echo " -bios bios to use (default $UEFI_BIOS_CODE)"
echo " -kernel kernel to use"
echo " -initrd initrd to use"
echo " -cdrom CDROM image"
echo " -virtio use virtio devices"
# echo " -gdb start gdbserver"
echo " -cbitpos location of the C-bit"
echo " -hosthttp host http port"
echo " -guesthttp guest http port"
echo " -hostgrpc host grpc port"
echo " -guestgrpc guest grpc port"
echo " -origuefivars UEFI BIOS vars original file (default $UEFI_BIOS_VARS_ORIG)"
echo " -copyuefivars UEFI BIOS vars copy file (default $UEFI_BIOS_VARS_COPY)"
echo " -exec execute the QEMU command (default $EXEC_QEMU_CMDLINE)"
echo " -filelog enable/disable QEMU cmd line file log (default: $ENABLE_FILE_LOG)"
exit 1
}

add_opts() {
echo -n "$* " >> ${QEMU_CMDLINE}
}

run_cmd() {
if ! "$@"; then
echo "Command '$*' failed"
exit 1
fi
}

if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root!"
exit 1
fi

while [[ $1 != "" ]]; do
case "$1" in
-hda)
HDA_FILE="${2}"
HDA_FILE=${2}
shift
;;
-nosev)
Expand Down Expand Up @@ -91,10 +84,10 @@ while [[ $1 != "" ]]; do
UEFI_BIOS_CODE=$(readlink -f "$2")
shift
;;
-netconsole)
NETCONSOLE_PORT=$2
shift
;;
# -netconsole)
# NETCONSOLE_PORT=$2
# shift
# ;;
-initrd)
INITRD_FILE=$2
shift
Expand All @@ -110,35 +103,77 @@ while [[ $1 != "" ]]; do
-virtio)
USE_VIRTIO="1"
;;
-gdb)
USE_GDB="1"
;;
# -gdb)
# USE_GDB="1"
# ;;
-cbitpos)
CBITPOS=$2
shift
;;
-hosthttp)
HOST_HTTP_PORT=$2
shift
;;
-guesthttp)
GUEST_HTTP_PORT=$2
shift
;;
-hostgrpc)
HOST_GRPC_PORT=$2
shift
;;
-guestgrpc)
GUEST_GRPC_PORT=$2
-origuefivars)
UEFI_BIOS_VARS_ORIG=$2
shift
;;
-copyuefivars)
UEFI_BIOS_VARS_COPY=$2
shift
;;
-exec)
EXEC_QEMU_CMDLINE="1"
;;
-filelog)
ENABLE_FILE_LOG="1"
;;
*)
usage;;
esac
shift
done

#
# func definitions
#

add_opts() {
echo -n "$* " >> ${QEMU_CMDLINE}
}

run_cmd() {
if ! "$@"; then
echo "Command '$*' failed"
exit 1
fi
}

if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root!"
exit 1
fi

# copy BIOS variables to new dest for VM use without modifying the original ones
cp "$UEFI_BIOS_VARS_ORIG" "$UEFI_BIOS_VARS_COPY"

#
# Qemu cmd line construction
#

# we add all the qemu command line options into a file
QEMU_CMDLINE=/tmp/cmdline.$$
rm -rf ${QEMU_CMDLINE}

add_opts "${QEMU_INSTALL_DIR}qemu-system-x86_64"
add_opts "$(which qemu-system-x86_64)"

# Basic virtual machine property
add_opts "-enable-kvm -cpu EPYC -machine q35"
Expand All @@ -152,8 +187,8 @@ add_opts "-m ${GUEST_SIZE_IN_MB}M,slots=5,maxmem=30G"
# The OVMF binary, including the non-volatile variable store, appears as a
# "normal" qemu drive on the host side, and it is exposed to the guest as a
# persistent flash device.
add_opts "-drive if=pflash,format=raw,unit=0,file=${UEFI_BIOS_CODE},readonly"
add_opts "-drive if=pflash,format=raw,unit=1,file=${UEFI_BIOS_VARS}"
add_opts "-drive if=pflash,format=raw,unit=0,file=${UEFI_BIOS_CODE},readonly=on"
add_opts "-drive if=pflash,format=raw,unit=1,file=${UEFI_BIOS_VARS_COPY}"

# add CDROM if specified
[ -n "$CDROM_FILE" ] && add_opts "-drive file=${CDROM_FILE},media=cdrom -boot d"
Expand Down Expand Up @@ -206,13 +241,30 @@ fi
# start monitor on pty
add_opts "-monitor pty"

# log the console output in stdout.log
QEMU_CONSOLE_LOG=$(pwd)/stdout.log
#
# Qemu cmd line log
#

# save the command line args into log file
cat $QEMU_CMDLINE | tee "${QEMU_CONSOLE_LOG}"
echo | tee -a "${QEMU_CONSOLE_LOG}"
# Set the log file path if ENABLE_FILE_LOG is 1
if [ "$ENABLE_FILE_LOG" = "1" ]; then
LOG_FILE=$(pwd)/stdout.log

# Save the command line args into log file
cat "$QEMU_CMDLINE" > "$LOG_FILE"
echo >> "$LOG_FILE"
fi

# Log the command line to the console
cat "$QEMU_CMDLINE"
echo

#
# Qemu cmd line execution
#

if [[ "${EXEC_QEMU_CMDLINE}" = "0" ]]; then
exit 0
fi

# map CTRL-C to CTRL ]
echo "Mapping CTRL-C to CTRL-]"
Expand Down

0 comments on commit b7662c8

Please sign in to comment.