[libvirt_application_development_guide_using_python] Removed the Event loop integration chapter
by David Ashley
commit 49c0a77a5cf5854b5974d9905a2255e6447a2c11
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Fri Aug 14 13:31:56 2015 -0500
Removed the Event loop integration chapter
en-US/Event_Handling.xml | 2 +-
en-US/Event_Loop_Integration.xml | 15 ---------------
..._Application_Development_Guide_Using_Python.xml | 1 -
3 files changed, 1 insertions(+), 17 deletions(-)
---
diff --git a/en-US/Event_Handling.xml b/en-US/Event_Handling.xml
index 96326ea..103af24 100644
--- a/en-US/Event_Handling.xml
+++ b/en-US/Event_Handling.xml
@@ -4,7 +4,7 @@
%BOOK_ENTITIES;
]>
<chapter id="libvirt_application_development_guide_using_python-Events_and_Timers">
- <title>Event Handling</title>
+ <title>Event and Timer Handling</title>
<para>
The Python libvirt module provides a complete interface for handling both events and timers.
Both event and timer handling are invoked through a function interface as opposed to a
diff --git a/en-US/Libvirt_Application_Development_Guide_Using_Python.xml b/en-US/Libvirt_Application_Development_Guide_Using_Python.xml
index efe3256..609bec1 100644
--- a/en-US/Libvirt_Application_Development_Guide_Using_Python.xml
+++ b/en-US/Libvirt_Application_Development_Guide_Using_Python.xml
@@ -16,7 +16,6 @@
<xi:include href="Host_Devices.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Error_Handling.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Event_Handling.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
- <xi:include href="Event_Loop_Integration.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Security_Model.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Debugging.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Revision_History.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
8 years, 8 months
[libvirt_application_development_guide_using_python] Events chapter - started work on the events chapter - added example 1
by David Ashley
commit 93ff325ffb5ccd11661301f3b724f291c23cebec
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Fri Aug 14 13:26:59 2015 -0500
Events chapter
- started work on the events chapter
- added example 1
en-US/Event_Handling.xml | 28 +++++++++++-
en-US/extras/Events-Example-1.py | 89 ++++++++++++++++++++++++++++++++++++++
2 files changed, 115 insertions(+), 2 deletions(-)
---
diff --git a/en-US/Event_Handling.xml b/en-US/Event_Handling.xml
index 52a2085..96326ea 100644
--- a/en-US/Event_Handling.xml
+++ b/en-US/Event_Handling.xml
@@ -3,9 +3,33 @@
<!ENTITY % BOOK_ENTITIES SYSTEM "Libvirt_Application_Development_Guide_Using_Python.ent">
%BOOK_ENTITIES;
]>
-<chapter id="libvirt_application_development_guide_using_python-Event_Handling">
+<chapter id="libvirt_application_development_guide_using_python-Events_and_Timers">
<title>Event Handling</title>
<para>
- TBD
+ The Python libvirt module provides a complete interface for handling both events and timers.
+ Both event and timer handling are invoked through a function interface as opposed to a
+ class/method interface. This makes it easier to integrate the interface into either a graphical
+ or console program.
</para>
+ <section id="libvirt_application_development_guide_using_python-Event_Handling">
+ <title>Event Handling</title>
+ <para>
+ The Python libvirt module supplies a framework for event handling. While this is most useful
+ for graphical programs, it can also be used for console programs to provide a consistent user
+ interface and control the processing of console events.
+ </para>
+ <para>
+ Event handling is done through the functions <literal>virEventAddHandle</literal>,
+ <literal>virEventRegisterDefaultImpl</literal>, <literal>virEventRegisterImpl</literal>,
+ <literal>virEventRemoveHandle</literal>, <literal>virEventRunDefaultImpl</literal>,
+ and <literal>virEventUpdateHandle</literal>.
+ </para>
+ <para>
+ An example program that uses most of these functions follows:
+ </para>
+ <example id="Events-example_1">
+ <title>Provide a persistent console that survives guest reboots</title>
+ <programlisting language="Python"><xi:include href="extras/Events-Example-1.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
+ </example>
+ </section>
</chapter>
diff --git a/en-US/extras/Events-Example-1.py b/en-US/extras/Events-Example-1.py
new file mode 100644
index 0000000..a1fedc0
--- /dev/null
+++ b/en-US/extras/Events-Example-1.py
@@ -0,0 +1,89 @@
+# Example-1.py
+# consolecallback - provide a persistent console that survives guest reboots
+from __future__ import print_function
+
+import sys, os, logging, libvirt, tty, termios, atexit
+
+def reset_term():
+ termios.tcsetattr(0, termios.TCSADRAIN, attrs)
+
+def error_handler(unused, error):
+ # The console stream errors on VM shutdown; we don't care
+ if (error[0] == libvirt.VIR_ERR_RPC and
+ error[1] == libvirt.VIR_FROM_STREAMS):
+ return
+ logging.warn(error)
+
+class Console(object):
+ def __init__(self, uri, uuid):
+ self.uri = uri
+ self.uuid = uuid
+ self.connection = libvirt.open(uri)
+ self.domain = self.connection.lookupByUUIDString(uuid)
+ self.state = self.domain.state(0)
+ self.connection.domainEventRegister(lifecycle_callback, self)
+ self.stream = None
+ self.run_console = True
+ logging.info("%s initial state %d, reason %d",
+ self.uuid, self.state[0], self.state[1])
+
+def check_console(console):
+ if (console.state[0] == libvirt.VIR_DOMAIN_RUNNING or
+ console.state[0] == libvirt.VIR_DOMAIN_PAUSED):
+ if console.stream is None:
+ console.stream = console.connection.newStream(libvirt.VIR_STREAM_NONBLOCK)
+ console.domain.openConsole(None, console.stream, 0)
+ console.stream.eventAddCallback(libvirt.VIR_STREAM_EVENT_READABLE, stream_callback, console)
+ else:
+ if console.stream:
+ console.stream.eventRemoveCallback()
+ console.stream = None
+
+ return console.run_console
+
+def stdin_callback(watch, fd, events, console):
+ readbuf = os.read(fd, 1024)
+ if readbuf.startswith(""):
+ console.run_console = False
+ return
+ if console.stream:
+ console.stream.send(readbuf)
+
+def stream_callback(stream, events, console):
+ try:
+ received_data = console.stream.recv(1024)
+ except:
+ return
+ os.write(0, received_data)
+
+def lifecycle_callback (connection, domain, event, detail, console):
+ console.state = console.domain.state(0)
+ logging.info("%s transitioned to state %d, reason %d",
+ console.uuid, console.state[0], console.state[1])
+
+# main
+if len(sys.argv) != 3:
+ print("Usage:", sys.argv[0], "URI UUID")
+ print("for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'")
+ sys.exit(1)
+
+uri = sys.argv[1]
+uuid = sys.argv[2]
+
+print("Escape character is ^]")
+logging.basicConfig(filename='msg.log', level=logging.DEBUG)
+logging.info("URI: %s", uri)
+logging.info("UUID: %s", uuid)
+
+libvirt.virEventRegisterDefaultImpl()
+libvirt.registerErrorHandler(error_handler, None)
+
+atexit.register(reset_term)
+attrs = termios.tcgetattr(0)
+tty.setraw(0)
+
+console = Console(uri, uuid)
+console.stdin_watch = libvirt.virEventAddHandle(0, libvirt.VIR_EVENT_HANDLE_READABLE, stdin_callback, console)
+
+while check_console(console):
+ libvirt.virEventRunDefaultImpl()
8 years, 8 months
[libvirt_application_development_guide_using_python] All chapters - more proof reading fixups
by David Ashley
commit ba20a3f24025eb5ddc93038ac50083eb226d31e8
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Fri Aug 14 09:33:49 2015 -0500
All chapters
- more proof reading fixups
en-US/Error_Handling.xml | 2 +-
en-US/Storage_Pools.xml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
---
diff --git a/en-US/Error_Handling.xml b/en-US/Error_Handling.xml
index 0601dfc..543e7b3 100644
--- a/en-US/Error_Handling.xml
+++ b/en-US/Error_Handling.xml
@@ -98,7 +98,7 @@
<section id="libvirt_application_development_guide_using_python-Error_Handling-Subclassing_virError">
<title>Subclassing virError</title>
<para>
- It is possible to subclass the <literal>virtError</literal> class to add functionality.\
+ It is possible to subclass the <literal>virtError</literal> class to add functionality.
The default <literal>virError</literal> does not provide any ability to either save the error
information or present the information to the user. Subclassing <literal>virError</literal>
allows the programer the flexibility to add any functionality needed.
diff --git a/en-US/Storage_Pools.xml b/en-US/Storage_Pools.xml
index 52b1936..bcd085c 100644
--- a/en-US/Storage_Pools.xml
+++ b/en-US/Storage_Pools.xml
@@ -85,7 +85,7 @@
<section id="libvirt_application_development_guide_using_python-Storage_Pools-Listingt">
<title>Listing pools</title>
<para>
- Storage pool objects can be obtained using the <literal>listAllStoragePools</literal> method of the
+ A list of storage pool objects can be obtained using the <literal>listAllStoragePools</literal> method of the
<literal>virConnect</literal> class.
</para>
<example>
8 years, 8 months
[libvirt_application_development_guide_using_python] All chapters - proofread fixups
by David Ashley
commit 781f937210b199a2f80efa239d5d025d9ef9772b
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Fri Aug 14 09:25:02 2015 -0500
All chapters
- proofread fixups
en-US/Connections.xml | 14 +++++++-------
en-US/Guest_Domains.xml | 6 +++---
en-US/Introduction.xml | 10 +++++-----
en-US/Network_Interfaces.xml | 2 +-
en-US/Storage_Pools.xml | 2 +-
5 files changed, 17 insertions(+), 17 deletions(-)
---
diff --git a/en-US/Connections.xml b/en-US/Connections.xml
index b08c64c..e613772 100644
--- a/en-US/Connections.xml
+++ b/en-US/Connections.xml
@@ -81,7 +81,7 @@ conn = libvirt.openReadOnly(name)</programlisting>
for read-only access. Such a connection has a restricted set of method
calls that are allowed, and is typically useful for monitoring
applications that should not be allowed to make changes. As with
- <literal>open</literal>, this API has no scope for authentication callbacks, so
+ <literal>open</literal>, this method has no scope for authentication callbacks, so
relies on credentials.
</para>
<example>
@@ -109,7 +109,7 @@ conn = libvirt.openReadOnly(name)</programlisting>
the application to request a read-only connection with the
<parameter>VIR_CONNECT_RO</parameter> flag if desired. A simple example Python program that uses
<literal>openAuth</literal> with username and password credentials follows. As with
- <literal>open</literal>, this API has no scope for authentication callbacks, so
+ <literal>open</literal>, this method has no scope for authentication callbacks, so
relies on credentials.
</para>
<example id="openAuth-Example1">
@@ -317,7 +317,7 @@ driver+unix:///session</screen>
</tgroup>
</table>
<para>
- The following example shows how to local to a QEMU hypervisor using a local URI.
+ The following example shows how to connect to a local QEMU hypervisor using a local URI.
</para>
<example id="Example-Connect_Local">
<title>Connecting to a local QEMU hypervisor</title>
@@ -911,11 +911,11 @@ command -p port [-l username] hostname netcat -U socket
<secondary>getHost</secondary></indexterm>
<para>
The <command>getHostname</command> method can be used to obtain the hostname
- of the virtualization host as returned by gethostname(2). It takes a
- connection pointer in and, if successful, returns a string
+ of the virtualization host as returned by gethostname(2). It invoked via a the
+ virConnection instance and, if successful, returns a string
containing the hostname. If an error occurred, NULL will be returned
instead. It is the responsibility of the caller to free the memory
- returned from this API call. The following code demonstrates the use of
+ returned from this method call. The following code demonstrates the use of
<command>getHostname</command>:
</para>
<example id="Example-ConnectGetHostname">
@@ -1070,7 +1070,7 @@ command -p port [-l username] hostname netcat -U socket
nodes in the system. It takes as input thet starting cell
and the maximum number of cells to retrieve data from.
If successful, aPython <literal>list</literal> is returned with the
- amount of free memory in each node
+ amount of free memory in each node.
On failure <literal>None</literal> is returned. The following code
demonstrates the use of <command>getCellsFreeMemory</command>:
</para>
diff --git a/en-US/Guest_Domains.xml b/en-US/Guest_Domains.xml
index 04942d1..e6ef0e3 100644
--- a/en-US/Guest_Domains.xml
+++ b/en-US/Guest_Domains.xml
@@ -13,7 +13,7 @@
<para>
A domain is an instance of an operating system running on a virtualized machine.
A guest domain can refer to either a running virtual machine or a configuration
- which can be used to launch a virtual machine. The connection object provides APIs
+ which can be used to launch a virtual machine. The connection object provides methods
to enumerate the guest domains, create new guest domains and manage existing domains.
A guest domain is represented with the <literal>virDomainPtr</literal> object and
has a number of unique identifiers:
@@ -688,7 +688,7 @@
<para>
Some period of time later, the saved state file can then be
used to restart the guest where it left of, using the
- virDomainRestore API. The hypervisor driver will return an
+ virDomainRestore method. The hypervisor driver will return an
error if the guest is already running, however, it won't
prevent attempts to restore from the same state file multiple
times. As noted earlier, it is the applications' responsibility
@@ -1220,7 +1220,7 @@
<indexterm><primary>blockPull</primary></indexterm>
<indexterm><primary>Domains</primary>
<secondary>blockPull</secondary></indexterm>
- Libvirt provides a generic Block Job API that can be used to initiate
+ Libvirt provides a generic Block Job methods that can be used to initiate
and manage operations on disks that belong to a domain. Jobs are
started by calling the function associated with the desired operation
(eg. <literal>blockPull</literal>). Once started, all block
diff --git a/en-US/Introduction.xml b/en-US/Introduction.xml
index a1c06b1..d2a3497 100644
--- a/en-US/Introduction.xml
+++ b/en-US/Introduction.xml
@@ -22,18 +22,18 @@
<para>
Libvirt provides a common, generic and stable layer to securely manage
domains on a node. As nodes may be remotely located, libvirt provides
- all APIs required to provision, create, modify, monitor, control, migrate
+ all methods required to provision, create, modify, monitor, control, migrate
and stop the domains, within the limits of hypervisor support for these
operations. Although multiple nodes may be accessed with libvirt
- simultaneously, the APIs are limited to single node operations.
+ simultaneously, the methods are limited to single node operations.
</para>
<para>
Libvirt is designed to work across multiple virtualization environments,
- which means that more common capabilities are provided as APIs. Due to
+ which means that more common capabilities are provided as methods and functions. Due to
this, certain specific capabilities may not be provided. For example,
it does not provide high level virtualization policies or multi-node
- management features such as load balancing. However, API stability
+ management features such as load balancing. However, method stability
ensures that these features can be implemented on top of libvirt. To
maintain this level of stability, libvirt seeks to isolate applications
from the frequent changes expected at the lower level of the virtualization
@@ -44,7 +44,7 @@
Libvirt is intended as a building block for higher level management tools
and applications focusing on virtualization of a single node, with the
only exception being domain migration between multiple node capabilities.
- It provides APIs to enumerate, monitor and use the resources available
+ It provides methods to enumerate, monitor and use the resources available
on the managed node, including CPUs, memory, storage, networking and
Non-Uniform Memory Access (NUMA) partitions. Although a management node
can be located on a separate physical machine to the management program,
diff --git a/en-US/Network_Interfaces.xml b/en-US/Network_Interfaces.xml
index 63001ab..d4b8652 100644
--- a/en-US/Network_Interfaces.xml
+++ b/en-US/Network_Interfaces.xml
@@ -20,7 +20,7 @@
(briefly - enslave a physical interface to the bridge, then create a
tap device for each VM you want to share the interface), as well as
for general host network interface management. In addition to physical
- hardware, this API can also be used to configure bridges, bonded
+ hardware, the methods can also be used to configure bridges, bonded
interfaces, and vlan interfaces.
</para>
<para>
diff --git a/en-US/Storage_Pools.xml b/en-US/Storage_Pools.xml
index 705e2c0..52b1936 100644
--- a/en-US/Storage_Pools.xml
+++ b/en-US/Storage_Pools.xml
@@ -52,7 +52,7 @@
If at this point the value of pools and volumes over traditional system administration tools is unclear,
note that one of the features of libvirt is its remote protocol, so it's possible to manage all aspects
of a virtual machine's lifecycle as well as the configuration of the resources required by the VM. These
- operations can be performed on a remote host entirely within the libvirt API. In other words, a
+ operations can be performed on a remote host entirely within the Python libvirt module. In other words, a
management application using libvirt can enable a user to perform all the required tasks for configuring
the host for a VM: allocating resources, running the VM, shutting it down and deallocating the resources, without requiring shell access or any other control channel.
</para>
8 years, 8 months
[libvirt_application_development_guide_using_python] Error Handling chapter - added example 26 - aded a new section to support example 26
by David Ashley
commit 799ecdda86eddc59943fe10033a128071c965de0
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 13:56:26 2015 -0500
Error Handling chapter
- added example 26
- aded a new section to support example 26
en-US/Error_Handling.xml | 13 +++++++++++++
en-US/extras/Error-Example-26.py | 33 +++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/en-US/Error_Handling.xml b/en-US/Error_Handling.xml
index 79259b3..0601dfc 100644
--- a/en-US/Error_Handling.xml
+++ b/en-US/Error_Handling.xml
@@ -109,4 +109,17 @@
<programlisting language="Python"><xi:include href="extras/Error-Example-25.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
</example>
</section>
+
+ <section id="libvirt_application_development_guide_using_python-Error_Handling-Registering_Error_Handler">
+ <title>Registering an Error Handler Function</title>
+ <para>
+ Libvirt also supports setting up an error handler function. This is done using the libvirt
+ function <literal>registerErrorHandler</literal>.
+ An example of this follows:
+ </para>
+ <example>
+ <title>Subclassing virError</title>
+ <programlisting language="Python"><xi:include href="extras/Error-Example-26.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
+ </example>
+ </section>
</chapter>
diff --git a/en-US/extras/Error-Example-26.py b/en-US/extras/Error-Example-26.py
new file mode 100644
index 0000000..a8bf911
--- /dev/null
+++ b/en-US/extras/Error-Example-26.py
@@ -0,0 +1,33 @@
+# Example-26.py
+from __future__ import print_function
+import sys
+import libvirt
+
+def libvirt_error_handler(ctx, err):
+ print('Error code: '+str(err[0]), file=sys.stderr)
+ print('Error domain: '+str(err[1]), file=sys.stderr)
+ print('Error message: '+err[2], file=sys.stderr)
+ print('Error level: '+str(err[3]), file=sys.stderr)
+ if err[4] != None:
+ print('Error string1: '+err[4], file=sys.stderr)
+ else:
+ print('Error string1:', file=sys.stderr)
+ if err[5] != None:
+ print('Error string2: '+err[5], file=sys.stderr)
+ else:
+ print('Error string2:', file=sys.stderr)
+ if err[6] != None:
+ print('Error string3: '+err[6], file=sys.stderr)
+ else:
+ print('Error string3:', file=sys.stderr)
+ print('Error int1: '+str(err[7]), file=sys.stderr)
+ print('Error int2: '+str(err[8]), file=sys.stderr)
+ exit(1)
+
+ctx = 'just some information'
+libvirt.registerErrorHandler(libvirt_error_handler, ctx)
+
+conn = libvirt.open('qemu:///system') # invalidate the parameter to force an error
+
+conn.close()
+exit(0)
8 years, 8 months
[libvirt_application_development_guide_using_python] Virtual Networks chapter - added example 3 - aaded support text for example 3 - this should finis
by David Ashley
commit 7b043a5f613dcddfcf7b8b3f89802ea78fd8a613
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 13:39:41 2015 -0500
Virtual Networks chapter
- added example 3
- aaded support text for example 3
- this should finish this chapter
en-US/Virtual_Networks.xml | 14 ++++++++++-
en-US/extras/VirtualNetworks-Example-3.py | 35 +++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 2 deletions(-)
---
diff --git a/en-US/Virtual_Networks.xml b/en-US/Virtual_Networks.xml
index 09f610f..217ccec 100644
--- a/en-US/Virtual_Networks.xml
+++ b/en-US/Virtual_Networks.xml
@@ -78,15 +78,25 @@
<title>Lifecycle control</title>
<para>
The following example shows how to use the <literal>networkCreateXML</literal>,
- <literal>networkDefineXML</literal> and the <literal>
+ <literal>networkDefineXML</literal> and the <literal>destroy</literal> methods.
</para>
+ <example>
+ <title>Creating and destroying virtual networks</title>
+ <programlisting language="Python"><xi:include href="extras/VirtualNetworks-Example-2.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
+ </example>
</section>
<section id="libvirt_application_development_guide_using_python-Virtual_Networks-Config">
<title>Network configuration</title>
<para>
- TBD
+ The following example shows how to use the <literal>XMLDesc</literal>, <literal>autostart</literal>,
+ <literal>isActive</literal>, <literal>isPersistent</literal> and the <literal>setAutostart</literal>
+ methods.
</para>
+ <example>
+ <title>Configuring virtual networks</title>
+ <programlisting language="Python"><xi:include href="extras/VirtualNetworks-Example-3.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
+ </example>
</section>
</chapter>
diff --git a/en-US/extras/VirtualNetworks-Example-3.py b/en-US/extras/VirtualNetworks-Example-3.py
new file mode 100644
index 0000000..25fb589
--- /dev/null
+++ b/en-US/extras/VirtualNetworks-Example-3.py
@@ -0,0 +1,35 @@
+# 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)
8 years, 8 months
[libvirt_application_development_guide_using_python] Virtual Networks chapter - added example 2 - added support text for example 2
by David Ashley
commit 8c90ef6dc98f58036bc5283e423c110caffbee29
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 12:10:45 2015 -0500
Virtual Networks chapter
- added example 2
- added support text for example 2
en-US/Virtual_Networks.xml | 3 +-
en-US/extras/VirtualNetworks-Example-2.py | 59 +++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 1 deletions(-)
---
diff --git a/en-US/Virtual_Networks.xml b/en-US/Virtual_Networks.xml
index 7160cb1..09f610f 100644
--- a/en-US/Virtual_Networks.xml
+++ b/en-US/Virtual_Networks.xml
@@ -77,7 +77,8 @@
<section id="libvirt_application_development_guide_using_python-Virtual_Networks-Lifecycle">
<title>Lifecycle control</title>
<para>
- TBD
+ The following example shows how to use the <literal>networkCreateXML</literal>,
+ <literal>networkDefineXML</literal> and the <literal>
</para>
</section>
diff --git a/en-US/extras/VirtualNetworks-Example-2.py b/en-US/extras/VirtualNetworks-Example-2.py
new file mode 100644
index 0000000..326609f
--- /dev/null
+++ b/en-US/extras/VirtualNetworks-Example-2.py
@@ -0,0 +1,59 @@
+# 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)
8 years, 8 months
[libvirt_application_development_guide_using_python] Virtual Networks chapter - added example 1 - added support text for example 1
by David Ashley
commit 05edc1c31a433c513cd0d0961fcf54f96c2d7984
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 10:37:26 2015 -0500
Virtual Networks chapter
- added example 1
- added support text for example 1
en-US/Virtual_Networks.xml | 8 +++++-
en-US/extras/VirtualNetworks-Example-1.py | 35 +++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 1 deletions(-)
---
diff --git a/en-US/Virtual_Networks.xml b/en-US/Virtual_Networks.xml
index 22b6c08..7160cb1 100644
--- a/en-US/Virtual_Networks.xml
+++ b/en-US/Virtual_Networks.xml
@@ -64,8 +64,14 @@
<section id="libvirt_application_development_guide_using_python-Virtual_Networks-Listing">
<title>Listing networks</title>
<para>
- TBD
+ Virtual networks are discovered using the methods <literal>networkLookupByName</literal>,
+ <literal>networkLookupByUUID</literal>, and <literal>networkLookupByUUIDString</literal>
+ and <literal>listNetworks</literal>. The following example shws how to used these methods.
</para>
+ <example>
+ <title>Discovering and finding virtual networks</title>
+ <programlisting language="Python"><xi:include href="extras/VirtualNetworks-Example-1.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting>
+ </example>
</section>
<section id="libvirt_application_development_guide_using_python-Virtual_Networks-Lifecycle">
diff --git a/en-US/extras/VirtualNetworks-Example-1.py b/en-US/extras/VirtualNetworks-Example-1.py
new file mode 100644
index 0000000..ed1ab82
--- /dev/null
+++ b/en-US/extras/VirtualNetworks-Example-1.py
@@ -0,0 +1,35 @@
+# 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)
8 years, 8 months
[libvirt_application_development_guide_using_python] Virtual Networks chapter - moved intro text to the Overview section
by David Ashley
commit 3c077f8b76fcd55f5bf5223aa65158fddbdb2a3a
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 10:16:10 2015 -0500
Virtual Networks chapter
- moved intro text to the Overview section
en-US/Virtual_Networks.xml | 101 +++++++++++++++++++++----------------------
1 files changed, 49 insertions(+), 52 deletions(-)
---
diff --git a/en-US/Virtual_Networks.xml b/en-US/Virtual_Networks.xml
index 862d78f..22b6c08 100644
--- a/en-US/Virtual_Networks.xml
+++ b/en-US/Virtual_Networks.xml
@@ -5,62 +5,59 @@
]>
<chapter id="libvirt_application_development_guide_using_python-Virtual_Networks">
<title>Virtual Networks</title>
- <para>
- 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 either:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- Remain isolated to the host; or
- </para>
- </listitem>
- <listitem>
- <para>
- 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.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- A virtual network is represented by the virNetworkPtr object and has two unique identifiers.
- Unique identifiers
- </para>
- <itemizedlist>
- <listitem>
- <para>
- name: short string, unique amongst all virtual network on a single host, both running and
- inactive. For maximum portability between hypervisors, applications should only use the
- characters a-Z,0-9,-,_ in names.
- </para>
- </listitem>
- <listitem>
- <para>
- UUID: 16 unsigned bytes, guaranteed to be unique amongst all virtual networks on any host.
- RFC 4122 defines the format for UUIDs and provides a recommended algorithm for generating
- UUIDs with guaranteed uniqueness.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- A virtual network may be transient or persistent. A transient virtual network can only be managed
- while it it is running on the host. When taken offline, all trace 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 config. A transient network can be turned into a persistent network on the fly by defining a
- configuration for it.
- </para>
- <para>
- After 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. For example, laptops using
- wireless networking.
- </para>
<section id="libvirt_application_development_guide_using_python-Virtual_Networks-Overview">
<title>Overview</title>
<para>
- TBD
+ 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 either:
+ </para>
+ <itemizedlist>
+ <listitem>
+ <para>
+ Remain isolated to the host; or
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ 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.
+ </para>
+ </listitem>
+ </itemizedlist>
+ <para>
+ A virtual network is represented by the <literal>virNetwork</literal> object and has two unique
+ identifiers.
+ </para>
+ <itemizedlist>
+ <listitem>
+ <para>
+ name: short string, unique amongst all virtual network on a single host, both running and
+ inactive. For maximum portability between hypervisors, applications should only use the
+ characters a-Z,0-9,-,_ in names.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ UUID: 16 unsigned bytes, guaranteed to be unique amongst all virtual networks on any host.
+ RFC 4122 defines the format for UUIDs and provides a recommended algorithm for generating
+ UUIDs with guaranteed uniqueness.
+ </para>
+ </listitem>
+ </itemizedlist>
+ <para>
+ A virtual network may be transient or persistent. A transient virtual network can only be managed
+ while it it is running on the host. When taken offline, all trace 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 config. A transient network can be turned into a persistent network on the fly by defining a
+ configuration for it.
+ </para>
+ <para>
+ After 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. For example, laptops using
+ wireless networking.
</para>
</section>
8 years, 8 months
[libvirt_application_development_guide_using_python] Virtual Network chapter - added introduction text
by David Ashley
commit 08eb7e681671a35f839cbb5dd9579c9331119f93
Author: W. David Ashley <w.david.ashley(a)gmail.com>
Date: Wed Aug 5 09:25:40 2015 -0500
Virtual Network chapter
- added introduction text
en-US/Virtual_Networks.xml | 96 +++++++++++++++++++++++++++++++++-----------
1 files changed, 72 insertions(+), 24 deletions(-)
---
diff --git a/en-US/Virtual_Networks.xml b/en-US/Virtual_Networks.xml
index 8b7ac3c..862d78f 100644
--- a/en-US/Virtual_Networks.xml
+++ b/en-US/Virtual_Networks.xml
@@ -4,37 +4,85 @@
%BOOK_ENTITIES;
]>
<chapter id="libvirt_application_development_guide_using_python-Virtual_Networks">
- <title>Virtual Networks</title>
- <para>
- TBD
- </para>
-
- <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Overview">
- <title>Overview</title>
+ <title>Virtual Networks</title>
<para>
- TBD
+ 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 either:
</para>
- </section>
-
- <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Listing">
- <title>Listing networks</title>
+ <itemizedlist>
+ <listitem>
+ <para>
+ Remain isolated to the host; or
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ 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.
+ </para>
+ </listitem>
+ </itemizedlist>
<para>
- TBD
+ A virtual network is represented by the virNetworkPtr object and has two unique identifiers.
+ Unique identifiers
</para>
- </section>
-
- <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Lifecycle">
- <title>Lifecycle control</title>
+ <itemizedlist>
+ <listitem>
+ <para>
+ name: short string, unique amongst all virtual network on a single host, both running and
+ inactive. For maximum portability between hypervisors, applications should only use the
+ characters a-Z,0-9,-,_ in names.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ UUID: 16 unsigned bytes, guaranteed to be unique amongst all virtual networks on any host.
+ RFC 4122 defines the format for UUIDs and provides a recommended algorithm for generating
+ UUIDs with guaranteed uniqueness.
+ </para>
+ </listitem>
+ </itemizedlist>
<para>
- TBD
+ A virtual network may be transient or persistent. A transient virtual network can only be managed
+ while it it is running on the host. When taken offline, all trace 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 config. A transient network can be turned into a persistent network on the fly by defining a
+ configuration for it.
</para>
- </section>
-
- <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Config">
- <title>Network configuration</title>
<para>
- TBD
+ After 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. For example, laptops using
+ wireless networking.
</para>
- </section>
+
+ <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Overview">
+ <title>Overview</title>
+ <para>
+ TBD
+ </para>
+ </section>
+
+ <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Listing">
+ <title>Listing networks</title>
+ <para>
+ TBD
+ </para>
+ </section>
+
+ <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Lifecycle">
+ <title>Lifecycle control</title>
+ <para>
+ TBD
+ </para>
+ </section>
+
+ <section id="libvirt_application_development_guide_using_python-Virtual_Networks-Config">
+ <title>Network configuration</title>
+ <para>
+ TBD
+ </para>
+ </section>
</chapter>
8 years, 8 months