KVM
From ArchWiki
Dansk – English – Español – Français – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Polski – Português – Română – Slovenský – Suomi – Svenska – Türkçe – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어
KVM, Kernel-based Virtual Machine, is a hypervisor built right into the 2.6 kernel for kernels later than 2.6.20. It is similar to Xen in purpose but much simpler to get running. To start using the hypervisor, just load the appropriate kvm modules and the hypervisor is up. As with Xen's full virtualization, in order for KVM to work, you must have a processor that supports Intel's VT extensions or AMD's Pacifica extensions.
Using KVM, one can run multiple virtual machines running unmodified Linux, Windows or any other system images. (See Guest Support Status) Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc. See KVM Howto
Differences among KVM, Xen, VMware, and QEMU can be found at KVM FAQ.
Contents |
Get the packages
Arch Kernel 2.6.22 or newer now provide appropriate kvm modules. You could check if your kernel support kvm with the following command :
modprobe -l 'kvm*'
KVM also requires a modified QEMU (the qemu-kvm package in the EXTRA repository, providing the qemu-kvm) to launch and manage virtual machines. Or, you can choose to install qemu >= 0.9.0, which conflicts with the kvm packge, and now provides a qemu-kvm executable (qemu -enable-kvm) that take advantage of this technology.
pacman -S kernel26 qemu-kvm
or
pacman -S kernel26 qemu
Setup kernel modules
You could check if your computer support hardware acceleration with this command (must return something on screen) :
egrep '^flags.*(vmx|svm)' /proc/cpuinfo
Firstly, you need to add your user into the kvm group to use the /dev/kvm device.
gpasswd -a <Your_Login_Name> kvm
Secondly, modprobe kvm and kvm-intel modules if you have Intel extensions.
modprobe kvm modprobe kvm-intel
Or modprobe kvm and kvm-amd modules if you have AMD extensions.
modprobe kvm modprobe kvm-amd
If modprobing kvm succedes, but modprobing kvm-intel or kvm-amd fails (but /proc/cpuinfo claims that VT is supported), check your bios settings. Some vendors (especially laptop vendors) disable VT by default.
If you want these modules to persist, add them to rc.conf
How to use KVM
- Create a guest OS image
$ qemu-img create -f qcow2 <Image_Name> <size>
- Install the guest OS
A CD/DVD image (ISO files) can be used for the installation.$ qemu-kvm -hda <Image_Name> -m 512 -cdrom </Path/to/the/ISO/Image> -boot d -vga std
- Running the system
$ qemu-kvm -hda <Image_Name> -m 512 -vga std
Note: The default memory of KVM is 128M, if not provide "-m". Also note that recent Windows operating systems (tested with Vista and Windows 7) require the qcow2 filesystem. Other filesystems gave me a 0x80070057 error during the installation.
See QEMU for all informations, and the Using the Kernel-based Virtual Machine section.
Paravirtualized guests (virtio)
KVM offers guests to use paravirtualized block- and network devices, which leads to a better performance and less overhead. Linux has this ability with its virtio-modules since kernel 2.6.25. For Windows, a paravirtualized network driver can be obtained here: [1]
A virtio block device requires the option -drive instead of the simple -hd* plus if=virtio:
$ qemu-kvm -drive file=drive.img,if=virtio,boot=on
(ps: boot=on is absolutely required when you want to boot from it. There is no auto-detection as with -hd* ...)
Almost the same goes for the network:
$ qemu-kvm -net nic,model=virtio
Preparing an (arch)linux guest
To use virtio devices, the following modules can be loaded in the guest: virtio, virtio_pci, virtio_blk, virtio_net and virtio_ring ( for 32Bit guests, the specific "virtio" module isn't necessary). If you want to boot from a virtio-disk, the initial ramdisk must be rebuild. Add the appropriate modules in /etc/mkinitcpio.conf like this:
MODULES="virtio virtio_blk virtio_pci"
and build:
# mkinitcpio -p kernel26
virtio disks are recognized with the prefix v (like vda etc). Therefore changes have to be made in at least /etc/fstab and /boot/grub/menu.lst (when booting from a virtio disk). Off course, by using uuids, nothing has to be done.
Grub has problems detecting virtio disks. So if the bootloader is not installed or you want to reinstall it, also the file /boot/grub/device.map has to be changed (or created, when not present as in most cases) accordingly:
(hd0) /dev/vda
Now run grub with the option --device-map
# grub --device-map /boot/grub/device.map
In the interactive shell define the boot-partition; here vda1
> root (hd0,0)
And install the bootloader; here on vda
> setup (hd0)
If it was successful leave the shell
> quit
Unfortunately that manual grub-installation is required at the archlinux-installation (current arch-release-media 2010.05.) Though Aif correctly detects the virtio disks and sets up the right prefixes, the device.map must be created before setting up the bootloader.
Further information on paravirtualization with kvm: [2] section in the german qemu-book: [3]
Resizing the image
It is possible to increase the size of a qcow2 image later, at least with ext3. Convert it to a raw image, expand its size with dd, convert it back to qcow2, replace the partition with a larger one, do a fsck and resize the filesystem.
$ qemu-img convert -O raw image.qcow2 image.img $ dd if=/dev/zero of=image.img bs=1G count=0 seek=[NUMBER_OF_GB] $ qemu-img convert -O qcow2 -o cluster_size=64K image.img imageplus.qcow2 $ qemu-kvm -hda imageplus.qcow2 -m 512 -cdrom </Path/to/the/ISO/Image> -boot d -vga std $ fdisk /dev/sda [delete the partition, create new one occupying whole disk] $ e2fsck -f /dev/sda1 $ resize2fs /dev/sda1
Enabling KSM
KSM (Kernel Samepage Merging) is a feature of the Linux kernel introduced in kernel 2.6.32. KSM allows for an application to register with the kernel to have its pages merged with other processes that also register to have their pages merged. For KVM, the KSM mechanism allows for guest virtual machines to share pages with eachother. In an environment where many of the guest operating systems are similar, this can result in significant memory savings. To enable KSM, first ensure that you have a version of qemu-kvm installed which is at least 0.12.
# pacman -Qi qemu-kvm | grep Version Version : 0.12.1.2-1
Also ensure that your kernel is at least 2.6.32.
# uname -r 2.6.32-ARCH
If this is the case there should be a /sys/kernel/mm/ksm/ directory. Containing several files. You can turn KSM on or off by echoing a 1 or 0 to /sys/kernel/mm/ksm/run.
# echo 1 > /sys/kernel/mm/ksm/run
If KSM is running, and there are pages to be merged (ie, more than 1 similar VM is running) then /sys/kernel/mm/ksm/pages_shared, it should be non-zero. From the kernel documentation in Documentation/vm/ksm.txt
The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/: pages_shared - how many shared unswappable kernel pages KSM is using pages_sharing - how many more sites are sharing them i.e. how much saved pages_unshared - how many pages unique but repeatedly checked for merging pages_volatile - how many pages changing too fast to be placed in a tree full_scans - how many times all mergeable areas have been scanned A high ratio of pages_sharing to pages_shared indicates good sharing, but a high ratio of pages_unshared to pages_sharing indicates wasted effort. pages_volatile embraces several different kinds of activity, but a high proportion there would also indicate poor use of madvise MADV_MERGEABLE.
An easy way to see how well KSM is performing is to simply print the contents of all the files in that directory.
# for ii in /sys/kernel/mm/ksm/* ; do echo -n "$ii: " ; cat $ii ; done /sys/kernel/mm/ksm/full_scans: 151 /sys/kernel/mm/ksm/max_kernel_pages: 246793 /sys/kernel/mm/ksm/pages_shared: 92112 /sys/kernel/mm/ksm/pages_sharing: 131355 /sys/kernel/mm/ksm/pages_to_scan: 100 /sys/kernel/mm/ksm/pages_unshared: 123942 /sys/kernel/mm/ksm/pages_volatile: 1182 /sys/kernel/mm/ksm/run: 1 /sys/kernel/mm/ksm/sleep_millisecs: 20
Easy to Use for New User
If the qemu package has been installed, you can use an GUI tool, such as qtemu, to manage your virtual machine (you need to change "qemu" in the configure item "QEMU start command" to "qemu-kvm" / "qemu-system-x86_64") or append -enable-kvm to the additional start options.
Bridged Networking
See also QEMU#Tap_Networking_with_QEMU.
pacman -S bridge-utils
save this script in /etc/qemu-ifup
#!/bin/sh echo "Executing /etc/qemu-ifup" echo "Bringing up $1 for bridged mode..." sudo /sbin/ifconfig $1 0.0.0.0 promisc up echo "Adding $1 to br0..." sudo /usr/sbin/brctl addif br0 $1 sleep 2
chmod 755 the script
chmod 755 /etc/qemu-ifup
Then use this script to start kvm, adjust the ARGS line to your needs.
#!/bin/sh ARGS="-hda win2k.img -boot c -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=/etc/qemu-ifup -m 256 -localtime" echo "Starting QEMU with..." echo $ARGS echo "...." exec qemu $ARGS
Now the VM should get a ip from your dhcp server and you can acces it through that ip in your LAN.
Mounting the qemu image
modprobe nbd max_part=63 qemu-nbd -c /dev/nbd0 [image.img] mount /dev/nbd0p1 [/mnt/qemu]