2023-02-01 14:30:02 -08:00
{{- if eq .host.distro.family "linux" -}}
2023-01-31 21:18:59 -08:00
#!/usr/bin/env bash
2023-04-11 20:26:25 -07:00
# @file VMWare Configuration
# @brief Patches VMWare to leverage kernel optimizations, support macOS, and work harmoniously with Secure Boot. It also enables optional services such as the USB service.
# @description
# This script performs various VMWare optimizations that allow VMWare to work optimally with all features enabled.
2023-01-31 21:18:59 -08:00
{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}
2023-02-01 14:30:02 -08:00
### Run logic if VMware is installed
2023-01-31 21:18:59 -08:00
if command -v vmware > /dev/null; then
2023-02-01 14:30:02 -08:00
### Build kernel modules if they are not present
2023-02-01 18:49:45 -08:00
if [ ! -f "/lib/modules/$(uname -r)/misc/vmmon.ko" ] || [ ! -f "/lib/modules/$(uname -r)/misc/vmnet.ko" ]; then
2023-02-04 20:01:40 -08:00
### Build VMWare host modules
2023-02-01 14:30:02 -08:00
logg info 'Building VMware host modules'
2023-01-31 21:18:59 -08:00
if sudo vmware-modconfig --console --install-all; then
2023-02-01 18:49:45 -08:00
logg success 'Built VMWare host modules successfully with `sudo vmware-modconfig --console --install-all`'
2023-01-31 21:18:59 -08:00
else
2023-02-01 14:30:02 -08:00
logg info 'Acquiring VMware version from CLI'
2023-02-01 18:49:45 -08:00
VMW_VERSION="$(vmware --version | cut -f 3 -d' ')"
2023-01-31 21:18:59 -08:00
mkdir -p /tmp/vmw_patch
cd /tmp/vmw_patch
2023-02-01 14:30:02 -08:00
logg info 'Downloading VMware host module patches'
2023-02-01 18:49:45 -08:00
curl -sSL "https://github.com/mkubecek/vmware-host-modules/archive/workstation- $ VMW_VERSION . tar . gz " -o /tmp/vmw_patch/workstation.tar.gz
2023-01-31 21:18:59 -08:00
tar -xzf /tmp/vmw_patch/workstation.tar.gz
2023-01-31 23:04:55 -08:00
cd vmware*
2023-01-31 22:56:51 -08:00
logg info 'Running `sudo make` and `sudo make install`'
2023-01-31 21:18:59 -08:00
sudo make
sudo make install
2023-02-01 14:30:02 -08:00
logg success 'Successfully configured VMware host module patches'
2023-01-31 21:18:59 -08:00
fi
2023-02-01 14:30:02 -08:00
### Sign VMware host modules if Secure Boot is enabled
if [ -f /sys/firmware/efi ]; then
logg info 'Signing host modules'
mkdir -p /tmp/vmware
cd /tmp/vmware
openssl req -new -x509 -newkey rsa:2048 -keyout MOK.priv -outform DER -out MOK.der -nodes -days 36500 -subj "/CN=VMware/"
2023-02-01 18:49:45 -08:00
"/usr/src/linux-headers-$(uname -r)/scripts/sign-file" sha256 ./MOK.priv ./MOK.der "$(modinfo -n vmmon)"
"/usr/src/linux-headers-$(uname -r)/scripts/sign-file" sha256 ./MOK.priv ./MOK.der "$(modinfo -n vmnet)"
2023-02-01 14:30:02 -08:00
echo '' | mokutil --import MOK.der
logg success 'Successfully signed VMware host modules. Reboot the host before powering on VMs'
fi
### Patch VMware with Unlocker
2023-01-31 22:56:51 -08:00
if [ ! -f /usr/lib/vmware/isoimages/darwin.iso ]; then
2023-02-01 14:30:02 -08:00
logg info 'Acquiring VMware Unlocker latest release version'
2023-02-01 18:49:45 -08:00
UNLOCKER_URL="$(curl -sSL 'https://api.github.com/repos/DrDonk/unlocker/releases/latest' | jq -r '.assets[0].browser_download_url')"
2023-01-31 22:56:51 -08:00
mkdir -p /tmp/vmware-unlocker
cd /tmp/vmware-unlocker
logg info 'Downloading unlocker.zip'
curl -sSL " $ UNLOCKER_URL " -o unlocker.zip
unzip unlocker.zip
cd linux
logg info 'Running the unlocker'
echo "y" | sudo ./unlock
2023-02-01 14:30:02 -08:00
logg success 'Successfully unlocked VMware for macOS compatibility'
2023-01-31 22:56:51 -08:00
else
2023-02-01 14:30:02 -08:00
logg info '/usr/lib/vmware/isoimages/darwin.iso is already present on the system so VMware macOS unlocking will not be performed'
2023-01-31 22:56:51 -08:00
fi
2023-02-04 20:01:40 -08:00
2023-02-15 19:14:33 -08:00
if [[ ! "$(test -d /proc && grep Microsoft /proc/version > /dev/null)" ]]; then
2023-02-04 20:18:07 -08:00
### Start / enable VMWare service
logg info 'Ensuring `vmware.service` is enabled and running'
sudo systemctl enable vmware.service
sudo systemctl restart vmware.service
### Start / enable VMWare Workstation Server service
logg info 'Ensuring `vmware-workstation-server.service` is enabled and running'
sudo systemctl enable vmware-workstation-server.service
sudo systemctl restart vmware-workstation-server.service
### Start / enable VMWare USB Arbitrator service
if command -v vmware-usbarbitrator.service > /dev/null; then
logg info 'Ensuring `vmware-usbarbitrator.service` is enabled and running'
sudo systemctl enable vmware-usbarbitrator.service
sudo systemctl restart vmware-usbarbitrator.service
else
logg warn '`vmware-usbarbitrator` does not exist in the PATH'
fi
2023-02-04 20:01:40 -08:00
fi
2023-02-01 14:30:02 -08:00
else
logg info 'VMware host modules are present'
fi
2023-01-31 21:18:59 -08:00
else
2023-02-01 14:30:02 -08:00
logg warn 'VMware Workstation is not installed so the VMware Unlocker will not be installed'
2023-01-31 21:18:59 -08:00
fi
{{ end -}}