Last active
May 10, 2025 22:00
-
-
Save HoKim98/74d7e7103baf83341a0e2ee13f7c0c06 to your computer and use it in GitHub Desktop.
Switch to Windows 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# Copyright (c) 2024 Ho Kim ([email protected]). All rights reserved. | |
# Use of this source code is governed by a GPL-3-style license that can be | |
# found in the LICENSE file. | |
# Prehibit errors | |
set -e -o pipefail | |
# Verbose | |
set -x | |
# Disable drivers | |
mkdir -p /run/modprobe.d | |
cat <<EOF >/run/modprobe.d/blacklist-audio.conf | |
install snd_hda_intel /bin/true | |
EOF | |
cat <<EOF >/run/modprobe.d/blacklist-nvidia.conf | |
install nvidia_drm /bin/true | |
install nvidia_modeset /bin/true | |
install nvidia_uvm /bin/true | |
install nvidia /bin/true | |
EOF | |
depmod -a | |
# Disable Audio devices | |
lsof -t /dev/snd/* | xargs kill 2>/dev/null || true | |
# Disable NVIDIA GPU devices | |
if nvidia-smi >/dev/null 2>/dev/null; then | |
PCI_IDS=$( | |
nvidia-smi --query-gpu=gpu_bus_id --format=csv | | |
grep -Po '[0-9]{4}:[0-9]{2}:[0-9]{2}\.[0-9]' | |
) | |
fi | |
if [ ! -z ${PCI_IDS+x} ]; then | |
for pci_id in ${PCI_IDS}; do | |
nvidia-smi -i "${pci_id}" -c EXCLUSIVE_PROCESS | |
nvidia-smi -i "${pci_id}" -pm 0 | |
nvidia-smi drain -m 0 -p "${pci_id}" | |
nvidia-smi drain -m 1 -p "${pci_id}" | |
done | |
lsof -t /dev/nvidia[0-9-]* | xargs kill 2>/dev/null || true | |
fi | |
systemctl stop getty@tty1 | |
killall Xorg || true | |
# Unload NVIDIA GPU drivers | |
sleep 1 | |
if [ ! -z ${PCI_IDS+x} ]; then | |
rmmod nvidia_drm || true | |
rmmod nvidia_modeset || true | |
rmmod nvidia_uvm || true | |
rmmod nvidia || true | |
fi | |
# Unload Audio drivers | |
rmmod snd_hda_intel || true | |
# Create a VM | |
VM_NAME='win10' | |
if (virsh dominfo "${VM_NAME}" 2>/dev/null || true) | grep -Pq '^State:.*shut off$'; then | |
virsh start "${VM_NAME}" | |
sleep 5 | |
fi | |
while (virsh dominfo "${VM_NAME}" 2>/dev/null || true) | grep -Pq '^State:.*running$'; do | |
sleep 1 | |
done | |
virsh shutdown "${VM_NAME}" 2>/dev/null || true | |
# Enable drivers | |
rm -f /run/modprobe.d/blacklist-audio.conf | |
rm -f /run/modprobe.d/blacklist-nvidia.conf | |
depmod -a | |
# Reload NVIDIA GPU drivers | |
modprobe nvidia | |
# Reload Audio drivers | |
modprobe snd_hda_intel | |
# Enable NVIDIA GPU devices | |
if [ ! -z ${PCI_IDS+x} ]; then | |
for pci_id in ${PCI_IDS}; do | |
nvidia-smi drain -m 0 -p "${pci_id}" | |
nvidia-smi -i "${pci_id}" -pm 1 | |
nvidia-smi -i "${pci_id}" -c DEFAULT | |
done | |
fi | |
# Restart tty session | |
systemctl restart getty@tty1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment