A simple bash script to quickly enable or disable KVM (Kernel-based Virtual Machine) on Linux systems. Perfect for switching between Docker Desktop (which needs KVM) and VirtualBox (which conflicts with KVM).
I kept running into issues when trying to use VirtualBox while Docker Desktop was running. Both virtualization solutions don't play well together because they both need exclusive access to virtualization features. This script lets me quickly toggle KVM on for Docker or off for VirtualBox without manually unloading kernel modules.
-
Save the script as
kvm-toggle.sh -
Make it executable:
chmod +x kvm-toggle.sh -
Run it with either:
./kvm-toggle.sh on # Turns KVM ON (for Docker Desktop) ./kvm-toggle.sh off # Turns KVM OFF (for VirtualBox)
-
When turning OFF:
- Stops Docker Desktop processes
- Unloads KVM kernel modules (kvm_intel or kvm_amd)
-
When turning ON:
- Loads the appropriate KVM module for your CPU
- Restarts Docker Desktop in the background
-
sudo privileges (for loading/unloading kernel modules)
That's it! Simple solution to a frustrating virtualization conflict.