Skip to content

Instantly share code, notes, and snippets.

@Aldo-f
Last active August 21, 2025 15:30
Show Gist options
  • Select an option

  • Save Aldo-f/e845db2b36569387620e05363f481d2c to your computer and use it in GitHub Desktop.

Select an option

Save Aldo-f/e845db2b36569387620e05363f481d2c to your computer and use it in GitHub Desktop.
Toggle kvm - linux
#!/bin/bash
KVM_BLACKLIST_FILE="/etc/modprobe.d/blacklist-kvm.conf"
KVM_MODULES=("kvm_intel" "kvm_amd" "kvm")
# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Please use 'sudo ./kvm_toggle.sh'."
exit 1
fi
# Function to enable KVM (remove blacklist and load modules)
enable_kvm() {
echo "βœ… Enabling KVM..."
# 1. Remove blacklist entries from the configuration file
for module in "${KVM_MODULES[@]}"; do
sed -i "/blacklist $module/d" "$KVM_BLACKLIST_FILE"
done
echo " - Modules de-blacklisted from configuration."
# 2. Attempt to load the modules directly
for module in "${KVM_MODULES[@]}"; do
if ! lsmod | grep -q "$module"; then
echo " - Loading module '$module'..."
modprobe "$module"
if [ $? -eq 0 ]; then
echo " OK"
else
echo " Error loading module '$module'. A reboot might still be necessary."
fi
else
echo " - Module '$module' is already loaded, no action needed."
fi
done
# 3. Update initramfs for the next reboot
update_initramfs
echo "KVM is now activated for immediate use."
echo "Note: A system reboot is still recommended for all services to work correctly."
}
# Function to disable KVM (add blacklist and unload modules)
disable_kvm() {
echo "🚫 Disabling KVM..."
# 1. Add blacklist entries to the configuration file
touch "$KVM_BLACKLIST_FILE"
for module in "${KVM_MODULES[@]}"; do
if ! grep -q "^blacklist $module" "$KVM_BLACKLIST_FILE"; then
echo " - Blacklisting module '$module'."
echo "blacklist $module" >> "$KVM_BLACKLIST_FILE"
fi
done
# 2. Attempt to unload the modules directly
for module in "${KVM_MODULES[@]}"; do
if lsmod | grep -q "$module"; then
echo " - Unloading module '$module'..."
modprobe -r "$module"
if [ $? -eq 0 ]; then
echo " OK"
else
echo " Error unloading module '$module'. Services may still be using it. A reboot is required."
fi
else
echo " - Module '$module' is not loaded, no action needed."
fi
done
# 3. Update initramfs for the next reboot
update_initramfs
echo "KVM is now deactivated."
echo "Note: A system reboot is still highly recommended to prevent KVM from loading on boot."
}
# Function to update initramfs
update_initramfs() {
echo "βš™οΈ Updating initramfs..."
update-initramfs -u > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo " Initramfs updated successfully."
else
echo " Error updating initramfs."
fi
}
# Main script logic
case "$1" in
enable)
enable_kvm
;;
disable)
disable_kvm
;;
status)
echo "Checking KVM status..."
if grep -q "^blacklist kvm" "$KVM_BLACKLIST_FILE" || \
grep -q "^blacklist kvm_intel" "$KVM_BLACKLIST_FILE" || \
grep -q "^blacklist kvm_amd" "$KVM_BLACKLIST_FILE"; then
echo "KVM is currently: 🚫 **DISABLED** (blacklisted in $KVM_BLACKLIST_FILE)"
echo "Use 'sudo kvm_toggle.sh enable' to enable KVM."
else
if modprobe -n kvm &> /dev/null; then
if lsmod | grep -q "kvm" || lsmod | grep -q "kvm_intel" || lsmod | grep -q "kvm_amd"; then
echo "KVM is currently: βœ… **ENABLED** (modules loaded)"
else
echo "KVM is currently: βœ… **ENABLED** (modules can be loaded, but are not currently active)"
fi
else
echo "KVM is currently: ❓ **UNKNOWN/POTENTIALLY DISABLED** (KVM modules not loadable or other issue)"
fi
echo "Use 'sudo kvm_toggle.sh disable' to disable KVM."
fi
;;
*)
echo "Usage: $0 [enable|disable|status]"
echo " enable: Enables KVM (removes blacklist and loads modules)."
echo " disable: Disables KVM (adds blacklist and unloads modules)."
echo " status: Shows the current KVM status."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment