Change in vdsm[master]: VDSM hook for random network allocation during VM power on

ibarkan at redhat.com ibarkan at redhat.com
Tue Oct 14 07:55:05 UTC 2014


Ido Barkan has uploaded a new change for review.

Change subject: VDSM hook for random network allocation during VM power on
......................................................................

VDSM hook for random network allocation during VM
power on

The hook uses CustomDeciveProperties and a property
called 'equivnets' which is a comma separated list
of network names. When the VM is started, a random
network is allocated to it.

Change-Id: Ib14c76994918750e7eef460411acc337599b0186
Bug-Url: https://bugzilla.redhat.com/1065405
Signed-off-by: ibarkan <ibarkan at redhat.com>
---
M configure.ac
M vdsm.spec.in
M vdsm_hooks/Makefile.am
A vdsm_hooks/allocate_net/Makefile.am
A vdsm_hooks/allocate_net/README
A vdsm_hooks/allocate_net/before_device_create.py
6 files changed, 182 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/14/34114/1

diff --git a/configure.ac b/configure.ac
index 584b165..978493d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -340,6 +340,7 @@
 	vdsm/storage/vdsm-lvm.rules
 	vdsm/virt/Makefile
 	vdsm_hooks/Makefile
+	vdsm_hooks/allocate_net/Makefile
 	vdsm_hooks/checkimages/Makefile
 	vdsm_hooks/directlun/Makefile
 	vdsm_hooks/ethtool_options/Makefile
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 8fc4593..9b7b028 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -385,6 +385,14 @@
 %description tests
 A test suite for verifying the functionality of a running vdsm instance
 
+%package hook-allocate_net
+Summary:        random_network allocation hook for VDSM
+BuildArch:      noarch
+Requires:       %{name}
+
+%description hook-allocate_net
+VDSM hook used to allocate networks for vms in a random fashion
+
 %package hook-checkimages
 Summary:        Qcow2 disk image format check hook for VDSM
 BuildArch:      noarch
@@ -1352,6 +1360,10 @@
 %{_libexecdir}/%{vdsm_name}/hooks/after_network_setup/30_ethtool_options
 
 %if 0%{?with_hooks}
+%files hook-allocate_net
+%defattr(-, root, root, -)
+%{_libexecdir}/%{vdsm_name}/hooks/before_device_create/10_allocate_net
+
 %files hook-checkimages
 %defattr(-, root, root, -)
 %{_libexecdir}/%{vdsm_name}/hooks/before_vm_start/60_checkimages
diff --git a/vdsm_hooks/Makefile.am b/vdsm_hooks/Makefile.am
index c68076e..cf3ac75 100644
--- a/vdsm_hooks/Makefile.am
+++ b/vdsm_hooks/Makefile.am
@@ -25,6 +25,7 @@
 # Additional hooks
 if HOOKS
 SUBDIRS += \
+	allocate_net \
 	checkimages \
 	directlun \
 	extnet \
diff --git a/vdsm_hooks/allocate_net/Makefile.am b/vdsm_hooks/allocate_net/Makefile.am
new file mode 100644
index 0000000..d3f3446
--- /dev/null
+++ b/vdsm_hooks/allocate_net/Makefile.am
@@ -0,0 +1,31 @@
+#
+# Copyright 2013 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+EXTRA_DIST = \
+	before_device_create.py
+
+install-data-local:
+	$(MKDIR_P) $(DESTDIR)$(vdsmhooksdir)/before_device_create
+	$(INSTALL_SCRIPT) $(srcdir)/before_device_create.py \
+		$(DESTDIR)$(vdsmhooksdir)/before_device_create/10_allocate_net
+
+uninstall-local:
+	$(RM) $(DESTDIR)$(vdsmhooksdir)/before_device_create/10_allocate_net
+
diff --git a/vdsm_hooks/allocate_net/README b/vdsm_hooks/allocate_net/README
new file mode 100644
index 0000000..575dc17
--- /dev/null
+++ b/vdsm_hooks/allocate_net/README
@@ -0,0 +1,27 @@
+allocate_net vdsm hook
+=====================
+This hook allocates a random network to a VM when the VM is started.
+
+By defining a large enough pool of networks, the VMs will be roughly evenly
+distributed between the networks.
+
+Installation:
+* Use engine-config to set the appropriate custom properties as such:
+
+Custom property for a specific vNIC profile:
+    sudo engine-config -s CustomDeviceProperties="{type=interface;prop={equivnets=(.+)(,\s*.+)*}}"
+    sudo service ovirt-engine restart
+
+* Verify that the custom properties were added properly:
+	sudo engine-config -g CustomDeviceProperties
+
+Usage:
+Create a 'meta network'. Add to it a vNic profile that contains the extra
+property named 'equivnets' (as defined above) and input a comma separated list
+of network names as the network pool.
+
+If a VM is configured to use this network profile, it will be connected to a
+random network from the defined pool once powered on.
+
+# note that all the networks defined in the comma separated list should be
+configured on all the hosts on which VMs are planned run.
\ No newline at end of file
diff --git a/vdsm_hooks/allocate_net/before_device_create.py b/vdsm_hooks/allocate_net/before_device_create.py
new file mode 100644
index 0000000..5f7d4aa
--- /dev/null
+++ b/vdsm_hooks/allocate_net/before_device_create.py
@@ -0,0 +1,110 @@
+#!/usr/bin/python
+#
+# Copyright 2011 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+import os
+import random
+
+from xml.dom import minidom
+import sys
+import traceback
+
+import hooking
+AVAIL_NETS_KEY = 'equivnets'
+HELP_ARG = "-h"
+TEST_ARG = "-t"
+HELP_TEXT = """usage: %(prog)s [%(help)s] [%(test)s]
+
+vm network allocation Hook.
+
+optional arguments:
+  %(help)s  show this help message and exit
+  %(test)s  run a dry test for the hook
+""" % {'help': HELP_ARG,
+       'test': TEST_ARG,
+       'prog': sys.argv[0]}
+
+
+def _parse_available_nets():
+    return [net.strip()
+            for net in os.environ[AVAIL_NETS_KEY].split(',') if net]
+
+
+def _get_random_network():
+    available_nets = _parse_available_nets()
+    try:
+        net = random.choice(available_nets)
+    except IndexError:
+        raise Exception('Could no available networks to choose from')
+    return net
+
+
+def _change_assigned_network(interface, net):
+    for source in interface.getElementsByTagName('source'):
+        source.attributes.item(0).value = net
+
+
+def allocate_random_network(interface):
+    net = _get_random_network()
+    _change_assigned_network(interface, net)
+    hooking.log('allocating random network: %s' % (net,))
+
+
+def test():
+    os.environ[AVAIL_NETS_KEY] = 'special_net'
+
+    interface = minidom.parseString("""
+    <interface type="bridge">
+        <address bus="0x00" domain="0x0000" function="0x0" slot="0x03"\
+                                            type="pci"/>
+        <mac address="00:1a:4a:16:01:b0"/>
+        <model type="virtio"/>
+        <source bridge="ovirtmgmt"/>
+        <filterref filter="vdsm-no-mac-spoofing"/>
+        <link state="up"/>
+        <boot order="1"/>
+    </interface>
+    """).getElementsByTagName('interface')[0]
+
+    print "Interface before removing filter: %s" % \
+        interface.toxml(encoding='UTF-8')
+
+    allocate_random_network(interface)
+    print "Interface after removing filter: %s" % \
+        interface.toxml(encoding='UTF-8')
+
+
+def main():
+    device_xml = hooking.read_domxml()
+    allocate_random_network(device_xml)
+    hooking.write_domxml(device_xml)
+
+
+if __name__ == '__main__':
+    if HELP_ARG in sys.argv:
+        hooking.exit_hook(HELP_TEXT)
+
+    try:
+        if TEST_ARG in sys.argv:
+            test()
+        else:
+            main()
+    except:
+        hooking.exit_hook('vm net allocation hook: [unexpected error]: %s\n' %
+                          traceback.format_exc())
\ No newline at end of file


-- 
To view, visit http://gerrit.ovirt.org/34114
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib14c76994918750e7eef460411acc337599b0186
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ido Barkan <ibarkan at redhat.com>


More information about the vdsm-patches mailing list