个人工具

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

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示同一用户的13个中间版本)
第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 ==
== Running Guest Systems on Ubuntu 7.04 Feisty Fawn ==
+
Ubuntu uses [http://www.linux-kvm.org/page/Main_Page KVM] as the backend virtualisation technology and [http://libvirt.org/ libvirt] as its toolkit/APILibvirt frontends for managing VMs include [http://virt-manager.et.redhat.com/ virt-manager] (GUI) or virsh (CLI). Alternative management options include [http://www.convirture.com/ convirt] (GUI) or [http://www.convirture.com/wiki/index.php?title=Convirt2_Installation convirt2] (WWW).
=== Setting up KVM on Ubuntu 7.04 Feisty Fawn ===
+
== Documentation ==
* Install the ''''kvm'''' and ''''qemu'''' packages. See InstallingSoftware.
+
* [[UbuntuHelp:KVM/Installation|Installation]] - Installation and removal of KVM
* Manually load the driver:
+
* [[UbuntuHelp:KVM/Networking|Networking]] - Network configuration; includes bridging
** For AMD processors
+
* [[UbuntuHelp:KVM/CreateGuests|Guest Creation]] - Creation of different kinds of guests
<pre><nowiki>
+
* [[UbuntuHelp:KVM/Managing|Guest Management]] - Management of guests; command line or graphically
sudo modprobe kvm-amd
+
* [[UbuntuHelp:KVM/Directly|KVM Directly]] - Using KVM directly (without libvirt)
</nowiki></pre>
+
* [[UbuntuHelp:KVM/FAQ|FAQ]] - Frequently asked questions, troubleshooting tips and other recommendations
* For Intel processors
+
== Notes ==
<pre><nowiki>
+
* '''The previous version of this page (which was documenting KVM for Feisty) has been moved to [[UbuntuHelp:KVMFeisty|KVMFeisty]]'''
sudo modprobe kvm-intel
+
* '''Also checkout the official docs, http://doc.ubuntu.com/ubuntu/serverguide/C/virtualization.html'''
</nowiki></pre>
+
* The use case targeted when KVM was moved into main is '''"server virtualization"'''.  This means that even though KVM can be used to serve other purposes, it has been designed to be run on Ubuntu Server Edition to host non-graphical server operating systemsIf you are looking for software to serve graphically-based virtual machines, [[UbuntuHelp:VirtualBox|VirtualBox]], Parallels Workstation (or Parallels Desktop for Mac), or [[UbuntuHelp:VMware|VMware]] Player/Server are more suitable alternatives.
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". For more information, refer to this ubuntuforums.org thread:
+
----
http://ubuntuforums.org/showthread.php?t=350691
+
[[category:CategoryVirtualization]]
* Make /dev/kvm accessible to the user (needed in some cases):
+
<pre><nowiki>
+
sudo adduser $USER kvm
+
# logout and back in
+
</nowiki></pre>
+
=== Installing a Windows XP 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>
+
qemu-img create windows.img -f qcow 6G
+
</nowiki></pre>
+
* Start KVM and install Windows
+
Insert the Windows install CD and run:
+
<pre><nowiki>
+
kvm -no-acpi -m 384 -cdrom /dev/cdrom -boot d windows.img
+
</nowiki></pre>
+
-m tells how much memory to use - 256M is a safe minimum. 
+
Here is an alternate command with more memory, and using an iso.
+
<pre><nowiki>
+
kvm -no-acpi -m 512 -cdrom /backups/windows.iso -boot d windows.img
+
</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.
+
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>
+
kvm -no-acpi -m 384 -cdrom /dev/cdrom windows.img
+
</nowiki></pre>
+
You can create a panel launcher for XP by specifying the absolute path to the image (/home/user/windows.img)
+
If you get the error:
+
<pre><nowiki>
+
Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal
+
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>
+
Then this can be fixed by doing:
+
<pre><nowiki>
+
sudo sh -c "echo 1024 > /proc/sys/dev/rtc/max-user-freq"
+
</nowiki></pre>
+
If your virtual Windows boots and runs very slowly, you should use the ACPI-Workaround:
+
[http://kvm.qumranet.com/kvmwiki/Windows_ACPI_Workaround]
+
=== Installing Windows Vista ===
+
Vista requires acpi to installAlso, Vista does not include drivers for the ne2k_pci nic, but does include drivers for the rtl8139 nic.
+
<pre><nowiki>
+
kvm -m 1000 -cdrom /dev/cdrom -boot d -net nic,model=rtl8139 -net user windows-vista.img
+
</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).
+
<pre><nowiki>
+
kvm -m 1000 -cdrom /dev/cdrom -boot d windows-vista.img
+
</nowiki></pre>
+
And then install the driver in Vista following these instructions in the KVM [[UbuntuWiki:]]
+
[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>
+
qemu-img create edgy.img -f qcow 6G
+
</nowiki></pre>
+
* Start KVM and install Edgy
+
Insert the Edgy install CD and run:
+
<pre><nowiki>
+
kvm -m 256 -cdrom /dev/cdrom -boot d edgy.img
+
</nowiki></pre>
+
You may also specify an .iso file on your hard drive ("-cdrom ~/Desktop/ubuntu-6.10-server-i386.iso").
+
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.
+
Boot your virtual machine with
+
<pre><nowiki>
+
kvm -m 256 edgy.img
+
</nowiki></pre>
+
TODO: how do I tell it to boot a 32-bit VM vs. a 64-bit VM? Answer: it looks totally automatic?
+
== Advanced Networking ==
+
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>
+
$ kvm -net nic -net user ...
+
</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.
+
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.
+
=== Connecting VLANs to Each Other ===
+
==== 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>
+
$ kvm -net nic,vlan=0 -net socket,listen=:8010,vlan=0 -net nic,vlan=1 -net user,vlan=1 ...
+
</nowiki></pre>
+
==== Socket Connections ====
+
So, I can connect my virtual machines to private VLANs.  How do I connect those VLANs together?
+
The easiest is probably using regular TCP sockets.  One vlan must be the listener:
+
<pre><nowiki>
+
$ kvm -net nic -net socket,listen=:8010 ...
+
</nowiki></pre>
+
and the other vlan must be the initiator:
+
<pre><nowiki>
+
$ kvm -net nic -net socket,connect=127.0.0.1:8010 ...
+
</nowiki></pre>
+
If you don't specify an address, the listener will listen on all connected interfaces.  To only listen on localhost, specify this:
+
<pre><nowiki>
+
$ kvm -net nic -net socket,listen=127.0.0.1:8010 ...
+
</nowiki></pre>
+
This, of course, also allows you to connect virtual lans running on different hosts.
+
==== Multicast Sockets ====
+
If you want to connect multiple initiators to a single listener, you must use a multicast socket.
+
<pre><nowiki>
+
$ kvm -net nic -net socket,mcast=230.0.0.1:1234 ...
+
$ kvm -net nic -net socket,mcast=230.0.0.1:1234 ...
+
$ kvm -net nic -net socket,mcast=230.0.0.1:1234 ...
+
</nowiki></pre>
+
That connects 3 different VLANs at the same point.  Frames sent on any VLAN will be received by all others.
+
==== 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>
+
$ kvm -net nic -net tap ...
+
</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:
+
<pre><nowiki>
+
$ kvm -net nic -net tap,ifname=qtap0,script=/var/vm/vm0.ifup
+
</nowiki></pre>
+
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.
+
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' ?
+
=== Advanced Networking ===
+
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.
+
==== 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.
+
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.
+
* <code><nowiki>$ sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"</nowiki></code>
+
* add "tun" to /etc/modules.conf.  Also run <code><nowiki>sudo modprobe tun</nowiki></code>.
+
* <code><nowiki>$ sudo apt-get install vde dnsmasq</nowiki></code>
+
* <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 /tmp/vde.ctl (use <code><nowiki>-socket PATH</nowiki></code> to specify where to put the socket).
+
* <code><nowiki>$ sudo ifconfig qtap0 10.111.111.254 broadcast 10.111.111.255 netmask 255.255.255.0 up</nowiki></code>
+
* <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.
+
* <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.
+
* <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.
+
* 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.
+
* 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
+
===== Permanent Setup =====
+
* Install the dnsmasq and vde packages.
+
* Enable IP forwarding. Uncomment the line in /etc/sysctl.conf <code><nowiki>net.ipv4.conf.default.forwarding=1</nowiki></code>
+
* 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>
+
iface qtap0 inet static
+
address 10.111.111.254
+
netmask 255.255.255.0
+
pre-up /usr/bin/vde_switch --tap qtap0 --daemon --group vde2-net --mod 775 --mgmtmode 770 --pidfile /var/run/vde_switch.pid
+
pre-up /etc/init.d/dnsmasq restart
+
up iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
+
down iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
+
post-down kill -s HUP `cat /var/run/vde_switch.pid`
+
</nowiki></pre>
+
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>
+
user=nobody
+
domain=qemu.lan
+
interface=qtap0
+
dhcp-range=10.111.111.1,10.111.111.253,255.255.255.0,10.111.111.255,8h
+
</nowiki></pre>
+
* Once you restart, your system will be configured to operate as a NAT gateway for your guest machines.
+
* '''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>
+
sudo usermod -aG vde2-net $USER
+
</nowiki></pre>
+
* Start qemu/kvm using vdeq (If the user is a member of the vde2-net group, sudo isn't required):
+
<pre><nowiki>
+
vdeq kvm ... -net nic -net vde
+
</nowiki></pre>
+
* If using the GUIs qemu-launcher and/or qemuctl:
+
Create a shell script to start qemuctl with kvm called /usr/bin/qemuctl-kvm:
+
<pre><nowiki>
+
#!/bin/bash
+
CMDLINE=""
+
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>
+
Set its permissions:
+
<pre><nowiki>
+
sudo chmod 755 /usr/bin/qemuctl-kvm
+
</nowiki></pre>
+
Start the GUI control panel
+
<pre><nowiki>
+
qemuctl-kvm -qemu vdeq kvm ... -net nic -net vde
+
</nowiki></pre>
+
If starting from qemu-launcher, change the Launcher Settings '''Path to 'qemuctl':''' to /usr/bin/qemuctl-kvm
+
==== 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>
+
/etc/init.d/networking stop
+
</nowiki></pre>
+
* Edit /etc/network/interfaces  ... assumes eth0 as primary network interface
+
<pre><nowiki>
+
auto lo eth0 br0
+
iface lo inet loopback
+
iface br0 inet dhcp
+
bridge_ports eth0
+
bridge_maxwait 2
+
#kvm has to have this set to 0.0.0.0 to work... not sure why
+
#not sure if promisc is necessary
+
up /sbin/ifconfig eth0 inet 0.0.0.0 promisc
+
#set to something random, br0 initialization will undo this
+
iface eth0 inet static
+
address 172.16.5.0
+
netmask 255.255.255.0
+
</nowiki></pre>
+
* Start your host networking.  run ``ifconfig`` that br0 has an '''inet addr:''' and that eth0 does not.
+
<pre><nowiki>
+
/etc/init.d/networking start
+
</nowiki></pre>
+
* Start kvm
+
<pre><nowiki>
+
kvm -no-acpi -m 512 -net nic -net tap disk.img
+
</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.
+
<pre><nowiki>
+
-net nic,macaddr=00:xx:xx:xx:xx:xx
+
</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]].
+
==== Credits and References ====
+
Credits:
+
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
+
References:
+
http://compsoc.dur.ac.uk/~djw/qemu.html
+
http://kidsquid.com/cgi-bin/moin.cgi/bridge
+
http://kvm.qumranet.com/kvmwiki/Guest_Support_Status
+
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月19日 (三) 23:05的最新版本

  1. title The Kernel Virtual Machine

Introduction

Ubuntu uses KVM as the backend virtualisation technology and libvirt as its toolkit/API. Libvirt frontends for managing VMs include virt-manager (GUI) or virsh (CLI). Alternative management options include convirt (GUI) or convirt2 (WWW).

Documentation

  • Installation - Installation and removal of KVM
  • Networking - Network configuration; includes bridging
  • Guest Creation - Creation of different kinds of guests
  • Guest Management - Management of guests; command line or graphically
  • KVM Directly - Using KVM directly (without libvirt)
  • FAQ - Frequently asked questions, troubleshooting tips and other recommendations

Notes

  • The previous version of this page (which was documenting KVM for Feisty) has been moved to KVMFeisty
  • Also checkout the official docs, http://doc.ubuntu.com/ubuntu/serverguide/C/virtualization.html
  • The use case targeted when KVM was moved into main is "server virtualization". This means that even though KVM can be used to serve other purposes, it has been designed to be run on Ubuntu Server Edition to host non-graphical server operating systems. If you are looking for software to serve graphically-based virtual machines, VirtualBox, Parallels Workstation (or Parallels Desktop for Mac), or VMware Player/Server are more suitable alternatives.