Difference between revisions of "KVM"

From ArchWiki
Jump to navigation Jump to search
Line 126: Line 126:
  
 
  modprobe nbd max_part=63
 
  modprobe nbd max_part=63
  qemu-nbd -c /dev/nbd0 [image]
+
  qemu-nbd -c /dev/nbd0 [image.img]
  mount /dev/nbd0p1 [image_dir]
+
  mount /dev/nbd0p1 [/mnt/qemu]

Revision as of 20:01, 16 April 2010

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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.

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

  1. Create a guest OS image
    $ qemu-img create -f qcow2 <Image_Name> <size>
  2. 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
  3. 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.

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

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]