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 run
You 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 explained
VirtualBox 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 modules
Let'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
/sbin/rmmod kvm
/sbin/rmmod kvm
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 modules
Just 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
locate kvm
There will be many results, so you can restrict the search to kvm.ko and kvm-intel.ko.
locate kvm
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
/sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm-intel.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.
/sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm-intel.ko
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.
Scripts
Now, 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
/sbin/rmmod kvm_intel
/sbin/rmmod kvm
/etc/init.d/vboxdrv start
And one to load KVM and stop VirtualBox would be:
/sbin/rmmod kvm_intel
/sbin/rmmod kvm
/etc/init.d/vboxdrv start
#!/bin/bash
/etc/init.d/vboxdrv stop
/sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm.ko
/sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm-intel.ko
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.
/etc/init.d/vboxdrv stop
/sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm.ko
/sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm-intel.ko
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.
Conclusion
As you can see, the reality is not as harsh as you may have expected. This tutorial reveals the simple ways of getting VirtualBox and KVM to cooperate, without uninstalling one or the other or constantly rebooting. The Linux architecture allows live insertion of modules, so the only thing you really must reboot for is the kernel upgrade.This tutorial teaches you several things, including a clever way of combining comfort with usability, how to insert and remove kernel modules, how to script your work and how to achieve the desired goal of having KVM and VirtualBox side by side.
I hope you liked this one.
Cheers.
No comments:
Post a Comment