© W. David Ashley 2019
W. David AshleyFoundations of Libvirt Development https://doi.org/10.1007/978-1-4842-4862-1_6

6. Virtual Networks

W. David Ashley1 
(1)
Austin, TX, USA
 
A virtual network provides a method for connecting the network devices of one or more guest domains within a single host. The virtual network can do either of the following:
  • Remain isolated to the host.

  • Allow routing of traffic off-node via the active network interfaces of the host OS. This includes the option to apply NAT to IPv4 traffic.

A virtual network is represented by an instance of the virNetwork class and has two unique identifiers.
  • Name: This is a short string, unique among all the virtual networks on a single host, both running and inactive. For maximum portability between hypervisors, applications should use only alphanumeric (aZ, 09), hyphen (-), and underscore (_) characters in names.

  • UUID: This consists of 16 unsigned bytes, guaranteed to be unique among all the virtual networks on any host. RFC 4122 defines the format for UUIDs and provides a recommended algorithm for generating UUIDs with guaranteed uniqueness.

A virtual network can be transient or persistent. A transient virtual network can be managed only while it is running on the host. When taken offline, all traces of it will disappear. A persistent virtual network has its configuration maintained in a data store on the host, in an implementation-defined format. Thus, when a persistent network is brought offline, it is still possible to manage its inactive configuration. A transient network can be turned into a persistent network on the fly by defining a configuration for it.

After the installation of libvirt, every host will get a single virtual network instance called default, which provides DHCP services to guests and allows NAT’d IP connectivity to the host’s interfaces. This service is of most use to hosts with intermittent network connectivity such as laptops using wireless networking.

Bridged networking is also supported. This allows a virtualized client to share the host’s network adapter directly and thus exist on the host’s real network. There are two ways to create this type of network. The old way is to set up a bridged network on the host. The second way is a routed network, which is beyond the scope of this book.

Recently another network model has been added to libvirt, known as passthrough networking. This method allows the virtualized client to make itself visible to the outside world. This method is also beyond the scope of this book.

Listing Networks

Virtual networks are discovered using the class virConnect methods networkLookupByName, networkLookupByUUID, networkLookupByUUIDString, and listNetworks. Listing 6-1 shows how to use these methods.
# Example-1.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.open('qemu:///system')
if conn == None:
    print('Failed to open connection to qemu:///system',
          file=sys.stderr)
    exit(1)
# discover all the virtual networks
networks = conn.listNetworks()
print('Virtual networks:')
for network in networks:
    print('  '+network)
print()
# lookup the default network by name
network = conn.networkLookupByName('default')
print('Virtual network default:')
print('  name: '+network.name())
uuid = network.UUIDString()
print('  UUID: '+uuid)
print('  bridge: '+network.bridgeName())
print()
# lookup the default network by name
network = conn.networkLookupByUUIDString(uuid)
print('Virtual network default:')
print('  name: '+network.name())
print('  UUID: '+network.UUIDString())
print('  bridge: '+network.bridgeName())
conn.close()
exit(0)
Listing 6-1

Discovering and Finding Virtual Networks

Lifecycle Control

Listing 6-2 shows how to use the networkCreateXML, networkDefineXML, and destroy methods .
# Example-2.py
from __future__ import print_function
import sys
import libvirt
xml = """
<network>
  <name>mynetwork</name>
  <bridge name="virbr1" />
  <forward mode="nat"/>
  <ip address="192.168.142.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.142.2" end="192.168.142.254" />
    </dhcp>
  </ip>
</network>"""
conn = libvirt.open('qemu:///system')
if conn == None:
    print('Failed to open connection to qemu:///system',
          file=sys.stderr)
    exit(1)
# create a persistent virtual network
network = conn.networkCreateXML(xml)
if network == None:
    print('Failed to create a virtual network', file=sys.stderr)
    exit(1)
active = network.isActive()
if active == 1:
    print('The new persistent virtual network is active')
else:
    print('The new persistent virtual network is not active')
# now destroy the persistent virtual network
network.destroy()
print()
# create a transient virtual network
network = conn.networkDefineXML(xml)
if network == None:
    print('Failed to define a virtual network', file=sys.stderr)
    exit(1)
active = network.isActive()
if active == 1:
    print('The new transient virtual network is active')
else:
    print('The new transient virtual network is not active')
network.create() # set the network active
active = network.isActive()
if active == 1:
    print('The new transient virtual network is active')
else:
    print('The new transient virtual network is not active')
# now destroy the transient virtual network
network.destroy()
conn.close()
exit(0)
Listing 6-2

Creating and Destroying Virtual Networks

Network Configuration

Listing 6-3 shows how to use the XMLDesc, autostart, isActive, isPersistent, and setAutostart methods.
# Example-1.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.open('qemu:///system')
if conn == None:
    print('Failed to open connection to qemu:///system',
          file=sys.stderr)
    exit(1)
# lookup the default network by name
network = conn.networkLookupByName('default')
print('Virtual network default:')
print('  name: '+network.name())
print('  UUID: '+network.UUIDString())
print('  bridge: '+network.bridgeName())
print('  autostart: '+str(network.autostart()))
print('  is active: '+str(network.isActive()))
print('  is persistent: '+str(network.isPersistent()))
print()
print('Unsetting autostart')
network.setAutostart(0)
print('  autostart: '+str(network.autostart()))
print('Setting autostart')
network.setAutostart(1)
print('  autostart: '+str(network.autostart()))
print()
xml = network.XMLDesc(0)
print('XML description:')
print(xml)
conn.close()
exit(0)
Listing 6-3

Configuring Virtual Networks

Summary

In this chapter, you learned how to use the basic libvirt networking functions. You saw how to create and destroy networks as well as configure them. These are the basic libvirt functions for networking, but there are some others I have not covered. This chapter gave the basic set of functions for dealing with networks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.11.98