9 years ago
KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko. KVM also requires a modified QEMU although work is underway to get the required changes upstream.
Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.
It is recomended that you use a 64-bit system for your host OS because a 64-bit system can host both 32-bit and 64-bit guests, and 32-bit system can only host 32-bit guests . Also a 32-bit system limits maximum amount of RAM you assing to a guest OS to 2GB.
To check if your kernel is 64-bit execute:
$ uname -a
Output might look similar to this:
3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
x86_64 tells you are runnig 64-bit kernel. If you have i386, i486, i586 or i686 in output, then you are running a 32-bit kernel.
To check if your processor supports 64-bit instructions execute:
$ egrep -c ' lm ' /proc/cpuinfo
If 0 is printed, it means that your CPU is not 64-bit.
If 1 or higher, it is.
To run KVM, you need a processor that supports hardware virtualization. If hardware virtuaslisation is not supported, you can still run virtual machines, but it will be much slower without the KVM extension.
Intel and AMD both have developed extensions for their processors, deemed respectively Intel VT-x and AMD-V.
To see if your processor supports one of these, ecxecute:
$ egrep -c '(vmx|svm)' /proc/cpuinfo
If 0 it means that your CPU doesn't support hardware virtualization.
If 1 or more it does - but you still need to make sure that virtualization is enabled in the BIOS.
You can also execute:
If the output is:
INFO: /dev/kvm exists
KVM acceleration can be used
Hardware virtualisation is supported.
Install KVM and necessary user-space tools with apt-get command:
$ sudo apt-get install qemu-kvm libvirt-bin bridge-utils
If you wish to install GUI tool run command:
$ sudo apt-get install virt-manager
I don't recommend you to install qtemu.
$ sudo adduser username libvirt
Log out and log back in as the user to make the group membership change effective.
Run the following command.
$ virsh -c qemu:///system list
Id Name State ----------------------------------------------------
You can create two tipes of images. raw and qcow2. A hard disk image can be raw, so that it is literally byte-by-byte the same as what the guest sees and will always use the full capacity of the guest hard drive on the host. This method provides the least I/O overhead, but can waste a lot of space, as not-used space on the guest cannot be used on the host.
Alternatively, the hard disk image can be in a format such as qcow2 which only allocates space to the image file when the guest operating system actually writes to those sectors on its virtual hard disk. The image appears as the full size to the guest operating system, even though it may take up only a very small amount of space on the host system. Using this format instead of raw will likely affect performance.
QEMU provides the
qemu-img command to create hard disk images. For example, to create a 10 GB image in the raw format execute:
$ qemu-img create -f raw image_name.img 10G
To create a 10 GB image in the qcow2 format execute:
$ qemu-img create -f qcow2 image_name.qcow2 10G
Optionally, you can simply create a raw image by creating a file of the needed size using
First, make sure you have sufficient disk space to create an image file using df:
$ df -h
$ fallocate -l 10G image_name.img
$ ls -lh image_name.imgor
$ dd if=/dev/zero of=image_name.img bs=1 count=0 seek=10GVerify file size/blocks after this.
$ stat image_name.img
qemu-img executable has the
resize option, which enables easy resizing of a hard drive image. It works for both raw and qcow2. For example, to increase image space by 5 GB, run:
$ qemu-img resize disk_image +5G
To convert a qcow image, 'test.img', to a raw 'test.raw' file , run
$ qemu-img convert test.img -O raw test.raw
To convert a raw 'test.img' file to a qcow formatted 'test.qcow', run
$ qemu-img convert test.img -O qcow test.qcow
Instead of using the original image, you can create a storage image once (the 'backing' image) and have QEMU keep mutations to this image in an overlay image. This allows you to revert to a previous state of this storage image. You could revert by creating a new overlay image at the time you wish to revert, based on the original backing image.
To create an overlay image, run command:
$ qemu-img create -f qcow2 -b image_name.qcow2 image_name.ovl
After that you can run your QEMU VM as usual:
$ qemu-system-i386 -enable-kvm image_name.ovl
The backing image will then be left intact and mutations to this storage will be recorded in the overlay image file.
qemu-system-* binaries (for example
qemu-system-x86_64, depending on guest's architecture) are used to run the virtualized guest. The general usage is:
$ qemu-system-i386 options disk_image To see available options read the manual: $ man qemu Here is an example how to start linux Mint 17 without a virtual hard drive:
$ qemu-system-i386 -enable-kvm -soundhw ac97 -m 1024 -cdrom linuxmint-17-xfce-d vd-32bit.iso -vga vmware &For 64-bit version
: $ kvm -soundhw ac97 -m 2048 -cdrom linuxmint-17-xfce-dvd-64bit.iso -vmware std& With virtual hard drive attached:
$ kvm -hda myhda.qcow2 -soundhw ac97 -m 2048 -cdrom linuxmint-17-xfce-dvd-64 bit.iso -boot d -vga vmware& kvm for 64-bit systems you can use kvm wrapper sript. the script executes qemu-system-x86_64 -enable-kvm -enable-kvm enables KVM extensions -soundhw E nables audio and selected sound hardware. -m megs Set virtual RAM size to megs megabytes. Default is 128 MiB. Set virtual RAM size to megs megabytes. Examples: -m 1024 sets virtual RAM to 1 GB -m 2G sets virtual RAM to 2 GB -m 1200M sets virtual RAM to 1200 MB -cdrom Use file as CD-ROM image. -vga Selects type of VGA card to emulate. vmware works best in this case. -boot Specify boot order drives as a string of drive letters. Valid drive letters depend on the target achitecture. The x86 PC uses: a, b (floppy 1 and 2), c (first hard disk), d (first CD-ROM)
When QEMU starts you can use ctrl+alt+f to toggle fullscreen and ctrl+alt to exit mouse grab.
-device is used to add a device to your virtual machine. To see a list of all avaiable devices use:
$ qemu-system-i386 -device \?
In user mode networking traffic is NATed through the host interface to the outside network.
The rtl8139 is the default network adapter in qemu-kvm if no other device is specified. Change it to whatever you need:
$ qemu-system-i386 -netdev user,id=user.0 -device virtio-net-pci,netdev=user.0-device &
$ qemu-system-i386 -netdev user,id=user.0 -device e1000,netdev=user.0-device &
By default, the guest OS will get an IP address in the 10.0.2.0/24 address space and the host OS will be reachable at 10.0.2.2. You can change it with:
$ qemu-system-i386 -enable-kvm -soundhw ac97 -m 1024 -hda disk.img -boot c -vga std
&You can ssh into the host OS from inside the guest OS, install xnest on the host and then: #On guest OS
$ ssh -XC -c blowfish user @10.0.2.2
to start firefox from your host os use -display :1 on guest os.
firefox -display :1 &
Other application work without this option.
If you get this :
/usr/bin/X11/xauth: /home/me/.Xauthority not writable, changes will be ignored fix it with #host
$ sudo chown user:user /home/user/.Xau*
You can use scp to copy files from host and backwards.
To copy a file from host os.
$ scp -c blowfish email@example.com:/home/file.txt /home/user/Downloads
Copy a directory from host.
$ scp -c blowfish -r firstname.lastname@example.org:/path/ /target/directory
Copy files to a host os.
$ scp -c blowfish file1.txt file2.txt email@example.com:
To increase performance use -c blowfish option. Default is Triple DES and takes so much time to copy some files.
You can use your guest as a web server for your host. Set your server in a virtual machine and redirect port:
-redir tcp:host_ip::guest ip
$ qemu-system-i386 -m 1G -hda disk.img -redir tcp:5555::80 &
Access your web server content with:
$ firefox http://localhost:5555/ &
You can share files with a Windows guest.
$ qemu-system-i386 -m 1G -hda winxp.img -redir tcp:5556::445 -vga std &
$ mount -t cifs //localhost/someshare /mnt/qemu -o
Password is necessary or you'll get I/O error.
In 2021, it seems instead of install libvirt-bin I have to install libvirt-daemon-system and libvirt-clients?
Am I right?
My vmware workstation is no longer functional, but using this tutorial I've got an excellent replacement to test Mint 17.3 for updating the english and dutch manuals!!
But indeed you might want to correct the "sudo adduser username libvirtd" (include the "d")
And for the "kvm-ok" command to work I had to do "sudo apt-get install cpu-checker" (which Mint told me by itself :-)