KVM virtualisation is a Kernel based virtualisation. It actually stands for Kernel-based Virtual Machine. It means that each of your virtual machine will have it´s own virtual hardware, such as network card, graphic card, memory and hard drive.
To setup KVM virtualisation it require a multicore cpu. And virtualisation needs to be activated in the BIOS. Look for virtualisation, Intel VT-x (vmx) or AMD-v settings, and enable it. You might be able to install KVM without virtualisation enabled in the BIOS. But the virtual machines will be extremely slow.
Here is how to install KVM on a minimal Debian installation.
First we assume a ssh server is installed, and sudo.
Step 1: First we install the essential packages for KVM. apt-get install kvm libvirt-bin virtinst bridge-utils
If you want to be able to controll the virtual machines from your own users instead of root. Add your user to the libvirt group. adduser john libvirt
Step 2: To be able to give network access to your virtual computers you will need to bridge your network card. To do this we will change the network interface file. (Change the ip´s to match your network) nano /etc/network/interfaces
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5).
# The loopback network interface auto lo iface lo inet loopback
# The primary network interface iface eth0 inet manual
I prefer using a gui when controlling my virtual machines. Learning how to control them in the most basic way from the command line or terminal is easy. It is very self explanatory once you know about the virsh command. I have made some examples below on how to use these commands.
Here is the list of basic commands to start, stop and reboot your virtual machines.
List your running guests
virsh list [sourcecode language=”text”] Id Name State —————————————————- 2 apt-cacher.home running [/sourcecode]
List all guests:
virsh list –all [sourcecode language=”text”] Id Name State —————————————————- 2 apt-cacher.home running – db1.home shut off – db2.home shut off – devlab.home shut off – minecraft.home shut off – vault.home shut off – web.home shut off [/sourcecode]
To start a vm
we can only use their vm name to start them. virsh start db1.home
To reboot a vm
We can use name or id to reboot the vm. virsh reboot 2 virsh reboot apt-cacher.home
The Virtual Machine Manager is a small powerful tool when it comes to handling your kvm guests. To install the Virtual Machine Manager we do: apt-get install virt-manager
It is now ready to run. However to connect to your KVM hosts you need also to install a ssh askpass software. apt-get install ssh-askpass
First time you connect to your host you need to confirm the fingerprint. If you have accessed the host via ssh, and done re-installs, chances are the fingerprint won’t match. One simple solution to this is to delete the known_hosts file, or modify it if you are comfortable doing that. Normally on a Debian based system it’s located (example assume user is jondoe): /home/jondoe/.ssh/known_hosts
The virtual machine should now be ready to run. If you haven’t allready, installing screen would be handy. So you can free up your console if you like. apt-get install screen
To run the virtual machine from our example. screen vboxheadless –startvm “MySQL” You can now press ctrl+a and then ctrl+d to deattach from the process and have access to console again.
To stop the virtual machine you can do this. vboxmanage controlvm “MySQL” poweroff
To unmount the installation iso, you can do that like this. VBoxManage storageattach “MySQL” –storagectl “IDE Controller” –port 1 –device 0 –type dvddrive –medium none
To have your virtual machine to start up automaticly on boot. Add this to your boot script: vboxmanage startvm “MySQL” –type headless
If you need for some reasons to remove your virtual machine. You need to start with unregistering the hard drive, and then delete it. Then you can unregister the virtual machine and remove it. Here is how. To list the hard drives: vboxmanage list hdds Remove the hard drive: vboxmanage modifyvm “MySQL” –hda none Find the UUID for the hard drive: vboxmanage showvminfo “MySQL” | grep vdi* Unregister the hard drive: vboxmanage unregisterimage disk f3620364-399d-413b-b08e-d8b2f13392f0 Delete the hard drive file (of course use your own location): rm -rf /home/MySQL.vdi Unregister the virtual machine: vboxmanage unregister vm MySQL
Your virtual machine and virtual hard drive is now gone.
VirtualBox is now installed. We want to add the extention pack, so we can install guest systems via remote desktop. Download current version of the extionpack: wget http://download.virtualbox.org/virtualbox/4.2.18/Oracle_VM_VirtualBox_Extension_Pack-4.2.18-88780.vbox-extpack
If you need to run on another use than root: adduser hostman(or any user you want) vboxusers
Your system is now ready to run. Ready to create and start guest systems.