特殊:Badtitle/NS100:KVMFeisty:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
(未显示同一用户的1个中间版本) | |||
第3行: | 第3行: | ||
#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. | 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. | ||
/!\ For 8.04 implementation, which is a bit nicer, please have a look at [[KVM]] | /!\ For 8.04 implementation, which is a bit nicer, please have a look at [[UbuntuHelp:KVM|KVM]] | ||
== Running Guest Systems on Ubuntu 7.04 Feisty Fawn == | == Running Guest Systems on Ubuntu 7.04 Feisty Fawn == | ||
=== Setting up KVM on Ubuntu 7.04 Feisty Fawn === | === Setting up KVM on Ubuntu 7.04 Feisty Fawn === | ||
第56行: | 第56行: | ||
</nowiki></pre> | </nowiki></pre> | ||
If your virtual Windows boots and runs very slowly, you should use the ACPI-Workaround: | 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 === | === 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. | Vista requires acpi to install. Also, Vista does not include drivers for the ne2k_pci nic, but does include drivers for the rtl8139 nic. | ||
第67行: | 第67行: | ||
</nowiki></pre> | </nowiki></pre> | ||
And then install the driver in Vista following these instructions in the KVM [[UbuntuWiki:]] | 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 === | === 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). | * 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). | ||
第84行: | 第84行: | ||
</nowiki></pre> | </nowiki></pre> | ||
then try creating the disk image in a different format, e.g. vmdk instead of qcow. | then try creating the disk image in a different format, e.g. vmdk instead of qcow. | ||
With Intel processors the standard installer will fail due to use of | 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 | Boot your virtual machine with | ||
<pre><nowiki> | <pre><nowiki> | ||
第276行: | 第276行: | ||
-net nic,macaddr=00:xx:xx:xx:xx:xx | -net nic,macaddr=00:xx:xx:xx:xx:xx | ||
</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 [[VPNClient]]. | 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]]. | ||
==== Credits and References ==== | ==== Credits and References ==== | ||
Credits: | Credits: | ||
Thanks to | 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: | References: | ||
http://compsoc.dur.ac.uk/~djw/qemu.html | http://compsoc.dur.ac.uk/~djw/qemu.html |
2008年10月19日 (日) 23:38的最新版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/KVMFeisty }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/af | • {{#if: UbuntuHelp:KVMFeisty|Afrikaans| [[::KVMFeisty/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ar | • {{#if: UbuntuHelp:KVMFeisty|العربية| [[::KVMFeisty/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/az | • {{#if: UbuntuHelp:KVMFeisty|azərbaycanca| [[::KVMFeisty/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/bcc | • {{#if: UbuntuHelp:KVMFeisty|جهلسری بلوچی| [[::KVMFeisty/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/bg | • {{#if: UbuntuHelp:KVMFeisty|български| [[::KVMFeisty/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/br | • {{#if: UbuntuHelp:KVMFeisty|brezhoneg| [[::KVMFeisty/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ca | • {{#if: UbuntuHelp:KVMFeisty|català| [[::KVMFeisty/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/cs | • {{#if: UbuntuHelp:KVMFeisty|čeština| [[::KVMFeisty/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/de | • {{#if: UbuntuHelp:KVMFeisty|Deutsch| [[::KVMFeisty/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/el | • {{#if: UbuntuHelp:KVMFeisty|Ελληνικά| [[::KVMFeisty/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/es | • {{#if: UbuntuHelp:KVMFeisty|español| [[::KVMFeisty/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/fa | • {{#if: UbuntuHelp:KVMFeisty|فارسی| [[::KVMFeisty/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/fi | • {{#if: UbuntuHelp:KVMFeisty|suomi| [[::KVMFeisty/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/fr | • {{#if: UbuntuHelp:KVMFeisty|français| [[::KVMFeisty/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/gu | • {{#if: UbuntuHelp:KVMFeisty|ગુજરાતી| [[::KVMFeisty/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/he | • {{#if: UbuntuHelp:KVMFeisty|עברית| [[::KVMFeisty/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/hu | • {{#if: UbuntuHelp:KVMFeisty|magyar| [[::KVMFeisty/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/id | • {{#if: UbuntuHelp:KVMFeisty|Bahasa Indonesia| [[::KVMFeisty/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/it | • {{#if: UbuntuHelp:KVMFeisty|italiano| [[::KVMFeisty/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ja | • {{#if: UbuntuHelp:KVMFeisty|日本語| [[::KVMFeisty/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ko | • {{#if: UbuntuHelp:KVMFeisty|한국어| [[::KVMFeisty/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ksh | • {{#if: UbuntuHelp:KVMFeisty|Ripoarisch| [[::KVMFeisty/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/mr | • {{#if: UbuntuHelp:KVMFeisty|मराठी| [[::KVMFeisty/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ms | • {{#if: UbuntuHelp:KVMFeisty|Bahasa Melayu| [[::KVMFeisty/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/nl | • {{#if: UbuntuHelp:KVMFeisty|Nederlands| [[::KVMFeisty/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/no | • {{#if: UbuntuHelp:KVMFeisty|norsk| [[::KVMFeisty/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/oc | • {{#if: UbuntuHelp:KVMFeisty|occitan| [[::KVMFeisty/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/pl | • {{#if: UbuntuHelp:KVMFeisty|polski| [[::KVMFeisty/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/pt | • {{#if: UbuntuHelp:KVMFeisty|português| [[::KVMFeisty/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ro | • {{#if: UbuntuHelp:KVMFeisty|română| [[::KVMFeisty/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/ru | • {{#if: UbuntuHelp:KVMFeisty|русский| [[::KVMFeisty/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/si | • {{#if: UbuntuHelp:KVMFeisty|සිංහල| [[::KVMFeisty/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/sq | • {{#if: UbuntuHelp:KVMFeisty|shqip| [[::KVMFeisty/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/sr | • {{#if: UbuntuHelp:KVMFeisty|српски / srpski| [[::KVMFeisty/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/sv | • {{#if: UbuntuHelp:KVMFeisty|svenska| [[::KVMFeisty/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/th | • {{#if: UbuntuHelp:KVMFeisty|ไทย| [[::KVMFeisty/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/tr | • {{#if: UbuntuHelp:KVMFeisty|Türkçe| [[::KVMFeisty/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/vi | • {{#if: UbuntuHelp:KVMFeisty|Tiếng Việt| [[::KVMFeisty/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/yue | • {{#if: UbuntuHelp:KVMFeisty|粵語| [[::KVMFeisty/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/zh | • {{#if: UbuntuHelp:KVMFeisty|中文| [[::KVMFeisty/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/zh-hans | • {{#if: UbuntuHelp:KVMFeisty|中文(简体)| [[::KVMFeisty/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:KVMFeisty | UbuntuHelp:KVMFeisty | {{#if: | :}}KVMFeisty}}/zh-hant | • {{#if: UbuntuHelp:KVMFeisty|中文(繁體)| [[::KVMFeisty/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:KVMFeisty|:KVMFeisty|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :KVMFeisty/zh | | {{#ifexist: KVMFeisty/zh | | {{#ifeq: {{#titleparts:KVMFeisty|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:KVMFeisty|1|-1|}} | zh | | }}
- 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. /!\ For 8.04 implementation, which is a bit nicer, please have a look at KVM
Running Guest Systems on Ubuntu 7.04 Feisty Fawn
Setting up KVM on Ubuntu 7.04 Feisty Fawn
- Install the 'kvm' and 'qemu' packages. See InstallingSoftware.
- Install the 'uml-utilities' and 'bridge-utils' packages if you want to use tap/tun and bridged networking options.
- Manually load the driver:
- For AMD processors
sudo modprobe kvm-amd
- For Intel processors
sudo modprobe kvm-intel
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: http://ubuntuforums.org/showthread.php?t=350691
- Make /dev/kvm accessible to the user (needed in some cases):
sudo adduser $USER kvm # logout and back in
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).
qemu-img create windows.img -f qcow 6G
- Start KVM and install Windows
Insert the Windows install CD and run:
kvm -no-acpi -m 384 -cdrom /dev/cdrom -boot d windows.img
-m tells how much memory to use - 256M is a safe minimum. Here is an alternate command with more memory, and using an iso.
kvm -no-acpi -m 512 -cdrom /backups/windows.iso -boot d windows.img
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
kvm -no-acpi -m 384 -cdrom /dev/cdrom windows.img
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:
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.
Then this can be fixed by doing:
sudo sh -c "echo 1024 > /proc/sys/dev/rtc/max-user-freq"
If your virtual Windows boots and runs very slowly, you should use the ACPI-Workaround: [1]
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.
kvm -m 1000 -cdrom /dev/cdrom -boot d -net nic,model=rtl8139 -net user windows-vista.img
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).
kvm -m 1000 -cdrom /dev/cdrom -boot d windows-vista.img
And then install the driver in Vista following these instructions in the KVM UbuntuWiki: [2]
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).
qemu-img create edgy.img -f qcow 6G
- Start KVM and install Edgy
Insert the Edgy install CD and run:
kvm -m 256 -cdrom /dev/cdrom -boot d edgy.img
You may also specify an .iso file on your hard drive ("-cdrom ~/Desktop/ubuntu-6.10-server-i386.iso"). If you get
qemu: could not open disk image edgy.img
then try creating the disk image in a different format, e.g. vmdk instead of qcow. With Intel processors the standard installer will fail due to use of real mode. Try the netboot mini.iso or alternate installer instead. Boot your virtual machine with
kvm -m 256 edgy.img
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:
$ kvm -net nic -net user ...
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:
$ kvm -net nic,vlan=0 -net socket,listen=:8010,vlan=0 -net nic,vlan=1 -net user,vlan=1 ...
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:
$ kvm -net nic -net socket,listen=:8010 ...
and the other vlan must be the initiator:
$ kvm -net nic -net socket,connect=127.0.0.1:8010 ...
If you don't specify an address, the listener will listen on all connected interfaces. To only listen on localhost, specify this:
$ kvm -net nic -net socket,listen=127.0.0.1:8010 ...
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.
$ 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 ...
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).
$ kvm -net nic -net tap ...
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:
$ kvm -net nic -net tap,ifname=qtap0,script=/var/vm/vm0.ifup
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' ? NOTE: A bug in Qemu causes the tap problem mentioned above for kernels >=2.6.18. Further information is in the Qemu FAQ at: 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.
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.
$ sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
- Edit /etc/udev/rules.d/20-names.rules and change
KERNEL=="tun", NAME="net/%k" to KERNEL=="tun", NAME="net/%k" GROUP="kvm", MODE="0660
- add "tun" to /etc/modules. Also run
sudo modprobe tun
. $ sudo apt-get install vde dnsmasq
$ sudo /etc/init.d/dnsmasq stop
$ sudo vde_switch -tap qtap0 -daemon
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--sock PATH
if you want to specify where to put the socket.$ sudo ifconfig qtap0 10.111.111.254 broadcast 10.111.111.255 netmask 255.255.255.0 up
$ sudo iptables -t nat -A POSTROUTING -o `route -n | egrep '^0\.0\.0\.0 ' | nawk '{print $8}'` -j MASQUERADE
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.
$ 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
TODO: tell how to configure /etc/dnsmasq.conf to do this.$ sudo vdeq kvm -hda v2.qcow -boot c -net nic -net vde -m 192
- In the guest, put
nameserver 10.111.111.254
into /etc/resolv.conf. Also, check that DHCP gave it a sane IP address. - In the guest,
ping 10.111.111.254
should work. In the host,ping 10.111.111.140
(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. Add the line in /etc/sysctl.conf
net.ipv4.conf.all.forwarding=1
- Edit /etc/udev/rules.d/20-names.rules and change
KERNEL=="tun", NAME="net/%k" to KERNEL=="tun", NAME="net/%k" GROUP="kvm", MODE="0660
- Load the tun module. Add
tun
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
auto qtap0 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`
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". 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.
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
- 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)
sudo usermod -aG vde2-net $USER
- Start qemu/kvm using vdeq (If the user is a member of the vde2-net group, sudo isn't required):
vdeq kvm ... -net nic -net vde
- If using the GUIs qemu-launcher and/or qemuctl:
Create a shell script to start qemuctl with kvm called /usr/bin/qemuctl-kvm:
#!/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
Set its permissions:
sudo chmod 755 /usr/bin/qemuctl-kvm
Start the GUI control panel
qemuctl-kvm -qemu vdeq kvm ... -net nic -net vde
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
/etc/init.d/networking stop
- Edit /etc/network/interfaces ... assumes eth0 as primary network interface
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
- Start your host networking. run ``ifconfig`` that br0 has an inet addr: and that eth0 does not.
/etc/init.d/networking start
- Start kvm
kvm -no-acpi -m 512 -net nic -net tap disk.img
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.
-net nic,macaddr=00:xx:xx:xx:xx:xx
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 VPNClient.
Credits and References
Credits: Thanks to 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