个人工具

UbuntuHelp:KVM/Networking

来自Ubuntu中文

Wikibot讨论 | 贡献2009年5月18日 (一) 16:34的版本 (创建新页面为 '{{From|https://help.ubuntu.com/community/KVM/Networking}} {{Languages|UbuntuHelp:KVM/Networking}} #title KVM Networking <<Include(KVM/Header)>> == Configuring the network == The...')

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索


  1. title KVM Networking

<<Include(KVM/Header)>>

Configuring the network

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. If you do not want to access network services on your virtual machine then you can skip this next step. However 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! Warning 2: It seems that due to the implementation of CAP_NET_ADMIN in kernel 2.6.18, root access is needed to bring tap interfaces up and down. This means you'll need to run the VM Manager with gksudo (bad) or place a wrapper around the bits that bring up the taps (messy). More info when I figure this one out. (srodden) Warning 3: A number of people are having problems with the network bridge losing connection with the client after large amounts of data transfer (eg. during rsync) For a Hardy or Intrepid host/client see [[UbuntuHelp:[virtio|below]]].

Creating a network bridge on the host

Install the bridge-utils package:

sudo apt-get install bridge-utils

We are going to change the network configuration. To do it properly, we should first stop networking<<FootNote(This is needed for example when you move from DHCP to static address: it will stop the DHCP client, which a restart won't do if you changed the configuration already. If you are changing this remotely, then you should prepare your new configuration into a separate file and the use a script to stop networking, put the new configuration in place and start it back.)>>:

sudo invoke-rc.d networking stop

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 eth0
iface eth0 inet manual

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

or to use DHCP

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

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

This will create a virtual interface br0. 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>

Ubuntu 8.10 Notes:

  • The template moved to /etc/vmbuilder/libvirt/libvirtxml.tmpl
  • The <mac address='%MAC%'/> definition was removed.

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>

Note: Make sure the first octet in your MAC address is EVEN (eg. 00:) as MAC addresses with ODD first-bytes (eg. 01:) are reserved for multicast communication and can cause confusing problems for you. For instance, the guest will be able to receive ARP packets and reply to them, but the reply will confuse other machines. This is not a KVM issue, but just the way Ethernet works. 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$ virsh --connect qemu:///system
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.

DNS and DHCP Guests

libvirt uses dnsmasq to hand out IP addresses to guests which are configured to use dhcp. If on your host machine, you add 192.168.122.1 (the default IP of your host in libvirt) as your first nameserver in /etc/resolv.conf, then you can do name resolution for your guests. dnsmasq is smart enough to use the other 'nameserver' entries in your /etc/resolv.conf for resolving non-libvirt addresses. For example, if your current /etc/resolv.conf is:
search example.com
nameserver 10.0.0.1
Change this to be:
search example.com
nameserver 192.168.122.1
nameserver 10.0.0.1
Now, if you have a virtual machine named 'hardy-amd64', after starting it, you can do:
$ host hardy-amd64
hardy-amd64 has address <IP address given by dnsmasq>
Note that when using ssh you may need to use a trailing '.' after the hostname:
$ ssh hardy-amd64.
Finally, for this to work, your guest must send its hostname as part of the dhcp request. This is done automatically on many operating systems. For systems that do not send this automatically and use dhcp3, you can adjust the dhclient.conf file. For example, on Ubuntu 6.06 LTS (Dapper), adjust /etc/dhcp3/dhclient.conf to have:
send host-name "<your guest hostname here>";

IMPORTANT: Depending on your network configuration, your host's /etc/resolv.conf file might be periodically overwritten. You will have to either adjust the dhcp server on your network to hand out the additional libvirt name server for your libvirt hosts, or adjust each host machine accordingly. As there are many possible configurations for host machines, user's are encouraged to look at resolvconf and/or man interfaces.

Booting Over the Network Using PXE

The current Ubuntu release does not ship pxe binary ROM images because the source code is not included to recreate the images in the upstream tarball. There may be a way to automate the creation of these files as part of the package. In order to use boot -n, you will need to download or create the appropriate ROM images from [1] KVM and QEMU can emulate a number of network cards. Here is the current ROM files

'KVM Name' nic,model= 'Etherboot Identification' 'Etherboot Filename' 'KVM filename'
i82551 pxe-i82551.bin
i82557b pxe-i82557b.bin
i82559er pxe-i82559er.bin
ne2k_pci (default) ns8390:rtl8029 -- [10ec,8029] gpxe-0.9.3-rtl8029.rom pxe-ne2k_pci.bin
ne2k_isa pxe-ne2k_isa.bin
pcnet pxe-pcnet.bin
rtl8139 pxe-rtl8139.bin
e1000 ((e1000:e1000-0x1026 -- [8086,1026])) gpxe-0.9.3-e1000-0x1026.rom pxe-e1000.bin
smc91c111 pxe-smc91c111.bin
lance pxe-lance.bin
mcf_fec pxe-mcf_fec.bin

Copy the respective file to /usr/share/kvm and/or /usr/share/qemu. <<Anchor(virtio)>>

Use virtio for Ubuntu Hardy/Intrepid or Windows guests

For Windows guests follow this instruction. You may find the performances of the network relatively poor (approx. 100/120mbits on my servers, which are quite fast). If you are running Ubuntu Hardy or Intrepid, you can enable virtio. Go to the definition file of your VM, and add the virtio line to the definition of your network interface:

    <interface type='bridge'>
      <mac address='52:54:00:a0:41:92'/>
      <source bridge='br0'/>
      <model type='virtio'/>   <-- add this line, leave the rest
    </interface>

Or, if you're using KVM on the command line, add the options:

-net nic,model=virtio -net user

This improves the network performances by a lot (factor 10, nearly). But this works only with Ubuntu Hardy or Intrepid guests for the moment, which is why it is not by default. Note that this also corrects the issue some are reporting with their network connections going away after a period of time or data transfer. <<Include(KVM/Header)>>