libvirt XML 学习笔记-网络配置部分
Addressing
The final set of elements define the addresses (IPv4 and/or IPv6, as well as MAC) to be assigned to the bridge device associated with the virtual network, and optionally enable DHCP services. These elements are only valid for isolated networks (no forward
element specified), and for those with a forward mode of 'route' or 'nat'.
... <mac address='00:16:3E:5D:C7:9E'/> <domain name="example.com"/> <dns> <txt name="example" value="example value" /> <srv service='name' protocol='tcp' domain='test-domain-name' target='.' port='1024' priority='10' weight='10'/> <host ip='192.168.122.2'> <hostname>myhost</hostname> <hostname>myhostalias</hostname> </dns> <ip address="192.168.122.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.122.100" end="192.168.122.254" /> <host mac="00:16:3e:77:e2:ed" name="foo.example.com" ip="192.168.122.10" /> <host mac="00:16:3e:3e:a9:1a" name="bar.example.com" ip="192.168.122.11" /> </dhcp> </ip> <ip family="ipv6" address="2001:db8:ca2:2::1" prefix="64" /> </network>
mac
- The
address
attribute defines a MAC (hardware) address formatted as 6 groups of 2-digit hexadecimal numbers, the groups separated by colons (eg,"52:54:00:1C:DA:2F"
). This MAC address is assigned to the bridge device when it is created. Generally it is best to not specify a MAC address when creating a network - in this case, if a defined MAC address is needed for proper operation, libvirt will automatically generate a random MAC address and save it in the config. Allowing libvirt to generate the MAC address will assure that it is compatible with the idiosyncrasies of the platform where libvirt is running. Since 0.8.8 dns
- The dns element of a network contains configuration information for the virtual network's DNS server. Since 0.9.3Currently supported elements are:
txt
- A
dns
element can have 0 or moretxt
elements. Each txt element defines a DNS TXT record and has two attributes, both required: a name that can be queried via dns, and a value that will be returned when that name is queried. names cannot contain embedded spaces or commas. value is a single string that can contain multiple values separated by commas. Since 0.9.3 host
- The
host
element withindns
is the definition of DNS hosts to be passed to the DNS service. The IP address is identified by theip
attribute and the names for that IP address are identified in thehostname
sub-elements of thehost
element. Since 0.9.3
srv
- The
dns
element can have also 0 or moresrv
record elements. Eachsrv
record element defines a DNS SRV record and has 2 mandatory and 5 optional attributes. The mandatory attributes are service name and protocol (tcp, udp) and the optional attributes are target, port, priority, weight and domain as defined in DNS server SRV RFC (RFC 2782). Since 0.9.9
ip
- The
address
attribute defines an IPv4 address in dotted-decimal format, or an IPv6 address in standard colon-separated hexadecimal format, that will be configured on the bridge device associated with the virtual network. To the guests this address will be their default route. For IPv4 addresses, thenetmask
attribute defines the significant bits of the network address, again specified in dotted-decimal format. For IPv6 addresses, and as an alternate method for IPv4 addresses, you can specify the significant bits of the network address with theprefix
attribute, which is an integer (for example,netmask='255.255.255.0'
could also be given asprefix='24'
. Thefamily
attribute is used to specify the type of address - 'ipv4' or 'ipv6'; if nofamily
is given, 'ipv4' is assumed. A network can have more than one of each family of address defined, but only a single address can have adhcp
ortftp
element. Since 0.3.0; IPv6, multiple addresses on a single network,family
, andprefix
since 0.8.7tftp
- Immediately within the
ip
element there is an optionaltftp
element. The presence of this element and of its attributeroot
enables TFTP services. The attribute specifies the path to the root directory served via TFTP.tftp
is not supported for IPv6 addresses, and can only be specified on a single IPv4 address per network. Since 0.7.1 dhcp
- Also within the
ip
element there is an optionaldhcp
element. The presence of this element enables DHCP services on the virtual network. It will further contain one or morerange
elements. Thedhcp
element is not supported for IPv6, and is only supported on a single IP address per network for IPv4. Since 0.3.0range
- The
start
andend
attributes on therange
element specify the boundaries of a pool of IPv4 addresses to be provided to DHCP clients. These two addresses must lie within the scope of the network defined on the parentip
element. Since 0.3.0 host
- Within the
dhcp
element there may be zero or morehost
elements; these specify hosts which will be given names and predefined IP addresses by the built-in DHCP server. Any such element must specify the MAC address of the host to be assigned a given name (via themac
attribute), the IP to be assigned to that host (via theip
attribute), and the name to be given that host by the DHCP server (via thename
attribute). Since 0.4.5 bootp
- The optional
bootp
element specifies BOOTP options to be provided by the DHCP server. Two attributes are supported:file
is mandatory and gives the file to be used for the boot image;server
is optional and gives the address of the TFTP server from which the boot image will be fetched.server
defaults to the same host that runs the DHCP server, as is the case when thetftp
element is used. The BOOTP options currently have to be the same for all address ranges and statically assigned addresses.Since 0.7.1 (server
since 0.7.3).
Example configuration
NAT based network
This example is the so called "default" virtual network. It is provided and enabled out-of-the-box for all libvirt installations. This is a configuration that allows guest OS to get outbound connectivity regardless of whether the host uses ethernet, wireless, dialup, or VPN networking without requiring any specific admin configuration. In the absence of host networking, it at least allows guests to talk directly to each other.
<network> <name>default</name> <bridge name="virbr0" /> <forward mode="nat"/> <ip address="192.168.122.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.122.2" end="192.168.122.254" /> </dhcp> </ip> <ip family="ipv6" address="2001:db8:ca2:2::1" prefix="64" /> </network>
Routed network config
This is a variant on the default network which routes traffic from the virtual network to the LAN without applying any NAT. It requires that the IP address range be pre-configured in the routing tables of the router on the host network. This example further specifies that guest traffic may only go out via the eth1
host network device.
<network> <name>local</name> <bridge name="virbr1" /> <forward mode="route" dev="eth1"/> <ip address="192.168.122.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.122.2" end="192.168.122.254" /> </dhcp> </ip> <ip family="ipv6" address="2001:db8:ca2:2::1" prefix="64" /> </network>
Isolated network config
This variant provides a completely isolated private network for guests. The guests can talk to each other, and the host OS, but cannot reach any other machines on the LAN, due to the omission of the forward
element in the XML description.
<network> <name>private</name> <bridge name="virbr2" /> <ip address="192.168.152.1" netmask="255.255.255.0"> <dhcp> <range start="192.168.152.2" end="192.168.152.254" /> </dhcp> </ip> <ip family="ipv6" address="2001:db8:ca2:3::1" prefix="64" /> </network>
Using an existing host bridge
Since 0.9.4 This shows how to use a pre-existing host bridge "br0". The guests will effectively be directly connected to the physical network (i.e. their IP addresses will all be on the subnet of the physical network, and there will be no restrictions on inbound or outbound connections).
<network> <name>host-bridge</name> <forward mode="bridge"/> <bridge name="br0"/> </network>
Using a macvtap "direct" connection
Since 0.9.4, QEMU and KVM only, requires Linux kernel 2.6.34 or newer This shows how to use macvtap to connect to the physical network directly through one of a group of physical devices (without using a host bridge device). As with the host bridge network, the guests will effectively be directly connected to the physical network so their IP addresses will all be on the subnet of the physical network, and there will be no restrictions on inbound or outbound connections. Note that, due to a limitation in the implementation of macvtap, these connections do not allow communication directly between the host and the guests - if you require this you will either need the attached physical switch to be operating in a mirroring mode (so that all traffic coming to the switch is reflected back to the host's interface), or provide alternate means for this communication (e.g. a second interface on each guest that is connected to an isolated network). The other forward modes that use macvtap (private, vepa, and passthrough) would be used in a similar fashion.
<network> <name>direct-macvtap</name> <forward mode="bridge"> <interface dev="eth20"/> <interface dev="eth21"/> <interface dev="eth22"/> <interface dev="eth23"/> <interface dev="eth24"/> </forward> </network>