Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # fix_nvidia_powerd.sh - script to install just the nvidia-powerd service from the nvidia website on Ubuntu 64bit on top of the regular nvidia-driver-* pacjage
- # Usage: sudo fix_nvidia_powerd.sh --install
- # or sudo fix_nvidia_powerd.sh --uninstall
- # Variables
- TEMP_DIR="fix_nvidia_powerd.$$"
- NVIDIA_POWERD_PATH="/usr/bin/nvidia-powerd"
- SERVICE_FILE="/etc/systemd/system/nvidia-powerd.service"
- DBUS_CONF_PATH="/etc/dbus-1/system.d/nvidia-dbus.conf"
- # Function to check for existing files and prompt user
- check_for_existing_files() {
- if [ -f "$NVIDIA_POWERD_PATH" ] || [ -f "$SERVICE_FILE" ] || [ -f "$DBUS_CONF_PATH" ]; then
- echo "Warning: One or more files already exist:"
- [ -f "$NVIDIA_POWERD_PATH" ] && echo " - $NVIDIA_POWERD_PATH"
- [ -f "$SERVICE_FILE" ] && echo " - $SERVICE_FILE"
- [ -f "$DBUS_CONF_PATH" ] && echo " - $DBUS_CONF_PATH"
- read -p "Do you want to overwrite these files? (y/n): " choice
- case "$choice" in
- y|Y) echo "Overwriting existing files..." ;;
- n|N) echo "Skipping installation to avoid clobbering files." ; exit 0 ;;
- *) echo "Invalid choice. Skipping installation."; exit 1 ;;
- esac
- fi
- }
- # Function to get the current installed NVIDIA driver version
- get_nvidia_version() {
- if ! command -v nvidia-smi &> /dev/null; then
- echo "Error: NVIDIA driver not installed or nvidia-smi not available."
- exit 1
- fi
- # Get the full version (e.g., 550.107.02)
- NVIDIA_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -n 1)
- echo "Installed NVIDIA driver version: $NVIDIA_VERSION"
- }
- # Function to build the correct .run file URL based on the installed driver version
- build_run_file_url() {
- RUN_FILE_NAME="NVIDIA-Linux-x86_64-${NVIDIA_VERSION}.run"
- RUN_FILE_URL="https://us.download.nvidia.com/XFree86/Linux-x86_64/${NVIDIA_VERSION}/${RUN_FILE_NAME}"
- }
- # Function to download the .run file if it's not in the current directory
- download_run_file() {
- if [ ! -f "./$RUN_FILE_NAME" ]; then
- echo "Downloading NVIDIA driver .run file: $RUN_FILE_URL"
- wget "$RUN_FILE_URL" -O "$RUN_FILE_NAME"
- if [ $? -ne 0 ]; then
- echo "Error: Failed to download $RUN_FILE_NAME from $RUN_FILE_URL"
- exit 1
- fi
- else
- echo "NVIDIA .run file found locally: $RUN_FILE_NAME"
- fi
- }
- # Install nvidia-powerd and nvidia-dbus.conf
- install_nvidia_powerd() {
- # Check for existing files
- check_for_existing_files
- # Get the installed NVIDIA version
- get_nvidia_version
- # Build the download URL for the .run file
- build_run_file_url
- # Download the .run file if not present
- download_run_file
- # Step 1: Extract the .run file
- echo "Extracting the NVIDIA .run file..."
- chmod +x "$RUN_FILE_NAME"
- ./"$RUN_FILE_NAME" --extract-only --target "$TEMP_DIR"
- # Step 2: Find and copy nvidia-powerd binary
- echo "Looking for the nvidia-powerd binary..."
- POWERD_BIN=$(find "$TEMP_DIR" -name 'nvidia-powerd' | head -n 1)
- if [ -z "$POWERD_BIN" ]; then
- echo "nvidia-powerd binary not found in the .run file."
- exit 1
- fi
- echo "Copying nvidia-powerd to /usr/bin/..."
- sudo cp "$POWERD_BIN" "$NVIDIA_POWERD_PATH"
- sudo chmod +x "$NVIDIA_POWERD_PATH"
- # Step 3: Copy the nvidia-dbus.conf file
- echo "Looking for nvidia-dbus.conf..."
- DBUS_CONF_BIN=$(find "$TEMP_DIR" -name 'nvidia-dbus.conf' | head -n 1)
- if [ -z "$DBUS_CONF_BIN" ]; then
- echo "nvidia-dbus.conf not found in the .run file."
- exit 1
- fi
- echo "Copying nvidia-dbus.conf to /etc/dbus-1/system.d/..."
- sudo cp "$DBUS_CONF_BIN" "$DBUS_CONF_PATH"
- sudo chmod 644 "$DBUS_CONF_PATH"
- # Step 4: Create the systemd service
- echo "Creating systemd service file for nvidia-powerd..."
- sudo bash -c "cat > $SERVICE_FILE" <<EOL
- [Unit]
- Description=NVIDIA Power Daemon
- Wants=multi-user.target
- After=multi-user.target
- [Service]
- Type=simple
- ExecStart=$NVIDIA_POWERD_PATH
- Restart=on-failure
- [Install]
- WantedBy=multi-user.target
- EOL
- # Step 5: Reload systemd, enable and start the service
- echo "Reloading systemd, enabling and starting nvidia-powerd service..."
- sudo systemctl daemon-reload
- sudo systemctl enable nvidia-powerd
- sudo systemctl start nvidia-powerd
- # Step 6: Clean up
- echo "Cleaning up temporary files..."
- rm -rf "$TEMP_DIR"
- # Step 7: Verify service status
- echo "nvidia-powerd service status:"
- sudo systemctl status nvidia-powerd --no-pager
- echo "nvidia-powerd installation completed successfully!"
- }
- # Uninstall nvidia-powerd and nvidia-dbus.conf
- uninstall_nvidia_powerd() {
- echo "Stopping and disabling nvidia-powerd service..."
- sudo systemctl stop nvidia-powerd
- sudo systemctl disable nvidia-powerd
- echo "Removing nvidia-powerd binary, nvidia-dbus.conf and service file..."
- sudo rm -f "$NVIDIA_POWERD_PATH"
- sudo rm -f "$SERVICE_FILE"
- sudo rm -f "$DBUS_CONF_PATH"
- echo "Reloading systemd..."
- sudo systemctl daemon-reload
- echo "nvidia-powerd uninstalled successfully!"
- }
- # Main logic
- case "$1" in
- --install)
- install_nvidia_powerd
- ;;
- --uninstall)
- uninstall_nvidia_powerd
- ;;
- *)
- echo "Invalid option. Usage: $0 --install | --uninstall"
- exit 1
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement