Last week I was asked whether ESXi could run the KVM hypervisor as a Virtual Machine (often referred to as Nested Virtualization). I personally have not used KVM before or run it on top of ESXi, but I have heard of many folks successfully virtualizing KVM as a Virtual Machine on top of ESXi. I figure since I have already written several articles on Nesting VMware ESXi, Microsoft Hyper-V and Xen on top of ESXi, I might as well also take a look at KVM!
Disclaimer: Nested Virtualization is not supported by VMware, please use at your own risk.
As mentioned already, I have not used KVM before and one thing I wanted to understand before trying to run it as a Virtual Machine is what the difference is between Qemu and KVM as I have heard both these terms used in-conjunction before. I found this post to be quite helpful in helping me understand the differences between Qemu, KQemu and KVM. I recommend a read if you are new to Qemu or KVM like I am.
From the article above, we now see that you can run either Qemu as a standalone system or KVM which is an accelerator that runs on top of Qemu. With this, I will now demonstrate how you can run Qemu as well as KVM as Virtual Machine on top of ESXi. In the example below, I have selected the latest Ubuntu release (14.04.1) to run both Qemu and KVM.
To be able to run either Qemu or KVM on top of ESXi, you just need to create a Virtual Machine running Virtual Hardware 10 and enable the (VHV) Hardware Assisted Virtualization feature which available in the vSphere Web Client as seen in the screenshot below:
egrep '(vmx|svm)' /proc/cpuinfo
You should see some output when running the command, else it was not properly enabled as seen in the screenshot below:
You should also see a message stating that KVM acceleration is possible:
Step 1 - Install Qemu by running the following command:
sudo apt-get -y install qemu
Step 2 - Download and extract a simple Linux Qemu image by running the following two commands :
Note: You can also find other Qemu images here.
Step 3 - Launch the Linux image by running the following command:
qemu-system-x86_64 linux-0.2.img -curses
Installing/Running KVM on Ubuntu VM
Step 1 - Install KVM by running the following command:
sudo apt-get install qemu-kvm libvirt-bin
Step 2 - Install bridge networking components by running the following command:
sudo apt-get install bridge-utils
Step 3 - Add a bridge interface by running the following command:
sudo brctl addbr br0
Step 4 - Append the following configuration to /etc/networking/interfaces:
auto br0 iface br0 inet dhcp bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0
Step 5 - Restart the networking service by running the following command:
sudo /etc/init.d/networking restart
Step 6 - Next, we will need to create a VM which is based on an XML file. Here is what my VM definition looks like:
<domain type='kvm'> <name>alice</name> <uuid>61a813ba-3c4c-11e4-b048-005056a85388</uuid> <memory>1048576</memory> <currentMemory>1048576</currentMemory> <vcpu>1</vcpu> <os> <type>hvm</type> <boot dev='cdrom'/> </os> <features> <acpi/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/kvm</emulator> <disk type="file" device="disk"> <driver name="qemu" type="raw"/> <source file="/home/lamw/alice.img"/> <target dev="vda" bus="virtio"/> <address type="pci" domain="0x0000" bus="0x00" slot="0x04" function="0x0"/> </disk> <disk type="file" device="cdrom"> <driver name="qemu" type="raw"/> <source file="/home/lamw/ubuntu-14.04.1-desktop-amd64.iso"/> <target dev="hdc" bus="ide"/> <readonly/> <address type="drive" controller="0" bus="1" target="0" unit="0"/> </disk> <controller type="ide" index="0"> <address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x1"/> </controller> <interface type='bridge'> <mac address='52:54:aa:00:f0:51'/> <source bridge='br0'/> </interface> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' autoport="yes" listen='172.30.0.207'/> </devices> </domain>
Step 7 - You will need to make some changes to the XML file such as the location of the Ubuntu ISO (which is required to boot the VM) as well as the lP Address of the VNC service for console access to VM and lastly the UUID identifier of the VM which can be generated by running 'uuid' command.
Step 8 - Before we can create our VM, we will need to create the Image file by running the following command:
qemu-img create -f qcow2 /home/lamw/alice.img 5G
Step 9 - We are now ready to initialize and boot up our VM by running the following command:
virsh --connect qemu:///system create alice.xml
Step 10 - We can get information about the VM we just created by running the following command:
virsh --connect qemu:///system dominfo alice
Step 11 - Lastly, we can connect to the VM console using a VNC client and if you modified the XML definition of the VM to listen on the public address of your Ubuntu host VM, then you should be able to see the installer of ISO bootup (in my case, it's Ubuntu Desktop Edition) as seen in the screenshot below: