qemu-vm/start

98 lines
2.5 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eo pipefail
if [[ -z "$VMNAME" ]]; then
echo "VMNAME not specified, aborting" >&2
exit 1
fi
if [[ -z "$DEBUG" ]]; then
DEBUG=
fi
set -u
SUDO=sudo
$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"
if [[ -e "$base_path.conf.tmpl" ]]; then
(
# shellcheck disable=SC1090
[[ -e "$base_path.sh" ]] && source "$base_path.sh"
envsubst < "$base_path.conf.tmpl" > "$base_path.conf"
)
fi
if [[ -e "$default_path.conf.tmpl" ]]; then
(
# shellcheck disable=SC1090
[[ -e "$default_path.sh" ]] && source "$default_path.sh"
envsubst < "$default_path.conf.tmpl" > "$default_path.conf"
)
fi
if [[ -e "$hardware_path.conf.tmpl" ]]; then
(
# shellcheck disable=SC1090
[[ -e "$hardware_path.sh" ]] && source "$hardware_path.sh"
envsubst < "$hardware_path.conf.tmpl" > "$hardware_path.conf"
)
fi
if [[ -e "$specific_path.conf.tmpl" ]]; then
(
# shellcheck disable=SC1090
[[ -e "$specific_path.sh" ]] && source "$specific_path.sh"
envsubst < "$specific_path.conf.tmpl" > "$specific_path.conf"
)
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 '' < "$base_path.conf" || true
read -ra default_arguments -d '' < "$default_path.conf" || true
read -ra hardware_arguments -d '' < "$hardware_path.conf" || true
read -ra specific_arguments -d '' < "$specific_path.conf" || true
$DEBUG $SUDO nice --adjustment=-20 taskset --cpu-list '1-5,7-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