Last active
June 26, 2025 10:51
-
-
Save m1st0/e6348a7ef50d5e17d41485fdac9292f3 to your computer and use it in GitHub Desktop.
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/env zsh | |
# Simplified Nvidia card management on Ubuntu 25.04 | |
# provided drivers are installed correctly from other script. | |
# Author: Maulik Mistry | |
# Please share support: https://www.paypal.com/paypalme/m1st0 | |
# License: BSD License 2.0 | |
# Copyright (c) 2023–2025, Maulik Mistry | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * Neither the name of the <organization> nor the | |
# names of its contributors may be used to endorse or promote products | |
# derived from this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# PCI Device ID of NVIDIA GPU (change if different) | |
GPU_DEV="0000:01:00.0" | |
# Get the directory of the current script | |
SCRIPT_DIR="$(dirname "$0")" | |
remove_modules_zsh() { | |
bash -c "source \"$SCRIPT_DIR/nvidia_functions.sh\"; remove_modules" | |
} | |
# If no command is provided, power off the GPU | |
if [[ -z "$1" ]]; then | |
echo "Turning off NVIDIA GPU..." | |
# Power off the GPU | |
echo auto | sudo tee /sys/bus/pci/devices/$GPU_DEV/power/control | |
sudo cat /sys/bus/pci/devices/$GPU_DEV/power/control | |
# Optionally restart the NVIDIA services to ensure proper state | |
#sudo systemctl restart nvidia-suspend.service nvidia-resume.service nvidia-powerd.service | |
#sudo modprobe acpi_call | |
#sudo tee /proc/acpi/call <<<'\_SB_.PCI0.PEG0.PEGP._OFF' | |
# Run the Bash script in a subshell and call the function | |
remove_modules_zsh | |
echo "NVIDIA GPU is now powered off." | |
# If a command is provided, power on the GPU and run the command | |
else | |
echo "Turning on NVIDIA GPU..." | |
# Ensure NVIDIA modules are loaded. Already in "/etc/modules" for now. | |
sudo modprobe nvidia nvidia_modeset nvidia_uvm nvidia_drm | |
# Power on the GPU | |
echo on | sudo tee /sys/bus/pci/devices/$GPU_DEV/power/control | |
sudo cat /sys/bus/pci/devices/$GPU_DEV/power/control | |
# Restart NVIDIA services (to ensure it's ready) | |
#sudo systemctl restart nvidia-suspend.service nvidia-resume.service nvidia-powerd.service | |
# Run the specified command on NVIDIA GPU | |
echo "Running on NVIDIA GPU: $1" | |
GBM_BACKEND=nvidia-drm __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/nvidia_icd.json "$@" | |
# Power off the GPU once done | |
remove_modules_zsh | |
echo auto | sudo tee /sys/bus/pci/devices/$GPU_DEV/power/control | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment