1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright (C) 2015 Red Hat, Inc.
#
#
# This example configures a Bond from ethernet devices and activates it
#
# NetworkManager D-Bus API:
# https://networkmanager.dev/docs/api/latest/spec.html
#
import dbus, sys, uuid
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
DBusGMainLoop(set_as_default=True)
def add_connection(con):
bus = dbus.SystemBus()
proxy = bus.get_object(
"org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings"
)
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
return settings.AddConnection(con)
def create_bond(bond_name):
bond_opts = dbus.Dictionary({"mode": "4"})
s_bond = dbus.Dictionary({"options": bond_opts})
s_con = dbus.Dictionary(
{
"type": "bond",
"uuid": str(uuid.uuid4()),
"id": bond_name,
"interface-name": bond_name,
"autoconnect": False,
"autoconnect-slaves": 1,
}
)
s_ip4 = dbus.Dictionary({"method": "disabled"})
s_ip6 = dbus.Dictionary({"method": "disabled"})
con = dbus.Dictionary(
{"bond": s_bond, "connection": s_con, "ipv4": s_ip4, "ipv6": s_ip6}
)
print("Creating bond connection: %s" % bond_name)
return add_connection(con)
def create_slave(device, master):
slave_name = "bond-" + master + "-slave-" + device
s_wired = dbus.Dictionary({"duplex": "full"})
s_con = dbus.Dictionary(
{
"type": "802-3-ethernet",
"uuid": str(uuid.uuid4()),
"id": slave_name,
"interface-name": device,
"autoconnect": False,
"master": master,
"slave-type": "bond",
}
)
con = dbus.Dictionary({"802-3-ethernet": s_wired, "connection": s_con})
print("Creating slave connection: %s" % slave_name)
add_connection(con)
def usage():
print("Usage: %s <bond_name> <ifname1> ..." % sys.argv[0])
sys.exit(0)
if len(sys.argv) < 3:
usage()
# Create bond master and slave connections
bond_name = sys.argv[1]
bond_path = create_bond(bond_name)
for ifname in sys.argv[2:]:
create_slave(ifname, bond_name)
# Activate the bond
bus = dbus.SystemBus()
proxy = bus.get_object(
"org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager"
)
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
ac = manager.ActivateConnection(bond_path, "/", "/")
print("Activating bond: %s (%s)" % (bond_name, ac))
# Monitor the active bond connection
loop = GLib.MainLoop()
def properties_changed(interface_name, changed_properties, invalidated_properties):
if (
interface_name == "org.freedesktop.NetworkManager.Connection.Active"
and "State" in changed_properties
):
state = changed_properties["State"]
if state == 2:
print("Successfully connected")
loop.quit()
if state == 3 or state == 4:
print("Bond activation failed")
loop.quit()
obj = bus.get_object("org.freedesktop.NetworkManager", ac)
iface = dbus.Interface(obj, "org.freedesktop.DBus.Properties")
iface.connect_to_signal("PropertiesChanged", properties_changed)
loop.run()
|