### Bash Script for VM Display Manager Configuration Source: https://gitlab.com/risingprismtv/single-gpu-passthrough/-/blob/master/README.md This bash script, `vfio-startup.sh`, is part of the GPU passthrough setup. It includes logic for handling different display managers and custom commands for starting graphical environments like `startx` within the VM context. It requires the user to specify their username and potentially modify the script for unsupported display managers. ```bash #!/bin/bash # Example snippet for demonstration purposes # In a real scenario, this would contain logic to detect and manage display managers # and potentially start a VM session. # Placeholder for custom startx command, user needs to replace 'username' # su -s /bin/bash -c "/usr/bin/startx" -g username username echo "VM startup script executed." ``` -------------------------------- ### Install GPU Passthrough Hooks and Services (Bash) Source: https://context7.com/risingprismtv/single-gpu-passthrough/llms.txt Automates the installation of libvirt hooks, VFIO startup/teardown scripts, and a systemd service to prevent host sleep during VM operation. It backs up existing configurations, copies new scripts to system locations, and sets executable permissions. ```bash #!/bin/bash # Run as root to install GPU passthrough hooks sudo ./install_hooks.sh # The script performs the following operations: # 1. Creates /etc/libvirt/hooks/ if it doesn't exist # 2. Backs up existing /etc/libvirt/hooks/qemu to qemu_last_backup # 3. Backs up existing vfio-startup to vfio-startup.bkp # 4. Backs up existing vfio-teardown to vfio-teardown.bkp # 5. Copies new scripts to system locations: # - systemd-no-sleep/libvirt-nosleep@.service -> /etc/systemd/system/ # - hooks/vfio-startup -> /usr/local/bin/ # - hooks/vfio-teardown -> /usr/local/bin/ # - hooks/qemu -> /etc/libvirt/hooks/ # 6. Sets executable permissions on all hook scripts # After installation, the hooks will automatically trigger when # the VM named "win10" starts or stops # Verify installation ls -l /etc/libvirt/hooks/qemu ls -l /usr/local/bin/vfio-* systemctl cat libvirt-nosleep@.service ``` -------------------------------- ### VFIO Startup Script for GPU Passthrough Preparation (Bash) Source: https://context7.com/risingprismtv/single-gpu-passthrough/llms.txt This script prepares the host system for GPU passthrough by stopping the display manager, unbinding VT consoles, and unloading native GPU drivers to load VFIO drivers. It detects display managers and GPU vendors (NVIDIA/AMD) and stores state information in temporary files. ```bash #!/bin/bash # /usr/local/bin/vfio-startup # Called automatically by libvirt hook before VM starts # The script performs the following sequence: # 1. Detects display manager from systemd # 2. Stops display manager and isolates to multi-user target # 3. Unbinds VT consoles from framebuffer devices # 4. Detects GPU vendor (NVIDIA or AMD) # 5. Unbinds EFI framebuffer # 6. Unloads vendor-specific GPU drivers # 7. Loads VFIO drivers for GPU passthrough # Example: Manual execution for testing sudo /usr/local/bin/vfio-startup # Monitor the process tail -f /var/log/libvirt/custom_hooks.log # For NVIDIA GPUs, the script unloads: # - nvidia_uvm, nvidia_drm, nvidia_modeset, nvidia, i2c_nvidia_gpu # - drm_kms_helper, drm # For AMD GPUs, the script unloads: # - amdgpu, radeon, drm_kms_helper, drm ``` -------------------------------- ### Libvirt QEMU Hook for VM Lifecycle Events (Bash) Source: https://context7.com/risingprismtv/single-gpu-passthrough/llms.txt This script acts as the main entry point for libvirt VM lifecycle events. It triggers VFIO startup and teardown scripts for a specific VM ('win10') and manages a systemd service to prevent host sleep during VM operation. Operations are logged to /var/log/libvirt/custom_hooks.log. ```bash #!/bin/bash # /etc/libvirt/hooks/qemu # This script is automatically called by libvirt with parameters: # $1 = VM name (OBJECT) # $2 = operation (OPERATION): prepare, started, stopped, release, etc. OBJECT="$1" OPERATION="$2" # Only trigger for VM named "win10" if [[ $OBJECT == "win10" ]]; then case "$OPERATION" in "prepare") # Before VM starts: prevent sleep and prepare GPU systemctl start libvirt-nosleep@"$OBJECT" 2>&1 | tee -a /var/log/libvirt/custom_hooks.log /usr/local/bin/vfio-startup 2>&1 | tee -a /var/log/libvirt/custom_hooks.log ;; "release") # After VM stops: restore GPU and allow sleep systemctl stop libvirt-nosleep@"$OBJECT" 2>&1 | tee -a /var/log/libvirt/custom_hooks.log /usr/local/bin/vfio-teardown 2>&1 | tee -a /var/log/libvirt/custom_hooks.log ;; esac fi # To use with a different VM name, change "win10" to your VM name # Check available VMs with: virsh list --all # Monitor hook execution: tail -f /var/log/libvirt/custom_hooks.log ``` -------------------------------- ### VM Configuration for GPU Passthrough Display Managers Source: https://context7.com/risingprismtv/single-gpu-passthrough/llms.txt Instructions for configuring the VM name and ensuring proper display manager handling for GPU passthrough. It covers modifying the QEMU hook script to recognize a custom VM name and lists supported display managers. ```bash # Change VM name from "win10" to another name sudo vim /etc/libvirt/hooks/qemu # Change: if [[ $OBJECT == "win10" ]]; then # To: if [[ $OBJECT == "your-vm-name" ]]; then # List available VMs virsh list --all # Example output: # Id Name State # ------------------------- # - win10 shut off # - win11 shut off # Supported display managers (auto-detected): # - GDM (GNOME Display Manager) # - SDDM (Simple Desktop Display Manager) # - LightDM # - XDM # - KDE/Plasma (special handling via display-manager.service) ``` -------------------------------- ### Bash Script for VM Teardown and Desktop Environment Restart Source: https://gitlab.com/risingprismtv/single-gpu-passthrough/-/blob/master/README.md The `vfio-teardown.sh` script is designed to clean up after a GPU passthrough session. It includes instructions on how to restart the host's window manager or desktop environment after the VM is shut down, specifically mentioning how to correctly use `startx` as a non-root user. ```bash #!/bin/bash # Example snippet for demonstration purposes # In a real scenario, this would contain logic to restore the host environment. # Placeholder for restarting the user's desktop environment # su -s /bin/bash -c "/usr/bin/startx" -g username username echo "VM teardown script executed." ``` -------------------------------- ### VFIO Teardown Script for GPU Restoration Source: https://context7.com/risingprismtv/single-gpu-passthrough/llms.txt This bash script restores the host GPU after a VM shutdown by unloading VFIO drivers, loading vendor-specific drivers, rebinding consoles, and restarting the display manager. It relies on state files created during the VFIO startup process. ```bash #!/bin/bash # /usr/local/bin/vfio-teardown # Called automatically by libvirt hook after VM stops # The script performs the following sequence: # 1. Unloads VFIO drivers (vfio_pci, vfio_iommu_type1, vfio) # 2. Checks GPU vendor from /tmp/vfio-is-nvidia or /tmp/vfio-is-amd # 3. Loads appropriate vendor drivers # 4. Restarts display manager from /tmp/vfio-store-display-manager # 5. Rebinds VT consoles from /tmp/vfio-bound-consoles # Example: Manual execution for testing (after vfio-startup) sudo /usr/local/bin/vfio-teardown # Monitor the restoration process tail -f /var/log/libvirt/custom_hooks.log # For NVIDIA GPUs, the script loads: # modprobe drm # modprobe drm_kms_helper # modprobe i2c_nvidia_gpu # modprobe nvidia # modprobe nvidia_modeset # modprobe nvidia_drm # modprobe nvidia_uvm # For AMD GPUs, the script loads: # modprobe drm # modprobe amdgpu # modprobe radeon # modprobe drm_kms_helper # Display manager restart: # - Reads display manager name from /tmp/vfio-store-display-manager # - Uses systemctl start $DISPMGR.service (systemd) # - Falls back to sv start $DISPMGR (runit) # Console rebinding: # - Reads console numbers from /tmp/vfio-bound-consoles # - Rebinds each framebuffer console that was previously unbound # - Uses sysfs: echo 1 > /sys/class/vtconsole/vtcon${N}/bind # For startx users restoration: # Add to end of vfio-teardown: # su -s /bin/bash -c "/usr/bin/startx" -g username username # Replace 'username' with your actual username ``` -------------------------------- ### Systemd Service to Prevent Sleep During VM Operation Source: https://context7.com/risingprismtv/single-gpu-passthrough/llms.txt This systemd template service, named 'libvirt-nosleep@.service', utilizes 'systemd-inhibit' to block system sleep operations while a specified virtual machine is running. It is designed to be automatically managed by libvirt QEMU hooks. ```bash # /etc/systemd/system/libvirt-nosleep@.service # Systemd template service that prevents sleep for running VMs # Service file contents: [Unit] Description=Preventing sleep while libvirt domain "%i" is running [Service] Type=simple ExecStart=/usr/bin/systemd-inhibit --what=sleep --why="Libvirt domain \"%i\" is running" --who=%U --mode=block sleep infinity # The service is started/stopped automatically by the qemu hook # Manual control examples: # Start nosleep for VM named "win10" sudo systemctl start libvirt-nosleep@win10.service # Check status systemctl status libvirt-nosleep@win10.service # View active sleep inhibitors systemd-inhibit --list # Stop nosleep (allows system to sleep again) sudo systemctl stop libvirt-nosleep@win10.service # The %i parameter is replaced with the VM name # For a VM named "win11", it would be: sudo systemctl start libvirt-nosleep@win11.service # The service uses systemd-inhibit with: # --what=sleep: Blocks sleep/suspend operations # --why="...": Provides reason in logs and inhibitor list # --who=%U: Uses the username of the service starter # --mode=block: Blocks the operation completely # sleep infinity: Keeps the inhibitor active indefinitely ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.