个人工具

“UbuntuHelp:KVM”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第2行: 第2行:
 
{{Languages|UbuntuHelp:KVM}}
 
{{Languages|UbuntuHelp:KVM}}
 
#title The Kernel Virtual Machine
 
#title The Kernel Virtual Machine
The Kernel Virtual Machine is part of Ubuntu 7.04 and 7.10. It's a fast and simple way to run entire operating systems, including Windows, inside Linux.
+
== Introduction ==
/!\ For 8.04 implementation, which is a bit nicer, please have a look at https://wiki.ubuntu.com/KvmVirtManagerEtc
+
=== Preliminary notes ===
== Running Guest Systems on Ubuntu 7.04 Feisty Fawn ==
+
* '''This is still work in progress! Feel free to jump in and extend this documentation! '''
=== Setting up KVM on Ubuntu 7.04 Feisty Fawn ===
+
* '''The previous version of this page (which was documenting KVM for Feisty) has been moved to [[UbuntuHelp:KVMFeisty|KVMFeisty]]'''
* Install the ''''kvm'''' and ''''qemu'''' packages. See [[UbuntuHelp:InstallingSoftware|InstallingSoftware]].
+
=== Basic info ===
* Install the ''''uml-utilities'''' and ''''bridge-utils'''' packages if you want to use tap/tun and bridged networking options.
+
Ubuntu uses [http://kvm.qumranet.com/ kvm] as the backend virtualisation technology. To manage VMs we use [http://libvirt.org/ libvirt] as the basic toolbox and [http://virt-manager.et.redhat.com/ virt-manager] as the graphical frontend for managing your vm's.
* Manually load the driver:
+
== How to get started ==
* For AMD processors
+
=== How to check if your CPU supports hardware virtualisation ===
 +
To run KVM, you need a processor that supports virtualisation; Intel and AMD both have developed extensions for their processors, respectively INTEL-VT and AMD-V. To see if your processor supports one of these, you can run the following command:
 
<pre><nowiki>
 
<pre><nowiki>
  sudo modprobe kvm-amd
+
egrep '(vmx|svm)' /proc/cpuinfo
 
</nowiki></pre>
 
</nowiki></pre>
* For Intel processors
+
If nothing is printed, it means that your CPU doesn't support hardware virtualisation. Otherwise, it does - but you still need to make sure that virtualisation is enabled in the BIOS.
 +
=== Installation of KVM ===
 +
For the following setup, we will assume that you are deploying KVM on a server, and therefore do not have any X server on the machine.
 +
You need to install a few packages first:
 
<pre><nowiki>
 
<pre><nowiki>
  sudo modprobe kvm-intel
+
$ sudo apt-get install kvm libvirt-bin ubuntu-vm-builder
 
</nowiki></pre>
 
</nowiki></pre>
Especially for Intel based machines, you might need to update BIOS and enable virtualization in BIOS before you can process. Otherwise modprobe kvm-intel tell that "Operation is not supported". In the BIOS, look for VT or Vanderpool Technology. For more information, refer to this ubuntuforums.org thread:
+
* libvirt-bin provides libvirtd which you need to administer qemu and kvm instances using libvirt
http://ubuntuforums.org/showthread.php?t=350691
+
* kvm is the backend
* Make /dev/kvm accessible to the user (needed in some cases):
+
* ubuntu-vm-builder powerful command line tool for building virtual machines
 +
Then, add yourself to the ''libvirtd'' and ''kvm'' groups:
 
<pre><nowiki>
 
<pre><nowiki>
sudo adduser $USER kvm
+
$ sudo adduser `id -un` libvirtd
# logout and back in
+
$ sudo adduser `id -un` kvm
 
</nowiki></pre>
 
</nowiki></pre>
=== Installing a Windows XP Guest ===
+
This will give you access to the system-wide libvirtd instance. This is preferable for you because it gives you access to the advanced networking options rather than simply the "userspace networking" option as you may know it from QEmu.
* Create a file for the virtual disk drive. Using the '-f qcow' option as shown below saves space, by not using the space until the guest does (till the maximum size of the disk is read).  
+
Note: You need to log out and log back in for the new group membership to take effect.
 +
Note: The `id -un` command will return the current username, for example if your username is ''joe'' you will be effectively be running ''sudo adduser joe libvirtd''.
 +
You can test if your install has been successful with the following command:
 
<pre><nowiki>
 
<pre><nowiki>
qemu-img create windows.img -f qcow 6G
+
$ virsh -c qemu:///system list
 +
Id Name                State
 +
----------------------------------
 +
 
 +
$
 
</nowiki></pre>
 
</nowiki></pre>
* Start KVM and install Windows
+
If on the other hand you get something like this:
Insert the Windows install CD and run:
+
 
<pre><nowiki>
 
<pre><nowiki>
kvm -no-acpi -m 384 -cdrom /dev/cdrom -boot d windows.img
+
$ virsh -c qemu:///system list
 +
libvir: Remote error : Permission denied
 +
error: failed to connect to the hypervisor
 +
$
 
</nowiki></pre>
 
</nowiki></pre>
-m tells how much memory to use - 256M is a safe minimum.
+
Something is wrong and you probably want to fix this before you move on. The critical point here is whether or not you have write access to ''/var/run/libvirt/libvirt-sock''.
Here is an alternate command with more memory, and using an iso.
+
== Network Bridging ==
 +
There are a few different ways to allow a virtual machine access to the external network. The default virtual network configuration is usermode networking, which uses the SLIRP protocol and traffic is NATed through the host interface to the outside network.
 +
To enable external hosts to directly access services on virtual machines a bridge needs to be configured. This allows the virtual interfaces to connect to the outside network through the physical interface, making them appear as normal hosts to the rest of the network.  
 +
'''Warning:''' Network bridging will not work when the physcial network device (eg eth1, ath0) used for bridging is a wireless device (eg ipw3945), as most wireless device drivers do not support bridging!
 +
=== Creating a network bridge on the host ===
 +
To setup a bridge interface edit /etc/network/interfaces and either comment or replace the existing config with (replace with the values for your network):
 
<pre><nowiki>
 
<pre><nowiki>
kvm -no-acpi -m 512 -cdrom /backups/windows.iso -boot d windows.img
+
auto lo
 +
iface lo inet loopback
 +
 
 +
auto br0
 +
iface br0 inet static
 +
        address 192.168.0.10
 +
        network 192.168.0.0
 +
        netmask 255.255.255.0
 +
        broadcast 192.168.0.255
 +
        gateway 192.168.0.1
 +
        bridge_ports eth0
 +
        bridge_fd 9
 +
        bridge_hello 2
 +
        bridge_maxage 12
 +
        bridge_stp off
 +
 
 +
iface eth0 inet static
 +
address 172.16.5.0
 +
netmask 255.255.255.0
 
</nowiki></pre>
 
</nowiki></pre>
To avoid issues with acpi later, when the Windows XP installer says "Press F6 for additional drivers", press F5 instead. The installer will give you the option to select Standard PC or other. Pick Standard PC and the install will continue.
+
or to use DHCP
IMPORTANT: the '-boot d' flag tells KVM to attempt to boot from cdrom first. After installing XP, you can boot your virtual machine simply with
+
 
<pre><nowiki>
 
<pre><nowiki>
kvm -no-acpi -m 384 -cdrom /dev/cdrom windows.img
+
auto lo
 +
iface lo inet loopback
 +
 
 +
auto br0
 +
iface br0 inet dhcp
 +
        bridge_ports eth0
 +
        bridge_fd 9
 +
        bridge_hello 2
 +
        bridge_maxage 12
 +
        bridge_stp off
 +
 
 +
iface eth0 inet static
 +
address 172.16.5.0
 +
netmask 255.255.255.0
 
</nowiki></pre>
 
</nowiki></pre>
You can create a panel launcher for XP by specifying the absolute path to the image (/home/user/windows.img)
+
This will create a virtual interface br0. Don't worry about the static eth0 line. It gets overwritten by the bridge but the statement is necessary or you get an eth0 error.
If you get the error:
+
Now restart networking :
 
<pre><nowiki>
 
<pre><nowiki>
Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal
+
sudo /etc/init.d/networking restart
error, but for better emulation accuracy either use a 2.6 host Linux kernel or
+
type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.
+
 
</nowiki></pre>
 
</nowiki></pre>
Then this can be fixed by doing:
+
=== Configuring ubuntu-vm-builder to create bridged guests by default ===
 +
Virtual machines are defined in XML files; ubuntu-vm-builder, the tool we will use to create VMs, bases them on the template file /usr/share/ubuntu-vm-builder/templates/libvirt.tmpl.
 +
Open that file, and change:
 
<pre><nowiki>
 
<pre><nowiki>
sudo sh -c "echo 1024 > /proc/sys/dev/rtc/max-user-freq"
+
    <interface type='network'>
 +
      <mac address='%MAC%'/>
 +
      <source network='default'/>
 +
    </interface>
 
</nowiki></pre>
 
</nowiki></pre>
If your virtual Windows boots and runs very slowly, you should use the ACPI-Workaround:
+
To:
[http://kvm.qumranet.com/kvmwiki/Windows_ACPI_Workaround]
+
=== Installing Windows Vista ===
+
Vista requires acpi to install.  Also, Vista does not include drivers for the ne2k_pci nic, but does include drivers for the rtl8139 nic.
+
 
<pre><nowiki>
 
<pre><nowiki>
kvm -m 1000 -cdrom /dev/cdrom -boot d -net nic,model=rtl8139 -net user windows-vista.img
+
    <interface type='bridge'>
 +
      <mac address='%MAC%'/>
 +
      <source bridge='br0'/>
 +
    </interface>
 
</nowiki></pre>
 
</nowiki></pre>
The KVM wiki, however, indicates that the emulated rtl8139 device doesn't work with the Vista driver, so you may have to install with the ne2k_pci nic (the default).
+
=== Converting an existing guest ===
 +
If you have already created VMs before, you can make them use bridged networking if you change the XML definition (in /etc/libvirt/qemu/) for the network interface, adjusting the mac address as desired from:
 
<pre><nowiki>
 
<pre><nowiki>
kvm -m 1000 -cdrom /dev/cdrom -boot d windows-vista.img
+
    <interface type='network'>
 +
      <mac address='00:11:22:33:44:55'/>
 +
      <source network='default'/>
 +
    </interface>
 
</nowiki></pre>
 
</nowiki></pre>
And then install the driver in Vista following these instructions in the KVM [[UbuntuWiki:]]
+
to:
[http://kvm.qumranet.com/kvmwiki/Vista_Networking_Workaround]
+
=== Installing a Ubuntu Edgy Guest ===
+
* Create a file for the virtual disk drive. Using the '-f qcow' option as shown below saves space, by not using the space until the guest does (till the maximum size of the disk is read).
+
 
<pre><nowiki>
 
<pre><nowiki>
qemu-img create edgy.img -f qcow 6G
+
    <interface type='bridge'>
 +
      <mac address='00:11:22:33:44:55'/>
 +
      <source bridge='br0'/>
 +
    </interface>
 
</nowiki></pre>
 
</nowiki></pre>
* Start KVM and install Edgy
+
You do not need to restart libvirtd to reload the changes; the easiest way is to log into virsh (a command line tool to manage VMs), stop the VM, reread its configuration file, and restart the VM:
Insert the Edgy install CD and run:
+
 
<pre><nowiki>
 
<pre><nowiki>
kvm -m 256 -cdrom /dev/cdrom -boot d edgy.img
+
yhamon@paris:/etc/libvirt/qemu$ ls
 +
mirror.xml  networks  vm2.xml
 +
yhamon@paris:/etc/libvirt/qemu$ sudo virsh
 +
Connecting to uri: qemu:///system
 +
Welcome to virsh, the virtualization interactive terminal.
 +
 
 +
Type:  'help' for help with commands
 +
      'quit' to quit
 +
 
 +
virsh # list
 +
Id Name                State
 +
----------------------------------
 +
10 vm2                  running
 +
15 mirror              running
 +
 
 +
virsh # shutdown mirror
 +
Domain mirror is being shutdown
 +
 
 +
virsh # define mirror.xml
 +
Domain mirror defined from mirror.xml
 +
 
 +
virsh # start mirror
 +
Domain mirror started
 +
 
 
</nowiki></pre>
 
</nowiki></pre>
You may also specify an .iso file on your hard drive ("-cdrom ~/Desktop/ubuntu-6.10-server-i386.iso").
+
The VM "mirror" is now using bridged networking.
With Intel processors the standard installer will fail due to use of [http://kvm.qumranet.com/kvmwiki/Intel_Real_Mode_Emulation_Problems real mode].  Try the [http://archive.ubuntu.com/ubuntu/dists/edgy/main/installer-i386/current/images/netboot/mini.iso netboot mini.iso] or alternate installer instead.
+
== Creating virtual machines ==
Boot your virtual machine with
+
Now that KVM is installed, let s see how we install our first VM. There is a tool to manage VMs on a remote host: virt-manager. Sadly, VM creation on a remote host is not supported yet - therefore you will have to create the VM in command line.
 +
There are two different tools to do this; virt-install, a python script developed by redhat, and ubuntu-vm-builder, developed by Canonical.  
 +
=== Ubuntu-vm-builder: the fast & powerful way to create Ubuntu JEOS VMs ===
 +
[http://doc.ubuntu.com/ubuntu/serverguide/C/ubuntu-vm-builder.html Ubuntu-vm-builder] is probably the best tool to use if you want to create VMs running Ubuntu JEOS 8.04, as the install is entirely scripted (very fast).  
 +
This tool is packaged, and in universe:
 
<pre><nowiki>
 
<pre><nowiki>
kvm -m 256 edgy.img
+
sudo apt-get install ubuntu-vm-builder
 
</nowiki></pre>
 
</nowiki></pre>
TODO: how do I tell it to boot a 32-bit VM vs. a 64-bit VM?  Answer: it looks totally automatic?
+
==== Basic use ====
== Advanced Networking ==
+
Here is a very basic example of how to use it:
KVM by default does not allow the outside world to connect to your virtual machines.  If you want them to appear on the network like regular computers, skip down to '''Virtual NICs Bridged Directly to Outside'''.  Other more complex setups are described below.
+
If the following doesn't address your needs, all existing qemu documentation should be relevant for KVM as well.
+
=== Terminology ===
+
VLAN: a virtual network segment.  Usually you can picture it as just a virtual switch.  You plug virtual NICs into VLANs, and wire VLANs together, and can ultimately create a switch fabric that very closely matches real-world setups.
+
=== Default, Usermode Networking ===
+
If you don't specify any networking options, KVM by default constructs a NIC connected to a private VLAN.  On this VLAN it also emulates single host that acts as a DHCP server and default router.  Any connections initiated by the guest are routed through KVM's private stack and appear to the host computer as requests coming from sockets opened by the KVM process on 127.0.0.1.
+
KVM's usermode networks typically contain only two addresses: 10.0.2.2 (the virtual host) and 10.0.2.15 (the guest).
+
Here it is in KVM's language:
+
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net user ...
+
sudo ubuntu-vm-builder kvm hardy
 
</nowiki></pre>
 
</nowiki></pre>
In other words: add a nic and connect it vlan 1.  Also add a virtual host connected to vlan 1.  The virtual host  connects the VLAN via NAT to the physical host.
+
This will create an Ubuntu Hardy, with all options set to default.
Because usermode networking is implemented using the SLIRP protocol, UDP is not supported.  If you need to move UDP packets to the host or the outside world, you will need to use a different technique.
+
==== More complex example ====
=== Connecting VLANs to Each Other ===
+
Now here is a somewhat more complex example:
==== Multiple NICs / VLANs ====
+
What if you want to set up a virtual machine with multiple NICs?  You need to create multiple VLANs to plug the NICs into.  To create more than one VLAN, assign each VLAN a unique ID.  For instance, this will create two NICs and two VLANs:
+
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic,vlan=0 -net socket,listen=:8010,vlan=0 -net nic,vlan=1 -net user,vlan=1 ...
+
ubuntu-vm-builder kvm hardy \
 +
                  --domain newvm \
 +
                  --dest newvm \
 +
                  --arch i386 \
 +
                  --hostname hostnameformyvm \
 +
                  --mem 256 \
 +
                  --user john \
 +
                  --pass doe \
 +
                  --ip 192.168.0.12 \
 +
                  --mask 255.255.255.0 \
 +
                  --net 192.168.0.0 \
 +
                  --bcast 192.168.0.255 \
 +
                  --gw 192.168.0.1 \
 +
                  --dns 192.168.0.1 \
 +
                  --mirror http://archive.localubuntumirror.net/ubuntu \
 +
                  --components main,universe \
 +
                  --addpkg vim \
 +
                  --libvirt qemu:///system ;
 
</nowiki></pre>
 
</nowiki></pre>
==== Socket Connections ====
+
This will create a new Ubuntu Hardy VM called "newvm", the hostname will be set to "hostnameformyvm", the network will be configured with a static IP address and a gateway at address 192.168.0.1. The --mirror will tell the script to download the packages from a local Ubuntu mirror instead of the default server (this may speed up by a lot the time necessary to create the VM). The components argument will enable main and universe by default on the VM, --addpkg vim will install vim, and finally the last argument will automatically add the newly created VM to KVM.
So, I can connect my virtual machines to private VLANs.  How do I connect those VLANs together?
+
Note: The manual isn't specific about the --exec option, used to execute a script file during the vm build process. If you specify the --exec option then you must provide the full path to the script you are executing. Not doing so will cause the installer to tell you that it can't find the script file.
The easiest is probably using regular TCP sockets.  One vlan must be the listener:
+
==== Install on a raw block device ====
 +
Ubuntu-vm-builder doesn't allow you to create the VM on a raw block device yet (like a standalone partition, or a iSCSI share). You can use ubuntu-vm-builder to create the qcow2 image and then move the VM to the block device with qemu-img though; if /dev/sdb is the disk device on which you want to move the virtual machine:
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net socket,listen=:8010 ...
+
sudo qemu-img convert root.qcow2 -O raw /dev/sdb
 
</nowiki></pre>
 
</nowiki></pre>
and the other vlan must be the initiator:
+
Edit the XML definition file for the VM in /etc/libvirt/qemu/, and set the source file to be:
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net socket,connect=127.0.0.1:8010 ...
+
<source file='/dev/sdb'/>
 
</nowiki></pre>
 
</nowiki></pre>
If you don't specify an address, the listener will listen on all connected interfacesTo only listen on localhost, specify this:
+
Redefine the VM and start it; it is now running from /dev/sdb.
 +
Ubuntu-vm-builder is a very powerful tool - to get a more detailed list of its capabilities, use ubuntu-vm-builder --help.
 +
=== Create VMs running other operating systems: virt-install ===
 +
Virt-install is radically different in the way it works. Instead of creating automagically a new VM, it will allow you to boot on an iso, and therefore to install almost any operating systemFor a list of supported operating system, consult [http://kvm.qumranet.com/kvmwiki/Guest_Support_Status kvm's official webpage].
 +
Copy the ISO of the system you want to install to your working directory:
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net socket,listen=127.0.0.1:8010 ...
+
yhamon@paris:~$ ls *.iso
 +
ubuntu-8.04-server-i386.iso  windowsxpsp2.iso
 
</nowiki></pre>
 
</nowiki></pre>
This, of course, also allows you to connect virtual lans running on different hosts.
+
Then, run virt-install:
==== Multicast Sockets ====
+
If you want to connect multiple initiators to a single listener, you must use a multicast socket.
+
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net socket,mcast=230.0.0.1:1234 ...
+
sudo virt-install -n xpsp2 -r 512 -f devserver.img -s 5 -c windowsxpsp2.iso --accelerate --vnc --noautoconsole
  $ kvm -net nic -net socket,mcast=230.0.0.1:1234 ...
+
  $ kvm -net nic -net socket,mcast=230.0.0.1:1234 ...
+
 
</nowiki></pre>
 
</nowiki></pre>
That connects 3 different VLANs at the same point.  Frames sent on any VLAN will be received by all others.
+
This will boot a new VM from the ISO. Connect to the new VM using virt-viewer:
==== VDE ====
+
Multiple VLANs can also be connected to a single VDE.  VDEs are described further in Advanced Networking below.  TODO: introduce VDEs here, provide examples.
+
=== Connecting VLANs to the Host ===
+
Great, we can now create and wire up a huge virtual switching fabric, but how do we connect it to the outside world?  Our packets are still entirely virtual. We did see how to masquerade TCP connections through the usermode device but that has too many limitations.  How do we connect VLANs to existing, physical networks?
+
==== The Tap Device ====
+
Most (all?) virtual-to-physical connections are made through a tap device.  Tap devices are regular network interfaces, not any different from eth0, eth1, lo, etc.  One end of the tap is connected to the VLAN, the other end is configured and routed using regular networking tools (ifconfig, route, etc).
+
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net tap ...
+
virt-viewer -c qemu+ssh://10.10.10.10/system xpsp2
 
</nowiki></pre>
 
</nowiki></pre>
That command created a new, unique tap ethernet device (tap0, tap1, etc).  The /etc/qemu-ifup script is used to provision the new network device.  The default /etc/qemu-ifup simply assigns the new interface the IP address 172.20.0.1. You can specify an explicit network name using ifname=IF, and a different script to run using script=SCRIPT, like this:
+
You should now have a graphical interface to install your VM!
 +
== Manage your virtual machines ==
 +
=== From the shell ===
 +
You can manage your VMs from the shell using [http://linux.die.net/man/1/virsh virsh]. You can get a list of the available commands if you type "help". Type "help command" to get additional infos for a particular command.
 +
==== List your VMs ====
 +
Virsh allows you to list the virtual machines available on the current host:
 
<pre><nowiki>
 
<pre><nowiki>
  $ kvm -net nic -net tap,ifname=qtap0,script=/var/vm/vm0.ifup
+
yhamon@paris:/etc/libvirt/qemu$ sudo virsh
</nowiki></pre>
+
Connecting to uri: qemu:///system
Taps cleanly solve the networking problem for a single virtual machine.  Unfortunately, each guest requires its own tap device.  As you might imagine, this gets unweildy fast.
+
Welcome to virsh, the virtualization interactive terminal.
CLARIFICATION REQUIRED HERE: the 'kvm -net nic -net tap ...' command gives the output "warning: could not open /dev/net/tun: no virtual network emulation // Could not initialize device 'tap'". More instructions needed.
+
 
CORRECTION?: according to 'man kvm' the 'script=' parameter should perhaps be '/etc/kvm/kvm-ifup' ?
+
Type: 'help' for help with commands
NOTE: A bug in Qemu causes the tap problem mentioned above for kernels >=2.6.18.  Further information is in the Qemu FAQ at:
+
      'quit' to quit
http://calamari.reverse-dns.net:980/cgi-bin/moin.cgi/FrequentlyAskedQuestions#head-2511814cb92c14dbe1480089c04f83c281117a86
+
 
I found if I create the tap device manually with sudo (create, bring up, and in my case attach to the bridge interface) and pass that ifname to kvm, even though it complains still about not being able to set the tap up it works ok.
+
virsh # help list
=== Advanced Networking ===
+
  NAME
So, how can we run an arbitrary number of virtual machines, all able to talk to each other and the outside world?  Alas, there are a huge number of different ways to solve this, all with their own benefits and drawbacks (that's why network engineers get paid the big bucks).  Here are some common techniques.
+
    list - list domains
==== Virtual NICs on VDE, VDE Tap'd to Host, Tap NATed to Outside ====
+
 
This allows guests to initiate connections with each other, the host, and the outside world.  It also allows the host to initiate connections with any guest.  It doesn't allow the outside world to initiate connections with guests however (although you could manually proxy the connections through the host using kvm's -redir, port forwarding or ssh -L).  It's realtively unobtrusive to set up; you don't need to modify the host's network configuration at all.
+
  SYNOPSIS
These steps show how to test out this type of network, but not how to make it persistent. You will have to run these programs manually every time your machine boots.
+
    list [--inactive | --all]
* <code><nowiki>$ sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"</nowiki></code>
+
 
* Edit /etc/udev/rules.d/20-names.rules and change
+
  DESCRIPTION
KERNEL=="tun", NAME="net/%k"
+
    Returns list of domains.
to
+
 
KERNEL=="tun", NAME="net/%k" GROUP="kvm", MODE="0660
+
  OPTIONS
* add "tun" to /etc/modules.  Also run <code><nowiki>sudo modprobe tun</nowiki></code>.
+
    --inactive      list inactive domains
* <code><nowiki>$ sudo apt-get install vde dnsmasq</nowiki></code>
+
    --all           list inactive & active domains
* <code><nowiki>$ sudo /etc/init.d/dnsmasq stop</nowiki></code>
+
 
* <code><nowiki>$ sudo vde_switch -tap qtap0 -daemon</nowiki></code>  Now vde_switch is listening on /var/run/vde.ctl/ctl (this is a *buntu 7.10 change, normally the default is /tmp/vde.ctl). Use <code><nowiki>--sock PATH</nowiki></code> if you want to specify where to put the socket.
+
virsh # list
* <code><nowiki>$ sudo ifconfig qtap0 10.111.111.254 broadcast 10.111.111.255 netmask 255.255.255.0 up</nowiki></code>
+
  Id Name                State
* <code><nowiki>$ sudo iptables -t nat -A POSTROUTING -o `route -n | egrep '^0\.0\.0\.0 ' | nawk '{print $8}'` -j MASQUERADE</nowiki></code> 
+
----------------------------------
This command should work on 99% of setups; ie with one default route. IF you run a machine with more than one default route you should be able to work out a substitute for this command.
+
15 mirror              running
* <code><nowiki>$ sudo dnsmasq --log-queries --user=nobody --dhcp-leasefile=/var/tmp/dnsmasq-leasefile --dhcp-range=10.111.111.129,10.111.111.199,255.255.255.0,10.111.111.255,8h --interface=qtap0 --domain=qemu.lan -d</nowiki></code> TODO: tell how to configure /etc/dnsmasq.conf to do this.
+
16 vm2                  running
* <code><nowiki>$ sudo vdeq kvm -hda v2.qcow -boot c -net nic -net vde -m 192</nowiki></code>
+
 
* In the guest, put <code><nowiki>nameserver 10.111.111.254</nowiki></code> into /etc/resolv.conf. Also, check that DHCP gave it a sane IP address.
+
virsh # list --all
* In the guest, <code><nowiki>ping 10.111.111.254</nowiki></code> should work. In the host, <code><nowiki>ping 10.111.111.140</nowiki></code> (or whatever the guest's IP address is) should work.  If so, the tap device works great.
+
Id Name                State
* Now, from the guest, try pinging an external IP address.  If that works, then masquerading works.  Now try pinging an external domain name, like google.com.  If that works, congratulations, dnsmasq works and everything should be set up correctly.
+
----------------------------------
Cribbed from the exellent http://alien.slackbook.org/dokuwiki/doku.php?id=slackware:vde
+
  15 mirror              running
===== Permanent Setup =====
+
  16 vm2                  running
* Install the dnsmasq and vde packages.
+
  - test5                shut off
* Enable IP forwarding. Add the line in /etc/sysctl.conf <code><nowiki>net.ipv4.conf.all.forwarding=1</nowiki></code>
+
</nowiki></pre>  
* Edit /etc/udev/rules.d/20-names.rules and change
+
==== Define, undefine, start, shutdown, destroy VMs ====
KERNEL=="tun", NAME="net/%k"
+
The VMs you see with list --all are VMs that have been "defined" from an XML file. Every VM is configured via a XML file in /etc/libvirt/qemu. If you want to remove a VM from the list of VMs, you need to undefine it:
to
+
KERNEL=="tun", NAME="net/%k" GROUP="kvm", MODE="0660
+
* Load the tun module. Add <code><nowiki>tun</nowiki></code> to /etc/modules
+
* Configure the VDE qtap0 interface. Add the following lines to /etc/network/interfaces.
+
'''Make sure the IP and subnet you choose are not already in use, first.'''
+
'''Assumes your gateway interface is eth0'''
+
 
<pre><nowiki>
 
<pre><nowiki>
auto qtap0
+
virsh # undefine test5  # WARNING: undefine will delete your XML file!
iface qtap0 inet static
+
Domain test5 has been undefined
        address 10.111.111.254
+
 
        netmask 255.255.255.0
+
virsh # list --all
        pre-up /usr/bin/vde_switch --tap qtap0 --daemon --group vde2-net --mod 775 --mgmtmode 770 --pidfile /var/run/vde_switch.pid
+
Id Name                State
pre-up /etc/init.d/dnsmasq restart
+
----------------------------------
        up iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
+
15 mirror              running
        down iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
+
16 vm2                  running
post-down kill -s HUP `cat /var/run/vde_switch.pid`
+
 
</nowiki></pre>
 
</nowiki></pre>
The first line "auto qtap0" allows ubuntu to automatically start the qtap0 interface at boot.  You can omit this line if you want to manually start the qtap0 interface each time, by issuing "sudo ifup qtap0".
+
To be able to undefine a virtual machine, it needs to be shutdown first:
The options '''--group vde2-net --mod 775 --mgmtmode 770''' allows any user in group '''vde2-net''' to run vdeq without superuser (sudo) privileges. The '''--pidfile /var/run/vde_switch.pid''' option  allows the '''post-down''' command to stop the virtual switch when the interface is brought down (using ifdown qtap0).
+
* Configure dnsmasq. Set the following values in /etc/dnsmasq.conf: '''Make sure the IP and subnet you choose are not already in use, first.'''
+
 
<pre><nowiki>
 
<pre><nowiki>
user=nobody
+
virsh # shutdown mirror
domain=qemu.lan
+
Domain mirror is being shutdown
interface=qtap0
+
dhcp-range=10.111.111.1,10.111.111.253,255.255.255.0,10.111.111.255,8h
+
 
</nowiki></pre>
 
</nowiki></pre>
* Once you restart, your system will be configured to operate as a NAT gateway for your guest machines.
+
This command asks for a nice shutdown (like running shutdown in command line) - but you can also use "destroy", the more brutal way of shutting down a VM, equivalent of taking the power cable off:
* '''To avoid the need for sudo''' add the group "vde2-net" to all users that will use VDE (log-out and log-in for this to take effect)
+
 
<pre><nowiki>
 
<pre><nowiki>
sudo usermod -aG vde2-net $USER
+
virsh # destroy mirror
 +
Domain mirror destroyed
 
</nowiki></pre>
 
</nowiki></pre>
* Start qemu/kvm using vdeq (If the user is a member of the vde2-net group, sudo isn't required):
+
If you have made a change to the XML configuration file, you need to tell KVM to reload it before restarting the VM:
 
<pre><nowiki>
 
<pre><nowiki>
vdeq kvm ... -net nic -net vde
+
virsh # define /etc/libvirt/qemu/mirror.xml
 +
Domain mirror defined from /etc/libvirt/qemu/mirror.xml
 
</nowiki></pre>
 
</nowiki></pre>
* If using the GUIs qemu-launcher and/or qemuctl:
+
Then, to restart the VM:
Create a shell script to start qemuctl with kvm called /usr/bin/qemuctl-kvm:
+
 
<pre><nowiki>
 
<pre><nowiki>
#!/bin/bash
+
virsh # start mirror
CMDLINE=""
+
Domain mirror started
for arg in $*; do
+
if [ "${arg}" = "-no-kqemu" ]; then
+
  arg="-no-kvm"
+
fi
+
CMDLINE="$CMDLINE $arg"
+
if [ "${arg}" = "vde" ]; then
+
  LAUNCHER="vdeq"
+
fi
+
done
+
qemuctl -qemu $LAUNCHER kvm $CMDLINE
+
 
</nowiki></pre>
 
</nowiki></pre>
Set its permissions:
+
==== Suspend and resume a Virtual Machine ====
 +
Virsh allows you to easily suspend and resume a virtual machine.
 
<pre><nowiki>
 
<pre><nowiki>
sudo chmod 755 /usr/bin/qemuctl-kvm
+
virsh # suspend mirror
 +
Domain mirror suspended
 +
 
 +
virsh # resume mirror
 +
Domain mirror resumed
 
</nowiki></pre>
 
</nowiki></pre>
Start the GUI control panel
+
=== Using a graphical interface ===
 +
There is also an easier way to manage your virtual machines. The tool virt-manager allows you to use a graphical interface to interact with KVM. Install virt-manager on your desktop:
 
<pre><nowiki>
 
<pre><nowiki>
qemuctl-kvm -qemu vdeq kvm ... -net nic -net vde
+
sudo apt-get install virt-manager virt-viewer
 
</nowiki></pre>
 
</nowiki></pre>
If starting from qemu-launcher, change the Launcher Settings '''Path to 'qemuctl':''' to /usr/bin/qemuctl-kvm
+
And use it to connect to your server:
==== Virtual NICs Bridged Directly to Outside ====
+
When you use this method, your guest machines appear to the external network exactly as if they were real.  The host and guest interface will share a ''bridged interface'' (br0) and br0 will use eth0 to actually connect to the network.  When you use this within a small home network, this actually is the easiest approach.
+
* Stop your host networking
+
 
<pre><nowiki>
 
<pre><nowiki>
/etc/init.d/networking stop
+
$ virt-manager -c qemu+ssh://10.10.10.10/system
 
</nowiki></pre>
 
</nowiki></pre>
* Edit /etc/network/interfaces  ... assumes eth0 as primary network interface
+
10.10.10.10 being the IP address of your host running KVM.
 +
https://help.ubuntu.com/community/KVM?action=AttachFile&do=get&target=virt-manager.png
 +
== How-to edit the attributes of a Virtual Machine (add CPUs, RAM) ==
 +
KVM allows you to create SMP guests: If you have several processors/cores on the KVM host, each VM can use one or more than one of these. To add CPUs to one VM, you need to edit the '/etc/libvirt/qemu/yourvm.xml' file:
 
<pre><nowiki>
 
<pre><nowiki>
auto lo eth0 br0
+
<domain type='kvm'>
 
+
  <name>mirror</name>
iface lo inet loopback
+
  <uuid>ec13fb81-7960-ec5a-fa99-d8928f75d3ea</uuid>
 
+
  <memory>524288</memory>
iface br0 inet dhcp
+
  <currentMemory>524288</currentMemory>
        bridge_ports eth0
+
  <vcpu>1</vcpu>
        bridge_maxwait 2
+
  <os>
        #kvm has to have this set to 0.0.0.0 to work... not sure why
+
    <type>hvm</type>
        #not sure if promisc is necessary
+
    <boot dev='hd'/>
        up /sbin/ifconfig eth0 inet 0.0.0.0 promisc
+
  </os>
       
+
  <clock offset='utc'/>
#set to something random, br0 initialization will undo this
+
  <on_poweroff>destroy</on_poweroff>
iface eth0 inet static
+
  <on_reboot>restart</on_reboot>
        address 172.16.5.0
+
  <on_crash>destroy</on_crash>
        netmask 255.255.255.0
+
  <devices>
 +
    <emulator>/usr/bin/kvm</emulator>
 +
    <disk type='file' device='disk'>
 +
      <source file='/home/yhamon/mirror.img'/>
 +
      <target dev='hda' bus='ide'/>
 +
    </disk>
 +
    <interface type='bridge'>
 +
      <mac address='00:16:3e:4d:94:c0'/>
 +
      <source bridge='br0'/>
 +
    </interface>
 +
    <input type='mouse' bus='ps2'/>
 +
    <graphics type='vnc' port='-1' listen='127.0.0.1'/>
 +
  </devices>
 +
</domain>
 
</nowiki></pre>
 
</nowiki></pre>
* Start your host networking. run ``ifconfig`` that br0 has an '''inet addr:''' and that eth0 does not.
+
This file is the equivalent of *.vmx files in vmware, and is pretty easy to understand. To assign 2 CPUs to your VM, just change '<vcpu>1</vcpu>' by '<vcpu>2</vcpu>'. You will need to shutdown your vm, redefine it, and restart it, to have the changes active (see previous chapter for how to do this).
 +
== Troubleshooting/FAQ ==
 +
=== How to boot Dapper, Edgy, Feisty or Gutsy ISO ===
 +
* Q: I'm on Intel hardware, and I'm trying to boot Dapper, Edgy, Feisty, or Gutsy, but kvm fails immediately.
 +
* A: Yes, this is rather unfortunate. The issue is a limitation in Intel's virtualisation extensions that don't interact very well with gfxboot. The evil, hacky workaround is to modify the ISO to disable gfxboot. The following has worked for me, but it might kill your cat or make your coffee go cold or make other unpleasantries happen to you. You've been warned!
 
<pre><nowiki>
 
<pre><nowiki>
/etc/init.d/networking start
+
$ sed -e 's/GFXBOOT bootlogo/#FXBOOT bootlogo/g' < ubuntu-7.10-server-amd64.iso > ubuntu-7.10-server-amd64-nogfxboot.iso
 
</nowiki></pre>
 
</nowiki></pre>
* Start kvm
+
/!\ Don't change the above command! The length of the string mustn't change or you will have knackered the filesystem on the CD.
 +
A slightly safer way is to download gfxboot-disable from [http://hg.codemonkey.ws/gfxboot-disable] and then run:
 
<pre><nowiki>
 
<pre><nowiki>
kvm -no-acpi -m 512 -net nic -net tap disk.img
+
$ gfxboot-disable ubuntu-7.10-server-amd64.iso
 
</nowiki></pre>
 
</nowiki></pre>
Be careful!  This technique makes your virtual interfaces visible all over the office.  You may want to specify mac addresses on your guests to reduce the odds of a conflict.
+
=== How to convert [[UbuntuHelp:VMware|VMware]] machines to virt-manager ===
 +
kvm has the ability to use [[UbuntuHelp:VMware|VMware]]'s vmdk disk files directly, as long as the disk is wholly contained in the vmdk (ie [[UbuntuHelp:VMware|VMware]] allows splitting a disk into smaller, usually 2GB, vmdk files. kvm can't use these). Point kvm at the vmdk with the appropriate options (see <code><nowiki> man kvm-qemu </nowiki></code>), and it should work.
 +
To use the [[UbuntuHelp:VMware|VMware]] machine from within virt-manager, the .vmx file must be converted to libvirt's .xml. vmware2libvirt was created to help with this, and it can be used like so:
 
<pre><nowiki>
 
<pre><nowiki>
-net nic,macaddr=00:xx:xx:xx:xx:xx
+
$ vmware2libvirt -f ./file.vmx > file.xml
 +
$ virsh -c qemu:///system define file.xml
 
</nowiki></pre>
 
</nowiki></pre>
One downside to this approach is that network-manager will not manage br0 due to the fact that it is configured in ``/etc/network/interfaces``. This prevents you from being able to set it up as a [[UbuntuHelp:VPNClient|VPNClient]].
+
The first command converts the [[UbuntuHelp:VMware|VMware]] 'file.vmx' file to the libvirt compatible 'file.xml'. See <code><nowiki> man vmware2libvirt </nowiki></code> for details. '''Note:''' until vmware2libvirt is shipped with the libvirt packages, you can download it from [http://people.ubuntu.com/~soren/vmware2libvirt/]. The second command imports file.xml into libvirt. The imported .xml files are stored in /etc/libvirt/qemu.
==== Credits and References ====
+
'''IMPORTANT:''' keep in mind that while the .vmx file is converted to .xml, the disks are used as is. Please make backups, especially if you want to use the virtual machine in [[UbuntuHelp:VMware|VMware]] later.
Credits:
+
==== Caveats ====
Thanks to [http://linux.inet.hr/finally-user-friendly-virtualization-for-linux.html Finally User Friendly Virtualization For Linux] and http://wiki.u32.net/KVM
+
While vmware2libvirt works well on simple virtual machines, there are limitations because .vmx files don't always contain enough information, and also because vmware2libvirt tries not to make too many assumptions about what it finds. A couple of things to look out for:
References:
+
<ol><li>While vmware2libvirt attempts to detect a 64-bit guest, be sure that your 64-bit guest has in its .xml file: <pre><nowiki>
http://compsoc.dur.ac.uk/~djw/qemu.html
+
<os>
http://kidsquid.com/cgi-bin/moin.cgi/bridge
+
<type arch='x86_64' machine='pc'>hvm</type>
http://kvm.qumranet.com/kvmwiki/Guest_Support_Status
+
...
 +
</os>
 +
</nowiki></pre>
 +
</li><li>vmware2libvirt only detects and uses the first ethernet interface found. Additional interfaces can be added from within virt-manager.
 +
</li><li>Currently the first scsi disk is used if found, otherwise the first ide disk. Additional disks can be added from within virt-manager.
 +
</li><li>The converted virtual machine is hard-coded to use 1 cpu.  This can be changed with:<pre><nowiki>
 +
<vcpu>2</vcpu>
 +
</nowiki></pre>
 +
</li><li>vmware2libvirt does not (and cannot) convert anything that was VMware-specific within the guest. See 'Guest Notes' below for more details.</li></ol>
 +
 
 +
==== Guest notes ====
 +
<ol><li>Be sure to remove vmware-tools if you have it installed (otherwise it will overwrite xorg.conf on reboot)
 +
</li><li>Guests should not use ntp to synchronize the clock, so be sure to remove/disable ntpd
 +
</li><li>Linux guests with Xorg should be using the 'cirrus' video driver.  To use, adjust /etc/X11/xorg.conf to have: <pre><nowiki>
 +
Section "Device"
 +
        Identifier      "Configured Video Device"
 +
        Driver          "cirrus"
 +
EndSection
 +
</nowiki></pre> then be sure that your Screen section uses 'Configured Video Device' for its Device.
 +
</li><li>Linux guests with Xorg should be using the 'vmmouse' driver (not available on Ubuntu Dapper).  To use, perform within the guest: <pre><nowiki>
 +
aptitude install xserver-xorg-input-vmmouse
 +
</nowiki></pre> then adjust /etc/X11/xorg.conf to have (the Identifier line should not change, and you should have only an Identifier line and Driver line for the mouse): <pre><nowiki>
 +
Section "InputDevice"
 +
        Identifier      "Configured Mouse"
 +
        Driver          "vmmouse"
 +
EndSection
 +
</nowiki></pre>
 +
</li><li>Linux guests with Xorg need to adjust the resolution for Xorg in /etc/X11/xorg.conf.  Look for the Screen section, and make sure each of the 'Modes' lines has a reasonable resolution for your system (due to [https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/193456 bug #193456] the resolution in the guest's resolution needs to be smaller than the host). Eg: <pre><nowiki>
 +
Section "Screen"
 +
        ...
 +
        SubSection "Display"
 +
                Depth          16
 +
                Modes          "800x600" "640x480"
 +
        EndSubSection
 +
        SubSection "Display"
 +
                Depth          24
 +
                Modes          "800x600" "640x480"
 +
        EndSubSection
 +
EndSection
 +
</nowiki></pre>
 +
</li><li>Windows (other than Vista) virtual machines should substitute in the .xml file: <pre><nowiki>
 +
<features>
 +
  <acpi/>
 +
</features>
 +
</nowiki></pre> with: <pre><nowiki>
 +
<features/>
 +
</nowiki></pre>
 +
</li><li>Windows Vista virtual machines should add these to the /etc/libvirt/qemu/my-vista.xml file: <pre><nowiki>
 +
<domain type='kvm'>
 +
...
 +
  <features>
 +
    <acpi/>
 +
  </features>
 +
</domain>
 +
</nowiki></pre>
 +
</li><li>Debian Sarge (oldstable) may write /boot/grub/menu.lst incorrectly, resulting in the following on boot: <pre><nowiki>
 +
pivot_root: no such file or directory
 +
/sbin/init: 432: cannot open dev/console: no such file
 +
Kernel panic: Attempted to kill init!
 +
</nowiki></pre> The fix is at the grub menu, press 'e' and adjust <code><nowiki>root=/dev/hdb1</nowiki></code> to be <code><nowiki>root=/dev/hda1</nowiki></code>. After a successful boot, you will need to update /boot/grub/menu.lst to have: <pre><nowiki>
 +
# kopt=root=/dev/hda1 ro
 +
</nowiki></pre> and run: <pre><nowiki>
 +
# update-grub
 +
</nowiki></pre></li></ol>
 +
 
 +
----
 +
[[category:CategoryVirtualization]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2008年5月9日 (五) 19:08的版本

  1. title The Kernel Virtual Machine

Introduction

Preliminary notes

  • This is still work in progress! Feel free to jump in and extend this documentation!
  • The previous version of this page (which was documenting KVM for Feisty) has been moved to KVMFeisty

Basic info

Ubuntu uses kvm as the backend virtualisation technology. To manage VMs we use libvirt as the basic toolbox and virt-manager as the graphical frontend for managing your vm's.

How to get started

How to check if your CPU supports hardware virtualisation

To run KVM, you need a processor that supports virtualisation; Intel and AMD both have developed extensions for their processors, respectively INTEL-VT and AMD-V. To see if your processor supports one of these, you can run the following command:

egrep '(vmx|svm)' /proc/cpuinfo

If nothing is printed, it means that your CPU doesn't support hardware virtualisation. Otherwise, it does - but you still need to make sure that virtualisation is enabled in the BIOS.

Installation of KVM

For the following setup, we will assume that you are deploying KVM on a server, and therefore do not have any X server on the machine. You need to install a few packages first:

$ sudo apt-get install kvm libvirt-bin ubuntu-vm-builder
  • libvirt-bin provides libvirtd which you need to administer qemu and kvm instances using libvirt
  • kvm is the backend
  • ubuntu-vm-builder powerful command line tool for building virtual machines

Then, add yourself to the libvirtd and kvm groups:

$ sudo adduser `id -un` libvirtd
$ sudo adduser `id -un` kvm

This will give you access to the system-wide libvirtd instance. This is preferable for you because it gives you access to the advanced networking options rather than simply the "userspace networking" option as you may know it from QEmu. Note: You need to log out and log back in for the new group membership to take effect. Note: The `id -un` command will return the current username, for example if your username is joe you will be effectively be running sudo adduser joe libvirtd. You can test if your install has been successful with the following command:

$ virsh -c qemu:///system list
 Id Name                 State
----------------------------------

$

If on the other hand you get something like this:

$ virsh -c qemu:///system list
libvir: Remote error : Permission denied
error: failed to connect to the hypervisor
$

Something is wrong and you probably want to fix this before you move on. The critical point here is whether or not you have write access to /var/run/libvirt/libvirt-sock.

Network Bridging

There are a few different ways to allow a virtual machine access to the external network. The default virtual network configuration is usermode networking, which uses the SLIRP protocol and traffic is NATed through the host interface to the outside network. To enable external hosts to directly access services on virtual machines a bridge needs to be configured. This allows the virtual interfaces to connect to the outside network through the physical interface, making them appear as normal hosts to the rest of the network. Warning: Network bridging will not work when the physcial network device (eg eth1, ath0) used for bridging is a wireless device (eg ipw3945), as most wireless device drivers do not support bridging!

Creating a network bridge on the host

To setup a bridge interface edit /etc/network/interfaces and either comment or replace the existing config with (replace with the values for your network):

auto lo
iface lo inet loopback

auto br0
iface br0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off

iface eth0 inet static
	address 172.16.5.0
	netmask 255.255.255.0

or to use DHCP

auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off

iface eth0 inet static
	address 172.16.5.0
	netmask 255.255.255.0

This will create a virtual interface br0. Don't worry about the static eth0 line. It gets overwritten by the bridge but the statement is necessary or you get an eth0 error. Now restart networking :

sudo /etc/init.d/networking restart

Configuring ubuntu-vm-builder to create bridged guests by default

Virtual machines are defined in XML files; ubuntu-vm-builder, the tool we will use to create VMs, bases them on the template file /usr/share/ubuntu-vm-builder/templates/libvirt.tmpl. Open that file, and change:

    <interface type='network'>
      <mac address='%MAC%'/>
      <source network='default'/>
    </interface>

To:

    <interface type='bridge'>
      <mac address='%MAC%'/>
      <source bridge='br0'/>
    </interface>

Converting an existing guest

If you have already created VMs before, you can make them use bridged networking if you change the XML definition (in /etc/libvirt/qemu/) for the network interface, adjusting the mac address as desired from:

    <interface type='network'>
      <mac address='00:11:22:33:44:55'/>
      <source network='default'/>
    </interface>

to:

    <interface type='bridge'>
      <mac address='00:11:22:33:44:55'/>
      <source bridge='br0'/>
    </interface>

You do not need to restart libvirtd to reload the changes; the easiest way is to log into virsh (a command line tool to manage VMs), stop the VM, reread its configuration file, and restart the VM:

yhamon@paris:/etc/libvirt/qemu$ ls
mirror.xml  networks  vm2.xml
yhamon@paris:/etc/libvirt/qemu$ sudo virsh
Connecting to uri: qemu:///system
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # list
 Id Name                 State
----------------------------------
 10 vm2                  running
 15 mirror               running

virsh # shutdown mirror
Domain mirror is being shutdown

virsh # define mirror.xml
Domain mirror defined from mirror.xml

virsh # start mirror
Domain mirror started

The VM "mirror" is now using bridged networking.

Creating virtual machines

Now that KVM is installed, let s see how we install our first VM. There is a tool to manage VMs on a remote host: virt-manager. Sadly, VM creation on a remote host is not supported yet - therefore you will have to create the VM in command line. There are two different tools to do this; virt-install, a python script developed by redhat, and ubuntu-vm-builder, developed by Canonical.

Ubuntu-vm-builder: the fast & powerful way to create Ubuntu JEOS VMs

Ubuntu-vm-builder is probably the best tool to use if you want to create VMs running Ubuntu JEOS 8.04, as the install is entirely scripted (very fast). This tool is packaged, and in universe:

sudo apt-get install ubuntu-vm-builder

Basic use

Here is a very basic example of how to use it:

sudo ubuntu-vm-builder kvm hardy

This will create an Ubuntu Hardy, with all options set to default.

More complex example

Now here is a somewhat more complex example:

ubuntu-vm-builder kvm hardy \
                  --domain newvm \
                  --dest newvm \
                  --arch i386 \
                  --hostname hostnameformyvm \
                  --mem 256 \
                  --user john \
                  --pass doe \
                  --ip 192.168.0.12 \
                  --mask 255.255.255.0 \
                  --net 192.168.0.0 \
                  --bcast 192.168.0.255 \
                  --gw 192.168.0.1 \
                  --dns 192.168.0.1 \
                  --mirror http://archive.localubuntumirror.net/ubuntu \
                  --components main,universe \
                  --addpkg vim \
                  --libvirt qemu:///system ;

This will create a new Ubuntu Hardy VM called "newvm", the hostname will be set to "hostnameformyvm", the network will be configured with a static IP address and a gateway at address 192.168.0.1. The --mirror will tell the script to download the packages from a local Ubuntu mirror instead of the default server (this may speed up by a lot the time necessary to create the VM). The components argument will enable main and universe by default on the VM, --addpkg vim will install vim, and finally the last argument will automatically add the newly created VM to KVM. Note: The manual isn't specific about the --exec option, used to execute a script file during the vm build process. If you specify the --exec option then you must provide the full path to the script you are executing. Not doing so will cause the installer to tell you that it can't find the script file.

Install on a raw block device

Ubuntu-vm-builder doesn't allow you to create the VM on a raw block device yet (like a standalone partition, or a iSCSI share). You can use ubuntu-vm-builder to create the qcow2 image and then move the VM to the block device with qemu-img though; if /dev/sdb is the disk device on which you want to move the virtual machine:

sudo qemu-img convert root.qcow2 -O raw /dev/sdb

Edit the XML definition file for the VM in /etc/libvirt/qemu/, and set the source file to be:

<source file='/dev/sdb'/>

Redefine the VM and start it; it is now running from /dev/sdb. Ubuntu-vm-builder is a very powerful tool - to get a more detailed list of its capabilities, use ubuntu-vm-builder --help.

Create VMs running other operating systems: virt-install

Virt-install is radically different in the way it works. Instead of creating automagically a new VM, it will allow you to boot on an iso, and therefore to install almost any operating system. For a list of supported operating system, consult kvm's official webpage. Copy the ISO of the system you want to install to your working directory:

yhamon@paris:~$ ls *.iso
ubuntu-8.04-server-i386.iso  windowsxpsp2.iso

Then, run virt-install:

sudo virt-install -n xpsp2 -r 512 -f devserver.img -s 5 -c windowsxpsp2.iso --accelerate --vnc --noautoconsole

This will boot a new VM from the ISO. Connect to the new VM using virt-viewer:

virt-viewer -c qemu+ssh://10.10.10.10/system xpsp2

You should now have a graphical interface to install your VM!

Manage your virtual machines

From the shell

You can manage your VMs from the shell using virsh. You can get a list of the available commands if you type "help". Type "help command" to get additional infos for a particular command.

List your VMs

Virsh allows you to list the virtual machines available on the current host:

yhamon@paris:/etc/libvirt/qemu$ sudo virsh
Connecting to uri: qemu:///system
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # help list
  NAME
    list - list domains

  SYNOPSIS
    list [--inactive | --all]

  DESCRIPTION
    Returns list of domains.

  OPTIONS
    --inactive       list inactive domains
    --all            list inactive & active domains

virsh # list
 Id Name                 State
----------------------------------
 15 mirror               running
 16 vm2                  running

virsh # list --all
 Id Name                 State
----------------------------------
 15 mirror               running
 16 vm2                  running
  - test5                shut off

Define, undefine, start, shutdown, destroy VMs

The VMs you see with list --all are VMs that have been "defined" from an XML file. Every VM is configured via a XML file in /etc/libvirt/qemu. If you want to remove a VM from the list of VMs, you need to undefine it:

virsh # undefine test5   # WARNING: undefine will delete your XML file!
Domain test5 has been undefined

virsh # list --all
 Id Name                 State
----------------------------------
 15 mirror               running
 16 vm2                  running

To be able to undefine a virtual machine, it needs to be shutdown first:

virsh # shutdown mirror
Domain mirror is being shutdown

This command asks for a nice shutdown (like running shutdown in command line) - but you can also use "destroy", the more brutal way of shutting down a VM, equivalent of taking the power cable off:

virsh # destroy mirror
Domain mirror destroyed

If you have made a change to the XML configuration file, you need to tell KVM to reload it before restarting the VM:

virsh # define /etc/libvirt/qemu/mirror.xml
Domain mirror defined from /etc/libvirt/qemu/mirror.xml

Then, to restart the VM:

virsh # start mirror
Domain mirror started

Suspend and resume a Virtual Machine

Virsh allows you to easily suspend and resume a virtual machine.

virsh # suspend mirror
Domain mirror suspended

virsh # resume mirror
Domain mirror resumed

Using a graphical interface

There is also an easier way to manage your virtual machines. The tool virt-manager allows you to use a graphical interface to interact with KVM. Install virt-manager on your desktop:

sudo apt-get install virt-manager virt-viewer

And use it to connect to your server:

$ virt-manager -c qemu+ssh://10.10.10.10/system

10.10.10.10 being the IP address of your host running KVM. KVM?action=AttachFile&do=get&target=virt-manager.png

How-to edit the attributes of a Virtual Machine (add CPUs, RAM)

KVM allows you to create SMP guests: If you have several processors/cores on the KVM host, each VM can use one or more than one of these. To add CPUs to one VM, you need to edit the '/etc/libvirt/qemu/yourvm.xml' file:

<domain type='kvm'>
  <name>mirror</name>
  <uuid>ec13fb81-7960-ec5a-fa99-d8928f75d3ea</uuid>
  <memory>524288</memory>
  <currentMemory>524288</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type>hvm</type>
    <boot dev='hd'/>
  </os>
  <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'>
      <source file='/home/yhamon/mirror.img'/>
      <target dev='hda' bus='ide'/>
    </disk>
    <interface type='bridge'>
      <mac address='00:16:3e:4d:94:c0'/>
      <source bridge='br0'/>
    </interface>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' listen='127.0.0.1'/>
  </devices>
</domain>

This file is the equivalent of *.vmx files in vmware, and is pretty easy to understand. To assign 2 CPUs to your VM, just change '<vcpu>1</vcpu>' by '<vcpu>2</vcpu>'. You will need to shutdown your vm, redefine it, and restart it, to have the changes active (see previous chapter for how to do this).

Troubleshooting/FAQ

How to boot Dapper, Edgy, Feisty or Gutsy ISO

  • Q: I'm on Intel hardware, and I'm trying to boot Dapper, Edgy, Feisty, or Gutsy, but kvm fails immediately.
  • A: Yes, this is rather unfortunate. The issue is a limitation in Intel's virtualisation extensions that don't interact very well with gfxboot. The evil, hacky workaround is to modify the ISO to disable gfxboot. The following has worked for me, but it might kill your cat or make your coffee go cold or make other unpleasantries happen to you. You've been warned!
$ sed -e 's/GFXBOOT bootlogo/#FXBOOT bootlogo/g' < ubuntu-7.10-server-amd64.iso > ubuntu-7.10-server-amd64-nogfxboot.iso

/!\ Don't change the above command! The length of the string mustn't change or you will have knackered the filesystem on the CD. A slightly safer way is to download gfxboot-disable from [1] and then run:

$ gfxboot-disable ubuntu-7.10-server-amd64.iso

How to convert VMware machines to virt-manager

kvm has the ability to use VMware's vmdk disk files directly, as long as the disk is wholly contained in the vmdk (ie VMware allows splitting a disk into smaller, usually 2GB, vmdk files. kvm can't use these). Point kvm at the vmdk with the appropriate options (see man kvm-qemu ), and it should work. To use the VMware machine from within virt-manager, the .vmx file must be converted to libvirt's .xml. vmware2libvirt was created to help with this, and it can be used like so:

$ vmware2libvirt -f ./file.vmx > file.xml
$ virsh -c qemu:///system define file.xml

The first command converts the VMware 'file.vmx' file to the libvirt compatible 'file.xml'. See man vmware2libvirt for details. Note: until vmware2libvirt is shipped with the libvirt packages, you can download it from [2]. The second command imports file.xml into libvirt. The imported .xml files are stored in /etc/libvirt/qemu. IMPORTANT: keep in mind that while the .vmx file is converted to .xml, the disks are used as is. Please make backups, especially if you want to use the virtual machine in VMware later.

Caveats

While vmware2libvirt works well on simple virtual machines, there are limitations because .vmx files don't always contain enough information, and also because vmware2libvirt tries not to make too many assumptions about what it finds. A couple of things to look out for:

  1. While vmware2libvirt attempts to detect a 64-bit guest, be sure that your 64-bit guest has in its .xml file:
    <os>
     <type arch='x86_64' machine='pc'>hvm</type>
     ...
    </os>
    
  2. vmware2libvirt only detects and uses the first ethernet interface found. Additional interfaces can be added from within virt-manager.
  3. Currently the first scsi disk is used if found, otherwise the first ide disk. Additional disks can be added from within virt-manager.
  4. The converted virtual machine is hard-coded to use 1 cpu. This can be changed with:
    <vcpu>2</vcpu>
    
  5. vmware2libvirt does not (and cannot) convert anything that was VMware-specific within the guest. See 'Guest Notes' below for more details.

Guest notes

  1. Be sure to remove vmware-tools if you have it installed (otherwise it will overwrite xorg.conf on reboot)
  2. Guests should not use ntp to synchronize the clock, so be sure to remove/disable ntpd
  3. Linux guests with Xorg should be using the 'cirrus' video driver. To use, adjust /etc/X11/xorg.conf to have:
    Section "Device"
            Identifier      "Configured Video Device"
            Driver          "cirrus"
    EndSection
    
    then be sure that your Screen section uses 'Configured Video Device' for its Device.
  4. Linux guests with Xorg should be using the 'vmmouse' driver (not available on Ubuntu Dapper). To use, perform within the guest:
    aptitude install xserver-xorg-input-vmmouse
    
    then adjust /etc/X11/xorg.conf to have (the Identifier line should not change, and you should have only an Identifier line and Driver line for the mouse):

    Section "InputDevice"

           Identifier      "Configured Mouse"
           Driver          "vmmouse"
    

    EndSection

  5. Linux guests with Xorg need to adjust the resolution for Xorg in /etc/X11/xorg.conf. Look for the Screen section, and make sure each of the 'Modes' lines has a reasonable resolution for your system (due to bug #193456 the resolution in the guest's resolution needs to be smaller than the host). Eg:
    Section "Screen"
            ...
            SubSection "Display"
                    Depth           16
                    Modes           "800x600" "640x480"
            EndSubSection
            SubSection "Display"
                    Depth           24
                    Modes           "800x600" "640x480"
            EndSubSection
    EndSection
    
  6. Windows (other than Vista) virtual machines should substitute in the .xml file:
    <features>
      <acpi/>
    </features>
    
    with:

    <features/>

  7. Windows Vista virtual machines should add these to the /etc/libvirt/qemu/my-vista.xml file:
    <domain type='kvm'>
    ...
      <features>
        <acpi/>
      </features>
    </domain>
    
  8. Debian Sarge (oldstable) may write /boot/grub/menu.lst incorrectly, resulting in the following on boot:
    pivot_root: no such file or directory
    /sbin/init: 432: cannot open dev/console: no such file
    Kernel panic: Attempted to kill init!
    
    The fix is at the grub menu, press 'e' and adjust root=/dev/hdb1 to be root=/dev/hda1. After a successful boot, you will need to update /boot/grub/menu.lst to have:
    1. kopt=root=/dev/hda1 ro
    and run:
    1. update-grub