Advertisement
Guest User

fix_nvidia_powerd.sh v1

a guest
Oct 15th, 2024
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.47 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. # 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
  4. # Usage: sudo fix_nvidia_powerd.sh --install
  5. # or     sudo fix_nvidia_powerd.sh --uninstall
  6.  
  7. # Variables
  8. TEMP_DIR="fix_nvidia_powerd.$$"
  9. NVIDIA_POWERD_PATH="/usr/bin/nvidia-powerd"
  10. SERVICE_FILE="/etc/systemd/system/nvidia-powerd.service"
  11. DBUS_CONF_PATH="/etc/dbus-1/system.d/nvidia-dbus.conf"
  12.  
  13. # Function to check for existing files and prompt user
  14. check_for_existing_files() {
  15.     if [ -f "$NVIDIA_POWERD_PATH" ] || [ -f "$SERVICE_FILE" ] || [ -f "$DBUS_CONF_PATH" ]; then
  16.         echo "Warning: One or more files already exist:"
  17.         [ -f "$NVIDIA_POWERD_PATH" ] && echo " - $NVIDIA_POWERD_PATH"
  18.         [ -f "$SERVICE_FILE" ] && echo " - $SERVICE_FILE"
  19.         [ -f "$DBUS_CONF_PATH" ] && echo " - $DBUS_CONF_PATH"
  20.  
  21.         read -p "Do you want to overwrite these files? (y/n): " choice
  22.         case "$choice" in
  23.             y|Y) echo "Overwriting existing files..." ;;
  24.             n|N) echo "Skipping installation to avoid clobbering files." ; exit 0 ;;
  25.             *) echo "Invalid choice. Skipping installation."; exit 1 ;;
  26.         esac
  27.     fi
  28. }
  29.  
  30. # Function to get the current installed NVIDIA driver version
  31. get_nvidia_version() {
  32.     if ! command -v nvidia-smi &> /dev/null; then
  33.         echo "Error: NVIDIA driver not installed or nvidia-smi not available."
  34.         exit 1
  35.     fi
  36.     # Get the full version (e.g., 550.107.02)
  37.     NVIDIA_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader | head -n 1)
  38.     echo "Installed NVIDIA driver version: $NVIDIA_VERSION"
  39. }
  40.  
  41. # Function to build the correct .run file URL based on the installed driver version
  42. build_run_file_url() {
  43.     RUN_FILE_NAME="NVIDIA-Linux-x86_64-${NVIDIA_VERSION}.run"
  44.     RUN_FILE_URL="https://us.download.nvidia.com/XFree86/Linux-x86_64/${NVIDIA_VERSION}/${RUN_FILE_NAME}"
  45. }
  46.  
  47. # Function to download the .run file if it's not in the current directory
  48. download_run_file() {
  49.     if [ ! -f "./$RUN_FILE_NAME" ]; then
  50.         echo "Downloading NVIDIA driver .run file: $RUN_FILE_URL"
  51.         wget "$RUN_FILE_URL" -O "$RUN_FILE_NAME"
  52.         if [ $? -ne 0 ]; then
  53.             echo "Error: Failed to download $RUN_FILE_NAME from $RUN_FILE_URL"
  54.             exit 1
  55.         fi
  56.     else
  57.         echo "NVIDIA .run file found locally: $RUN_FILE_NAME"
  58.     fi
  59. }
  60.  
  61. # Install nvidia-powerd and nvidia-dbus.conf
  62. install_nvidia_powerd() {
  63.     # Check for existing files
  64.     check_for_existing_files
  65.  
  66.     # Get the installed NVIDIA version
  67.     get_nvidia_version
  68.  
  69.     # Build the download URL for the .run file
  70.     build_run_file_url
  71.  
  72.     # Download the .run file if not present
  73.     download_run_file
  74.  
  75.     # Step 1: Extract the .run file
  76.     echo "Extracting the NVIDIA .run file..."
  77.     chmod +x "$RUN_FILE_NAME"
  78.     ./"$RUN_FILE_NAME" --extract-only --target "$TEMP_DIR"
  79.  
  80.     # Step 2: Find and copy nvidia-powerd binary
  81.     echo "Looking for the nvidia-powerd binary..."
  82.     POWERD_BIN=$(find "$TEMP_DIR" -name 'nvidia-powerd' | head -n 1)
  83.  
  84.     if [ -z "$POWERD_BIN" ]; then
  85.         echo "nvidia-powerd binary not found in the .run file."
  86.         exit 1
  87.     fi
  88.  
  89.     echo "Copying nvidia-powerd to /usr/bin/..."
  90.     sudo cp "$POWERD_BIN" "$NVIDIA_POWERD_PATH"
  91.     sudo chmod +x "$NVIDIA_POWERD_PATH"
  92.  
  93.     # Step 3: Copy the nvidia-dbus.conf file
  94.     echo "Looking for nvidia-dbus.conf..."
  95.     DBUS_CONF_BIN=$(find "$TEMP_DIR" -name 'nvidia-dbus.conf' | head -n 1)
  96.  
  97.     if [ -z "$DBUS_CONF_BIN" ]; then
  98.         echo "nvidia-dbus.conf not found in the .run file."
  99.         exit 1
  100.     fi
  101.  
  102.     echo "Copying nvidia-dbus.conf to /etc/dbus-1/system.d/..."
  103.     sudo cp "$DBUS_CONF_BIN" "$DBUS_CONF_PATH"
  104.     sudo chmod 644 "$DBUS_CONF_PATH"
  105.  
  106.     # Step 4: Create the systemd service
  107.     echo "Creating systemd service file for nvidia-powerd..."
  108.     sudo bash -c "cat > $SERVICE_FILE" <<EOL
  109. [Unit]
  110. Description=NVIDIA Power Daemon
  111. Wants=multi-user.target
  112. After=multi-user.target
  113.  
  114. [Service]
  115. Type=simple
  116. ExecStart=$NVIDIA_POWERD_PATH
  117. Restart=on-failure
  118.  
  119. [Install]
  120. WantedBy=multi-user.target
  121. EOL
  122.  
  123.     # Step 5: Reload systemd, enable and start the service
  124.     echo "Reloading systemd, enabling and starting nvidia-powerd service..."
  125.     sudo systemctl daemon-reload
  126.     sudo systemctl enable nvidia-powerd
  127.     sudo systemctl start nvidia-powerd
  128.  
  129.     # Step 6: Clean up
  130.     echo "Cleaning up temporary files..."
  131.     rm -rf "$TEMP_DIR"
  132.  
  133.     # Step 7: Verify service status
  134.     echo "nvidia-powerd service status:"
  135.     sudo systemctl status nvidia-powerd --no-pager
  136.  
  137.     echo "nvidia-powerd installation completed successfully!"
  138. }
  139.  
  140. # Uninstall nvidia-powerd and nvidia-dbus.conf
  141. uninstall_nvidia_powerd() {
  142.     echo "Stopping and disabling nvidia-powerd service..."
  143.     sudo systemctl stop nvidia-powerd
  144.     sudo systemctl disable nvidia-powerd
  145.  
  146.     echo "Removing nvidia-powerd binary, nvidia-dbus.conf and service file..."
  147.     sudo rm -f "$NVIDIA_POWERD_PATH"
  148.     sudo rm -f "$SERVICE_FILE"
  149.     sudo rm -f "$DBUS_CONF_PATH"
  150.  
  151.     echo "Reloading systemd..."
  152.     sudo systemctl daemon-reload
  153.  
  154.     echo "nvidia-powerd uninstalled successfully!"
  155. }
  156.  
  157. # Main logic
  158. case "$1" in
  159.     --install)
  160.         install_nvidia_powerd
  161.         ;;
  162.     --uninstall)
  163.         uninstall_nvidia_powerd
  164.         ;;
  165.     *)
  166.         echo "Invalid option. Usage: $0 --install | --uninstall"
  167.         exit 1
  168.         ;;
  169. esac
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement