KVM and VirtualBox supposedly cannot coexist, or rather, VirtualBox won't run while the KVM  modules are loaded in memory. KVM can work fine alongside VirtualBox, so it's a one-way problem, really. Solving it is easy too. Unfortunately, I've come across many terrible tutorials that recommend uninstalling either this or that product. And among the saner guides that go for a less stringent solution, most simply tell you rmmod this or that, but they don't tell you how to get things back running again. I will show you how to use KVM and VirtualBox side-by-side, without uninstalling any one or rebooting in between uses. You will learn how to disable and enable the products so the conflicting counterpart can run. In vivo, no reboots. Let's do this. Symptom - VirtualBox won't runYou may see this kind of error when you launch VirtualBox: It says, VirtualBox can't operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot. Wow, wow, slow down. This is a very geeky and frightening message. There's no need to recompile anything. The fix takes five seconds and it's completely non-destructive. Problem explainedVirtualBox and KVM can't work together. This means that KVM needs to be disabled if we want to run VirtualBox. The question is how we do this. The answer is: by inserting and removing kernel modules. The Linux operating system supports insertion and removal of kernel modules on the fly, without a reboot. This means you can load and unload kernel modules to and from the memory in vivo. More details in my Linux commands article and the Crash Book. To achieve what we need, we will use insmod and rmmod commands. Remove modulesLet's examine the kernel space. We will issue the lsmod command first, to see what modules are loaded into memory: As you can see, VirtualBox uses vboxdrv and vboxnetflt drivers, while KVM uses kvm and kvm_intel drivers. Note: kvm_intel is specific to Intel architecture. There's kvm_amd for AMD platforms. We need to unload KVM modules. /sbin/rmmod kvm_intel Big question: But now, KVM is gone. How can I get it back, please? Ah, very good! Let's see how we can recover. Insert modulesJust as we removed the modules, we can insert them. You will need to locate the modules on your disk and then insert them. You can also use the modprobe command, which is always useful if there are dependencies. Let's find the modules (assume sudo in commands, but can be run as root, too): updatedb There will be many results, so you can restrict the search to kvm.ko and kvm-intel.ko. Now, insert the one that matches your running kernel. You can check using uname -r. /sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm.ko Here's a screenshot. First, we check for kvm module. lsmod command shows it is not loaded. Then, we load the kvm modules and issue the lsmod command once again. Please note the screenshot commands differ slightly from the commands written in the code box above. I assumed that /sbin was in the PATH, so I used a shortened insmod command for brevity. At the same time, I also used the full module path, to show you what it looks like. If you intend to script the procedure, which you should, then full paths are a must and command substitution using backticks is recommended for greater modularity. ScriptsNow, time to automate this. You need two scripts. One to unload KVM modules and one to load them. If you really want to be strict, then you can also stop the VirtualBox service when using KVM and start it after using KVM. Example, mucking about with VirtualBox drivers: Therefore, a sample script to enable VirtualBox and disable KVM would be: #!/bin/bash And one to load KVM and stop VirtualBox would be: #!/bin/bash For AMD machines, replace kvm-intel.ko with kvm-amd.ko. Please note that sanity checks are required to make sure that you're not trying to start a started service. I have not added these; consider them as homework. Likewise, please note the processor architecture. For most people, x86 (and x86_64) will work, but you may have other architecture, like SPARC, ARM, Itanium, or others, so change accordingly. Now, place desktop shortcuts that links to these scripts and Bob's your uncle. Using uname instead of specific kernel versions ensures that your scripts will work even after kernel upgrades. |
HOWTO‎ > ‎