[Libguestfs] [PATCH 2/3] lib: extract NETWORK_ADDRESS and NETWORK_PREFIX as macros

Laszlo Ersek lersek at redhat.com
Thu Dec 23 10:37:00 UTC 2021


The 169.254.0.0/16 network specification (for the appliance) is currently
duplicated between the direct backend and the libvirt backend. In a
subsequent patch, we're going to need the network specification in yet
another spot; extract it now to the NETWORK_ADDRESS and NETWORK_PREFIX
macros (simply as strings).

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2034160
Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---

Notes:
    v1:
    
    - Regression-tested with "virt-rescue --network" and "ping", using the
      default "direct" back-end, after setting "net.ipv4.ping_group_range"
      properly in sysctl on the host.
    
    - The direct back-end continues to generate the following QEMU option:
    
        -netdev user,id=usernet,net=169.254.0.0/16
    
    - Did the same with the libvirt backend. The generated libvirt XML,
      per "virsh dumpxml", contains
    
        <qemu:commandline>
          <qemu:arg value='-netdev'/>
          <qemu:arg value='user,id=usernet,net=169.254.0.0/16'/>
          <qemu:arg value='-device'/>
          <qemu:arg value='virtio-net-pci,netdev=usernet,addr=1e.0'/>
          <qemu:env name='TMPDIR' value='/home/lacos/src/v2v/guestfs-tools/tmp'/>
        </qemu:commandline>
    
      and the consequent QEMU cmdline has
    
        -netdev user,id=usernet,net=169.254.0.0/16 \
        -device virtio-net-pci,netdev=usernet,addr=1e.0 \

 lib/guestfs-internal.h | 6 ++++++
 lib/launch-direct.c    | 2 +-
 lib/launch-libvirt.c   | 3 ++-
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h
index 8eb2dd3adddb..e24d570f5aa7 100644
--- a/lib/guestfs-internal.h
+++ b/lib/guestfs-internal.h
@@ -183,6 +183,12 @@ cleanup_mutex_unlock (pthread_mutex_t **ptr)
 #endif
 #endif
 
+/* Network address and network mask (expressed as address prefix) that the
+ * appliance will see (if networking is enabled).
+ */
+#define NETWORK_ADDRESS "169.254.0.0"
+#define NETWORK_PREFIX  "16"
+
 /* Guestfs handle and associated structures. */
 
 /* State. */
diff --git a/lib/launch-direct.c b/lib/launch-direct.c
index e5b9a561176e..4f038f4f04d5 100644
--- a/lib/launch-direct.c
+++ b/lib/launch-direct.c
@@ -689,7 +689,7 @@ launch_direct (guestfs_h *g, void *datav, const char *arg)
     start_list ("-netdev") {
       append_list ("user");
       append_list ("id=usernet");
-      append_list ("net=169.254.0.0/16");
+      append_list ("net=" NETWORK_ADDRESS "/" NETWORK_PREFIX);
     } end_list ();
     start_list ("-device") {
       append_list (VIRTIO_DEVICE_NAME ("virtio-net"));
diff --git a/lib/launch-libvirt.c b/lib/launch-libvirt.c
index 9e833693810f..266d88824686 100644
--- a/lib/launch-libvirt.c
+++ b/lib/launch-libvirt.c
@@ -1843,7 +1843,8 @@ construct_libvirt_xml_qemu_cmdline (guestfs_h *g,
       } end_element ();
 
       start_element ("qemu:arg") {
-        attribute ("value", "user,id=usernet,net=169.254.0.0/16");
+        attribute ("value",
+                   "user,id=usernet,net=" NETWORK_ADDRESS "/" NETWORK_PREFIX);
       } end_element ();
 
       start_element ("qemu:arg") {
-- 
2.19.1.3.g30247aa5d201




More information about the Libguestfs mailing list