1
# Copyright 2014 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Code to generate `udev` rules."""
6
from __future__ import (
16
'compose_network_interfaces_udev_rules',
19
from textwrap import dedent
22
def compose_udev_equality(key, value):
23
"""Return a udev comparison clause, like `ACTION=="add"`."""
24
assert key == key.upper()
25
return '%s=="%s"' % (key, value)
28
def compose_udev_attr_equality(attribute, value):
29
"""Return a udev attribute comparison clause, like `ATTR{type}=="1"`."""
30
assert attribute == attribute.lower()
31
return 'ATTR{%s}=="%s"' % (attribute, value)
34
def compose_udev_setting(key, value):
35
"""Return a udev assignment clause, like `NAME="eth0"`."""
36
assert key == key.upper()
37
return '%s="%s"' % (key, value)
40
def compose_udev_rule(interface, mac):
41
"""Return a udev rule to set the name of network interface with `mac`.
43
The rule ends up as a single line looking something like:
45
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
46
ATTR{address}="ff:ee:dd:cc:bb:aa", NAME="eth0"
48
(Note the difference between `=` and `==`: they both occur.)
51
compose_udev_equality('SUBSYSTEM', 'net'),
52
compose_udev_equality('ACTION', 'add'),
53
compose_udev_equality('DRIVERS', '?*'),
54
compose_udev_attr_equality('address', mac),
55
compose_udev_setting('NAME', interface),
60
def compose_network_interfaces_udev_rules(interfaces):
61
"""Return text for a udev persistent-net rules file.
63
These rules assign fixed names to network interfaces. They ensure that
64
the same network interface cards come up with the same interface names on
65
every boot. Otherwise, the kernel may assign interface names in different
66
orders on every boot, and so network interfaces can "switch identities"
67
every other time the machine reboots.
69
:param interfaces: List of tuples of interface name and MAC address.
70
:return: Text to write into a udev rules file.
73
compose_udev_rule(interface, mac)
74
for interface, mac in interfaces
77
# MAAS-assigned network interface names.
79
# End of MAAS-assigned network interface names.
80
""") % '\n\n'.join(rules)