From 5d3e6527b17254cad7815f13e002b43aa6d94918 Mon Sep 17 00:00:00 2001 From: T-vK Date: Tue, 24 Aug 2021 03:59:45 +0200 Subject: [PATCH] Clean up vm.sh; Add plugin mechanism for display modes --- default.conf | 55 +- scripts/main/generate-helper-iso.sh | 2 - scripts/main/generate-vm-config.sh | 45 +- scripts/main/vm.sh | 680 ++++++++++++-------- scripts/utils/common/libs/helpers | 1 + scripts/utils/common/plugins/display-mode-1 | 27 + scripts/utils/common/plugins/display-mode-2 | 22 + scripts/utils/common/plugins/display-mode-3 | 24 + scripts/utils/common/plugins/display-mode-4 | 22 + scripts/utils/common/plugins/display-mode-5 | 24 + vm-files/network-scripts/nat_ifdown | 18 - vm-files/network-scripts/nat_ifup | 91 --- vm-files/network-scripts/tap_ifdown | 31 - vm-files/network-scripts/tap_ifup | 72 --- 14 files changed, 559 insertions(+), 555 deletions(-) create mode 100644 scripts/utils/common/plugins/display-mode-1 create mode 100644 scripts/utils/common/plugins/display-mode-2 create mode 100644 scripts/utils/common/plugins/display-mode-3 create mode 100644 scripts/utils/common/plugins/display-mode-4 create mode 100644 scripts/utils/common/plugins/display-mode-5 delete mode 100755 vm-files/network-scripts/nat_ifdown delete mode 100755 vm-files/network-scripts/nat_ifup delete mode 100755 vm-files/network-scripts/tap_ifdown delete mode 100755 vm-files/network-scripts/tap_ifup diff --git a/default.conf b/default.conf index 0511995..9e91df8 100644 --- a/default.conf +++ b/default.conf @@ -25,7 +25,7 @@ DGPU_PCI_ADDRESS=auto # Required; If you don't use Bumblebee, you have to set th # This project has only been tested with Bumblebee enabled. IGPU_PCI_ADDRESS=auto # Recommended; required for mediated iGPU passthrough. you can cehck your iGPU PCI address using lspci -DGPU_PASSTHROUGH=true # Recommended +DGPU_PASSTHROUGH=auto # Recommended SHARE_IGPU=auto # Recommended to save battery life (this is mediated iGPU passthrough using GVT-g) @@ -34,59 +34,18 @@ HOST_DGPU_DRIVER=auto # Only tested with "nvidia" and "amdgpu", not tested with VIRTUAL_INPUT_TYPE="virtio" # "usb-tablet", "virtio" or "". If keyboard input doesn't work properly for you, you may want to use "virtio" instead of "usb-tablet" MAC_ADDRESS="" # Required -NETWORK_MODE=TAP # This option is currently ignored (bridged, NAT and none don't work atm) +NETWORK_MODE="bridged" # Supports bridged or none -USE_LOOKING_GLASS=true # Optional -LOOKING_GLASS_MAX_SCREEN_WIDTH=1920 # Recommended (bigger resolutions will result in a bigger buffer with worse performance) -LOOKING_GLASS_MAX_SCREEN_HEIGHT=1080 # Recommended (bigger resolutions will result in a bigger buffer with worse performance) -LOOKING_GLASS_VERSION=B4 # Recommended +LOOKING_GLASS_MAX_SCREEN_WIDTH=1920 # 1920 Recommended (bigger resolutions will result in a bigger buffer with worse performance) +LOOKING_GLASS_MAX_SCREEN_HEIGHT=1080 # 1080 Recommended (bigger resolutions will result in a bigger buffer with worse performance) +LOOKING_GLASS_VERSION=B4 # B4 Recommended USE_SPICE=true # Required for Windows installation and recommended for Looking Glass -SPICE_PORT=5900 # 5900 is recommended +SPICE_PORT=5904 # 5900 is the normal port, but it may already be used by something else on your system like VNC USE_SPICE_CLIENT="true" # Required for installation -USE_DMA_BUF=false # Optional - -USE_QXL=true # Required for RDP - -DISPLAY_MODE=1 - -# DISPLAY_MODES -# if [ "$DISPLAY_MODE" == 1 ]; then -# if [ "$DMA_BUF_AVAILABLE" == true ]; then -# LOOKING_GLASS=true -# DMA_BUF=true -# QXL=false -# RDP=false -# else -# DMA_BUF=false -# RDP=true -# if [ "$VM_INSTALL" == true ]; then -# QXL=true -# LOOKING_GLASS=false -# else -# QXL=false -# LOOKING_GLASS=true -# fi -# fi -# elif [ "$DISPLAY_MODE" == 2 ]; then -# RDP=true -# if [ "$DMA_BUF_AVAILABLE" == true ]; then -# LOOKING_GLASS=true -# DMA_BUF=true -# QXL=false -# else -# DMA_BUF=false -# if [ "$VM_INSTALL" == true ]; then -# QXL=true -# LOOKING_GLASS=false -# else -# QXL=false -# LOOKING_GLASS=true -# fi -# fi -# fi +DISPLAY_MODE=4 # See scripts/utils/common/plugins ; Mode 4 is the only working one atm ; Looking Glass doesn't play nicely yet USE_FAKE_BATTERY="true" # Recommended to avoid Error 43 diff --git a/scripts/main/generate-helper-iso.sh b/scripts/main/generate-helper-iso.sh index 837c54b..b20b119 100755 --- a/scripts/main/generate-helper-iso.sh +++ b/scripts/main/generate-helper-iso.sh @@ -42,8 +42,6 @@ else echo "> Spice WebDAV daemon already exists in iso folder..." fi -https://www.spice-space.org/download/windows/spice-webdavd/spice-webdavd-x64-2.4.msi - if [ ! -f "${HELPER_ISO_FILES_DIR}/bin/looking-glass-host-setup.exe" ]; then #echo "> Downloading Looking Glass Host application..." #wget "https://github.com/gnif/LookingGlass/releases/download/${LOOKING_GLASS_VERSION}/looking-glass-host.exe" -O "${HELPER_ISO_FILES_DIR}/bin/looking-glass-host.exe" diff --git a/scripts/main/generate-vm-config.sh b/scripts/main/generate-vm-config.sh index 442b4ac..f484c4b 100755 --- a/scripts/main/generate-vm-config.sh +++ b/scripts/main/generate-vm-config.sh @@ -33,9 +33,9 @@ interactiveCfg() { # TODO: change modified value on config file USER_CONFIG_FILE } -echo "!!!!!!" -echo "IF IN DOUBT WITH ANY OF THE FOLLOWING, JUST PRESS ENTER TO USE THE RECOMMENDED/DEFAULT VALUE!" -echo "!!!!!!" +echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" +echo "!!IF IN DOUBT WITH ANY OF THE FOLLOWING, JUST PRESS ENTER TO USE THE RECOMMENDED/DEFAULT VALUE!!" +echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" interactiveCfg "Where should the VM files be saved?" VM_FILES_DIR echo "> Directory set to '${VM_FILES_DIR}'" @@ -52,11 +52,11 @@ cp "${PROJECT_DIR}/default.conf" "${USER_CONFIG_FILE}" echo "> Config will be created at ${USER_CONFIG_FILE}'" interactiveCfg "What should the name of the VM be?" VM_NAME -interactiveCfg "Where to save the VM drive image?" DRIVE_IMG +interactiveCfg "Where to save the VM drive image? (At least 40G is highly recommended; Can't be changed wihtout a reinstall)" DRIVE_IMG interactiveCfg "How big should the VM drive image be?" VM_DISK_SIZE -interactiveCfg "How many CPU cores should the VM get?" CPU_CORE_COUNT -interactiveCfg "How much RAM should the VM get?" RAM_SIZE -interactiveCfg "Path to your Windows installation iso. (If it doesn't exist it will be downloaded automatically.)" INSTALL_IMG +interactiveCfg "How many CPU cores should the VM get? (e.g. 8 or auto; auto=AVAILABLE_CORES-1G)" CPU_CORE_COUNT +interactiveCfg "How much RAM should the VM get? (e.g. 16G or auto; auto=FREE_RAM-1G)" RAM_SIZE +interactiveCfg "Path to your Windows installation iso. (If it doesn't exist it will be downloaded to that location automatically.)" INSTALL_IMG interactiveCfg "Path to a dGPU ROM. (Optional)" DGPU_ROM interactiveCfg "Path to a iGPU ROM. (Optional)" IGPU_ROM interactiveCfg "Path to a folder to share with the VM via SMB. (Optional)" SMB_SHARE_FOLDER @@ -64,33 +64,22 @@ interactiveCfg "Location of OVMF_VARS.fd." OVMF_VARS interactiveCfg "Where to create Creating a copy of OVMF_VARS.fd (containing the executable firmware code and but the non-volatile variable store) for the VM?" OVMF_VARS_VM interactiveCfg "Location of OVMF_CODE.fd." OVMF_CODE interactiveCfg "Location of helper iso or where to create it." HELPER_ISO -interactiveCfg "Pass the dGPU through to the VM." DGPU_PASSTHROUGH -interactiveCfg "Share the iGPU with the VM to allow using Optimus within the VM to save battery life" SHARE_IGPU -interactiveCfg "dGPU driver used by the Linux host (nvidia, nouveau, amdgpu, radeon). (nouveau & radeon are untested)" HOST_DGPU_DRIVER -interactiveCfg "The PCI address of your dGPU as obtained by 'lspci' or 'optimus lspci'. (01:00.0 if you use Bumblebee)" DGPU_PCI_ADDRESS -interactiveCfg "The PCI address of your iGPU as obtained by 'lspci'. (Usually 00:02.0)" IGPU_PCI_ADDRESS +interactiveCfg "Pass the dGPU through to the VM. (true, false or auto to enable if more than one GPU is in this system)" DGPU_PASSTHROUGH +interactiveCfg "Share the iGPU with the VM to allow using Optimus within the VM to save battery life (true, false or auto to share it only if available)" SHARE_IGPU +interactiveCfg "dGPU driver used by the Linux host (E.g. nvidia, nouveau, amdgpu, radeon or auto to detect it automatically)" HOST_DGPU_DRIVER +interactiveCfg "The PCI address of your dGPU as obtained by 'lspci' or 'optimus lspci'. (E.g. 01:00.0 or auto to detect it automatically)" DGPU_PCI_ADDRESS +interactiveCfg "The PCI address of your iGPU as obtained by 'lspci'. (E.g. 00:02.0 or auto to detect it automatically)" IGPU_PCI_ADDRESS interactiveCfg "Virtual input device mode for keyboard and mouse. (if usb-tablet doesn't work properly, you may want to switch to virtio)" VIRTUAL_INPUT_TYPE -interactiveCfg "MAC address to use or leave empty to generate a random one" MAC_ADDRESS -if [ "$MAC_ADDRESS" == "" ]; then - MAC_ADDRESS=$(printf '52:54:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))) - echo "> MAC_ADDRESS generated: ${MAC_ADDRESS}" - echo "> Set MAC_ADDRESS to '${MAC_ADDRESS}'" - crudini --set "${USER_CONFIG_FILE}" "" "MAC_ADDRESS" "\"${MAC_ADDRESS}\"" -fi -interactiveCfg "Network mode to use? Only supports TAP at the moment." NETWORK_MODE -interactiveCfg "Use Looking Glass to get super low latency video output." USE_LOOKING_GLASS +interactiveCfg "MAC address to use (e.g. 11:22:33:44:55:66 or auto to generate it automatically)" MAC_ADDRESS +interactiveCfg "Network mode to use? Only supports bridge at the moment." NETWORK_MODE interactiveCfg "Max screen width with Looking Glass." LOOKING_GLASS_MAX_SCREEN_WIDTH interactiveCfg "Max screen height with Looking Glass." LOOKING_GLASS_MAX_SCREEN_HEIGHT interactiveCfg "Version of Looking Glass to use (B4 is highly recommended)" LOOKING_GLASS_VERSION -interactiveCfg "Enable spice. (Recommended for Looking Glass, required to install Windows)" USE_SPICE +interactiveCfg "Enable spice. (Leave this on unless you know what you're doing!)" USE_SPICE interactiveCfg "Port to use for spice." SPICE_PORT -interactiveCfg "Enable dma-buf. (Yet another way to get display access to your VM)" USE_DMA_BUF -interactiveCfg "Enable QXL. (Required for Windows installation; has to be disabled after the Nvidia driver has been installed!)" USE_QXL +interactiveCfg "Display mode to use (e.g. 1 or 2 ... see scripts/utils/common/plugins)" DISPLAY_MODE interactiveCfg "Provide the VM with a fake battery (Highly recommended to avoid Error 43)" USE_FAKE_BATTERY interactiveCfg "Patch OVMF with your dGPU ROM if you supply one. (Highly recommended to avoid Error 43)" PATCH_OVMF_WITH_VROM interactiveCfg "Tool to use to start/install the VM. (qemu or virt-install)" VM_START_MODE interactiveCfg "List of USB devices to pass through. (Semicolon separated, e.g. vendorid=0x0b12,productid=0x9348;vendorid=0x0b95,productid=0x1790)" USB_DEVICES -# TODO: Make selecting USB devices easier -# TODO: Auto detect PCI addresses for GPUs -# TODO: Don't ask for Bumblebee if there is no nvidia gpu -# TODO: Add auto option for CPU cores, RAM and dGPU driver \ No newline at end of file +# TODO: Make selecting USB devices easier \ No newline at end of file diff --git a/scripts/main/vm.sh b/scripts/main/vm.sh index 6c221fb..13cac67 100755 --- a/scripts/main/vm.sh +++ b/scripts/main/vm.sh @@ -8,29 +8,35 @@ loadConfig # or start the previously created Windows VM, if called like this: `./vm.sh` ##################################################################################################### -echo "> Action: $1" -if [ "$1" = "install" ]; then - VM_INSTALL=true -elif [ "$1" = "start" ]; then - VM_INSTALL=false -elif [ "$1" = "stop" ]; then +ORIGINAL_VM_ACTION="$1" +VM_ACTION="$ORIGINAL_VM_ACTION" +if [ "$VM_ACTION" != "auto" ]; then + echo "> Action: $VM_ACTION" +fi + +if [ "$VM_ACTION" = "install" ]; then + VM_ACTION="install" +elif [ "$VM_ACTION" = "start" ]; then + VM_ACTION="start" +elif [ "$VM_ACTION" = "stop" ]; then if [ "$VM_START_MODE" = "virt-install" ]; then sudo virsh destroy --domain "${VM_NAME}" elif [ "$VM_START_MODE" = "qemu" ]; then - killall qemu-system-x86_64 + sudo killall qemu-system-x86_64 &> /dev/null fi -elif [ "$1" = "auto" ]; then +elif [ "$VM_ACTION" = "auto" ]; then if sudo fdisk -lu "${DRIVE_IMG}" 2> /dev/null | grep --quiet 'Microsoft'; then - VM_INSTALL=false + VM_ACTION="start" else - VM_INSTALL=true + VM_ACTION="install" fi -elif [ "$1" = "remove" ]; then + echo "> Action: $VM_ACTION" +elif [ "$VM_ACTION" = "remove" ]; then if [ "$VM_START_MODE" = "virt-install" ]; then sudo virsh destroy --domain "${VM_NAME}" sudo virsh undefine --domain "${VM_NAME}" --nvram elif [ "$VM_START_MODE" = "qemu" ]; then - killall qemu-system-x86_64 + sudo killall qemu-system-x86_64 &> /dev/null fi if [[ ${DRIVE_IMG} == *.img ]]; then sudo rm -f "${DRIVE_IMG}" @@ -46,7 +52,7 @@ echo "> Start mode: $VM_START_MODE" GET_XML=false DRY_RUN=false -if [ "$1" = "install" ] || [ "$1" = "start" ]; then +if [ "$VM_ACTION" = "install" ] || [ "$VM_ACTION" = "start" ]; then if [ "$2" = "dry-run" ]; then DRY_RUN=true elif [ "$2" = "get-xml" ]; then @@ -63,6 +69,9 @@ alias vgpu="sudo '$COMMON_UTILS_TOOLS_DIR/vgpu-util'" VIRT_INSTALL_PARAMS=() QEMU_PARAMS=() +##################################################################################### +############################## Set basic VM parameters ############################## +##################################################################################### if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-name" "${VM_NAME}") elif [ "$VM_START_MODE" = "virt-install" ]; then @@ -91,11 +100,64 @@ elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./features/hyperv/vendor_id/@state=on" "--xml" "xpath.set=./features/hyperv/vendor_id/@value='12alphanum'") fi -if [ "$MAC_ADDRESS" = "auto" ]; then - MAC_ADDRESS=$(printf '52:54:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))) +if [ "$VM_START_MODE" = "qemu" ]; then + QEMU_PARAMS+=("-mem-prealloc") # for virt-install this enabled by default fi -echo "> Using MAC address: ${MAC_ADDRESS}..." +QEMU_PARAMS+=("-rtc" "clock=host,base=localtime") +QEMU_PARAMS+=("-nographic") +QEMU_PARAMS+=("-serial" "none") +QEMU_PARAMS+=("-parallel" "none") +QEMU_PARAMS+=("-boot" "menu=on") +QEMU_PARAMS+=("-boot" "once=d") +QEMU_PARAMS+=("-k" "en-us") + +QEMU_PARAMS+=("-device" "ich9-intel-hda") +QEMU_PARAMS+=("-device" "hda-output") +QEMU_PARAMS+=("-device" "pci-bridge,addr=12.0,chassis_nr=2,id=head.2") +# More parameters are added throughout the whole script + +VIRT_INSTALL_PARAMS+=("--virt-type" "kvm") +VIRT_INSTALL_PARAMS+=("--os-variant" "win10") +VIRT_INSTALL_PARAMS+=("--arch=x86_64") + +##################################################################################### +################################# Set up networking ################################# +##################################################################################### +if [ "$NETWORK_MODE" == "bridged" ]; then + echo "> Using network mode ${NETWORK_MODE}..." + if ! sudo virsh net-list | grep default | grep --quiet active; then + sudo virsh net-start default + fi + + if [ "$MAC_ADDRESS" = "auto" ]; then + MAC_ADDRESS=$(printf '52:54:BE:EF:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256))) + fi + echo "> Using MAC address: ${MAC_ADDRESS}..." + + INTERFACE_NAME="$(sudo cat /var/lib/libvirt/dnsmasq/default.conf | grep "^interface=" | cut -d'=' -f2-)" + NETWORK="$(sudo ip route | grep " ${INTERFACE_NAME} " | cut -d' ' -f1)" + + if [ "$VM_START_MODE" = "qemu" ]; then + QEMU_PARAMS+=("-net" "nic,model=e1000,macaddr=${MAC_ADDRESS}" "-net" "bridge,br=virbr0") + #QEMU_PARAMS+=("-netdev" "type=tap,id=net0,ifname=tap0,script=${VM_FILES_DIR}/network-scripts/tap_ifup,downscript=${VM_FILES_DIR}/network-scripts/tap_ifdown,vhost=on") + #QEMU_PARAMS+=("-device" "virtio-net-pci,netdev=net0,addr=19.0,mac=${MAC_ADDRESS}") + #-net user,hostfwd=tcp::13389-:3389 -net nic + elif [ "$VM_START_MODE" = "virt-install" ]; then + VIRT_INSTALL_PARAMS+=("--network" "network=default,model=e1000,mac=${MAC_ADDRESS}") + #VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/interface[type=network]/mac@address='${MAC_ADDRESS}'") + #VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/interface[0]/source@network=default") + #if ! sudo virsh net-list | grep default | grep --quiet active; then + # sudo virsh net-start default + #fi + fi +else + echo "Networking will not be enabled for this VM..." +fi + +##################################################################################### +################################### Set CPU Cores ################################### +##################################################################################### if [ "$CPU_CORE_COUNT" = "auto" ]; then AVAILABLE_CPU_CORE_COUNT="$(nproc)" CPU_CORE_COUNT="$((AVAILABLE_CPU_CORE_COUNT-1))" @@ -103,7 +165,6 @@ if [ "$CPU_CORE_COUNT" = "auto" ]; then CPU_CORE_COUNT=16 fi fi - echo "> Using ${CPU_CORE_COUNT} CPU cores..." if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-smp" "${CPU_CORE_COUNT}") @@ -111,15 +172,17 @@ elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--vcpu" "${CPU_CORE_COUNT}") fi +##################################################################################### +################################### Set RAM size #################################### +##################################################################################### if [ "$RAM_SIZE" = "auto" ]; then - FREE_RAM="$(free -g | grep 'Mem: ' | tr -s ' ' | cut -d ' ' -f4)" + FREE_RAM="$(free -g | grep 'Mem: ' | tr -s ' ' | cut -d ' ' -f7)" RAM_SIZE_GB="$((FREE_RAM-1))" if [[ $RAM_SIZE_GB -gt 16 ]]; then RAM_SIZE_GB=16 fi RAM_SIZE="${RAM_SIZE_GB}G" fi - echo "> Using ${RAM_SIZE} of RAM..." if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-m" "${RAM_SIZE}") @@ -128,19 +191,10 @@ elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--memory" "${RAM_SIZE_GB}") fi -if [ "$VM_START_MODE" = "qemu" ]; then - QEMU_PARAMS+=("-mem-prealloc") # for virt-install this enabled by default -fi - -QEMU_PARAMS+=("-rtc" "clock=host,base=localtime") -QEMU_PARAMS+=("-nographic") -QEMU_PARAMS+=("-serial" "none") -QEMU_PARAMS+=("-parallel" "none") -QEMU_PARAMS+=("-boot" "menu=on") -QEMU_PARAMS+=("-boot" "once=d") -QEMU_PARAMS+=("-k" "en-us") - -if [ "$VM_INSTALL" = true ]; then +##################################################################################### +############################## Set install media (ISO) ############################## +##################################################################################### +if [ "$VM_ACTION" = "install" ]; then if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-drive" "file=${INSTALL_IMG},index=1,media=cdrom") elif [ "$VM_START_MODE" = "virt-install" ]; then @@ -148,43 +202,18 @@ if [ "$VM_INSTALL" = true ]; then fi fi +##################################################################################### +################################## Set helper ISO ################################### +##################################################################################### if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-drive" "file=${HELPER_ISO},index=2,media=cdrom") elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--disk" "device=cdrom,path=${HELPER_ISO}") fi -if ! sudo virsh net-list | grep default | grep --quiet active; then - sudo virsh net-start default -fi - -INTERFACE_NAME="$(sudo cat /var/lib/libvirt/dnsmasq/default.conf | grep "^interface=" | cut -d'=' -f2-)" -NETWORK="$(sudo ip route | grep " ${INTERFACE_NAME} " | cut -d' ' -f1)" - -if [ "$VM_START_MODE" = "qemu" ]; then - QEMU_PARAMS+=("-net" "nic,model=e1000,macaddr=${MAC_ADDRESS}" "-net" "bridge,br=virbr0") - #QEMU_PARAMS+=("-netdev" "type=tap,id=net0,ifname=tap0,script=${VM_FILES_DIR}/network-scripts/tap_ifup,downscript=${VM_FILES_DIR}/network-scripts/tap_ifdown,vhost=on") - #QEMU_PARAMS+=("-device" "virtio-net-pci,netdev=net0,addr=19.0,mac=${MAC_ADDRESS}") - #-net user,hostfwd=tcp::13389-:3389 -net nic -elif [ "$VM_START_MODE" = "virt-install" ]; then - VIRT_INSTALL_PARAMS+=("--network" "network=default,model=e1000,mac=${MAC_ADDRESS}") - #VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/interface[type=network]/mac@address='${MAC_ADDRESS}'") - #VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/interface[0]/source@network=default") - #if ! sudo virsh net-list | grep default | grep --quiet active; then - # sudo virsh net-start default - #fi -fi - -QEMU_PARAMS+=("-device" "ich9-intel-hda") -QEMU_PARAMS+=("-device" "hda-output") -QEMU_PARAMS+=("-device" "pci-bridge,addr=12.0,chassis_nr=2,id=head.2") -# More parameters are added throughout the whole script - -VIRT_INSTALL_PARAMS+=("--virt-type" "kvm") -VIRT_INSTALL_PARAMS+=("--os-variant" "win10") -VIRT_INSTALL_PARAMS+=("--arch=x86_64") -#VIRT_INSTALL_PARAMS+=("--unattended") - +##################################################################################### +########################### Set/create OS install drive ############################# +##################################################################################### if [[ ${DRIVE_IMG} == /dev/* ]]; then echo "> Using a physical OS drive..." if [ "$VM_START_MODE" = "qemu" ]; then @@ -195,9 +224,13 @@ if [[ ${DRIVE_IMG} == /dev/* ]]; then #QEMU_PARAMS+=("-drive" "file=/dev/sda,if=virtio" "-drive" "file=/dev/sdb,if=virtio" "-drive" "file=/dev/sdc,if=virtio" "-drive" "file=/dev/sdd,if=virtio" "-snapshot") elif [[ ${DRIVE_IMG} == *.img ]]; then echo "> Using a virtual OS drive..." + if [ "$VM_ACTION" = "install" ] && [ -f "${DRIVE_IMG}" ]; then + echo "> Removing old virtual disk..." + sudo rm -rf "${DRIVE_IMG}" + fi if [ ! -f "${DRIVE_IMG}" ]; then echo "> Creating a virtual disk for the VM..." - qemu-img create -f raw "${DRIVE_IMG}" "${VM_DISK_SIZE}" + qemu-img create -f raw "${DRIVE_IMG}" "${VM_DISK_SIZE}" > /dev/null sudo chown "$(whoami):$(id -gn "$(whoami)")" "${DRIVE_IMG}" fi if [ "$VM_START_MODE" = "qemu" ]; then @@ -205,18 +238,16 @@ elif [[ ${DRIVE_IMG} == *.img ]]; then elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--disk" "${DRIVE_IMG}") fi + OS_DRIVE_SIZE="$(sudo ls -l --b=G "${DRIVE_IMG}" | cut -d " " -f5)" + echo "> Virtual OS drive has ${OS_DRIVE_SIZE} of storage." else echo "> Error: It appears that no proper OS drive (image) has been provided. Check your 'DRIVE_IMG' var: '${DRIVE_IMG}'" exit fi -if [ ! -f "${OVMF_VARS_VM}" ] || [ "$VM_INSTALL" = true ]; then - echo "> Creating fresh OVMF_VARS copy for this VM..." - sudo rm -f "${OVMF_VARS_VM}" - sudo cp "${OVMF_VARS}" "${OVMF_VARS_VM}" - sudo chown "$(whoami):$(id -gn "$(whoami)")" "${OVMF_VARS_VM}" -fi - +##################################################################################### +################ Figure out if optirun or DRI_PRIME should be used ################## +##################################################################################### if sudo which optirun &> /dev/null && sudo optirun echo > /dev/null ; then OPTIRUN_PREFIX="optirun " DRI_PRIME_PREFIX="" @@ -226,39 +257,13 @@ else if [ "$SUPPORTS_DRI_PRIME" = true ]; then DRI_PRIME_PREFIX="DRI_PRIME=1 " else - echo "> Warning: Bumblebee is not available or doesn't work properly. Continuing anyway..." - fi -fi - -echo "> Loading vfio-pci kernel module..." -sudo modprobe vfio-pci - -if [ "$USE_LOOKING_GLASS" = true ]; then - echo "> Using Looking Glass..." - echo "> Calculating required buffer size for ${LOOKING_GLASS_MAX_SCREEN_WIDTH}x${LOOKING_GLASS_MAX_SCREEN_HEIGHT} for Looking Glass..." - UNROUNDED_BUFFER_SIZE=$((($LOOKING_GLASS_MAX_SCREEN_WIDTH * $LOOKING_GLASS_MAX_SCREEN_HEIGHT * 4 * 2)/1024/1024+10)) - BUFFER_SIZE=1 - while [[ $BUFFER_SIZE -le $UNROUNDED_BUFFER_SIZE ]]; do - BUFFER_SIZE=$(($BUFFER_SIZE*2)) - done - LOOKING_GLASS_BUFFER_SIZE="${BUFFER_SIZE}" - echo "> Looking Glass buffer size set to: ${LOOKING_GLASS_BUFFER_SIZE}MB" - if [ "$VM_START_MODE" = "qemu" ]; then - QEMU_PARAMS+=("-device" "ivshmem-plain,memdev=ivshmem,bus=pcie.0") - QEMU_PARAMS+=("-object" "memory-backend-file,id=ivshmem,share=on,mem-path=/dev/shm/looking-glass,size=${LOOKING_GLASS_BUFFER_SIZE}M") - elif [ "$VM_START_MODE" = "virt-install" ]; then - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/@name=looking-glass") - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/model/@type=ivshmem-plain") - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/size=${LOOKING_GLASS_BUFFER_SIZE}") - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/size/@unit=M") + echo "> Bumblebee is not available..." fi - #sudo bash -c "echo '#Type Path Mode UID GID Age Argument' > /etc/tmpfiles.d/10-looking-glass.conf" - #sudo bash -c "echo 'f /dev/shm/looking-glass 0660 qemu kvm - ' >> /etc/tmpfiles.d/10-looking-glass.conf" - #sudo systemd-tmpfiles --create --prefix=/dev/shm/looking-glass -else - echo "> Not using Looking Glass..." fi +##################################################################################### +########################## Set up samba share directory ############################# +##################################################################################### if [ -z "$SMB_SHARE_FOLDER" ]; then echo "> Not using SMB share..." else @@ -266,7 +271,11 @@ else QEMU_PARAMS+=("-net" "user,smb=${SMB_SHARE_FOLDER}") fi +##################################################################################### +####################### Set parameters for dGPU passthrough ######################### +##################################################################################### if [ "$DGPU_PASSTHROUGH" != false ]; then + DGPU_PASSTHROUGH=false availableGpusIds="$(sudo ${OPTIRUN_PREFIX}lshw -C display -businfo | grep 'pci@' | cut -d'@' -f2 | cut -d' ' -f1 | cut -d':' -f2-)" if [ "$DGPU_PCI_ADDRESS" = "auto" ]; then DGPU_PCI_ADDRESS="" @@ -281,14 +290,17 @@ if [ "$DGPU_PASSTHROUGH" != false ]; then if [ "$(echo -e "$availableGpusIds" | wc -l)" -le 1 ]; then echo "> Not using dGPU passthrough because single GPU passthrough is not supported yet..." - if [ "$DGPU_PCI_ADDRESS" != "" ]; then + elif [ "$DGPU_PCI_ADDRESS" != "" ]; then + DGPU_PASSTHROUGH=true echo "> Using dGPU passthrough..." if [ "$HOST_DGPU_DRIVER" = "auto" ]; then HOST_DGPU_DRIVER="$(sudo ${OPTIRUN_PREFIX}lspci -s "$DGPU_PCI_ADDRESS" -vv | grep driver | cut -d':' -f2 | cut -d' ' -f2-)" fi - echo "> dGPU is: '$DGPU_PCI_ADDRESS' with driver '$HOST_DGPU_DRIVER'" + DGPU_INFO="$(sudo lspci | grep "$DGPU_PCI_ADDRESS" | cut -d' ' -f2-)" + echo "> dGPU is: $DGPU_INFO" + echo "> dGPU dirver is $HOST_DGPU_DRIVER" echo "> Retrieving and parsing DGPU IDs..." DGPU_IDS=$(export DRI_PRIME=1 && sudo ${OPTIRUN_PREFIX}lspci -n -s "${DGPU_PCI_ADDRESS}" | grep -oP "\w+:\w+" | tail -1) @@ -310,12 +322,6 @@ if [ "$DGPU_PASSTHROUGH" != false ]; then exit 1 fi - echo "> Unbinding dGPU from ${HOST_DGPU_DRIVER} driver..." - driver unbind "${DGPU_PCI_ADDRESS}" - echo "> Binding dGPU to VFIO driver..." - driver bind "${DGPU_PCI_ADDRESS}" "vfio-pci" - #sudo bash -c "echo 'options vfio-pci ids=${DGPU_VENDOR_ID}:${DGPU_DEVICE_ID}' > '/etc/modprobe.d/vfio.conf'" - # TODO: Make sure to also do the rebind for the other devices that are in the same iommu group (exclude stuff like PCI Bridge root ports that don't have vfio drivers) if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-device" "ioh3420,bus=pcie.0,addr=1c.0,multifunction=on,port=1,chassis=1,id=pci.1") # DGPU root port elif [ "$VM_START_MODE" = "virt-install" ]; then @@ -353,9 +359,14 @@ else echo "> Not using dGPU passthrough..." fi -if [ "$SHARE_IGPU" = true ] || [ "$SHARE_IGPU" = auto ]; then +############################################################################################## +### If mediated iGPU passthrough is enabled, check if vGPU exists or if one can be created ### +############################################################################################## +DMA_BUF_AVAILABLE=false +if [ "$SHARE_IGPU" = true ] || [ "$SHARE_IGPU" = "auto" ]; then if [ "$IGPU_PCI_ADDRESS" = "auto" ]; then + IGPU_PCI_ADDRESS="" availableGpusIds="$(sudo ${OPTIRUN_PREFIX}lshw -C display -businfo | grep 'pci@' | cut -d'@' -f2 | cut -d' ' -f1 | cut -d':' -f2-)" while IFS= read -r pciAddress; do @@ -367,10 +378,7 @@ if [ "$SHARE_IGPU" = true ] || [ "$SHARE_IGPU" = auto ]; then done <<< "$availableGpusIds" fi - if [ "$IGPU_PCI_ADDRESS" != "auto" ]; then - echo "> Using mediated iGPU passthrough..." - echo "> iGPU is: $IGPU_PCI_ADDRESS" - + if [ "$IGPU_PCI_ADDRESS" != "" ]; then vgpu init # load required kernel modules # FIXME: There is a bug in Linux that prevents creating new vGPUs without rebooting after removing one. @@ -381,58 +389,18 @@ if [ "$SHARE_IGPU" = true ] || [ "$SHARE_IGPU" = auto ]; then echo "> Creating a vGPU for mediated iGPU passthrough..." VGPU_UUID="$(vgpu create "${IGPU_PCI_ADDRESS}")" if [ "$?" = "1" ]; then - echo "> Failed creating a vGPU. (You can try again. If you still get this error, you have to reboot. This seems to be a bug in Linux.)" + echo "> [Error] Failed creating a vGPU. (You can try again. If you still get this error, you have to reboot. This seems to be a bug in Linux.)" echo "> Continuing without mediated iGPU passthrough..." VGPU_UUID="" fi fi - + if [ "$VGPU_UUID" != "" ]; then - # TODO: same as for iGPU - if [ "$VM_START_MODE" = "qemu" ]; then - - if [ "$USE_DMA_BUF" = true ]; then - echo "> Using dma-buf..." - QEMU_PARAMS+=("-display" "egl-headless") #"-display" "gtk,gl=on" # DMA BUF Display - DMA_BUF_PARAM=",display=on,x-igd-opregion=on" - else - echo "> Not using dma-buf..." - DMA_BUF_PARAM="" - fi - - if [ -z "$IGPU_ROM" ]; then - echo "> Not using iGPU vBIOS override..." - #IGPU_ROM_PARAM=",rom.bar=on" - else - echo "> Using iGPU vBIOS override..." - IGPU_ROM_PARAM=",romfile=${IGPU_ROM}" - fi - - QEMU_PARAMS+=("-device" "vfio-pci,bus=pcie.0,addr=05.0,sysfsdev=/sys/bus/mdev/devices/${VGPU_UUID}${IGPU_ROM_PARAM}${DMA_BUF_PARAM}") # GVT-G - elif [ "$VM_START_MODE" = "virt-install" ]; then - if [ "$USE_DMA_BUF" = true ]; then - echo "> Using dma-buf..." - #QEMU_PARAMS+=("-display" "egl-headless") #"-display" "gtk,gl=on" # DMA BUF Display - QEMU_PARAMS+=("-set" "device.hostdev1.x-igd-opregion=on") - GVTG_DISPLAY_STATE="on" - else - echo "> Not using dma-buf..." - GVTG_DISPLAY_STATE="off" - fi - - if [ -z "$IGPU_ROM" ]; then - echo "> Not using iGPU vBIOS override..." - IGPU_ROM_PARAM=",rom.bar=on" - fi - VIRT_INSTALL_PARAMS+=("--hostdev" "type=mdev,alias.name=hostdev1,address.domain=0000,address.bus=0,address.slot=2,address.function=0,address.type=pci,address.multifunction=on${IGPU_ROM_PARAM}") - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/hostdev[2]/@model=vfio-pci") - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/hostdev[2]/source/address/@uuid=${VGPU_UUID}") - - if [ ! -z "$IGPU_ROM" ]; then - echo "> Using iGPU vBIOS override..." - VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/hostdev[2]/rom/@file=${IGPU_ROM}") - fi - fi + IGPU_INFO="$(sudo lspci | grep "$IGPU_PCI_ADDRESS" | cut -d' ' -f2-)" + echo "> iGPU is: $IGPU_INFO" + echo "> iGPU dirver is: $HOST_DGPU_DRIVER" + echo "> UUID of vGPU is: $VGPU_UUID" + DMA_BUF_AVAILABLE=true fi else echo "> No iGPU found. - Not using mediated iGPU passthrough..." @@ -441,19 +409,108 @@ else echo "> Not using mediated iGPU passthrough..." fi -if [ "$USE_SPICE" = true ]; then - echo "> Using spice on port ${SPICE_PORT}..." - #QEMU_PARAMS+=("-spice" "port=${SPICE_PORT},addr=127.0.0.1,disable-ticketing") #Spice +##################################################################################### +############################ Load display output plugin ############################# +##################################################################################### +if [ "$DISPLAY_MODE" != "" ]; then + echo "> Loading display-mode-${DISPLAY_MODE} plugin..." + source "${COMMON_UTILS_PLUGINS_DIR}/display-mode-${DISPLAY_MODE}" +else + echo "> [Error] No display mode provided..." +fi + +######################################################################################################### +### If there is a vGPU for mediated iGPU passthrough, set parameters for iGPU passthrough and dma-buf ### +######################################################################################################### +if [ "$VGPU_UUID" != "" ]; then if [ "$VM_START_MODE" = "qemu" ]; then - QEMU_PARAMS+=("-spice" "port=${SPICE_PORT},addr=127.0.0.1,disable-ticketing") #Spice + + if [ "$USE_DMA_BUF" = true ]; then + echo "> Using dma-buf..." + QEMU_PARAMS+=("-display" "egl-headless") #"-display" "gtk,gl=on" # DMA BUF Display + DMA_BUF_PARAM=",display=on,x-igd-opregion=on" + else + echo "> Not using dma-buf..." + DMA_BUF_PARAM="" + fi + + if [ -z "$IGPU_ROM" ]; then + echo "> Not using iGPU vBIOS override..." + #IGPU_ROM_PARAM=",rom.bar=on" + else + echo "> Using iGPU vBIOS override..." + IGPU_ROM_PARAM=",romfile=${IGPU_ROM}" + fi + + QEMU_PARAMS+=("-device" "vfio-pci,bus=pcie.0,addr=05.0,sysfsdev=/sys/bus/mdev/devices/${VGPU_UUID}${IGPU_ROM_PARAM}${DMA_BUF_PARAM}") # GVT-G elif [ "$VM_START_MODE" = "virt-install" ]; then - VIRT_INSTALL_PARAMS+=("--channel" "spicevmc,target.address=127.0.0.1:${SPICE_PORT}") - #VIRT_INSTALL_PARAMS+=("--graphics" "spice,port=${SPICE_PORT}") + if [ "$USE_DMA_BUF" = true ]; then + echo "> Using dma-buf..." + #QEMU_PARAMS+=("-display" "egl-headless") #"-display" "gtk,gl=on" # DMA BUF Display + QEMU_PARAMS+=("-set" "device.hostdev1.x-igd-opregion=on") + GVTG_DISPLAY_STATE="on" + else + echo "> Not using dma-buf..." + GVTG_DISPLAY_STATE="off" + fi + + if [ -z "$IGPU_ROM" ]; then + echo "> Not using iGPU vBIOS override..." + IGPU_ROM_PARAM=",rom.bar=on" + fi + VIRT_INSTALL_PARAMS+=("--hostdev" "type=mdev,alias.name=hostdev1,address.domain=0000,address.bus=0,address.slot=2,address.function=0,address.type=pci,address.multifunction=on${IGPU_ROM_PARAM}") + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/hostdev[2]/@model=vfio-pci") + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/hostdev[2]/source/address/@uuid=${VGPU_UUID}") + + if [ ! -z "$IGPU_ROM" ]; then + echo "> Using iGPU vBIOS override..." + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/hostdev[2]/rom/@file=${IGPU_ROM}") + fi fi +fi + +##################################################################################### +############################### Set Spice parameters ################################ +##################################################################################### +if [ "$USE_SPICE" = true ]; then + if sudo lsof -i ":${SPICE_PORT}" | grep --quiet LISTEN; then + echo "[Error] Something is blocking the SPICE_PORT (${SPICE_PORT})! Change it in your config or kill whatever is blocking it." + else + echo "> Using spice on port ${SPICE_PORT}..." + #QEMU_PARAMS+=("-spice" "port=${SPICE_PORT},addr=127.0.0.1,disable-ticketing") #Spice + if [ "$VM_START_MODE" = "qemu" ]; then + QEMU_PARAMS+=("-spice" "port=${SPICE_PORT},addr=127.0.0.1,disable-ticketing") #Spice + elif [ "$VM_START_MODE" = "virt-install" ]; then + VIRT_INSTALL_PARAMS+=("--channel" "spicevmc,target.address=127.0.0.1:${SPICE_PORT}") + #VIRT_INSTALL_PARAMS+=("--graphics" "spice,port=${SPICE_PORT}") + fi + fi + function waitForSpice() { + while true; do + if sudo lsof -i ":${SPICE_PORT}" | grep --quiet LISTEN &> /dev/null; then + break + fi + sleep 1 + done + } else echo "> Not using Spice..." fi +##################################################################################### +###################### Check if spice client should be started ###################### +##################################################################################### +if [ "$USE_SPICE_CLIENT" = "auto" ] && [ "$USE_SPICE" = true ]; then + if [ "$VM_ACTION" = "install" ]; then + USE_SPICE_CLIENT=true + elif [ "$USE_LOOKING_GLASS" = true ]; then + USE_SPICE_CLIENT=true + fi +fi + +##################################################################################### +############ Set QXL parameters if loaded display output plugin wants it ############ +##################################################################################### if [ "$USE_QXL" = true ]; then echo "> Using QXL..." if [ "$VM_START_MODE" = "qemu" ]; then @@ -461,11 +518,63 @@ if [ "$USE_QXL" = true ]; then #QEMU_PARAMS+=("-vga" "qxl") elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--video" "qxl") + #-video qxl --channel spicevmc fi else echo "> Not using QXL..." fi -#-video qxl --channel spicevmc + +##################################################################################### +####### Set Looking Glass parameters if loaded display output plugin wants it ####### +##################################################################################### +if [ "$USE_LOOKING_GLASS" = true ]; then + echo "> Using Looking Glass..." + echo "> Calculating required buffer size for ${LOOKING_GLASS_MAX_SCREEN_WIDTH}x${LOOKING_GLASS_MAX_SCREEN_HEIGHT} for Looking Glass..." + UNROUNDED_BUFFER_SIZE=$((($LOOKING_GLASS_MAX_SCREEN_WIDTH * $LOOKING_GLASS_MAX_SCREEN_HEIGHT * 4 * 2)/1024/1024+10)) + BUFFER_SIZE=1 + while [[ $BUFFER_SIZE -le $UNROUNDED_BUFFER_SIZE ]]; do + BUFFER_SIZE=$(($BUFFER_SIZE*2)) + done + LOOKING_GLASS_BUFFER_SIZE="${BUFFER_SIZE}" + echo "> Looking Glass buffer size set to: ${LOOKING_GLASS_BUFFER_SIZE}MB" + if [ "$VM_START_MODE" = "qemu" ]; then + QEMU_PARAMS+=("-device" "ivshmem-plain,memdev=ivshmem,bus=pcie.0") + QEMU_PARAMS+=("-object" "memory-backend-file,id=ivshmem,share=on,mem-path=/dev/shm/looking-glass,size=${LOOKING_GLASS_BUFFER_SIZE}M") + elif [ "$VM_START_MODE" = "virt-install" ]; then + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/@name=looking-glass") + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/model/@type=ivshmem-plain") + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/size=${LOOKING_GLASS_BUFFER_SIZE}") + VIRT_INSTALL_PARAMS+=("--xml" "xpath.set=./devices/shmem/size/@unit=M") + fi + #sudo bash -c "echo '#Type Path Mode UID GID Age Argument' > /etc/tmpfiles.d/10-looking-glass.conf" + #sudo bash -c "echo 'f /dev/shm/looking-glass 0660 qemu kvm - ' >> /etc/tmpfiles.d/10-looking-glass.conf" + #sudo systemd-tmpfiles --create --prefix=/dev/shm/looking-glass + + function autoConnectLookingGlass() { + while true; do + VM_IP="$(sudo nmap -sn -n ${NETWORK} -T5 | grep "MAC Address: ${MAC_ADDRESS}" -B 2 | head -1 | rev | cut -d' ' -f1 | rev)" + if [ "$VM_IP" != "" ]; then + while true; do + if nc -vz "$VM_IP" 3389 &> /dev/null; then + sleep 5 + echo "> Starting the Looking Glass client to connect with the VM..." + sudo -u "$(logname)" "${THIRDPARTY_DIR}/LookingGlass/client/build/looking-glass-client" -p "${SPICE_PORT}" 2>&1 | grep '^\[E\]' & + break + fi + sleep 1 + done + break + fi + sleep 1 + done + } +else + echo "> Not using Looking Glass..." +fi + +##################################################################################### +########################## Set up fake battery if enabled ########################### +##################################################################################### if [ "$USE_FAKE_BATTERY" = true ]; then echo "> Using fake battery..." if [ ! -f "${VM_FILES_DIR}/fake-battery.aml" ]; then @@ -481,6 +590,20 @@ else echo "> Not using fake battery..." fi + +##################################################################################### +########### Create copy of OVMF_VARS_VM which is required for UEFI VMs ############# +##################################################################################### +if [ ! -f "${OVMF_VARS_VM}" ] || [ "$VM_ACTION" = "install" ]; then + echo "> Creating fresh OVMF_VARS copy for this VM..." + sudo rm -f "${OVMF_VARS_VM}" + sudo cp "${OVMF_VARS}" "${OVMF_VARS_VM}" + sudo chown "$(whoami):$(id -gn "$(whoami)")" "${OVMF_VARS_VM}" +fi + +##################################################################################### +####################### Patch OVMF with vBIOS ROM if enabled ######################## +##################################################################################### if [ "$PATCH_OVMF_WITH_VROM" = true ]; then PATCHED_OVMF_FILES_DIR="${VM_FILES_DIR}/patched-ovmf-files" if [ "$DGPU_ROM" != "" ]; then @@ -500,7 +623,7 @@ if [ "$PATCH_OVMF_WITH_VROM" = true ]; then sudo rm -rf "${PATCHED_OVMF_FILES_DIR}/tmp-build" fi OVMF_CODE="${PATCHED_OVMF_FILES_DIR}/${DGPU_ROM_NAME}_OVMF_CODE.fd" - if [ "$VM_INSTALL" = true ]; then + if [ "$VM_ACTION" = "install" ]; then echo "> Creating fresh copy of patched OVMF VARS..." rm -f "${OVMF_VARS_VM}" sudo cp "${PATCHED_OVMF_FILES_DIR}/${DGPU_ROM_NAME}_OVMF_VARS.fd" "${OVMF_VARS_VM}" @@ -513,6 +636,9 @@ else echo "> Not using patched OVMF..." fi +##################################################################################### +############################### Set OVMF parameters ################################# +##################################################################################### if [ "$VM_START_MODE" = "qemu" ]; then QEMU_PARAMS+=("-drive" "if=pflash,format=raw,readonly=on,file=${OVMF_CODE}") QEMU_PARAMS+=("-drive" "if=pflash,format=raw,file=${OVMF_VARS_VM}") @@ -520,6 +646,9 @@ elif [ "$VM_START_MODE" = "virt-install" ]; then VIRT_INSTALL_PARAMS+=("--boot" "loader=${OVMF_CODE},loader.readonly=yes,loader.type=pflash,nvram.template=${OVMF_VARS_VM},loader_secure=no") fi +##################################################################################### +########################## Set up USB device passthrough ############################ +##################################################################################### QEMU_PARAMS+=("-usb") if [ -z "$USB_DEVICES" ]; then echo "> Not using USB passthrough..." @@ -533,6 +662,9 @@ else done fi +##################################################################################### +############################### Set up input method ################################# +##################################################################################### if [ "$VIRTUAL_INPUT_TYPE" = "virtio" ]; then echo "> Using virtual input method 'virtio' for keyboard/mouse input..." QEMU_PARAMS+=("-device" "virtio-keyboard-pci,bus=head.2,addr=03.0,display=video.2") @@ -544,66 +676,75 @@ else echo "> Not using virtual input method for keyboard/mouse input..." fi -RDP_USER=Administrator -RDP_PASSWORD=admin - -# Run it once because the first time it prints a useless message instead of actually encrypting -echo "$RDP_PASSWORD" | remmina --encrypt-password &> /dev/null - -# Run it again, hoping it always works the second time -RDP_PASSWORD_ENCRYPTED="$(echo "$RDP_PASSWORD" | remmina --encrypt-password | grep 'Encrypted password: ' | cut -d':' -f2- | tr -d ' ')" - -function autoConnectRdp() { - while true; do - VM_IP="$(sudo nmap -sn -n ${NETWORK} -T5 | grep "MAC Address: ${MAC_ADDRESS}" -B 2 | head -1 | rev | cut -d' ' -f1 | rev)" - if [ "$VM_IP" != "" ]; then - echo "" - echo "> The IP address of the VM is: ${VM_IP}" - echo "" - echo "> Waiting for RDP to be available in the VM..." - while true; do - if nc -vz "$VM_IP" 3389 &> /dev/null; then - echo "> Opening Remmina to start an RDP connection with the VM..." - remmina -c "rdp://${RDP_USER}:${RDP_PASSWORD_ENCRYPTED}@${VM_IP}" &> /dev/null & - #if [ "$USE_LOOKING_GLASS" = true ]; then - # echo "> Starting the Looking Glass client..." - # sudo -u "$(logname)" "${THIRDPARTY_DIR}/LookingGlass/client/build/looking-glass-client" -p "${SPICE_PORT}" 2>&1 | grep '^\[E\]' & - #fi - break - fi - done - break - fi - sleep 1 - done -} - -#echo "> Starting RDP autoconnect background task..." -#autoConnectRdp & - -if [ "$VM_INSTALL" = true ]; then - echo "> Deleting VM if it already exists..." - sudo virsh destroy --domain "${VM_NAME}" &> /dev/null - sudo virsh undefine --domain "${VM_NAME}" --nvram &> /dev/null +##################################################################################### +################################### Set up RDP ###################################### +##################################################################################### +if [ "$USE_RDP" = true ] && [ "$NETWORK_MODE" != "none" ]; then + echo "> Using RDP..." + RDP_USER=Administrator + RDP_PASSWORD=admin + # Run it once because the first time it prints a useless message instead of actually encrypting + echo "$RDP_PASSWORD" | remmina --encrypt-password &> /dev/null + # Run it again, hoping it always works the second time + RDP_PASSWORD_ENCRYPTED="$(echo "$RDP_PASSWORD" | remmina --encrypt-password | grep 'Encrypted password: ' | cut -d':' -f2- | tr -d ' ')" + + function autoConnectRdp() { + while true; do + VM_IP="$(sudo nmap -sn -n ${NETWORK} -T5 | grep "MAC Address: ${MAC_ADDRESS}" -B 2 | head -1 | rev | cut -d' ' -f1 | rev)" + if [ "$VM_IP" != "" ]; then + echo "" + echo "> The IP address of the VM is: ${VM_IP}" + echo "" + echo "> Waiting for RDP to be available in the VM..." + while true; do + if nc -vz "$VM_IP" 3389 &> /dev/null; then + echo "> Opening Remmina to start an RDP connection with the VM..." + remmina -c "rdp://${RDP_USER}:${RDP_PASSWORD_ENCRYPTED}@${VM_IP}" &> /dev/null & + break + fi + sleep 1 + done + break + fi + sleep 1 + done + } +else + echo "> Not using RDP..." fi +##################################################################################### +################# Load vfio-pci and bind dGPU to vfio-pci driver #################### +##################################################################################### +if [ "$DRY_RUN" = false ]; then + if [ "$DGPU_PASSTHROUGH" = true ] || [ "$SHARE_IGPU" = true ]; then + echo "> Loading vfio-pci kernel module..." + sudo modprobe vfio-pci + fi - -if [ "$USE_SPICE_CLIENT" = "auto" ] && [ "$USE_SPICE" = true ]; then - if [ "$VM_INSTALL" = true ]; then - USE_SPICE_CLIENT=true - elif [ "$USE_LOOKING_GLASS" = true ]; then - USE_SPICE_CLIENT=false + if [ "$DGPU_PASSTHROUGH" = true ]; then + echo "> Unbinding dGPU from ${HOST_DGPU_DRIVER} driver..." + driver unbind "${DGPU_PCI_ADDRESS}" + echo "> Binding dGPU to VFIO driver..." + driver bind "${DGPU_PCI_ADDRESS}" "vfio-pci" + #sudo bash -c "echo 'options vfio-pci ids=${DGPU_VENDOR_ID}:${DGPU_DEVICE_ID}' > '/etc/modprobe.d/vfio.conf'" + # TODO: Make sure to also do the rebind for the other devices that are in the same iommu group (exclude stuff like PCI Bridge root ports that don't have vfio drivers) fi fi -if [ "$DRY_RUN" = false ] && [ "$VM_INSTALL" = true ]; then - echo "> Repeatedly sending keystrokes to the new VM for 30 seconds to ensure the Windows ISO boots..." +if [ "$VM_ACTION" = "install" ]; then + echo "> Deleting VM if it already exists..." + sudo virsh destroy --domain "${VM_NAME}" &> /dev/null + sudo virsh undefine --domain "${VM_NAME}" --nvram &> /dev/null fi -if [ "$VM_START_MODE" = "qemu" ]; then +##################################################################################### +########################## Start a few background tasks ############################# +##################################################################################### +if [ "$VM_START_MODE" = "qemu" ]; then if [ "$DRY_RUN" = false ]; then - if [ "$VM_INSTALL" = true ]; then + if [ "$VM_ACTION" = "install" ]; then + echo "> Repeatedly sending keystrokes to the new VM for 30 seconds to ensure the Windows ISO boots..." QEMU_PARAMS+=("-monitor" "unix:/tmp/${VM_NAME}-monitor,server,nowait") bash -c "for i in {1..30}; do echo 'sendkey home' | sudo socat - 'UNIX-CONNECT:/tmp/${VM_NAME}-monitor'; sleep 1; done" &> /dev/null & fi @@ -612,10 +753,29 @@ if [ "$VM_START_MODE" = "qemu" ]; then echo "> Starting the spice client at localhost:${SPICE_PORT}..." bash -c "sleep 2; spicy -h localhost -p ${SPICE_PORT}" & fi - - echo "> Starting the Virtual Machine using qemu..." fi +elif [ "$VM_START_MODE" = "virt-install" ]; then + if [ "$DRY_RUN" = false ]; then + if [ "$VM_ACTION" = "install" ]; then + echo "> Repeatedly sending keystrokes to the new VM for 30 seconds to ensure the Windows ISO boots..." + bash -c "for i in {1..30}; do sudo virsh send-key ${VM_NAME} KEY_HOME; sleep 1; done" &> /dev/null & + fi + fi +fi +if [ "$USE_RDP" = true ] && [ "$NETWORK_MODE" != "none" ]; then + echo "> Starting RDP autoconnect background task..." + autoConnectRdp & +fi +if [ "$USE_LOOKING_GLASS" = true ]; then + echo "> Starting the Looking Glass client..." + #while true; do sleep 1 && echo "lg" && sudo lsof -i ":${SPICE_PORT}" | grep --quiet LISTEN &> /dev/null && sleep 5 && sudo -u "$(logname)" "${THIRDPARTY_DIR}/LookingGlass/client/build/looking-glass-client" -p "${SPICE_PORT}" 2>&1 | grep '^\[E\]'; done & + autoConnectLookingGlass & +fi +##################################################################################### +################################## Start the VM ##################################### +##################################################################################### +if [ "$VM_START_MODE" = "qemu" ]; then if [ "$DRY_RUN" = true ]; then echo "> Generating qemu-system-x86_64 command (dry-run)..." echo "" @@ -632,58 +792,46 @@ if [ "$VM_START_MODE" = "qemu" ]; then echo "" echo "" else + echo "> Starting the Virtual Machine using qemu..." sudo qemu-system-x86_64 "${QEMU_PARAMS[@]}" fi elif [ "$VM_START_MODE" = "virt-install" ]; then - if [ "$DRY_RUN" = false ]; then - if [ "$VM_INSTALL" = true ]; then - bash -c "for i in {1..30}; do sudo virsh send-key ${VM_NAME} KEY_HOME; sleep 1; done" &> /dev/null & - fi - echo "> Starting the Virtual Machine using virt-install..." - fi #VIRT_INSTALL_PARAMS+=("--debug") for param in "${QEMU_PARAMS[@]}"; do VIRT_INSTALL_PARAMS+=("--qemu-commandline='${param}'") done - #if [ "$VM_INSTALL" = true ]; then - if [ "$DRY_RUN" = true ]; then - echo "> Generating virt-install command (dry-run)..." - echo "" - printf "sudo virt-install" - for param in "${VIRT_INSTALL_PARAMS[@]}"; do - if [[ "${param}" == -* ]]; then - printf " \\\\\n ${param}" - elif [[ $param = *" "* ]]; then - printf " \"${param}\"" - else - printf " ${param}" - fi - done - echo "" - echo "" - elif [ "$GET_XML" = true ]; then - VIRT_INSTALL_PARAMS+=("--print-xml") - sudo virt-install "${VIRT_INSTALL_PARAMS[@]}" - elif [ "$VM_INSTALL" = true ]; then - sudo virt-install "${VIRT_INSTALL_PARAMS[@]}" - elif [ "$VM_INSTALL" = false ]; then - sudo virsh start "${VM_NAME}" - fi - #else - # if [ "$DRY_RUN" = true ]; then - # echo "" - # printf "sudo virt-install" - # else - # virsh start "${VM_NAME}" - # fi - #fi + if [ "$DRY_RUN" = true ]; then + echo "> Generating virt-install command (dry-run)..." + echo "" + printf "sudo virt-install" + for param in "${VIRT_INSTALL_PARAMS[@]}"; do + if [[ "${param}" == -* ]]; then + printf " \\\\\n ${param}" + elif [[ $param = *" "* ]]; then + printf " \"${param}\"" + else + printf " ${param}" + fi + done + echo "" + echo "" + elif [ "$GET_XML" = true ]; then + VIRT_INSTALL_PARAMS+=("--print-xml") + sudo virt-install "${VIRT_INSTALL_PARAMS[@]}" + elif [ "$VM_ACTION" = "install" ]; then + echo "> Starting the Virtual Machine using virt-install..." + sudo virt-install "${VIRT_INSTALL_PARAMS[@]}" + elif [ "$VM_ACTION" = "start" ]; then + echo "> Starting the Virtual Machine using virsh..." + sudo virsh start "${VM_NAME}" + fi fi -# This gets executed when the vm exits - +##################################################################################### +################ Everything below gets executed when the vm exits ################### +##################################################################################### if [ "$DGPU_PASSTHROUGH" = true ]; then - echo "> Unbinding dGPU from vfio driver..." driver unbind "${DGPU_PCI_ADDRESS}" if [ "$HOST_DGPU_DRIVER" = "nvidia" ] || [ "$HOST_DGPU_DRIVER" = "nouveau" ]; then @@ -692,7 +840,6 @@ if [ "$DGPU_PASSTHROUGH" = true ]; then fi echo "> Binding dGPU back to ${HOST_DGPU_DRIVER} driver..." driver bind "${DGPU_PCI_ADDRESS}" "${HOST_DGPU_DRIVER}" - fi if [ "$VGPU_UUID" != "" ]; then @@ -705,8 +852,11 @@ if [ "$VGPU_UUID" != "" ]; then fi -if [ "$VM_INSTALL" = true ]; then +if [ "$VM_ACTION" = "install" ]; then if sudo fdisk -lu "${DRIVE_IMG}" 2> /dev/null | grep --quiet 'Microsoft'; then + if [ "$ORIGINAL_VM_ACTION" = "auto" ]; then + sudo "${MAIN_SCRIPTS_DIR}/vm.sh" start + fi exit 0 else echo "> [Error] Seems like the installation failed..." diff --git a/scripts/utils/common/libs/helpers b/scripts/utils/common/libs/helpers index a34537c..747ad33 100644 --- a/scripts/utils/common/libs/helpers +++ b/scripts/utils/common/libs/helpers @@ -11,6 +11,7 @@ SCRIPTS_DIR="${PROJECT_DIR}/scripts" UTILS_DIR="${SCRIPTS_DIR}/utils" COMMON_UTILS_DIR="${UTILS_DIR}/common" COMMON_UTILS_LIBS_DIR="${COMMON_UTILS_DIR}/libs" + COMMON_UTILS_PLUGINS_DIR="${COMMON_UTILS_DIR}/plugins" COMMON_UTILS_SETUP_DIR="${COMMON_UTILS_DIR}/setup" COMMON_UTILS_TOOLS_DIR="${COMMON_UTILS_DIR}/tools" DISTRO_UTILS_DIR="${UTILS_DIR}/distro-specific/$("${COMMON_UTILS_TOOLS_DIR}/distro-info")" diff --git a/scripts/utils/common/plugins/display-mode-1 b/scripts/utils/common/plugins/display-mode-1 new file mode 100644 index 0000000..71a555d --- /dev/null +++ b/scripts/utils/common/plugins/display-mode-1 @@ -0,0 +1,27 @@ +##################################################################################################### +# This script has to be sourced and is not meant to be executed directly! +# It sets the display output configuration. +# How to use: +# source "$PLUGIN_DIR/display-mode-x" +##################################################################################################### + +if [ "$DMA_BUF_AVAILABLE" = true ]; then + USE_QXL=false + USE_DMA_BUF=true + USE_RDP=false + if [ "$VM_ACTION" = "start" ]; then + USE_LOOKING_GLASS=true + elif [ "$VM_ACTION" = "install" ]; then + USE_LOOKING_GLASS=false + fi +else + USE_DMA_BUF=false + USE_RDP=true + if [ "$VM_ACTION" = "install" ]; then + USE_QXL=true + USE_LOOKING_GLASS=false + elif [ "$VM_ACTION" = "start" ]; then + USE_QXL=false + USE_LOOKING_GLASS=true + fi +fi \ No newline at end of file diff --git a/scripts/utils/common/plugins/display-mode-2 b/scripts/utils/common/plugins/display-mode-2 new file mode 100644 index 0000000..bcfdfce --- /dev/null +++ b/scripts/utils/common/plugins/display-mode-2 @@ -0,0 +1,22 @@ +##################################################################################################### +# This script has to be sourced and is not meant to be executed directly! +# It sets the display output configuration. +# How to use: +# source "$PLUGIN_DIR/display-mode-x" +##################################################################################################### + +RDP=true +if [ "$DMA_BUF_AVAILABLE" = true ]; then + USE_LOOKING_GLASS=true + USE_DMA_BUF=true + USE_QXL=false +else + USE_DMA_BUF=false + if [ "$VM_ACTION" = "install" ]; then + USE_QXL=true + USE_LOOKING_GLASS=false + elif [ "$VM_ACTION" = "start" ]; then + USE_QXL=false + USE_LOOKING_GLASS=true + fi +fi \ No newline at end of file diff --git a/scripts/utils/common/plugins/display-mode-3 b/scripts/utils/common/plugins/display-mode-3 new file mode 100644 index 0000000..b318a73 --- /dev/null +++ b/scripts/utils/common/plugins/display-mode-3 @@ -0,0 +1,24 @@ +##################################################################################################### +# This script has to be sourced and is not meant to be executed directly! +# It sets the display output configuration. +# How to use: +# source "$PLUGIN_DIR/display-mode-x" +##################################################################################################### + +if [ "$VM_ACTION" = "install" ]; then + USE_LOOKING_GLASS=false + USE_RDP=false + if [ "$DMA_BUF_AVAILABLE" = true ]; then + USE_DMA_BUF=true + USE_QXL=false + elif [ "$VM_ACTION" = "start" ]; then + USE_DMA_BUF=false + USE_QXL=true + fi +else + USE_SPICE_CLIENT=true + USE_LOOKING_GLASS=true + USE_RDP=true + USE_QXL=true + USE_DMA_BUF=false +fi \ No newline at end of file diff --git a/scripts/utils/common/plugins/display-mode-4 b/scripts/utils/common/plugins/display-mode-4 new file mode 100644 index 0000000..6c4e2fe --- /dev/null +++ b/scripts/utils/common/plugins/display-mode-4 @@ -0,0 +1,22 @@ +##################################################################################################### +# This script has to be sourced and is not meant to be executed directly! +# It sets the display output configuration. +# How to use: +# source "$PLUGIN_DIR/display-mode-x" +##################################################################################################### + +USE_LOOKING_GLASS=false + +if [ "$VM_ACTION" = "start" ]; then + USE_RDP=true +elif [ "$VM_ACTION" = "install" ]; then + USE_RDP=false +fi + +if [ "$DMA_BUF_AVAILABLE" = true ]; then + USE_QXL=false + USE_DMA_BUF=true +else + USE_DMA_BUF=false + USE_RDP=true +fi \ No newline at end of file diff --git a/scripts/utils/common/plugins/display-mode-5 b/scripts/utils/common/plugins/display-mode-5 new file mode 100644 index 0000000..2a02cf0 --- /dev/null +++ b/scripts/utils/common/plugins/display-mode-5 @@ -0,0 +1,24 @@ +##################################################################################################### +# This script has to be sourced and is not meant to be executed directly! +# It sets the display output configuration. +# How to use: +# source "$PLUGIN_DIR/display-mode-x" +##################################################################################################### + +if [ "$VM_ACTION" = "install" ]; then + USE_LOOKING_GLASS=false + USE_RDP=false + if [ "$DMA_BUF_AVAILABLE" = true ]; then + USE_DMA_BUF=true + USE_QXL=false + elif [ "$VM_ACTION" = "start" ]; then + USE_DMA_BUF=false + USE_QXL=true + fi +else + USE_SPICE_CLIENT=true + USE_LOOKING_GLASS=true + USE_RDP=true + USE_QXL=false + USE_DMA_BUF=false +fi \ No newline at end of file diff --git a/vm-files/network-scripts/nat_ifdown b/vm-files/network-scripts/nat_ifdown deleted file mode 100755 index bda185b..0000000 --- a/vm-files/network-scripts/nat_ifdown +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -# qemu-ifdown script for QEMU/KVM with NAT network mode - -# set your bridge name -BRIDGE="virbr0" - -if [ -n "$1" ]; then - echo "Tearing down network bridge for $1" > /tmp/temp-nat.log - ip link set $1 down - brctl delif "$BRIDGE" $1 - ip link set "$BRIDGE" down - brctl delbr "$BRIDGE" - iptables -t nat -F - exit 0 -else - echo "Error: no interface specified" > /tmp/temp-nat.log - exit 1 -fi diff --git a/vm-files/network-scripts/nat_ifup b/vm-files/network-scripts/nat_ifup deleted file mode 100755 index 2b130a1..0000000 --- a/vm-files/network-scripts/nat_ifup +++ /dev/null @@ -1,91 +0,0 @@ -#!/bin/bash -# qemu-ifup script for QEMU/KVM with NAT netowrk mode - -# set your bridge name -BRIDGE=virbr0 - -# Network information -NETWORK=192.168.122.0 -NETMASK=255.255.255.0 -# GATEWAY for internal guests is the bridge in host -GATEWAY=192.168.122.1 -DHCPRANGE=192.168.122.2,192.168.122.254 - -# Optionally parameters to enable PXE support -TFTPROOT= -BOOTP= - -function check_bridge() -{ - if brctl show | grep "^$BRIDGE" &> /dev/null; then - return 1 - else - return 0 - fi -} - -function create_bridge() -{ - brctl addbr "$BRIDGE" - brctl stp "$BRIDGE" on - brctl setfd "$BRIDGE" 0 - ifconfig "$BRIDGE" "$GATEWAY" netmask "$NETMASK" up -} - -function enable_ip_forward() -{ - echo 1 > /proc/sys/net/ipv4/ip_forward -} - -function add_filter_rules() -{ - iptables -t nat -A POSTROUTING -s "$NETWORK"/"$NETMASK" \ - ! -d "$NETWORK"/"$NETMASK" -j MASQUERADE -} - -function start_dnsmasq() -{ - # don't run dnsmasq repeatedly - ps -ef | grep "dnsmasq" | grep -v "grep" &> /dev/null - if [ $? -eq 0 ]; then - echo "Warning:dnsmasq is already running. No need to run it again." - return 1 - fi - - dnsmasq \ - --strict-order \ - --except-interface=lo \ - --interface=$BRIDGE \ - --listen-address=$GATEWAY \ - --bind-interfaces \ - --dhcp-range=$DHCPRANGE \ - --conf-file="" \ - --pid-file=/var/run/qemu-dnsmasq-$BRIDGE.pid \ - --dhcp-leasefile=/var/run/qemu-dnsmasq-$BRIDGE.leases \ - --dhcp-no-override \ - ${TFTPROOT:+"--enable-tftp"} \ - ${TFTPROOT:+"--tftp-root=$TFTPROOT"} \ - ${BOOTP:+"--dhcp-boot=$BOOTP"} -} - -function setup_bridge_nat() -{ - check_bridge "$BRIDGE" - if [ $? -eq 0 ]; then - create_bridge - fi - enable_ip_forward - add_filter_rules "$BRIDGE" - start_dnsmasq "$BRIDGE" -} - -# need to check $1 arg before setup -if [ -n "$1" ]; then - setup_bridge_nat - ifconfig "$1" 0.0.0.0 up - brctl addif "$BRIDGE" "$1" - exit 0 -else - echo "Error: no interface specified." - exit 1 -fi diff --git a/vm-files/network-scripts/tap_ifdown b/vm-files/network-scripts/tap_ifdown deleted file mode 100755 index 612d0d8..0000000 --- a/vm-files/network-scripts/tap_ifdown +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -# tap device name -TAP=tap0 - -# Network information -NETWORK=192.168.99.0 -NETMASK=255.255.255.0 -GATEWAY=192.168.99.1 - -DNSMASQPID=$(cat "/var/run/qemu-dnsmasq-$TAP.pid") - -if [ ! "$DNSMASQPID" = "" ]; then - kill -s SIGTERM $DNSMASQPID && echo "DNSMASQ terminated" -fi - -ip link set $TAP down -ip addr flush dev $TAP - -iptables -t nat -D POSTROUTING -s $NETWORK/$NETMASK -j MASQUERADE -iptables -D INPUT -i $TAP -s $NETWORK/$NETMASK -d $NETWORK/$NETMASK -j ACCEPT -iptables -D INPUT -i $TAP -p tcp -m tcp --dport 67 -j ACCEPT -iptables -D INPUT -i $TAP -p udp -m udp --dport 67 -j ACCEPT -iptables -D INPUT -i $TAP -p tcp -m tcp --dport 53 -j ACCEPT -iptables -D INPUT -i $TAP -p udp -m udp --dport 53 -j ACCEPT -iptables -D FORWARD -i $TAP -o $TAP -j ACCEPT -iptables -D FORWARD -s $NETWORK/$NETMASK -i $TAP -j ACCEPT -iptables -D FORWARD -s $GATEWAY -i $TAP -j ACCEPT -iptables -D FORWARD -d $NETWORK/$NETMASK -o $TAP -m state --state RELATED,ESTABLISHED -j ACCEPT - -echo 0 | dd of=/proc/sys/net/ipv4/ip_forward > /dev/null && echo "ip_forward disabled" \ No newline at end of file diff --git a/vm-files/network-scripts/tap_ifup b/vm-files/network-scripts/tap_ifup deleted file mode 100755 index e89ac40..0000000 --- a/vm-files/network-scripts/tap_ifup +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash - -# Set to the name of your tap device -TAP=tap0 - -# Network information -NETWORK=192.168.99.0 -NETMASK=255.255.255.0 -GATEWAY=192.168.99.1 -DHCPRANGE=192.168.99.2,192.168.99.10 - -check_tap() { - if ip link show | grep $TAP > /dev/null; then - return - else - exit 1 - fi -} - -enable_ip_forward() { - echo 1 | dd of=/proc/sys/net/ipv4/ip_forward > /dev/null -} - -start_dnsmasq(){ - dnsmasq \ - --strict-order \ - --interface=$TAP \ - --listen-address=$GATEWAY \ - --bind-interfaces \ - --dhcp-range=$DHCPRANGE \ - --dhcp-no-override \ - --pid-file=/var/run/qemu-dnsmasq-$TAP.pid -} - -add_iptable_rules() { -iptables-restore -n <