#!/usr/bin/env bash set -eo pipefail if [[ ! -t 1 ]]; then if [[ -z "$TERMINAL" ]]; then TERMINAL=i3-sensible-terminal fi "$TERMINAL" -e "$0" "$@" exit $? fi if [[ -n "$1" ]] && [[ -z "$VMNAME" ]]; then VMNAME="$1" fi if [[ -z "$VMNAME" ]]; then echo "VMNAME not specified, aborting" >&2 exit 1 fi if [[ -z "$DEBUG" ]]; then DEBUG= fi # Sudo prompt if [[ -n "$SUDO" ]]; then true elif [[ -t 1 ]]; then SUDO=sudo else export SUDO_ASKPASS=/usr/lib/ssh/ssh-askpass SUDO='sudo --askpass' fi if ! which "$SUDO" >/dev/null 2>&1; then SUDO=sudo fi set -u cd "$(dirname "$0")" $SUDO true NET_CONF_FILE="$($SUDO mktemp)" # create bridge NET_CONF_FILE="$NET_CONF_FILE" $DEBUG $SUDO --preserve-env=NET_CONF_FILE \ ./net create # rebind devices for device in $(< vfio_devices.txt); do $DEBUG $SUDO ./pci vfio_rebind_device "$device" done # efi variables EFI_VARS="$(mktemp)" cp /usr/share/ovmf/x64/OVMF_VARS.fd "$EFI_VARS" EFI_FIRMWARE=/usr/share/ovmf/x64/OVMF_CODE.fd base_path=base default_path=default hardware_path=hardware specific_path="$VMNAME" o_base_path="$(mktemp)" o_default_path="$(mktemp)" o_hardware_path="$(mktemp)" o_specific_path="$(mktemp)" if [[ -e "$base_path.conf.tmpl" ]]; then ( # shellcheck disable=SC1090 [[ -e "$base_path.sh" ]] && source "$base_path.sh" envsubst < "$base_path.conf.tmpl" > "$o_base_path" ) else cp "$base_path.conf" "$o_base_path" fi if [[ -e "$default_path.conf.tmpl" ]]; then ( # shellcheck disable=SC1090 [[ -e "$default_path.sh" ]] && source "$default_path.sh" envsubst < "$default_path.conf.tmpl" > "$o_default_path" ) else cp "$default_path.conf" "$o_default_path" fi if [[ -e "$hardware_path.conf.tmpl" ]]; then ( # shellcheck disable=SC1090 [[ -e "$hardware_path.sh" ]] && source "$hardware_path.sh" envsubst < "$hardware_path.conf.tmpl" > "$o_hardware_path" ) else cp "$hardware_path.conf" "$o_hardware_path" fi if [[ -e "$specific_path.conf.tmpl" ]]; then ( # shellcheck disable=SC1090 [[ -e "$specific_path.sh" ]] && source "$specific_path.sh" envsubst < "$specific_path.conf.tmpl" > "$o_specific_path" ) else cp "$specific_path.conf" "$o_specific_path" fi if [[ -e "pre-start.sh" ]]; then ./pre-start.sh fi if [[ -e "pre-start-$VMNAME.sh" ]]; then ./"pre-start-$VMNAME.sh" fi # run qemu base_arguments=() default_arguments=() hardware_arguments=() specific_arguments=() # mask EOF read -ra base_arguments -d '' < "$o_base_path" || true read -ra default_arguments -d '' < "$o_default_path" || true read -ra hardware_arguments -d '' < "$o_hardware_path" || true read -ra specific_arguments -d '' < "$o_specific_path" || true $DEBUG $SUDO nice --adjustment=-20 taskset --cpu-list '2-5,8-11' \ qemu-system-x86_64 \ -name "$VMNAME,process=VMNAME" \ -drive if=pflash,format=raw,readonly=on,file="$EFI_FIRMWARE" \ -drive if=pflash,format=raw,file="$EFI_VARS" \ "${base_arguments[@]}" \ "${default_arguments[@]}" \ "${hardware_arguments[@]}" \ "${specific_arguments[@]}" NET_CONF_FILE="$NET_CONF_FILE" $DEBUG $SUDO --preserve-env=NET_CONF_FILE \ ./net delete